From 58e8f71dda1eaae9dcd211d7461aa26a860cf9ff Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Mon, 22 Jul 2024 18:04:25 +0400 Subject: [PATCH] Moved directory monitoring to ContentManager --- src/contentmanager.cpp | 21 ++++++++++++--------- src/contentmanager.h | 7 +++++++ src/kiwixapp.cpp | 7 ++++--- src/library.h | 5 ----- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index 031f374..cbdda20 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -850,20 +850,22 @@ void ContentManager::setSortBy(const QString& sortBy, const bool sortOrderAsc) emit(booksChanged()); } -void Library::setMonitorDirZims(QString monitorDir, QStringSet zimList) +void ContentManager::setMonitorDirZims(QString monitorDir, Library::QStringSet zimList) { m_knownZimsInDir[monitorDir] = zimList; } -void Library::asyncUpdateFromDir(QString dir) +void ContentManager::asyncUpdateLibraryFromDir(QString dir) { (void) QtConcurrent::run([=]() { - updateFromDir(dir); + updateLibraryFromDir(dir); }); } -void Library::updateFromDir(QString monitorDir) +void ContentManager::updateLibraryFromDir(QString monitorDir) { + typedef Library::QStringSet QStringSet; + QMutexLocker locker(&m_updateFromDirMutex); const QDir dir(monitorDir); const QStringSet oldDirEntries = m_knownZimsInDir[monitorDir]; @@ -873,11 +875,12 @@ void Library::updateFromDir(QString monitorDir) } const QStringSet addedZims = newDirEntries - oldDirEntries; const QStringSet removedZims = oldDirEntries - newDirEntries; - kiwix::Manager manager(getKiwixLibrary()); + const auto kiwixLib = mp_library->getKiwixLibrary(); + kiwix::Manager manager(kiwixLib); bool needsRefresh = !removedZims.empty(); for (auto bookPath : addedZims) { - if ( isBeingDownloadedByUs(bookPath) ) { - // qDebug() << "DBG: Library::updateFromDir(): " + if ( mp_library->isBeingDownloadedByUs(bookPath) ) { + // qDebug() << "DBG: ContentManager::updateLibraryFromDir(): " // << bookPath // << " ignored since it is being downloaded by us."; } else { @@ -886,8 +889,8 @@ void Library::updateFromDir(QString monitorDir) } for (auto bookPath : removedZims) { try { - const auto book = mp_library->getBookByPath(bookPath.toStdString()); - removeBookFromLibraryById(QString::fromStdString(book.getId())); + const auto book = kiwixLib->getBookByPath(bookPath.toStdString()); + mp_library->removeBookFromLibraryById(QString::fromStdString(book.getId())); } catch (...) {} } if (needsRefresh) { diff --git a/src/contentmanager.h b/src/contentmanager.h index 4569715..4e31de9 100644 --- a/src/contentmanager.h +++ b/src/contentmanager.h @@ -69,6 +69,9 @@ public: // functions QStringList getCategories() const { return m_categories; } LanguageList getLanguages() const { return m_languages; } + void setMonitorDirZims(QString monitorDir, Library::QStringSet zimList); + void asyncUpdateLibraryFromDir(QString dir); + signals: void filterParamsChanged(); void booksChanged(); @@ -113,6 +116,7 @@ private: // functions void updateModel(); void setCategories(); void setLanguages(); + void updateLibraryFromDir(QString dir); // Get the book with the specified id from // the remote or local library (in that order). @@ -141,6 +145,9 @@ private: // data ContentManagerModel *managerModel; QMutex remoteLibraryLocker; + + QMutex m_updateFromDirMutex; + QMap m_knownZimsInDir; }; #endif // CONTENTMANAGER_H diff --git a/src/kiwixapp.cpp b/src/kiwixapp.cpp index 65f0f81..878824f 100644 --- a/src/kiwixapp.cpp +++ b/src/kiwixapp.cpp @@ -103,7 +103,7 @@ void KiwixApp::init() } }); connect(&m_watcher, &QFileSystemWatcher::directoryChanged, this, [=](QString monitorDir) { - m_library.asyncUpdateFromDir(monitorDir); + mp_manager->asyncUpdateLibraryFromDir(monitorDir); }); setupDirectoryMonitoring(); @@ -119,9 +119,10 @@ void KiwixApp::setupDirectoryMonitoring() auto dirList = QSet({monitorDir, downloadDir}); for (auto dir : dirList) { if (dir != "") { - m_library.setMonitorDirZims(dir, m_library.getLibraryZimsFromDir(dir)); + const auto zimsInDir = m_library.getLibraryZimsFromDir(dir); + mp_manager->setMonitorDirZims(dir, zimsInDir); m_watcher.addPath(dir); - m_library.asyncUpdateFromDir(dir); + mp_manager->asyncUpdateLibraryFromDir(dir); } } } diff --git a/src/library.h b/src/library.h index 3644b08..0ca75fb 100644 --- a/src/library.h +++ b/src/library.h @@ -37,7 +37,6 @@ public: QStringList listBookIds(const kiwix::Filter& filter, kiwix::supportedListSortBy sortBy, bool ascending) const; const std::vector getBookmarks(bool onlyValidBookmarks = false) const { return mp_library->getBookmarks(onlyValidBookmarks); } QStringSet getLibraryZimsFromDir(QString dir) const; - void setMonitorDirZims(QString monitorDir, QStringSet zimList); void addBookToLibrary(kiwix::Book& book); void addBookBeingDownloaded(const kiwix::Book& book, QString downloadDir); bool isBeingDownloadedByUs(QString path) const; @@ -47,8 +46,6 @@ public: void addBookmark(kiwix::Bookmark& bookmark); void removeBookmark(const QString& zimId, const QString& url); void save(); - void updateFromDir(QString dir); - void asyncUpdateFromDir(QString dir); kiwix::LibraryPtr getKiwixLibrary() { return mp_library; } public slots: const kiwix::Book& getBookById(QString id) const; @@ -58,10 +55,8 @@ signals: void bookmarksChanged(); private: - QMutex m_updateFromDirMutex; kiwix::LibraryPtr mp_library; QString m_libraryDirectory; - QMap m_knownZimsInDir; friend class LibraryManipulator; };