| 1 |
/**************************************************************************************** |
| 2 |
* Copyright (c) 2009-2010 Joffrey Clavel <[email protected]> * |
| 3 |
* Copyright (c) 2024 Tuomas Nurmi <[email protected]> * |
| 4 |
* * |
| 5 |
* This program is free software; you can redistribute it and/or modify it under * |
| 6 |
* the terms of the GNU General Public License as published by the Free Software * |
| 7 |
* Foundation; either version 2 of the License, or (at your option) any later * |
| 8 |
* version. * |
| 9 |
* * |
| 10 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY * |
| 11 |
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * |
| 12 |
* PARTICULAR PURPOSE. See the GNU General Public License for more details. * |
| 13 |
* * |
| 14 |
* You should have received a copy of the GNU General Public License along with * |
| 15 |
* this program. If not, see <http://www.gnu.org/licenses/>. * |
| 16 |
****************************************************************************************/ |
| 17 |
|
| 18 |
#include "SimilarArtistModel.h" |
| 19 |
#include "SimilarArtistsEngine.h" |
| 20 |
|
| 21 |
#include "core/support/Debug.h" |
| 22 |
|
| 23 |
#include <QXmlStreamReader> |
| 24 |
|
| 25 |
SimilarArtistModel::SimilarArtistModel( QObject *parent ) |
| 26 |
: QStandardItemModel( parent ) |
| 27 |
{ |
| 28 |
|
| 29 |
} |
| 30 |
|
| 31 |
QString |
| 32 |
SimilarArtistModel::SimilarArtistItem::name() const |
| 33 |
{ |
| 34 |
return m_name; |
| 35 |
} |
| 36 |
|
| 37 |
int |
| 38 |
SimilarArtistModel::SimilarArtistItem::match() const |
| 39 |
{ |
| 40 |
return m_match; |
| 41 |
} |
| 42 |
|
| 43 |
QUrl |
| 44 |
SimilarArtistModel::SimilarArtistItem::url() const |
| 45 |
{ |
| 46 |
return m_url; |
| 47 |
} |
| 48 |
|
| 49 |
QUrl |
| 50 |
SimilarArtistModel::SimilarArtistItem::urlImage() const |
| 51 |
{ |
| 52 |
return m_urlImage; |
| 53 |
} |
| 54 |
|
| 55 |
QString |
| 56 |
SimilarArtistModel::SimilarArtistItem::bioText() const |
| 57 |
{ |
| 58 |
return m_bioText; |
| 59 |
} |
| 60 |
|
| 61 |
QString |
| 62 |
SimilarArtistModel::SimilarArtistItem::listenerCount() const |
| 63 |
{ |
| 64 |
return m_listenerCount; |
| 65 |
} |
| 66 |
|
| 67 |
QString |
| 68 |
SimilarArtistModel::SimilarArtistItem::playCount() const |
| 69 |
{ |
| 70 |
return m_playCount; |
| 71 |
} |
| 72 |
|
| 73 |
QString |
| 74 |
SimilarArtistModel::SimilarArtistItem::ownPlayCount() const |
| 75 |
{ |
| 76 |
return m_ownPlayCount; |
| 77 |
} |
| 78 |
|
| 79 |
QUrl |
| 80 |
SimilarArtistModel::SimilarArtistItem::albumCover() const |
| 81 |
{ |
| 82 |
return m_albumCover; |
| 83 |
} |
| 84 |
|
| 85 |
QVariant |
| 86 |
SimilarArtistModel::data( const QModelIndex &index, int role ) const |
| 87 |
{ |
| 88 |
if( !index.isValid() ) |
| 89 |
return QVariant(); |
| 90 |
|
| 91 |
const QStandardItem *item = itemFromIndex( index ); |
| 92 |
|
| 93 |
if( const auto artist = dynamic_cast<const SimilarArtistItem *>( item ) ) |
| 94 |
{ |
| 95 |
if (role == NameRole) |
| 96 |
return artist->name(); |
| 97 |
if (role == MatchRole) |
| 98 |
return artist->match(); |
| 99 |
if (role == LinkRole) |
| 100 |
return artist->url(); |
| 101 |
if (role == ImageRole) |
| 102 |
return artist->urlImage(); |
| 103 |
if (role == AlbumCoverRole) |
| 104 |
return artist->albumCover(); |
| 105 |
if (role == BioRole || role == ListenerCountRole || role == PlayCountRole || role == OwnPlayCountRole) |
| 106 |
{ |
| 107 |
if(artist->bioText() == QString()) |
| 108 |
{ |
| 109 |
if( const auto engine = dynamic_cast<SimilarArtistsEngine *>( parent() ) ) |
| 110 |
{ |
| 111 |
engine->artistInfoRequest( artist->name() ); |
| 112 |
} |
| 113 |
return QString(); |
| 114 |
} |
| 115 |
switch( role ) |
| 116 |
{ |
| 117 |
case BioRole: |
| 118 |
return artist->bioText(); |
| 119 |
case ListenerCountRole: |
| 120 |
return artist->listenerCount(); |
| 121 |
case PlayCountRole: |
| 122 |
return artist->playCount(); |
| 123 |
case OwnPlayCountRole: |
| 124 |
return artist->ownPlayCount(); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
return itemFromIndex( index )->data( role ); |
| 129 |
} |
| 130 |
|
| 131 |
void |
| 132 |
SimilarArtistModel::clearAll() |
| 133 |
{ |
| 134 |
m_similarTo = QString(); |
| 135 |
clear(); |
| 136 |
} |
| 137 |
|
| 138 |
void |
| 139 |
SimilarArtistModel::setCurrentTarget( const QString &target ) |
| 140 |
{ |
| 141 |
if( target != m_similarTo ) |
| 142 |
{ |
| 143 |
clearAll(); |
| 144 |
m_similarTo = target; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
void |
| 149 |
SimilarArtistModel::setCover( const QString &target, const QUrl &cover ) |
| 150 |
{ |
| 151 |
for( auto i : findItems( target ) ) |
| 152 |
{ |
| 153 |
if( auto artist = dynamic_cast< SimilarArtistItem *>( i ) ) |
| 154 |
{ |
| 155 |
artist->m_albumCover = cover; |
| 156 |
artist->emitDataChanged(); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
QHash<int, QByteArray> |
| 162 |
SimilarArtistModel::roleNames() const { |
| 163 |
QHash<int, QByteArray> roles; |
| 164 |
roles[NameRole] = "name"; |
| 165 |
roles[MatchRole] = "match"; |
| 166 |
roles[LinkRole] = "link"; |
| 167 |
roles[ImageRole] = "image"; |
| 168 |
roles[BioRole] = "bio"; |
| 169 |
roles[ListenerCountRole] = "listeners"; |
| 170 |
roles[PlayCountRole] = "plays"; |
| 171 |
roles[OwnPlayCountRole] = "ownplays"; |
| 172 |
roles[AlbumCoverRole] = "albumcover"; |
| 173 |
return roles; |
| 174 |
} |
| 175 |
|
| 176 |
void |
| 177 |
SimilarArtistModel::fillArtistInfoFromXml( QXmlStreamReader &xml ) |
| 178 |
{ |
| 179 |
xml.readNextStartElement(); // lfm |
| 180 |
if( xml.attributes().value(QLatin1String("status")) != QLatin1String("ok") ) |
| 181 |
return; |
| 182 |
|
| 183 |
QString name; |
| 184 |
QString listeners; |
| 185 |
QString plays; |
| 186 |
QString ownPlays; |
| 187 |
QString tags; |
| 188 |
QString bio=QLatin1String(" "); |
| 189 |
|
| 190 |
while( xml.name() != QLatin1String("name") ) |
| 191 |
xml.readNextStartElement(); |
| 192 |
name = xml.readElementText(); |
| 193 |
|
| 194 |
if( name == QString() ) |
| 195 |
return; // something's not right, stop |
| 196 |
|
| 197 |
if( const auto engine = dynamic_cast<SimilarArtistsEngine *>( parent() ) ) |
| 198 |
{ // we know a similar artist's name, also check if we've got some in our local collection |
| 199 |
engine->searchLocalCollection( name ); |
| 200 |
} |
| 201 |
|
| 202 |
while( xml.readNextStartElement() ) |
| 203 |
{ |
| 204 |
const QStringView &n = xml.name(); |
| 205 |
const QXmlStreamAttributes &a = xml.attributes(); |
| 206 |
if( n == QLatin1String("stats") ) |
| 207 |
{ |
| 208 |
while (xml.readNextStartElement()) |
| 209 |
{ |
| 210 |
if( xml.name() == QLatin1String("listeners") ) |
| 211 |
listeners = xml.readElementText(); |
| 212 |
else if( xml.name() == QLatin1String("playcount") ) |
| 213 |
plays = xml.readElementText(); |
| 214 |
else if( xml.name() == QLatin1String("userplaycount") ) |
| 215 |
ownPlays = xml.readElementText(); |
| 216 |
else |
| 217 |
xml.skipCurrentElement(); |
| 218 |
} |
| 219 |
} |
| 220 |
else if( n == QLatin1String("tags") ) |
| 221 |
{ |
| 222 |
while (xml.readNextStartElement()) |
| 223 |
{ |
| 224 |
//<tag><name>glam rock</name> |
| 225 |
//<url>https://www.last.fm/tag/glam+rock</url> |
| 226 |
//</tag> |
| 227 |
if( xml.name() == QLatin1String("tag") ) |
| 228 |
{ |
| 229 |
while (xml.readNextStartElement()) |
| 230 |
{ |
| 231 |
xml.skipCurrentElement(); |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
else if( n == QLatin1String("bio") ) |
| 237 |
{ |
| 238 |
while (xml.readNextStartElement()) |
| 239 |
{ |
| 240 |
//if( xml.name() == QLatin1String("summary") ) |
| 241 |
if( xml.name() == QLatin1String("content") ) |
| 242 |
{ |
| 243 |
bio = xml.readElementText().replace( QStringLiteral("\n"), QStringLiteral("<br>") ); |
| 244 |
if(bio == QString()) |
| 245 |
bio = QLatin1String(" "); |
| 246 |
} |
| 247 |
else |
| 248 |
xml.skipCurrentElement(); |
| 249 |
} |
| 250 |
} |
| 251 |
else |
| 252 |
xml.skipCurrentElement(); |
| 253 |
} |
| 254 |
|
| 255 |
for( auto i : findItems( name ) ) |
| 256 |
{ |
| 257 |
if( auto artist = dynamic_cast< SimilarArtistItem *>( i ) ) |
| 258 |
{ |
| 259 |
artist->m_bioText = bio.isNull() ? QLatin1String(" ") : bio; |
| 260 |
artist->m_listenerCount = listeners; |
| 261 |
artist->m_playCount = plays; |
| 262 |
artist->m_ownPlayCount = ownPlays; |
| 263 |
artist->emitDataChanged(); |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
void |
| 269 |
SimilarArtistModel::fillFromXml( QXmlStreamReader &xml ) |
| 270 |
{ |
| 271 |
clear(); |
| 272 |
xml.readNextStartElement(); // lfm |
| 273 |
if( xml.attributes().value(QLatin1String("status")) != QLatin1String("ok") ) |
| 274 |
return; |
| 275 |
|
| 276 |
xml.readNextStartElement(); // similarartists |
| 277 |
if( xml.attributes().hasAttribute(QLatin1String("artist")) ) |
| 278 |
m_similarTo = xml.attributes().value(QLatin1String("artist")).toString(); |
| 279 |
|
| 280 |
while( xml.readNextStartElement() ) |
| 281 |
{ |
| 282 |
if( xml.name() == QLatin1String("artist") ) |
| 283 |
{ |
| 284 |
SimilarArtistItem *similarArtistItem = new SimilarArtistItem(); |
| 285 |
while( xml.readNextStartElement() ) |
| 286 |
{ |
| 287 |
const QStringView &n = xml.name(); |
| 288 |
const QXmlStreamAttributes &a = xml.attributes(); |
| 289 |
if( n == QLatin1String("name") ) |
| 290 |
similarArtistItem->m_name = xml.readElementText(); |
| 291 |
else if( n == QLatin1String("match") ) |
| 292 |
similarArtistItem->m_match = xml.readElementText().toFloat() * 100.0; |
| 293 |
else if( n == QLatin1String("url") ) |
| 294 |
similarArtistItem->m_url = QUrl( xml.readElementText() ); |
| 295 |
// unfortunately disabled in last.fm api since 2019 or so, now returns only generic placeholder image |
| 296 |
else if( n == QLatin1String("image") |
| 297 |
&& a.hasAttribute(QLatin1String("size")) |
| 298 |
&& a.value(QLatin1String("size")) == QLatin1String("large") ) |
| 299 |
similarArtistItem->m_urlImage = QUrl( xml.readElementText() ); |
| 300 |
else |
| 301 |
xml.skipCurrentElement(); |
| 302 |
} |
| 303 |
similarArtistItem->setText( similarArtistItem->m_name ); |
| 304 |
appendRow( similarArtistItem ); |
| 305 |
} |
| 306 |
else |
| 307 |
xml.skipCurrentElement(); |
| 308 |
} |
| 309 |
debug() << "Found" << rowCount() << "similar artists to" << m_similarTo; |
| 310 |
} |