1/*
2 This file is part of KNewStuff2.
3 SPDX-FileCopyrightText: 2006, 2007 Josef Spillner <spillner@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.1-or-later
6*/
7
8#include "imageloader_p.h"
9
10using namespace KNSCore;
11
12ImageLoader::ImageLoader(const Entry &entry, Entry::PreviewType type, QObject *parent)
13 : QObject(parent)
14 , m_entry(entry)
15 , m_previewType(type)
16{
17}
18
19void ImageLoader::start()
20{
21 QUrl url(m_entry.previewUrl(type: m_previewType));
22 if (!url.isEmpty()) {
23 m_job = HTTPJob::get(source: url, loadType: NoReload, flags: JobFlag::HideProgressInfo, parent: this);
24 connect(sender: m_job, signal: &KJob::result, context: this, slot: &ImageLoader::slotDownload);
25 connect(sender: m_job, signal: &HTTPJob::data, context: this, slot: &ImageLoader::slotData);
26 } else {
27 Q_EMIT signalError(m_entry, m_previewType, QStringLiteral("Empty url"));
28 deleteLater();
29 }
30}
31
32KJob *ImageLoader::job()
33{
34 return m_job;
35}
36
37void ImageLoader::slotData(KJob * /*job*/, const QByteArray &buf)
38{
39 m_buffer.append(a: buf);
40}
41
42void ImageLoader::slotDownload(KJob *job)
43{
44 if (job->error()) {
45 m_buffer.clear();
46 Q_EMIT signalError(m_entry, m_previewType, job->errorText());
47 deleteLater();
48 return;
49 }
50 QImage image;
51 image.loadFromData(data: std::move(m_buffer));
52
53 if (m_previewType == Entry::PreviewSmall1 || m_previewType == Entry::PreviewSmall2 || m_previewType == Entry::PreviewSmall3) {
54 if (image.width() > PreviewWidth || image.height() > PreviewHeight) {
55 // if the preview is really big, first scale fast to a smaller size, then smooth to desired size
56 if (image.width() > 4 * PreviewWidth || image.height() > 4 * PreviewHeight) {
57 image = image.scaled(w: 2 * PreviewWidth, h: 2 * PreviewHeight, aspectMode: Qt::KeepAspectRatio, mode: Qt::FastTransformation);
58 }
59 image = image.scaled(w: PreviewWidth, h: PreviewHeight, aspectMode: Qt::KeepAspectRatio, mode: Qt::SmoothTransformation);
60 } else if (image.width() <= PreviewWidth / 2 && image.height() <= PreviewHeight / 2) {
61 // upscale tiny previews to double size
62 image = image.scaled(w: 2 * image.width(), h: 2 * image.height());
63 }
64 }
65
66 m_entry.setPreviewImage(image, type: m_previewType);
67 Q_EMIT signalPreviewLoaded(m_entry, m_previewType);
68 deleteLater();
69}
70
71#include "moc_imageloader_p.cpp"
72

source code of knewstuff/src/core/imageloader.cpp