Removing a warning on startup
[gc-dialer] / src / alarm_handler.py
index 779804a..ddcd955 100644 (file)
@@ -4,7 +4,9 @@ import os
 import time
 import datetime
 import ConfigParser
+import logging
 
+from PyQt4 import QtCore
 import dbus
 
 
@@ -16,14 +18,17 @@ _NO_ALARM = "None"
 try:
        import alarm
        ALARM_TYPE = _FREMANTLE_ALARM
-except ImportError:
+except (ImportError, OSError):
        try:
                import osso.alarmd as alarmd
                ALARM_TYPE = _DIABLO_ALARM
-       except ImportError:
+       except (ImportError, OSError):
                ALARM_TYPE = _NO_ALARM
 
 
+_moduleLogger = logging.getLogger(__name__)
+
+
 def _get_start_time(recurrence):
        now = datetime.datetime.now()
        startTimeMinute = now.minute + max(recurrence, 5) # being safe
@@ -105,12 +110,19 @@ class _FremantleAlarmHandler(object):
                                self._launcher = launcher
                except ConfigParser.NoOptionError:
                        pass
+               except ConfigParser.NoSectionError:
+                       pass
 
        def save_settings(self, config, sectionName):
-               config.set(sectionName, "recurrence", str(self._recurrence))
-               config.set(sectionName, "alarmCookie", str(self._alarmCookie))
-               launcher = self._launcher if self._launcher != self._LAUNCHER else ""
-               config.set(sectionName, "notifier", launcher)
+               try:
+                       config.set(sectionName, "recurrence", str(self._recurrence))
+                       config.set(sectionName, "alarmCookie", str(self._alarmCookie))
+                       launcher = self._launcher if self._launcher != self._LAUNCHER else ""
+                       config.set(sectionName, "notifier", launcher)
+               except ConfigParser.NoOptionError:
+                       pass
+               except ConfigParser.NoSectionError:
+                       pass
 
        def apply_settings(self, enabled, recurrence):
                if recurrence != self._recurrence or enabled != self.isEnabled:
@@ -134,16 +146,14 @@ class _FremantleAlarmHandler(object):
 
                event = alarm.Event()
                event.appid = self._TITLE
-               event.message = "GoogleVoice updates"
                event.alarm_time = alarmTime
-               event.recurrences_left = -1
+               event.recurrences_left = self._REPEAT_FOREVER
 
-               action = event.add_action(1)[0]
+               action = event.add_actions(1)[0]
                action.flags |= alarm.ACTION_TYPE_EXEC | alarm.ACTION_WHEN_TRIGGERED
                action.command = self._launcher
 
-               recurrence = event.add_recurrence(1)[0]
-               recurrence.recurrences_left = self._REPEAT_FOREVER
+               recurrence = event.add_recurrences(1)[0]
                recurrence.mask_min |= _create_recurrence_mask(recurrenceMins, 60)
                recurrence.mask_hour |= alarm.RECUR_HOUR_DONTCARE
                recurrence.mask_mday |= alarm.RECUR_MDAY_DONTCARE
@@ -185,6 +195,8 @@ class _DiabloAlarmHandler(object):
                                self._launcher = launcher
                except ConfigParser.NoOptionError:
                        pass
+               except ConfigParser.NoSectionError:
+                       pass
 
        def save_settings(self, config, sectionName):
                config.set(sectionName, "recurrence", str(self._recurrence))
@@ -249,38 +261,163 @@ class _DiabloAlarmHandler(object):
                assert deleteResult != -1, "Deleting of alarm event failed"
 
 
+class _ApplicationAlarmHandler(object):
+
+       _REPEAT_FOREVER = -1
+       _MIN_TO_MS_FACTORY = 1000 * 60
+
+       def __init__(self):
+               self._timer = QtCore.QTimer()
+               self._timer.setSingleShot(False)
+               self._timer.setInterval(5 * self._MIN_TO_MS_FACTORY)
+
+       def load_settings(self, config, sectionName):
+               try:
+                       self._timer.setInterval(config.getint(sectionName, "recurrence") * self._MIN_TO_MS_FACTORY)
+               except ConfigParser.NoOptionError:
+                       pass
+               except ConfigParser.NoSectionError:
+                       pass
+               self._timer.start()
+
+       def save_settings(self, config, sectionName):
+               config.set(sectionName, "recurrence", str(self.recurrence))
+
+       def apply_settings(self, enabled, recurrence):
+               self._timer.setInterval(recurrence * self._MIN_TO_MS_FACTORY)
+               if enabled:
+                       self._timer.start()
+               else:
+                       self._timer.stop()
+
+       @property
+       def notifySignal(self):
+               return self._timer.timeout
+
+       @property
+       def recurrence(self):
+               return int(self._timer.interval() / self._MIN_TO_MS_FACTORY)
+
+       @property
+       def isEnabled(self):
+               return self._timer.isActive()
+
+
 class _NoneAlarmHandler(object):
 
        def __init__(self):
