Add configuration file

This commit is contained in:
Aurélie Delhaie
2022-11-05 09:58:47 +01:00
parent 3626ffa54e
commit c00dd40def
7 changed files with 176 additions and 2 deletions

View File

@@ -69,6 +69,7 @@ linux-* {
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \ SOURCES += \
src/services/configmanager.cpp \
src/frames/exportdialog.cpp \ src/frames/exportdialog.cpp \
src/frames/renamedialog.cpp \ src/frames/renamedialog.cpp \
src/frames/aboutdialog.cpp \ src/frames/aboutdialog.cpp \
@@ -76,17 +77,20 @@ SOURCES += \
src/frames/movedialog.cpp \ src/frames/movedialog.cpp \
src/main.cpp \ src/main.cpp \
src/frames/mainwindow.cpp \ src/frames/mainwindow.cpp \
src/models/appconfiguration.cpp \
src/models/note.cpp \ src/models/note.cpp \
src/models/folder.cpp \ src/models/folder.cpp \
src/services/savemanager.cpp src/services/savemanager.cpp
HEADERS += \ HEADERS += \
src/services/configmanager.h \
src/frames/exportdialog.h \ src/frames/exportdialog.h \
src/frames/renamedialog.h \ src/frames/renamedialog.h \
src/frames/aboutdialog.h \ src/frames/aboutdialog.h \
src/frames/createdialog.h \ src/frames/createdialog.h \
src/frames/mainwindow.h \ src/frames/mainwindow.h \
src/frames/movedialog.h \ src/frames/movedialog.h \
src/models/appconfiguration.h \
src/models/note.h \ src/models/note.h \
src/models/folder.h \ src/models/folder.h \
src/services/savemanager.h src/services/savemanager.h

View File

@@ -26,6 +26,7 @@ MainWindow::MainWindow(QWidget *parent)
ui->plainTextEdit->setFont(fixedFont); ui->plainTextEdit->setFont(fixedFont);
ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu); ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
this->savemng = new SaveManager(); this->savemng = new SaveManager();
this->cfgmng = new ConfigManager();
updateListView(); updateListView();
connect(timer, &QTimer::timeout, this, &MainWindow::save); connect(timer, &QTimer::timeout, this, &MainWindow::save);
} }
@@ -355,7 +356,10 @@ void MainWindow::markdownContentChanged()
ui->plainTextEdit->blockSignals(false); ui->plainTextEdit->blockSignals(false);
ui->contentEdit->blockSignals(false); ui->contentEdit->blockSignals(false);
ui->actionSave->setDisabled(false); ui->actionSave->setDisabled(false);
timer->start(1000); if (cfgmng->getConfiguration()->isEnableAutoSave())
{
timer->start(cfgmng->getConfiguration()->getAutoSaveDelay());
}
} }
void MainWindow::plainContentChanged() void MainWindow::plainContentChanged()
@@ -378,7 +382,10 @@ void MainWindow::plainContentChanged()
ui->plainTextEdit->blockSignals(false); ui->plainTextEdit->blockSignals(false);
ui->contentEdit->blockSignals(false); ui->contentEdit->blockSignals(false);
ui->actionSave->setDisabled(false); ui->actionSave->setDisabled(false);
timer->start(1000); if (cfgmng->getConfiguration()->isEnableAutoSave())
{
timer->start(cfgmng->getConfiguration()->getAutoSaveDelay());
}
} }
void MainWindow::updateListView() void MainWindow::updateListView()

View File

@@ -12,6 +12,7 @@
#include <QScrollBar> #include <QScrollBar>
#include "../services/savemanager.h" #include "../services/savemanager.h"
#include "../services/configmanager.h"
#define COLUMN_NAME 0 #define COLUMN_NAME 0
#define COLUMN_UUID 1 #define COLUMN_UUID 1
@@ -49,6 +50,7 @@ private slots:
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
SaveManager *savemng; SaveManager *savemng;
ConfigManager *cfgmng;
QTimer *timer; QTimer *timer;
void updateListView(); void updateListView();

View File

@@ -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;
}

View File

@@ -0,0 +1,27 @@
#ifndef APPCONFIGURATION_H
#define APPCONFIGURATION_H
#define ENABLEAUTOSAVEKEY "enable_auto_save"
#define AUTOSAVEDELAY "auto_save_delay"
#include <QJsonObject>
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

View File

@@ -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;
}

View File

@@ -0,0 +1,29 @@
#ifndef CONFIGMANAGER_H
#define CONFIGMANAGER_H
#define FILENAME "config.json"
#include <QJsonDocument>
#include <QJsonObject>
#include <QStandardPaths>
#include <QString>
#include <QDir>
#include "../models/appconfiguration.h"
class ConfigManager
{
private:
QString getSaveFilePath();
void load();
AppConfiguration *cfg;
public:
ConfigManager();
~ConfigManager();
AppConfiguration *getConfiguration();
void writeToDisk();
};
#endif // CONFIGMANAGER_H