KJobWidgets

kdialogjobuidelegate.cpp
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2000 Stephan Kulow <coolo@kde.org>
4 SPDX-FileCopyrightText: 2000 David Faure <faure@kde.org>
5 SPDX-FileCopyrightText: 2006, 2007 Kevin Ottens <ervin@kde.org>
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 <KJobWidgets>
18#include <KMessageBox>
19
20#if __has_include(<private/qtx11extras_p.h>)
21#include <private/qtx11extras_p.h>
22#define HAVE_X11 1
23#else
24#define HAVE_X11 0
25#endif
26
27enum DialogType { ErrorDialog, WarningDialog };
28
29struct MessageBoxData {
30 QWidget *widget;
31 DialogType type = ErrorDialog;
32 QString msg;
33};
34
35class KDialogJobUiDelegatePrivate : public QObject
36{
38public:
39 explicit KDialogJobUiDelegatePrivate(QObject *parent = nullptr);
40 ~KDialogJobUiDelegatePrivate() override;
41 void queuedMessageBox(QWidget *widget, DialogType type, const QString &msg);
42
43 QWidget *window;
44
45public Q_SLOTS:
46 void next();
47
48private:
49 bool running;
51};
52
53KDialogJobUiDelegatePrivate::KDialogJobUiDelegatePrivate(QObject *parent)
54 : QObject(parent)
55 , window(nullptr)
56 , running(false)
57{
58}
59
60KDialogJobUiDelegatePrivate::~KDialogJobUiDelegatePrivate()
61{
62 queue.clear();
63}
64
65void KDialogJobUiDelegatePrivate::next()
66{
67 if (queue.isEmpty()) {
68 running = false;
69 return;
70 }
71
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
78 switch (data->type) {
79 case ErrorDialog:
80 KMessageBox::error(data->widget, data->msg);
81 break;
82 case WarningDialog:
83 KMessageBox::information(data->widget, data->msg);
84 break;
85 };
86
87 if (!thisGuard) {
88 return;
89 }
90
91 QMetaObject::invokeMethod(this, &KDialogJobUiDelegatePrivate::next, Qt::QueuedConnection);
92}
93
94void KDialogJobUiDelegatePrivate::queuedMessageBox(QWidget *widget, DialogType type, const QString &msg)
95{
96 QSharedPointer<MessageBoxData> data(new MessageBoxData{widget, type, msg});
97
98 queue.enqueue(data);
99
100 if (!running) {
101 running = true;
102 QMetaObject::invokeMethod(this, &KDialogJobUiDelegatePrivate::next, Qt::QueuedConnection);
103 }
104}
105
108 , d(new KDialogJobUiDelegatePrivate)
109{
110}
111
113 : KJobUiDelegate(flags)
114 , d(new KDialogJobUiDelegatePrivate)
115{
116 d->window = window;
117}
118
120
122{
123 bool ret = KJobUiDelegate::setJob(job);
124#if HAVE_X11
125 if (ret) {
126 unsigned long time = QX11Info::appUserTime();
128 }
129#endif
130 return ret;
131}
132
134{
135 if (job()) {
137 }
138 d->window = window;
139}
140
142{
143 if (d->window) {
144 return d->window;
145 } else if (job()) {
146 return KJobWidgets::window(job());
147 }
148 return nullptr;
149}
150
155
157{
159}
160
161void KDialogJobUiDelegate::showErrorMessage()
162{
163 if (job()->error() != KJob::KilledJobError) {
164 d->queuedMessageBox(window(), ErrorDialog, job()->errorString());
165 }
166}
167
168void KDialogJobUiDelegate::slotWarning(KJob * /*job*/, const QString &message)
169{
171 d->queuedMessageBox(window(), WarningDialog, message);
172 }
173}
174
175#include "kdialogjobuidelegate.moc"
176#include "moc_kdialogjobuidelegate.cpp"
KDialogJobUiDelegate()
Constructs a new KDialogJobUiDelegate.
virtual void setWindow(QWidget *window)
Associate this delegate with a window given by window.
QWidget * window() const
Returns the window this delegate is associated with.
bool setJob(KJob *job) override
void updateUserTimestamp(unsigned long time)
Updates the last user action timestamp to the given time.
unsigned long userTimestamp() const
~KDialogJobUiDelegate() override
Destroys the KDialogJobUiDelegate.
bool isAutoWarningHandlingEnabled() const
KJob * job() const
virtual bool setJob(KJob *job)
void setWindow(QObject *job, QWidget *widget)
void updateUserTimestamp(QObject *job, unsigned long time)
unsigned long userTimestamp(QObject *job)
QWidget * window(QObject *job)
void information(QWidget *parent, const QString &text, const QString &title=QString(), const QString &dontShowAgainName=QString(), Options options=Notify)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
bool invokeMethod(QObject *context, Functor &&function, FunctorReturnType *ret)
Q_OBJECTQ_OBJECT
Q_SLOTSQ_SLOTS
QObject * parent() const const
T dequeue()
void enqueue(const T &t)
QueuedConnection
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 14 2024 11:54:49 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.