diff --git a/src/settings.cpp b/src/settings.cpp new file mode 100644 index 0000000..d0036a2 --- /dev/null +++ b/src/settings.cpp @@ -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; } diff --git a/src/settings.h b/src/settings.h new file mode 100644 index 0000000..f748d65 --- /dev/null +++ b/src/settings.h @@ -0,0 +1,46 @@ +#ifndef SETTINGS_H +#define SETTINGS_H + +#include +#include +#include +#include +#include + +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