diff --git a/src/dialog/vdeletenotebookdialog.cpp b/src/dialog/vdeletenotebookdialog.cpp index 4fba5f30..6a6c9eb6 100644 --- a/src/dialog/vdeletenotebookdialog.cpp +++ b/src/dialog/vdeletenotebookdialog.cpp @@ -111,7 +111,8 @@ void VDeleteNotebookDialog::notDeleteCheckChanged(int p_state) .arg(vconfig.c_dataTextStyle).arg(m_path)); } else { m_warningLabel->setText(tr("WARNING: " - "VNote will delete ANY files under directory %3! " + "VNote may delete ANY files under directory %3! " + "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)); } diff --git a/src/vnotebook.cpp b/src/vnotebook.cpp index 5aa1a53e..9b52afbd 100644 --- a/src/vnotebook.cpp +++ b/src/vnotebook.cpp @@ -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 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) diff --git a/src/vnotebook.h b/src/vnotebook.h index 96216f6a..51a8ddd4 100644 --- a/src/vnotebook.h +++ b/src/vnotebook.h @@ -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(); diff --git a/src/vnotebookselector.cpp b/src/vnotebookselector.cpp index 3a80ce1f..e91ef85c 100644 --- a/src/vnotebookselector.cpp +++ b/src/vnotebookselector.cpp @@ -7,6 +7,8 @@ #include #include #include +#include +#include #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 " + "%2 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)