File indexing completed on 2025-01-05 04:25:43

0001 /****************************************************************************************
0002  * Copyright (c) 2009-2010 Joffrey Clavel <jclavel@clabert.info>                        *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "SimilarArtist.h"
0018 
0019 #include <QXmlStreamReader>
0020 
0021 SimilarArtist::SimilarArtist() {}
0022 
0023 SimilarArtist::SimilarArtist( const QString &name, const int match, const QUrl &url,
0024                               const QUrl &urlImage, const QString &similarTo )
0025     : m_name( name )
0026     , m_match( match )
0027     , m_url( url )
0028     , m_urlImage( urlImage )
0029     , m_similarTo( similarTo )
0030 {
0031 
0032     static bool metaTypeRegistered = false;
0033     if ( !metaTypeRegistered )
0034     {
0035         qRegisterMetaType<SimilarArtist>( "SimilarArtists" );
0036         metaTypeRegistered = true;
0037     }
0038 }
0039 
0040 SimilarArtist::SimilarArtist( const SimilarArtist &other )
0041     : QSharedData( other )
0042     , m_name( other.m_name )
0043     , m_match( other.m_match )
0044     , m_url( other.m_url )
0045     , m_urlImage( other.m_urlImage )
0046     , m_similarTo( other.m_similarTo )
0047 {
0048 }
0049 
0050 QString
0051 SimilarArtist::name() const
0052 {
0053     return m_name;
0054 }
0055 
0056 int
0057 SimilarArtist::match() const
0058 {
0059     return m_match;
0060 }
0061 
0062 QUrl
0063 SimilarArtist::url() const
0064 {
0065     return m_url;
0066 }
0067 
0068 QUrl
0069 SimilarArtist::urlImage() const
0070 {
0071     return m_urlImage;
0072 }
0073 
0074 QString
0075 SimilarArtist::similarTo() const
0076 {
0077     return m_similarTo;
0078 }
0079 
0080 void
0081 SimilarArtist::setSimilarTo( const QString &artist )
0082 {
0083     m_similarTo = artist;
0084 }
0085 
0086 SimilarArtist::List
0087 SimilarArtist::listFromXml( QXmlStreamReader &xml )
0088 {
0089     SimilarArtist::List saList;
0090     xml.readNextStartElement(); // lfm
0091     if( xml.attributes().value(QLatin1String("status")) != QLatin1String("ok") )
0092         return saList;
0093 
0094     QString similarTo;
0095     xml.readNextStartElement(); // similarartists
0096     if( xml.attributes().hasAttribute(QLatin1String("artist")) )
0097         similarTo = xml.attributes().value(QLatin1String("artist")).toString();
0098 
0099     while( xml.readNextStartElement() )
0100     {
0101         if( xml.name() == QLatin1String("artist") )
0102         {
0103             QString name;
0104             QUrl artistUrl;
0105             QUrl imageUrl;
0106             float match( 0.0 );
0107             while( xml.readNextStartElement() )
0108             {
0109                 const QStringRef &n = xml.name();
0110                 const QXmlStreamAttributes &a = xml.attributes();
0111                 if( n == QLatin1String("name") )
0112                     name = xml.readElementText();
0113                 else if( n == QLatin1String("match") )
0114                     match = xml.readElementText().toFloat() * 100.0;
0115                 else if( n == QLatin1String("url") )
0116                     artistUrl = QUrl( xml.readElementText() );
0117                 else if( n == QLatin1String("image")
0118                          && a.hasAttribute(QLatin1String("size"))
0119                          && a.value(QLatin1String("size")) == QLatin1String("large") )
0120                     imageUrl = QUrl( xml.readElementText() );
0121                 else
0122                     xml.skipCurrentElement();
0123             }
0124             SimilarArtistPtr artist( new SimilarArtist( name, match, artistUrl, imageUrl, similarTo ) );
0125             saList.append( artist );
0126         }
0127         else
0128             xml.skipCurrentElement();
0129     }
0130     return saList;
0131 }