PageRenderTime 188ms CodeModel.GetById 80ms app.highlight 50ms RepoModel.GetById 51ms app.codeStats 0ms

/thirdparty/liblastfm2/src/core/UrlBuilder.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 83 lines | 60 code | 4 blank | 19 comment | 0 complexity | 776e5de4ae518ed5219527a2b1665d9a MD5 | raw file
 1/*
 2   Copyright 2009 Last.fm Ltd. 
 3      - Primarily authored by Max Howell, Jono Cole and Doug Mansell
 4
 5   This file is part of liblastfm.
 6
 7   liblastfm is free software: you can redistribute it and/or modify
 8   it under the terms of the GNU General Public License as published by
 9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   liblastfm is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with liblastfm.  If not, see <http://www.gnu.org/licenses/>.
19*/
20#include "UrlBuilder.h"
21#include <QRegExp>
22#include <QStringList>
23
24
25QUrl
26lastfm::UrlBuilder::url() const
27{
28    QUrl url;
29    url.setScheme( "http" );
30    url.setHost( host() );
31    url.setEncodedPath( path );
32    return url;
33}
34
35
36QByteArray //static
37lastfm::UrlBuilder::encode( QString s )
38{
39    foreach (QChar c, QList<QChar>() << '%' << '&' << '/' << ';' << '+' << '#' << '"')
40        if (s.contains( c ))
41            // the middle step may seem odd but this is what the site does
42            // eg. search for the exact string "Radiohead 2 + 2 = 5"
43            return QUrl::toPercentEncoding( s ).replace( "%20", "+" ).toPercentEncoding( "", "+" );;
44
45    return QUrl::toPercentEncoding( s.replace( ' ', '+' ), "+" );
46}
47
48
49QString //static
50lastfm::UrlBuilder::host( const QLocale& locale )
51{
52    switch (locale.language())
53    {
54        case QLocale::Portuguese: return "www.lastfm.com.br";
55        case QLocale::Turkish:    return "www.lastfm.com.tr";                    
56        case QLocale::French:     return "www.lastfm.fr";
57        case QLocale::Italian:    return "www.lastfm.it";
58        case QLocale::German:     return "www.lastfm.de";
59        case QLocale::Spanish:    return "www.lastfm.es";
60        case QLocale::Polish:     return "www.lastfm.pl";
61        case QLocale::Russian:    return "www.lastfm.ru";
62        case QLocale::Japanese:   return "www.lastfm.jp";
63        case QLocale::Swedish:    return "www.lastfm.se";
64        case QLocale::Chinese:    return "cn.last.fm";
65        default:                  return "www.last.fm";
66    }
67}
68
69
70QUrl //static
71lastfm::UrlBuilder::localize( QUrl url)
72{
73    url.setHost( url.host().replace( QRegExp("^(www.)?last.fm"), host() ) );
74    return url;
75}
76
77
78QUrl //static
79lastfm::UrlBuilder::mobilize( QUrl url )
80{
81    url.setHost( url.host().replace( QRegExp("^(www.)?last"), "m.last" ) );
82    return url;
83}