Music Hub  ..
A session-wide music playback service
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 
22 #include "power/battery_observer.h"
23 
24 #include "logging.h"
25 
26 #include <QDBusConnection>
27 #include <QDBusInterface>
28 #include <QDBusPendingCall>
29 #include <QDBusPendingReply>
30 
31 using namespace lomiri::MediaHubService::power;
32 
33 namespace lomiri {
34 namespace MediaHubService {
35 namespace 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 
51 public:
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 
118 private:
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"
QObject
lomiri::MediaHubService::power::Level::critical
@ critical
lomiri::MediaHubService::power::Level::unknown
@ unknown
lomiri::MediaHubService::power::Level
Level
Definition: battery_observer.h:34
lomiri::MediaHubService::power::Level::very_low
@ very_low
lomiri::MediaHubService::power::BatteryObserver::isWarningActive
bool isWarningActive() const
Definition: battery_observer.cpp:141
lomiri::MediaHubService::power::BatteryObserver::~BatteryObserver
virtual ~BatteryObserver()
battery_observer.h
lomiri::MediaHubService::power::Level::low
@ low
lomiri::MediaHubService::power::BatteryObserverPrivate
Definition: battery_observer.cpp:37
lomiri::MediaHubService::power::BatteryObserver::BatteryObserver
BatteryObserver(QObject *parent=nullptr)
Definition: battery_observer.cpp:127
lomiri::MediaHubService::power::BatteryObserver::level
Level level() const
Definition: battery_observer.cpp:135
lomiri::MediaHubService::power
Definition: battery_observer.cpp:35
lomiri::MediaHubService::power::Level::ok
@ ok
lomiri
Definition: dbus_utils.h:24
lomiri::MediaHubService::power::BatteryObserver
Definition: battery_observer.h:46
lomiri::MediaHubService::power::BatteryObserverPrivate::BatteryObserverPrivate
BatteryObserverPrivate(BatteryObserver *q)
Definition: battery_observer.cpp:53
lomiri::MediaHubService::power::BatteryObserverPrivate::updateProperties
void updateProperties(const QVariantMap &properties)
Definition: battery_observer.cpp:95
lomiri::MediaHubService::power::BatteryObserverPrivate::onPropertiesChanged
Q_INVOKABLE void onPropertiesChanged(const QString &interface, const QVariantMap &changed, const QStringList &invalid)
Definition: battery_observer.cpp:85