From b139e7717345c0ee29b267210a8ef4e487f37966 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Fri, 31 Aug 2018 20:08:45 +0800 Subject: [PATCH] fix bugs when copying/cutting folders across notebooks - Maintain tags of the destination notebook; - Fix the notebook of the source directory; --- src/vdirectory.cpp | 41 +++++++++++++++++++++++++++++++++++++---- src/vdirectory.h | 3 +++ src/vedittab.cpp | 9 ++++++++- src/vfile.cpp | 10 +++++++++- src/vfile.h | 2 +- src/vnotebook.cpp | 21 +++++++++++++++++++++ src/vnotebook.h | 3 +++ src/vnotefile.cpp | 10 ++++++++-- 8 files changed, 90 insertions(+), 9 deletions(-) diff --git a/src/vdirectory.cpp b/src/vdirectory.cpp index 82f3321c..1f52c468 100644 --- a/src/vdirectory.cpp +++ b/src/vdirectory.cpp @@ -100,9 +100,9 @@ QString VDirectory::fetchPath(const VDirectory *p_dir) const VDirectory *parentDir = (VDirectory *)p_dir->parent(); if (parentDir) { // Not the root directory - return QDir(fetchPath(parentDir)).filePath(p_dir->getName()); + return QDir(parentDir->fetchPath()).filePath(p_dir->getName()); } else { - return m_notebook->getPath(); + return p_dir->getNotebook()->getPath(); } } @@ -349,6 +349,12 @@ bool VDirectory::addFile(VNoteFile *p_file, int p_index) p_file->setParent(this); + // Add tags from this file to the notebook. + const QStringList &tags = p_file->getTags(); + for (auto const & tag : tags) { + m_notebook->addTag(tag); + } + qDebug() << "note" << p_file->getName() << "added to folder" << m_name; return true; @@ -402,6 +408,9 @@ bool VDirectory::addSubDirectory(VDirectory *p_dir, int p_index) } p_dir->setParent(this); + p_dir->m_notebook = m_notebook; + + m_notebook->addTags(p_dir); qDebug() << "folder" << p_dir->getName() << "added to folder" << m_name; @@ -588,8 +597,6 @@ bool VDirectory::copyDirectory(VDirectory *p_destDir, return false; } - qDebug() << "copyDirectory:" << p_dir << "to" << destDir; - *p_targetDir = destDir; return ret; } @@ -741,6 +748,32 @@ QList VDirectory::collectFiles() return files; } +QStringList VDirectory::collectTags() +{ + QStringList tags; + bool opened = isOpened(); + if (!opened && !open()) { + qWarning() << "fail to open directory" << fetchPath(); + return tags; + } + + // Files. + for (auto const & file : m_files) { + tags.append(file->getTags()); + } + + // Subfolders. + for (auto const & dir : m_subDirs) { + tags.append(dir->collectTags()); + } + + if (!opened) { + close(); + } + + return tags; +} + VDirectory *VDirectory::buildDirectory(const QString &p_path, VDirectory *p_parent, QString *p_errMsg) diff --git a/src/vdirectory.h b/src/vdirectory.h index 52a9700f..adec155d 100644 --- a/src/vdirectory.h +++ b/src/vdirectory.h @@ -122,6 +122,9 @@ public: // Return path of files in this directory recursively. QList collectFiles(); + // Return tags of files in this directory recursively. + QStringList collectTags(); + // Delete directory @p_dir. static bool deleteDirectory(VDirectory *p_dir, bool p_skipRecycleBin = false, diff --git a/src/vedittab.cpp b/src/vedittab.cpp index 54d708e3..8acbc3c0 100644 --- a/src/vedittab.cpp +++ b/src/vedittab.cpp @@ -151,7 +151,14 @@ void VEditTab::checkFileChangeOutside() return; } - if (m_file->isChangedOutside()) { + bool missing = false; + if (m_file->isChangedOutside(missing)) { + // It may be caused by cutting files. + if (missing) { + qWarning() << "file is missing when check file's outside change" << m_file->fetchPath(); + return; + } + int ret = VUtils::showMessage(QMessageBox::Information, tr("Information"), tr("Note %2 has been modified by another program.") diff --git a/src/vfile.cpp b/src/vfile.cpp index 5bdc4deb..1e1f476b 100644 --- a/src/vfile.cpp +++ b/src/vfile.cpp @@ -45,6 +45,7 @@ bool VFile::open() QString filePath = fetchPath(); if (!QFileInfo::exists(filePath)) { + qWarning() << "file does not exist" << filePath; return false; } @@ -112,8 +113,15 @@ bool VFile::isInternalImageFolder(const QString &p_path) const || VUtils::equalPath(p_path, fetchImageFolderPath()); } -bool VFile::isChangedOutside() const +bool VFile::isChangedOutside(bool &p_missing) const { + QFileInfo fi(fetchPath()); + if (!fi.exists()) { + p_missing = true; + return true; + } + + p_missing = false; QDateTime lm = QFileInfo(fetchPath()).lastModified(); return lm.toSecsSinceEpoch() != m_lastModified.toSecsSinceEpoch(); } diff --git a/src/vfile.h b/src/vfile.h index b1cbb36e..19a40a05 100644 --- a/src/vfile.h +++ b/src/vfile.h @@ -77,7 +77,7 @@ public: QDateTime getModifiedTimeUtc() const; // Whether this file was changed outside VNote. - bool isChangedOutside() const; + bool isChangedOutside(bool &p_missing) const; // Return backup file of previous session if there exists one. QString backupFileOfPreviousSession() const; diff --git a/src/vnotebook.cpp b/src/vnotebook.cpp index 11b5736e..1caada89 100644 --- a/src/vnotebook.cpp +++ b/src/vnotebook.cpp @@ -443,6 +443,27 @@ bool VNotebook::addTag(const QString &p_tag) return true; } +bool VNotebook::addTags(VDirectory *p_dir) +{ + QStringList tags = p_dir->collectTags(); + + for (auto const & tag : tags) { + if (tag.isEmpty() || hasTag(tag)) { + continue; + } + + m_tags.append(tag); + } + + if (!writeConfigNotebook()) { + qWarning() << "fail to update config of notebook" << m_name + << "in directory" << m_path; + return false; + } + + return true; +} + void VNotebook::removeTag(const QString &p_tag) { if (p_tag.isEmpty() || m_tags.isEmpty()) { diff --git a/src/vnotebook.h b/src/vnotebook.h index 891367bf..5c2ea43a 100644 --- a/src/vnotebook.h +++ b/src/vnotebook.h @@ -60,6 +60,9 @@ public: bool addTag(const QString &p_tag); + // Walk through @p_dir recursively to add all tags to notebook. + bool addTags(VDirectory *p_dir); + void removeTag(const QString &p_tag); bool hasTag(const QString &p_tag) const; diff --git a/src/vnotefile.cpp b/src/vnotefile.cpp index 9981a543..e8b7c4c2 100644 --- a/src/vnotefile.cpp +++ b/src/vnotefile.cpp @@ -513,6 +513,14 @@ bool VNoteFile::copyFile(VDirectory *p_destDir, } } else { destFile = p_destDir->addFile(p_destName, -1); + // Copy tags to this file. + if (destFile) { + const QStringList &tags = p_file->getTags(); + for (auto const & tag : tags) { + destFile->addTag(tag); + destFile->getNotebook()->addTag(tag); + } + } } if (!destFile) { @@ -636,8 +644,6 @@ void VNoteFile::removeTag(const QString &p_tag) bool VNoteFile::addTag(const QString &p_tag) { - Q_ASSERT(isOpened()); - if (p_tag.isEmpty() || hasTag(p_tag)) { return false; }