Use QSettings instead of QSqlDatabase, move defaults to headers.
This is what over thinking looks like. QSettings need *absolutely no configuration*, whereas QSqlDatabase couldn't even get the directory created, and for some reason (simple write permissions, I think) QDir couldn't even create the data folder. Gah!
This commit is contained in:
parent
2a9db678b0
commit
5eea9e9732
4 changed files with 29 additions and 67 deletions
|
@ -22,14 +22,11 @@ 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;
|
||||
// TODO
|
||||
// What if the files can't be opened?
|
||||
// What if the device doesn't have a battery?
|
||||
|
||||
nextCharge = currentCharge;
|
||||
nextCharging = isCharging;
|
||||
|
||||
this->updateData();
|
||||
updateData();
|
||||
}
|
||||
|
||||
Battery::~Battery() { }
|
||||
|
|
|
@ -42,11 +42,12 @@ private:
|
|||
QFile* chargeFile;
|
||||
QFile* chargingFile;
|
||||
|
||||
int currentCharge;
|
||||
bool isCharging;
|
||||
// Default values
|
||||
int currentCharge = 100;
|
||||
bool isCharging = true;
|
||||
|
||||
int nextCharge;
|
||||
bool nextCharging;
|
||||
int nextCharge = 100;
|
||||
bool nextCharging = true;
|
||||
|
||||
signals:
|
||||
int chargeChanged();
|
||||
|
|
|
@ -17,66 +17,31 @@
|
|||
*/
|
||||
#include "settings.h"
|
||||
|
||||
Settings::Settings(QObject *parent) : QObject(parent)
|
||||
{
|
||||
// Defaults
|
||||
lowerLimit = 25;
|
||||
upperLimit = 75;
|
||||
|
||||
// Just to be sure...
|
||||
QDir datadir = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
|
||||
if(!datadir.exists())
|
||||
datadir.mkdir(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
|
||||
|
||||
// Use Sailfish-provided sounds
|
||||
lowAlertFile = "/usr/share/sounds/jolla-ambient/stereo/general_warning.wav";
|
||||
highAlertFile = "/usr/share/sounds/jolla-ambient/stereo/positive_confirmation.wav";
|
||||
Settings::Settings(QObject *parent) : QObject(parent) { load(); }
|
||||
|
||||
Settings::~Settings() { save(); }
|
||||
|
||||
void Settings::load()
|
||||
{
|
||||
QDir::setCurrent(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
|
||||
QFile file;
|
||||
|
||||
file.setFileName("lowerLimit");
|
||||
if(file.exists() && file.open(QIODevice::ReadOnly)) {
|
||||
int nextLimit = file.readAll().toInt();
|
||||
if(nextLimit >= 10 && nextLimit <= 50) {
|
||||
lowerLimit = nextLimit;
|
||||
emit lowerLimitChanged();
|
||||
QSettings mySettings;
|
||||
int tempValue;
|
||||
if(mySettings.contains("lowerLimit")) {
|
||||
tempValue = mySettings.value("lowerLimit").toInt();
|
||||
if(tempValue <= 10 && tempValue >= 99)
|
||||
lowerLimit = tempValue;
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
file.setFileName("upperLimit");
|
||||
if(file.exists() && file.open(QIODevice::ReadOnly)) {
|
||||
int nextLimit = file.readAll().toInt();
|
||||
if(nextLimit >= 60 && nextLimit <= 99) {
|
||||
upperLimit = nextLimit;
|
||||
emit upperLimitChanged();
|
||||
}
|
||||
file.close();
|
||||
if(mySettings.contains("upperLimit")) {
|
||||
tempValue = mySettings.value("upperLimit").toInt();
|
||||
if(tempValue <= 60 && tempValue >= 99)
|
||||
upperLimit = tempValue;
|
||||
}
|
||||
}
|
||||
|
||||
void Settings::save()
|
||||
{
|
||||
QDir::setCurrent(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
|
||||
QFile file;
|
||||
|
||||
file.setFileName("lowerLimit");
|
||||
if(file.open(QIODevice::WriteOnly)) {
|
||||
if(file.write(QString("%1").arg(lowerLimit).toUtf8())) {
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
|
||||
file.setFileName("upperLimit");
|
||||
if(file.open(QIODevice::WriteOnly)) {
|
||||
if(file.write(QString("%1").arg(upperLimit).toUtf8())) {
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
QSettings mySettings;
|
||||
mySettings.setValue("lowerLimit", QByteArray::number(lowerLimit));
|
||||
mySettings.setValue("upperLimit", QByteArray::number(upperLimit));
|
||||
}
|
||||
|
||||
int Settings::getLowerLimit() { return lowerLimit; }
|
||||
|
|
|
@ -19,9 +19,7 @@
|
|||
#define SETTINGS_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QFile>
|
||||
#include <QDir>
|
||||
#include <QStandardPaths>
|
||||
#include <QSettings>
|
||||
|
||||
class Settings : public QObject
|
||||
{
|
||||
|
@ -44,10 +42,11 @@ public:
|
|||
void setUpperLimit(int newLimit);
|
||||
|
||||
private:
|
||||
int lowerLimit;
|
||||
int upperLimit;
|
||||
QString lowAlertFile;
|
||||
QString highAlertFile;
|
||||
// Default values
|
||||
int lowerLimit = 25;
|
||||
int upperLimit = 75;
|
||||
QString lowAlertFile = "/usr/share/sounds/jolla-ambient/stereo/general_warning.wav";
|
||||
QString highAlertFile = "/usr/share/sounds/jolla-ambient/stereo/positive_confirmation.wav";
|
||||
|
||||
void load();
|
||||
void save();
|
||||
|
|
Loading…
Reference in a new issue