Skip to content

Commit

Permalink
Changed how thumbnails of local books are obtained
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
veloman-yunkan committed Feb 19, 2024
1 parent cb17169 commit 8f914e2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
17 changes: 16 additions & 1 deletion src/contentmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand All @@ -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);
Expand Down
25 changes: 6 additions & 19 deletions src/contentmanagermodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char*>(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];
Expand Down Expand Up @@ -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());
}
}
}
Expand Down

0 comments on commit 8f914e2

Please sign in to comment.