File indexing completed on 2024-05-12 09:41:25

0001 /*  This file is part of the KDE project
0002     SPDX-FileCopyrightText: 2006 Kevin Ottens <ervin@kde.org>
0003     SPDX-FileCopyrightText: 2008-2010 Dario Freddi <drf@kde.org>
0004     SPDX-FileCopyrightText: 2010 Alejandro Fiestas <alex@eyeos.org>
0005     SPDX-FileCopyrightText: 2010-2013 Lukáš Tinkl <ltinkl@redhat.com>
0006     SPDX-FileCopyrightText: 2015 Kai Uwe Broulik <kde@privat.broulik.de>
0007 
0008     SPDX-License-Identifier: LGPL-2.0-only
0009 
0010 */
0011 
0012 #pragma once
0013 
0014 #include <QDBusObjectPath>
0015 #include <QObject>
0016 
0017 #include "powerdevilcore_export.h"
0018 #include "upower_interface.h"
0019 #include <upowerdevice.h>
0020 
0021 class POWERDEVILCORE_EXPORT BatteryController : public QObject
0022 {
0023     Q_OBJECT
0024 public:
0025     explicit BatteryController();
0026 
0027     /**
0028      * This enum type defines the different states of the AC adapter.
0029      *
0030      * - UnknownAcAdapterState: The AC adapter has an unknown state
0031      * - Plugged: The AC adapter is plugged
0032      * - Unplugged: The AC adapter is unplugged
0033      */
0034     enum AcAdapterState {
0035         UnknownAcAdapterState,
0036         Plugged,
0037         Unplugged,
0038     };
0039     Q_ENUM(AcAdapterState)
0040 
0041     /**
0042      * Retrieves the current state of the system AC adapter.
0043      *
0044      * @return the current AC adapter state
0045      * @see BatteryController::AcAdapterState
0046      */
0047     AcAdapterState acAdapterState() const;
0048 
0049     /**
0050      * Retrieves the current estimated remaining time of the system batteries
0051      *
0052      * @return the current global estimated remaining time in milliseconds
0053      */
0054     qulonglong batteryRemainingTime() const;
0055 
0056     /**
0057      * Retrieves the current estimated remaining time of the system batteries,
0058      * with exponential moving average filter applied to the history records.
0059      *
0060      * @return the current global estimated remaining time in milliseconds
0061      */
0062     qulonglong smoothedBatteryRemainingTime() const;
0063 
0064 Q_SIGNALS:
0065 
0066     /**
0067      * This signal is emitted when the AC adapter is plugged or unplugged.
0068      *
0069      * @param newState the new state of the AC adapter, it's one of the
0070      * type @see PowerDevil::BackendInterface::AcAdapterState
0071      */
0072     void acAdapterStateChanged(BatteryController::AcAdapterState newState);
0073 
0074     /**
0075      * This signal is emitted when the estimated battery remaining time changes.
0076      *
0077      * @param time the new remaining time
0078      */
0079     void batteryRemainingTimeChanged(qulonglong time);
0080 
0081     /**
0082      * This signal is emitted when the estimated battery remaining time changes.
0083      *
0084      * @param time the new remaining time
0085      */
0086     void smoothedBatteryRemainingTimeChanged(qulonglong time);
0087 
0088 private:
0089     void addDevice(const QString &);
0090     void setBatteryRate(const double rate, qulonglong timestamp);
0091 
0092 private Q_SLOTS:
0093     void onPropertiesChanged(const QString &ifaceName, const QVariantMap &changedProps, const QStringList &invalidatedProps);
0094     void updateDeviceProps();
0095     void slotDeviceAdded(const QDBusObjectPath &path);
0096     void slotDeviceRemoved(const QDBusObjectPath &path);
0097 
0098 private:
0099     // upower devices
0100     std::map<QString, std::unique_ptr<UPowerDevice>> m_devices;
0101     std::unique_ptr<UPowerDevice> m_displayDevice = nullptr;
0102 
0103     OrgFreedesktopUPowerInterface *m_upowerInterface;
0104 
0105     bool m_onBattery = false;
0106 
0107     AcAdapterState m_acAdapterState = UnknownAcAdapterState;
0108 
0109     qulonglong m_batteryRemainingTime = 0;
0110     qulonglong m_smoothedBatteryRemainingTime = 0;
0111     qulonglong m_lastRateTimestamp = 0;
0112     double m_batteryEnergyFull = 0;
0113     double m_batteryEnergy = 0;
0114     double m_smoothedBatteryDischargeRate = 0;
0115 };