File: Status.cpp

package info (click to toggle)
lomiri 0.5.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,088 kB
  • sloc: cpp: 39,498; python: 2,559; javascript: 1,426; ansic: 1,012; sh: 289; xml: 252; makefile: 69
file content (143 lines) | stat: -rw-r--r-- 5,203 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * Copyright (C) 2015 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <QDebug>
#include <QDBusConnection>

#include "Status.h"

Status::Status()
{
    initNM();
    initUPower();
}

void Status::initNM()
{
    m_nmIface = new QDBusInterface("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager", "org.freedesktop.NetworkManager",
                                   QDBusConnection::systemBus(), this);

    QDBusConnection::systemBus().connect("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager", "org.freedesktop.NetworkManager", "PropertiesChanged",
                                         this, SLOT(onNMPropertiesChanged(QVariantMap)));
}

void Status::onNMPropertiesChanged(const QVariantMap &changedProps)
{
    if (changedProps.contains("State") || changedProps.contains("Connectivity")) {
        Q_EMIT onlineChanged();
        Q_EMIT networkIconChanged();
    }

    if (changedProps.contains("PrimaryConnection") || changedProps.contains("SpecificObject") || changedProps.contains("Strength")) {
        Q_EMIT networkIconChanged();
    }
}

bool Status::online() const
{
    if (!m_nmIface->isValid())
        return false;

    return m_nmIface->property("State").toUInt() == 70;
}

QString Status::networkIcon()
{
    QString iconName = QStringLiteral("nm-no-connection");

    if (!online()) {
        return iconName;
    }

    const QString primaryConn = m_nmIface->property("PrimaryConnection").value<QDBusObjectPath>().path();
    const QString primaryConnType = m_nmIface->property("PrimaryConnectionType").toString();

    if (primaryConn.isEmpty()) {
        qWarning() << "Empty primary connection";
        return iconName;
    }

    if (primaryConnType == "802-11-wireless") {
        QDBusInterface activeConn("org.freedesktop.NetworkManager", primaryConn, "org.freedesktop.NetworkManager.Connection.Active", QDBusConnection::systemBus());

        if (activeConn.isValid()) {
            const QString apPath = activeConn.property("SpecificObject").value<QDBusObjectPath>().path();

            if (apPath.isEmpty()) {
                qWarning() << "No AP path";
                return iconName;
            }

            QDBusConnection::systemBus().connect("org.freedesktop.NetworkManager", primaryConn, "org.freedesktop.NetworkManager.Connection.Active", "PropertiesChanged",
                                                 this, SLOT(onNMPropertiesChanged(QVariantMap)));

            QDBusInterface ap("org.freedesktop.NetworkManager", apPath, "org.freedesktop.NetworkManager.AccessPoint", QDBusConnection::systemBus());

            if (!ap.isValid()) {
                qWarning() << "Invalid AP";
                return iconName;
            }

            QDBusConnection::systemBus().connect("org.freedesktop.NetworkManager", apPath, "org.freedesktop.NetworkManager.AccessPoint", "PropertiesChanged",
                                                 this, SLOT(onNMPropertiesChanged(QVariantMap)));

            const uint strength = ap.property("Strength").toUInt();
            const uint flags = ap.property("Flags").toUInt();

            if (strength == 0) {
                iconName = "nm-signal-00";
            } else if (strength <= 25) {
                iconName = "nm-signal-25";
            } else if (strength <= 50) {
                iconName = "nm-signal-50";
            } else if (strength <= 75) {
                iconName = "nm-signal-75";
            } else if (strength <= 100) {
                iconName = "nm-signal-100";
            }

            if (flags >= 1) {
                iconName += "-secure";
            }
        }
    }

    return iconName;
}

void Status::initUPower()
{
    m_upowerIface = new QDBusInterface("org.freedesktop.UPower", "/org/freedesktop/UPower/devices/DisplayDevice", "org.freedesktop.UPower.Device",
                                       QDBusConnection::systemBus(), this);
    QDBusConnection::systemBus().connect("org.freedesktop.UPower", "/org/freedesktop/UPower/devices/DisplayDevice", "org.freedesktop.DBus.Properties",
                                         "PropertiesChanged", this, SLOT(onUPowerPropertiesChanged(QString,QVariantMap,QStringList)));
}

void Status::onUPowerPropertiesChanged(const QString &iface, const QVariantMap &changedProps, const QStringList &invalidatedProps)
{
    Q_UNUSED(iface)
    Q_UNUSED(invalidatedProps)

    if (changedProps.contains("IconName")) {
        Q_EMIT batteryIconChanged();
    }
}

QString Status::batteryIcon() const
{
    const QString iconName = m_upowerIface->property("IconName").toString();
    return iconName;
}