Use const QStringList for storing filenames

This commit is contained in:
Matti Viljanen 2023-03-04 14:42:15 +02:00
parent b1384a6abb
commit fdc373cb83
No known key found for this signature in database
GPG key ID: CF32A1495158F888
2 changed files with 86 additions and 65 deletions

View file

@ -26,9 +26,12 @@ Battery::Battery(Settings* newSettings, Logger* newLogger, QObject* parent) : QO
QStringList filenames;
// Battery charge percentage, number, e.g. 42
filenames << "/sys/class/power_supply/battery/capacity"
<< "/sys/class/power_supply/dollar_cove_battery/capacity";
foreach(const QString& file, filenames) {
const QStringList batteryFiles = {
"/sys/class/power_supply/battery/capacity",
"/sys/class/power_supply/dollar_cove_battery/capacity"
};
foreach(const QString& file, batteryFiles) {
if(!chargeFile && QFile::exists(file)) {
chargeFile = new QFile(file, this);
break;
@ -38,11 +41,12 @@ Battery::Battery(Settings* newSettings, Logger* newLogger, QObject* parent) : QO
logL("Battery charge file: " + (chargeFile ? chargeFile->fileName() : notFound));
// Number: battery/charging current, e.g. -1450000 (-145mA)
filenames.clear();
filenames << "/sys/class/power_supply/battery/current_now"
<< "/sys/class/power_supply/dollar_cove_battery/current_now";
const QStringList currentFiles = {
"/sys/class/power_supply/battery/current_now",
"/sys/class/power_supply/dollar_cove_battery/current_now"
};
foreach(const QString& file, filenames) {
foreach(const QString& file, currentFiles) {
if(!currentFile && QFile::exists(file)) {
currentFile = new QFile(file, this);
break;
@ -52,11 +56,12 @@ Battery::Battery(Settings* newSettings, Logger* newLogger, QObject* parent) : QO
logL("Charging/discharging current file: " + (currentFile ? currentFile->fileName() : notFound));
// String: charging, discharging, full, empty, unknown (others?)
filenames.clear();
filenames << "/sys/class/power_supply/battery/status"
<< "/sys/class/power_supply/dollar_cove_battery/status";
const QStringList stateFiles = {
"/sys/class/power_supply/battery/status",
"/sys/class/power_supply/dollar_cove_battery/status"
};
foreach(const QString& file, filenames) {
foreach(const QString& file, stateFiles) {
if(!stateFile && QFile::exists(file)) {
stateFile = new QFile(file, this);
break;
@ -66,11 +71,12 @@ Battery::Battery(Settings* newSettings, Logger* newLogger, QObject* parent) : QO
logL("Status file: " + (stateFile ? stateFile->fileName() : notFound));
// Number: 0 or 1
filenames.clear();
filenames << "/sys/class/power_supply/usb/present"
<< "/sys/class/power_supply/dollar_cove_charger/present";
const QStringList usbPresentFiles = {
"/sys/class/power_supply/usb/present",
"/sys/class/power_supply/dollar_cove_charger/present"
};
foreach(const QString& file, filenames) {
foreach(const QString& file, usbPresentFiles) {
if(!chargerConnectedFile && QFile::exists(file)) {
chargerConnectedFile = new QFile(file, this);
break;
@ -80,10 +86,11 @@ Battery::Battery(Settings* newSettings, Logger* newLogger, QObject* parent) : QO
logL("Charger status file: " + (chargerConnectedFile ? chargerConnectedFile->fileName() : notFound));
// Number: 0 or 1
filenames.clear();
filenames << "/sys/class/power_supply/ac/present";
const QStringList acPresentFiles = {
"/sys/class/power_supply/ac/present"
};
foreach(const QString& file, filenames) {
foreach(const QString& file, acPresentFiles) {
if(!acConnectedFile && QFile::exists(file)) {
acConnectedFile = new QFile(file, this);
break;
@ -93,11 +100,12 @@ Battery::Battery(Settings* newSettings, Logger* newLogger, QObject* parent) : QO
logL("AC status file: " + (acConnectedFile ? acConnectedFile->fileName() : notFound));
// Number: temperature
filenames.clear();
filenames << "/sys/class/power_supply/battery/temp"
<< "/sys/class/power_supply/dollar_cove_battery/temp";
const QStringList tempFiles = {
"/sys/class/power_supply/battery/temp",
"/sys/class/power_supply/dollar_cove_battery/temp"
};
foreach(const QString& file, filenames) {
foreach(const QString& file, tempFiles) {
if(!temperatureFile && QFile::exists(file)) {
temperatureFile = new QFile(file, this);
break;
@ -107,9 +115,10 @@ Battery::Battery(Settings* newSettings, Logger* newLogger, QObject* parent) : QO
logL("Battery temperature file: " + (temperatureFile ? temperatureFile->fileName() : notFound));
// String: health state
filenames.clear();
filenames << "/sys/class/power_supply/battery/health"
<< "/sys/class/power_supply/dollar_cove_battery/health";
const QStringList healthFiles = {
"/sys/class/power_supply/battery/health",
"/sys/class/power_supply/dollar_cove_battery/health"
};
foreach(const QString& file, filenames) {
if(!healthFile && QFile::exists(file)) {
healthFile = new QFile(file, this);
@ -120,13 +129,14 @@ Battery::Battery(Settings* newSettings, Logger* newLogger, QObject* parent) : QO
logL("Battery health file: " + (healthFile ? healthFile->fileName() : notFound));
// Charger control file
filenames.clear();
filenames << "/sys/class/power_supply/battery/input_suspend" // e.g. Sony Xperia XA2
<< "/sys/class/power_supply/battery/charging_enabled" // e.g. for Sony Xperia Z3 Compact Tablet
<< "/sys/class/power_supply/usb/charger_disable" // e.g. for Jolla Phone
<< "/sys/class/power_supply/dollar_cove_battery/enable_charging"; // e.g. for Jolla Tablet
const QStringList controlFiles = {
"/sys/class/power_supply/battery/input_suspend", // e.g. Sony Xperia XA2
"/sys/class/power_supply/battery/charging_enabled", // e.g. for Sony Xperia Z3 Compact Tablet
"/sys/class/power_supply/usb/charger_disable", // e.g. for Jolla Phone
"/sys/class/power_supply/dollar_cove_battery/enable_charging" // e.g. for Jolla Tablet
};
foreach(const QString& file, filenames) {
foreach(const QString& file, controlFiles) {
if(!chargingEnabledFile && QFile::exists(file)) {
chargingEnabledFile = new QFile(file, this);
break;

View file

@ -37,9 +37,12 @@ Battery::Battery(Logger* newLogger, bool loglevelSet, QCoreApplication *app, QOb
QStringList filenames;
// Battery charge percentage, number, e.g. 42
filenames << "/sys/class/power_supply/battery/capacity"
<< "/sys/class/power_supply/dollar_cove_battery/capacity";
foreach(const QString& file, filenames) {
const QStringList capacityFiles = {
"/sys/class/power_supply/battery/capacity",
"/sys/class/power_supply/dollar_cove_battery/capacity"
};
foreach(const QString& file, capacityFiles) {
if(!chargeFile && QFile::exists(file)) {
chargeFile = new QFile(file, this);
break;
@ -49,11 +52,12 @@ Battery::Battery(Logger* newLogger, bool loglevelSet, QCoreApplication *app, QOb
logL("Battery charge file: " + (chargeFile ? chargeFile->fileName() : notFound));
// Charging/discharging current in microamps, e.g. -1450000 (-145mA)
filenames.clear();
filenames << "/sys/class/power_supply/battery/current_now"
<< "/sys/class/power_supply/dollar_cove_battery/current_now";
const QStringList currentFiles = {
"/sys/class/power_supply/battery/current_now",
"/sys/class/power_supply/dollar_cove_battery/current_now"
};
foreach(const QString& file, filenames) {
foreach(const QString& file, currentFiles) {
if(!currentFile && QFile::exists(file)) {
currentFile = new QFile(file, this);
break;
@ -63,10 +67,11 @@ Battery::Battery(Logger* newLogger, bool loglevelSet, QCoreApplication *app, QOb
logL("Charging/discharging current file: " + (currentFile ? currentFile->fileName() : notFound));
// Maximum charge current in microamps, e.g. 3500000 (3500mA)
filenames.clear();
filenames << "/sys/class/power_supply/battery/constant_charge_current_max";
const QStringList maxCurrentFiles = {
"/sys/class/power_supply/battery/constant_charge_current_max"
};
foreach(const QString& file, filenames) {
foreach(const QString& file, maxCurrentFiles) {
if(!maxChargeCurrentFile && QFile::exists(file)) {
maxChargeCurrentFile = new QFile(file, this);
break;
@ -103,11 +108,12 @@ Battery::Battery(Logger* newLogger, bool loglevelSet, QCoreApplication *app, QOb
}
// Battery/charging status: charging, discharging, full, empty, unknown (others?)
filenames.clear();
filenames << "/sys/class/power_supply/battery/status"
<< "/sys/class/power_supply/dollar_cove_battery/status";
const QStringList statusFiles = {
"/sys/class/power_supply/battery/status",
"/sys/class/power_supply/dollar_cove_battery/status"
};
foreach(const QString& file, filenames) {
foreach(const QString& file, statusFiles) {
if(!stateFile && QFile::exists(file)) {
stateFile = new QFile(file, this);
break;
@ -117,11 +123,12 @@ Battery::Battery(Logger* newLogger, bool loglevelSet, QCoreApplication *app, QOb
logL("Status file: " + (stateFile ? stateFile->fileName() : notFound));
// Charger connected, bool (number): 0 or 1
filenames.clear();
filenames << "/sys/class/power_supply/usb/present"
<< "/sys/class/power_supply/dollar_cove_charger/present";
const QStringList chargerFiles = {
"/sys/class/power_supply/usb/present",
"/sys/class/power_supply/dollar_cove_charger/present"
};
foreach(const QString& file, filenames) {
foreach(const QString& file, chargerFiles) {
if(!chargerConnectedFile && QFile::exists(file)) {
chargerConnectedFile = new QFile(file, this);
break;
@ -131,10 +138,11 @@ Battery::Battery(Logger* newLogger, bool loglevelSet, QCoreApplication *app, QOb
logL("Charger status file: " + (chargerConnectedFile ? chargerConnectedFile->fileName() : notFound));
// Charger connected, bool (number): 0 or 1
filenames.clear();
filenames << "/sys/class/power_supply/ac/present";
const QStringList acFiles = {
"/sys/class/power_supply/ac/present"
};
foreach(const QString& file, filenames) {
foreach(const QString& file, acFiles) {
if(!acConnectedFile && QFile::exists(file)) {
acConnectedFile = new QFile(file, this);
break;
@ -144,11 +152,12 @@ Battery::Battery(Logger* newLogger, bool loglevelSet, QCoreApplication *app, QOb
logL("AC status file: " + (acConnectedFile ? acConnectedFile->fileName() : notFound));
// Number: temperature
filenames.clear();
filenames << "/sys/class/power_supply/battery/temp"
<< "/sys/class/power_supply/dollar_cove_battery/temp";
const QStringList tempFiles = {
"/sys/class/power_supply/battery/temp",
"/sys/class/power_supply/dollar_cove_battery/temp"
};
foreach(const QString& file, filenames) {
foreach(const QString& file, tempFiles) {
if(!temperatureFile && QFile::exists(file)) {
temperatureFile = new QFile(file, this);
break;
@ -158,9 +167,10 @@ Battery::Battery(Logger* newLogger, bool loglevelSet, QCoreApplication *app, QOb
logL("Battery temperature file: " + (temperatureFile ? temperatureFile->fileName() : notFound));
// String: health state
filenames.clear();
filenames << "/sys/class/power_supply/battery/health"
<< "/sys/class/power_supply/dollar_cove_battery/health";
const QStringList healthFiles = {
"/sys/class/power_supply/battery/health",
"/sys/class/power_supply/dollar_cove_battery/health"
};
foreach(const QString& file, filenames) {
if(!healthFile && QFile::exists(file)) {
healthFile = new QFile(file, this);
@ -171,13 +181,14 @@ Battery::Battery(Logger* newLogger, bool loglevelSet, QCoreApplication *app, QOb
logL("Battery health file: " + (healthFile ? healthFile->fileName() : notFound));
// Charger control file
filenames.clear();
filenames << "/sys/class/power_supply/battery/input_suspend" // e.g. Sony Xperia XA2
<< "/sys/class/power_supply/battery/charging_enabled" // e.g. for Sony Xperia Z3 Compact Tablet
<< "/sys/class/power_supply/usb/charger_disable" // e.g. for Jolla Phone
<< "/sys/class/power_supply/dollar_cove_battery/enable_charging"; // e.g. for Jolla Tablet
const QStringList controlFiles = {
"/sys/class/power_supply/battery/input_suspend", // e.g. Sony Xperia XA2
"/sys/class/power_supply/battery/charging_enabled", // e.g. for Sony Xperia Z3 Compact Tablet
"/sys/class/power_supply/usb/charger_disable", // e.g. for Jolla Phone
"/sys/class/power_supply/dollar_cove_battery/enable_charging" // e.g. for Jolla Tablet
};
foreach(const QString& file, filenames) {
foreach(const QString& file, controlFiles) {
if(!chargingEnabledFile && QFile::exists(file)) {
chargingEnabledFile = new QFile(file, this);
break;