From 73e00e26c44a5f4495c7a82f550c0878af1d75ed Mon Sep 17 00:00:00 2001 From: "Uwe L. Korn" Date: Fri, 15 Feb 2013 15:05:20 +0100 Subject: [PATCH 1/3] Style notifications if the window manager supports it. --- .../linux/fdonotify/FdoNotifyPlugin.cpp | 33 +++++++++++++++++-- .../linux/fdonotify/FdoNotifyPlugin.h | 4 +++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/infoplugins/linux/fdonotify/FdoNotifyPlugin.cpp b/src/infoplugins/linux/fdonotify/FdoNotifyPlugin.cpp index 8efd484a1..e52e6bcc4 100644 --- a/src/infoplugins/linux/fdonotify/FdoNotifyPlugin.cpp +++ b/src/infoplugins/linux/fdonotify/FdoNotifyPlugin.cpp @@ -58,11 +58,15 @@ namespace InfoSystem FdoNotifyPlugin::FdoNotifyPlugin() : InfoPlugin() , m_nowPlayingId( 0 ) + , m_wmSupportsBodyMarkup( false ) { qDebug() << Q_FUNC_INFO; m_supportedPushTypes << InfoNotifyUser << InfoNowPlaying << InfoTrackUnresolved << InfoNowStopped; -} + // Query the window manager for its capabilties in styling notifications. + QDBusMessage message = QDBusMessage::createMethodCall( "org.freedesktop.Notifications", "/org/freedesktop/Notifications", "org.freedesktop.Notifications", "GetCapabilities" ); + QDBusConnection::sessionBus().callWithCallback( message, this, SLOT( dbusCapabiltiesReplyReceived( QDBusMessage ) ) ); +} FdoNotifyPlugin::~FdoNotifyPlugin() { @@ -70,6 +74,24 @@ FdoNotifyPlugin::~FdoNotifyPlugin() } +void +FdoNotifyPlugin::dbusCapabiltiesReplyReceived( const QDBusMessage &reply ) +{ + if (reply.type() != QDBusMessage::ReplyMessage ) { + tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Did not receive a ReplyMessage"; + } + const QStringList &list = reply.arguments().at( 0 ).toStringList(); + QListIterator iter( list ); + while ( iter.hasNext() ) { + QString capabilty = iter.next(); + if ( capabilty.compare( "body-markup" ) == 0 ) { + m_wmSupportsBodyMarkup = true; + } + } +} + + + void FdoNotifyPlugin::pushInfo( Tomahawk::InfoSystem::InfoPushData pushData ) { @@ -152,7 +174,12 @@ FdoNotifyPlugin::nowPlaying( const QVariant &input ) if ( !hash.contains( "title" ) || !hash.contains( "artist" ) || !hash.contains( "album" ) ) return; - QString messageText = tr( "Tomahawk is playing \"%1\" by %2%3." ) + // If the window manager supports notification styling then use it. + const char* messageTemplate = "\"%1\" by %2%3."; + if ( m_wmSupportsBodyMarkup ) { + messageTemplate = "%1
by %2%3."; + } + QString messageText = tr( messageTemplate ) .arg( hash[ "title" ] ) .arg( hash[ "artist" ] ) .arg( hash[ "album" ].isEmpty() ? QString() : QString( " %1" ).arg( tr( "on \"%1\"" ).arg( hash[ "album" ] ) ) ); @@ -164,7 +191,7 @@ FdoNotifyPlugin::nowPlaying( const QVariant &input ) arguments << QString( "Tomahawk" ); //app_name arguments << m_nowPlayingId; //notification_id arguments << QString(); //app_icon - arguments << QString( "Tomahawk" ); //summary + arguments << QString( "Tomahawk - Now Playing" ); //summary arguments << messageText; //body arguments << QStringList(); //actions QVariantMap dict; diff --git a/src/infoplugins/linux/fdonotify/FdoNotifyPlugin.h b/src/infoplugins/linux/fdonotify/FdoNotifyPlugin.h index 0dddb0727..f65bf0b02 100644 --- a/src/infoplugins/linux/fdonotify/FdoNotifyPlugin.h +++ b/src/infoplugins/linux/fdonotify/FdoNotifyPlugin.h @@ -45,6 +45,7 @@ protected slots: virtual void init() {} virtual void dbusPlayingReplyReceived( const QDBusMessage &reply ); + virtual void dbusCapabiltiesReplyReceived( const QDBusMessage &reply ); virtual void getInfo( Tomahawk::InfoSystem::InfoRequestData requestData ) { @@ -67,6 +68,9 @@ private: void nowPlaying( const QVariant &input ); quint32 m_nowPlayingId; + + // Does the window manger support basic XML-based markup (a small HTML subset), see Desktop Notifications specification + bool m_wmSupportsBodyMarkup; }; } From 689d97d6896f9eba58f5f55ad3a75c87efbf1a41 Mon Sep 17 00:00:00 2001 From: "Uwe L. Korn" Date: Sun, 24 Feb 2013 12:44:01 +0100 Subject: [PATCH 2/3] Style album message --- src/infoplugins/linux/fdonotify/FdoNotifyPlugin.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/infoplugins/linux/fdonotify/FdoNotifyPlugin.cpp b/src/infoplugins/linux/fdonotify/FdoNotifyPlugin.cpp index e52e6bcc4..acbf21092 100644 --- a/src/infoplugins/linux/fdonotify/FdoNotifyPlugin.cpp +++ b/src/infoplugins/linux/fdonotify/FdoNotifyPlugin.cpp @@ -176,13 +176,15 @@ FdoNotifyPlugin::nowPlaying( const QVariant &input ) // If the window manager supports notification styling then use it. const char* messageTemplate = "\"%1\" by %2%3."; + const char* albumMessageTemplate = "on \"%1\""; if ( m_wmSupportsBodyMarkup ) { messageTemplate = "%1
by %2%3."; + albumMessageTemplate = "
on %1"; } QString messageText = tr( messageTemplate ) .arg( hash[ "title" ] ) .arg( hash[ "artist" ] ) - .arg( hash[ "album" ].isEmpty() ? QString() : QString( " %1" ).arg( tr( "on \"%1\"" ).arg( hash[ "album" ] ) ) ); + .arg( hash[ "album" ].isEmpty() ? QString() : QString( " %1" ).arg( tr( albumMessageTemplate ).arg( hash[ "album" ] ) ) ); tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "sending message" << messageText; From 971e84337f669e39b00ae537060ea8ba457d30db Mon Sep 17 00:00:00 2001 From: "Uwe L. Korn" Date: Sun, 24 Feb 2013 16:30:28 +0100 Subject: [PATCH 3/3] More translator friendly string formatting * Add escaping of supplied strings since they may interfere with the xml-based markup. --- .../linux/fdonotify/FdoNotifyPlugin.cpp | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/infoplugins/linux/fdonotify/FdoNotifyPlugin.cpp b/src/infoplugins/linux/fdonotify/FdoNotifyPlugin.cpp index acbf21092..c07694560 100644 --- a/src/infoplugins/linux/fdonotify/FdoNotifyPlugin.cpp +++ b/src/infoplugins/linux/fdonotify/FdoNotifyPlugin.cpp @@ -48,6 +48,8 @@ #include #include +// QTextDocument provides Qt::escape() +#include namespace Tomahawk { @@ -174,17 +176,28 @@ FdoNotifyPlugin::nowPlaying( const QVariant &input ) if ( !hash.contains( "title" ) || !hash.contains( "artist" ) || !hash.contains( "album" ) ) return; + QString messageText; // If the window manager supports notification styling then use it. - const char* messageTemplate = "\"%1\" by %2%3."; - const char* albumMessageTemplate = "on \"%1\""; if ( m_wmSupportsBodyMarkup ) { - messageTemplate = "%1
by %2%3."; - albumMessageTemplate = "
on %1"; + // Remark: If using xml-based markup in notifications, the supplied strings need to be escaped. + QString album; + if ( !hash[ "album" ].isEmpty() ) + album = tr( "
on %1" ).arg( Qt::escape( hash[ "album" ] ) ); + messageText = tr( "%1
by %2%3." ) + .arg( Qt::escape( hash[ "title" ] ) ) + .arg( Qt::escape( hash[ "artist" ] ) ) + .arg( album ); } - QString messageText = tr( messageTemplate ) + else + { + QString album; + if ( !hash[ "album" ].isEmpty() ) + album = QString( " %1" ).arg( tr( "on \"%1\"" ).arg( hash[ "album" ] ) ); + messageText = tr( "\"%1\" by %2%3." ) .arg( hash[ "title" ] ) .arg( hash[ "artist" ] ) - .arg( hash[ "album" ].isEmpty() ? QString() : QString( " %1" ).arg( tr( albumMessageTemplate ).arg( hash[ "album" ] ) ) ); + .arg( album ); + } tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "sending message" << messageText;