33 lines
1.2 KiB
C++
33 lines
1.2 KiB
C++
// SPDX-License-Identifier: MIT
|
|
#include "lighting.h"
|
|
#include <QFile>
|
|
#include <QJsonDocument>
|
|
#include <cmath>
|
|
|
|
QString validatePowerButtonConfig(const QVariantMap &config)
|
|
{
|
|
const auto operation = config.value("operation").toString();
|
|
if (operation == "powerAuto") return {};
|
|
bool valid;
|
|
const double value = config.value("value").toDouble(&valid);
|
|
if (operation == "powerBrightness" && valid && value >= 1 && value <= 100 && std::floor(value) == value)
|
|
return {};
|
|
return "Invalid power-button setting: choose Auto or a whole-number brightness from 1 to 100%.";
|
|
}
|
|
|
|
QVariantMap readPowerButtonConfig(QString &error, const QString &path)
|
|
{
|
|
error.clear();
|
|
QFile file(path);
|
|
if (!file.open(QIODevice::ReadOnly)) { error = file.errorString(); return {}; }
|
|
QJsonParseError parseError;
|
|
const auto document = QJsonDocument::fromJson(file.readAll(), &parseError);
|
|
if (parseError.error != QJsonParseError::NoError || !document.isObject()) {
|
|
error = "Invalid power-button settings file.";
|
|
return {};
|
|
}
|
|
const auto config = document.toVariant().toMap();
|
|
error = validatePowerButtonConfig(config);
|
|
return error.isEmpty() ? config : QVariantMap{};
|
|
}
|