Adding support for PySide's naming scheme
[gc-dialer] / src / gv_views.py
index 8451693..ab3fad4 100644 (file)
@@ -8,8 +8,9 @@ import string
 import itertools
 import logging
 
-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")
 
 from util import qtpie
 from util import qui_utils
@@ -22,6 +23,9 @@ import backends.file_backend as file_backend
 _moduleLogger = logging.getLogger(__name__)
 
 
+_SENTINEL_ICON = QtGui.QIcon()
+
+
 class Dialpad(object):
 
        def __init__(self, app, session, errorLog):
@@ -156,8 +160,8 @@ class Dialpad(object):
                with qui_utils.notify_error(self._errorLog):
                        self._entry.clear()
 
-       @QtCore.pyqtSlot()
-       @QtCore.pyqtSlot(bool)
+       @qt_compat.Slot()
+       @qt_compat.Slot(bool)
        @misc_utils.log_exception(_moduleLogger)
        def _on_sms_clicked(self, checked = False):
                with qui_utils.notify_error(self._errorLog):
@@ -168,10 +172,10 @@ class Dialpad(object):
                        title = misc_utils.make_pretty(number)
                        description = misc_utils.make_pretty(number)
                        numbersWithDescriptions = [(number, "")]
-                       self._session.draft.add_contact(contactId, title, description, numbersWithDescriptions)
+                       self._session.draft.add_contact(contactId, None, title, description, numbersWithDescriptions)
 
-       @QtCore.pyqtSlot()
-       @QtCore.pyqtSlot(bool)
+       @qt_compat.Slot()
+       @qt_compat.Slot(bool)
        @misc_utils.log_exception(_moduleLogger)
        def _on_call_clicked(self, checked = False):
                with qui_utils.notify_error(self._errorLog):
@@ -183,7 +187,7 @@ class Dialpad(object):
                        description = misc_utils.make_pretty(number)
                        numbersWithDescriptions = [(number, "")]
                        self._session.draft.clear()
-                       self._session.draft.add_contact(contactId, title, description, numbersWithDescriptions)
+                       self._session.draft.add_contact(contactId, None, title, description, numbersWithDescriptions)
                        self._session.draft.call()
 
 
@@ -260,7 +264,12 @@ class History(object):
        FROM_IDX = 1
        MAX_IDX = 2
 
-       HISTORY_ITEM_TYPES = ["Received", "Missed", "Placed", "All"]
+       HISTORY_RECEIVED = "Received"
+       HISTORY_MISSED = "Missed"
+       HISTORY_PLACED = "Placed"
+       HISTORY_ALL = "All"
+
+       HISTORY_ITEM_TYPES = [HISTORY_RECEIVED, HISTORY_MISSED, HISTORY_PLACED, HISTORY_ALL]
        HISTORY_COLUMNS = ["Details", "From"]
        assert len(HISTORY_COLUMNS) == MAX_IDX
 
@@ -277,6 +286,23 @@ class History(object):
                        self.HISTORY_ITEM_TYPES.index(self._selectedFilter)
                )
                self._typeSelection.currentIndexChanged[str].connect(self._on_filter_changed)
+               refreshIcon = qui_utils.get_theme_icon(
+                       ("view-refresh", "general_refresh", "gtk-refresh", ),
+                       _SENTINEL_ICON
+               )
+               if refreshIcon is not _SENTINEL_ICON:
+                       self._refreshButton = QtGui.QPushButton(refreshIcon, "")
+               else:
+                       self._refreshButton = QtGui.QPushButton("Refresh")
+               self._refreshButton.clicked.connect(self._on_refresh_clicked)
+               self._refreshButton.setSizePolicy(QtGui.QSizePolicy(
+                       QtGui.QSizePolicy.Minimum,
+                       QtGui.QSizePolicy.Minimum,
+                       QtGui.QSizePolicy.PushButton,
+               ))
+               self._managerLayout = QtGui.QHBoxLayout()
+               self._managerLayout.addWidget(self._typeSelection, 1000)
+               self._managerLayout.addWidget(self._refreshButton, 0)
 
                self._itemStore = QtGui.QStandardItemModel()
                self._itemStore.setHorizontalHeaderLabels(self.HISTORY_COLUMNS)
