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("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("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("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("Percentage").toDouble());
96}
97
98int Battery::capacity() const
99{
100 return qRound(m_device.data()->prop("Capacity").toDouble());
101}
102
103bool Battery::isRechargeable() const
104{
105 return m_device.data()->prop("IsRechargeable").toBool();
106}
107
108bool Battery::isPowerSupply() const
109{
110 return m_device.data()->prop("PowerSupply").toBool();
111}
112
113Solid::Battery::ChargeState Battery::chargeState() const
114{
115 Solid::Battery::ChargeState result = Solid::Battery::NoCharge;
116 const UpDeviceState state = static_cast<UpDeviceState>(m_device.data()->prop("State").toUInt());
117 switch (state) {
118 case UP_DEVICE_STATE_UNKNOWN:
119 result = Solid::Battery::NoCharge; // stable or unknown
120 break;
121 case UP_DEVICE_STATE_CHARGING:
122 result = Solid::Battery::Charging;
123 break;
124 case UP_DEVICE_STATE_DISCHARGING:
125 result = Solid::Battery::Discharging;
126 break;
127 case UP_DEVICE_STATE_EMPTY: // TODO "Empty"
128 break;
129 case UP_DEVICE_STATE_FULLY_CHARGED:
130 result = Solid::Battery::FullyCharged;
131 break;
132 case UP_DEVICE_STATE_PENDING_CHARGE: // TODO "Pending charge"
133 break;
134 case UP_DEVICE_STATE_PENDING_DISCHARGE: // TODO "Pending discharge"
135 break;
136 case UP_DEVICE_STATE_LAST:
137 break;
138 }
139 return result;
140}
141
142qlonglong Battery::timeToEmpty() const
143{
144 return m_device.data()->prop("TimeToEmpty").toLongLong();
145}
146
147qlonglong Battery::timeToFull() const
148{
149 return m_device.data()->prop("TimeToFull").toLongLong();
150}
151
152Solid::Battery::Technology Battery::technology() const
153{
154 const UpDeviceTechnology tech = static_cast<UpDeviceTechnology>(m_device.data()->prop("Technology").toUInt());
155 switch (tech) {
156 case UP_DEVICE_TECHNOLOGY_UNKNOWN:
157 return Solid::Battery::UnknownTechnology;
158 case UP_DEVICE_TECHNOLOGY_LITHIUM_ION:
159 return Solid::Battery::LithiumIon;
160 case UP_DEVICE_TECHNOLOGY_LITHIUM_POLYMER:
161 return Solid::Battery::LithiumPolymer;
162 case UP_DEVICE_TECHNOLOGY_LITHIUM_IRON_PHOSPHATE:
163 return Solid::Battery::LithiumIronPhosphate;
164 case UP_DEVICE_TECHNOLOGY_LEAD_ACID:
165 return Solid::Battery::LeadAcid;
166 case UP_DEVICE_TECHNOLOGY_NICKEL_CADMIUM:
167 return Solid::Battery::NickelCadmium;
168 case UP_DEVICE_TECHNOLOGY_NICKEL_METAL_HYDRIDE:
169 return Solid::Battery::NickelMetalHydride;
170 case UP_DEVICE_TECHNOLOGY_LAST:
171 return Solid::Battery::UnknownTechnology;
172 }
173 return Solid::Battery::UnknownTechnology;
174}
175
176double Battery::energy() const
177{
178 return m_device.data()->prop("Energy").toDouble();
179}
180
181double Battery::energyFull() const
182{
183 return m_device.data()->prop("EnergyFull").toDouble();
184}
185
186double Battery::energyFullDesign() const
187{
188 return m_device.data()->prop("EnergyFullDesign").toDouble();
189}
190
191double Battery::energyRate() const
192{
193 return m_device.data()->prop("EnergyRate").toDouble();
194}
195
196double Battery::voltage() const
197{
198 return m_device.data()->prop("Voltage").toDouble();
199}
200
201double Battery::temperature() const
202{
203 return m_device.data()->prop("Temperature").toDouble();
204}
205
206QString Battery::serial() const
207{
208 return m_device.data()->prop("Serial").toString();
209}
210
211qlonglong Battery::remainingTime() const
212{
213 if (chargeState() == Solid::Battery::Charging) {
214 return timeToFull();
215 } else if (chargeState() == Solid::Battery::Discharging) {
216 return timeToEmpty();
217 }
218
219 return -1;
220}
221
222void Battery::slotChanged()
223{
224 if (m_device) {
225 const bool old_isPresent = m_isPresent;
226 const int old_chargePercent = m_chargePercent;
227 const int old_capacity = m_capacity;
228 const bool old_isPowerSupply = m_isPowerSupply;
229 const Solid::Battery::ChargeState old_chargeState = m_chargeState;
230 const qlonglong old_timeToEmpty = m_timeToEmpty;
231 const qlonglong old_timeToFull = m_timeToFull;
232 const double old_energy = m_energy;
233 const double old_energyFull = m_energyFull;
234 const double old_energyFullDesign = m_energyFullDesign;
235 const double old_energyRate = m_energyRate;
236 const double old_voltage = m_voltage;
237 const double old_temperature = m_temperature;
238 updateCache();
239
240 if (old_isPresent != m_isPresent) {
241 Q_EMIT presentStateChanged(m_isPresent, m_device.data()->udi());
242 }
243
244 if (old_chargePercent != m_chargePercent) {
245 Q_EMIT chargePercentChanged(m_chargePercent, m_device.data()->udi());
246 }
247
248 if (old_capacity != m_capacity) {
249 Q_EMIT capacityChanged(m_capacity, m_device.data()->udi());
250 }
251
252 if (old_isPowerSupply != m_isPowerSupply) {
253 Q_EMIT powerSupplyStateChanged(m_isPowerSupply, m_device.data()->udi());
254 }
255
256 if (old_chargeState != m_chargeState) {
257 Q_EMIT chargeStateChanged(m_chargeState, m_device.data()->udi());
258 }
259
260 if (old_timeToEmpty != m_timeToEmpty) {
261 Q_EMIT timeToEmptyChanged(m_timeToEmpty, m_device.data()->udi());
262 }
263
264 if (old_timeToFull != m_timeToFull) {
265 Q_EMIT timeToFullChanged(m_timeToFull, m_device.data()->udi());
266 }
267
268 if (old_energy != m_energy) {
269 Q_EMIT energyChanged(m_energy, m_device.data()->udi());
270 }
271
272 if (old_energyFull != m_energyFull) {
273 Q_EMIT energyFullChanged(m_energyFull, m_device.data()->udi());
274 }
275
276 if (old_energyFullDesign != m_energyFullDesign) {
277 Q_EMIT energyFullChanged(m_energyFullDesign, m_device.data()->udi());
278 }
279
280 if (old_energyRate != m_energyRate) {
281 Q_EMIT energyRateChanged(m_energyRate, m_device.data()->udi());
282 }
283
284 if (old_voltage != m_voltage) {
285 Q_EMIT voltageChanged(m_voltage, m_device.data()->udi());
286 }
287
288 if (old_temperature != m_temperature) {
289 Q_EMIT temperatureChanged(m_temperature, m_device.data()->udi());
290 }
291
292 if (old_timeToFull != m_timeToFull || old_timeToEmpty != m_timeToEmpty) {
293 Q_EMIT remainingTimeChanged(remainingTime(), m_device.data()->udi());
294 }
295 }
296}
297
298void Battery::updateCache()
299{
300 m_isPresent = isPresent();
301 m_chargePercent = chargePercent();
302 m_capacity = capacity();
303 m_isPowerSupply = isPowerSupply();
304 m_chargeState = chargeState();
305 m_timeToEmpty = timeToEmpty();
306 m_timeToFull = timeToFull();
307 m_energy = energy();
308 m_energyFull = energyFull();
309 m_energyFullDesign = energyFullDesign();
310 m_energyRate = energyRate();
311 m_voltage = voltage();
312 m_temperature = temperature();
313}
314
315#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.
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 Tue Mar 26 2024 11:17:12 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.