2 * Copyright (C) 2010 Ixonos Plc.
3 * Copyright (C) 2011 Philipp Spitzer, gregor herrmann
5 * This file is part of ConfClerk.
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)
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
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/>.
24 #include <QApplication>
30 //#include <dbus-1.0/dbus/dbus-protocol.h>
32 int Alarm::addAlarm(int aEventId, const QDateTime &aDateTime)
35 alarm_event_t *eve = 0;
36 alarm_action_t *act = 0;
38 /* Create alarm event structure and set application identifier */
39 eve = alarm_event_create();
40 alarm_event_set_alarm_appid(eve, APPID);
42 /* for Deleting purposes */
43 alarm_event_set_message(eve, QString::number(aEventId).toLocal8Bit().data());
45 /* Use absolute time triggering */
46 //eve->alarm_time = time(0) + 5; // for testing (5 seconds from now)
47 QDateTime local( aDateTime);
48 qDebug() << "UTC: " << local.toTime_t();
49 local.setTimeSpec(Qt::LocalTime);
50 qDebug() << "LocalTime: " << local.toTime_t();
52 eve->alarm_time = local.toTime_t();
53 eve->flags = ALARM_EVENT_BOOT;
55 /* Add exec command action */
56 act = alarm_event_add_actions(eve, 1);
57 alarm_action_set_label(act, "ConfClerk");
59 QString command = QFileInfo(*qApp->argv()).absoluteFilePath() + QString(" %1").arg(QString::number(aEventId));
60 qDebug() << "Setting alarm: " << command;
61 alarm_action_set_exec_command(act, command.toLocal8Bit().data());
62 act->flags |= ALARM_ACTION_TYPE_EXEC;
63 act->flags |= ALARM_ACTION_WHEN_RESPONDED;
64 act->flags |= ALARM_ACTION_EXEC_ADD_COOKIE; // adds assigned cookie at the end of command string
66 // // setup this action to be a "DBus command"
67 // act->flags |= ALARM_ACTION_WHEN_RESPONDED;
68 // act->flags |= ALARM_ACTION_TYPE_DBUS;
70 // // DBus params for this action
71 // alarm_action_set_dbus_interface(act, "at.priv.toastfreeware.confclerk.AlarmInterface");
72 // alarm_action_set_dbus_service(act, "at.priv.toastfreeware.confclerk");
73 // alarm_action_set_dbus_path(act, "/ConfClerk");
74 // alarm_action_set_dbus_name(act, "Alarm");
76 // // DBus arguments for the action
77 // alarm_action_set_dbus_args(act, DBUS_TYPE_INT32, &aEventId, DBUS_TYPE_INVALID);
79 // act->flags |= ALARM_ACTION_TYPE_EXEC;
80 // alarm_action_set_exec_command(act, command.toLocal8Bit().data());
81 // alarm_event_set_icon(eve, "fosdem");
82 // alarm_event_set_title(eve, "ConfClerk");
83 // adds assigned cookie at the end of command string
84 // act->flags |= ALARM_ACTION_EXEC_ADD_COOKIE;
86 /* Add stop button action */
87 /* TODO: send a DBus message to remove that alarm from database */
88 act = alarm_event_add_actions(eve, 1);
89 alarm_action_set_label(act, "Stop");
90 act->flags |= ALARM_ACTION_WHEN_RESPONDED;
91 act->flags |= ALARM_ACTION_TYPE_NOP;
93 /* Add snooze button action */
94 act = alarm_event_add_actions(eve, 1);
95 alarm_action_set_label(act, "Snooze");
96 act->flags |= ALARM_ACTION_WHEN_RESPONDED;
97 act->flags |= ALARM_ACTION_TYPE_SNOOZE;
99 /* Send the alarm to alarmd */
100 cookie = alarmd_event_add(eve);
102 // adding alarm failed
104 emit(addAlarmFailed(aEventId));
106 emit(alarmAdded(aEventId));
108 /* Free all dynamic memory associated with the alarm event */
109 alarm_event_delete(eve);
114 void Alarm::deleteAlarm(int aEventId)
118 alarm_event_t *event = 0;
120 // query the APPID's list of alarms
121 if( (list = alarmd_event_query(0,0, 0,0, APPID)) != 0 ) // query OK
123 for( int i = 0; (cookie = list[i]) != 0; ++i )
125 alarm_event_delete(event);
127 // get the event for specified alarm cookie (alarmId)
128 if( (event = alarmd_event_get(cookie)) == 0 )
130 // should we inform user about it ???
134 if(aEventId==atoi(alarm_event_get_message(event)))
137 if( alarmd_event_del(cookie) == -1 )
139 // was not able to delete alarm for given aEventId
140 emit(deleteAlarmFailed(aEventId));
145 emit(alarmDeleted(aEventId));
157 alarm_event_delete(event);
160 bool Alarm::hasEventAlarm(int aEventId)
164 alarm_event_t *event = 0;
166 bool eventHasAlarm = false;
168 // query the APPID's list of alarms
169 if( (list = alarmd_event_query(0,0, 0,0, APPID)) != 0 ) // query OK
171 for( int i = 0; (cookie = list[i]) != 0; ++i )
173 alarm_event_delete(event);
175 // get the event for specified alarm cookie (alarmId)
176 if( (event = alarmd_event_get(cookie)) == 0 )
178 // should we inform user about it ???
182 if(aEventId==atoi(alarm_event_get_message(event)))
184 eventHasAlarm = true;
195 alarm_event_delete(event);
197 return eventHasAlarm;