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
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() { }

View file

@ -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();

View file

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

View file

@ -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();