source: src/alarm/alarm.cpp @ d7986a6

qt5
Last change on this file since d7986a6 was 6831c6a, checked in by hanzes <hanzes@…>, 12 years ago

Alarm dbus connection added

  • Property mode set to 100644
File size: 4.5 KB
Line 
1#include "alarm.h"
2
3#include <QDateTime>
4
5#include <QApplication>
6#include <QDir>
7#include <QDebug>
8
9#include <dbus-1.0/dbus/dbus-protocol.h>
10
11int Alarm::addAlarm(int aEventId, const QDateTime &aDateTime)
12{
13    cookie_t cookie = 0;
14    alarm_event_t *eve = 0;
15    alarm_action_t *act = 0;
16
17    /* Create alarm event structure and set application identifier */
18    eve = alarm_event_create();
19    alarm_event_set_alarm_appid(eve, APPID);
20
21    /* for Deleting purposes */
22    alarm_event_set_message(eve, QString::number(aEventId).toLocal8Bit().data());
23
24    /* Use absolute time triggering */
25    eve->alarm_time = time(0) + 5; //aDateTime.toTime_t();
26    eve->flags = ALARM_EVENT_BOOT;
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.fosdem.schedule.AlarmInterface");
38    alarm_action_set_dbus_service(act, "org.fosdem.schedule");
39    alarm_action_set_dbus_path(act, "/Fosdem");
40    alarm_action_set_dbus_name(act, "Alarm");
41
42    // DBus arguments for the action
43    alarm_action_set_dbus_args(act,  DBUS_TYPE_INT32, &aEventId, DBUS_TYPE_INVALID);
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
80void 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
126bool 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
Note: See TracBrowser for help on using the repository browser.