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::updateModel()` - the thumbnail data, if
available, is loaded by `ContentManager::getBookInfos()` and passed to
`ContentManagerModel::setBooksData()` through a new entry `favicon`.
This commit is contained in:
Veloman Yunkan 2024-02-19 19:11:51 +04:00
parent 14d3c8ff04
commit cf0f408f23
2 changed files with 30 additions and 20 deletions

View File

@ -140,7 +140,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);
@ -294,6 +294,28 @@ QString getFaviconUrl(const kiwix::Book& b)
return QString::fromStdString(url);
}
QByteArray getFaviconData(const kiwix::Book& b)
{
QByteArray qdata;
try {
// Try to obtain favicons only from local books (otherwise
// kiwix::Book::Illustration::getData() attempts to download the image
// on its own, whereas we want that operation to be performed
// asynchronously by ThumbnailDownloader).
if ( b.isPathValid() ) {
const auto illustration = b.getIllustration(48);
const std::string data = illustration->getData();
qdata = QByteArray::fromRawData(data.data(), data.size());
qdata.detach(); // deep copy
}
} catch ( ... ) {
return QByteArray();
}
return qdata;
}
QVariant getBookAttribute(const kiwix::Book& b, const QString& a)
{
if ( a == "id" ) return QString::fromStdString(b.getId());
@ -304,6 +326,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);

View File

@ -113,16 +113,8 @@ void ContentManagerModel::setBooksData(const BookInfoList& data)
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];
@ -174,16 +166,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());
}
}
}