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(QStringLiteral("isPresent")).toBool();
26}
27
28Solid::Battery::BatteryType FakeBattery::type() const
29{
30 QString name = fakeDevice()->property(QStringLiteral("batteryType")).toString();
31
32 if (name == QLatin1String("pda")) {
33 return Solid::Battery::PdaBattery;
34 } else if (name == QLatin1String("ups")) {
35 return Solid::Battery::UpsBattery;
36 } else if (name == QLatin1String("primary")) {
37 return Solid::Battery::PrimaryBattery;
38 } else if (name == QLatin1String("mouse")) {
39 return Solid::Battery::MouseBattery;
40 } else if (name == QLatin1String("keyboard")) {
41 return Solid::Battery::KeyboardBattery;
42 } else if (name == QLatin1String("keyboard_mouse")) {
43 return Solid::Battery::KeyboardMouseBattery;
44 } else if (name == QLatin1String("camera")) {
45 return Solid::Battery::CameraBattery;
46 } else if (name == QLatin1String("gaminginput")) {
47 return Solid::Battery::GamingInputBattery;
48 } else if (name == QLatin1String("bluetooth")) {
49 return Solid::Battery::BluetoothBattery;
50 } else if (name == QLatin1String("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(QStringLiteral("lastFullLevel")).toInt();
60 int current = fakeDevice()->property(QStringLiteral("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(QStringLiteral("capacity")).toInt();
73}
74
75int FakeBattery::cycleCount() const
76{
77 return fakeDevice()->property(QStringLiteral("cycleCount")).toInt();
78}
79
80bool FakeBattery::isRechargeable() const
81{
82 return fakeDevice()->property(QStringLiteral("isRechargeable")).toBool();
83}
84
85bool FakeBattery::isPowerSupply() const
86{
87 return fakeDevice()->property(QStringLiteral("isPowerSupply")).toBool();
88}
89
90Solid::Battery::ChargeState FakeBattery::chargeState() const
91{
92 QString state = fakeDevice()->property(QStringLiteral("chargeState")).toString();
93
94 if (state == QLatin1String("charging")) {
95 return Solid::Battery::Charging;
96 } else if (state == QLatin1String("discharging")) {
97 return Solid::Battery::Discharging;
98 } else if (state == QLatin1String("fullyCharged")) {
99 return Solid::Battery::FullyCharged;
100 } else {
101 return Solid::Battery::NoCharge;
102 }
103}
104
105qlonglong FakeBattery::timeToEmpty() const
106{
107 return fakeDevice()->property(QStringLiteral("timeToEmpty")).toLongLong();
108}
109
110qlonglong FakeBattery::timeToFull() const
111{
112 return fakeDevice()->property(QStringLiteral("timeToFull")).toLongLong();
113}
114
115void FakeBattery::setChargeState(Solid::Battery::ChargeState newState)
116{
118
119 switch (newState) {
120 case Solid::Battery::Charging:
121 name = QStringLiteral("charging");
122 break;
123 case Solid::Battery::Discharging:
124 name = QStringLiteral("discharging");
125 break;
126 case Solid::Battery::NoCharge:
127 name = QStringLiteral("noCharge");
128 break;
129 case Solid::Battery::FullyCharged:
130 name = QStringLiteral("fullyCharged");
131 break;
132 }
133
134 fakeDevice()->setProperty(QStringLiteral("chargeState"), name);
135 Q_EMIT chargeStateChanged(newState, fakeDevice()->udi());
136}
137
138void FakeBattery::setChargeLevel(int newLevel)
139{
140 fakeDevice()->setProperty(QStringLiteral("currentLevel"), newLevel);
141 Q_EMIT chargePercentChanged(chargePercent(), fakeDevice()->udi());
142}
143
144Solid::Battery::Technology FakeBattery::technology() const
145{
146 return (Solid::Battery::Technology)fakeDevice()->property(QStringLiteral("technology")).toInt();
147}
148
149double FakeBattery::energy() const
150{
151 return fakeDevice()->property(QStringLiteral("energy")).toDouble();
152}
153
154double FakeBattery::energyFull() const
155{
156 return fakeDevice()->property(QStringLiteral("energyFull")).toDouble();
157}
158
159double FakeBattery::energyFullDesign() const
160{
161 return fakeDevice()->property(QStringLiteral("energyFullDesign")).toDouble();
162}
163
164double FakeBattery::energyRate() const
165{
166 return fakeDevice()->property(QStringLiteral("energyRate")).toDouble();
167}
168
169double FakeBattery::voltage() const
170{
171 return fakeDevice()->property(QStringLiteral("voltage")).toDouble();
172}
173
174double FakeBattery::temperature() const
175{
176 return fakeDevice()->property(QStringLiteral("temperature")).toDouble();
177}
178
179QString FakeBattery::serial() const
180{
181 return fakeDevice()->property(QStringLiteral("serial")).toString();
182}
183
184qlonglong FakeBattery::remainingTime() const
185{
186 return fakeDevice()->property(QStringLiteral("remainingTime")).toLongLong();
187}
188
189#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 Sat Dec 21 2024 17:03:23 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.