-               pass
+               self._enabled = False
+               self._recurrence = 5
 
        def load_settings(self, config, sectionName):
-               pass
+               try:
+                       self._recurrence = config.getint(sectionName, "recurrence")
+                       self._enabled = True
+               except ConfigParser.NoOptionError:
+                       pass
+               except ConfigParser.NoSectionError:
+                       pass
 
        def save_settings(self, config, sectionName):
-               pass
+               config.set(sectionName, "recurrence", str(self.recurrence))
 
        def apply_settings(self, enabled, recurrence):
-               pass
+               self._enabled = enabled
 
        @property
        def recurrence(self):
-               return 0
+               return self._recurrence
 
        @property
        def isEnabled(self):
-               return False
+               return self._enabled
 
 
-AlarmHandler = {
+_BACKGROUND_ALARM_FACTORY = {
        _FREMANTLE_ALARM: _FremantleAlarmHandler,
        _DIABLO_ALARM: _DiabloAlarmHandler,
-       _NO_ALARM: _NoneAlarmHandler,
+       _NO_ALARM: None,
 }[ALARM_TYPE]
 
 
+class AlarmHandler(object):
+
+       ALARM_NONE = "No Alert"
+       ALARM_BACKGROUND = "Background Alert"
+       ALARM_APPLICATION = "Application Alert"
+       ALARM_TYPES = [ALARM_NONE, ALARM_BACKGROUND, ALARM_APPLICATION]
+
+       ALARM_FACTORY = {
+               ALARM_NONE: _NoneAlarmHandler,
+               ALARM_BACKGROUND: _BACKGROUND_ALARM_FACTORY,
+               ALARM_APPLICATION: _ApplicationAlarmHandler,
+       }
+
+       def __init__(self):
+               self._alarms = {self.ALARM_NONE: _NoneAlarmHandler()}
+               self._currentAlarmType = self.ALARM_NONE
+
+       def load_settings(self, config, sectionName):
+               try:
+                       self._currentAlarmType = config.get(sectionName, "alarm")
+               except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
+                       _moduleLogger.exception("Falling back to old style")
+                       self._currentAlarmType = self.ALARM_BACKGROUND
+               if self._currentAlarmType not in self.ALARM_TYPES:
+                       self._currentAlarmType = self.ALARM_NONE
+
+               self._init_alarm(self._currentAlarmType)
+               if self._currentAlarmType in self._alarms:
+                       self._alarms[self._currentAlarmType].load_settings(config, sectionName)
+                       if not self._alarms[self._currentAlarmType].isEnabled:
+                               _moduleLogger.info("Config file lied, not actually enabled")
+                               self._currentAlarmType = self.ALARM_NONE
+               else:
+                       _moduleLogger.info("Background alerts not supported")
+                       self._currentAlarmType = self.ALARM_NONE
+
+       def save_settings(self, config, sectionName):
+               config.set(sectionName, "alarm", self._currentAlarmType)
+               self._alarms[self._currentAlarmType].save_settings(config, sectionName)
+
+       def apply_settings(self, t, recurrence):
+               self._init_alarm(t)
+               newHandler = self._alarms[t]
+               oldHandler = self._alarms[self._currentAlarmType]
+               if newHandler != oldHandler:
+                       oldHandler.apply_settings(False, 0)
+               newHandler.apply_settings(True, recurrence)
+               self._currentAlarmType = t
+
+       @property
+       def alarmType(self):
+               return self._currentAlarmType
+
+       @property
+       def backgroundNotificationsSupported(self):
+               return self.ALARM_FACTORY[self.ALARM_BACKGROUND] is not None
+
+       @property
+       def applicationNotifySignal(self):
+               self._init_alarm(self.ALARM_APPLICATION)
+               return self._alarms[self.ALARM_APPLICATION].notifySignal
+
+       @property
+       def recurrence(self):
+               return self._alarms[self._currentAlarmType].recurrence
+
+       @property
+       def isEnabled(self):
+               return self._currentAlarmType != self.ALARM_NONE
+
+       def _init_alarm(self, t):
+               if t not in self._alarms and self.ALARM_FACTORY[t] is not None:
+                       self._alarms[t] = self.ALARM_FACTORY[t]()
+
+
 def main():
-       import ConfigParser
+       logFormat = '(%(relativeCreated)5d) %(levelname)-5s %(threadName)s.%(name)s.%(funcName)s: %(message)s'
+       logging.basicConfig(level=logging.DEBUG, format=logFormat)
        import constants
        try:
                import optparse