source: src/alarm/alarm.cpp @ 83f0ba7

qt5
Last change on this file since 83f0ba7 was 5d9409d, checked in by timkoma <timkoma@…>, 12 years ago

UTC/LocalTime fix for import conference XML, DB queries for multiple conferences fixes

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