• 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
  • opencaching
OpenCachingModel.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 2011 Daniel Marth <danielmarth@gmx.at>
9 //
10 
11 #include "OpenCachingModel.h"
12 #include "OpenCachingItem.h"
13 
14 #include "MarbleGlobal.h"
15 #include "MarbleModel.h"
16 #include "GeoDataCoordinates.h"
17 #include "GeoDataLatLonAltBox.h"
18 #include "MarbleDebug.h"
19 #include "OpenCachingCache.h"
20 
21 #include <QDebug>
22 #include <QString>
23 #include <QUrl>
24 #include <QXmlStreamReader>
25 
26 namespace Marble {
27 
28 class OpenCachingModelPrivate
29 {
30 public:
31  QHash<QString, QVariant> parseCache( QXmlStreamReader& reader );
32  QHash<QString, QVariant> parseLogEntry( QXmlStreamReader& reader );
33  QHash<QString, QVariant> parseDescription( QXmlStreamReader& reader );
34 };
35 
36 QHash<QString, QVariant> OpenCachingModelPrivate::parseCache( QXmlStreamReader& reader )
37 {
38  QHash<QString, QVariant> cache;
39  while ( !reader.atEnd() ) {
40  if ( reader.isStartElement() && reader.name() != "cache" ) {
41  if( reader.name() == "id" ) {
42  cache["id"] = reader.attributes().value("id").toString();
43  }
44  else if( reader.name() != "attributes" && reader.name() != "attribute" ) {
45  cache[reader.name().toString()] = reader.readElementText();
46  }
47  }
48  else if( reader.isEndElement() && reader.name() == "cache" ) {
49  return cache;
50  }
51  reader.readNext();
52  }
53  return QHash<QString, QVariant>();
54 }
55 
56 QHash<QString, QVariant> OpenCachingModelPrivate::parseLogEntry( QXmlStreamReader& reader )
57 {
58  QHash<QString, QVariant> cacheLogEntry;
59  while ( !reader.atEnd() ) {
60  if ( reader.isStartElement() && reader.name() != "cachelog" ) {
61  if( reader.name() == "cacheid" ) {
62  cacheLogEntry["cacheid"] = reader.attributes().value( "id" ).toString();
63  }
64  else {
65  cacheLogEntry[reader.name().toString()] = reader.readElementText();
66  }
67  }
68  else if( reader.isEndElement() && reader.name() == "cachelog" ) {
69  return cacheLogEntry;
70  }
71  reader.readNext();
72  }
73  return QHash<QString, QVariant>();
74 }
75 
76 QHash<QString, QVariant> OpenCachingModelPrivate::parseDescription( QXmlStreamReader& reader )
77 {
78  QHash<QString, QVariant> cacheDesc;
79  while ( !reader.atEnd() ) {
80  if ( reader.isStartElement() && reader.name() != "cachedesc" ) {
81  if( reader.name() == "cacheid" ) {
82  cacheDesc["cacheid"] = reader.attributes().value( "id" ).toString();
83  }
84  else {
85  cacheDesc[reader.name().toString()] = reader.readElementText();
86  }
87  }
88  else if( reader.isEndElement() && reader.name() == "cachedesc" ) {
89  return cacheDesc;
90  }
91  reader.readNext();
92  }
93  return QHash<QString, QVariant>();
94 }
95 
96 OpenCachingModel::OpenCachingModel( const PluginManager *pluginManager, QObject *parent )
97  : AbstractDataPluginModel( "opencaching", pluginManager, parent ),
98  m_numResults( numberOfItemsOnScreen ),
99  m_maxDistance( 20 ),
100  m_minDifficulty( 0.0 ),
101  m_maxDifficulty( 5.0 ),
102  m_startDate( QDateTime::fromString( "2006-01-01", "yyyy-MM-dd" ) ),
103  m_endDate( QDateTime::currentDateTime() ),
104  d( new OpenCachingModelPrivate )
105 {
106 }
107 
108 OpenCachingModel::~OpenCachingModel()
109 {
110 }
111 
112 void OpenCachingModel::setNumResults( int numResults )
113 {
114  m_numResults = numResults;
115 }
116 
117 void OpenCachingModel::setMaxDistance( int maxDistance )
118 {
119  m_maxDistance = maxDistance;
120 }
121 
122 void OpenCachingModel::setMinDifficulty( double minDifficulty )
123 {
124  m_minDifficulty = minDifficulty;
125 }
126 
127 void OpenCachingModel::setMaxDifficulty( double maxDifficulty )
128 {
129  m_maxDifficulty = maxDifficulty;
130 }
131 
132 void OpenCachingModel::setStartDate( const QDateTime& startDate )
133 {
134  m_startDate = startDate;
135 }
136 
137 void OpenCachingModel::setEndDate( const QDateTime& endDate )
138 {
139  m_endDate = endDate;
140 }
141 
142 void OpenCachingModel::getAdditionalItems( const GeoDataLatLonAltBox& box, const MarbleModel *model, qint32 number )
143 {
144  Q_UNUSED( number );
145 
146  if( model->planetId() != "earth" ) {
147  return;
148  }
149 
150  // http://www.opencaching.de/doc/xml/xml11.htm
151  QString openCachingUrl( "http://www.opencaching.de/xml/ocxml11.php" );
152  openCachingUrl += "?modifiedsince=" + m_startDate.toString( "yyyyMMddhhmmss" );
153  openCachingUrl += "&cache=1&cachedesc=1&picture=0&cachelog=1&removedobject=0";
154  openCachingUrl += "&lat=" + QString::number( box.center().latitude() * RAD2DEG );
155  openCachingUrl += "&lon=" + QString::number( box.center().longitude() * RAD2DEG );
156  openCachingUrl += "&distance=" + QString::number( m_maxDistance );
157  openCachingUrl += "&charset=utf-8&cdata=0&session=0&zip=0";
158  downloadDescriptionFile( QUrl( openCachingUrl ) );
159 }
160 
161 void OpenCachingModel::parseFile( const QByteArray& file )
162 {
163  QXmlStreamReader reader( file );
164  QXmlStreamReader::TokenType token;
165  QHash<int, OpenCachingCache> caches;
166  QHash<int, QHash<QString, OpenCachingCacheDescription> > descriptions;
167  QHash<int, OpenCachingCacheLog> logs;
168 
169  while( !reader.atEnd() && !reader.hasError() ) {
170  token = reader.readNext();
171  if( token == QXmlStreamReader::StartDocument ) {
172  continue;
173  }
174  if( token == QXmlStreamReader::StartElement ) {
175  if( reader.name() == "cache" ) {
176  OpenCachingCache cache = d->parseCache( reader );
177  caches[cache.id()] = cache;
178  }
179  else if( reader.name() == "cachedesc" ) {
180  OpenCachingCacheDescription description = d->parseDescription( reader );
181  descriptions[description.cacheId()][description.language()] = description;
182  }
183  else if( reader.name() == "cachelog" ) {
184  OpenCachingCacheLogEntry logEntry = d->parseLogEntry( reader );
185  logs[logEntry.cacheId()].addLogEntry( logEntry );
186  }
187  }
188  }
189 
190  QHash<int, OpenCachingCache>::iterator itpoint = caches.begin();
191  QHash<int, OpenCachingCache>::iterator const endpoint = caches.end();
192  for (; itpoint != endpoint; ++itpoint ) {
193  if( caches[itpoint.key()].difficulty() >= m_minDifficulty &&
194  caches[itpoint.key()].difficulty() <= m_maxDifficulty )
195  {
196  caches[itpoint.key()].setDescription( descriptions[itpoint.key()] );
197  caches[itpoint.key()].setLog( logs[itpoint.key()] );
198  addItemToList( new OpenCachingItem( caches[itpoint.key()], this ) );
199  }
200  }
201 }
202 
203 }
204 
205 #include "OpenCachingModel.moc"
QXmlStreamReader::atEnd
bool atEnd() const
GeoDataCoordinates.h
Marble::RAD2DEG
const qreal RAD2DEG
Definition: MarbleGlobal.h:220
QDateTime::toString
QString toString(Qt::DateFormat format) const
Marble::PluginManager
The class that handles Marble's plugins.
Definition: PluginManager.h:45
Marble::OpenCachingCacheDescription
Stores the description of a cache in a single language.
Definition: OpenCachingCacheDescription.h:25
Marble::OpenCachingModel::OpenCachingModel
OpenCachingModel(const PluginManager *pluginManager, QObject *parent=0)
Definition: OpenCachingModel.cpp:96
QByteArray
Marble::OpenCachingCache
Contains all information about a cache, including logs and descriptions in all available languages...
Definition: OpenCachingCache.h:27
MarbleModel.h
This file contains the headers for MarbleModel.
QStringRef::toString
QString toString() const
Marble::MarbleModel::planetId
QString planetId() const
Definition: MarbleModel.cpp:532
Marble::AbstractDataPluginModel
An abstract data model (not based on QAbstractModel) for a AbstractDataPlugin.
Definition: AbstractDataPluginModel.h:45
QXmlStreamAttributes::value
QStringRef value(const QString &namespaceUri, const QString &name) const
Marble::GeoDataCoordinates::latitude
qreal latitude(GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian) const
retrieves the latitude of the GeoDataCoordinates object use the unit parameter to switch between Radi...
Definition: GeoDataCoordinates.cpp:751
Marble::OpenCachingModel::getAdditionalItems
virtual void getAdditionalItems(const GeoDataLatLonAltBox &box, const MarbleModel *model, qint32 number=10)
Generates the download url for the description file from the web service depending on the box surroun...
Definition: OpenCachingModel.cpp:142
MarbleDebug.h
Marble::OpenCachingItem
Item representing a single cache.
Definition: OpenCachingItem.h:33
QHash::iterator
OpenCachingModel.h
Marble::AbstractDataPluginModel::downloadDescriptionFile
void downloadDescriptionFile(const QUrl &url)
Download the description file from the url.
Definition: AbstractDataPluginModel.cpp:392
Marble::OpenCachingModel::setMinDifficulty
void setMinDifficulty(double minDifficulty)
Definition: OpenCachingModel.cpp:122
QString::number
QString number(int n, int base)
Marble::OpenCachingCacheDescription::language
const QString & language() const
Definition: OpenCachingCacheDescription.cpp:41
QXmlStreamReader::readElementText
QString readElementText(ReadElementTextBehaviour behaviour)
QHash< QString, QVariant >
QObject
Marble::OpenCachingModel::setMaxDistance
void setMaxDistance(int maxDistance)
Definition: OpenCachingModel.cpp:117
QHash::begin
iterator begin()
Marble::OpenCachingModel::setNumResults
void setNumResults(int numResults)
Definition: OpenCachingModel.cpp:112
Marble::OpenCachingModel::setStartDate
void setStartDate(const QDateTime &startDate)
Definition: OpenCachingModel.cpp:132
OpenCachingItem.h
QString
MarbleGlobal.h
QXmlStreamReader::readNext
TokenType readNext()
Marble::OpenCachingModel::setEndDate
void setEndDate(const QDateTime &endDate)
Definition: OpenCachingModel.cpp:137
Marble::OpenCachingCacheLogEntry
Single log entry for a cache.
Definition: OpenCachingCacheLogEntry.h:25
Marble::GeoDataCoordinates::longitude
qreal longitude(GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian) const
retrieves the longitude of the GeoDataCoordinates object use the unit parameter to switch between Rad...
Definition: GeoDataCoordinates.cpp:739
QUrl
Marble::GeoDataLatLonAltBox::center
virtual GeoDataCoordinates center() const
returns the center of this box
Definition: GeoDataLatLonAltBox.cpp:151
QXmlStreamReader
Marble::OpenCachingModel::~OpenCachingModel
~OpenCachingModel()
Definition: OpenCachingModel.cpp:108
Marble::MarbleModel
The data model (not based on QAbstractModel) for a MarbleWidget.
Definition: MarbleModel.h:97
QXmlStreamReader::isStartElement
bool isStartElement() const
Marble::OpenCachingCacheLogEntry::cacheId
int cacheId() const
Definition: OpenCachingCacheLogEntry.cpp:32
QXmlStreamReader::hasError
bool hasError() const
GeoDataLatLonAltBox.h
Marble::OpenCachingModel::setMaxDifficulty
void setMaxDifficulty(double maxDifficulty)
Definition: OpenCachingModel.cpp:127
QXmlStreamReader::attributes
QXmlStreamAttributes attributes() const
Marble::AbstractDataPluginModel::addItemToList
void addItemToList(AbstractDataPluginItem *item)
Convenience method to add one item to the list.
Definition: AbstractDataPluginModel.cpp:403
QHash::end
iterator end()
Marble::OpenCachingModel::parseFile
void parseFile(const QByteArray &file)
Parses the file which getAdditionalItems downloads and prepares the data for usage.
Definition: OpenCachingModel.cpp:161
OpenCachingCache.h
QXmlStreamReader::name
QStringRef name() const
Marble::OpenCachingCacheDescription::cacheId
unsigned long long cacheId() const
Definition: OpenCachingCacheDescription.cpp:31
Marble::GeoDataLatLonAltBox
A class that defines a 3D bounding box for geographic data.
Definition: GeoDataLatLonAltBox.h:49
Marble::OpenCachingCache::id
unsigned long long id() const
Definition: OpenCachingCache.cpp:39
QDateTime
QXmlStreamReader::isEndElement
bool isEndElement() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:13:41 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