| 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 "qbatteryinfo.h" |
| 42 | |
| 43 | #include <QtCore/qnumeric.h> |
| 44 | |
| 45 | #if defined(QT_SIMULATOR) |
| 46 | # include "simulator/qsysteminfo_simulator_p.h" |
| 47 | #elif defined(Q_OS_LINUX) |
| 48 | #if defined(QT_NO_UPOWER) |
| 49 | #include "linux/qbatteryinfo_linux_p.h" |
| 50 | #else |
| 51 | #include "linux/qbatteryinfo_upower_p.h" |
| 52 | #endif |
| 53 | #elif defined(Q_OS_WIN) |
| 54 | # include "windows/qbatteryinfo_win_p.h" |
| 55 | #elif defined(Q_OS_MAC) |
| 56 | # include "mac/qbatteryinfo_mac_p.h" |
| 57 | #else |
| 58 | QT_BEGIN_NAMESPACE |
| 59 | class QBatteryInfoPrivate |
| 60 | { |
| 61 | public: |
| 62 | QBatteryInfoPrivate(QBatteryInfo *) {} |
| 63 | QBatteryInfoPrivate(int batteryIndex, QBatteryInfo *): index(batteryIndex) {} |
| 64 | |
| 65 | int batteryCount() { return -1; } |
| 66 | int batteryIndex() { return index; } |
| 67 | bool isValid() { return false; } |
| 68 | void setBatteryIndex(int batteryIndex) { index = batteryIndex; } |
| 69 | int currentFlow() { return 0; } |
| 70 | int cycleCount() { return -1; } |
| 71 | int maximumCapacity() { return -1; } |
| 72 | int remainingCapacity() { return -1; } |
| 73 | int remainingChargingTime() { return -1; } |
| 74 | int voltage() { return -1; } |
| 75 | QBatteryInfo::ChargerType chargerType() { return QBatteryInfo::UnknownCharger; } |
| 76 | QBatteryInfo::ChargingState chargingState() { return QBatteryInfo::UnknownChargingState; } |
| 77 | QBatteryInfo::LevelStatus levelStatus() { return QBatteryInfo::LevelUnknown; } |
| 78 | QBatteryInfo::Health health() { return QBatteryInfo::HealthUnknown; } |
| 79 | float temperature() { return qQNaN(); } |
| 80 | private: |
| 81 | int index; |
| 82 | }; |
| 83 | QT_END_NAMESPACE |
| 84 | #endif |
| 85 | |
| 86 | #include <QtCore/qmetaobject.h> |
| 87 | |
| 88 | QT_BEGIN_NAMESPACE |
| 89 | |
| 90 | /*! |
| 91 | \class QBatteryInfo |
| 92 | \inmodule QtSystemInfo |
| 93 | \brief The QBatteryInfo class provides various information about the batteries. |
| 94 | \ingroup systeminfo |
| 95 | |
| 96 | Note that on some platforms, listening to the signals could lead to a heavy CPU usage. Therefore, |
| 97 | you are strongly suggested to disconnect the signals when no longer needed in your application. |
| 98 | |
| 99 | Battery index starts at \c 0, which indicates the first battery. |
| 100 | */ |
| 101 | |
| 102 | /*! |
| 103 | \enum QBatteryInfo::ChargerType |
| 104 | This enum describes the type of charger used. |
| 105 | |
| 106 | \value UnknownCharger The charger type is unknown, or no charger. |
| 107 | \value WallCharger Using wall (mains) charger. |
| 108 | \value USBCharger Using USB charger when the system cannot differentiate the current. |
| 109 | \value VariableCurrentCharger Using variable current charger such as bicycle or solar. |
| 110 | */ |
| 111 | |
| 112 | /*! |
| 113 | \enum QBatteryInfo::ChargingState |
| 114 | This enum describes the charging state. |
| 115 | |
| 116 | \value UnknownChargingState The charging state is unknown or charging error occured. |
| 117 | \value Charging The battery is charging. |
| 118 | \value IdleChargingState The battery is idle (neither Charging nor Discharging) |
| 119 | \value Discharging The battery is discharging. |
| 120 | */ |
| 121 | |
| 122 | /*! |
| 123 | \enum QBatteryInfo::LevelStatus |
| 124 | This enum describes the level status of the battery. |
| 125 | |
| 126 | \value LevelUnknown Battery level undetermined. |
| 127 | \value LevelEmpty Battery is considered be empty and device needs to shut down. |
| 128 | \value LevelLow Battery level is low and warnings need to be issued to the user. |
| 129 | \value LevelOk Battery level is Ok. It is above "Low" but not "Full". |
| 130 | \value LevelFull Battery is fully charged. |
| 131 | */ |
| 132 | |
| 133 | /*! |
| 134 | \enum QBatteryInfo::Health |
| 135 | This enum describes the health of the battery. |
| 136 | |
| 137 | \value HealthUnknown Battery health undetermined |
| 138 | \value HealthOk Battery health is OK |
| 139 | \value HealthBad Battery health is bad |
| 140 | */ |
| 141 | |
| 142 | /*! |
| 143 | Constructs a \l QBatteryInfo object with the given \a parent. The \l batteryIndex() |
| 144 | will default to \c 0. |
| 145 | */ |
| 146 | QBatteryInfo::QBatteryInfo(QObject *parent) |
| 147 | : QObject(parent) |
| 148 | #if !defined(QT_SIMULATOR) |
| 149 | , d_ptr(new QBatteryInfoPrivate(this)) |
| 150 | #else |
| 151 | , d_ptr(new QBatteryInfoSimulator(this)) |
| 152 | #endif // QT_SIMULATOR |
| 153 | |
| 154 | { |
| 155 | } |
| 156 | |
| 157 | /*! |
| 158 | Constructs a \l QBatteryInfo object with the given \a batteryIndex and \a parent. |
| 159 | */ |
| 160 | QBatteryInfo::QBatteryInfo(int batteryIndex, QObject *parent) |
| 161 | : QObject(parent) |
| 162 | #if !defined(QT_SIMULATOR) |
| 163 | , d_ptr(new QBatteryInfoPrivate(batteryIndex, this)) |
| 164 | #else |
| 165 | , d_ptr(new QBatteryInfoSimulator(batteryIndex, this)) |
| 166 | #endif // QT_SIMULATOR |
| 167 | |
| 168 | { |
| 169 | } |
| 170 | |
| 171 | /*! |
| 172 | Destroys the object |
| 173 | */ |
| 174 | QBatteryInfo::~QBatteryInfo() |
| 175 | { |
| 176 | } |
| 177 | |
| 178 | /*! |
| 179 | \property QBatteryInfo::batteryCount |
| 180 | \brief The number of batteries available. |
| 181 | |
| 182 | In case of an error or if the information is not available \c -1 is returned. |
| 183 | */ |
| 184 | int QBatteryInfo::batteryCount() const |
| 185 | { |
| 186 | return d_ptr->batteryCount(); |
| 187 | } |
| 188 | |
| 189 | /*! |
| 190 | \property QBatteryInfo::batteryIndex |
| 191 | \brief The current battery index |
| 192 | |
| 193 | The first battery is represented by \c 0. |
| 194 | */ |
| 195 | int QBatteryInfo::batteryIndex() const |
| 196 | { |
| 197 | return d_ptr->batteryIndex(); |
| 198 | } |
| 199 | |
| 200 | void QBatteryInfo::setBatteryIndex(int batteryIndex) |
| 201 | { |
| 202 | d_ptr->setBatteryIndex(batteryIndex); |
| 203 | } |
| 204 | |
| 205 | /*! |
| 206 | \property QBatteryInfo::valid |
| 207 | \brief The validity of this instance |
| 208 | |
| 209 | If this property returns \c false \l batteryIndex() is the only other method that will return |
| 210 | a valid value. All other methods will return default or invalid values and should not be relied |
| 211 | upon for displaying to the user or for comparisons. |
| 212 | */ |
| 213 | bool QBatteryInfo::isValid() const |
| 214 | { |
| 215 | return d_ptr->isValid(); |
| 216 | } |
| 217 | |
| 218 | /*! |
| 219 | \property QBatteryInfo::currentFlow |
| 220 | \brief The current flow of the battery |
| 221 | |
| 222 | This value is measured in milliamperes (mA). A positive returned value means the battery is |
| 223 | discharging, while a negative value means the battery is charging. In case of an error or if the |
| 224 | information is not available \c 0 is returned. |
| 225 | */ |
| 226 | int QBatteryInfo::currentFlow() const |
| 227 | { |
| 228 | return d_ptr->currentFlow(); |
| 229 | } |
| 230 | |
| 231 | /*! |
| 232 | \property QBatteryInfo::level |
| 233 | \brief The level of the battery as a percentage |
| 234 | |
| 235 | In case of an error or if the information is not available \c -1 is returned. |
| 236 | |
| 237 | \sa maximumCapacity(), remainingCapacity(), levelStatus() |
| 238 | */ |
| 239 | int QBatteryInfo::level() const |
| 240 | { |
| 241 | return d_ptr->level(); |
| 242 | } |
| 243 | |
| 244 | /*! |
| 245 | \property QBatteryInfo::cycleCount |
| 246 | \brief The cycle count of the battery |
| 247 | |
| 248 | In case of an error or if the information is not available \c -1 is returned. |
| 249 | */ |
| 250 | int QBatteryInfo::cycleCount() const |
| 251 | { |
| 252 | return d_ptr->cycleCount(); |
| 253 | } |
| 254 | |
| 255 | /*! |
| 256 | \property QBatteryInfo::maximumCapacity |
| 257 | \brief The maximum capacity of the battery |
| 258 | |
| 259 | This value is measured in mAh. In case of an error or if the information is not available \c -1 |
| 260 | is returned. |
| 261 | |
| 262 | \sa remainingCapacity(), level(), levelStatus() |
| 263 | */ |
| 264 | int QBatteryInfo::maximumCapacity() const |
| 265 | { |
| 266 | return d_ptr->maximumCapacity(); |
| 267 | } |
| 268 | |
| 269 | /*! |
| 270 | \property QBatteryInfo::remainingCapacity |
| 271 | \brief The remaining capacity of the battery |
| 272 | |
| 273 | This value is measured in mAh. In case of an error or if the information is not available \c -1 |
| 274 | is returned. |
| 275 | |
| 276 | \sa maximumCapacity(), level(), levelStatus() |
| 277 | */ |
| 278 | int QBatteryInfo::remainingCapacity() const |
| 279 | { |
| 280 | return d_ptr->remainingCapacity(); |
| 281 | } |
| 282 | |
| 283 | /*! |
| 284 | \property QBatteryInfo::remainingChargingTime |
| 285 | \brief The remaining charging time needed for the battery |
| 286 | |
| 287 | This value is measured in seconds. If the battery is full or not charging \c 0 is returned. In |
| 288 | case of an error or if the information is not available \c -1 is returned. |
| 289 | */ |
| 290 | int QBatteryInfo::remainingChargingTime() const |
| 291 | { |
| 292 | return d_ptr->remainingChargingTime(); |
| 293 | } |
| 294 | |
| 295 | /*! |
| 296 | \property QBatteryInfo::voltage |
| 297 | \brief The voltage of the battery |
| 298 | |
| 299 | This value is measured in millivolts (mV). In case of an error or if the information is not |
| 300 | available \c -1 is returned. |
| 301 | */ |
| 302 | int QBatteryInfo::voltage() const |
| 303 | { |
| 304 | return d_ptr->voltage(); |
| 305 | } |
| 306 | |
| 307 | /*! |
| 308 | \property QBatteryInfo::chargerType |
| 309 | \brief The type of the charger |
| 310 | */ |
| 311 | QBatteryInfo::ChargerType QBatteryInfo::chargerType() const |
| 312 | { |
| 313 | return d_ptr->chargerType(); |
| 314 | } |
| 315 | |
| 316 | /*! |
| 317 | \property QBatteryInfo::chargingState |
| 318 | \brief The charging state of the battery |
| 319 | */ |
| 320 | QBatteryInfo::ChargingState QBatteryInfo::chargingState() const |
| 321 | { |
| 322 | return d_ptr->chargingState(); |
| 323 | } |
| 324 | |
| 325 | /*! |
| 326 | \property QBatteryInfo::levelStatus |
| 327 | \brief The level status of the battery |
| 328 | |
| 329 | This represents an Empty/Low/Full style representation of the \l level(). |
| 330 | |
| 331 | \sa maximumCapacity(), remainingCapacity(), level() |
| 332 | */ |
| 333 | QBatteryInfo::LevelStatus QBatteryInfo::levelStatus() const |
| 334 | { |
| 335 | return d_ptr->levelStatus(); |
| 336 | } |
| 337 | |
| 338 | /*! |
| 339 | \property QBatteryInfo::health |
| 340 | \brief The health of the battery |
| 341 | */ |
| 342 | QBatteryInfo::Health QBatteryInfo::health() const |
| 343 | { |
| 344 | return d_ptr->health(); |
| 345 | } |
| 346 | |
| 347 | /*! |
| 348 | \property QBatteryInfo::temperature |
| 349 | \brief The temperature of the battery |
| 350 | |
| 351 | This value is measured in Celsius. In case of an error or if the information is not available, |
| 352 | \c NaN is returned. |
| 353 | |
| 354 | \sa qQNaN() |
| 355 | */ |
| 356 | float QBatteryInfo::temperature() const |
| 357 | { |
| 358 | return d_ptr->temperature(); |
| 359 | } |
| 360 | |
| 361 | /*! |
| 362 | \internal |
| 363 | |
| 364 | Returns the signal that corresponds to \a proxySignal in the |
| 365 | meta-object of the \a sourceObject. |
| 366 | */ |
| 367 | QMetaMethod proxyToSourceSignal(const QMetaMethod &proxySignal, QObject *sourceObject) |
| 368 | { |
| 369 | if (!proxySignal.isValid()) |
| 370 | return proxySignal; |
| 371 | Q_ASSERT(proxySignal.methodType() == QMetaMethod::Signal); |
| 372 | Q_ASSERT(sourceObject != 0); |
| 373 | const QMetaObject *sourceMeta = sourceObject->metaObject(); |
| 374 | int sourceIndex = sourceMeta->indexOfSignal(proxySignal.methodSignature()); |
| 375 | Q_ASSERT(sourceIndex != -1); |
| 376 | return sourceMeta->method(sourceIndex); |
| 377 | } |
| 378 | |
| 379 | /*! |
| 380 | \internal |
| 381 | */ |
| 382 | void QBatteryInfo::connectNotify(const QMetaMethod &signal) |
| 383 | { |
| 384 | #if defined(Q_OS_LINUX) || defined(Q_OS_WIN) || defined(QT_SIMULATOR) || defined(Q_OS_MAC) |
| 385 | QMetaMethod sourceSignal = proxyToSourceSignal(signal, d_ptr); |
| 386 | connect(d_ptr, sourceSignal, this, signal, Qt::UniqueConnection); |
| 387 | #else |
| 388 | Q_UNUSED(signal) |
| 389 | #endif |
| 390 | } |
| 391 | |
| 392 | /*! |
| 393 | \internal |
| 394 | */ |
| 395 | void QBatteryInfo::disconnectNotify(const QMetaMethod &signal) |
| 396 | { |
| 397 | #if defined(Q_OS_LINUX) || defined(Q_OS_WIN) || defined(QT_SIMULATOR) || defined(Q_OS_MAC) |
| 398 | // We can only disconnect with the private implementation, when there is no receivers for the signal. |
| 399 | if (isSignalConnected(signal)) |
| 400 | return; |
| 401 | |
| 402 | QMetaMethod sourceSignal = proxyToSourceSignal(signal, d_ptr); |
| 403 | disconnect(d_ptr, sourceSignal, this, signal); |
| 404 | #else |
| 405 | Q_UNUSED(signal) |
| 406 | #endif |
| 407 | } |
| 408 | |
| 409 | QT_END_NAMESPACE |