move config logics to VConfigManager

Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
Le Tan 2016-10-05 17:13:40 +08:00
parent 5d08a6f93d
commit 0f7f433833
8 changed files with 106 additions and 102 deletions

View File

@ -17,13 +17,15 @@ SOURCES += main.cpp\
vdirectorytree.cpp \ vdirectorytree.cpp \
vnote.cpp \ vnote.cpp \
vnotebook.cpp \ vnotebook.cpp \
vnewdirdialog.cpp vnewdirdialog.cpp \
vconfigmanager.cpp
HEADERS += vmainwindow.h \ HEADERS += vmainwindow.h \
vdirectorytree.h \ vdirectorytree.h \
vnote.h \ vnote.h \
vnotebook.h \ vnotebook.h \
vnewdirdialog.h vnewdirdialog.h \
vconfigmanager.h
RESOURCES += \ RESOURCES += \
vnote.qrc vnote.qrc

62
vconfigmanager.cpp Normal file
View 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
View 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

View File

@ -2,9 +2,10 @@
#include <QJsonObject> #include <QJsonObject>
#include "vdirectorytree.h" #include "vdirectorytree.h"
#include "vnewdirdialog.h" #include "vnewdirdialog.h"
#include "vconfigmanager.h"
VDirectoryTree::VDirectoryTree(const QString &dirConfigFileName, QWidget *parent) VDirectoryTree::VDirectoryTree(QWidget *parent)
: QTreeWidget(parent), dirConfigFileName(dirConfigFileName) : QTreeWidget(parent)
{ {
setColumnCount(1); setColumnCount(1);
setHeaderHidden(true); setHeaderHidden(true);
@ -54,14 +55,7 @@ void VDirectoryTree::setTreePath(const QString& path)
bool VDirectoryTree::validatePath(const QString &path) bool VDirectoryTree::validatePath(const QString &path)
{ {
QDir dir(path); return QDir(path).exists();
if (!dir.exists()) {
return false;
}
QString configFile = dir.filePath(dirConfigFileName);
QFileInfo fileInfo(configFile);
return fileInfo.exists() && fileInfo.isFile();
} }
void VDirectoryTree::updateDirectoryTree() void VDirectoryTree::updateDirectoryTree()
@ -141,8 +135,8 @@ void VDirectoryTree::updateDirectoryTreeTopLevel()
return; return;
} }
QJsonObject configJson = readDirectoryConfig(path); QJsonObject configJson = VConfigManager::readDirectoryConfig(path);
if (!validateDirConfigFile(configJson)) { if (configJson.isEmpty()) {
qDebug() << "invalid notebook configuration for path:" << path; qDebug() << "invalid notebook configuration for path:" << path;
QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), tr("Invalid notebook configuration.")); QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), tr("Invalid notebook configuration."));
msgBox.setInformativeText(QString("Notebook path \"%1\" does not contain a valid configuration file.") 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; return;
} }
QJsonObject configJson = readDirectoryConfig(path); QJsonObject configJson = VConfigManager::readDirectoryConfig(path);
if (!validateDirConfigFile(configJson)) { if (configJson.isEmpty()) {
qDebug() << "invalid notebook configuration for directory:" << path; qDebug() << "invalid notebook configuration for directory:" << path;
QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), tr("Invalid notebook directory configuration.")); QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), tr("Invalid notebook directory configuration."));
msgBox.setInformativeText(QString("Notebook path \"%1\" does not contain a valid configuration file.") 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) void VDirectoryTree::updateItemSubtree(QTreeWidgetItem *item)
{ {
QJsonObject itemJson = item->data(0, Qt::UserRole).toJsonObject(); QJsonObject itemJson = item->data(0, Qt::UserRole).toJsonObject();
@ -443,20 +370,21 @@ QTreeWidgetItem* VDirectoryTree::createDirectoryAndUpdateTree(QTreeWidgetItem *p
configJson["sub_directories"] = QJsonArray(); configJson["sub_directories"] = QJsonArray();
configJson["files"] = QJsonArray(); configJson["files"] = QJsonArray();
if (!writeDirectoryConfig(QDir(path).filePath(name), configJson)) { if (!VConfigManager::writeDirectoryConfig(QDir(path).filePath(name), configJson)) {
return NULL; return NULL;
} }
// Update parent's config file to include this new directory // Update parent's config file to include this new directory
configJson = readDirectoryConfig(path); configJson = VConfigManager::readDirectoryConfig(path);
Q_ASSERT(!configJson.isEmpty());
QJsonObject itemJson; QJsonObject itemJson;
itemJson["name"] = name; itemJson["name"] = name;
itemJson["description"] = description; itemJson["description"] = description;
QJsonArray subDirArray = configJson["sub_directories"].toArray(); QJsonArray subDirArray = configJson["sub_directories"].toArray();
subDirArray.append(itemJson); subDirArray.append(itemJson);
configJson["sub_directories"] = subDirArray; configJson["sub_directories"] = subDirArray;
if (!writeDirectoryConfig(path, configJson)) { if (!VConfigManager::writeDirectoryConfig(path, configJson)) {
deleteDirectoryConfig(QDir(path).filePath(name)); VConfigManager::deleteDirectoryConfig(QDir(path).filePath(name));
dir.rmdir(name); dir.rmdir(name);
return NULL; return NULL;
} }
@ -472,7 +400,8 @@ void VDirectoryTree::deleteDirectoryAndUpdateTree(QTreeWidgetItem *item)
// Update parent's config file to exclude this directory // Update parent's config file to exclude this directory
QString path = QDir(treePath).filePath(relativePath); 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(); QJsonArray subDirArray = configJson["sub_directories"].toArray();
bool deleted = false; bool deleted = false;
for (int i = 0; i < subDirArray.size(); ++i) { for (int i = 0; i < subDirArray.size(); ++i) {
@ -488,7 +417,7 @@ void VDirectoryTree::deleteDirectoryAndUpdateTree(QTreeWidgetItem *item)
return; return;
} }
configJson["sub_directories"] = subDirArray; 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; qWarning() << "error: fail to update parent's configuration file to delete" << itemName;
return; return;
} }

View File

@ -9,7 +9,7 @@ class VDirectoryTree : public QTreeWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
VDirectoryTree(const QString &dirConfigFileName, QWidget *parent = 0); VDirectoryTree(QWidget *parent = 0);
signals: signals:
@ -38,8 +38,6 @@ private:
void updateDirectoryTreeOne(QTreeWidgetItem &parent, int depth); void updateDirectoryTreeOne(QTreeWidgetItem &parent, int depth);
// Validate if a directory is valid // Validate if a directory is valid
bool validatePath(const QString &path); 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. // Fill the QTreeWidgetItem according to its QJsonObject.
// @relative_path is the path related to treePath. // @relative_path is the path related to treePath.
void fillDirectoryTreeItem(QTreeWidgetItem &item, QJsonObject itemJson, const QString &relativePath); void fillDirectoryTreeItem(QTreeWidgetItem &item, QJsonObject itemJson, const QString &relativePath);
@ -49,18 +47,12 @@ private:
void deleteDirectoryAndUpdateTree(QTreeWidgetItem *item); void deleteDirectoryAndUpdateTree(QTreeWidgetItem *item);
// If @name conflict with the children's names of @parent. // If @name conflict with the children's names of @parent.
bool isConflictNameWithChildren(const QTreeWidgetItem *parent, const QString &name); 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, QTreeWidgetItem* insertDirectoryTreeItem(QTreeWidgetItem *parent, QTreeWidgetItem *preceding,
const QJsonObject &newItem); const QJsonObject &newItem);
void removeDirectoryTreeItem(QTreeWidgetItem *item); void removeDirectoryTreeItem(QTreeWidgetItem *item);
// The path of the directory tree root // The path of the directory tree root
QString treePath; QString treePath;
// The name of the config file in each subdirectory
QString dirConfigFileName;
// Actions // Actions
QAction *newRootDirAct; QAction *newRootDirAct;

