~ubuntu-branches/ubuntu/saucy/indicators-client/saucy-proposed

« back to all changes in this revision

Viewing changes to plugins/datetimeplugin/chewie_datetimeplugin.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2013-05-02 22:59:25 UTC
  • Revision ID: package-import@ubuntu.com-20130502225925-15hjnoqshx6e02nm
Tags: upstream-0.31daily13.05.02
ImportĀ upstreamĀ versionĀ 0.31daily13.05.02

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 */
 
20
 
 
21
#include "chewie_datetimeplugin.h"
 
22
 
 
23
#include <qdbusmenumodel.h>
 
24
#include <qdbusactiongroup.h>
 
25
#include <qstateaction.h>
 
26
#include <QDateTime>
 
27
#include <QDebug>
 
28
 
 
29
ChewieDateTimePlugin::ChewieDateTimePlugin(QObject *parent)
 
30
    : ChewieCommonPlugin(parent),
 
31
      m_action(0)
 
32
{
 
33
    connect(&m_timer, SIGNAL(timeout()), SLOT(onTimeout()));
 
34
}
 
35
 
 
36
ChewieDateTimePlugin::~ChewieDateTimePlugin()
 
37
{
 
38
    if (m_action) {
 
39
        delete m_action;
 
40
    }
 
41
}
 
42
 
 
43
void ChewieDateTimePlugin::init(const QJsonObject &config)
 
44
{
 
45
    ChewieCommonPlugin::init(config);
 
46
    m_timer.start(1000 * 10);
 
47
}
 
48
 
 
49
void ChewieDateTimePlugin::shutdown()
 
50
{
 
51
    m_timer.stop();
 
52
}
 
53
 
 
54
 
 
55
QString ChewieDateTimePlugin::dateTime() const
 
56
{
 
57
    static char timestr[255];
 
58
    time_t t;
 
59
    struct tm *tmp;
 
60
 
 
61
    t = time(NULL);
 
62
    tmp = localtime(&t);
 
63
    if (tmp == 0) {
 
64
        return "";
 
65
    }
 
66
 
 
67
    int size = strftime(timestr, 255, m_format.toUtf8().data(), tmp);
 
68
    if (size == 0) {
 
69
        return "";
 
70
    }
 
71
 
 
72
    return QString::fromLatin1(timestr, size);
 
73
}
 
74
 
 
75
bool ChewieDateTimePlugin::parseRootElement(const QString &type, QMap<int, QVariant> data)
 
76
{
 
77
    if (type == "com.canonical.indicator.root.time") {
 
78
        if (m_action != 0) {
 
79
            delete m_action;
 
80
        }
 
81
 
 
82
        QVariant action = data[QDBusMenuModel::Action];
 
83
        m_action = actionGroup()->action(action.toString());
 
84
        if (m_action->isValid()) {
 
85
            updateTimeFormat(m_action->state());
 
86
        }
 
87
        QObject::connect(m_action, SIGNAL(stateChanged(QVariant)), this, SLOT(updateTimeFormat(QVariant)));
 
88
        return true;
 
89
    } else {
 
90
        return false;
 
91
    }
 
92
}
 
93
 
 
94
void ChewieDateTimePlugin::updateTimeFormat(const QVariant &state)
 
95
{
 
96
    if (state.isValid()) {
 
97
        // (sssb) : time format, icon, accessible name format, visible
 
98
        QVariantList states = state.toList();
 
99
        if (states.size() == 4) {
 
100
            m_format = states[0].toString();
 
101
            setIcon(QUrl(states[1].toString()));
 
102
            setVisible(states[2].toBool());
 
103
            setAccessibleName(states[3].toString());
 
104
            onTimeout();
 
105
        }
 
106
    }
 
107
}
 
108
 
 
109
QQmlComponent *ChewieDateTimePlugin::createComponent(QQmlEngine *engine, QObject *parent) const
 
110
{
 
111
    return new QQmlComponent(engine, QUrl("qrc:/datetimeplugin/qml/ClockPage.qml"), parent);
 
112
}
 
113
 
 
114
void ChewieDateTimePlugin::onTimeout()
 
115
{
 
116
    QString newValue = dateTime();
 
117
    setLabel(newValue);
 
118
}