on 48005cf help sign in
unity8/plugins/Wizard/Status.cpp
149 lines 5.5 kB history raw
1/*
2 * Copyright (C) 2015 Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3, as published
6 * by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranties of
10 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#include <QDebug>
18#include <QDBusConnection>
20#include "Status.h"
22Status::Status()
24 initNM();
25 initUPower();
28void Status::initNM()
30 m_nmIface = new QDBusInterface("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager", "org.freedesktop.NetworkManager",
31 QDBusConnection::systemBus(), this);
33 QDBusConnection::systemBus().connect("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager", "org.freedesktop.NetworkManager", "PropertiesChanged",
34 this, SLOT(onNMPropertiesChanged(QVariantMap)));
37void Status::onNMPropertiesChanged(const QVariantMap &changedProps)
39 if (changedProps.contains("State") || changedProps.contains("Connectivity")) {
40 Q_EMIT onlineChanged();
41 Q_EMIT networkIconChanged();
42 }
44 if (changedProps.contains("PrimaryConnection") || changedProps.contains("SpecificObject") || changedProps.contains("Strength")) {
45 Q_EMIT networkIconChanged();
46 }
49bool Status::online() const
51 if (!m_nmIface->isValid())
52 return false;
54 return m_nmIface->property("State").toUInt() == 70;
57QString Status::networkIcon()
59 QString iconName = QStringLiteral("nm-no-connection");
61 if (!online()) {
62 return iconName;
63 }
65 const QString primaryConn = m_nmIface->property("PrimaryConnection").value<QDBusObjectPath>().path();
66 const QString primaryConnType = m_nmIface->property("PrimaryConnectionType").toString();
68 qDebug() << "NM init, primary connection type:" << primaryConnType << ", connection:" << primaryConn;
70 if (primaryConn.isEmpty()) {
71 qWarning() << "Empty primary connection";
72 return iconName;
73 }
75 if (primaryConnType == "802-11-wireless") {
76 QDBusInterface activeConn("org.freedesktop.NetworkManager", primaryConn, "org.freedesktop.NetworkManager.Connection.Active", QDBusConnection::systemBus());
78 if (activeConn.isValid()) {
79 const QString apPath = activeConn.property("SpecificObject").value<QDBusObjectPath>().path();
80 qDebug() << "AP path:" << apPath;
82 if (apPath.isEmpty()) {
83 qWarning() << "No AP path";
84 return iconName;
85 }
87 QDBusConnection::systemBus().connect("org.freedesktop.NetworkManager", primaryConn, "org.freedesktop.NetworkManager.Connection.Active", "PropertiesChanged",
88 this, SLOT(onNMPropertiesChanged(QVariantMap)));
90 QDBusInterface ap("org.freedesktop.NetworkManager", apPath, "org.freedesktop.NetworkManager.AccessPoint", QDBusConnection::systemBus());
92 if (!ap.isValid()) {
93 qWarning() << "Invalid AP";
94 return iconName;
95 }
97 QDBusConnection::systemBus().connect("org.freedesktop.NetworkManager", apPath, "org.freedesktop.NetworkManager.AccessPoint", "PropertiesChanged",
98 this, SLOT(onNMPropertiesChanged(QVariantMap)));
100 const uint strength = ap.property("Strength").toUInt();
101 const uint flags = ap.property("Flags").toUInt();
102 qDebug() << "Strength:" << strength;
103 qDebug() << "Flags:" << flags;
105 if (strength == 0) {
106 iconName = "nm-signal-00";
107 } else if (strength <= 25) {
108 iconName = "nm-signal-25";
109 } else if (strength <= 50) {
110 iconName = "nm-signal-50";
111 } else if (strength <= 75) {
112 iconName = "nm-signal-75";
113 } else if (strength <= 100) {
114 iconName = "nm-signal-100";
115 }
117 if (flags >= 1) {
118 iconName += "-secure";
119 }
120 }
121 }
123 return iconName;
126void Status::initUPower()
128 m_upowerIface = new QDBusInterface("org.freedesktop.UPower", "/org/freedesktop/UPower/devices/DisplayDevice", "org.freedesktop.UPower.Device",
129 QDBusConnection::systemBus(), this);
130 QDBusConnection::systemBus().connect("org.freedesktop.UPower", "/org/freedesktop/UPower/devices/DisplayDevice", "org.freedesktop.DBus.Properties",
131 "PropertiesChanged", this, SLOT(onUPowerPropertiesChanged(QString,QVariantMap,QStringList)));
134void Status::onUPowerPropertiesChanged(const QString &iface, const QVariantMap &changedProps, const QStringList &invalidatedProps)
136 Q_UNUSED(iface)
137 Q_UNUSED(invalidatedProps)
139 if (changedProps.contains("IconName")) {
140 Q_EMIT batteryIconChanged();
141 }
144QString Status::batteryIcon() const
146 const QString iconName = m_upowerIface->property("IconName").toString();
147 qDebug() << "UPower icon name:" << iconName;
148 return iconName;