91d5e30e880c830dffda41a9f13b4e8d2dbacd80
[staging/HomeScreen.git] / SampleAppTimeDate / src / timedateprovider.cpp
1 #include "timedateprovider.h"
2 #include <QTimerEvent>
3
4 TimeDateProvider::TimeDateProvider(QObject *parent) :
5     QObject(parent),
6     m_secondsTimerId(-1),
7     mp_dBusStatusBarProxy(0),
8     m_statusBarPlaceholder(-1)
9 {
10     qDebug("D-Bus: register as org.agl.SampleAppTimeDate");
11     // dbus setup
12     QDBusConnection dbus = QDBusConnection::sessionBus();
13
14     dbus.registerObject("/", this);
15     dbus.registerService("org.agl.sampleapptimedate");
16
17
18     qDebug("D-Bus: connect to org.agl.mainwindow /StatusBar");
19     mp_dBusStatusBarProxy = new org::agl::statusbar("org.agl.homescreen",
20                                               "/StatusBar",
21                                               QDBusConnection::sessionBus(),
22                                               0);
23 }
24
25 TimeDateProvider::~TimeDateProvider()
26 {
27     stop();
28
29     if (0 != mp_dBusStatusBarProxy)
30     {
31         qDebug("x");
32         mp_dBusStatusBarProxy->setStatusText(1, "");
33         delete mp_dBusStatusBarProxy;
34     }
35 }
36
37 void TimeDateProvider::start()
38 {
39     qDebug("trying to start timer (if this lasts long, maybe the Home Screen Application is not launched.");
40     if ((-1 == m_statusBarPlaceholder) && (0 != mp_dBusStatusBarProxy))
41     {
42         QList<int> availablePlaceholder = mp_dBusStatusBarProxy->getAvailablePlaceholders();
43         if (availablePlaceholder.size() > 0)
44         {
45             // just take the first available placeholder
46             m_statusBarPlaceholder = availablePlaceholder[0];
47             qDebug("- using statusbar placeholder %d", m_statusBarPlaceholder);
48
49             qDebug("- timer started");
50             // callback every second
51             m_secondsTimerId = startTimer(1000);
52         }
53     }
54 }
55
56 void TimeDateProvider::stop()
57 {
58     if (-1 != m_secondsTimerId)
59     {
60         killTimer(m_secondsTimerId);
61         m_secondsTimerId = -1;
62         m_statusBarPlaceholder = -1;
63     }
64 }
65
66 void TimeDateProvider::timerEvent(QTimerEvent *e)
67 {
68     if (e->timerId() == m_secondsTimerId)
69     {
70         if (0 != mp_dBusStatusBarProxy)
71         {
72             QString toDisplay = QDateTime::currentDateTime().toString("hh:mm");
73             qDebug("%s", toDisplay.toStdString().c_str());
74             mp_dBusStatusBarProxy->setStatusText(m_statusBarPlaceholder, toDisplay);
75         }
76     }
77 }