From a36b512e94ead84432ba3bb09facab64793716b3 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Wed, 13 Sep 2017 21:50:41 +0800 Subject: [PATCH] do not allow changing type of note --- src/dialog/vfileinfodialog.cpp | 9 +++++++++ src/vdirectory.cpp | 4 +--- src/vfile.cpp | 29 ++-------------------------- src/vfile.h | 2 -- src/vfilelist.cpp | 35 +++------------------------------- src/vfilelist.h | 7 ------- src/vorphanfile.cpp | 5 ----- src/vorphanfile.h | 1 - 8 files changed, 15 insertions(+), 77 deletions(-) diff --git a/src/dialog/vfileinfodialog.cpp b/src/dialog/vfileinfodialog.cpp index b7087770..51ffe3b0 100644 --- a/src/dialog/vfileinfodialog.cpp +++ b/src/dialog/vfileinfodialog.cpp @@ -3,6 +3,7 @@ #include "vdirectory.h" #include "vfile.h" #include "vconfigmanager.h" +#include "utils/vutils.h" extern VConfigManager *g_config; @@ -74,6 +75,14 @@ void VFileInfoDialog::handleInputChanged() "Please choose another name.") .arg(g_config->c_warningTextStyle); m_warnLabel->setText(nameConflictText); + } else if (VUtils::docTypeFromName(name) != m_file->getDocType()) { + // Check if the name change the doc type. + nameOk = false; + showWarnLabel = true; + QString nameConflictText = tr("WARNING: Changing type of the note is not supported. " + "Please use the same suffix as the old one.") + .arg(g_config->c_warningTextStyle); + m_warnLabel->setText(nameConflictText); } } diff --git a/src/vdirectory.cpp b/src/vdirectory.cpp index 378f3e80..d9d05015 100644 --- a/src/vdirectory.cpp +++ b/src/vdirectory.cpp @@ -528,9 +528,7 @@ VFile *VDirectory::copyFile(VDirectory *p_destDir, const QString &p_destName, return NULL; } - if (docType != newDocType) { - destFile->convert(docType, newDocType); - } + Q_ASSERT(docType == newDocType); // We need to copy internal images when it is still markdown. if (!images.isEmpty()) { diff --git a/src/vfile.cpp b/src/vfile.cpp index d30c0c81..f22106e5 100644 --- a/src/vfile.cpp +++ b/src/vfile.cpp @@ -69,27 +69,6 @@ bool VFile::save() return ret; } -void VFile::convert(DocType p_curType, DocType p_targetType) -{ - Q_ASSERT(!m_opened); - m_docType = p_targetType; - if (p_curType == p_targetType) { - return; - } - QString path = fetchPath(); - QString fileText = VUtils::readFileFromDisk(path); - QTextEdit editor; - if (p_curType == DocType::Markdown) { - editor.setPlainText(fileText); - fileText = editor.toHtml(); - } else { - editor.setHtml(fileText); - fileText = editor.toPlainText(); - } - VUtils::writeFileToDisk(path, fileText); - qDebug() << getName() << "converted" << (int)p_curType << (int)p_targetType; -} - void VFile::setModified(bool p_modified) { m_modified = p_modified; @@ -264,12 +243,8 @@ bool VFile::rename(const QString &p_name) return false; } - // Handle DocType change. - DocType newType = VUtils::docTypeFromName(m_name); - if (m_docType != newType) { - convert(m_docType, newType); - m_docType = newType; - } + // Can't not change doc type. + Q_ASSERT(m_docType == VUtils::docTypeFromName(m_name)); qDebug() << "note renamed from" << oldName << "to" << m_name; diff --git a/src/vfile.h b/src/vfile.h index 3186be97..d1717e03 100644 --- a/src/vfile.h +++ b/src/vfile.h @@ -19,8 +19,6 @@ public: virtual bool open(); virtual void close(); virtual bool save(); - // Convert current file type. - virtual void convert(DocType p_curType, DocType p_targetType); const QString &getName() const; virtual void setName(const QString &p_name); diff --git a/src/vfilelist.cpp b/src/vfilelist.cpp index 78ef715e..615b56a0 100644 --- a/src/vfilelist.cpp +++ b/src/vfilelist.cpp @@ -198,9 +198,7 @@ void VFileList::fileInfo(VFile *p_file) return; } - if (!promptForDocTypeChange(p_file, QDir(p_file->fetchBasePath()).filePath(name))) { - return; - } + Q_ASSERT(p_file->getDocType() == VUtils::docTypeFromName(name)); if (!p_file->rename(name)) { VUtils::showMessage(QMessageBox::Warning, tr("Warning"), @@ -579,10 +577,8 @@ bool VFileList::copyFile(VDirectory *p_destDir, const QString &p_destName, VFile return true; } - // If change the file type, we need to close it first - if (!promptForDocTypeChange(p_file, destPath)) { - return false; - } + // DocType is not allowed to change. + Q_ASSERT(p_file->getDocType() == VUtils::docTypeFromName(destPath)); VFile *destFile = VDirectory::copyFile(p_destDir, p_destName, p_file, p_cut); updateFileList(); @@ -592,31 +588,6 @@ bool VFileList::copyFile(VDirectory *p_destDir, const QString &p_destName, VFile return destFile != NULL; } -bool VFileList::promptForDocTypeChange(const VFile *p_file, const QString &p_newFilePath) -{ - DocType docType = p_file->getDocType(); - DocType newDocType = VUtils::docTypeFromName(p_newFilePath); - - if (docType != newDocType) { - if (editArea->isFileOpened(p_file)) { - int ret = VUtils::showMessage(QMessageBox::Warning, tr("Warning"), - tr("The renaming will change the note type."), - tr("You should close the note %2 before continue.") - .arg(g_config->c_dataTextStyle).arg(p_file->getName()), - QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok, this); - if (QMessageBox::Ok == ret) { - if (!editArea->closeFile(p_file, false)) { - return false; - } - } else { - return false; - } - } - } - - return true; -} - void VFileList::keyPressEvent(QKeyEvent *event) { int key = event->key(); diff --git a/src/vfilelist.h b/src/vfilelist.h index 04727821..6460176d 100644 --- a/src/vfilelist.h +++ b/src/vfilelist.h @@ -91,13 +91,6 @@ private: bool identicalListWithDirectory() const; QList getVisibleItems() const; - // @p_file: the file to be renamed or copied. - // @p_newFilePath: the new file path of @p_file. - // Check if the rename/copy will change the DocType. If yes, then ask - // user for confirmation. - // Return true if we can continue. - bool promptForDocTypeChange(const VFile *p_file, const QString &p_newFilePath); - // Fill the info of @p_item according to @p_file. void fillItem(QListWidgetItem *p_item, const VFile *p_file); diff --git a/src/vorphanfile.cpp b/src/vorphanfile.cpp index 33d10748..0d864091 100644 --- a/src/vorphanfile.cpp +++ b/src/vorphanfile.cpp @@ -69,11 +69,6 @@ bool VOrphanFile::save() return VUtils::writeFileToDisk(fetchPath(), m_content); } -void VOrphanFile::convert(DocType /* p_curType */, DocType /* p_targetType */) -{ - V_ASSERT(false); -} - void VOrphanFile::setName(const QString & /* p_name */) { V_ASSERT(false); diff --git a/src/vorphanfile.h b/src/vorphanfile.h index 7f3e8b7f..4deb3a8e 100644 --- a/src/vorphanfile.h +++ b/src/vorphanfile.h @@ -40,7 +40,6 @@ public: private: bool save() Q_DECL_OVERRIDE; - void convert(DocType p_curType, DocType p_targetType) Q_DECL_OVERRIDE; void setName(const QString &p_name) Q_DECL_OVERRIDE; QString fetchImagePath() const Q_DECL_OVERRIDE; void setContent(const QString &p_content) Q_DECL_OVERRIDE;