From c00dd40def7c8fdb86eaeeb0af37913c1a3f8fe1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lie=20Delhaie?= Date: Sat, 5 Nov 2022 09:58:47 +0100 Subject: [PATCH] Add configuration file --- WorkPad.pro | 4 +++ src/frames/mainwindow.cpp | 11 ++++-- src/frames/mainwindow.h | 2 ++ src/models/appconfiguration.cpp | 43 +++++++++++++++++++++++ src/models/appconfiguration.h | 27 ++++++++++++++ src/services/configmanager.cpp | 62 +++++++++++++++++++++++++++++++++ src/services/configmanager.h | 29 +++++++++++++++ 7 files changed, 176 insertions(+), 2 deletions(-) create mode 100644 src/models/appconfiguration.cpp create mode 100644 src/models/appconfiguration.h create mode 100644 src/services/configmanager.cpp create mode 100644 src/services/configmanager.h diff --git a/WorkPad.pro b/WorkPad.pro index 5412722..6309da1 100755 --- a/WorkPad.pro +++ b/WorkPad.pro @@ -69,6 +69,7 @@ linux-* { #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ + src/services/configmanager.cpp \ src/frames/exportdialog.cpp \ src/frames/renamedialog.cpp \ src/frames/aboutdialog.cpp \ @@ -76,17 +77,20 @@ SOURCES += \ src/frames/movedialog.cpp \ src/main.cpp \ src/frames/mainwindow.cpp \ + src/models/appconfiguration.cpp \ src/models/note.cpp \ src/models/folder.cpp \ src/services/savemanager.cpp HEADERS += \ + src/services/configmanager.h \ src/frames/exportdialog.h \ src/frames/renamedialog.h \ src/frames/aboutdialog.h \ src/frames/createdialog.h \ src/frames/mainwindow.h \ src/frames/movedialog.h \ + src/models/appconfiguration.h \ src/models/note.h \ src/models/folder.h \ src/services/savemanager.h diff --git a/src/frames/mainwindow.cpp b/src/frames/mainwindow.cpp index 8c12d03..4718f52 100755 --- a/src/frames/mainwindow.cpp +++ b/src/frames/mainwindow.cpp @@ -26,6 +26,7 @@ MainWindow::MainWindow(QWidget *parent) ui->plainTextEdit->setFont(fixedFont); ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu); this->savemng = new SaveManager(); + this->cfgmng = new ConfigManager(); updateListView(); connect(timer, &QTimer::timeout, this, &MainWindow::save); } @@ -355,7 +356,10 @@ void MainWindow::markdownContentChanged() ui->plainTextEdit->blockSignals(false); ui->contentEdit->blockSignals(false); ui->actionSave->setDisabled(false); - timer->start(1000); + if (cfgmng->getConfiguration()->isEnableAutoSave()) + { + timer->start(cfgmng->getConfiguration()->getAutoSaveDelay()); + } } void MainWindow::plainContentChanged() @@ -378,7 +382,10 @@ void MainWindow::plainContentChanged() ui->plainTextEdit->blockSignals(false); ui->contentEdit->blockSignals(false); ui->actionSave->setDisabled(false); - timer->start(1000); + if (cfgmng->getConfiguration()->isEnableAutoSave()) + { + timer->start(cfgmng->getConfiguration()->getAutoSaveDelay()); + } } void MainWindow::updateListView() diff --git a/src/frames/mainwindow.h b/src/frames/mainwindow.h index d07b463..262edc0 100755 --- a/src/frames/mainwindow.h +++ b/src/frames/mainwindow.h @@ -12,6 +12,7 @@ #include #include "../services/savemanager.h" +#include "../services/configmanager.h" #define COLUMN_NAME 0 #define COLUMN_UUID 1 @@ -49,6 +50,7 @@ private slots: private: Ui::MainWindow *ui; SaveManager *savemng; + ConfigManager *cfgmng; QTimer *timer; void updateListView(); diff --git a/src/models/appconfiguration.cpp b/src/models/appconfiguration.cpp new file mode 100644 index 0000000..52a4739 --- /dev/null +++ b/src/models/appconfiguration.cpp @@ -0,0 +1,43 @@ +#include "appconfiguration.h" + +AppConfiguration::AppConfiguration() +{ + this->enableAutoSave = true; + this->autoSaveDelay = 1000; +} + +AppConfiguration::AppConfiguration(QJsonObject obj) +{ + this->enableAutoSave = obj[ENABLEAUTOSAVEKEY].toBool(true); + this->autoSaveDelay = obj[AUTOSAVEDELAY].toInt(1000); +} + +QJsonObject AppConfiguration::toJson() +{ + QJsonObject obj; + + obj[ENABLEAUTOSAVEKEY] = this->enableAutoSave; + obj[AUTOSAVEDELAY] = this->autoSaveDelay; + + return obj; +} + +bool AppConfiguration::isEnableAutoSave() +{ + return enableAutoSave; +} + +int AppConfiguration::getAutoSaveDelay() +{ + return autoSaveDelay; +} + +void AppConfiguration::setEnableAutoSave(bool value) +{ + this->enableAutoSave = value; +} + +void AppConfiguration::setAutoSaveDelay(int value) +{ + this->autoSaveDelay = value; +} diff --git a/src/models/appconfiguration.h b/src/models/appconfiguration.h new file mode 100644 index 0000000..e2fea63 --- /dev/null +++ b/src/models/appconfiguration.h @@ -0,0 +1,27 @@ +#ifndef APPCONFIGURATION_H +#define APPCONFIGURATION_H + +#define ENABLEAUTOSAVEKEY "enable_auto_save" +#define AUTOSAVEDELAY "auto_save_delay" + +#include + +class AppConfiguration +{ +private: + bool enableAutoSave; + int autoSaveDelay; + +public: + AppConfiguration(); + AppConfiguration(QJsonObject obj); + + QJsonObject toJson(); + bool isEnableAutoSave(); + int getAutoSaveDelay(); + + void setEnableAutoSave(bool); + void setAutoSaveDelay(int); +}; + +#endif // APPCONFIGURATION_H diff --git a/src/services/configmanager.cpp b/src/services/configmanager.cpp new file mode 100644 index 0000000..1aa3a96 --- /dev/null +++ b/src/services/configmanager.cpp @@ -0,0 +1,62 @@ +#include "configmanager.h" + +ConfigManager::ConfigManager() +{ + load(); +} + +ConfigManager::~ConfigManager() +{ + delete cfg; +} + +AppConfiguration *ConfigManager::getConfiguration() +{ + return cfg; +} + +QString ConfigManager::getSaveFilePath() { + QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); + if (!QDir(path).exists()) + { + QDir().mkpath(path); + } + + path += "/data/"; + if (!QDir(path).exists()) + { + QDir().mkpath(path); + } + path += FILENAME; + return QDir::cleanPath(path); +} + +void ConfigManager::load() { + QFile* file = new QFile(getSaveFilePath()); + if (!file->open(QIODevice::ReadOnly)) + { + delete file; + this->cfg = new AppConfiguration(); + writeToDisk(); + return; + } + auto json = QString(file->readAll()); + file->close(); + delete file; + + QJsonObject obj = QJsonDocument::fromJson(json.toUtf8()).object(); + this->cfg = new AppConfiguration(obj); + return; +} + +void ConfigManager::writeToDisk() +{ + QJsonDocument doc(cfg->toJson()); + QFile *f = new QFile(getSaveFilePath()); + if (f->open(QIODevice::WriteOnly)) + { + f->write(doc.toJson()); + f->close(); + } + delete f; +} diff --git a/src/services/configmanager.h b/src/services/configmanager.h new file mode 100644 index 0000000..e203412 --- /dev/null +++ b/src/services/configmanager.h @@ -0,0 +1,29 @@ +#ifndef CONFIGMANAGER_H +#define CONFIGMANAGER_H + +#define FILENAME "config.json" + +#include +#include +#include +#include +#include + +#include "../models/appconfiguration.h" + +class ConfigManager +{ +private: + QString getSaveFilePath(); + void load(); + AppConfiguration *cfg; + +public: + ConfigManager(); + ~ConfigManager(); + + AppConfiguration *getConfiguration(); + void writeToDisk(); +}; + +#endif // CONFIGMANAGER_H