@@ -295,7 +321,7 @@ class History(object):
                self._itemView.activated.connect(self._on_row_activated)
 
                self._layout = QtGui.QVBoxLayout()
-               self._layout.addWidget(self._typeSelection)
+               self._layout.addLayout(self._managerLayout)
                self._layout.addWidget(self._itemView)
                self._widget = QtGui.QWidget()
                self._widget.setLayout(self._layout)
@@ -329,7 +355,21 @@ class History(object):
                self._itemView.clear()
 
        def refresh(self, force=True):
-               self._session.update_history(force)
+               self._itemView.setFocus(QtCore.Qt.OtherFocusReason)
+
+               if self._selectedFilter == self.HISTORY_RECEIVED:
+                       self._session.update_history(self._session.HISTORY_RECEIVED, force)
+               elif self._selectedFilter == self.HISTORY_MISSED:
+                       self._session.update_history(self._session.HISTORY_MISSED, force)
+               elif self._selectedFilter == self.HISTORY_PLACED:
+                       self._session.update_history(self._session.HISTORY_PLACED, force)
+               elif self._selectedFilter == self.HISTORY_ALL:
+                       self._session.update_history(self._session.HISTORY_ALL, force)
+               else:
+                       assert False, "How did we get here?"
+
+               if self._app.notifyOnMissed and self._app.alarmHandler.alarmType == self._app.alarmHandler.ALARM_BACKGROUND:
+                       self._app.ledHandler.off()
 
        def _populate_items(self):
                self._categoryManager.prepare_for_update(self._session.get_when_history_updated())
@@ -366,32 +406,39 @@ class History(object):
                        self._categoryManager.add_row(event["time"], row)
                self._itemView.expandAll()
 
-       @QtCore.pyqtSlot(str)
+       @qt_compat.Slot(str)
        @misc_utils.log_exception(_moduleLogger)
        def _on_filter_changed(self, newItem):
                with qui_utils.notify_error(self._errorLog):
                        self._selectedFilter = str(newItem)
                        self._populate_items()
 
-       @QtCore.pyqtSlot()
+       @qt_compat.Slot()
        @misc_utils.log_exception(_moduleLogger)
        def _on_history_updated(self):
                with qui_utils.notify_error(self._errorLog):
                        self._populate_items()
 
-       @QtCore.pyqtSlot(QtCore.QModelIndex)
+       @qt_compat.Slot()
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_refresh_clicked(self, arg = None):
+               with qui_utils.notify_error(self._errorLog):
+                       self.refresh(force=True)
+
+       @qt_compat.Slot(QtCore.QModelIndex)
        @misc_utils.log_exception(_moduleLogger)
        def _on_row_activated(self, index):
                with qui_utils.notify_error(self._errorLog):
                        timeIndex = index.parent()
-                       assert timeIndex.isValid(), "Invalid row"
+                       if not timeIndex.isValid():
+                               return
                        timeRow = timeIndex.row()
                        row = index.row()
                        detailsItem = self._categoryManager.get_item(timeRow, row, self.DETAILS_IDX)
                        fromItem = self._categoryManager.get_item(timeRow, row, self.FROM_IDX)
                        contactDetails = detailsItem.data().toPyObject()
 
-                       title = str(fromItem.text())
+                       title = unicode(fromItem.text())
                        number = str(contactDetails[QtCore.QString("number")])
                        contactId = number # ids don't seem too unique so using numbers
 
@@ -412,7 +459,7 @@ class History(object):
                                        descriptionRows.append("<tr><td>%s</td></tr>" % "</td><td>".join(rowItems))
                        description = "<table>%s</table>" % "".join(descriptionRows)
                        numbersWithDescriptions = [(str(contactDetails[QtCore.QString("number")]), "")]
-                       self._session.draft.add_contact(contactId, title, description, numbersWithDescriptions)
+                       self._session.draft.add_contact(contactId, None, title, description, numbersWithDescriptions)
 
 
 class Messages(object):
