PageRenderTime 366ms CodeModel.GetById 50ms app.highlight 102ms RepoModel.GetById 210ms app.codeStats 1ms

/thirdparty/liblastfm2/src/radio/RadioStation.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 337 lines | 233 code | 83 blank | 21 comment | 41 complexity | 6493cdfe261ed09a044a186bdf72bcda MD5 | raw file
  1/*
  2   Copyright 2009 Last.fm Ltd. 
  3
  4   This file is part of liblastfm.
  5
  6   liblastfm 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   liblastfm 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 liblastfm.  If not, see <http://www.gnu.org/licenses/>.
 18*/
 19
 20#include <QRegExp>
 21#include <QStringList>
 22
 23#include "RadioStation.h"
 24#include "../core/XmlQuery.h"
 25
 26
 27const float k_defaultRep(0.5);
 28const float k_defaultMainstr(0.5);
 29const bool k_defaultDisco(false);
 30
 31lastfm::RadioStation
 32lastfm::RadioStation::library( const lastfm::User& user )
 33{
 34    QList<lastfm::User> users;
 35    users << user;
 36    return library( users );
 37}
 38
 39lastfm::RadioStation
 40lastfm::RadioStation::library( QList<lastfm::User>& users )
 41{
 42    RadioStation s( libraryStr( users ) );
 43    if( users.count() == 1 ) 
 44        s.setTitle( QObject::tr( "%1%2s Library Radio").arg( lastfm::ws::Username, QChar(0x2019) ));
 45
 46    else {
 47        QString title;
 48        for( QList<lastfm::User>::const_iterator i = users.begin(); i != users.end(); i++ ) {
 49            if( i == users.end() - 1 )
 50                title += " and " + *i;
 51            else
 52                title += ", " + *i;
 53        }
 54
 55        s.setTitle( title );
 56    }
 57
 58    return s;
 59}
 60
 61
 62lastfm::RadioStation
 63lastfm::RadioStation::recommendations( const lastfm::User& user )
 64{
 65    RadioStation s( recommendationsStr( user ) );
 66    
 67    s.setTitle( QObject::tr( "%1%2s Recommended Radio").arg( lastfm::ws::Username, QChar(0x2019) ));
 68
 69    return s;
 70}
 71
 72lastfm::RadioStation
 73lastfm::RadioStation::friends( const lastfm::User& user )
 74{
 75    RadioStation s( friendsStr( user ) );
 76
 77    s.setTitle( QObject::tr( "%1%2s Friends Radio").arg( lastfm::ws::Username, QChar(0x2019) ));
 78
 79    return s; 
 80}
 81
 82lastfm::RadioStation
 83lastfm::RadioStation::neighbourhood( const lastfm::User& user )
 84{
 85    RadioStation s( neighbourhoodStr( user ) );
 86    s.setTitle( QObject::tr( "%1%2s Neighbours%2 Radio").arg( lastfm::ws::Username, QChar(0x2019) ));
 87    return s;
 88}
 89
 90
 91lastfm::RadioStation
 92lastfm::RadioStation::tag( const lastfm::Tag& tag )
 93{
 94    QList<lastfm::Tag> tags;
 95    tags << tag;
 96    return lastfm::RadioStation::tag( tags );
 97}
 98
 99
