Handle saving and loading config more carefully. Fixes #35
This commit is contained in:
parent
bd3bfb8365
commit
308f0ae060
4 changed files with 114 additions and 89 deletions
|
@ -37,25 +37,20 @@ Settings::Settings(Logger *newLogger, QObject *parent) : QObject(parent)
|
||||||
loadInteger(sLowLimit, lowLimit, 5, 99);
|
loadInteger(sLowLimit, lowLimit, 5, 99);
|
||||||
loadInteger(sHighLimit, highLimit, 6, 100);
|
loadInteger(sHighLimit, highLimit, 6, 100);
|
||||||
loadInteger(sLogLevel, logLevel, 0, 2);
|
loadInteger(sLogLevel, logLevel, 0, 2);
|
||||||
logFilename = mySettings->value(sLogFilename).toString();
|
|
||||||
|
|
||||||
notificationTitle = tr("Battery charge %1%");
|
loadString(sLogFilename, logFilename);
|
||||||
notificationLowText = tr("Please connect the charger.");
|
|
||||||
notificationHighText = tr("Please disconnect the charger.");
|
loadString(sNotificationTitle, notificationTitle);
|
||||||
|
loadString(sNotificationLowText, notificationLowText);
|
||||||
|
loadString(sNotificationHighText, notificationHighText);
|
||||||
|
|
||||||
|
saveString(sNotificationTitle, tr("Battery charge %1%"), notificationTitle);
|
||||||
|
saveString(sNotificationLowText, tr("Please connect the charger."), notificationLowText);
|
||||||
|
saveString(sNotificationHighText, tr("Please disconnect the charger."), notificationHighText);
|
||||||
}
|
}
|
||||||
|
|
||||||
Settings::~Settings()
|
Settings::~Settings()
|
||||||
{
|
{
|
||||||
saveInteger(sLowAlert, lowAlert);
|
|
||||||
saveInteger(sHighAlert, highAlert);
|
|
||||||
saveInteger(sHighNotificationsInterval, highNotificationsInterval);
|
|
||||||
saveInteger(sLowNotificationsInterval, lowNotificationsInterval);
|
|
||||||
saveInteger(sLimitEnabled, limitEnabled);
|
|
||||||
saveInteger(sLowLimit, lowLimit);
|
|
||||||
saveInteger(sHighLimit, highLimit);
|
|
||||||
mySettings->setValue(sNotificationTitle, notificationTitle);
|
|
||||||
mySettings->setValue(sNotificationLowText, notificationLowText);
|
|
||||||
mySettings->setValue(sNotificationHighText, notificationHighText);
|
|
||||||
mySettings->sync();
|
mySettings->sync();
|
||||||
logV(QString("Settings saved: %1").arg(mySettings->status() == QSettings::NoError));
|
logV(QString("Settings saved: %1").arg(mySettings->status() == QSettings::NoError));
|
||||||
}
|
}
|
||||||
|
@ -77,92 +72,105 @@ QString Settings::getNotificationHighText() { return notificationHighText;
|
||||||
int Settings::getLogLevel() { return logLevel; }
|
int Settings::getLogLevel() { return logLevel; }
|
||||||
|
|
||||||
void Settings::setLowAlert(const int newLimit) {
|
void Settings::setLowAlert(const int newLimit) {
|
||||||
lowAlert = newLimit;
|
if(saveInteger(sLowAlert, newLimit, lowAlert)) {
|
||||||
saveInteger(sLowAlert, lowAlert);
|
emit lowAlertChanged(lowAlert);
|
||||||
// Lows and highs are always saved in pairs!
|
}
|
||||||
//mySettings->sync();
|
|
||||||
emit lowAlertChanged(lowAlert);
|
|
||||||
logD(QString("%1 %2").arg(sLowAlert).arg(newLimit));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Settings::setHighAlert(const int newLimit) {
|
void Settings::setHighAlert(const int newLimit) {
|
||||||
highAlert = newLimit;
|
if(saveInteger(sHighAlert, newLimit, highAlert)) {
|
||||||
saveInteger(sHighAlert, highAlert);
|
emit highAlertChanged(highAlert);
|
||||||
mySettings->sync();
|
}
|
||||||
emit highAlertChanged(highAlert);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Settings::setHighNotificationsInterval(const int newInterval) {
|
void Settings::setHighNotificationsInterval(const int newInterval) {
|
||||||
highNotificationsInterval = newInterval;
|
if(saveInteger(sHighNotificationsInterval, newInterval, highNotificationsInterval)) {
|
||||||
saveInteger(sHighNotificationsInterval, highNotificationsInterval);
|
emit highNotificationsIntervalChanged(highNotificationsInterval);
|
||||||
mySettings->sync();
|
}
|
||||||
emit highNotificationsIntervalChanged(highNotificationsInterval);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Settings::setLowNotificationsInterval(const int newInterval) {
|
void Settings::setLowNotificationsInterval(const int newInterval) {
|
||||||
lowNotificationsInterval = newInterval;
|
if(saveInteger(sLowNotificationsInterval, newInterval, lowNotificationsInterval)) {
|
||||||
saveInteger(sLowNotificationsInterval, lowNotificationsInterval);
|
emit highNotificationsIntervalChanged(lowNotificationsInterval);
|
||||||
mySettings->sync();
|
}
|
||||||
emit highNotificationsIntervalChanged(lowNotificationsInterval);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Settings::setLowLimit(const int newLimit) {
|
void Settings::setLowLimit(const int newLimit) {
|
||||||
lowLimit = newLimit;
|
if(saveInteger(sLowLimit, newLimit, lowLimit)) {
|
||||||
saveInteger(sLowLimit, lowLimit);
|
emit lowLimitChanged(lowLimit);
|
||||||
// Lows and highs are always saved in pairs!
|
}
|
||||||
//mySettings->sync();
|
|
||||||
emit lowLimitChanged(lowLimit);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Settings::setHighLimit(const int newLimit) {
|
void Settings::setHighLimit(const int newLimit) {
|
||||||
highLimit = newLimit;
|
if(saveInteger(sHighLimit, newLimit, highLimit))
|
||||||
saveInteger(sHighLimit, highLimit);
|
emit highLimitChanged(highLimit);
|
||||||
mySettings->sync();
|
|
||||||
emit highLimitChanged(highLimit);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Settings::setLimitEnabled(const bool newEnabled) {
|
void Settings::setLimitEnabled(const bool newEnabled) {
|
||||||
limitEnabled = (newEnabled ? 1 : 0);
|
if(saveInteger(sLimitEnabled, (newEnabled ? 1 : 0), limitEnabled))
|
||||||
saveInteger(sLimitEnabled, limitEnabled);
|
emit limitEnabledChanged(limitEnabled);
|
||||||
mySettings->sync();
|
|
||||||
emit limitEnabledChanged(limitEnabled);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Settings::setNotificationTitle(const QString newText) {
|
void Settings::setNotificationTitle(const QString newText) {
|
||||||
notificationTitle = QString(newText).replace("\"", "\\\"");
|
if(saveString(sNotificationTitle, newText, notificationTitle))
|
||||||
mySettings->setValue(sNotificationTitle, notificationTitle);
|
emit notificationTitleChanged(notificationTitle);
|
||||||
mySettings->sync();
|
|
||||||
emit notificationTitleChanged(notificationTitle);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Settings::setNotificationLowText(const QString newText) {
|
void Settings::setNotificationLowText(const QString newText) {
|
||||||
notificationLowText = QString(newText).replace("\"", "\\\"");
|
if(saveString(sNotificationLowText, newText, notificationLowText))
|
||||||
mySettings->setValue(sNotificationLowText, notificationLowText);
|
emit notificationLowTextChanged(notificationLowText);
|
||||||
mySettings->sync();
|
|
||||||
emit notificationLowTextChanged(notificationLowText);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Settings::setNotificationHighText(const QString newText) {
|
void Settings::setNotificationHighText(const QString newText) {
|
||||||
notificationHighText = QString(newText).replace("\"", "\\\"");
|
if(saveString(sNotificationHighText, newText, notificationHighText))
|
||||||
mySettings->setValue(sNotificationHighText, notificationHighText);
|
emit notificationHighTextChanged(notificationHighText);
|
||||||
mySettings->sync();
|
|
||||||
emit notificationHighTextChanged(notificationHighText);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Settings::setLogLevel(const int newLogLevel) {
|
void Settings::setLogLevel(const int newLogLevel) {
|
||||||
logLevel = newLogLevel;
|
if(saveInteger(sLogLevel, newLogLevel, logLevel))
|
||||||
saveInteger(sLogLevel, logLevel);
|
emit logLevelChanged(logLevel);
|
||||||
mySettings->sync();
|
|
||||||
emit logLevelChanged(logLevel);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Settings::loadInteger(const char *key, int &value, const int min, const int max) {
|
bool Settings::loadInteger(const char *key, int &currValue, const int min, const int max) {
|
||||||
int newValue = mySettings->value(key, value).toInt();
|
int newValue = mySettings->value(key, currValue).toInt();
|
||||||
value = (newValue <= min ? min : (newValue >= max ? max : newValue));
|
newValue = (newValue <= min ? min : (newValue >= max ? max : newValue));
|
||||||
logV(QString("Load: %1 %2").arg(key).arg(value));
|
if(currValue == newValue) {
|
||||||
|
logD(QString("Load: %1 %2 (unchanged)").arg(key).arg(currValue));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
currValue = newValue;
|
||||||
|
logV(QString("Load: %1 %2").arg(key).arg(currValue));
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Settings::saveInteger(const char* key, const int &value) {
|
bool Settings::loadString(const char *key, QString & currValue) {
|
||||||
mySettings->setValue(key, QByteArray::number(value));
|
QString newValue = mySettings->value(key, currValue).toString();
|
||||||
logV(QString("Save: %1 %2").arg(key).arg(value));
|
if(currValue == newValue) {
|
||||||
|
logD(QString("Load: %1 %2 (unchanged)").arg(key).arg(currValue));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
currValue = newValue;
|
||||||
|
logV(QString("Load: %1 %2").arg(key).arg(currValue));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Settings::saveInteger(const char* key, const int &newValue, int &currValue) {
|
||||||
|
if(currValue == newValue) {
|
||||||
|
logD(QString("Save: %1 %2 (unchanged)").arg(key).arg(currValue));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
currValue = newValue;
|
||||||
|
mySettings->setValue(key, QByteArray::number(newValue));
|
||||||
|
logV(QString("Save: %1 %2").arg(key).arg(newValue));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Settings::saveString(const char* key, const QString &newValue, QString &currValue) {
|
||||||
|
if(currValue == newValue) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
currValue = newValue;
|
||||||
|
mySettings->setValue(key, QString(newValue).replace("\"", "\\\"").toUtf8());
|
||||||
|
logV(QString("Save: %1 %2").arg(key).arg(newValue));
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -108,8 +108,11 @@ private:
|
||||||
const char* sLogFilename = "logFilename";
|
const char* sLogFilename = "logFilename";
|
||||||
const char* sLogLevel = "logLevel";
|
const char* sLogLevel = "logLevel";
|
||||||
|
|
||||||
void loadInteger(const char *key, int &value, const int min, const int max);
|
bool loadInteger(const char *key, int &currValue, const int min, const int max);
|
||||||
void saveInteger(const char *key, const int &value);
|
bool loadString(const char *key, QString & currValue);
|
||||||
|
bool saveInteger(const char* key, const int &newValue, int &currValue);
|
||||||
|
bool saveString(const char* key, const QString &newValue, QString &currValue);
|
||||||
|
void save();
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void lowAlertChanged(int);
|
void lowAlertChanged(int);
|
||||||
|
|
|
@ -88,29 +88,45 @@ Settings::Settings(Logger* newLogger, QObject *parent) : QObject(parent)
|
||||||
logV(migrate.arg(key));
|
logV(migrate.arg(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// These are updated and localized from the config file
|
||||||
|
notificationTitle = "Battery charge %1%";
|
||||||
|
notificationLowText = "Please connect the charger.";
|
||||||
|
notificationHighText = "Please disconnect the charger.";
|
||||||
|
|
||||||
// Do this here, because...
|
// Do this here, because...
|
||||||
watcher = new QFileSystemWatcher(QStringList(mySettings->fileName()), this);
|
watcher = new QFileSystemWatcher(QStringList(mySettings->fileName()), this);
|
||||||
connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(updateConfig(QString)));
|
connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(updateConfig(QString)));
|
||||||
|
|
||||||
// ...calling this deletes mySettings!
|
// ...calling this deletes mySettings!
|
||||||
updateConfig(mySettings->fileName());
|
updateConfig(mySettings->fileName());
|
||||||
|
|
||||||
// Battery Buddy GUI application changes the settings file,
|
|
||||||
// so we must monitor it and update when it changes.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Settings::~Settings() { }
|
Settings::~Settings() { }
|
||||||
|
|
||||||
bool Settings::loadInteger(const char *key, int &value, const int min, const int max) {
|
bool Settings::loadInteger(const char *key, int &currValue, const int min, const int max) {
|
||||||
oldValue = value;
|
int newValue = mySettings->value(key, currValue).toInt();
|
||||||
value = mySettings->value(key, value).toInt();
|
newValue = (newValue <= min ? min : (newValue >= max ? max : newValue));
|
||||||
value = (value <= min ? min : (value >= max ? max : value));
|
if(currValue == newValue) {
|
||||||
if(oldValue != value) {
|
logD(QString("Load: %1 %2 (unchanged)").arg(key).arg(currValue));
|
||||||
logV(QString("Load: %1 %2").arg(key).arg(value));
|
return false;
|
||||||
}
|
}
|
||||||
return oldValue != value;
|
currValue = newValue;
|
||||||
|
logV(QString("Load: %1 %2").arg(key).arg(currValue));
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Settings::loadString(const char *key, QString & currValue) {
|
||||||
|
QString newValue = mySettings->value(key, currValue).toString();
|
||||||
|
if(currValue == newValue) {
|
||||||
|
logD(QString("Load: %1 %2 (unchanged)").arg(key).arg(currValue));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
currValue = newValue;
|
||||||
|
logV(QString("Load: %1 %2").arg(key).arg(currValue));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void Settings::updateConfig(const QString path) {
|
void Settings::updateConfig(const QString path) {
|
||||||
|
|
||||||
// Use the same file location as GUI for data exchange
|
// Use the same file location as GUI for data exchange
|
||||||
|
@ -131,22 +147,19 @@ void Settings::updateConfig(const QString path) {
|
||||||
loadInteger(sLowLimit, lowLimit, 5, 99);
|
loadInteger(sLowLimit, lowLimit, 5, 99);
|
||||||
loadInteger(sHighLimit, highLimit, 6, 100);
|
loadInteger(sHighLimit, highLimit, 6, 100);
|
||||||
|
|
||||||
|
loadString(sNotificationTitle, notificationTitle);
|
||||||
|
loadString(sNotificationLowText, notificationLowText);
|
||||||
|
loadString(sNotificationHighText, notificationHighText);
|
||||||
|
|
||||||
// Update log level
|
// Update log level
|
||||||
int oldLogLevel = logLevel;
|
int oldLogLevel = logLevel;
|
||||||
loadInteger(sLogLevel, logLevel, 0, 2);
|
loadInteger(sLogLevel, logLevel, 0, 2);
|
||||||
if(oldLogLevel != logLevel) {
|
if(oldLogLevel != logLevel) {
|
||||||
logger->debug = (logLevel == 2);
|
logger->debug = (logLevel == 2);
|
||||||
logger->verbose = (logLevel > 1);
|
logger->verbose = (logLevel > 1);
|
||||||
logE(QString("Log level set to %1").arg((logLevel == 0 ? "low" : (logLevel == 1 ? "medium" : "high"))));
|
logE(QString("Log level set to %1").arg((logLevel == 0 ? "low" : (logLevel == 1 ? "medium" : "high"))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 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();
|
|
||||||
|
|
||||||
delete mySettings;
|
delete mySettings;
|
||||||
mySettings = nullptr;
|
mySettings = nullptr;
|
||||||
|
|
||||||
|
|
|
@ -94,7 +94,8 @@ private:
|
||||||
const char* sLogFilename = "logFilename";
|
const char* sLogFilename = "logFilename";
|
||||||
const char* sLogLevel = "logLevel";
|
const char* sLogLevel = "logLevel";
|
||||||
|
|
||||||
bool loadInteger(const char *key, int &value, const int min, const int max);
|
bool loadInteger(const char *key, int &currValue, const int min, const int max);
|
||||||
|
bool loadString(const char *key, QString & currValue);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void updateConfig(const QString path);
|
void updateConfig(const QString path);
|
||||||
|
|
Loading…
Reference in a new issue