File indexing completed on 2024-07-14 03:53:51

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2013 David Faure <faure+bluesystems@kde.org>
0004     SPDX-FileCopyrightText: 2022 Harald Sitter <sitter@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef KIO_JOBUIDELEGATEFACTORY_H
0010 #define KIO_JOBUIDELEGATEFACTORY_H
0011 
0012 #include "job_base.h"
0013 #include "kiocore_export.h"
0014 #include <KJobUiDelegate>
0015 #include <QDateTime>
0016 #include <kio/global.h>
0017 
0018 #include <KCompositeJob>
0019 
0020 class QWidget;
0021 
0022 namespace KIO
0023 {
0024 /**
0025  * @class KIO::JobUiDelegateFactory jobuidelegatefactory.h <KIO/JobUiDelegateFactory>
0026  *
0027  * A factory for creating job ui delegates.
0028  * Every KIO job will get a delegate from this factory.
0029  * \since 5.0
0030  */
0031 class KIOCORE_EXPORT JobUiDelegateFactory
0032 {
0033 protected:
0034     /**
0035      * Constructor
0036      */
0037     JobUiDelegateFactory();
0038 
0039     /**
0040      * Destructor
0041      */
0042     virtual ~JobUiDelegateFactory();
0043 
0044 public:
0045     virtual KJobUiDelegate *createDelegate() const = 0;
0046 
0047     /**
0048      * @since 6.0
0049      */
0050     virtual KJobUiDelegate *createDelegate(KJobUiDelegate::Flags flags, QWidget *window) const = 0;
0051 
0052 private:
0053     class Private;
0054     Private *const d;
0055 };
0056 
0057 /**
0058  * Convenience method: use default factory, if there's one, to create a delegate and return it.
0059  */
0060 KIOCORE_EXPORT KJobUiDelegate *createDefaultJobUiDelegate();
0061 
0062 /**
0063  * Convenience method: use default factory, if there's one, to create a delegate and return it.
0064  *
0065  * @since 5.98
0066  */
0067 KIOCORE_EXPORT KJobUiDelegate *createDefaultJobUiDelegate(KJobUiDelegate::Flags flags, QWidget *window);
0068 
0069 /**
0070  * Returns the default job UI delegate factory to be used by all KIO jobs (in which HideProgressInfo is not set)
0071  * Can return nullptr, if no kio GUI library is loaded.
0072  * @since 6.0
0073  */
0074 KIOCORE_EXPORT JobUiDelegateFactory *defaultJobUiDelegateFactory();
0075 
0076 /**
0077  * Internal. Allows the KIO widgets library to register its widget-based job UI delegate factory
0078  * automatically.
0079  * @since 6.0
0080  */
0081 KIOCORE_EXPORT void setDefaultJobUiDelegateFactory(JobUiDelegateFactory *factory);
0082 
0083 /**
0084  * Returns the child of the job's uiDelegate() that implements the given extension,
0085  * or nullptr if none was found (or if the job had no uiDelegate).
0086  * @since 5.78
0087  */
0088 template<typename T>
0089 inline T delegateExtension(KJob *job)
0090 {
0091     KJobUiDelegate *ui = job->uiDelegate();
0092 
0093     // If setParentJob() was used, try the uiDelegate of parentJob first
0094     if (!ui) {
0095         if (KIO::Job *kiojob = qobject_cast<KIO::Job *>(job)) {
0096             if (KJob *parentJob = kiojob->parentJob()) {
0097                 ui = parentJob->uiDelegate();
0098             }
0099         }
0100     }
0101 
0102     // Still nothing? if compositeJob->addSubjob(job) was used, try the ui delegate
0103     // of compositeJob
0104     while (!ui) {
0105         job = qobject_cast<KCompositeJob *>(job->parent());
0106         if (job) {
0107             ui = job->uiDelegate();
0108         } else {
0109             break;
0110         }
0111     }
0112 
0113     return ui ? ui->findChild<T>(QString(), Qt::FindDirectChildrenOnly) : nullptr;
0114 }
0115 
0116 } // namespace KIO
0117 
0118 #endif