1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-10 16:14:40 +02:00

Clean up auto-updating xspf handling to make it actually usable

This commit is contained in:
Leo Franchi
2011-10-13 21:52:23 -04:00
parent 891692d914
commit 3873189d79
6 changed files with 68 additions and 7 deletions

View File

@@ -66,6 +66,22 @@ PlaylistUpdaterInterface::PlaylistUpdaterInterface( const playlist_ptr& pl )
QTimer::singleShot( 0, this, SLOT( doSave() ) ); 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 void
PlaylistUpdaterInterface::doSave() PlaylistUpdaterInterface::doSave()
{ {
@@ -92,6 +108,8 @@ PlaylistUpdaterInterface::remove()
s->remove( QString( "%1/type" ).arg( key ) ); s->remove( QString( "%1/type" ).arg( key ) );
s->remove( QString( "%1/autoupdate" ).arg( key ) ); s->remove( QString( "%1/autoupdate" ).arg( key ) );
s->remove( QString( "%1/interval" ).arg( key ) ); s->remove( QString( "%1/interval" ).arg( key ) );
deleteLater();
} }

View File

@@ -37,6 +37,7 @@ class DLLEXPORT PlaylistUpdaterInterface : public QObject
Q_OBJECT Q_OBJECT
public: public:
PlaylistUpdaterInterface( const playlist_ptr& pl ); PlaylistUpdaterInterface( const playlist_ptr& pl );
PlaylistUpdaterInterface( const playlist_ptr& pl, int interval, bool autoUpdate );
virtual ~PlaylistUpdaterInterface(){} virtual ~PlaylistUpdaterInterface(){}

View File

@@ -23,6 +23,7 @@
#include <QTimer> #include <QTimer>
#include <tomahawksettings.h> #include <tomahawksettings.h>
#include <pipeline.h>
using namespace Tomahawk; 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 ) XspfUpdater::XspfUpdater( const playlist_ptr& pl )
: PlaylistUpdaterInterface( pl ) : PlaylistUpdaterInterface( pl )
{ {
@@ -46,6 +55,7 @@ void
XspfUpdater::updateNow() XspfUpdater::updateNow()
{ {
XSPFLoader* l = new XSPFLoader( false, false ); XSPFLoader* l = new XSPFLoader( false, false );
l->setAutoResolveTracks( false );
l->load( m_url ); l->load( m_url );
connect( l, SIGNAL( ok ( Tomahawk::playlist_ptr ) ), this, SLOT( playlistLoaded() ) ); connect( l, SIGNAL( ok ( Tomahawk::playlist_ptr ) ), this, SLOT( playlistLoaded() ) );
} }
@@ -56,8 +66,35 @@ XspfUpdater::playlistLoaded()
XSPFLoader* loader = qobject_cast<XSPFLoader*>( sender() ); XSPFLoader* loader = qobject_cast<XSPFLoader*>( sender() );
Q_ASSERT( loader ); Q_ASSERT( loader );
QList< query_ptr > queries = loader->entries(); QList< query_ptr> oldqueries;
QList<plentry_ptr> el = playlist()->entriesFromQueries( queries, true ); 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<plentry_ptr> el = playlist()->entriesFromQueries( tosave, true );
playlist()->createNewRevision( uuid(), playlist()->currentrevision(), el ); playlist()->createNewRevision( uuid(), playlist()->currentrevision(), el );
// // if there are any different from the current playlist, clear and use the new one, update // // if there are any different from the current playlist, clear and use the new one, update

View File

@@ -31,6 +31,7 @@ class XspfUpdater : public PlaylistUpdaterInterface
Q_OBJECT Q_OBJECT
public: public:
XspfUpdater( const playlist_ptr& pl, const QString& xspfUrl ); 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 explicit XspfUpdater( const playlist_ptr& pl ); // used by factory
virtual ~XspfUpdater(); virtual ~XspfUpdater();

View File

@@ -34,6 +34,7 @@ XSPFLoader::XSPFLoader( bool autoCreate, bool autoUpdate, QObject *parent )
: QObject( parent ) : QObject( parent )
, m_autoCreate( autoCreate ) , m_autoCreate( autoCreate )
, m_autoUpdate( autoUpdate ) , m_autoUpdate( autoUpdate )
, m_autoResolve( false )
, m_NS("http://xspf.org/ns/0/") , m_NS("http://xspf.org/ns/0/")
{ {
qRegisterMetaType< XSPFErrorCode >("XSPFErrorCode"); qRegisterMetaType< XSPFErrorCode >("XSPFErrorCode");
@@ -102,6 +103,9 @@ void
XSPFLoader::networkLoadFinished() XSPFLoader::networkLoadFinished()
{ {
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender()); QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
if ( reply->error() != QNetworkReply::NoError )
return;
m_body = reply->readAll(); m_body = reply->readAll();
gotBody(); gotBody();
} }
@@ -196,7 +200,7 @@ XSPFLoader::gotBody()
continue; 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 ); q->setDuration( duration.toInt() / 1000 );
if ( !url.isEmpty() ) if ( !url.isEmpty() )
q->setResultHint( url ); q->setResultHint( url );
@@ -222,9 +226,8 @@ XSPFLoader::gotBody()
false, false,
m_entries ); m_entries );
Tomahawk::XspfUpdater* updater = new Tomahawk::XspfUpdater( m_playlist, m_url.toString() ); // 10 minute default---for now, no way to change it
updater->setInterval( 1200000 ); // 20 minute default for now, no way to change it new Tomahawk::XspfUpdater( m_playlist, 6000000, m_autoUpdate, m_url.toString() );
updater->setAutoUpdate( m_autoUpdate );
deleteLater(); deleteLater();
} }

View File

@@ -46,6 +46,7 @@ public:
QList< Tomahawk::query_ptr > entries() const; QList< Tomahawk::query_ptr > entries() const;
void setOverrideTitle( const QString& newTitle ); void setOverrideTitle( const QString& newTitle );
void setAutoResolveTracks( bool autoResolve ) { m_autoResolve = autoResolve; }
signals: signals:
void error( XSPFLoader::XSPFErrorCode error ); void error( XSPFLoader::XSPFErrorCode error );
@@ -63,7 +64,7 @@ private:
void reportError(); void reportError();
void gotBody(); void gotBody();
bool m_autoCreate, m_autoUpdate; bool m_autoCreate, m_autoUpdate, m_autoResolve;
QString m_NS,m_overrideTitle; QString m_NS,m_overrideTitle;
QList< Tomahawk::query_ptr > m_entries; QList< Tomahawk::query_ptr > m_entries;
QString m_title, m_info, m_creator; QString m_title, m_info, m_creator;