Debian Codegraph @

src/context/applets/similarartists/plugin/SimilarArtistsEngine.cpp

raw
1 /***************************************************************************************
2 * Copyright (c) 2009 Nathan Sala <[email protected]> *
3 * Copyright (c) 2009 Oleksandr Khayrullin <[email protected]> *
4 * Copyright (c) 2009-2010 Joffrey Clavel <[email protected]> *
5 * Copyright (c) 2024 Tuomas Nurmi <[email protected]> *
6 * *
7 * This program is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU General Public License as published by the Free Software *
9 * Foundation; either version 2 of the License, or (at your option) any later *
10 * version. *
11 * *
12 * This program is distributed in the hope that it will be useful, but WITHOUT ANY *
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
14 * PARTICULAR PURPOSE. See the GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License along with *
17 * this program. If not, see <http://www.gnu.org/licenses/>. *
18 ****************************************************************************************/
19
20 #define DEBUG_PREFIX "SimilarArtistsEngine"
21
22 #include "SimilarArtistsEngine.h"
23
24 #include "amarokurls/AmarokUrl.h"
25 #include "EngineController.h"
26 #include "collections/QueryMaker.h"
27 #include "core/support/Amarok.h"
28 #include "core/support/Debug.h"
29 #include "core-impl/collections/support/CollectionManager.h"
30 #include "services/lastfm/LastFmServiceConfig.h"
31
32 #include <QUrlQuery>
33 #include <QXmlStreamReader>
34
35
36 SimilarArtistsEngine::SimilarArtistsEngine( QObject *parent )
37 : QObject( parent )
38 , m_maxArtists( 5 )
39 , m_model( new SimilarArtistModel( this ) )
40 , m_artistInfoQueryInProcess( false )
41 {
42 EngineController *engine = The::engineController();
43 connect( engine, &EngineController::trackChanged, this, [this](auto) {this->SimilarArtistsEngine::update();} );
44 connect( engine, &EngineController::trackMetadataChanged, this, [this](auto){this->SimilarArtistsEngine::update() ;} );
45 }
46
47 SimilarArtistsEngine::~SimilarArtistsEngine()
48 {
49 }
50
51 bool
52 SimilarArtistsEngine::update( bool force )
53 {
54 QString newArtist;
55
56 Meta::TrackPtr track = The::engineController()->currentTrack();
57 if( track )
58 {
59 if( Meta::ArtistPtr artistPtr = track->artist() )
60 newArtist = artistPtr->name();
61 }
62
63 if( newArtist.isEmpty() )
64 {
65 m_model->clearAll();
66 return false;
67 }
68 else //valid artist
69 {
70 // wee make a request only if the artist is different
71 if( force || ( newArtist != m_model->currentTarget() ) )
72 {
73 // if the artist has changed
74 m_model->setCurrentTarget( newArtist );
75 similarArtistsRequest( newArtist );
76 Q_EMIT targetChanged();
77 return true;
78 }
79 }
80 return false;
81 }
82
83 void
84 SimilarArtistsEngine::searchLocalCollection( const QString &artistName )
85 {
86 DEBUG_BLOCK
87
88 // -- search the collection for albums with the artist
89 Collections::QueryMaker *qm = CollectionManager::instance()->queryMaker();
90 qm->setAutoDelete( true );
91 qm->addFilter( Meta::valArtist, artistName, true, true );
92 qm->setAlbumQueryMode( Collections::QueryMaker::AllAlbums );
93 qm->setQueryType( Collections::QueryMaker::Album );
94 qm->limitMaxResultSize( 3 );
95
96 connect( qm, &Collections::QueryMaker::newAlbumsReady,
97 this, &SimilarArtistsEngine::resultReady, Qt::QueuedConnection );
98
99 m_lastQueryMaker = qm;
100 m_queriedArtist = artistName;
101 qm->run();
102 }
103
104 void SimilarArtistsEngine::resultReady( const Meta::AlbumList &albums )
105 {
106 if( sender() != m_lastQueryMaker )
107 return;
108
109 if( albums.length() > 0 )
110 debug() << "found albums in our own collection";
111 for( auto album : albums )
112 {
113 if( album->name().isEmpty() )
114 continue;
115
116 QUrl cover = album->imageLocation( 200 );
117 if( cover != QUrl() )
118 {
119 m_model->setCover( m_queriedArtist, cover );
120 return;
121 }
122 }
123 }
124
125 void
126 SimilarArtistsEngine::navigateToArtist( const QString &artist )
127 {
128 AmarokUrl url;
129 url.setCommand( QStringLiteral("navigate") );
130 url.setPath( QStringLiteral("collections") );
131 url.setArg( QStringLiteral("filter"), QStringLiteral("artist:\"") + AmarokUrl::escape( artist ) + QLatin1Char('\"') );
132 url.run();
133 }
134
135
136 void
137 SimilarArtistsEngine::similarArtistsRequest( const QString &artistName )
138 {
139 // we generate the url for the demand on the lastFM Api
140 QUrl url;
141 url.setScheme( QStringLiteral("https") );
142 url.setHost( QStringLiteral("ws.audioscrobbler.com") );
143 url.setPath( QStringLiteral("/2.0/") );
144
145 QUrlQuery query;
146 query.addQueryItem( QStringLiteral("method"), QStringLiteral("artist.getSimilar") );
147 query.addQueryItem( QStringLiteral("api_key"), QLatin1String(Amarok::lastfmApiKey()) );
148 query.addQueryItem( QStringLiteral("artist"), artistName );
149 query.addQueryItem( QStringLiteral("limit"), QString::number( m_maxArtists ) );
150 url.setQuery( query );
151
152 The::networkAccessManager()->getData( url, this, &SimilarArtistsEngine::parseSimilarArtists );
153 }
154
155 void
156 SimilarArtistsEngine::artistInfoRequest( const QString &artistName )
157 {
158 if(m_artistInfoQueryInProcess)
159 return;
160 m_artistInfoQueryInProcess = true;
161 // we generate the url for the demand on the lastFM Api
162 QUrl url;
163 url.setScheme( QStringLiteral("https") );
164 url.setHost( QStringLiteral("ws.audioscrobbler.com") );
165 url.setPath( QStringLiteral("/2.0/") );
166
167 QUrlQuery query;
168 query.addQueryItem( QStringLiteral("method"), QStringLiteral("artist.getInfo") );
169 query.addQueryItem( QStringLiteral("api_key"), QLatin1String(Amarok::lastfmApiKey()) );
170 query.addQueryItem( QStringLiteral("artist"), artistName );
171 if( !LastFmServiceConfig::instance()->username().isNull() )
172 query.addQueryItem( QStringLiteral("username"), LastFmServiceConfig::instance()->username() );
173 url.setQuery( query );
174
175 The::networkAccessManager()->getData( url, this, &SimilarArtistsEngine::parseArtistInfo );
176 }
177
178 void
179 SimilarArtistsEngine::parseSimilarArtists( const QUrl &url, const QByteArray &data,
180 NetworkAccessManagerProxy::Error e )
181 {
182 if( e.code != QNetworkReply::NoError )
183 {
184 m_model->clearAll();
185 warning() << "Failed to parse similar artists xml:" << url << e.description;
186 return;
187 }
188
189 if( data.isEmpty() )
190 return;
191
192 QXmlStreamReader xml( data );
193 m_model->fillFromXml( xml );
194 }
195
196 void
197 SimilarArtistsEngine::parseArtistInfo( const QUrl &url, const QByteArray &data,
198 NetworkAccessManagerProxy::Error e )
199 {
200 if( e.code != QNetworkReply::NoError )
201 {
202 m_model->clearAll();
203 warning() << "Failed to parse artist info xml:" << url << e.description;
204 m_artistInfoQueryInProcess = false;
205 return;
206 }
207
208 if( data.isEmpty() )
209 {
210 m_artistInfoQueryInProcess = false;
211 return;
212 }
213
214 QXmlStreamReader xml( data );
215 m_model->fillArtistInfoFromXml( xml );
216 m_artistInfoQueryInProcess = false;
217 }
218
219 int
220 SimilarArtistsEngine::maximumArtists() const
221 {
222 return m_maxArtists;
223 }
224
225 void
226 SimilarArtistsEngine::setMaximumArtists( int number )
227 {
228 m_maxArtists = number;
229 Q_EMIT maxArtistsChanged();
230 }
231
232 QString
233 SimilarArtistsEngine::currentTarget() const
234 {
235 return m_model->currentTarget();
236 }