Skip to content

Commit 0530dd0

Browse files
committed
GUI: Initialise DBus notifications in another thread
QDBusInterface's constructor can hang if org.freedesktop.Notifications is missing, so avoid delaying startup waiting for a timeout
1 parent 41cced2 commit 0530dd0

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

src/qt/notificator.cpp

+17-8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@
3131
#ifdef USE_DBUS
3232
// https://wiki.ubuntu.com/NotificationDevelopmentGuidelines recommends at least 128
3333
const int FREEDESKTOP_NOTIFICATION_ICON_SIZE = 128;
34+
35+
void DBusInitThread::run() {
36+
auto interface = new QDBusInterface("org.freedesktop.Notifications", "/org/freedesktop/Notifications", "org.freedesktop.Notifications");
37+
if (!interface->isValid()) {
38+
delete interface;
39+
return;
40+
}
41+
interface->moveToThread(m_notificator.thread());
42+
m_notificator.interface = interface;
43+
m_notificator.mode = Notificator::Freedesktop;
44+
}
45+
3446
#endif
3547

3648
Notificator::Notificator(const QString &programName, QSystemTrayIcon *trayicon, QWidget *parent) :
@@ -48,12 +60,8 @@ Notificator::Notificator(const QString &programName, QSystemTrayIcon *trayicon,
4860
mode = QSystemTray;
4961
}
5062
#ifdef USE_DBUS
51-
interface = new QDBusInterface("org.freedesktop.Notifications",
52-
"/org/freedesktop/Notifications", "org.freedesktop.Notifications");
53-
if(interface->isValid())
54-
{
55-
mode = Freedesktop;
56-
}
63+
m_dbus_init_thread = new DBusInitThread(*this);
64+
m_dbus_init_thread->start();
5765
#endif
5866
#ifdef Q_OS_MAC
5967
// check if users OS has support for NSUserNotification
@@ -82,6 +90,8 @@ Notificator::Notificator(const QString &programName, QSystemTrayIcon *trayicon,
8290
Notificator::~Notificator()
8391
{
8492
#ifdef USE_DBUS
93+
m_dbus_init_thread->wait();
94+
delete m_dbus_init_thread;
8595
delete interface;
8696
#endif
8797
}
@@ -296,8 +306,7 @@ void Notificator::notifyMacUserNotificationCenter(Class cls, const QString &titl
296306

297307
void Notificator::notify(Class cls, const QString &title, const QString &text, const QIcon &icon, int millisTimeout)
298308
{
299-
switch(mode)
300-
{
309+
switch (Mode(mode)) {
301310
#ifdef USE_DBUS
302311
case Freedesktop:
303312
notifyDBus(cls, title, text, icon, millisTimeout);

src/qt/notificator.h

+23-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <QIcon>
1313
#include <QObject>
14+
#include <QThread>
1415

1516
QT_BEGIN_NAMESPACE
1617
class QSystemTrayIcon;
@@ -20,6 +21,23 @@ class QDBusInterface;
2021
#endif
2122
QT_END_NAMESPACE
2223

24+
class Notificator;
25+
26+
#ifdef USE_DBUS
27+
class DBusInitThread : public QThread
28+
{
29+
Q_OBJECT
30+
31+
Notificator& m_notificator;
32+
33+
public:
34+
DBusInitThread(Notificator& notificator) : m_notificator(notificator) {};
35+
36+
protected:
37+
void run() override;
38+
};
39+
#endif
40+
2341
/** Cross-platform desktop notification client. */
2442
class Notificator: public QObject
2543
{
@@ -63,11 +81,15 @@ public slots:
6381
UserNotificationCenter /**< Use the 10.8+ User Notification Center (Mac only) */
6482
};
6583
QString programName;
66-
Mode mode;
84+
std::atomic<Mode> mode;
6785
QSystemTrayIcon *trayIcon;
6886
#ifdef USE_DBUS
87+
QThread *m_dbus_init_thread{nullptr};
88+
protected:
6989
QDBusInterface *interface;
90+
friend class DBusInitThread;
7091

92+
private:
7193
void notifyDBus(Class cls, const QString &title, const QString &text, const QIcon &icon, int millisTimeout);
7294
#endif
7395
void notifySystray(Class cls, const QString &title, const QString &text, const QIcon &icon, int millisTimeout);

0 commit comments

Comments
 (0)