Adding orientation to the account dialog
[gc-dialer] / src / dialcentral_qt.py
index d625fb3..99bf92b 100755 (executable)
@@ -10,12 +10,12 @@ import functools
 import logging
 import logging.handlers
 
-from PyQt4 import QtGui
-from PyQt4 import QtCore
+import util.qt_compat as qt_compat
+QtCore = qt_compat.QtCore
+QtGui = qt_compat.import_module("QtGui")
 
 import constants
 import alarm_handler
-import call_handler
 from util import qtpie
 from util import qwrappers
 from util import qui_utils
@@ -127,8 +127,8 @@ class Dialcentral(qwrappers.ApplicationWrapper):
        def _new_main_window(self):
                return MainWindow(None, self)
 
-       @QtCore.pyqtSlot()
-       @QtCore.pyqtSlot(bool)
+       @qt_compat.Slot()
+       @qt_compat.Slot(bool)
        @misc_utils.log_exception(_moduleLogger)
        def _on_about(self, checked = True):
                with qui_utils.notify_error(self._errorLog):
@@ -256,6 +256,7 @@ class MainWindow(qwrappers.WindowWrapper):
        def __init__(self, parent, app):
                qwrappers.WindowWrapper.__init__(self, parent, app)
                self._window.setWindowTitle("%s" % constants.__pretty_app_name__)
+               self._window.resized.connect(self._on_window_resized)
                self._errorLog = self._app.errorLog
 
                self._session = session.Session(self._errorLog, constants._data_path_)
@@ -269,8 +270,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 = "", ""
@@ -350,7 +351,7 @@ class MainWindow(qwrappers.WindowWrapper):
 
                self._initialize_tab(self._tabWidget.currentIndex())
                self.set_fullscreen(self._app.fullscreenAction.isChecked())
-               self.set_orientation(self._app.orientationAction.isChecked())
+               self.update_orientation(self._app.orientation)
 
        def set_default_credentials(self, username, password):
                self._defaultCredentials = username, password
@@ -401,7 +402,7 @@ class MainWindow(qwrappers.WindowWrapper):
        def load_settings(self, config):
                blobs = "", ""
                isFullscreen = False
-               isPortrait = qui_utils.screen_orientation() == QtCore.Qt.Vertical
+               orientation = self._app.orientation
                tabIndex = 0
                try:
                        blobs = [
@@ -410,7 +411,7 @@ class MainWindow(qwrappers.WindowWrapper):
                        ]
                        isFullscreen = config.getboolean(constants.__pretty_app_name__, "fullscreen")
                        tabIndex = config.getint(constants.__pretty_app_name__, "tab")
-                       isPortrait = config.getboolean(constants.__pretty_app_name__, "portrait")
+                       orientation = config.get(constants.__pretty_app_name__, "orientation")
                except ConfigParser.NoOptionError, e:
                        _moduleLogger.info(
                                "Settings file %s is missing option %s" % (
@@ -433,6 +434,7 @@ class MainWindow(qwrappers.WindowWrapper):
                        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" % (
@@ -456,7 +458,7 @@ class MainWindow(qwrappers.WindowWrapper):
                )
                self.set_default_credentials(*creds)
                self._app.fullscreenAction.setChecked(isFullscreen)
-               self._app.orientationAction.setChecked(isPortrait)
+               self._app.set_orientation(orientation)
                self.set_current_tab(tabIndex)
 
                backendId = 2 # For backwards compatibility
@@ -492,7 +494,7 @@ class MainWindow(qwrappers.WindowWrapper):
                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()))
+               config.set(constants.__pretty_app_name__, "orientation", str(self._app.orientation))
                for i, value in enumerate(self.get_default_credentials()):
                        blob = base64.b64encode(value)
                        config.set(constants.__pretty_app_name__, "bin_blob_%i" % i, blob)
@@ -503,6 +505,7 @@ class MainWindow(qwrappers.WindowWrapper):
                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):
@@ -512,12 +515,13 @@ class MainWindow(qwrappers.WindowWrapper):
                        for settingName, settingValue in tabSettings.iteritems():
                                config.set(sectionName, settingName, settingValue)
 
-       def set_orientation(self, isPortrait):
-               qwrappers.WindowWrapper.set_orientation(self, isPortrait)
-               if isPortrait:
-                       self._tabWidget.setTabPosition(QtGui.QTabWidget.South)
-               else:
+       def update_orientation(self, orientation):
+               qwrappers.WindowWrapper.update_orientation(self, orientation)
+               windowOrientation = self.idealWindowOrientation
+               if windowOrientation == QtCore.Qt.Horizontal:
                        self._tabWidget.setTabPosition(QtGui.QTabWidget.West)
+               else:
+                       self._tabWidget.setTabPosition(QtGui.QTabWidget.South)
 
        def _initialize_tab(self, index):
                assert index < self.MAX_TABS, "Invalid tab"
@@ -544,6 +548,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
@@ -556,6 +567,8 @@ class MainWindow(qwrappers.WindowWrapper):
                if not accountNumberToDisplay:
                        accountNumberToDisplay = "Not Available (%s)" % self._session.state
                self._accountDialog.set_account_number(accountNumberToDisplay)
+               self._accountDialog.orientation = self._app.orientation
+
                response = self._accountDialog.run(self.window)
                if response == QtGui.QDialog.Accepted:
                        if self._accountDialog.doClear:
@@ -568,6 +581,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
@@ -581,13 +602,24 @@ class MainWindow(qwrappers.WindowWrapper):
                        self._app.notifyOnMissed = self._accountDialog.notifyOnMissed
                        self._app.notifyOnVoicemail = self._accountDialog.notifyOnVoicemail
                        self._app.notifyOnSms = self._accountDialog.notifyOnSms
