Introduced ContentManagerError

This commit is contained in:
Veloman Yunkan 2024-02-09 17:35:43 +04:00
parent 142e6fed58
commit 7ec85ac2fb
2 changed files with 40 additions and 18 deletions

View File

@ -26,6 +26,33 @@
namespace namespace
{ {
class ContentManagerError : public std::runtime_error
{
public:
ContentManagerError(const QString& summary, const QString& details)
: std::runtime_error(summary.toStdString())
, m_details(details)
{}
QString summary() const { return QString::fromStdString(what()); }
QString details() const { return m_details; }
private:
QString m_details;
};
void throwDownloadUnavailableError()
{
throw ContentManagerError(gt("download-unavailable"),
gt("download-unavailable-text"));
}
void throwStorageError()
{
throw ContentManagerError(gt("download-storage-error"),
gt("download-storage-error-text"));
}
// Opens the directory containing the input file path. // Opens the directory containing the input file path.
// parent is the widget serving as the parent for the error dialog in case of // parent is the widget serving as the parent for the error dialog in case of
// failure. // failure.
@ -447,19 +474,15 @@ ContentManager::DownloadInfo ContentManager::updateDownloadInfos(QString bookId,
void ContentManager::downloadBook(const QString &id, QModelIndex index) void ContentManager::downloadBook(const QString &id, QModelIndex index)
{ {
QString downloadStatus = downloadBook(id); try
QString dialogHeader, dialogText; {
if (downloadStatus.size() == 0) { downloadBook(id);
dialogHeader = gt("download-unavailable");
dialogText = gt("download-unavailable-text");
} else if (downloadStatus == "storage_error") {
dialogHeader = gt("download-storage-error");
dialogText = gt("download-storage-error-text");
} else {
emit managerModel->startDownload(index); emit managerModel->startDownload(index);
} }
catch ( const ContentManagerError& err )
showInfoBox(dialogHeader, dialogText, mp_view); {
showInfoBox(err.summary(), err.details(), mp_view);
}
} }
const kiwix::Book& ContentManager::getRemoteOrLocalBook(const QString &id) const kiwix::Book& ContentManager::getRemoteOrLocalBook(const QString &id)
@ -472,23 +495,23 @@ const kiwix::Book& ContentManager::getRemoteOrLocalBook(const QString &id)
} }
} }
QString ContentManager::downloadBook(const QString &id) void ContentManager::downloadBook(const QString &id)
{ {
if (!mp_downloader) if (!mp_downloader)
return ""; throwDownloadUnavailableError();
const auto& book = getRemoteOrLocalBook(id); const auto& book = getRemoteOrLocalBook(id);
auto downloadPath = KiwixApp::instance()->getSettingsManager()->getDownloadDir(); auto downloadPath = KiwixApp::instance()->getSettingsManager()->getDownloadDir();
QStorageInfo storage(downloadPath); QStorageInfo storage(downloadPath);
auto bytesAvailable = storage.bytesAvailable(); auto bytesAvailable = storage.bytesAvailable();
if (bytesAvailable == -1 || book.getSize() > (unsigned long long) bytesAvailable) { if (bytesAvailable == -1 || book.getSize() > (unsigned long long) bytesAvailable) {
return "storage_error"; throwStorageError();
} }
auto booksList = mp_library->getBookIds(); auto booksList = mp_library->getBookIds();
for (auto b : booksList) { for (auto b : booksList) {
if (b.toStdString() == book.getId()) if (b.toStdString() == book.getId())
return ""; throwDownloadUnavailableError(); // but why???
} }
std::shared_ptr<kiwix::Download> download; std::shared_ptr<kiwix::Download> download;
@ -497,10 +520,9 @@ QString ContentManager::downloadBook(const QString &id)
const std::vector<std::pair<std::string, std::string>> options = { downloadDir }; const std::vector<std::pair<std::string, std::string>> options = { downloadDir };
download = mp_downloader->startDownload(book.getUrl(), options); download = mp_downloader->startDownload(book.getUrl(), options);
} catch (std::exception& e) { } catch (std::exception& e) {
return ""; throwDownloadUnavailableError();
} }
downloadStarted(book, download->getDid()); downloadStarted(book, download->getDid());
return QString::fromStdString(download->getDid());
} }
void ContentManager::eraseBookFilesFromComputer(const QString dirPath, const QString fileName, const bool moveToTrash) void ContentManager::eraseBookFilesFromComputer(const QString dirPath, const QString fileName, const bool moveToTrash)

View File

@ -53,7 +53,7 @@ public slots:
BookInfo getBookInfos(QString id, const QStringList &keys); BookInfo getBookInfos(QString id, const QStringList &keys);
void openBook(const QString& id); void openBook(const QString& id);
DownloadInfo updateDownloadInfos(QString bookId, QStringList keys); DownloadInfo updateDownloadInfos(QString bookId, QStringList keys);
QString downloadBook(const QString& id); void downloadBook(const QString& id);
void downloadBook(const QString& id, QModelIndex index); void downloadBook(const QString& id, QModelIndex index);
void updateLibrary(); void updateLibrary();
void setSearch(const QString& search); void setSearch(const QString& search);