mirror of
https://github.com/kiwix/kiwix-desktop.git
synced 2025-08-03 20:56:49 -04:00
Moved directory monitoring to ContentManager
This commit is contained in:
parent
52583ad6a9
commit
58e8f71dda
@ -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) {
|
||||
|
@ -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<QString, Library::QStringSet> m_knownZimsInDir;
|
||||
};
|
||||
|
||||
#endif // CONTENTMANAGER_H
|
||||
|
@ -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<QString>({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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -37,7 +37,6 @@ public:
|
||||
QStringList listBookIds(const kiwix::Filter& filter, kiwix::supportedListSortBy sortBy, bool ascending) const;
|
||||
const std::vector<kiwix::Bookmark> 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<QString, QStringSet> m_knownZimsInDir;
|
||||
friend class LibraryManipulator;
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user