2
* Copyright 2013 Canonical Ltd.
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.
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.
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/>.
17
* Renato Araujo Oliveira Filho <renato@canonical.com>
20
#include "chewie_commonplugin.h"
29
#include <QQmlContext>
30
#include <QJsonObject>
32
#include <qdbusmenumodel.h>
33
#include <qdbusactiongroup.h>
34
#include <qstateaction.h>
36
//TODO: load this property dynamically
37
#define TARGET_DEVICE "phone"
39
ChewieCommonPlugin::ChewieCommonPlugin(QObject *parent)
51
ChewieCommonPlugin::~ChewieCommonPlugin()
56
void ChewieCommonPlugin::init(const QJsonObject &config)
58
Q_INIT_RESOURCE(chewie_commonplugin);
60
QVariantMap info = config.value("MetaData").toVariant().toMap();
61
m_title = info["name"].toString();
62
if (info.contains("priority")) {
63
setPriority(info["priority"].toInt());
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);
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()));
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());
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"]);
88
m_actionGroup->start();
92
void ChewieCommonPlugin::onModelChanged()
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));
106
parseRootElement("", QMap<int, QVariant>());
110
bool ChewieCommonPlugin::parseRootElement(const QString &type, QMap<int, QVariant> data)
112
if (type.isEmpty()) {
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";
124
QVariant action = data[QDBusMenuModel::Action];
125
m_action = m_actionGroup->action(action.toString());
126
if (m_action->isValid()) {
127
updateState(m_action->state());
129
QObject::connect(m_action, SIGNAL(stateChanged(QVariant)), this, SLOT(updateState(QVariant)));
134
void ChewieCommonPlugin::updateState(const QVariant &state)
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());
151
setAccessibleName("");
154
void ChewieCommonPlugin::shutdown()
165
delete m_actionGroup;
169
QUrl ChewieCommonPlugin::icon() const
171
if (!m_icon.isEmpty() && m_icon.scheme().isEmpty()) {
172
return QString("image://gicon/") + QUrl::toPercentEncoding(m_icon.toString());
178
void ChewieCommonPlugin::setIcon(const QUrl &icon)
180
if (icon != m_icon) {
182
Q_EMIT iconChanged(m_icon);
186
QString ChewieCommonPlugin::title() const
191
void ChewieCommonPlugin::setTitle(const QString &title)
193
if (title != m_title) {
195
Q_EMIT titleChanged(m_title);
199
int ChewieCommonPlugin::priority() const
204
void ChewieCommonPlugin::setPriority(int priority)
206
if (priority != m_priority) {
207
m_priority = priority;
208
Q_EMIT priorityChanged(m_priority);
212
QString ChewieCommonPlugin::accessibleName() const
214
return m_accessibleName;
217
void ChewieCommonPlugin::setAccessibleName(const QString &accesibleName)
219
if (accesibleName != m_accessibleName) {
220
m_accessibleName = accesibleName;
221
Q_EMIT accessibleNameChanged(m_accessibleName);
225
bool ChewieCommonPlugin::visible() const
230
void ChewieCommonPlugin::setVisible(bool visible)
232
if (visible != m_visible) {
234
Q_EMIT visibleChanged(m_visible);
238
QString ChewieCommonPlugin::label() const
243
void ChewieCommonPlugin::setLabel(const QString &label)
245
if (label != m_label) {
247
Q_EMIT labelChanged(m_label);
251
QDBusActionGroup *ChewieCommonPlugin::actionGroup() const
253
return m_actionGroup;
256
QQmlComponent *ChewieCommonPlugin::createComponent(QQmlEngine *engine, QObject *parent) const
258
return new QQmlComponent(engine, QUrl("qrc:/commonplugin/qml/commonplugin.qml"), parent);
261
QQmlComponent *ChewieCommonPlugin::component(QQmlEngine *engine, QObject *parent)
263
if (m_component == 0) {
264
m_component = createComponent(engine, parent);
269
ChewiePluginInterface::PropertiesMap ChewieCommonPlugin::initialProperties()
271
return m_initialProperties;
274
ChewiePluginInterface::WidgetsMap ChewieCommonPlugin::widgets()
276
// Register all basic/native widgets
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"));
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"));