diff --git a/src/libtomahawk/CMakeLists.txt b/src/libtomahawk/CMakeLists.txt index a808526bd..d0ff77f72 100644 --- a/src/libtomahawk/CMakeLists.txt +++ b/src/libtomahawk/CMakeLists.txt @@ -168,6 +168,7 @@ set( libSources utils/xspfgenerator.cpp utils/jspfloader.cpp utils/spotifyparser.cpp + utils/shortenedlinkparser.cpp widgets/newplaylistwidget.cpp widgets/searchwidget.cpp @@ -340,6 +341,7 @@ set( libHeaders utils/xspfgenerator.h utils/jspfloader.h utils/spotifyparser.h + utils/shortenedlinkparser.h widgets/newplaylistwidget.h widgets/searchwidget.h diff --git a/src/libtomahawk/globalactionmanager.cpp b/src/libtomahawk/globalactionmanager.cpp index 5d4795084..89f354d7b 100644 --- a/src/libtomahawk/globalactionmanager.cpp +++ b/src/libtomahawk/globalactionmanager.cpp @@ -45,6 +45,7 @@ #include "utils/jspfloader.h" #include #include "utils/spotifyparser.h" +#include "utils/shortenedlinkparser.h" GlobalActionManager* GlobalActionManager::s_instance = 0; @@ -740,6 +741,13 @@ GlobalActionManager::acceptsMimeData( const QMimeData* data, bool tracksOnly ) ( tracksOnly ? data->data( "text/plain" ).contains( "track" ) : true ) ) return true; + // We whitelist t.co and bit.ly (and j.mp) since they do some link checking. Often playable (e.g. spotify..) links hide behind them, + // so we do an extra level of lookup + if ( ( data->hasFormat( "text/plain" ) && data->data( "text/plain" ).contains( "bit.ly" ) ) || + ( data->hasFormat( "text/plain" ) && data->data( "text/plain" ).contains( "j.mp" ) ) || + ( data->hasFormat( "text/plain" ) && data->data( "text/plain" ).contains( "t.co" ) ) ) + return true; + return false; } @@ -755,7 +763,7 @@ GlobalActionManager::tracksFromMimeData( const QMimeData* data ) { QString plainData = QString::fromUtf8( data->data( "text/plain" ).constData() ); tDebug() << "Got text/plain mime data:" << data->data( "text/plain" ) << "decoded to:" << plainData; - if( plainData.contains( "open.spotify.com/track") || + if ( plainData.contains( "open.spotify.com/track") || plainData.contains( "spotify:track" ) ) { QStringList tracks = plainData.split( "\n" ); @@ -763,10 +771,34 @@ GlobalActionManager::tracksFromMimeData( const QMimeData* data ) tDebug() << "Got a list of spotify urls!" << tracks; SpotifyParser* spot = new SpotifyParser( tracks, this ); connect( spot, SIGNAL( tracks( QList ) ), this, SIGNAL( tracks( QList ) ) ); + } else if ( plainData.contains( "bit.ly" ) || + plainData.contains( "j.mp" ) || + plainData.contains( "t.co" ) ) + { + QStringList tracks = plainData.split( "\n" ); + + tDebug() << "Got a list of shortened urls!" << tracks; + ShortenedLinkParser* parser = new ShortenedLinkParser( tracks, this ); + connect( parser, SIGNAL( urls( QStringList ) ), this, SLOT( expandedUrls( QStringList ) ) ); } } } +void +GlobalActionManager::expandedUrls( QStringList urls ) +{ + QStringList spotifyUrls; + foreach ( const QString& url, urls ) + { + if( url.contains( "open.spotify.com/track") || url.contains( "spotify:track" ) ) + spotifyUrls << url; + } + + SpotifyParser* spot = new SpotifyParser( spotifyUrls, this ); + connect( spot, SIGNAL( tracks( QList ) ), this, SIGNAL( tracks( QList ) ) ); +} + + QList< query_ptr > GlobalActionManager::tracksFromQueryList( const QMimeData* data ) { diff --git a/src/libtomahawk/globalactionmanager.h b/src/libtomahawk/globalactionmanager.h index af5dedd5c..ce18d7297 100644 --- a/src/libtomahawk/globalactionmanager.h +++ b/src/libtomahawk/globalactionmanager.h @@ -76,6 +76,7 @@ private slots: void showPlaylist(); void xspfCreated( const QByteArray& xspf ); + void expandedUrls( QStringList ); private: explicit GlobalActionManager( QObject* parent = 0 ); void doBookmark( const Tomahawk::playlist_ptr& pl, const Tomahawk::query_ptr& q ); diff --git a/src/libtomahawk/utils/shortenedlinkparser.cpp b/src/libtomahawk/utils/shortenedlinkparser.cpp new file mode 100644 index 000000000..6b1cade6c --- /dev/null +++ b/src/libtomahawk/utils/shortenedlinkparser.cpp @@ -0,0 +1,89 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2010-2011, Leo Franchi + * + * 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 "shortenedlinkparser.h" + +#include "utils/logger.h" +#include "utils/tomahawkutils.h" +#include "query.h" + +#include + +#include +#include + +using namespace Tomahawk; + +ShortenedLinkParser::ShortenedLinkParser ( const QStringList& urls, QObject* parent ) + : QObject( parent ) +{ + foreach ( const QString& url, urls ) + lengthenUrl( url ); +} + +ShortenedLinkParser::~ShortenedLinkParser() {} + +void +ShortenedLinkParser::lengthenUrl( const QString& url ) +{ + // Whitelisted links + if ( !( url.contains( "t.co" ) || + url.contains( "bit.ly" ) || + url.contains( "j.mp" ) ) ) + return; + + tDebug() << "Looking up..." << url; + + QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( QUrl( url ) ) ); + connect( reply, SIGNAL( finished() ), this, SLOT( lookupFinished() ) ); + + m_queries.insert( reply ); +} + +void +ShortenedLinkParser::lookupFinished() +{ + QNetworkReply* r = qobject_cast< QNetworkReply* >( sender() ); + Q_ASSERT( r ); + + QVariant redir = r->attribute( QNetworkRequest::RedirectionTargetAttribute ); + if ( redir.isValid() && !redir.toUrl().isEmpty() ) + { + tLog() << "Got a redirected url:" << redir.toUrl().toString(); + m_links << redir.toUrl().toString(); + } + + r->deleteLater(); + + m_queries.remove( r ); + checkFinished(); +} + + +void +ShortenedLinkParser::checkFinished() +{ + if ( m_queries.isEmpty() ) // we're done + { + qDebug() << "DONE and found redirected urls:" << m_links; + emit urls( m_links ); + + deleteLater(); + } + +} diff --git a/src/libtomahawk/utils/shortenedlinkparser.h b/src/libtomahawk/utils/shortenedlinkparser.h new file mode 100644 index 000000000..6a61eec9c --- /dev/null +++ b/src/libtomahawk/utils/shortenedlinkparser.h @@ -0,0 +1,61 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2010-2011, Leo Franchi + * + * 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 SNORTENED_LINK_PARSER_H +#define SNORTENED_LINK_PARSER_H + +#include "dllmacro.h" +#include "typedefs.h" + +#include +#include +#include + +class QNetworkReply; +namespace Tomahawk +{ + + /** + * Small class to parse whitelisted shortened links into the redirected urls + * + * Connect to urls() to get the result + * + */ + class DLLEXPORT ShortenedLinkParser : public QObject + { + Q_OBJECT + public: + explicit ShortenedLinkParser( const QStringList& urls, QObject* parent = 0 ); + virtual ~ShortenedLinkParser(); + + signals: + void urls( const QStringList& urls ); + + private: + void lengthenUrl( const QString& url ); + void checkFinished(); + + QStringList m_links; + QSet< QNetworkReply* > m_queries; + public slots: + void lookupFinished(); + }; + +} + +#endif