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

« back to all changes in this revision

Viewing changes to plugins/commonplugin/chewie_commonplugin.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
 */
 
19
 
 
20
#include "chewie_commonplugin.h"
 
21
 
 
22
#include <QUrl>
 
23
#include <QtPlugin>
 
24
#include <QResource>
 
25
#include <QDebug>
 
26
#include <QFileInfo>
 
27
#include <QDir>
 
28
#include <QQmlEngine>
 
29
#include <QQmlContext>
 
30
#include <QJsonObject>
 
31
 
 
32
#include <qdbusmenumodel.h>
 
33
#include <qdbusactiongroup.h>
 
34
#include <qstateaction.h>
 
35
 
 
36
//TODO: load this property dynamically
 
37
#define TARGET_DEVICE           "phone"
 
38
 
 
39
ChewieCommonPlugin::ChewieCommonPlugin(QObject *parent)
 
40
    : QObject(parent),
 
41
      m_icon(""),
 
42
      m_visible(false),
 
43
      m_priority(0),
 
44
      m_model(0),
 
45
      m_actionGroup(0),
 
46
      m_action(0),
 
47
      m_component(0)
 
48
{
 
49
}
 
50
 
 
51
ChewieCommonPlugin::~ChewieCommonPlugin()
 
52
{
 
53
    shutdown();
 
54
}
 
55
 
 
56
void ChewieCommonPlugin::init(const QJsonObject &config)
 
57
{
 
58
    Q_INIT_RESOURCE(chewie_commonplugin);
 
59
 
 
60
    QVariantMap info = config.value("MetaData").toVariant().toMap();
 
61
    m_title = info["name"].toString();
 
62
    if (info.contains("priority")) {
 
63
        setPriority(info["priority"].toInt());
 
64
    }
 
65
 
 
66
    m_model = new QDBusMenuModel(this);
 
67
    m_model->setBusName(info["dbus-service"].toString());
 
68
    m_model->setObjectPath(info["dbus-object-path"].toString() + "/" + TARGET_DEVICE);
 
69
    m_model->setBusType(DBusEnums::SessionBus);
 
70
 
 
71
    //keep track of any change in the model
 
72
    connect(m_model, SIGNAL(statusChanged(DBusEnums::ConnectionStatus)), SLOT(onModelChanged()));
 
73
    connect(m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), SLOT(onModelChanged()));
 
74
    connect(m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), SLOT(onModelChanged()));
 
75
    connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), SLOT(onModelChanged()));
 
76
 
 
77
    m_actionGroup = new QDBusActionGroup(this);
 
78
    m_actionGroup->setBusType(m_model->busType());
 
79
    m_actionGroup->setBusName(m_model->busName());
 
80
    m_actionGroup->setObjectPath(info["dbus-object-path"].toString());
 
81
 
 
82
    m_initialProperties.clear();
 
83
    m_initialProperties.insert("title", m_title);
 
84
    m_initialProperties.insert("busType", 1);
 
85
    m_initialProperties.insert("busName", info["dbus-service"]);
 
86
    m_initialProperties.insert("objectPath", info["dbus-object-path"]);
 
87
 
 
88
    m_actionGroup->start();
 
89
    m_model->start();
 
90
}
 
91
 
 
92
void ChewieCommonPlugin::onModelChanged()
 
93
{
 
94
    DBusEnums::ConnectionStatus status = m_model->status();
 
95
    if (status == DBusEnums::Connected) {
 
96
        QModelIndex index = m_model->index(0);
 
97
        if (index.isValid()) {
 
98
            QVariant extra = m_model->data(index, QDBusMenuModel::Extra);
 
99
            QVariantMap extraMap = extra.toMap();
 
100
            if (extraMap.contains("canonical_type")) {
 
101
                QString type = extraMap["canonical_type"].toString();
 
102
                parseRootElement(type, m_model->itemData(index));
 
103
            }
 
104
        }
 
105
    } else {
 
106
        parseRootElement("", QMap<int, QVariant>());
 
107
    }
 
108
}
 
109
 
 
110
bool ChewieCommonPlugin::parseRootElement(const QString &type, QMap<int, QVariant> data)
 
111
{
 
112
    if (type.isEmpty()) {
 
113
        return false;
 
114
    } else if (type != "com.canonical.indicator.root") {
 
115
        if (!type.startsWith("com.canonical.indicator.root")) {
 
116
            qWarning() << "indicator" << title() << "does not support root element";
 
117
        }
 
118
        return false;
 
119
    } else {
 
120
        if (m_action != 0) {
 
121
            delete m_action;
 
122
        }
 
123
 
 
124
        QVariant action = data[QDBusMenuModel::Action];
 
125
        m_action = m_actionGroup->action(action.toString());
 
126
        if (m_action->isValid()) {
 
127
            updateState(m_action->state());
 
128
        }
 
129
        QObject::connect(m_action, SIGNAL(stateChanged(QVariant)), this, SLOT(updateState(QVariant)));
 
130
        return true;
 
131
    }
 
132
}
 
133
 
 
134
void ChewieCommonPlugin::updateState(const QVariant &state)
 
135
{
 
136
    if (state.isValid()) {
 
137
        // (sssb) : the current label, icon name, accessible name, and visibility state of the indicator.
 
138
        QVariantList states = state.toList();
 
139
        if (states.size() == 4) {
 
140
            setLabel(states[0].toString());
 
141
            setIcon(QUrl(states[1].toString()));
 
142
            setVisible(states[2].toBool());
 
143
            setAccessibleName(states[3].toString());
 
144
            return;
 
145
        }
 
146
    }
 
147
 
 
148
    setLabel("");
 
149
    setIcon(QUrl());
 
150
    setVisible(false);
 
151
    setAccessibleName("");
 
152
}
 
