From 17f36926698049ce31d3d9141f1ef24383dc503f Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Fri, 31 May 2024 18:35:24 +0400 Subject: [PATCH] Moved error detection to DownloadManager --- src/contentmanager.cpp | 39 +------------------------- src/downloadmanagement.cpp | 57 ++++++++++++++++++++++++++++++++++---- src/downloadmanagement.h | 2 +- 3 files changed, 54 insertions(+), 44 deletions(-) diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index 8ce78ad..0530de4 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -8,7 +8,6 @@ #include #include #include -#include #include #include "contentmanagermodel.h" #include @@ -32,35 +31,6 @@ SettingsManager* getSettingsManager() return KiwixApp::instance()->getSettingsManager(); } -void throwDownloadUnavailableError() -{ - throw KiwixAppError(gt("download-unavailable"), - gt("download-unavailable-text")); -} - -void checkThatBookCanBeSaved(const kiwix::Book& book, QString targetDir) -{ - const QFileInfo targetDirInfo(targetDir); - if ( !targetDirInfo.isDir() ) { - throw KiwixAppError(gt("download-storage-error"), - gt("download-dir-missing")); - } - - // XXX: This may lie under Windows - // XXX: (see https://doc.qt.io/qt-5/qfile.html#platform-specific-issues) - if ( !targetDirInfo.isWritable() ) { - throw KiwixAppError(gt("download-storage-error"), - gt("download-dir-not-writable")); - } - - QStorageInfo storage(targetDir); - auto bytesAvailable = storage.bytesAvailable(); - if (bytesAvailable == -1 || book.getSize() > (unsigned long long) bytesAvailable) { - throw KiwixAppError(gt("download-storage-error"), - gt("download-storage-error-text")); - } -} - // Opens the directory containing the input file path. // parent is the widget serving as the parent for the error dialog in case of // failure. @@ -567,14 +537,7 @@ QString ContentManager::getRemoteLibraryUrl() const void ContentManager::downloadBook(kiwix::Book book, const QString& downloadPath) { - checkThatBookCanBeSaved(book, downloadPath); - - std::string downloadId; - try { - downloadId = DownloadManager::startDownload(book, downloadPath.toStdString()); - } catch (std::exception& e) { - throwDownloadUnavailableError(); - } + const std::string downloadId = DownloadManager::startDownload(book, downloadPath); book.setDownloadId(downloadId); mp_library->addBookBeingDownloaded(book, downloadPath); diff --git a/src/downloadmanagement.cpp b/src/downloadmanagement.cpp index e6cc447..88b6f76 100644 --- a/src/downloadmanagement.cpp +++ b/src/downloadmanagement.cpp @@ -1,5 +1,9 @@ #include "downloadmanagement.h" +#include "kiwixapp.h" +#include "kiwixconfirmbox.h" + +#include #include //////////////////////////////////////////////////////////////////////////////// @@ -135,20 +139,63 @@ DownloadInfo DownloadManager::getDownloadInfo(QString bookId) const }; } -std::string DownloadManager::startDownload(const kiwix::Book& book, const std::string& downloadDirPath) +namespace +{ + +void throwDownloadUnavailableError() +{ + throw KiwixAppError(gt("download-unavailable"), + gt("download-unavailable-text")); +} + +void checkThatBookCanBeSaved(const kiwix::Book& book, QString targetDir) +{ + const QFileInfo targetDirInfo(targetDir); + if ( !targetDirInfo.isDir() ) { + throw KiwixAppError(gt("download-storage-error"), + gt("download-dir-missing")); + } + + // XXX: This may lie under Windows + // XXX: (see https://doc.qt.io/qt-5/qfile.html#platform-specific-issues) + if ( !targetDirInfo.isWritable() ) { + throw KiwixAppError(gt("download-storage-error"), + gt("download-dir-not-writable")); + } + + QStorageInfo storage(targetDir); + auto bytesAvailable = storage.bytesAvailable(); + if (bytesAvailable == -1 || book.getSize() > (unsigned long long) bytesAvailable) { + throw KiwixAppError(gt("download-storage-error"), + gt("download-storage-error-text")); + } +} + +} // unnamed namespace + + +std::string DownloadManager::startDownload(const kiwix::Book& book, const QString& downloadDirPath) { if ( ! DownloadManager::downloadingFunctionalityAvailable() ) - throw std::runtime_error("Downloading functionality is not available"); + throwDownloadUnavailableError(); + + checkThatBookCanBeSaved(book, downloadDirPath); typedef std::vector> DownloadOptions; const std::string& url = book.getUrl(); const QString bookId = QString::fromStdString(book.getId()); - const DownloadOptions downloadOptions{{"dir", downloadDirPath}}; + const DownloadOptions downloadOptions{{"dir", downloadDirPath.toStdString()}}; - const auto d = mp_downloader->startDownload(url, downloadOptions); + std::string downloadId; + try { + const auto d = mp_downloader->startDownload(url, downloadOptions); + downloadId = d->getDid(); + } catch (std::exception& e) { + throwDownloadUnavailableError(); + } m_downloads.set(bookId, std::make_shared()); - return d->getDid(); + return downloadId; } void DownloadManager::pauseDownload(const QString& bookId) diff --git a/src/downloadmanagement.h b/src/downloadmanagement.h index f273df3..5d1d16c 100644 --- a/src/downloadmanagement.h +++ b/src/downloadmanagement.h @@ -81,7 +81,7 @@ public: // functions void updateDownloads(); // returns the download id - std::string startDownload(const kiwix::Book& book, const std::string& downloadDirPath); + std::string startDownload(const kiwix::Book& book, const QString& downloadDirPath); void pauseDownload(const QString& bookId); void resumeDownload(const QString& bookId); bool cancelDownload(const QString& bookId);