| 1 |
/**************************************************************************************** |
| 2 |
* Copyright (c) 2009 Leo Franchi <[email protected]> * |
| 3 |
* * |
| 4 |
* This program is free software; you can redistribute it and/or modify it under * |
| 5 |
* the terms of the GNU General Public License as published by the Free Software * |
| 6 |
* Foundation; either version 2 of the License, or (at your option) any later * |
| 7 |
* version. * |
| 8 |
* * |
| 9 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY * |
| 10 |
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * |
| 11 |
* PARTICULAR PURPOSE. See the GNU General Public License for more details. * |
| 12 |
* * |
| 13 |
* You should have received a copy of the GNU General Public License along with * |
| 14 |
* this program. If not, see <http://www.gnu.org/licenses/>. * |
| 15 |
****************************************************************************************/ |
| 16 |
|
| 17 |
#define DEBUG_PREFIX "LastFmBias" |
| 18 |
|
| 19 |
#include "LastFmBias.h" |
| 20 |
|
| 21 |
#include "core/meta/Meta.h" |
| 22 |
#include "core/support/Amarok.h" |
| 23 |
#include "core/support/Debug.h" |
| 24 |
#include "core-impl/collections/support/CollectionManager.h" |
| 25 |
|
| 26 |
#include <KLocalizedString> |
| 27 |
#include <QStandardPaths> |
| 28 |
|
| 29 |
#include <QDomDocument> |
| 30 |
#include <QDomNode> |
| 31 |
#include <QFile> |
| 32 |
#include <QLabel> |
| 33 |
#include <QPixmap> |
| 34 |
#include <QRadioButton> |
| 35 |
#include <QTimer> |
| 36 |
#include <QVBoxLayout> |
| 37 |
#include <QXmlStreamReader> |
| 38 |
#include <QXmlStreamWriter> |
| 39 |
|
| 40 |
#include <Artist.h> |
| 41 |
#include <ws.h> |
| 42 |
|
| 43 |
QString |
| 44 |
Dynamic::LastFmBiasFactory::i18nName() const |
| 45 |
{ return i18nc("Name of the \"Last.fm\" similar bias", "Last.fm similar"); } |
| 46 |
|
| 47 |
QString |
| 48 |
Dynamic::LastFmBiasFactory::name() const |
| 49 |
{ return Dynamic::LastFmBias::sName(); } |
| 50 |
|
| 51 |
QString |
| 52 |
Dynamic::LastFmBiasFactory::i18nDescription() const |
| 53 |
{ return i18nc("Description of the \"Last.fm\" bias", |
| 54 |
"The \"Last.fm\" similar bias looks up tracks on Last.fm and only adds similar tracks."); } |
| 55 |
|
| 56 |
Dynamic::BiasPtr |
| 57 |
Dynamic::LastFmBiasFactory::createBias() |
| 58 |
{ return Dynamic::BiasPtr( new Dynamic::LastFmBias() ); } |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
// ----- LastFmBias -------- |
| 63 |
|
| 64 |
Dynamic::LastFmBias::LastFmBias() |
| 65 |
: SimpleMatchBias() |
| 66 |
, m_match( SimilarArtist ) |
| 67 |
{ |
| 68 |
loadDataFromFile(); |
| 69 |
} |
| 70 |
|
| 71 |
Dynamic::LastFmBias::~LastFmBias() |
| 72 |
{ |
| 73 |
// TODO: kill all running queries |
| 74 |
} |
| 75 |
|
| 76 |
void |
| 77 |
Dynamic::LastFmBias::fromXml( QXmlStreamReader *reader ) |
| 78 |
{ |
| 79 |
while (!reader->atEnd()) { |
| 80 |
reader->readNext(); |
| 81 |
|
| 82 |
if( reader->isStartElement() ) |
| 83 |
{ |
| 84 |
QStringView name = reader->name(); |
| 85 |
if( name == QStringLiteral("match") ) |
| 86 |
m_match = matchForName( reader->readElementText(QXmlStreamReader::SkipChildElements) ); |
| 87 |
else |
| 88 |
{ |
| 89 |
debug()<<"Unexpected xml start element"<<reader->name()<<"in input"; |
| 90 |
reader->skipCurrentElement(); |
| 91 |
} |
| 92 |
} |
| 93 |
else if( reader->isEndElement() ) |
| 94 |
{ |
| 95 |
break; |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
void |
| 101 |
Dynamic::LastFmBias::toXml( QXmlStreamWriter *writer ) const |
| 102 |
{ |
| 103 |
writer->writeTextElement( QStringLiteral("match"), nameForMatch( m_match ) ); |
| 104 |
} |
| 105 |
|
| 106 |
QString |
| 107 |
Dynamic::LastFmBias::sName() |
| 108 |
{ |
| 109 |
return QStringLiteral("lastfm_similarartists"); |
| 110 |
} |
| 111 |
|
| 112 |
QString |
| 113 |
Dynamic::LastFmBias::name() const |
| 114 |
{ |
| 115 |
return Dynamic::LastFmBias::sName(); |
| 116 |
} |
| 117 |
|
| 118 |
QString |
| 119 |
Dynamic::LastFmBias::toString() const |
| 120 |
{ |
| 121 |
switch( m_match ) |
| 122 |
{ |
| 123 |
case SimilarTrack: |
| 124 |
return i18nc("Last.fm bias representation", |
| 125 |
"Similar to the previous track (as reported by Last.fm)"); |
| 126 |
case SimilarArtist: |
| 127 |
return i18nc("Last.fm bias representation", |
| 128 |
"Similar to the previous artist (as reported by Last.fm)"); |
| 129 |
} |
| 130 |
return QString(); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
QWidget* |
| 135 |
Dynamic::LastFmBias::widget( QWidget* parent ) |
| 136 |
{ |
| 137 |
QWidget *widget = new QWidget( parent ); |
| 138 |
QVBoxLayout *layout = new QVBoxLayout( widget ); |
| 139 |
|
| 140 |
QLabel *imageLabel = new QLabel(); |
| 141 |
imageLabel->setPixmap( QPixmap( QStandardPaths::locate( QStandardPaths::GenericDataLocation, QStringLiteral("amarok/images/lastfm.png") ) ) ); |
| 142 |
QLabel *label = new QLabel( i18n( "<a href=\"http://www.last.fm/\">Last.fm</a> thinks the track is similar to" ) ); |
| 143 |
|
| 144 |
QRadioButton *rb1 = new QRadioButton( i18n( "the previous track's artist" ) ); |
| 145 |
QRadioButton *rb2 = new QRadioButton( i18n( "the previous track" ) ); |
| 146 |
|
| 147 |
rb1->setChecked( m_match == SimilarArtist ); |
| 148 |
rb2->setChecked( m_match == SimilarTrack ); |
| 149 |
|
| 150 |
connect( rb1, &QRadioButton::toggled, |
| 151 |
this, &LastFmBias::setMatchTypeArtist ); |
| 152 |
|
| 153 |
layout->addWidget( imageLabel ); |
| 154 |
layout->addWidget( label ); |
| 155 |
layout->addWidget( rb1 ); |
| 156 |
layout->addWidget( rb2 ); |
| 157 |
|
| 158 |
return widget; |
| 159 |
} |
| 160 |
|
| 161 |
Dynamic::TrackSet |
| 162 |
Dynamic::LastFmBias::matchingTracks( const Meta::TrackList& playlist, |
| 163 |
int contextCount, int finalCount, |
| 164 |
const Dynamic::TrackCollectionPtr& universe ) const |
| 165 |
{ |
| 166 |
Q_UNUSED( contextCount ); |
| 167 |
Q_UNUSED( finalCount ); |
| 168 |
|
| 169 |
if( playlist.isEmpty() ) |
| 170 |
return Dynamic::TrackSet( universe, true ); |
| 171 |
|
| 172 |
// determine the last track and artist |
| 173 |
Meta::TrackPtr lastTrack = playlist.last(); |
| 174 |
Meta::ArtistPtr lastArtist = lastTrack->artist(); |
| 175 |
|
| 176 |
m_currentTrack = lastTrack->name(); |
| 177 |
m_currentArtist = lastArtist ? lastArtist->name() : QString(); |
| 178 |
|
| 179 |
{ |
| 180 |
QMutexLocker locker( &m_mutex ); |
| 181 |
|
| 182 |
if( m_match == SimilarArtist ) |
| 183 |
{ |
| 184 |
if( m_currentArtist.isEmpty() ) |
| 185 |
return Dynamic::TrackSet( universe, true ); |
| 186 |
if( m_tracksMap.contains( m_currentArtist ) ) |
| 187 |
return m_tracksMap.value( m_currentArtist ); |
| 188 |
} |
| 189 |
else if( m_match == SimilarTrack ) |
| 190 |
{ |
| 191 |
if( m_currentTrack.isEmpty() ) |
| 192 |
return Dynamic::TrackSet( universe, true ); |
| 193 |
QString key = m_currentTrack + QLatin1Char('|') + m_currentArtist; |
| 194 |
if( m_tracksMap.contains( key ) ) |
| 195 |
return m_tracksMap.value( key ); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
m_tracks = Dynamic::TrackSet( universe, false ); |
| 200 |
QTimer::singleShot(0, |
| 201 |
const_cast<LastFmBias*>(this), |
| 202 |
&LastFmBias::newQuery); // create the new query from my parent thread |
| 203 |
|
| 204 |
return Dynamic::TrackSet(); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
bool |
| 209 |
Dynamic::LastFmBias::trackMatches( int position, |
| 210 |
const Meta::TrackList& playlist, |
| 211 |
int contextCount ) const |
| 212 |
{ |
| 213 |
Q_UNUSED( contextCount ); |
| 214 |
|
| 215 |
if( position <= 0 || position >= playlist.count()) |
| 216 |
return false; |
| 217 |
|
| 218 |
// determine the last track and artist |
| 219 |
Meta::TrackPtr lastTrack = playlist[position-1]; |
| 220 |
Meta::ArtistPtr lastArtist = lastTrack->artist(); |
| 221 |
QString lastTrackName = lastTrack->name(); |
| 222 |
QString lastArtistName = lastArtist ? lastArtist->name() : QString(); |
| 223 |
|
| 224 |
Meta::TrackPtr currentTrack = playlist[position]; |
| 225 |
Meta::ArtistPtr currentArtist = currentTrack->artist(); |
| 226 |
QString currentTrackName = currentTrack->name(); |
| 227 |
QString currentArtistName = currentArtist ? currentArtist->name() : QString(); |
| 228 |
|
| 229 |
{ |
| 230 |
QMutexLocker locker( &m_mutex ); |
| 231 |
|
| 232 |
if( m_match == SimilarArtist ) |
| 233 |
{ |
| 234 |
if( lastArtistName.isEmpty() ) |
| 235 |
return true; |
| 236 |
if( currentArtistName.isEmpty() ) |
| 237 |
return false; |
| 238 |
if( lastArtistName == currentArtistName ) |
| 239 |
return true; |
| 240 |
if( m_similarArtistMap.contains( lastArtistName ) ) |
| 241 |
return m_similarArtistMap.value( lastArtistName ).contains( currentArtistName ); |
| 242 |
} |
| 243 |
else if( m_match == SimilarTrack ) |
| 244 |
{ |
| 245 |
if( lastTrackName.isEmpty() ) |
| 246 |
return true; |
| 247 |
if( currentTrackName.isEmpty() ) |
| 248 |
return false; |
| 249 |
if( lastTrackName == currentTrackName ) |
| 250 |
return true; |
| 251 |
TitleArtistPair lastKey( lastTrackName, lastArtistName ); |
| 252 |
TitleArtistPair currentKey( currentTrackName, currentArtistName ); |
| 253 |
if( m_similarTrackMap.contains( lastKey ) ) |
| 254 |
return m_similarTrackMap.value( lastKey ).contains( currentKey ); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
debug() << "didn't have a cached suggestions for track:" << lastTrackName; |
| 259 |
return false; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
void |
| 264 |
Dynamic::LastFmBias::invalidate() |
| 265 |
{ |
| 266 |
SimpleMatchBias::invalidate(); |
| 267 |
m_tracksMap.clear(); |
| 268 |
} |
| 269 |
|
| 270 |
void |
| 271 |
Dynamic::LastFmBias::newQuery() |
| 272 |
{ |
| 273 |
DEBUG_BLOCK; |
| 274 |
|
| 275 |
debug() << "similarArtists:"<<m_similarArtistMap.count() << "similarTracks:"<<m_similarTrackMap.count(); |
| 276 |
// - get the similar things |
| 277 |
QStringList similarArtists; |
| 278 |
QList<TitleArtistPair> similarTracks; |
| 279 |
{ |
| 280 |
QMutexLocker locker( &m_mutex ); |
| 281 |
if( m_match == SimilarArtist ) |
| 282 |
{ |
| 283 |
if( m_similarArtistMap.contains( m_currentArtist ) ) |
| 284 |
{ |
| 285 |
similarArtists = m_similarArtistMap.value( m_currentArtist ); |
| 286 |
debug() << "for"<<m_currentArtist<<"got similar artists:" << similarArtists.join(QStringLiteral(", ")); |
| 287 |
} |
| 288 |
else |
| 289 |
{ |
| 290 |
locker.unlock(); |
| 291 |
newSimilarQuery(); |
| 292 |
return; // not yet ready to do construct a query maker |
| 293 |
} |
| 294 |
} |
| 295 |
else if( m_match == SimilarTrack ) |
| 296 |
{ |
| 297 |
TitleArtistPair key( m_currentTrack, m_currentArtist ); |
| 298 |
if( m_similarTrackMap.contains( key ) ) |
| 299 |
{ |
| 300 |
similarTracks = m_similarTrackMap.value( key ); |
| 301 |
debug() << "for"<<key<<"got similar tracks:" << similarTracks.count(); |
| 302 |
} |
| 303 |
else |
| 304 |
{ |
| 305 |
locker.unlock(); |
| 306 |
newSimilarQuery(); |
| 307 |
return; // not yet ready to do construct a query maker |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
// ok, I need a new query maker |
| 313 |
m_qm.reset( CollectionManager::instance()->queryMaker() ); |
| 314 |
|
| 315 |
// - construct the query |
| 316 |
m_qm->beginOr(); |
| 317 |
if( m_match == SimilarArtist ) |
| 318 |
{ |
| 319 |
for( const QString &name : similarArtists ) |
| 320 |
{ |
| 321 |
m_qm->addFilter( Meta::valArtist, name, true, true ); |
| 322 |
} |
| 323 |
} |
| 324 |
else if( m_match == SimilarTrack ) |
| 325 |
{ |
| 326 |
for( const TitleArtistPair &name : similarTracks ) |
| 327 |
{ |
| 328 |
m_qm->beginAnd(); |
| 329 |
m_qm->addFilter( Meta::valTitle, name.first, true, true ); |
| 330 |
m_qm->addFilter( Meta::valArtist, name.second, true, true ); |
| 331 |
m_qm->endAndOr(); |
| 332 |
} |
| 333 |
} |
| 334 |
m_qm->endAndOr(); |
| 335 |
|
| 336 |
m_qm->setQueryType( Collections::QueryMaker::Custom ); |
| 337 |
m_qm->addReturnValue( Meta::valUniqueId ); |
| 338 |
|
| 339 |
connect( m_qm.data(), &Collections::QueryMaker::newResultReady, |
| 340 |
this, &LastFmBias::updateReady ); |
| 341 |
connect( m_qm.data(), &Collections::QueryMaker::queryDone, |
| 342 |
this, &LastFmBias::updateFinished ); |
| 343 |
|
| 344 |
// - run the query |
| 345 |
m_qm->run(); |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
void Dynamic::LastFmBias::newSimilarQuery() |
| 350 |
{ |
| 351 |
DEBUG_BLOCK |
| 352 |
|
| 353 |
QMap< QString, QString > params; |
| 354 |
// params[ "limit" ] = "70"; |
| 355 |
if( m_match == SimilarArtist ) |
| 356 |
{ |
| 357 |
params[ QStringLiteral("method") ] = QStringLiteral("artist.getSimilar"); |
| 358 |
params[ QStringLiteral("artist") ] = m_currentArtist; |
| 359 |
QNetworkReply* request = lastfm::ws::get( params ); |
| 360 |
connect( request, &QNetworkReply::finished, |
| 361 |
this, &LastFmBias::similarArtistQueryDone ); |
| 362 |
} |
| 363 |
else if( m_match == SimilarTrack ) |
| 364 |
{ |
| 365 |
// if( track->mb |
| 366 |
// TODO add mbid if the track has one |
| 367 |
params[ QStringLiteral("method") ] = QStringLiteral("track.getSimilar"); |
| 368 |
params[ QStringLiteral("artist") ] = m_currentArtist; |
| 369 |
params[ QStringLiteral("track") ] = m_currentTrack; |
| 370 |
QNetworkReply* request = lastfm::ws::get( params ); |
| 371 |
connect( request, &QNetworkReply::finished, |
| 372 |
this, &LastFmBias::similarTrackQueryDone ); |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
void |
| 378 |
Dynamic::LastFmBias::similarArtistQueryDone() // slot |
| 379 |
{ |
| 380 |
DEBUG_BLOCK |
| 381 |
|
| 382 |
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender()); |
| 383 |
|
| 384 |
if( !reply ) |
| 385 |
{ |
| 386 |
queryFailed( "job was deleted from under us...wtf! blame the gerbils." ); |
| 387 |
return; |
| 388 |
} |
| 389 |
reply->deleteLater(); |
| 390 |
|
| 391 |
QByteArray data = reply->readAll(); |
| 392 |
// debug() << "artistQuery has data:" << data; |
| 393 |
QDomDocument d; |
| 394 |
if( !d.setContent( data ) ) |
| 395 |
{ |
| 396 |
queryFailed( "Got invalid XML data from last.fm!" ); |
| 397 |
return; |
| 398 |
} |
| 399 |
|
| 400 |
QDomNodeList nodes = d.elementsByTagName( QStringLiteral("artist") ); |
| 401 |
QStringList similarArtists; |
| 402 |
for( int i =0; i < nodes.size(); ++i ) |
| 403 |
{ |
| 404 |
QDomElement n = nodes.at( i ).toElement(); |
| 405 |
// n.firstChildElement( "match" ).text().toFloat() * 100, |
| 406 |
similarArtists.append( n.firstChildElement( QStringLiteral("name") ).text() ); |
| 407 |
} |
| 408 |
|
| 409 |
QMutexLocker locker( &m_mutex ); |
| 410 |
|
| 411 |
m_similarArtistMap.insert( m_currentArtist, similarArtists ); |
| 412 |
|
| 413 |
saveDataToFile(); |
| 414 |
|
| 415 |
// -- try again to do the query |
| 416 |
newQuery(); |
| 417 |
} |
| 418 |
|
| 419 |
void Dynamic::LastFmBias::similarTrackQueryDone() |
| 420 |
{ |
| 421 |
DEBUG_BLOCK |
| 422 |
|
| 423 |
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender()); |
| 424 |
|
| 425 |
if( !reply ) |
| 426 |
{ |
| 427 |
queryFailed( "who send this...wtf! blame the gerbils." ); |
| 428 |
return; |
| 429 |
} |
| 430 |
reply->deleteLater(); |
| 431 |
|
| 432 |
// double match value, qpair title - artist |
| 433 |
QMap< int, QPair<QString,QString> > similar; |
| 434 |
QByteArray data = reply->readAll(); |
| 435 |
// debug() << "trackQuery has data:" << data; |
| 436 |
QDomDocument d; |
| 437 |
if( !d.setContent( data ) ) |
| 438 |
{ |
| 439 |
queryFailed( "Got invalid XML data from last.fm!" ); |
| 440 |
return; |
| 441 |
} |
| 442 |
|
| 443 |
QDomNodeList nodes = d.elementsByTagName( QStringLiteral("track") ); |
| 444 |
QList<TitleArtistPair> similarTracks; |
| 445 |
for( int i =0; i < nodes.size(); ++i ) |
| 446 |
{ |
| 447 |
QDomElement n = nodes.at( i ).toElement(); |
| 448 |
// n.firstChildElement( "match" ).text().toFloat() * 100, |
| 449 |
TitleArtistPair pair( n.firstChildElement( QStringLiteral("name") ).text(), |
| 450 |
n.firstChildElement( QStringLiteral("artist") ).firstChildElement( QStringLiteral("name") ).text() ); |
| 451 |
similarTracks.append( pair ); |
| 452 |
} |
| 453 |
|
| 454 |
QMutexLocker locker( &m_mutex ); |
| 455 |
|
| 456 |
TitleArtistPair key( m_currentTrack, m_currentArtist ); |
| 457 |
m_similarTrackMap.insert( key, similarTracks ); |
| 458 |
|
| 459 |
saveDataToFile(); |
| 460 |
|
| 461 |
// -- try again to do the query |
| 462 |
newQuery(); |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
void |
| 467 |
Dynamic::LastFmBias::queryFailed( const char *message ) |
| 468 |
{ |
| 469 |
debug() << message; |
| 470 |
|
| 471 |
m_tracks.reset( false ); |
| 472 |
Q_EMIT resultReady( m_tracks ); |
| 473 |
return; |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
void |
| 478 |
Dynamic::LastFmBias::saveDataToFile() const |
| 479 |
{ |
| 480 |
QFile file( Amarok::saveLocation() + QStringLiteral("dynamic_lastfm_similar.xml") ); |
| 481 |
if( !file.open( QIODevice::WriteOnly | QIODevice::Truncate ) ) |
| 482 |
return; |
| 483 |
|
| 484 |
QXmlStreamWriter writer( &file ); |
| 485 |
writer.setAutoFormatting( true ); |
| 486 |
|
| 487 |
writer.writeStartDocument(); |
| 488 |
writer.writeStartElement( QLatin1String("lastfmSimilar") ); |
| 489 |
|
| 490 |
// -- write the similar artists |
| 491 |
for( const QString& key : m_similarArtistMap.keys() ) |
| 492 |
{ |
| 493 |
writer.writeStartElement( QLatin1String("similarArtist") ); |
| 494 |
writer.writeTextElement( QLatin1String("artist"), key ); |
| 495 |
for( const QString& name : m_similarArtistMap.value( key ) ) |
| 496 |
{ |
| 497 |
writer.writeTextElement( QLatin1String("similar"), name ); |
| 498 |
} |
| 499 |
writer.writeEndElement(); |
| 500 |
} |
| 501 |
|
| 502 |
// -- write the similar tracks |
| 503 |
for( const TitleArtistPair& key : m_similarTrackMap.keys() ) |
| 504 |
{ |
| 505 |
writer.writeStartElement( QLatin1String("similarTrack") ); |
| 506 |
writer.writeStartElement( QLatin1String("track") ); |
| 507 |
writer.writeTextElement( QLatin1String("title"), key.first ); |
| 508 |
writer.writeTextElement( QLatin1String("artist"), key.second ); |
| 509 |
writer.writeEndElement(); |
| 510 |
|
| 511 |
for( const TitleArtistPair& name : m_similarTrackMap.value( key ) ) |
| 512 |
{ |
| 513 |
writer.writeStartElement( QLatin1String("similar") ); |
| 514 |
writer.writeTextElement( QLatin1String("title"), name.first ); |
| 515 |
writer.writeTextElement( QLatin1String("artist"), name.second ); |
| 516 |
writer.writeEndElement(); |
| 517 |
} |
| 518 |
writer.writeEndElement(); |
| 519 |
} |
| 520 |
|
| 521 |
writer.writeEndElement(); |
| 522 |
writer.writeEndDocument(); |
| 523 |
} |
| 524 |
|
| 525 |
void |
| 526 |
Dynamic::LastFmBias::readSimilarArtists( QXmlStreamReader *reader ) |
| 527 |
{ |
| 528 |
QString key; |
| 529 |
QList<QString> artists; |
| 530 |
|
| 531 |
while (!reader->atEnd()) { |
| 532 |
reader->readNext(); |
| 533 |
QStringView name = reader->name(); |
| 534 |
|
| 535 |
if( reader->isStartElement() ) |
| 536 |
{ |
| 537 |
if( name == QLatin1String("artist") ) |
| 538 |
key = reader->readElementText(QXmlStreamReader::SkipChildElements); |
| 539 |
else if( name == QLatin1String("similar") ) |
| 540 |
artists.append( reader->readElementText(QXmlStreamReader::SkipChildElements) ); |
| 541 |
else |
| 542 |
reader->skipCurrentElement(); |
| 543 |
} |
| 544 |
else if( reader->isEndElement() ) |
| 545 |
{ |
| 546 |
break; |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
m_similarArtistMap.insert( key, artists ); |
| 551 |
} |
| 552 |
|
| 553 |
Dynamic::LastFmBias::TitleArtistPair |
| 554 |
Dynamic::LastFmBias::readTrack( QXmlStreamReader *reader ) |
| 555 |
{ |
| 556 |
TitleArtistPair track; |
| 557 |
|
| 558 |
while (!reader->atEnd()) { |
| 559 |
reader->readNext(); |
| 560 |
QStringView name = reader->name(); |
| 561 |
|
| 562 |
if( reader->isStartElement() ) |
| 563 |
{ |
| 564 |
if( name == QLatin1String("title") ) |
| 565 |
track.first = reader->readElementText(QXmlStreamReader::SkipChildElements); |
| 566 |
else if( name == QLatin1String("artist") ) |
| 567 |
track.second = reader->readElementText(QXmlStreamReader::SkipChildElements); |
| 568 |
else |
| 569 |
reader->skipCurrentElement(); |
| 570 |
} |
| 571 |
else if( reader->isEndElement() ) |
| 572 |
{ |
| 573 |
break; |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
return track; |
| 578 |
} |
| 579 |
|
| 580 |
void |
| 581 |
Dynamic::LastFmBias::readSimilarTracks( QXmlStreamReader *reader ) |
| 582 |
{ |
| 583 |
TitleArtistPair key; |
| 584 |
QList<TitleArtistPair> tracks; |
| 585 |
|
| 586 |
while (!reader->atEnd()) { |
| 587 |
reader->readNext(); |
| 588 |
QStringView name = reader->name(); |
| 589 |
|
| 590 |
if( reader->isStartElement() ) |
| 591 |
{ |
| 592 |
if( name == QLatin1String("track") ) |
| 593 |
key = readTrack( reader ); |
| 594 |
else if( name == QLatin1String("similar") ) |
| 595 |
tracks.append( readTrack( reader ) ); |
| 596 |
else |
| 597 |
reader->skipCurrentElement(); |
| 598 |
} |
| 599 |
else if( reader->isEndElement() ) |
| 600 |
{ |
| 601 |
break; |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
m_similarTrackMap.insert( key, tracks ); |
| 606 |
} |
| 607 |
|
| 608 |
void |
| 609 |
Dynamic::LastFmBias::loadDataFromFile() |
| 610 |
{ |
| 611 |
m_similarArtistMap.clear(); |
| 612 |
m_similarTrackMap.clear(); |
| 613 |
|
| 614 |
QFile file( Amarok::saveLocation() + QStringLiteral("dynamic_lastfm_similar.xml") ); |
| 615 |
|
| 616 |
if( !file.exists() || |
| 617 |
!file.open( QIODevice::ReadOnly ) ) |
| 618 |
return; |
| 619 |
|
| 620 |
QXmlStreamReader reader( &file ); |
| 621 |
|
| 622 |
while (!reader.atEnd()) { |
| 623 |
reader.readNext(); |
| 624 |
|
| 625 |
QStringView name = reader.name(); |
| 626 |
if( reader.isStartElement() ) |
| 627 |
{ |
| 628 |
if( name == QLatin1String("lastfmSimilar") ) |
| 629 |
{ |
| 630 |
; // just recurse into the element |
| 631 |
} |
| 632 |
else if( name == QLatin1String("similarArtist") ) |
| 633 |
{ |
| 634 |
readSimilarArtists( &reader ); |
| 635 |
} |
| 636 |
else if( name == QLatin1String("similarTrack") ) |
| 637 |
{ |
| 638 |
readSimilarTracks( &reader ); |
| 639 |
} |
| 640 |
else |
| 641 |
{ |
| 642 |
reader.skipCurrentElement(); |
| 643 |
} |
| 644 |
} |
| 645 |
else if( reader.isEndElement() ) |
| 646 |
{ |
| 647 |
break; |
| 648 |
} |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
Dynamic::LastFmBias::MatchType |
| 653 |
Dynamic::LastFmBias::match() const |
| 654 |
{ return m_match; } |
| 655 |
|
| 656 |
void |
| 657 |
Dynamic::LastFmBias::setMatch( Dynamic::LastFmBias::MatchType value ) |
| 658 |
{ |
| 659 |
m_match = value; |
| 660 |
invalidate(); |
| 661 |
Q_EMIT changed( BiasPtr(this) ); |
| 662 |
} |
| 663 |
|
| 664 |
void |
| 665 |
Dynamic::LastFmBias::setMatchTypeArtist( bool matchArtist ) |
| 666 |
{ |
| 667 |
setMatch( matchArtist ? SimilarArtist : SimilarTrack ); |
| 668 |
} |
| 669 |
|
| 670 |
QString |
| 671 |
Dynamic::LastFmBias::nameForMatch( Dynamic::LastFmBias::MatchType match ) |
| 672 |
{ |
| 673 |
switch( match ) |
| 674 |
{ |
| 675 |
case SimilarArtist: return QStringLiteral("artist"); |
| 676 |
case SimilarTrack: return QStringLiteral("track"); |
| 677 |
} |
| 678 |
return QString(); |
| 679 |
} |
| 680 |
|
| 681 |
Dynamic::LastFmBias::MatchType |
| 682 |
Dynamic::LastFmBias::matchForName( const QString &name ) |
| 683 |
{ |
| 684 |
if( name == QStringLiteral("artist") ) return SimilarArtist; |
| 685 |
else if( name == QStringLiteral("track") ) return SimilarTrack; |
| 686 |
else return SimilarArtist; |
| 687 |
} |
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|