Initial commit
[timedsilencer] / alarmd_backend.h
1 /*
2  * This file is part of TimedSilencer.
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 #ifndef ALARMD_TALKER_H
19 #define ALARMD_TALKER_H
20
21 #include <QTime>
22 #include <QString>
23 #include <QSettings>
24 #include <alarmd/libalarm.h>
25 #include <dbus-1.0/dbus/dbus-protocol.h>
26 #include <time.h>
27
28 #include "phone_profile.h"
29
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 {
33   Q_OBJECT
34 private:
35   static time_t toTime_t(QTime t) {
36     int time_diff = QTime::currentTime().secsTo(t);
37     if(time_diff < 0) {
38       // Add 24 hours
39       time_diff += 86400;
40     }
41     qDebug("time diff: %d", time_diff);
42     return (time_t) time(0) + time_diff;
43   }
44
45 public:
46   static void deleteEvents() {
47     // Get events cookies
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);
53     }
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);
58     }
59   }
60
61   static void setProfileEvent(Profile p, QTime event_time) {
62     // Get event cookie
63     QSettings settings("TimedSilencer", "TimedSilencer");
64     cookie_t cookie;
65     if(p == SILENT)
66       cookie = settings.value("silencing_cookie", 0).toLongLong();
67     else
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);
74       return;
75     }
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);
80   }
81
82 protected:
83   static void newProfileEvent(Profile p, QTime event_time) {
84     // Create the default alarm struct.
85     alarm_event_t *newEvent = alarm_event_create();
86     // Set the APP ID
87     alarm_event_set_alarm_appid(newEvent, "TimedSilencer");
88     // Set the title
89     if(p == SILENT)
90       alarm_event_set_title(newEvent, "silent_profile");
91     else
92       alarm_event_set_title(newEvent, "general_profile");
93     // Timing
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;
103
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");
109
110     if(p == SILENT) {
111       const char* param = "silent";
112       alarm_action_set_dbus_args(act, DBUS_TYPE_STRING, &param, DBUS_TYPE_INVALID);
113     } else {
114       const char* param = "general";
115       alarm_action_set_dbus_args(act, DBUS_TYPE_STRING, &param, DBUS_TYPE_INVALID);
116     }
117
118     // Finally with everything setup, try to add your event to the alarm queue
119     cookie_t cookie = alarmd_event_add(newEvent);
120     if(cookie != 0) {
121       // Save cookie
122       QSettings settings("TimedSilencer", "TimedSilencer");
123       if(p == SILENT) {
124         qDebug("Saving silent profile event cookie: %ld", (long)cookie);
125         settings.setValue("silencing_cookie", static_cast<qlonglong>(cookie));
126       } else {
127         qDebug("Saving silent general event cookie: %ld", (long)cookie);
128         settings.setValue("unsilencing_cookie", static_cast<qlonglong>(cookie));
129       }
130       return;
131     }
132     qDebug("ERROR: Failed to add profile event to the queue!");
133   }
134 };
135
136 #endif // ALARMD_TALKER_H