From a43c53bf3d12dd52c7cfdb4a21be688a6387d310 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Sun, 25 Aug 2024 17:46:08 +0400 Subject: [PATCH] Enter MonitoredZimFileInfo::ZimFileStatus At this point the new enum serves mainly for separating logging from the logic. But it enables to enhance the logic with more elaborate handling. --- src/contentmanager.cpp | 36 +++++++++++++++++++++++++++++------- src/contentmanager.h | 17 +++++++++++++++++ 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index 99818b0..7b92a8f 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -906,27 +906,49 @@ size_t ContentManager::handleNewZimFiles(const QString& dirPath, const QStringSe { size_t countOfSuccessfullyAddedZims = 0; for (const auto& file : fileNames) { - const bool addedToLib = handleZimFileInMonitoredDir(dirPath, file); + const bool addedToLib = handleZimFileInMonitoredDirLogged(dirPath, file); countOfSuccessfullyAddedZims += addedToLib; } return countOfSuccessfullyAddedZims; } +namespace +{ + +#ifndef QT_NO_DEBUG + +// indexed by MonitoredZimFileInfo::ZimFileStatus enum +const char* monitoredDirZimFileHandlingMsgs[] = { + "it is being downloaded by us, ignoring...", + "the file was added to the library", + "the file could not be added to the library" +}; + +#endif + +} // unnamed namespace + +bool ContentManager::handleZimFileInMonitoredDirLogged(QString dir, QString fileName) +{ + DBGOUT("ContentManager::handleZimFileInMonitoredDir(" << dir << ", " << fileName << ")"); + const int status = handleZimFileInMonitoredDir(dir, fileName); + DBGOUT("\t" << monitoredDirZimFileHandlingMsgs[status]); + return status == MonitoredZimFileInfo::ADDED_TO_THE_LIBRARY; +} + int ContentManager::handleZimFileInMonitoredDir(QString dir, QString fileName) { const auto bookPath = QDir::toNativeSeparators(dir + "/" + fileName); kiwix::Manager manager(mp_library->getKiwixLibrary()); - DBGOUT("directory monitoring: file appeared: " << bookPath); + if ( mp_library->isBeingDownloadedByUs(bookPath) ) { - DBGOUT(" it is being downloaded by us, ignoring..."); + return MonitoredZimFileInfo::BEING_DOWNLOADED_BY_US; } else if ( manager.addBookFromPath(bookPath.toStdString()) ) { - DBGOUT(" and was added to the library"); m_knownZimsInDir[dir].insert(fileName); - return 1; + return MonitoredZimFileInfo::ADDED_TO_THE_LIBRARY; } else { - DBGOUT(" but could not be added to the library"); + return MonitoredZimFileInfo::COULD_NOT_BE_ADDED_TO_THE_LIBRARY; } - return 0; } void ContentManager::updateLibraryFromDir(QString dirPath) diff --git a/src/contentmanager.h b/src/contentmanager.h index f692adb..e3b6e02 100644 --- a/src/contentmanager.h +++ b/src/contentmanager.h @@ -108,6 +108,22 @@ public slots: void downloadWasCancelled(const QString& id); void handleError(QString errSummary, QString errDetails); +private: // types + struct MonitoredZimFileInfo + { + enum ZimFileStatus + { + // the file is known to be downloaded by our own download manager + BEING_DOWNLOADED_BY_US, + + // the file was added the library successfully + ADDED_TO_THE_LIBRARY, + + // the attempt to add the file to the library failed + COULD_NOT_BE_ADDED_TO_THE_LIBRARY + }; + }; + private: // functions QStringList getBookIds(); // reallyEraseBook() doesn't ask for confirmation (unlike eraseBook()) @@ -120,6 +136,7 @@ private: // functions void updateLibraryFromDir(QString dir); void handleDisappearedZimFiles(const QString& dirPath, const QStringSet& fileNames); size_t handleNewZimFiles(const QString& dirPath, const QStringSet& fileNames); + bool handleZimFileInMonitoredDirLogged(QString dirPath, QString fileName); int handleZimFileInMonitoredDir(QString dirPath, QString fileName); bool handleDisappearedBook(QString bookId);