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"
|
|
|
|
|
2021-04-17 22:01:19 +03:00
|
|
|
Settings::Settings(Logger* newLogger, QObject *parent) : QObject(parent)
|
2020-04-26 11:30:30 +03:00
|
|
|
{
|
2021-04-17 22:01:19 +03:00
|
|
|
logger = newLogger;
|
2020-04-26 11:30:30 +03:00
|
|
|
// Use the same file location as GUI for data exchange
|
|
|
|
if(!mySettings) {
|
2021-04-10 04:08:50 +03:00
|
|
|
mySettings = new QSettings(appName, appName, this);
|
2020-04-26 11:30:30 +03:00
|
|
|
}
|
2020-04-26 18:20:28 +03:00
|
|
|
|
2021-04-17 22:01:19 +03:00
|
|
|
logV("Using " + mySettings->fileName());
|
|
|
|
|
|
|
|
QString migrate = "Migrated value %1";
|
|
|
|
QString key = "";
|
2020-04-26 11:30:30 +03:00
|
|
|
|
|
|
|
// Migrate old settings
|
2021-04-17 22:01:19 +03:00
|
|
|
key = "lowerLimit";
|
|
|
|
if(mySettings->contains(key)) {
|
|
|
|
mySettings->setValue(sLowAlert, mySettings->value(key));
|
|
|
|
mySettings->remove(key);
|
|
|
|
logV(migrate.arg(key));
|
2020-04-26 11:30:30 +03:00
|
|
|
}
|
|
|
|
|
2021-04-17 22:01:19 +03:00
|
|
|
key = "upperLimit";
|
|
|
|
if(mySettings->contains(key)) {
|
|
|
|
mySettings->setValue(sHighAlert, mySettings->value(key));
|
|
|
|
mySettings->remove(key);
|
|
|
|
logV(migrate.arg(key));
|
2020-04-26 11:30:30 +03:00
|
|
|
}
|
|
|
|
|
2021-04-17 22:01:19 +03:00
|
|
|
key = "notificationsEnabled";
|
|
|
|
if(mySettings->contains(key)) {
|
|
|
|
if(mySettings->value(key).toInt() == 0) {
|
|
|
|
mySettings->setValue(sHighNotificationsInterval, 610);
|
|
|
|
mySettings->setValue(sLowNotificationsInterval, 610);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mySettings->setValue(sHighNotificationsInterval, highNotificationsInterval);
|
|
|
|
mySettings->setValue(sLowNotificationsInterval, lowNotificationsInterval);
|
|
|
|
}
|
|
|
|
mySettings->remove(key);
|
|
|
|
logV(migrate.arg(key));
|
2020-06-14 19:22:41 +03:00
|
|
|
}
|
|
|
|
|
2021-04-17 22:01:19 +03:00
|
|
|
key = "interval";
|
|
|
|
if(mySettings->contains(key)) {
|
|
|
|
mySettings->setValue(sHighNotificationsInterval, mySettings->value(key));
|
|
|
|
mySettings->setValue(sLowNotificationsInterval, mySettings->value(key));
|
|
|
|
mySettings->remove(key);
|
|
|
|
logV(migrate.arg(key));
|
2020-06-14 19:22:41 +03:00
|
|
|
}
|
|
|
|
|
2021-04-17 22:01:19 +03:00
|
|
|
key = "highNotificationsEnabled";
|
|
|
|
if(mySettings->contains(key)) {
|
|
|
|
if(mySettings->value(key).toInt() == 0)
|
2020-11-30 23:58:37 +03:00
|
|
|
mySettings->setValue(sHighNotificationsInterval, 610);
|
2021-04-17 22:01:19 +03:00
|
|
|
mySettings->remove(key);
|
|
|
|
logV(migrate.arg(key));
|
2020-11-30 23:58:37 +03:00
|
|
|
}
|
|
|
|
|
2021-04-17 22:01:19 +03:00
|
|
|
key = "lowNotificationsEnabled";
|
|
|
|
if(mySettings->contains(key)) {
|
|
|
|
if(mySettings->value(key).toInt() == 0)
|
2020-11-30 23:58:37 +03:00
|
|
|
mySettings->setValue(sLowNotificationsInterval, 610);
|
2021-04-17 22:01:19 +03:00
|
|
|
mySettings->remove(key);
|
|
|
|
logV(migrate.arg(key));
|
2020-11-30 23:58:37 +03:00
|
|
|
}
|
|
|
|
|
2020-04-26 11:30:30 +03:00
|
|
|
// Do this here, because...
|
2021-04-17 22:01:19 +03:00
|
|
|
watcher = new QFileSystemWatcher(QStringList(mySettings->fileName()), this);
|
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
|
|
|
|
|
|
|
// Battery Buddy GUI application changes the settings file,
|
|
|
|
// so we must monitor it and update when it changes.
|
|
|
|
}
|
|
|
|
|
2021-04-10 04:08:50 +03:00
|
|
|
Settings::~Settings() { }
|
2020-04-26 11:30:30 +03:00
|
|
|
|
2021-04-17 22:13:57 +03:00
|
|
|
bool Settings::loadInteger(const char *key, int &value, const int min, const int max) {
|
|
|
|
oldValue = value;
|
|
|
|
value = mySettings->value(key, value).toInt();
|
|
|
|
value = (value <= min ? min : (value >= max ? max : value));
|
|
|
|
if(oldValue != value) {
|
2021-04-17 22:01:19 +03:00
|
|
|
logV(QString("Load: %1 %2").arg(key).arg(value));
|
2020-11-30 23:28:59 +03:00
|
|
|
}
|
2021-04-17 22:13:57 +03:00
|
|
|
return oldValue != value;
|
2020-04-26 11:30:30 +03:00
|
|
|
}
|
|
|
|
|
2021-04-17 22:13:57 +03:00
|
|
|
void Settings::updateConfig(const QString path) {
|
2020-04-26 11:30:30 +03:00
|
|
|
|
|
|
|
// Use the same file location as GUI for data exchange
|
|
|
|
if(!mySettings) {
|
2021-04-10 04:08:50 +03:00
|
|
|
mySettings = new QSettings(appName, appName, this);
|
2020-04-26 11:30:30 +03:00
|
|
|
}
|
|
|
|
|
2021-04-17 22:01:19 +03:00
|
|
|
logV("Loading values...");
|
2020-04-26 11:30:30 +03:00
|
|
|
// Read in the values
|
2020-11-30 23:33:27 +03:00
|
|
|
bool restartTimers = false;
|
|
|
|
|
2021-04-17 22:13:57 +03:00
|
|
|
loadInteger(sLowAlert, lowAlert, 5, 99);
|
|
|
|
loadInteger(sHighAlert, highAlert, 6, 100);
|
|
|
|
restartTimers |= loadInteger(sHighNotificationsInterval, highNotificationsInterval, 50, 610);
|
|
|
|
restartTimers |= loadInteger(sLowNotificationsInterval, lowNotificationsInterval, 50, 610);
|
|
|
|
loadInteger(sLimitEnabled, limitEnabled, 0, 1);
|
|
|
|
loadInteger(sLowLimit, lowLimit, 5, 99);
|
|
|
|
loadInteger(sHighLimit, highLimit, 6, 100);
|
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();
|
|
|
|
|
2021-04-17 22:01:19 +03:00
|
|
|
QString logFilename = logger->getLogFilename();
|
|
|
|
if(mySettings->value(sLogFilename,QString()).toString() != logFilename) {
|
|
|
|
mySettings->setValue(sLogFilename, logFilename);
|
|
|
|
}
|
|
|
|
|
|
|
|
logV("Values loaded.");
|
2020-04-26 11:30:30 +03:00
|
|
|
|
|
|
|
delete mySettings;
|
|
|
|
mySettings = nullptr;
|
|
|
|
|
|
|
|
// Let the file system settle...
|
2021-04-17 22:01:19 +03:00
|
|
|
QThread::msleep(100);
|
2020-04-26 11:30:30 +03:00
|
|
|
|
|
|
|
if(watcher->files().contains(path)) {
|
2021-04-17 22:01:19 +03:00
|
|
|
logD("Config file already on watchlist");
|
2020-04-26 11:30:30 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
watcher->addPath(path);
|
2021-04-17 22:01:19 +03:00
|
|
|
logD("Config file added to watchlist.");
|
2020-04-26 11:30:30 +03:00
|
|
|
}
|
2020-11-30 23:33:27 +03:00
|
|
|
|
|
|
|
if(restartTimers) {
|
|
|
|
emit resetTimers();
|
|
|
|
}
|
2020-04-26 11:30:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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; }
|
|
|
|
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; }
|