1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-20 07:49:42 +01:00

Mark tracks as listened when they are played in the Inbox.

This commit is contained in:
Teo Mrnjavac 2013-04-23 02:26:41 +02:00
parent 866c8d4bec
commit f9edc04b04
7 changed files with 157 additions and 15 deletions

@ -274,6 +274,7 @@ list(APPEND libSources
database/DatabaseCommand_PlaybackCharts.cpp
database/DatabaseCommand_ShareTrack.cpp
database/DatabaseCommand_DeleteInboxEntry.cpp
database/DatabaseCommand_ModifyInboxEntry.cpp
database/Database.cpp
database/TomahawkSqlQuery.cpp
database/IdThreadWorker.cpp

@ -0,0 +1,64 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2013, Teo Mrnjavac <teo@kde.org>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tomahawk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#include "DatabaseCommand_ModifyInboxEntry.h"
#include "DatabaseImpl.h"
#include "Query.h"
DatabaseCommand_ModifyInboxEntry::DatabaseCommand_ModifyInboxEntry( const Tomahawk::query_ptr& query,
bool newValue,
QObject* parent )
: DatabaseCommand( parent )
, m_query( query )
, m_newValue( newValue )
{
}
void
DatabaseCommand_ModifyInboxEntry::exec( DatabaseImpl* dbi )
{
TomahawkSqlQuery query = dbi->newquery();
Q_ASSERT( !m_query.isNull() );
if ( m_query->queryTrack()->track().isEmpty() || m_query->queryTrack()->artist().isEmpty() )
{
emit done();
return;
}
query.prepare(
"UPDATE social_attributes "
"SET v = ? "
"WHERE social_attributes.k = ? AND social_attributes.id = ( "
"SELECT id FROM track "
"WHERE track.name = ? AND track.artist = ( "
"SELECT id FROM artist WHERE artist.name = ? "
") "
")" );
query.addBindValue( m_newValue );
query.addBindValue( "Inbox" );
query.addBindValue( m_query->queryTrack()->track() );
query.addBindValue( m_query->queryTrack()->artist() );
query.exec();
emit done();
}

@ -0,0 +1,44 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2013, Teo Mrnjavac <teo@kde.org>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tomahawk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DATABASECOMMAND_MODIFYINBOXENTRY_H
#define DATABASECOMMAND_MODIFYINBOXENTRY_H
#include "DatabaseCommand.h"
class DatabaseCommand_ModifyInboxEntry : public DatabaseCommand
{
Q_OBJECT
public:
explicit DatabaseCommand_ModifyInboxEntry( const Tomahawk::query_ptr& query, bool newValue, QObject *parent = 0 );
virtual void exec( DatabaseImpl* dbi );
virtual bool doesMutates() const { return true; }
virtual bool groupable() const { return true; }
virtual bool localOnly() const { return true; }
virtual QString commandname() const { return "modifyinboxentry"; }
signals:
void done();
private:
Tomahawk::query_ptr m_query;
bool m_newValue;
};
#endif // DATABASECOMMAND_MODIFYINBOXENTRY_H

@ -90,7 +90,7 @@ DatabaseCommand_ShareTrack::postCommitHook()
action.value = true; //unlistened
action.timestamp = timestamp();
QList< Tomahawk::SocialAction > actions;
QList< Tomahawk::SocialAction > actions = m_track->allSocialActions();
actions << action;
m_track->setAllSocialActions( actions );

@ -21,6 +21,7 @@
#include "database/Database.h"
#include "database/DatabaseCommand_GenericSelect.h"
#include "database/DatabaseCommand_DeleteInboxEntry.h"
#include "database/DatabaseCommand_ModifyInboxEntry.h"
#include "SourceList.h"
#include "utils/Logger.h"
#include "utils/Closure.h"
@ -34,6 +35,9 @@ InboxModel::InboxModel( QObject* parent )
else
NewClosure( SourceList::instance(), SIGNAL( ready() ),
this, SLOT( loadTracks() ) );
connect( this, SIGNAL( currentIndexChanged() ),
SLOT( onCurrentIndexChanged() ) );
}
@ -44,28 +48,28 @@ InboxModel::~InboxModel()
QList<Tomahawk::SocialAction>
InboxModel::mergeSocialActions( QList<Tomahawk::SocialAction> first, QList<Tomahawk::SocialAction> second)
{
foreach ( Tomahawk::SocialAction sa, second )
foreach ( Tomahawk::SocialAction saInSecond, second )
{
if ( sa.action != "Inbox" )
if ( saInSecond.action != "Inbox" )
{
first.append( sa );
first.append( saInSecond );
continue;
}
bool contains = false;
for ( int i = 0; i < first.count(); ++i )
{
Tomahawk::SocialAction &sb = first[ i ];
if ( sa.source == sb.source )
Tomahawk::SocialAction &saInFirst = first[ i ];
if ( saInSecond.source == saInFirst.source )
{
sb.timestamp = qMax( sa.timestamp.toInt(), sb.timestamp.toInt() );
sb.value = sa.value.toBool() && sb.value.toBool();
saInFirst.timestamp = qMax( saInSecond.timestamp.toInt(), saInFirst.timestamp.toInt() );
saInFirst.value = saInFirst.value.toBool() && saInSecond.value.toBool();
contains = true;
break;
}
}
if ( !contains )
first.append( sa );
first.append( saInSecond );
}
return first;
}
@ -88,8 +92,8 @@ InboxModel::insertEntries( const QList< Tomahawk::plentry_ptr >& entries, int ro
if ( entry->query()->equals( existingEntry->query(), true /*ignoreCase*/) )
{
//We got a dupe, let's merge the social actions
entry->query()->track()->setAllSocialActions( mergeSocialActions( existingEntry->query()->track()->allSocialActions(),
entry->query()->track()->allSocialActions() ) );
entry->query()->queryTrack()->setAllSocialActions( mergeSocialActions( existingEntry->query()->queryTrack()->allSocialActions(),
entry->query()->queryTrack()->allSocialActions() ) );
toInsert.erase( jt );
break;
}
@ -103,8 +107,8 @@ InboxModel::insertEntries( const QList< Tomahawk::plentry_ptr >& entries, int ro
{
if ( plEntry->query()->equals( toInsert.at( i )->query(), true ) )
{
plEntry->query()->track()->setAllSocialActions( mergeSocialActions( plEntry->query()->track()->allSocialActions(),
toInsert.at( i )->query()->track()->allSocialActions() ) );
plEntry->query()->queryTrack()->setAllSocialActions( mergeSocialActions( plEntry->query()->queryTrack()->allSocialActions(),
toInsert.at( i )->query()->queryTrack()->allSocialActions() ) );
toInsert.removeAt( i );
dataChanged( index( playlistEntries().indexOf( plEntry ), 0, QModelIndex() ),
@ -189,7 +193,7 @@ InboxModel::tracksLoaded( QList< Tomahawk::query_ptr > incoming )
QList< Tomahawk::SocialAction > actions;
actions << action;
newQuery->track()->setAllSocialActions( actions );
newQuery->queryTrack()->setAllSocialActions( actions );
newQuery->setProperty( "data", QVariant() ); //clear
}
@ -206,3 +210,31 @@ InboxModel::tracksLoaded( QList< Tomahawk::query_ptr > incoming )
}
}
void
InboxModel::onCurrentIndexChanged()
{
QPersistentModelIndex idx = currentItem();
if ( idx.isValid() )
{
PlayableItem* item = itemFromIndex( idx );
if ( item && !item->query().isNull() )
{
Tomahawk::query_ptr qry = item->query();
DatabaseCommand_ModifyInboxEntry* cmd = new DatabaseCommand_ModifyInboxEntry( qry, false );
Database::instance()->enqueue( QSharedPointer< DatabaseCommand >( cmd ) );
QList< Tomahawk::SocialAction > actions = item->query()->queryTrack()->allSocialActions();
for ( QList< Tomahawk::SocialAction >::iterator it = actions.begin();
it != actions.end(); ++it )
{
if ( it->action == "Inbox" )
{
it->value = false; //listened!
}
}
item->query()->queryTrack()->setAllSocialActions( actions );
}
}
}

@ -47,6 +47,7 @@ private slots:
void loadTracks();
void tracksLoaded( QList< Tomahawk::query_ptr > );
void onCurrentIndexChanged();
private:
static QList< Tomahawk::SocialAction > mergeSocialActions( QList< Tomahawk::SocialAction > first,

@ -125,7 +125,7 @@ PlaylistLargeItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem&
if ( isUnlistened && ! item->isPlaying() )
{
prepareStyleOption( &opt, index, item );
opt.backgroundBrush = QColor( Qt::yellow ).lighter( 190 );
opt.backgroundBrush = QColor( Qt::yellow ).lighter( 185 );
}
else
prepareStyleOption( &opt, index, item );