summaryrefslogtreecommitdiff
path: root/src/leap/bitmask/services/mail/conductor.py
diff options
context:
space:
mode:
authorTomás Touceda <chiiph@leap.se>2014-05-21 18:14:27 -0300
committerTomás Touceda <chiiph@leap.se>2014-05-21 18:14:27 -0300
commit2ba353fbc87eb81dde2f169b8facdb2104107f70 (patch)
treebff5220bc7e0f89febd053bc8b924b613f00234d /src/leap/bitmask/services/mail/conductor.py
parentfcdd3f8c1c2689798a8ffb8cff7313bc887a2377 (diff)
parenta61889110118d04703b023936048b44517947516 (diff)
Merge remote-tracking branch 'refs/remotes/ivan/feature/refactor-soledad-to-backend' into develop
Diffstat (limited to 'src/leap/bitmask/services/mail/conductor.py')
-rw-r--r--src/leap/bitmask/services/mail/conductor.py135
1 files changed, 39 insertions, 96 deletions
diff --git a/src/leap/bitmask/services/mail/conductor.py b/src/leap/bitmask/services/mail/conductor.py
index 1766a39d..7fc19f1c 100644
--- a/src/leap/bitmask/services/mail/conductor.py
+++ b/src/leap/bitmask/services/mail/conductor.py
@@ -19,15 +19,10 @@ Mail Services Conductor
"""
import logging
-from zope.proxy import sameProxiedObjects
-
+from leap.bitmask.config import flags
from leap.bitmask.gui import statemachines
from leap.bitmask.services.mail import connection as mail_connection
-from leap.bitmask.services.mail import imap
-from leap.bitmask.services.mail.smtpbootstrapper import SMTPBootstrapper
-from leap.bitmask.services.mail.smtpconfig import SMTPConfig
-from leap.common.check import leap_assert
from leap.common.events import events_pb2 as leap_events
from leap.common.events import register as leap_register
@@ -44,9 +39,6 @@ class IMAPControl(object):
Initializes smtp variables.
"""
self.imap_machine = None
- self.imap_service = None
- self.imap_port = None
- self.imap_factory = None
self.imap_connection = None
leap_register(signal=leap_events.IMAP_SERVICE_STARTED,
@@ -55,10 +47,13 @@ class IMAPControl(object):
leap_register(signal=leap_events.IMAP_SERVICE_FAILED_TO_START,
callback=self._handle_imap_events,
reqcbk=lambda req, resp: None)
+ leap_register(signal=leap_events.IMAP_CLIENT_LOGIN,
+ callback=self._handle_imap_events,
+ reqcbk=lambda req, resp: None)
def set_imap_connection(self, imap_connection):
"""
- Sets the imap connection to an initialized connection.
+ Set the imap connection to an initialized connection.
:param imap_connection: an initialized imap connection
:type imap_connection: IMAPConnection instance.
@@ -67,67 +62,18 @@ class IMAPControl(object):
def start_imap_service(self):
"""
- Starts imap service.
+ Start imap service.
"""
- from leap.bitmask.config import flags
-
- logger.debug('Starting imap service')
- leap_assert(sameProxiedObjects(self._soledad, None)
- is not True,
- "We need a non-null soledad for initializing imap service")
- leap_assert(sameProxiedObjects(self._keymanager, None)
- is not True,
- "We need a non-null keymanager for initializing imap "
- "service")
-
- offline = flags.OFFLINE
- self.imap_service, self.imap_port, \
- self.imap_factory = imap.start_imap_service(
- self._soledad,
- self._keymanager,
- userid=self.userid,
- offline=offline)
+ self._backend.imap_start_service(self.userid, flags.OFFLINE)
- if offline is False:
- logger.debug("Starting loop")
- self.imap_service.start_loop()
-
- def stop_imap_service(self, cv):
+ def stop_imap_service(self):
"""
- Stops imap service (fetcher, factory and port).
-
- :param cv: A condition variable to which we can signal when imap
- indeed stops.
- :type cv: threading.Condition
+ Stop imap service.
"""
self.imap_connection.qtsigs.disconnecting_signal.emit()
- # TODO We should homogenize both services.
- if self.imap_service is not None:
- logger.debug('Stopping imap service.')
- # Stop the loop call in the fetcher
- self.imap_service.stop()
- self.imap_service = None
- # Stop listening on the IMAP port
- self.imap_port.stopListening()
- # Stop the protocol
- self.imap_factory.theAccount.closed = True
- self.imap_factory.doStop(cv)
- else:
- # main window does not have to wait because there's no service to
- # be stopped, so we release the condition variable
- cv.acquire()
- cv.notify()
- cv.release()
-
- def fetch_incoming_mail(self):
- """
- Fetches incoming mail.
- """
- if self.imap_service:
- logger.debug('Client connected, fetching mail...')
- self.imap_service.fetch()
-
- # handle events
+ logger.debug('Stopping imap service.')
+
+ self._backend.imap_stop_service()
def _handle_imap_events(self, req):
"""
@@ -137,25 +83,31 @@ class IMAPControl(object):
:type req: leap.common.events.events_pb2.SignalRequest
"""
if req.event == leap_events.IMAP_SERVICE_STARTED:
- self.on_imap_connected()
+ self._on_imap_connected()
elif req.event == leap_events.IMAP_SERVICE_FAILED_TO_START:
- self.on_imap_failed()
+ self._on_imap_failed()
+ elif req.event == leap_events.IMAP_CLIENT_LOGIN:
+ self._on_mail_client_logged_in()
- # emit connection signals
+ def _on_mail_client_logged_in(self):
+ """
+ On mail client logged in, fetch incoming mail.
+ """
+ self._controller.imap_service_fetch()
- def on_imap_connecting(self):
+ def _on_imap_connecting(self):
"""
Callback for IMAP connecting state.
"""
self.imap_connection.qtsigs.connecting_signal.emit()
- def on_imap_connected(self):
+ def _on_imap_connected(self):
"""
Callback for IMAP connected state.
"""
self.imap_connection.qtsigs.connected_signal.emit()
- def on_imap_failed(self):
+ def _on_imap_failed(self):
"""
Callback for IMAP failed state.
"""
@@ -167,12 +119,9 @@ class SMTPControl(object):
"""
Initializes smtp variables.
"""
- self.smtp_config = SMTPConfig()
self.smtp_connection = None
self.smtp_machine = None
- self.smtp_bootstrapper = SMTPBootstrapper()
-
leap_register(signal=leap_events.SMTP_SERVICE_STARTED,
callback=self._handle_smtp_events,
reqcbk=lambda req, resp: None)
@@ -188,29 +137,23 @@ class SMTPControl(object):
"""
self.smtp_connection = smtp_connection
- def start_smtp_service(self, provider_config, download_if_needed=False):
+ def start_smtp_service(self, download_if_needed=False):
"""
Starts the SMTP service.
- :param provider_config: Provider configuration
- :type provider_config: ProviderConfig
:param download_if_needed: True if it should check for mtime
for the file
:type download_if_needed: bool
"""
self.smtp_connection.qtsigs.connecting_signal.emit()
- self.smtp_bootstrapper.start_smtp_service(
- provider_config, self.smtp_config, self._keymanager,
- self.userid, download_if_needed)
+ self._backend.smtp_start_service(self.userid, download_if_needed)
def stop_smtp_service(self):
"""
Stops the SMTP service.
"""
self.smtp_connection.qtsigs.disconnecting_signal.emit()
- self.smtp_bootstrapper.stop_smtp_service()
-
- # handle smtp events
+ self._backend.smtp_stop_service()
def _handle_smtp_events(self, req):
"""
@@ -224,8 +167,6 @@ class SMTPControl(object):
elif req.event == leap_events.SMTP_SERVICE_FAILED_TO_START:
self.on_smtp_failed()
- # emit connection signals
-
def on_smtp_connecting(self):
"""
Callback for SMTP connecting state.
@@ -253,22 +194,17 @@ class MailConductor(IMAPControl, SMTPControl):
"""
# XXX We could consider to use composition instead of inheritance here.
- def __init__(self, soledad, keymanager):
+ def __init__(self, backend):
"""
Initializes the mail conductor.
- :param soledad: a transparent proxy that eventually will point to a
- Soledad Instance.
- :type soledad: zope.proxy.ProxyBase
-
- :param keymanager: a transparent proxy that eventually will point to a
- Keymanager Instance.
- :type keymanager: zope.proxy.ProxyBase
+ :param backend: Backend being used
+ :type backend: Backend
"""
IMAPControl.__init__(self)
SMTPControl.__init__(self)
- self._soledad = soledad
- self._keymanager = keymanager
+
+ self._backend = backend
self._mail_machine = None
self._mail_connection = mail_connection.MailConnection()
@@ -309,6 +245,13 @@ class MailConductor(IMAPControl, SMTPControl):
self._smtp_machine = smtp
self._smtp_machine.start()
+ def stop_mail_services(self):
+ """
+ Stop the IMAP and SMTP services.
+ """
+ self.imap_stop_service()
+ self.smtp_stop_service()
+
def connect_mail_signals(self, widget):
"""
Connects the mail signals to the mail_status widget slots.