Skip to content

Commit 805f9d7

Browse files
authored
Fix hang caused by no Notifications service registered in DBus (#4357)
Issue #4353 Add a check when looking up the `org.freedesktop.Notifications` service to see if the it actually exists before trying to fetch it Add a check on QDBus service lookup in screengrabber freedesktop
1 parent 1d74602 commit 805f9d7

File tree

2 files changed

+48
-26
lines changed

2 files changed

+48
-26
lines changed

src/utils/screengrabber.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,18 @@ void ScreenGrabber::freeDesktopPortal(bool& ok, QPixmap& res)
6161
{
6262

6363
#if !(defined(Q_OS_MACOS) || defined(Q_OS_WIN))
64+
auto* connectionInterface = QDBusConnection::sessionBus().interface();
65+
auto service = QStringLiteral("org.freedesktop.portal.Desktop");
66+
67+
if (!connectionInterface->isServiceRegistered(service)) {
68+
ok = false;
69+
AbstractLogger::error() << tr(
70+
"Could not locate the `org.freedesktop.portal.Desktop` service");
71+
return;
72+
}
73+
6474
QDBusInterface screenshotInterface(
65-
QStringLiteral("org.freedesktop.portal.Desktop"),
75+
service,
6676
QStringLiteral("/org/freedesktop/portal/desktop"),
6777
QStringLiteral("org.freedesktop.portal.Screenshot"));
6878

@@ -72,7 +82,7 @@ void ScreenGrabber::freeDesktopPortal(bool& ok, QPixmap& res)
7282

7383
// premake interface
7484
auto* request = new OrgFreedesktopPortalRequestInterface(
75-
QStringLiteral("org.freedesktop.portal.Desktop"),
85+
service,
7686
"/org/freedesktop/portal/desktop/request/" +
7787
QDBusConnection::sessionBus().baseService().remove(':').replace('.',
7888
'_') +

src/utils/systemnotification.cpp

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#include "systemnotification.h"
22
#include "src/core/flameshot.h"
3+
#include "src/utils/abstractlogger.h"
34
#include "src/utils/confighandler.h"
45
#include <QApplication>
56
#include <QUrl>
67

78
#if !(defined(Q_OS_MACOS) || defined(Q_OS_WIN))
89
#include <QDBusConnection>
10+
#include <QDBusConnectionInterface>
911
#include <QDBusInterface>
1012
#include <QDBusMessage>
1113
#else
@@ -27,12 +29,20 @@ SystemNotification::SystemNotification(QObject* parent)
2729
if (!ConfigHandler().showDesktopNotification()) {
2830
return;
2931
}
30-
m_interface =
31-
new QDBusInterface(QStringLiteral("org.freedesktop.Notifications"),
32-
QStringLiteral("/org/freedesktop/Notifications"),
33-
QStringLiteral("org.freedesktop.Notifications"),
34-
QDBusConnection::sessionBus(),
35-
this);
32+
auto bus = QDBusConnection::sessionBus();
33+
auto* connectionInterface = bus.interface();
34+
35+
auto service = QStringLiteral("org.freedesktop.Notifications");
36+
auto path = QStringLiteral("/org/freedesktop/Notifications");
37+
auto interface = QStringLiteral("org.freedesktop.Notifications");
38+
39+
if (connectionInterface->isServiceRegistered(service)) {
40+
m_interface = new QDBusInterface(service, path, interface, bus, this);
41+
} else {
42+
AbstractLogger::warning(AbstractLogger::Stderr |
43+
AbstractLogger::LogFile)
44+
<< tr("No DBus System Notification service found");
45+
}
3646
#endif
3747
}
3848

@@ -63,24 +73,26 @@ void SystemNotification::sendMessage(const QString& text,
6373
},
6474
Qt::QueuedConnection);
6575
#else
66-
QList<QVariant> args;
67-
QVariantMap hintsMap;
68-
if (!savePath.isEmpty()) {
69-
QUrl fullPath = QUrl::fromLocalFile(savePath);
70-
// allows the notification to be dragged and dropped
71-
hintsMap[QStringLiteral("x-kde-urls")] =
72-
QStringList({ fullPath.toString() });
73-
}
76+
if (nullptr != m_interface && m_interface->isValid()) {
77+
QList<QVariant> args;
78+
QVariantMap hintsMap;
79+
if (!savePath.isEmpty()) {
80+
QUrl fullPath = QUrl::fromLocalFile(savePath);
81+
// allows the notification to be dragged and dropped
82+
hintsMap[QStringLiteral("x-kde-urls")] =
83+
QStringList({ fullPath.toString() });
84+
}
7485

75-
args << (qAppName()) // appname
76-
<< static_cast<unsigned int>(0) // id
77-
<< FLAMESHOT_ICON // icon
78-
<< title // summary
79-
<< text // body
80-
<< QStringList() // actions
81-
<< hintsMap // hints
82-
<< timeout; // timeout
83-
m_interface->callWithArgumentList(
84-
QDBus::AutoDetect, QStringLiteral("Notify"), args);
86+
args << (qAppName()) // appname
87+
<< static_cast<unsigned int>(0) // id
88+
<< FLAMESHOT_ICON // icon
89+
<< title // summary
90+
<< text // body
91+
<< QStringList() // actions
92+
<< hintsMap // hints
93+
<< timeout; // timeout
94+
m_interface->callWithArgumentList(
95+
QDBus::AutoDetect, QStringLiteral("Notify"), args);
96+
}
8597
#endif
8698
}

0 commit comments

Comments
 (0)