File indexing completed on 2024-05-05 03:50:48

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2009 Bastian Holst <bastianholst@gmx.de>
0004 //
0005 
0006 // Self
0007 #include "FlickrParser.h"
0008 
0009 // Marble
0010 #include "PhotoPluginItem.h"
0011 
0012 // Qt
0013 #include <QByteArray>
0014 
0015 using namespace Marble;
0016 
0017 FlickrParser::FlickrParser( MarbleWidget *widget,
0018                             QList<PhotoPluginItem *> *list,
0019                             QObject *parent )
0020     : m_marbleWidget( widget ),
0021       m_list( list ),
0022       m_parent( parent )
0023 {
0024 }
0025 
0026 bool FlickrParser::read( const QByteArray& data )
0027 {
0028     addData( data );
0029 
0030     while (!atEnd()) {
0031         readNext();
0032 
0033         if (isStartElement()) {
0034             if (name() == QLatin1String("rsp")) {
0035                 if (attributes().value(QLatin1String("stat")) == QLatin1String("ok")) {
0036                     readFlickr();
0037                 } else {
0038                     raiseError(QObject::tr("Query failed"));
0039                 }
0040             } else {
0041                 raiseError(QObject::tr("The file is not a valid Flickr answer."));
0042             }
0043         }
0044     }
0045 
0046     return !error();
0047 }
0048 
0049 void FlickrParser::readUnknownElement()
0050 {
0051     Q_ASSERT( isStartElement() );
0052 
0053     while ( !atEnd() ) {
0054         readNext();
0055 
0056         if ( isEndElement() )
0057             break;
0058 
0059         if ( isStartElement() )
0060             readUnknownElement();
0061     }
0062 }
0063 
0064 void FlickrParser::readFlickr()
0065 {
0066     Q_ASSERT( isStartElement()
0067               && name() == QLatin1String("rsp")
0068               && attributes().value(QLatin1String("stat")) == QLatin1String("ok"));
0069               
0070     while( !atEnd() ) {
0071         readNext();
0072         
0073         if( isEndElement() )
0074             break;
0075         
0076         if( isStartElement() ) {
0077             if (name() == QLatin1String("photos"))
0078                 readPhotos();
0079             else
0080                 readUnknownElement();
0081         }
0082     }
0083 }
0084 
0085 void FlickrParser::readPhotos()
0086 {
0087     Q_ASSERT( isStartElement()
0088               && name() == QLatin1String("photos"));
0089 
0090     while( !atEnd() ) {
0091         readNext();
0092         
0093         if( isEndElement() )
0094             break;
0095         
0096         if( isStartElement() ) {
0097             if (name() == QLatin1String("photo"))
0098                 readPhoto();
0099             else
0100                 readUnknownElement();
0101         }
0102     }
0103 }
0104 
0105 void FlickrParser::readPhoto()
0106 {
0107     Q_ASSERT( isStartElement()
0108               && name() == QLatin1String("photo"));
0109 
0110     if( attributes().hasAttribute(QLatin1String("id")) ) {
0111         PhotoPluginItem *item = new PhotoPluginItem( m_marbleWidget, m_parent );
0112         item->setId( attributes().value(QLatin1String("id")).toString() );
0113         item->setServer( attributes().value(QLatin1String("server")).toString() );
0114         item->setFarm( attributes().value(QLatin1String("farm")).toString() );
0115         item->setSecret( attributes().value(QLatin1String("secret")).toString() );
0116         item->setOwner( attributes().value(QLatin1String("owner")).toString() );
0117         item->setTitle( attributes().value(QLatin1String("title")).toString() );
0118         m_list->append( item );
0119     }
0120     
0121     while( !atEnd() ) {
0122         readNext();
0123         
0124         if( isEndElement() )
0125             break;
0126         
0127         if( isStartElement() )
0128             break;
0129     }
0130 }