Ejemplo n.º 1
0
Maemo::Timed::Event tst_Events::createEvent(const qint64 timestamp, const int dueInSeconds)
{
    // Create an event, which will go off in dueInSeconds
    Maemo::Timed::Event event;
    event.setAttribute("APPLICATION", APPNAME);
    event.setAttribute("TITLE", QString("%1").arg(timestamp));
    event.setAlarmFlag();
    event.setReminderFlag();
    QDateTime dateTime;
    dateTime.setMSecsSinceEpoch(timestamp);
    event.setTicker(dateTime.toTime_t() + dueInSeconds);

    // Add an action to the event, the action writes a message to a file when the event is triggered
    Maemo::Timed::Event::Action &act = event.addAction();
    act.setSendCookieFlag();
    // Timed will replace <COOKIE> with the cookie of the event that triggers this action
    QString message = QString(ACTIONSTRING).arg(timestamp).arg("<COOKIE>");
    act.runCommand(QString("echo -n %1 > %2")
                   .arg(message)
                   .arg(PATH));
    act.whenTriggered();

    return event;
}
void AlarmObject::save()
{
    try {
        Maemo::Timed::Event ev;
        // Keep the event after it has triggered
        ev.setKeepAliveFlag();
        // Trigger the voland alarm/reminder dialog
        ev.setReminderFlag();
        if (!m_title.isEmpty())
            ev.setAttribute(QLatin1String("TITLE"), m_title);

        ev.setAttribute(QLatin1String("timeOfDay"), QString::number(m_hour * 60 + m_minute));
        ev.setAttribute(QLatin1String("APPLICATION"), QLatin1String("nemoalarms"));
        ev.setAttribute(QLatin1String("createdDate"), QString::number(m_createdDate.toMSecsSinceEpoch()));
        ev.setAlarmFlag();

        if (!m_countdown) {
            ev.setBootFlag();

            if (!m_daysOfWeek.isEmpty())
                ev.setAttribute(QLatin1String("daysOfWeek"), m_daysOfWeek);

            if (m_enabled) {
                Maemo::Timed::Event::Recurrence rec = ev.addRecurrence();
                rec.addHour(m_hour);
                rec.addMinute(m_minute);
                rec.everyDayOfMonth();
                rec.everyMonth();

                // Single-shot alarms are done with a recurrence and the single-shot
                // flag, which removes recurrence information after the first trigger.
                if (m_daysOfWeek.isEmpty()) {
                    rec.everyDayOfWeek();
                    ev.setSingleShotFlag();
                }

                // Map characters to numeric weekdays used by libtimed
                QString weekdayMap(QLatin1String("SmtwTfs"));
                for (int i = 0; i < m_daysOfWeek.size(); i++) {
                    int day = weekdayMap.indexOf(m_daysOfWeek[i]);
                    if (day >= 0)
                        rec.addDayOfWeek(day);
                }
            }
            ev.setAttribute(QLatin1String("type"), QLatin1String("clock"));
        } else {
            uint duration = m_hour * 3600 + m_minute * 60;
            QDateTime now = QDateTime::currentDateTimeUtc();
            if (m_enabled) {
                QDateTime triggerDateTime = now.addSecs(duration - m_elapsed);
                m_triggerTime = triggerDateTime.toTime_t();
                ev.setTicker(triggerDateTime.toTime_t());
            } else {
                if (m_triggerTime > 0) {
                    m_elapsed = (duration - (m_triggerTime - now.toTime_t()));
                    m_triggerTime = 0;
                } else {
                    m_elapsed = 0;
                }
                emit elapsedChanged();
                ev.setAttribute(QLatin1String("elapsed"), QString::number(m_elapsed));
            }
            emit triggerTimeChanged();
            ev.setAttribute(QLatin1String("triggerTime"), QString::number(m_triggerTime));
            ev.setAttribute(QLatin1String("type"), QLatin1String("countdown"));
        }

        QDBusPendingCallWatcher *w;
        if (m_cookie)
            w = new QDBusPendingCallWatcher(TimedInterface::instance()->replace_event_async(ev, m_cookie), this);
        else
            w = new QDBusPendingCallWatcher(TimedInterface::instance()->add_event_async(ev), this);
        connect(w, SIGNAL(finished(QDBusPendingCallWatcher*)), SLOT(saveReply(QDBusPendingCallWatcher*)));

        // Emit the updated signal immediately to update UI
        emit updated();
    } catch (Maemo::Timed::Exception &e) {
        qWarning() << "org.nemomobile.alarms: Cannot sync alarm to timed:" << e.what();
    }
}