1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-09-06 12:10:47 +02:00

Compare commits

...

17 Commits

Author SHA1 Message Date
Hugo Lindström
7645862c7b Merge branch 'master' into resulthintcheck
Conflicts:
	src/libtomahawk/utils/TomahawkUtils.cpp
2013-01-21 12:09:18 +01:00
Hugo Lindström
c63fff9981 Allow resolving file:// directly 2012-12-21 08:44:01 +01:00
Hugo Lindström
1eb01a9074 Merge branch 'master' into resulthintcheck 2012-12-21 07:54:07 +01:00
Hugo Lindström
02409789bb Merge branch 'master' into resulthintcheck 2012-12-13 20:48:13 +01:00
Hugo Lindström
cfaecb32d3 Add results to expirationTimer when resolving 2012-12-13 20:47:39 +01:00
Hugo Lindström
da77db0c36 Merge branch 'master' into resulthintcheck 2012-12-10 19:06:23 +01:00
Hugo Lindström
41424326a0 Merge branch 'master' into resulthintcheck 2012-12-09 12:57:09 +01:00
Hugo Lindström
b1f1cea325 Merge branch 'master' into resulthintcheck 2012-12-07 13:29:59 +01:00
Hugo Lindström
8b7ad7ef0e Merge branch 'master' into resulthintcheck 2012-12-06 15:20:18 +01:00
Hugo Lindström
3476ff11ef Merge branch 'master' into resulthintcheck 2012-12-05 22:09:45 +01:00
Hugo Lindström
a415342650 Merge branch 'master' into resulthintcheck 2012-12-05 11:33:13 +01:00
Hugo Lindström
49c51c3dc1 Merge branch 'master' into resulthintcheck 2012-12-05 02:24:44 +01:00
Hugo Lindström
cebcf9d789 Merge branch 'master' into resulthintcheck 2012-12-04 23:26:34 +01:00
Hugo Lindström
17aa3316e9 Add ResultExpirationTimer 2012-12-04 23:22:40 +01:00
Hugo Lindström
ae12066439 Merge resulthint 2012-12-04 11:15:19 +01:00
Hugo Lindström
3b95e09512 Merge branch 'master' into resulthintcheck 2012-12-04 10:54:25 +01:00
Hugo Lindström
3c5108ccc8 Accept custom uri in databaseImpl to 2012-12-03 20:52:13 +01:00
17 changed files with 705 additions and 112 deletions

View File

