summaryrefslogtreecommitdiff
path: root/kde-frameworks/kio/files/kio-5.113.0-fix-crash-while-copying.patch
blob: 845e6bc64339659f82f2804774bf8e3c83f07b50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
From 6bea074739d5a75920d5540bc266549df5642511 Mon Sep 17 00:00:00 2001
From: Akseli Lahtinen <akselmo@akselmo.dev>
Date: Fri, 1 Dec 2023 11:27:26 +0000
Subject: [PATCH] WidgetsAskUserActionHandler: Use QPointer to check the
 validity of parent widgets

If Dolphin is closed during the copying process,
and the overwrite/skip dialog appears,
this crashes the copying process since the
parent shows to faulty location.

Use QPointer to check that the parent widgets are still
valid. If not, we just use nullptr, and the dialogs
will still open.

This also cleans some repetition in code.

BUG:448532
(cherry picked from commit bdef648edd54e146c30e881d8eb95990a59c5bbc)
---
 src/widgets/widgetsaskuseractionhandler.cpp | 90 +++++++++------------
 1 file changed, 39 insertions(+), 51 deletions(-)

diff --git a/src/widgets/widgetsaskuseractionhandler.cpp b/src/widgets/widgetsaskuseractionhandler.cpp
index fe2975d0ce..9cbaaec99f 100644
--- a/src/widgets/widgetsaskuseractionhandler.cpp
+++ b/src/widgets/widgetsaskuseractionhandler.cpp
@@ -22,6 +22,7 @@
 
 #include <QApplication>
 #include <QDialogButtonBox>
+#include <QPointer>
 #include <QRegularExpression>
 #include <QUrl>
 
@@ -40,7 +41,10 @@
     void savePersistentUserReply(KIO::AskUserActionInterface::MessageDialogType type, KConfigGroup &cg, const QString &dontAskAgainName, int result);
 
     WidgetsAskUserActionHandler *const q;
-    QWidget *m_parentWidget = nullptr;
+    QPointer<QWidget> m_parentWidget = nullptr;
+
+    QWidget *getParentWidget(KJob *job);
+    QWidget *getParentWidget(QWidget *widget);
 };
 
 bool KIO::WidgetsAskUserActionHandlerPrivate::gotPersistentUserReply(KIO::AskUserActionInterface::MessageDialogType type,
@@ -106,6 +110,36 @@
     }
 }
 
+QWidget *KIO::WidgetsAskUserActionHandlerPrivate::getParentWidget(KJob *job)
+{
+    // This needs to be in qpointer, otherwise copying process
+    // will crash if done in background and dolphin is closed
+    QPointer<QWidget> parentWidget = nullptr;
+
+    if (job) {
+        parentWidget = KJobWidgets::window(job);
+    }
+
+    return getParentWidget(parentWidget);
+}
+
+QWidget *KIO::WidgetsAskUserActionHandlerPrivate::getParentWidget(QWidget *widget)
+{
+    // This needs to be in qpointer, otherwise copying process
+    // will crash if done in background and dolphin is closed
+    QPointer<QWidget> parentWidget = widget;
+
+    if (!parentWidget) {
+        parentWidget = this->m_parentWidget;
+    }
+
+    if (!parentWidget) {
+        parentWidget = qApp->activeWindow();
+    }
+
+    return parentWidget;
+}
+
 KIO::WidgetsAskUserActionHandler::WidgetsAskUserActionHandler(QObject *parent)
     : KIO::AskUserActionInterface(parent)
     , d(new WidgetsAskUserActionHandlerPrivate(this))
@@ -128,22 +162,8 @@
                                                      const QDateTime &mtimeSrc,
                                                      const QDateTime &mtimeDest)
 {
-    QWidget *parentWidget = nullptr;
-
-    if (job) {
-        parentWidget = KJobWidgets::window(job);
-    }
-
-    if (!parentWidget) {
-        parentWidget = d->m_parentWidget;
-    }
-
-    if (!parentWidget) {
-        parentWidget = qApp->activeWindow();
-    }
-
     QMetaObject::invokeMethod(qGuiApp, [=] {
-        auto *dlg = new KIO::RenameDialog(parentWidget, title, src, dest, options, sizeSrc, sizeDest, ctimeSrc, ctimeDest, mtimeSrc, mtimeDest);
+        auto *dlg = new KIO::RenameDialog(d->getParentWidget(job), title, src, dest, options, sizeSrc, sizeDest, ctimeSrc, ctimeDest, mtimeSrc, mtimeDest);
 
         dlg->setAttribute(Qt::WA_DeleteOnClose);
         dlg->setWindowModality(Qt::WindowModal);
@@ -161,22 +181,8 @@
 
 void KIO::WidgetsAskUserActionHandler::askUserSkip(KJob *job, KIO::SkipDialog_Options options, const QString &errorText)
 {
-    QWidget *parentWidget = nullptr;
-
-    if (job) {
-        parentWidget = KJobWidgets::window(job);
-    }
-
-    if (!parentWidget) {
-        parentWidget = d->m_parentWidget;
-    }
-
-    if (!parentWidget) {
-        parentWidget = qApp->activeWindow();
-    }
-
     QMetaObject::invokeMethod(qGuiApp, [=] {
-        auto *dlg = new KIO::SkipDialog(parentWidget, options, errorText);
+        auto *dlg = new KIO::SkipDialog(d->getParentWidget(job), options, errorText);
         dlg->setAttribute(Qt::WA_DeleteOnClose);
         dlg->setWindowModality(Qt::WindowModal);
 
@@ -373,16 +379,6 @@
         return;
     }
 
-    QWidget *parentWidget = parent;
-
-    if (!parentWidget) {
-        parentWidget = d->m_parentWidget;
-    }
-
-    if (!parentWidget) {
-        parentWidget = qApp->activeWindow();
-    }
-
     const KGuiItem primaryActionButton(primaryActionText, primaryActionIconName);
     const KGuiItem secondaryActionButton(secondaryActionText, secondaryActionIconName);
 
@@ -412,7 +408,7 @@
         hasCancelButton = true;
         break;
     case AskUserActionInterface::SSLMessageBox:
-        d->sslMessageBox(text, metaData, parentWidget);
+        d->sslMessageBox(text, metaData, d->getParentWidget(parent));
         return;
     case AskUserActionInterface::Information:
         dlgType = KMessageDialog::Information;
@@ -442,7 +438,7 @@
 
     QMetaObject::invokeMethod(qGuiApp, [=]() {
         auto cancelButton = hasCancelButton ? KStandardGuiItem::cancel() : KGuiItem();
-        auto *dialog = new KMessageDialog(dlgType, text, parentWidget);
+        auto *dialog = new KMessageDialog(dlgType, text, d->getParentWidget(parent));
 
         dialog->setAttribute(Qt::WA_DeleteOnClose);
         dialog->setCaption(title);
@@ -492,15 +488,7 @@
 
 void KIO::WidgetsAskUserActionHandlerPrivate::sslMessageBox(const QString &text, const KIO::MetaData &metaData, QWidget *parent)
 {
-    QWidget *parentWidget = parent;
-
-    if (!parentWidget) {
-        parentWidget = m_parentWidget;
-    }
-
-    if (!parentWidget) {
-        parentWidget = qApp->activeWindow();
-    }
+    QWidget *parentWidget = getParentWidget(parent);
 
     const QStringList sslList = metaData.value(QStringLiteral("ssl_peer_chain")).split(QLatin1Char('\x01'), Qt::SkipEmptyParts);