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, QString aEventTitle, 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 */
44 //alarm_event_set_message(eve, QString::number(aEventId).toLocal8Bit().data());
45 alarm_event_set_message(eve, aEventTitle.toLocal8Bit().data());
47 /* Use absolute time triggering */
48 //eve->alarm_time = time(0) + 5; // for testing (5 seconds from now)
49 QDateTime local( aDateTime);
50 qDebug() << "UTC: " << local.toTime_t();
51 local.setTimeSpec(Qt::LocalTime);
52 qDebug() << "LocalTime: " << local.toTime_t();
54 eve->alarm_time = local.toTime_t();
55 eve->flags = ALARM_EVENT_BOOT;
57 /* Add exec command action */
58 act = alarm_event_add_actions(eve, 1);
59 alarm_action_set_label(act, "ConfClerk");
61 QString command = QFileInfo(*qApp->argv()).absoluteFilePath() + QString(" %1").arg(QString::number(aEventId));
62 qDebug() << "Setting alarm: " << command;
63 alarm_action_set_exec_command(act, command.toLocal8Bit().data());
64 act->flags |= ALARM_ACTION_TYPE_EXEC;
65 act->flags |= ALARM_ACTION_WHEN_RESPONDED;
66 act->flags |= ALARM_ACTION_EXEC_ADD_COOKIE; // adds assigned cookie at the end of command string
68 // // setup this action to be a "DBus command"
69 // act->flags |= ALARM_ACTION_WHEN_RESPONDED;
70 // act->flags |= ALARM_ACTION_TYPE_DBUS;
72 // // DBus params for this action
73 // alarm_action_set_dbus_interface(act, "at.priv.toastfreeware.confclerk.AlarmInterface");
74 // alarm_action_set_dbus_service(act, "at.priv.toastfreeware.confclerk");
75 // alarm_action_set_dbus_path(act, "/ConfClerk");
76 // alarm_action_set_dbus_name(act, "Alarm");
78 // // DBus arguments for the action
79 // alarm_action_set_dbus_args(act, DBUS_TYPE_INT32, &aEventId, DBUS_TYPE_INVALID);
81 // act->flags |= ALARM_ACTION_TYPE_EXEC;
82 // alarm_action_set_exec_command(act, command.toLocal8Bit().data());
83 // alarm_event_set_icon(eve, "fosdem");
84 // alarm_event_set_title(eve, "ConfClerk");
85 // adds assigned cookie at the end of command string
86 // act->flags |= ALARM_ACTION_EXEC_ADD_COOKIE;
88 /* Add stop button action */
89 /* TODO: send a DBus message to remove that alarm from database */
90 act = alarm_event_add_actions(eve, 1);
91 alarm_action_set_label(act, "Stop");
92 act->flags |= ALARM_ACTION_WHEN_RESPONDED;
93 act->flags |= ALARM_ACTION_TYPE_NOP;
95 /* Add snooze button action */
96 act = alarm_event_add_actions(eve, 1);
97 alarm_action_set_label(act, "Snooze");
98 act->flags |= ALARM_ACTION_WHEN_RESPONDED;
99 act->flags |= ALARM_ACTION_TYPE_SNOOZE;
101 /* Send the alarm to alarmd */
102 cookie = alarmd_event_add(eve);
104 // adding alarm failed
106 emit(addAlarmFailed(aEventId));
108 emit(alarmAdded(aEventId));
110 /* Free all dynamic memory associated with the alarm event */
111 alarm_event_delete(eve);
116 void Alarm::deleteAlarm(int aEventId)
120 alarm_event_t *event = 0;
122 // query the APPID's list of alarms
123 if( (list = alarmd_event_query(0,0, 0,0, APPID)) != 0 ) // query OK
125 for( int i = 0; (cookie = list[i]) != 0; ++i )
127 alarm_event_delete(event);
129 // get the event for specified alarm cookie (alarmId)
130 if( (event = alarmd_event_get(cookie)) == 0 )
132 // should we inform user about it ???
136 if(aEventId==atoi(alarm_event_get_message(event)))
139 if( alarmd_event_del(cookie) == -1 )
141 // was not able to delete alarm for given aEventId
142 emit(deleteAlarmFailed(aEventId));
147 emit(alarmDeleted(aEventId));
159 alarm_event_delete(event);
162 bool Alarm::hasEventAlarm(int aEventId)
166 alarm_event_t *event = 0;
168 bool eventHasAlarm = false;
170 // query the APPID's list of alarms
171 if( (list = alarmd_event_query(0,0, 0,0, APPID)) != 0 ) // query OK
173 for( int i = 0; (cookie = list[i]) != 0; ++i )
175 alarm_event_delete(event);
177 // get the event for specified alarm cookie (alarmId)
178 if( (event = alarmd_event_get(cookie)) == 0 )
180 // should we inform user about it ???
184 if(aEventId==atoi(alarm_event_get_message(event)))
186 eventHasAlarm = true;
197 alarm_event_delete(event);
199 return eventHasAlarm;