From 4f9cb175bab7710c2f8628f28c36280caf977885 Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Mon, 5 Jun 2023 08:24:49 +0300 Subject: [PATCH] plasma: simplify favicons data engine Signed-off-by: Ivailo Monev --- plasma/dataengines/favicons/CMakeLists.txt | 3 +- .../dataengines/favicons/faviconprovider.cpp | 107 ------------------ plasma/dataengines/favicons/faviconprovider.h | 86 -------------- plasma/dataengines/favicons/favicons.cpp | 50 ++++---- plasma/dataengines/favicons/favicons.h | 15 +-- .../qguiplatformplugin_kde.cpp | 2 +- 6 files changed, 30 insertions(+), 233 deletions(-) delete mode 100644 plasma/dataengines/favicons/faviconprovider.cpp delete mode 100644 plasma/dataengines/favicons/faviconprovider.h diff --git a/plasma/dataengines/favicons/CMakeLists.txt b/plasma/dataengines/favicons/CMakeLists.txt index 13762973..e9220e4e 100644 --- a/plasma/dataengines/favicons/CMakeLists.txt +++ b/plasma/dataengines/favicons/CMakeLists.txt @@ -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} ) diff --git a/plasma/dataengines/favicons/faviconprovider.cpp b/plasma/dataengines/favicons/faviconprovider.cpp deleted file mode 100644 index 0ab24a7a..00000000 --- a/plasma/dataengines/favicons/faviconprovider.cpp +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Copyright (C) 2007 Tobias Koenig - * Copyright (C) 2008 Marco Martin - * - * 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 -#include - -#include -#include -#include -#include -#include -#include - -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(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" diff --git a/plasma/dataengines/favicons/faviconprovider.h b/plasma/dataengines/favicons/faviconprovider.h deleted file mode 100644 index 2e36473b..00000000 --- a/plasma/dataengines/favicons/faviconprovider.h +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright (C) 2007 Tobias Koenig - * Copyright (C) 2008 Marco Martin - * - * 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 - -#include - -/** - * 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 diff --git a/plasma/dataengines/favicons/favicons.cpp b/plasma/dataengines/favicons/favicons.cpp index 458941d5..873cdb45 100644 --- a/plasma/dataengines/favicons/favicons.cpp +++ b/plasma/dataengines/favicons/favicons.cpp @@ -19,47 +19,45 @@ #include "favicons.h" -#include "faviconprovider.h" +#include +#include +#include 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" diff --git a/plasma/dataengines/favicons/favicons.h b/plasma/dataengines/favicons/favicons.h index 79565bf5..c8c1988d 100644 --- a/plasma/dataengines/favicons/favicons.h +++ b/plasma/dataengines/favicons/favicons.h @@ -23,8 +23,6 @@ #include -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 diff --git a/qguiplatformplugin_kde/qguiplatformplugin_kde.cpp b/qguiplatformplugin_kde/qguiplatformplugin_kde.cpp index 2ab34e5e..c20df0f2 100644 --- a/qguiplatformplugin_kde/qguiplatformplugin_kde.cpp +++ b/qguiplatformplugin_kde/qguiplatformplugin_kde.cpp @@ -55,7 +55,7 @@ static QString qt2KdeFilter(const QString &f) if (cb != -1 && ob < cb) { if (first) { - first=false; + first = false; } else { str << '\n'; }