Extracted ContentManager::getRemoteOrLocalBook()

... mainly for readability.
This commit is contained in:
Veloman Yunkan 2024-02-09 17:06:12 +04:00
parent c70ab0ed0d
commit a31aec56f8
2 changed files with 19 additions and 9 deletions

View File

@ -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<kiwix::Download> download;
try {
std::pair<std::string, std::string> downloadDir("dir", downloadPath.toStdString());

View File

@ -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);