Libkdepim

progressmanager.h
1/*
2 progressmanager.h
3
4 This file is part of libkdepim.
5
6 SPDX-FileCopyrightText: 2004 Till Adam <adam@kde.org>
7
8 SPDX-License-Identifier: LGPL-2.0-or-later
9*/
10
11#pragma once
12
13#include "kdepim_export.h"
14
15#include <QHash>
16#include <QMap>
17#include <QObject>
18#include <QPointer>
19#include <QString>
20
21namespace KPIM
22{
23class ProgressItem;
24class ProgressManager;
25using ProgressItemMap = QMap<ProgressItem *, bool>;
26/**
27 * @brief The ProgressItem class
28 */
29class KDEPIM_EXPORT ProgressItem : public QObject
30{
31 Q_OBJECT
32 friend class ProgressManager;
33
34public:
35 enum CryptoStatus {
36 Encrypted,
37 Unencrypted,
38 Unknown,
39 };
40
41 /**
42 * @return The id string which uniquely identifies the operation
43 * represented by this item.
44 */
45 [[nodiscard]] const QString &id() const;
46
47 /**
48 * @return The parent item of this one, if there is one.
49 */
50 [[nodiscard]] ProgressItem *parent() const;
51
52 /**
53 * @return The user visible string to be used to represent this item.
54 */
55 [[nodiscard]] const QString &label() const;
56
57 /**
58 * @param v Set the user visible string identifying this item.
59 */
60 void setLabel(const QString &v);
61
62 /**
63 * @return The string to be used for showing this item's current status.
64 */
65 [[nodiscard]] const QString &status() const;
66 /**
67 * Set the string to be used for showing this item's current status.
68 * @param v The status string.
69 */
70 void setStatus(const QString &v);
71
72 /**
73 * @return Whether this item can be canceled.
74 */
75 [[nodiscard]] bool canBeCanceled() const;
76
77 /**
78 * @param b Set if can be canceled
79 */
80 void setCanBeCanceled(bool b);
81
82 /**
83 * @return Whether this item uses secure communication
84 * (Account uses ssl, for example.).
85 */
86 [[nodiscard]] CryptoStatus cryptoStatus() const;
87
88 /**
89 * Set whether this item uses encrypted communication, so listeners
90 * can display a nice crypto icon.
91 * @param v The value.
92 */
93 void setCryptoStatus(ProgressItem::CryptoStatus v);
94
95 /**
96 * @return whether this item uses a busy indicator instead of real progress display
97 */
98 [[nodiscard]] bool usesBusyIndicator() const;
99
100 /**
101 * Sets whether this item uses a busy indicator instead of real progress for its progress bar.
102 * If it uses a busy indicator, you are still responsible for calling setProgress() from time to
103 * time to update the busy indicator.
104 */
105 void setUsesBusyIndicator(bool useBusyIndicator);
106
107 /**
108 * @return The current progress value of this item in percent.
109 */
110 [[nodiscard]] unsigned int progress() const;
111
112 /**
113 * Set the progress (percentage of completion) value of this item.
114 * @param v The percentage value.
115 */
116 void setProgress(unsigned int v);
117
118 /**
119 * Tell the item it has finished. This will Q_EMIT progressItemCompleted()
120 * result in the destruction of the item after all slots connected to this
121 * signal have executed. This is the only way to get rid of an item and
122 * needs to be called even if the item is canceled. Don't use the item
123 * after this has been called on it.
124 */
125 void setComplete();
126
127 /**
128 * Reset the progress value of this item to 0 and the status string to
129 * the empty string.
130 */
131 void reset();
132
133 void cancel();
134
135 // Often needed values for calculating progress.
136 void setTotalItems(unsigned int v);
137 [[nodiscard]] unsigned int totalItems() const;
138 void setCompletedItems(unsigned int v);
139 void incCompletedItems(unsigned int v = 1);
140 [[nodiscard]] unsigned int completedItems() const;
141
142 /**
143 * Recalculate progress according to total/completed items and update.
144 */
145 void updateProgress();
146
147 void addChild(ProgressItem *kiddo);
148 void removeChild(ProgressItem *kiddo);
149
150 [[nodiscard]] bool canceled() const;
151
152 unsigned int typeProgressItem() const;
153 void setTypeProgressItem(unsigned int);
154
155Q_SIGNALS:
156 /**
157 * Emitted when a new ProgressItem is added.
158 * @param The ProgressItem that was added.
159 */
161
162 /**
163 * Emitted when the progress value of an item changes.
164 * @param The item which got a new value.
165 * @param The value, for convenience.
166 */
168
169 /**
170 * Emitted when a progress item was completed. The item will be
171 * deleted afterwards, so slots connected to this are the last
172 * chance to work with this item.
173 * @param The completed item.
174 */
176
177 /**
178 * Emitted when an item was canceled. It will _not_ go away immediately,
179 * only when the owner sets it complete, which will usually happen. Can be
180 * used to visually indicate the canceled status of an item. Should be used
181 * by the owner of the item to make sure it is set completed even if it is
182 * canceled. There is a ProgressManager::slotStandardCancelHandler which
183 * simply sets the item completed and can be used if no other work needs to
184 * be done on cancel.
185 * @param The canceled item;
186 */
188
189 /**
190 * Emitted when the status message of an item changed. Should be used by
191 * progress dialogs to update the status message for an item.
192 * @param The updated item.
193 * @param The new message.
194 */
196
197 /**
198 * Emitted when the label of an item changed. Should be used by
199 * progress dialogs to update the label of an item.
200 * @param The updated item.
201 * @param The new label.
202 */
204
205 /**
206 * Emitted when the crypto status of an item changed. Should be used by
207 * progress dialogs to update the crypto indicator of an item.
208 * @param The updated item.
209 * @param The new state.
210 */
211 void progressItemCryptoStatus(KPIM::ProgressItem *, KPIM::ProgressItem::CryptoStatus);
212
213 /**
214 * Emitted when the busy indicator state of an item changes. Should be used
215 * by progress dialogs so that they can adjust the display of the progress bar
216 * to the new mode.
217 * @param item The updated item
218 * @param value True if the item uses a busy indicator now, false otherwise
219 */
221
222protected:
223 /* Only to be used by our good friend the ProgressManager */
224 ProgressItem(ProgressItem *parent, const QString &id, const QString &label, const QString &status, bool isCancellable, CryptoStatus cryptoStatus);
225 ~ProgressItem() override;
226
227private:
228 QString mId;
229 QString mLabel;
230 QString mStatus;
232 bool mCanBeCanceled = false;
233 unsigned int mProgress = 0;
234 ProgressItemMap mChildren;
235 unsigned int mTotal = 0;
236 unsigned int mCompleted = 0;
237 CryptoStatus mCryptoStatus;
238 unsigned int mType = 0;
239 bool mWaitingForKids = false;
240 bool mCanceled = false;
241 bool mUsesBusyIndicator = false;
242 bool mCompletedCalled = false;
243};
244
245struct ProgressManagerPrivate;
246
247/**
248 * The ProgressManager singleton keeps track of all ongoing transactions
249 * and notifies observers (progress dialogs) when their progress percent value
250 * changes, when they are completed (by their owner), and when they are canceled.
251 * Each ProgressItem emits those signals individually and the singleton
252 * broadcasts them. Use the ::createProgressItem() statics to acquire an item
253 * and then call ->setProgress( int percent ) on it every time you want to
254 * update the item and ->setComplete() when the operation is done. This will
255 * delete the item. Connect to the item's progressItemCanceled() signal to be
256 * notified when the user cancels the transaction using one of the observing
257 * progress dialogs or by calling item->cancel() in some other way. The owner
258 * is responsible for calling setComplete() on the item, even if it is canceled.
259 * Use the standardCancelHandler() slot if that is all you want to do on cancel.
260 *
261 * Note that if you request an item with a certain id and there is already
262 * one with that id, there will not be a new one created but the existing
263 * one will be returned. This is convenient for accessing items that are
264 * needed regularly without the to store a pointer to them or to add child
265 * items to parents by id.
266 */
267class KDEPIM_EXPORT ProgressManager : public QObject
268{
269 Q_OBJECT
270
271 friend struct ProgressManagerPrivate;
272
273public:
274 ~ProgressManager() override;
275
276 /**
277 * @return The singleton instance of this class.
278 */
279 static ProgressManager *instance();
280
281 /**
282 * Use this to acquire a unique id number which can be used to discern
283 * an operation from all others going on at the same time. Use that
284 * number as the id string for your progressItem to ensure it is unique.
285 * @return
286 */
287 [[nodiscard]] static QString getUniqueID();
288
289 /**
290 * Creates a ProgressItem with a unique id and the given label.
291 * This is the simplest way to acquire a progress item. It will not
292 * have a parent and will be set to be cancellable and not using crypto.
293 */
294 static ProgressItem *createProgressItem(unsigned int progressType, const QString &label);
295
296 /**
297 * Creates a ProgressItem with a unique id and the given label.
298 * This is the simplest way to acquire a progress item. It will not
299 * have a parent and will be set to be cancellable and not using crypto.
300 */
301 static ProgressItem *createProgressItem(const QString &label);
302
303 /**
304 * Creates a new progressItem with the given parent, id, label and initial
305 * status.
306 *
307 * @param parent Specify an already existing item as the parent of this one.
308 * @param id Used to identify this operation for cancel and progress info.
309 * @param label The text to be displayed by progress handlers
310 * @param status Additional text to be displayed for the item.
311 * @param canBeCanceled can the user cancel this operation?
312 * @param usesCrypto does the operation use secure transports (SSL)
313 * Cancelling the parent will cancel the children as well (if they can be
314 * canceled) and ongoing children prevent parents from finishing.
315 * @return The ProgressItem representing the operation.
316 */
317 static ProgressItem *createProgressItem(ProgressItem *parent,
318 const QString &id,
319 const QString &label,
320 const QString &status = QString(),
321 bool canBeCanceled = true,
322 KPIM::ProgressItem::CryptoStatus cryptoStatus = KPIM::ProgressItem::Unencrypted);
323
324 /**
325 * Use this version if you have the id string of the parent and want to
326 * add a subjob to it.
327 */
328 static ProgressItem *createProgressItem(const QString &parent,
329 const QString &id,
330 const QString &label,
331 const QString &status = QString(),
332 bool canBeCanceled = true,
333 KPIM::ProgressItem::CryptoStatus cryptoStatus = KPIM::ProgressItem::Unencrypted);
334
335 /**
336 * Version without a parent.
337 */
338 static ProgressItem *createProgressItem(const QString &id,
339 const QString &label,
340 const QString &status = QString(),
341 bool canBeCanceled = true,
342 KPIM::ProgressItem::CryptoStatus cryptoStatus = KPIM::ProgressItem::Unencrypted);
343
344 /**
345 * @return true when there are no more progress items.
346 */
347 [[nodiscard]] bool isEmpty() const;
348
349 /**
350 * @return the only top level progressitem when there's only one.
351 * Returns 0 if there is no item, or more than one top level item.
352 * Since this is used to calculate the overall progress, it will also return
353 * 0 if there is an item which uses a busy indicator, since that will invalidate
354 * the overall progress.
355 */
356 ProgressItem *singleItem() const;
357
358 /**
359 * Ask all listeners to show the progress dialog, because there is
360 * something that wants to be shown.
361 */
362 static void emitShowProgressDialog();
363
364 ProgressItem *progressItem(const QString &id) const;
365
366Q_SIGNALS:
367 /** @see ProgressItem::progressItemAdded() */
369 /** @see ProgressItem::progressItemProgress() */
371 /** @see ProgressItem::progressItemCompleted() */
373 /** @see ProgressItem::progressItemCanceled() */
375 /** @see ProgressItem::progressItemStatus() */
377 /** @see ProgressItem::progressItemLabel() */
379 /** @see ProgressItem::progressItemCryptoStatus() */
380 void progressItemCryptoStatus(KPIM::ProgressItem *, KPIM::ProgressItem::CryptoStatus);
381 /** @see ProgressItem::progressItemUsesBusyIndicator */
383
384 /**
385 * Emitted when an operation requests the listeners to be shown.
386 * Use emitShowProgressDialog() to trigger it.
387 */
389
390public Q_SLOTS:
391
392 /**
393 * Calls setCompleted() on the item, to make sure it goes away.
394 * Provided for convenience.
395 * @param item the canceled item.
396 */
397 void slotStandardCancelHandler(KPIM::ProgressItem *item);
398
399 /**
400 * Aborts all running jobs. Bound to "Esc"
401 */
402 void slotAbortAll();
403
404private Q_SLOTS:
405 void slotTransactionCompleted(KPIM::ProgressItem *item);
406
407private:
408 KDEPIM_NO_EXPORT ProgressManager();
409 // prevent unsolicited copies
410 KDEPIM_NO_EXPORT ProgressManager(const ProgressManager &);
411
412 KDEPIM_NO_EXPORT ProgressItem *createProgressItemImpl(ProgressItem *parent,
413 const QString &id,
414 const QString &label,
415 const QString &status,
416 bool cancellable,
417 ProgressItem::CryptoStatus cryptoStatus,
418 unsigned int progressType = 0);
419
420 KDEPIM_NO_EXPORT ProgressItem *createProgressItemImpl(const QString &parent,
421 const QString &id,
422 const QString &label,
423 const QString &status,
424 bool cancellable,
425 ProgressItem::CryptoStatus cryptoStatus,
426 unsigned int progressType = 0);
427 KDEPIM_NO_EXPORT void emitShowProgressDialogImpl();
428
429 QHash<QString, ProgressItem *> mTransactions;
430 static unsigned int uID;
431};
432}
The ProgressItem class.
void progressItemAdded(KPIM::ProgressItem *)
Emitted when a new ProgressItem is added.
void progressItemCryptoStatus(KPIM::ProgressItem *, KPIM::ProgressItem::CryptoStatus)
Emitted when the crypto status of an item changed.
void progressItemLabel(KPIM::ProgressItem *, const QString &)
Emitted when the label of an item changed.
void progressItemCompleted(KPIM::ProgressItem *)
Emitted when a progress item was completed.
void progressItemStatus(KPIM::ProgressItem *, const QString &)
Emitted when the status message of an item changed.
void progressItemUsesBusyIndicator(KPIM::ProgressItem *item, bool value)
Emitted when the busy indicator state of an item changes.
void progressItemProgress(KPIM::ProgressItem *, unsigned int)
Emitted when the progress value of an item changes.
void progressItemCanceled(KPIM::ProgressItem *)
Emitted when an item was canceled.
The ProgressManager singleton keeps track of all ongoing transactions and notifies observers (progres...
void progressItemAdded(KPIM::ProgressItem *)
void progressItemCanceled(KPIM::ProgressItem *)
void progressItemLabel(KPIM::ProgressItem *, const QString &)
void progressItemUsesBusyIndicator(KPIM::ProgressItem *, bool)
void progressItemStatus(KPIM::ProgressItem *, const QString &)
void progressItemCryptoStatus(KPIM::ProgressItem *, KPIM::ProgressItem::CryptoStatus)
void progressItemProgress(KPIM::ProgressItem *, unsigned int)
void progressItemCompleted(KPIM::ProgressItem *)
void showProgressDialog()
Emitted when an operation requests the listeners to be shown.
Q_SCRIPTABLE CaptureState status()
Class KCheckComboBox::KCheckComboBoxPrivate.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:18:17 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.