Unity 8
Status.cpp
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  */
16 
17 #include <QDebug>
18 #include <QDBusConnection>
19 
20 #include "Status.h"
21 
22 Status::Status()
23 {
24  initNM();
25  initUPower();
26 }
27 
28 void Status::initNM()
29 {
30  m_nmIface = new QDBusInterface("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager", "org.freedesktop.NetworkManager",
31  QDBusConnection::systemBus(), this);
32 
33  QDBusConnection::systemBus().connect("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager", "org.freedesktop.NetworkManager", "PropertiesChanged",
34  this, SLOT(onNMPropertiesChanged(QVariantMap)));
35 }
36 
37 void Status::onNMPropertiesChanged(const QVariantMap &changedProps)
38 {
39  if (changedProps.contains("State") || changedProps.contains("Connectivity")) {
40  Q_EMIT onlineChanged();
41  Q_EMIT networkIconChanged();
42  }
43 
44  if (changedProps.contains("PrimaryConnection") || changedProps.contains("SpecificObject") || changedProps.contains("Strength")) {
45  Q_EMIT networkIconChanged();
46  }
47 }
48 
49 bool Status::online() const
50 {
51  if (!m_nmIface->isValid())
52  return false;
53 
54  return m_nmIface->property("State").toUInt() == 70;
55 }
56 
57 QString Status::networkIcon()
58 {
59  QString iconName = QStringLiteral("nm-no-connection");
60 
61  if (!online()) {
62  return iconName;
63  }
64 
65  const QString primaryConn = m_nmIface->property("PrimaryConnection").value<QDBusObjectPath>().path();
66  const QString primaryConnType = m_nmIface->property("PrimaryConnectionType").toString();
67 
68  if (primaryConn.isEmpty()) {
69  qWarning() << "Empty primary connection";
70  return iconName;
71  }
72 
73  if (primaryConnType == "802-11-wireless") {
74  QDBusInterface activeConn("org.freedesktop.NetworkManager", primaryConn, "org.freedesktop.NetworkManager.Connection.Active", QDBusConnection::systemBus());
75 
76  if (activeConn.isValid()) {
77  const QString apPath = activeConn.property("SpecificObject").value<QDBusObjectPath>().path();
78 
79  if (apPath.isEmpty()) {
80  qWarning() << "No AP path";
81  return iconName;
82  }
83 
84  QDBusConnection::systemBus().connect("org.freedesktop.NetworkManager", primaryConn, "org.freedesktop.NetworkManager.Connection.Active", "PropertiesChanged",
85  this, SLOT(onNMPropertiesChanged(QVariantMap)));
86 
87  QDBusInterface ap("org.freedesktop.NetworkManager", apPath, "org.freedesktop.NetworkManager.AccessPoint", QDBusConnection::systemBus());
88 
89  if (!ap.isValid()) {
90  qWarning() << "Invalid AP";
91  return iconName;
92  }
93 
94  QDBusConnection::systemBus().connect("org.freedesktop.NetworkManager", apPath, "org.freedesktop.NetworkManager.AccessPoint", "PropertiesChanged",
95  this, SLOT(onNMPropertiesChanged(QVariantMap)));
96 
97  const uint strength = ap.property("Strength").toUInt();
98  const uint flags = ap.property("Flags").toUInt();
99 
100  if (strength == 0) {
101  iconName = "nm-signal-00";
102  } else if (strength <= 25) {
103  iconName = "nm-signal-25";
104  } else if (strength <= 50) {
105  iconName = "nm-signal-50";
106  } else if (strength <= 75) {
107  iconName = "nm-signal-75";
108  } else if (strength <= 100) {
109  iconName = "nm-signal-100";
110  }
111 
112  if (flags >= 1) {
113  iconName += "-secure";
114  }
115  }
116  }
117 
118  return iconName;
119 }
120 
121 void Status::initUPower()
122 {
123  m_upowerIface = new QDBusInterface("org.freedesktop.UPower", "/org/freedesktop/UPower/devices/DisplayDevice", "org.freedesktop.UPower.Device",
124  QDBusConnection::systemBus(), this);
125  QDBusConnection::systemBus().connect("org.freedesktop.UPower", "/org/freedesktop/UPower/devices/DisplayDevice", "org.freedesktop.DBus.Properties",
126  "PropertiesChanged", this, SLOT(onUPowerPropertiesChanged(QString,QVariantMap,QStringList)));
127 }
128 
129 void Status::onUPowerPropertiesChanged(const QString &iface, const QVariantMap &changedProps, const QStringList &invalidatedProps)
130 {
131  Q_UNUSED(iface)
132  Q_UNUSED(invalidatedProps)
133 
134  if (changedProps.contains("IconName")) {
135  Q_EMIT batteryIconChanged();
136  }
137 }
138 
139 QString Status::batteryIcon() const
140 {
141  const QString iconName = m_upowerIface->property("IconName").toString();
142  return iconName;
143 }