From 3ba9ee97797e4c41f2475139582bbdbf8f590a92 Mon Sep 17 00:00:00 2001 From: Teo Mrnjavac Date: Mon, 27 May 2013 21:35:40 +0200 Subject: [PATCH] Send tracks via context menu. --- src/libtomahawk/ContextMenu.cpp | 54 ++++++++++++++++++++++++++++++--- src/libtomahawk/ContextMenu.h | 29 ++++++++++-------- 2 files changed, 66 insertions(+), 17 deletions(-) diff --git a/src/libtomahawk/ContextMenu.cpp b/src/libtomahawk/ContextMenu.cpp index 9096d3c80..277723b8c 100644 --- a/src/libtomahawk/ContextMenu.cpp +++ b/src/libtomahawk/ContextMenu.cpp @@ -1,6 +1,7 @@ /* === This file is part of Tomahawk Player - === * * Copyright 2010-2011, Christian Muehlhaeuser + * Copyright 2013, Teo Mrnjavac * * Tomahawk is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -39,12 +40,14 @@ using namespace Tomahawk; ContextMenu::ContextMenu( QWidget* parent ) : QMenu( parent ) + , m_playlists_sigmap( 0 ) + , m_sources_sigmap( 0 ) , m_loveAction( 0 ) { m_sigmap = new QSignalMapper( this ); connect( m_sigmap, SIGNAL( mapped( int ) ), SLOT( onTriggered( int ) ) ); - m_supportedActions = ActionPlay | ActionQueue | ActionPlaylist | ActionCopyLink | ActionLove | ActionStopAfter | ActionPage | ActionEditMetadata; + m_supportedActions = ActionPlay | ActionQueue | ActionPlaylist | ActionCopyLink | ActionLove | ActionStopAfter | ActionPage | ActionEditMetadata | ActionSend; } @@ -79,13 +82,31 @@ ContextMenu::addToPlaylist( int playlistIdx ) } +void +ContextMenu::sendToSource( int sourceIdx ) +{ + const Tomahawk::source_ptr &src = m_sources.at( sourceIdx ); + foreach ( Tomahawk::query_ptr query, m_queries ) + { + query->queryTrack()->share( src ); + } +} + + bool -caseInsensitiveLessThan( Tomahawk::playlist_ptr &s1, Tomahawk::playlist_ptr &s2 ) +playlistsLessThan( const Tomahawk::playlist_ptr& s1, const Tomahawk::playlist_ptr& s2 ) { return s1->title().toLower() < s2->title().toLower(); } +bool +sourcesLessThan( const Tomahawk::source_ptr& s1, const Tomahawk::source_ptr& s2 ) +{ + return s1->friendlyName().toLower() < s2->friendlyName().toLower(); +} + + void ContextMenu::setQueries( const QList& queries ) { @@ -107,21 +128,44 @@ ContextMenu::setQueries( const QList& queries ) // Get the current list of all playlists. m_playlists = QList< Tomahawk::playlist_ptr >( SourceList::instance()->getLocal()->dbCollection()->playlists() ); // Sort the playlist - qSort( m_playlists.begin(), m_playlists.end(), caseInsensitiveLessThan ); + qSort( m_playlists.begin(), m_playlists.end(), playlistsLessThan ); + if ( m_playlists_sigmap != 0 ) + m_playlists_sigmap->deleteLater(); m_playlists_sigmap = new QSignalMapper( this ); // Build the menu listing all available playlists QMenu* playlistMenu = addMenu( tr( "Add to &Playlist" ) ); for ( int i = 0; i < m_playlists.length(); ++i ) { - QAction* action = new QAction( m_playlists.at(i)->title() , this ); + QAction* action = new QAction( m_playlists.at( i )->title() , this ); playlistMenu->addAction(action); m_playlists_sigmap->setMapping( action, i ); - connect( action, SIGNAL( triggered() ), m_playlists_sigmap, SLOT( map() )); + connect( action, SIGNAL( triggered() ), m_playlists_sigmap, SLOT( map() ) ); } connect( m_playlists_sigmap, SIGNAL( mapped( int ) ), this, SLOT( addToPlaylist( int ) ) ); } + if ( m_supportedActions & ActionSend ) //Send to someone's Inbox! + { + // Get the buddies list + m_sources = SourceList::instance()->sources( true ); + qSort( m_sources.begin(), m_sources.end(), sourcesLessThan ); + + if ( m_sources_sigmap != 0 ) + m_sources_sigmap->deleteLater(); + m_sources_sigmap = new QSignalMapper( this ); + + QMenu* sourcesMenu = addMenu( tr( "Send to &Friend" ) ); + for ( int i = 0; i < m_sources.length(); ++i ) + { + QAction* action = new QAction( m_sources.at( i )->friendlyName(), this ); + sourcesMenu->addAction( action ); + m_sources_sigmap->setMapping( action, i ); + connect( action, SIGNAL( triggered() ), m_sources_sigmap, SLOT( map() ) ); + } + connect( m_sources_sigmap, SIGNAL( mapped( int ) ), this, SLOT( sendToSource( int ) ) ); + } + if ( m_supportedActions & ActionStopAfter && itemCount() == 1 ) { if ( AudioEngine::instance()->stopAfterTrack() == queries.first() ) diff --git a/src/libtomahawk/ContextMenu.h b/src/libtomahawk/ContextMenu.h index 19484669d..5b60324d6 100644 --- a/src/libtomahawk/ContextMenu.h +++ b/src/libtomahawk/ContextMenu.h @@ -2,6 +2,7 @@ * * Copyright 2010-2011, Christian Muehlhaeuser * Copyright 2010-2011, Jeff Mitchell + * Copyright 2013, Teo Mrnjavac * * Tomahawk is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -36,18 +37,19 @@ Q_OBJECT public: enum MenuActions { - ActionPlay = 1, - ActionQueue = 2, - ActionDelete = 4, - ActionCopyLink = 8, - ActionLove = 16, - ActionStopAfter = 32, - ActionPage = 64, - ActionTrackPage = 65, - ActionArtistPage = 66, - ActionAlbumPage = 67, - ActionEditMetadata = 128, - ActionPlaylist = 256 + ActionPlay = 1, + ActionQueue = 2, + ActionDelete = 4, + ActionCopyLink = 8, + ActionLove = 16, + ActionStopAfter = 32, + ActionPage = 64, + ActionTrackPage = 65, + ActionArtistPage = 66, + ActionAlbumPage = 67, + ActionEditMetadata = 128, + ActionPlaylist = 256, + ActionSend = 512 }; explicit ContextMenu( QWidget* parent = 0 ); @@ -80,12 +82,14 @@ private slots: void openPage( MenuActions action ); void addToQueue(); void addToPlaylist( int playlistIdx ); + void sendToSource( int sourceIdx ); void onSocialActionsLoaded(); private: QSignalMapper* m_sigmap; QSignalMapper* m_playlists_sigmap; + QSignalMapper* m_sources_sigmap; int m_supportedActions; QAction* m_loveAction; @@ -94,6 +98,7 @@ private: QList< Tomahawk::query_ptr > m_queries; QList< Tomahawk::artist_ptr > m_artists; QList< Tomahawk::album_ptr > m_albums; + QList< Tomahawk::source_ptr > m_sources; Tomahawk::playlistinterface_ptr m_interface; };