source: src/alarm/alarm.cpp @ 68b2df2

qt5
Last change on this file since 68b2df2 was 68b2df2, checked in by gregor herrmann <gregoa@…>, 10 years ago

Add copyright to source.

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