1 | #include "alarm.h" |
---|
2 | |
---|
3 | #include <QDateTime> |
---|
4 | |
---|
5 | #include <QApplication> |
---|
6 | #include <QDir> |
---|
7 | |
---|
8 | int Alarm::addAlarm(int aEventId, const QDateTime &aDateTime) |
---|
9 | { |
---|
10 | cookie_t cookie = 0; |
---|
11 | alarm_event_t *event = 0; |
---|
12 | alarm_action_t *action = 0; |
---|
13 | |
---|
14 | /* Create alarm event structure and set application identifier */ |
---|
15 | event = alarm_event_create(); |
---|
16 | alarm_event_set_alarm_appid(event, APPID); |
---|
17 | alarm_event_set_message(event, QString::number(aEventId).toLocal8Bit().data()); // for Deleting purposes |
---|
18 | |
---|
19 | /* Use absolute time triggering */ |
---|
20 | event->alarm_time = aDateTime.toTime_t(); |
---|
21 | |
---|
22 | /* Add exec command action */ |
---|
23 | action = alarm_event_add_actions(event, 1); |
---|
24 | QString command = QDir::currentPath() + "/" + *qApp->argv() + QString(" %1").arg(QString::number(aEventId)); |
---|
25 | alarm_action_set_exec_command(action, command.toLocal8Bit().data()); |
---|
26 | action->flags |= ALARM_ACTION_TYPE_EXEC; |
---|
27 | action->flags |= ALARM_ACTION_WHEN_TRIGGERED; |
---|
28 | action->flags |= ALARM_ACTION_EXEC_ADD_COOKIE; // adds assigned cookie at the end of command string |
---|
29 | |
---|
30 | /* Send the alarm to alarmd */ |
---|
31 | cookie = alarmd_event_add(event); |
---|
32 | if(cookie==0) // adding alarm failed |
---|
33 | emit(addAlarmFailed(aEventId)); |
---|
34 | else |
---|
35 | emit(alarmAdded(aEventId)); |
---|
36 | |
---|
37 | /* Free all dynamic memory associated with the alarm event */ |
---|
38 | alarm_event_delete(event); |
---|
39 | |
---|
40 | return cookie; |
---|
41 | } |
---|
42 | |
---|
43 | void Alarm::deleteAlarm(int aEventId) |
---|
44 | { |
---|
45 | cookie_t *list = 0; |
---|
46 | cookie_t cookie = 0; |
---|
47 | alarm_event_t *event = 0; |
---|
48 | |
---|
49 | // query the APPID's list of alarms |
---|
50 | if( (list = alarmd_event_query(0,0, 0,0, APPID)) != 0 ) // query OK |
---|
51 | { |
---|
52 | for( int i = 0; (cookie = list[i]) != 0; ++i ) |
---|
53 | { |
---|
54 | alarm_event_delete(event); |
---|
55 | |
---|
56 | // get the event for specified alarm cookie (alarmId) |
---|
57 | if( (event = alarmd_event_get(cookie)) == 0 ) |
---|
58 | { |
---|
59 | // should we inform user about it ??? |
---|
60 | continue; |
---|
61 | } |
---|
62 | |
---|
63 | if(aEventId==atoi(alarm_event_get_message(event))) |
---|
64 | { |
---|
65 | // delete cookie |
---|
66 | if( alarmd_event_del(cookie) == -1 ) |
---|
67 | { |
---|
68 | // was not able to delete alarm for given aEventId |
---|
69 | emit(deleteAlarmFailed(aEventId)); |
---|
70 | break; |
---|
71 | } |
---|
72 | else |
---|
73 | { |
---|
74 | emit(alarmDeleted(aEventId)); |
---|
75 | break; |
---|
76 | } |
---|
77 | } |
---|
78 | } |
---|
79 | } |
---|
80 | else |
---|
81 | { |
---|
82 | // query failed |
---|
83 | } |
---|
84 | |
---|
85 | free(list); |
---|
86 | alarm_event_delete(event); |
---|
87 | } |
---|
88 | |
---|
89 | bool Alarm::hasEventAlarm(int aEventId) |
---|
90 | { |
---|
91 | cookie_t *list = 0; |
---|
92 | cookie_t cookie = 0; |
---|
93 | alarm_event_t *event = 0; |
---|
94 | |
---|
95 | bool eventHasAlarm = false; |
---|
96 | |
---|
97 | // query the APPID's list of alarms |
---|
98 | if( (list = alarmd_event_query(0,0, 0,0, APPID)) != 0 ) // query OK |
---|
99 | { |
---|
100 | for( int i = 0; (cookie = list[i]) != 0; ++i ) |
---|
101 | { |
---|
102 | alarm_event_delete(event); |
---|
103 | |
---|
104 | // get the event for specified alarm cookie (alarmId) |
---|
105 | if( (event = alarmd_event_get(cookie)) == 0 ) |
---|
106 | { |
---|
107 | // should we inform user about it ??? |
---|
108 | continue; |
---|
109 | } |
---|
110 | |
---|
111 | if(aEventId==atoi(alarm_event_get_message(event))) |
---|
112 | { |
---|
113 | eventHasAlarm = true; |
---|
114 | break; |
---|
115 | } |
---|
116 | } |
---|
117 | } |
---|
118 | else |
---|
119 | { |
---|
120 | // query failed |
---|
121 | } |
---|
122 | |
---|
123 | free(list); |
---|
124 | alarm_event_delete(event); |
---|
125 | |
---|
126 | return eventHasAlarm; |
---|
127 | } |
---|
128 | |
---|