+                       self._app.set_orientation(self._accountDialog.orientation)
                        self._app.save_settings()
                elif response == QtGui.QDialog.Rejected:
                        _moduleLogger.info("Cancelled")
                else:
                        _moduleLogger.info("Unknown response")
 
-       @QtCore.pyqtSlot()
+       @qt_compat.Slot()
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_window_resized(self):
+               with qui_utils.notify_error(self._app.errorLog):
+                       windowOrientation = self.idealWindowOrientation
+                       if windowOrientation == QtCore.Qt.Horizontal:
+                               self._tabWidget.setTabPosition(QtGui.QTabWidget.West)
+                       else:
+                               self._tabWidget.setTabPosition(QtGui.QTabWidget.South)
+
+       @qt_compat.Slot()
        @misc_utils.log_exception(_moduleLogger)
        def _on_new_message_alert(self):
                with qui_utils.notify_error(self._errorLog):
@@ -597,19 +629,19 @@ class MainWindow(qwrappers.WindowWrapper):
                                else:
                                        self._app.ledHandler.on()
 
-       @QtCore.pyqtSlot()
+       @qt_compat.Slot()
        @misc_utils.log_exception(_moduleLogger)
        def _on_call_missed(self):
                with qui_utils.notify_error(self._errorLog):
                        self._session.update_messages(self._session.MESSAGE_VOICEMAILS, force=True)
 
-       @QtCore.pyqtSlot(str)
+       @qt_compat.Slot(str)
        @misc_utils.log_exception(_moduleLogger)
        def _on_session_error(self, message):
                with qui_utils.notify_error(self._errorLog):
                        self._errorLog.push_error(message)
 
-       @QtCore.pyqtSlot()
+       @qt_compat.Slot()
        @misc_utils.log_exception(_moduleLogger)
        def _on_login(self):
                with qui_utils.notify_error(self._errorLog):
@@ -623,17 +655,23 @@ class MainWindow(qwrappers.WindowWrapper):
                        for tab in self._tabsContents:
                                tab.enable()
                        self._initialize_tab(self._currentTab)
-                       self._callHandler.start()
-
-       @QtCore.pyqtSlot()
+                       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()
+
+       @qt_compat.Slot()
        @misc_utils.log_exception(_moduleLogger)
        def _on_logout(self):
                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()
+       @qt_compat.Slot()
        @misc_utils.log_exception(_moduleLogger)
        def _on_app_alert(self):
                with qui_utils.notify_error(self._errorLog):
@@ -645,7 +683,7 @@ class MainWindow(qwrappers.WindowWrapper):
                                }[(self._app.notifyOnSms, self._app.notifyOnVoicemail)]
                                self._session.update_messages(messageType, force=True)
 
-       @QtCore.pyqtSlot()
+       @qt_compat.Slot()
        @misc_utils.log_exception(_moduleLogger)
        def _on_recipients_changed(self):
                with qui_utils.notify_error(self._errorLog):
@@ -663,14 +701,14 @@ class MainWindow(qwrappers.WindowWrapper):
        def _on_child_close(self, obj = None):
                self._smsEntryDialog = None
 
-       @QtCore.pyqtSlot()
-       @QtCore.pyqtSlot(bool)
+       @qt_compat.Slot()
+       @qt_compat.Slot(bool)
        @misc_utils.log_exception(_moduleLogger)
        def _on_login_requested(self, checked = True):
                with qui_utils.notify_error(self._errorLog):
                        self._prompt_for_login()
 
-       @QtCore.pyqtSlot(int)
+       @qt_compat.Slot(int)
        @misc_utils.log_exception(_moduleLogger)
        def _on_tab_changed(self, index):
                with qui_utils.notify_error(self._errorLog):
@@ -679,22 +717,22 @@ class MainWindow(qwrappers.WindowWrapper):
                        if self._app.alarmHandler.alarmType == self._app.alarmHandler.ALARM_APPLICATION:
                                self._app.ledHandler.off()
 
-       @QtCore.pyqtSlot()
-       @QtCore.pyqtSlot(bool)
+       @qt_compat.Slot()
+       @qt_compat.Slot(bool)
        @misc_utils.log_exception(_moduleLogger)
        def _on_refresh(self, checked = True):
                with qui_utils.notify_error(self._errorLog):
                        self._tabsContents[self._currentTab].refresh(force=True)
 
-       @QtCore.pyqtSlot()
-       @QtCore.pyqtSlot(bool)
+       @qt_compat.Slot()
+       @qt_compat.Slot(bool)
        @misc_utils.log_exception(_moduleLogger)
        def _on_refresh_connection(self, checked = True):
                with qui_utils.notify_error(self._errorLog):
                        self._session.refresh_connection()
 
-       @QtCore.pyqtSlot()
-       @QtCore.pyqtSlot(bool)
+       @qt_compat.Slot()
+       @qt_compat.Slot(bool)
        @misc_utils.log_exception(_moduleLogger)
        def _on_import(self, checked = True):
                with qui_utils.notify_error(self._errorLog):
@@ -707,8 +745,8 @@ class MainWindow(qwrappers.WindowWrapper):
                        if self._tabsContents[self.CONTACTS_TAB].has_child:
                                self._tabsContents[self.CONTACTS_TAB].child.update_addressbooks()
 
-       @QtCore.pyqtSlot()
-       @QtCore.pyqtSlot(bool)
+       @qt_compat.Slot()
+       @qt_compat.Slot(bool)
        @misc_utils.log_exception(_moduleLogger)
        def _on_account(self, checked = True):
                with qui_utils.notify_error(self._errorLog):