8 import osso.alarmd as alarmd
11 class AlarmHandler(object):
14 _TITLE = "Dialcentral Notifications"
15 _LAUNCHER = os.abspath(os.path.join(os.path.dirname(__file__), "alarm_notify.py"))
18 alarmd.ALARM_EVENT_NO_DIALOG |
19 alarmd.ALARM_EVENT_NO_SNOOZE |
20 alarmd.ALARM_EVENT_CONNECTED
26 bus = dbus.SystemBus()
27 self._alarmdDBus = bus.get_object("com.nokia.alarmd", "/com/nokia/alarmd");
28 self._alarmCookie = self._INVALID_COOKIE
30 def load_settings(self, config, sectionName):
31 self._recurrence = config.getint(sectionName, "recurrence")
32 self._alarmCookie = config.getint(sectionName, "alarmCookie")
34 def save_settings(self, config, sectionName):
35 config.set(sectionName, "recurrence", str(self._recurrence))
36 config.set(sectionName, "alarmCookie", str(self._alarmCookie))
38 def apply_settings(self, enabled, recurrence):
39 if recurrence != self._recurrence or enabled != self.isEnabled:
43 self._set_alarm(recurrence)
44 self._recurrence = int(recurrence)
46 def delete_alarm(self):
47 if self._alarmCookie == self._INVALID_COOKIE:
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"
55 return self._recurrence
59 return self._alarmCookie != self._INVALID_COOKIE
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()))
68 def _set_alarm(self, recurrence):
69 alarmTime = self._get_start_time(recurrence)
71 #Setup the alarm arguments so that they can be passed to the D-Bus add_event method
73 action.extend(['flags', self._DEFAULT_FLAGS])
74 action.extend(['title', self._TITLE])
75 action.extend(['path', self._LAUNCHER])
80 signature=dbus.Signature('v')
82 ]) #int(27) used in place of alarm_index
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))
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)])
93 self._alarmCookie = self._alarmdDBus.add_event(*event);
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()
111 alarmHandler = AlarmHandler()
112 config = ConfigParser.SafeConfigParser()
113 config.read(constants._user_settings_)
114 alarmHandler.load_settings(config, "alarm")
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,
123 isEnabled = commandOptions.enabled
124 recurrence = commandOptions.recurrence
125 alarmHandler.apply_settings(isEnabled, recurrence)
127 alarmHandler.save_settings(config, "alarm")
128 configFile = open(constants._user_settings_, "wb")
130 config.write(configFile)
135 if __name__ == "__main__":