Delay load call handler
[gc-dialer] / src / dialcentral_qt.py
index b026943..1377bd2 100755 (executable)
@@ -15,7 +15,6 @@ from PyQt4 import QtCore
 
 import constants
 import alarm_handler
-import call_handler
 from util import qtpie
 from util import qwrappers
 from util import qui_utils
@@ -63,86 +62,12 @@ class Dialcentral(qwrappers.ApplicationWrapper):
                except Exception:
                        _moduleLogger.exception("Unknown loading error")
 
-               blobs = "", ""
-               isFullscreen = False
-               isPortrait = qui_utils.screen_orientation() == QtCore.Qt.Vertical
-               tabIndex = 0
-               try:
-                       blobs = [
-                               config.get(constants.__pretty_app_name__, "bin_blob_%i" % i)
-                               for i in xrange(len(self._mainWindow.get_default_credentials()))
-                       ]
-                       isFullscreen = config.getboolean(constants.__pretty_app_name__, "fullscreen")
-                       tabIndex = config.getint(constants.__pretty_app_name__, "tab")
-                       isPortrait = config.getboolean(constants.__pretty_app_name__, "portrait")
-               except ConfigParser.NoOptionError, e:
-                       _moduleLogger.info(
-                               "Settings file %s is missing option %s" % (
-                                       constants._user_settings_,
-                                       e.option,
-                               ),
-                       )
-               except ConfigParser.NoSectionError, e:
-                       _moduleLogger.info(
-                               "Settings file %s is missing section %s" % (
-                                       constants._user_settings_,
-                                       e.section,
-                               ),
-                       )
-               except Exception:
-                       _moduleLogger.exception("Unknown loading error")
-
-               try:
-                       self._alarmHandler.load_settings(config, "alarm")
-                       self.notifyOnMissed = config.getboolean("2 - Account Info", "notifyOnMissed")
-                       self.notifyOnVoicemail = config.getboolean("2 - Account Info", "notifyOnVoicemail")
-                       self.notifyOnSms = config.getboolean("2 - Account Info", "notifyOnSms")
-               except ConfigParser.NoOptionError, e:
-                       _moduleLogger.info(
-                               "Settings file %s is missing option %s" % (
-                                       constants._user_settings_,
-                                       e.option,
-                               ),
-                       )
-               except ConfigParser.NoSectionError, e:
-                       _moduleLogger.info(
-                               "Settings file %s is missing section %s" % (
-                                       constants._user_settings_,
-                                       e.section,
-                               ),
-                       )
-               except Exception:
-                       _moduleLogger.exception("Unknown loading error")
-
-               creds = (
-                       base64.b64decode(blob)
-                       for blob in blobs
-               )
-               self._mainWindow.set_default_credentials(*creds)
-               self._fullscreenAction.setChecked(isFullscreen)
-               self._orientationAction.setChecked(isPortrait)
-               self._mainWindow.set_current_tab(tabIndex)
                self._mainWindow.load_settings(config)
 
        def save_settings(self):
                _moduleLogger.info("Saving settings")
                config = ConfigParser.SafeConfigParser()
 
-               config.add_section(constants.__pretty_app_name__)
-               config.set(constants.__pretty_app_name__, "tab", str(self._mainWindow.get_current_tab()))
-               config.set(constants.__pretty_app_name__, "fullscreen", str(self._fullscreenAction.isChecked()))
-               config.set(constants.__pretty_app_name__, "portrait", str(self._orientationAction.isChecked()))
-               for i, value in enumerate(self._mainWindow.get_default_credentials()):
-                       blob = base64.b64encode(value)
-                       config.set(constants.__pretty_app_name__, "bin_blob_%i" % i, blob)
-
-               config.add_section("alarm")
-               self._alarmHandler.save_settings(config, "alarm")
-               config.add_section("2 - Account Info")
-               config.set("2 - Account Info", "notifyOnMissed", repr(self.notifyOnMissed))
-               config.set("2 - Account Info", "notifyOnVoicemail", repr(self.notifyOnVoicemail))
-               config.set("2 - Account Info", "notifyOnSms", repr(self.notifyOnSms))
-
                self._mainWindow.save_settings(config)
 
                with open(constants._user_settings_, "wb") as configFile:
