do remote library update in a different thread

Put updateRemoteLibrary in a new thread so it doesn't block the main UI, particularly the loader widget
This commit is contained in:
Nikhil Tanwar 2023-07-03 18:55:31 +05:30
parent 3abb4b4b4c
commit d2061a69a5
2 changed files with 13 additions and 5 deletions

View File

@ -18,6 +18,7 @@
#include "contentmanagerdelegate.h" #include "contentmanagerdelegate.h"
#include "node.h" #include "node.h"
#include "kiwixconfirmbox.h" #include "kiwixconfirmbox.h"
#include <QtConcurrent/QtConcurrentRun>
ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader, QObject *parent) ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader, QObject *parent)
: QObject(parent), : QObject(parent),
@ -157,6 +158,7 @@ QMap<QString, QVariant> ContentManager::getBookInfos(QString id, const QStringLi
return &mp_library->getBookById(id); return &mp_library->getBookById(id);
} catch (...) { } catch (...) {
try { try {
QMutexLocker locker(&remoteLibraryLocker);
return &m_remoteLibrary.getBookById(id.toStdString()); return &m_remoteLibrary.getBookById(id.toStdString());
} catch(...) { return nullptr; } } catch(...) { return nullptr; }
} }
@ -345,6 +347,7 @@ QString ContentManager::downloadBook(const QString &id)
return ""; return "";
const auto& book = [&]()->const kiwix::Book& { const auto& book = [&]()->const kiwix::Book& {
try { try {
QMutexLocker locker(&remoteLibraryLocker);
return m_remoteLibrary.getBookById(id.toStdString()); return m_remoteLibrary.getBookById(id.toStdString());
} catch (...) { } catch (...) {
return mp_library->getBookById(id); return mp_library->getBookById(id);
@ -536,11 +539,14 @@ void ContentManager::updateLibrary() {
#define CATALOG_URL "library.kiwix.org" #define CATALOG_URL "library.kiwix.org"
void ContentManager::updateRemoteLibrary(const QString& content) { void ContentManager::updateRemoteLibrary(const QString& content) {
m_remoteLibrary = kiwix::Library(); QtConcurrent::run([=]() {
kiwix::Manager manager(&m_remoteLibrary); QMutexLocker locker(&remoteLibraryLocker);
manager.readOpds(content.toStdString(), CATALOG_URL); m_remoteLibrary = kiwix::Library();
emit(this->booksChanged()); kiwix::Manager manager(&m_remoteLibrary);
emit(this->pendingRequest(false)); manager.readOpds(content.toStdString(), CATALOG_URL);
emit(this->booksChanged());
emit(this->pendingRequest(false));
});
} }
void ContentManager::setSearch(const QString &search) void ContentManager::setSearch(const QString &search)
@ -586,6 +592,7 @@ QStringList ContentManager::getBookIds()
return mp_library->listBookIds(filter, m_sortBy, m_sortOrderAsc); return mp_library->listBookIds(filter, m_sortBy, m_sortOrderAsc);
} else { } else {
filter.remote(true); filter.remote(true);
QMutexLocker locker(&remoteLibraryLocker);
auto bookIds = m_remoteLibrary.filter(filter); auto bookIds = m_remoteLibrary.filter(filter);
m_remoteLibrary.sort(bookIds, m_sortBy, m_sortOrderAsc); m_remoteLibrary.sort(bookIds, m_sortBy, m_sortOrderAsc);
QStringList list; QStringList list;

View File

@ -47,6 +47,7 @@ private:
void eraseBookFilesFromComputer(const QString dirPath, const QString filename); void eraseBookFilesFromComputer(const QString dirPath, const QString filename);
QList<QMap<QString, QVariant>> getBooksList(); QList<QMap<QString, QVariant>> getBooksList();
ContentManagerModel *managerModel; ContentManagerModel *managerModel;
QMutex remoteLibraryLocker;
signals: signals:
void filterParamsChanged(); void filterParamsChanged();