on fa85355 help sign in
unity8/plugins/IndicatorsClient/indicators/network/indicatorclient_network.cpp
162 lines 4.5 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);
50 qmlRegisterType<NetworkAgent>("NetworkSettings", 0, 1, "NetworkAgent");
51 setIcon(QUrl("image://gicon/wifi-none"));
54QUrl IndicatorClientNetwork::componentSource() const
56 return QUrl("qrc:/indicatorsclient/qml/NetworkIndicator.qml");
59bool IndicatorClientNetwork::parseRootElement(const QString &type, QMap<int, QVariant> data)
61 if (type == "com.canonical.indicator.root.network") {
62 if (m_action != 0) {
63 delete m_action;
64 }
66 QVariant action = data[QDBusMenuModel::Action];
67 m_action = actionGroup()->action(action.toString());
68 if (m_action->isValid()) {
69 updateNetworkStatus(m_action->state());
70 }
71 connect(m_action, SIGNAL(stateChanged(QVariant)), SLOT(updateNetworkStatus(QVariant)));
72 return true;
73 } else {
74 return false;
75 }
78QString IndicatorClientNetwork::getIconBasedOnSingal(uint signal) const
80 if (signal == 0) {
81 return "nm-signal-00";
82 } else if (signal <= 25) {
83 return "nm-signal-25";
84 } else if (signal <= 50) {
85 return "nm-signal-50";
86 } else if (signal <= 75) {
87 return "nm-signal-75";
88 } else {
89 return "nm-signal-100";
90 }
93void IndicatorClientNetwork::animationNextFrame()
95 static int frame = 0;
96 QString iconName;
97 switch (frame) {
98 case 0:
99 iconName = "nm-signal-00";
100 break;
101 case 1:
102 iconName = "nm-signal-25";
103 break;
104 case 2:
105 iconName = "nm-signal-50";
106 break;
107 case 3:
108 iconName = "nm-signal-75";
109 break;
110 case 4:
111 iconName = "nm-signal-100";
112 break;
113 }
115 if (frame < 4) {
116 frame++;
117 } else {
118 frame = 0;
119 }
120 setIcon(QUrl("image://gicon/" + iconName));
123void IndicatorClientNetwork::updateNetworkStatus(const QVariant &state)
125 if (state.isValid()) {
126 // (uuu)
127 // - Device type
128 // - Connection state
129 // - Extended state
130 QVariantList states = state.toList();
131 if (states.size() == 3) {
132 QString iconName;
133 uint connectionState = states[1].toUInt();
134 switch (connectionState)
135 {
136 case 3: // NM_ACTIVE_CONNECTION_STATE_DEACTIVATING
137 case 1: // NM_ACTIVE_CONNECTION_STATE_ACTIVATING
138 m_timedAnimation.start();
139 iconName = "nm-signal-100";
140 break;
141 case 2: // NM_ACTIVE_CONNECTION_STATE_ACTIVATED
142 m_timedAnimation.stop();
143 iconName = getIconBasedOnSingal(states[2].toUInt());
144 break;
145 case 0: // NM_ACTIVE_CONNECTION_STATE_UNKNOWN
146 default:
147 m_timedAnimation.stop();
148 iconName = "wifi-none";
149 break;
150 }
152 setIcon(QUrl("image://gicon/" + iconName));
153 return;
154 } else {
155 qWarning() << "Invalid network root state value";
156 m_timedAnimation.stop();
157 setIcon(QUrl());
158 }
159 } else {
160 qWarning() << "Invalid network root state";
161 }