From 354a4d1f8a5d1daa7f4446d470afa1619d5c694f Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Tue, 30 Jul 2024 16:55:54 +0400 Subject: [PATCH] Directory monitoring works via file names instead of paths - ContentManager::m_knownZimFiles[dirPath] now stores filenames (rather than full paths) of known ZIM files in directory at dirPath. - Library::getLibraryZimsFromDir() returns a set of file names (rather than paths) Note that the change of the semantics of ContentManager::m_knownZimFiles has been carried out via the change in the value of the second argument of ContentManager::setMonitorDirZims(): 1. In KiwixApp::setupDirectoryMonitoring() the latter is fed with the output of (the now changed) Library::getLibraryZimsFromDir() 2. In ContentManager::updateLibraryFromDir() all variables representing a set of files now contain filenames only (note that ContentManager::handleNewZimFiles() returns just a subset of its second parameter) and therefore produce a set of filenames. --- src/contentmanager.cpp | 18 ++++++++++-------- src/contentmanager.h | 4 ++-- src/library.cpp | 6 +++--- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index ec21e47..589a932 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -864,10 +864,11 @@ void ContentManager::asyncUpdateLibraryFromDir(QString dir) }); } -void ContentManager::handleDisappearedZimFiles(const QStringSet& zimPaths) +void ContentManager::handleDisappearedZimFiles(const QString& dirPath, const QStringSet& fileNames) { const auto kiwixLib = mp_library->getKiwixLibrary(); - for (auto bookPath : zimPaths) { + for (const auto& file : fileNames) { + const auto bookPath = QDir::toNativeSeparators(dirPath + "/" + file); try { DBGOUT("directory monitoring: file disappeared: " << bookPath); const auto book = kiwixLib->getBookByPath(bookPath.toStdString()); @@ -879,18 +880,19 @@ void ContentManager::handleDisappearedZimFiles(const QStringSet& zimPaths) } } -ContentManager::QStringSet ContentManager::handleNewZimFiles(const QStringSet& zimPaths) +ContentManager::QStringSet ContentManager::handleNewZimFiles(const QString& dirPath, const QStringSet& fileNames) { QStringSet successfullyAddedZims; const auto kiwixLib = mp_library->getKiwixLibrary(); kiwix::Manager manager(kiwixLib); - for (auto bookPath : zimPaths) { + for (const auto& file : fileNames) { + const auto bookPath = QDir::toNativeSeparators(dirPath + "/" + file); DBGOUT("directory monitoring: file appeared: " << bookPath); if ( mp_library->isBeingDownloadedByUs(bookPath) ) { DBGOUT(" it is being downloaded by us, ignoring..."); } else if ( manager.addBookFromPath(bookPath.toStdString()) ) { DBGOUT(" and was added to the library"); - successfullyAddedZims.insert(bookPath); + successfullyAddedZims.insert(file); } else { DBGOUT(" but could not be added to the library"); } @@ -906,13 +908,13 @@ void ContentManager::updateLibraryFromDir(QString dirPath) QStringSet zimsInDir; for (const auto &file : dir.entryList({"*.zim"})) { - zimsInDir.insert(QDir::toNativeSeparators(dirPath + "/" + file)); + zimsInDir.insert(file); } const QStringSet zimsNotInLib = zimsInDir - zimsPresentInLib; const QStringSet removedZims = zimsPresentInLib - zimsInDir; - handleDisappearedZimFiles(removedZims); - const auto successfullyAddedZims = handleNewZimFiles(zimsNotInLib); + handleDisappearedZimFiles(dirPath, removedZims); + const auto successfullyAddedZims = handleNewZimFiles(dirPath, zimsNotInLib); if (!removedZims.empty() || !successfullyAddedZims.empty()) { mp_library->save(); emit(booksChanged()); diff --git a/src/contentmanager.h b/src/contentmanager.h index c43eaa7..e2c9c7f 100644 --- a/src/contentmanager.h +++ b/src/contentmanager.h @@ -118,8 +118,8 @@ private: // functions void setCategories(); void setLanguages(); void updateLibraryFromDir(QString dir); - void handleDisappearedZimFiles(const QStringSet& zimPaths); - QStringSet handleNewZimFiles(const QStringSet& zimPaths); + void handleDisappearedZimFiles(const QString& dirPath, const QStringSet& fileNames); + QStringSet handleNewZimFiles(const QString& dirPath, const QStringSet& fileNames); void handleDisappearedZimFile(QString bookId); // Get the book with the specified id from diff --git a/src/library.cpp b/src/library.cpp index 8dd1763..f28a1b0 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -201,9 +201,9 @@ Library::QStringSet Library::getLibraryZimsFromDir(QString dir) const auto filePath = QString::fromStdString(getBookById(str).getPath()); if ( filePath.endsWith(BEINGDOWNLOADEDSUFFIX) ) continue; - QDir absoluteDir = QFileInfo(filePath).absoluteDir(); - if (absoluteDir == dir) { - zimsInDir.insert(filePath); + const QFileInfo fileInfo(filePath); + if (fileInfo.absoluteDir() == dir) { + zimsInDir.insert(fileInfo.fileName()); } } return zimsInDir;