diff --git a/src/infoplugins/generic/charts/ChartsPlugin.cpp b/src/infoplugins/generic/charts/ChartsPlugin.cpp index 1092abdf4..7f64f1bc6 100644 --- a/src/infoplugins/generic/charts/ChartsPlugin.cpp +++ b/src/infoplugins/generic/charts/ChartsPlugin.cpp @@ -37,8 +37,8 @@ #include "utils/TomahawkCache.h" #include "Source.h" -#define CHART_URL "http://charts.tomahawk-player.org/" -//#define CHART_URL "http://localhost:8080/" +//#define CHART_URL "http://charts.tomahawk-player.org/" +#define CHART_URL "http://localhost:8080/" #include #include @@ -665,8 +665,11 @@ ChartsPlugin::chartReturned() else setChartType( None ); + int count = 0; foreach ( const QVariant& chartR, chartResponse ) { + if( count++ >= 1) + break; QString title, artist, album, streamUrl; QVariantMap chartMap = chartR.toMap(); diff --git a/src/libtomahawk/CMakeLists.txt b/src/libtomahawk/CMakeLists.txt index 35d33e356..4968dcd52 100644 --- a/src/libtomahawk/CMakeLists.txt +++ b/src/libtomahawk/CMakeLists.txt @@ -120,6 +120,8 @@ set( libGuiSources utils/BinaryInstallerHelper.cpp utils/BinaryExtractWorker.cpp utils/SharedTimeLine.cpp + utils/ResultHintChecker.cpp + utils/CustomResultHintChecker.cpp utils/WebResultHintChecker.cpp utils/NetworkReply.cpp diff --git a/src/libtomahawk/playlist/PlaylistModel.cpp b/src/libtomahawk/playlist/PlaylistModel.cpp index f470aa157..e5f99fffa 100644 --- a/src/libtomahawk/playlist/PlaylistModel.cpp +++ b/src/libtomahawk/playlist/PlaylistModel.cpp @@ -35,7 +35,7 @@ #include "PlayableItem.h" #include "utils/TomahawkUtils.h" #include "utils/Logger.h" -#include +#include using namespace Tomahawk; @@ -281,7 +281,7 @@ PlaylistModel::insertEntries( const QList< Tomahawk::plentry_ptr >& entries, int connect( plitem, SIGNAL( dataChanged() ), SLOT( onDataChanged() ) ); - WebResultHintChecker::checkQuery( entry->query() ); + ResultHintChecker::checkQuery( entry->query() ); } if ( !m_waitingForResolved.isEmpty() ) diff --git a/src/libtomahawk/utils/CustomResultHintChecker.cpp b/src/libtomahawk/utils/CustomResultHintChecker.cpp new file mode 100644 index 000000000..bfab472ed --- /dev/null +++ b/src/libtomahawk/utils/CustomResultHintChecker.cpp @@ -0,0 +1,119 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2012, Hugo Lindström + * + * Tomahawk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tomahawk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tomahawk. If not, see . + */ + +#include "CustomResultHintChecker.h" +#include "ResultHintChecker.h" + +#include +#include +#include +#include "Query.h" +#include "utils/NetworkReply.h" +#include "utils/Closure.h" +using namespace Tomahawk; + + +CustomResultHintChecker::CustomResultHintChecker( const query_ptr& q ) + : ResultHintChecker( q ) + , m_query( q ) +{ + if( !isValid() ) + { + qDebug() << Q_FUNC_INFO << "invalid:" << url(); + return; + } + + qDebug() << Q_FUNC_INFO << url(); + + handleResultHint(); +} + +void +CustomResultHintChecker::handleResultHint() +{ + + if ( url().startsWith( "hnhh" ) ) + { + QUrl httpUrl = QUrl::fromUserInput( url() ); + httpUrl.setScheme( "http" ); + + NetworkReply* reply = new NetworkReply( TomahawkUtils::nam()->get( QNetworkRequest( httpUrl ) ) ); + + connect( this, SIGNAL( result( result_ptr ) ), this, SLOT( handleHnhhResult( result_ptr ) ) ); + connect( reply, SIGNAL( finished() ), SLOT( hnhhFinished() ) ); + return; + } +} + +void +CustomResultHintChecker::handleHnhhResult( result_ptr result ) +{ + if( !result.isNull() ) + { + + qDebug() << Q_FUNC_INFO << "Setting expiration for hotnewhiphop" << result->toString(); + qDebug() << Q_FUNC_INFO << url() << resultHint(); + + } +} + +void +CustomResultHintChecker::hnhhFinished() +{ + NetworkReply* r = qobject_cast( sender() ); + r->deleteLater(); + + bool foundStreamable = false; + // Intentionally accepts unknown error + if ( r->reply()->error() != ( QNetworkReply::NoError | QNetworkReply::UnknownNetworkError ) ) + { + + QXmlStreamReader xmlStream( r->reply()->readAll() ); + + if ( xmlStream.error() != QXmlStreamReader::NoError ) + { + qDebug() << "XML ERROR!" << xmlStream.errorString(); + } + else + { + while (!xmlStream.atEnd() ) + { + xmlStream.readNext(); + if ( xmlStream.isStartElement() ) + { + if ( xmlStream.name().toString() == "song" ) + { + const QString streamUrl = QString( xmlStream.attributes().value("filename").toLatin1() ); + // Dont save this resulthint, it needs to revalidate + qDebug() << Q_FUNC_INFO << "GOT STREAMABLE " << streamUrl; + setResultHint( streamUrl, false ); + foundStreamable = true; + break; + } + } + } + } + } + + if ( !foundStreamable) + { + removeHint(); + } + + deleteLater(); +} diff --git a/src/libtomahawk/utils/CustomResultHintChecker.h b/src/libtomahawk/utils/CustomResultHintChecker.h new file mode 100644 index 000000000..179560e58 --- /dev/null +++ b/src/libtomahawk/utils/CustomResultHintChecker.h @@ -0,0 +1,53 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2012, Hugo Lindström + * + * Tomahawk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tomahawk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tomahawk. If not, see . + */ +#ifndef Custom_RESULT_HINT_CHECKER_H +#define Custom_RESULT_HINT_CHECKER_H + +#include "Typedefs.h" +#include +#include +#include "ResultHintChecker.h" + +namespace Tomahawk +{ + +/** + * @brief The CustomResultHintChecker class + * This class is to be used with custom protocol to retrieve + * streamable urls for resulthints. + */ +class CustomResultHintChecker : public ResultHintChecker +{ + Q_OBJECT +public: + CustomResultHintChecker( const query_ptr& q ); + virtual ~CustomResultHintChecker(){} +private slots: + /* HotNewHiphop specific */ + void hnhhFinished(); + void handleHnhhResult( result_ptr result ); +private: + void handleResultHint(); + QString m_streamUrl; + query_ptr m_query; +}; + + +} + +#endif diff --git a/src/libtomahawk/utils/ResultHintChecker.cpp b/src/libtomahawk/utils/ResultHintChecker.cpp new file mode 100644 index 000000000..975ad44f0 --- /dev/null +++ b/src/libtomahawk/utils/ResultHintChecker.cpp @@ -0,0 +1,201 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2012, Leo Franchi + * Copyright 2012, Hugo Lindström + * + * Tomahawk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tomahawk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tomahawk. If not, see . + */ + +#include "WebResultHintChecker.h" +#include "CustomResultHintChecker.h" +#include +#include +#include +#include "Query.h" +#include "Result.h" +#include "Source.h" +#include "Pipeline.h" +#include "utils/NetworkReply.h" +#include "utils/Logger.h" + +using namespace Tomahawk; + +ResultHintChecker::ResultHintChecker( const query_ptr& q ) + : QObject( 0 ) + , m_query( q ) + , m_isValid( false ) +{ + Q_ASSERT( !m_query.isNull() ); + m_url = q->resultHint(); + + connect( m_query.data(), SIGNAL( resolvingFinished( bool ) ), this, SLOT( onResolvingFinished( bool ) ) ); + connect( m_query.data(), SIGNAL( resultsRemoved( const Tomahawk::result_ptr& ) ), this, SLOT( onResultsRemoved( const Tomahawk::result_ptr& ) ) ); + connect( m_query.data(), SIGNAL( resultsChanged() ), this, SLOT( onResultsChanged() ) ); + connect( m_query.data(), SIGNAL( updated() ), this, SLOT( onResultsChanged() ) ); + connect( m_query.data(), SIGNAL( resultsAdded( const QList& ) ), this, SLOT( onResultsAdded( const QList& ) ) ); + + check( m_url ); +} + + +ResultHintChecker::~ResultHintChecker() +{ +} + +/** + * @brief ResultHintChecker::checkQuery + * @param query + * Static function to initiate resulthint checkers + */ +void +ResultHintChecker::checkQuery( const query_ptr& query ) +{ + if ( !query->resultHint().isEmpty() && TomahawkUtils::whitelistedHttpResultHint( query->resultHint() ) ) + new WebResultHintChecker( query ); + + else if ( !query->resultHint().isEmpty() && TomahawkUtils::whitelistedCustomProtocolResultHint( query->resultHint() ) ) + new CustomResultHintChecker( query ); +} + + +void +ResultHintChecker::onResultsRemoved(const result_ptr &result) +{ + qDebug() << Q_FUNC_INFO << result->toString(); +} + +void +ResultHintChecker::onResultsChanged() +{ + foreach( const result_ptr& rp, m_query->results() ) + { + qDebug() << Q_FUNC_INFO << rp->toString(); + } +} + + +void +ResultHintChecker::checkQueries( const QList< query_ptr >& queries ) +{ + foreach ( const query_ptr& query, queries ) + checkQuery( query ); +} + +void +ResultHintChecker::onResolvingFinished( bool hasResults ) +{ + foreach ( const result_ptr& result, m_query->results() ) + { + qDebug() << Q_FUNC_INFO << "result url():" << result->url(); + } +} + +void +ResultHintChecker::onResultsAdded( const QList& results ) +{ + foreach( const result_ptr& result, results ) + { + qDebug() << Q_FUNC_INFO << "result url():" << result->url(); + } + +} + +QString +ResultHintChecker::resultHint() const +{ + return m_query->resultHint(); +} + +bool +ResultHintChecker::isValid() +{ + return m_isValid; +} + + +void +ResultHintChecker::check( const QUrl &url ) +{ + + qDebug() << Q_FUNC_INFO << url; + // Nothing to do + if ( url.isEmpty() || + ( !TomahawkUtils::whitelistedHttpResultHint( url.toString() ) && + !TomahawkUtils::whitelistedCustomProtocolResultHint( url.toString() ) + ) ) + { + if ( !url.isEmpty() || m_query->saveHTTPResultHint() ) + removeHint(); + + deleteLater(); + return; + } + + m_isValid = true; + +} + +result_ptr +ResultHintChecker::getResultPtr() +{ + result_ptr foundResult; + foreach ( const result_ptr& result, m_query->results() ) + { + if ( result->url() == m_url ) + { + foundResult = result; + break; + } + } + + return foundResult; +} + +void +ResultHintChecker::setResultHint(const QString &url, const bool saveHint) +{ + + qDebug() << Q_FUNC_INFO << "Setting new resulthint" << url << "save it? " << saveHint; + + result_ptr foundResult = getResultPtr(); + + if ( !foundResult.isNull() ) + { + qDebug() << Q_FUNC_INFO << "Removing old result " << foundResult->toString(); + m_query->removeResult( foundResult ); + } + + m_query->setResultHint( url ); + m_query->setSaveHTTPResultHint( saveHint ); + +} + + +void +ResultHintChecker::removeHint() +{ + tLog() << "Removing invalid resulthint from query:" << m_url; + + result_ptr foundResult = getResultPtr(); + + if ( !foundResult.isNull() ) + m_query->removeResult( foundResult ); + + if ( m_query->resultHint() == m_url ) + { + m_query->setResultHint( QString() ); + } + + m_query->setSaveHTTPResultHint( false ); +} diff --git a/src/libtomahawk/utils/ResultHintChecker.h b/src/libtomahawk/utils/ResultHintChecker.h new file mode 100644 index 000000000..b35c3966f --- /dev/null +++ b/src/libtomahawk/utils/ResultHintChecker.h @@ -0,0 +1,66 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2012, Leo Franchi + * Copyright 2012, Hugo Lindström + * + * Tomahawk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tomahawk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tomahawk. If not, see . + */ +#ifndef RESULT_HINT_CHECKER_H +#define RESULT_HINT_CHECKER_H + +#include "Typedefs.h" +#include + +namespace Tomahawk +{ + +class ResultHintChecker : public QObject +{ + Q_OBJECT +public: + explicit ResultHintChecker( const query_ptr& q ); + virtual ~ResultHintChecker(); + + static void checkQuery( const query_ptr& query ); + static void checkQueries( const QList< query_ptr >& queries ); + + bool isValid(); + + QString url() const { return m_url; } + QString resultHint() const; + + void setUrl( const QString& url ) { m_url = url; } + void setResultHint( const QString &url, const bool saveHint ); + void removeHint(); + +signals: + void result( result_ptr ); + +private slots: + void onResolvingFinished( bool hasResults ); + void onResultsAdded( const QList& results ); + void onResultsRemoved( const Tomahawk::result_ptr& result ); + void onResultsChanged(); + +private: + void check( const QUrl& url ); + result_ptr getResultPtr(); + query_ptr m_query; + QString m_url; + bool m_isValid; +}; + +} + +#endif diff --git a/src/libtomahawk/utils/TomahawkUtils.cpp b/src/libtomahawk/utils/TomahawkUtils.cpp index 77a0ccb82..306fb2bcf 100644 --- a/src/libtomahawk/utils/TomahawkUtils.cpp +++ b/src/libtomahawk/utils/TomahawkUtils.cpp @@ -950,6 +950,12 @@ whitelistedHttpResultHint( const QString& url ) } +bool +whitelistedCustomProtocolResultHint( const QString& url ) +{ + return url.startsWith( "hnhh" ); +} + } // ns #include "TomahawkUtils.moc" diff --git a/src/libtomahawk/utils/TomahawkUtils.h b/src/libtomahawk/utils/TomahawkUtils.h index 65d52d6e9..7488c7929 100644 --- a/src/libtomahawk/utils/TomahawkUtils.h +++ b/src/libtomahawk/utils/TomahawkUtils.h @@ -129,6 +129,7 @@ namespace TomahawkUtils void copyWithAuthentication( const QString& srcFile, const QDir dest, QObject* receiver ); DLLEXPORT bool whitelistedHttpResultHint( const QString& url ); + DLLEXPORT bool whitelistedCustomProtocolResultHint( const QString& url ); /** * This helper is designed to help "update" an existing playlist with a newer revision of itself. diff --git a/src/libtomahawk/utils/WebResultHintChecker.cpp b/src/libtomahawk/utils/WebResultHintChecker.cpp index ddea82547..fb2537799 100644 --- a/src/libtomahawk/utils/WebResultHintChecker.cpp +++ b/src/libtomahawk/utils/WebResultHintChecker.cpp @@ -1,7 +1,7 @@ /* === This file is part of Tomahawk Player - === * * Copyright 2012, Leo Franchi - * + * Copyright 2012, Hugo Lindström * Tomahawk is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or @@ -17,107 +17,29 @@ */ #include "WebResultHintChecker.h" +#include "ResultHintChecker.h" #include #include #include - #include "Query.h" -#include "Result.h" -#include "Source.h" -#include "Pipeline.h" #include "utils/NetworkReply.h" -#include "utils/Logger.h" using namespace Tomahawk; WebResultHintChecker::WebResultHintChecker( const query_ptr& q ) - : QObject( 0 ) - , m_query( q ) + : ResultHintChecker( q ) { - Q_ASSERT( !m_query.isNull() ); - - 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 ) ); -} - - -WebResultHintChecker::~WebResultHintChecker() -{ -} - - -void -WebResultHintChecker::checkQuery( const query_ptr& query ) -{ - 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( !isValid() ) { - if ( !url.isEmpty() || m_query->saveHTTPResultHint() ) - removeHint(); - - deleteLater(); return; } - NetworkReply* reply = new NetworkReply( TomahawkUtils::nam()->head( QNetworkRequest( url ) ) ); + NetworkReply* reply = new NetworkReply( TomahawkUtils::nam()->head( QNetworkRequest( QUrl::fromUserInput( url() ) ) ) ); 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 ); -} - - void WebResultHintChecker::headFinished() { diff --git a/src/libtomahawk/utils/WebResultHintChecker.h b/src/libtomahawk/utils/WebResultHintChecker.h index 58dc79c95..469e2fb68 100644 --- a/src/libtomahawk/utils/WebResultHintChecker.h +++ b/src/libtomahawk/utils/WebResultHintChecker.h @@ -1,6 +1,7 @@ /* === This file is part of Tomahawk Player - === * * Copyright 2012, Leo Franchi + * Copyright 2012, Hugo Lindström * * Tomahawk is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,31 +20,21 @@ #define WEB_RESULT_HINT_CHECKER_H #include "Typedefs.h" - #include +#include "ResultHintChecker.h" namespace Tomahawk { -class WebResultHintChecker : public QObject +class WebResultHintChecker : public ResultHintChecker { Q_OBJECT public: WebResultHintChecker( const query_ptr& q ); - virtual ~WebResultHintChecker(); - - static void checkQuery( const query_ptr& query ); - static void checkQueries( const QList< query_ptr >& queries ); - + virtual ~WebResultHintChecker(){} private slots: void headFinished(); - - void check( const QUrl& url ); - - void onResolvingFinished( bool hasResults ); private: - void removeHint(); - query_ptr m_query; QString m_url; };