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:
Matti Viljanen 2019-01-06 16:39:33 +02:00
parent 2a9db678b0
commit 5eea9e9732
No known key found for this signature in database
GPG key ID: CF32A1495158F888
4 changed files with 29 additions and 67 deletions

View file

@ -22,14 +22,11 @@ Battery::Battery(QObject* parent) : QObject(parent)
chargeFile = new QFile("/run/state/namespaces/Battery/ChargePercentage"); // Number, meaning percentage, e.g. 42 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 chargingFile = new QFile("/run/state/namespaces/Battery/IsCharging"); // Number, 0 or 1
// Default values... // TODO
currentCharge = 100; // What if the files can't be opened?
isCharging = true; // What if the device doesn't have a battery?
nextCharge = currentCharge; updateData();
nextCharging = isCharging;
this->updateData();
} }
Battery::~Battery() { } Battery::~Battery() { }

View file

@ -42,11 +42,12 @@ private:
QFile* chargeFile; QFile* chargeFile;
QFile* chargingFile; QFile* chargingFile;
int currentCharge; // Default values
bool isCharging; int currentCharge = 100;
bool isCharging = true;
int nextCharge; int nextCharge = 100;
bool nextCharging; bool nextCharging = true;
signals: signals:
int chargeChanged(); int chargeChanged();

View file

@ -17,66 +17,31 @@
*/ */
#include "settings.h" #include "settings.h"
Settings::Settings(QObject *parent) : QObject(parent) Settings::Settings(QObject *parent) : QObject(parent) { load(); }
{
// 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() { save(); } Settings::~Settings() { save(); }
void Settings::load() void Settings::load()
{ {
QDir::setCurrent(QStandardPaths::writableLocation(QStandardPaths::DataLocation)); QSettings mySettings;
QFile file; int tempValue;
if(mySettings.contains("lowerLimit")) {
file.setFileName("lowerLimit"); tempValue = mySettings.value("lowerLimit").toInt();
if(file.exists() && file.open(QIODevice::ReadOnly)) { if(tempValue <= 10 && tempValue >= 99)
int nextLimit = file.readAll().toInt(); lowerLimit = tempValue;
if(nextLimit >= 10 && nextLimit <= 50) {
lowerLimit = nextLimit;
emit lowerLimitChanged();
} }
file.close(); if(mySettings.contains("upperLimit")) {
} tempValue = mySettings.value("upperLimit").toInt();
file.setFileName("upperLimit"); if(tempValue <= 60 && tempValue >= 99)
if(file.exists() && file.open(QIODevice::ReadOnly)) { upperLimit = tempValue;
int nextLimit = file.readAll().toInt();
if(nextLimit >= 60 && nextLimit <= 99) {
upperLimit = nextLimit;
emit upperLimitChanged();
}
file.close();
} }
} }
void Settings::save() void Settings::save()
{ {
QDir::setCurrent(QStandardPaths::writableLocation(QStandardPaths::DataLocation)); QSettings mySettings;
QFile file; mySettings.setValue("lowerLimit", QByteArray::number(lowerLimit));
mySettings.setValue("upperLimit", QByteArray::number(upperLimit));
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();
}
} }
int Settings::getLowerLimit() { return lowerLimit; } int Settings::getLowerLimit() { return lowerLimit; }

View file

@ -19,9 +19,7 @@
#define SETTINGS_H #define SETTINGS_H
#include <QObject> #include <QObject>
#include <QFile> #include <QSettings>
#include <QDir>
#include <QStandardPaths>
class Settings : public QObject class Settings : public QObject
{ {
@ -44,10 +42,11 @@ public:
void setUpperLimit(int newLimit); void setUpperLimit(int newLimit);
private: private:
int lowerLimit; // Default values
int upperLimit; int lowerLimit = 25;
QString lowAlertFile; int upperLimit = 75;
QString highAlertFile; 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 load();
void save(); void save();