delete notebook by deleting root directories

This commit is contained in:
Le Tan 2017-05-03 22:50:34 +08:00
parent d330bdae0f
commit 27b0d99965
4 changed files with 62 additions and 14 deletions

View File

@ -111,7 +111,8 @@ void VDeleteNotebookDialog::notDeleteCheckChanged(int p_state)
.arg(vconfig.c_dataTextStyle).arg(m_path));
} else {
m_warningLabel->setText(tr("<span style=\"%1\">WARNING</span>: "
"VNote will delete <b>ANY</b> files under directory <span style=\"%2\">%3</span>! "
"VNote may delete <b>ANY</b> files under directory <span style=\"%2\">%3</span>! "
"VNote will try to delete all the root directories within this notebook one by one. "
"It may be UNRECOVERABLE!")
.arg(vconfig.c_warningTextStyle).arg(vconfig.c_dataTextStyle).arg(m_path));
}

View File

@ -60,22 +60,48 @@ VNotebook *VNotebook::createNotebook(const QString &p_name, const QString &p_pat
return nb;
}
void VNotebook::deleteNotebook(VNotebook *p_notebook, bool p_deleteFiles)
bool VNotebook::deleteNotebook(VNotebook *p_notebook, bool p_deleteFiles)
{
if (!p_notebook) {
return;
}
QString path = p_notebook->getPath();
bool ret = true;
if (!p_notebook) {
return true;
}
if (p_deleteFiles) {
if (!p_notebook->open()) {
qWarning() << "fail to open notebook" << p_notebook->getName()
<< "to delete";
ret = false;
goto exit;
}
VDirectory *rootDir = p_notebook->getRootDir();
QVector<VDirectory *> subdirs = rootDir->getSubDirs();
for (auto dir : subdirs) {
rootDir->deleteSubDirectory(dir);
}
// Delete the config file.
if (!VConfigManager::deleteDirectoryConfig(p_notebook->getPath())) {
ret = false;
goto exit;
}
// If it is now an empty directory, delete it.
QDir dir(p_notebook->getPath());
dir.cdUp();
if (!dir.rmdir(rootDir->getName())) {
qWarning() << "fail to delete notebook root directory" << rootDir->getName();
ret = false;
}
}
exit:
p_notebook->close();
delete p_notebook;
if (p_deleteFiles) {
QDir dir(path);
if (!dir.removeRecursively()) {
qWarning() << "fail to delete" << path;
}
}
return ret;
}
void VNotebook::rename(const QString &p_name)

View File

@ -28,7 +28,7 @@ public:
static VNotebook *createNotebook(const QString &p_name, const QString &p_path, bool p_import,
QObject *p_parent = 0);
static void deleteNotebook(VNotebook *p_notebook, bool p_deleteFiles);
static bool deleteNotebook(VNotebook *p_notebook, bool p_deleteFiles);
signals:
void contentChanged();

View File

@ -7,6 +7,8 @@
#include <QGuiApplication>
#include <QScreen>
#include <QLabel>
#include <QDesktopServices>
#include <QUrl>
#include "vnotebook.h"
#include "vconfigmanager.h"
#include "dialog/vnewnotebookdialog.h"
@ -230,6 +232,8 @@ void VNotebookSelector::deleteNotebook()
void VNotebookSelector::deleteNotebook(VNotebook *p_notebook, bool p_deleteFiles)
{
V_ASSERT(p_notebook);
int idx = indexOfNotebook(p_notebook);
m_notebooks.remove(idx);
@ -237,7 +241,24 @@ void VNotebookSelector::deleteNotebook(VNotebook *p_notebook, bool p_deleteFiles
removeNotebookItem(idx);
VNotebook::deleteNotebook(p_notebook, p_deleteFiles);
QString name(p_notebook->getName());
QString path(p_notebook->getPath());
bool ret = VNotebook::deleteNotebook(p_notebook, p_deleteFiles);
if (!ret) {
// Notebook could not be deleted completely.
int cho = VUtils::showMessage(QMessageBox::Information, tr("Delete Notebook Folder From Disk"),
tr("Fail to delete the root folder of notebook "
"<span style=\"%1\">%2</span> from disk. You may open "
"the directory and check it manually.")
.arg(vconfig.c_dataTextStyle).arg(name), "",
QMessageBox::Open | QMessageBox::Ok,
QMessageBox::Ok, this);
if (cho == QMessageBox::Open) {
// Open the notebook location.
QUrl url = QUrl::fromLocalFile(path);
QDesktopServices::openUrl(url);
}
}
}
int VNotebookSelector::indexOfNotebook(const VNotebook *p_notebook)