Browse Source

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 <[email protected]>
Le Tan 9 years ago
parent
commit
a73090b6d7
4 changed files with 77 additions and 3 deletions
  1. 44 3
      vfilelist.cpp
  2. 1 0
      vfilelist.h
  3. 30 0
      vmainwindow.cpp
  4. 2 0
      vmainwindow.h

+ 44 - 3
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);
+}

+ 1 - 0
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);

+ 30 - 0
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<VNotebook> &notebooks)
@@ -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();
+}

+ 2 - 0
vmainwindow.h

@@ -32,6 +32,7 @@ private slots:
     void onNewNotebookBtnClicked();
     void onDeleteNotebookBtnClicked();
     void updateNotebookComboBox(const QVector<VNotebook> &notebooks);
+    void importNoteFromFile();
 
 signals:
     void curNotebookIndexChanged(const QString &path);
@@ -59,6 +60,7 @@ private:
     QAction *editNoteAct;
     QAction *saveNoteAct;
     QAction *readNoteAct;
+    QAction *importNoteAct;
 };
 
 #endif // VMAINWINDOW_H