Bump to 1.2.30
[gc-dialer] / src / session.py
index 8dbdba0..7697bf1 100644 (file)
@@ -15,6 +15,7 @@ except ImportError:
 from PyQt4 import QtCore
 
 from util import qore_utils
+from util import qui_utils
 from util import concurrent
 from util import misc as misc_utils
 
@@ -24,15 +25,6 @@ import constants
 _moduleLogger = logging.getLogger(__name__)
 
 
-@contextlib.contextmanager
-def notify_busy(log, message):
-       log.push_busy(message)
-       try:
-               yield
-       finally:
-               log.pop(message)
-
-
 class _DraftContact(object):
 
        def __init__(self, title, description, numbersWithDescriptions):
@@ -60,15 +52,19 @@ class Draft(QtCore.QObject):
                self._contacts = {}
                self._pool = pool
                self._backend = backend
+               self._busyReason = None
+               self._message = ""
 
-       def send(self, text):
+       def send(self):
                assert 0 < len(self._contacts), "No contacts selected"
+               assert 0 < len(self._message), "No message to send"
                numbers = [misc_utils.make_ugly(contact.selectedNumber) for contact in self._contacts.itervalues()]
                le = concurrent.AsyncLinearExecution(self._pool, self._send)
-               le.start(numbers, text)
+               le.start(numbers, self._message)
 
        def call(self):
                assert len(self._contacts) == 1, "Must select 1 and only 1 contact"
+               assert len(self._message) == 0, "Cannot send message with call"
                (contact, ) = self._contacts.itervalues()
                number = misc_utils.make_ugly(contact.selectedNumber)
                le = concurrent.AsyncLinearExecution(self._pool, self._call)
@@ -78,7 +74,17 @@ class Draft(QtCore.QObject):
                le = concurrent.AsyncLinearExecution(self._pool, self._cancel)
                le.start()
 
+       def _get_message(self):
+               return self._message
+
+       def _set_message(self, message):
+               self._message = message
+
+       message = property(_get_message, _set_message)
+
        def add_contact(self, contactId, title, description, numbersWithDescriptions):
+               if self._busyReason is not None:
+                       raise RuntimeError("Please wait for %r" % self._busyReason)
                if contactId in self._contacts:
                        _moduleLogger.info("Adding duplicate contact %r" % contactId)
                        # @todo Remove this evil hack to re-popup the dialog
@@ -89,6 +95,8 @@ class Draft(QtCore.QObject):
                self.recipientsChanged.emit()
 
        def remove_contact(self, contactId):
+               if self._busyReason is not None:
+                       raise RuntimeError("Please wait for %r" % self._busyReason)
                assert contactId in self._contacts, "Contact missing"
                del self._contacts[contactId]
                self.recipientsChanged.emit()
@@ -118,43 +126,61 @@ class Draft(QtCore.QObject):
                self._contacts[cid].selectedNumber = number
 
        def clear(self):
+               if self._busyReason is not None:
+                       raise RuntimeError("Please wait for %r" % self._busyReason)
+               self._clear()
+
+       def _clear(self):
                oldContacts = self._contacts
                self._contacts = {}
+               self._message = ""
                if oldContacts:
                        self.recipientsChanged.emit()
 
+       @contextlib.contextmanager
+       def _busy(self, message):
+               if self._busyReason is not None:
+                       raise RuntimeError("Already busy doing %r" % self._busyReason)
+               try:
+                       self._busyReason = message
+                       yield
+               finally:
+                       self._busyReason = None
+
        def _send(self, numbers, text):
                self.sendingMessage.emit()
                try:
