From 7ef7775c892df7cf65b0f0c0f50e29c12ce0000f Mon Sep 17 00:00:00 2001 From: Matti Viljanen Date: Tue, 8 Jan 2019 20:16:10 +0200 Subject: [PATCH] Add state to battery data, return translated state --- src/battery.cpp | 45 +++++++++++++++++++++++++++++++++++++-------- src/battery.h | 26 +++++++++++++++++--------- 2 files changed, 54 insertions(+), 17 deletions(-) diff --git a/src/battery.cpp b/src/battery.cpp index 8726af1..dd8a3bd 100644 --- a/src/battery.cpp +++ b/src/battery.cpp @@ -19,8 +19,12 @@ Battery::Battery(QObject* parent) : QObject(parent) { - chargeFile = new QFile("/run/state/namespaces/Battery/ChargePercentage"); // Number, meaning percentage, e.g. 42 - chargingFile = new QFile("/run/state/namespaces/Battery/IsCharging"); // Number, 0 or 1 + // Number: meaning percentage, e.g. 42 + chargeFile = new QFile("/run/state/namespaces/Battery/ChargePercentage"); + // Number: 0 or 1 + chargingFile = new QFile("/run/state/namespaces/Battery/IsCharging"); + // String: charging, discharging, idle, unknown (others?) + stateFile = new QFile("/run/state/namespaces/Battery/ChargingState"); // TODO // What if the files can't be opened? @@ -35,22 +39,47 @@ void Battery::updateData() { if(chargeFile->open(QIODevice::ReadOnly)) { nextCharge = chargeFile->readAll().toInt(); - if(nextCharge != currentCharge) { - currentCharge = nextCharge; + qDebug() << "Charge:" << nextCharge; + if(nextCharge != charge) { + charge = nextCharge; emit chargeChanged(); } chargeFile->close(); } if(chargingFile->open(QIODevice::ReadOnly)) { nextCharging = (chargingFile->readAll().toInt() == 0 ? false : true); - if(nextCharging != isCharging) { - isCharging = nextCharging; + qDebug() << "Charging:" << nextCharge; + if(nextCharging != charging) { + charging = nextCharging; emit chargingChanged(); } chargingFile->close(); } + if(stateFile->open(QIODevice::ReadOnly)) { + nextStatus = (QString(stateFile->readAll())); + qDebug() << "Status:" << nextStatus; + if(nextStatus != state) { + state = nextStatus; + + // Update translated text accordingly + if(state == "idle") + stateTr = tr("idle", "Battery in charger, not using nor charging battery"); + else if(state == "discharging") + stateTr = tr("discharging", "Charger not plugged in, battery discharging"); + else if(state == "charging") + stateTr = tr("charging", "Charger plugged in and battery charging"); + else if(state == "unknown") + stateTr = tr("unknown", "Battery not detected, or faulty, or something"); + + // Finally, emit the signal + emit stateChanged(); + } + stateFile->close(); + } } -int Battery::getCharge(){ return currentCharge; } +int Battery::getCharge(){ return charge; } -bool Battery::getCharging() { return isCharging; } +bool Battery::getCharging() { return charging; } + +QString Battery::getState() { return stateTr; } diff --git a/src/battery.h b/src/battery.h index 89c2f39..3aff4b9 100644 --- a/src/battery.h +++ b/src/battery.h @@ -1,7 +1,5 @@ #ifndef BATTERY_H #define BATTERY_H - -#include /** * Battery Buddy, a Sailfish application to prolong battery lifetime * @@ -19,14 +17,18 @@ * * Author: Matti Viljanen */ + +#include #include #include +#include class Battery : public QObject { Q_OBJECT - Q_PROPERTY(int charge READ getCharge NOTIFY chargeChanged ) - Q_PROPERTY(bool charging READ getCharging NOTIFY chargingChanged) + Q_PROPERTY(int charge READ getCharge NOTIFY chargeChanged ) + Q_PROPERTY(bool charging READ getCharging NOTIFY chargingChanged) + Q_PROPERTY(QString state READ getState NOTIFY stateChanged) public: Battery(QObject* parent = nullptr); @@ -34,6 +36,7 @@ public: int getCharge(); bool getCharging(); + QString getState(); public slots: void updateData(); @@ -41,17 +44,22 @@ public slots: private: QFile* chargeFile; QFile* chargingFile; + QFile* stateFile; - // Default values - int currentCharge = 100; - bool isCharging = true; + // Default values: + int charge = 100; // 100% full + bool charging = true; // Charger plugged in + QString state = "idle"; // Not charging + QString stateTr = state; // Translated state text - int nextCharge = 100; - bool nextCharging = true; + int nextCharge = charge; + bool nextCharging = charging; + QString nextStatus = state; signals: int chargeChanged(); bool chargingChanged(); + QString stateChanged(); }; #endif // BATTERY_H