diff --git a/src/libtomahawk/resolvers/QtScriptResolver.cpp b/src/libtomahawk/resolvers/QtScriptResolver.cpp index 1c5247ec3..da08c9a86 100644 --- a/src/libtomahawk/resolvers/QtScriptResolver.cpp +++ b/src/libtomahawk/resolvers/QtScriptResolver.cpp @@ -192,6 +192,46 @@ QtScriptResolverHelper::addAlbumResults( const QVariantMap& results ) } +void +QtScriptResolverHelper::addAlbumTrackResults( const QVariantMap& results ) +{ + qDebug() << "Resolver reporting album tracks:" << results; + QString artistName = results.value( "artist" ).toString(); + if ( artistName.trimmed().isEmpty() ) + return; + QString albumName = results.value( "album" ).toString(); + if ( albumName.trimmed().isEmpty() ) + return; + + Tomahawk::artist_ptr artist = Tomahawk::Artist::get( artistName, false ); + Tomahawk::album_ptr album = Tomahawk::Album::get( artist, albumName, false ); + + QList< Tomahawk::result_ptr > tracks = m_resolver->parseResultVariantList( results.value("results").toList() ); + + QString qid = results.value("qid").toString(); + + Tomahawk::collection_ptr collection = Tomahawk::collection_ptr(); + foreach ( const Tomahawk::collection_ptr& coll, m_resolver->collections() ) + { + if ( coll->name() == qid ) + { + collection = coll; + } + } + if ( collection.isNull() ) + return; + + QList< Tomahawk::query_ptr > queries; + foreach ( const Tomahawk::result_ptr& result, tracks ) + queries.append( result->toQuery() ); + + tDebug() << Q_FUNC_INFO << "about to push" << tracks.count() << "tracks"; + + QMetaObject::invokeMethod( collection.data(), "onTracksFetched", Qt::QueuedConnection, + Q_ARG( QList< Tomahawk::query_ptr >, queries ) ); +} + + void QtScriptResolverHelper::setResolverConfig( const QVariantMap& config ) { @@ -518,6 +558,37 @@ QtScriptResolver::albums( const Tomahawk::collection_ptr& collection, const Toma void QtScriptResolver::tracks( const Tomahawk::collection_ptr& collection, const Tomahawk::album_ptr& album ) { + if ( QThread::currentThread() != thread() ) + { + QMetaObject::invokeMethod( this, "tracks", Qt::QueuedConnection, + Q_ARG( Tomahawk::collection_ptr, collection ), + Q_ARG( Tomahawk::album_ptr, album ) ); + return; + } + + if ( !m_collections.contains( collection->name() ) || //if the collection doesn't belong to this resolver + !capabilities().testFlag( Browsable ) ) //or this resolver doesn't even support collections + { + QMetaObject::invokeMethod( collection.data(), "onTracksFetched", Qt::QueuedConnection, + Q_ARG( QList< Tomahawk::query_ptr >, QList< Tomahawk::query_ptr >() ) ); + return; + } + + QString eval = QString( "resolver.tracks( '%1', '%2', '%3' );" ) + .arg( collection->name().replace( "'", "\\'" ) ) + .arg( album->artist()->name().replace( "'", "\\'" ) ) + .arg( album->name().replace( "'", "\\'" ) ); + + QVariantMap m = m_engine->mainFrame()->evaluateJavaScript( eval ).toMap(); + if ( m.isEmpty() ) + { + // if the resolver doesn't return anything, async api is used + return; + } + + qDebug() << "Tracks JavaScript Result:" << m; + + m_resolverHelper->addAlbumTrackResults( m ); } diff --git a/src/libtomahawk/resolvers/QtScriptResolver.h b/src/libtomahawk/resolvers/QtScriptResolver.h index 1a7573c1b..659754592 100644 --- a/src/libtomahawk/resolvers/QtScriptResolver.h +++ b/src/libtomahawk/resolvers/QtScriptResolver.h @@ -74,6 +74,7 @@ public slots: void addArtistResults( const QVariantMap& results ); void addAlbumResults( const QVariantMap& results ); + void addAlbumTrackResults( const QVariantMap& results ); private: QString m_scriptPath, m_urlCallback; diff --git a/src/libtomahawk/resolvers/ScriptCollection.cpp b/src/libtomahawk/resolvers/ScriptCollection.cpp index 2f0aa4142..93d7d9fce 100644 --- a/src/libtomahawk/resolvers/ScriptCollection.cpp +++ b/src/libtomahawk/resolvers/ScriptCollection.cpp @@ -124,7 +124,7 @@ ScriptCollection::albums( const Tomahawk::artist_ptr& artist ) void ScriptCollection::tracks( const Tomahawk::album_ptr& album ) { - emit tracksResult( QList< Tomahawk::query_ptr >() ); + m_resolver->tracks( m_resolver->collections().value( name() ), album ); } @@ -145,4 +145,5 @@ ScriptCollection::onAlbumsFetched( const QList& albums ) void ScriptCollection::onTracksFetched( const QList& tracks ) { + emit tracksResult( tracks ); }