1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-18 23:09:42 +01:00

Don't blindly refresh top loved tracks all the time.

Refactor update function from xspfupdater into tomahawkutils, use it in the loved tracks backend.
also, a few misc cleanups
This commit is contained in:
Leo Franchi 2012-01-06 17:14:42 -05:00
parent 7abc185586
commit 1ccc368195
11 changed files with 92 additions and 60 deletions

View File

@ -20,10 +20,11 @@
#include "playlist.h"
#include "utils/xspfloader.h"
#include "tomahawksettings.h"
#include "pipeline.h"
#include "utils/tomahawkutils.h"
#include <QTimer>
#include <tomahawksettings.h>
#include <pipeline.h>
using namespace Tomahawk;
@ -66,46 +67,15 @@ XspfUpdater::playlistLoaded()
XSPFLoader* loader = qobject_cast<XSPFLoader*>( sender() );
Q_ASSERT( loader );
QList< query_ptr> oldqueries;
foreach ( const plentry_ptr& ple, playlist()->entries() )
oldqueries << ple->query();
QList< query_ptr > tracks;
foreach ( const plentry_ptr ple, playlist()->entries() )
tracks << ple->query();
QList< query_ptr > newqueries = loader->entries();
int sameCount = 0;
QList< query_ptr > tosave = newqueries;
foreach ( const query_ptr& newquery, newqueries )
{
foreach ( const query_ptr& oldq, oldqueries )
{
if ( newquery->track() == oldq->track() &&
newquery->artist() == oldq->artist() &&
newquery->album() == oldq->album() )
{
sameCount++;
if ( tosave.contains( newquery ) )
tosave.replace( tosave.indexOf( newquery ), oldq );
QList< query_ptr > mergedTracks = TomahawkUtils::mergePlaylistChanges( tracks, loader->entries() );
break;
}
}
}
// No work to be done if all are the same
if ( oldqueries.size() == newqueries.size() && sameCount == oldqueries.size() )
return;
QList<plentry_ptr> el = playlist()->entriesFromQueries( tosave, true );
QList<Tomahawk::plentry_ptr> el = playlist()->entriesFromQueries( mergedTracks, true );
playlist()->createNewRevision( uuid(), playlist()->currentrevision(), el );
// // if there are any different from the current playlist, clear and use the new one, update
// bool changed = ( queries.size() == playlist()->entries().count() );
// if ( !changed )
// {
// foreach( const query_ptr& newSong, queries )
// {
// if ( !playlist()->entries.contains() )
// }
// }
}
void

View File

