initial binding of alarm to a DBus call
[toast/confclerk.git] / src / alarm / alarm.cpp
1 #include "alarm.h"
2
3 #include <QDateTime>
4
5 #include <QApplication>
6 #include <QDir>
7
8 int Alarm::addAlarm(int aEventId, const QDateTime &aDateTime)
9 {
10     cookie_t cookie = 0;
11     alarm_event_t *eve = 0;
12     alarm_action_t *act = 0;
13
14     /* Create alarm event structure and set application identifier */
15     eve = alarm_event_create();
16     alarm_event_set_alarm_appid(eve, APPID);
17
18     /* for Deleting purposes */
19     alarm_event_set_message(eve, QString::number(aEventId).toLocal8Bit().data()); 
20
21     /* Use absolute time triggering */
22     eve->alarm_time = time(0) + 5; //aDateTime.toTime_t();
23     eve->flags = ALARM_EVENT_BOOT;
24
25     QString command = QDir::currentPath() + "/" + *qApp->argv() + 
26       QString(" %1").arg(QString::number(aEventId));
27
28     /* Add exec command action */
29     act = alarm_event_add_actions(eve, 1);
30     alarm_action_set_label(act, "FOSDEM'10");
31
32     // setup this action to be a "DBus command"
33     act->flags |= ALARM_ACTION_WHEN_RESPONDED;
34     act->flags |= ALARM_ACTION_TYPE_DBUS;
35     
36     // DBus params for this action 
37     alarm_action_set_dbus_interface(act, "org.maemo.testApp");
38     alarm_action_set_dbus_service(act, "org.maemo.testApp");
39     alarm_action_set_dbus_path(act, "/org/maemo/testApp");
40     alarm_action_set_dbus_name(act, "triggerAlarm");
41     
42     // DBus arguments for the action
43     alarm_action_set_dbus_args(act, aEventId);
44
45     //    act->flags |= ALARM_ACTION_TYPE_EXEC;
46     //     alarm_action_set_exec_command(act, command.toLocal8Bit().data());
47     //    alarm_event_set_icon(eve, "fosdem");
48     //    alarm_event_set_title(eve, "FOSDEM'10");
49     // adds assigned cookie at the end of command string 
50     //    act->flags |= ALARM_ACTION_EXEC_ADD_COOKIE; 
51
52     /* Add stop button action */
53     /* TODO: send a DBus message to remove that alarm from database */
54     act = alarm_event_add_actions(eve, 1);
55     alarm_action_set_label(act, "Stop");
56     act->flags |= ALARM_ACTION_WHEN_RESPONDED;
57     act->flags |= ALARM_ACTION_TYPE_NOP;
58   
59     /* Add snooze button action */
60     act = alarm_event_add_actions(eve, 1);
61     alarm_action_set_label(act, "Snooze");
62     act->flags |= ALARM_ACTION_WHEN_RESPONDED;
63     act->flags |= ALARM_ACTION_TYPE_SNOOZE;
64
65     /* Send the alarm to alarmd */
66     cookie = alarmd_event_add(eve);
67
68     // adding alarm failed
69     if (cookie == 0)
70         emit(addAlarmFailed(aEventId));
71     else
72         emit(alarmAdded(aEventId));
73
74     /* Free all dynamic memory associated with the alarm event */
75     alarm_event_delete(eve);
76
77     return cookie;
78 }
79
80 void Alarm::deleteAlarm(int aEventId)
81 {
82     cookie_t *list = 0;
83     cookie_t cookie = 0;
84     alarm_event_t *event = 0;
85
86     // query the APPID's list of alarms
87     if( (list = alarmd_event_query(0,0, 0,0, APPID)) != 0 ) // query OK
88     {
89         for( int i = 0; (cookie = list[i]) != 0; ++i )
90         {
91             alarm_event_delete(event);
92
93             // get the event for specified alarm cookie (alarmId)
94             if( (event = alarmd_event_get(cookie)) == 0 )
95             {
96                 // should we inform user about it ???
97                 continue;
98             }
99
100             if(aEventId==atoi(alarm_event_get_message(event)))
101             {
102                 // delete cookie
103                 if( alarmd_event_del(cookie) == -1 )
104                 {
105                     // was not able to delete alarm for given aEventId
106                     emit(deleteAlarmFailed(aEventId));
107                     break;
108                 }
109                 else
110                 {
111                     emit(alarmDeleted(aEventId));
112                     break;
113                 }
114             }
115         }
116     }
117     else
118     {
119         // query failed
120     }
121
122     free(list);
123     alarm_event_delete(event);
124 }
125
126 bool Alarm::hasEventAlarm(int aEventId)
127 {
128     cookie_t *list = 0;
129     cookie_t cookie = 0;
130     alarm_event_t *event = 0;
131
132     bool eventHasAlarm = false;
133
134     // query the APPID's list of alarms
135     if( (list = alarmd_event_query(0,0, 0,0, APPID)) != 0 ) // query OK
136     {
137         for( int i = 0; (cookie = list[i]) != 0; ++i )
138         {
139             alarm_event_delete(event);
140
141             // get the event for specified alarm cookie (alarmId)
142             if( (event = alarmd_event_get(cookie)) == 0 )
143             {
144                 // should we inform user about it ???
145                 continue;
146             }
147
148             if(aEventId==atoi(alarm_event_get_message(event)))
149             {
150                 eventHasAlarm = true;
151                 break;
152             }
153         }
154     }
155     else
156     {
157         // query failed
158     }
159
160     free(list);
161     alarm_event_delete(event);
162
163     return eventHasAlarm;
164 }
165