100lastfm::RadioStation
101lastfm::RadioStation::tag( QList<lastfm::Tag>& tag )
102{
103    return RadioStation( tagStr( tag ) );
104}
105
106
107lastfm::RadioStation
108lastfm::RadioStation::similar( const lastfm::Artist& artist )
109{
110    QList<lastfm::Artist> artists;
111    artists << artist;
112    return similar( artists );
113}
114
115
116lastfm::RadioStation
117lastfm::RadioStation::similar( QList<lastfm::Artist>& artists )
118{
119    return RadioStation( similarStr( artists ) );
120}
121
122
123lastfm::RadioStation
124lastfm::RadioStation::mix( const lastfm::User& user )
125{
126    RadioStation s( mixStr( user ) );
127    s.setTitle( QObject::tr( "%1%2s Mix Radio").arg( lastfm::ws::Username, QChar(0x2019) ) );
128    return s;
129}
130
131
132QString
133lastfm::RadioStation::url() const
134{
135    return m_url.toString() + (m_tagFilter.isEmpty() ? "" : "/tag/" + m_tagFilter);
136}
137
138
139void
140lastfm::RadioStation::setTitle( const QString& s )
141{
142    // Stop the radio station getting renamed when the web services don't know what it's called
143    if ( !m_title.isEmpty() && s.compare( "a radio station", Qt::CaseInsensitive ) == 0 )
144        return;
145
146    QString title = s.trimmed();
147
148    if ( title.compare( QObject::tr("%1%2s Library Radio").arg( lastfm::ws::Username, QChar(0x2019) ), Qt::CaseInsensitive ) == 0 )
149        title = QObject::tr("My Library Radio");
150    else if ( title.compare( QObject::tr("%1%2s Mix Radio").arg( lastfm::ws::Username, QChar(0x2019) ), Qt::CaseInsensitive ) == 0  )
151        title = QObject::tr("My Mix Radio");
152    else if ( title.compare( QObject::tr("%1%2s Recommended Radio").arg( lastfm::ws::Username, QChar(0x2019) ), Qt::CaseInsensitive ) == 0  )
153        title = QObject::tr("My Recommended Radio");
154    else if ( title.compare( QObject::tr("%1%2s Friends%2 Radio").arg( lastfm::ws::Username, QChar(0x2019) ), Qt::CaseInsensitive ) == 0  )
155        title = QObject::tr("My Friends%1 Radio").arg( QChar( 0x2019 ) );
156    else if ( title.compare( QObject::tr("%1%2s Friends Radio").arg( lastfm::ws::Username, QChar(0x2019) ), Qt::CaseInsensitive ) == 0  )
157        title = QObject::tr("My Friends%1 Radio").arg( QChar( 0x2019 ) );
158    else if ( title.compare( QObject::tr("%1%2s Neighbours%2 Radio").arg( lastfm::ws::Username, QChar(0x2019) ), Qt::CaseInsensitive ) == 0  )
159        title = QObject::tr("My Neighbours%1 Radio").arg( QChar( 0x2019 ) );
160    else if ( title.compare( QObject::tr("%1%2s Neighbours Radio").arg( lastfm::ws::Username ), Qt::CaseInsensitive ) == 0  )
161        title = QObject::tr("My Neighbours%1 Radio").arg( QChar( 0x2019 ) );
162
163    m_title = title;
164}
165
166
167QString
168lastfm::RadioStation::title() const
169{
170    return m_title; // + (m_tagFilter.isEmpty() ? "" : ": " + m_tagFilter);
171}
172
173
174void
175lastfm::RadioStation::setTagFilter( const QString& tag )
176{
177    m_tagFilter = tag;
178}
179
180
181QNetworkReply*
182lastfm::RadioStation::getSampleArtists( int limit ) const
183{
184    QMap<QString, QString> map;
185    map["method"] = "radio.getSampleArtists";
186    map["station"] = m_url.toString();
187    map["limit"] = QString::number( limit );
188    return ws::get( map );
189}
190
191
192QNetworkReply*
193lastfm::RadioStation::getTagSuggestions( int limit ) const
194{
195    QMap<QString, QString> map;
196    map["method"] = "radio.getTagSuggestions";
197    map["station"] = m_url.toString();
198    map["limit"] = QString::number( limit );
199    return ws::get( map );
200}
201
202
203//static 
204QList<lastfm::RadioStation> 
205lastfm::RadioStation::list( QNetworkReply* r )
206{
207    QList<lastfm::RadioStation> result;
208    try {
209        foreach (XmlQuery xq, XmlQuery( r->readAll() ).children("station")) {
210            lastfm::RadioStation rs( QUrl::fromPercentEncoding( xq["url"].text().toUtf8() ) );
211            rs.setTitle(xq["name"].text());
212            result.append(rs);
213        }
214    }
215    catch (ws::ParseError& e)
216    {
217        qWarning() << e.what();
218    }
219    return result;
220}
221
222
223bool
224lastfm::RadioStation::operator==( const RadioStation& that ) const
225{
226    return this->m_url == that.m_url && this->m_tagFilter == that.m_tagFilter;
227}
228
229
230void
231lastfm::RadioStation::setString( const QString& string )
232{
233    // If it's a tag filtered station then extract that part
234    QString tempString = string;
235
236    if ( !tempString.startsWith("lastfm://tag/") )
237    {
238        int index = tempString.indexOf("/tag/");
239
240        if ( index != -1 )
241        {
242            m_tagFilter = tempString.mid( index + 5, tempString.count() - (index + 5) );
243            tempString = tempString.mid( 0, index );
244        }
245    }
246
247    m_url = tempString;
248}
249
250
251void
252lastfm::RadioStation::setRep(float rep)
253{
254    m_rep = rep;
255}
256
257
258void
259lastfm::RadioStation::setMainstr(float mainstr)
260{
261    m_mainstr = mainstr;
262}
263
264
265void
266lastfm::RadioStation::setDisco(bool disco)
267{
268    m_disco = disco;
269}
270
271
272float lastfm::RadioStation::rep() const
273{
274    return m_rep;
275}
276
277
278float lastfm::RadioStation::mainstr() const
279{
280    return m_mainstr;
281}
282
283
284bool lastfm::RadioStation::disco() const
285{
286    return m_disco;
287}
288
289
290QString lastfm::RadioStation::libraryStr( QList<lastfm::User>& users )
291{
292    qSort(users.begin(), users.end());
293
294    QString url = (users.count() > 1) ? "lastfm://users/" : "lastfm://user/";
295
296    url.append( users[0].name() );
297
298    for ( int i = 1 ; i < users.count() ; ++i )
299        url.append( "," + users[i].name() );
300
301    url.append("/personal");
302
303    return url;
304}
305
306
307QString lastfm::RadioStation::tagStr( QList<lastfm::Tag>& tags )
308{
309    qSort(tags.begin(), tags.end());
310
311    QString url = (tags.count() > 1) ? "lastfm://tag/" : "lastfm://globaltags/";
312
313    url.append( tags[0].name() );
314
315    for ( int i = 1 ; i < tags.count() ; ++i )
316        url.append( "*" + tags[i].name() );
317
318    return url;
319}
320
321
322QString lastfm::RadioStation::similarStr( QList<lastfm::Artist>& artists )
323{
324    qSort(artists.begin(), artists.end());
325
326    QString url = (artists.count() > 1) ? "lastfm://artistnames/" : "lastfm://artist/";
327
328    url.append( artists[0].name() );
329
330    for ( int i = 1 ; i < artists.count() ; ++i )
331        url.append( "," + artists[i].name() );
332
333    if (artists.count() == 1)
334        url.append( "/similarartists" );
335
336    return url;
337}