@@ -343,8 +268,8 @@ class MainWindow(qwrappers.WindowWrapper):
                self._voicemailRefreshDelay.setInterval(30 * 1000)
                self._voicemailRefreshDelay.timeout.connect(self._on_call_missed)
                self._voicemailRefreshDelay.setSingleShot(True)
-               self._callHandler = call_handler.MissedCallWatcher()
-               self._callHandler.callMissed.connect(self._voicemailRefreshDelay.start)
+               self._callHandler = None
+               self._updateVoicemailOnMissedCall = False
 
                self._defaultCredentials = "", ""
                self._curentCredentials = "", ""
@@ -473,6 +398,67 @@ class MainWindow(qwrappers.WindowWrapper):
                self._tabWidget.setCurrentIndex(tabIndex)
 
        def load_settings(self, config):
+               blobs = "", ""
+               isFullscreen = False
+               isPortrait = qui_utils.screen_orientation() == QtCore.Qt.Vertical
+               tabIndex = 0
+               try:
+                       blobs = [
+                               config.get(constants.__pretty_app_name__, "bin_blob_%i" % i)
+                               for i in xrange(len(self.get_default_credentials()))
+                       ]
+                       isFullscreen = config.getboolean(constants.__pretty_app_name__, "fullscreen")
+                       tabIndex = config.getint(constants.__pretty_app_name__, "tab")
+                       isPortrait = config.getboolean(constants.__pretty_app_name__, "portrait")
+               except ConfigParser.NoOptionError, e:
+                       _moduleLogger.info(
+                               "Settings file %s is missing option %s" % (
+                                       constants._user_settings_,
+                                       e.option,
+                               ),
+                       )
+               except ConfigParser.NoSectionError, e:
+                       _moduleLogger.info(
+                               "Settings file %s is missing section %s" % (
+                                       constants._user_settings_,
+                                       e.section,
+                               ),
+                       )
+               except Exception:
+                       _moduleLogger.exception("Unknown loading error")
+
+               try:
+                       self._app.alarmHandler.load_settings(config, "alarm")
+                       self._app.notifyOnMissed = config.getboolean("2 - Account Info", "notifyOnMissed")
+                       self._app.notifyOnVoicemail = config.getboolean("2 - Account Info", "notifyOnVoicemail")
+                       self._app.notifyOnSms = config.getboolean("2 - Account Info", "notifyOnSms")
+                       self._updateVoicemailOnMissedCall = config.getboolean("2 - Account Info", "updateVoicemailOnMissedCall")
+               except ConfigParser.NoOptionError, e:
+                       _moduleLogger.info(
+                               "Settings file %s is missing option %s" % (
+                                       constants._user_settings_,
+                                       e.option,
+                               ),
+                       )
+               except ConfigParser.NoSectionError, e:
+                       _moduleLogger.info(
+                               "Settings file %s is missing section %s" % (
+                                       constants._user_settings_,
+                                       e.section,
+                               ),
+                       )
+               except Exception:
+                       _moduleLogger.exception("Unknown loading error")
+
+               creds = (
+                       base64.b64decode(blob)
+                       for blob in blobs
+               )
+               self.set_default_credentials(*creds)
+               self._app.fullscreenAction.setChecked(isFullscreen)
+               self._app.orientationAction.setChecked(isPortrait)
+               self.set_current_tab(tabIndex)
+
                backendId = 2 # For backwards compatibility
                for tabIndex, tabTitle in enumerate(self._TAB_TITLES):
                        sectionName = "%s - %s" % (backendId, tabTitle)
@@ -503,6 +489,22 @@ class MainWindow(qwrappers.WindowWrapper):
                        self._tabsContents[tabIndex].set_settings(settings)
 
        def save_settings(self, config):
