From 7bef51073114fd895adb3525bfcdcec78c048613 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Fri, 3 May 2024 16:23:10 +0200 Subject: [PATCH] Enter ContentManager::BookState --- src/contentmanager.cpp | 52 +++++++++++++++++++++++++++++++++++------- src/contentmanager.h | 38 ++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 8 deletions(-) diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index 2e44125..d8d037b 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -390,6 +390,19 @@ QVariant getBookAttribute(const kiwix::Book& b, const QString& a) return QVariant(); } +ContentManager::BookState getStateOfLocalBook(const kiwix::Book& book) +{ + if ( !book.isPathValid() ) { + return ContentManager::BookState::ERROR_MISSING_ZIM_FILE; + } + + // XXX: When a book is detected to be corrupted, information about that + // XXX: has to be recorded somewhere so that we can return + // XXX: ERROR_CORRUPTED_ZIM_FILE here + + return ContentManager::BookState::AVAILABLE_LOCALLY_AND_HEALTHY; +} + } // unnamed namespace ContentManager::BookInfo ContentManager::getBookInfos(QString id, const QStringList &keys) @@ -420,17 +433,40 @@ ContentManager::BookInfo ContentManager::getBookInfos(QString id, const QStringL return values; } +ContentManager::BookState ContentManager::getBookState(QString bookId) +{ + if ( const auto downloadState = m_downloads.value(bookId) ) { + return downloadState->paused + ? BookState::DOWNLOAD_PAUSED + : BookState::DOWNLOADING; + // TODO: a download may be in error state + } + + try { + const kiwix::Book& b = mp_library->getBookById(bookId); + return b.getDownloadId().empty() + ? getStateOfLocalBook(b) + : BookState::DOWNLOADING; + } catch (...) {} + + try { + QMutexLocker locker(&remoteLibraryLocker); + const kiwix::Book& b = mp_remoteLibrary->getBookById(bookId.toStdString()); + return !b.getUrl().empty() + ? BookState::AVAILABLE_ONLINE + : BookState::METADATA_ONLY; + } catch (...) {} + + return BookState::INVALID; +} + void ContentManager::openBookWithIndex(const QModelIndex &index) { - try { - auto bookNode = static_cast(index.internalPointer()); - const QString bookId = bookNode->getBookId(); - // throws std::out_of_range if the book isn't available in local library - const kiwix::Book& book = mp_library->getBookById(bookId); - if ( !book.getDownloadId().empty() ) - return; + auto bookNode = static_cast(index.internalPointer()); + const QString bookId = bookNode->getBookId(); + if ( getBookState(bookId) == BookState::AVAILABLE_LOCALLY_AND_HEALTHY ) { openBook(bookId); - } catch (std::out_of_range &e) {} + } } void ContentManager::openBook(const QString &id) diff --git a/src/contentmanager.h b/src/contentmanager.h index 69a2ad6..cf1869c 100644 --- a/src/contentmanager.h +++ b/src/contentmanager.h @@ -20,6 +20,43 @@ public: // types typedef ContentManagerModel::BookInfo BookInfo; typedef ContentManagerModel::BookInfoList BookInfoList; + enum class BookState + { + // Nothing known about a book with that id + INVALID, + + // Only (some) metadata is available for the book, however neither a + // ZIM-file nor a URL is associated with it. + METADATA_ONLY, + + // No ZIM file is associated with the book but a URL is provided. + AVAILABLE_ONLINE, + + // The book is being downloaded. + DOWNLOADING, + + // The book started downloading, but the download is currently paused. + DOWNLOAD_PAUSED, + + // The book started downloading but the download was stopped due to + // errors. + DOWNLOAD_ERROR, + + // A valid ZIM file path is associated with the book and no evidence + // about any issues with that ZIM file has so far been obtained. + AVAILABLE_LOCALLY_AND_HEALTHY, + + // A ZIM file path is associated with the book but no such file seems + // to exist (may be caused by missing read permissions for the directory + // containing the ZIM file). + ERROR_MISSING_ZIM_FILE, + + // A ZIM file is associated with the book but it cannot be opened + // due to issues with its content. + ERROR_CORRUPTED_ZIM_FILE + }; + + public: // functions explicit ContentManager(Library* library, kiwix::Downloader *downloader, QObject *parent = nullptr); virtual ~ContentManager(); @@ -49,6 +86,7 @@ signals: public slots: QStringList getTranslations(const QStringList &keys); BookInfo getBookInfos(QString id, const QStringList &keys); + BookState getBookState(QString id); void openBook(const QString& id); void downloadBook(const QString& id); void downloadBook(const QString& id, QModelIndex index);