File indexing completed on 2024-05-19 04:50:15
0001 /**************************************************************************************** 0002 * Copyright (c) 2008 Shane King <kde@dontletsstart.com> * 0003 * Copyright (c) 2008 Nikolaj Hald Nielsen <nhn@kde.org> * 0004 * Copyright (c) 2008 Leo Franchi <lfranchi@gmail.com> * 0005 * * 0006 * This program is free software; you can redistribute it and/or modify it under * 0007 * the terms of the GNU General Public License as published by the Free Software * 0008 * Foundation; either version 2 of the License, or (at your option) any later * 0009 * version. * 0010 * * 0011 * This program is distributed in the hope that it will be useful, but WITHOUT ANY * 0012 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * 0013 * PARTICULAR PURPOSE. See the GNU General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU General Public License along with * 0016 * this program. If not, see <http://www.gnu.org/licenses/>. * 0017 ****************************************************************************************/ 0018 0019 #define DEBUG_PREFIX "lastfm" 0020 0021 #include "LastFmServiceCollection.h" 0022 #include "meta/LastFmMeta.h" 0023 0024 #include <XmlQuery.h> 0025 0026 using namespace Collections; 0027 0028 LastFmServiceCollection::LastFmServiceCollection( const QString &userName ) 0029 : ServiceCollection( nullptr, "last.fm", "last.fm" ) 0030 { 0031 DEBUG_BLOCK 0032 Meta::ServiceGenre * userStreams = new Meta::ServiceGenre( i18n( "%1's Streams", userName ) ); 0033 Meta::GenrePtr userStreamsPtr( userStreams ); 0034 addGenre( userStreamsPtr ); 0035 0036 Meta::ServiceGenre * globalTags = new Meta::ServiceGenre( i18n( "Global Tags" ) ); 0037 Meta::GenrePtr globalTagsPtr( globalTags ); 0038 addGenre( globalTagsPtr ); 0039 0040 m_friendsLoved = new Meta::ServiceGenre( i18n( "Friends' Loved Radio" ) ); 0041 Meta::GenrePtr friendsLovedPtr( m_friendsLoved ); 0042 addGenre( friendsLovedPtr ); 0043 0044 m_friendsPersonal = new Meta::ServiceGenre( i18n( "Friends' Personal Radio" ) ); 0045 Meta::GenrePtr friendsPersonalPtr( m_friendsPersonal ); 0046 addGenre( friendsPersonalPtr ); 0047 0048 // Only show these if the user is a subscriber. 0049 QStringList lastfmPersonal; 0050 lastfmPersonal << "personal" << "recommended" << "loved"; 0051 0052 foreach( const QString &station, lastfmPersonal ) 0053 { 0054 LastFm::Track * track = new LastFm::Track( "lastfm://user/" + userName + "/" + station ); 0055 Meta::TrackPtr trackPtr( track ); 0056 userStreams->addTrack( trackPtr ); 0057 addTrack( trackPtr ); 0058 } 0059 0060 QStringList lastfmGenres; 0061 lastfmGenres << "Alternative" << "Ambient" << "Chill Out" << "Classical"<< "Dance" 0062 << "Electronica" << "Favorites" << "Gospel" << "Heavy Metal" << "Hip Hop" 0063 << "Indie Rock" << "Industrial" << "Japanese" << "Pop" << "Psytrance" 0064 << "Rap" << "Rock" << "Soundtrack" << "Techno" << "Trance"; 0065 0066 0067 foreach( const QString &genre, lastfmGenres ) 0068 { 0069 LastFm::Track * track = new LastFm::Track( "lastfm://globaltags/" + genre ); 0070 Meta::TrackPtr trackPtr( track ); 0071 globalTags->addTrack( trackPtr ); 0072 addTrack( trackPtr ); 0073 } 0074 0075 QMap< QString, QString > params; 0076 params[ "method" ] = "user.getFriends"; 0077 params[ "user" ] = userName; 0078 m_jobs[ "user.getFriends" ] = lastfm::ws::post( params ); 0079 0080 connect( m_jobs[ "user.getFriends" ], &QNetworkReply::finished, this, &LastFmServiceCollection::slotAddFriendsLoved ); 0081 //connect( m_jobs[ "user.getFriends" ], &QNetworkReply::finished, this, &LastFmServiceCollection::slotAddFriendsPersonal ); 0082 0083 //TODO Automatically add similar artist streams for the users favorite artists. 0084 } 0085 0086 LastFmServiceCollection::~LastFmServiceCollection() 0087 { 0088 DEBUG_BLOCK 0089 } 0090 0091 bool 0092 LastFmServiceCollection::possiblyContainsTrack( const QUrl &url ) const 0093 { 0094 return url.scheme() == "lastfm"; 0095 } 0096 0097 0098 Meta::TrackPtr 0099 LastFmServiceCollection::trackForUrl( const QUrl &url ) 0100 { 0101 return Meta::TrackPtr( new LastFm::Track( url.url() ) ); 0102 } 0103 0104 0105 QString 0106 LastFmServiceCollection::collectionId() const 0107 { 0108 return QLatin1String( "last.fm" ); 0109 } 0110 0111 0112 QString 0113 LastFmServiceCollection::prettyName() const 0114 { 0115 return i18n( "Last.fm" ); 0116 } 0117 0118 void LastFmServiceCollection::slotAddFriendsLoved() 0119 { 0120 DEBUG_BLOCK 0121 if( !m_jobs[ "user.getFriends" ] ) 0122 { 0123 debug() << "BAD! got no result object"; 0124 return; 0125 } 0126 switch (m_jobs[ "user.getFriends" ]->error()) 0127 { 0128 case QNetworkReply::NoError: 0129 { 0130 lastfm::XmlQuery lfm; 0131 if( lfm.parse( m_jobs[ "user.getFriends" ]->readAll() ) ) 0132 { 0133 foreach( const lastfm::XmlQuery &e, lfm[ "friends" ].children( "user" ) ) 0134 { 0135 const QString name = e[ "name" ].text(); 0136 LastFm::Track *track = new LastFm::Track( "lastfm://user/" + name + "/loved" ); 0137 Meta::TrackPtr trackPtr( track ); 0138 m_friendsLoved->addTrack( trackPtr ); 0139 addTrack( trackPtr ); 0140 } 0141 0142 } 0143 else 0144 { 0145 debug() << "Got exception in parsing from last.fm:" << lfm.parseError().message(); 0146 } 0147 break; 0148 } 0149 0150 case QNetworkReply::AuthenticationRequiredError: 0151 debug() << "Last.fm: errorMessage: Sorry, we don't recognise that username, or you typed the password incorrectly."; 0152 break; 0153 0154 default: 0155 debug() << "Last.fm: errorMessage: There was a problem communicating with the Last.fm services. Please try again later."; 0156 break; 0157 } 0158 0159 m_jobs[ "user.getFriends" ]->deleteLater(); 0160 } 0161 0162 void LastFmServiceCollection::slotAddFriendsPersonal() 0163 { 0164 DEBUG_BLOCK 0165 if( !m_jobs[ "user.getFriends" ] ) 0166 { 0167 debug() << "BAD! got no result object"; 0168 return; 0169 } 0170 0171 switch (m_jobs[ "user.getFriends" ]->error()) 0172 { 0173 case QNetworkReply::NoError: 0174 { 0175 lastfm::XmlQuery lfm; 0176 if( lfm.parse( m_jobs[ "user.getFriends" ]->readAll() ) ) 0177 { 0178 foreach( const lastfm::XmlQuery &e, lfm[ "friends" ].children( "user" ) ) 0179 { 0180 const QString name = e[ "name" ].text(); 0181 LastFm::Track *track = new LastFm::Track( "lastfm://user/" + name + "/personal" ); 0182 Meta::TrackPtr trackPtr( track ); 0183 m_friendsPersonal->addTrack( trackPtr ); 0184 addTrack( trackPtr ); 0185 } 0186 0187 } 0188 else 0189 { 0190 debug() << "Got exception in parsing from last.fm:" << lfm.parseError().message(); 0191 } 0192 break; 0193 } 0194 0195 case QNetworkReply::AuthenticationRequiredError: 0196 debug() << "Last.fm: errorMessage: Sorry, we don't recognise that username, or you typed the password incorrectly."; 0197 break; 0198 0199 default: 0200 debug() << "Last.fm: errorMessage: There was a problem communicating with the Last.fm services. Please try again later."; 0201 break; 0202 } 0203 0204 m_jobs[ "user.getFriends" ]->deleteLater(); 0205 } 0206 0207 QueryMaker* 0208 LastFmServiceCollection::queryMaker() 0209 { 0210 // TODO 0211 //return new LastFmServiceQueryMaker( this ); 0212 return ServiceCollection::queryMaker(); 0213 } 0214 0215