import ConfigParser
import functools
import logging
+import logging.handlers
from PyQt4 import QtGui
from PyQt4 import QtCore
import constants
+import alarm_handler
+import led_handler
from util import qtpie
from util import qwrappers
from util import qui_utils
_moduleLogger = logging.getLogger(__name__)
-class LedWrapper(object):
-
- def __init__(self):
- self._ledHandler = None
- self._init = False
-
- def off(self):
- self._lazy_init()
- if self._ledHandler is not None:
- self._ledHandler.off()
-
- def _lazy_init(self):
- if self._init:
- return
- self._init = True
- try:
- import led_handler
- self._ledHandler = led_handler.LedHandler()
- except Exception, e:
- _moduleLogger.exception('Unable to initialize LED Handling: "%s"' % str(e))
- self._ledHandler = None
-
-
class Dialcentral(qwrappers.ApplicationWrapper):
_DATA_PATHS = [
def __init__(self, app):
self._dataPath = None
self._aboutDialog = None
- self._ledHandler = LedWrapper()
self.notifyOnMissed = False
self.notifyOnVoicemail = False
self.notifyOnSms = False
- try:
- import alarm_handler
- if alarm_handler.AlarmHandler is not alarm_handler._NoneAlarmHandler:
- self._alarmHandler = alarm_handler.AlarmHandler()
- else:
- self._alarmHandler = None
- except (ImportError, OSError):
- self._alarmHandler = None
- except Exception:
- _moduleLogger.exception("Notification failure")
- self._alarmHandler = None
- if self._alarmHandler is None:
- _moduleLogger.info("No notification support")
+ self._ledHandler = led_handler.LedHandler()
+ self._alarmHandler = alarm_handler.AlarmHandler()
qwrappers.ApplicationWrapper.__init__(self, app, constants)
except Exception:
_moduleLogger.exception("Unknown loading error")
- if self._alarmHandler is not None:
- try:
- self._alarmHandler.load_settings(config, "alarm")
- self.notifyOnMissed = config.getboolean("2 - Account Info", "notifyOnMissed")
- self.notifyOnVoicemail = config.getboolean("2 - Account Info", "notifyOnVoicemail")
- self.notifyOnSms = config.getboolean("2 - Account Info", "notifyOnSms")
- except ConfigParser.NoOptionError, e:
- _moduleLogger.info(
- "Settings file %s is missing option %s" % (
- constants._user_settings_,
- e.option,
- ),
- )
- except ConfigParser.NoSectionError, e:
- _moduleLogger.info(
- "Settings file %s is missing section %s" % (
- constants._user_settings_,
- e.section,
- ),
- )
- except Exception:
- _moduleLogger.exception("Unknown loading error")
+ try:
+ self._alarmHandler.load_settings(config, "alarm")
+ self.notifyOnMissed = config.getboolean("2 - Account Info", "notifyOnMissed")
+ self.notifyOnVoicemail = config.getboolean("2 - Account Info", "notifyOnVoicemail")
+ self.notifyOnSms = config.getboolean("2 - Account Info", "notifyOnSms")
+ except ConfigParser.NoOptionError, e:
+ _moduleLogger.info(
+ "Settings file %s is missing option %s" % (
+ constants._user_settings_,
+ e.option,
+ ),
+ )
+ except ConfigParser.NoSectionError, e:
+ _moduleLogger.info(
+ "Settings file %s is missing section %s" % (
+ constants._user_settings_,
+ e.section,
+ ),
+ )
+ except Exception:
+ _moduleLogger.exception("Unknown loading error")
creds = (
base64.b64decode(blob)
blob = base64.b64encode(value)
config.set(constants.__pretty_app_name__, "bin_blob_%i" % i, blob)
- if self._alarmHandler is not None:
- config.add_section("alarm")
- self._alarmHandler.save_settings(config, "alarm")
+ config.add_section("alarm")
+ self._alarmHandler.save_settings(config, "alarm")
config.add_section("2 - Account Info")
config.set("2 - Account Info", "notifyOnMissed", repr(self.notifyOnMissed))
config.set("2 - Account Info", "notifyOnVoicemail", repr(self.notifyOnVoicemail))
else:
return None
+ def get_resource(self, name):
+ if self._dataPath is None:
+ for path in self._DATA_PATHS:
+ if os.path.exists(os.path.join(path, name)):
+ self._dataPath = path
+ break
+ if self._dataPath is not None:
+ return os.path.join(self._dataPath, name)
+ else:
+ return None
+
def _close_windows(self):
qwrappers.ApplicationWrapper._close_windows(self)
if self._aboutDialog is not None:
else:
self._child.disable()
+ @property
+ def child(self):
+ return self._child
+
def enable(self):
self._isEnabled = True
if self._child is not None:
return gv_views.__dict__[tab](app, session, errorLog)
-class MainWindow(object):
+class MainWindow(qwrappers.WindowWrapper):
KEYPAD_TAB = 0
RECENT_TAB = 1
assert len(_TAB_SETTINGS_NAMES) == MAX_TABS
def __init__(self, parent, app):
- self._app = app
-
- self._errorLog = qui_utils.QErrorLog()
- self._errorDisplay = qui_utils.ErrorDisplay(self._errorLog)
+ qwrappers.WindowWrapper.__init__(self, parent, app)
+ self._window.setWindowTitle("%s" % constants.__pretty_app_name__)
+ self._errorLog = self._app.errorLog
self._session = session.Session(self._errorLog, constants._data_path_)
self._session.error.connect(self._on_session_error)
self._session.loggedIn.connect(self._on_login)
self._session.loggedOut.connect(self._on_logout)
self._session.draft.recipientsChanged.connect(self._on_recipients_changed)
+ self._session.newMessages.connect(self._on_new_message_alert)
+ self._app.alarmHandler.applicationNotifySignal.connect(self._on_app_alert)
self._defaultCredentials = "", ""
self._curentCredentials = "", ""
self._currentTab = 0
self._tabWidget.currentChanged.connect(self._on_tab_changed)
self._tabWidget.setContentsMargins(0, 0, 0, 0)
- self._layout = QtGui.QVBoxLayout()
- self._layout.setContentsMargins(0, 0, 0, 0)
- self._layout.addWidget(self._errorDisplay.toplevel)
self._layout.addWidget(self._tabWidget)
- centralWidget = QtGui.QWidget()
- centralWidget.setLayout(self._layout)
- centralWidget.setContentsMargins(0, 0, 0, 0)
-
- self._window = QtGui.QMainWindow(parent)
- self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
- qui_utils.set_stackable(self._window, True)
- self._window.setWindowTitle("%s" % constants.__pretty_app_name__)
- self._window.setCentralWidget(centralWidget)
-
self._loginTabAction = QtGui.QAction(None)
self._loginTabAction.setText("Login")
self._loginTabAction.triggered.connect(self._on_login_requested)
self._refreshTabAction.setShortcut(QtGui.QKeySequence("CTRL+r"))
self._refreshTabAction.triggered.connect(self._on_refresh)
- self._closeWindowAction = QtGui.QAction(None)
- self._closeWindowAction.setText("Close")
- self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w"))
- self._closeWindowAction.triggered.connect(self._on_close_window)
-
- if constants.IS_MAEMO:
- fileMenu = self._window.menuBar().addMenu("&File")
- fileMenu.addAction(self._loginTabAction)
- fileMenu.addAction(self._refreshTabAction)
+ fileMenu = self._window.menuBar().addMenu("&File")
+ fileMenu.addAction(self._loginTabAction)
+ fileMenu.addAction(self._refreshTabAction)
- toolsMenu = self._window.menuBar().addMenu("&Tools")
- toolsMenu.addAction(self._accountTabAction)
- toolsMenu.addAction(self._importTabAction)
- toolsMenu.addAction(self._app.aboutAction)
-
- self._window.addAction(self._closeWindowAction)
- self._window.addAction(self._app.quitAction)
- self._window.addAction(self._app.fullscreenAction)
- else:
- fileMenu = self._window.menuBar().addMenu("&File")
- fileMenu.addAction(self._loginTabAction)
- fileMenu.addAction(self._refreshTabAction)
- fileMenu.addAction(self._closeWindowAction)
- fileMenu.addAction(self._app.quitAction)
-
- viewMenu = self._window.menuBar().addMenu("&View")
- viewMenu.addAction(self._app.fullscreenAction)
-
- toolsMenu = self._window.menuBar().addMenu("&Tools")
- toolsMenu.addAction(self._accountTabAction)
- toolsMenu.addAction(self._importTabAction)
- toolsMenu.addAction(self._app.aboutAction)
-
- self._window.addAction(self._app.orientationAction)
- self._window.addAction(self._app.logAction)
+ toolsMenu = self._window.menuBar().addMenu("&Tools")
+ toolsMenu.addAction(self._accountTabAction)
+ toolsMenu.addAction(self._importTabAction)
+ toolsMenu.addAction(self._app.aboutAction)
self._initialize_tab(self._tabWidget.currentIndex())
self.set_fullscreen(self._app.fullscreenAction.isChecked())
self.set_orientation(self._app.orientationAction.isChecked())
- @property
- def window(self):
- return self._window
-
def set_default_credentials(self, username, password):
self._defaultCredentials = username, password
return self._defaultCredentials
def walk_children(self):
- return ()
+ if self._smsEntryDialog is not None:
+ return (self._smsEntryDialog, )
+ else:
+ return ()
def start(self):
+ qwrappers.WindowWrapper.start(self)
assert self._session.state == self._session.LOGGEDOUT_STATE, "Initialization messed up"
if self._defaultCredentials != ("", ""):
username, password = self._defaultCredentials[0], self._defaultCredentials[1]
self._prompt_for_login()
def close(self):
- for child in self.walk_children():
- child.window.destroyed.disconnect(self._on_child_close)
- child.close()
for diag in (
self._credentialsDialog,
- self._smsEntryDialog,
self._accountDialog,
):
if diag is not None:
diag.close()
+ for child in self.walk_children():
+ child.window.destroyed.disconnect(self._on_child_close)
+ child.window.closed.disconnect(self._on_child_close)
+ child.close()
self._window.close()
def destroy(self):
+ qwrappers.WindowWrapper.destroy(self)
if self._session.state != self._session.LOGGEDOUT_STATE:
self._session.logout()
for settingName, settingValue in tabSettings.iteritems():
config.set(sectionName, settingName, settingValue)
- def show(self):
- self._window.show()
- for child in self.walk_children():
- child.show()
-
- def hide(self):
- for child in self.walk_children():
- child.hide()
- self._window.hide()
-
- def set_fullscreen(self, isFullscreen):
- if isFullscreen:
- self._window.showFullScreen()
- else:
- self._window.showNormal()
- for child in self.walk_children():
- child.set_fullscreen(isFullscreen)
-
def set_orientation(self, isPortrait):
+ qwrappers.WindowWrapper.set_orientation(self, isPortrait)
if isPortrait:
self._tabWidget.setTabPosition(QtGui.QTabWidget.South)
- qui_utils.set_window_orientation(self.window, QtCore.Qt.Vertical)
else:
self._tabWidget.setTabPosition(QtGui.QTabWidget.West)
- qui_utils.set_window_orientation(self.window, QtCore.Qt.Horizontal)
- for child in (self._smsEntryDialog, ):
- if child is not None:
- child.set_orientation(isPortrait)
- for child in self.walk_children():
- child.set_orientation(isPortrait)
def _initialize_tab(self, index):
assert index < self.MAX_TABS, "Invalid tab"
if not self._tabsContents[index].has_child():
tab = self._TAB_CLASS[index](self._app, self._session, self._errorLog)
self._tabsContents[index].set_child(tab)
- self._tabsContents[index].refresh(force=False)
+ self._tabsContents[index].refresh(force=False)
def _prompt_for_login(self):
if self._credentialsDialog is None:
import dialogs
self._credentialsDialog = dialogs.CredentialsDialog(self._app)
- username, password = self._credentialsDialog.run(
+ credentials = self._credentialsDialog.run(
self._defaultCredentials[0], self._defaultCredentials[1], self.window
)
+ if credentials is None:
+ return
+ username, password = credentials
self._curentCredentials = username, password
self._session.login(username, password)
if self._accountDialog is None:
import dialogs
self._accountDialog = dialogs.AccountDialog(self._app)
- if self._app.alarmHandler is None:
- self._accountDialog.setIfNotificationsSupported(False)
- if self._app.alarmHandler is not None:
- self._accountDialog.notifications = self._app.alarmHandler.isEnabled
- self._accountDialog.notificationTime = self._app.alarmHandler.recurrence
- self._accountDialog.notifyOnMissed = self._app.notifyOnMissed
- self._accountDialog.notifyOnVoicemail = self._app.notifyOnVoicemail
- self._accountDialog.notifyOnSms = self._app.notifyOnSms
+ self._accountDialog.setIfNotificationsSupported(self._app.alarmHandler.backgroundNotificationsSupported)
+ self._accountDialog.notifications = self._app.alarmHandler.alarmType
+ self._accountDialog.notificationTime = self._app.alarmHandler.recurrence
+ self._accountDialog.notifyOnMissed = self._app.notifyOnMissed
+ self._accountDialog.notifyOnVoicemail = self._app.notifyOnVoicemail
+ self._accountDialog.notifyOnSms = self._app.notifyOnSms
self._accountDialog.set_callbacks(
self._session.get_callback_numbers(), self._session.get_callback_number()
)
- self._accountDialog.accountNumber = self._session.get_account_number()
+ accountNumberToDisplay = self._session.get_account_number()
+ if not accountNumberToDisplay:
+ accountNumberToDisplay = "Not Available (%s)" % self._session.state
+ self._accountDialog.set_account_number(accountNumberToDisplay)
response = self._accountDialog.run(self.window)
if response == QtGui.QDialog.Accepted:
if self._accountDialog.doClear:
self._session.logout_and_clear()
+ self._defaultCredentials = "", ""
+ self._curentCredentials = "", ""
+ for tab in self._tabsContents:
+ tab.disable()
else:
callbackNumber = self._accountDialog.selectedCallback
self._session.set_callback_number(callbackNumber)
- if self._app.alarmHandler is not None:
- self._app.alarmHandler.apply_settings(self._accountDialog.notifications, self._accountDialog.notificationTime)
- self._app.notifyOnMissed = self._accountDialog.notifyOnMissed
- self._app.notifyOnVoicemail = self._accountDialog.notifyOnVoicemail
- self._app.notifyOnSms = self._accountDialog.notifyOnSms
+
+ if (
+ self._accountDialog.notifyOnMissed or
+ self._accountDialog.notifyOnVoicemail or
+ self._accountDialog.notifyOnSms
+ ):
+ notifications = self._accountDialog.notifications
+ else:
+ notifications = self._accountDialog.ALARM_NONE
+ self._app.alarmHandler.apply_settings(notifications, self._accountDialog.notificationTime)
+
+ self._app.notifyOnMissed = self._accountDialog.notifyOnMissed
+ self._app.notifyOnVoicemail = self._accountDialog.notifyOnVoicemail
+ self._app.notifyOnSms = self._accountDialog.notifyOnSms
+ self._app.save_settings()
elif response == QtGui.QDialog.Rejected:
_moduleLogger.info("Cancelled")
else:
_moduleLogger.info("Unknown response")
+ @QtCore.pyqtSlot()
+ @misc_utils.log_exception(_moduleLogger)
+ def _on_new_message_alert(self):
+ with qui_utils.notify_error(self._errorLog):
+ if self._app.alarmHandler.alarmType == self._app.alarmHandler.ALARM_APPLICATION:
+ if self._currentTab == self.MESSAGES_TAB or not self._app.ledHandler.isReal:
+ self._errorLog.push_message("New messages available")
+ else:
+ self._app.ledHandler.on()
+
@QtCore.pyqtSlot(str)
@misc_utils.log_exception(_moduleLogger)
def _on_session_error(self, message):
for tab in self._tabsContents:
tab.enable()
+ self._initialize_tab(self._currentTab)
@QtCore.pyqtSlot()
@misc_utils.log_exception(_moduleLogger)
@QtCore.pyqtSlot()
@misc_utils.log_exception(_moduleLogger)
+ def _on_app_alert(self):
+ with qui_utils.notify_error(self._errorLog):
+ if self._session.state == self._session.LOGGEDIN_STATE:
+ messageType = {
+ (True, True): self._session.MESSAGE_ALL,
+ (True, False): self._session.MESSAGE_TEXTS,
+ (False, True): self._session.MESSAGE_VOICEMAILS,
+ }[(self._app.notifyOnSms, self._app.notifyOnVoicemail)]
+ self._session.update_messages(messageType, force=True)
+
+ @QtCore.pyqtSlot()
+ @misc_utils.log_exception(_moduleLogger)
def _on_recipients_changed(self):
with qui_utils.notify_error(self._errorLog):
if self._session.draft.get_num_contacts() == 0:
if self._smsEntryDialog is None:
import dialogs
self._smsEntryDialog = dialogs.SMSEntryWindow(self.window, self._app, self._session, self._errorLog)
+ self._smsEntryDialog.window.destroyed.connect(self._on_child_close)
+ self._smsEntryDialog.window.closed.connect(self._on_child_close)
+ self._smsEntryDialog.window.show()
+
+ @misc_utils.log_exception(_moduleLogger)
+ def _on_child_close(self, obj = None):
+ self._smsEntryDialog = None
@QtCore.pyqtSlot()
@QtCore.pyqtSlot(bool)
with qui_utils.notify_error(self._errorLog):
self._currentTab = index
self._initialize_tab(index)
+ if self._app.alarmHandler.alarmType == self._app.alarmHandler.ALARM_APPLICATION:
+ self._app.ledHandler.off()
@QtCore.pyqtSlot()
@QtCore.pyqtSlot(bool)
def _on_import(self, checked = True):
with qui_utils.notify_error(self._errorLog):
csvName = QtGui.QFileDialog.getOpenFileName(self._window, caption="Import", filter="CSV Files (*.csv)")
+ csvName = unicode(csvName)
if not csvName:
return
import shutil
shutil.copy2(csvName, self._app.fsContactsPath)
- self._tabsContents[self.CONTACTS_TAB].update_addressbooks()
+ if self._tabsContents[self.CONTACTS_TAB].has_child:
+ self._tabsContents[self.CONTACTS_TAB].child.update_addressbooks()
@QtCore.pyqtSlot()
@QtCore.pyqtSlot(bool)
@misc_utils.log_exception(_moduleLogger)
def _on_account(self, checked = True):
with qui_utils.notify_error(self._errorLog):
+ assert self._session.state == self._session.LOGGEDIN_STATE, "Must be logged in for settings"
self._show_account_dialog()
- @QtCore.pyqtSlot()
- @QtCore.pyqtSlot(bool)
- @misc_utils.log_exception(_moduleLogger)
- def _on_close_window(self, checked = True):
- self.close()
-
def run():
+ try:
+ os.makedirs(constants._data_path_)
+ except OSError, e:
+ if e.errno != 17:
+ raise
+
+ logFormat = '(%(relativeCreated)5d) %(levelname)-5s %(threadName)s.%(name)s.%(funcName)s: %(message)s'
+ logging.basicConfig(level=logging.DEBUG, format=logFormat)
+ rotating = logging.handlers.RotatingFileHandler(constants._user_logpath_, maxBytes=512*1024, backupCount=1)
+ rotating.setFormatter(logging.Formatter(logFormat))
+ root = logging.getLogger()
+ root.addHandler(rotating)
+ _moduleLogger.info("%s %s-%s" % (constants.__app_name__, constants.__version__, constants.__build__))
+ _moduleLogger.info("OS: %s" % (os.uname()[0], ))
+ _moduleLogger.info("Kernel: %s (%s) for %s" % os.uname()[2:])
+ _moduleLogger.info("Hostname: %s" % os.uname()[1])
+
app = QtGui.QApplication([])
handle = Dialcentral(app)
qtpie.init_pies()
if __name__ == "__main__":
import sys
- logFormat = '(%(relativeCreated)5d) %(levelname)-5s %(threadName)s.%(name)s.%(funcName)s: %(message)s'
- logging.basicConfig(level=logging.DEBUG, format=logFormat)
- try:
- os.makedirs(constants._data_path_)
- except OSError, e:
- if e.errno != 17:
- raise
-
val = run()
sys.exit(val)