@@ -112,6 +112,9 @@ set( libGuiSources
utils/BinaryInstallerHelper.cpp
utils/BinaryExtractWorker.cpp
utils/SharedTimeLine.cpp
utils/ResultExpirationTimer.cpp
utils/ResultHintChecker.cpp
utils/CustomResultHintChecker.cpp
utils/WebResultHintChecker.cpp
utils/NetworkReply.cpp

View File

@@ -30,6 +30,7 @@
#include "filemetadata/MetadataEditor.h"
#include "utils/TomahawkUtilsGui.h"
#include "utils/ResultExpirationTimer.h"
#include "utils/Logger.h"
#include "ExternalResolverGui.h"
@@ -42,6 +43,7 @@ typedef QMap< QString, QPixmap > SourceIconCache;
Q_GLOBAL_STATIC( SourceIconCache, sourceIconCache );
static QMutex s_sourceIconMutex;
inline QString sourceCacheKey( Resolver* resolver, const QSize& size, TomahawkUtils::ImageMode style )
{
QString str;
@@ -88,8 +90,10 @@ Result::Result( const QString& url )
, m_score( 0 )
, m_trackId( 0 )
, m_fileId( 0 )
, m_expires( 0 )
{
connect( Pipeline::instance(), SIGNAL( resolverRemoved( Tomahawk::Resolver* ) ), SLOT( onResolverRemoved( Tomahawk::Resolver* ) ), Qt::QueuedConnection );
}
@@ -111,6 +115,19 @@ Result::deleteLater()
QObject::deleteLater();
}
void
Result::setExpires( const qint64 &expires )
{
m_expires = expires;
}
void
Result::expired()
{
m_resolvedBy.clear();
m_url.clear();
emit statusChanged();
}
void
Result::onResolverRemoved( Tomahawk::Resolver* resolver )

View File

@@ -37,9 +37,11 @@ class DatabaseCommand_AllTracks;
class DatabaseCommand_AddFiles;
class DatabaseCommand_LoadFile;
namespace Tomahawk
{
class ResultHintChecker;
class Resolver;
class DLLEXPORT Result : public QObject
@@ -51,6 +53,7 @@ friend class ::DatabaseCommand_Resolve;
friend class ::DatabaseCommand_AllTracks;
friend class ::DatabaseCommand_AddFiles;
friend class ::DatabaseCommand_LoadFile;
friend class Tomahawk::ResultHintChecker;
public:
static Tomahawk::result_ptr get( const QString& url );
@@ -64,6 +67,7 @@ public:
Tomahawk::Resolver* resolvedBy() const;
void setResolvedBy( Tomahawk::Resolver* resolver );
void expired();
float score() const;
RID id() const;
bool isOnline() const;
@@ -109,7 +113,8 @@ public:
void setModificationTime( unsigned int modtime ) { m_modtime = modtime; }
void setYear( unsigned int year ) { m_year = year; }
void setDiscNumber( unsigned int discnumber ) { m_discnumber = discnumber; }
void setExpires( const qint64& expires );
qint64 getExpires() const { return m_expires; }
QVariantMap attributes() const { return m_attributes; }
void setAttributes( const QVariantMap& map ) { m_attributes = map; updateAttributes(); }
@@ -137,7 +142,7 @@ private:
explicit Result();
void updateAttributes();
void setUrl( const QString& url ){ m_url = url; }
mutable RID m_rid;
collection_ptr m_collection;
Tomahawk::query_ptr m_query;
@@ -161,6 +166,7 @@ private:
unsigned int m_discnumber;
int m_year;
float m_score;
qint64 m_expires;
QVariantMap m_attributes;
unsigned int m_trackId, m_fileId;

View File

@@ -615,12 +615,8 @@ DatabaseImpl::resultFromHint( const Tomahawk::query_ptr& origquery )
if ( s.isNull() )
return res;
}
else if ( url.contains( "file://" ) )
{
s = SourceList::instance()->getLocal();
fileUrl = url;
}
else if ( TomahawkUtils::whitelistedHttpResultHint( url ) )
else if ( TomahawkUtils::whitelistedHttpResultHint( url ) ||
TomahawkUtils::whitelistedCustomProtocolResultHint( url ) )
{
// Return http resulthint directly
res = Tomahawk::Result::get( url );

View File

@@ -35,7 +35,7 @@
#include "PlayableItem.h"
#include "utils/TomahawkUtils.h"
#include "utils/Logger.h"
#include <utils/WebResultHintChecker.h>
#include <utils/ResultHintChecker.h>
using namespace Tomahawk;
@@ -285,7 +285,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() )

View File

@@ -33,6 +33,7 @@
#include "utils/TomahawkUtilsGui.h"
#include "utils/Logger.h"
#include "utils/ResultExpirationTimer.h"
#include "config.h"
@@ -459,6 +460,7 @@ QtScriptResolver::parseResultVariantList( const QVariantList& reslist )
rp->setFriendlySource( name() );
rp->setPurchaseUrl( m.value( "purchaseUrl" ).toString() );
rp->setLinkUrl( m.value( "linkUrl" ).toString() );
rp->setExpires( m.value( "expires" ).toLongLong() );
rp->setScore( m.value( "score" ).toFloat() );
rp->setDiscNumber( m.value( "discnumber" ).toUInt() );
@@ -484,6 +486,12 @@ QtScriptResolver::parseResultVariantList( const QVariantList& reslist )
}
rp->setResolvedBy( this );
if ( m.contains( "expires" ) )
{
rp->setExpires( m.value( "expires" ).toLongLong() );
Tomahawk::ResultExpirationTimer::instance()->addResult( rp );
}
results << rp;
}

View File

