Remove a user step: OSK pops up and have to scroll down to then see what text you...
[gc-dialer] / src / dialogs.py
index 4cbcd84..2feb6a2 100644 (file)
@@ -173,6 +173,10 @@ class AccountDialog(object):
        ALARM_BACKGROUND = "Background Alert"
        ALARM_APPLICATION = "Application Alert"
 
+       VOICEMAIL_CHECK_NOT_SUPPORTED = "Not Supported"
+       VOICEMAIL_CHECK_DISABLED = "Disabled"
+       VOICEMAIL_CHECK_ENABLED = "Enabled"
+
        def __init__(self, app):
                self._app = app
                self._doClear = False
@@ -188,6 +192,7 @@ class AccountDialog(object):
                self._missedCallsNotificationButton = QtGui.QCheckBox("Missed Calls")
                self._voicemailNotificationButton = QtGui.QCheckBox("Voicemail")
                self._smsNotificationButton = QtGui.QCheckBox("SMS")
+               self._voicemailOnMissedButton = QtGui.QCheckBox("Voicemail Update on Missed Calls")
                self._clearButton = QtGui.QPushButton("Clear Account")
                self._clearButton.clicked.connect(self._on_clear)
                self._callbackSelector = QtGui.QComboBox()
@@ -209,17 +214,26 @@ class AccountDialog(object):
                self._credLayout.addWidget(self._voicemailNotificationButton, 4, 1)
                self._credLayout.addWidget(QtGui.QLabel(""), 5, 0)
                self._credLayout.addWidget(self._smsNotificationButton, 5, 1)
-
-               self._credLayout.addWidget(QtGui.QLabel(""), 6, 0)
-               self._credLayout.addWidget(self._clearButton, 6, 1)
-               self._credLayout.addWidget(QtGui.QLabel(""), 3, 0)
+               self._credLayout.addWidget(QtGui.QLabel("Other"), 6, 0)
+               self._credLayout.addWidget(self._voicemailOnMissedButton, 6, 1)
+
+               self._credLayout.addWidget(QtGui.QLabel(""), 7, 0)
+               self._credLayout.addWidget(self._clearButton, 7, 1)
+               self._credWidget = QtGui.QWidget()
+               self._credWidget.setLayout(self._credLayout)
+               self._credWidget.setContentsMargins(0, 0, 0, 0)
+               self._scrollSettings = QtGui.QScrollArea()
+               self._scrollSettings.setWidget(self._credWidget)
+               self._scrollSettings.setWidgetResizable(True)
+               self._scrollSettings.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
+               self._scrollSettings.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
 
                self._loginButton = QtGui.QPushButton("&Apply")
                self._buttonLayout = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Cancel)
                self._buttonLayout.addButton(self._loginButton, QtGui.QDialogButtonBox.AcceptRole)
 
                self._layout = QtGui.QVBoxLayout()
-               self._layout.addLayout(self._credLayout)
+               self._layout.addWidget(self._scrollSettings)
                self._layout.addWidget(self._buttonLayout)
 
                self._dialog = QtGui.QDialog()
@@ -260,6 +274,29 @@ class AccountDialog(object):
        def set_account_number(self, num):
                self._accountNumberLabel.setText(num)
 
+       def _set_voicemail_on_missed(self, status):
+               if status == self.VOICEMAIL_CHECK_NOT_SUPPORTED:
+                       self._voicemailOnMissedButton.setChecked(False)
+                       self._voicemailOnMissedButton.hide()
+               elif status == self.VOICEMAIL_CHECK_DISABLED:
+                       self._voicemailOnMissedButton.setChecked(False)
+                       self._voicemailOnMissedButton.show()
+               elif status == self.VOICEMAIL_CHECK_ENABLED:
+                       self._voicemailOnMissedButton.setChecked(True)
+                       self._voicemailOnMissedButton.show()
+               else:
+                       raise RuntimeError("Unsupported option for updating voicemail on missed calls %r" % status)
+
+       def _get_voicemail_on_missed(self):
+               if not self._voicemailOnMissedButton.isVisible():
+                       return self.VOICEMAIL_CHECK_NOT_SUPPORTED
+               elif self._voicemailOnMissedButton.isChecked():
+                       return self.VOICEMAIL_CHECK_ENABLED
+               else:
+                       return self.VOICEMAIL_CHECK_DISABLED
+
+       updateVMOnMissedCall = property(_get_voicemail_on_missed, _set_voicemail_on_missed)
+
        def _set_notifications(self, enabled):
                for i in xrange(self._notificationSelecter.count()):
                        if self._notificationSelecter.itemText(i) == enabled:
