source: src/alarm/alarm.cpp @ d785a93

qt5
Last change on this file since d785a93 was ff9a2cb, checked in by gregor herrmann <gregoa@…>, 11 years ago

Show event title instead of id in alarms.

  • Property mode set to 100644
File size: 6.0 KB
Line 
1/*
2 * Copyright (C) 2010 Ixonos Plc.
3 * Copyright (C) 2011 Philipp Spitzer, gregor herrmann
4 *
5 * This file is part of ConfClerk.
6 *
7 * ConfClerk is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * ConfClerk is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * ConfClerk.  If not, see <http://www.gnu.org/licenses/>.
19 */
20#include "alarm.h"
21
22#include <QDateTime>
23
24#include <QApplication>
25#include <QDir>
26#include <QFileInfo>
27
28#include <QDebug>
29
30//#include <dbus-1.0/dbus/dbus-protocol.h>
31
32int Alarm::addAlarm(int aEventId, QString aEventTitle, const QDateTime &aDateTime)
33{
34    cookie_t cookie = 0;
35    alarm_event_t *eve = 0;
36    alarm_action_t *act = 0;
37
38    /* Create alarm event structure and set application identifier */
39    eve = alarm_event_create();
40    alarm_event_set_alarm_appid(eve, APPID);
41
42    /* for Deleting purposes */
43    // ?!
44    //alarm_event_set_message(eve, QString::number(aEventId).toLocal8Bit().data());
45    alarm_event_set_message(eve, aEventTitle.toLocal8Bit().data());
46
47    /* Use absolute time triggering */
48    //eve->alarm_time = time(0) + 5; // for testing (5 seconds from now)
49    QDateTime local( aDateTime);
50    qDebug() << "UTC: " << local.toTime_t();
51    local.setTimeSpec(Qt::LocalTime);
52    qDebug() << "LocalTime: " << local.toTime_t();
53
54    eve->alarm_time = local.toTime_t();
55    eve->flags = ALARM_EVENT_BOOT;
56
57    /* Add exec command action */
58    act = alarm_event_add_actions(eve, 1);
59    alarm_action_set_label(act, "ConfClerk");
60
61    QString command = QFileInfo(*qApp->argv()).absoluteFilePath() + QString(" %1").arg(QString::number(aEventId));
62    qDebug() << "Setting alarm: " << command;
63    alarm_action_set_exec_command(act, command.toLocal8Bit().data());
64    act->flags |= ALARM_ACTION_TYPE_EXEC;
65    act->flags |= ALARM_ACTION_WHEN_RESPONDED;
66    act->flags |= ALARM_ACTION_EXEC_ADD_COOKIE; // adds assigned cookie at the end of command string
67
68//    // setup this action to be a "DBus command"
69//    act->flags |= ALARM_ACTION_WHEN_RESPONDED;
70//    act->flags |= ALARM_ACTION_TYPE_DBUS;
71//
72//    // DBus params for this action
73//    alarm_action_set_dbus_interface(act, "at.priv.toastfreeware.confclerk.AlarmInterface");
74//    alarm_action_set_dbus_service(act, "at.priv.toastfreeware.confclerk");
75//    alarm_action_set_dbus_path(act, "/ConfClerk");
76//    alarm_action_set_dbus_name(act, "Alarm");
77//
78//    // DBus arguments for the action
79//    alarm_action_set_dbus_args(act,  DBUS_TYPE_INT32, &aEventId, DBUS_TYPE_INVALID);
80
81    //    act->flags |= ALARM_ACTION_TYPE_EXEC;
82    //     alarm_action_set_exec_command(act, command.toLocal8Bit().data());
83    //    alarm_event_set_icon(eve, "fosdem");
84    //    alarm_event_set_title(eve, "ConfClerk");
85    // adds assigned cookie at the end of command string
86    //    act->flags |= ALARM_ACTION_EXEC_ADD_COOKIE;
87
88    /* Add stop button action */
89    /* TODO: send a DBus message to remove that alarm from database */
90    act = alarm_event_add_actions(eve, 1);
91    alarm_action_set_label(act, "Stop");
92    act->flags |= ALARM_ACTION_WHEN_RESPONDED;
93    act->flags |= ALARM_ACTION_TYPE_NOP;
94
95    /* Add snooze button action */
96    act = alarm_event_add_actions(eve, 1);
97    alarm_action_set_label(act, "Snooze");
98    act->flags |= ALARM_ACTION_WHEN_RESPONDED;
99    act->flags |= ALARM_ACTION_TYPE_SNOOZE;
100
101    /* Send the alarm to alarmd */
102    cookie = alarmd_event_add(eve);
103
104    // adding alarm failed
105    if (cookie == 0)
106        emit(addAlarmFailed(aEventId));
107    else
108        emit(alarmAdded(aEventId));
109
110    /* Free all dynamic memory associated with the alarm event */
111    alarm_event_delete(eve);
112
113    return cookie;
114}
115
116void Alarm::deleteAlarm(int aEventId)
117{
118    cookie_t *list = 0;
119    cookie_t cookie = 0;
120    alarm_event_t *event = 0;
121
122    // query the APPID's list of alarms
123    if( (list = alarmd_event_query(0,0, 0,0, APPID)) != 0 ) // query OK
124    {
125        for( int i = 0; (cookie = list[i]) != 0; ++i )
126        {
127            alarm_event_delete(event);
128
129            // get the event for specified alarm cookie (alarmId)
130            if( (event = alarmd_event_get(cookie)) == 0 )
131            {
132                // should we inform user about it ???
133                continue;
134            }
135
136            if(aEventId==atoi(alarm_event_get_message(event)))
137            {
138                // delete cookie
139                if( alarmd_event_del(cookie) == -1 )
140                {
141                    // was not able to delete alarm for given aEventId
142                    emit(deleteAlarmFailed(aEventId));
143                    break;
144                }
145                else
146                {
147                    emit(alarmDeleted(aEventId));
148                    break;
149                }
150            }
151        }
152    }
153    else
154    {
155        // query failed
156    }
157
158    free(list);
159    alarm_event_delete(event);
160}
161
162bool Alarm::hasEventAlarm(int aEventId)
163{
164    cookie_t *list = 0;
165    cookie_t cookie = 0;
166    alarm_event_t *event = 0;
167
168    bool eventHasAlarm = false;
169
170    // query the APPID's list of alarms
171    if( (list = alarmd_event_query(0,0, 0,0, APPID)) != 0 ) // query OK
172    {
173        for( int i = 0; (cookie = list[i]) != 0; ++i )
174        {
175            alarm_event_delete(event);
176
177            // get the event for specified alarm cookie (alarmId)
178            if( (event = alarmd_event_get(cookie)) == 0 )
179            {
180                // should we inform user about it ???
181                continue;
182            }
183
184            if(aEventId==atoi(alarm_event_get_message(event)))
185            {
186                eventHasAlarm = true;
187                break;
188            }
189        }
190    }
191    else
192    {
193        // query failed
194    }
195
196    free(list);
197    alarm_event_delete(event);
198
199    return eventHasAlarm;
200}
201
Note: See TracBrowser for help on using the repository browser.