source: src/alarm/alarm.cpp @ 6df32f2

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

Update GPL blurb in source files.

  • Property mode set to 100644
File size: 5.9 KB
RevLine 
[ca90cb1]1/*
2 * Copyright (C) 2010 Ixonos Plc.
3 *
[6df32f2]4 * This file is part of ConfClerk.
[ca90cb1]5 *
[6df32f2]6 * ConfClerk is free software: you can redistribute it and/or modify it
[ca90cb1]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 *
[6df32f2]11 * ConfClerk is distributed in the hope that it will be useful, but
[ca90cb1]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
[6df32f2]17 * ConfClerk.  If not, see <http://www.gnu.org/licenses/>.
[ca90cb1]18 */
[6ae1026]19#include "alarm.h"
20
21#include <QDateTime>
22
[d4a8bbf]23#include <QApplication>
24#include <QDir>
[a7d8638]25#include <QFileInfo>
[6831c6a]26
[2104023]27#include <QDebug>
28
[17cb895]29//#include <dbus-1.0/dbus/dbus-protocol.h>
[d4a8bbf]30
[6ae1026]31int Alarm::addAlarm(int aEventId, const QDateTime &aDateTime)
32{
33    cookie_t cookie = 0;
[6197713]34    alarm_event_t *eve = 0;
35    alarm_action_t *act = 0;
[6ae1026]36
37    /* Create alarm event structure and set application identifier */
[6197713]38    eve = alarm_event_create();
39    alarm_event_set_alarm_appid(eve, APPID);
40
41    /* for Deleting purposes */
[6831c6a]42    alarm_event_set_message(eve, QString::number(aEventId).toLocal8Bit().data());
[6ae1026]43
44    /* Use absolute time triggering */
[2104023]45    //eve->alarm_time = time(0) + 5; // for testing (5 seconds from now)
[ba3fc20]46    QDateTime local( aDateTime);
47    qDebug() << "UTC: " << local.toTime_t();
48    local.setTimeSpec(Qt::LocalTime);
49    qDebug() << "LocalTime: " << local.toTime_t();
[5d9409d]50
[ba3fc20]51    eve->alarm_time = local.toTime_t();
[6645e1f]52    eve->flags = ALARM_EVENT_BOOT;
[6197713]53
[6ae1026]54    /* Add exec command action */
[6197713]55    act = alarm_event_add_actions(eve, 1);
56    alarm_action_set_label(act, "FOSDEM'10");
[6645e1f]57
[2104023]58    QString command = QFileInfo(*qApp->argv()).absoluteFilePath() + QString(" %1").arg(QString::number(aEventId));
59    qDebug() << "Setting alarm: " << command;
[a7d8638]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);
[6645e1f]77
78    //    act->flags |= ALARM_ACTION_TYPE_EXEC;
79    //     alarm_action_set_exec_command(act, command.toLocal8Bit().data());
[6197713]80    //    alarm_event_set_icon(eve, "fosdem");
81    //    alarm_event_set_title(eve, "FOSDEM'10");
[6831c6a]82    // adds assigned cookie at the end of command string
83    //    act->flags |= ALARM_ACTION_EXEC_ADD_COOKIE;
[6197713]84
85    /* Add stop button action */
[6645e1f]86    /* TODO: send a DBus message to remove that alarm from database */
[6197713]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;
[6831c6a]91
[6197713]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;
[6ae1026]97
98    /* Send the alarm to alarmd */
[6197713]99    cookie = alarmd_event_add(eve);
100
101    // adding alarm failed
102    if (cookie == 0)
[6ae1026]103        emit(addAlarmFailed(aEventId));
104    else
105        emit(alarmAdded(aEventId));
106
107    /* Free all dynamic memory associated with the alarm event */
[6197713]108    alarm_event_delete(eve);
[6ae1026]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
[b6cd05c]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.