@@ -428,7 +475,7 @@ class Messages(object):
        ALL_STATUS = "Any"
        MESSAGE_STATUSES = [UNREAD_STATUS, UNARCHIVED_STATUS, ALL_STATUS]
 
-       _MIN_MESSAGES_SHOWN = 4
+       _MIN_MESSAGES_SHOWN = 1
 
        def __init__(self, app, session, errorLog):
                self._selectedTypeFilter = self.ALL_TYPES
@@ -452,9 +499,25 @@ class Messages(object):
                )
                self._statusSelection.currentIndexChanged[str].connect(self._on_status_filter_changed)
 
+               refreshIcon = qui_utils.get_theme_icon(
+                       ("view-refresh", "general_refresh", "gtk-refresh", ),
+                       _SENTINEL_ICON
+               )
+               if refreshIcon is not _SENTINEL_ICON:
+                       self._refreshButton = QtGui.QPushButton(refreshIcon, "")
+               else:
+                       self._refreshButton = QtGui.QPushButton("Refresh")
+               self._refreshButton.clicked.connect(self._on_refresh_clicked)
+               self._refreshButton.setSizePolicy(QtGui.QSizePolicy(
+                       QtGui.QSizePolicy.Minimum,
+                       QtGui.QSizePolicy.Minimum,
+                       QtGui.QSizePolicy.PushButton,
+               ))
+
                self._selectionLayout = QtGui.QHBoxLayout()
-               self._selectionLayout.addWidget(self._typeSelection)
-               self._selectionLayout.addWidget(self._statusSelection)
+               self._selectionLayout.addWidget(self._typeSelection, 1000)
+               self._selectionLayout.addWidget(self._statusSelection, 1000)
+               self._selectionLayout.addWidget(self._refreshButton, 0)
 
                self._itemStore = QtGui.QStandardItemModel()
                self._itemStore.setHorizontalHeaderLabels(["Messages"])
@@ -472,6 +535,7 @@ class Messages(object):
                self._itemView.setItemsExpandable(False)
                self._itemView.setItemDelegate(self._htmlDelegate)
                self._itemView.activated.connect(self._on_row_activated)
+               self._itemView.header().sectionResized.connect(self._on_column_resized)
 
                self._layout = QtGui.QVBoxLayout()
                self._layout.addLayout(self._selectionLayout)
@@ -516,7 +580,21 @@ class Messages(object):
                self._itemView.clear()
 
        def refresh(self, force=True):
-               self._session.update_messages(force)
+               self._itemView.setFocus(QtCore.Qt.OtherFocusReason)
+
+               if self._selectedTypeFilter == self.NO_MESSAGES:
+                       pass
+               elif self._selectedTypeFilter == self.TEXT_MESSAGES:
+                       self._session.update_messages(self._session.MESSAGE_TEXTS, force)
+               elif self._selectedTypeFilter == self.VOICEMAIL_MESSAGES:
+                       self._session.update_messages(self._session.MESSAGE_VOICEMAILS, force)
+               elif self._selectedTypeFilter == self.ALL_TYPES:
+                       self._session.update_messages(self._session.MESSAGE_ALL, force)
+               else:
+                       assert False, "How did we get here?"
+
+               if self._app.notifyOnSms or self._app.notifyOnVoicemail and self._app.alarmHandler.alarmType == self._app.alarmHandler.ALARM_BACKGROUND:
+                       self._app.ledHandler.off()
 
        def _populate_items(self):
                self._categoryManager.prepare_for_update(self._session.get_when_messages_updated())
@@ -558,11 +636,11 @@ class Messages(object):
                                        for messagePart in messageParts
                                ]
 
-                       firstMessage = "<b>%s - %s</b> <i>(%s)</i>" % (name, prettyNumber, relTime)
+                       firstMessage = "<b>%s<br/>%s</b> <i>(%s)</i>" % (name, prettyNumber, relTime)
 
                        expandedMessages = [firstMessage]
                        expandedMessages.extend(messages)
