1 | /* |
2 | This file is part of the KDE libraries |
3 | SPDX-FileCopyrightText: 2013 David Faure <faure+bluesystems@kde.org> |
4 | SPDX-FileCopyrightText: 2022 Harald Sitter <sitter@kde.org> |
5 | |
6 | SPDX-License-Identifier: LGPL-2.0-or-later |
7 | */ |
8 | |
9 | #ifndef KIO_JOBUIDELEGATEFACTORY_H |
10 | #define KIO_JOBUIDELEGATEFACTORY_H |
11 | |
12 | #include "job_base.h" |
13 | #include "kiocore_export.h" |
14 | #include <KJobUiDelegate> |
15 | #include <QDateTime> |
16 | #include <kio/global.h> |
17 | |
18 | #include <KCompositeJob> |
19 | |
20 | class QWidget; |
21 | |
22 | namespace KIO |
23 | { |
24 | /** |
25 | * @class KIO::JobUiDelegateFactory jobuidelegatefactory.h <KIO/JobUiDelegateFactory> |
26 | * |
27 | * A factory for creating job ui delegates. |
28 | * Every KIO job will get a delegate from this factory. |
29 | * \since 5.0 |
30 | */ |
31 | class KIOCORE_EXPORT JobUiDelegateFactory |
32 | { |
33 | protected: |
34 | /** |
35 | * Constructor |
36 | */ |
37 | JobUiDelegateFactory(); |
38 | |
39 | /** |
40 | * Destructor |
41 | */ |
42 | virtual ~JobUiDelegateFactory(); |
43 | |
44 | public: |
45 | virtual KJobUiDelegate *createDelegate() const = 0; |
46 | |
47 | /** |
48 | * @since 6.0 |
49 | */ |
50 | virtual KJobUiDelegate *createDelegate(KJobUiDelegate::Flags flags, QWidget *window) const = 0; |
51 | |
52 | private: |
53 | class Private; |
54 | Private *const d; |
55 | }; |
56 | |
57 | /** |
58 | * Convenience method: use default factory, if there's one, to create a delegate and return it. |
59 | */ |
60 | KIOCORE_EXPORT KJobUiDelegate *createDefaultJobUiDelegate(); |
61 | |
62 | /** |
63 | * Convenience method: use default factory, if there's one, to create a delegate and return it. |
64 | * |
65 | * @since 5.98 |
66 | */ |
67 | KIOCORE_EXPORT KJobUiDelegate *createDefaultJobUiDelegate(KJobUiDelegate::Flags flags, QWidget *window); |
68 | |
69 | /** |
70 | * Returns the default job UI delegate factory to be used by all KIO jobs (in which HideProgressInfo is not set) |
71 | * Can return nullptr, if no kio GUI library is loaded. |
72 | * @since 6.0 |
73 | */ |
74 | KIOCORE_EXPORT JobUiDelegateFactory *defaultJobUiDelegateFactory(); |
75 | |
76 | /** |
77 | * Internal. Allows the KIO widgets library to register its widget-based job UI delegate factory |
78 | * automatically. |
79 | * @since 6.0 |
80 | */ |
81 | KIOCORE_EXPORT void setDefaultJobUiDelegateFactory(JobUiDelegateFactory *factory); |
82 | |
83 | /** |
84 | * Returns the child of the job's uiDelegate() that implements the given extension, |
85 | * or nullptr if none was found (or if the job had no uiDelegate). |
86 | * @since 5.78 |
87 | */ |
88 | template<typename T> |
89 | inline T delegateExtension(KJob *job) |
90 | { |
91 | KJobUiDelegate *ui = job->uiDelegate(); |
92 | |
93 | // If setParentJob() was used, try the uiDelegate of parentJob first |
94 | if (!ui) { |
95 | if (KIO::Job *kiojob = qobject_cast<KIO::Job *>(object: job)) { |
96 | if (KJob *parentJob = kiojob->parentJob()) { |
97 | ui = parentJob->uiDelegate(); |
98 | } |
99 | } |
100 | } |
101 | |
102 | // Still nothing? if compositeJob->addSubjob(job) was used, try the ui delegate |
103 | // of compositeJob |
104 | while (!ui) { |
105 | job = qobject_cast<KCompositeJob *>(object: job->parent()); |
106 | if (job) { |
107 | ui = job->uiDelegate(); |
108 | } else { |
109 | break; |
110 | } |
111 | } |
112 | |
113 | return ui ? ui->findChild<T>(QString(), Qt::FindDirectChildrenOnly) : nullptr; |
114 | } |
115 | |
116 | } // namespace KIO |
117 | |
118 | #endif |
119 | |