on 331bab5 help sign in
unity8/plugins/IndicatorsClient/indicators/network/indicatorclient_network.cpp
174 lines 5.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 */
20#include "indicatorclient_network.h"
21#include "networkagent.h"
23#include <QUrl>
24#include <QtQml>
25#include <QtPlugin>
27#include <qdbusmenumodel.h>
28#include <qdbusactiongroup.h>
29#include <qstateaction.h>
31IndicatorClientNetwork::IndicatorClientNetwork(QObject *parent)
32 : IndicatorClientCommon(parent),
33 m_action(0)
35 setTitle("Networks");
36 setPriority(IndicatorPriority::NETWORK);
38 m_timedAnimation.setInterval(500);
39 connect(&m_timedAnimation, SIGNAL(timeout()), SLOT(animationNextFrame()));
42IndicatorClientNetwork::~IndicatorClientNetwork()
46void IndicatorClientNetwork::init(const QSettings& settings)
48 IndicatorClientCommon::init(settings);
49 Q_INIT_RESOURCE(indicatorclient_network);
51 qmlRegisterType<NetworkAgent>("NetworkSettings", 0, 1, "NetworkAgent");
52 setIcon(QUrl("image://gicon/wifi-none"));
55QQmlComponent *IndicatorClientNetwork::createComponent(QQmlEngine *engine, QObject *parent) const
57 return new QQmlComponent(engine, QUrl("qrc:/indicatorclient/network/qml/NetworkPage.qml"), parent);
60IndicatorClientInterface::WidgetsMap IndicatorClientNetwork::widgets()
62 static WidgetsMap w;
63 if (w.isEmpty()) {
64 w.insert("unity.widget.systemsettings.tablet.sectiontitle", QUrl("qrc:/indicatorclient/network/qml/WifiSection.qml"));
65 w.insert("unity.widgets.systemsettings.tablet.wifisection", QUrl("qrc:/indicatorclient/network/qml/WifiSection.qml"));
66 w.insert("unity.widgets.systemsettings.tablet.accesspoint", QUrl("qrc:/indicatorclient/network/qml/Accesspoint.qml"));
67 }
68 return w;
71bool IndicatorClientNetwork::parseRootElement(const QString &type, QMap<int, QVariant> data)
73 if (type == "com.canonical.indicator.root.network") {
74 if (m_action != 0) {
75 delete m_action;
76 }
78 QVariant action = data[QDBusMenuModel::Action];
79 m_action = actionGroup()->action(action.toString());
80 if (m_action->isValid()) {
81 updateNetworkStatus(m_action->state());
82 }
83 connect(m_action, SIGNAL(stateChanged(QVariant)), SLOT(updateNetworkStatus(QVariant)));
84 return true;
85 } else {
86 return false;
87 }
90QString IndicatorClientNetwork::getIconBasedOnSingal(uint signal) const
92 if (signal == 0) {
93 return "nm-signal-00";
94 } else if (signal <= 25) {
95 return "nm-signal-25";
96 } else if (signal <= 50) {
97 return "nm-signal-50";
98 } else if (signal <= 75) {
99 return "nm-signal-75";
100 } else {
101 return "nm-signal-100";
102 }
105void IndicatorClientNetwork::animationNextFrame()
107 static int frame = 0;
108 QString iconName;
109 switch (frame) {
110 case 0:
111 iconName = "nm-signal-00";
112 break;
113 case 1:
114 iconName = "nm-signal-25";
115 break;
116 case 2:
117 iconName = "nm-signal-50";
118 break;
119 case 3:
120 iconName = "nm-signal-75";
121 break;
122 case 4:
123 iconName = "nm-signal-100";
124 break;
125 }
127 if (frame < 4) {
128 frame++;
129 } else {
130 frame = 0;
131 }
132 setIcon(QUrl("image://gicon/" + iconName));
135void IndicatorClientNetwork::updateNetworkStatus(const QVariant &state)
137 if (state.isValid()) {
138 // (uuu)
139 // - Device type
140 // - Connection state
141 // - Extended state
142 QVariantList states = state.toList();
143 if (states.size() == 3) {
144 QString iconName;
145 uint connectionState = states[1].toUInt();
146 switch (connectionState)
147 {
148 case 3: // NM_ACTIVE_CONNECTION_STATE_DEACTIVATING
149 case 1: // NM_ACTIVE_CONNECTION_STATE_ACTIVATING
150 m_timedAnimation.start();
151 iconName = "nm-signal-100";
152 break;
153 case 2: // NM_ACTIVE_CONNECTION_STATE_ACTIVATED
154 m_timedAnimation.stop();
155 iconName = getIconBasedOnSingal(states[2].toUInt());
156 break;
157 case 0: // NM_ACTIVE_CONNECTION_STATE_UNKNOWN
158 default:
159 m_timedAnimation.stop();
160 iconName = "wifi-none";
161 break;
162 }
164 setIcon(QUrl("image://gicon/" + iconName));
165 return;
166 } else {
167 qWarning() << "Invalid network root state value";
168 m_timedAnimation.stop();
169 setIcon(QUrl());
170 }
171 } else {
172 qWarning() << "Invalid network root state";
173 }