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#include <config-kjobwidgets.h>
21#if WITH_X11
22#include <private/qtx11extras_p.h>
23#endif
24
25enum DialogType { ErrorDialog, WarningDialog };
26
27struct MessageBoxData {
28 QWidget *widget;
29 DialogType type = ErrorDialog;
30 QString msg;
31};
32
33class KDialogJobUiDelegatePrivate : public QObject
34{
36public:
37 explicit KDialogJobUiDelegatePrivate(QObject *parent = nullptr);
38 ~KDialogJobUiDelegatePrivate() override;
39 void queuedMessageBox(QWidget *widget, DialogType type, const QString &msg);
40
41 QWidget *window;
42
43public Q_SLOTS:
44 void next();
45
46private:
47 bool running;
49};
50
51KDialogJobUiDelegatePrivate::KDialogJobUiDelegatePrivate(QObject *parent)
52 : QObject(parent)
53 , window(nullptr)
54 , running(false)
55{
56}
57
58KDialogJobUiDelegatePrivate::~KDialogJobUiDelegatePrivate()
59{
60 queue.clear();
61}
62
63void KDialogJobUiDelegatePrivate::next()
64{
65 if (queue.isEmpty()) {
66 running = false;
67 return;
68 }
69
71
72 // kmessagebox starts a new event loop which can cause this to get deleted
73 // https://bugs.kde.org/show_bug.cgi?id=356321#c16
75
76 switch (data->type) {
77 case ErrorDialog:
78 KMessageBox::error(data->widget, data->msg);
79 break;
80 case WarningDialog:
81 KMessageBox::information(data->widget, data->msg);
82 break;
83 };
84
85 if (!thisGuard) {
86 return;
87 }
88
89 QMetaObject::invokeMethod(this, &KDialogJobUiDelegatePrivate::next, Qt::QueuedConnection);
90}
91
92void KDialogJobUiDelegatePrivate::queuedMessageBox(QWidget *widget, DialogType type, const QString &msg)
93{
94 QSharedPointer<MessageBoxData> data(new MessageBoxData{widget, type, msg});
95
96 queue.enqueue(data);
97
98 if (!running) {
99 running = true;
100 QMetaObject::invokeMethod(this, &KDialogJobUiDelegatePrivate::next, Qt::QueuedConnection);
101 }
102}
103
106 , d(new KDialogJobUiDelegatePrivate)
107{
108}
109
111 : KJobUiDelegate(flags)
112 , d(new KDialogJobUiDelegatePrivate)
113{
114 d->window = window;
115}
116
118
120{
121 bool ret = KJobUiDelegate::setJob(job);
122#if WITH_X11
123 if (ret) {
124 unsigned long time = QX11Info::appUserTime();
126 }
127#endif
128 return ret;
129}
130
132{
133 if (job()) {
135 }
136 d->window = window;
137}
138
140{
141 if (d->window) {
142 return d->window;
143 } else if (job()) {
144 return KJobWidgets::window(job());
145 }
146 return nullptr;
147}
148
153
155{
157}
158
159void KDialogJobUiDelegate::showErrorMessage()
160{
161 if (job()->error() != KJob::KilledJobError) {
162 d->queuedMessageBox(window(), ErrorDialog, job()->errorString());
163 }
164}
165
166void KDialogJobUiDelegate::slotWarning(KJob * /*job*/, const QString &message)
167{
169 d->queuedMessageBox(window(), WarningDialog, message);
170 }
171}
172
173#include "kdialogjobuidelegate.moc"
174#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)
Type type(const QSqlDatabase &db)
void setWindow(QObject *job, QWidget *widget)
void updateUserTimestamp(QObject *job, unsigned long time)
unsigned long userTimestamp(QObject *job)
QWidget * window(QObject *job)
KGUIADDONS_EXPORT QWindow * 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)
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 Sat Dec 21 2024 16:57:50 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.