From 9482bfb95bb6524ec780c6b9370626dad22acf20 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 17 Nov 2021 15:09:15 +0100 Subject: [PATCH] Add a method to get the a book illustration for a specific size. --- include/book.h | 1 + src/book.cpp | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/include/book.h b/include/book.h index cdcbf51fe..f0ac10aea 100644 --- a/include/book.h +++ b/include/book.h @@ -100,6 +100,7 @@ class Book const std::string& getFaviconMimeType() const; Illustrations getIllustrations() const; + std::shared_ptr getIllustration(unsigned int size) const; const std::string& getDownloadId() const { return m_downloadId; } diff --git a/src/book.cpp b/src/book.cpp index 5e1952a43..e04b0192e 100644 --- a/src/book.cpp +++ b/src/book.cpp @@ -217,14 +217,23 @@ void Book::setPath(const std::string& path) const Book::Illustration Book::missingDefaultIllustration; -const Book::Illustration& Book::getDefaultIllustration() const +std::shared_ptr Book::getIllustration(unsigned int size) const { for ( const auto& ilPtr : m_illustrations ) { - if (ilPtr->width == 48 && ilPtr->height == 48) { - return *ilPtr; + if (ilPtr->width == size && ilPtr->height == size) { + return ilPtr; } } - return missingDefaultIllustration; + throw std::runtime_error("Cannot find illustration"); +} + +const Book::Illustration& Book::getDefaultIllustration() const +{ + try { + return *getIllustration(48); + } catch (...) { + return missingDefaultIllustration; + } } const std::string& Book::Illustration::getData() const