1/*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2007 Rafael Fernández López <ereslibre@kde.org>
4 SPDX-FileCopyrightText: 2007 Kevin Ottens <ervin@kde.org>
5 SPDX-FileCopyrightText: 2009 Shaun Reich <shaun.reich@kdemail.net>
6
7 SPDX-License-Identifier: LGPL-2.0-only
8*/
9
10#ifndef KWIDGETJOBTRACKER_P_H
11#define KWIDGETJOBTRACKER_P_H
12
13#include "kabstractwidgetjobtracker_p.h"
14#include "kwidgetjobtracker.h"
15
16#include <QCheckBox>
17#include <QDebug>
18#include <QElapsedTimer>
19#include <QEventLoopLocker>
20#include <QMap>
21#include <QQueue>
22#include <QUrl>
23#include <QWidget>
24
25class QPushButton;
26class KSqueezedTextLabel;
27class QLabel;
28class QProgressBar;
29
30class KWidgetJobTrackerPrivate : public KAbstractWidgetJobTrackerPrivate
31{
32 Q_DECLARE_PUBLIC(KWidgetJobTracker)
33
34public:
35 KWidgetJobTrackerPrivate(QWidget *parent, KWidgetJobTracker *qq)
36 : KAbstractWidgetJobTrackerPrivate(qq)
37 , parent(parent)
38 , eventLoopLocker(nullptr)
39 {
40 }
41
42 ~KWidgetJobTrackerPrivate() override
43 {
44 delete eventLoopLocker;
45 }
46
47 void setStopOnClose(KJob *job, bool stopOnClose) override;
48 bool stopOnClose(KJob *job) const override;
49 void setAutoDelete(KJob *job, bool autoDelete) override;
50 bool autoDelete(KJob *job) const override;
51
52 void _k_showProgressWidget();
53
54 class ProgressWidget;
55
56 QWidget *parent;
57 QEventLoopLocker *eventLoopLocker;
58 QMap<KJob *, ProgressWidget *> progressWidget;
59 QQueue<KJob *> progressWidgetsToBeShown;
60};
61
62class KWidgetJobTrackerPrivate::ProgressWidget : public QWidget
63{
64 Q_OBJECT
65
66public:
67 ProgressWidget(KJob *job, KWidgetJobTracker *object, QWidget *parent)
68 : QWidget(parent)
69 , tracker(object)
70 , job(job)
71 , totalSize(0)
72 , totalFiles(0)
73 , totalDirs(0)
74 , totalItems(0)
75 , processedSize(0)
76 , processedDirs(0)
77 , processedFiles(0)
78 , processedItems(0)
79 , totalSizeKnown(false)
80 , stopOnClose(true)
81 , jobRegistered(false)
82 , cancelClose(nullptr)
83 , openFile(nullptr)
84 , openLocation(nullptr)
85 , keepOpenCheck(nullptr)
86 , pauseButton(nullptr)
87 , sourceEdit(nullptr)
88 , destEdit(nullptr)
89 , progressLabel(nullptr)
90 , destInvite(nullptr)
91 , speedLabel(nullptr)
92 , sizeLabel(nullptr)
93 , resumeLabel(nullptr)
94 , progressBar(nullptr)
95 , suspendedProperty(false)
96 , refCount(1)
97 {
98 if (!parent) {
99 setWindowFlags(windowFlags() | Qt::Dialog);
100 }
101
102 init();
103 }
104
105 ~ProgressWidget() override
106 {
107 delete tracker->d_func()->eventLoopLocker;
108 tracker->d_func()->eventLoopLocker = nullptr;
109 }
110
111 KWidgetJobTracker *const tracker;
112 KJob *const job;
113
114 qulonglong totalSize;
115 qulonglong totalFiles;
116 qulonglong totalDirs;
117 qulonglong totalItems;
118 qulonglong processedSize;
119 qulonglong processedDirs;
120 qulonglong processedFiles;
121 qulonglong processedItems;
122
123 bool totalSizeKnown;
124 bool stopOnClose;
125 bool jobRegistered;
126 QString caption;
127
128 QPushButton *cancelClose;
129 QPushButton *openFile;
130 QPushButton *openLocation;
131 QCheckBox *keepOpenCheck;
132 QUrl location;
133 QElapsedTimer startTime;
134 QPushButton *pauseButton;
135 KSqueezedTextLabel *sourceEdit;
136 KSqueezedTextLabel *destEdit;
137 QLabel *progressLabel;
138 QLabel *sourceInvite;
139 QLabel *destInvite;
140 QLabel *speedLabel;
141 QLabel *sizeLabel;
142 QLabel *resumeLabel;
143 QProgressBar *progressBar;
144 QPushButton *arrowButton;
145 Qt::ArrowType arrowState;
146
147 bool suspendedProperty;
148
149 int refCount; // will not close the dialog if a modal menu is shown
150
151 void init();
152 void showTotals();
153 void setDestVisible(bool visible);
154 void checkDestination(const QUrl &dest);
155 void ref();
156 void deref();
157 void closeNow();
158
159 bool eventFilter(QObject *watched, QEvent *event) override;
160
161public Q_SLOTS:
162 virtual void infoMessage(const QString &plain);
163 virtual void description(const QString &title, const QPair<QString, QString> &field1, const QPair<QString, QString> &field2);
164 virtual void totalAmount(KJob::Unit unit, qulonglong amount);
165 virtual void processedAmount(KJob::Unit unit, qulonglong amount);
166 virtual void percent(unsigned long percent);
167 virtual void speed(unsigned long value);
168 virtual void slotClean();
169 virtual void suspended();
170 virtual void resumed();
171
172 // TODO: Misses canResume()
173
174protected:
175 void closeEvent(QCloseEvent *event) override;
176
177private Q_SLOTS:
178 void keepOpenToggled(bool);
179 void openFileClicked();
180 void openLocationClicked();
181 void pauseResumeClicked();
182 void cancelClicked();
183 void arrowClicked();
184};
185
186void KWidgetJobTrackerPrivate::setStopOnClose(KJob *job, bool stopOnClose)
187{
188 if (!progressWidget.contains(key: job)) {
189 return;
190 }
191 progressWidget[job]->stopOnClose = stopOnClose;
192}
193
194bool KWidgetJobTrackerPrivate::stopOnClose(KJob *job) const
195{
196 if (!progressWidget.contains(key: job)) {
197 qWarning() << "no widget found for job" << job;
198 return true;
199 }
200 return progressWidget[job]->stopOnClose;
201}
202
203void KWidgetJobTrackerPrivate::setAutoDelete(KJob *job, bool autoDelete)
204{
205 if (!progressWidget.contains(key: job)) {
206 return;
207 }
208 progressWidget[job]->setAttribute(Qt::WA_DeleteOnClose, on: autoDelete);
209}
210
211bool KWidgetJobTrackerPrivate::autoDelete(KJob *job) const
212{
213 if (!progressWidget.contains(key: job)) {
214 qWarning() << "no widget found for job" << job;
215 return true;
216 }
217 return progressWidget[job]->testAttribute(attribute: Qt::WA_DeleteOnClose);
218}
219
220#endif // KWIDGETJOBTRACKER_P_H
221

source code of kjobwidgets/src/kwidgetjobtracker_p.h