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
_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):
self._contacts = {}
self._pool = pool
self._backend = backend
+ self._busyReason = None
def send(self, text):
assert 0 < len(self._contacts), "No contacts selected"
le.start()
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
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()
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 = {}
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,
(),
return self._draft
def login(self, username, password):
- assert self.state == self.LOGGEDOUT_STATE, "Can only log-in when logged out"
+ assert self.state == self.LOGGEDOUT_STATE, "Can only log-in when logged out (currently %s" % self.state
assert username != "", "No username specified"
if self._cachePath is not None:
cookiePath = os.path.join(self._cachePath, "%s.cookies" % username)
le.start(username, password)
def logout(self):
- assert self.state != self.LOGGEDOUT_STATE, "Can only logout if logged in"
+ assert self.state != self.LOGGEDOUT_STATE, "Can only logout if logged in (currently %s" % self.state
self._pool.stop()
self._loggedInTime = self._LOGGEDOUT_TIME
self._backend[0].persist()
self._save_to_cache()
def clear(self):
- assert self.state == self.LOGGEDOUT_STATE, "Can only clear when logged out"
+ assert self.state == self.LOGGEDOUT_STATE, "Can only clear when logged out (currently %s" % self.state
self._backend[0].logout()
del self._backend[0]
self._clear_cache()
self._draft.clear()
def logout_and_clear(self):
- assert self.state != self.LOGGEDOUT_STATE, "Can only logout if logged in"
+ assert self.state != self.LOGGEDOUT_STATE, "Can only logout if logged in (currently %s" % self.state
self._pool.stop()
self._loggedInTime = self._LOGGEDOUT_TIME
self.clear()
def _set_dnd(self, dnd):
# I'm paranoid about our state geting out of sync so we set no matter
# what but act as if we have the cannonical state
- assert self.state == self.LOGGEDIN_STATE, "DND requires being logged in"
+ 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, ),
def _set_callback_number(self, callback):
# I'm paranoid about our state geting out of sync so we set no matter
# what but act as if we have the cannonical state
- assert self.state == self.LOGGEDIN_STATE, "Callbacks configurable only when logged in"
+ assert self.state == self.LOGGEDIN_STATE, "Callbacks configurable only when logged in (currently %s" % self.state
oldCallback = self._callback
try:
- with notify_busy(self._errorLog, "Setting Callback"):
- yield (
- self._backend[0].set_callback_number,
- (callback, ),
- {},
- )
+ yield (
+ self._backend[0].set_callback_number,
+ (callback, ),
+ {},
+ )
except Exception, e:
self.error.emit(str(e))
return
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
del self._loginOps[:]
for asyncOp in loginOps:
asyncOp.start()
+ else:
+ self._loggedInTime = self._LOGGEDOUT_TIME
+ self.error.emit("Error logging in")
except Exception, e:
self._loggedInTime = self._LOGGEDOUT_TIME
self.error.emit(str(e))
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,
(),
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,
(),
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,
(),