mirror of
https://github.com/kiwix/kiwix-desktop.git
synced 2025-09-08 11:45:09 -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());
|
emit(booksChanged());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Library::setMonitorDirZims(QString monitorDir, QStringSet zimList)
|
void ContentManager::setMonitorDirZims(QString monitorDir, Library::QStringSet zimList)
|
||||||
{
|
{
|
||||||
m_knownZimsInDir[monitorDir] = zimList;
|
m_knownZimsInDir[monitorDir] = zimList;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Library::asyncUpdateFromDir(QString dir)
|
void ContentManager::asyncUpdateLibraryFromDir(QString dir)
|
||||||
{
|
{
|
||||||
(void) QtConcurrent::run([=]() {
|
(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);
|
QMutexLocker locker(&m_updateFromDirMutex);
|
||||||
const QDir dir(monitorDir);
|
const QDir dir(monitorDir);
|
||||||
const QStringSet oldDirEntries = m_knownZimsInDir[monitorDir];
|
const QStringSet oldDirEntries = m_knownZimsInDir[monitorDir];
|
||||||
@ -873,11 +875,12 @@ void Library::updateFromDir(QString monitorDir)
|
|||||||
}
|
}
|
||||||
const QStringSet addedZims = newDirEntries - oldDirEntries;
|
const QStringSet addedZims = newDirEntries - oldDirEntries;
|
||||||
const QStringSet removedZims = oldDirEntries - newDirEntries;
|
const QStringSet removedZims = oldDirEntries - newDirEntries;
|
||||||
kiwix::Manager manager(getKiwixLibrary());
|
const auto kiwixLib = mp_library->getKiwixLibrary();
|
||||||
|
kiwix::Manager manager(kiwixLib);
|
||||||
bool needsRefresh = !removedZims.empty();
|
bool needsRefresh = !removedZims.empty();
|
||||||
for (auto bookPath : addedZims) {
|
for (auto bookPath : addedZims) {
|
||||||
if ( isBeingDownloadedByUs(bookPath) ) {
|
if ( mp_library->isBeingDownloadedByUs(bookPath) ) {
|
||||||
// qDebug() << "DBG: Library::updateFromDir(): "
|
// qDebug() << "DBG: ContentManager::updateLibraryFromDir(): "
|
||||||
// << bookPath
|
// << bookPath
|
||||||
// << " ignored since it is being downloaded by us.";
|
// << " ignored since it is being downloaded by us.";
|
||||||
} else {
|
} else {
|
||||||
@ -886,8 +889,8 @@ void Library::updateFromDir(QString monitorDir)
|
|||||||
}
|
}
|
||||||
for (auto bookPath : removedZims) {
|
for (auto bookPath : removedZims) {
|
||||||
try {
|
try {
|
||||||
const auto book = mp_library->getBookByPath(bookPath.toStdString());
|
const auto book = kiwixLib->getBookByPath(bookPath.toStdString());
|
||||||
removeBookFromLibraryById(QString::fromStdString(book.getId()));
|
mp_library->removeBookFromLibraryById(QString::fromStdString(book.getId()));
|
||||||
} catch (...) {}
|
} catch (...) {}
|
||||||
}
|
}
|
||||||
if (needsRefresh) {
|
if (needsRefresh) {
|
||||||
|
@ -69,6 +69,9 @@ public: // functions
|
|||||||
QStringList getCategories() const { return m_categories; }
|
QStringList getCategories() const { return m_categories; }
|
||||||
LanguageList getLanguages() const { return m_languages; }
|
LanguageList getLanguages() const { return m_languages; }
|
||||||
|
|
||||||
|
void setMonitorDirZims(QString monitorDir, Library::QStringSet zimList);
|
||||||
|
void asyncUpdateLibraryFromDir(QString dir);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void filterParamsChanged();
|
void filterParamsChanged();
|
||||||
void booksChanged();
|
void booksChanged();
|
||||||
@ -113,6 +116,7 @@ private: // functions
|
|||||||
void updateModel();
|
void updateModel();
|
||||||
void setCategories();
|
void setCategories();
|
||||||
void setLanguages();
|
void setLanguages();
|
||||||
|
void updateLibraryFromDir(QString dir);
|
||||||
|
|
||||||
// Get the book with the specified id from
|
// Get the book with the specified id from
|
||||||
// the remote or local library (in that order).
|
// the remote or local library (in that order).
|
||||||
@ -141,6 +145,9 @@ private: // data
|
|||||||
|
|
||||||
ContentManagerModel *managerModel;
|
ContentManagerModel *managerModel;
|
||||||
QMutex remoteLibraryLocker;
|
QMutex remoteLibraryLocker;
|
||||||
|
|
||||||
|
QMutex m_updateFromDirMutex;
|
||||||
|
QMap<QString, Library::QStringSet> m_knownZimsInDir;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // CONTENTMANAGER_H
|
#endif // CONTENTMANAGER_H
|
||||||
|
@ -103,7 +103,7 @@ void KiwixApp::init()
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
connect(&m_watcher, &QFileSystemWatcher::directoryChanged, this, [=](QString monitorDir) {
|
connect(&m_watcher, &QFileSystemWatcher::directoryChanged, this, [=](QString monitorDir) {
|
||||||
m_library.asyncUpdateFromDir(monitorDir);
|
mp_manager->asyncUpdateLibraryFromDir(monitorDir);
|
||||||
});
|
});
|
||||||
|
|
||||||
setupDirectoryMonitoring();
|
setupDirectoryMonitoring();
|
||||||
@ -119,9 +119,10 @@ void KiwixApp::setupDirectoryMonitoring()
|
|||||||
auto dirList = QSet<QString>({monitorDir, downloadDir});
|
auto dirList = QSet<QString>({monitorDir, downloadDir});
|
||||||
for (auto dir : dirList) {
|
for (auto dir : dirList) {
|
||||||
if (dir != "") {
|
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_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;
|
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); }
|
const std::vector<kiwix::Bookmark> getBookmarks(bool onlyValidBookmarks = false) const { return mp_library->getBookmarks(onlyValidBookmarks); }
|
||||||
QStringSet getLibraryZimsFromDir(QString dir) const;
|
QStringSet getLibraryZimsFromDir(QString dir) const;
|
||||||
void setMonitorDirZims(QString monitorDir, QStringSet zimList);
|
|
||||||
void addBookToLibrary(kiwix::Book& book);
|
void addBookToLibrary(kiwix::Book& book);
|
||||||
void addBookBeingDownloaded(const kiwix::Book& book, QString downloadDir);
|
void addBookBeingDownloaded(const kiwix::Book& book, QString downloadDir);
|
||||||
bool isBeingDownloadedByUs(QString path) const;
|
bool isBeingDownloadedByUs(QString path) const;
|
||||||
@ -47,8 +46,6 @@ public:
|
|||||||
void addBookmark(kiwix::Bookmark& bookmark);
|
void addBookmark(kiwix::Bookmark& bookmark);
|
||||||
void removeBookmark(const QString& zimId, const QString& url);
|
void removeBookmark(const QString& zimId, const QString& url);
|
||||||
void save();
|
void save();
|
||||||
void updateFromDir(QString dir);
|
|
||||||
void asyncUpdateFromDir(QString dir);
|
|
||||||
kiwix::LibraryPtr getKiwixLibrary() { return mp_library; }
|
kiwix::LibraryPtr getKiwixLibrary() { return mp_library; }
|
||||||
public slots:
|
public slots:
|
||||||
const kiwix::Book& getBookById(QString id) const;
|
const kiwix::Book& getBookById(QString id) const;
|
||||||
@ -58,10 +55,8 @@ signals:
|
|||||||
void bookmarksChanged();
|
void bookmarksChanged();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QMutex m_updateFromDirMutex;
|
|
||||||
kiwix::LibraryPtr mp_library;
|
kiwix::LibraryPtr mp_library;
|
||||||
QString m_libraryDirectory;
|
QString m_libraryDirectory;
|
||||||
QMap<QString, QStringSet> m_knownZimsInDir;
|
|
||||||
friend class LibraryManipulator;
|
friend class LibraryManipulator;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user