-                       if (self._MIN_MESSAGES_SHOWN + 1) < len(messages):
+                       if self._MIN_MESSAGES_SHOWN < len(messages):
                                secondMessage = "<i>%d Messages Hidden...</i>" % (len(messages) - self._MIN_MESSAGES_SHOWN, )
                                collapsedMessages = [firstMessage, secondMessage]
                                collapsedMessages.extend(messages[-(self._MIN_MESSAGES_SHOWN+0):])
@@ -581,49 +659,65 @@ class Messages(object):
                        self._categoryManager.add_row(item["time"], row)
                self._itemView.expandAll()
 
-       @QtCore.pyqtSlot(str)
+       @qt_compat.Slot(str)
        @misc_utils.log_exception(_moduleLogger)
        def _on_type_filter_changed(self, newItem):
                with qui_utils.notify_error(self._errorLog):
                        self._selectedTypeFilter = str(newItem)
                        self._populate_items()
 
-       @QtCore.pyqtSlot(str)
+       @qt_compat.Slot(str)
        @misc_utils.log_exception(_moduleLogger)
        def _on_status_filter_changed(self, newItem):
                with qui_utils.notify_error(self._errorLog):
                        self._selectedStatusFilter = str(newItem)
                        self._populate_items()
 
-       @QtCore.pyqtSlot()
+       @qt_compat.Slot()
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_refresh_clicked(self, arg = None):
+               with qui_utils.notify_error(self._errorLog):
+                       self.refresh(force=True)
+
+       @qt_compat.Slot()
        @misc_utils.log_exception(_moduleLogger)
        def _on_messages_updated(self):
                with qui_utils.notify_error(self._errorLog):
                        self._populate_items()
 
-       @QtCore.pyqtSlot(QtCore.QModelIndex)
+       @qt_compat.Slot(QtCore.QModelIndex)
        @misc_utils.log_exception(_moduleLogger)
        def _on_row_activated(self, index):
                with qui_utils.notify_error(self._errorLog):
                        timeIndex = index.parent()
-                       assert timeIndex.isValid(), "Invalid row"
+                       if not timeIndex.isValid():
+                               return
                        timeRow = timeIndex.row()
                        row = index.row()
                        item = self._categoryManager.get_item(timeRow, row, 0)
                        contactDetails = item.data().toPyObject()
 
-                       name = str(contactDetails[QtCore.QString("name")])
+                       name = unicode(contactDetails[QtCore.QString("name")])
                        number = str(contactDetails[QtCore.QString("number")])
                        if not name or name == number:
-                               name = str(contactDetails[QtCore.QString("location")])
+                               name = unicode(contactDetails[QtCore.QString("location")])
                        if not name:
                                name = "Unknown"
 
-                       contactId = str(contactDetails[QtCore.QString("id")])
+                       if str(contactDetails[QtCore.QString("type")]) == "Voicemail":
+                               messageId = str(contactDetails[QtCore.QString("id")])
+                       else:
+                               messageId = None
+                       contactId = str(contactDetails[QtCore.QString("contactId")])
                        title = name
-                       description = str(contactDetails[QtCore.QString("expandedMessages")])
+                       description = unicode(contactDetails[QtCore.QString("expandedMessages")])
                        numbersWithDescriptions = [(number, "")]
-                       self._session.draft.add_contact(contactId, title, description, numbersWithDescriptions)
+                       self._session.draft.add_contact(contactId, messageId, title, description, numbersWithDescriptions)
+
+       @qt_compat.Slot(QtCore.QModelIndex)
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_column_resized(self, index, oldSize, newSize):
+               self._htmlDelegate.setWidth(newSize, self._itemStore)
 
 
 class Contacts(object):
@@ -633,7 +727,7 @@ class Contacts(object):
        def __init__(self, app, session, errorLog):
                self._app = app
                self._session = session
