Plasma5Support

faviconprovider.cpp
1/*
2 SPDX-FileCopyrightText: 2007 Tobias Koenig <tokoe@kde.org>
3 SPDX-FileCopyrightText: 2008 Marco Martin <notmart@gmail.com>
4 SPDX-FileCopyrightText: 2013 Andrea Scarpino <scarpino@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "faviconprovider.h"
10
11#include <QImage>
12#include <QStandardPaths>
13#include <QUrl>
14
15#include <KIO/Job>
16#include <KIO/StoredTransferJob>
17#include <KJob>
18
19using namespace Qt::StringLiterals;
20
21class FaviconProvider::Private
22{
23public:
24 Private(FaviconProvider *parent)
25 : q(parent)
26 {
27 }
28
29 void imageRequestFinished(KIO::StoredTransferJob *job);
30
32 QImage image;
33 QString cachePath;
34};
35
36void FaviconProvider::Private::imageRequestFinished(KIO::StoredTransferJob *job)
37{
38 if (job->error()) {
39 Q_EMIT q->error(q);
40 return;
41 }
42
43 image = QImage::fromData(job->data());
44 if (!image.isNull()) {
45 image.save(cachePath, "PNG");
46 }
47 Q_EMIT q->finished(q);
48}
49
52 , m_url(url)
53 , d(new Private(this))
54{
55 QUrl faviconUrl = QUrl::fromUserInput(url);
56 const QString fileName = KIO::favIconForUrl(faviconUrl);
57
58 if (!fileName.isEmpty()) {
59 d->cachePath = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + u'/' + fileName + ".png"_L1;
60 d->image.load(d->cachePath, "PNG");
61 } else {
62 d->cachePath = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/favicons/"_L1 + faviconUrl.host() + ".png"_L1;
63 faviconUrl.setPath(QStringLiteral("/favicon.ico"));
64
65 if (faviconUrl.isValid()) {
66 KIO::StoredTransferJob *job = KIO::storedGet(faviconUrl, KIO::NoReload, KIO::HideProgressInfo);
67 // job->setProperty("uid", id);
68 connect(job, &KJob::result, this, [this, job]() {
69 d->imageRequestFinished(job);
70 });
71 }
72 }
73}
74
76{
77 delete d;
78}
79
81{
82 return d->image;
83}
84
86{
87 return m_url;
88}
89
90#include "moc_faviconprovider.cpp"
This class provides a favicon for a given url.
QString identifier() const
Returns the identifier of the comic request (name + date).
QImage image() const
Returns the requested image.
~FaviconProvider() override
Destroys the favicon provider.
FaviconProvider(QObject *parent, const QString &url)
Creates a new favicon provider.
void finished(FaviconProvider *provider)
This signal is emitted whenever a request has been finished successfully.
void error(FaviconProvider *provider)
This signal is emitted whenever an error has occurred.
QByteArray data() const
int error() const
void result(KJob *job)
KIOCORE_EXPORT StoredTransferJob * storedGet(const QUrl &url, LoadType reload=NoReload, JobFlags flags=DefaultFlags)
KIOCORE_EXPORT QString favIconForUrl(const QUrl &url)
HideProgressInfo
QImage fromData(QByteArrayView data, const char *format)
bool isNull() const const
bool load(QIODevice *device, const char *format)
bool save(QIODevice *device, const char *format, int quality) const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
QString writableLocation(StandardLocation type)
bool isEmpty() const const
QUrl fromUserInput(const QString &userInput, const QString &workingDirectory, UserInputResolutionOptions options)
QString host(ComponentFormattingOptions options) const const
bool isValid() const const
void setPath(const QString &path, ParsingMode mode)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:38 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.