on fa85355 help sign in
unity8/plugins/IndicatorsClient/indicators/datetime/indicatorclient_datetime.cpp
121 lines 3.2 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);
50 m_timer.start(1000 * 10);
53void IndicatorClientDateTime::shutdown()
55 m_timer.stop();
59QString IndicatorClientDateTime::dateTime() const
61 static char timestr[255];
62 time_t t;
63 struct tm *tmp;
65 t = time(NULL);
66 tmp = localtime(&t);
67 if (tmp == 0) {
68 return "";
69 }
71 int size = strftime(timestr, 255, m_format.toUtf8().data(), tmp);
72 if (size == 0) {
73 return "";
74 }
76 return QString::fromLatin1(timestr, size);
79bool IndicatorClientDateTime::parseRootElement(const QString &type, QMap<int, QVariant> data)
81 if (type == "com.canonical.indicator.root.time") {
82 if (m_action != 0) {
83 delete m_action;
84 }
86 QVariant action = data[QDBusMenuModel::Action];
87 m_action = actionGroup()->action(action.toString());
88 if (m_action->isValid()) {
89 updateTimeFormat(m_action->state());
90 }
91 QObject::connect(m_action, SIGNAL(stateChanged(QVariant)), this, SLOT(updateTimeFormat(QVariant)));
92 return true;
93 } else {
94 return false;
95 }
98void IndicatorClientDateTime::updateTimeFormat(const QVariant &state)
100 if (state.isValid()) {
101 // (sssb) : time format, icon, accessible name format, visible
102 QVariantList states = state.toList();
103 if (states.size() == 4) {
104 m_format = states[0].toString();
105 setIcon(QUrl(states[1].toString()));
106 setVisible(states[2].toBool());
107 onTimeout();
108 }
109 }
112QUrl IndicatorClientDateTime::componentSource() const
114 return QUrl("qrc:/indicatorsclient/qml/DatetimeIndicator.qml");
117void IndicatorClientDateTime::onTimeout()
119 QString newValue = dateTime();
120 setLabel(newValue);