diff --git a/data/images/inbox-512x512.png b/data/images/inbox-512x512.png new file mode 100644 index 000000000..806661b5e Binary files /dev/null and b/data/images/inbox-512x512.png differ diff --git a/resources.qrc b/resources.qrc index cf7910429..7e742aca0 100644 --- a/resources.qrc +++ b/resources.qrc @@ -153,5 +153,6 @@ data/images/inbox.svg data/images/new-inbox.svg data/images/outbox.svg + data/images/inbox-512x512.png diff --git a/src/infoplugins/linux/fdonotify/FdoNotifyPlugin.cpp b/src/infoplugins/linux/fdonotify/FdoNotifyPlugin.cpp index 67c9028a0..425af986e 100644 --- a/src/infoplugins/linux/fdonotify/FdoNotifyPlugin.cpp +++ b/src/infoplugins/linux/fdonotify/FdoNotifyPlugin.cpp @@ -63,7 +63,7 @@ FdoNotifyPlugin::FdoNotifyPlugin() , m_wmSupportsBodyMarkup( false ) { qDebug() << Q_FUNC_INFO; - m_supportedPushTypes << InfoNotifyUser << InfoNowPlaying << InfoTrackUnresolved << InfoNowStopped; + m_supportedPushTypes << InfoNotifyUser << InfoNowPlaying << InfoTrackUnresolved << InfoNowStopped << InfoInboxReceived; // Query the window manager for its capabilties in styling notifications. QDBusMessage message = QDBusMessage::createMethodCall( "org.freedesktop.Notifications", "/org/freedesktop/Notifications", "org.freedesktop.Notifications", "GetCapabilities" ); @@ -127,6 +127,10 @@ FdoNotifyPlugin::pushInfo( Tomahawk::InfoSystem::InfoPushData pushData ) nowPlaying( pushData.infoPair.second ); return; + case Tomahawk::InfoSystem::InfoInboxReceived: + inboxReceived( pushData.infoPair.second ); + return; + default: return; } @@ -166,6 +170,73 @@ FdoNotifyPlugin::notifyUser( const QString& messageText ) QDBusConnection::sessionBus().send( message ); } +void FdoNotifyPlugin::inboxReceived(const QVariant &input) +{ + tDebug( LOGVERBOSE ) << Q_FUNC_INFO; + if ( !input.canConvert< QVariantMap >() ) + return; + + QVariantMap map = input.toMap(); + + if ( !map.contains( "trackinfo" ) || !map[ "trackinfo" ].canConvert< Tomahawk::InfoSystem::InfoStringHash >() ) + return; + + if ( !map.contains( "sourceinfo" ) || !map[ "sourceinfo" ].canConvert< Tomahawk::InfoSystem::InfoStringHash >() ) + return; + + InfoStringHash hash = map[ "trackinfo" ].value< Tomahawk::InfoSystem::InfoStringHash >(); + if ( !hash.contains( "title" ) || !hash.contains( "artist" ) ) + return; + + InfoStringHash src = map[ "sourceinfo" ].value< Tomahawk::InfoSystem::InfoStringHash >(); + if ( !src.contains( "friendlyname" ) ) + return; + + QString messageText; + // If the window manager supports notification styling then use it. + if ( m_wmSupportsBodyMarkup ) + { + // Remark: If using xml-based markup in notifications, the supplied strings need to be escaped. + messageText = tr( "%1 sent you\n%2%4 %3.", "%1 is a nickname, %2 is a title, %3 is an artist, %4 is the preposition used to link track and artist ('by' in english)" ) + .arg( Qt::escape( src["friendlyname"] ) ) + .arg( Qt::escape( hash[ "title" ] ) ) + .arg( Qt::escape( hash[ "artist" ] ) ) + .arg( QString( "\n%1" ).arg( tr( "by", "preposition to link track and artist" ) ) ); + + // Dirty hack(TM) so that KNotify/QLabel recognizes the message as Rich Text + messageText = QString( "%1" ).arg( messageText ); + } + else + { + messageText = tr( "%1 sent you \"%2\" by %3.", "%1 is a nickname, %2 is a title, %3 is an artist" ) + .arg( src["friendlyname"] ) + .arg( hash[ "title" ] ) + .arg( hash[ "artist" ] ); + } + + tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "sending message" << messageText; + + QDBusMessage message = QDBusMessage::createMethodCall( "org.freedesktop.Notifications", "/org/freedesktop/Notifications", "org.freedesktop.Notifications", "Notify" ); + QList arguments; + arguments << QString( "Tomahawk" ); //app_name + arguments << m_nowPlayingId; //notification_id + arguments << QString(); //app_icon + arguments << QString( "Tomahawk - Track received" ); //summary + arguments << messageText; //body + arguments << QStringList(); //actions + QVariantMap dict; + dict["desktop-entry"] = QString( "tomahawk" ); + + // Convert image to QVariant and scale to a consistent size. + dict[ "image_data" ] = ImageConverter::variantForImage( QImage( RESPATH "images/inbox-512x512.png" ).scaledToHeight( getNotificationIconHeight() ) ); + + arguments << dict; //hints + arguments << qint32( -1 ); //expire_timeout + message.setArguments( arguments ); + + // Handle reply in a callback, so that this a non-blocking call + QDBusConnection::sessionBus().send( message ); +} void FdoNotifyPlugin::nowPlaying( const QVariant& input ) diff --git a/src/infoplugins/linux/fdonotify/FdoNotifyPlugin.h b/src/infoplugins/linux/fdonotify/FdoNotifyPlugin.h index ff4743cdd..32003633f 100644 --- a/src/infoplugins/linux/fdonotify/FdoNotifyPlugin.h +++ b/src/infoplugins/linux/fdonotify/FdoNotifyPlugin.h @@ -65,6 +65,7 @@ private: int getNotificationIconHeight(); void notifyUser( const QString& messageText ); + void inboxReceived( const QVariant& input ); void nowPlaying( const QVariant& input ); quint32 m_nowPlayingId; diff --git a/src/libtomahawk/Typedefs.h b/src/libtomahawk/Typedefs.h index c876b084d..639a24555 100644 --- a/src/libtomahawk/Typedefs.h +++ b/src/libtomahawk/Typedefs.h @@ -216,7 +216,9 @@ namespace Tomahawk InfoNotifyUser = 100, - InfoLastInfo = 101 //WARNING: *ALWAYS* keep this last! + InfoInboxReceived = 101, + + InfoLastInfo = 102 //WARNING: *ALWAYS* keep this last! }; class InfoPlugin; diff --git a/src/libtomahawk/infosystem/InfoSystem.cpp b/src/libtomahawk/infosystem/InfoSystem.cpp index 0ba727535..fb303cffa 100644 --- a/src/libtomahawk/infosystem/InfoSystem.cpp +++ b/src/libtomahawk/infosystem/InfoSystem.cpp @@ -225,7 +225,7 @@ InfoSystem::getInfo( const QString &caller, const QVariantMap &customData, const bool InfoSystem::pushInfo( InfoPushData pushData ) { - tDebug() << Q_FUNC_INFO << "type is " << pushData.type; + tDebug() << Q_FUNC_INFO << "type is" << pushData.type; if ( !m_inited || !m_infoSystemWorkerThreadController->worker() ) { init(); diff --git a/src/libtomahawk/jobview/InboxJobItem.h b/src/libtomahawk/jobview/InboxJobItem.h index 91dd243b6..e9e90f35e 100644 --- a/src/libtomahawk/jobview/InboxJobItem.h +++ b/src/libtomahawk/jobview/InboxJobItem.h @@ -33,7 +33,7 @@ public: enum Side { Sending = 0, - Receiving + Receiving = 1 }; explicit InboxJobItem( Side side, diff --git a/src/libtomahawk/playlist/InboxModel.cpp b/src/libtomahawk/playlist/InboxModel.cpp index dd16e30d7..79aa0ed5b 100644 --- a/src/libtomahawk/playlist/InboxModel.cpp +++ b/src/libtomahawk/playlist/InboxModel.cpp @@ -23,6 +23,7 @@ #include "database/DatabaseCommand_DeleteInboxEntry.h" #include "database/DatabaseCommand_ModifyInboxEntry.h" #include "SourceList.h" +#include "TomahawkSettings.h" #include "utils/Logger.h" #include "utils/Closure.h" #include "jobview/JobStatusModel.h" @@ -138,6 +139,24 @@ InboxModel::showNotification( InboxJobItem::Side side, JobStatusView::instance()->model()->addJob( new InboxJobItem( side, src->friendlyName(), track ) ); + + if ( side == InboxJobItem::Receiving ) + { + Tomahawk::InfoSystem::InfoStringHash trackInfo; + trackInfo["title"] = track->track(); + trackInfo["artist"] = track->artist(); + + Tomahawk::InfoSystem::InfoStringHash sourceInfo; + sourceInfo["friendlyname"] = src->friendlyName(); + + QVariantMap playInfo; + playInfo["trackinfo"] = QVariant::fromValue< Tomahawk::InfoSystem::InfoStringHash >( trackInfo ); + playInfo["private"] = TomahawkSettings::instance()->privateListeningMode(); + playInfo["sourceinfo"] = QVariant::fromValue< Tomahawk::InfoSystem::InfoStringHash >( sourceInfo ); + + Tomahawk::InfoSystem::InfoPushData pushData ( "InboxModel", Tomahawk::InfoSystem::InfoInboxReceived, playInfo, Tomahawk::InfoSystem::PushShortUrlFlag ); + Tomahawk::InfoSystem::InfoSystem::instance()->pushInfo( pushData ); + } }