diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index 85575f4..c679874 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -463,29 +463,35 @@ QString ContentManager::downloadBook(const QString &id, QModelIndex index) return downloadStatus; } +const kiwix::Book& ContentManager::getRemoteOrLocalBook(const QString &id) +{ + try { + QMutexLocker locker(&remoteLibraryLocker); + return mp_remoteLibrary->getBookById(id.toStdString()); + } catch (...) { + return mp_library->getBookById(id); + } +} QString ContentManager::downloadBook(const QString &id) { if (!mp_downloader) return ""; - const auto& book = [&]()->const kiwix::Book& { - try { - QMutexLocker locker(&remoteLibraryLocker); - return mp_remoteLibrary->getBookById(id.toStdString()); - } catch (...) { - return mp_library->getBookById(id); - } - }(); + + const auto& book = getRemoteOrLocalBook(id); auto downloadPath = KiwixApp::instance()->getSettingsManager()->getDownloadDir(); QStorageInfo storage(downloadPath); auto bytesAvailable = storage.bytesAvailable(); if (bytesAvailable == -1 || book.getSize() > (unsigned long long) bytesAvailable) { return "storage_error"; } + auto booksList = mp_library->getBookIds(); - for (auto b : booksList) + for (auto b : booksList) { if (b.toStdString() == book.getId()) return ""; + } + std::shared_ptr download; try { std::pair downloadDir("dir", downloadPath.toStdString()); diff --git a/src/contentmanager.h b/src/contentmanager.h index b5f5a24..e804754 100644 --- a/src/contentmanager.h +++ b/src/contentmanager.h @@ -81,6 +81,10 @@ private: // functions void setCategories(); void setLanguages(); + // Get the book with the specified id from + // the remote or local library (in that order). + const kiwix::Book& getRemoteOrLocalBook(const QString &id); + void downloadStarted(const kiwix::Book& book, const std::string& downloadId); void downloadCancelled(QString bookId); void downloadCompleted(QString bookId, QString path);