fix bugs when copying/cutting folders across notebooks

- Maintain tags of the destination notebook;
- Fix the notebook of the source directory;
This commit is contained in:
Le Tan 2018-08-31 20:08:45 +08:00
parent a96c23fff1
commit b139e77173
8 changed files with 90 additions and 9 deletions

View File

@ -100,9 +100,9 @@ QString VDirectory::fetchPath(const VDirectory *p_dir) const
VDirectory *parentDir = (VDirectory *)p_dir->parent(); VDirectory *parentDir = (VDirectory *)p_dir->parent();
if (parentDir) { if (parentDir) {
// Not the root directory // Not the root directory
return QDir(fetchPath(parentDir)).filePath(p_dir->getName()); return QDir(parentDir->fetchPath()).filePath(p_dir->getName());
} else { } 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); 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; qDebug() << "note" << p_file->getName() << "added to folder" << m_name;
return true; return true;
@ -402,6 +408,9 @@ bool VDirectory::addSubDirectory(VDirectory *p_dir, int p_index)
} }
p_dir->setParent(this); 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; qDebug() << "folder" << p_dir->getName() << "added to folder" << m_name;
@ -588,8 +597,6 @@ bool VDirectory::copyDirectory(VDirectory *p_destDir,
return false; return false;
} }
qDebug() << "copyDirectory:" << p_dir << "to" << destDir;
*p_targetDir = destDir; *p_targetDir = destDir;
return ret; return ret;
} }
@ -741,6 +748,32 @@ QList<QString> VDirectory::collectFiles()
return files; 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 *VDirectory::buildDirectory(const QString &p_path,
VDirectory *p_parent, VDirectory *p_parent,
QString *p_errMsg) QString *p_errMsg)

View File

@ -122,6 +122,9 @@ public:
// Return path of files in this directory recursively. // Return path of files in this directory recursively.
QList<QString> collectFiles(); QList<QString> collectFiles();
// Return tags of files in this directory recursively.
QStringList collectTags();
// Delete directory @p_dir. // Delete directory @p_dir.
static bool deleteDirectory(VDirectory *p_dir, static bool deleteDirectory(VDirectory *p_dir,
bool p_skipRecycleBin = false, bool p_skipRecycleBin = false,

View File

@ -151,7 +151,14 @@ void VEditTab::checkFileChangeOutside()
return; 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, int ret = VUtils::showMessage(QMessageBox::Information,
tr("Information"), tr("Information"),
tr("Note <span style=\"%1\">%2</span> has been modified by another program.") tr("Note <span style=\"%1\">%2</span> has been modified by another program.")

View File

@ -45,6 +45,7 @@ bool VFile::open()
QString filePath = fetchPath(); QString filePath = fetchPath();
if (!QFileInfo::exists(filePath)) { if (!QFileInfo::exists(filePath)) {
qWarning() << "file does not exist" << filePath;
return false; return false;
} }
@ -112,8 +113,15 @@ bool VFile::isInternalImageFolder(const QString &p_path) const
|| VUtils::equalPath(p_path, fetchImageFolderPath()); || 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(); QDateTime lm = QFileInfo(fetchPath()).lastModified();
return lm.toSecsSinceEpoch() != m_lastModified.toSecsSinceEpoch(); return lm.toSecsSinceEpoch() != m_lastModified.toSecsSinceEpoch();
} }

View File

@ -77,7 +77,7 @@ public:
QDateTime getModifiedTimeUtc() const; QDateTime getModifiedTimeUtc() const;
// Whether this file was changed outside VNote. // 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. // Return backup file of previous session if there exists one.
QString backupFileOfPreviousSession() const; QString backupFileOfPreviousSession() const;

View File

@ -443,6 +443,27 @@ bool VNotebook::addTag(const QString &p_tag)
return true; 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) void VNotebook::removeTag(const QString &p_tag)
{ {
if (p_tag.isEmpty() || m_tags.isEmpty()) { if (p_tag.isEmpty() || m_tags.isEmpty()) {

View File

@ -60,6 +60,9 @@ public:
bool addTag(const QString &p_tag); 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); void removeTag(const QString &p_tag);
bool hasTag(const QString &p_tag) const; bool hasTag(const QString &p_tag) const;

View File

@ -513,6 +513,14 @@ bool VNoteFile::copyFile(VDirectory *p_destDir,
} }
} else { } else {
destFile = p_destDir->addFile(p_destName, -1); 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) { if (!destFile) {
@ -636,8 +644,6 @@ void VNoteFile::removeTag(const QString &p_tag)
bool VNoteFile::addTag(const QString &p_tag) bool VNoteFile::addTag(const QString &p_tag)
{ {
Q_ASSERT(isOpened());
if (p_tag.isEmpty() || hasTag(p_tag)) { if (p_tag.isEmpty() || hasTag(p_tag)) {
return false; return false;
} }