View File

@ -23,7 +23,7 @@ void VMainWindow::setupUI()
// Notebook directory browser tree // Notebook directory browser tree
notebookLabel = new QLabel(tr("Notebook")); notebookLabel = new QLabel(tr("Notebook"));
notebookComboBox = new QComboBox(); notebookComboBox = new QComboBox();
directoryTree = new VDirectoryTree(VNote::dirConfigFileName); directoryTree = new VDirectoryTree();
QHBoxLayout *nbTopLayout = new QHBoxLayout; QHBoxLayout *nbTopLayout = new QHBoxLayout;
notebookComboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); notebookComboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

View File

@ -4,7 +4,6 @@
const QString VNote::orgName = QString("tamlok"); const QString VNote::orgName = QString("tamlok");
const QString VNote::appName = QString("VNote"); const QString VNote::appName = QString("VNote");
const QString VNote::dirConfigFileName = QString(".vnote.json");
VNote::VNote() VNote::VNote()
: curNotebookIndex(0) : curNotebookIndex(0)

View File

@ -17,8 +17,6 @@ public:
int getCurNotebookIndex() const; int getCurNotebookIndex() const;
void setCurNotebookIndex(int index); void setCurNotebookIndex(int index);
// The name of the config file in each subdirectory
static const QString dirConfigFileName;
static const QString orgName; static const QString orgName;
static const QString appName; static const QString appName;