From 2fbfe40c11941cc3f3157464a77575c1dd0ac435 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Thu, 8 Feb 2024 18:55:58 +0400 Subject: [PATCH] Extracted ContentManager::getDownloadInfo() --- src/contentmanager.cpp | 39 +++++++++++++++++++++++++++------------ src/contentmanager.h | 7 ++++--- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index 4eb9034..b065b32 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -366,18 +366,18 @@ QString getDownloadInfo(const kiwix::Download& d, const QString& k) } // unnamed namespace -void ContentManager::downloadCancelled(const kiwix::Book& b) +void ContentManager::downloadCancelled(QString bookId) { - kiwix::Book bCopy(b); + kiwix::Book bCopy(mp_library->getBookById(bookId)); bCopy.setDownloadId(""); mp_library->getKiwixLibrary()->addOrUpdateBook(bCopy); mp_library->save(); emit(mp_library->booksChanged()); } -void ContentManager::downloadCompleted(const kiwix::Book& b, QString path) +void ContentManager::downloadCompleted(QString bookId, QString path) { - kiwix::Book bCopy(b); + kiwix::Book bCopy(mp_library->getBookById(bookId)); bCopy.setPath(QDir::toNativeSeparators(path).toStdString()); bCopy.setDownloadId(""); bCopy.setPathValid(true); @@ -387,13 +387,13 @@ void ContentManager::downloadCompleted(const kiwix::Book& b, QString path) mp_library->save(); mp_library->bookmarksChanged(); if (!m_local) { - emit(oneBookChanged(QString::fromStdString(b.getId()))); + emit(oneBookChanged(bookId)); } else { emit(mp_library->booksChanged()); } } -ContentManager::DownloadInfo ContentManager::updateDownloadInfos(QString id, const QStringList &keys) +ContentManager::DownloadInfo ContentManager::getDownloadInfo(QString bookId, const QStringList &keys) const { DownloadInfo values; if (!mp_downloader) { @@ -402,25 +402,40 @@ ContentManager::DownloadInfo ContentManager::updateDownloadInfos(QString id, con } return values; } - auto& b = mp_library->getBookById(id); + + auto& b = mp_library->getBookById(bookId); std::shared_ptr d; try { d = mp_downloader->getDownload(b.getDownloadId()); } catch(...) { - downloadCancelled(b); return values; } d->updateStatus(true); - if (d->getStatus() == kiwix::Download::K_COMPLETE) { - downloadCompleted(b, QString::fromStdString(d->getPath())); - } + for(auto& key: keys){ - values.insert(key, getDownloadInfo(*d, key)); + values.insert(key, ::getDownloadInfo(*d, key)); } + return values; } +ContentManager::DownloadInfo ContentManager::updateDownloadInfos(QString bookId, QStringList keys) +{ + if ( !keys.contains("status") ) keys.append("status"); + if ( !keys.contains("path") ) keys.append("path"); + + const DownloadInfo result = getDownloadInfo(bookId, keys); + + if ( result.isEmpty() ) { + downloadCancelled(bookId); + } else if ( result["status"] == "completed" ) { + downloadCompleted(bookId, result["path"].toString()); + } + + return result; +} + QString ContentManager::downloadBook(const QString &id, QModelIndex index) { QString downloadStatus = downloadBook(id); diff --git a/src/contentmanager.h b/src/contentmanager.h index 90f7f19..7b66103 100644 --- a/src/contentmanager.h +++ b/src/contentmanager.h @@ -65,8 +65,9 @@ private: QMutex remoteLibraryLocker; void setCategories(); void setLanguages(); - void downloadCancelled(const kiwix::Book& b); - void downloadCompleted(const kiwix::Book& book, QString path); + void downloadCancelled(QString bookId); + void downloadCompleted(QString bookId, QString path); + DownloadInfo getDownloadInfo(QString bookId, const QStringList& keys) const; signals: void filterParamsChanged(); @@ -84,7 +85,7 @@ public slots: QStringList getTranslations(const QStringList &keys); BookInfo getBookInfos(QString id, const QStringList &keys); void openBook(const QString& id); - DownloadInfo updateDownloadInfos(QString id, const QStringList& keys); + DownloadInfo updateDownloadInfos(QString bookId, QStringList keys); QString downloadBook(const QString& id); QString downloadBook(const QString& id, QModelIndex index); void updateLibrary();