@ -41,12 +41,12 @@ CustomPlaylistView::CustomPlaylistView( CustomPlaylistView::PlaylistType type, c
generateTracks();
if ( m_type == SourceLovedTracks )
connect( m_source.data(), SIGNAL( socialAttributesChanged() ), this, SLOT( reload() ) );
connect( m_source.data(), SIGNAL( socialAttributesChanged( QString ) ), this, SLOT( socialAttributesChanged( QString ) ) );
else if ( m_type == AllLovedTracks )
{
connect( SourceList::instance()->getLocal().data(), SIGNAL( socialAttributesChanged() ), this, SLOT( reload() ) );
connect( SourceList::instance()->getLocal().data(), SIGNAL( socialAttributesChanged( QString ) ), this, SLOT( socialAttributesChanged( QString ) ) );
foreach ( const source_ptr& s, SourceList::instance()->sources( true ) )
connect( s.data(), SIGNAL( socialAttributesChanged() ), this, SLOT( reload() ) );
connect( s.data(), SIGNAL( socialAttributesChanged( QString ) ), this, SLOT( socialAttributesChanged( QString ) ) );
connect( SourceList::instance(), SIGNAL( sourceAdded( Tomahawk::source_ptr ) ), this, SLOT( sourceAdded( Tomahawk::source_ptr ) ) );
}
@ -104,7 +104,10 @@ CustomPlaylistView::generateTracks()
void
CustomPlaylistView::tracksGenerated( QList< query_ptr > tracks )
{
m_model->append( tracks );
QList< query_ptr > newTracks = TomahawkUtils::mergePlaylistChanges( m_model->queries(), tracks );
m_model->clear();
m_model->append( newTracks );
}
@ -153,15 +156,17 @@ CustomPlaylistView::pixmap() const
void
CustomPlaylistView::reload()
CustomPlaylistView::socialAttributesChanged( const QString& action )
{
m_model->clear();
generateTracks();
if ( action == "Love" )
{
generateTracks();
}
}
void
CustomPlaylistView::sourceAdded( const source_ptr& s )
{
connect( s.data(), SIGNAL( socialAttributesChanged() ), this, SLOT( reload() ) );
connect( s.data(), SIGNAL( socialAttributesChanged( QString ) ), this, SLOT( socialAttributesChanged( QString ) ) );
}

View File

@ -54,7 +54,7 @@ public:
private slots:
void tracksGenerated( QList<Tomahawk::query_ptr> tracks );
void reload();
void socialAttributesChanged( const QString& );
void sourceAdded( const Tomahawk::source_ptr& );
private:

View File

@ -229,7 +229,7 @@ PlaylistModel::insert( const QList< Tomahawk::plentry_ptr >& entries, int row )
TrackModelItem* plitem;
foreach( const plentry_ptr& entry, entries )
{
plitem = new TrackModelItem( entry, m_rootItem, row + i );
plitem = new TrackModelItem( entry, rootItem(), row + i );
plitem->index = createIndex( row + i, 0, plitem );
i++;

View File

@ -1,6 +1,7 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* Copyright 2011 Leo Franchi <lfranchi@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
@ -336,6 +337,21 @@ TrackModel::clear()
}
QList< query_ptr >
TrackModel::queries() const
{
Q_ASSERT( m_rootItem );
QList< query_ptr > tracks;
foreach ( TrackModelItem* item, m_rootItem->children )
{
tracks << item->query();
}
return tracks;
}
void
TrackModel::append( const Tomahawk::query_ptr& query )
{

View File

@ -1,6 +1,7 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* Copyright 2011 Leo Franchi <lfranchi@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
@ -93,7 +94,8 @@ public:
virtual void ensureResolved();
TrackModelItem* itemFromIndex( const QModelIndex& index ) const;
TrackModelItem* m_rootItem;
/// Returns a flat list of all tracks in this model
QList< Tomahawk::query_ptr > queries() const;
signals:
void repeatModeChanged( Tomahawk::PlaylistInterface::RepeatMode mode );
@ -125,11 +127,15 @@ public slots:
virtual void setRepeatMode( Tomahawk::PlaylistInterface::RepeatMode /*mode*/ ) {}
virtual void setShuffled( bool /*shuffled*/ ) {}
protected:
TrackModelItem* rootItem() const { return m_rootItem; }
private slots:
void onPlaybackStarted( const Tomahawk::result_ptr& result );
void onPlaybackStopped();
private:
TrackModelItem* m_rootItem;
QPersistentModelIndex m_currentIndex;
Tomahawk::QID m_currentUuid;

View File

@ -54,15 +54,6 @@ public:
virtual PlaylistInterface::RepeatMode repeatMode() const { return m_repeatMode; }
virtual bool shuffled() const { return m_shuffled; }
signals:
void repeatModeChanged( Tomahawk::PlaylistInterface::RepeatMode mode );
void shuffleModeChanged( bool enabled );
void trackCountChanged( unsigned int tracks );
void sourceTrackCountChanged( unsigned int tracks );
void nextTrackReady();
public slots:
virtual void setRepeatMode( RepeatMode mode ) { m_repeatMode = mode; emit repeatModeChanged( mode ); }
virtual void setShuffled( bool enabled ) { m_shuffled = enabled; emit shuffleModeChanged( enabled ); }

View File

@ -411,7 +411,9 @@ Source::executeCommands()
void
Source::reportSocialAttributesChanged( DatabaseCommand_SocialAction* action )
{
emit socialAttributesChanged();
Q_ASSERT( action );
emit socialAttributesChanged( action->action() );
if ( action->action() == "latchOn" )
{

View File

@ -106,7 +106,7 @@ signals:
void stateChanged();
void commandsFinished();
void socialAttributesChanged();
void socialAttributesChanged( const QString& action );
void latchedOn( const Tomahawk::source_ptr& to );
void latchedOff( const Tomahawk::source_ptr& from );

View File

@ -532,6 +532,36 @@ newerVersion( const QString& oldVersion, const QString& newVersion )
}
QList< Tomahawk::query_ptr >
mergePlaylistChanges( const QList< Tomahawk::query_ptr >& orig, const QList< Tomahawk::query_ptr >& newTracks )
{
int sameCount = 0;
QList< Tomahawk::query_ptr > tosave = newTracks;
foreach ( const Tomahawk::query_ptr& newquery, newTracks )
{
foreach ( const Tomahawk::query_ptr& oldq, orig )
{
if ( newquery->track() == oldq->track() &&
newquery->artist() == oldq->artist() &&
newquery->album() == oldq->album() )
{
sameCount++;
if ( tosave.contains( newquery ) )
tosave.replace( tosave.indexOf( newquery ), oldq );
break;
}
}
}
// No work to be done if all are the same
if ( orig.size() == newTracks.size() && sameCount == orig.size() )
return orig;
return tosave;
}
// taken from util/fileutils.cpp in kdevplatform
bool
removeDirectory( const QString& dir )

View File

@ -24,6 +24,7 @@
#include <QtCore/QThread>
#include <QtNetwork/QNetworkProxy>
#include <QtCore/QStringList>
#include <typedefs.h>
#define RESPATH ":/data/"
@ -88,6 +89,17 @@ namespace TomahawkUtils
DLLEXPORT QString md5( const QByteArray& data );
DLLEXPORT bool removeDirectory( const QString& dir );
/**
* This helper is designed to help "update" an existing playlist with a newer revision of itself.
* To avoid re-loading the whole playlist and re-resolving tracks that are the same in the old playlist,
* it goes through the new playlist and adds only new tracks.
*
* The new list of tracks is returned
*
* \return true if some changes were made, false if the new tracks are the same as the current tracks in \param orig
*/
DLLEXPORT QList< Tomahawk::query_ptr > mergePlaylistChanges( const QList< Tomahawk::query_ptr >& orig, const QList< Tomahawk::query_ptr >& newTracks );
DLLEXPORT void crash();
}