File indexing completed on 2024-05-12 05:36:17
0001 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0002 // SPDX-FileCopyrightText: 2020 Tomaz Canabrava <tcanabrava@kde.org> 0003 0004 #include "mobilepower.h" 0005 #include "statisticsprovider.h" 0006 0007 #include <KConfigGroup> 0008 #include <KLocalizedString> 0009 #include <KPluginFactory> 0010 #include <KSharedConfig> 0011 0012 #include <Solid/Battery> 0013 0014 K_PLUGIN_CLASS_WITH_JSON(MobilePower, "kcm_mobile_power.json") 0015 0016 enum { 0017 THIRTY_SECONDS, 0018 ONE_MINUTE, 0019 TWO_MINUTES, 0020 FIVE_MINUTES, 0021 TEN_MINUTES, 0022 FIFTEEN_MINUTES, 0023 THIRTY_MINUTES, 0024 NEVER, 0025 }; 0026 0027 const QStringList timeValues = { 0028 i18n("30 sec"), 0029 i18n("1 min"), 0030 i18n("2 min"), 0031 i18n("5 min"), 0032 i18n("10 min"), 0033 i18n("15 min"), 0034 i18n("30 min"), 0035 i18n("Never"), 0036 }; 0037 0038 // Maps the indices of the timeValues indexes 0039 // to seconds. 0040 const QMap<int, qreal> idxToSeconds = { 0041 {THIRTY_SECONDS, 30}, 0042 {ONE_MINUTE, 60}, 0043 {TWO_MINUTES, 120}, 0044 {FIVE_MINUTES, 300}, 0045 {TEN_MINUTES, 600}, 0046 {FIFTEEN_MINUTES, 900}, 0047 {THIRTY_MINUTES, 1800}, 0048 {NEVER, 0}, 0049 }; 0050 0051 MobilePower::MobilePower(QObject *parent, const KPluginMetaData &metaData) 0052 : KQuickConfigModule(parent, metaData) 0053 , m_batteries{new BatteryModel(this)} 0054 , m_profilesConfig{KSharedConfig::openConfig("powerdevilrc", KConfig::SimpleConfig | KConfig::CascadeConfig)} 0055 { 0056 qmlRegisterUncreatableType<BatteryModel>("org.kde.kcm.power.mobile.private", 1, 0, "BatteryModel", QStringLiteral("Use BatteryModel")); 0057 qmlRegisterUncreatableType<Solid::Battery>("org.kde.kcm.power.mobile.private", 1, 0, "Battery", ""); 0058 qmlRegisterType<StatisticsProvider>("org.kde.kcm.power.mobile.private", 1, 0, "HistoryModel"); 0059 0060 setButtons(KQuickConfigModule::NoAdditionalButton); 0061 load(); 0062 } 0063 0064 void MobilePower::load() 0065 { 0066 // we assume that the [AC], [Battery], and [LowBattery] groups have the same value 0067 // (which is done by this kcm) 0068 0069 KConfigGroup batteryGroup = m_profilesConfig->group("Battery"); 0070 0071 if (batteryGroup.hasGroup("Display")) { 0072 qDebug() << "[Battery][Display] group is listed"; 0073 KConfigGroup displaySettings = batteryGroup.group("Display"); 0074 m_dimScreenTime = displaySettings.readEntry("DimDisplayIdleTimeoutSec", 30); 0075 m_dimScreen = displaySettings.readEntry("DimDisplayWhenIdle", true); 0076 0077 m_screenOffTime = displaySettings.readEntry("TurnOffDisplayIdleTimeoutSec", 60); 0078 m_screenOff = displaySettings.readEntry("TurnOffDisplayWhenIdle", true); 0079 } else { 0080 qDebug() << "[Battery][Display] Group is not listed"; 0081 m_dimScreenTime = 30; 0082 m_dimScreen = true; 0083 m_screenOffTime = 60; 0084 m_screenOff = true; 0085 } 0086 0087 if (batteryGroup.hasGroup("SuspendAndShutdown")) { 0088 qDebug() << "[Battery][SuspendAndShutdown] group is listed"; 0089 KConfigGroup suspendSessionGroup = batteryGroup.group("SuspendAndShutdown"); 0090 m_suspendSessionTime = suspendSessionGroup.readEntry("AutoSuspendIdleTimeoutSec", 300); 0091 } else { 0092 qDebug() << "[Battery][SuspendAndShutdown] is not listed"; 0093 m_suspendSessionTime = 300; 0094 } 0095 } 0096 0097 void MobilePower::save() 0098 { 0099 // we set all profiles at the same time, since our UI is a simple global toggle 0100 KConfigGroup acGroup = m_profilesConfig->group("AC"); 0101 KConfigGroup batteryGroup = m_profilesConfig->group("Battery"); 0102 KConfigGroup lowBatteryGroup = m_profilesConfig->group("LowBattery"); 0103 0104 acGroup.group("Display").writeEntry("DimDisplayWhenIdle", m_dimScreen, KConfigGroup::Notify); 0105 acGroup.group("Display").writeEntry("DimDisplayIdleTimeoutSec", m_dimScreenTime, KConfigGroup::Notify); 0106 batteryGroup.group("Display").writeEntry("DimDisplayWhenIdle", m_dimScreen, KConfigGroup::Notify); 0107 batteryGroup.group("Display").writeEntry("DimDisplayIdleTimeoutSec", m_dimScreenTime, KConfigGroup::Notify); 0108 lowBatteryGroup.group("Display").writeEntry("DimDisplayWhenIdle", m_dimScreen, KConfigGroup::Notify); 0109 lowBatteryGroup.group("Display").writeEntry("DimDisplayIdleTimeoutSec", m_dimScreenTime, KConfigGroup::Notify); 0110 0111 acGroup.group("Display").writeEntry("TurnOffDisplayWhenIdle", m_screenOff, KConfigGroup::Notify); 0112 acGroup.group("Display").writeEntry("TurnOffDisplayIdleTimeoutSec", m_screenOffTime, KConfigGroup::Notify); 0113 batteryGroup.group("Display").writeEntry("TurnOffDisplayWhenIdle", m_screenOff, KConfigGroup::Notify); 0114 batteryGroup.group("Display").writeEntry("TurnOffDisplayIdleTimeoutSec", m_screenOffTime, KConfigGroup::Notify); 0115 lowBatteryGroup.group("Display").writeEntry("TurnOffDisplayWhenIdle", m_screenOff, KConfigGroup::Notify); 0116 lowBatteryGroup.group("Display").writeEntry("TurnOffDisplayIdleTimeoutSec", m_screenOffTime, KConfigGroup::Notify); 0117 0118 acGroup.group("SuspendAndShutdown").writeEntry("AutoSuspendIdleTimeoutSec", m_suspendSessionTime, KConfigGroup::Notify); 0119 batteryGroup.group("SuspendAndShutdown").writeEntry("AutoSuspendIdleTimeoutSec", m_suspendSessionTime, KConfigGroup::Notify); 0120 lowBatteryGroup.group("SuspendAndShutdown").writeEntry("AutoSuspendIdleTimeoutSec", m_suspendSessionTime, KConfigGroup::Notify); 0121 0122 m_profilesConfig->sync(); 0123 } 0124 0125 QStringList MobilePower::timeOptions() const 0126 { 0127 return timeValues; 0128 } 0129 0130 void MobilePower::setDimScreenIdx(int idx) 0131 { 0132 qreal value = idxToSeconds.value(idx); 0133 qDebug() << "Got the value" << value; 0134 0135 if (m_dimScreenTime == value) { 0136 return; 0137 } 0138 0139 if (value == 0) { 0140 qDebug() << "Setting to never dim"; 0141 m_dimScreen = false; 0142 } else { 0143 qDebug() << "Setting to dim in " << value << "Minutes"; 0144 m_dimScreen = true; 0145 } 0146 0147 m_dimScreenTime = value; 0148 Q_EMIT dimScreenIdxChanged(); 0149 save(); 0150 } 0151 0152 void MobilePower::setScreenOffIdx(int idx) 0153 { 0154 qreal value = idxToSeconds.value(idx); 0155 qDebug() << "Got the value" << value; 0156 0157 if (m_screenOffTime == value) { 0158 return; 0159 } 0160 0161 if (value == 0) { 0162 qDebug() << "Setting to never screen off"; 0163 m_screenOff = false; 0164 } else { 0165 qDebug() << "Setting to screen off in " << value << "Minutes"; 0166 m_screenOff = true; 0167 } 0168 m_screenOffTime = value; 0169 0170 Q_EMIT screenOffIdxChanged(); 0171 save(); 0172 } 0173 0174 void MobilePower::setSuspendSessionIdx(int idx) 0175 { 0176 qreal value = idxToSeconds.value(idx); 0177 qDebug() << "Got the value" << value; 0178 0179 if (m_suspendSessionTime == value) { 0180 return; 0181 } 0182 0183 if (value == 0) { 0184 qDebug() << "Setting to never suspend"; 0185 } else { 0186 qDebug() << "Setting to suspend in " << value << "Minutes"; 0187 } 0188 0189 m_suspendSessionTime = value; 0190 Q_EMIT suspendSessionIdxChanged(); 0191 save(); 0192 } 0193 0194 int MobilePower::suspendSessionIdx() 0195 { 0196 if (m_suspendSessionTime == 0) { 0197 return NEVER; 0198 } 0199 0200 return idxToSeconds.key(std::round(m_suspendSessionTime)); 0201 } 0202 0203 int MobilePower::dimScreenIdx() 0204 { 0205 if (!m_dimScreen) { 0206 return NEVER; 0207 } 0208 0209 return idxToSeconds.key(std::round(m_dimScreenTime)); 0210 } 0211 0212 int MobilePower::screenOffIdx() 0213 { 0214 if (!m_screenOff) { 0215 return NEVER; 0216 } 0217 0218 return idxToSeconds.key(std::round(m_screenOffTime)); 0219 } 0220 0221 BatteryModel *MobilePower::batteries() 0222 { 0223 return m_batteries; 0224 } 0225 0226 #include "mobilepower.moc"