@@ -0,0 +1,129 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2012, Hugo Lindström <hugolm84@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "CustomResultHintChecker.h"
#include "ResultHintChecker.h"
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QXmlStreamReader>
#include "Query.h"
#include "utils/NetworkReply.h"
#include <QFileInfo>
#include <QFile>
using namespace Tomahawk;
/**
* @brief CustomResultHintChecker::CustomResultHintChecker
* !!@note: only set prevUrl if you know that you have a previous result that needs revalidate
*/
CustomResultHintChecker::CustomResultHintChecker( const query_ptr& q, const QString& prevUrl )
: ResultHintChecker( q )
, m_prevUrl( prevUrl )
{
if ( !isValid() )
{
qDebug() << Q_FUNC_INFO << "invalid:" << url();
return;
}
handleResultHint();
}
void
CustomResultHintChecker::handleResultHint()
{
if ( url().startsWith( "file:" ) )
{
QFileInfo tmpFile( QUrl::fromUserInput( url() ).toLocalFile() );
if( !tmpFile.exists() )
{
qDebug() << Q_FUNC_INFO << url();
removeHint();
}
}
if ( url().startsWith( "hnhh" ) )
{
QUrl httpUrl = QUrl::fromUserInput( url() );
httpUrl.setScheme( "http" );
NetworkReply* reply = new NetworkReply( TomahawkUtils::nam()->get( QNetworkRequest( httpUrl ) ) );
connect( reply, SIGNAL( finished() ), SLOT( hnhhFinished() ) );
return;
}
}
void
CustomResultHintChecker::hnhhFinished()
{
NetworkReply* r = qobject_cast<NetworkReply*>( 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 QUrl stream = QUrl::fromUserInput( QString( xmlStream.attributes().value("filename").toLatin1() ) );
// Dont save this resulthint, it needs to revalidate
qDebug() << Q_FUNC_INFO << "GOT STREAMABLE " << stream.toString();
bool ok;
const qint64 expires = stream.queryItemValue( QString("e") ).toLongLong( &ok );
if( ok )
{
/// @note: Previous url is set, use that to find result
/// that needs to be updated
if ( !m_prevUrl.isEmpty() )
setUrl( m_prevUrl );
setExpires( expires );
setResultUrl( stream.toString() );
foundStreamable = true;
}
break;
}
}
}
}
}
if ( !foundStreamable)
{
removeHint();
}
deleteLater();
}

View File

@@ -0,0 +1,59 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2012, Hugo Lindström <hugolm84@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef Custom_RESULT_HINT_CHECKER_H
#define Custom_RESULT_HINT_CHECKER_H
#include "Typedefs.h"
#include <QUrl>
#include <QObject>
#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:
/**
* @brief CustomResultHintChecker
* @note Set a previous url when you you know that you have a result, and that it
* needs to be revalidated
*/
explicit CustomResultHintChecker( const query_ptr& q, const QString& prevUrl = QString() );
virtual ~CustomResultHintChecker(){}
private slots:
/**
* Add slots for specific custom protocol checks
*/
/* HotNewHiphop specific */
void hnhhFinished();
private:
void handleResultHint();
QString m_prevUrl;
};
}
#endif

View File

