2020-04-26 11:30:30 +03:00
|
|
|
/**
|
|
|
|
* Battery Buddy, a Sailfish application to prolong battery lifetime
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019-2020 Matti Viljanen
|
|
|
|
*
|
|
|
|
* Battery Buddy is free software: you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Battery Buddy is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU
|
|
|
|
* General Public License along with Battery Buddy. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Author: Matti Viljanen
|
|
|
|
*/
|
|
|
|
#include "settings.h"
|
|
|
|
|
|
|
|
Settings::Settings(QObject *parent) : QObject(parent)
|
|
|
|
{
|
|
|
|
// Use the same file location as GUI for data exchange
|
|
|
|
if(!mySettings) {
|
|
|
|
mySettings = new QSettings("harbour-batterybuddy", "harbour-batterybuddy");
|
|
|
|
}
|
2020-04-26 18:20:28 +03:00
|
|
|
|
2020-04-26 11:30:30 +03:00
|
|
|
qDebug() << "Using" << mySettings->fileName();
|
|
|
|
|
|
|
|
// Migrate old settings
|
|
|
|
if(mySettings->contains("lowerLimit")) {
|
|
|
|
mySettings->setValue(sLowAlert, mySettings->value("lowerLimit"));
|
|
|
|
mySettings->remove("lowerLimit");
|
|
|
|
qInfo() << "Migrated old lowerLimit value";
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mySettings->contains("upperLimit")) {
|
|
|
|
mySettings->setValue(sHighAlert, mySettings->value("upperLimit"));
|
|
|
|
mySettings->remove("upperLimit");
|
|
|
|
qInfo() << "Migrated old upperLimit value";
|
|
|
|
}
|
|
|
|
|
2020-06-14 19:22:41 +03:00
|
|
|
if(mySettings->contains("notificationsEnabled")) {
|
|
|
|
mySettings->setValue(sHighNotificationsEnabled, mySettings->value("notificationsEnabled"));
|
|
|
|
mySettings->remove("notificationsEnabled");
|
|
|
|
qInfo() << "Migrated old upperLimit value";
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mySettings->contains("interval")) {
|
|
|
|
mySettings->setValue(sHighNotificationsInterval, mySettings->value("interval"));
|
|
|
|
mySettings->setValue(sLowNotificationsInterval, mySettings->value("interval"));
|
|
|
|
mySettings->remove("interval");
|
|
|
|
qInfo() << "Migrated old notification interval value";
|
|
|
|
}
|
|
|
|
|
2020-04-26 11:30:30 +03:00
|
|
|
// Do this here, because...
|
|
|
|
watcher = new QFileSystemWatcher(QStringList(mySettings->fileName()));
|
2020-04-26 18:20:28 +03:00
|
|
|
connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(updateConfig(QString)));
|
2020-04-26 11:30:30 +03:00
|
|
|
|
|
|
|
// ...calling this deletes mySettings!
|
2020-04-26 18:20:28 +03:00
|
|
|
updateConfig(mySettings->fileName());
|
2020-04-26 11:30:30 +03:00
|
|
|
|
|
|
|
qInfo() << "Loaded" << sLimitEnabled << limitEnabled;
|
|
|
|
|
|
|
|
// Battery Buddy GUI application changes the settings file,
|
|
|
|
// so we must monitor it and update when it changes.
|
|
|
|
}
|
|
|
|
|
|
|
|
Settings::~Settings()
|
|
|
|
{
|
|
|
|
delete mySettings;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Settings::bound(int value, int min, int max) {
|
|
|
|
return (value <= min ? min : (value >= max ? max : value));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Settings::loadInteger(const char* key, int *value, int min, int max) {
|
|
|
|
*value = bound(mySettings->value(key, *value).toInt(), min, max);
|
|
|
|
qInfo() << "Loaded" << key << *value;
|
|
|
|
}
|
|
|
|
|
2020-04-26 18:20:28 +03:00
|
|
|
void Settings::updateConfig(QString path) {
|
2020-04-26 11:30:30 +03:00
|
|
|
|
|
|
|
// Use the same file location as GUI for data exchange
|
|
|
|
if(!mySettings) {
|
|
|
|
mySettings = new QSettings("harbour-batterybuddy", "harbour-batterybuddy");
|
|
|
|
}
|
|
|
|
|
|
|
|
qDebug() << "Reading values...";
|
|
|
|
// Read in the values
|
|
|
|
loadInteger(sLowAlert, &lowAlert, 10, 99);
|
|
|
|
loadInteger(sHighAlert, &highAlert, 11, 100);
|
2020-06-14 19:22:41 +03:00
|
|
|
loadInteger(sHighNotificationsInterval, &highNotificationsInterval, 60, 600);
|
|
|
|
loadInteger(sLowNotificationsInterval, &lowNotificationsInterval, 60, 600);
|
2020-04-26 11:30:30 +03:00
|
|
|
loadInteger(sLimitEnabled, &limitEnabled, 0, 1);
|
2020-06-14 19:22:41 +03:00
|
|
|
loadInteger(sHighNotificationsEnabled, &highNotificationsEnabled, 0, 1);
|
|
|
|
loadInteger(sLowNotificationsEnabled, &lowNotificationsEnabled, 0, 1);
|
2020-04-26 11:30:30 +03:00
|
|
|
loadInteger(sLowLimit, &lowLimit, 20, 94);
|
|
|
|
loadInteger(sHighLimit, &highLimit, 21, 95);
|
2020-04-26 18:20:28 +03:00
|
|
|
|
|
|
|
// These are translated in the GUI application
|
|
|
|
// and delivered here via the config file
|
|
|
|
notificationTitle = mySettings->value(sNotificationTitle, "Battery charge %1%").toString();
|
|
|
|
notificationLowText = mySettings->value(sNotificationLowText, "Please connect the charger.").toString();
|
|
|
|
notificationHighText = mySettings->value(sNotificationHighText, "Please disconnect the charger.").toString();
|
|
|
|
|
2020-04-26 11:30:30 +03:00
|
|
|
qDebug() << "Values read.";
|
|
|
|
|
|
|
|
delete mySettings;
|
|
|
|
mySettings = nullptr;
|
|
|
|
|
|
|
|
// Let the file system settle...
|
|
|
|
QThread::msleep(50);
|
|
|
|
|
|
|
|
if(watcher->files().contains(path)) {
|
|
|
|
qDebug() << "File OK";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
qDebug() << "File replaced, re-adding.";
|
|
|
|
watcher->addPath(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Getters condensed
|
2020-06-14 19:22:41 +03:00
|
|
|
int Settings::getLowAlert() { return lowAlert; }
|
|
|
|
int Settings::getHighAlert() { return highAlert; }
|
|
|
|
int Settings::getHighNotificationsInterval() { return highNotificationsInterval; }
|
|
|
|
int Settings::getLowNotificationsInterval() { return lowNotificationsInterval; }
|
|
|
|
int Settings::getLowLimit() { return lowLimit; }
|
|
|
|
int Settings::getHighLimit() { return highLimit; }
|
|
|
|
bool Settings::getLimitEnabled() { return limitEnabled == 1; }
|
|
|
|
bool Settings::getHighNotificationsEnabled() { return highNotificationsEnabled == 1; }
|
|
|
|
bool Settings::getLowNotificationsEnabled() { return lowNotificationsEnabled == 1; }
|
|
|
|
QString Settings::getLowAlertFile() { return lowAlertFile; }
|
|
|
|
QString Settings::getHighAlertFile() { return highAlertFile; }
|
|
|
|
QString Settings::getNotificationTitle() { return notificationTitle; }
|
|
|
|
QString Settings::getNotificationLowText() { return notificationLowText; }
|
|
|
|
QString Settings::getNotificationHighText() { return notificationHighText; }
|