From f31272966ec5b52848dd7dedbb6bd4b1b4d31d49 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Mon, 17 Oct 2016 21:17:52 +0800 Subject: [PATCH] support deleting notebook Signed-off-by: Le Tan --- vmainwindow.cpp | 18 ++++++++++++++++++ vmainwindow.h | 1 + vnote.cpp | 29 +++++++++++++++++++++++++++++ vnote.h | 1 + 4 files changed, 49 insertions(+) diff --git a/vmainwindow.cpp b/vmainwindow.cpp index d4db6eba..18ed152a 100644 --- a/vmainwindow.cpp +++ b/vmainwindow.cpp @@ -86,6 +86,8 @@ void VMainWindow::setupUI() tabs, &VTabWidget::openFile); connect(newNotebookBtn, &QPushButton::clicked, this, &VMainWindow::onNewNotebookBtnClicked); + connect(deleteNotebookBtn, &QPushButton::clicked, + this, &VMainWindow::onDeleteNotebookBtnClicked); connect(vnote, &VNote::notebooksChanged, this, &VMainWindow::updateNotebookComboBox); @@ -194,3 +196,19 @@ bool VMainWindow::isConflictWithExistingNotebooks(const QString &name, const QSt } return false; } + +void VMainWindow::onDeleteNotebookBtnClicked() +{ + int curIndex = notebookComboBox->currentIndex(); + QString curName = vnote->getNotebooks()[curIndex].getName(); + QString curPath = vnote->getNotebooks()[curIndex].getPath(); + + QMessageBox msgBox(QMessageBox::Warning, tr("Warning"), QString("Are you sure you want to delete notebook \"%1\"?") + .arg(curName)); + msgBox.setInformativeText(QString("This will delete any files in this notebook (\"%1\").").arg(curPath)); + msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel); + msgBox.setDefaultButton(QMessageBox::Ok); + if (msgBox.exec() == QMessageBox::Ok) { + vnote->removeNotebook(curName); + } +} diff --git a/vmainwindow.h b/vmainwindow.h index 5fe44cd2..4c6e4dc6 100644 --- a/vmainwindow.h +++ b/vmainwindow.h @@ -30,6 +30,7 @@ private slots: void setCurNotebookIndex(int index); // Create a notebook void onNewNotebookBtnClicked(); + void onDeleteNotebookBtnClicked(); void updateNotebookComboBox(const QVector ¬ebooks); signals: diff --git a/vnote.cpp b/vnote.cpp index b9b187f7..f77b2690 100644 --- a/vnote.cpp +++ b/vnote.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "vnote.h" #include "utils/vutils.h" #include "vconfigmanager.h" @@ -50,3 +51,31 @@ void VNote::createNotebook(const QString &name, const QString &path) emit notebooksChanged(notebooks); } + +void VNote::removeNotebook(const QString &name) +{ + // Update notebooks settings + QString path; + int index; + for (index = 0; index < notebooks.size(); ++index) { + if (notebooks[index].getName() == name) { + path = notebooks[index].getPath(); + break; + } + } + if (index == notebooks.size()) { + return; + } + notebooks.remove(index); + vconfig.setNotebooks(notebooks); + vconfig.setCurNotebookIndex(notebooks.isEmpty() ? -1 : 0); + + // Delete the directory + QDir dir(path); + if (!dir.removeRecursively()) { + qWarning() << "error: fail to delete" << path << "recursively"; + } else { + qDebug() << "delete" << path << "recursively"; + } + emit notebooksChanged(notebooks); +} diff --git a/vnote.h b/vnote.h index 9d2b8898..2f38ca3b 100644 --- a/vnote.h +++ b/vnote.h @@ -21,6 +21,7 @@ public: static QString templateHtml; void createNotebook(const QString &name, const QString &path); + void removeNotebook(const QString &name); signals: void notebooksChanged(const QVector ¬ebooks);