File indexing completed on 2024-04-14 03:53:16

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2016 David Faure <faure@kde.org>
0004     SPDX-FileCopyrightText: 2001 Malte Starostik <malte@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef KIO_FAVICONREQUESTJOB_H
0010 #define KIO_FAVICONREQUESTJOB_H
0011 
0012 #include "kiogui_export.h"
0013 #include <kio/job_base.h> // for LoadType
0014 
0015 #include <memory>
0016 
0017 class QUrl;
0018 
0019 namespace KIO
0020 {
0021 class FavIconRequestJobPrivate;
0022 /**
0023  * @class FavIconRequestJob faviconrequestjob.h <KIO/FavIconRequestJob>
0024  *
0025  * FavIconRequestJob handles the retrieval of a favicon (either from the local cache or from the internet)
0026  *
0027  * For instance, the icon for http://www.google.com exists at http://www.google.com/favicon.ico
0028  * This job will (the first time) download the favicon, and make it available as a local PNG
0029  * for fast lookups afterwards.
0030  *
0031  * Usage:
0032  * Create a FavIconRequestJob, connect to result(KJob *), and from there use iconFile().
0033  *
0034  * @code
0035  * // Let's say we want to show the icon for QUrl m_url
0036  * KIO::FavIconRequestJob *job = new KIO::FavIconRequestJob(m_url);
0037  * connect(job, &KIO::FavIconRequestJob::result, this, [job, this](KJob *){
0038  *     if (!job->error()) {
0039  *         // show the icon using QIcon(job->iconFile())
0040  *     }
0041  * });
0042  * @endcode
0043  *
0044  * For a given HTTP URL, you can find out if a favicon is available by calling KIO::favIconForUrl() in KIOCore.
0045  * It is however not necessary to check this first, FavIconRequestJob will do this
0046  * first and emit result right away if a cached icon is available and not too old.
0047  *
0048  * In Web Browsers, additional information exists: the HTML for a given page can
0049  * specify something like
0050  *  &lt;link rel="shortcut icon" href="another_favicon.ico" /&gt;
0051  * To handle this, call job->setIconUrl(iconUrl).
0052  * (KParts-based web engines use the signal BrowserExtension::setIconUrl to call back
0053  * into the web browser application, which should then call this).
0054  * The signal urlIconChanged will be emitted once the icon has been downloaded.
0055  *
0056  * The on-disk cache is shared between processes.
0057  *
0058  * @since 5.19
0059  */
0060 class KIOGUI_EXPORT FavIconRequestJob : public KCompositeJob
0061 {
0062     Q_OBJECT
0063 public:
0064     /**
0065      * @brief FavIconRequestJob constructor
0066      * @param hostUrl The web page URL. We only use the scheme and host.
0067      * @param reload set this to reload to skip the cache and force a refresh of the favicon.
0068      * @param parent parent object
0069      */
0070     explicit FavIconRequestJob(const QUrl &hostUrl, KIO::LoadType reload = KIO::NoReload, QObject *parent = nullptr);
0071 
0072     /**
0073      * Destructor. You do not need to delete the job, it will delete automatically,
0074      * unless you call setAutoDelete(false).
0075      */
0076     ~FavIconRequestJob() override;
0077 
0078     /**
0079      * @brief setIconUrl allows to set, for a specific URL, a different icon URL
0080      * than the default one for the host (http://host/favicon.ico)
0081      *
0082      * This information is stored in the on-disk cache, so that
0083      * other FavIconRequestJobs for this url and KIO::favIconForUrl
0084      * will return the icon specified here.
0085      *
0086      * @param iconUrl the URL to the icon, usually parsed from the HTML
0087      */
0088     void setIconUrl(const QUrl &iconUrl);
0089 
0090     /**
0091      * Returns the full local path to the icon from the cache.
0092      * Only call this in the slot connected to the result(KJob*) signal.
0093      * @return the path to the icon file
0094      */
0095     QString iconFile() const;
0096 
0097     /**
0098      * Returns the URL passed to the constructor
0099      * @since 5.20
0100      */
0101     QUrl hostUrl() const;
0102 
0103     /**
0104      * @internal
0105      * Do not call start(), KIO jobs are autostarted
0106      */
0107     void start() override
0108     {
0109     }
0110 
0111 private Q_SLOTS:
0112     KIOGUI_NO_EXPORT void doStart(); // not called start() so that exec() doesn't call it too
0113     void slotResult(KJob *job) override;
0114 
0115 private:
0116     std::unique_ptr<FavIconRequestJobPrivate> const d;
0117 };
0118 
0119 }
0120 #endif