do not allow changing type of note

This commit is contained in:
Le Tan 2017-09-13 21:50:41 +08:00
parent 1ae30b645e
commit a36b512e94
8 changed files with 15 additions and 77 deletions

View File

@ -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("<span style=\"%1\">WARNING</span>: 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);
}
}

View File

@ -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()) {

View File

@ -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;

View File

@ -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);

View File

@ -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 <span style=\"%1\">%2</span> 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();

View File

@ -91,13 +91,6 @@ private:
bool identicalListWithDirectory() const;
QList<QListWidgetItem *> 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);

View File

@ -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);

View File

@ -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;