Protection against 0 timeout and a spelling correction
[gc-dialer] / src / alarm_handler.py
1 #!/usr/bin/env python
2
3 import os
4 import time
5 import datetime
6
7 import dbus
8 import osso.alarmd as alarmd
9
10
11 class AlarmHandler(object):
12
13         _INVALID_COOKIE = -1
14         _TITLE = "Dialcentral Notifications"
15         _LAUNCHER = os.path.abspath(os.path.join(os.path.dirname(__file__), "alarm_notify.py"))
16         _REPEAT_FOREVER = -1
17         _DEFAULT_FLAGS = (
18                 alarmd.ALARM_EVENT_NO_DIALOG |
19                 alarmd.ALARM_EVENT_NO_SNOOZE |
20                 alarmd.ALARM_EVENT_CONNECTED
21         )
22
23         def __init__(self):
24                 self._recurrence = 5
25
26                 bus = dbus.SystemBus()
27                 self._alarmdDBus = bus.get_object("com.nokia.alarmd", "/com/nokia/alarmd");
28                 self._alarmCookie = self._INVALID_COOKIE
29
30         def load_settings(self, config, sectionName):
31                 self._recurrence = config.getint(sectionName, "recurrence")
32                 self._alarmCookie = config.getint(sectionName, "alarmCookie")
33
34         def save_settings(self, config, sectionName):
35                 config.set(sectionName, "recurrence", str(self._recurrence))
36                 config.set(sectionName, "alarmCookie", str(self._alarmCookie))
37
38         def apply_settings(self, enabled, recurrence):
39                 if recurrence != self._recurrence or enabled != self.isEnabled:
40                         if self.isEnabled:
41                                 self._clear_alarm()
42                         if enabled:
43                                 self._set_alarm(recurrence)
44                 self._recurrence = int(recurrence)
45
46         @property
47         def recurrence(self):
48                 return self._recurrence
49
50         @property
51         def isEnabled(self):
52                 return self._alarmCookie != self._INVALID_COOKIE
53
54         def _get_start_time(self, recurrence):
55                 now = datetime.datetime.now()
56                 startTimeMinute = now.minute + recurrence
57                 now.replace(minute=startTimeMinute)
58                 timestamp = int(time.mktime(now.timetuple()))
59                 return timestamp
60
61         def _set_alarm(self, recurrence):
62                 assert 1 <= recurrence, "Notifications set to occur too frequently: %d" % recurrence
63                 alarmTime = self._get_start_time(recurrence)
64
65                 #Setup the alarm arguments so that they can be passed to the D-Bus add_event method
66                 action = []
67                 action.extend(['flags', self._DEFAULT_FLAGS])
68                 action.extend(['title', self._TITLE])
69                 action.extend(['path', self._LAUNCHER])
70                 action.extend([
71                         'arguments',
72                         dbus.Array(
73                                 [alarmTime, int(27)],
74                                 signature=dbus.Signature('v')
75                         )
76                 ])  #int(27) used in place of alarm_index
77
78                 event = []
79                 event.extend([dbus.ObjectPath('/AlarmdEventRecurring'), dbus.UInt32(4)])
80                 event.extend(['action', dbus.ObjectPath('/AlarmdActionExec')])  #use AlarmdActionExec instead of AlarmdActionDbus
81                 event.append(dbus.UInt32(len(action) / 2))
82                 event.extend(action)
83                 event.extend(['time', dbus.Int64(alarmTime)])
84                 event.extend(['recurr_interval', dbus.UInt32(recurrence)])
85                 event.extend(['recurr_count', dbus.Int32(self._REPEAT_FOREVER)])
86
87                 self._alarmCookie = self._alarmdDBus.add_event(*event);
88
89         def _clear_alarm(self):
90                 if self._alarmCookie == self._INVALID_COOKIE:
91                         return
92                 deleteResult = self._alarmdDBus.del_event(dbus.Int32(self._alarmCookie))
93                 self._alarmCookie = self._INVALID_COOKIE
94                 assert deleteResult != -1, "Deleting of alarm event failed"
95
96
97 def main():
98         import ConfigParser
99         import constants
100         try:
101                 import optparse
102         except ImportError:
103                 return
104
105         parser = optparse.OptionParser()
106         parser.add_option("-x", "--display", action="store_true", dest="display", help="Display data")
107         parser.add_option("-e", "--enable", action="store_true", dest="enabled", help="Whether the alarm should be enabled or not", default=False)
108         parser.add_option("-d", "--disable", action="store_false", dest="enabled", help="Whether the alarm should be enabled or not", default=False)
109         parser.add_option("-r", "--recurrence", action="store", type="int", dest="recurrence", help="How often the alarm occurs", default=5)
110         (commandOptions, commandArgs) = parser.parse_args()
111
112         alarmHandler = AlarmHandler()
113         config = ConfigParser.SafeConfigParser()
114         config.read(constants._user_settings_)
115         alarmHandler.load_settings(config, "alarm")
116
117         if commandOptions.display:
118                 print "Alarm (%s) is %s for every %d minutes" % (
119                         alarmHandler._alarmCookie,
120                         "enabled" if alarmHandler.isEnabled else "disabled",
121                         alarmHandler.recurrence,
122                 )
123         else:
124                 isEnabled = commandOptions.enabled
125                 recurrence = commandOptions.recurrence
126                 alarmHandler.apply_settings(isEnabled, recurrence)
127
128                 alarmHandler.save_settings(config, "alarm")
129                 configFile = open(constants._user_settings_, "wb")
130                 try:
131                         config.write(configFile)
132                 finally:
133                         configFile.close()
134
135
136 if __name__ == "__main__":
137         main()