PageRenderTime 356ms CodeModel.GetById 141ms app.highlight 70ms RepoModel.GetById 142ms app.codeStats 0ms

/thirdparty/liblastfm2/src/types/User.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 317 lines | 235 code | 54 blank | 28 comment | 9 complexity | 1cca4e4f5d0470d3b396cddd8990df01 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 "User.h"
 21#include "Track.h"
 22#include "../core/UrlBuilder.h"
 23#include "../core/XmlQuery.h"
 24#include <QStringList>
 25#include <lastfm/UserList>
 26#include <QAbstractNetworkCache>
 27
 28using lastfm::User;
 29using lastfm::UserList;
 30using lastfm::UserDetails;
 31using lastfm::XmlQuery;
 32using lastfm::ImageSize;
 33
 34User::User( const XmlQuery& xml ) 
 35     :AbstractType(), m_match( -1.0f )
 36{
 37    m_name = xml["name"].text();
 38    m_images << xml["image size=small"].text()
 39             << xml["image size=medium"].text()
 40             << xml["image size=large"].text();
 41    m_realName = xml["realname"].text();
 42}
 43
 44
 45QUrl 
 46User::imageUrl( ImageSize size, bool square ) const
 47{
 48    if( !square ) return m_images.value( size ); 
 49
 50    QUrl url = m_images.value( size );
 51    QRegExp re( "/serve/(\\d*)s?/" );
 52    return QUrl( url.toString().replace( re, "/serve/\\1s/" ));
 53}
 54
 55
 56QMap<QString, QString>
 57User::params(const QString& method) const
 58{
 59    QMap<QString, QString> map;
 60    map["method"] = "user."+method;
 61    map["user"] = m_name;
 62    return map;
 63}
 64
 65
 66QNetworkReply*
 67User::getFriends( bool recentTracks, int limit, int page ) const
 68{
 69    QMap<QString, QString> map = params( "getFriends" );
 70    map["limit"] = QString::number( limit );
 71    map["page"] = QString::number( page );
 72    if ( recentTracks ) map["recenttracks"] = "1";
 73    return ws::get( map );
 74}
 75
 76
 77QNetworkReply*
 78User::getFriendsListeningNow( int limit, int page ) const
 79{
 80    QMap<QString, QString> map = params( "getFriendsListeningNow" );
 81    map["limit"] = QString::number( limit );
 82    map["page"] = QString::number( page );
 83    return ws::get( map );
 84}
 85
 86
 87QNetworkReply*
 88User::getTopTags() const
 89{
 90    return ws::get( params( "getTopTags" ) );
 91}
 92
 93
 94QNetworkReply*
 95User::getTopArtists( QString period, int limit, int page ) const
 96{
 97    QMap<QString, QString> map = params( "getTopArtists" );
 98    map["period"] = period;
 99    map["limit"] = QString::number( limit );
100    map["page"] = QString::number( page );
101    return ws::get( map );
102}
103
104
105QNetworkReply*
106User::getRecentArtists() const
107{
108    return ws::get( params( "getRecentArtists" ) );
109}
110
111
112QNetworkReply*
113User::getRecentTracks( int limit , int page ) const
114{
115    QMap<QString, QString> map = params( "getRecentTracks" );
116    map["limit"] = QString::number( limit );
117    map["page"] = QString::number( page );
118
119    QAbstractNetworkCache* cache = lastfm::nam()->cache();
120    if ( cache )
121        cache->remove( lastfm::ws::url( map ) );
122
123    return ws::get( map );
124}
125
126QNetworkReply*
127User::getRecentStations( int limit, int page ) const
128{
129    QMap<QString, QString> map = params( "getRecentStations" );
130    map["limit"] = QString::number( limit );
131    map["page"] = QString::number( page );
132    return ws::get( map );
133}
134
135
136QNetworkReply*
137User::getRecommendedArtists( int limit, int page ) const
138{
139    QMap<QString, QString> map = params( "getRecommendedArtists" );
140    map["limit"] = QString::number( limit );
141    map["page"] = QString::number( page );
142    return ws::get( map );
143}
144
145
146QNetworkReply*
147User::getNeighbours( int limit, int page ) const
148{
149    QMap<QString, QString> map = params( "getNeighbours" );
150    map["limit"] = QString::number( limit );
151    map["page"] = QString::number( page );
152    return ws::get( map );
153}
154
155
156QNetworkReply*
157User::getPlaylists() const
158{
159    return ws::get( params( "getPlaylists" ) );
160}
161
162
163UserList //static
164User::list( QNetworkReply* r )
165{
166    UserList users;
167    try {
168        XmlQuery lfm = r->readAll();
169        foreach (XmlQuery e, lfm.children( "user" ))
170        {
171            User u( e );
172            users += u;
173        }
174
175        users.total = lfm["friends"].attribute("total").toInt();
176        users.page = lfm["friends"].attribute("page").toInt();
177        users.perPage = lfm["friends"].attribute("perPage").toInt();
178        users.totalPages = lfm["friends"].attribute("totalPages").toInt();
179    }
180    catch (ws::ParseError& e)
181    {
182        qWarning() << e.what();
183    }    
184    return users;
185}
186
187
188QNetworkReply* //static
189UserDetails::getInfo( const QString& username )
190{
191    QMap<QString, QString> map;
192    map["method"] = "user.getInfo";
193    map["user"] = username;
194    return ws::post( map );
195}
196
197
198
199
200/*
201QNetworkReply* //static
202UserDetails::getRecommendedArtists()
203{
204    QMap<QString, QString> map;
205    map["method"] = "user.getRecommendedArtists";
206    return ws::post( map );
207}
208*/
209
210QUrl
211User::www() const
212{ 
213    return UrlBuilder( "user" ).slash( m_name ).url();
214}
215
216UserDetails::UserDetails()
217    : User()
218    , m_age( 0 )
219    , m_scrobbles( 0 )
220    , m_registered( QDateTime() )
221    , m_isSubscriber( false )
222    , m_canBootstrap( false )
223{}
224
225UserDetails::UserDetails( QNetworkReply* reply )
226{
227    try
228    {
229        XmlQuery user = XmlQuery( reply->readAll() )["user"];
230        m_age = user["age"].text().toUInt();
231        m_scrobbles = user["playcount"].text().toUInt();
232        m_registered = QDateTime::fromTime_t(user["registered"].attribute("unixtime").toUInt());
233        m_country = user["country"].text();
234        m_isSubscriber = ( user["subscriber"].text() == "1" );
235        m_canBootstrap = ( user["bootstrap"].text() == "1" );
236        m_gender = user["gender"].text();
237        m_realName = user["realname"].text();
238        m_name = user["name"].text();
239        m_images << user["image size=small"].text()
240                 << user["image size=medium"].text()
241                 << user["image size=large"].text();
242    }
243    catch (ws::ParseError& e)
244    {
245        qWarning() << e.what();
246    }
247}
248
249
250QString
251UserDetails::getInfoString() const
252{
253    QString text;
254
255    text = QObject::tr("%1").arg( m_realName.isEmpty() ? m_name : m_realName );
256    if ( m_age ) text.append( QObject::tr(", %1").arg( m_age ) );
257    if ( m_gender.known() ) text.append( QObject::tr(", %1").arg( m_gender.toString() ) );
258    if ( !m_country.isEmpty() ) text.append( QObject::tr(", %1").arg( m_country ) );
259    if ( m_scrobbles ) text.append( QObject::tr(", %L1 scrobbles").arg( m_scrobbles ) );
260
261    return text;
262}
263
264void 
265UserDetails::setScrobbleCount( quint32 scrobbleCount )
266{
267    m_scrobbles = scrobbleCount;
268}
269
270
271void
272UserDetails::setDateRegistered( const QDateTime& date )
273{
274    m_registered = date;
275}
276
277void 
278UserDetails::setImages( const QList<QUrl>& images )
279{
280    m_images = images;
281}
282
283void 
284UserDetails::setRealName( const QString& realName )
285{
286    m_realName = realName;
287}
288void 
289UserDetails::setAge( unsigned short age )
290{
291    m_age = age;
292}
293
294void 
295UserDetails::setIsSubscriber( bool subscriber )
296{
297    m_isSubscriber = subscriber;
298}
299
300void 
301UserDetails::setCanBootstrap( bool canBootstrap )
302{
303    m_canBootstrap = canBootstrap;
304}
305
306void 
307UserDetails::setGender( const QString& s )
308{
309    m_gender = Gender( s );
310}
311
312void 
313UserDetails::setCountry( const QString& country )
314{
315    m_country = country;
316}
317