QSystemTrayIcon uses D-Bus org.freedesktop.Notifications on Linux

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 <mitya57@gmail.com>
Reviewed-by: Jørgen Lind <jorgen.lind@theqtcompany.com>
bb10
Shawn Rutledge 2015-01-14 16:59:38 +01:00
parent d24cfc8b86
commit cec103897f
6 changed files with 263 additions and 1 deletions

View File

@ -0,0 +1,37 @@
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
<interface name="org.freedesktop.Notifications">
<signal name="NotificationClosed">
<arg name="id" type="u" direction="out"/>
<arg name="reason" type="u" direction="out"/>
</signal>
<signal name="ActionInvoked">
<arg name="id" type="u" direction="out"/>
<arg name="action_key" type="s" direction="out"/>
</signal>
<method name="Notify">
<annotation name="org.qtproject.QtDBus.QtTypeName.In6" value="QVariantMap"/>
<arg type="u" direction="out"/>
<arg name="app_name" type="s" direction="in"/>
<arg name="replaces_id" type="u" direction="in"/>
<arg name="app_icon" type="s" direction="in"/>
<arg name="summary" type="s" direction="in"/>
<arg name="body" type="s" direction="in"/>
<arg name="actions" type="as" direction="in"/>
<arg name="hints" type="a{sv}" direction="in"/>
<arg name="timeout" type="i" direction="in"/>
</method>
<method name="CloseNotification">
<arg name="id" type="u" direction="in"/>
</method>
<method name="GetCapabilities">
<arg type="as" name="caps" direction="out"/>
</method>
<method name="GetServerInformation">
<arg type="s" name="name" direction="out"/>
<arg type="s" name="vendor" direction="out"/>
<arg type="s" name="version" direction="out"/>
<arg type="s" name="spec_version" direction="out"/>
</method>
</interface>
</node>

View File

@ -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 \

View File

@ -38,6 +38,7 @@
#include "qstatusnotifieritemadaptor_p.h"
#include "qdbusmenuadaptor_p.h"
#include "dbusmenu/qdbusplatformmenu_p.h"
#include "qxdgnotificationproxy_p.h"
#include <qplatformmenu.h>
#include <qstring.h>
@ -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<int>(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

View File

@ -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;

View File

@ -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

View File

@ -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 <QtCore/QObject>
#include <QtCore/QByteArray>
#include <QtCore/QList>
#include <QtCore/QMap>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QVariant>
#include <QtDBus/QtDBus>
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<QVariant> argumentList;
argumentList << QVariant::fromValue(id);
return asyncCallWithArgumentList(QStringLiteral("CloseNotification"), argumentList);
}
inline QDBusPendingReply<QStringList> getCapabilities()
{
QList<QVariant> argumentList;
return asyncCallWithArgumentList(QStringLiteral("GetCapabilities"), argumentList);
}
inline QDBusPendingReply<QString, QString, QString, QString> getServerInformation()
{
QList<QVariant> argumentList;
return asyncCallWithArgumentList(QStringLiteral("GetServerInformation"), argumentList);
}
inline QDBusReply<QString> getServerInformation(QString &vendor, QString &version, QString &specVersion)
{
QList<QVariant> argumentList;
QDBusMessage reply = callWithArgumentList(QDBus::Block, QStringLiteral("GetServerInformation"), argumentList);
if (reply.type() == QDBusMessage::ReplyMessage && reply.arguments().count() == 4) {
vendor = qdbus_cast<QString>(reply.arguments().at(1));
version = qdbus_cast<QString>(reply.arguments().at(2));
specVersion = qdbus_cast<QString>(reply.arguments().at(3));
}
return reply;
}
// see https://developer.gnome.org/notification-spec/#basic-design
inline QDBusPendingReply<uint> 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<QVariant> 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