source: src/alarm/alarm.cpp @ a7d8638

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

Alarm dbus connection added

  • Property mode set to 100644
File size: 5.0 KB
Line 
1#include "alarm.h"
2
3#include <QDateTime>
4
5#include <QApplication>
6#include <QDir>
7#include <QFileInfo>
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    QFileInfo fi(*qApp->argv());
33    QString name(fi.fileName());
34
35    QString command = QDir::currentPath() + "/" + name + QString(" %1").arg(QString::number(aEventId));
36    //QString command =  *qApp->argv() + QString(" %1").arg(QString::number(aEventId));
37    alarm_action_set_exec_command(act, command.toLocal8Bit().data());
38    act->flags |= ALARM_ACTION_TYPE_EXEC;
39    act->flags |= ALARM_ACTION_WHEN_RESPONDED;
40    act->flags |= ALARM_ACTION_EXEC_ADD_COOKIE; // adds assigned cookie at the end of command string
41
42//    // setup this action to be a "DBus command"
43//    act->flags |= ALARM_ACTION_WHEN_RESPONDED;
44//    act->flags |= ALARM_ACTION_TYPE_DBUS;
45//
46//    // DBus params for this action
47//    alarm_action_set_dbus_interface(act, "org.fosdem.schedule.AlarmInterface");
48//    alarm_action_set_dbus_service(act, "org.fosdem.schedule");
49//    alarm_action_set_dbus_path(act, "/Fosdem");
50//    alarm_action_set_dbus_name(act, "Alarm");
51//
52//    // DBus arguments for the action
53//    alarm_action_set_dbus_args(act,  DBUS_TYPE_INT32, &aEventId, DBUS_TYPE_INVALID);
54
55    //    act->flags |= ALARM_ACTION_TYPE_EXEC;
56    //     alarm_action_set_exec_command(act, command.toLocal8Bit().data());
57    //    alarm_event_set_icon(eve, "fosdem");
58    //    alarm_event_set_title(eve, "FOSDEM'10");
59    // adds assigned cookie at the end of command string
60    //    act->flags |= ALARM_ACTION_EXEC_ADD_COOKIE;
61
62    /* Add stop button action */
63    /* TODO: send a DBus message to remove that alarm from database */
64    act = alarm_event_add_actions(eve, 1);
65    alarm_action_set_label(act, "Stop");
66    act->flags |= ALARM_ACTION_WHEN_RESPONDED;
67    act->flags |= ALARM_ACTION_TYPE_NOP;
68
69    /* Add snooze button action */
70    act = alarm_event_add_actions(eve, 1);
71    alarm_action_set_label(act, "Snooze");
72    act->flags |= ALARM_ACTION_WHEN_RESPONDED;
73    act->flags |= ALARM_ACTION_TYPE_SNOOZE;
74
75    /* Send the alarm to alarmd */
76    cookie = alarmd_event_add(eve);
77
78    // adding alarm failed
79    if (cookie == 0)
80        emit(addAlarmFailed(aEventId));
81    else
82        emit(alarmAdded(aEventId));
83
84    /* Free all dynamic memory associated with the alarm event */
85    alarm_event_delete(eve);
86
87    return cookie;
88}
89
90void Alarm::deleteAlarm(int aEventId)
91{
92    cookie_t *list = 0;
93    cookie_t cookie = 0;
94    alarm_event_t *event = 0;
95
96    // query the APPID's list of alarms
97    if( (list = alarmd_event_query(0,0, 0,0, APPID)) != 0 ) // query OK
98    {
99        for( int i = 0; (cookie = list[i]) != 0; ++i )
100        {
101            alarm_event_delete(event);
102
103            // get the event for specified alarm cookie (alarmId)
104            if( (event = alarmd_event_get(cookie)) == 0 )
105            {
106                // should we inform user about it ???
107                continue;
108            }
109
110            if(aEventId==atoi(alarm_event_get_message(event)))
111            {
112                // delete cookie
113                if( alarmd_event_del(cookie) == -1 )
114                {
115                    // was not able to delete alarm for given aEventId
116                    emit(deleteAlarmFailed(aEventId));
117                    break;
118                }
119                else
120                {
121                    emit(alarmDeleted(aEventId));
122                    break;
123                }
124            }
125        }
126    }
127    else
128    {
129        // query failed
130    }
131
132    free(list);
133    alarm_event_delete(event);
134}
135
136bool Alarm::hasEventAlarm(int aEventId)
137{
138    cookie_t *list = 0;
139    cookie_t cookie = 0;
140    alarm_event_t *event = 0;
141
142    bool eventHasAlarm = false;
143
144    // query the APPID's list of alarms
145    if( (list = alarmd_event_query(0,0, 0,0, APPID)) != 0 ) // query OK
146    {
147        for( int i = 0; (cookie = list[i]) != 0; ++i )
148        {
149            alarm_event_delete(event);
150
151            // get the event for specified alarm cookie (alarmId)
152            if( (event = alarmd_event_get(cookie)) == 0 )
153            {
154                // should we inform user about it ???
155                continue;
156            }
157
158            if(aEventId==atoi(alarm_event_get_message(event)))
159            {
160                eventHasAlarm = true;
161                break;
162            }
163        }
164    }
165    else
166    {
167        // query failed
168    }
169
170    free(list);
171    alarm_event_delete(event);
172
173    return eventHasAlarm;
174}
175
Note: See TracBrowser for help on using the repository browser.