+               config.add_section(constants.__pretty_app_name__)
+               config.set(constants.__pretty_app_name__, "tab", str(self.get_current_tab()))
+               config.set(constants.__pretty_app_name__, "fullscreen", str(self._app.fullscreenAction.isChecked()))
+               config.set(constants.__pretty_app_name__, "portrait", str(self._app.orientationAction.isChecked()))
+               for i, value in enumerate(self.get_default_credentials()):
+                       blob = base64.b64encode(value)
+                       config.set(constants.__pretty_app_name__, "bin_blob_%i" % i, blob)
+
+               config.add_section("alarm")
+               self._app.alarmHandler.save_settings(config, "alarm")
+               config.add_section("2 - Account Info")
+               config.set("2 - Account Info", "notifyOnMissed", repr(self._app.notifyOnMissed))
+               config.set("2 - Account Info", "notifyOnVoicemail", repr(self._app.notifyOnVoicemail))
+               config.set("2 - Account Info", "notifyOnSms", repr(self._app.notifyOnSms))
+               config.set("2 - Account Info", "updateVoicemailOnMissedCall", repr(self._updateVoicemailOnMissedCall))
+
                backendId = 2 # For backwards compatibility
                for tabIndex, tabTitle in enumerate(self._TAB_TITLES):
                        sectionName = "%s - %s" % (backendId, tabTitle)
@@ -543,6 +545,13 @@ class MainWindow(qwrappers.WindowWrapper):
                        import dialogs
                        self._accountDialog = dialogs.AccountDialog(self._app)
                        self._accountDialog.setIfNotificationsSupported(self._app.alarmHandler.backgroundNotificationsSupported)
+
+               if self._callHandler is None or not self._callHandler.isSupported:
+                       self._accountDialog.updateVMOnMissedCall = self._accountDialog.VOICEMAIL_CHECK_NOT_SUPPORTED
+               elif self._updateVoicemailOnMissedCall:
+                       self._accountDialog.updateVMOnMissedCall = self._accountDialog.VOICEMAIL_CHECK_ENABLED
+               else:
+                       self._accountDialog.updateVMOnMissedCall = self._accountDialog.VOICEMAIL_CHECK_DISABLED
                self._accountDialog.notifications = self._app.alarmHandler.alarmType
                self._accountDialog.notificationTime = self._app.alarmHandler.recurrence
                self._accountDialog.notifyOnMissed = self._app.notifyOnMissed
@@ -567,6 +576,14 @@ class MainWindow(qwrappers.WindowWrapper):
                                callbackNumber = self._accountDialog.selectedCallback
                                self._session.set_callback_number(callbackNumber)
 
+                       if self._callHandler is None or self._accountDialog.updateVMOnMissedCall == self._accountDialog.VOICEMAIL_CHECK_DISABLEDD:
+                               pass
+                       elif self._accountDialog.updateVMOnMissedCall == self._accountDialog.VOICEMAIL_CHECK_ENABLED:
+                               self._updateVoicemailOnMissedCall = True
+                               self._callHandler.start()
+                       else:
+                               self._updateVoicemailOnMissedCall = False
+                               self._callHandler.stop()
                        if (
                                self._accountDialog.notifyOnMissed or
                                self._accountDialog.notifyOnVoicemail or
@@ -622,7 +639,12 @@ class MainWindow(qwrappers.WindowWrapper):
                        for tab in self._tabsContents:
                                tab.enable()
                        self._initialize_tab(self._currentTab)
-                       self._callHandler.start()
+                       if self._updateVoicemailOnMissedCall:
+                               if self._callHandler is None:
+                                       import call_handler
+                                       self._callHandler = call_handler.MissedCallWatcher()
+                                       self._callHandler.callMissed.connect(self._voicemailRefreshDelay.start)
+                               self._callHandler.start()
 
        @QtCore.pyqtSlot()
        @misc_utils.log_exception(_moduleLogger)
@@ -630,7 +652,8 @@ class MainWindow(qwrappers.WindowWrapper):
                with qui_utils.notify_error(self._errorLog):
                        for tab in self._tabsContents:
                                tab.disable()
-                       self._callHandler.stop()
+                       if self._callHandler is not None:
+                               self._callHandler.stop()
 
        @QtCore.pyqtSlot()
        @misc_utils.log_exception(_moduleLogger)