1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-30 17:20:26 +02:00

Move ShareTrack post-commit to InboxModel.

This commit is contained in:
Teo Mrnjavac
2013-07-06 17:13:44 +02:00
parent 0c81889010
commit 29f09e4ed1
4 changed files with 63 additions and 53 deletions

View File

@@ -49,7 +49,7 @@ public:
dbcmd_ptr newInstance(); dbcmd_ptr newInstance();
signals: signals:
void created( const dbcmd_ptr& command ); void created( const Tomahawk::dbcmd_ptr& command );
protected: protected:
virtual DatabaseCommand* create() const = 0; virtual DatabaseCommand* create() const = 0;

View File

@@ -74,58 +74,6 @@ DatabaseCommand_ShareTrack::postCommitHook()
{ {
if ( source()->isLocal() ) if ( source()->isLocal() )
Servent::instance()->triggerDBSync(); 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 ) );
}
} }

View File

@@ -22,9 +22,11 @@
#include "database/DatabaseCommand_LoadInboxEntries.h" #include "database/DatabaseCommand_LoadInboxEntries.h"
#include "database/DatabaseCommand_DeleteInboxEntry.h" #include "database/DatabaseCommand_DeleteInboxEntry.h"
#include "database/DatabaseCommand_ModifyInboxEntry.h" #include "database/DatabaseCommand_ModifyInboxEntry.h"
#include "database/DatabaseCommand_ShareTrack.h"
#include "jobview/JobStatusModel.h" #include "jobview/JobStatusModel.h"
#include "utils/Logger.h" #include "utils/Logger.h"
#include "utils/Closure.h" #include "utils/Closure.h"
#include "ViewManager.h"
#include "PlaylistEntry.h" #include "PlaylistEntry.h"
#include "SourceList.h" #include "SourceList.h"
@@ -39,6 +41,12 @@ InboxModel::InboxModel( QObject* parent )
else else
NewClosure( SourceList::instance(), SIGNAL( ready() ), NewClosure( SourceList::instance(), SIGNAL( ready() ),
this, SLOT( loadTracks() ) ); 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<Tomahawk::DatabaseCommand_ShareTrack>();
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 );
}
}

View File

@@ -61,6 +61,9 @@ private slots:
void tracksLoaded( QList< Tomahawk::query_ptr > ); void tracksLoaded( QList< Tomahawk::query_ptr > );
void onDbcmdCreated( const Tomahawk::dbcmd_ptr& cmd );
void onDbcmdCommitted( const Tomahawk::dbcmd_ptr& cmd );
private: private:
static QList< Tomahawk::SocialAction > mergeSocialActions( QList< Tomahawk::SocialAction > first, static QList< Tomahawk::SocialAction > mergeSocialActions( QList< Tomahawk::SocialAction > first,
QList< Tomahawk::SocialAction > second ); QList< Tomahawk::SocialAction > second );