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

View file

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