Alarm implementation modified
[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
24     QString command = QDir::currentPath() + "/" + *qApp->argv() + 
25       QString(" %1").arg(QString::number(aEventId));
26
27     /* Add exec command action */
28     act = alarm_event_add_actions(eve, 1);
29     alarm_action_set_label(act, "FOSDEM'10");
30     //    alarm_event_set_icon(eve, "fosdem");
31     //    alarm_event_set_title(eve, "FOSDEM'10");
32     act->flags |= ALARM_ACTION_TYPE_EXEC;
33     act->flags |= ALARM_ACTION_WHEN_RESPONDED;
34     // adds assigned cookie at the end of command string 
35     //    act->flags |= ALARM_ACTION_EXEC_ADD_COOKIE; 
36     alarm_action_set_exec_command(act, command.toLocal8Bit().data());
37
38     /* Add stop button action */
39     act = alarm_event_add_actions(eve, 1);
40     alarm_action_set_label(act, "Stop");
41     act->flags |= ALARM_ACTION_WHEN_RESPONDED;
42     act->flags |= ALARM_ACTION_TYPE_NOP;
43   
44     /* Add snooze button action */
45     act = alarm_event_add_actions(eve, 1);
46     alarm_action_set_label(act, "Snooze");
47     act->flags |= ALARM_ACTION_WHEN_RESPONDED;
48     act->flags |= ALARM_ACTION_TYPE_SNOOZE;
49
50     /* Send the alarm to alarmd */
51     cookie = alarmd_event_add(eve);
52
53     // adding alarm failed
54     if (cookie == 0)
55         emit(addAlarmFailed(aEventId));
56     else
57         emit(alarmAdded(aEventId));
58
59     /* Free all dynamic memory associated with the alarm event */
60     alarm_event_delete(eve);
61
62     return cookie;
63 }
64
65 void Alarm::deleteAlarm(int aEventId)
66 {
67     cookie_t *list = 0;
68     cookie_t cookie = 0;
69     alarm_event_t *event = 0;
70
71     // query the APPID's list of alarms
72     if( (list = alarmd_event_query(0,0, 0,0, APPID)) != 0 ) // query OK
73     {
74         for( int i = 0; (cookie = list[i]) != 0; ++i )
75         {
76             alarm_event_delete(event);
77
78             // get the event for specified alarm cookie (alarmId)
79             if( (event = alarmd_event_get(cookie)) == 0 )
80             {
81                 // should we inform user about it ???
82                 continue;
83             }
84
85             if(aEventId==atoi(alarm_event_get_message(event)))
86             {
87                 // delete cookie
88                 if( alarmd_event_del(cookie) == -1 )
89                 {
90                     // was not able to delete alarm for given aEventId
91                     emit(deleteAlarmFailed(aEventId));
92                     break;
93                 }
94                 else
95                 {
96                     emit(alarmDeleted(aEventId));
97                     break;
98                 }
99             }
100         }
101     }
102     else
103     {
104         // query failed
105     }
106
107     free(list);
108     alarm_event_delete(event);
109 }
110
111 bool Alarm::hasEventAlarm(int aEventId)
112 {
113     cookie_t *list = 0;
114     cookie_t cookie = 0;
115     alarm_event_t *event = 0;
116
117     bool eventHasAlarm = false;
118
119     // query the APPID's list of alarms
120     if( (list = alarmd_event_query(0,0, 0,0, APPID)) != 0 ) // query OK
121     {
122         for( int i = 0; (cookie = list[i]) != 0; ++i )
123         {
124             alarm_event_delete(event);
125
126             // get the event for specified alarm cookie (alarmId)
127             if( (event = alarmd_event_get(cookie)) == 0 )
128             {
129                 // should we inform user about it ???
130                 continue;
131             }
132
133             if(aEventId==atoi(alarm_event_get_message(event)))
134             {
135                 eventHasAlarm = true;
136                 break;
137             }
138         }
139     }
140     else
141     {
142         // query failed
143     }
144
145     free(list);
146     alarm_event_delete(event);
147
148     return eventHasAlarm;
149 }
150