KJobWidgets

kdialogjobuidelegate.cpp
1 /*
2  This file is part of the KDE libraries
3  SPDX-FileCopyrightText: 2000 Stephan Kulow <[email protected]>
4  SPDX-FileCopyrightText: 2000 David Faure <[email protected]>
5  SPDX-FileCopyrightText: 2006, 2007 Kevin Ottens <[email protected]>
6 
7  SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9 
10 #include "kdialogjobuidelegate.h"
11 
12 #include <QPointer>
13 #include <QQueue>
14 #include <QWidget>
15 
16 #include <KJob>
17 #include <KMessageBox>
18 #include <kjobwidgets.h>
19 
20 #include <config-kjobwidgets.h>
21 #if HAVE_X11
22 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
23 #include <private/qtx11extras_p.h>
24 #else
25 #include <QX11Info>
26 #endif
27 #endif
28 
29 struct MessageBoxData {
30  QWidget *widget;
32  QString msg;
33 };
34 
35 class KDialogJobUiDelegatePrivate : public QObject
36 {
37  Q_OBJECT
38 public:
39  explicit KDialogJobUiDelegatePrivate(QObject *parent = nullptr);
40  ~KDialogJobUiDelegatePrivate() override;
41  void queuedMessageBox(QWidget *widget, KMessageBox::DialogType type, const QString &msg);
42 
43  QWidget *window;
44 
45 public Q_SLOTS:
46  void next();
47 
48 private:
49  bool running;
51 };
52 
53 KDialogJobUiDelegatePrivate::KDialogJobUiDelegatePrivate(QObject *parent)
54  : QObject(parent)
55  , window(nullptr)
56  , running(false)
57 {
58 }
59 
60 KDialogJobUiDelegatePrivate::~KDialogJobUiDelegatePrivate()
61 {
62  queue.clear();
63 }
64 
65 void KDialogJobUiDelegatePrivate::next()
66 {
67  if (queue.isEmpty()) {
68  running = false;
69  return;
70  }
71 
72  QSharedPointer<MessageBoxData> data = queue.dequeue();
73 
74  // kmessagebox starts a new event loop which can cause this to get deleted
75  // https://bugs.kde.org/show_bug.cgi?id=356321#c16
77  KMessageBox::messageBox(data->widget, data->type, data->msg);
78 
79  if (!thisGuard) {
80  return;
81  }
82 
83  QMetaObject::invokeMethod(this, &KDialogJobUiDelegatePrivate::next, Qt::QueuedConnection);
84 }
85 
86 void KDialogJobUiDelegatePrivate::queuedMessageBox(QWidget *widget, KMessageBox::DialogType type, const QString &msg)
87 {
88  QSharedPointer<MessageBoxData> data(new MessageBoxData);
89  data->type = type;
90  data->widget = widget;
91  data->msg = msg;
92 
93  queue.enqueue(data);
94 
95  if (!running) {
96  running = true;
97  QMetaObject::invokeMethod(this, &KDialogJobUiDelegatePrivate::next, Qt::QueuedConnection);
98  }
99 }
100 
102  : KJobUiDelegate()
103  , d(new KDialogJobUiDelegatePrivate)
104 {
105 }
106 
108  : KJobUiDelegate(flags)
109  , d(new KDialogJobUiDelegatePrivate)
110 {
111  d->window = window;
112 }
113 
115 
117 {
118  bool ret = KJobUiDelegate::setJob(job);
119 #if HAVE_X11
120  if (ret) {
121  unsigned long time = QX11Info::appUserTime();
123  }
124 #endif
125  return ret;
126 }
127 
129 {
130  if (job()) {
132  }
133  d->window = window;
134 }
135 
137 {
138  if (d->window) {
139  return d->window;
140  } else if (job()) {
141  return KJobWidgets::window(job());
142  }
143  return nullptr;
144 }
145 
147 {
149 }
150 
152 {
154 }
155 
156 void KDialogJobUiDelegate::showErrorMessage()
157 {
158  if (job()->error() != KJob::KilledJobError) {
159  d->queuedMessageBox(window(), KMessageBox::Error, job()->errorString());
160  }
161 }
162 
163 void KDialogJobUiDelegate::slotWarning(KJob * /*job*/, const QString &plain, const QString & /*rich*/)
164 {
166  d->queuedMessageBox(window(), KMessageBox::Information, plain);
167  }
168 }
169 
170 #include "kdialogjobuidelegate.moc"
Q_OBJECTQ_OBJECT
Q_SLOTSQ_SLOTS
KJOBWIDGETS_EXPORT void setWindow(KJob *job, QWidget *widget)
Associate this job with a window given by window.
Definition: kjobwidgets.cpp:14
bool setJob(KJob *job) override
void updateUserTimestamp(unsigned long time)
Updates the last user action timestamp to the given time.
KJOBWIDGETS_EXPORT unsigned long userTimestamp(KJob *job)
Returns the last user action timestamp.
Definition: kjobwidgets.cpp:44
virtual void setWindow(QWidget *window)
Associate this delegate with a window given by window.
virtual bool setJob(KJob *job)
QWidget * window() const
Returns the window this delegate is associated with.
QueuedConnection
unsigned long userTimestamp() const
ButtonCode messageBox(QWidget *parent, DialogType type, const QString &text, const QString &title=QString(), const KGuiItem &buttonYes=KStandardGuiItem::yes(), const KGuiItem &buttonNo=KStandardGuiItem::no(), const KGuiItem &buttonCancel=KStandardGuiItem::cancel(), const QString &dontShowAskAgainName=QString(), Options options=Notify)
KJob * job() const
KDialogJobUiDelegate()
Constructs a new KDialogJobUiDelegate.
~KDialogJobUiDelegate() override
Destroys the KDialogJobUiDelegate.
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0, QGenericArgument val1, QGenericArgument val2, QGenericArgument val3, QGenericArgument val4, QGenericArgument val5, QGenericArgument val6, QGenericArgument val7, QGenericArgument val8, QGenericArgument val9)
const QList< QKeySequence > & next()
KJOBWIDGETS_EXPORT void updateUserTimestamp(KJob *job, unsigned long time)
Updates the last user action timestamp to the given time.
Definition: kjobwidgets.cpp:36
KJOBWIDGETS_EXPORT QWidget * window(KJob *job)
Return the window associated with this job.
Definition: kjobwidgets.cpp:20
bool isAutoWarningHandlingEnabled() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Oct 6 2022 04:15:43 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.