-               self._session.contactsUpdated.connect(self._on_contacts_updated)
+               self._session.accountUpdated.connect(self._on_contacts_updated)
                self._errorLog = errorLog
                self._addressBookFactories = [
                        null_backend.NullAddressBookFactory(),
@@ -645,6 +739,23 @@ class Contacts(object):
                self._listSelection.addItems([])
                self._listSelection.currentIndexChanged[str].connect(self._on_filter_changed)
                self._activeList = "None"
+               refreshIcon = qui_utils.get_theme_icon(
+                       ("view-refresh", "general_refresh", "gtk-refresh", ),
+                       _SENTINEL_ICON
+               )
+               if refreshIcon is not _SENTINEL_ICON:
+                       self._refreshButton = QtGui.QPushButton(refreshIcon, "")
+               else:
+                       self._refreshButton = QtGui.QPushButton("Refresh")
+               self._refreshButton.clicked.connect(self._on_refresh_clicked)
+               self._refreshButton.setSizePolicy(QtGui.QSizePolicy(
+                       QtGui.QSizePolicy.Minimum,
+                       QtGui.QSizePolicy.Minimum,
+                       QtGui.QSizePolicy.PushButton,
+               ))
+               self._managerLayout = QtGui.QHBoxLayout()
+               self._managerLayout.addWidget(self._listSelection, 1000)
+               self._managerLayout.addWidget(self._refreshButton, 0)
 
                self._itemStore = QtGui.QStandardItemModel()
                self._itemStore.setHorizontalHeaderLabels(["Contacts"])
@@ -662,7 +773,7 @@ class Contacts(object):
                self._itemView.activated.connect(self._on_row_activated)
 
                self._layout = QtGui.QVBoxLayout()
-               self._layout.addWidget(self._listSelection)
+               self._layout.addLayout(self._managerLayout)
                self._layout.addWidget(self._itemView)
                self._widget = QtGui.QWidget()
                self._widget.setLayout(self._layout)
@@ -700,7 +811,8 @@ class Contacts(object):
                self._itemView.clear()
 
        def refresh(self, force=True):
-               self._backend.update_contacts(force)
+               self._itemView.setFocus(QtCore.Qt.OtherFocusReason)
+               self._backend.update_account(force)
 
        @property
        def _backend(self):
@@ -784,7 +896,7 @@ class Contacts(object):
                contacts.sort(key=lambda contact: contact["name"].lower())
                return contacts
 
-       @QtCore.pyqtSlot(str)
+       @qt_compat.Slot(str)
        @misc_utils.log_exception(_moduleLogger)
        def _on_filter_changed(self, newItem):
                with qui_utils.notify_error(self._errorLog):
@@ -792,18 +904,25 @@ class Contacts(object):
                        self.refresh(force=False)
                        self._populate_items()
 
-       @QtCore.pyqtSlot()
+       @qt_compat.Slot()
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_refresh_clicked(self, arg = None):
+               with qui_utils.notify_error(self._errorLog):
+                       self.refresh(force=True)
+
+       @qt_compat.Slot()
        @misc_utils.log_exception(_moduleLogger)
        def _on_contacts_updated(self):
                with qui_utils.notify_error(self._errorLog):
                        self._populate_items()
 
-       @QtCore.pyqtSlot(QtCore.QModelIndex)
+       @qt_compat.Slot(QtCore.QModelIndex)
        @misc_utils.log_exception(_moduleLogger)
        def _on_row_activated(self, index):
                with qui_utils.notify_error(self._errorLog):
                        letterIndex = index.parent()
-                       assert letterIndex.isValid(), "Invalid row"
+                       if not letterIndex.isValid():
+                               return
                        letterRow = letterIndex.row()
                        letter = list(self._prefixes())[letterRow]
                        letterItem = self._alphaItem[letter]
@@ -811,9 +930,9 @@ class Contacts(object):
                        item = letterItem.child(rowIndex, 0)
                        contactDetails = item.data().toPyObject()
 
-                       name = str(contactDetails[QtCore.QString("name")])
+                       name = unicode(contactDetails[QtCore.QString("name")])
                        if not name:
-                               name = str(contactDetails[QtCore.QString("location")])
+                               name = unicode(contactDetails[QtCore.QString("location")])
                        if not name:
                                name = "Unknown"
 
@@ -835,7 +954,7 @@ class Contacts(object):
                        ]
                        title = name
                        description = name
-                       self._session.draft.add_contact(contactId, title, description, numbersWithDescriptions)
+                       self._session.draft.add_contact(contactId, None, title, description, numbersWithDescriptions)
 
        @staticmethod
        def _choose_phonetype(numberDetails):