Commit 0260e596 authored by Jan Grulich's avatar Jan Grulich
Browse files

Background portal: avoid crash when allowing app to run in background

We need to return as soon as possible from the notification callback, otherwise it can
happen that the notification is closed and deleted while we block the callback with
a message dialog.

BUG:438954
parent 921f8a83
......@@ -112,23 +112,28 @@ uint BackgroundPortal::NotifyBackground(const QDBusObjectPath &handle, const QSt
const QString text = i18n(
"This might be for a legitimate reason, but the application has not provided one."
"\n\nNote that forcing an application to quit might cause data loss.");
QMessageBox messageBox(QMessageBox::Question, title, text);
QPushButton *quitButton = messageBox.addButton(i18n("Force quit"), QMessageBox::RejectRole);
QPushButton *allowButton = messageBox.addButton(i18n("Allow"), QMessageBox::AcceptRole);
messageBox.exec();
BackgroundPortal::NotifyResult result = BackgroundPortal::Ignore;
if (messageBox.clickedButton() == quitButton) {
result = BackgroundPortal::Forbid;
} else if (messageBox.clickedButton() == allowButton) {
result = BackgroundPortal::Allow;
}
QMessageBox *messageBox = new QMessageBox(QMessageBox::Question, title, text);
messageBox->addButton(i18n("Force quit"), QMessageBox::RejectRole);
messageBox->addButton(i18n("Allow"), QMessageBox::AcceptRole);
const QVariantMap map = {{QStringLiteral("result"), static_cast<uint>(result)}};
QDBusMessage reply = message.createReply({static_cast<uint>(0), map});
if (!QDBusConnection::sessionBus().send(reply)) {
qCWarning(XdgDesktopPortalKdeBackground) << "Failed to send response";
}
messageBox->show();
connect(messageBox, &QMessageBox::accepted, this, [message, messageBox]() {
const QVariantMap map = {{QStringLiteral("result"), static_cast<uint>(BackgroundPortal::Allow)}};
QDBusMessage reply = message.createReply({static_cast<uint>(0), map});
if (!QDBusConnection::sessionBus().send(reply)) {
qCWarning(XdgDesktopPortalKdeBackground) << "Failed to send response";
}
messageBox->deleteLater();
});
connect(messageBox, &QMessageBox::rejected, this, [message, messageBox]() {
const QVariantMap map = {{QStringLiteral("result"), static_cast<uint>(BackgroundPortal::Forbid)}};
QDBusMessage reply = message.createReply({static_cast<uint>(0), map});
if (!QDBusConnection::sessionBus().send(reply)) {
qCWarning(XdgDesktopPortalKdeBackground) << "Failed to send response";
}
messageBox->deleteLater();
});
});
connect(notify, &KNotification::closed, this, [=]() {
if (notify->property("activated").toBool()) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment