2 * Copyright (C) 2010 Ixonos Plc.
4 * This file is part of fosdem-schedule.
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)
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
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/>.
23 #include <QApplication>
29 //#include <dbus-1.0/dbus/dbus-protocol.h>
31 int Alarm::addAlarm(int aEventId, const QDateTime &aDateTime)
34 alarm_event_t *eve = 0;
35 alarm_action_t *act = 0;
37 /* Create alarm event structure and set application identifier */
38 eve = alarm_event_create();
39 alarm_event_set_alarm_appid(eve, APPID);
41 /* for Deleting purposes */
42 alarm_event_set_message(eve, QString::number(aEventId).toLocal8Bit().data());
44 /* Use absolute time triggering */
45 //eve->alarm_time = time(0) + 5; // for testing (5 seconds from now)
46 QDateTime local( aDateTime);
47 qDebug() << "UTC: " << local.toTime_t();
48 local.setTimeSpec(Qt::LocalTime);
49 qDebug() << "LocalTime: " << local.toTime_t();
51 eve->alarm_time = local.toTime_t();
52 eve->flags = ALARM_EVENT_BOOT;
54 /* Add exec command action */
55 act = alarm_event_add_actions(eve, 1);
56 alarm_action_set_label(act, "FOSDEM'10");
58 QString command = QFileInfo(*qApp->argv()).absoluteFilePath() + QString(" %1").arg(QString::number(aEventId));
59 qDebug() << "Setting alarm: " << command;
60 alarm_action_set_exec_command(act, command.toLocal8Bit().data());
61 act->flags |= ALARM_ACTION_TYPE_EXEC;
62 act->flags |= ALARM_ACTION_WHEN_RESPONDED;
63 act->flags |= ALARM_ACTION_EXEC_ADD_COOKIE; // adds assigned cookie at the end of command string
65 // // setup this action to be a "DBus command"
66 // act->flags |= ALARM_ACTION_WHEN_RESPONDED;
67 // act->flags |= ALARM_ACTION_TYPE_DBUS;
69 // // DBus params for this action
70 // alarm_action_set_dbus_interface(act, "org.fosdem.schedule.AlarmInterface");
71 // alarm_action_set_dbus_service(act, "org.fosdem.schedule");
72 // alarm_action_set_dbus_path(act, "/Fosdem");
73 // alarm_action_set_dbus_name(act, "Alarm");
75 // // DBus arguments for the action
76 // alarm_action_set_dbus_args(act, DBUS_TYPE_INT32, &aEventId, DBUS_TYPE_INVALID);
78 // act->flags |= ALARM_ACTION_TYPE_EXEC;
79 // alarm_action_set_exec_command(act, command.toLocal8Bit().data());
80 // alarm_event_set_icon(eve, "fosdem");
81 // alarm_event_set_title(eve, "FOSDEM'10");
82 // adds assigned cookie at the end of command string
83 // act->flags |= ALARM_ACTION_EXEC_ADD_COOKIE;
85 /* Add stop button action */
86 /* TODO: send a DBus message to remove that alarm from database */
87 act = alarm_event_add_actions(eve, 1);
88 alarm_action_set_label(act, "Stop");
89 act->flags |= ALARM_ACTION_WHEN_RESPONDED;
90 act->flags |= ALARM_ACTION_TYPE_NOP;
92 /* Add snooze button action */
93 act = alarm_event_add_actions(eve, 1);
94 alarm_action_set_label(act, "Snooze");
95 act->flags |= ALARM_ACTION_WHEN_RESPONDED;
96 act->flags |= ALARM_ACTION_TYPE_SNOOZE;
98 /* Send the alarm to alarmd */
99 cookie = alarmd_event_add(eve);
101 // adding alarm failed
103 emit(addAlarmFailed(aEventId));
105 emit(alarmAdded(aEventId));
107 /* Free all dynamic memory associated with the alarm event */
108 alarm_event_delete(eve);
113 void Alarm::deleteAlarm(int aEventId)
117 alarm_event_t *event = 0;
119 // query the APPID's list of alarms
120 if( (list = alarmd_event_query(0,0, 0,0, APPID)) != 0 ) // query OK
122 for( int i = 0; (cookie = list[i]) != 0; ++i )
124 alarm_event_delete(event);
126 // get the event for specified alarm cookie (alarmId)
127 if( (event = alarmd_event_get(cookie)) == 0 )
129 // should we inform user about it ???
133 if(aEventId==atoi(alarm_event_get_message(event)))
136 if( alarmd_event_del(cookie) == -1 )
138 // was not able to delete alarm for given aEventId
139 emit(deleteAlarmFailed(aEventId));
144 emit(alarmDeleted(aEventId));
156 alarm_event_delete(event);
159 bool Alarm::hasEventAlarm(int aEventId)
163 alarm_event_t *event = 0;
165 bool eventHasAlarm = false;
167 // query the APPID's list of alarms
168 if( (list = alarmd_event_query(0,0, 0,0, APPID)) != 0 ) // query OK
170 for( int i = 0; (cookie = list[i]) != 0; ++i )
172 alarm_event_delete(event);
174 // get the event for specified alarm cookie (alarmId)
175 if( (event = alarmd_event_get(cookie)) == 0 )
177 // should we inform user about it ???
181 if(aEventId==atoi(alarm_event_get_message(event)))
183 eventHasAlarm = true;
194 alarm_event_delete(event);
196 return eventHasAlarm;