| 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 | |
| 12 | using namespace Solid::Backends::Fake; |
| 13 | |
| 14 | FakeBattery::FakeBattery(FakeDevice *device) |
| 15 | : FakeDeviceInterface(device) |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | FakeBattery::~FakeBattery() |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | bool FakeBattery::isPresent() const |
| 24 | { |
| 25 | return fakeDevice()->property(QStringLiteral("isPresent")).toBool(); |
| 26 | } |
| 27 | |
| 28 | Solid::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 | |
| 57 | int 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 | |
| 70 | int FakeBattery::capacity() const |
| 71 | { |
| 72 | return fakeDevice()->property(QStringLiteral("capacity")).toInt(); |
| 73 | } |
| 74 | |
| 75 | int FakeBattery::cycleCount() const |
| 76 | { |
| 77 | return fakeDevice()->property(QStringLiteral("cycleCount")).toInt(); |
| 78 | } |
| 79 | |
| 80 | bool FakeBattery::isRechargeable() const |
| 81 | { |
| 82 | return fakeDevice()->property(QStringLiteral("isRechargeable")).toBool(); |
| 83 | } |
| 84 | |
| 85 | bool FakeBattery::isPowerSupply() const |
| 86 | { |
| 87 | return fakeDevice()->property(QStringLiteral("isPowerSupply")).toBool(); |
| 88 | } |
| 89 | |
| 90 | Solid::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 | |
| 105 | qlonglong FakeBattery::timeToEmpty() const |
| 106 | { |
| 107 | return fakeDevice()->property(QStringLiteral("timeToEmpty")).toLongLong(); |
| 108 | } |
| 109 | |
| 110 | qlonglong FakeBattery::timeToFull() const |
| 111 | { |
| 112 | return fakeDevice()->property(QStringLiteral("timeToFull")).toLongLong(); |
| 113 | } |
| 114 | |
| 115 | void FakeBattery::setChargeState(Solid::Battery::ChargeState newState) |
| 116 | { |
| 117 | QString name; |
| 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"), value: name); |
| 135 | Q_EMIT chargeStateChanged(newState, udi: fakeDevice()->udi()); |
| 136 | } |
| 137 | |
| 138 | void FakeBattery::setChargeLevel(int newLevel) |
| 139 | { |
| 140 | fakeDevice()->setProperty(QStringLiteral("currentLevel"), value: newLevel); |
| 141 | Q_EMIT chargePercentChanged(value: chargePercent(), udi: fakeDevice()->udi()); |
| 142 | } |
| 143 | |
| 144 | Solid::Battery::Technology FakeBattery::technology() const |
| 145 | { |
| 146 | return (Solid::Battery::Technology)fakeDevice()->property(QStringLiteral("technology")).toInt(); |
| 147 | } |
| 148 | |
| 149 | double FakeBattery::energy() const |
| 150 | { |
| 151 | return fakeDevice()->property(QStringLiteral("energy")).toDouble(); |
| 152 | } |
| 153 | |
| 154 | double FakeBattery::energyFull() const |
| 155 | { |
| 156 | return fakeDevice()->property(QStringLiteral("energyFull")).toDouble(); |
| 157 | } |
| 158 | |
| 159 | double FakeBattery::energyFullDesign() const |
| 160 | { |
| 161 | return fakeDevice()->property(QStringLiteral("energyFullDesign")).toDouble(); |
| 162 | } |
| 163 | |
| 164 | double FakeBattery::energyRate() const |
| 165 | { |
| 166 | return fakeDevice()->property(QStringLiteral("energyRate")).toDouble(); |
| 167 | } |
| 168 | |
| 169 | double FakeBattery::voltage() const |
| 170 | { |
| 171 | return fakeDevice()->property(QStringLiteral("voltage")).toDouble(); |
| 172 | } |
| 173 | |
| 174 | double FakeBattery::temperature() const |
| 175 | { |
| 176 | return fakeDevice()->property(QStringLiteral("temperature")).toDouble(); |
| 177 | } |
| 178 | |
| 179 | QString FakeBattery::serial() const |
| 180 | { |
| 181 | return fakeDevice()->property(QStringLiteral("serial")).toString(); |
| 182 | } |
| 183 | |
| 184 | qlonglong FakeBattery::remainingTime() const |
| 185 | { |
| 186 | return fakeDevice()->property(QStringLiteral("remainingTime")).toLongLong(); |
| 187 | } |
| 188 | |
| 189 | #include "moc_fakebattery.cpp" |
| 190 |
