9#include "upowerbattery.h"
13using namespace Solid::Backends::UPower;
15Battery::Battery(UPowerDevice *device)
16 : DeviceInterface(device)
18 connect(device, &UPowerDevice::propertyChanged,
this, &Battery::slotChanged);
27bool Battery::isPresent()
const
29 return m_device.
data()->prop(QStringLiteral(
"IsPresent")).toBool();
35 const auto t =
static_cast<UpDeviceKind
>(m_device.
data()->prop(QStringLiteral(
"Type")).toUInt());
37 case UP_DEVICE_KIND_LINE_POWER:
39 case UP_DEVICE_KIND_BATTERY:
40 result = Solid::Battery::PrimaryBattery;
42 case UP_DEVICE_KIND_UPS:
43 result = Solid::Battery::UpsBattery;
45 case UP_DEVICE_KIND_MONITOR:
46 result = Solid::Battery::MonitorBattery;
48 case UP_DEVICE_KIND_MOUSE:
49 result = Solid::Battery::MouseBattery;
51 case UP_DEVICE_KIND_KEYBOARD:
52 result = Solid::Battery::KeyboardBattery;
54 case UP_DEVICE_KIND_PDA:
55 result = Solid::Battery::PdaBattery;
57 case UP_DEVICE_KIND_PHONE:
58 result = Solid::Battery::PhoneBattery;
60 case UP_DEVICE_KIND_TABLET:
61 result = Solid::Battery::TabletBattery;
63 case UP_DEVICE_KIND_GAMING_INPUT:
64 result = Solid::Battery::GamingInputBattery;
66 case UP_DEVICE_KIND_HEADPHONES:
67 result = Solid::Battery::HeadphoneBattery;
69 case UP_DEVICE_KIND_HEADSET:
70 result = Solid::Battery::HeadsetBattery;
72 case UP_DEVICE_KIND_TOUCHPAD:
73 result = Solid::Battery::TouchpadBattery;
75 case UP_DEVICE_KIND_BLUETOOTH_GENERIC:
76 result = Solid::Battery::BluetoothBattery;
78 case UP_DEVICE_KIND_UNKNOWN:
82 if (result == Solid::Battery::UnknownBattery) {
86 result = Solid::Battery::BluetoothBattery;
93int Battery::chargePercent()
const
95 return qRound(m_device.
data()->prop(QStringLiteral(
"Percentage")).toDouble());
98int Battery::capacity()
const
100 return qRound(m_device.
data()->prop(QStringLiteral(
"Capacity")).toDouble());
103int Battery::cycleCount()
const
107 const int cycleCount = m_device.
data()->prop(QStringLiteral(
"ChargeCycles")).toInt(&ok);
115bool Battery::isRechargeable()
const
117 return m_device.
data()->prop(QStringLiteral(
"IsRechargeable")).toBool();
120bool Battery::isPowerSupply()
const
122 return m_device.
data()->prop(QStringLiteral(
"PowerSupply")).toBool();
128 const UpDeviceState state =
static_cast<UpDeviceState
>(m_device.
data()->prop(QStringLiteral(
"State")).toUInt());
130 case UP_DEVICE_STATE_UNKNOWN:
131 result = Solid::Battery::NoCharge;
133 case UP_DEVICE_STATE_CHARGING:
134 result = Solid::Battery::Charging;
136 case UP_DEVICE_STATE_DISCHARGING:
137 result = Solid::Battery::Discharging;
139 case UP_DEVICE_STATE_EMPTY:
141 case UP_DEVICE_STATE_FULLY_CHARGED:
142 result = Solid::Battery::FullyCharged;
144 case UP_DEVICE_STATE_PENDING_CHARGE:
146 case UP_DEVICE_STATE_PENDING_DISCHARGE:
148 case UP_DEVICE_STATE_LAST:
154qlonglong Battery::timeToEmpty()
const
156 return m_device.
data()->prop(QStringLiteral(
"TimeToEmpty")).toLongLong();
159qlonglong Battery::timeToFull()
const
161 return m_device.
data()->prop(QStringLiteral(
"TimeToFull")).toLongLong();
166 const UpDeviceTechnology tech =
static_cast<UpDeviceTechnology
>(m_device.
data()->prop(QStringLiteral(
"Technology")).toUInt());
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;
185 return Solid::Battery::UnknownTechnology;
188double Battery::energy()
const
190 return m_device.
data()->prop(QStringLiteral(
"Energy")).toDouble();
193double Battery::energyFull()
const
195 return m_device.
data()->prop(QStringLiteral(
"EnergyFull")).toDouble();
198double Battery::energyFullDesign()
const
200 return m_device.
data()->prop(QStringLiteral(
"EnergyFullDesign")).toDouble();
203double Battery::energyRate()
const
205 return m_device.
data()->prop(QStringLiteral(
"EnergyRate")).toDouble();
208double Battery::voltage()
const
210 return m_device.
data()->prop(QStringLiteral(
"Voltage")).toDouble();
213double Battery::temperature()
const
215 return m_device.
data()->prop(QStringLiteral(
"Temperature")).toDouble();
220 return m_device.
data()->prop(QStringLiteral(
"Serial")).toString();
223qlonglong Battery::remainingTime()
const
225 if (chargeState() == Solid::Battery::Charging) {
227 }
else if (chargeState() == Solid::Battery::Discharging) {
228 return timeToEmpty();
234void Battery::slotChanged()
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;
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;
253 if (old_isPresent != m_isPresent) {
254 Q_EMIT presentStateChanged(m_isPresent, m_device.
data()->udi());
257 if (old_chargePercent != m_chargePercent) {
258 Q_EMIT chargePercentChanged(m_chargePercent, m_device.
data()->udi());
261 if (old_capacity != m_capacity) {
262 Q_EMIT capacityChanged(m_capacity, m_device.
data()->udi());
265 if (old_cycleCount != m_cycleCount) {
266 Q_EMIT cycleCountChanged(m_cycleCount, m_device.
data()->udi());
269 if (old_isPowerSupply != m_isPowerSupply) {
270 Q_EMIT powerSupplyStateChanged(m_isPowerSupply, m_device.
data()->udi());
273 if (old_chargeState != m_chargeState) {
274 Q_EMIT chargeStateChanged(m_chargeState, m_device.
data()->udi());
277 if (old_timeToEmpty != m_timeToEmpty) {
278 Q_EMIT timeToEmptyChanged(m_timeToEmpty, m_device.
data()->udi());
281 if (old_timeToFull != m_timeToFull) {
282 Q_EMIT timeToFullChanged(m_timeToFull, m_device.
data()->udi());
285 if (old_energy != m_energy) {
286 Q_EMIT energyChanged(m_energy, m_device.
data()->udi());
289 if (old_energyFull != m_energyFull) {
290 Q_EMIT energyFullChanged(m_energyFull, m_device.
data()->udi());
293 if (old_energyFullDesign != m_energyFullDesign) {
294 Q_EMIT energyFullChanged(m_energyFullDesign, m_device.
data()->udi());
297 if (old_energyRate != m_energyRate) {
298 Q_EMIT energyRateChanged(m_energyRate, m_device.
data()->udi());
301 if (old_voltage != m_voltage) {
302 Q_EMIT voltageChanged(m_voltage, m_device.
data()->udi());
305 if (old_temperature != m_temperature) {
306 Q_EMIT temperatureChanged(m_temperature, m_device.
data()->udi());
309 if (old_timeToFull != m_timeToFull || old_timeToEmpty != m_timeToEmpty) {
310 Q_EMIT remainingTimeChanged(remainingTime(), m_device.
data()->udi());
315void Battery::updateCache()
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();
326 m_energyFull = energyFull();
327 m_energyFullDesign = energyFullDesign();
328 m_energyRate = energyRate();
329 m_voltage = voltage();
330 m_temperature = temperature();
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)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)