@@ -31,6 +31,7 @@
/* taglib */
#include <taglib/fileref.h>
#include <taglib/tag.h>
#include "filemetadata/taghandlers/tag.h"
using namespace Tomahawk;
@@ -77,12 +78,15 @@ M3uLoader::getTags( const QFileInfo& info )
TagLib::FileRef f( encodedName );
if( f.isNull() )
return;
TagLib::Tag *tag = f.tag();
Tag* tag = Tag::fromFile( f );
if( !tag )
return;
QString artist = TStringToQString( tag->artist() ).trimmed();
QString album = TStringToQString( tag->album() ).trimmed();
QString track = TStringToQString( tag->title() ).trimmed();
QString artist, album, track;
artist = tag->artist().trimmed();
album = tag->album().trimmed();
track = tag->title().trimmed();
if ( artist.isEmpty() || track.isEmpty() )
{
@@ -91,12 +95,21 @@ M3uLoader::getTags( const QFileInfo& info )
}
else
{
qDebug() << Q_FUNC_INFO << artist << track << album;
Tomahawk::query_ptr q = Tomahawk::Query::get( artist, track, album, uuid(), !m_createNewPlaylist );
if ( !q.isNull() )
{
q->setResultHint( "file://" + info.absoluteFilePath() );
q->setSaveHTTPResultHint( true );
TagLib::AudioProperties *audioProp = f.audioProperties();
if ( audioProp )
{
q->setDuration( audioProp->length() );
}
q->setComposer( tag->composer() );
q->setAlbumPos( tag->track() );
q->setDiscNumber( tag->discNumber() );
qDebug() << "Adding resulthint" << q->resultHint();
m_tracks << q;
}

View File

@@ -0,0 +1,148 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2012, Hugo Lindström <hugolm84@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "ResultExpirationTimer.h"
#include "WebResultHintChecker.h"
#include "CustomResultHintChecker.h"
#include "Query.h"
#include "Result.h"
#include "utils/Logger.h"
using namespace Tomahawk;
ResultExpirationTimer* ResultExpirationTimer::s_instance = 0;
ResultExpirationTimer*
ResultExpirationTimer::instance()
{
if ( !s_instance )
s_instance = new ResultExpirationTimer;
return s_instance;
}
/**
* @brief ResultExpirationTimer::ResultExpirationTimer
*/
ResultExpirationTimer::ResultExpirationTimer( QObject *parent )
:QObject(parent)
,m_currentTimeout( 0 )
{
connect( this, SIGNAL( resultAdded() ), this, SLOT( updateTimer() ) );
}
/**
* @brief ResultExpirationTimer::updateTimer
* Takes first pair and set a new expire timer
*/
void
ResultExpirationTimer::updateTimer()
{
QMap< qint64, QList< result_ptr > >::Iterator it = m_results.begin();
if ( it != m_results.end() && ( it.key() < m_currentTimeout || m_currentTimeout == 0 ) )
{
m_currentTimeout = it.key();
qint64 expire = expires( m_currentTimeout )*1000;
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "SET TIMER TO EXPIRE IN " << m_currentTimeout << expire << "msec";
QTimer::singleShot( expire, this, SLOT( onExpired() ) );
}
}
/**
* @brief ResultExpirationTimer::expires
* @param expires
* @return qint64
*/
qint64
ResultExpirationTimer::expires( qint64 expires ) const
{
qint64 currentEpoch = QDateTime::currentMSecsSinceEpoch()/1000;
qint64 expiresInSeconds = expires-currentEpoch;
if ( expiresInSeconds > 0 )
{
return expiresInSeconds;
}
return 0;
}
/**
* @brief ResultExpirationTimer::onExpired
* onExpired slot, takes first pair, and force a new
* url lookup on it
*/
void
ResultExpirationTimer::onExpired()
{
qDebug() << Q_FUNC_INFO;
QMap< qint64, QList< result_ptr > >::Iterator it = m_results.begin();
if ( it != m_results.end() )
{
foreach ( const result_ptr& result, it.value() )
{
if ( m_queries[ result->url() ]->resultHint().isEmpty() )
{
result->expired();
}
else
{
if ( TomahawkUtils::whitelistedHttpResultHint( m_queries[ result->url() ]->resultHint() ) )
{
new WebResultHintChecker( m_queries[ result->url() ] );
}
else if ( TomahawkUtils::whitelistedCustomProtocolResultHint( m_queries[ result->url() ]->resultHint() ) )
{
new CustomResultHintChecker( m_queries[ result->url() ], result->url() );
}
}
}
m_results.remove( it.key() );
m_currentTimeout = 0;
}
}
/**
* @brief ResultExpirationTimer::addResult
* @param query_ptr
* @param result_ptr
* Adds a result to be checked on expiration
*/
void
ResultExpirationTimer::addResult( const query_ptr& query, const Tomahawk::result_ptr& result )
{
if ( result->getExpires() > 0 )
{
m_results[ result->getExpires() ].append( result );
m_queries[ result->url() ] = query;
emit resultAdded();
}
}
void
ResultExpirationTimer::addResult( const Tomahawk::result_ptr& result )
{
addResult( result->toQuery(), result );
}

View File

@@ -0,0 +1,67 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2012, Hugo Lindström <hugolm84@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef RESULT_EXPIRATION_TIMER_H
#define RESULT_EXPIRATION_TIMER_H
#include "Typedefs.h"
#include <QObject>
#include <QTimer>
#include <QDateTime>
#include "Resolver.h"
namespace Tomahawk
{
/**
* @brief The ResultExpirationTimer class
* Use this when you have a result that expires in a certain time
*
* (result_ptr) result->setExpires( expireTimestamp );
* ResultExpirationTimer::instance()->addResult( query_ptr, result_ptr );
* Will fire on next expiration ( timestamp - msecSinceEpoch ) and trigger
* result url update
*/
class ResultExpirationTimer : public QObject
{
Q_OBJECT
public:
static ResultExpirationTimer* instance();
void addResult( const query_ptr& query, const Tomahawk::result_ptr& result );
void addResult(const Tomahawk::result_ptr& result);
signals:
void resultAdded();
private slots:
void updateTimer();
void onExpired();
private:
ResultExpirationTimer( QObject *parent = 0 );
static ResultExpirationTimer* s_instance;
QMap< qint64, QList<Tomahawk::result_ptr > > m_results;
QMap< QString, query_ptr > m_queries;
qint64 expires( qint64 expires ) const;
qint64 m_currentTimeout;
};
}
#endif

View File

@@ -0,0 +1,167 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2012, Leo Franchi <lfranchi@kde.org>
* Copyright 2012, Hugo Lindström <hugolm84@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "ResultExpirationTimer.h"
#include "WebResultHintChecker.h"
#include "CustomResultHintChecker.h"
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QUrl>
#include "Query.h"
#include "Result.h"
#include "Source.h"
#include "Pipeline.h"
#include "utils/NetworkReply.h"
#include "utils/Logger.h"
using namespace Tomahawk;
/**
* @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 );
}
ResultHintChecker::ResultHintChecker( const query_ptr& q, qint64 expires )
: QObject( 0 )
, m_query( q )
, m_isValid( false )
, m_expires( expires )
{
Q_ASSERT( !m_query.isNull() );
m_url = q->resultHint();
check( m_url );
}
ResultHintChecker::~ResultHintChecker()
{
}
void
ResultHintChecker::checkQueries( const QList< query_ptr >& queries )
{
foreach ( const query_ptr& query, queries )
checkQuery( query );
}
void
ResultHintChecker::setExpires( qint64 expires )
{
m_expires = expires;
}
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::setResultUrl( const QString &url )
{
result_ptr result = getResultPtr();
if ( !result.isNull() )
{
result->setUrl( url );
if( m_expires <= 0 )
return;
result->setExpires( m_expires );
ResultExpirationTimer::instance()->addResult( m_query, result );
return;
}
qDebug() << Q_FUNC_INFO << "Error updating result" << url << m_expires;
}
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 );
}

View File

@@ -0,0 +1,60 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2012, Leo Franchi <lfranchi@kde.org>
* Copyright 2012, Hugo Lindström <hugolm84@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef RESULT_HINT_CHECKER_H
#define RESULT_HINT_CHECKER_H
#include "Typedefs.h"
#include <QObject>
namespace Tomahawk
{
class ResultHintChecker : public QObject
{
Q_OBJECT
public:
explicit ResultHintChecker( const query_ptr& q, qint64 expires = 0 );
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 setResultUrl( const QString &url );
void setUrl( const QString& url ) { m_url = url; }
void setExpires( qint64 expires );
void removeHint();
private:
void check( const QUrl& url );
result_ptr getResultPtr();
query_ptr m_query;
QString m_url;
qint64 m_expires;
bool m_isValid;
};
}
#endif

View File

@@ -1010,6 +1010,13 @@ whitelistedHttpResultHint( const QString& url )
}
bool
whitelistedCustomProtocolResultHint( const QString& url )
{
return url.startsWith( "hnhh" ) | url.startsWith( "file:" );
}
void
urlAddQueryItem( QUrl& url, const QString& key, const QString& value )
{

View File

@@ -204,6 +204,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.

View File

@@ -1,7 +1,7 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2012, Leo Franchi <lfranchi@kde.org>
*
* Copyright 2012, Hugo Lindström <hugolm84@gmail.com>
* 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 <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QUrl>
#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()
{

View File

@@ -1,6 +1,7 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2012, Leo Franchi <lfranchi@kde.org>
* Copyright 2012, Hugo Lindström <hugolm84@gmail.com>
*
* 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,33 +20,22 @@
#define WEB_RESULT_HINT_CHECKER_H
#include "Typedefs.h"
#include <QObject>
#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;
};
}