| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies). |
| 4 | ** Copyright (C) 2018 BlackBerry Limited. All rights reserved. |
| 5 | ** Contact: http://www.qt-project.org/legal |
| 6 | ** |
| 7 | ** This file is part of the QtSystems module of the Qt Toolkit. |
| 8 | ** |
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 10 | ** Commercial License Usage |
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 12 | ** accordance with the commercial license agreement provided with the |
| 13 | ** Software or, alternatively, in accordance with the terms contained in |
| 14 | ** a written agreement between you and The Qt Company. For licensing terms |
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 16 | ** information use the contact form at https://www.qt.io/contact-us. |
| 17 | ** |
| 18 | ** GNU Lesser General Public License Usage |
| 19 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 20 | ** General Public License version 3 as published by the Free Software |
| 21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 22 | ** packaging of this file. Please review the following information to |
| 23 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 25 | ** |
| 26 | ** GNU General Public License Usage |
| 27 | ** Alternatively, this file may be used under the terms of the GNU |
| 28 | ** General Public License version 2.0 or (at your option) the GNU General |
| 29 | ** Public license version 3 or any later version approved by the KDE Free |
| 30 | ** Qt Foundation. The licenses are as published by the Free Software |
| 31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 32 | ** included in the packaging of this file. Please review the following |
| 33 | ** information to ensure the GNU General Public License requirements will |
| 34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 35 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 36 | ** |
| 37 | ** $QT_END_LICENSE$ |
| 38 | ** |
| 39 | ****************************************************************************/ |
| 40 | |
| 41 | #include "qsysteminfobackend_simulator_p.h" |
| 42 | |
| 43 | #include <QMutex> |
| 44 | |
| 45 | QT_BEGIN_NAMESPACE |
| 46 | |
| 47 | QBatteryInfoSimulatorBackend *QBatteryInfoSimulatorBackend::globalSimulatorBackend = 0; |
| 48 | |
| 49 | // QBatteryInfoSimulatorBackend |
| 50 | |
| 51 | QBatteryInfoSimulatorBackend::QBatteryInfoSimulatorBackend(QObject *parent) |
| 52 | : QObject(parent) |
| 53 | { |
| 54 | data.index = 0; |
| 55 | data.currentFlow = 0; |
| 56 | data.cycleCount = -1; |
| 57 | data.maximumCapacity = -1; |
| 58 | data.remainingCapacity = -1; |
| 59 | data.remainingChargingTime = -1; |
| 60 | data.voltage = -1; |
| 61 | data.chargingState = QBatteryInfo::UnknownChargingState; |
| 62 | data.chargerType = QBatteryInfo::UnknownCharger; |
| 63 | data.levelStatus = QBatteryInfo::LevelUnknown; |
| 64 | data.health = QBatteryInfo::HealthUnknown; |
| 65 | } |
| 66 | |
| 67 | QBatteryInfoSimulatorBackend::~QBatteryInfoSimulatorBackend() |
| 68 | { |
| 69 | } |
| 70 | |
| 71 | QBatteryInfoSimulatorBackend *QBatteryInfoSimulatorBackend::getSimulatorBackend() |
| 72 | { |
| 73 | static QMutex mutex; |
| 74 | |
| 75 | mutex.lock(); |
| 76 | if (!globalSimulatorBackend) |
| 77 | globalSimulatorBackend = new QBatteryInfoSimulatorBackend(); |
| 78 | mutex.unlock(); |
| 79 | |
| 80 | return globalSimulatorBackend; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | int QBatteryInfoSimulatorBackend::getBatteryCount() |
| 85 | { |
| 86 | return 1; |
| 87 | } |
| 88 | |
| 89 | int QBatteryInfoSimulatorBackend::getBatteryIndex() const |
| 90 | { |
| 91 | return data.index; |
| 92 | } |
| 93 | |
| 94 | int QBatteryInfoSimulatorBackend::getLevel(int battery) |
| 95 | { |
| 96 | if (battery == 0) { |
| 97 | int maxCapacity = getMaximumCapacity(battery); |
| 98 | int remCapacity = getRemainingCapacity(battery); |
| 99 | |
| 100 | if (maxCapacity == 0) |
| 101 | return -1; |
| 102 | |
| 103 | return remCapacity * 100 / maxCapacity; |
| 104 | } |
| 105 | |
| 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | int QBatteryInfoSimulatorBackend::getCurrentFlow(int battery) |
| 110 | { |
| 111 | if (battery == 0) |
| 112 | return data.currentFlow; |
| 113 | return -1; |
| 114 | } |
| 115 | |
| 116 | int QBatteryInfoSimulatorBackend::getCycleCount(int battery) |
| 117 | { |
| 118 | if (battery == 0) |
| 119 | return data.cycleCount; |
| 120 | |
| 121 | return -1; |
| 122 | } |
| 123 | |
| 124 | int QBatteryInfoSimulatorBackend::getMaximumCapacity(int battery) |
| 125 | { |
| 126 | if (battery == 0) |
| 127 | return data.maximumCapacity; |
| 128 | return -1; |
| 129 | } |
| 130 | |
| 131 | int QBatteryInfoSimulatorBackend::getRemainingCapacity(int battery) |
| 132 | { |
| 133 | if (battery == 0) |
| 134 | return data.remainingCapacity; |
| 135 | return -1; |
| 136 | } |
| 137 | |
| 138 | int QBatteryInfoSimulatorBackend::getRemainingChargingTime(int battery) |
| 139 | { |
| 140 | if (battery == 0) |
| 141 | return data.remainingChargingTime; |
| 142 | return -1; |
| 143 | } |
| 144 | |
| 145 | int QBatteryInfoSimulatorBackend::getVoltage(int battery) |
| 146 | { |
| 147 | if (battery == 0) |
| 148 | return data.voltage; |
| 149 | return -1; |
| 150 | } |
| 151 | |
| 152 | QBatteryInfo::ChargingState QBatteryInfoSimulatorBackend::getChargingState(int battery) |
| 153 | { |
| 154 | if (battery == 0) |
| 155 | return data.chargingState; |
| 156 | |
| 157 | return QBatteryInfo::UnknownChargingState; |
| 158 | } |
| 159 | |
| 160 | QBatteryInfo::ChargerType QBatteryInfoSimulatorBackend::getChargerType() |
| 161 | { |
| 162 | return data.chargerType; |
| 163 | } |
| 164 | |
| 165 | QBatteryInfo::LevelStatus QBatteryInfoSimulatorBackend::getLevelStatus(int battery) |
| 166 | { |
| 167 | if (battery == 0) |
| 168 | return data.levelStatus; |
| 169 | |
| 170 | return QBatteryInfo::LevelUnknown; |
| 171 | } |
| 172 | |
| 173 | QBatteryInfo::Health QBatteryInfoSimulatorBackend::getHealth(int battery) |
| 174 | { |
| 175 | if (battery == 0) |
| 176 | return data.health; |
| 177 | |
| 178 | return QBatteryInfo::HealthUnknown; |
| 179 | } |
| 180 | |
| 181 | float QBatteryInfoSimulatorBackend::getTemperature(int battery) |
| 182 | { |
| 183 | if (battery == 0) |
| 184 | return data.temperature; |
| 185 | |
| 186 | return -1.0f; |
| 187 | } |
| 188 | |
| 189 | void QBatteryInfoSimulatorBackend::setBatteryIndex(int batteryIndex) |
| 190 | { |
| 191 | if (data.index != batteryIndex) { |
| 192 | data.index = batteryIndex; |
| 193 | emit batteryIndexChanged(data.index); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | void QBatteryInfoSimulatorBackend::setCurrentFlow(int flow) |
| 198 | { |
| 199 | if (data.currentFlow != flow) { |
| 200 | data.currentFlow = flow; |
| 201 | emit currentFlowChanged(flow); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | void QBatteryInfoSimulatorBackend::setCycleCount(int cycleCount) |
| 206 | { |
| 207 | if (data.cycleCount != cycleCount) { |
| 208 | data.cycleCount = cycleCount; |
| 209 | emit cycleCountChanged(cycleCount); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | void QBatteryInfoSimulatorBackend::setMaximumCapacity(int capacity) |
| 214 | { |
| 215 | if (data.maximumCapacity != capacity) { |
| 216 | int levelBefore = getLevel(0); |
| 217 | data.maximumCapacity = capacity; |
| 218 | int levelNow = getLevel(0); |
| 219 | if (levelBefore != levelNow) { |
| 220 | emit levelChanged(levelNow); |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | void QBatteryInfoSimulatorBackend::setRemainingCapacity(int capacity) |
| 226 | { |
| 227 | if (data.remainingCapacity != capacity) { |
| 228 | int levelBefore = getLevel(0); |
| 229 | data.remainingCapacity = capacity; |
| 230 | emit remainingCapacityChanged(capacity); |
| 231 | int levelNow = getLevel(0); |
| 232 | if (levelBefore != levelNow) { |
| 233 | emit levelChanged(levelNow); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | void QBatteryInfoSimulatorBackend::setVoltage(int vol) |
| 239 | { |
| 240 | if (data.voltage != vol) { |
| 241 | data.voltage = vol; |
| 242 | emit voltageChanged(vol); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | void QBatteryInfoSimulatorBackend::setRemainingChargingTime(int time) |
| 247 | { |
| 248 | if (data.remainingChargingTime != time) { |
| 249 | data.remainingChargingTime = time; |
| 250 | emit remainingChargingTimeChanged(time); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | void QBatteryInfoSimulatorBackend::setChargingState(QBatteryInfo::ChargingState state) |
| 255 | { |
| 256 | if (data.chargingState != state) { |
| 257 | data.chargingState = state; |
| 258 | emit chargingStateChanged(state); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | void QBatteryInfoSimulatorBackend::setChargerType(QBatteryInfo::ChargerType type) |
| 263 | { |
| 264 | if (data.chargerType != type) { |
| 265 | data.chargerType = type; |
| 266 | emit chargerTypeChanged(type); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | void QBatteryInfoSimulatorBackend::setLevelStatus(QBatteryInfo::LevelStatus levelStatus) |
| 271 | { |
| 272 | if (data.levelStatus != levelStatus) { |
| 273 | data.levelStatus = levelStatus; |
| 274 | emit levelStatusChanged(levelStatus); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | void QBatteryInfoSimulatorBackend::setHealth(QBatteryInfo::Health health) |
| 279 | { |
| 280 | if (data.health != health) { |
| 281 | data.health = health; |
| 282 | emit healthChanged(health); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | void QBatteryInfoSimulatorBackend::setTemperature(float temperature) |
| 287 | { |
| 288 | if (!qFuzzyCompare(data.temperature, temperature)) { |
| 289 | data.temperature = temperature; |
| 290 | emit temperatureChanged(temperature); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | QT_END_NAMESPACE |