×
Namespaces

Variants
Actions
(Difference between revisions)

How to get Alarms list in Qt Maemo application

From Nokia Developer Wiki
Jump to: navigation, search
Avis (Talk | contribs)
(Created page with 'Category:MaemoCategory:Qt for Maemo {{CodeSnippet |id= |platform=Qt for Maemo |devices=Nokia N900 |category=Qt, GTK |subcategory= |creationdate=November 25, 2009 |keyword…')
 
hamishwillee (Talk | contribs)
m (Text replace - "<code cpp>" to "<code cpp-qt>")
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
[[Category:Maemo]][[Category:Qt for Maemo]]
+
[[Category:Maemo]][[Category:PIM]][[Category:Qt]]
{{CodeSnippet
+
{{ArticleMetaData <!-- v1.2 -->
|id=
+
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) -->
|platform=Qt for Maemo
+
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|devices=Nokia N900
+
|devices= Nokia N900
|category=Qt, GTK
+
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
|subcategory=
+
|platform= Qt for Maemo
|creationdate=November 25, 2009
+
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
|keywords=alarms, alarmd
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= alarms
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20101130
 +
|author= [[User:Avis]]
 +
<!-- The following are not in current metadata -->
 +
|subcategory= PIM
 
}}
 
}}
  
 
==Overview==
 
==Overview==
  
This example shows how to get list of System alarms in Qt Maemo application.
+
{{Abstract|This example shows how to get list of System alarms in Qt Maemo application. There are no Qt classes for this operation - so using GTK functions.}}
There is now direct Qt classes for this operation - so using GTK functions.
+
  
 
==Project .pro file==
 
==Project .pro file==
<code cpp>
+
<code cpp-qt>
 
// Maemo specific config
 
// Maemo specific config
 