@@ -350,15 +387,13 @@ class AccountDialog(object):
                        self._notificationTimeSelector.setEnabled(True)
 
                        self._missedCallsNotificationButton.setEnabled(False)
-                       self._voicemailNotificationButton.setEnabled(False)
-                       self._smsNotificationButton.setEnabled(False)
+                       self._voicemailNotificationButton.setEnabled(True)
+                       self._smsNotificationButton.setEnabled(True)
 
                        self._missedCallsNotificationButton.setChecked(False)
-                       self._voicemailNotificationButton.setChecked(True)
-                       self._smsNotificationButton.setChecked(True)
                else:
-
                        self._notificationTimeSelector.setEnabled(False)
+
                        self._missedCallsNotificationButton.setEnabled(False)
                        self._voicemailNotificationButton.setEnabled(False)
                        self._smsNotificationButton.setEnabled(False)
@@ -535,6 +570,216 @@ class ContactList(object):
                        self._session.draft.remove_contact(self._uiItems[index]["cid"])
 
 
+class VoicemailPlayer(object):
+
+       def __init__(self, app, session, errorLog):
+               self._app = app
+               self._session = session
+               self._errorLog = errorLog
+               self._token = None
+               self._session.voicemailAvailable.connect(self._on_voicemail_downloaded)
+               self._session.draft.recipientsChanged.connect(self._on_recipients_changed)
+
+               self._playButton = QtGui.QPushButton("Play")
+               self._playButton.clicked.connect(self._on_voicemail_play)
+               self._pauseButton = QtGui.QPushButton("Pause")
+               self._pauseButton.clicked.connect(self._on_voicemail_pause)
+               self._pauseButton.hide()
+               self._resumeButton = QtGui.QPushButton("Resume")
+               self._resumeButton.clicked.connect(self._on_voicemail_resume)
+               self._resumeButton.hide()
+               self._stopButton = QtGui.QPushButton("Stop")
+               self._stopButton.clicked.connect(self._on_voicemail_stop)
+               self._stopButton.hide()
+
+               self._downloadButton = QtGui.QPushButton("Download Voicemail")
+               self._downloadButton.clicked.connect(self._on_voicemail_download)
+               self._downloadLayout = QtGui.QHBoxLayout()
+               self._downloadLayout.addWidget(self._downloadButton)
+               self._downloadWidget = QtGui.QWidget()
+               self._downloadWidget.setLayout(self._downloadLayout)
+
+               self._playLabel = QtGui.QLabel("Voicemail")
+               self._saveButton = QtGui.QPushButton("Save")
+               self._saveButton.clicked.connect(self._on_voicemail_save)
+               self._playerLayout = QtGui.QHBoxLayout()
+               self._playerLayout.addWidget(self._playLabel)
+               self._playerLayout.addWidget(self._playButton)
+               self._playerLayout.addWidget(self._pauseButton)
+               self._playerLayout.addWidget(self._resumeButton)
+               self._playerLayout.addWidget(self._stopButton)
+               self._playerLayout.addWidget(self._saveButton)
+               self._playerWidget = QtGui.QWidget()
+               self._playerWidget.setLayout(self._playerLayout)
+
+               self._visibleWidget = None
+               self._layout = QtGui.QHBoxLayout()
+               self._layout.setContentsMargins(0, 0, 0, 0)
+               self._widget = QtGui.QWidget()
+               self._widget.setLayout(self._layout)
+               self._update_state()
+
+       @property
+       def toplevel(self):
+               return self._widget
+
+       def destroy(self):
+               self._session.voicemailAvailable.disconnect(self._on_voicemail_downloaded)
+               self._session.draft.recipientsChanged.disconnect(self._on_recipients_changed)
+               self._invalidate_token()
+
+       def _invalidate_token(self):
+               if self._token is not None:
+                       self._token.invalidate()
+                       self._token.error.disconnect(self._on_play_error)
+                       self._token.stateChange.connect(self._on_play_state)
+                       self._token.invalidated.connect(self._on_play_invalidated)
+
+       def _show_download(self, messageId):
+               if self._visibleWidget is self._downloadWidget:
+                       return
+               self._hide()
+               self._layout.addWidget(self._downloadWidget)
+               self._visibleWidget = self._downloadWidget
+               self._visibleWidget.show()
+
+       def _show_player(self, messageId):
+               if self._visibleWidget is self._playerWidget:
+                       return
+               self._hide()
+               self._layout.addWidget(self._playerWidget)
+               self._visibleWidget = self._playerWidget
+               self._visibleWidget.show()
+
+       def _hide(self):
+               if self._visibleWidget is None:
+                       return
+               self._visibleWidget.hide()
+               self._layout.removeWidget(self._visibleWidget)
+               self._visibleWidget = None
+
+       def _update_play_state(self):
+               if self._token is not None and self._token.isValid:
+                       self._playButton.setText("Stop")
+               else:
+                       self._playButton.setText("Play")
+
+       def _update_state(self):
+               if self._session.draft.get_num_contacts() != 1:
+                       self._hide()
+                       return
+
+               (cid, ) = self._session.draft.get_contacts()
+               messageId = self._session.draft.get_message_id(cid)
+               if messageId is None:
+                       self._hide()
+                       return
+
+               if self._session.is_available(messageId):
+                       self._show_player(messageId)
+               else:
+                       self._show_download(messageId)
+               if self._token is not None:
+                       self._token.invalidate()
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_voicemail_save(self, arg):
+               with qui_utils.notify_error(self._app.errorLog):
+                       targetPath = QtGui.QFileDialog.getSaveFileName(None, caption="Save Voicemail", filter="Audio File (*.mp3)")
+                       targetPath = unicode(targetPath)
+                       if not targetPath:
+                               return
+
+                       (cid, ) = self._session.draft.get_contacts()
+                       messageId = self._session.draft.get_message_id(cid)
+                       sourcePath = self._session.voicemail_path(messageId)
+                       import shutil
+                       shutil.copy2(sourcePath, targetPath)
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_play_error(self, error):
+               with qui_utils.notify_error(self._app.errorLog):
+                       self._app.errorLog.push_error(error)
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_play_invalidated(self):
+               with qui_utils.notify_error(self._app.errorLog):
+                       self._playButton.show()
+                       self._pauseButton.hide()
+                       self._resumeButton.hide()
+                       self._stopButton.hide()
+                       self._invalidate_token()
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_play_state(self, state):
+               with qui_utils.notify_error(self._app.errorLog):
+                       if state == self._token.STATE_PLAY:
+                               self._playButton.hide()
+                               self._pauseButton.show()
+                               self._resumeButton.hide()
+                               self._stopButton.show()
+                       elif state == self._token.STATE_PAUSE:
+                               self._playButton.hide()
+                               self._pauseButton.hide()
+                               self._resumeButton.show()
+                               self._stopButton.show()
+                       elif state == self._token.STATE_STOP:
+                               self._playButton.show()
+                               self._pauseButton.hide()
+                               self._resumeButton.hide()
+                               self._stopButton.hide()
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_voicemail_play(self, arg):
+               with qui_utils.notify_error(self._app.errorLog):
+                       (cid, ) = self._session.draft.get_contacts()
+                       messageId = self._session.draft.get_message_id(cid)
+                       sourcePath = self._session.voicemail_path(messageId)
+
+                       self._invalidate_token()
+                       uri = "file://%s" % sourcePath
+                       self._token = self._app.streamHandler.set_file(uri)
+                       self._token.stateChange.connect(self._on_play_state)
+                       self._token.invalidated.connect(self._on_play_invalidated)
+                       self._token.error.connect(self._on_play_error)
+                       self._token.play()
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_voicemail_pause(self, arg):
+               with qui_utils.notify_error(self._app.errorLog):
+                       self._token.pause()
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_voicemail_resume(self, arg):
+               with qui_utils.notify_error(self._app.errorLog):
+                       self._token.play()
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_voicemail_stop(self, arg):
+               with qui_utils.notify_error(self._app.errorLog):
+                       self._token.stop()
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_voicemail_download(self, arg):
+               with qui_utils.notify_error(self._app.errorLog):
+                       (cid, ) = self._session.draft.get_contacts()
+                       messageId = self._session.draft.get_message_id(cid)
+                       self._session.download_voicemail(messageId)
+                       self._hide()
+
+       @QtCore.pyqtSlot()
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_recipients_changed(self):
+               with qui_utils.notify_error(self._app.errorLog):
+                       self._update_state()
+
+       @QtCore.pyqtSlot(str, str)
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_voicemail_downloaded(self, messageId, filepath):
+               with qui_utils.notify_error(self._app.errorLog):
+                       self._update_state()
+
+
 class SMSEntryWindow(qwrappers.WindowWrapper):
 
        MAX_CHAR = 160
