Skip to content

Commit

Permalink
Introduce getValidBookById in Library
Browse files Browse the repository at this point in the history
Raise an exception the book does not exist in the file system. Since the change to directory monitoring alters the behavior of the default getBookById, the function should be called if you need the book to actually exist.
  • Loading branch information
ShaopengLin committed Jul 27, 2024
1 parent b880cfd commit c358223
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/contentmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ ContentManager::BookInfo ContentManager::getBookInfos(QString id, const QStringL
{
const kiwix::Book* b = nullptr;
try {
b = &mp_library->getBookById(id);
b = &mp_library->getValidBookById(id);
if ( ! b->getDownloadId().empty() ) {
// The book is still being downloaded and has been entered into the
// local library for technical reasons only. Get the book info from
Expand Down Expand Up @@ -395,7 +395,7 @@ ContentManager::BookState ContentManager::getBookState(QString bookId)
}

try {
const kiwix::Book& b = mp_library->getBookById(bookId);
const kiwix::Book& b = mp_library->getValidBookById(bookId);
return b.getDownloadId().empty()
? getStateOfLocalBook(b)
: BookState::DOWNLOADING;
Expand Down Expand Up @@ -469,7 +469,7 @@ void ContentManager::downloadDisappeared(QString bookId)
removeDownload(bookId);
kiwix::Book bCopy;
try {
bCopy = mp_library->getBookById(bookId);
bCopy = mp_library->getValidBookById(bookId);
} catch ( const std::out_of_range& ) {
// If the download has disappeared as a result of some
// obscure chain of events, the book may have disappeared too.
Expand Down
8 changes: 8 additions & 0 deletions src/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,11 @@ const kiwix::Book &Library::getBookById(QString id) const
{
return mp_library->getBookById(id.toStdString());
}

const kiwix::Book &Library::getValidBookById(QString id) const
{
auto& book = mp_library->getBookById(id.toStdString());
if (book.isPathValid())
return book;
throw std::out_of_range ("Book is invalid or does not exist.");
}
1 change: 1 addition & 0 deletions src/library.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Library : public QObject
kiwix::LibraryPtr getKiwixLibrary() { return mp_library; }
public slots:
const kiwix::Book& getBookById(QString id) const;
const kiwix::Book& getValidBookById(QString id) const;

signals:
void booksChanged();
Expand Down
4 changes: 2 additions & 2 deletions src/urlschemehandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ UrlSchemeHandler::handleMetaRequest(QWebEngineUrlRequestJob* request)
if (metaName == "favicon") {
try {
auto library = KiwixApp::instance()->getLibrary();
auto book = library->getBookById(zimId);
auto book = library->getValidBookById(zimId);
auto illustration = book.getIllustration(48);
std::string content = illustration->getData();
std::string mimeType = illustration->mimeType;
Expand Down Expand Up @@ -184,7 +184,7 @@ UrlSchemeHandler::replyZimNotFoundPage(QWebEngineUrlRequestJob *request,
QString path = "N/A", name = "N/A";
try
{
auto& book = KiwixApp::instance()->getLibrary()->getBookById(zimId);
auto& book = KiwixApp::instance()->getLibrary()->getValidBookById(zimId);
path = QString::fromStdString(book.getPath());
name = QString::fromStdString(book.getName());
}
Expand Down

0 comments on commit c358223

Please sign in to comment.