From a73090b6d7526e334ee64a43dd4ca74ce5c38300 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Mon, 17 Oct 2016 22:53:26 +0800 Subject: [PATCH] support importing notes from files Add menu action importNoteAct to import notes from files. Currently only HTML and Markdown files are supported. Importing other files (such as txt) may not work correctly. Signed-off-by: Le Tan --- vfilelist.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++--- vfilelist.h | 1 + vmainwindow.cpp | 30 ++++++++++++++++++++++++++++++ vmainwindow.h | 2 ++ 4 files changed, 77 insertions(+), 3 deletions(-) diff --git a/vfilelist.cpp b/vfilelist.cpp index d9b02e71..7e36b0d1 100644 --- a/vfilelist.cpp +++ b/vfilelist.cpp @@ -20,8 +20,8 @@ void VFileList::initActions() { newFileAct = new QAction(tr("&New note"), this); newFileAct->setStatusTip(tr("Create a new note in current directory")); - connect(newFileAct, &QAction::triggered, - this, &VFileList::newFile); + connect(newFileAct, SIGNAL(triggered(bool)), + this, SLOT(newFile())); deleteFileAct = new QAction(tr("&Delete"), this); deleteFileAct->setStatusTip(tr("Delete selected note")); @@ -248,7 +248,6 @@ void VFileList::deleteFileAndUpdateList(QListWidgetItem *item) return; } - // Delete the file QFile file(filePath); if (!file.remove()) { @@ -272,3 +271,45 @@ void VFileList::handleItemClicked(QListWidgetItem *currentItem) qDebug() << "click file:" << itemJson; emit fileClicked(itemJson); } + +bool VFileList::importFile(const QString &name) +{ + if (name.isEmpty()) { + return false; + } + if (isConflictNameWithExisting(name)) { + return false; + } + + // Copy file @name to current directory + QString targetPath = QDir(rootPath).filePath(relativePath); + QString srcName = QFileInfo(name).fileName(); + if (srcName.isEmpty()) { + return false; + } + QString targetName = QDir(targetPath).filePath(srcName); + + bool ret = QFile::copy(name, targetName); + if (!ret) { + qWarning() << "error: fail to copy" << name << "to" << targetName; + return false; + } + + // Update current directory's config file to include this new file + QJsonObject dirJson = VConfigManager::readDirectoryConfig(targetPath); + Q_ASSERT(!dirJson.isEmpty()); + QJsonObject fileJson; + fileJson["name"] = srcName; + fileJson["description"] = ""; + QJsonArray fileArray = dirJson["files"].toArray(); + fileArray.push_front(fileJson); + dirJson["files"] = fileArray; + if (!VConfigManager::writeDirectoryConfig(targetPath, dirJson)) { + qWarning() << "error: fail to update directory's configuration file to add a new file" + << srcName; + QFile(targetName).remove(); + return false; + } + + return insertFileListItem(fileJson, true); +} diff --git a/vfilelist.h b/vfilelist.h index 1bff3687..eac91bd6 100644 --- a/vfilelist.h +++ b/vfilelist.h @@ -11,6 +11,7 @@ class VFileList : public QListWidget Q_OBJECT public: explicit VFileList(QWidget *parent = 0); + bool importFile(const QString &name); signals: void fileClicked(QJsonObject fileJson); diff --git a/vmainwindow.cpp b/vmainwindow.cpp index 18ed152a..4bf78350 100644 --- a/vmainwindow.cpp +++ b/vmainwindow.cpp @@ -112,6 +112,11 @@ void VMainWindow::initActions() saveNoteAct->setStatusTip(tr("Save current note")); connect(saveNoteAct, &QAction::triggered, tabs, &VTabWidget::saveFile); + + importNoteAct = new QAction(tr("&Import note from file"), this); + importNoteAct->setStatusTip(tr("Import notes into current directory from files")); + connect(importNoteAct, &QAction::triggered, + this, &VMainWindow::importNoteFromFile); } void VMainWindow::initToolBar() @@ -129,6 +134,7 @@ void VMainWindow::initMenuBar() QMenu *helpMenu = menuBar()->addMenu(tr("&Help")); // To be implemented + fileMenu->addAction(importNoteAct); } void VMainWindow::updateNotebookComboBox(const QVector ¬ebooks) @@ -212,3 +218,27 @@ void VMainWindow::onDeleteNotebookBtnClicked() vnote->removeNotebook(curName); } } + +void VMainWindow::importNoteFromFile() +{ + QStringList files = QFileDialog::getOpenFileNames(this,tr("Select files(HTML or Markdown) to be imported as notes"), + QDir::homePath()); + if (files.isEmpty()) { + return; + } + QStringList failedFiles; + for (int i = 0; i < files.size(); ++i) { + bool ret = fileList->importFile(files[i]); + if (!ret) { + failedFiles.append(files[i]); + } + } + QMessageBox msgBox(QMessageBox::Information, tr("Import note from file"), + QString("Imported notes: %1 succeed, %2 failed.") + .arg(files.size() - failedFiles.size()).arg(failedFiles.size())); + if (!failedFiles.isEmpty()) { + msgBox.setInformativeText(tr("Failed to import files may be due to name conflicts.")); + } + msgBox.setStandardButtons(QMessageBox::Ok); + msgBox.exec(); +} diff --git a/vmainwindow.h b/vmainwindow.h index 4c6e4dc6..85d889f6 100644 --- a/vmainwindow.h +++ b/vmainwindow.h @@ -32,6 +32,7 @@ private slots: void onNewNotebookBtnClicked(); void onDeleteNotebookBtnClicked(); void updateNotebookComboBox(const QVector ¬ebooks); + void importNoteFromFile(); signals: void curNotebookIndexChanged(const QString &path); @@ -59,6 +60,7 @@ private: QAction *editNoteAct; QAction *saveNoteAct; QAction *readNoteAct; + QAction *importNoteAct; }; #endif // VMAINWINDOW_H