-                       with notify_busy(self._errorLog, "Sending Text"):
-                               yield (
-                                       self._backend[0].send_sms,
-                                       (numbers, text),
-                                       {},
-                               )
-                       self.sentMessage.emit()
-                       self.clear()
+                       with self._busy("Sending Text"):
+                               with qui_utils.notify_busy(self._errorLog, "Sending Text"):
+                                       yield (
+                                               self._backend[0].send_sms,
+                                               (numbers, text),
+                                               {},
+                                       )
+                               self.sentMessage.emit()
+                               self._clear()
                except Exception, e:
                        self.error.emit(str(e))
 
        def _call(self, number):
                self.calling.emit()
                try:
-                       with notify_busy(self._errorLog, "Calling"):
-                               yield (
-                                       self._backend[0].call,
-                                       (number, ),
-                                       {},
-                               )
-                       self.called.emit()
-                       self.clear()
+                       with self._busy("Calling"):
+                               with qui_utils.notify_busy(self._errorLog, "Calling"):
+                                       yield (
+                                               self._backend[0].call,
+                                               (number, ),
+                                               {},
+                                       )
+                               self.called.emit()
+                               self._clear()
                except Exception, e:
                        self.error.emit(str(e))
 
        def _cancel(self):
                self.cancelling.emit()
                try:
-                       with notify_busy(self._errorLog, "Cancelling"):
+                       with qui_utils.notify_busy(self._errorLog, "Cancelling"):
                                yield (
                                        self._backend[0].cancel,
                                        (),
@@ -308,7 +334,7 @@ class Session(QtCore.QObject):
                assert self.state == self.LOGGEDIN_STATE, "DND requires being logged in (currently %s" % self.state
                oldDnd = self._dnd
                try:
-                       with notify_busy(self._errorLog, "Setting DND Status"):
+                       with qui_utils.notify_busy(self._errorLog, "Setting DND Status"):
                                yield (
                                        self._backend[0].set_dnd,
                                        (dnd, ),
@@ -357,7 +383,7 @@ class Session(QtCore.QObject):
                        self.callbackNumberChanged.emit(self._callback)
 
        def _login(self, username, password):
-               with notify_busy(self._errorLog, "Logging In"):
+               with qui_utils.notify_busy(self._errorLog, "Logging In"):
                        self._loggedInTime = self._LOGGINGIN_TIME
                        self.stateChange.emit(self.LOGGINGIN_STATE)
                        finalState = self.LOGGEDOUT_STATE
@@ -523,11 +549,11 @@ class Session(QtCore.QObject):
                oldCallback = self._callback
 
                self._contacts = {}
-               self._contactUpdateTime = datetime.datetime(1, 1, 1)
+               self._contactUpdateTime = datetime.datetime(1971, 1, 1)
                self._messages = []
-               self._messageUpdateTime = datetime.datetime(1, 1, 1)
+               self._messageUpdateTime = datetime.datetime(1971, 1, 1)
                self._history = []
-               self._historyUpdateTime = datetime.datetime(1, 1, 1)
+               self._historyUpdateTime = datetime.datetime(1971, 1, 1)
                self._dnd = False
                self._callback = ""
 
@@ -546,7 +572,7 @@ class Session(QtCore.QObject):
 
        def _update_contacts(self):
                try:
-                       with notify_busy(self._errorLog, "Updating Contacts"):
+                       with qui_utils.notify_busy(self._errorLog, "Updating Contacts"):
                                self._contacts = yield (
                                        self._backend[0].get_contacts,
                                        (),
@@ -560,7 +586,7 @@ class Session(QtCore.QObject):
 
        def _update_messages(self):
                try:
-                       with notify_busy(self._errorLog, "Updating Messages"):
+                       with qui_utils.notify_busy(self._errorLog, "Updating Messages"):
                                self._messages = yield (
                                        self._backend[0].get_messages,
                                        (),
@@ -574,7 +600,7 @@ class Session(QtCore.QObject):
 
        def _update_history(self):
                try:
-                       with notify_busy(self._errorLog, "Updating History"):
+                       with qui_utils.notify_busy(self._errorLog, "Updating History"):
                                self._history = yield (
                                        self._backend[0].get_recent,
                                        (),