plasma: simplify favicons data engine

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2023-06-05 08:24:49 +03:00
parent 91e09232ba
commit 4f9cb175ba
6 changed files with 30 additions and 233 deletions

View file

@ -1,10 +1,9 @@
set(favicons_engine_SRCS
favicons.cpp
faviconprovider.cpp
)
kde4_add_plugin(plasma_engine_favicons ${favicons_engine_SRCS} )
target_link_libraries(plasma_engine_favicons ${KDE4_PLASMA_LIBS} ${KDE4_KDEUI_LIBS} ${KDE4_KIO_LIBS})
target_link_libraries(plasma_engine_favicons ${KDE4_PLASMA_LIBS} ${KDE4_KDECORE_LIBS})
install(TARGETS plasma_engine_favicons DESTINATION ${KDE4_PLUGIN_INSTALL_DIR} )
install(FILES plasma-dataengine-favicons.desktop DESTINATION ${KDE4_SERVICES_INSTALL_DIR} )

View file

@ -1,107 +0,0 @@
/*
* Copyright (C) 2007 Tobias Koenig <tokoe@kde.org>
* Copyright (C) 2008 Marco Martin <notmart@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "faviconprovider.h"
#include <QtGui/QImage>
#include <QtCore/QFile>
#include <KUrl>
#include <KIO/Job>
#include <KIO/StoredTransferJob>
#include <KMimeType>
#include <KDebug>
#include <kstandarddirs.h>
class FaviconProvider::Private
{
public:
Private( FaviconProvider *parent )
: q(parent)
{
}
void imageRequestFinished( KJob *job );
FaviconProvider *q;
QImage image;
QString cachePath;
};
void FaviconProvider::Private::imageRequestFinished(KJob *job)
{
if (job->error()) {
emit q->error(q);
return;
}
KIO::StoredTransferJob *storedJob = qobject_cast<KIO::StoredTransferJob*>(job);
image = QImage::fromData(storedJob->data());
if (!image.isNull()) {
image.save(cachePath, "PNG");
}
emit q->finished(q);
}
FaviconProvider::FaviconProvider(QObject *parent, const QString &url)
: QObject(parent),
m_url(url),
d(new Private(this))
{
KUrl faviconUrl(url);
if (faviconUrl.protocol().isEmpty()) {
faviconUrl = KUrl("http://" + url);
}
const QString fileName = KMimeType::favIconForUrl(faviconUrl.url());
if (!fileName.isEmpty()) {
d->cachePath = KStandardDirs::locateLocal("cache", fileName + ".png");
d->image.load(d->cachePath, "PNG");
} else {
d->cachePath = KStandardDirs::locateLocal("cache", "favicons/" + faviconUrl.host() + ".png");
faviconUrl.setPath("/favicon.ico");
if (faviconUrl.isValid()) {
KIO::StoredTransferJob *job = KIO::storedGet(faviconUrl, KIO::NoReload, KIO::HideProgressInfo);
//job->setProperty("uid", id);
connect(job, SIGNAL(result(KJob*)), this, SLOT(imageRequestFinished(KJob*)));
}
}
}
FaviconProvider::~FaviconProvider()
{
delete d;
}
QImage FaviconProvider::image() const
{
return d->image;
}
QString FaviconProvider::identifier() const
{
return m_url;
}
#include "moc_faviconprovider.cpp"

View file

@ -1,86 +0,0 @@
/*
* Copyright (C) 2007 Tobias Koenig <tokoe@kde.org>
* Copyright (C) 2008 Marco Martin <notmart@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2 as
* published by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef FAVICONPROVIDER_H
#define FAVICONPROVIDER_H
#include <QtCore/QObject>
#include <QImage>
/**
* This class provides a favicon for a given url
*/
class FaviconProvider : public QObject
{
Q_OBJECT
public:
/**
* Creates a new favicon provider.
*
* @param parent The parent object.
*/
FaviconProvider( QObject *parent, const QString &url);
/**
* Destroys the favicon provider.
*/
~FaviconProvider();
/**
* Returns the requested image.
*
* Note: This method returns only a valid image after the
* finished() signal has been emitted.
*/
QImage image() const;
/**
* Returns the identifier of the comic request (name + date).
*/
QString identifier() const;
Q_SIGNALS:
/**
* This signal is emitted whenever a request has been finished
* successfully.
*
* @param provider The provider which emitted the signal.
*/
void finished( FaviconProvider *provider );
/**
* This signal is emitted whenever an error has occurred.
*
* @param provider The provider which emitted the signal.
*/
void error( FaviconProvider *provider );
private:
QString m_url;
class Private;
Private* const d;
Q_PRIVATE_SLOT( d, void imageRequestFinished(KJob *job) )
};
#endif