@@ -564,12 +809,14 @@ class SMSEntryWindow(qwrappers.WindowWrapper):
                self._history = QtGui.QLabel()
                self._history.setTextFormat(QtCore.Qt.RichText)
                self._history.setWordWrap(True)
+               self._voicemailPlayer = VoicemailPlayer(self._app, self._session, self._errorLog)
                self._smsEntry = QtGui.QTextEdit()
                self._smsEntry.textChanged.connect(self._on_letter_count_changed)
 
                self._entryLayout = QtGui.QVBoxLayout()
                self._entryLayout.addWidget(self._targetList.toplevel)
                self._entryLayout.addWidget(self._history)
+               self._entryLayout.addWidget(self._voicemailPlayer.toplevel, 0)
                self._entryLayout.addWidget(self._smsEntry)
                self._entryLayout.setContentsMargins(0, 0, 0, 0)
                self._entryWidget = QtGui.QWidget()
@@ -610,6 +857,7 @@ class SMSEntryWindow(qwrappers.WindowWrapper):
                self._window.setWindowTitle("Contact")
                self._window.closed.connect(self._on_close_window)
                self._window.hidden.connect(self._on_close_window)
+               self._window.resized.connect(self._on_window_resized)
 
                self._scrollTimer = QtCore.QTimer()
                self._scrollTimer.setInterval(100)
@@ -648,6 +896,7 @@ class SMSEntryWindow(qwrappers.WindowWrapper):
                self._session.draft.called.disconnect(self._on_op_finished)
                self._session.draft.cancelled.disconnect(self._on_op_finished)
                self._session.draft.error.disconnect(self._on_op_error)
+               self._voicemailPlayer.destroy()
                window = self._window
                self._window = None
                try:
@@ -805,12 +1054,13 @@ class SMSEntryWindow(qwrappers.WindowWrapper):
        @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)
+               with qui_utils.notify_error(self._app.errorLog):
+                       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)
@@ -865,6 +1115,12 @@ class SMSEntryWindow(qwrappers.WindowWrapper):
                        self._update_button_state()
 
        @QtCore.pyqtSlot()
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_window_resized(self, checked = True):
+               with qui_utils.notify_error(self._app.errorLog):
+                       self._scroll_to_bottom()
+
+       @QtCore.pyqtSlot()
        @QtCore.pyqtSlot(bool)
        @misc_utils.log_exception(_moduleLogger)
        def _on_close_window(self, checked = True):