From d5d5987fc19c03b019cd543ef4b181195176e834 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Mon, 29 Jul 2024 16:19:04 +0400 Subject: [PATCH] ContentManager::handleNewZimFiles() --- src/contentmanager.cpp | 40 +++++++++++++++++++++------------------- src/contentmanager.h | 1 + 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index b611631..0e13700 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -879,10 +879,27 @@ void ContentManager::handleDisappearedZimFiles(const QStringSet& zimPaths) } } +ContentManager::QStringSet ContentManager::handleNewZimFiles(const QStringSet& zimPaths) +{ + QStringSet successfullyAddedZims; + const auto kiwixLib = mp_library->getKiwixLibrary(); + kiwix::Manager manager(kiwixLib); + for (auto bookPath : zimPaths) { + 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); + } else { + DBGOUT(" but could not be added to the library"); + } + } + return successfullyAddedZims; +} + void ContentManager::updateLibraryFromDir(QString monitorDir) { - typedef Library::QStringSet QStringSet; - QMutexLocker locker(&m_updateFromDirMutex); const QDir dir(monitorDir); const QStringSet oldDirEntries = m_knownZimsInDir[monitorDir]; @@ -892,24 +909,9 @@ void ContentManager::updateLibraryFromDir(QString monitorDir) } const QStringSet addedZims = newDirEntries - oldDirEntries; const QStringSet removedZims = oldDirEntries - newDirEntries; - const auto kiwixLib = mp_library->getKiwixLibrary(); - kiwix::Manager manager(kiwixLib); - bool needsRefresh = !removedZims.empty(); handleDisappearedZimFiles(removedZims); - for (auto bookPath : addedZims) { - if ( mp_library->isBeingDownloadedByUs(bookPath) ) { - DBGOUT("directory monitoring: " << bookPath - << " ignored since it is being downloaded by us."); - } else { - DBGOUT("directory monitoring: file appeared: " << bookPath); - const bool added = manager.addBookFromPath(bookPath.toStdString()); - DBGOUT(" " - << (added ? "and was added" : "but could not be added") - << " to the library"); - needsRefresh |= added; - } - } - if (needsRefresh) { + const auto successfullyAddedZims = handleNewZimFiles(addedZims); + if (!removedZims.empty() || !successfullyAddedZims.empty()) { mp_library->save(); emit(booksChanged()); setMonitorDirZims(monitorDir, newDirEntries); diff --git a/src/contentmanager.h b/src/contentmanager.h index b25a266..c43eaa7 100644 --- a/src/contentmanager.h +++ b/src/contentmanager.h @@ -119,6 +119,7 @@ private: // functions void setLanguages(); void updateLibraryFromDir(QString dir); void handleDisappearedZimFiles(const QStringSet& zimPaths); + QStringSet handleNewZimFiles(const QStringSet& zimPaths); void handleDisappearedZimFile(QString bookId); // Get the book with the specified id from