[ca90cb1] | 1 | /* |
---|
| 2 | * Copyright (C) 2010 Ixonos Plc. |
---|
[68b2df2] | 3 | * Copyright (C) 2011 Philipp Spitzer, gregor herrmann |
---|
[ca90cb1] | 4 | * |
---|
[6df32f2] | 5 | * This file is part of ConfClerk. |
---|
[ca90cb1] | 6 | * |
---|
[6df32f2] | 7 | * ConfClerk is free software: you can redistribute it and/or modify it |
---|
[ca90cb1] | 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 | * |
---|
[6df32f2] | 12 | * ConfClerk is distributed in the hope that it will be useful, but |
---|
[ca90cb1] | 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 |
---|
[6df32f2] | 18 | * ConfClerk. If not, see <http://www.gnu.org/licenses/>. |
---|
[ca90cb1] | 19 | */ |
---|
[6ae1026] | 20 | #include "alarm.h" |
---|
| 21 | |
---|
| 22 | #include <QDateTime> |
---|
| 23 | |
---|
[d4a8bbf] | 24 | #include <QApplication> |
---|
| 25 | #include <QDir> |
---|
[a7d8638] | 26 | #include <QFileInfo> |
---|
[6831c6a] | 27 | |
---|
[2104023] | 28 | #include <QDebug> |
---|
| 29 | |
---|
[17cb895] | 30 | //#include <dbus-1.0/dbus/dbus-protocol.h> |
---|
[d4a8bbf] | 31 | |
---|
[6ae1026] | 32 | int Alarm::addAlarm(int aEventId, const QDateTime &aDateTime) |
---|
| 33 | { |
---|
| 34 | cookie_t cookie = 0; |
---|
[6197713] | 35 | alarm_event_t *eve = 0; |
---|
| 36 | alarm_action_t *act = 0; |
---|
[6ae1026] | 37 | |
---|
| 38 | /* Create alarm event structure and set application identifier */ |
---|
[6197713] | 39 | eve = alarm_event_create(); |
---|
| 40 | alarm_event_set_alarm_appid(eve, APPID); |
---|
| 41 | |
---|
| 42 | /* for Deleting purposes */ |
---|
[6831c6a] | 43 | alarm_event_set_message(eve, QString::number(aEventId).toLocal8Bit().data()); |
---|
[6ae1026] | 44 | |
---|
| 45 | /* Use absolute time triggering */ |
---|
[2104023] | 46 | //eve->alarm_time = time(0) + 5; // for testing (5 seconds from now) |
---|
[ba3fc20] | 47 | QDateTime local( aDateTime); |
---|
| 48 | qDebug() << "UTC: " << local.toTime_t(); |
---|
| 49 | local.setTimeSpec(Qt::LocalTime); |
---|
| 50 | qDebug() << "LocalTime: " << local.toTime_t(); |
---|
[5d9409d] | 51 | |
---|
[ba3fc20] | 52 | eve->alarm_time = local.toTime_t(); |
---|
[6645e1f] | 53 | eve->flags = ALARM_EVENT_BOOT; |
---|
[6197713] | 54 | |
---|
[6ae1026] | 55 | /* Add exec command action */ |
---|
[6197713] | 56 | act = alarm_event_add_actions(eve, 1); |
---|
| 57 | alarm_action_set_label(act, "FOSDEM'10"); |
---|
[6645e1f] | 58 | |
---|
[2104023] | 59 | QString command = QFileInfo(*qApp->argv()).absoluteFilePath() + QString(" %1").arg(QString::number(aEventId)); |
---|
| 60 | qDebug() << "Setting alarm: " << command; |
---|
[a7d8638] | 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 |
---|
| 65 | |
---|
| 66 | // // setup this action to be a "DBus command" |
---|
| 67 | // act->flags |= ALARM_ACTION_WHEN_RESPONDED; |
---|
| 68 | // act->flags |= ALARM_ACTION_TYPE_DBUS; |
---|
| 69 | // |
---|
| 70 | // // DBus params for this action |
---|
| 71 | // alarm_action_set_dbus_interface(act, "org.fosdem.schedule.AlarmInterface"); |
---|
| 72 | // alarm_action_set_dbus_service(act, "org.fosdem.schedule"); |
---|
| 73 | // alarm_action_set_dbus_path(act, "/Fosdem"); |
---|
| 74 | // alarm_action_set_dbus_name(act, "Alarm"); |
---|
| 75 | // |
---|
| 76 | // // DBus arguments for the action |
---|
| 77 | // alarm_action_set_dbus_args(act, DBUS_TYPE_INT32, &aEventId, DBUS_TYPE_INVALID); |
---|
[6645e1f] | 78 | |
---|
| 79 | // act->flags |= ALARM_ACTION_TYPE_EXEC; |
---|
| 80 | // alarm_action_set_exec_command(act, command.toLocal8Bit().data()); |
---|
[6197713] | 81 | // alarm_event_set_icon(eve, "fosdem"); |
---|
| 82 | // alarm_event_set_title(eve, "FOSDEM'10"); |
---|
[6831c6a] | 83 | // adds assigned cookie at the end of command string |
---|
| 84 | // act->flags |= ALARM_ACTION_EXEC_ADD_COOKIE; |
---|
[6197713] | 85 | |
---|
| 86 | /* Add stop button action */ |
---|
[6645e1f] | 87 | /* TODO: send a DBus message to remove that alarm from database */ |
---|
[6197713] | 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; |
---|
[6831c6a] | 92 | |
---|
[6197713] | 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; |
---|
[6ae1026] | 98 | |
---|
| 99 | /* Send the alarm to alarmd */ |
---|
[6197713] | 100 | cookie = alarmd_event_add(eve); |
---|
| 101 | |
---|
| 102 | // adding alarm failed |
---|
| 103 | if (cookie == 0) |
---|
[6ae1026] | 104 | emit(addAlarmFailed(aEventId)); |
---|
| 105 | else |
---|
| 106 | emit(alarmAdded(aEventId)); |
---|
| 107 | |
---|
| 108 | /* Free all dynamic memory associated with the alarm event */ |
---|
[6197713] | 109 | alarm_event_delete(eve); |
---|
[6ae1026] | 110 | |
---|
| 111 | return cookie; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | void Alarm::deleteAlarm(int aEventId) |
---|
| 115 | { |
---|
| 116 | cookie_t *list = 0; |
---|
| 117 | cookie_t cookie = 0; |
---|
| 118 | alarm_event_t *event = 0; |
---|
| 119 | |
---|
| 120 | // query the APPID's list of alarms |
---|
| 121 | if( (list = alarmd_event_query(0,0, 0,0, APPID)) != 0 ) // query OK |
---|
| 122 | { |
---|
| 123 | for( int i = 0; (cookie = list[i]) != 0; ++i ) |
---|
| 124 | { |
---|
| 125 | alarm_event_delete(event); |
---|
| 126 | |
---|
| 127 | // get the event for specified alarm cookie (alarmId) |
---|
| 128 | if( (event = alarmd_event_get(cookie)) == 0 ) |
---|
| 129 | { |
---|
| 130 | // should we inform user about it ??? |
---|
| 131 | continue; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | if(aEventId==atoi(alarm_event_get_message(event))) |
---|
| 135 | { |
---|
| 136 | // delete cookie |
---|
| 137 | if( alarmd_event_del(cookie) == -1 ) |
---|
| 138 | { |
---|
| 139 | // was not able to delete alarm for given aEventId |
---|
| 140 | emit(deleteAlarmFailed(aEventId)); |
---|
| 141 | break; |
---|
| 142 | } |
---|
| 143 | else |
---|
| 144 | { |
---|
| 145 | emit(alarmDeleted(aEventId)); |
---|
| 146 | break; |
---|
| 147 | } |
---|
| 148 | } |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | else |
---|
| 152 | { |
---|
| 153 | // query failed |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | free(list); |
---|
| 157 | alarm_event_delete(event); |
---|
| 158 | } |
---|
| 159 | |
---|
[b6cd05c] | 160 | bool Alarm::hasEventAlarm(int aEventId) |
---|
| 161 | { |
---|
| 162 | cookie_t *list = 0; |
---|
| 163 | cookie_t cookie = 0; |
---|
| 164 | alarm_event_t *event = 0; |
---|
| 165 | |
---|
| 166 | bool eventHasAlarm = false; |
---|
| 167 | |
---|
| 168 | // query the APPID's list of alarms |
---|
| 169 | if( (list = alarmd_event_query(0,0, 0,0, APPID)) != 0 ) // query OK |
---|
| 170 | { |
---|
| 171 | for( int i = 0; (cookie = list[i]) != 0; ++i ) |
---|
| 172 | { |
---|
| 173 | alarm_event_delete(event); |
---|
| 174 | |
---|
| 175 | // get the event for specified alarm cookie (alarmId) |
---|
| 176 | if( (event = alarmd_event_get(cookie)) == 0 ) |
---|
| 177 | { |
---|
| 178 | // should we inform user about it ??? |
---|
| 179 | continue; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | if(aEventId==atoi(alarm_event_get_message(event))) |
---|
| 183 | { |
---|
| 184 | eventHasAlarm = true; |
---|
| 185 | break; |
---|
| 186 | } |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | else |
---|
| 190 | { |
---|
| 191 | // query failed |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | free(list); |
---|
| 195 | alarm_event_delete(event); |
---|
| 196 | |
---|
| 197 | return eventHasAlarm; |
---|
| 198 | } |
---|
| 199 | |
---|