From 807eacd272767d624937fa818e0bace0e76274d7 Mon Sep 17 00:00:00 2001 From: Leo Franchi Date: Fri, 21 Sep 2012 15:31:21 -0400 Subject: [PATCH] More robust result hint checking --- .../utils/WebResultHintChecker.cpp | 96 ++++++++++++++----- src/libtomahawk/utils/WebResultHintChecker.h | 9 +- 2 files changed, 77 insertions(+), 28 deletions(-) diff --git a/src/libtomahawk/utils/WebResultHintChecker.cpp b/src/libtomahawk/utils/WebResultHintChecker.cpp index 710fcbe5f..444337184 100644 --- a/src/libtomahawk/utils/WebResultHintChecker.cpp +++ b/src/libtomahawk/utils/WebResultHintChecker.cpp @@ -22,10 +22,12 @@ #include "Source.h" #include "utils/Closure.h" #include "utils/Logger.h" +#include "Pipeline.h" #include #include #include +#include using namespace Tomahawk; @@ -33,25 +35,14 @@ WebResultHintChecker::WebResultHintChecker( const query_ptr& q ) : QObject( 0 ) , m_query( q ) { + Q_ASSERT( !m_query.isNull() ); + m_url = q->resultHint(); - foreach ( const result_ptr& result, q->results() ) - { - if ( result->url() == m_url ) - { - m_result = result; - break; - } - } - - // Nothing to do - if ( m_url.isEmpty() || !m_url.startsWith( "http" ) ) - { - deleteLater(); - return; - } - - check( m_url ); + if ( Pipeline::instance()->isResolving( m_query ) ) + connect( m_query.data(), SIGNAL( resolvingFinished( bool ) ), this, SLOT( onResolvingFinished( bool ) ) ); + else + check( QUrl::fromUserInput( m_url ) ); } WebResultHintChecker::~WebResultHintChecker() @@ -61,13 +52,71 @@ WebResultHintChecker::~WebResultHintChecker() void -WebResultHintChecker::check( const QString &url ) +WebResultHintChecker::checkQuery( const query_ptr& query ) { - QNetworkReply* reply = TomahawkUtils::nam()->head( QNetworkRequest( QUrl( url ) ) ); + if ( !query->resultHint().isEmpty() && query->resultHint().startsWith( "http" ) ) + new WebResultHintChecker( query ); +} + + +void +WebResultHintChecker::checkQueries( const QList< query_ptr >& queries ) +{ + foreach ( const query_ptr& query, queries ) + checkQuery( query ); +} + + +void +WebResultHintChecker::onResolvingFinished( bool hasResults ) +{ + Q_UNUSED( hasResults ); + + check( QUrl::fromUserInput( m_url ) ); +} + + +void +WebResultHintChecker::check( const QUrl &url ) +{ + // Nothing to do + if ( url.isEmpty() || !url.toString().startsWith( "http" ) ) + { + if ( !url.isEmpty() || m_query->saveHTTPResultHint() ) + removeHint(); + + deleteLater(); + return; + } + + QNetworkReply* reply = TomahawkUtils::nam()->head( QNetworkRequest( url ) ); NewClosure( reply, SIGNAL( finished() ), this, SLOT( headFinished( QNetworkReply* ) ), reply ); } +void +WebResultHintChecker::removeHint() +{ + tLog() << "Removing HTTP result from query since HEAD request failed to verify it was a valid url:" << m_url; + + result_ptr foundResult; + foreach ( const result_ptr& result, m_query->results() ) + { + if ( result->url() == m_url ) + { + foundResult = result; + break; + } + } + + if ( !foundResult.isNull() ) + m_query->removeResult( foundResult ); + if ( m_query->resultHint() == m_url ) + m_query->setResultHint( QString() ); + m_query->setSaveHTTPResultHint( false ); +} + + void WebResultHintChecker::headFinished( QNetworkReply* reply ) { @@ -77,7 +126,7 @@ WebResultHintChecker::headFinished( QNetworkReply* reply ) if ( redir.isValid() ) { const QUrl url = reply->url().resolved( redir ); - check( url.toString() ); + check( url ); return; } @@ -85,12 +134,7 @@ WebResultHintChecker::headFinished( QNetworkReply* reply ) { // Error getting headers for the http resulthint, remove it from the result // as it's definitely not playable - tLog() << "Removing HTTP result from query since HEAD request failed to verify it was a valid url:" << m_url; - if ( !m_result.isNull() ) - m_query->removeResult( m_result ); - if ( m_query->resultHint() == m_url ) - m_query->setResultHint( QString() ); - m_query->setSaveHTTPResultHint( false ); + removeHint(); } deleteLater(); diff --git a/src/libtomahawk/utils/WebResultHintChecker.h b/src/libtomahawk/utils/WebResultHintChecker.h index 92e5f306e..cdfeee8d7 100644 --- a/src/libtomahawk/utils/WebResultHintChecker.h +++ b/src/libtomahawk/utils/WebResultHintChecker.h @@ -33,14 +33,19 @@ public: WebResultHintChecker( const query_ptr& q ); virtual ~WebResultHintChecker(); + static void checkQuery( const query_ptr& query ); + static void checkQueries( const QList< query_ptr >& queries ); + private slots: void headFinished( QNetworkReply* reply ); + void check( const QUrl& url ); + + void onResolvingFinished( bool hasResults ); private: - void check( const QString& url ); + void removeHint(); query_ptr m_query; - result_ptr m_result; QString m_url; };