Solid

upowerbattery.cpp
1/*
2 SPDX-FileCopyrightText: 2009 Pino Toscano <pino@kde.org>
3 SPDX-FileCopyrightText: 2010, 2012, 2015 Lukáš Tinkl <ltinkl@redhat.com>
4 SPDX-FileCopyrightText: 2014 Kai Uwe Broulik <kde@privat.broulik.de>
5
6 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7*/
8
9#include "upowerbattery.h"
10
11#include "upower.h"
12
13using namespace Solid::Backends::UPower;
14
15Battery::Battery(UPowerDevice *device)
16 : DeviceInterface(device)
17{
18 connect(device, &UPowerDevice::propertyChanged, this, &Battery::slotChanged);
19
20 updateCache();
21}
22
23Battery::~Battery()
24{
25}
26
27bool Battery::isPresent() const
28{
29 return m_device.data()->prop(QStringLiteral("IsPresent")).toBool();
30}
31
32Solid::Battery::BatteryType Battery::type() const
33{
34 Solid::Battery::BatteryType result = Solid::Battery::UnknownBattery;
35 const auto t = static_cast<UpDeviceKind>(m_device.data()->prop(QStringLiteral("Type")).toUInt());
36 switch (t) {
37 case UP_DEVICE_KIND_LINE_POWER: // TODO
38 break;
39 case UP_DEVICE_KIND_BATTERY:
40 result = Solid::Battery::PrimaryBattery;
41 break;
42 case UP_DEVICE_KIND_UPS:
43 result = Solid::Battery::UpsBattery;
44 break;
45 case UP_DEVICE_KIND_MONITOR:
46 result = Solid::Battery::MonitorBattery;
47 break;
48 case UP_DEVICE_KIND_MOUSE:
49 result = Solid::Battery::MouseBattery;
50 break;
51 case UP_DEVICE_KIND_KEYBOARD:
52 result = Solid::Battery::KeyboardBattery;
53 break;
54 case UP_DEVICE_KIND_PDA:
55 result = Solid::Battery::PdaBattery;
56 break;
57 case UP_DEVICE_KIND_PHONE:
58 result = Solid::Battery::PhoneBattery;
59 break;
60 case UP_DEVICE_KIND_TABLET:
61 result = Solid::Battery::TabletBattery;
62 break;
63 case UP_DEVICE_KIND_GAMING_INPUT:
64 result = Solid::Battery::GamingInputBattery;
65 break;
66 case UP_DEVICE_KIND_HEADPHONES:
67 result = Solid::Battery::HeadphoneBattery;
68 break;
69 case UP_DEVICE_KIND_HEADSET:
70 result = Solid::Battery::HeadsetBattery;
71 break;
72 case UP_DEVICE_KIND_TOUCHPAD:
73 result = Solid::Battery::TouchpadBattery;
74 break;
75 case UP_DEVICE_KIND_BLUETOOTH_GENERIC:
76 result = Solid::Battery::BluetoothBattery;
77 break;
78 case UP_DEVICE_KIND_UNKNOWN:
79 break;
80 }
81
82 if (result == Solid::Battery::UnknownBattery) {
83 // Check if the battery came from Bluez, which is more useful than unknown battery type
84 // UP_DEVICE_KIND_BLUETOOTH_GENERIC is only in UPower 0.99.12
85 if (m_device.data()->prop(QStringLiteral("NativePath")).toString().startsWith(QLatin1String("/org/bluez/"))) {
86 result = Solid::Battery::BluetoothBattery;
87 }
88 }
89
90 return result;
91}
92
93int Battery::chargePercent() const
94{
95 return qRound(m_device.data()->prop(QStringLiteral("Percentage")).toDouble());
96}
97
98int Battery::capacity() const
99{
100 return qRound(m_device.data()->prop(QStringLiteral("Capacity")).toDouble());
101}
102
103int Battery::cycleCount() const
104{
105 // New in upower v0.99.14.
106 bool ok;
107 const int cycleCount = m_device.data()->prop(QStringLiteral("ChargeCycles")).toInt(&ok);
108 if (ok) {
109 return cycleCount;
110 } else {
111 return -1;
112 }
113}
114
115bool Battery::isRechargeable() const
116{
117 return m_device.data()->prop(QStringLiteral("IsRechargeable")).toBool();
118}
119
120bool Battery::isPowerSupply() const
121{
122 return m_device.data()->prop(QStringLiteral("PowerSupply")).toBool();
123}
124
125Solid::Battery::ChargeState Battery::chargeState() const
126{
127 Solid::Battery::ChargeState result = Solid::Battery::NoCharge;
128 const UpDeviceState state = static_cast<UpDeviceState>(m_device.data()->prop(QStringLiteral("State")).toUInt());
129 switch (state) {
130 case UP_DEVICE_STATE_UNKNOWN:
131 result = Solid::Battery::NoCharge; // stable or unknown
132 break;
133 case UP_DEVICE_STATE_CHARGING:
134 result = Solid::Battery::Charging;
135 break;
136 case UP_DEVICE_STATE_DISCHARGING:
137 result = Solid::Battery::Discharging;
138 break;
139 case UP_DEVICE_STATE_EMPTY: // TODO "Empty"
140 break;
141 case UP_DEVICE_STATE_FULLY_CHARGED:
142 result = Solid::Battery::FullyCharged;
143 break;
144 case UP_DEVICE_STATE_PENDING_CHARGE: // TODO "Pending charge"
145 break;
146 case UP_DEVICE_STATE_PENDING_DISCHARGE: // TODO "Pending discharge"
147 break;
148 case UP_DEVICE_STATE_LAST:
149 break;
150 }
151 return result;
152}
153
154qlonglong Battery::timeToEmpty() const
155{
156 return m_device.data()->prop(QStringLiteral("TimeToEmpty")).toLongLong();
157}
158
159qlonglong Battery::timeToFull() const
160{
161 return m_device.data()->prop(QStringLiteral("TimeToFull")).toLongLong();
162}
163
164Solid::Battery::Technology Battery::technology() const
165{
166 const UpDeviceTechnology tech = static_cast<UpDeviceTechnology>(m_device.data()->prop(QStringLiteral("Technology")).toUInt());
167 switch (tech) {
168 case UP_DEVICE_TECHNOLOGY_UNKNOWN:
169 return Solid::Battery::UnknownTechnology;
170 case UP_DEVICE_TECHNOLOGY_LITHIUM_ION:
171 return Solid::Battery::LithiumIon;
172 case UP_DEVICE_TECHNOLOGY_LITHIUM_POLYMER:
173 return Solid::Battery::LithiumPolymer;
174 case UP_DEVICE_TECHNOLOGY_LITHIUM_IRON_PHOSPHATE:
175 return Solid::Battery::LithiumIronPhosphate;
176 case UP_DEVICE_TECHNOLOGY_LEAD_ACID:
177 return Solid::Battery::LeadAcid;
178 case UP_DEVICE_TECHNOLOGY_NICKEL_CADMIUM:
179 return Solid::Battery::NickelCadmium;
180 case UP_DEVICE_TECHNOLOGY_NICKEL_METAL_HYDRIDE:
181 return Solid::Battery::NickelMetalHydride;
182 case UP_DEVICE_TECHNOLOGY_LAST:
183 return Solid::Battery::UnknownTechnology;
184 }
185 return Solid::Battery::UnknownTechnology;
186}
187
188double Battery::energy() const
189{
190 return m_device.data()->prop(QStringLiteral("Energy")).toDouble();
191}
192
193double Battery::energyFull() const
194{
195 return m_device.data()->prop(QStringLiteral("EnergyFull")).toDouble();
196}
197
198double Battery::energyFullDesign() const
199{
200 return m_device.data()->prop(QStringLiteral("EnergyFullDesign")).toDouble();
201}
202
203double Battery::energyRate() const
204{
205 return m_device.data()->prop(QStringLiteral("EnergyRate")).toDouble();
206}
207
208double Battery::voltage() const
209{
210 return m_device.data()->prop(QStringLiteral("Voltage")).toDouble();
211}
212
213double Battery::temperature() const
214{
215 return m_device.data()->prop(QStringLiteral("Temperature")).toDouble();
216}
217
218QString Battery::serial() const
219{
220 return m_device.data()->prop(QStringLiteral("Serial")).toString();
221}
222
223qlonglong Battery::remainingTime() const
224{
225 if (chargeState() == Solid::Battery::Charging) {
226 return timeToFull();
227 } else if (chargeState() == Solid::Battery::Discharging) {
228 return timeToEmpty();
229 }
230
231 return -1;
232}
233
234void Battery::slotChanged()
235{
236 if (m_device) {
237 const bool old_isPresent = m_isPresent;
238 const int old_chargePercent = m_chargePercent;
239 const int old_capacity = m_capacity;
240 const int old_cycleCount = m_cycleCount;
241 const bool old_isPowerSupply = m_isPowerSupply;
242 const Solid::Battery::ChargeState old_chargeState = m_chargeState;
243 const qlonglong old_timeToEmpty = m_timeToEmpty;
244 const qlonglong old_timeToFull = m_timeToFull;
245 const double old_energy = m_energy;
246 const double old_energyFull = m_energyFull;
247 const double old_energyFullDesign = m_energyFullDesign;
248 const double old_energyRate = m_energyRate;
249 const double old_voltage = m_voltage;
250 const double old_temperature = m_temperature;
251 updateCache();
252
253 if (old_isPresent != m_isPresent) {
254 Q_EMIT presentStateChanged(m_isPresent, m_device.data()->udi());
255 }
256
257 if (old_chargePercent != m_chargePercent) {
258 Q_EMIT chargePercentChanged(m_chargePercent, m_device.data()->udi());
259 }
260
261 if (old_capacity != m_capacity) {
262 Q_EMIT capacityChanged(m_capacity, m_device.data()->udi());
263 }
264
265 if (old_cycleCount != m_cycleCount) {
266 Q_EMIT cycleCountChanged(m_cycleCount, m_device.data()->udi());
267 }
268
269 if (old_isPowerSupply != m_isPowerSupply) {
270 Q_EMIT powerSupplyStateChanged(m_isPowerSupply, m_device.data()->udi());
271 }
272
273 if (old_chargeState != m_chargeState) {
274 Q_EMIT chargeStateChanged(m_chargeState, m_device.data()->udi());
275 }
276
277 if (old_timeToEmpty != m_timeToEmpty) {
278 Q_EMIT timeToEmptyChanged(m_timeToEmpty, m_device.data()->udi());
279 }
280
281 if (old_timeToFull != m_timeToFull) {
282 Q_EMIT timeToFullChanged(m_timeToFull, m_device.data()->udi());
283 }
284
285 if (old_energy != m_energy) {
286 Q_EMIT energyChanged(m_energy, m_device.data()->udi());
287 }
288
289 if (old_energyFull != m_energyFull) {
290 Q_EMIT energyFullChanged(m_energyFull, m_device.data()->udi());
291 }
292
293 if (old_energyFullDesign != m_energyFullDesign) {
294 Q_EMIT energyFullChanged(m_energyFullDesign, m_device.data()->udi());
295 }
296
297 if (old_energyRate != m_energyRate) {
298 Q_EMIT energyRateChanged(m_energyRate, m_device.data()->udi());
299 }
300
301 if (old_voltage != m_voltage) {
302 Q_EMIT voltageChanged(m_voltage, m_device.data()->udi());
303 }
304
305 if (old_temperature != m_temperature) {
306 Q_EMIT temperatureChanged(m_temperature, m_device.data()->udi());
307 }
308
309 if (old_timeToFull != m_timeToFull || old_timeToEmpty != m_timeToEmpty) {
310 Q_EMIT remainingTimeChanged(remainingTime(), m_device.data()->udi());
311 }
312 }
313}
314
315void Battery::updateCache()
316{
317 m_isPresent = isPresent();
318 m_chargePercent = chargePercent();
319 m_capacity = capacity();
320 m_cycleCount = cycleCount();
321 m_isPowerSupply = isPowerSupply();
322 m_chargeState = chargeState();
323 m_timeToEmpty = timeToEmpty();
324 m_timeToFull = timeToFull();
325 m_energy = energy();
326 m_energyFull = energyFull();
327 m_energyFullDesign = energyFullDesign();
328 m_energyRate = energyRate();
329 m_voltage = voltage();
330 m_temperature = temperature();
331}
332
333#include "moc_upowerbattery.cpp"
BatteryType
This enum type defines the type of the device holding the battery.
Technology
Technology used in the battery.
ChargeState
This enum type defines charge state of a battery.
char * toString(const EngineQuery &query)
Q_EMITQ_EMIT
T * data() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:01:49 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.