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
75bool FakeBattery::isRechargeable() const
76{
77 return fakeDevice()->property(QStringLiteral("isRechargeable")).toBool();
78}
79
80bool FakeBattery::isPowerSupply() const
81{
82 return fakeDevice()->property(QStringLiteral("isPowerSupply")).toBool();
83}
84
85Solid::Battery::ChargeState FakeBattery::chargeState() const
86{
87 QString state = fakeDevice()->property(QStringLiteral("chargeState")).toString();
88
89 if (state == QLatin1String("charging")) {
90 return Solid::Battery::Charging;
91 } else if (state == QLatin1String("discharging")) {
92 return Solid::Battery::Discharging;
93 } else if (state == QLatin1String("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(QStringLiteral("timeToEmpty")).toLongLong();
103}
104
105qlonglong FakeBattery::timeToFull() const
106{
107 return fakeDevice()->property(QStringLiteral("timeToFull")).toLongLong();
108}
109
110void FakeBattery::setChargeState(Solid::Battery::ChargeState newState)
111{
113
114 switch (newState) {
115 case Solid::Battery::Charging:
116 name = QStringLiteral("charging");
117 break;
118 case Solid::Battery::Discharging:
119 name = QStringLiteral("discharging");
120 break;
121 case Solid::Battery::NoCharge:
122 name = QStringLiteral("noCharge");
123 break;
124 case Solid::Battery::FullyCharged:
125 name = QStringLiteral("fullyCharged");
126 break;
127 }
128
129 fakeDevice()->setProperty(QStringLiteral("chargeState"), name);
130 Q_EMIT chargeStateChanged(newState, fakeDevice()->udi());
131}
132
133void FakeBattery::setChargeLevel(int newLevel)
134{
135 fakeDevice()->setProperty(QStringLiteral("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(QStringLiteral("technology")).toInt();
142}
143
144double FakeBattery::energy() const
145{
146 return fakeDevice()->property(QStringLiteral("energy")).toDouble();
147}
148
149double FakeBattery::energyFull() const
150{
151 return fakeDevice()->property(QStringLiteral("energyFull")).toDouble();
152}
153
154double FakeBattery::energyFullDesign() const
155{
156 return fakeDevice()->property(QStringLiteral("energyFullDesign")).toDouble();
157}
158
159double FakeBattery::energyRate() const
160{
161 return fakeDevice()->property(QStringLiteral("energyRate")).toDouble();
162}
163
164double FakeBattery::voltage() const
165{
166 return fakeDevice()->property(QStringLiteral("voltage")).toDouble();
167}
168
169double FakeBattery::temperature() const
170{
171 return fakeDevice()->property(QStringLiteral("temperature")).toDouble();
172}
173
174QString FakeBattery::serial() const
175{
176 return fakeDevice()->property(QStringLiteral("serial")).toString();
177}
178
179qlonglong FakeBattery::remainingTime() const
180{
181 return fakeDevice()->property(QStringLiteral("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 Oct 11 2024 12:08:14 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.