Music Hub ..
A session-wide music playback service
Loading...
Searching...
No Matches
battery_observer.cpp
Go to the documentation of this file.
1/*
2 * Copyright © 2014 Canonical Ltd.
3 * Copyright © 2022 UBports Foundation.
4 *
5 * Contact: Alberto Mardegan <mardy@users.sourceforge.net>
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License version 3,
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Authored by: Thomas Voß <thomas.voss@canonical.com>
20 */
21
23
24#include "logging.h"
25
26#include <QDBusConnection>
27#include <QDBusInterface>
28#include <QDBusPendingCall>
29#include <QDBusPendingReply>
30
31using namespace lomiri::MediaHubService::power;
32
33namespace lomiri {
34namespace MediaHubService {
35namespace power {
36
38{
39 Q_OBJECT
40
41 static lomiri::MediaHubService::power::Level powerLevelFromString(const QString &s)
42 {
44 if (s == "ok") return Level::ok;
45 if (s == "low") return Level::low;
46 if (s == "very_low") return Level::very_low;
47 if (s == "critical") return Level::critical;
48 return Level::unknown;
49 }
50
51public:
52
54 QObject(q),
55 m_powerLevel(Level::ok),
56 m_isWarningActive(false),
57 q_ptr(q)
58 {
59 QDBusConnection connection = QDBusConnection::sessionBus();
60 auto iface = new QDBusInterface(QStringLiteral("org.ayatana.indicator.power"),
61 QStringLiteral("/org/ayatana/indicator/power/Battery"),
62 QStringLiteral("org.freedesktop.DBus.Properties"),
63 connection, this);
64 iface->connection().connect(
65 iface->service(),
66 iface->path(),
67 iface->interface(),
68 QStringLiteral("PropertiesChanged"),
69 this,
70 SLOT(onPropertiesChanged(QString, QVariantMap, QStringList)));
71
72 QDBusPendingCall call =
73 iface->asyncCall(QStringLiteral("GetAll"),
74 QStringLiteral("org.ayatana.indicator.power.Battery"));
75 auto *watcher = new QDBusPendingCallWatcher(call);
76 QObject::connect(watcher, &QDBusPendingCallWatcher::finished,
77 this, [this](QDBusPendingCallWatcher *watcher) {
78 QDBusPendingReply<QVariantMap> reply = *watcher;
79 updateProperties(reply.value());
80 watcher->deleteLater();
81 });
82 }
83
84 Q_INVOKABLE
85 void onPropertiesChanged(const QString &interface,
86 const QVariantMap &changed,
87 const QStringList &invalid)
88 {
89 Q_UNUSED(interface);
90 Q_UNUSED(invalid);
91
92 updateProperties(changed);
93 }
94
95 void updateProperties(const QVariantMap &properties)
96 {
97 Q_Q(BatteryObserver);
98
99 auto i = properties.find(QStringLiteral("PowerLevel"));
100 if (i != properties.end()) {
101 auto oldPowerLevel = m_powerLevel;
102 m_powerLevel = powerLevelFromString(i->toString());
103 if (m_powerLevel != oldPowerLevel) {
104 Q_EMIT q->levelChanged();
105 }
106 }
107
108 i = properties.find(QStringLiteral("IsWarning"));
109 if (i != properties.end()) {
110 bool oldIsWarningActive = m_isWarningActive;
111 m_isWarningActive = i->toBool();
112 if (m_isWarningActive != oldIsWarningActive) {
113 Q_EMIT q->isWarningActiveChanged();
114 }
115 }
116 }
117
118private:
119 Q_DECLARE_PUBLIC(BatteryObserver)
121 bool m_isWarningActive;
122 BatteryObserver *q_ptr;
123};
124
125}}} // namespace
126
128 QObject(parent),
129 d_ptr(new BatteryObserverPrivate(this))
130{
131}
132
134
136{
137 Q_D(const BatteryObserver);
138 return d->m_powerLevel;
139}
140
142{
143 Q_D(const BatteryObserver);
144 return d->m_isWarningActive;
145}
146
147#include "battery_observer.moc"
Q_INVOKABLE void onPropertiesChanged(const QString &interface, const QVariantMap &changed, const QStringList &invalid)