| 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 "qsysteminfo_simulator_p.h" |
| 42 | #include "qsysteminfobackend_simulator_p.h" |
| 43 | #include "qsysteminfoconnection_simulator_p.h" |
| 44 | |
| 45 | #include <QtCore/qmetaobject.h> |
| 46 | |
| 47 | QT_BEGIN_NAMESPACE |
| 48 | |
| 49 | // QBatteryInfoSimulator |
| 50 | |
| 51 | QBatteryInfoSimulator::QBatteryInfoSimulator(QBatteryInfo *parent) |
| 52 | : QObject(parent) |
| 53 | , q_ptr(parent) |
| 54 | , batteryInfoSimulatorBackend(QBatteryInfoSimulatorBackend::getSimulatorBackend()) |
| 55 | { |
| 56 | SystemInfoConnection::ensureSimulatorConnection(); |
| 57 | } |
| 58 | |
| 59 | QBatteryInfoSimulator::QBatteryInfoSimulator(int batteryIndex, QBatteryInfo *parent) |
| 60 | : QObject(parent) |
| 61 | , q_ptr(parent) |
| 62 | , batteryInfoSimulatorBackend(QBatteryInfoSimulatorBackend::getSimulatorBackend()) |
| 63 | { |
| 64 | SystemInfoConnection::ensureSimulatorConnection(); |
| 65 | setBatteryIndex(batteryIndex); |
| 66 | } |
| 67 | |
| 68 | QBatteryInfoSimulator::~QBatteryInfoSimulator() |
| 69 | { |
| 70 | } |
| 71 | |
| 72 | int QBatteryInfoSimulator::batteryCount() |
| 73 | { |
| 74 | if (batteryInfoSimulatorBackend) |
| 75 | return batteryInfoSimulatorBackend->getBatteryCount(); |
| 76 | |
| 77 | return -1; |
| 78 | } |
| 79 | |
| 80 | int QBatteryInfoSimulator::batteryIndex() const |
| 81 | { |
| 82 | if (batteryInfoSimulatorBackend) |
| 83 | return batteryInfoSimulatorBackend->getBatteryIndex(); |
| 84 | |
| 85 | return -1; |
| 86 | } |
| 87 | |
| 88 | bool QBatteryInfoSimulator::isValid() |
| 89 | { |
| 90 | // valid if the index < total count. |
| 91 | return (batteryIndex() >= 0) && (batteryIndex() < batteryCount()); |
| 92 | } |
| 93 | |
| 94 | void QBatteryInfoSimulator::setBatteryIndex(int batteryIndex) |
| 95 | { |
| 96 | if (batteryInfoSimulatorBackend) |
| 97 | batteryInfoSimulatorBackend->setBatteryIndex(batteryIndex); |
| 98 | } |
| 99 | |
| 100 | int QBatteryInfoSimulator::level() |
| 101 | { |
| 102 | if (batteryInfoSimulatorBackend) |
| 103 | return batteryInfoSimulatorBackend->getLevel(batteryInfoSimulatorBackend->getBatteryIndex()); |
| 104 | |
| 105 | return -1; |
| 106 | } |
| 107 | |
| 108 | int QBatteryInfoSimulator::currentFlow(int battery) |
| 109 | { |
| 110 | if (batteryInfoSimulatorBackend) |
| 111 | return batteryInfoSimulatorBackend->getCurrentFlow(battery); |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | int QBatteryInfoSimulator::currentFlow() |
| 117 | { |
| 118 | return currentFlow(batteryInfoSimulatorBackend->getBatteryIndex()); |
| 119 | } |
| 120 | |
| 121 | int QBatteryInfoSimulator::cycleCount() |
| 122 | { |
| 123 | if (batteryInfoSimulatorBackend) |
| 124 | return batteryInfoSimulatorBackend->getCycleCount(batteryInfoSimulatorBackend->getBatteryIndex()); |
| 125 | |
| 126 | return -1; |
| 127 | } |
| 128 | |
| 129 | int QBatteryInfoSimulator::maximumCapacity(int battery) |
| 130 | { |
| 131 | if (batteryInfoSimulatorBackend) |
| 132 | return batteryInfoSimulatorBackend->getMaximumCapacity(battery); |
| 133 | |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | int QBatteryInfoSimulator::maximumCapacity() |
| 138 | { |
| 139 | return maximumCapacity(batteryInfoSimulatorBackend->getBatteryIndex()); |
| 140 | } |
| 141 | |
| 142 | int QBatteryInfoSimulator::remainingCapacity(int battery) |
| 143 | { |
| 144 | if (batteryInfoSimulatorBackend) |
| 145 | return batteryInfoSimulatorBackend->getRemainingCapacity(battery); |
| 146 | |
| 147 | return -1; |
| 148 | } |
| 149 | |
| 150 | int QBatteryInfoSimulator::remainingCapacity() |
| 151 | { |
| 152 | return remainingCapacity(batteryInfoSimulatorBackend->getBatteryIndex()); |
| 153 | } |
| 154 | |
| 155 | int QBatteryInfoSimulator::remainingChargingTime(int battery) |
| 156 | { |
| 157 | if (batteryInfoSimulatorBackend) |
| 158 | return batteryInfoSimulatorBackend->getRemainingChargingTime(battery); |
| 159 | |
| 160 | return -1; |
| 161 | } |
| 162 | |
| 163 | int QBatteryInfoSimulator::remainingChargingTime() |
| 164 | { |
| 165 | return remainingChargingTime(batteryInfoSimulatorBackend->getBatteryIndex()); |
| 166 | } |
| 167 | |
| 168 | int QBatteryInfoSimulator::voltage(int battery) |
| 169 | { |
| 170 | if (batteryInfoSimulatorBackend) |
| 171 | return batteryInfoSimulatorBackend->getVoltage(battery); |
| 172 | |
| 173 | return -1; |
| 174 | } |
| 175 | |
| 176 | int QBatteryInfoSimulator::voltage() |
| 177 | { |
| 178 | return voltage(batteryInfoSimulatorBackend->getBatteryIndex()); |
| 179 | } |
| 180 | |
| 181 | QBatteryInfo::ChargerType QBatteryInfoSimulator::chargerType() |
| 182 | { |
| 183 | if (batteryInfoSimulatorBackend) |
| 184 | return batteryInfoSimulatorBackend->getChargerType(); |
| 185 | |
| 186 | return QBatteryInfo::UnknownCharger; |
| 187 | } |
| 188 | |
| 189 | QBatteryInfo::ChargingState QBatteryInfoSimulator::chargingState(int battery) |
| 190 | { |
| 191 | if (batteryInfoSimulatorBackend) |
| 192 | return batteryInfoSimulatorBackend->getChargingState(battery); |
| 193 | |
| 194 | return QBatteryInfo::UnknownChargingState; |
| 195 | } |
| 196 | |
| 197 | QBatteryInfo::ChargingState QBatteryInfoSimulator::chargingState() |
| 198 | { |
| 199 | return chargingState(batteryInfoSimulatorBackend->getBatteryIndex()); |
| 200 | } |
| 201 | |
| 202 | QBatteryInfo::LevelStatus QBatteryInfoSimulator::levelStatus(int battery) |
| 203 | { |
| 204 | if (batteryInfoSimulatorBackend) |
| 205 | return batteryInfoSimulatorBackend->getLevelStatus(battery); |
| 206 | |
| 207 | return QBatteryInfo::LevelUnknown; |
| 208 | } |
| 209 | |
| 210 | QBatteryInfo::LevelStatus QBatteryInfoSimulator::levelStatus() |
| 211 | { |
| 212 | return levelStatus(batteryInfoSimulatorBackend->getBatteryIndex()); |
| 213 | } |
| 214 | |
| 215 | QBatteryInfo::Health QBatteryInfoSimulator::health() |
| 216 | { |
| 217 | if (batteryInfoSimulatorBackend) |
| 218 | return batteryInfoSimulatorBackend->getHealth(batteryInfoSimulatorBackend->getBatteryIndex()); |
| 219 | |
| 220 | return QBatteryInfo::HealthUnknown; |
| 221 | } |
| 222 | |
| 223 | float QBatteryInfoSimulator::temperature() |
| 224 | { |
| 225 | if (batteryInfoSimulatorBackend) |
| 226 | return batteryInfoSimulatorBackend->getTemperature(batteryInfoSimulatorBackend->getBatteryIndex()); |
| 227 | |
| 228 | return -1.0f; |
| 229 | } |
| 230 | |
| 231 | extern QMetaMethod proxyToSourceSignal(const QMetaMethod &, QObject *); |
| 232 | |
| 233 | void QBatteryInfoSimulator::connectNotify(const QMetaMethod &signal) |
| 234 | { |
| 235 | static const QMetaMethod batteryCountChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoSimulator::batteryCountChanged); |
| 236 | static const QMetaMethod chargerTypeChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoSimulator::chargerTypeChanged); |
| 237 | static const QMetaMethod chargingStateChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoSimulator::chargingStateChanged); |
| 238 | static const QMetaMethod currentFlowChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoSimulator::currentFlowChanged); |
| 239 | static const QMetaMethod remainingCapacityChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoSimulator::remainingCapacityChanged); |
| 240 | static const QMetaMethod remainingChargingTimeChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoSimulator::remainingChargingTimeChanged); |
| 241 | static const QMetaMethod voltageChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoSimulator::voltageChanged); |
| 242 | static const QMetaMethod levelStatusChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoSimulator::levelStatusChanged); |
| 243 | static const QMetaMethod healthChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoSimulator::healthChanged); |
| 244 | static const QMetaMethod temperatureChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoSimulator::temperatureChanged); |
| 245 | |
| 246 | if (batteryInfoSimulatorBackend && (signal == batteryCountChangedSignal |
| 247 | || signal == currentFlowChangedSignal |
| 248 | || signal == voltageChangedSignal |
| 249 | || signal == remainingCapacityChangedSignal |
| 250 | || signal == remainingChargingTimeChangedSignal |
| 251 | || signal == chargerTypeChangedSignal |
| 252 | || signal == chargingStateChangedSignal |
| 253 | || signal == levelStatusChangedSignal |
| 254 | || signal == healthChangedSignal |
| 255 | || signal == temperatureChangedSignal)) { |
| 256 | QMetaMethod sourceSignal = proxyToSourceSignal(signal, batteryInfoSimulatorBackend); |
| 257 | connect(batteryInfoSimulatorBackend, sourceSignal, this, signal); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | void QBatteryInfoSimulator::disconnectNotify(const QMetaMethod &signal) |
| 262 | { |
| 263 | static const QMetaMethod batteryCountChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoSimulator::batteryCountChanged); |
| 264 | static const QMetaMethod chargerTypeChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoSimulator::chargerTypeChanged); |
| 265 | static const QMetaMethod chargingStateChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoSimulator::chargingStateChanged); |
| 266 | static const QMetaMethod currentFlowChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoSimulator::currentFlowChanged); |
| 267 | static const QMetaMethod remainingCapacityChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoSimulator::remainingCapacityChanged); |
| 268 | static const QMetaMethod remainingChargingTimeChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoSimulator::remainingChargingTimeChanged); |
| 269 | static const QMetaMethod voltageChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoSimulator::voltageChanged); |
| 270 | static const QMetaMethod levelStatusChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoSimulator::levelStatusChanged); |
| 271 | static const QMetaMethod healthChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoSimulator::healthChanged); |
| 272 | static const QMetaMethod temperatureChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoSimulator::temperatureChanged); |
| 273 | |
| 274 | if (batteryInfoSimulatorBackend && (signal == batteryCountChangedSignal |
| 275 | || signal == currentFlowChangedSignal |
| 276 | || signal == voltageChangedSignal |
| 277 | || signal == remainingCapacityChangedSignal |
| 278 | || signal == remainingChargingTimeChangedSignal |
| 279 | || signal == chargerTypeChangedSignal |
| 280 | || signal == chargingStateChangedSignal |
| 281 | || signal == levelStatusChangedSignal |
| 282 | || signal == healthChangedSignal |
| 283 | || signal == temperatureChangedSignal)) { |
| 284 | QMetaMethod sourceSignal = proxyToSourceSignal(signal, batteryInfoSimulatorBackend); |
| 285 | disconnect(batteryInfoSimulatorBackend, sourceSignal, this, signal); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | QT_END_NAMESPACE |