Got rid of faviconUrl in ContentManager::BookInfo

Merged "faviconUrl" entry of `ContentManager::BookInfo` into "favicon".
Now the latter represents either the illustration data (and then it is
of QByteArray type) or the url (in which case it is a QString).
This commit is contained in:
Veloman Yunkan 2024-02-20 16:52:37 +04:00
parent cf0f408f23
commit 4af4923faa
2 changed files with 18 additions and 13 deletions

View File

@ -140,7 +140,7 @@ void ContentManager::updateModel()
{ {
const auto bookIds = getBookIds(); const auto bookIds = getBookIds();
BookInfoList bookList; BookInfoList bookList;
QStringList keys = {"title", "tags", "date", "id", "size", "description", "faviconUrl", "favicon"}; QStringList keys = {"title", "tags", "date", "id", "size", "description", "favicon"};
QIcon bookIcon; QIcon bookIcon;
for (auto bookId : bookIds) { for (auto bookId : bookIds) {
auto mp = getBookInfos(bookId, keys); auto mp = getBookInfos(bookId, keys);
@ -316,6 +316,12 @@ QByteArray getFaviconData(const kiwix::Book& b)
return qdata; return qdata;
} }
QVariant getFaviconDataOrUrl(const kiwix::Book& b)
{
const QByteArray data = getFaviconData(b);
return !data.isNull() ? QVariant(data) : QVariant(getFaviconUrl(b));
}
QVariant getBookAttribute(const kiwix::Book& b, const QString& a) QVariant getBookAttribute(const kiwix::Book& b, const QString& a)
{ {
if ( a == "id" ) return QString::fromStdString(b.getId()); if ( a == "id" ) return QString::fromStdString(b.getId());
@ -326,8 +332,7 @@ QVariant getBookAttribute(const kiwix::Book& b, const QString& a)
if ( a == "url" ) return QString::fromStdString(b.getUrl()); if ( a == "url" ) return QString::fromStdString(b.getUrl());
if ( a == "name" ) return QString::fromStdString(b.getName()); if ( a == "name" ) return QString::fromStdString(b.getName());
if ( a == "downloadId" ) return QString::fromStdString(b.getDownloadId()); if ( a == "downloadId" ) return QString::fromStdString(b.getDownloadId());
if ( a == "favicon") return getFaviconData(b); if ( a == "favicon") return getFaviconDataOrUrl(b);
if ( a == "faviconUrl") return getFaviconUrl(b);
if ( a == "size" ) return QString::number(b.getSize()); if ( a == "size" ) return QString::number(b.getSize());
if ( a == "tags" ) return getBookTags(b); if ( a == "tags" ) return getBookTags(b);

View File

@ -113,14 +113,14 @@ void ContentManagerModel::setBooksData(const BookInfoList& data)
QByteArray ContentManagerModel::getThumbnail(const BookInfo& bookItem) const QByteArray ContentManagerModel::getThumbnail(const BookInfo& bookItem) const
{ {
QByteArray bookIcon = bookItem["favicon"].toByteArray(); const QVariant faviconEntry = bookItem["favicon"];
if ( bookIcon.isNull() ) { if ( faviconEntry.type() == QVariant::ByteArray )
const auto faviconUrl = bookItem["faviconUrl"].toString(); return faviconEntry.toByteArray();
if (m_iconMap.contains(faviconUrl)) {
bookIcon = m_iconMap[faviconUrl]; const auto faviconUrl = faviconEntry.toString();
} return m_iconMap.contains(faviconUrl)
} ? m_iconMap[faviconUrl]
return bookIcon; : QByteArray();
} }
std::shared_ptr<RowNode> ContentManagerModel::createNode(BookInfo bookItem) const std::shared_ptr<RowNode> ContentManagerModel::createNode(BookInfo bookItem) const
@ -167,8 +167,8 @@ void ContentManagerModel::refreshIcons()
td.clearQueue(); td.clearQueue();
for (auto i = 0; i < rowCount() && i < m_data.size(); i++) { for (auto i = 0; i < rowCount() && i < m_data.size(); i++) {
const auto& bookItem = m_data[i]; const auto& bookItem = m_data[i];
if ( bookItem["favicon"].toByteArray().isNull() ) { if ( bookItem["favicon"].type() == QVariant::String ) {
const auto faviconUrl = bookItem["faviconUrl"].toString(); const auto faviconUrl = bookItem["favicon"].toString();
if (faviconUrl != "" && !m_iconMap.contains(faviconUrl)) { if (faviconUrl != "" && !m_iconMap.contains(faviconUrl)) {
td.addDownload(faviconUrl, bookItem["id"].toString()); td.addDownload(faviconUrl, bookItem["id"].toString());
} }