A header with the proper copyright/lincence statement was added into each source...
[toast/confclerk.git] / src / alarm / alarm.cpp
index af3120e..0cfb31b 100644 (file)
+/*
+ * Copyright (C) 2010 Ixonos Plc.
+ *
+ * This file is part of fosdem-schedule.
+ *
+ * fosdem-schedule is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation, either version 2 of the License, or (at your option)
+ * any later version.
+ *
+ * fosdem-schedule is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * fosdem-schedule.  If not, see <http://www.gnu.org/licenses/>.
+ */
 #include "alarm.h"
 
 #include <QDateTime>
 
+#include <QApplication>
+#include <QDir>
+#include <QFileInfo>
+
+#include <QDebug>
+
+//#include <dbus-1.0/dbus/dbus-protocol.h>
+
 int Alarm::addAlarm(int aEventId, const QDateTime &aDateTime)
 {
     cookie_t cookie = 0;
-    alarm_event_t *event = 0;
-    alarm_action_t *action = 0;
+    alarm_event_t *eve = 0;
+    alarm_action_t *act = 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
+    eve = alarm_event_create();
+    alarm_event_set_alarm_appid(eve, APPID);
+
+    /* for Deleting purposes */
+    alarm_event_set_message(eve, QString::number(aEventId).toLocal8Bit().data());
 
     /* Use absolute time triggering */
-    event->alarm_time = aDateTime.toTime_t();
+    //eve->alarm_time = time(0) + 5; // for testing (5 seconds from now)
+    eve->alarm_time = aDateTime.toTime_t();
+    eve->flags = ALARM_EVENT_BOOT;
 
     /* 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
+    act = alarm_event_add_actions(eve, 1);
+    alarm_action_set_label(act, "FOSDEM'10");
+
+    QString command = QFileInfo(*qApp->argv()).absoluteFilePath() + QString(" %1").arg(QString::number(aEventId));
+    qDebug() << "Setting alarm: " << command;
+    alarm_action_set_exec_command(act, command.toLocal8Bit().data());
+    act->flags |= ALARM_ACTION_TYPE_EXEC;
+    act->flags |= ALARM_ACTION_WHEN_RESPONDED;
+    act->flags |= ALARM_ACTION_EXEC_ADD_COOKIE; // adds assigned cookie at the end of command string
+
+//    // setup this action to be a "DBus command"
+//    act->flags |= ALARM_ACTION_WHEN_RESPONDED;
+//    act->flags |= ALARM_ACTION_TYPE_DBUS;
+//
+//    // DBus params for this action
+//    alarm_action_set_dbus_interface(act, "org.fosdem.schedule.AlarmInterface");
+//    alarm_action_set_dbus_service(act, "org.fosdem.schedule");
+//    alarm_action_set_dbus_path(act, "/Fosdem");
+//    alarm_action_set_dbus_name(act, "Alarm");
+//
+//    // DBus arguments for the action
+//    alarm_action_set_dbus_args(act,  DBUS_TYPE_INT32, &aEventId, DBUS_TYPE_INVALID);
+
+    //    act->flags |= ALARM_ACTION_TYPE_EXEC;
+    //     alarm_action_set_exec_command(act, command.toLocal8Bit().data());
+    //    alarm_event_set_icon(eve, "fosdem");
+    //    alarm_event_set_title(eve, "FOSDEM'10");
+    // adds assigned cookie at the end of command string
+    //    act->flags |= ALARM_ACTION_EXEC_ADD_COOKIE;
+
+    /* Add stop button action */
+    /* TODO: send a DBus message to remove that alarm from database */
+    act = alarm_event_add_actions(eve, 1);
+    alarm_action_set_label(act, "Stop");
+    act->flags |= ALARM_ACTION_WHEN_RESPONDED;
+    act->flags |= ALARM_ACTION_TYPE_NOP;
+
+    /* Add snooze button action */
+    act = alarm_event_add_actions(eve, 1);
+    alarm_action_set_label(act, "Snooze");
+    act->flags |= ALARM_ACTION_WHEN_RESPONDED;
+    act->flags |= ALARM_ACTION_TYPE_SNOOZE;
 
     /* Send the alarm to alarmd */
-    cookie = alarmd_event_add(event);
-    if(cookie==0) // adding alarm failed
+    cookie = alarmd_event_add(eve);
+
+    // adding alarm failed
+    if (cookie == 0)
         emit(addAlarmFailed(aEventId));
     else
         emit(alarmAdded(aEventId));
 
     /* Free all dynamic memory associated with the alarm event */
-    alarm_event_delete(event);
+    alarm_event_delete(eve);
 
     return cookie;
 }
@@ -83,3 +151,43 @@ void Alarm::deleteAlarm(int aEventId)
     alarm_event_delete(event);
 }
 
+bool Alarm::hasEventAlarm(int aEventId)
+{
+    cookie_t *list = 0;
+    cookie_t cookie = 0;
+    alarm_event_t *event = 0;
+
+    bool eventHasAlarm = false;
+
+    // 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)))
+            {
+                eventHasAlarm = true;
+                break;
+            }
+        }
+    }
+    else
+    {
+        // query failed
+    }
+
+    free(list);
+    alarm_event_delete(event);
+
+    return eventHasAlarm;
+}
+