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

0001 /****************************************************************************************
0002  * Copyright (c) 2010 Rick W. Chen <stuffcorpse@archlinux.us>                           *
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 "LastFmEventXmlParser.h"
0018 
0019 
0020 LastFmEventXmlParser::LastFmEventXmlParser( QXmlStreamReader &reader )
0021     : m_xml( reader )
0022 {
0023 }
0024 
0025 LastFmEventXmlParser::~LastFmEventXmlParser()
0026 {}
0027 
0028 bool
0029 LastFmEventXmlParser::read()
0030 {
0031     while( !m_xml.atEnd() && !m_xml.hasError() )
0032     {
0033         m_xml.readNext();
0034         if( m_xml.isStartElement() && m_xml.name() == "event" )
0035         {
0036             QHash<QString, QString> artists;
0037             LastFmEventPtr event( new LastFmEvent );
0038             while( !m_xml.atEnd() )
0039             {
0040                 m_xml.readNext();
0041                 const QStringRef &n = m_xml.name();
0042                 if( m_xml.isEndElement() && n == "event" )
0043                     break;
0044 
0045                 if( m_xml.isStartElement() )
0046                 {
0047                     const QXmlStreamAttributes &a = m_xml.attributes();
0048                     if( n == "title" )
0049                         event->setName( m_xml.readElementText() );
0050                     else if( n == "artists" )
0051                         artists = readEventArtists();
0052                     else if( n == "venue" )
0053                     {
0054                         LastFmVenueXmlParser venueParser( m_xml );
0055                         if( venueParser.read() )
0056                             event->setVenue( venueParser.venue() );
0057                     }
0058                     else if( n == "startDate" )
0059                         event->setDate( KDateTime::fromString( m_xml.readElementText(), "%a, %d %b %Y %H:%M:%S" ) );
0060                     else if( n == "image" && a.hasAttribute("size") )
0061                         event->setImageUrl( LastFmEvent::stringToImageSize(a.value("size").toString()), QUrl( m_xml.readElementText() ) );
0062                     else if( n == "url" )
0063                         event->setUrl( QUrl( m_xml.readElementText() ) );
0064                     else if( n == "attendance" )
0065                         event->setAttendance( m_xml.readElementText().toInt() );
0066                     else if( n == "cancelled" )
0067                         event->setCancelled( bool( m_xml.readElementText().toInt() ) );
0068                     else if( n == "tags" )
0069                         event->setTags( readEventTags() );
0070                     else
0071                         m_xml.skipCurrentElement();
0072                 }
0073             }
0074             event->setHeadliner( artists.value("headliner") );
0075             event->setParticipants( artists.values("artist") );
0076             m_events << event;
0077         }
0078     }
0079     return !m_xml.error();
0080 }
0081 
0082 QStringList
0083 LastFmEventXmlParser::readEventTags()
0084 {
0085     QStringList tags;
0086     while( !m_xml.atEnd() )
0087     {
0088         m_xml.readNext();
0089         if( m_xml.isEndElement() && m_xml.name() == "tags" )
0090             break;
0091 
0092         if( m_xml.isStartElement() )
0093         {
0094             if( m_xml.name() == "tag" )
0095                 tags << m_xml.readElementText();
0096             else
0097                 m_xml.skipCurrentElement();
0098         }
0099     }
0100     return tags;
0101 }
0102 
0103 QHash<QString, QString>
0104 LastFmEventXmlParser::readEventArtists()
0105 {
0106     QHash<QString, QString> artists;
0107     while( !m_xml.atEnd() )
0108     {
0109         m_xml.readNext();
0110         if( m_xml.isEndElement() && m_xml.name() == "artists" )
0111             break;
0112 
0113         if( m_xml.isStartElement() )
0114         {
0115             if( m_xml.name() == "artist" )
0116                 artists.insertMulti( "artist", m_xml.readElementText() );
0117             else if( m_xml.name() == "headliner" )
0118                 artists.insert( "headliner", m_xml.readElementText() );
0119             else
0120                 m_xml.skipCurrentElement();
0121         }
0122     }
0123     return artists;
0124 }
0125 
0126 
0127 LastFmVenueXmlParser::LastFmVenueXmlParser( QXmlStreamReader &reader )
0128     : m_xml( reader )
0129 {}
0130 
0131 bool
0132 LastFmVenueXmlParser::read()
0133 {
0134     m_venue = new LastFmVenue;
0135     while( !m_xml.atEnd() && !m_xml.hasError() )
0136     {
0137         m_xml.readNext();
0138         const QStringRef &n = m_xml.name();
0139         if( m_xml.isEndElement() && n == "venue" )
0140             break;
0141 
0142         if( m_xml.isStartElement() )
0143         {
0144             const QXmlStreamAttributes &a = m_xml.attributes();
0145             if( n == "id" )
0146                 m_venue->id = m_xml.readElementText().toInt();
0147             else if( n == "name" )
0148                 m_venue->name = m_xml.readElementText();
0149             else if( n == "location" )
0150             {
0151                 LastFmLocationXmlParser locationParser( m_xml );
0152                 if( locationParser.read() )
0153                     m_venue->location = locationParser.location();
0154             }
0155             else if( n == "url" )
0156                 m_venue->url = QUrl( m_xml.readElementText() );
0157             else if( n == "website" )
0158                 m_venue->website = QUrl( m_xml.readElementText() );
0159             else if( n == "phonenumber" )
0160                 m_venue->phoneNumber = m_xml.readElementText();
0161             else if( n == "image" && a.hasAttribute("size") )
0162             {
0163                 LastFmEvent::ImageSize size = LastFmEvent::stringToImageSize( a.value("size").toString() );
0164                 m_venue->imageUrls[ size ] = QUrl( m_xml.readElementText() );
0165             }
0166             else
0167                 m_xml.skipCurrentElement();
0168         }
0169     }
0170     return !m_xml.error();
0171 }
0172 
0173 LastFmVenueXmlParser::~LastFmVenueXmlParser()
0174 {}
0175 
0176 LastFmLocationXmlParser::LastFmLocationXmlParser( QXmlStreamReader &reader )
0177     : m_xml( reader )
0178 {}
0179 
0180 bool
0181 LastFmLocationXmlParser::read()
0182 {
0183     m_location = new LastFmLocation;
0184     while( !m_xml.atEnd() && !m_xml.hasError() )
0185     {
0186         m_xml.readNext();
0187         if( m_xml.isEndElement() && m_xml.name() == "location" )
0188             break;
0189 
0190         if( m_xml.isStartElement() )
0191         {
0192             if( m_xml.name() == "city" )
0193                 m_location->city = m_xml.readElementText();
0194             else if( m_xml.name() == "country" )
0195                 m_location->country = m_xml.readElementText();
0196             else if( m_xml.name() == "street" )
0197                 m_location->street = m_xml.readElementText();
0198             else if( m_xml.name() == "postalcode" )
0199                 m_location->postalCode = m_xml.readElementText();
0200             else if( m_xml.prefix() == "geo" && m_xml.name() == "point" )
0201                 readGeoPoint();
0202             else
0203                 m_xml.skipCurrentElement();
0204         }
0205     }
0206     return !m_xml.error();
0207 }
0208 
0209 LastFmLocationXmlParser::~LastFmLocationXmlParser()
0210 {}
0211 
0212 void
0213 LastFmLocationXmlParser::readGeoPoint()
0214 {
0215     while( !m_xml.atEnd() && !m_xml.hasError() )
0216     {
0217         m_xml.readNext();
0218         if( m_xml.isEndElement() && m_xml.name() == "point" )
0219             break;
0220 
0221         if( m_xml.isStartElement() )
0222         {
0223             if( m_xml.name() == "lat" )
0224                 m_location->latitude = m_xml.readElementText().toDouble();
0225             else if( m_xml.name() == "long" )
0226                 m_location->longitude = m_xml.readElementText().toDouble();
0227             else
0228                 m_xml.skipCurrentElement();
0229         }
0230     }
0231 }