diff --git a/src/veditwindow.h b/src/veditwindow.h index 7ed482e7..85c270be 100644 --- a/src/veditwindow.h +++ b/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() ? "*" : ""); diff --git a/src/vmdeditor.cpp b/src/vmdeditor.cpp index 35708377..1594ee13 100644 --- a/src/vmdeditor.cpp +++ b/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((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((VFile *)m_file); + QString destFile; + if (!note->addAttachment(file, &destFile)) { + VUtils::showMessage(QMessageBox::Warning, + tr("Warning"), + tr("Fail to add attachment %1 for note %3.") + .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 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)); diff --git a/src/vnotefile.cpp b/src/vnotefile.cpp index 355f7a24..f80bf4bc 100644 --- a/src/vnotefile.cpp +++ b/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 &p_sortedIdx) { V_ASSERT(m_opened); diff --git a/src/vnotefile.h b/src/vnotefile.h index 6e0b3076..56ed9178 100644 --- a/src/vnotefile.h +++ b/src/vnotefile.h @@ -80,7 +80,7 @@ public: void setAttachments(const QVector &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);