Battery class added

This commit is contained in:
Matti Viljanen 2019-01-05 17:14:00 +02:00
parent 46e27d18b7
commit 5b99d11f3f
3 changed files with 94 additions and 1 deletions

View file

@ -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

51
src/battery.cpp Normal file
View file

@ -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;
}

38
src/battery.h Normal file
View file

@ -0,0 +1,38 @@
#ifndef BATTERY_H
#define BATTERY_H
#include <QObject>
#include <QString>
#include <QFile>
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