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 "CoordinatesParser.h"
0008 
0009 // Marble
0010 #include "GeoDataCoordinates.h"
0011 
0012 using namespace Marble;
0013 
0014 CoordinatesParser::CoordinatesParser( GeoDataCoordinates *coordinates )
0015     : m_coordinates( coordinates )
0016 {
0017 }
0018 
0019 bool CoordinatesParser::read( QIODevice *device )
0020 {
0021     setDevice( device );
0022     
0023     while( !atEnd() ) {
0024         readNext();
0025         
0026         if( isStartElement() ) {
0027             if (name() == QLatin1String("rsp")) {
0028                 if (attributes().value(QLatin1String("stat")) == QLatin1String("ok")) {
0029                     readRsp();
0030                 } else {
0031                     raiseError(QObject::tr("Query failed"));
0032                 }
0033             } else {
0034                 raiseError(QObject::tr("The file is not a valid Flickr answer."));
0035             }
0036         }
0037     }
0038     
0039     return !error();
0040 }
0041 
0042 
0043 void CoordinatesParser::readUnknownElement()
0044 {
0045     Q_ASSERT( isStartElement() );
0046 
0047     while ( !atEnd() ) {
0048         readNext();
0049 
0050         if ( isEndElement() )
0051             break;
0052 
0053         if ( isStartElement() )
0054             readUnknownElement();
0055     }
0056 }
0057 
0058 void CoordinatesParser::readRsp()
0059 {
0060     Q_ASSERT( isStartElement() );
0061     
0062     while( !atEnd() ) {
0063         readNext();
0064         
0065         if( isEndElement() )
0066             break;
0067         
0068         if( isStartElement() ) {
0069             if (name() == QLatin1String("photo"))
0070                 readPhoto();
0071             else
0072                 readUnknownElement();
0073         }
0074     }
0075 }
0076 
0077 void CoordinatesParser::readPhoto()
0078 {
0079     Q_ASSERT( isStartElement()
0080               && name() == QLatin1String("photo"));
0081     
0082     while( !atEnd() ) {
0083         readNext();
0084         
0085         if( isEndElement() )
0086             break;
0087         
0088         if( isStartElement() ) {
0089             if (name() == QLatin1String("location"))
0090                 readLocation();
0091             else
0092                 readUnknownElement();
0093         }
0094     }
0095 }
0096 
0097 void CoordinatesParser::readLocation()
0098 {
0099     Q_ASSERT( isStartElement()
0100               && name() == QLatin1String("location"));
0101  
0102     m_coordinates->setLatitude(attributes().value(QLatin1String("latitude")).toString().toDouble() * DEG2RAD);
0103     m_coordinates->setLongitude(attributes().value(QLatin1String("longitude")).toString().toDouble() * DEG2RAD);
0104     
0105     while( !atEnd() ) {
0106         readNext();
0107         
0108         if( isEndElement() )
0109             break;
0110         
0111         if( isStartElement() )
0112             break;
0113     }
0114 }