From 6ae10266001ffdab474cc686af03bc17f1f6fa7f Mon Sep 17 00:00:00 2001 From: pavelpa Date: Mon, 18 Jan 2010 12:27:57 +0000 Subject: [PATCH] started work on alarm(libaalarm) --- src/alarm/alarm.cpp | 85 +++++++++++++++++++++++++++++++++++++++++ src/alarm/alarm.h | 30 +++++++++++++++ src/alarm/alarm.pro | 13 +++++++ src/fosdem.pro | 2 + src/gui/alarmdialog.cpp | 49 ++++++++++++++++++++++++ src/gui/alarmdialog.h | 22 +++++++++++ src/gui/alarmdialog.ui | 85 +++++++++++++++++++++++++++++++++++++++++ src/gui/gui.pro | 17 +++++---- 8 files changed, 296 insertions(+), 7 deletions(-) create mode 100644 src/alarm/alarm.cpp create mode 100644 src/alarm/alarm.h create mode 100644 src/alarm/alarm.pro create mode 100644 src/gui/alarmdialog.cpp create mode 100644 src/gui/alarmdialog.h create mode 100644 src/gui/alarmdialog.ui diff --git a/src/alarm/alarm.cpp b/src/alarm/alarm.cpp new file mode 100644 index 0000000..af3120e --- /dev/null +++ b/src/alarm/alarm.cpp @@ -0,0 +1,85 @@ +#include "alarm.h" + +#include + +int Alarm::addAlarm(int aEventId, const QDateTime &aDateTime) +{ + cookie_t cookie = 0; + alarm_event_t *event = 0; + alarm_action_t *action = 0; + + /* Create alarm event structure and set application identifier */ + event = alarm_event_create(); + alarm_event_set_alarm_appid(event, APPID); + alarm_event_set_message(event, QString::number(aEventId).toLocal8Bit().data()); // for Deleting purposes + + /* Use absolute time triggering */ + event->alarm_time = aDateTime.toTime_t(); + + /* Add exec command action */ + action = alarm_event_add_actions(event, 1); + QString command = QString("/home/maemo/work/alarm/bin/alarmdialog %1").arg(QString::number(aEventId)); + alarm_action_set_exec_command(action, command.toLocal8Bit().data()); + action->flags |= ALARM_ACTION_TYPE_EXEC; + action->flags |= ALARM_ACTION_WHEN_TRIGGERED; + action->flags |= ALARM_ACTION_EXEC_ADD_COOKIE; // adds assigned cookie at the end of command string + + /* Send the alarm to alarmd */ + cookie = alarmd_event_add(event); + if(cookie==0) // adding alarm failed + emit(addAlarmFailed(aEventId)); + else + emit(alarmAdded(aEventId)); + + /* Free all dynamic memory associated with the alarm event */ + alarm_event_delete(event); + + return cookie; +} + +void Alarm::deleteAlarm(int aEventId) +{ + cookie_t *list = 0; + cookie_t cookie = 0; + alarm_event_t *event = 0; + + // query the APPID's list of alarms + if( (list = alarmd_event_query(0,0, 0,0, APPID)) != 0 ) // query OK + { + for( int i = 0; (cookie = list[i]) != 0; ++i ) + { + alarm_event_delete(event); + + // get the event for specified alarm cookie (alarmId) + if( (event = alarmd_event_get(cookie)) == 0 ) + { + // should we inform user about it ??? + continue; + } + + if(aEventId==atoi(alarm_event_get_message(event))) + { + // delete cookie + if( alarmd_event_del(cookie) == -1 ) + { + // was not able to delete alarm for given aEventId + emit(deleteAlarmFailed(aEventId)); + break; + } + else + { + emit(alarmDeleted(aEventId)); + break; + } + } + } + } + else + { + // query failed + } + + free(list); + alarm_event_delete(event); +} + diff --git a/src/alarm/alarm.h b/src/alarm/alarm.h new file mode 100644 index 0000000..dd19bd9 --- /dev/null +++ b/src/alarm/alarm.h @@ -0,0 +1,30 @@ +#ifndef ALARM_H +#define ALARM_H + +#include +#include + +extern "C" +{ +#include +} + +#define APPID "alarm-example" + +class Alarm : public QObject +{ + Q_OBJECT + public: + Alarm() {} + ~Alarm() {} + int addAlarm(int aEventId, const QDateTime &aDateTime); + void deleteAlarm(int aEventId); + signals: + void alarmAdded(int aEventId); + void addAlarmFailed(int aEventId); + void alarmDeleted(int aEventId); + void deleteAlarmFailed(int aEventId); +}; + +#endif /* ALARM_H */ + diff --git a/src/alarm/alarm.pro b/src/alarm/alarm.pro new file mode 100644 index 0000000..ea4b04b --- /dev/null +++ b/src/alarm/alarm.pro @@ -0,0 +1,13 @@ +TEMPLATE = lib +TARGET = qalarm +DESTDIR = ../bin +CONFIG += static + +# module dependencies +LIBS += -lalarm +DEPENDPATH += . + +HEADERS += alarm.h + +SOURCES += alarm.cpp + diff --git a/src/fosdem.pro b/src/fosdem.pro index 5e72c0e..fb46540 100644 --- a/src/fosdem.pro +++ b/src/fosdem.pro @@ -1,5 +1,7 @@ +include(global.pri) TEMPLATE = subdirs SUBDIRS = orm mvc sql gui app +maemo : SUBDIRS += alarm #SUBDIRS += test CONFIG += ordered diff --git a/src/gui/alarmdialog.cpp b/src/gui/alarmdialog.cpp new file mode 100644 index 0000000..413ca89 --- /dev/null +++ b/src/gui/alarmdialog.cpp @@ -0,0 +1,49 @@ +#include "alarmdialog.h" + +#include +#include + +const int SNOOZE_TIME = 5; // in minutes + +AlarmDialog::AlarmDialog(int argc, char *argv[], QWidget *aParent) + : QDialog(aParent) + , mEventId(0) + , mAlarmId(0) +{ + setupUi(this); + + if(argc<3) + { + // not enough arguments passed to the dialog + // Usage: $ ./dialog eventId alarmId + // Example: $ ./dialog 521 13 + // + // TODO: handle this case + } + else + { + mEventId = QString(argv[1]).toInt(); + mAlarmId = QString(argv[2]).toInt(); + } + + connect(stopPB, SIGNAL(clicked()), qApp, SLOT(quit())); + connect(appPB, SIGNAL(clicked()), SLOT(runApp())); + connect(snoozePB, SIGNAL(clicked()), SLOT(snooze())); + + message->setText(QString(argv[1]).append("-").append(QString(argv[2]))); +} + +void AlarmDialog::runApp() +{ +} + +void AlarmDialog::snooze() +{ + if(mEventId==0) // not valid event ID + return; + + Alarm alarm; + alarm.addAlarm(mEventId,QDateTime::currentDateTime().addSecs(60*SNOOZE_TIME)); + qApp->quit(); +} + diff --git a/src/gui/alarmdialog.h b/src/gui/alarmdialog.h new file mode 100644 index 0000000..c77031a --- /dev/null +++ b/src/gui/alarmdialog.h @@ -0,0 +1,22 @@ +#ifndef ALARMDIALOG_H +#define ALARMDIALOG_H + +#include +#include "ui_alarmdialog.h" + +class AlarmDialog : public QDialog, Ui::AlarmDialog +{ + Q_OBJECT + public: + AlarmDialog(int argc, char *argv[], QWidget *aParent = NULL); + ~AlarmDialog() {} + private slots: + void runApp(); + void snooze(); + private: + int mEventId; // event ID obtained from 'schedule' + int mAlarmId; // cookie assigned by alarmd +}; + +#endif /* ALARMDIALOG_H */ + diff --git a/src/gui/alarmdialog.ui b/src/gui/alarmdialog.ui new file mode 100644 index 0000000..c150231 --- /dev/null +++ b/src/gui/alarmdialog.ui @@ -0,0 +1,85 @@ + + AlarmDialog + + + + 0 + 0 + 334 + 135 + + + + Dialog + + + + + + + + + + + + + :/icons/fosdem.png + + + + + + + Alarm message goes here + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + App + + + + + + + Snooze + + + + + + + Stop + + + + + + + + + + + + + + diff --git a/src/gui/gui.pro b/src/gui/gui.pro index 0abcbbd..8d62f28 100644 --- a/src/gui/gui.pro +++ b/src/gui/gui.pro @@ -5,10 +5,10 @@ CONFIG += static QT += sql xml # module dependencies -LIBS += -L$$DESTDIR -lmvc -lorm -lsql -INCLUDEPATH += ../orm ../mvc ../sql -DEPENDPATH += . ../orm ../mvc ../sql -TARGETDEPS += $$DESTDIR/liborm.a $$DESTDIR/libmvc.a $$DESTDIR/libsql.a +LIBS += -L$$DESTDIR -lmvc -lorm -lsql -lqalarm +INCLUDEPATH += ../orm ../mvc ../sql ../alarm +DEPENDPATH += . ../orm ../mvc ../sql ../alarm +TARGETDEPS += $$DESTDIR/liborm.a $$DESTDIR/libmvc.a $$DESTDIR/libsql.a $$DESTDIR/libqalarm.a # A shamelessly long list of sources, headers and forms. @@ -18,11 +18,14 @@ TARGETDEPS += $$DESTDIR/liborm.a $$DESTDIR/libmvc.a $$DESTDIR/libsql.a FORMS += mainwindow.ui \ daynavigatorwidget.ui \ - about.ui + about.ui \ + alarmdialog.ui HEADERS += mainwindow.h \ - daynavigatorwidget.h + daynavigatorwidget.h \ + alarmdialog.h SOURCES += mainwindow.cpp \ - daynavigatorwidget.cpp + daynavigatorwidget.cpp \ + alarmdialog.cpp -- 2.39.5