1 | /* |
---|
2 | * Copyright (C) 2010 Ixonos Plc. |
---|
3 | * Copyright (C) 2011-2012 Philipp Spitzer, gregor herrmann, Stefan Stahl |
---|
4 | * |
---|
5 | * This file is part of ConfClerk. |
---|
6 | * |
---|
7 | * ConfClerk is free software: you can redistribute it and/or modify it |
---|
8 | * under the terms of the GNU General Public License as published by the Free |
---|
9 | * Software Foundation, either version 2 of the License, or (at your option) |
---|
10 | * any later version. |
---|
11 | * |
---|
12 | * ConfClerk is distributed in the hope that it will be useful, but |
---|
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
---|
14 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
---|
15 | * more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License along with |
---|
18 | * ConfClerk. If not, see <http://www.gnu.org/licenses/>. |
---|
19 | */ |
---|
20 | #include "alarm.h" |
---|
21 | |
---|
22 | #include <QDateTime> |
---|
23 | |
---|
24 | #include <QApplication> |
---|
25 | #include <QDir> |
---|
26 | #include <QFileInfo> |
---|
27 | |
---|
28 | #include <QDebug> |
---|
29 | |
---|
30 | //#include "dbus-1.0/dbus/dbus-protocol.h" |
---|
31 | |
---|
32 | int Alarm::addAlarm(int conferenceId, int eventId, QString eventTitle, const QDateTime &alarmDateTime) { |
---|
33 | cookie_t alarmCookie = 0; |
---|
34 | alarm_event_t *alarmEvent = 0; |
---|
35 | alarm_action_t *alarmAction = 0; |
---|
36 | |
---|
37 | /* Create alarm event structure and set application identifier */ |
---|
38 | alarmEvent = alarm_event_create(); |
---|
39 | alarm_event_set_alarm_appid(alarmEvent, APPID); |
---|
40 | |
---|
41 | // message |
---|
42 | alarm_event_set_title(alarmEvent, "ConfClerk"); |
---|
43 | alarm_event_set_message(alarmEvent, eventTitle.toLocal8Bit().data()); |
---|
44 | |
---|
45 | // for deleting purposes |
---|
46 | alarm_event_set_attr_int(alarmEvent, "conferenceId", conferenceId); |
---|
47 | alarm_event_set_attr_int(alarmEvent, "eventId", eventId); |
---|
48 | |
---|
49 | /* Use absolute time triggering */ |
---|
50 | QDateTime local(alarmDateTime); |
---|
51 | local.setTimeSpec(Qt::LocalTime); |
---|
52 | |
---|
53 | alarmEvent->alarm_time = local.toTime_t(); |
---|
54 | alarmEvent->flags = ALARM_EVENT_BOOT; |
---|
55 | |
---|
56 | /* Add exec command action */ |
---|
57 | alarmAction = alarm_event_add_actions(alarmEvent, 1); |
---|
58 | alarm_action_set_label(alarmAction, "ConfClerk"); |
---|
59 | |
---|
60 | QString command = QFileInfo(*qApp->argv()).absoluteFilePath() + QString(" %1").arg(QString::number(eventId)); |
---|
61 | qDebug() << "Setting alarm: " << command; |
---|
62 | alarm_action_set_exec_command(alarmAction, command.toLocal8Bit().data()); |
---|
63 | alarmAction->flags |= ALARM_ACTION_TYPE_EXEC; |
---|
64 | alarmAction->flags |= ALARM_ACTION_WHEN_RESPONDED; |
---|
65 | alarmAction->flags |= ALARM_ACTION_EXEC_ADD_COOKIE; // adds assigned cookie at the end of command string |
---|
66 | |
---|
67 | // // setup this action to be a "DBus command" |
---|
68 | // act->flags |= ALARM_ACTION_WHEN_RESPONDED; |
---|
69 | // act->flags |= ALARM_ACTION_TYPE_DBUS; |
---|
70 | // |
---|
71 | // // DBus params for this action |
---|
72 | // alarm_action_set_dbus_interface(act, "at.priv.toastfreeware.confclerk.AlarmInterface"); |
---|
73 | // alarm_action_set_dbus_service(act, "at.priv.toastfreeware.confclerk"); |
---|
74 | // alarm_action_set_dbus_path(act, "/ConfClerk"); |
---|
75 | // alarm_action_set_dbus_name(act, "Alarm"); |
---|
76 | // |
---|
77 | // // DBus arguments for the action |
---|
78 | // alarm_action_set_dbus_args(act, DBUS_TYPE_INT32, &aEventId, DBUS_TYPE_INVALID); |
---|
79 | |
---|
80 | // act->flags |= ALARM_ACTION_TYPE_EXEC; |
---|
81 | // alarm_action_set_exec_command(act, command.toLocal8Bit().data()); |
---|
82 | // alarm_event_set_icon(eve, "fosdem"); |
---|
83 | // alarm_event_set_title(eve, "ConfClerk"); |
---|
84 | // adds assigned cookie at the end of command string |
---|
85 | // act->flags |= ALARM_ACTION_EXEC_ADD_COOKIE; |
---|
86 | |
---|
87 | /* Add stop button action */ |
---|
88 | /* TODO: send a DBus message to remove that alarm from database */ |
---|
89 | alarmAction = alarm_event_add_actions(alarmEvent, 1); |
---|
90 | alarm_action_set_label(alarmAction, "Stop"); |
---|
91 | alarmAction->flags |= ALARM_ACTION_WHEN_RESPONDED; |
---|
92 | alarmAction->flags |= ALARM_ACTION_TYPE_NOP; |
---|
93 | |
---|
94 | /* Add snooze button action */ |
---|
95 | alarmAction = alarm_event_add_actions(alarmEvent, 1); |
---|
96 | alarm_action_set_label(alarmAction, "Snooze"); |
---|
97 | alarmAction->flags |= ALARM_ACTION_WHEN_RESPONDED; |
---|
98 | alarmAction->flags |= ALARM_ACTION_TYPE_SNOOZE; |
---|
99 | |
---|
100 | /* Send the alarm to alarmd */ |
---|
101 | alarmCookie = alarmd_event_add(alarmEvent); |
---|
102 | |
---|
103 | /* Free all dynamic memory associated with the alarm event */ |
---|
104 | alarm_event_delete(alarmEvent); |
---|
105 | |
---|
106 | return alarmCookie; |
---|
107 | } |
---|
108 | |
---|
109 | void Alarm::deleteAlarm(int conferenceId, int eventId) { |
---|
110 | cookie_t *alarmList = 0; |
---|
111 | cookie_t alarmCookie = 0; |
---|
112 | alarm_event_t *alarmEvent = 0; |
---|
113 | |
---|
114 | // query the APPID's list of alarms |
---|
115 | if( (alarmList = alarmd_event_query(0,0, 0,0, APPID)) != 0) { // query OK |
---|
116 | for (int i = 0; (alarmCookie = alarmList[i]) != 0; ++i ) { |
---|
117 | // get the event for specified alarm cookie (alarmId) |
---|
118 | alarmEvent = alarmd_event_get(alarmCookie); |
---|
119 | Q_ASSERT(alarmEvent); |
---|
120 | |
---|
121 | bool found = (conferenceId == alarm_event_get_attr_int(alarmEvent, "conferenceId", -1) && eventId == alarm_event_get_attr_int(alarmEvent, "eventId", -1)); |
---|
122 | if (found) alarmd_event_del(alarmCookie); // delete cookie |
---|
123 | alarm_event_delete(alarmEvent); |
---|
124 | if (found) break; |
---|
125 | } |
---|
126 | } |
---|
127 | free(alarmList); |
---|
128 | } |
---|