Actually include settings.h and settings.cpp

This commit is contained in:
Matti Viljanen 2019-01-05 23:38:41 +02:00
parent e25ae8d2cc
commit 629bdbd1f7
2 changed files with 131 additions and 0 deletions

85
src/settings.cpp Normal file
View file

@ -0,0 +1,85 @@
#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/general_warning.wav";
highAlertFile = "/usr/share/sounds/jolla-ambient/positive_confirmation.wav";
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;
qDebug() << "Lower limit loaded:" << lowerLimit;
emit lowerLimitChanged();
}
file.close();
}
file.setFileName("upperLimit");
if(file.exists() && file.open(QIODevice::ReadOnly)) {
int nextLimit = file.readAll().toInt();
if(nextLimit >= 60 && nextLimit <= 99) {
upperLimit = nextLimit;
qDebug() << "Upper limit loaded:" << upperLimit;
emit upperLimitChanged();
}
file.close();
}
}
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())) {
qDebug() << "Lower limit saved:" << lowerLimit;
}
file.close();
}
file.setFileName("upperLimit");
if(file.open(QIODevice::WriteOnly)) {
if(file.write(QString("%1").arg(upperLimit).toUtf8())) {
qDebug() << "Upper limit saved:" << upperLimit;
}
file.close();
}
}
int Settings::getLowerLimit() { return lowerLimit; }
int Settings::getUpperLimit() { return upperLimit; }
QString Settings::getLowAlert() { return lowAlertFile; }
QString Settings::getHighAlert() { return highAlertFile; }
void Settings::setLowerLimit(int newLimit) { lowerLimit = newLimit; }
void Settings::setUpperLimit(int newLimit) { upperLimit = newLimit; }

46
src/settings.h Normal file
View file

@ -0,0 +1,46 @@
#ifndef SETTINGS_H
#define SETTINGS_H
#include <QObject>
#include <QDebug>
#include <QFile>
#include <QDir>
#include <QStandardPaths>
class Settings : public QObject
{
Q_OBJECT
Q_PROPERTY(int lowerLimit READ getLowerLimit WRITE setLowerLimit NOTIFY lowerLimitChanged)
Q_PROPERTY(int upperLimit READ getUpperLimit WRITE setUpperLimit NOTIFY upperLimitChanged)
Q_PROPERTY(QString lowAlertFile READ getLowAlert NOTIFY lowAlertChanged)
Q_PROPERTY(QString highAlertFile READ getHighAlert NOTIFY highAlertChanged)
public:
Settings(QObject* parent = nullptr);
~Settings();
// QML-exposed stuff
int getLowerLimit();
int getUpperLimit();
QString getLowAlert();
QString getHighAlert();
void setLowerLimit(int newLimit);
void setUpperLimit(int newLimit);
private:
int lowerLimit;
int upperLimit;
QString lowAlertFile;
QString highAlertFile;
void load();
void save();
signals:
int lowerLimitChanged();
int upperLimitChanged();
QString lowAlertChanged();
QString highAlertChanged();
};
#endif // SETTINGS_H