Add configuration file
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include <QScrollBar>
|
||||
|
||||
#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();
|
||||
|
||||
43
src/models/appconfiguration.cpp
Normal file
43
src/models/appconfiguration.cpp
Normal 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;
|
||||
}
|
||||
27
src/models/appconfiguration.h
Normal file
27
src/models/appconfiguration.h
Normal 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
|
||||
62
src/services/configmanager.cpp
Normal file
62
src/services/configmanager.cpp
Normal 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;
|
||||
}
|
||||
29
src/services/configmanager.h
Normal file
29
src/services/configmanager.h
Normal 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
|
||||
Reference in New Issue
Block a user