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