diff --git a/harbour-batterybuddy.pro b/harbour-batterybuddy.pro index 421f6e5..f4b63c9 100644 --- a/harbour-batterybuddy.pro +++ b/harbour-batterybuddy.pro @@ -14,7 +14,8 @@ TARGET = harbour-batterybuddy CONFIG += sailfishapp -SOURCES += src/harbour-batterybuddy.cpp +SOURCES += src/harbour-batterybuddy.cpp \ + src/battery.cpp DISTFILES += qml/harbour-batterybuddy.qml \ qml/cover/CoverPage.qml \ @@ -38,3 +39,6 @@ CONFIG += sailfishapp_i18n # following TRANSLATIONS line. And also do not forget to # modify the localized app name in the the .desktop file. TRANSLATIONS += translations/harbour-batterybuddy-de.ts + +HEADERS += \ + src/battery.h diff --git a/src/battery.cpp b/src/battery.cpp new file mode 100644 index 0000000..f0659db --- /dev/null +++ b/src/battery.cpp @@ -0,0 +1,51 @@ +#include "battery.h" + +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 + + // Default values... + currentCharge = 100; + isCharging = true; + + nextCharge = currentCharge; + nextCharging = isCharging; + + this->updateData(); +} + +Battery::~Battery() +{ + +} + +void Battery::updateData() +{ + if(chargeFile->open(QIODevice::ReadOnly)) { + nextCharge = chargeFile->readAll().toInt(); + if(nextCharge != currentCharge) { + currentCharge = nextCharge; + emit chargeChanged(); + } + chargeFile->close(); + } + if(chargingFile->open(QIODevice::ReadOnly)) { + nextCharging = (chargingFile->readAll().toInt() == 0 ? false : true); + if(nextCharging != isCharging) { + isCharging = nextCharging; + emit chargingChanged(); + } + chargingFile->close(); + } +} + +int Battery::getCharge() +{ + return currentCharge; +} + +bool Battery::getCharging() +{ + return isCharging; +} diff --git a/src/battery.h b/src/battery.h new file mode 100644 index 0000000..a82669e --- /dev/null +++ b/src/battery.h @@ -0,0 +1,38 @@ +#ifndef BATTERY_H +#define BATTERY_H + +#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) + +public: + Battery(QObject* parent = nullptr); + ~Battery(); + + int getCharge(); + bool getCharging(); + +private: + void updateData(); + + QFile* chargeFile; + QFile* chargingFile; + + int currentCharge; + bool isCharging; + + int nextCharge; + bool nextCharging; + +signals: + int chargeChanged(); + bool chargingChanged(); +}; + +#endif // BATTERY_H