kcms/powermanagement: Update to new powerdevil config

Fixes https://invent.kde.org/plasma/plasma-mobile/-/issues/295
This commit is contained in:
Devin Lin 2024-01-02 14:17:41 -05:00
parent 1dbd1505dc
commit b895e8a203
2 changed files with 63 additions and 118 deletions

View file

@ -20,6 +20,7 @@ enum {
FIVE_MINUTES, FIVE_MINUTES,
TEN_MINUTES, TEN_MINUTES,
FIFTEEN_MINUTES, FIFTEEN_MINUTES,
THIRTY_MINUTES,
NEVER, NEVER,
}; };
@ -30,25 +31,27 @@ const QStringList timeValues = {
i18n("5 min"), i18n("5 min"),
i18n("10 min"), i18n("10 min"),
i18n("15 min"), i18n("15 min"),
i18n("30 min"),
i18n("Never"), i18n("Never"),
}; };
// Maps the indices of the timeValues indexes // Maps the indices of the timeValues indexes
// to minutes. // to seconds.
const QMap<int, qreal> idxToMinutes = { const QMap<int, qreal> idxToSeconds = {
{THIRTY_SECONDS, 0.5}, {THIRTY_SECONDS, 30},
{ONE_MINUTE, 1}, {ONE_MINUTE, 60},
{TWO_MINUTES, 2}, {TWO_MINUTES, 120},
{FIVE_MINUTES, 5}, {FIVE_MINUTES, 300},
{TEN_MINUTES, 10}, {TEN_MINUTES, 600},
{FIFTEEN_MINUTES, 15}, {FIFTEEN_MINUTES, 900},
{THIRTY_MINUTES, 1800},
{NEVER, 0}, {NEVER, 0},
}; };
MobilePower::MobilePower(QObject *parent, const KPluginMetaData &metaData) MobilePower::MobilePower(QObject *parent, const KPluginMetaData &metaData)
: KQuickConfigModule(parent, metaData) : KQuickConfigModule(parent, metaData)
, m_batteries{new BatteryModel(this)} , m_batteries{new BatteryModel(this)}
, m_profilesConfig{KSharedConfig::openConfig("powermanagementprofilesrc", KConfig::SimpleConfig | KConfig::CascadeConfig)} , m_profilesConfig{KSharedConfig::openConfig("powerdevilrc", KConfig::SimpleConfig | KConfig::CascadeConfig)}
{ {
qmlRegisterUncreatableType<BatteryModel>("org.kde.kcm.power.mobile.private", 1, 0, "BatteryModel", QStringLiteral("Use BatteryModel")); qmlRegisterUncreatableType<BatteryModel>("org.kde.kcm.power.mobile.private", 1, 0, "BatteryModel", QStringLiteral("Use BatteryModel"));
qmlRegisterUncreatableType<Solid::Battery>("org.kde.kcm.power.mobile.private", 1, 0, "Battery", ""); qmlRegisterUncreatableType<Solid::Battery>("org.kde.kcm.power.mobile.private", 1, 0, "Battery", "");
@ -58,21 +61,6 @@ MobilePower::MobilePower(QObject *parent, const KPluginMetaData &metaData)
load(); load();
} }
// contents of powermanagementprofilesrc
//
// [Battery][SuspendSession] // our LockScreen
// idleTime=600000
// suspendThenHibernate = enabled / disabled.
// suspendType=1
// type 1 = sleep
// type 8 = shutdown
// type 32 = lock screen
//
// [Battery][DimDisplay] // our "Sleep Screen"
// idleTime=300000
// Aparently KDE removes this group when it's false.
//
void MobilePower::load() void MobilePower::load()
{ {
// we assume that the [AC], [Battery], and [LowBattery] groups have the same value // we assume that the [AC], [Battery], and [LowBattery] groups have the same value
@ -80,95 +68,58 @@ void MobilePower::load()
KConfigGroup batteryGroup = m_profilesConfig->group("Battery"); KConfigGroup batteryGroup = m_profilesConfig->group("Battery");
if (batteryGroup.hasGroup("DimDisplay")) { if (batteryGroup.hasGroup("Display")) {
qDebug() << "[Battery][DimDisplay] group is listed"; qDebug() << "[Battery][Display] group is listed";
KConfigGroup dimSettings = batteryGroup.group("DimDisplay"); KConfigGroup displaySettings = batteryGroup.group("Display");
m_dimScreenTime = displaySettings.readEntry("DimDisplayIdleTimeoutSec", 30);
m_dimScreen = displaySettings.readEntry("DimDisplayWhenIdle", true);
// powerdevil/dimdisplayconfig.cpp - here we load time / 60 / 1000 m_screenOffTime = displaySettings.readEntry("TurnOffDisplayIdleTimeoutSec", 60);
// We should really, really, stop doing that. m_screenOff = displaySettings.readEntry("TurnOffDisplayWhenIdle", true);
m_dimScreenTime = (dimSettings.readEntry("idleTime").toDouble() / 60) / 1000;
} else { } else {
qDebug() << "[Battery][DimDisplay] Group is not listed"; qDebug() << "[Battery][Display] Group is not listed";
m_dimScreenTime = 0; m_dimScreenTime = 30;
m_dimScreen = true;
m_screenOffTime = 60;
m_screenOff = true;
} }
if (batteryGroup.hasGroup("DPMSControl")) { if (batteryGroup.hasGroup("SuspendAndShutdown")) {
qDebug() << "[Battery][DPMSControl] group is listed"; qDebug() << "[Battery][SuspendAndShutdown] group is listed";
KConfigGroup dpmsSettings = batteryGroup.group("DPMSControl"); KConfigGroup suspendSessionGroup = batteryGroup.group("SuspendAndShutdown");
m_screenOffTime = dpmsSettings.readEntry("idleTime").toDouble() / 60 / 1000; m_suspendSessionTime = suspendSessionGroup.readEntry("AutoSuspendIdleTimeoutSec", 300);
} else { } else {
qDebug() << "[Battery][DPMSControl] is not listed"; qDebug() << "[Battery][SuspendAndShutdown] is not listed";
m_screenOffTime = 0; m_suspendSessionTime = 300;
}
if (batteryGroup.hasGroup("SuspendSession")) {
qDebug() << "[Battery][SuspendSession] group is listed";
KConfigGroup suspendSessionGroup = batteryGroup.group("SuspendSession");
m_suspendSessionTime = suspendSessionGroup.readEntry("idleTime").toDouble() / 60 / 1000;
} else {
qDebug() << "[Battery][SuspendSession] is not listed";
m_suspendSessionTime = 0;
} }
} }
void MobilePower::save() void MobilePower::save()
{ {
// we set all profiles at the same time, since our UI is quite a simple global toggle // we set all profiles at the same time, since our UI is a simple global toggle
KConfigGroup acGroup = m_profilesConfig->group("AC"); KConfigGroup acGroup = m_profilesConfig->group("AC");
KConfigGroup batteryGroup = m_profilesConfig->group("Battery"); KConfigGroup batteryGroup = m_profilesConfig->group("Battery");
KConfigGroup lowBatteryGroup = m_profilesConfig->group("LowBattery"); KConfigGroup lowBatteryGroup = m_profilesConfig->group("LowBattery");
if (m_dimScreenTime == 0) { acGroup.group("Display").writeEntry("DimDisplayWhenIdle", m_dimScreen, KConfigGroup::Notify);
qDebug() << "Deleting the group DimDisplay"; acGroup.group("Display").writeEntry("DimDisplayIdleTimeoutSec", m_dimScreenTime, KConfigGroup::Notify);
batteryGroup.group("Display").writeEntry("DimDisplayWhenIdle", m_dimScreen, KConfigGroup::Notify);
batteryGroup.group("Display").writeEntry("DimDisplayIdleTimeoutSec", m_dimScreenTime, KConfigGroup::Notify);
lowBatteryGroup.group("Display").writeEntry("DimDisplayWhenIdle", m_dimScreen, KConfigGroup::Notify);
lowBatteryGroup.group("Display").writeEntry("DimDisplayIdleTimeoutSec", m_dimScreenTime, KConfigGroup::Notify);
acGroup.deleteGroup("DimDisplay", KConfigGroup::Notify); acGroup.group("Display").writeEntry("TurnOffDisplayWhenIdle", m_screenOff, KConfigGroup::Notify);
batteryGroup.deleteGroup("DimDisplay", KConfigGroup::Notify); acGroup.group("Display").writeEntry("TurnOffDisplayIdleTimeoutSec", m_screenOffTime, KConfigGroup::Notify);
lowBatteryGroup.deleteGroup("DimDisplay", KConfigGroup::Notify); batteryGroup.group("Display").writeEntry("TurnOffDisplayWhenIdle", m_screenOff, KConfigGroup::Notify);
} else { batteryGroup.group("Display").writeEntry("TurnOffDisplayIdleTimeoutSec", m_screenOffTime, KConfigGroup::Notify);
// powerdevil/dimdisplayconfig.cpp - here we store time * 60 * 1000 lowBatteryGroup.group("Display").writeEntry("TurnOffDisplayWhenIdle", m_screenOff, KConfigGroup::Notify);
// We should really, really, stop doing that. lowBatteryGroup.group("Display").writeEntry("TurnOffDisplayIdleTimeoutSec", m_screenOffTime, KConfigGroup::Notify);
acGroup.group("DimDisplay").writeEntry("idleTime", m_dimScreenTime * 60 * 1000, KConfigGroup::Notify);
batteryGroup.group("DimDisplay").writeEntry("idleTime", m_dimScreenTime * 60 * 1000, KConfigGroup::Notify);
lowBatteryGroup.group("DimDisplay").writeEntry("idleTime", m_dimScreenTime * 60 * 1000, KConfigGroup::Notify);
}
if (m_screenOffTime == 0) { acGroup.group("SuspendAndShutdown").writeEntry("AutoSuspendIdleTimeoutSec", m_suspendSessionTime, KConfigGroup::Notify);
qDebug() << "Deleting the group DPMSControl"; batteryGroup.group("SuspendAndShutdown").writeEntry("AutoSuspendIdleTimeoutSec", m_suspendSessionTime, KConfigGroup::Notify);
lowBatteryGroup.group("SuspendAndShutdown").writeEntry("AutoSuspendIdleTimeoutSec", m_suspendSessionTime, KConfigGroup::Notify);
acGroup.deleteGroup("DPMSControl", KConfigGroup::Notify);
batteryGroup.deleteGroup("DPMSControl", KConfigGroup::Notify);
lowBatteryGroup.deleteGroup("DPMSControl", KConfigGroup::Notify);
} else {
acGroup.group("DPMSControl").writeEntry("idleTime", m_screenOffTime * 60 * 1000, KConfigGroup::Notify);
batteryGroup.group("DPMSControl").writeEntry("idleTime", m_screenOffTime * 60 * 1000, KConfigGroup::Notify);
lowBatteryGroup.group("DPMSControl").writeEntry("idleTime", m_screenOffTime * 60 * 1000, KConfigGroup::Notify);
}
// ensure the system is locked when the screen is turned off
acGroup.group("DPMSControl").writeEntry("lockBeforeTurnOff", 1, KConfigGroup::Notify);
batteryGroup.group("DPMSControl").writeEntry("lockBeforeTurnOff", 1, KConfigGroup::Notify);
lowBatteryGroup.group("DPMSControl").writeEntry("lockBeforeTurnOff", 1, KConfigGroup::Notify);
if (m_suspendSessionTime == 0) {
qDebug() << "Deleting the group SuspendDisplay";
acGroup.deleteGroup("SuspendSession", KConfigGroup::Notify);
batteryGroup.deleteGroup("SuspendSession", KConfigGroup::Notify);
lowBatteryGroup.deleteGroup("SuspendSession", KConfigGroup::Notify);
} else {
acGroup.group("SuspendSession").writeEntry("idleTime", m_suspendSessionTime * 60 * 1000, KConfigGroup::Notify);
acGroup.group("SuspendSession").writeEntry("suspendType", 1, KConfigGroup::Notify);
batteryGroup.group("SuspendSession").writeEntry("idleTime", m_suspendSessionTime * 60 * 1000, KConfigGroup::Notify);
batteryGroup.group("SuspendSession").writeEntry("suspendType", 1, KConfigGroup::Notify);
lowBatteryGroup.group("SuspendSession").writeEntry("idleTime", m_suspendSessionTime * 60 * 1000, KConfigGroup::Notify);
lowBatteryGroup.group("SuspendSession").writeEntry("suspendType", 1, KConfigGroup::Notify);
}
m_profilesConfig->sync(); m_profilesConfig->sync();
// Do not mess with Suspend Type
// suspendSessionGroup.writeEntry("suspendType", 32); // always lock screen.
} }
QStringList MobilePower::timeOptions() const QStringList MobilePower::timeOptions() const
@ -178,7 +129,7 @@ QStringList MobilePower::timeOptions() const
void MobilePower::setDimScreenIdx(int idx) void MobilePower::setDimScreenIdx(int idx)
{ {
qreal value = idxToMinutes.value(idx); qreal value = idxToSeconds.value(idx);
qDebug() << "Got the value" << value; qDebug() << "Got the value" << value;
if (m_dimScreenTime == value) { if (m_dimScreenTime == value) {
@ -187,8 +138,10 @@ void MobilePower::setDimScreenIdx(int idx)
if (value == 0) { if (value == 0) {
qDebug() << "Setting to never dim"; qDebug() << "Setting to never dim";
m_dimScreen = false;
} else { } else {
qDebug() << "Setting to dim in " << value << "Minutes"; qDebug() << "Setting to dim in " << value << "Minutes";
m_dimScreen = true;
} }
m_dimScreenTime = value; m_dimScreenTime = value;
@ -198,7 +151,7 @@ void MobilePower::setDimScreenIdx(int idx)
void MobilePower::setScreenOffIdx(int idx) void MobilePower::setScreenOffIdx(int idx)
{ {
qreal value = idxToMinutes.value(idx); qreal value = idxToSeconds.value(idx);
qDebug() << "Got the value" << value; qDebug() << "Got the value" << value;
if (m_screenOffTime == value) { if (m_screenOffTime == value) {
@ -207,8 +160,10 @@ void MobilePower::setScreenOffIdx(int idx)
if (value == 0) { if (value == 0) {
qDebug() << "Setting to never screen off"; qDebug() << "Setting to never screen off";
m_screenOff = false;
} else { } else {
qDebug() << "Setting to screen off in " << value << "Minutes"; qDebug() << "Setting to screen off in " << value << "Minutes";
m_screenOff = true;
} }
m_screenOffTime = value; m_screenOffTime = value;
@ -218,7 +173,7 @@ void MobilePower::setScreenOffIdx(int idx)
void MobilePower::setSuspendSessionIdx(int idx) void MobilePower::setSuspendSessionIdx(int idx)
{ {
qreal value = idxToMinutes.value(idx); qreal value = idxToSeconds.value(idx);
qDebug() << "Got the value" << value; qDebug() << "Got the value" << value;
if (m_suspendSessionTime == value) { if (m_suspendSessionTime == value) {
@ -240,39 +195,27 @@ int MobilePower::suspendSessionIdx()
{ {
if (m_suspendSessionTime == 0) { if (m_suspendSessionTime == 0) {
return NEVER; return NEVER;
} else if (qFuzzyIsNull(m_suspendSessionTime)) {
return NEVER;
} else if (qFuzzyCompare(m_suspendSessionTime, 0.5)) {
return THIRTY_SECONDS;
} }
return idxToMinutes.key(std::round(m_suspendSessionTime)); return idxToSeconds.key(std::round(m_suspendSessionTime));
} }
int MobilePower::dimScreenIdx() int MobilePower::dimScreenIdx()
{ {
if (m_dimScreenTime == 0) { if (!m_dimScreen) {
return NEVER; return NEVER;
} else if (qFuzzyIsNull(m_dimScreenTime)) {
return NEVER;
} else if (qFuzzyCompare(m_dimScreenTime, 0.5)) {
return THIRTY_SECONDS;
} }
return idxToMinutes.key(std::round(m_dimScreenTime)); return idxToSeconds.key(std::round(m_dimScreenTime));
} }
int MobilePower::screenOffIdx() int MobilePower::screenOffIdx()
{ {
if (m_screenOffTime == 0) { if (!m_screenOff) {
return NEVER; return NEVER;
} else if (qFuzzyIsNull(m_screenOffTime)) {
return NEVER;
} else if (qFuzzyCompare(m_screenOffTime, 0.5)) {
return THIRTY_SECONDS;
} }
return idxToMinutes.key(std::round(m_screenOffTime)); return idxToSeconds.key(std::round(m_screenOffTime));
} }
BatteryModel *MobilePower::batteries() BatteryModel *MobilePower::batteries()

View file

@ -44,7 +44,9 @@ private:
BatteryModel *m_batteries; BatteryModel *m_batteries;
KSharedConfig::Ptr m_profilesConfig; KSharedConfig::Ptr m_profilesConfig;
qreal m_suspendSessionTime; int m_suspendSessionTime;
qreal m_dimScreenTime; int m_dimScreenTime;
qreal m_screenOffTime; bool m_dimScreen;
int m_screenOffTime;
bool m_screenOff;
}; };