File indexing completed on 2024-05-12 05:38:48

0001 /*
0002  *  SPDX-FileCopyrightText: 2023 Jakob Petsovits <jpetso@petsovits.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #pragma once
0008 
0009 #include <KCModuleData>
0010 #include <KQuickManagedConfigModule>
0011 
0012 #include <QList>
0013 #include <QVariant>
0014 
0015 class PowerButtonActionModel;
0016 class PowerProfileModel;
0017 class SleepModeModel;
0018 
0019 namespace PowerDevil
0020 {
0021 class ProfileSettings;
0022 class GlobalSettings;
0023 class ExternalServiceSettings;
0024 
0025 class PowerConfigData : public KCModuleData
0026 {
0027     Q_OBJECT
0028 
0029     Q_PROPERTY(QObject *global READ global CONSTANT)
0030     Q_PROPERTY(QObject *profileAC READ profileAC CONSTANT)
0031     Q_PROPERTY(QObject *profileBattery READ profileBattery CONSTANT)
0032     Q_PROPERTY(QObject *profileLowBattery READ profileLowBattery CONSTANT)
0033 
0034 public:
0035     explicit PowerConfigData(QObject *parent, const KPluginMetaData &metaData);
0036     explicit PowerConfigData(QObject *parent, bool isMobile, bool isVM, bool canSuspend, bool canHibernate);
0037     ~PowerConfigData() override;
0038 
0039     GlobalSettings *global() const;
0040     ProfileSettings *profileAC() const;
0041     ProfileSettings *profileBattery() const;
0042     ProfileSettings *profileLowBattery() const;
0043 
0044 private:
0045     GlobalSettings *m_globalSettings;
0046     ProfileSettings *m_settingsAC;
0047     ProfileSettings *m_settingsBattery;
0048     ProfileSettings *m_settingsLowBattery;
0049 };
0050 
0051 class PowerKCM : public KQuickManagedConfigModule
0052 {
0053     Q_OBJECT
0054 
0055     // e.g. {"BrightnessControl": true, "KeyboardBrightnessControl": false, "SuspendSession": true, ...}
0056     Q_PROPERTY(QVariantMap supportedActions READ supportedActions NOTIFY supportedActionsChanged)
0057 
0058     Q_PROPERTY(QObject *settings READ settings CONSTANT)
0059     Q_PROPERTY(QObject *externalServiceSettings READ externalServiceSettings CONSTANT)
0060     Q_PROPERTY(QString currentProfile READ currentProfile NOTIFY currentProfileChanged)
0061     Q_PROPERTY(bool supportsBatteryProfiles READ supportsBatteryProfiles NOTIFY supportsBatteryProfilesChanged)
0062 
0063     Q_PROPERTY(bool isPowerSupplyBatteryPresent READ isPowerSupplyBatteryPresent NOTIFY isPowerSupplyBatteryPresentChanged)
0064     Q_PROPERTY(bool isPeripheralBatteryPresent READ isPeripheralBatteryPresent NOTIFY isPeripheralBatteryPresentChanged)
0065     Q_PROPERTY(bool isLidPresent READ isLidPresent NOTIFY isLidPresentChanged)
0066     Q_PROPERTY(bool isPowerButtonPresent READ isPowerButtonPresent NOTIFY isPowerButtonPresentChanged)
0067 
0068     Q_PROPERTY(bool isChargeStartThresholdSupported READ isChargeStartThresholdSupported NOTIFY isChargeStartThresholdSupportedChanged)
0069     Q_PROPERTY(bool isChargeStopThresholdSupported READ isChargeStopThresholdSupported NOTIFY isChargeStopThresholdSupportedChanged)
0070     Q_PROPERTY(bool chargeStopThresholdMightNeedReconnect READ chargeStopThresholdMightNeedReconnect NOTIFY chargeStopThresholdMightNeedReconnectChanged)
0071 
0072     Q_PROPERTY(bool powerManagementServiceRegistered READ powerManagementServiceRegistered NOTIFY powerManagementServiceRegisteredChanged)
0073     Q_PROPERTY(QString powerManagementServiceErrorReason READ powerManagementServiceErrorReason NOTIFY powerManagementServiceErrorReasonChanged)
0074 
0075     Q_PROPERTY(QObject *autoSuspendActionModel READ autoSuspendActionModel CONSTANT)
0076     Q_PROPERTY(QObject *batteryCriticalActionModel READ batteryCriticalActionModel CONSTANT)
0077     Q_PROPERTY(QObject *powerButtonActionModel READ powerButtonActionModel CONSTANT)
0078     Q_PROPERTY(QObject *lidActionModel READ lidActionModel CONSTANT)
0079     Q_PROPERTY(QObject *sleepModeModel READ sleepModeModel CONSTANT)
0080     Q_PROPERTY(QObject *powerProfileModel READ powerProfileModel CONSTANT)
0081 
0082 public:
0083     PowerKCM(QObject *parent, const KPluginMetaData &metaData);
0084 
0085     bool isSaveNeeded() const override;
0086 
0087     QVariantMap supportedActions() const;
0088 
0089     PowerConfigData *settings() const;
0090     ExternalServiceSettings *externalServiceSettings() const;
0091     QString currentProfile() const;
0092     bool supportsBatteryProfiles() const;
0093 
0094     bool isPowerSupplyBatteryPresent() const;
0095     bool isPeripheralBatteryPresent() const;
0096     bool isLidPresent() const;
0097     bool isPowerButtonPresent() const;
0098 
0099     bool isChargeStartThresholdSupported() const;
0100     bool isChargeStopThresholdSupported() const;
0101     bool chargeStopThresholdMightNeedReconnect() const;
0102 
0103     QObject *autoSuspendActionModel() const;
0104     QObject *batteryCriticalActionModel() const;
0105     QObject *powerButtonActionModel() const;
0106     QObject *lidActionModel() const;
0107     QObject *sleepModeModel() const;
0108     QObject *powerProfileModel() const;
0109 
0110     bool powerManagementServiceRegistered() const;
0111     QString powerManagementServiceErrorReason() const;
0112 
0113 public Q_SLOTS:
0114     void load() override;
0115     void save() override;
0116 
0117 Q_SIGNALS:
0118     void supportedActionsChanged();
0119 
0120     void currentProfileChanged();
0121     void supportsBatteryProfilesChanged();
0122 
0123     void isPowerSupplyBatteryPresentChanged();
0124     void isPeripheralBatteryPresentChanged();
0125     void isLidPresentChanged();
0126     void isPowerButtonPresentChanged();
0127 
0128     void isChargeStartThresholdSupportedChanged();
0129     void isChargeStopThresholdSupportedChanged();
0130     void chargeStopThresholdMightNeedReconnectChanged();
0131 
0132     void powerManagementServiceRegisteredChanged();
0133     void powerManagementServiceErrorReasonChanged();
0134 
0135 private:
0136     void setCurrentProfile(const QString &currentProfile);
0137     void setSupportsBatteryProfiles(bool);
0138 
0139     void setPowerSupplyBatteryPresent(bool);
0140     void setPeripheralBatteryPresent(bool);
0141     void setLidPresent(bool);
0142     void setPowerButtonPresent(bool);
0143 
0144     void setPowerManagementServiceRegistered(bool);
0145     void setPowerManagementServiceErrorReason(const QString &);
0146     void onServiceRegistered(const QString &service);
0147     void onServiceUnregistered(const QString &service);
0148 
0149     QVariantMap m_supportedActions;
0150 
0151     PowerConfigData *m_settings;
0152     ExternalServiceSettings *m_externalServiceSettings;
0153     QString m_currentProfile;
0154     bool m_supportsBatteryProfiles;
0155 
0156     bool m_isPowerSupplyBatteryPresent;
0157     bool m_isPeripheralBatteryPresent;
0158     bool m_isLidPresent;
0159     bool m_isPowerButtonPresent;
0160 
0161     bool m_powerManagementServiceRegistered;
0162     QString m_powerManagementServiceErrorReason;
0163 
0164     PowerButtonActionModel *m_autoSuspendActionModel;
0165     PowerButtonActionModel *m_batteryCriticalActionModel;
0166     PowerButtonActionModel *m_powerButtonActionModel;
0167     PowerButtonActionModel *m_lidActionModel;
0168     SleepModeModel *m_sleepModeModel;
0169     PowerProfileModel *m_powerProfileModel;
0170 };
0171 
0172 } // namespace PowerDevil