~ubuntu-branches/ubuntu/trusty/tomahawk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/libtomahawk/utils/ShortenedLinkParser.cpp

  • Committer: Package Import Robot
  • Author(s): Harald Sitter
  • Date: 2013-03-07 21:50:13 UTC
  • Revision ID: package-import@ubuntu.com-20130307215013-6gdjkdds7i9uenvs
Tags: upstream-0.6.0+dfsg
ImportĀ upstreamĀ versionĀ 0.6.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
 
2
 *
 
3
 *   Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
 
4
 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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/>.
 
18
 */
 
19
 
 
20
#include "ShortenedLinkParser.h"
 
21
 
 
22
#include <qjson/parser.h>
 
23
 
 
24
#include <QtNetwork/QNetworkAccessManager>
 
25
 
 
26
#include "DropJobNotifier.h"
 
27
#include "Query.h"
 
28
#include "Source.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"
 
35
 
 
36
using namespace Tomahawk;
 
37
 
 
38
 
 
39
ShortenedLinkParser::ShortenedLinkParser ( const QStringList& urls, QObject* parent )
 
40
    : QObject( parent )
 
41
{
 
42
    foreach ( const QString& url, urls )
 
43
        if ( handlesUrl( url ) )
 
44
            lookupUrl( url ) ;
 
45
}
 
46
 
 
47
 
 
48
ShortenedLinkParser::~ShortenedLinkParser()
 
49
{
 
50
}
 
51
 
 
52
 
 
53
bool
 
54
ShortenedLinkParser::handlesUrl( const QString& url )
 
55
{
 
56
    // Whitelisted links
 
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" ) );
 
70
}
 
71
 
 
72
 
 
73
void
 
74
ShortenedLinkParser::lookupUrl( const QString& url )
 
75
{
 
76
    tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Looking up..." << url;
 
77
    QString cleaned = url;
 
78
    if ( cleaned.contains( "/#/s/" ) )
 
79
        cleaned.replace( "/#", "" );
 
80
 
 
81
    NetworkReply* reply = new NetworkReply( TomahawkUtils::nam()->get( QNetworkRequest( QUrl( cleaned ) ) ) );
 
82
    connect( reply, SIGNAL( finished() ), SLOT( lookupFinished() ) );
 
83
 
 
84
    m_queries.insert( reply );
 
85
 
 
86
    m_expandJob = new DropJobNotifier( pixmap(), "shortened", DropJob::Track, reply );
 
87
    JobStatusView::instance()->model()->addJob( m_expandJob );
 
88
}
 
89
 
 
90
 
 
91
void
 
92
ShortenedLinkParser::lookupFinished()
 
93
{
 
94
    NetworkReply* r = qobject_cast< NetworkReply* >( sender() );
 
95
    Q_ASSERT( r );
 
96
 
 
97
    if ( r->reply()->error() != QNetworkReply::NoError )
 
98
        JobStatusView::instance()->model()->addJob( new ErrorStatusMessage( tr( "Network error parsing shortened link!" ) ) );
 
99
 
 
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 );
 
103
    r->deleteLater();
 
104
 
 
105
    checkFinished();
 
106
}
 
107
 
 
108
 
 
109
void
 
110
ShortenedLinkParser::checkFinished()
 
111
{
 
112
    if ( m_queries.isEmpty() ) // we're done
 
113
    {
 
114
        emit urls( m_links );
 
115
 
 
116
        deleteLater();
 
117
    }
 
118
}
 
119
 
 
120
 
 
121
#ifndef ENABLE_HEADLESS
 
122
 
 
123
QPixmap
 
124
ShortenedLinkParser::pixmap()
 
125
{
 
126
    return TomahawkUtils::defaultPixmap( TomahawkUtils::Add );
 
127
}
 
128
 
 
129
#endif