maemo5 {
 
maemo5 {
Line 24: Line 38:
  
 
==Packing in DEB - control file==
 
==Packing in DEB - control file==
<code cpp>
+
<code cpp-qt>
 
// in source section add for Build-depends line
 
// in source section add for Build-depends line
 
libalarm-dev
 
libalarm-dev
Line 30: Line 44:
  
 
==Header==
 
==Header==
<code cpp>
+
<code cpp-qt>
 
#include <time.h>
 
#include <time.h>
 
#include <alarmd/alarm_dbus.h>
 
#include <alarmd/alarm_dbus.h>
Line 38: Line 52:
 
==Source==
 
==Source==
  
<code cpp>
+
<code cpp-qt>
// get the refence to QComboBox for filling
+
// get the reference to QComboBox for filling
void SampleWisget::fillAlarmList(QComboBox* &aBox)
+
void SampleWidget::fillAlarmList(QComboBox* &aBox)
 
{
 
{
 
     // clear the list
 
     // clear the list
Line 59: Line 73:
 
         for (iter = list; *iter != (cookie_t) 0; iter++)  
 
         for (iter = list; *iter != (cookie_t) 0; iter++)  
 
         {
 
         {
                // get alarm event by it's cookie
+
            // get alarm event by it's cookie
                alarm_event_t * aevent = alarmd_event_get (*iter);
+
            alarm_event_t * aevent = alarmd_event_get (*iter);
  // check alarm recurrence
+
            // check alarm recurrence
                int recur = alarm_event_is_recurring (aevent);
+
            int recur = alarm_event_is_recurring (aevent);
  // get time of alarm action
+
            // get time of alarm action
                time_t ttime = alarm_event_get_trigger(aevent);
+
            time_t ttime = alarm_event_get_trigger(aevent);
  // convert GTK's ttime to Qt's DateTime  
+
            // convert GTK's ttime to Qt's DateTime  
  QDateTime qdtm = QDateTime::fromTime_t(ttime);
+
            QDateTime qdtm = QDateTime::fromTime_t(ttime);
  // get time part from DateTime variable
+
            // get time part from DateTime variable
                QTime qtm = qdtm.time();
+
            QTime qtm = qdtm.time();
                // let's count day to current alarm triggering
+
            // let's count day to current alarm triggering
                int days = 0;
+
            int days = 0;
                QDateTime currDate;
+
            QDateTime currDate;
                if (timeFrame > 24) // if we need to look forward for more then one day
+
            if (timeFrame > 24) // if we need to look forward for more then one day
                {
+
            {
                    //  get current date from system
+
                //  get current date from system
                    currDate = QDateTime::currentDateTime();
+
                currDate = QDateTime::currentDateTime();
                    // calculate seconds from current time to alarm's trigger
+
                // calculate seconds from current time to alarm's trigger
                    int secs = currDate.secsTo(qdtm);
+
                int secs = currDate.secsTo(qdtm);
                    // get full days until alarm
+
                // get full days until alarm
                    days = secs / (60 * 60 * 24);
+
                days = secs / (60 * 60 * 24);
                }
+
            }
                // here3 we will form alarm's string
+
            // here we will form alarm's string
                QString sTime;
+
            QString sTime;
                // using locale settings to convert time into string
+
            // using locale settings to convert time into string
                sTime = qtm.toString(Qt::DefaultLocaleShortDate);
+
            sTime = qtm.toString(Qt::DefaultLocaleShortDate);
                if (days > 0) // calc for more than 1 day alarms
+
            if (days > 0) // calc for more than 1 day alarms
                {
+
            {
                    sTime += " (";
+
                sTime += " (";
                    sTime += QString::number(days) + tr("d");
+
                sTime += QString::number(days) + tr("d");
                    sTime += " ";
+
                sTime += " ";
                    // calc remaining time
+
                // calc remaining time
                    // get current time
+
                // get current time
                    QTime currTime = QTime::currentTime();
+
                QTime currTime = QTime::currentTime();
                    // calculate seconds remaining to alarm trigger
+
                // calculate seconds remaining to alarm trigger
                    int remain = currTime.secsTo(qtm);
+
                int remain = currTime.secsTo(qtm);
                    // we need some fix if get negative result
+
                // we need some fix if get negative result
                    if (remain < 0) remain += 86400;
+
                if (remain < 0) remain += 86400;
                    // calc remaining hours
+
                // calc remaining hours
                    int h = remain/3600;
+
                int h = remain/3600;
                    // calc minutes
+
                // calc minutes
                    int m = (remain - h*3600)/60;
+
                int m = (remain - h*3600)/60;
                    // form hour + minutes into QTime to leter get formatted string
+
                // form hour + minutes into QTime to leter get formatted string
                    QTime remTime(h,m);
+
                QTime remTime(h,m);
                    sTime += remTime.toString("H:mm");
+
                sTime += remTime.toString("H:mm");
                    sTime += ")";
+
                sTime += ")";
                }
+
            }
                // get the title of alarm
+
            // get the title of alarm
                QString aTitle;
+
            QString aTitle;
                // each alarm can have Message, Title and AppId
+
            // each alarm can have Message, Title and AppId
                // for the first check the Message
+
            // for the first check the Message
                const char * cTitle = alarm_event_get_message(aevent);
+
            const char * cTitle = alarm_event_get_message(aevent);
 +
            aTitle = QString::fromUtf8(cTitle);
 +
            if (aTitle.isEmpty())
 +
            {
 +
                // next, check the title if message is empty
 +
                cTitle = alarm_event_get_title(aevent);
 
                 aTitle = QString::fromUtf8(cTitle);
 
                 aTitle = QString::fromUtf8(cTitle);
 
                 if (aTitle.isEmpty())
 
                 if (aTitle.isEmpty())
 
                 {
 
                 {
                     // next, check the title if message is empty
+
                     // if message and Title both are empty - use AppID as alarm's title
                     cTitle = alarm_event_get_title(aevent);
+
                     cTitle = alarm_event_get_alarm_appid(aevent);
 
                     aTitle = QString::fromUtf8(cTitle);
 
                     aTitle = QString::fromUtf8(cTitle);
                    if (aTitle.isEmpty())
 
                    {
 
                        // if message and Title both are empty - use AppID as alarm's title
 
                        cTitle = alarm_event_get_alarm_appid(aevent);
 
                        aTitle = QString::fromUtf8(cTitle);
 
                    }
 
 
                 }
 
                 }
                sTime += ": " + aTitle;
+
            }
 +
            sTime += ": " + aTitle;
 +
            aBox->addItem(sTime);
 +
            // free alarm event structure
 +
            alarm_event_delete(aevent);
 
         }
 
         }
        aBox->addItem(sTime);
 
 
     }
 
     }
 
     free(list);
 
     free(list);
Line 135: Line 151:
 
===Postconditions===
 
===Postconditions===
  
QComboBox filled with list of alarms (and time remaining to that alarms)
+
{{Icode|QComboBox}} filled with list of alarms (and time remaining to that alarms)
  
--[[User:Avis|Avis]] 12:24, 30 November 2010 (UTC)
+
--[[User:Avis|Avis]] 12:24, 30 November 2010 (UTC)[[Category:MeeGo Harmattan]] [[Category:Symbian]]

Latest revision as of 04:17, 11 October 2012

Article Metadata
Tested with
Devices(s): Nokia N900
Compatibility
Platform(s): Qt for Maemo
Symbian
Article
Keywords: alarms
Created: Avis (30 Nov 2010)
Last edited: hamishwillee (11 Oct 2012)

Contents

[edit] Overview

This example shows how to get list of System alarms in Qt Maemo application. There are no Qt classes for this operation - so using GTK functions.

[edit] Project .pro file

// Maemo specific config
maemo5 {
LIBS += libalarm.so
}

[edit] Packing in DEB - control file

// in source section add for Build-depends line
libalarm-dev

[edit] Header

#include <time.h>
#include <alarmd/alarm_dbus.h>
#include <alarmd/libalarm.h>

[edit] Source

// get the reference to QComboBox for filling
void SampleWidget::fillAlarmList(QComboBox* &aBox)
{
// clear the list
aBox->clear();
 
cookie_t *list, *iter; // cookie_t - alarm cookies from GTK
time_t now; // time_t structure from GTK
 
// set number of hours to search alarms
int timeFrame = 96;
 
now = time(NULL);
// get list of alarms
list = alarmd_event_query(now, now + timeFrame*3600, ALARM_EVENT_DISABLED, 0, NULL);
 
if (list[0] != (cookie_t) 0) {
// iterate through alarm list
for (iter = list; *iter != (cookie_t) 0; iter++)
{
// get alarm event by it's cookie
alarm_event_t * aevent = alarmd_event_get (*iter);
// check alarm recurrence
int recur = alarm_event_is_recurring (aevent);
// get time of alarm action
time_t ttime = alarm_event_get_trigger(aevent);
// convert GTK's ttime to Qt's DateTime
QDateTime qdtm = QDateTime::fromTime_t(ttime);
// get time part from DateTime variable
QTime qtm = qdtm.time();
// let's count day to current alarm triggering
int days = 0;
QDateTime currDate;
if (timeFrame > 24) // if we need to look forward for more then one day
{
// get current date from system
currDate = QDateTime::currentDateTime();
// calculate seconds from current time to alarm's trigger
int secs = currDate.secsTo(qdtm);
// get full days until alarm
days = secs / (60 * 60 * 24);
}
// here we will form alarm's string
QString sTime;
// using locale settings to convert time into string
sTime = qtm.toString(Qt::DefaultLocaleShortDate);
if (days > 0) // calc for more than 1 day alarms
{
sTime += " (";
sTime += QString::number(days) + tr("d");
sTime += " ";
// calc remaining time
// get current time
QTime currTime = QTime::currentTime();
// calculate seconds remaining to alarm trigger
int remain = currTime.secsTo(qtm);
// we need some fix if get negative result
if (remain < 0) remain += 86400;
// calc remaining hours
int h = remain/3600;
// calc minutes
int m = (remain - h*3600)/60;
// form hour + minutes into QTime to leter get formatted string
QTime remTime(h,m);
sTime += remTime.toString("H:mm");
sTime += ")";
}
// get the title of alarm
QString aTitle;
// each alarm can have Message, Title and AppId
// for the first check the Message
const char * cTitle = alarm_event_get_message(aevent);
aTitle = QString::fromUtf8(cTitle);
if (aTitle.isEmpty())
{
// next, check the title if message is empty
cTitle = alarm_event_get_title(aevent);
aTitle = QString::fromUtf8(cTitle);
if (aTitle.isEmpty())
{
// if message and Title both are empty - use AppID as alarm's title
cTitle = alarm_event_get_alarm_appid(aevent);
aTitle = QString::fromUtf8(cTitle);
}
}
sTime += ": " + aTitle;
aBox->addItem(sTime);
// free alarm event structure
alarm_event_delete(aevent);
}
}
free(list);
}


[edit] Postconditions

QComboBox filled with list of alarms (and time remaining to that alarms)

--Avis 12:24, 30 November 2010 (UTC)

This page was last modified on 11 October 2012, at 04:17.
51 page views in the last 30 days.