Commit 29154c13 authored by Méven Car's avatar Méven Car Committed by Nate Graham
Browse files

Support hardware with only one charging threshold, not both

getthreshold will return -1 for unsupported thresholds.

setthreshold will tollerate receving no value or -1 to ignore setting a
threshold.

BUG: 449997
FIXED-IN: 5.24.1


(cherry picked from commit f7b1f2f1)
parent 2c6f585f
Pipeline #137423 passed with stage
in 1 minute and 21 seconds
......@@ -54,7 +54,6 @@ static QVector<int> getThresholds(const QString &which)
for (const QString &battery : batteries) {
QFile file(s_powerSupplySysFsPath + QLatin1Char('/') + battery + QLatin1Char('/') + which);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "Failed to open" << file.fileName() << "for reading";
continue;
}
......@@ -101,20 +100,26 @@ ActionReply ChargeThresholdHelper::getthreshold(const QVariantMap &args)
auto startThresholds = getThresholds(s_chargeStartThreshold);
auto stopThresholds = getThresholds(s_chargeEndThreshold);
if (startThresholds.isEmpty() || stopThresholds.isEmpty()) {
if (startThresholds.isEmpty() && stopThresholds.isEmpty()) {
startThresholds = getThresholds(s_oldChargeStartThreshold);
stopThresholds = getThresholds(s_oldChargeStopThreshold);
}
if (startThresholds.isEmpty() || stopThresholds.isEmpty() || startThresholds.count() != stopThresholds.count()) {
if (startThresholds.isEmpty() && stopThresholds.isEmpty()) {
auto reply = ActionReply::HelperErrorReply();
reply.setErrorDescription(QStringLiteral("Charge thresholds not supported"));
return reply;
}
if (!startThresholds.isEmpty() && !stopThresholds.isEmpty() && startThresholds.count() != stopThresholds.count()) {
auto reply = ActionReply::HelperErrorReply();
reply.setErrorDescription(QStringLiteral("Charge thresholds not supported"));
return reply;
}
// In the rare case there are multiple batteries with varying charge thresholds, try to use something sensible
const int startThreshold = *std::max_element(startThresholds.begin(), startThresholds.end());
const int stopThreshold = *std::min_element(stopThresholds.begin(), stopThresholds.end());
const int startThreshold = !startThresholds.empty() ? *std::max_element(startThresholds.begin(), startThresholds.end()) : -1;
const int stopThreshold = !stopThresholds.empty() ? *std::min_element(stopThresholds.begin(), stopThresholds.end()) : -1;
ActionReply reply;
reply.setData({
......@@ -126,26 +131,30 @@ ActionReply ChargeThresholdHelper::getthreshold(const QVariantMap &args)
ActionReply ChargeThresholdHelper::setthreshold(const QVariantMap &args)
{
const int startThreshold = args.value(QStringLiteral("chargeStartThreshold")).toInt();
const int stopThreshold = args.value(QStringLiteral("chargeStopThreshold")).toInt();
if (startThreshold < 0
|| startThreshold > 100
|| stopThreshold < 0
|| stopThreshold > 100
|| startThreshold > stopThreshold) {
bool hasStartThreshold;
const int startThreshold = args.value(QStringLiteral("chargeStartThreshold"), -1).toInt(&hasStartThreshold);
hasStartThreshold &= startThreshold != -1;
bool hasStopThreshold;
const int stopThreshold = args.value(QStringLiteral("chargeStopThreshold"), -1).toInt(&hasStopThreshold);
hasStopThreshold &= stopThreshold != -1;
if ((hasStartThreshold && (startThreshold < 0|| startThreshold > 100))
|| (hasStopThreshold && (stopThreshold < 0 || stopThreshold > 100))
|| (hasStartThreshold && hasStopThreshold && startThreshold > stopThreshold)
|| (!hasStartThreshold && !hasStopThreshold)) {
auto reply = ActionReply::HelperErrorReply(); // is there an "invalid arguments" error?
reply.setErrorDescription(QStringLiteral("Invalid thresholds provided"));
return reply;
}
if (!(setThresholds(s_chargeStartThreshold, startThreshold) || setThresholds(s_oldChargeStartThreshold, startThreshold))) {
if (hasStartThreshold && !(setThresholds(s_chargeStartThreshold, startThreshold) || setThresholds(s_oldChargeStartThreshold, startThreshold))) {
auto reply = ActionReply::HelperErrorReply();
reply.setErrorDescription(QStringLiteral("Failed to write start charge threshold"));
return reply;
}
if (!(setThresholds(s_chargeEndThreshold, stopThreshold) || setThresholds(s_oldChargeStopThreshold, stopThreshold))) {
if (hasStopThreshold && !(setThresholds(s_chargeEndThreshold, stopThreshold) || setThresholds(s_oldChargeStopThreshold, stopThreshold))) {
auto reply = ActionReply::HelperErrorReply();
reply.setErrorDescription(QStringLiteral("Failed to write stop charge threshold"));
return reply;
......
......@@ -964,13 +964,13 @@ void Core::readChargeThreshold()
const auto data = job->data();
const int chargeStartThreshold = data.value(QStringLiteral("chargeStartThreshold")).toInt();
if (m_chargeStartThreshold != chargeStartThreshold) {
if (chargeStartThreshold != -1 && m_chargeStartThreshold != chargeStartThreshold) {
m_chargeStartThreshold = chargeStartThreshold;
Q_EMIT chargeStartThresholdChanged(chargeStartThreshold);
}
const int chargeStopThreshold = data.value(QStringLiteral("chargeStopThreshold")).toInt();
if (m_chargeStopThreshold != chargeStopThreshold) {
if (chargeStopThreshold != -1 && m_chargeStopThreshold != chargeStopThreshold) {
m_chargeStopThreshold = chargeStopThreshold;
Q_EMIT chargeStopThresholdChanged(chargeStopThreshold);
}
......
......@@ -200,19 +200,19 @@ void GeneralPage::save()
PowerDevilSettings::self()->save();
if (chargeStartThresholdSpin->value() != m_chargeStartThreshold
|| chargeStopThresholdSpin->value() != m_chargeStopThreshold) {
if ((m_chargeStartThreshold != -1 && chargeStartThresholdSpin->value() != m_chargeStartThreshold) || (
m_chargeStopThreshold != -1 && chargeStopThresholdSpin->value() != m_chargeStopThreshold)) {
KAuth::Action action(QStringLiteral("org.kde.powerdevil.chargethresholdhelper.setthreshold"));
action.setHelperId(QStringLiteral("org.kde.powerdevil.chargethresholdhelper"));
action.setArguments({
{QStringLiteral("chargeStartThreshold"), chargeStartThresholdSpin->value()},
{QStringLiteral("chargeStopThreshold"), chargeStopThresholdSpin->value()}
{QStringLiteral("chargeStartThreshold"), m_chargeStartThreshold != -1 ? chargeStartThresholdSpin->value() : -1},
{QStringLiteral("chargeStopThreshold"), m_chargeStopThreshold != -1 ? chargeStopThresholdSpin->value() : -1}
});
KAuth::ExecuteJob *job = action.execute();
job->exec();
if (!job->error()) {
m_chargeStartThreshold = chargeStartThresholdSpin->value();
m_chargeStopThreshold = chargeStopThresholdSpin->value();
m_chargeStartThreshold = m_chargeStartThreshold != -1 ? chargeStartThresholdSpin->value() : -1;
m_chargeStopThreshold = m_chargeStopThreshold != -1 ? chargeStopThresholdSpin->value() : -1;
}
}
......@@ -237,10 +237,11 @@ void GeneralPage::setChargeThresholdSupported(bool supported)
batteryThresholdLabel->setVisible(supported);
batteryThresholdExplanation->setVisible(supported);
chargeStartThresholdLabel->setVisible(supported);
chargeStartThresholdSpin->setVisible(supported);
chargeStopThresholdLabel->setVisible(supported);
chargeStopThresholdSpin->setVisible(supported);
chargeStartThresholdLabel->setVisible(supported && m_chargeStartThreshold != -1);
chargeStartThresholdSpin->setVisible(supported && m_chargeStartThreshold != -1);
chargeStopThresholdLabel->setVisible(supported && m_chargeStopThreshold != -1);
chargeStopThresholdSpin->setVisible(supported && m_chargeStopThreshold != -1);
}
void GeneralPage::onServiceRegistered(const QString& service)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment