From 8f914e2b66d32bbfe48e4c78930de1315ed7327d Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Mon, 19 Feb 2024 19:11:51 +0400 Subject: [PATCH] Changed how thumbnails of local books are obtained Before this change, thumbnails of local books were obtained by `ContentManagerModel` (via ugly calls to `KiwixApp::instance()->getLibrary()`). Now that task is transferred to `ContentManager`. --- src/contentmanager.cpp | 17 ++++++++++++++++- src/contentmanagermodel.cpp | 25 ++++++------------------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index cecf3fb5f..7b49b91fe 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -167,7 +167,7 @@ void ContentManager::updateModel() { const auto bookIds = getBookIds(); BookInfoList bookList; - QStringList keys = {"title", "tags", "date", "id", "size", "description", "faviconUrl"}; + QStringList keys = {"title", "tags", "date", "id", "size", "description", "faviconUrl", "favicon"}; QIcon bookIcon; for (auto bookId : bookIds) { auto mp = getBookInfos(bookId, keys); @@ -321,6 +321,20 @@ QString getFaviconUrl(const kiwix::Book& b) return QString::fromStdString(url); } +QByteArray getFaviconData(const kiwix::Book& b) +{ + try { + const auto illustration = b.getIllustration(48); + const std::string data = illustration->getData(); + + QByteArray qdata = QByteArray::fromRawData(data.data(), data.size()); + qdata.detach(); // deep copy + return qdata; + } catch ( ... ) { + return QByteArray(); + } +} + QVariant getBookAttribute(const kiwix::Book& b, const QString& a) { if ( a == "id" ) return QString::fromStdString(b.getId()); @@ -331,6 +345,7 @@ QVariant getBookAttribute(const kiwix::Book& b, const QString& a) if ( a == "url" ) return QString::fromStdString(b.getUrl()); if ( a == "name" ) return QString::fromStdString(b.getName()); if ( a == "downloadId" ) return QString::fromStdString(b.getDownloadId()); + if ( a == "favicon") return getFaviconData(b); if ( a == "faviconUrl") return getFaviconUrl(b); if ( a == "size" ) return QString::number(b.getSize()); if ( a == "tags" ) return getBookTags(b); diff --git a/src/contentmanagermodel.cpp b/src/contentmanagermodel.cpp index 883d09926..a641f2a9b 100644 --- a/src/contentmanagermodel.cpp +++ b/src/contentmanagermodel.cpp @@ -112,16 +112,8 @@ void ContentManagerModel::setBooksData(const BookInfoList& data, const Downloads QByteArray ContentManagerModel::getThumbnail(const BookInfo& bookItem) const { - QString id = bookItem["id"].toString(); - QByteArray bookIcon; - try { - auto book = KiwixApp::instance()->getLibrary()->getBookById(id); - std::string favicon; - auto item = book.getIllustration(48); - favicon = item->getData(); - bookIcon = QByteArray::fromRawData(reinterpret_cast(favicon.data()), favicon.size()); - bookIcon.detach(); // deep copy - } catch (...) { + QByteArray bookIcon = bookItem["favicon"].toByteArray(); + if ( bookIcon.isNull() ) { const auto faviconUrl = bookItem["faviconUrl"].toString(); if (m_iconMap.contains(faviconUrl)) { bookIcon = m_iconMap[faviconUrl]; @@ -173,16 +165,11 @@ void ContentManagerModel::refreshIcons() return; td.clearQueue(); for (auto i = 0; i < rowCount() && i < m_data.size(); i++) { - auto bookItem = m_data[i]; - auto id = bookItem["id"].toString(); - const auto faviconUrl = bookItem["faviconUrl"].toString(); - auto app = KiwixApp::instance(); - try { - auto book = app->getLibrary()->getBookById(id); - auto item = book.getIllustration(48); - } catch (...) { + const auto& bookItem = m_data[i]; + if ( bookItem["favicon"].toByteArray().isNull() ) { + const auto faviconUrl = bookItem["faviconUrl"].toString(); if (faviconUrl != "" && !m_iconMap.contains(faviconUrl)) { - td.addDownload(faviconUrl, id); + td.addDownload(faviconUrl, bookItem["id"].toString()); } } }