2 * This file is part of TimedSilencer.
4 * TimedSilencer is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * TimedSilencer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with TimedSilencer. If not, see <http://www.gnu.org/licenses/>.
18 #ifndef ALARMD_TALKER_H
19 #define ALARMD_TALKER_H
24 #include <alarmd/libalarm.h>
25 #include <dbus-1.0/dbus/dbus-protocol.h>
28 #include "phone_profile.h"
30 // Alarmd documentation found at:
31 // http://wiki.maemo.org/Documentation/Maemo_5_Developer_Guide/Using_Generic_Platform_Components/Alarm_Framework
32 class AlarmdBackend : public QObject {
35 static time_t toTime_t(QTime t) {
36 int time_diff = QTime::currentTime().secsTo(t);
41 qDebug("time diff: %d", time_diff);
42 return (time_t) time(0) + time_diff;
46 static void deleteEvents() {
48 QSettings settings("TimedSilencer", "TimedSilencer");
49 cookie_t silent_cookie = settings.value("silencing_cookie", 0).toLongLong();
50 if(silent_cookie != 0) {
51 qDebug("Deleting silent profile event with cookie %ld", (long) silent_cookie);
52 alarmd_event_del(silent_cookie);
54 cookie_t general_cookie = settings.value("unsilencing_cookie", 0).toLongLong();
55 if(general_cookie != 0) {
56 qDebug("Deleting general profile event with cookie %ld", (long) general_cookie);
57 alarmd_event_del(general_cookie);
61 static void setProfileEvent(Profile p, QTime event_time) {
63 QSettings settings("TimedSilencer", "TimedSilencer");
66 cookie = settings.value("silencing_cookie", 0).toLongLong();
68 cookie = settings.value("unsilencing_cookie", 0).toLongLong();
69 alarm_event_t *eve = 0;
70 if(cookie == 0 || (eve = alarmd_event_get(cookie)) == 0) {
71 qDebug("Profile event does not exist yet, creating it...");
72 // The event does not exist yet
73 newProfileEvent(p, event_time);
76 // Update existing event
77 qDebug("Updating profile event with cookie %ld", (long)cookie);
78 eve->alarm_time = toTime_t(event_time);
79 alarmd_event_update(eve);
83 static void newProfileEvent(Profile p, QTime event_time) {
84 // Create the default alarm struct.
85 alarm_event_t *newEvent = alarm_event_create();
87 alarm_event_set_alarm_appid(newEvent, "TimedSilencer");
90 alarm_event_set_title(newEvent, "silent_profile");
92 alarm_event_set_title(newEvent, "general_profile");
94 newEvent->recur_secs = 86400; // 24 hours interval
95 newEvent->recur_count = -1; // Reoccur infinitely
96 newEvent->alarm_time = toTime_t(event_time);
97 //Add 1 action to our alarm event, and assign it to the "act" variable
98 alarm_action_t *act = alarm_event_add_actions(newEvent, 1);
99 // Actions are documented here:
100 // http://maemo.org/api_refs/5.0/5.0-final/libalarm/libalarm_8h.html#cc8e6f439d134448001132132476683c910f7626ec85a4170659b53fa2f0abc7
101 //Setup this action to be an "DBus command" one; also set it up to use DBUS auto-activation.
102 act->flags = ALARM_ACTION_WHEN_TRIGGERED | ALARM_ACTION_DBUS_USE_ACTIVATION | ALARM_ACTION_TYPE_DBUS;
104 //Setup the DBus params for this action
105 alarm_action_set_dbus_interface(act, "com.nokia.profiled");
106 alarm_action_set_dbus_service(act, "com.nokia.profiled");
107 alarm_action_set_dbus_path(act, "/com/nokia/profiled");
108 alarm_action_set_dbus_name(act, "set_profile");
111 const char* param = "silent";
112 alarm_action_set_dbus_args(act, DBUS_TYPE_STRING, ¶m, DBUS_TYPE_INVALID);
114 const char* param = "general";
115 alarm_action_set_dbus_args(act, DBUS_TYPE_STRING, ¶m, DBUS_TYPE_INVALID);
118 // Finally with everything setup, try to add your event to the alarm queue
119 cookie_t cookie = alarmd_event_add(newEvent);
122 QSettings settings("TimedSilencer", "TimedSilencer");
124 qDebug("Saving silent profile event cookie: %ld", (long)cookie);
125 settings.setValue("silencing_cookie", static_cast<qlonglong>(cookie));
127 qDebug("Saving silent general event cookie: %ld", (long)cookie);
128 settings.setValue("unsilencing_cookie", static_cast<qlonglong>(cookie));
132 qDebug("ERROR: Failed to add profile event to the queue!");
136 #endif // ALARMD_TALKER_H