• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

marble

  • sources
  • kde-4.14
  • kdeedu
  • marble
  • src
  • plugins
  • render
  • wikipedia
GeonamesParser.cpp
Go to the documentation of this file.
1 //
2 // This file is part of the Marble Virtual Globe.
3 //
4 // This program is free software licensed under the GNU LGPL. You can
5 // find a copy of this license in LICENSE.txt in the top directory of
6 // the source code.
7 //
8 // Copyright 2009 Bastian Holst <bastianholst@gmx.de>
9 //
10 
11 // Self
12 #include "GeonamesParser.h"
13 
14 // Marble
15 #include "MarbleGlobal.h"
16 #include "WikipediaItem.h"
17 #include "MarbleDebug.h"
18 
19 // Qt
20 #include <QByteArray>
21 
22 using namespace Marble;
23 
24 GeonamesParser::GeonamesParser( MarbleWidget * widget,
25  QList<WikipediaItem *> *list,
26  QObject *parent )
27  : m_marbleWidget( widget ),
28  m_list( list ),
29  m_parent( parent )
30 {
31 }
32 
33 bool GeonamesParser::read( const QByteArray& data )
34 {
35  addData( data );
36 
37  while (!atEnd()) {
38  readNext();
39 
40  if ( isStartElement() ) {
41  if ( name() == "geonames" )
42  readGeonames();
43  else
44  raiseError( QObject::tr("The file is not a valid Geonames answer.") );
45  }
46  }
47 
48  return !error();
49 }
50 
51 void GeonamesParser::readUnknownElement()
52 {
53  Q_ASSERT( isStartElement() );
54 
55  while ( !atEnd() ) {
56  readNext();
57 
58  if ( isEndElement() )
59  break;
60 
61  if ( isStartElement() )
62  readUnknownElement();
63  }
64 }
65 
66 void GeonamesParser::readGeonames()
67 {
68  Q_ASSERT( isStartElement()
69  && name() == "geonames" );
70 
71  while ( !atEnd() ) {
72  readNext();
73 
74  if ( isEndElement() )
75  break;
76 
77  if ( isStartElement() ) {
78  if ( name() == "entry" )
79  readEntry();
80  else
81  readUnknownElement();
82  }
83  }
84 }
85 
86 void GeonamesParser::readEntry()
87 {
88  Q_ASSERT( isStartElement()
89  && name() == "entry" );
90 
91  WikipediaItem *item = new WikipediaItem( m_marbleWidget, m_parent );
92  m_list->append( item );
93 
94  while ( !atEnd() ) {
95  readNext();
96 
97  if ( isEndElement() )
98  break;
99 
100  if ( isStartElement() ) {
101  if ( name() == "title" )
102  readTitle( item );
103  else if ( name() == "lng" )
104  readLongitude( item );
105  else if ( name() == "lat" )
106  readLatitude( item );
107  else if ( name() == "wikipediaUrl" )
108  readUrl( item );
109  else if ( name() == "summary" )
110  readSummary( item );
111  else if ( name() == "thumbnailImg" )
112  readThumbnailImage( item );
113  else if ( name() == "rank" )
114  readRank( item );
115  else
116  readUnknownElement();
117  }
118  }
119 }
120 
121 void GeonamesParser::readTitle( WikipediaItem *item )
122 {
123  Q_ASSERT( isStartElement()
124  && name() == "title" );
125 
126  while ( !atEnd() ) {
127  readNext();
128 
129  if ( isEndElement() )
130  break;
131 
132  if ( isCharacters() ) {
133  item->setName( text().toString() );
134  }
135  }
136 }
137 
138 void GeonamesParser::readLongitude( WikipediaItem *item )
139 {
140  Q_ASSERT( isStartElement()
141  && name() == "lng" );
142 
143  while ( !atEnd() ) {
144  readNext();
145 
146  if ( isEndElement() )
147  break;
148 
149  if ( isCharacters() ) {
150  item->setLongitude( text().toString().toDouble() * DEG2RAD );
151  }
152  }
153 }
154 
155 void GeonamesParser::readLatitude( WikipediaItem *item )
156 {
157  Q_ASSERT( isStartElement()
158  && name() == "lat" );
159 
160  while ( !atEnd() ) {
161  readNext();
162 
163  if ( isEndElement() )
164  break;
165 
166  if ( isCharacters() ) {
167  item->setLatitude( text().toString().toDouble() * DEG2RAD );
168  }
169  }
170 }
171 
172 void GeonamesParser::readUrl( WikipediaItem *item )
173 {
174  Q_ASSERT( isStartElement()
175  && name() == "wikipediaUrl" );
176 
177  while ( !atEnd() ) {
178  readNext();
179 
180  if ( isEndElement() )
181  break;
182 
183  if ( isCharacters() ) {
184  // Try to switch to the mobile version, geonames
185  // lacks API for that unfortunately
186  QString url = text().toString();
187  if ( !url.contains( "m.wikipedia.org" ) ) {
188  url.replace( "wikipedia.org", "m.wikipedia.org" );
189  }
190  item->setUrl( QUrl::fromEncoded( url.toUtf8() ) );
191  }
192  }
193 }
194 
195 void GeonamesParser::readSummary( WikipediaItem *item )
196 {
197  Q_ASSERT( isStartElement()
198  && name() == "summary" );
199 
200  while ( !atEnd() ) {
201  readNext();
202 
203  if ( isEndElement() )
204  break;
205 
206  if ( isCharacters() ) {
207  item->setSummary( text().toString() );
208  }
209  }
210 }
211 
212 void GeonamesParser::readThumbnailImage( WikipediaItem *item )
213 {
214  Q_ASSERT( isStartElement()
215  && name() == "thumbnailImg" );
216 
217  while ( !atEnd() ) {
218  readNext();
219 
220  if ( isEndElement() )
221  break;
222 
223  if ( isCharacters() ) {
224  item->setThumbnailImageUrl( QUrl( text().toString() ) );
225  }
226  }
227 }
228 
229 void GeonamesParser::readRank( WikipediaItem *item )
230 {
231  Q_ASSERT( isStartElement()
232  && name() == "rank" );
233 
234  while ( !atEnd() ) {
235  readNext();
236 
237  if ( isEndElement() )
238  break;
239 
240  if ( isCharacters() ) {
241  item->setRank( text().toString().toDouble() );
242  }
243  }
244 }
QXmlStreamReader::atEnd
bool atEnd() const
Marble::GeonamesParser::GeonamesParser
GeonamesParser(MarbleWidget *widget, QList< WikipediaItem * > *list, QObject *parent)
Definition: GeonamesParser.cpp:24
QByteArray
Marble::WikipediaItem::setLongitude
void setLongitude(qreal longitude)
Definition: WikipediaItem.cpp:116
QStringRef::toString
QString toString() const
Marble::WikipediaItem::setName
void setName(const QString &name)
Definition: WikipediaItem.cpp:63
QXmlStreamReader::error
Error error() const
WikipediaItem.h
MarbleDebug.h
QObject::tr
QString tr(const char *sourceText, const char *disambiguation, int n)
QXmlStreamReader::raiseError
void raiseError(const QString &message)
Marble::MarbleWidget
A widget class that displays a view of the earth.
Definition: MarbleWidget.h:104
QObject
Marble::WikipediaItem::setThumbnailImageUrl
void setThumbnailImageUrl(const QUrl &thumbnailImageUrl)
Definition: WikipediaItem.cpp:150
QXmlStreamReader::isCharacters
bool isCharacters() const
Marble::WikipediaItem::setUrl
void setUrl(const QUrl &url)
Definition: WikipediaItem.cpp:140
GeonamesParser.h
Marble::WikipediaItem
Definition: WikipediaItem.h:29
QString
QList
MarbleGlobal.h
QXmlStreamReader::readNext
TokenType readNext()
Marble::DEG2RAD
const qreal DEG2RAD
Definition: MarbleGlobal.h:219
QXmlStreamReader::addData
void addData(const QByteArray &data)
QString::contains
bool contains(QChar ch, Qt::CaseSensitivity cs) const
QUrl
Marble::WikipediaItem::setSummary
void setSummary(const QString &summary)
Definition: WikipediaItem.cpp:160
QString::replace
QString & replace(int position, int n, QChar after)
Marble::WikipediaItem::setRank
void setRank(double rank)
Set a popularity rank.
Definition: WikipediaItem.cpp:209
QXmlStreamReader::isStartElement
bool isStartElement() const
QTest::toString
char * toString(const T &value)
QXmlStreamReader::text
QStringRef text() const
Marble::WikipediaItem::setLatitude
void setLatitude(qreal latitude)
Definition: WikipediaItem.cpp:128
Marble::GeonamesParser::read
bool read(const QByteArray &data)
Definition: GeonamesParser.cpp:33
QXmlStreamReader::name
QStringRef name() const
QUrl::fromEncoded
QUrl fromEncoded(const QByteArray &input)
QXmlStreamReader::isEndElement
bool isEndElement() const
QString::toUtf8
QByteArray toUtf8() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:13:39 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

marble

Skip menu "marble"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal