Trying to be a bit more informative when reporting problems to the user
[gc-dialer] / src / dialogs.py
index e381dc3..0312ee6 100644 (file)
@@ -11,6 +11,7 @@ from PyQt4 import QtGui
 from PyQt4 import QtCore
 
 import constants
+from util import qwrappers
 from util import qui_utils
 from util import misc as misc_utils
 
@@ -21,6 +22,7 @@ _moduleLogger = logging.getLogger(__name__)
 class CredentialsDialog(object):
 
        def __init__(self, app):
+               self._app = app
                self._usernameField = QtGui.QLineEdit()
                self._passwordField = QtGui.QLineEdit()
                self._passwordField.setEchoMode(QtGui.QLineEdit.Password)
@@ -89,6 +91,7 @@ class CredentialsDialog(object):
 class AboutDialog(object):
 
        def __init__(self, app):
+               self._app = app
                self._title = QtGui.QLabel(
                        "<h1>%s</h1><h3>Version: %s</h3>" % (
                                constants.__pretty_app_name__, constants.__version__
@@ -167,6 +170,7 @@ class AccountDialog(object):
        ]
 
        def __init__(self, app):
+               self._app = app
                self._doClear = False
 
                self._accountNumberLabel = QtGui.QLabel("NUMBER NOT SET")
@@ -280,10 +284,10 @@ class AccountDialog(object):
        def _set_notification_time(self, minutes):
                for i, (time, _) in enumerate(self._RECURRENCE_CHOICES):
                        if time == minutes:
-                               self._callbackSelector.setCurrentIndex(i)
+                               self._notificationTimeSelector.setCurrentIndex(i)
                                break
                else:
-                               self._callbackSelector.setCurrentIndex(0)
+                               self._notificationTimeSelector.setCurrentIndex(0)
 
        notificationTime = property(_get_notification_time, _set_notification_time)
 
@@ -463,7 +467,7 @@ class ContactList(object):
                selectedNumber = self._session.draft.get_selected_number(cid)
                if len(numbers) == 1:
                        # If no alt numbers available, check the address book
-                       numbers, defaultIndex = _get_contact_numbers(self._session, cid, selectedNumber)
+                       numbers, defaultIndex = _get_contact_numbers(self._session, cid, selectedNumber, numbers[0][1])
                else:
                        defaultIndex = _index_number(numbers, selectedNumber)
 
@@ -502,14 +506,16 @@ class ContactList(object):
                        self._session.draft.remove_contact(self._uiItems[index]["cid"])
 
 
-class SMSEntryWindow(object):
+class SMSEntryWindow(qwrappers.WindowWrapper):
 
        MAX_CHAR = 160
        # @bug Somehow a window is being destroyed on object creation which causes glitches on Maemo 5
 
        def __init__(self, parent, app, session, errorLog):
-               self._app = app
+               qwrappers.WindowWrapper.__init__(self, parent, app)
                self._session = session
+               self._session.messagesUpdated.connect(self._on_refresh_history)
+               self._session.historyUpdated.connect(self._on_refresh_history)
                self._session.draft.recipientsChanged.connect(self._on_recipients_changed)
 
                self._session.draft.sendingMessage.connect(self._on_op_started)
@@ -567,34 +573,15 @@ class SMSEntryWindow(object):
                self._buttonLayout.addWidget(self._dialButton)
                self._buttonLayout.addWidget(self._cancelButton)
 
-               self._layout = QtGui.QVBoxLayout()
                self._layout.addWidget(self._errorDisplay.toplevel)
                self._layout.addWidget(self._scrollEntry)
                self._layout.addLayout(self._buttonLayout)
+               self._layout.setDirection(QtGui.QBoxLayout.TopToBottom)
 
-               centralWidget = QtGui.QWidget()
-               centralWidget.setLayout(self._layout)
-
-               self._window = qui_utils.QSignalingMainWindow(parent)
-               qui_utils.set_window_orientation(self._window, QtCore.Qt.Horizontal)
-               qui_utils.set_stackable(self._window, True)
                self._window.setWindowTitle("Contact")
-               self._window.setCentralWidget(centralWidget)
-               self._window.addAction(self._app.orientationAction)
                self._window.closed.connect(self._on_close_window)
                self._window.hidden.connect(self._on_close_window)
 
-               self._closeWindowAction = QtGui.QAction(None)
-               self._closeWindowAction.setText("Close")
-               self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w"))
-               self._closeWindowAction.triggered.connect(self._on_close_window)
-
-               fileMenu = self._window.menuBar().addMenu("&File")
-               fileMenu.addAction(self._closeWindowAction)
-               fileMenu.addAction(app.quitAction)
-               viewMenu = self._window.menuBar().addMenu("&View")
-               viewMenu.addAction(app.fullscreenAction)
-
                self._scrollTimer = QtCore.QTimer()
                self._scrollTimer.setInterval(100)
                self._scrollTimer.setSingleShot(True)
@@ -603,11 +590,8 @@ class SMSEntryWindow(object):
                self._smsEntry.setPlainText(self._session.draft.message)
                self._update_letter_count()
                self._update_target_fields()
-               self._window.show()
-
-       @property
-       def window(self):
-               return self._window
+               self.set_fullscreen(self._app.fullscreenAction.isChecked())
+               self.set_orientation(self._app.orientationAction.isChecked())
 
        def close(self):
                if self._window is None:
@@ -617,13 +601,15 @@ class SMSEntryWindow(object):
                try:
                        message = unicode(self._smsEntry.toPlainText())
                        self._session.draft.message = message
-                       window.hide()
+                       self.hide()
                except AttributeError:
                        _moduleLogger.exception("Oh well")
                except RuntimeError:
                        _moduleLogger.exception("Oh well")
 
        def destroy(self):
+               self._session.messagesUpdated.disconnect(self._on_refresh_history)
+               self._session.historyUpdated.disconnect(self._on_refresh_history)
                self._session.draft.recipientsChanged.disconnect(self._on_recipients_changed)
                self._session.draft.sendingMessage.disconnect(self._on_op_started)
                self._session.draft.calling.disconnect(self._on_op_started)
@@ -644,10 +630,7 @@ class SMSEntryWindow(object):
                        _moduleLogger.exception("Oh well")
 
        def set_orientation(self, isPortrait):
-               if isPortrait:
-                       qui_utils.set_window_orientation(self._window, QtCore.Qt.Vertical)
-               else:
-                       qui_utils.set_window_orientation(self._window, QtCore.Qt.Horizontal)
+               qwrappers.WindowWrapper.set_orientation(self, isPortrait)
                self._scroll_to_bottom()
 
        def _update_letter_count(self):
@@ -678,16 +661,12 @@ class SMSEntryWindow(object):
                        else:
                                self._smsButton.setEnabled(True)
 
-       def _update_target_fields(self):
+       def _update_history(self, cid):
                draftContactsCount = self._session.draft.get_num_contacts()
-               if draftContactsCount == 0:
-                       self._window.hide()
-                       del self._cids[:]
-               elif draftContactsCount == 1:
-                       (cid, ) = self._session.draft.get_contacts()
-                       title = self._session.draft.get_title(cid)
+               if draftContactsCount != 1:
+                       self._history.setVisible(False)
+               else:
                        description = self._session.draft.get_description(cid)
-                       numbers = self._session.draft.get_numbers(cid)
 
                        self._targetList.setVisible(False)
                        if description:
@@ -696,13 +675,26 @@ class SMSEntryWindow(object):
                        else:
                                self._history.setText("")
                                self._history.setVisible(False)
+
+       def _update_target_fields(self):
+               draftContactsCount = self._session.draft.get_num_contacts()
+               if draftContactsCount == 0:
+                       self.hide()
+                       del self._cids[:]
+               elif draftContactsCount == 1:
+                       (cid, ) = self._session.draft.get_contacts()
+                       title = self._session.draft.get_title(cid)
+                       numbers = self._session.draft.get_numbers(cid)
+
+                       self._targetList.setVisible(False)
+                       self._update_history(cid)
                        self._populate_number_selector(self._singleNumberSelector, cid, 0, numbers)
                        self._cids = [cid]
 
                        self._scroll_to_bottom()
                        self._window.setWindowTitle(title)
                        self._smsEntry.setFocus(QtCore.Qt.OtherFocusReason)
-                       self._window.show()
+                       self.show()
                        self._window.raise_()
                else:
                        self._targetList.setVisible(True)
@@ -714,7 +706,7 @@ class SMSEntryWindow(object):
                        self._scroll_to_bottom()
                        self._window.setWindowTitle("Contacts")
                        self._smsEntry.setFocus(QtCore.Qt.OtherFocusReason)
-                       self._window.show()
+                       self.show()
                        self._window.raise_()
 
        def _populate_number_selector(self, selector, cid, cidIndex, numbers):
@@ -723,7 +715,7 @@ class SMSEntryWindow(object):
                selectedNumber = self._session.draft.get_selected_number(cid)
                if len(numbers) == 1:
                        # If no alt numbers available, check the address book
-                       numbers, defaultIndex = _get_contact_numbers(self._session, cid, selectedNumber)
+                       numbers, defaultIndex = _get_contact_numbers(self._session, cid, selectedNumber, numbers[0][1])
                else:
                        defaultIndex = _index_number(numbers, selectedNumber)
 
@@ -783,6 +775,16 @@ class SMSEntryWindow(object):
 
        @QtCore.pyqtSlot()
        @misc_utils.log_exception(_moduleLogger)
+       def _on_refresh_history(self):
+               draftContactsCount = self._session.draft.get_num_contacts()
+               if draftContactsCount != 1:
+                       # Changing contact count will automatically refresh it
+                       return
+               (cid, ) = self._session.draft.get_contacts()
+               self._update_history(cid)
+
+       @QtCore.pyqtSlot()
+       @misc_utils.log_exception(_moduleLogger)
        def _on_recipients_changed(self):
                with qui_utils.notify_error(self._app.errorLog):
                        self._update_target_fields()
@@ -795,7 +797,7 @@ class SMSEntryWindow(object):
                        self._smsEntry.setReadOnly(True)
                        self._smsButton.setVisible(False)
                        self._dialButton.setVisible(False)
-                       self._window.show()
+                       self.show()
 
        @QtCore.pyqtSlot()
        @misc_utils.log_exception(_moduleLogger)
@@ -812,7 +814,8 @@ class SMSEntryWindow(object):
                        self._cancelButton.setVisible(False)
                        self._smsButton.setVisible(True)
                        self._dialButton.setVisible(True)
-                       self._window.hide()
+                       self.close()
+                       self.destroy()
 
        @QtCore.pyqtSlot()
        @misc_utils.log_exception(_moduleLogger)
@@ -841,18 +844,19 @@ class SMSEntryWindow(object):
 
 
 def _index_number(numbers, default):
-       uglyContactNumbers = (
+       uglyDefault = misc_utils.make_ugly(default)
+       uglyContactNumbers = list(
                misc_utils.make_ugly(contactNumber)
                for (contactNumber, _) in numbers
        )
        defaultMatches = [
-               misc_utils.similar_ugly_numbers(default, contactNumber)
+               misc_utils.similar_ugly_numbers(uglyDefault, contactNumber)
                for contactNumber in uglyContactNumbers
        ]
        try:
                defaultIndex = defaultMatches.index(True)
        except ValueError:
-               defaultIndex = 0
+               defaultIndex = -1
                _moduleLogger.warn(
                        "Could not find contact number %s among %r" % (
                                default, numbers
@@ -861,7 +865,7 @@ def _index_number(numbers, default):
        return defaultIndex
 
 
-def _get_contact_numbers(session, contactId, number):
+def _get_contact_numbers(session, contactId, number, description):
        contactPhoneNumbers = []
        if contactId and contactId != "0":
                try:
@@ -875,8 +879,8 @@ def _get_contact_numbers(session, contactId, number):
                ]
                defaultIndex = _index_number(contactPhoneNumbers, number)
 
-       if not contactPhoneNumbers:
-               contactPhoneNumbers = [(number, "Unknown")]
+       if not contactPhoneNumbers or defaultIndex == -1:
+               contactPhoneNumbers += [(number, description)]
                defaultIndex = 0
 
        return contactPhoneNumbers, defaultIndex