Browse Source

MdEditor: support Attach And Insert Link action

Le Tan 7 years ago
parent
commit
33350e6bdf
4 changed files with 76 additions and 6 deletions
  1. 1 1
      src/veditwindow.h
  2. 45 3
      src/vmdeditor.cpp
  3. 27 1
      src/vnotefile.cpp
  4. 3 1
      src/vnotefile.h

+ 1 - 1
src/veditwindow.h

@@ -240,7 +240,7 @@ inline QString VEditWindow::generateTabText(int p_index, const VEditTab *p_tab)
         return "";
     }
 
-    return QString("%1.%2%3%4").arg(QString::number(p_index + c_tabSequenceBase, 10))
+    return QString("%1. %2%3%4").arg(QString::number(p_index + c_tabSequenceBase, 10))
                                .arg(file->getName())
                                .arg(file->isModifiable() ? "" : "#")
                                .arg(p_tab->isModified() ? "*" : "");

+ 45 - 3
src/vmdeditor.cpp

@@ -1884,7 +1884,16 @@ bool VMdEditor::processUrlFromMimeData(const QMimeData *p_source)
     dialog.addSelection(tr("Insert As Link"), 2);
     if (isLocalFile) {
         dialog.addSelection(tr("Insert As Relative Link"), 3);
+
+        // Attach as attachment.
+        if (m_file->getType() == FileType::Note) {
+            VNoteFile *note = static_cast<VNoteFile *>((VFile *)m_file);
+            if (-1 == note->findAttachmentByPath(url.toLocalFile(), false)) {
+                dialog.addSelection(tr("Attach And Insert Link"), 6);
+            }
+        }
     }
+
     dialog.addSelection(tr("Insert As Text"), 4);
     if (!localTextFilePath.isEmpty()) {
         dialog.addSelection(tr("Insert File Content"), 5);
@@ -1909,6 +1918,37 @@ bool VMdEditor::processUrlFromMimeData(const QMimeData *p_source)
             return true;
         }
 
+        case 6:
+        {
+            // Attach And Insert Link.
+            QString file = url.toLocalFile();
+
+            Q_ASSERT(m_file->getType() == FileType::Note);
+            VNoteFile *note = static_cast<VNoteFile *>((VFile *)m_file);
+            QString destFile;
+            if (!note->addAttachment(file, &destFile)) {
+                VUtils::showMessage(QMessageBox::Warning,
+                                    tr("Warning"),
+                                    tr("Fail to add attachment %1 for note <span style=\"%2\">%3</span>.")
+                                      .arg(file)
+                                      .arg(g_config->c_dataTextStyle)
+                                      .arg(note->getName()),
+                                    "",
+                                    QMessageBox::Ok,
+                                    QMessageBox::Ok,
+                                    this);
+                return true;
+            }
+
+            emit m_object->statusMessage(tr("1 file added as attachment"));
+
+            // Update url to point to the attachment file.
+            Q_ASSERT(!destFile.isEmpty());
+            url = QUrl::fromLocalFile(destFile);
+
+            V_FALLTHROUGH;
+        }
+
         case 3:
             // Insert As Relative link.
             relativeLink = true;
@@ -2024,9 +2064,11 @@ void VMdEditor::replaceTextWithLocalImages(QString &p_text)
         QString suffix = info.suffix();
         QScopedPointer<QTemporaryFile> tmpFile;
 
-        if (info.exists() && info.isAbsolute()) {
-            // Absolute local path.
-            srcImagePath = info.absoluteFilePath();
+        if (info.exists()) {
+            if (info.isAbsolute()) {
+                // Absolute local path.
+                srcImagePath = info.absoluteFilePath();
+            }
         } else {
             // Network path.
             QByteArray data = VDownloader::downloadSync(QUrl(imageUrl));

+ 27 - 1
src/vnotefile.cpp

@@ -235,7 +235,7 @@ bool VNoteFile::deleteInternalImages()
     return deleted == images.size();
 }
 
-bool VNoteFile::addAttachment(const QString &p_file)
+bool VNoteFile::addAttachment(const QString &p_file, QString *p_destFile)
 {
     if (p_file.isEmpty() || !QFileInfo::exists(p_file)) {
         return false;
@@ -260,6 +260,10 @@ bool VNoteFile::addAttachment(const QString &p_file)
         return false;
     }
 
+    if (p_destFile) {
+        *p_destFile = destPath;
+    }
+
     return true;
 }
 
@@ -360,6 +364,28 @@ int VNoteFile::findAttachment(const QString &p_name, bool p_caseSensitive)
     return -1;
 }
 
+int VNoteFile::findAttachmentByPath(const QString &p_file, bool p_caseSensitive)
+{
+    QFileInfo fi(p_file);
+    int idx = findAttachment(fi.fileName(), p_caseSensitive);
+    if (idx == -1) {
+        return -1;
+    }
+
+    // Check path.
+    QString attPath = QDir(fetchAttachmentFolderPath()).filePath(m_attachments[idx].m_name);
+
+    bool equal = false;
+    if (p_caseSensitive) {
+        equal = VUtils::equalPath(attPath, fi.absoluteFilePath());
+    } else {
+        equal = VUtils::equalPath(attPath.toLower(),
+                                  fi.absoluteFilePath().toLower());
+    }
+
+    return equal ? idx : -1;
+}
+
 bool VNoteFile::sortAttachments(const QVector<int> &p_sortedIdx)
 {
     V_ASSERT(m_opened);

+ 3 - 1
src/vnotefile.h

@@ -80,7 +80,7 @@ public:
     void setAttachments(const QVector<VAttachment> &p_attas);
 
     // Add @p_file as an attachment to this note.
-    bool addAttachment(const QString &p_file);
+    bool addAttachment(const QString &p_file, QString *p_destFile = NULL);
 
     // Fetch attachment folder path.
     // Will create it if it does not exist.
@@ -102,6 +102,8 @@ public:
     // -1 if not found.
     int findAttachment(const QString &p_name, bool p_caseSensitive = true);
 
+    int findAttachmentByPath(const QString &p_file, bool p_caseSensitive = true);
+
     // Rename attachment @p_oldName to @p_newName.
     bool renameAttachment(const QString &p_oldName, const QString &p_newName);