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