mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 05:49:53 +08:00
move config logics to VConfigManager
Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
parent
5d08a6f93d
commit
0f7f433833
@ -17,13 +17,15 @@ SOURCES += main.cpp\
|
||||
vdirectorytree.cpp \
|
||||
vnote.cpp \
|
||||
vnotebook.cpp \
|
||||
vnewdirdialog.cpp
|
||||
vnewdirdialog.cpp \
|
||||
vconfigmanager.cpp
|
||||
|
||||
HEADERS += vmainwindow.h \
|
||||
vdirectorytree.h \
|
||||
vnote.h \
|
||||
vnotebook.h \
|
||||
vnewdirdialog.h
|
||||
vnewdirdialog.h \
|
||||
vconfigmanager.h
|
||||
|
||||
RESOURCES += \
|
||||
vnote.qrc
|
||||
|
62
vconfigmanager.cpp
Normal file
62
vconfigmanager.cpp
Normal file
@ -0,0 +1,62 @@
|
||||
#include "vconfigmanager.h"
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QString>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
#include <QtDebug>
|
||||
|
||||
const QString VConfigManager::dirConfigFileName = QString(".vnote.json");
|
||||
|
||||
VConfigManager::VConfigManager()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
QJsonObject VConfigManager::readDirectoryConfig(const QString &path)
|
||||
{
|
||||
QString configFile = QDir(path).filePath(dirConfigFileName);
|
||||
|
||||
qDebug() << "read config file:" << configFile;
|
||||
QFile config(configFile);
|
||||
if (!config.open(QIODevice::ReadOnly)) {
|
||||
qWarning() << "error: fail to read directory configuration file:"
|
||||
<< configFile;
|
||||
return QJsonObject();
|
||||
}
|
||||
|
||||
QByteArray configData = config.readAll();
|
||||
return QJsonDocument::fromJson(configData).object();
|
||||
}
|
||||
|
||||
bool VConfigManager::writeDirectoryConfig(const QString &path, const QJsonObject &configJson)
|
||||
{
|
||||
QString configFile = QDir(path).filePath(dirConfigFileName);
|
||||
|
||||
qDebug() << "write config file:" << configFile;
|
||||
QFile config(configFile);
|
||||
if (!config.open(QIODevice::WriteOnly)) {
|
||||
qWarning() << "error: fail to open directory configuration file for write:"
|
||||
<< configFile;
|
||||
return false;
|
||||
}
|
||||
|
||||
QJsonDocument configDoc(configJson);
|
||||
config.write(configDoc.toJson());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool VConfigManager::deleteDirectoryConfig(const QString &path)
|
||||
{
|
||||
QString configFile = QDir(path).filePath(dirConfigFileName);
|
||||
|
||||
QFile config(configFile);
|
||||
if (!config.remove()) {
|
||||
qWarning() << "error: fail to delete directory configuration file:"
|
||||
<< configFile;
|
||||
return false;
|
||||
}
|
||||
qDebug() << "delete config file:" << configFile;
|
||||
return true;
|
||||
}
|
22
vconfigmanager.h
Normal file
22
vconfigmanager.h
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef VCONFIGMANAGER_H
|
||||
#define VCONFIGMANAGER_H
|
||||
|
||||
class QJsonObject;
|
||||
class QString;
|
||||
|
||||
class VConfigManager
|
||||
{
|
||||
public:
|
||||
VConfigManager();
|
||||
|
||||
// Read config from the directory config json file into a QJsonObject
|
||||
static QJsonObject readDirectoryConfig(const QString &path);
|
||||
static bool writeDirectoryConfig(const QString &path, const QJsonObject &configJson);
|
||||
static bool deleteDirectoryConfig(const QString &path);
|
||||
|
||||
private:
|
||||
// The name of the config file in each directory
|
||||
static const QString dirConfigFileName;
|
||||
};
|
||||
|
||||
#endif // VCONFIGMANAGER_H
|
@ -2,9 +2,10 @@
|
||||
#include <QJsonObject>
|
||||
#include "vdirectorytree.h"
|
||||
#include "vnewdirdialog.h"
|
||||
#include "vconfigmanager.h"
|
||||
|
||||
VDirectoryTree::VDirectoryTree(const QString &dirConfigFileName, QWidget *parent)
|
||||
: QTreeWidget(parent), dirConfigFileName(dirConfigFileName)
|
||||
VDirectoryTree::VDirectoryTree(QWidget *parent)
|
||||
: QTreeWidget(parent)
|
||||
{
|
||||
setColumnCount(1);
|
||||
setHeaderHidden(true);
|
||||
@ -54,14 +55,7 @@ void VDirectoryTree::setTreePath(const QString& path)
|
||||
|
||||
bool VDirectoryTree::validatePath(const QString &path)
|
||||
{
|
||||
QDir dir(path);
|
||||
if (!dir.exists()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QString configFile = dir.filePath(dirConfigFileName);
|
||||
QFileInfo fileInfo(configFile);
|
||||
return fileInfo.exists() && fileInfo.isFile();
|
||||
return QDir(path).exists();
|
||||
}
|
||||
|
||||
void VDirectoryTree::updateDirectoryTree()
|
||||
@ -141,8 +135,8 @@ void VDirectoryTree::updateDirectoryTreeTopLevel()
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonObject configJson = readDirectoryConfig(path);
|
||||
if (!validateDirConfigFile(configJson)) {
|
||||
QJsonObject configJson = VConfigManager::readDirectoryConfig(path);
|
||||
if (configJson.isEmpty()) {
|
||||
qDebug() << "invalid notebook configuration for path:" << path;
|
||||
QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), tr("Invalid notebook configuration."));
|
||||
msgBox.setInformativeText(QString("Notebook path \"%1\" does not contain a valid configuration file.")
|
||||
@ -182,8 +176,8 @@ void VDirectoryTree::updateDirectoryTreeOne(QTreeWidgetItem &parent, int depth)
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonObject configJson = readDirectoryConfig(path);
|
||||
if (!validateDirConfigFile(configJson)) {
|
||||
QJsonObject configJson = VConfigManager::readDirectoryConfig(path);
|
||||
if (configJson.isEmpty()) {
|
||||
qDebug() << "invalid notebook configuration for directory:" << path;
|
||||
QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), tr("Invalid notebook directory configuration."));
|
||||
msgBox.setInformativeText(QString("Notebook path \"%1\" does not contain a valid configuration file.")
|
||||
@ -205,73 +199,6 @@ void VDirectoryTree::updateDirectoryTreeOne(QTreeWidgetItem &parent, int depth)
|
||||
}
|
||||
}
|
||||
|
||||
QJsonObject VDirectoryTree::readDirectoryConfig(const QString &path)
|
||||
{
|
||||
QString configFile = QDir(path).filePath(dirConfigFileName);
|
||||
|
||||
qDebug() << "read config file:" << configFile;
|
||||
QFile config(configFile);
|
||||
if (!config.open(QIODevice::ReadOnly)) {
|
||||
qWarning() << "error: fail to read directory configuration file:"
|
||||
<< configFile;
|
||||
QMessageBox msgBox(QMessageBox::Warning, tr("Warning"),
|
||||
QString("Could not read directory configuration file \"%1\"")
|
||||
.arg(dirConfigFileName));
|
||||
msgBox.setInformativeText(QString("Notebook directory \"%1\" may be corrupted").arg(path));
|
||||
msgBox.exec();
|
||||
return QJsonObject();
|
||||
}
|
||||
|
||||
QByteArray configData = config.readAll();
|
||||
return QJsonDocument::fromJson(configData).object();
|
||||
}
|
||||
|
||||
bool VDirectoryTree::writeDirectoryConfig(const QString &path, const QJsonObject &configJson)
|
||||
{
|
||||
QString configFile = QDir(path).filePath(dirConfigFileName);
|
||||
|
||||
qDebug() << "write config file:" << configFile;
|
||||
QFile config(configFile);
|
||||
if (!config.open(QIODevice::WriteOnly)) {
|
||||
qWarning() << "error: fail to open directory configuration file for write:"
|
||||
<< configFile;
|
||||
QMessageBox msgBox(QMessageBox::Warning, tr("Warning"),
|
||||
QString("Could not write directory configuration file \"%1\"")
|
||||
.arg(dirConfigFileName));
|
||||
msgBox.exec();
|
||||
return false;
|
||||
}
|
||||
|
||||
QJsonDocument configDoc(configJson);
|
||||
config.write(configDoc.toJson());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool VDirectoryTree::deleteDirectoryConfig(const QString &path)
|
||||
{
|
||||
QString configFile = QDir(path).filePath(dirConfigFileName);
|
||||
|
||||
QFile config(configFile);
|
||||
if (!config.remove()) {
|
||||
qWarning() << "error: fail to delete directory configuration file:"
|
||||
<< configFile;
|
||||
return false;
|
||||
}
|
||||
qDebug() << "delete config file:" << configFile;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool VDirectoryTree::validateDirConfigFile(const QJsonObject &configJson)
|
||||
{
|
||||
if (configJson.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
if (!configJson.contains("version") || !configJson.contains("name")) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void VDirectoryTree::updateItemSubtree(QTreeWidgetItem *item)
|
||||
{
|
||||
QJsonObject itemJson = item->data(0, Qt::UserRole).toJsonObject();
|
||||
@ -443,20 +370,21 @@ QTreeWidgetItem* VDirectoryTree::createDirectoryAndUpdateTree(QTreeWidgetItem *p
|
||||
configJson["sub_directories"] = QJsonArray();
|
||||
configJson["files"] = QJsonArray();
|
||||
|
||||
if (!writeDirectoryConfig(QDir(path).filePath(name), configJson)) {
|
||||
if (!VConfigManager::writeDirectoryConfig(QDir(path).filePath(name), configJson)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Update parent's config file to include this new directory
|
||||
configJson = readDirectoryConfig(path);
|
||||
configJson = VConfigManager::readDirectoryConfig(path);
|
||||
Q_ASSERT(!configJson.isEmpty());
|
||||
QJsonObject itemJson;
|
||||
itemJson["name"] = name;
|
||||
itemJson["description"] = description;
|
||||
QJsonArray subDirArray = configJson["sub_directories"].toArray();
|
||||
subDirArray.append(itemJson);
|
||||
configJson["sub_directories"] = subDirArray;
|
||||
if (!writeDirectoryConfig(path, configJson)) {
|
||||
deleteDirectoryConfig(QDir(path).filePath(name));
|
||||
if (!VConfigManager::writeDirectoryConfig(path, configJson)) {
|
||||
VConfigManager::deleteDirectoryConfig(QDir(path).filePath(name));
|
||||
dir.rmdir(name);
|
||||
return NULL;
|
||||
}
|
||||
@ -472,7 +400,8 @@ void VDirectoryTree::deleteDirectoryAndUpdateTree(QTreeWidgetItem *item)
|
||||
|
||||
// Update parent's config file to exclude this directory
|
||||
QString path = QDir(treePath).filePath(relativePath);
|
||||
QJsonObject configJson = readDirectoryConfig(path);
|
||||
QJsonObject configJson = VConfigManager::readDirectoryConfig(path);
|
||||
Q_ASSERT(!configJson.isEmpty());
|
||||
QJsonArray subDirArray = configJson["sub_directories"].toArray();
|
||||
bool deleted = false;
|
||||
for (int i = 0; i < subDirArray.size(); ++i) {
|
||||
@ -488,7 +417,7 @@ void VDirectoryTree::deleteDirectoryAndUpdateTree(QTreeWidgetItem *item)
|
||||
return;
|
||||
}
|
||||
configJson["sub_directories"] = subDirArray;
|
||||
if (!writeDirectoryConfig(path, configJson)) {
|
||||
if (!VConfigManager::writeDirectoryConfig(path, configJson)) {
|
||||
qWarning() << "error: fail to update parent's configuration file to delete" << itemName;
|
||||
return;
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ class VDirectoryTree : public QTreeWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
VDirectoryTree(const QString &dirConfigFileName, QWidget *parent = 0);
|
||||
VDirectoryTree(QWidget *parent = 0);
|
||||
|
||||
signals:
|
||||
|
||||
@ -38,8 +38,6 @@ private:
|
||||
void updateDirectoryTreeOne(QTreeWidgetItem &parent, int depth);
|
||||
// Validate if a directory is valid
|
||||
bool validatePath(const QString &path);
|
||||
// Validate if a directory config file is valid
|
||||
bool validateDirConfigFile(const QJsonObject &configJson);
|
||||
// Fill the QTreeWidgetItem according to its QJsonObject.
|
||||
// @relative_path is the path related to treePath.
|
||||
void fillDirectoryTreeItem(QTreeWidgetItem &item, QJsonObject itemJson, const QString &relativePath);
|
||||
@ -49,18 +47,12 @@ private:
|
||||
void deleteDirectoryAndUpdateTree(QTreeWidgetItem *item);
|
||||
// If @name conflict with the children's names of @parent.
|
||||
bool isConflictNameWithChildren(const QTreeWidgetItem *parent, const QString &name);
|
||||
// Read config from the directory config json file into a QJsonObject
|
||||
QJsonObject readDirectoryConfig(const QString &path);
|
||||
bool writeDirectoryConfig(const QString &path, const QJsonObject &configJson);
|
||||
bool deleteDirectoryConfig(const QString &path);
|
||||
QTreeWidgetItem* insertDirectoryTreeItem(QTreeWidgetItem *parent, QTreeWidgetItem *preceding,
|
||||
const QJsonObject &newItem);
|
||||
void removeDirectoryTreeItem(QTreeWidgetItem *item);
|
||||
|
||||
// The path of the directory tree root
|
||||
QString treePath;
|
||||
// The name of the config file in each subdirectory
|
||||
QString dirConfigFileName;
|
||||
|
||||
// Actions
|
||||
QAction *newRootDirAct;
|
||||
|
@ -23,7 +23,7 @@ void VMainWindow::setupUI()
|
||||
// Notebook directory browser tree
|
||||
notebookLabel = new QLabel(tr("Notebook"));
|
||||
notebookComboBox = new QComboBox();
|
||||
directoryTree = new VDirectoryTree(VNote::dirConfigFileName);
|
||||
directoryTree = new VDirectoryTree();
|
||||
|
||||
QHBoxLayout *nbTopLayout = new QHBoxLayout;
|
||||
notebookComboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
||||
|
@ -4,7 +4,6 @@
|
||||
|
||||
const QString VNote::orgName = QString("tamlok");
|
||||
const QString VNote::appName = QString("VNote");
|
||||
const QString VNote::dirConfigFileName = QString(".vnote.json");
|
||||
|
||||
VNote::VNote()
|
||||
: curNotebookIndex(0)
|
||||
|
Loading…
x
Reference in New Issue
Block a user