Enter ContentManager::BookState

This commit is contained in:
Veloman Yunkan 2024-05-03 16:23:10 +02:00 committed by Kelson
parent b7fa28b811
commit 7bef510731
2 changed files with 82 additions and 8 deletions

View File

@ -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<Node*>(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<Node*>(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)

View File

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