Solid

fakebattery.cpp
1/*
2 SPDX-FileCopyrightText: 2006 Kevin Ottens <ervin@kde.org>
3 SPDX-FileCopyrightText: 2012 Lukas 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 "fakebattery.h"
10#include <QVariant>
11
12using namespace Solid::Backends::Fake;
13
14FakeBattery::FakeBattery(FakeDevice *device)
15 : FakeDeviceInterface(device)
16{
17}
18
19FakeBattery::~FakeBattery()
20{
21}
22
23bool FakeBattery::isPresent() const
24{
25 return fakeDevice()->property("isPresent").toBool();
26}
27
28Solid::Battery::BatteryType FakeBattery::type() const
29{
30 QString name = fakeDevice()->property("batteryType").toString();
31
32 if (name == "pda") {
33 return Solid::Battery::PdaBattery;
34 } else if (name == "ups") {
35 return Solid::Battery::UpsBattery;
36 } else if (name == "primary") {
37 return Solid::Battery::PrimaryBattery;
38 } else if (name == "mouse") {
39 return Solid::Battery::MouseBattery;
40 } else if (name == "keyboard") {
41 return Solid::Battery::KeyboardBattery;
42 } else if (name == "keyboard_mouse") {
43 return Solid::Battery::KeyboardMouseBattery;
44 } else if (name == "camera") {
45 return Solid::Battery::CameraBattery;
46 } else if (name == "gaminginput") {
47 return Solid::Battery::GamingInputBattery;
48 } else if (name == "bluetooth") {
49 return Solid::Battery::BluetoothBattery;
50 } else if (name == "tablet") {
51 return Solid::Battery::TabletBattery;
52 } else {
53 return Solid::Battery::UnknownBattery;
54 }
55}
56
57int FakeBattery::chargePercent() const
58{
59 int last_full = fakeDevice()->property("lastFullLevel").toInt();
60 int current = fakeDevice()->property("currentLevel").toInt();
61
62 int percent = 0;
63 if (last_full > 0) {
64 percent = (100 * current) / last_full;
65 }
66
67 return percent;
68}
69
70int FakeBattery::capacity() const
71{
72 return fakeDevice()->property("capacity").toInt();
73}
74
75bool FakeBattery::isRechargeable() const
76{
77 return fakeDevice()->property("isRechargeable").toBool();
78}
79
80bool FakeBattery::isPowerSupply() const
81{
82 return fakeDevice()->property("isPowerSupply").toBool();
83}
84
85Solid::Battery::ChargeState FakeBattery::chargeState() const
86{
87 QString state = fakeDevice()->property("chargeState").toString();
88
89 if (state == "charging") {
90 return Solid::Battery::Charging;
91 } else if (state == "discharging") {
92 return Solid::Battery::Discharging;
93 } else if (state == "fullyCharged") {
94 return Solid::Battery::FullyCharged;
95 } else {
96 return Solid::Battery::NoCharge;
97 }
98}
99
100qlonglong FakeBattery::timeToEmpty() const
101{
102 return fakeDevice()->property("timeToEmpty").toLongLong();
103}
104
105qlonglong FakeBattery::timeToFull() const
106{
107 return fakeDevice()->property("timeToFull").toLongLong();
108}
109
110void FakeBattery::setChargeState(Solid::Battery::ChargeState newState)
111{
113
114 switch (newState) {
115 case Solid::Battery::Charging:
116 name = "charging";
117 break;
118 case Solid::Battery::Discharging:
119 name = "discharging";
120 break;
121 case Solid::Battery::NoCharge:
122 name = "noCharge";
123 break;
124 case Solid::Battery::FullyCharged:
125 name = "fullyCharged";
126 break;
127 }
128
129 fakeDevice()->setProperty("chargeState", name);
130 Q_EMIT chargeStateChanged(newState, fakeDevice()->udi());
131}
132
133void FakeBattery::setChargeLevel(int newLevel)
134{
135 fakeDevice()->setProperty("currentLevel", newLevel);
136 Q_EMIT chargePercentChanged(chargePercent(), fakeDevice()->udi());
137}
138
139Solid::Battery::Technology FakeBattery::technology() const
140{
141 return (Solid::Battery::Technology)fakeDevice()->property("technology").toInt();
142}
143
144double FakeBattery::energy() const
145{
146 return fakeDevice()->property("energy").toDouble();
147}
148
149double FakeBattery::energyFull() const
150{
151 return fakeDevice()->property("energyFull").toDouble();
152}
153
154double FakeBattery::energyFullDesign() const
155{
156 return fakeDevice()->property("energyFullDesign").toDouble();
157}
158
159double FakeBattery::energyRate() const
160{
161 return fakeDevice()->property("energyRate").toDouble();
162}
163
164double FakeBattery::voltage() const
165{
166 return fakeDevice()->property("voltage").toDouble();
167}
168
169double FakeBattery::temperature() const
170{
171 return fakeDevice()->property("temperature").toDouble();
172}
173
174QString FakeBattery::serial() const
175{
176 return fakeDevice()->property("serial").toString();
177}
178
179qlonglong FakeBattery::remainingTime() const
180{
181 return fakeDevice()->property("remainingTime").toLongLong();
182}
183
184#include "moc_fakebattery.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.
QString name(StandardAction id)
Q_EMITQ_EMIT
bool toBool() const const
double toDouble(bool *ok) const const
int toInt(bool *ok) const const
qlonglong toLongLong(bool *ok) const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:53:47 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.