diff --git a/src/libtomahawk/playlist/PlaylistUpdaterInterface.cpp b/src/libtomahawk/playlist/PlaylistUpdaterInterface.cpp index ac0da13da..c057ceb59 100644 --- a/src/libtomahawk/playlist/PlaylistUpdaterInterface.cpp +++ b/src/libtomahawk/playlist/PlaylistUpdaterInterface.cpp @@ -66,6 +66,22 @@ PlaylistUpdaterInterface::PlaylistUpdaterInterface( const playlist_ptr& pl ) QTimer::singleShot( 0, this, SLOT( doSave() ) ); } +PlaylistUpdaterInterface::PlaylistUpdaterInterface( const playlist_ptr& pl, int interval, bool autoUpdate ) + : QObject( 0 ) + , m_timer( new QTimer( this ) ) + , m_autoUpdate( autoUpdate ) + , m_playlist( pl ) +{ + Q_ASSERT( !m_playlist.isNull() ); + + m_playlist->setUpdater( this ); + m_timer->setInterval( interval ); + connect( m_timer, SIGNAL( timeout() ), this, SLOT( updateNow() ) ); + + QTimer::singleShot( 0, this, SLOT( doSave() ) ); +} + + void PlaylistUpdaterInterface::doSave() { @@ -92,6 +108,8 @@ PlaylistUpdaterInterface::remove() s->remove( QString( "%1/type" ).arg( key ) ); s->remove( QString( "%1/autoupdate" ).arg( key ) ); s->remove( QString( "%1/interval" ).arg( key ) ); + + deleteLater(); } diff --git a/src/libtomahawk/playlist/PlaylistUpdaterInterface.h b/src/libtomahawk/playlist/PlaylistUpdaterInterface.h index 5fd3532b4..fbbc40cfe 100644 --- a/src/libtomahawk/playlist/PlaylistUpdaterInterface.h +++ b/src/libtomahawk/playlist/PlaylistUpdaterInterface.h @@ -37,6 +37,7 @@ class DLLEXPORT PlaylistUpdaterInterface : public QObject Q_OBJECT public: PlaylistUpdaterInterface( const playlist_ptr& pl ); + PlaylistUpdaterInterface( const playlist_ptr& pl, int interval, bool autoUpdate ); virtual ~PlaylistUpdaterInterface(){} diff --git a/src/libtomahawk/playlist/XspfUpdater.cpp b/src/libtomahawk/playlist/XspfUpdater.cpp index 7b417025a..fa966f3f7 100644 --- a/src/libtomahawk/playlist/XspfUpdater.cpp +++ b/src/libtomahawk/playlist/XspfUpdater.cpp @@ -23,6 +23,7 @@ #include #include +#include using namespace Tomahawk; @@ -32,6 +33,14 @@ XspfUpdater::XspfUpdater( const playlist_ptr& pl, const QString& xUrl ) { } +XspfUpdater::XspfUpdater( const playlist_ptr& pl, int interval, bool autoUpdate, const QString& xspfUrl ) + : PlaylistUpdaterInterface( pl, interval, autoUpdate ) + , m_url( xspfUrl ) +{ + +} + + XspfUpdater::XspfUpdater( const playlist_ptr& pl ) : PlaylistUpdaterInterface( pl ) { @@ -46,6 +55,7 @@ void XspfUpdater::updateNow() { XSPFLoader* l = new XSPFLoader( false, false ); + l->setAutoResolveTracks( false ); l->load( m_url ); connect( l, SIGNAL( ok ( Tomahawk::playlist_ptr ) ), this, SLOT( playlistLoaded() ) ); } @@ -56,8 +66,35 @@ XspfUpdater::playlistLoaded() XSPFLoader* loader = qobject_cast( sender() ); Q_ASSERT( loader ); - QList< query_ptr > queries = loader->entries(); - QList el = playlist()->entriesFromQueries( queries, true ); + QList< query_ptr> oldqueries; + foreach ( const plentry_ptr& ple, playlist()->entries() ) + oldqueries << 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 ); + + break; + } + } + } + + // No work to be done if all are the same + if ( oldqueries.size() == newqueries.size() && sameCount == oldqueries.size() ) + return; + + QList el = playlist()->entriesFromQueries( tosave, true ); playlist()->createNewRevision( uuid(), playlist()->currentrevision(), el ); // // if there are any different from the current playlist, clear and use the new one, update diff --git a/src/libtomahawk/playlist/XspfUpdater.h b/src/libtomahawk/playlist/XspfUpdater.h index 148930fb4..c5254d79a 100644 --- a/src/libtomahawk/playlist/XspfUpdater.h +++ b/src/libtomahawk/playlist/XspfUpdater.h @@ -31,6 +31,7 @@ class XspfUpdater : public PlaylistUpdaterInterface Q_OBJECT public: XspfUpdater( const playlist_ptr& pl, const QString& xspfUrl ); + XspfUpdater( const playlist_ptr& pl, int interval, bool autoUpdate, const QString& xspfUrl ); explicit XspfUpdater( const playlist_ptr& pl ); // used by factory virtual ~XspfUpdater(); diff --git a/src/libtomahawk/utils/xspfloader.cpp b/src/libtomahawk/utils/xspfloader.cpp index 0318295a1..8de43cf62 100644 --- a/src/libtomahawk/utils/xspfloader.cpp +++ b/src/libtomahawk/utils/xspfloader.cpp @@ -34,6 +34,7 @@ XSPFLoader::XSPFLoader( bool autoCreate, bool autoUpdate, QObject *parent ) : QObject( parent ) , m_autoCreate( autoCreate ) , m_autoUpdate( autoUpdate ) + , m_autoResolve( false ) , m_NS("http://xspf.org/ns/0/") { qRegisterMetaType< XSPFErrorCode >("XSPFErrorCode"); @@ -102,6 +103,9 @@ void XSPFLoader::networkLoadFinished() { QNetworkReply* reply = qobject_cast(sender()); + if ( reply->error() != QNetworkReply::NoError ) + return; + m_body = reply->readAll(); gotBody(); } @@ -196,7 +200,7 @@ XSPFLoader::gotBody() continue; } - query_ptr q = Tomahawk::Query::get( artist, track, album, uuid() ); + query_ptr q = Tomahawk::Query::get( artist, track, album, uuid(), m_autoResolve ); q->setDuration( duration.toInt() / 1000 ); if ( !url.isEmpty() ) q->setResultHint( url ); @@ -222,9 +226,8 @@ XSPFLoader::gotBody() false, m_entries ); - Tomahawk::XspfUpdater* updater = new Tomahawk::XspfUpdater( m_playlist, m_url.toString() ); - updater->setInterval( 1200000 ); // 20 minute default for now, no way to change it - updater->setAutoUpdate( m_autoUpdate ); + // 10 minute default---for now, no way to change it + new Tomahawk::XspfUpdater( m_playlist, 6000000, m_autoUpdate, m_url.toString() ); deleteLater(); } diff --git a/src/libtomahawk/utils/xspfloader.h b/src/libtomahawk/utils/xspfloader.h index 54b11ad74..33826bc03 100644 --- a/src/libtomahawk/utils/xspfloader.h +++ b/src/libtomahawk/utils/xspfloader.h @@ -46,6 +46,7 @@ public: QList< Tomahawk::query_ptr > entries() const; void setOverrideTitle( const QString& newTitle ); + void setAutoResolveTracks( bool autoResolve ) { m_autoResolve = autoResolve; } signals: void error( XSPFLoader::XSPFErrorCode error ); @@ -63,7 +64,7 @@ private: void reportError(); void gotBody(); - bool m_autoCreate, m_autoUpdate; + bool m_autoCreate, m_autoUpdate, m_autoResolve; QString m_NS,m_overrideTitle; QList< Tomahawk::query_ptr > m_entries; QString m_title, m_info, m_creator;