From cec103897f5109c70f2fd69460d10d21fa4feded Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Wed, 14 Jan 2015 16:59:38 +0100 Subject: QSystemTrayIcon uses D-Bus org.freedesktop.Notifications on Linux MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If StatusNotifier is working, then QSystemTrayIcon::showMessage() will send notifications using the org.freedesktop.Notifications protocol. https://developer.gnome.org/notification-spec/ Task-number: QTBUG-4011 Task-number: QTBUG-31762 Change-Id: Ia1925ec3dd81b1b7b8f3b490b6364aaf8f93f395 Reviewed-by: Dmitry Shachnev Reviewed-by: Jørgen Lind --- src/platformsupport/dbustray/dbustray.pri | 2 + src/platformsupport/dbustray/qdbustrayicon.cpp | 39 +++++- src/platformsupport/dbustray/qdbustrayicon_p.h | 4 + .../dbustray/qxdgnotificationproxy.cpp | 47 +++++++ .../dbustray/qxdgnotificationproxy_p.h | 135 +++++++++++++++++++++ 5 files changed, 226 insertions(+), 1 deletion(-) create mode 100644 src/platformsupport/dbustray/qxdgnotificationproxy.cpp create mode 100644 src/platformsupport/dbustray/qxdgnotificationproxy_p.h (limited to 'src/platformsupport') diff --git a/src/platformsupport/dbustray/dbustray.pri b/src/platformsupport/dbustray/dbustray.pri index ca1ff8e434..734ee5fb40 100644 --- a/src/platformsupport/dbustray/dbustray.pri +++ b/src/platformsupport/dbustray/dbustray.pri @@ -6,8 +6,10 @@ HEADERS += \ $$PWD/qdbustrayicon_p.h \ $$PWD/qdbustraytypes_p.h \ $$PWD/qstatusnotifieritemadaptor_p.h \ + $$PWD/qxdgnotificationproxy_p.h \ SOURCES += \ $$PWD/qdbustrayicon.cpp \ $$PWD/qdbustraytypes.cpp \ $$PWD/qstatusnotifieritemadaptor.cpp \ + $$PWD/qxdgnotificationproxy.cpp \ diff --git a/src/platformsupport/dbustray/qdbustrayicon.cpp b/src/platformsupport/dbustray/qdbustrayicon.cpp index 1c21da849c..d753e94698 100644 --- a/src/platformsupport/dbustray/qdbustrayicon.cpp +++ b/src/platformsupport/dbustray/qdbustrayicon.cpp @@ -38,6 +38,7 @@ #include "qstatusnotifieritemadaptor_p.h" #include "qdbusmenuadaptor_p.h" #include "dbusmenu/qdbusplatformmenu_p.h" +#include "qxdgnotificationproxy_p.h" #include #include @@ -54,6 +55,9 @@ Q_LOGGING_CATEGORY(qLcTray, "qt.qpa.tray") static const QString KDEItemFormat = QStringLiteral("org.kde.StatusNotifierItem-%1-%2"); static const QString TempFileTemplate = QDir::tempPath() + QStringLiteral("/qt-trayicon-XXXXXX.png"); +static const QString XdgNotificationService = QStringLiteral("org.freedesktop.Notifications"); +static const QString XdgNotificationPath = QStringLiteral("/org/freedesktop/Notifications"); +static const QString DefaultAction = QStringLiteral("default"); static int instanceCount = 0; /*! @@ -66,6 +70,7 @@ QDBusTrayIcon::QDBusTrayIcon() , m_adaptor(new QStatusNotifierItemAdaptor(this)) , m_menuAdaptor(Q_NULLPTR) , m_menu(Q_NULLPTR) + , m_notifier(Q_NULLPTR) , m_instanceId(KDEItemFormat.arg(QCoreApplication::applicationPid()).arg(++instanceCount)) , m_category(QStringLiteral("ApplicationStatus")) , m_defaultStatus(QStringLiteral("Active")) // be visible all the time. QSystemTrayIcon has no API to control this. @@ -152,8 +157,13 @@ QTemporaryFile *QDBusTrayIcon::tempIcon(const QIcon &icon) QDBusMenuConnection * QDBusTrayIcon::dBusConnection() { - if (!m_dbusConnection) + if (!m_dbusConnection) { m_dbusConnection = new QDBusMenuConnection(this); + m_notifier = new QXdgNotificationInterface(XdgNotificationService, + XdgNotificationPath, m_dbusConnection->connection(), this); + connect(m_notifier, SIGNAL(NotificationClosed(uint,uint)), this, SLOT(notificationClosed(uint,uint))); + connect(m_notifier, SIGNAL(ActionInvoked(uint,QString)), this, SLOT(actionInvoked(uint,QString))); + } return m_dbusConnection; } @@ -214,6 +224,7 @@ void QDBusTrayIcon::showMessage(const QString &title, const QString &msg, const m_messageTitle = title; m_message = msg; m_attentionIcon = icon; + QStringList notificationActions; switch (iconType) { case Information: m_attentionIconName = QStringLiteral("dialog-information"); @@ -223,6 +234,10 @@ void QDBusTrayIcon::showMessage(const QString &title, const QString &msg, const break; case Critical: m_attentionIconName = QStringLiteral("dialog-error"); + // If there are actions, the desktop notification may appear as a message dialog + // with button(s), which will interrupt the user and require a response. + // That is an optional feature in implementations of org.freedesktop.Notifications + notificationActions << DefaultAction << tr("OK"); break; default: m_attentionIconName.clear(); @@ -243,6 +258,28 @@ void QDBusTrayIcon::showMessage(const QString &title, const QString &msg, const m_attentionTimer.start(msecs); emit tooltipChanged(); emit attention(); + + // Desktop notification + QVariantMap hints; + // urgency levels according to https://developer.gnome.org/notification-spec/#urgency-levels + // 0 low, 1 normal, 2 critical + int urgency = static_cast(iconType) - 1; + if (urgency < 0) // no icon + urgency = 0; + hints.insert(QLatin1String("urgency"), QVariant(urgency)); + m_notifier->notify(QCoreApplication::applicationName(), 0, + m_attentionIconName, title, msg, notificationActions, hints, msecs); +} + +void QDBusTrayIcon::actionInvoked(uint id, const QString &action) +{ + qCDebug(qLcTray) << id << action; + emit messageClicked(); +} + +void QDBusTrayIcon::notificationClosed(uint id, uint reason) +{ + qCDebug(qLcTray) << id << reason; } bool QDBusTrayIcon::isSystemTrayAvailable() const diff --git a/src/platformsupport/dbustray/qdbustrayicon_p.h b/src/platformsupport/dbustray/qdbustrayicon_p.h index 1b5f262ef1..97a700917b 100644 --- a/src/platformsupport/dbustray/qdbustrayicon_p.h +++ b/src/platformsupport/dbustray/qdbustrayicon_p.h @@ -59,6 +59,7 @@ QT_BEGIN_NAMESPACE class QStatusNotifierItemAdaptor; class QDBusMenuAdaptor; class QDBusPlatformMenu; +class QXdgNotificationInterface; class QDBusTrayIcon: public QPlatformSystemTrayIcon { @@ -126,6 +127,8 @@ signals: private Q_SLOTS: void attentionTimerExpired(); + void actionInvoked(uint id, const QString &action); + void notificationClosed(uint id, uint reason); private: void setStatus(const QString &status); @@ -136,6 +139,7 @@ private: QStatusNotifierItemAdaptor *m_adaptor; QDBusMenuAdaptor *m_menuAdaptor; QDBusPlatformMenu *m_menu; + QXdgNotificationInterface *m_notifier; QString m_instanceId; QString m_category; QString m_defaultStatus; diff --git a/src/platformsupport/dbustray/qxdgnotificationproxy.cpp b/src/platformsupport/dbustray/qxdgnotificationproxy.cpp new file mode 100644 index 0000000000..a6b623a9e1 --- /dev/null +++ b/src/platformsupport/dbustray/qxdgnotificationproxy.cpp @@ -0,0 +1,47 @@ +/**************************************************************************** +** +** Copyright (C) 2015 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtGui module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qxdgnotificationproxy_p.h" + +QT_BEGIN_NAMESPACE + +QXdgNotificationInterface::QXdgNotificationInterface(const QString &service, const QString &path, const QDBusConnection &connection, QObject *parent) + : QDBusAbstractInterface(service, path, staticInterfaceName(), connection, parent) +{ +} + +QXdgNotificationInterface::~QXdgNotificationInterface() +{ +} + +QT_END_NAMESPACE diff --git a/src/platformsupport/dbustray/qxdgnotificationproxy_p.h b/src/platformsupport/dbustray/qxdgnotificationproxy_p.h new file mode 100644 index 0000000000..2a2a41d8ce --- /dev/null +++ b/src/platformsupport/dbustray/qxdgnotificationproxy_p.h @@ -0,0 +1,135 @@ +/**************************************************************************** +** +** Copyright (C) 2015 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtGui module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/* + This file was originally created by qdbusxml2cpp version 0.8 + Command line was: + qdbusxml2cpp -p qxdgnotificationproxy ../../3rdparty/dbus-ifaces/org.freedesktop.Notifications.xml + + However it is maintained manually. + + It is also not part of the public API. This header file may change from + version to version without notice, or even be removed. +*/ + +#ifndef QXDGNOTIFICATIONPROXY_P_H +#define QXDGNOTIFICATIONPROXY_P_H + +#include +#include +#include +#include +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +Q_DECLARE_LOGGING_CATEGORY(qLcTray) + +/* + * Proxy class for interface org.freedesktop.Notifications + */ +class QXdgNotificationInterface: public QDBusAbstractInterface +{ + Q_OBJECT +public: + static inline const char *staticInterfaceName() + { return "org.freedesktop.Notifications"; } + +public: + QXdgNotificationInterface(const QString &service, const QString &path, + const QDBusConnection &connection, QObject *parent = 0); + + ~QXdgNotificationInterface(); + +public Q_SLOTS: // METHODS + inline QDBusPendingReply<> closeNotification(uint id) + { + QList argumentList; + argumentList << QVariant::fromValue(id); + return asyncCallWithArgumentList(QStringLiteral("CloseNotification"), argumentList); + } + + inline QDBusPendingReply getCapabilities() + { + QList argumentList; + return asyncCallWithArgumentList(QStringLiteral("GetCapabilities"), argumentList); + } + + inline QDBusPendingReply getServerInformation() + { + QList argumentList; + return asyncCallWithArgumentList(QStringLiteral("GetServerInformation"), argumentList); + } + inline QDBusReply getServerInformation(QString &vendor, QString &version, QString &specVersion) + { + QList argumentList; + QDBusMessage reply = callWithArgumentList(QDBus::Block, QStringLiteral("GetServerInformation"), argumentList); + if (reply.type() == QDBusMessage::ReplyMessage && reply.arguments().count() == 4) { + vendor = qdbus_cast(reply.arguments().at(1)); + version = qdbus_cast(reply.arguments().at(2)); + specVersion = qdbus_cast(reply.arguments().at(3)); + } + return reply; + } + + // see https://developer.gnome.org/notification-spec/#basic-design + inline QDBusPendingReply notify(const QString &appName, uint replacesId, const QString &appIcon, + const QString &summary, const QString &body, const QStringList &actions, + const QVariantMap &hints, int timeout) + { + qCDebug(qLcTray) << appName << replacesId << appIcon << summary << body << actions << hints << timeout; + QList argumentList; + argumentList << QVariant::fromValue(appName) << QVariant::fromValue(replacesId) << + QVariant::fromValue(appIcon) << QVariant::fromValue(summary) << + QVariant::fromValue(body) << QVariant::fromValue(actions) << + QVariant::fromValue(hints) << QVariant::fromValue(timeout); + return asyncCallWithArgumentList(QStringLiteral("Notify"), argumentList); + } + +Q_SIGNALS: + void ActionInvoked(uint id, const QString &action_key); + void NotificationClosed(uint id, uint reason); +}; + +namespace org { + namespace freedesktop { + typedef ::QXdgNotificationInterface Notifications; + } +} + +QT_END_NAMESPACE + +#endif -- cgit v1.2.3