diff --git a/src/libtomahawk/database/Database.h b/src/libtomahawk/database/Database.h index 8738dbb0f..e0f399d57 100644 --- a/src/libtomahawk/database/Database.h +++ b/src/libtomahawk/database/Database.h @@ -49,7 +49,7 @@ public: dbcmd_ptr newInstance(); signals: - void created( const dbcmd_ptr& command ); + void created( const Tomahawk::dbcmd_ptr& command ); protected: virtual DatabaseCommand* create() const = 0; diff --git a/src/libtomahawk/database/DatabaseCommand_ShareTrack.cpp b/src/libtomahawk/database/DatabaseCommand_ShareTrack.cpp index 74afbb42c..6e0d630f3 100644 --- a/src/libtomahawk/database/DatabaseCommand_ShareTrack.cpp +++ b/src/libtomahawk/database/DatabaseCommand_ShareTrack.cpp @@ -74,58 +74,6 @@ DatabaseCommand_ShareTrack::postCommitHook() { if ( source()->isLocal() ) Servent::instance()->triggerDBSync(); - - QString myDbid = SourceList::instance()->getLocal()->nodeId(); - QString sourceDbid = source()->nodeId(); - - qRegisterMetaType< InboxJobItem::Side >("InboxJobItem::Side"); - qRegisterMetaType< Tomahawk::trackdata_ptr >("Tomahawk::trackdata_ptr"); - if ( source()->isLocal() && sourceDbid != m_recipient ) //if I just sent a track - { - QMetaObject::invokeMethod( ViewManager::instance()->inboxModel(), - "showNotification", - Qt::QueuedConnection, - Q_ARG( InboxJobItem::Side, InboxJobItem::Sending ), - Q_ARG( const QString&, m_recipient ), - Q_ARG( const Tomahawk::trackdata_ptr&, m_track ) ); - } - - if ( m_track ) - return; - - if ( myDbid != m_recipient || sourceDbid == m_recipient ) - return; - - //From here on, everything happens only on the recipient, and only if recipient!=source - m_track = Tomahawk::TrackData::get( 0, artist(), track() ); - if ( !m_track ) - return; - - Tomahawk::SocialAction action; - action.action = "Inbox"; - action.source = source(); - action.value = true; //unlistened - action.timestamp = timestamp(); - - QList< Tomahawk::SocialAction > actions = m_track->allSocialActions(); - actions << action; - m_track->setAllSocialActions( actions ); - - QMetaObject::invokeMethod( ViewManager::instance()->inboxModel(), - "insertQuery", - Qt::QueuedConnection, - Q_ARG( const Tomahawk::query_ptr&, m_track->toQuery() ), - Q_ARG( int, 0 ) /*row*/ ); - - if ( ViewManager::instance()->currentPage() != ViewManager::instance()->inboxWidget() ) - { - QMetaObject::invokeMethod( ViewManager::instance()->inboxModel(), - "showNotification", - Qt::QueuedConnection, - Q_ARG( InboxJobItem::Side, InboxJobItem::Receiving ), - Q_ARG( const Tomahawk::source_ptr&, source() ), - Q_ARG( const Tomahawk::trackdata_ptr&, m_track ) ); - } } diff --git a/src/libtomahawk/playlist/InboxModel.cpp b/src/libtomahawk/playlist/InboxModel.cpp index 51ce6cddc..a3f8848d4 100644 --- a/src/libtomahawk/playlist/InboxModel.cpp +++ b/src/libtomahawk/playlist/InboxModel.cpp @@ -22,9 +22,11 @@ #include "database/DatabaseCommand_LoadInboxEntries.h" #include "database/DatabaseCommand_DeleteInboxEntry.h" #include "database/DatabaseCommand_ModifyInboxEntry.h" +#include "database/DatabaseCommand_ShareTrack.h" #include "jobview/JobStatusModel.h" #include "utils/Logger.h" #include "utils/Closure.h" +#include "ViewManager.h" #include "PlaylistEntry.h" #include "SourceList.h" @@ -39,6 +41,12 @@ InboxModel::InboxModel( QObject* parent ) else NewClosure( SourceList::instance(), SIGNAL( ready() ), this, SLOT( loadTracks() ) ); + + // Every time a ShareTrack dbcmd is created, we keep track of it until it's committed, + // so we can react with post-commit changes in the UI + Tomahawk::DatabaseCommandFactory* factory = Tomahawk::Database::instance()->commandFactory(); + connect( factory, SIGNAL(created(Tomahawk::dbcmd_ptr)), + this, SLOT(onDbcmdCreated(Tomahawk::dbcmd_ptr))); } @@ -234,3 +242,54 @@ InboxModel::tracksLoaded( QList< Tomahawk::query_ptr > incoming ) } } + +void +InboxModel::onDbcmdCreated( const Tomahawk::dbcmd_ptr& cmd ) +{ + connect( cmd.data(), SIGNAL( committed( Tomahawk::dbcmd_ptr ) ), + this, SLOT( onDbcmdCommitted( Tomahawk::dbcmd_ptr ) ) ); +} + + +void +InboxModel::onDbcmdCommitted( const Tomahawk::dbcmd_ptr& cmd ) +{ + Tomahawk::DatabaseCommand_ShareTrack* c = qobject_cast< Tomahawk::DatabaseCommand_ShareTrack* >( cmd.data() ); + Q_ASSERT( c ); + + QString myDbid = SourceList::instance()->getLocal()->nodeId(); + QString sourceDbid = c->source()->nodeId(); + + if ( myDbid != c->recipient() || sourceDbid == c->recipient() ) // if I'm not receiving, or if I'm sending to myself, bail out + return; + + Tomahawk::trackdata_ptr td = Tomahawk::TrackData::get( 0, c->artist(), c->track() ); + if ( td.isNull() ) + return; + + if ( c->source()->isLocal() && sourceDbid != c->recipient() ) //if I just sent a track + { + showNotification( InboxJobItem::Sending, c->recipient(), td ); + return; + } + + //From here on, everything happens only on the recipient, and only if recipient!=source + + Tomahawk::SocialAction action; + action.action = "Inbox"; + action.source = c->source(); + action.value = true; //unlistened + action.timestamp = c->timestamp(); + + QList< Tomahawk::SocialAction > actions = td->allSocialActions(); + actions << action; + td->setAllSocialActions( actions ); + + insertQuery( td->toQuery(), 0 ); + + if ( ViewManager::instance()->currentPage() != ViewManager::instance()->inboxWidget() ) + { + showNotification( InboxJobItem::Receiving, c->source(), td ); + } +} + diff --git a/src/libtomahawk/playlist/InboxModel.h b/src/libtomahawk/playlist/InboxModel.h index 7e3decd82..13dedc759 100644 --- a/src/libtomahawk/playlist/InboxModel.h +++ b/src/libtomahawk/playlist/InboxModel.h @@ -61,6 +61,9 @@ private slots: void tracksLoaded( QList< Tomahawk::query_ptr > ); + void onDbcmdCreated( const Tomahawk::dbcmd_ptr& cmd ); + void onDbcmdCommitted( const Tomahawk::dbcmd_ptr& cmd ); + private: static QList< Tomahawk::SocialAction > mergeSocialActions( QList< Tomahawk::SocialAction > first, QList< Tomahawk::SocialAction > second );