View file

@ -19,47 +19,45 @@
#include "favicons.h"
#include "faviconprovider.h"
#include <KMimeType>
#include <KStandardDirs>
#include <KDebug>
FaviconsEngine::FaviconsEngine(QObject* parent, const QVariantList& args)
: Plasma::DataEngine(parent, args)
{
}
FaviconsEngine::~FaviconsEngine()
bool FaviconsEngine::updateSourceEvent(const QString &identifier)
{
}
bool FaviconsEngine::updateSourceEvent( const QString &identifier )
{
FaviconProvider *provider = new FaviconProvider(this, identifier);
connect(provider, SIGNAL(finished(FaviconProvider*)), this, SLOT(finished(FaviconProvider*)));
connect(provider, SIGNAL(error(FaviconProvider*)), this, SLOT(error(FaviconProvider*)));
if (provider->image() != QImage()) {
setData(provider->identifier(), "Icon", provider->image());
KUrl faviconUrl(identifier);
if (faviconUrl.protocol().isEmpty()) {
faviconUrl = KUrl("http://" + identifier);
}
const QString fileName = KMimeType::favIconForUrl(faviconUrl.url(), true);
if (fileName.isEmpty()) {
kDebug() << "No icon available for " << identifier;
setData(identifier, QImage());
return false;
}
const QString cachePath = KStandardDirs::locateLocal("cache", fileName + ".png");
const QImage image(cachePath, "PNG");
if (image.isNull()) {
kWarning() << "Could not load the image" << cachePath;
setData(identifier, QImage());
return false;
}
setData(identifier, image);
return true;
}
bool FaviconsEngine::sourceRequestEvent(const QString &identifier)
{
setData(identifier, QPixmap());
setData(identifier, QImage());
return updateSourceEvent(identifier);
}
void FaviconsEngine::finished(FaviconProvider *provider)
{
setData(provider->identifier(), "Icon", provider->image());
provider->deleteLater();
}
void FaviconsEngine::error(FaviconProvider *provider)
{
setData(provider->identifier(), QImage());
provider->deleteLater();
}
#include "moc_favicons.cpp"

View file

@ -23,8 +23,6 @@
#include <Plasma/DataEngine>
class FaviconProvider;
/**
* This class provides favicons for websites
*
@ -35,20 +33,15 @@ class FaviconsEngine : public Plasma::DataEngine
Q_OBJECT
public:
FaviconsEngine( QObject* parent, const QVariantList& args );
~FaviconsEngine();
FaviconsEngine(QObject* parent, const QVariantList& args);
protected:
bool sourceRequestEvent( const QString &identifier );
bool sourceRequestEvent(const QString &identifier);
protected Q_SLOTS:
bool updateSourceEvent( const QString &identifier );
private Q_SLOTS:
void finished( FaviconProvider* );
void error( FaviconProvider* );
bool updateSourceEvent(const QString &identifier);
};
K_EXPORT_PLASMA_DATAENGINE(favicons, FaviconsEngine)
#endif
#endif // FAVICONS_DATAENGINE_H

View file

@ -55,7 +55,7 @@ static QString qt2KdeFilter(const QString &f)
if (cb != -1 && ob < cb) {
if (first) {
first=false;
first = false;
} else {
str << '\n';
}