File indexing completed on 2024-05-05 03:50:56
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 "GeonamesParser.h" 0008 0009 // Marble 0010 #include "MarbleGlobal.h" 0011 #include "WikipediaItem.h" 0012 #include "MarbleDebug.h" 0013 0014 // Qt 0015 #include <QByteArray> 0016 0017 using namespace Marble; 0018 0019 GeonamesParser::GeonamesParser( MarbleWidget * widget, 0020 QList<WikipediaItem *> *list, 0021 QObject *parent ) 0022 : m_marbleWidget( widget ), 0023 m_list( list ), 0024 m_parent( parent ) 0025 { 0026 } 0027 0028 bool GeonamesParser::read( const QByteArray& data ) 0029 { 0030 addData( data ); 0031 0032 while (!atEnd()) { 0033 readNext(); 0034 0035 if ( isStartElement() ) { 0036 if (name() == QLatin1String("geonames")) 0037 readGeonames(); 0038 else 0039 raiseError( QObject::tr("The file is not a valid Geonames answer.") ); 0040 } 0041 } 0042 0043 return !error(); 0044 } 0045 0046 void GeonamesParser::readUnknownElement() 0047 { 0048 Q_ASSERT( isStartElement() ); 0049 0050 while ( !atEnd() ) { 0051 readNext(); 0052 0053 if ( isEndElement() ) 0054 break; 0055 0056 if ( isStartElement() ) 0057 readUnknownElement(); 0058 } 0059 } 0060 0061 void GeonamesParser::readGeonames() 0062 { 0063 Q_ASSERT( isStartElement() 0064 && name() == QLatin1String("geonames")); 0065 0066 while ( !atEnd() ) { 0067 readNext(); 0068 0069 if ( isEndElement() ) 0070 break; 0071 0072 if ( isStartElement() ) { 0073 if (name() == QLatin1String("entry")) 0074 readEntry(); 0075 else 0076 readUnknownElement(); 0077 } 0078 } 0079 } 0080 0081 void GeonamesParser::readEntry() 0082 { 0083 Q_ASSERT( isStartElement() 0084 && name() == QLatin1String("entry")); 0085 0086 WikipediaItem *item = new WikipediaItem( m_marbleWidget, m_parent ); 0087 m_list->append( item ); 0088 0089 while ( !atEnd() ) { 0090 readNext(); 0091 0092 if ( isEndElement() ) 0093 break; 0094 0095 if ( isStartElement() ) { 0096 if ( name() == QLatin1String("title" )) 0097 readTitle( item ); 0098 else if (name() == QLatin1String("lng")) 0099 readLongitude( item ); 0100 else if (name() == QLatin1String("lat")) 0101 readLatitude( item ); 0102 else if (name() == QLatin1String("wikipediaUrl")) 0103 readUrl( item ); 0104 else if (name() == QLatin1String("summary")) 0105 readSummary( item ); 0106 else if (name() == QLatin1String("thumbnailImg")) 0107 readThumbnailImage( item ); 0108 else if (name() == QLatin1String("rank")) 0109 readRank( item ); 0110 else 0111 readUnknownElement(); 0112 } 0113 } 0114 } 0115 0116 void GeonamesParser::readTitle( WikipediaItem *item ) 0117 { 0118 Q_ASSERT( isStartElement() 0119 && name() == QLatin1String("title")); 0120 0121 while ( !atEnd() ) { 0122 readNext(); 0123 0124 if ( isEndElement() ) 0125 break; 0126 0127 if ( isCharacters() ) { 0128 item->setName( text().toString() ); 0129 } 0130 } 0131 } 0132 0133 void GeonamesParser::readLongitude( WikipediaItem *item ) 0134 { 0135 Q_ASSERT( isStartElement() 0136 && name() == QLatin1String("lng")); 0137 0138 while ( !atEnd() ) { 0139 readNext(); 0140 0141 if ( isEndElement() ) 0142 break; 0143 0144 if ( isCharacters() ) { 0145 item->setLongitude( text().toString().toDouble() * DEG2RAD ); 0146 } 0147 } 0148 } 0149 0150 void GeonamesParser::readLatitude( WikipediaItem *item ) 0151 { 0152 Q_ASSERT( isStartElement() 0153 && name() == QLatin1String("lat")); 0154 0155 while ( !atEnd() ) { 0156 readNext(); 0157 0158 if ( isEndElement() ) 0159 break; 0160 0161 if ( isCharacters() ) { 0162 item->setLatitude( text().toString().toDouble() * DEG2RAD ); 0163 } 0164 } 0165 } 0166 0167 void GeonamesParser::readUrl( WikipediaItem *item ) 0168 { 0169 Q_ASSERT( isStartElement() 0170 && name() == QLatin1String("wikipediaUrl")); 0171 0172 while ( !atEnd() ) { 0173 readNext(); 0174 0175 if ( isEndElement() ) 0176 break; 0177 0178 if ( isCharacters() ) { 0179 // Try to switch to the mobile version, geonames 0180 // lacks API for that unfortunately 0181 QString url = text().toString(); 0182 if (!url.contains(QLatin1String("m.wikipedia.org"))) { 0183 url.replace( "wikipedia.org", "m.wikipedia.org" ); 0184 } 0185 item->setUrl( QUrl::fromEncoded( url.toUtf8() ) ); 0186 } 0187 } 0188 } 0189 0190 void GeonamesParser::readSummary( WikipediaItem *item ) 0191 { 0192 Q_ASSERT( isStartElement() 0193 && name() == QLatin1String("summary")); 0194 0195 while ( !atEnd() ) { 0196 readNext(); 0197 0198 if ( isEndElement() ) 0199 break; 0200 0201 if ( isCharacters() ) { 0202 item->setSummary( text().toString() ); 0203 } 0204 } 0205 } 0206 0207 void GeonamesParser::readThumbnailImage( WikipediaItem *item ) 0208 { 0209 Q_ASSERT( isStartElement() 0210 && name() == QLatin1String("thumbnailImg")); 0211 0212 while ( !atEnd() ) { 0213 readNext(); 0214 0215 if ( isEndElement() ) 0216 break; 0217 0218 if ( isCharacters() ) { 0219 item->setThumbnailImageUrl( QUrl( text().toString() ) ); 0220 } 0221 } 0222 } 0223 0224 void GeonamesParser::readRank( WikipediaItem *item ) 0225 { 0226 Q_ASSERT( isStartElement() 0227 && name() == QLatin1String("rank")); 0228 0229 while ( !atEnd() ) { 0230 readNext(); 0231 0232 if ( isEndElement() ) 0233 break; 0234 0235 if ( isCharacters() ) { 0236 item->setRank( text().toString().toDouble() ); 0237 } 0238 } 0239 }