From 7d8a83cc97694be59baea728cbe8d7730e3c0f35 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Sun, 31 Oct 2021 16:46:46 +0400 Subject: [PATCH] Encapsulated access to Book::m_illustration --- include/book.h | 6 ++++-- src/book.cpp | 38 ++++++++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/include/book.h b/include/book.h index e18a8142b..b5ff34052 100644 --- a/include/book.h +++ b/include/book.h @@ -107,10 +107,12 @@ class Book void setSize(uint64_t size) { m_size = size; } void setDownloadId(const std::string& downloadId) { m_downloadId = downloadId; } - private: + private: // functions std::string getCategoryFromTags() const; + const Illustration& getDefaultIllustration() const; + Illustration& getMutableDefaultIllustration(); - protected: + protected: // data std::string m_id; std::string m_downloadId; std::string m_path; diff --git a/src/book.cpp b/src/book.cpp index 0ac5b6a8b..ed219ce79 100644 --- a/src/book.cpp +++ b/src/book.cpp @@ -102,7 +102,8 @@ void Book::update(const zim::Archive& archive) { m_mediaCount = getArchiveMediaCount(archive); m_size = static_cast(getArchiveFileSize(archive)) << 10; - getArchiveFavicon(archive, 48, m_illustration.data, m_illustration.mimeType); + Illustration& favicon = getMutableDefaultIllustration(); + getArchiveFavicon(archive, 48, favicon.data, favicon.mimeType); } #define ATTR(name) node.attribute(name).value() @@ -129,9 +130,10 @@ void Book::updateFromXml(const pugi::xml_node& node, const std::string& baseDir) m_articleCount = strtoull(ATTR("articleCount"), 0, 0); m_mediaCount = strtoull(ATTR("mediaCount"), 0, 0); m_size = strtoull(ATTR("size"), 0, 0) << 10; - m_illustration.data = base64_decode(ATTR("favicon")); - m_illustration.mimeType = ATTR("faviconMimeType"); - m_illustration.url = ATTR("faviconUrl"); + Illustration& favicon = getMutableDefaultIllustration(); + favicon.data = base64_decode(ATTR("favicon")); + favicon.mimeType = ATTR("faviconMimeType"); + favicon.url = ATTR("faviconUrl"); try { m_downloadId = ATTR("downloadId"); } catch(...) {} @@ -179,8 +181,9 @@ void Book::updateFromOpds(const pugi::xml_node& node, const std::string& urlHost m_size = strtoull(linkNode.attribute("length").value(), 0, 0); } if (rel == "http://opds-spec.org/image/thumbnail") { - m_illustration.url = urlHost + linkNode.attribute("href").value(); - m_illustration.mimeType = linkNode.attribute("type").value(); + Illustration& favicon = getMutableDefaultIllustration(); + favicon.url = urlHost + linkNode.attribute("href").value(); + favicon.mimeType = linkNode.attribute("type").value(); } } @@ -213,25 +216,36 @@ void Book::setPath(const std::string& path) : path; } +const Book::Illustration& Book::getDefaultIllustration() const +{ + return m_illustration; +} + +Book::Illustration& Book::getMutableDefaultIllustration() +{ + return m_illustration; +} + const std::string& Book::getFavicon() const { - if (m_illustration.data.empty() && !m_illustration.url.empty()) { + const Illustration& favicon = getDefaultIllustration(); + if (favicon.data.empty() && !favicon.url.empty()) { try { - m_illustration.data = download(m_illustration.url); + favicon.data = download(favicon.url); } catch(...) { - std::cerr << "Cannot download favicon from " << m_illustration.url; + std::cerr << "Cannot download favicon from " << favicon.url; } } - return m_illustration.data; + return favicon.data; } const std::string& Book::getFaviconUrl() const { - return m_illustration.url; + return getDefaultIllustration().url; } const std::string& Book::getFaviconMimeType() const { - return m_illustration.mimeType; + return getDefaultIllustration().mimeType; } std::string Book::getTagStr(const std::string& tagName) const {