Fixing a lot of random bugs
[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.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.delete_alarm()
42                         if enabled:
43                                 self._set_alarm(recurrence)
44                 self._recurrence = int(recurrence)
45
46         def delete_alarm(self):
47                 if self._alarmCookie == self._INVALID_COOKIE:
48                         return
49                 deleteResult = self._alarmdDBus.del_event(dbus.Int32(self._alarmCookie))
50                 self._alarmCookie = self._INVALID_COOKIE
51                 assert deleteResult != -1, "Deleting of alarm event failed"
52
53         @property
54         def recurrence(self):
55                 return self._recurrence
56
57         @property
58         def isEnabled(self):
59                 return self._alarmCookie != self._INVALID_COOKIE
60
61         def _get_start_time(self, recurrence):
62                 now = datetime.datetime.now()
63                 startTimeMinute = now.minute + recurrence
64                 now.replace(minute=startTimeMinute)
65                 timestamp = int(time.mktime(now.timetuple()))
66                 return timestamp
67
68         def _set_alarm(self, recurrence):
69                 alarmTime = self._get_start_time(recurrence)
70
71                 #Setup the alarm arguments so that they can be passed to the D-Bus add_event method
72                 action = []
73                 action.extend(['flags', self._DEFAULT_FLAGS])
74                 action.extend(['title', self._TITLE])
75                 action.extend(['path', self._LAUNCHER])
76                 action.extend([
77                         'arguments',
78                         dbus.Array(
79                                 [alarmTime, int(27)],
80                                 signature=dbus.Signature('v')
81                         )
82                 ])  #int(27) used in place of alarm_index
83
84                 event = []
85                 event.extend([dbus.ObjectPath('/AlarmdEventRecurring'), dbus.UInt32(4)])
86                 event.extend(['action', dbus.ObjectPath('/AlarmdActionExec')])  #use AlarmdActionExec instead of AlarmdActionDbus
87                 event.append(dbus.UInt32(len(action) / 2))
88                 event.extend(action)
89                 event.extend(['time', dbus.Int64(alarmTime)])
90                 event.extend(['recurr_interval', dbus.UInt32(recurrence)])
91                 event.extend(['recurr_count', dbus.Int32(self._REPEAT_FOREVER)])
92
93                 self._alarmCookie = self._alarmdDBus.add_event(*event);
94
95
96 def main():
97         import ConfigParser
98         import constants
99         try:
100                 import optparse
101         except ImportError:
102                 return
103
104         parser = optparse.OptionParser()
105         parser.add_option("-x", "--display", action="store_true", dest="display", help="Display data")
106         parser.add_option("-e", "--enable", action="store_true", dest="enabled", help="Whether the alarm should be enabled or not", default=False)
107         parser.add_option("-d", "--disable", action="store_false", dest="enabled", help="Whether the alarm should be enabled or not", default=False)
108         parser.add_option("-r", "--recurrence", action="store", type="int", dest="recurrence", help="How often the alarm occurs", default=5)
109         (commandOptions, commandArgs) = parser.parse_args()
110
111         alarmHandler = AlarmHandler()
112         config = ConfigParser.SafeConfigParser()
113         config.read(constants._user_settings_)
114         alarmHandler.load_settings(config, "alarm")
115
116         if commandOptions.display:
117                 print "Alarm (%s) is %s for every %d minutes" % (
118                         alarmHandler._alarmCookie,
119                         "enabled" if alarmHandler.isEnabled else "disabled",
120                         alarmHandler.recurrence,
121                 )
122         else:
123                 isEnabled = commandOptions.enabled
124                 recurrence = commandOptions.recurrence
125                 alarmHandler.apply_settings(isEnabled, recurrence)
126
127                 alarmHandler.save_settings(config, "alarm")
128                 configFile = open(constants._user_settings_, "wb")
129                 try:
130                         config.write(configFile)
131                 finally:
132                         configFile.close()
133
134
135 if __name__ == "__main__":
136         main()