| 316 | } |
| 317 | |
| 318 | std::optional<QIcon> ResourceModel::getIcon(QModelIndex& index, const QUrl& url) |
| 319 | { |
| 320 | QPixmap pixmap; |
| 321 | if (QPixmapCache::find(url.toString(), &pixmap)) |
| 322 | return { pixmap }; |
| 323 | |
| 324 | if (!m_current_icon_job) { |
| 325 | m_current_icon_job.reset(new NetJob("IconJob", APPLICATION->network())); |
| 326 | m_current_icon_job->setAskRetry(false); |
| 327 | } |
| 328 | |
| 329 | if (m_currently_running_icon_actions.contains(url)) |
| 330 | return {}; |
| 331 | if (m_failed_icon_actions.contains(url)) |
| 332 | return {}; |
| 333 | |
| 334 | auto cache_entry = APPLICATION->metacache()->resolveEntry( |
| 335 | metaEntryBase(), |
| 336 | QString("logos/%1").arg(QString(QCryptographicHash::hash(url.toEncoded(), QCryptographicHash::Algorithm::Sha1).toHex()))); |
| 337 | auto icon_fetch_action = Net::ApiDownload::makeCached(url, cache_entry); |
| 338 | |
| 339 | auto full_file_path = cache_entry->getFullPath(); |
| 340 | connect(icon_fetch_action.get(), &Task::succeeded, this, [=] { |
| 341 | auto icon = QIcon(full_file_path); |
| 342 | QPixmapCache::insert(url.toString(), icon.pixmap(icon.actualSize({ 64, 64 }))); |
| 343 | |
| 344 | m_currently_running_icon_actions.remove(url); |
| 345 | |
| 346 | emit dataChanged(index, index, { Qt::DecorationRole }); |
| 347 | }); |
| 348 | connect(icon_fetch_action.get(), &Task::failed, this, [=] { |
| 349 | m_currently_running_icon_actions.remove(url); |
| 350 | m_failed_icon_actions.insert(url); |
| 351 | }); |
| 352 | |
| 353 | m_currently_running_icon_actions.insert(url); |
| 354 | |
| 355 | m_current_icon_job->addNetAction(icon_fetch_action); |
| 356 | if (!m_current_icon_job->isRunning()) |
| 357 | QMetaObject::invokeMethod(m_current_icon_job.get(), &NetJob::start); |
| 358 | |
| 359 | return {}; |
| 360 | } |
| 361 | |
| 362 | // No 'forgor to implement' shall pass here :blobfox_knife: |
| 363 | #define NEED_FOR_CALLBACK_ASSERT(name) \ |
no test coverage detected