From 11c6d4cdca605cb203181925bc31b8ad8e40407b Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Sun, 22 Jan 2012 16:36:56 +0100 Subject: [PATCH] * Moved social actions from Result into Query. Added contextual menu entry for (un)loving. --- src/audiocontrols.cpp | 63 +++++-------- src/audiocontrols.h | 3 +- src/libtomahawk/contextmenu.cpp | 36 ++++++- src/libtomahawk/contextmenu.h | 7 +- .../databasecommand_loadsocialactions.cpp | 8 +- .../databasecommand_loadsocialactions.h | 12 +-- .../database/databasecommand_socialaction.h | 12 +-- src/libtomahawk/query.cpp | 94 +++++++++++++++++++ src/libtomahawk/query.h | 17 ++++ src/libtomahawk/result.cpp | 69 +++----------- src/libtomahawk/result.h | 19 +--- 11 files changed, 207 insertions(+), 133 deletions(-) diff --git a/src/audiocontrols.cpp b/src/audiocontrols.cpp index 7c974b384..46bb3d167 100644 --- a/src/audiocontrols.cpp +++ b/src/audiocontrols.cpp @@ -26,7 +26,6 @@ #include "audio/audioengine.h" #include "playlist/playlistview.h" #include "database/database.h" -#include "database/databasecommand_socialaction.h" #include "widgets/imagebutton.h" #include "utils/tomahawkutils.h" #include "utils/logger.h" @@ -37,8 +36,6 @@ using namespace Tomahawk; -static QString s_acInfoIdentifier = QString( "AUDIOCONTROLS" ); - AudioControls::AudioControls( QWidget* parent ) : QWidget( parent ) @@ -226,12 +223,12 @@ AudioControls::onPlaybackLoading( const Tomahawk::result_ptr& result ) if ( !m_currentTrack.isNull() ) { disconnect( m_currentTrack->album().data(), SIGNAL( updated() ), this, SLOT( onAlbumCoverUpdated() ) ); - disconnect( m_currentTrack.data(), SIGNAL( socialActionsLoaded() ), this, SLOT( socialActionsLoaded() ) ); + disconnect( m_currentTrack->toQuery().data(), SIGNAL( socialActionsLoaded() ), this, SLOT( onSocialActionsLoaded() ) ); } m_currentTrack = result; connect( m_currentTrack->album().data(), SIGNAL( updated() ), SLOT( onAlbumCoverUpdated() ) ); - connect( m_currentTrack.data(), SIGNAL( socialActionsLoaded() ), SLOT( socialActionsLoaded() ) ); + connect( m_currentTrack->toQuery().data(), SIGNAL( socialActionsLoaded() ), SLOT( onSocialActionsLoaded() ) ); ui->artistTrackLabel->setResult( result ); ui->albumLabel->setResult( result ); @@ -249,7 +246,7 @@ AudioControls::onPlaybackLoading( const Tomahawk::result_ptr& result ) ui->loveButton->setVisible( true ); setAlbumCover(); - result->loadSocialActions(); + setSocialActions(); } @@ -277,23 +274,28 @@ AudioControls::setAlbumCover() void -AudioControls::socialActionsLoaded() +AudioControls::onSocialActionsLoaded() { - Result* r = qobject_cast< Result* >( sender() ); - Q_ASSERT( r ); + Query* query = qobject_cast< Query* >( sender() ); + if ( !query || query != m_currentTrack->toQuery().data() ) + return; - if ( m_currentTrack.data() == r ) + setSocialActions(); +} + + +void +AudioControls::setSocialActions() +{ + if ( m_currentTrack->toQuery()->loved() ) { - if ( m_currentTrack->loved() ) - { - ui->loveButton->setPixmap( RESPATH "images/loved.png" ); - ui->loveButton->setChecked( true ); - } - else - { - ui->loveButton->setPixmap( RESPATH "images/not-loved.png" ); - ui->loveButton->setChecked( false ); - } + ui->loveButton->setPixmap( RESPATH "images/loved.png" ); + ui->loveButton->setChecked( true ); + } + else + { + ui->loveButton->setPixmap( RESPATH "images/not-loved.png" ); + ui->loveButton->setChecked( false ); } } @@ -583,30 +585,17 @@ AudioControls::droppedTracks( QList< query_ptr > tracks ) void AudioControls::onLoveButtonClicked( bool checked ) { - Tomahawk::InfoSystem::InfoStringHash trackInfo; - trackInfo["title"] = m_currentTrack->track(); - trackInfo["artist"] = m_currentTrack->artist()->name(); - trackInfo["album"] = m_currentTrack->album()->name(); - if ( checked ) { - Tomahawk::InfoSystem::InfoSystem::instance()->pushInfo( - s_acInfoIdentifier, Tomahawk::InfoSystem::InfoLove, - QVariant::fromValue< Tomahawk::InfoSystem::InfoStringHash >( trackInfo ) ); - - DatabaseCommand_SocialAction* cmd = new DatabaseCommand_SocialAction( m_currentTrack, QString( "Love" ), QString( "true") ); - Database::instance()->enqueue( QSharedPointer(cmd) ); ui->loveButton->setPixmap( RESPATH "images/loved.png" ); + + m_currentTrack->toQuery()->setLoved( true ); } else { - Tomahawk::InfoSystem::InfoSystem::instance()->pushInfo( - s_acInfoIdentifier, Tomahawk::InfoSystem::InfoUnLove, - QVariant::fromValue< Tomahawk::InfoSystem::InfoStringHash >( trackInfo ) ); - - DatabaseCommand_SocialAction* cmd = new DatabaseCommand_SocialAction( m_currentTrack, QString( "Love" ), QString( "false" ) ); - Database::instance()->enqueue( QSharedPointer(cmd) ); ui->loveButton->setPixmap( RESPATH "images/not-loved.png" ); + + m_currentTrack->toQuery()->setLoved( false ); } } diff --git a/src/audiocontrols.h b/src/audiocontrols.h index 0b8a6e5b3..7d7de87c6 100644 --- a/src/audiocontrols.h +++ b/src/audiocontrols.h @@ -83,10 +83,11 @@ private slots: void droppedTracks( QList ); - void socialActionsLoaded(); + void onSocialActionsLoaded(); private: void setAlbumCover(); + void setSocialActions(); Ui::AudioControls *ui; diff --git a/src/libtomahawk/contextmenu.cpp b/src/libtomahawk/contextmenu.cpp index b13540db0..da2812827 100644 --- a/src/libtomahawk/contextmenu.cpp +++ b/src/libtomahawk/contextmenu.cpp @@ -32,11 +32,12 @@ using namespace Tomahawk; ContextMenu::ContextMenu( QWidget* parent ) : QMenu( parent ) + , m_loveAction( 0 ) { m_sigmap = new QSignalMapper( this ); connect( m_sigmap, SIGNAL( mapped( int ) ), SLOT( onTriggered( int ) ) ); - m_supportedActions = ActionPlay | ActionQueue | ActionCopyLink; + m_supportedActions = ActionPlay | ActionQueue | ActionCopyLink | ActionLove; } @@ -59,7 +60,7 @@ ContextMenu::clear() unsigned int ContextMenu::itemCount() const { - return m_queries.count() + m_artists.count() + m_albums.count(); + return m_queries.count() + m_artists.count() + m_albums.count(); } @@ -81,8 +82,17 @@ ContextMenu::setQueries( const QList& queries ) addSeparator(); + if ( m_supportedActions & ActionLove && itemCount() == 1 ) + { + m_loveAction = addAction( tr( "&Love" ) ); + m_sigmap->setMapping( m_loveAction, ActionLove ); + + connect( queries.first().data(), SIGNAL( socialActionsLoaded() ), SLOT( onSocialActionsLoaded() ) ); + onSocialActionsLoaded(); + } + if ( m_supportedActions & ActionCopyLink && itemCount() == 1 ) - m_sigmap->setMapping( addAction( tr( "Copy Track &Link" ) ), ActionCopyLink ); + m_sigmap->setMapping( addAction( tr( "&Copy Track Link" ) ), ActionCopyLink ); addSeparator(); @@ -197,6 +207,10 @@ ContextMenu::onTriggered( int action ) copyLink(); break; + case ActionLove: + m_queries.first()->setLoved( !m_queries.first()->loved() ); + break; + default: emit triggered( action ); } @@ -232,3 +246,19 @@ ContextMenu::copyLink() GlobalActionManager::instance()->copyToClipboard( m_queries.first() ); } } + + +void +ContextMenu::onSocialActionsLoaded() +{ + if ( m_queries.first()->loved() ) + { + m_loveAction->setText( tr( "Un-&Love" ) ); + m_loveAction->setIcon( QIcon( RESPATH "images/not-loved.png" ) ); + } + else + { + m_loveAction->setText( tr( "&Love" ) ); + m_loveAction->setIcon( QIcon( RESPATH "images/loved.png" ) ); + } +} diff --git a/src/libtomahawk/contextmenu.h b/src/libtomahawk/contextmenu.h index af272262a..7330ef1f8 100644 --- a/src/libtomahawk/contextmenu.h +++ b/src/libtomahawk/contextmenu.h @@ -38,7 +38,8 @@ public: ActionPlay = 1, ActionQueue = 2, ActionDelete = 4, - ActionCopyLink = 8 + ActionCopyLink = 8, + ActionLove = 16 }; explicit ContextMenu( QWidget* parent = 0 ); @@ -68,10 +69,14 @@ private slots: void copyLink(); void addToQueue(); + void onSocialActionsLoaded(); + private: QSignalMapper* m_sigmap; int m_supportedActions; + QAction* m_loveAction; + QList m_queries; QList m_artists; QList m_albums; diff --git a/src/libtomahawk/database/databasecommand_loadsocialactions.cpp b/src/libtomahawk/database/databasecommand_loadsocialactions.cpp index 8f2b51e2f..efb5a9d50 100644 --- a/src/libtomahawk/database/databasecommand_loadsocialactions.cpp +++ b/src/libtomahawk/database/databasecommand_loadsocialactions.cpp @@ -39,13 +39,11 @@ DatabaseCommand_LoadSocialActions::exec( DatabaseImpl* dbi ) QVariant srcid = source()->isLocal() ? QVariant( QVariant::Int ) : source()->id(); - bool autoCreate = false; - int artid = dbi->artistId( m_artist, autoCreate ); + int artid = dbi->artistId( m_artist, false ); if( artid < 1 ) return; - autoCreate = false; // artistId overwrites autoCreate (reference) - int trkid = dbi->trackId( artid, m_track, autoCreate ); + int trkid = dbi->trackId( artid, m_track, false ); if( trkid < 1 ) return; @@ -72,6 +70,6 @@ DatabaseCommand_LoadSocialActions::exec( DatabaseImpl* dbi ) allSocialActions.append( action ); } - m_result->setAllSocialActions( allSocialActions ); + m_query->setAllSocialActions( allSocialActions ); } diff --git a/src/libtomahawk/database/databasecommand_loadsocialactions.h b/src/libtomahawk/database/databasecommand_loadsocialactions.h index 7a6ea5425..1cf6688d1 100644 --- a/src/libtomahawk/database/databasecommand_loadsocialactions.h +++ b/src/libtomahawk/database/databasecommand_loadsocialactions.h @@ -56,17 +56,17 @@ public: /** * \brief Overloaded constructor for DatabaseCommand_LoadSocialAction. - * \param result Pointer to a Tomahawk::Result. + * \param result A Tomahawk Query object. * \param parent Parent class. * * Constructor which creates a new database command for loading all social actions. */ - explicit DatabaseCommand_LoadSocialActions( Tomahawk::Result* result, QObject* parent = 0 ) - : DatabaseCommand( parent ), m_result( result ) + explicit DatabaseCommand_LoadSocialActions( const Tomahawk::query_ptr& query, QObject* parent = 0 ) + : DatabaseCommand( parent ), m_query( query ) { setSource( SourceList::instance()->getLocal() ); - setArtist( result->artist()->name() ); - setTrack( result->track() ); + setArtist( query->artist() ); + setTrack( query->track() ); } /** @@ -125,7 +125,7 @@ signals: void done( QList< Tomahawk::SocialAction >& allSocialActions ); private: - Tomahawk::Result* m_result; + Tomahawk::query_ptr m_query; QString m_artist; QString m_track; diff --git a/src/libtomahawk/database/databasecommand_socialaction.h b/src/libtomahawk/database/databasecommand_socialaction.h index 40e23ecf0..5bc480b71 100644 --- a/src/libtomahawk/database/databasecommand_socialaction.h +++ b/src/libtomahawk/database/databasecommand_socialaction.h @@ -61,20 +61,20 @@ public: /** * \brief Overloaded constructor for DatabaseCommand_SocialAction. - * \param result Pointer to a Tomahawk::Result. + * \param query A Tomahawk Query object. * \param action Name of the social action to be written to the database. * \param comment Comment associated with this social action. * \param parent Parent class. * * Constructor which creates a new database command for the specified social action. */ - explicit DatabaseCommand_SocialAction( const Tomahawk::result_ptr& result, QString action, QString comment="", QObject* parent = 0 ) - : DatabaseCommandLoggable( parent ), m_result( result ), m_action( action ) + explicit DatabaseCommand_SocialAction( const Tomahawk::query_ptr& query, QString action, QString comment = "", QObject* parent = 0 ) + : DatabaseCommandLoggable( parent ), m_query( query ), m_action( action ) { setSource( SourceList::instance()->getLocal() ); - setArtist( result->artist()->name() ); - setTrack( result->track() ); + setArtist( query->artist() ); + setTrack( query->track() ); setComment( comment ); setTimestamp( QDateTime::currentDateTime().toTime_t() ); } @@ -173,7 +173,7 @@ public: virtual bool groupable() const { return true; } private: - Tomahawk::result_ptr m_result; + Tomahawk::query_ptr m_query; QString m_artist; QString m_track; diff --git a/src/libtomahawk/query.cpp b/src/libtomahawk/query.cpp index 04daebaaa..625da37bc 100644 --- a/src/libtomahawk/query.cpp +++ b/src/libtomahawk/query.cpp @@ -25,6 +25,8 @@ #include "database/databasecommand_logplayback.h" #include "database/databasecommand_playbackhistory.h" #include "database/databasecommand_loadplaylistentries.h" +#include "database/databasecommand_loadsocialactions.h" +#include "database/databasecommand_socialaction.h" #include "album.h" #include "collection.h" #include "pipeline.h" @@ -71,6 +73,7 @@ Query::Query( const QString& artist, const QString& track, const QString& album, , m_artist( artist ) , m_album( album ) , m_track( track ) + , m_socialActionsLoaded( false ) { init(); @@ -475,6 +478,97 @@ Query::playedBy() const return m_playedBy; } + +void +Query::loadSocialActions() +{ + m_socialActionsLoaded = true; + query_ptr q = m_ownRef.toStrongRef(); + + DatabaseCommand_LoadSocialActions* cmd = new DatabaseCommand_LoadSocialActions( q ); + connect( cmd, SIGNAL( finished() ), SLOT( onSocialActionsLoaded() )); + Database::instance()->enqueue( QSharedPointer(cmd) ); +} + + +void +Query::onSocialActionsLoaded() +{ + parseSocialActions(); + + emit socialActionsLoaded(); +} + + +void +Query::setAllSocialActions( const QList< SocialAction >& socialActions ) +{ + m_allSocialActions = socialActions; +} + + +QList< SocialAction > +Query::allSocialActions() +{ + return m_allSocialActions; +} + + +void +Query::parseSocialActions() +{ + QListIterator< Tomahawk::SocialAction > it( m_allSocialActions ); + unsigned int highestTimestamp = 0; + + while ( it.hasNext() ) + { + Tomahawk::SocialAction socialAction; + socialAction = it.next(); + if ( socialAction.timestamp.toUInt() > highestTimestamp && socialAction.source.toInt() == SourceList::instance()->getLocal()->id() ) + { + m_currentSocialActions[ socialAction.action.toString() ] = socialAction.value.toBool(); + } + } +} + + +bool +Query::loved() +{ + if ( m_socialActionsLoaded ) + { + return m_currentSocialActions[ "Love" ].toBool(); + } + else + { + loadSocialActions(); + } +} + + +void +Query::setLoved( bool loved ) +{ + query_ptr q = m_ownRef.toStrongRef(); + if ( q ) + { + m_currentSocialActions[ "Loved" ] = loved; + + Tomahawk::InfoSystem::InfoStringHash trackInfo; + trackInfo["title"] = track(); + trackInfo["artist"] = artist(); + trackInfo["album"] = album(); + + Tomahawk::InfoSystem::InfoSystem::instance()->pushInfo( + id(), Tomahawk::InfoSystem::InfoLove, + QVariant::fromValue< Tomahawk::InfoSystem::InfoStringHash >( trackInfo ) ); + + DatabaseCommand_SocialAction* cmd = new DatabaseCommand_SocialAction( q, QString( "Love" ), loved ? QString( "true" ) : QString( "false" ) ); + Database::instance()->enqueue( QSharedPointer(cmd) ); + } +} + + int Query::levenshtein( const QString& source, const QString& target ) { diff --git a/src/libtomahawk/query.h b/src/libtomahawk/query.h index 57c5b1d22..b8b00f5c4 100644 --- a/src/libtomahawk/query.h +++ b/src/libtomahawk/query.h @@ -102,6 +102,13 @@ public: void setResolveFinished( bool resolved ) { m_resolveFinished = resolved; } void setPlayedBy( const Tomahawk::source_ptr& source, unsigned int playtime ); + void setLoved( bool loved ); + bool loved(); + + void loadSocialActions(); + QList< Tomahawk::SocialAction > allSocialActions(); + void setAllSocialActions( const QList< Tomahawk::SocialAction >& socialActions ); + QWeakPointer< Tomahawk::Query > weakRef() { return m_ownRef; } void setWeakRef( QWeakPointer< Tomahawk::Query > weakRef ) { m_ownRef = weakRef; } @@ -117,6 +124,9 @@ signals: void playableStateChanged( bool state ); void resolvingFinished( bool hasResults ); + // emitted when social actions are loaded + void socialActionsLoaded(); + public slots: /// (indirectly) called by resolver plugins when results are found void addResults( const QList< Tomahawk::result_ptr >& ); @@ -134,6 +144,7 @@ public slots: private slots: void onResultStatusChanged(); void refreshResults(); + void onSocialActionsLoaded(); private: Query(); @@ -149,6 +160,8 @@ private: void updateSortNames(); static int levenshtein( const QString& source, const QString& target ); + void parseSocialActions(); + QList< Tomahawk::artist_ptr > m_artists; QList< Tomahawk::album_ptr > m_albums; QList< Tomahawk::result_ptr > m_results; @@ -176,6 +189,10 @@ private: mutable QMutex m_mutex; QWeakPointer< Tomahawk::Query > m_ownRef; + + bool m_socialActionsLoaded; + QHash< QString, QVariant > m_currentSocialActions; + QList< SocialAction > m_allSocialActions; }; }; //ns diff --git a/src/libtomahawk/result.cpp b/src/libtomahawk/result.cpp index 1fd270c7a..013e60030 100644 --- a/src/libtomahawk/result.cpp +++ b/src/libtomahawk/result.cpp @@ -20,11 +20,11 @@ #include "album.h" #include "collection.h" +#include "source.h" #include "database/database.h" #include "database/databasecommand_resolve.h" #include "database/databasecommand_alltracks.h" #include "database/databasecommand_addfiles.h" -#include "database/databasecommand_loadsocialactions.h" #include "utils/logger.h" @@ -164,15 +164,19 @@ Result::toString() const Tomahawk::query_ptr -Result::toQuery() const +Result::toQuery() { - Tomahawk::query_ptr query = Tomahawk::Query::get( artist()->name(), track(), album()->name() ); - QList rl; - rl << Result::get( m_url ); + if ( m_query.isNull() ) + { + m_query = Tomahawk::Query::get( artist()->name(), track(), album()->name() ); + QList rl; + rl << Result::get( m_url ); - query->addResults( rl ); - query->setResolveFinished( true ); - return query; + m_query->addResults( rl ); + m_query->setResolveFinished( true ); + } + + return m_query; } @@ -200,55 +204,6 @@ Result::onOffline() } -void -Result::loadSocialActions() -{ - DatabaseCommand_LoadSocialActions* cmd = new DatabaseCommand_LoadSocialActions( this ); - connect( cmd, SIGNAL( finished() ), SLOT( onSocialActionsLoaded() )); - Database::instance()->enqueue( QSharedPointer(cmd) ); -} - - -void Result::onSocialActionsLoaded() -{ - parseSocialActions(); - - emit socialActionsLoaded(); -} - - -void -Result::setAllSocialActions(QList< SocialAction > socialActions) -{ - m_allSocialActions = socialActions; -} - - -QList< SocialAction > -Result::allSocialActions() -{ - return m_allSocialActions; -} - - -void -Result::parseSocialActions() -{ - QListIterator< Tomahawk::SocialAction > it( m_allSocialActions ); - unsigned int highestTimestamp = 0; - - while ( it.hasNext() ) - { - Tomahawk::SocialAction socialAction; - socialAction = it.next(); - if ( socialAction.timestamp.toUInt() > highestTimestamp && socialAction.source.toInt() == SourceList::instance()->getLocal()->id() ) - { - m_currentSocialActions[ socialAction.action.toString() ] = socialAction.value.toBool(); - } - } -} - - void Result::setArtist( const Tomahawk::artist_ptr& artist ) { diff --git a/src/libtomahawk/result.h b/src/libtomahawk/result.h index e7adc4a6e..5340ee6ff 100644 --- a/src/libtomahawk/result.h +++ b/src/libtomahawk/result.h @@ -60,7 +60,7 @@ public: QVariant toVariant() const; QString toString() const; - Tomahawk::query_ptr toQuery() const; + Tomahawk::query_ptr toQuery(); float score() const; RID id() const; @@ -80,8 +80,6 @@ public: unsigned int albumpos() const { return m_albumpos; } unsigned int modificationTime() const { return m_modtime; } int year() const { return m_year; } - bool loved() { return m_currentSocialActions[ "Love" ].toBool(); } - QList< Tomahawk::SocialAction > allSocialActions(); void setScore( float score ) { m_score = score; } void setTrackId( unsigned int id ) { m_trackId = id; } @@ -99,26 +97,17 @@ public: void setAlbumPos( unsigned int albumpos ) { m_albumpos = albumpos; } void setModificationTime( unsigned int modtime ) { m_modtime = modtime; } void setYear( unsigned int year ) { m_year = year; } - void setLoved( bool loved ) { m_currentSocialActions[ "Loved" ] = loved; } - void setAllSocialActions( QList< Tomahawk::SocialAction > socialActions ); - void loadSocialActions(); QVariantMap attributes() const { return m_attributes; } void setAttributes( const QVariantMap& map ) { m_attributes = map; updateAttributes(); } unsigned int trackId() const { return m_trackId; } unsigned int fileId() const { return m_fileId; } -public slots: - void onSocialActionsLoaded(); - signals: // emitted when the collection this result comes from is going offline/online: void statusChanged(); - // emitted when social actions are loaded - void socialActionsLoaded(); - private slots: void onOffline(); void onOnline(); @@ -129,10 +118,10 @@ private: explicit Result(); void updateAttributes(); - void parseSocialActions(); mutable RID m_rid; collection_ptr m_collection; + Tomahawk::query_ptr m_query; Tomahawk::artist_ptr m_artist; Tomahawk::album_ptr m_album; @@ -150,11 +139,7 @@ private: float m_score; QVariantMap m_attributes; - unsigned int m_trackId, m_fileId; - - QHash< QString, QVariant > m_currentSocialActions; - QList< SocialAction > m_allSocialActions; }; } //ns