on 331bab5 help sign in
unity8/plugins/IndicatorsClient/indicators/datetime/indicatorclient_datetime.cpp
123 lines 3.3 kB history raw
1/*
2 * Copyright 2013 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authors:
17 * Renato Araujo Oliveira Filho <renato@canonical.com>
18 * Ubo Riboni <ugo.riboni@canonical.com>
19 */
21#include "indicatorclient_datetime.h"
23#include <qdbusmenumodel.h>
24#include <qdbusactiongroup.h>
25#include <qstateaction.h>
26#include <QDateTime>
27#include <QDebug>
29IndicatorClientDateTime::IndicatorClientDateTime(QObject *parent)
30 : IndicatorClientCommon(parent),
31 m_action(0)
33 setTitle("Date and Time");
34 setPriority(IndicatorPriority::DATETIME);
36 connect(&m_timer, SIGNAL(timeout()), SLOT(onTimeout()));
39IndicatorClientDateTime::~IndicatorClientDateTime()
41 if (m_action) {
42 delete m_action;
43 }
46void IndicatorClientDateTime::init(const QSettings& settings)
48 IndicatorClientCommon::init(settings);
49 Q_INIT_RESOURCE(indicatorclient_datetime);
51 m_timer.start(1000 * 10);
54void IndicatorClientDateTime::shutdown()
56 m_timer.stop();
60QString IndicatorClientDateTime::dateTime() const
62 static char timestr[255];
63 time_t t;
64 struct tm *tmp;
66 t = time(NULL);
67 tmp = localtime(&t);
68 if (tmp == 0) {
69 return "";
70 }
72 int size = strftime(timestr, 255, m_format.toUtf8().data(), tmp);
73 if (size == 0) {
74 return "";
75 }
77 return QString::fromLatin1(timestr, size);
80bool IndicatorClientDateTime::parseRootElement(const QString &type, QMap<int, QVariant> data)
82 if (type == "com.canonical.indicator.root.time") {
83 if (m_action != 0) {
84 delete m_action;
85 }
87 QVariant action = data[QDBusMenuModel::Action];
88 m_action = actionGroup()->action(action.toString());
89 if (m_action->isValid()) {
90 updateTimeFormat(m_action->state());
91 }
92 QObject::connect(m_action, SIGNAL(stateChanged(QVariant)), this, SLOT(updateTimeFormat(QVariant)));
93 return true;
94 } else {
95 return false;
96 }
99void IndicatorClientDateTime::updateTimeFormat(const QVariant &state)
101 if (state.isValid()) {
102 // (sssb) : time format, icon, accessible name format, visible
103 QVariantList states = state.toList();
104 if (states.size() == 4) {
105 m_format = states[0].toString();
106 setIcon(QUrl(states[1].toString()));
107 setVisible(states[2].toBool());
108 setAccessibleName(states[3].toString());
109 onTimeout();
110 }
111 }
114QQmlComponent *IndicatorClientDateTime::createComponent(QQmlEngine *engine, QObject *parent) const
116 return new QQmlComponent(engine, QUrl("qrc:/indicatorsclient/datetime/qml/ClockPage.qml"), parent);
119void IndicatorClientDateTime::onTimeout()
121 QString newValue = dateTime();
122 setLabel(newValue);