Skip to content

Commit a9f3096

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 a9f3096

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-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

+25-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
#include <QIcon>
1313
#include <QObject>
14+
#include <QThread>
15+
16+
#include <atomic>
1417

1518
QT_BEGIN_NAMESPACE
1619
class QSystemTrayIcon;
@@ -20,6 +23,23 @@ class QDBusInterface;
2023
#endif
2124
QT_END_NAMESPACE
2225

26+
class Notificator;
27+
28+
#ifdef USE_DBUS
29+
class DBusInitThread : public QThread
30+
{
31+
Q_OBJECT
32+
33+
Notificator& m_notificator;
34+
35+
public:
36+
DBusInitThread(Notificator& notificator) : m_notificator(notificator) {};
37+
38+
protected:
39+
void run() override;
40+
};
41+
#endif
42+
2343
/** Cross-platform desktop notification client. */
2444
class Notificator: public QObject
2545
{
@@ -63,11 +83,15 @@ public slots:
6383
UserNotificationCenter /**< Use the 10.8+ User Notification Center (Mac only) */
6484
};
6585
QString programName;
66-
Mode mode;
86+
std::atomic<Mode> mode;
6787
QSystemTrayIcon *trayIcon;
6888
#ifdef USE_DBUS
89+
QThread *m_dbus_init_thread{nullptr};
90+
protected:
6991
QDBusInterface *interface;
92+
friend class DBusInitThread;
7093

94+
private:
7195
void notifyDBus(Class cls, const QString &title, const QString &text, const QIcon &icon, int millisTimeout);
7296
#endif
7397
void notifySystray(Class cls, const QString &title, const QString &text, const QIcon &icon, int millisTimeout);

0 commit comments

Comments
 (0)