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