Bump to 1.3.11-2
[gc-dialer] / dialcentral / alarm_handler.py
index a79f992..ce19e58 100644 (file)
@@ -1,15 +1,19 @@
 #!/usr/bin/env python
 
+from __future__ import with_statement
+
 import os
 import time
 import datetime
 import ConfigParser
 import logging
 
-import util.qt_compat as qt_compat
-QtCore = qt_compat.QtCore
 import dbus
 
+import dialcentral.util.qt_compat as qt_compat
+QtCore = qt_compat.QtCore
+import dialcentral.util.linux as linux_utils
+
 
 _FREMANTLE_ALARM = "Fremantle"
 _DIABLO_ALARM = "Diablo"
@@ -94,7 +98,7 @@ class _FremantleAlarmHandler(object):
        _INVALID_COOKIE = -1
        _REPEAT_FOREVER = -1
        _TITLE = "Dialcentral Notifications"
-       _LAUNCHER = os.path.abspath(os.path.join(os.path.dirname(__file__), "alarm_notify.py"))
+       _LAUNCHER = "python %s" % os.path.abspath(os.path.join(os.path.dirname(__file__), "alarm_notify.py"))
 
        def __init__(self):
                self._recurrence = 5
@@ -102,6 +106,10 @@ class _FremantleAlarmHandler(object):
                self._alarmCookie = self._INVALID_COOKIE
                self._launcher = self._LAUNCHER
 
+       @property
+       def alarmCookie(self):
+               return self._alarmCookie
+
        def load_settings(self, config, sectionName):
                try:
                        self._recurrence = config.getint(sectionName, "recurrence")
@@ -176,7 +184,7 @@ class _DiabloAlarmHandler(object):
 
        _INVALID_COOKIE = -1
        _TITLE = "Dialcentral Notifications"
-       _LAUNCHER = os.path.abspath(os.path.join(os.path.dirname(__file__), "alarm_notify.py"))
+       _LAUNCHER = "python %s" % os.path.abspath(os.path.join(os.path.dirname(__file__), "alarm_notify.py"))
        _REPEAT_FOREVER = -1
 
        def __init__(self):
@@ -187,6 +195,10 @@ class _DiabloAlarmHandler(object):
                self._alarmCookie = self._INVALID_COOKIE
                self._launcher = self._LAUNCHER
 
+       @property
+       def alarmCookie(self):
+               return self._alarmCookie
+
        def load_settings(self, config, sectionName):
                try:
                        self._recurrence = config.getint(sectionName, "recurrence")
@@ -272,6 +284,10 @@ class _ApplicationAlarmHandler(object):
                self._timer.setSingleShot(False)
                self._timer.setInterval(5 * self._MIN_TO_MS_FACTORY)
 
+       @property
+       def alarmCookie(self):
+               return 0
+
        def load_settings(self, config, sectionName):
                try:
                        self._timer.setInterval(config.getint(sectionName, "recurrence") * self._MIN_TO_MS_FACTORY)
@@ -310,6 +326,10 @@ class _NoneAlarmHandler(object):
                self._enabled = False
                self._recurrence = 5
 
+       @property
+       def alarmCookie(self):
+               return 0
+
        def load_settings(self, config, sectionName):
                try:
                        self._recurrence = config.getint(sectionName, "recurrence")
@@ -411,15 +431,19 @@ class AlarmHandler(object):
        def isEnabled(self):
                return self._currentAlarmType != self.ALARM_NONE
 
+       @property
+       def alarmCookie(self):
+               return self._alarms[self._currentAlarmType].alarmCookie
+
        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():
+def run():
        logFormat = '(%(relativeCreated)5d) %(levelname)-5s %(threadName)s.%(name)s.%(funcName)s: %(message)s'
        logging.basicConfig(level=logging.DEBUG, format=logFormat)
-       import constants
+       from dialcentral import constants
        try:
                import optparse
        except ImportError:
@@ -432,29 +456,33 @@ def main():
        parser.add_option("-r", "--recurrence", action="store", type="int", dest="recurrence", help="How often the alarm occurs", default=5)
        (commandOptions, commandArgs) = parser.parse_args()
 
+       settingsPath = linux_utils.get_resource_path("config", constants.__app_name__, "settings.ini")
+
        alarmHandler = AlarmHandler()
        config = ConfigParser.SafeConfigParser()
-       config.read(constants._user_settings_)
+       config.read(settingsPath)
        alarmHandler.load_settings(config, "alarm")
 
        if commandOptions.display:
                print "Alarm (%s) is %s for every %d minutes" % (
-                       alarmHandler._alarmCookie,
+                       alarmHandler.alarmCookie,
                        "enabled" if alarmHandler.isEnabled else "disabled",
                        alarmHandler.recurrence,
                )
        else:
                isEnabled = commandOptions.enabled
                recurrence = commandOptions.recurrence
-               alarmHandler.apply_settings(isEnabled, recurrence)
+
+               if alarmHandler.backgroundNotificationsSupported:
+                       enableType = AlarmHandler.ALARM_BACKGROUND
+               else:
+                       enableType = AlarmHandler.ALARM_APPLICATION
+               alarmHandler.apply_settings(enableType if isEnabled else AlarmHandler.ALARM_NONE, recurrence)
 
                alarmHandler.save_settings(config, "alarm")
-               configFile = open(constants._user_settings_, "wb")
-               try:
+               with open(settingsPath, "wb") as configFile:
                        config.write(configFile)
-               finally:
-                       configFile.close()
 
 
 if __name__ == "__main__":
-       main()
+       run()