1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
3
* Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
4
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
6
* Tomahawk is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* Tomahawk is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
20
#include "ShortenedLinkParser.h"
22
#include <qjson/parser.h>
24
#include <QtNetwork/QNetworkAccessManager>
26
#include "DropJobNotifier.h"
29
#include "jobview/ErrorStatusMessage.h"
30
#include "jobview/JobStatusModel.h"
31
#include "jobview/JobStatusView.h"
32
#include "utils/NetworkReply.h"
33
#include "utils/TomahawkUtilsGui.h"
34
#include "utils/Logger.h"
36
using namespace Tomahawk;
39
ShortenedLinkParser::ShortenedLinkParser ( const QStringList& urls, QObject* parent )
42
foreach ( const QString& url, urls )
43
if ( handlesUrl( url ) )
48
ShortenedLinkParser::~ShortenedLinkParser()
54
ShortenedLinkParser::handlesUrl( const QString& url )
57
return ( url.contains( "t.co" ) ||
58
url.contains( "bit.ly" ) ||
59
url.contains( "j.mp" ) ||
60
url.contains( "spoti.fi" ) ||
61
url.contains( "ow.ly" ) ||
62
url.contains( "fb.me" ) ||
63
url.contains( "itun.es" ) ||
64
url.contains( "tinyurl.com" ) ||
65
url.contains( "tinysong.com" ) ||
66
url.contains( "grooveshark.com/s/~/" ) || // These redirect to the 'real' grooveshark track url
67
url.contains( "grooveshark.com/#/s/~/" ) ||
68
url.contains( "rd.io" ) ||
69
url.contains( "snd.sc" ) );
74
ShortenedLinkParser::lookupUrl( const QString& url )
76
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Looking up..." << url;
77
QString cleaned = url;
78
if ( cleaned.contains( "/#/s/" ) )
79
cleaned.replace( "/#", "" );
81
NetworkReply* reply = new NetworkReply( TomahawkUtils::nam()->get( QNetworkRequest( QUrl( cleaned ) ) ) );
82
connect( reply, SIGNAL( finished() ), SLOT( lookupFinished() ) );
84
m_queries.insert( reply );
86
m_expandJob = new DropJobNotifier( pixmap(), "shortened", DropJob::Track, reply );
87
JobStatusView::instance()->model()->addJob( m_expandJob );
92
ShortenedLinkParser::lookupFinished()
94
NetworkReply* r = qobject_cast< NetworkReply* >( sender() );
97
if ( r->reply()->error() != QNetworkReply::NoError )
98
JobStatusView::instance()->model()->addJob( new ErrorStatusMessage( tr( "Network error parsing shortened link!" ) ) );
100
tLog( LOGVERBOSE ) << Q_FUNC_INFO << "Got an un-shortened url:" << r->reply()->url().toString();
101
m_links << r->reply()->url().toString();
102
m_queries.remove( r );
110
ShortenedLinkParser::checkFinished()
112
if ( m_queries.isEmpty() ) // we're done
114
emit urls( m_links );
121
#ifndef ENABLE_HEADLESS
124
ShortenedLinkParser::pixmap()
126
return TomahawkUtils::defaultPixmap( TomahawkUtils::Add );