/* * Copyright 2013 Canonical Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . * * Authors: * Renato Araujo Oliveira Filho * Ubo Riboni */ #include "indicatorclient_datetime.h" #include #include #include #include #include IndicatorClientDateTime::IndicatorClientDateTime(QObject *parent) : IndicatorClientCommon(parent), m_action(0) { setTitle("Date and Time"); setPriority(IndicatorPriority::DATETIME); connect(&m_timer, SIGNAL(timeout()), SLOT(onTimeout())); } IndicatorClientDateTime::~IndicatorClientDateTime() { if (m_action) { delete m_action; } } void IndicatorClientDateTime::init(const QSettings& settings) { IndicatorClientCommon::init(settings); Q_INIT_RESOURCE(indicatorclient_datetime); m_timer.start(1000 * 10); } void IndicatorClientDateTime::shutdown() { m_timer.stop(); } QString IndicatorClientDateTime::dateTime() const { static char timestr[255]; time_t t; struct tm *tmp; t = time(NULL); tmp = localtime(&t); if (tmp == 0) { return ""; } int size = strftime(timestr, 255, m_format.toUtf8().data(), tmp); if (size == 0) { return ""; } return QString::fromLatin1(timestr, size); } bool IndicatorClientDateTime::parseRootElement(const QString &type, QMap data) { if (type == "com.canonical.indicator.root.time") { if (m_action != 0) { delete m_action; } QVariant action = data[QDBusMenuModel::Action]; m_action = actionGroup()->action(action.toString()); if (m_action->isValid()) { updateTimeFormat(m_action->state()); } QObject::connect(m_action, SIGNAL(stateChanged(QVariant)), this, SLOT(updateTimeFormat(QVariant))); return true; } else { return false; } } void IndicatorClientDateTime::updateTimeFormat(const QVariant &state) { if (state.isValid()) { // (sssb) : time format, icon, accessible name format, visible QVariantList states = state.toList(); if (states.size() == 4) { m_format = states[0].toString(); setIcon(QUrl(states[1].toString())); setVisible(states[2].toBool()); setAccessibleName(states[3].toString()); onTimeout(); } } } QQmlComponent *IndicatorClientDateTime::createComponent(QQmlEngine *engine, QObject *parent) const { return new QQmlComponent(engine, QUrl("qrc:/indicatorsclient/datetime/qml/ClockPage.qml"), parent); } void IndicatorClientDateTime::onTimeout() { QString newValue = dateTime(); setLabel(newValue); }