153
 
 
154
void ChewieCommonPlugin::shutdown()
 
155
{
 
156
    delete m_component;
 
157
    m_component = 0;
 
158
 
 
159
    delete m_model;
 
160
    m_model = 0;
 
161
 
 
162
    delete m_action;
 
163
    m_action = 0;
 
164
 
 
165
    delete m_actionGroup;
 
166
    m_actionGroup = 0;
 
167
}
 
168
 
 
169
QUrl ChewieCommonPlugin::icon() const
 
170
{
 
171
    if (!m_icon.isEmpty() && m_icon.scheme().isEmpty()) {
 
172
        return QString("image://gicon/") + QUrl::toPercentEncoding(m_icon.toString());
 
173
    } else {
 
174
        return m_icon;
 
175
    }
 
176
}
 
177
 
 
178
void ChewieCommonPlugin::setIcon(const QUrl &icon)
 
179
{
 
180
    if (icon != m_icon) {
 
181
        m_icon = icon;
 
182
        Q_EMIT iconChanged(m_icon);
 
183
    }
 
184
}
 
185
 
 
186
QString ChewieCommonPlugin::title() const
 
187
{
 
188
    return m_title;
 
189
}
 
190
 
 
191
void ChewieCommonPlugin::setTitle(const QString &title)
 
192
{
 
193
    if (title != m_title) {
 
194
        m_title = title;
 
195
        Q_EMIT titleChanged(m_title);
 
196
    }
 
197
}
 
198
 
 
199
int ChewieCommonPlugin::priority() const
 
200
{
 
201
    return m_priority;
 
202
}
 
203
 
 
204
void ChewieCommonPlugin::setPriority(int priority)
 
205
{
 
206
    if (priority != m_priority) {
 
207
        m_priority = priority;
 
208
        Q_EMIT priorityChanged(m_priority);
 
209
    }
 
210
}
 
211
 
 
212
QString ChewieCommonPlugin::accessibleName() const
 
213
{
 
214
    return m_accessibleName;
 
215
}
 
216
 
 
217
void ChewieCommonPlugin::setAccessibleName(const QString &accesibleName)
 
218
{
 
219
    if (accesibleName != m_accessibleName) {
 
220
        m_accessibleName = accesibleName;
 
221
        Q_EMIT accessibleNameChanged(m_accessibleName);
 
222
    }
 
223
}
 
224
 
 
225
bool ChewieCommonPlugin::visible() const
 
226
{
 
227
    return m_visible;
 
228
}
 
229
 
 
230
void ChewieCommonPlugin::setVisible(bool visible)
 
231
{
 
232
    if (visible != m_visible) {
 
233
        m_visible = visible;
 
234
        Q_EMIT visibleChanged(m_visible);
 
235
    }
 
236
}
 
237
 
 
238
QString ChewieCommonPlugin::label() const
 
239
{
 
240
    return m_label;
 
241
}
 
242
 
 
243
void ChewieCommonPlugin::setLabel(const QString &label)
 
244
{
 
245
    if (label != m_label) {
 
246
        m_label = label;
 
247
        Q_EMIT labelChanged(m_label);
 
248
    }
 
249
}
 
250
 
 
251
QDBusActionGroup *ChewieCommonPlugin::actionGroup() const
 
252
{
 
253
    return m_actionGroup;
 
254
}
 
255
 
 
256
QQmlComponent *ChewieCommonPlugin::createComponent(QQmlEngine *engine, QObject *parent) const
 
257
{
 
258
    return new QQmlComponent(engine, QUrl("qrc:/commonplugin/qml/commonplugin.qml"), parent);
 
259
}
 
260
 
 
261
QQmlComponent *ChewieCommonPlugin::component(QQmlEngine *engine, QObject *parent)
 
262
{
 
263
    if (m_component == 0) {
 
264
        m_component = createComponent(engine, parent);
 
265
    }
 
266
    return m_component;
 
267
}
 
268
 
 
269
ChewiePluginInterface::PropertiesMap ChewieCommonPlugin::initialProperties()
 
270
{
 
271
    return m_initialProperties;
 
272
}
 
273
 
 
274
ChewiePluginInterface::WidgetsMap ChewieCommonPlugin::widgets()
 
275
{
 
276
    // Register all basic/native widgets
 
277
    static WidgetsMap w;
 
278
    if (w.isEmpty()) {
 
279
        //TODO: use a generic name for volumecontrol widget (Slider)
 
280
        w.insert("unity.widgets.systemsettings.tablet.volumecontrol", QUrl("SliderMenu.qml"));
 
281
        w.insert("unity.widgets.systemsettings.tablet.switch", QUrl("SwitchMenu.qml"));
 
282
 
 
283
        w.insert("com.canonical.indicator.button", QUrl("ButtonMenu.qml"));
 
284
        w.insert("com.canonical.indicator.div", QUrl("DivMenu.qml"));
 
285
        w.insert("com.canonical.indicator.section", QUrl("MenuSection.qml"));
 
286
        w.insert("com.canonical.indicator.progress", QUrl("ProgressMenu.qml"));
 
287
        w.insert("com.canonical.indicator.slider", QUrl("SliderMenu.qml"));
 
288
        w.insert("com.canonical.indicator.switch", QUrl("SwitchMenu.qml"));
 
289
    }
 
290
    return w;
 
291
}