Skip to content

Commit

Permalink
Merge branch 'jb51221' into 'master'
Browse files Browse the repository at this point in the history
[DBus] Allow p2p DBus to be used instead of the session bus. JB#51221

See merge request mer-core/nemo-qml-plugin-notifications!27
  • Loading branch information
Andrew Branson committed Jun 1, 2021
2 parents 2fd028e + a404fec commit 2fd616c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
34 changes: 29 additions & 5 deletions src/notification.cpp
Expand Up @@ -39,6 +39,9 @@

#include <time.h>

#define DBUS_SERVICE "org.freedesktop.Notifications"
#define DBUS_PATH "/org/freedesktop/Notifications"

namespace {

const char *HINT_CATEGORY = "category";
Expand Down Expand Up @@ -104,18 +107,24 @@ static inline QString processName() {
return QCoreApplication::applicationName();
}

//! A proxy for accessing the notification manager
Q_GLOBAL_STATIC_WITH_ARGS(NotificationManagerProxy, notificationManagerProxyInstance, ("org.freedesktop.Notifications", "/org/freedesktop/Notifications", QDBusConnection::sessionBus()))
Q_GLOBAL_STATIC(NotificationConnectionManager, connMgr)

NotificationManagerProxy *notificationManager()
{
if (!notificationManagerProxyInstance.exists()) {
if (connMgr()->proxy.isNull()) {
qDBusRegisterMetaType<NotificationData>();
qDBusRegisterMetaType<QList<NotificationData> >();
qDBusRegisterMetaType<NotificationImage>();
QString serviceName(DBUS_SERVICE);
QDBusConnection *conn = connMgr()->dBusConnection.data();
if (conn && conn->isConnected() && conn->baseService().isEmpty()) {
// p2p connection - no service name
serviceName.clear();
}
connMgr()->proxy.reset(new NotificationManagerProxy(serviceName, DBUS_PATH,
conn ? *conn : QDBusConnection::sessionBus()));
}

return notificationManagerProxyInstance();
return connMgr()->proxy.data();
}

QString encodeDBusCall(const QString &service, const QString &path, const QString &iface, const QString &method, const QVariantList &arguments)
Expand Down Expand Up @@ -1828,4 +1837,19 @@ const QDBusArgument &operator>>(const QDBusArgument &argument, NotificationData
return argument;
}

bool NotificationConnectionManager::useDBusConnection(const QDBusConnection &conn)
{
if (connMgr()->proxy.isNull()) {
if (conn.isConnected()) {
connMgr()->dBusConnection.reset(new QDBusConnection(conn));
return true;
} else {
qWarning() << "Supplied DBus connection is not connected.";
}
} else {
qWarning() << "Cannot override DBus connection - notifications already exist.";
}
return false;
}

#include "moc_notification.cpp"
11 changes: 11 additions & 0 deletions src/notification_p.h
Expand Up @@ -37,6 +37,7 @@
#include <QDateTime>
#include <QVariantHash>
#include <QDBusArgument>
#include <QSharedPointer>

struct NotificationData
{
Expand All @@ -55,6 +56,16 @@ struct NotificationData
qint32 expireTimeout = -1;
};

class NotificationManagerProxy;

class Q_DECL_EXPORT NotificationConnectionManager {
public:
QSharedPointer<NotificationManagerProxy> proxy;
QSharedPointer<QDBusConnection> dBusConnection;
// For platforms where the Notifications interface is hosted on a p2p bus
static bool useDBusConnection(const QDBusConnection &bus);
};

Q_DECL_EXPORT QDBusArgument &operator<<(QDBusArgument &, const NotificationData &);
Q_DECL_EXPORT const QDBusArgument &operator>>(const QDBusArgument &, NotificationData &);

Expand Down
2 changes: 1 addition & 1 deletion src/src.pro
Expand Up @@ -20,7 +20,7 @@ headers.files = notification.h notification_p.h
headers.path = /usr/include/nemonotifications-qt5

QMAKE_PKGCONFIG_NAME = lib$$TARGET
QMAKE_PKGCONFIG_DESCRIPTION = Convenience library or sending notifications
QMAKE_PKGCONFIG_DESCRIPTION = Convenience library for sending notifications
QMAKE_PKGCONFIG_LIBDIR = $$target.path
QMAKE_PKGCONFIG_INCDIR = $$headers.path
QMAKE_PKGCONFIG_DESTDIR = pkgconfig
Expand Down

0 comments on commit 2fd616c

Please sign in to comment.