From de982f8b50af288308d1171ede24c8d38a4f7262 Mon Sep 17 00:00:00 2001 From: hugolm84 Date: Mon, 5 Sep 2011 18:38:51 +0200 Subject: [PATCH] Added itunes artist link support --- src/libtomahawk/dropjob.cpp | 10 ++++ src/libtomahawk/utils/itunesparser.cpp | 81 +++++++++++--------------- src/libtomahawk/utils/itunesparser.h | 8 +-- 3 files changed, 48 insertions(+), 51 deletions(-) diff --git a/src/libtomahawk/dropjob.cpp b/src/libtomahawk/dropjob.cpp index 51d77d54f..f981133ec 100644 --- a/src/libtomahawk/dropjob.cpp +++ b/src/libtomahawk/dropjob.cpp @@ -123,6 +123,16 @@ DropJob::acceptsMimeData( const QMimeData* data, DropJob::DropTypes acceptedType // crude check for itunes tracks if ( data->hasFormat( "text/plain" ) && data->data( "text/plain" ).contains( "itunes" ) && data->data( "text/plain" ).contains( "album" ) + && ( acceptedType.testFlag(DropJob::Track) + || acceptedType.testFlag(DropJob::Album) + || acceptedType.testFlag(DropJob::All) + ) + ) + return true; + + // crude check for itunes artist + if ( data->hasFormat( "text/plain" ) && data->data( "text/plain" ).contains( "itunes" ) + && data->data( "text/plain" ).contains( "artist" ) && ( acceptedType.testFlag(DropJob::Track) || acceptedType.testFlag(DropJob::All) ) ) return true; diff --git a/src/libtomahawk/utils/itunesparser.cpp b/src/libtomahawk/utils/itunesparser.cpp index 551774564..ef0815ff3 100644 --- a/src/libtomahawk/utils/itunesparser.cpp +++ b/src/libtomahawk/utils/itunesparser.cpp @@ -19,7 +19,6 @@ */ #include "itunesparser.h" - #include "utils/logger.h" #include "utils/tomahawkutils.h" #include "query.h" @@ -38,8 +37,10 @@ ItunesParser::ItunesParser( const QStringList& Urls, QObject* parent, bool creat { m_createNewPlaylist = createNewPlaylist; - foreach ( const QString& url, Urls ) - lookupUrl( url ); + foreach ( const QString& url, Urls ){ + + lookupItunesUri( url ); + } } ItunesParser::ItunesParser( const QString& Url, QObject* parent, bool createNewPlaylist ) @@ -47,7 +48,7 @@ ItunesParser::ItunesParser( const QString& Url, QObject* parent, bool createNewP , m_single( true ) { m_createNewPlaylist = createNewPlaylist; - lookupUrl( Url ); + lookupItunesUri( Url ); } ItunesParser::~ItunesParser() @@ -55,64 +56,50 @@ ItunesParser::~ItunesParser() } - void -ItunesParser::lookupUrl( const QString& link ) +ItunesParser::lookupItunesUri( const QString& link ) { - qDebug() << Q_FUNC_INFO; - if( link.contains( "album" ) )// && link.contains( "?i=")) - lookupTrack(link); - - else return; // We only support tracks and playlists - -} - -void -ItunesParser::lookupTrack( const QString& link ) -{ - - tDebug() << "Got a QString " << link; - if ( !link.contains( "album" ) ) //&& !link.contains( "?i=" )) // we only support track links atm - return; // Itunes uri parsing, using regex // (\d+)(?:\?i=*)(\d+) = AlbumId and trackId - // (\d+)(?:\s*) = albumId + // (\d+)(?:\s*) = id QRegExp rxAlbumTrack( "(\\d+)(?:\\?i=*)(\\d+)" ); - QRegExp rxAlbum( "(\\d+)(?:\\s*)" ); - QString albumId, trackId; + QRegExp rxId( "(\\d+)(?:\\s*)" ); + QString id, trackId; // Doing a parse on regex in 2 stages, // first, look if we have both album and track id int pos = rxAlbumTrack.indexIn(link); - if (pos > -1) { - albumId = rxAlbumTrack.cap(1); + id = rxAlbumTrack.cap(1); trackId = rxAlbumTrack.cap(2); }else{ - // Second, if we dont have trackId, check for just albumid - int pos = rxAlbum.indexIn(link); + // Second, if we dont have trackId, check for just Id + int pos = rxId.indexIn(link); if (pos > -1) { - albumId = rxAlbum.cap(1); + id = rxId.cap(1); }else return; } - qDebug() << "Got Itunes link with Albumid " << albumId << "and trackid " <get( QNetworkRequest( url ) ); - connect( reply, SIGNAL( finished() ), this, SLOT( itunesTrackLookupFinished() ) ); - + connect( reply, SIGNAL( finished() ), this, SLOT( itunesResponseLookupFinished() ) ); m_queries.insert( reply ); } void -ItunesParser::itunesTrackLookupFinished() +ItunesParser::itunesResponseLookupFinished() { QNetworkReply* r = qobject_cast< QNetworkReply* >( sender() ); Q_ASSERT( r ); @@ -140,27 +127,27 @@ ItunesParser::itunesTrackLookupFinished() foreach(QVariant itune, itunesResponse){ QString title, artist, album; - if( !itune.toMap().value( "wrapperType" ).toString().contains( "track" ) ) - continue; + if( itune.toMap().value( "wrapperType" ).toString().contains( "track" ) ){ - title = itune.toMap().value( "trackName" ).toString(); - artist = itune.toMap().value( "artistName" ).toString(); - album = itune.toMap().value( "collectionName" ).toString(); - if ( title.isEmpty() && artist.isEmpty() ) // don't have enough... - { - tLog() << "Didn't get an artist and track name from itunes, not enough to build a query on. Aborting" << title << artist << album; + title = itune.toMap().value( "trackName" ).toString(); + artist = itune.toMap().value( "artistName" ).toString(); + album = itune.toMap().value( "collectionName" ).toString(); + if ( title.isEmpty() && artist.isEmpty() ) // don't have enough... + { + tLog() << "Didn't get an artist and track name from itunes, not enough to build a query on. Aborting" << title << artist << album; - }else{ + }else{ - Tomahawk::query_ptr q = Tomahawk::Query::get( artist, title, album, uuid(), true ); - m_tracks << q; + Tomahawk::query_ptr q = Tomahawk::Query::get( artist, title, album, uuid(), true ); + m_tracks << q; + } } } } else { - tLog() << "Error in network request to Spotify for track decoding:" << r->errorString(); + tLog() << "Error in network request to Itunes for track decoding:" << r->errorString(); } checkTrackFinished(); diff --git a/src/libtomahawk/utils/itunesparser.h b/src/libtomahawk/utils/itunesparser.h index 8b8499243..6a3f5d956 100644 --- a/src/libtomahawk/utils/itunesparser.h +++ b/src/libtomahawk/utils/itunesparser.h @@ -23,12 +23,12 @@ #include "dllmacro.h" #include "typedefs.h" - #include #include #include class QNetworkReply; +class TrackModel; namespace Tomahawk { @@ -45,17 +45,17 @@ public: explicit ItunesParser( const QStringList& trackUrls, QObject* parent = 0, bool createNewPl = false); virtual ~ItunesParser(); + signals: void track( const Tomahawk::query_ptr& track ); void tracks( const QList< Tomahawk::query_ptr > tracks ); void playlist( const Tomahawk::query_ptr& playlist ); private slots: - void itunesTrackLookupFinished(); + void itunesResponseLookupFinished(); private: - void lookupUrl( const QString& url ); - void lookupTrack( const QString& track ); + void lookupItunesUri( const QString& track ); void checkTrackFinished(); bool m_single;