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

* New WebResultHintChecker API. Class will be renamed soon.

This commit is contained in:
Christian Muehlhaeuser
2013-04-26 10:50:24 +02:00
parent 5fe963fa50
commit 13ab38ae8f
2 changed files with 44 additions and 86 deletions

View File

@@ -1,6 +1,7 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> === /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
* *
* Copyright 2012, Leo Franchi <lfranchi@kde.org> * Copyright 2012, Leo Franchi <lfranchi@kde.org>
* Copyright 2013, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* *
* Tomahawk is free software: you can redistribute it and/or modify * Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@@ -25,25 +26,17 @@
#include "Query.h" #include "Query.h"
#include "Result.h" #include "Result.h"
#include "Source.h" #include "Source.h"
#include "Pipeline.h"
#include "utils/NetworkReply.h"
#include "utils/Logger.h" #include "utils/Logger.h"
using namespace Tomahawk; using namespace Tomahawk;
WebResultHintChecker::WebResultHintChecker( const query_ptr& q ) WebResultHintChecker::WebResultHintChecker( const query_ptr& query, const QList< result_ptr >& results )
: QObject( 0 ) : QObject( 0 )
, m_query( q ) , m_query( query )
, m_results( results )
{ {
Q_ASSERT( !m_query.isNull() ); check();
m_url = q->resultHint();
if ( Pipeline::instance()->isResolving( m_query ) )
connect( m_query.data(), SIGNAL( resolvingFinished( bool ) ), this, SLOT( onResolvingFinished( bool ) ) );
else
check( QUrl::fromUserInput( m_url ) );
} }
@@ -53,68 +46,19 @@ WebResultHintChecker::~WebResultHintChecker()
void void
WebResultHintChecker::checkQuery( const query_ptr& query ) WebResultHintChecker::check()
{ {
if ( !query->resultHint().isEmpty() && query->resultHint().startsWith( "http" ) ) foreach ( const result_ptr& result, m_results )
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() ) tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Checking http url:" << result->url();
removeHint(); QUrl url = QUrl::fromUserInput( result->url() );
if ( url.isEmpty() || !url.toString().startsWith( "http" ) )
deleteLater(); continue;
return;
}
NetworkReply* reply = new NetworkReply( TomahawkUtils::nam()->head( QNetworkRequest( url ) ) ); NetworkReply* reply = new NetworkReply( TomahawkUtils::nam()->head( QNetworkRequest( url ) ) );
m_replies.insert( reply, result );
connect( reply, SIGNAL( finished() ), SLOT( headFinished() ) ); connect( reply, SIGNAL( finished() ), SLOT( headFinished() ) );
}
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 );
} }
@@ -124,12 +68,20 @@ WebResultHintChecker::headFinished()
NetworkReply* r = qobject_cast<NetworkReply*>( sender() ); NetworkReply* r = qobject_cast<NetworkReply*>( sender() );
r->deleteLater(); r->deleteLater();
if ( r->reply()->error() != QNetworkReply::NoError ) if ( !m_replies.contains( r ) )
{ return;
// Error getting headers for the http resulthint, remove it from the result
// as it's definitely not playable
removeHint();
}
deleteLater(); result_ptr result = m_replies.value( r );
m_replies.remove( r );
if ( r->reply()->error() == QNetworkReply::NoError )
{
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Found valid http url:" << result->url();
m_validResults << result;
}
else
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Found invalid http url:" << result->url() << r->reply()->error();
if ( m_replies.isEmpty() )
emit done();
} }

View File

@@ -1,6 +1,7 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> === /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
* *
* Copyright 2012, Leo Franchi <lfranchi@kde.org> * Copyright 2012, Leo Franchi <lfranchi@kde.org>
* Copyright 2013, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* *
* Tomahawk is free software: you can redistribute it and/or modify * Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@@ -15,12 +16,15 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>. * along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef WEB_RESULT_HINT_CHECKER_H #ifndef WEB_RESULT_HINT_CHECKER_H
#define WEB_RESULT_HINT_CHECKER_H #define WEB_RESULT_HINT_CHECKER_H
#include "Typedefs.h" #include "Typedefs.h"
#include "utils/NetworkReply.h"
#include <QObject> #include <QObject>
#include <QSet>
namespace Tomahawk namespace Tomahawk
{ {
@@ -29,23 +33,25 @@ class WebResultHintChecker : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
WebResultHintChecker( const query_ptr& q ); WebResultHintChecker( const query_ptr& query, const QList< result_ptr >& results );
virtual ~WebResultHintChecker(); virtual ~WebResultHintChecker();
static void checkQuery( const query_ptr& query ); query_ptr query() const { return m_query; }
static void checkQueries( const QList< query_ptr >& queries ); QList< result_ptr > results() const { return m_results; }
QList< result_ptr > validResults() const { return m_validResults; }
signals:
void done();
private slots: private slots:
void check();
void headFinished(); void headFinished();
void check( const QUrl& url );
void onResolvingFinished( bool hasResults );
private: private:
void removeHint();
query_ptr m_query; query_ptr m_query;
QString m_url; QList< result_ptr > m_results;
QList< result_ptr > m_validResults;
QHash< NetworkReply*, Tomahawk::result_ptr > m_replies;
}; };
} }