1 | /* |
---|
2 | * Copyright (C) 2010 Ixonos Plc. |
---|
3 | * |
---|
4 | * This file is part of fosdem-schedule. |
---|
5 | * |
---|
6 | * fosdem-schedule is free software: you can redistribute it and/or modify it |
---|
7 | * under the terms of the GNU General Public License as published by the Free |
---|
8 | * Software Foundation, either version 2 of the License, or (at your option) |
---|
9 | * any later version. |
---|
10 | * |
---|
11 | * fosdem-schedule is distributed in the hope that it will be useful, but |
---|
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
---|
13 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
---|
14 | * more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License along with |
---|
17 | * fosdem-schedule. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | */ |
---|
19 | #include "alarm.h" |
---|
20 | |
---|
21 | #include <QDateTime> |
---|
22 | |
---|
23 | #include <QApplication> |
---|
24 | #include <QDir> |
---|
25 | #include <QFileInfo> |
---|
26 | |
---|
27 | #include <QDebug> |
---|
28 | |
---|
29 | //#include <dbus-1.0/dbus/dbus-protocol.h> |
---|
30 | |
---|
31 | int Alarm::addAlarm(int aEventId, const QDateTime &aDateTime) |
---|
32 | { |
---|
33 | cookie_t cookie = 0; |
---|
34 | alarm_event_t *eve = 0; |
---|
35 | alarm_action_t *act = 0; |
---|
36 | |
---|
37 | /* Create alarm event structure and set application identifier */ |
---|
38 | eve = alarm_event_create(); |
---|
39 | alarm_event_set_alarm_appid(eve, APPID); |
---|
40 | |
---|
41 | /* for Deleting purposes */ |
---|
42 | alarm_event_set_message(eve, QString::number(aEventId).toLocal8Bit().data()); |
---|
43 | |
---|
44 | /* Use absolute time triggering */ |
---|
45 | //eve->alarm_time = time(0) + 5; // for testing (5 seconds from now) |
---|
46 | eve->alarm_time = aDateTime.toTime_t(); |
---|
47 | eve->flags = ALARM_EVENT_BOOT; |
---|
48 | |
---|
49 | /* Add exec command action */ |
---|
50 | act = alarm_event_add_actions(eve, 1); |
---|
51 | alarm_action_set_label(act, "FOSDEM'10"); |
---|
52 | |
---|
53 | QString command = QFileInfo(*qApp->argv()).absoluteFilePath() + QString(" %1").arg(QString::number(aEventId)); |
---|
54 | qDebug() << "Setting alarm: " << command; |
---|
55 | alarm_action_set_exec_command(act, command.toLocal8Bit().data()); |
---|
56 | act->flags |= ALARM_ACTION_TYPE_EXEC; |
---|
57 | act->flags |= ALARM_ACTION_WHEN_RESPONDED; |
---|
58 | act->flags |= ALARM_ACTION_EXEC_ADD_COOKIE; // adds assigned cookie at the end of command string |
---|
59 | |
---|
60 | // // setup this action to be a "DBus command" |
---|
61 | // act->flags |= ALARM_ACTION_WHEN_RESPONDED; |
---|
62 | // act->flags |= ALARM_ACTION_TYPE_DBUS; |
---|
63 | // |
---|
64 | // // DBus params for this action |
---|
65 | // alarm_action_set_dbus_interface(act, "org.fosdem.schedule.AlarmInterface"); |
---|
66 | // alarm_action_set_dbus_service(act, "org.fosdem.schedule"); |
---|
67 | // alarm_action_set_dbus_path(act, "/Fosdem"); |
---|
68 | // alarm_action_set_dbus_name(act, "Alarm"); |
---|
69 | // |
---|
70 | // // DBus arguments for the action |
---|
71 | // alarm_action_set_dbus_args(act, DBUS_TYPE_INT32, &aEventId, DBUS_TYPE_INVALID); |
---|
72 | |
---|
73 | // act->flags |= ALARM_ACTION_TYPE_EXEC; |
---|
74 | // alarm_action_set_exec_command(act, command.toLocal8Bit().data()); |
---|
75 | // alarm_event_set_icon(eve, "fosdem"); |
---|
76 | // alarm_event_set_title(eve, "FOSDEM'10"); |
---|
77 | // adds assigned cookie at the end of command string |
---|
78 | // act->flags |= ALARM_ACTION_EXEC_ADD_COOKIE; |
---|
79 | |
---|
80 | /* Add stop button action */ |
---|
81 | /* TODO: send a DBus message to remove that alarm from database */ |
---|
82 | act = alarm_event_add_actions(eve, 1); |
---|
83 | alarm_action_set_label(act, "Stop"); |
---|
84 | act->flags |= ALARM_ACTION_WHEN_RESPONDED; |
---|
85 | act->flags |= ALARM_ACTION_TYPE_NOP; |
---|
86 | |
---|
87 | /* Add snooze button action */ |
---|
88 | act = alarm_event_add_actions(eve, 1); |
---|
89 | alarm_action_set_label(act, "Snooze"); |
---|
90 | act->flags |= ALARM_ACTION_WHEN_RESPONDED; |
---|
91 | act->flags |= ALARM_ACTION_TYPE_SNOOZE; |
---|
92 | |
---|
93 | /* Send the alarm to alarmd */ |
---|
94 | cookie = alarmd_event_add(eve); |
---|
95 | |
---|
96 | // adding alarm failed |
---|
97 | if (cookie == 0) |
---|
98 | emit(addAlarmFailed(aEventId)); |
---|
99 | else |
---|
100 | emit(alarmAdded(aEventId)); |
---|
101 | |
---|
102 | /* Free all dynamic memory associated with the alarm event */ |
---|
103 | alarm_event_delete(eve); |
---|
104 | |
---|
105 | return cookie; |
---|
106 | } |
---|
107 | |
---|
108 | void Alarm::deleteAlarm(int aEventId) |
---|
109 | { |
---|
110 | cookie_t *list = 0; |
---|
111 | cookie_t cookie = 0; |
---|
112 | alarm_event_t *event = 0; |
---|
113 | |
---|
114 | // query the APPID's list of alarms |
---|
115 | if( (list = alarmd_event_query(0,0, 0,0, APPID)) != 0 ) // query OK |
---|
116 | { |
---|
117 | for( int i = 0; (cookie = list[i]) != 0; ++i ) |
---|
118 | { |
---|
119 | alarm_event_delete(event); |
---|
120 | |
---|
121 | // get the event for specified alarm cookie (alarmId) |
---|
122 | if( (event = alarmd_event_get(cookie)) == 0 ) |
---|
123 | { |
---|
124 | // should we inform user about it ??? |
---|
125 | continue; |
---|
126 | } |
---|
127 | |
---|
128 | if(aEventId==atoi(alarm_event_get_message(event))) |
---|
129 | { |
---|
130 | // delete cookie |
---|
131 | if( alarmd_event_del(cookie) == -1 ) |
---|
132 | { |
---|
133 | // was not able to delete alarm for given aEventId |
---|
134 | emit(deleteAlarmFailed(aEventId)); |
---|
135 | break; |
---|
136 | } |
---|
137 | else |
---|
138 | { |
---|
139 | emit(alarmDeleted(aEventId)); |
---|
140 | break; |
---|
141 | } |
---|
142 | } |
---|
143 | } |
---|
144 | } |
---|
145 | else |
---|
146 | { |
---|
147 | // query failed |
---|
148 | } |
---|
149 | |
---|
150 | free(list); |
---|
151 | alarm_event_delete(event); |
---|
152 | } |
---|
153 | |
---|
154 | bool Alarm::hasEventAlarm(int aEventId) |
---|
155 | { |
---|
156 | cookie_t *list = 0; |
---|
157 | cookie_t cookie = 0; |
---|
158 | alarm_event_t *event = 0; |
---|
159 | |
---|
160 | bool eventHasAlarm = false; |
---|
161 | |
---|
162 | // query the APPID's list of alarms |
---|
163 | if( (list = alarmd_event_query(0,0, 0,0, APPID)) != 0 ) // query OK |
---|
164 | { |
---|
165 | for( int i = 0; (cookie = list[i]) != 0; ++i ) |
---|
166 | { |
---|
167 | alarm_event_delete(event); |
---|
168 | |
---|
169 | // get the event for specified alarm cookie (alarmId) |
---|
170 | if( (event = alarmd_event_get(cookie)) == 0 ) |
---|
171 | { |
---|
172 | // should we inform user about it ??? |
---|
173 | continue; |
---|
174 | } |
---|
175 | |
---|
176 | if(aEventId==atoi(alarm_event_get_message(event))) |
---|
177 | { |
---|
178 | eventHasAlarm = true; |
---|
179 | break; |
---|
180 | } |
---|
181 | } |
---|
182 | } |
---|
183 | else |
---|
184 | { |
---|
185 | // query failed |
---|
186 | } |
---|
187 | |
---|
188 | free(list); |
---|
189 | alarm_event_delete(event); |
---|
190 | |
---|
191 | return eventHasAlarm; |
---|
192 | } |
---|
193 | |
---|