9 import osso.alarmd as alarmd
12 class AlarmHandler(object):
15 _TITLE = "Dialcentral Notifications"
16 _LAUNCHER = os.path.abspath(os.path.join(os.path.dirname(__file__), "alarm_notify.py"))
19 alarmd.ALARM_EVENT_NO_DIALOG |
20 alarmd.ALARM_EVENT_NO_SNOOZE |
21 alarmd.ALARM_EVENT_CONNECTED
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
32 def load_settings(self, config, sectionName):
34 self._recurrence = config.getint(sectionName, "recurrence")
35 self._alarmCookie = config.getint(sectionName, "alarmCookie")
36 launcher = config.get(sectionName, "notifier")
38 self._launcher = launcher
39 except ConfigParser.NoOptionError:
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)
48 def apply_settings(self, enabled, recurrence):
49 if recurrence != self._recurrence or enabled != self.isEnabled:
53 self._set_alarm(recurrence)
54 self._recurrence = int(recurrence)
58 return self._recurrence
62 return self._alarmCookie != self._INVALID_COOKIE
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()))
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)
77 #Setup the alarm arguments so that they can be passed to the D-Bus add_event method
79 action.extend(['flags', self._DEFAULT_FLAGS])
80 action.extend(['title', self._TITLE])
81 action.extend(['path', self._launcher])
86 signature=dbus.Signature('v')
88 ]) #int(27) used in place of alarm_index
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))
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)])
99 self._alarmCookie = self._alarmdDBus.add_event(*event);
101 def _clear_alarm(self):
102 if self._alarmCookie == self._INVALID_COOKIE:
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"
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()
124 alarmHandler = AlarmHandler()
125 config = ConfigParser.SafeConfigParser()
126 config.read(constants._user_settings_)
127 alarmHandler.load_settings(config, "alarm")
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,
136 isEnabled = commandOptions.enabled
137 recurrence = commandOptions.recurrence
138 alarmHandler.apply_settings(isEnabled, recurrence)
140 alarmHandler.save_settings(config, "alarm")
141 configFile = open(constants._user_settings_, "wb")
143 config.write(configFile)
148 if __name__ == "__main__":