Commit aa3e0ffe authored by Friedrich W. H. Kossebau's avatar Friedrich W. H. Kossebau
Browse files

Port FoursquarePlugin from QtScript to QJson*

parent c97daf20
......@@ -11,7 +11,5 @@ set( my_SRCS
FoursquareItem.cpp
)
set( FoursquarePlugin_LIBS Qt5::Script Qt5::Svg )
marble_add_plugin( FoursquarePlugin ${my_SRCS} )
......@@ -20,9 +20,9 @@
#include "MarbleMath.h"
#include <QUrl>
#include <QScriptEngine>
#include <QScriptValue>
#include <QScriptValueIterator>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
namespace Marble
{
......@@ -71,40 +71,46 @@ void FoursquareModel::getAdditionalItems( const GeoDataLatLonAltBox& box, qint32
void FoursquareModel::parseFile( const QByteArray& file )
{
QScriptValue data;
QScriptEngine engine;
// Qt requires parentheses around JSON
data = engine.evaluate(QLatin1Char('(') + QString::fromUtf8(file) + QLatin1Char(')'));
data = data.property("response");
const QJsonDocument jsonDoc = QJsonDocument::fromJson(file);
const QJsonObject responseObject = jsonDoc.object().value(QStringLiteral("response")).toObject();
const QJsonValue venuesValue = responseObject.value(QStringLiteral("response"));
// Parse if any result exists
if ( data.property( "venues" ).isArray() ) {
QScriptValueIterator iterator( data.property( "venues" ) );
if (venuesValue.isArray()) {
// Add items to the list
QList<AbstractDataPluginItem*> items;
do {
iterator.next();
QString id = iterator.value().property( "id" ).toString();
QString name = iterator.value().property( "name" ).toString();
QString category = iterator.value().property( "categories" ).property( 0 ).property( "name" ).toString();
QString address = iterator.value().property( "location" ).property( "address" ).toString();
QString city = iterator.value().property( "location" ).property( "city" ).toString();
QString country = iterator.value().property( "location" ).property( "country" ).toString();
double latitude = iterator.value().property( "location" ).property( "lat" ).toString().toDouble();
double longitude = iterator.value().property( "location" ).property( "lng" ).toString().toDouble();
int usersCount = iterator.value().property( "stats" ).property( "usersCount" ).toInteger();
QScriptValue categoryIcon = iterator.value().property( "categories" ).property( 0 ).property( "icon" );
const QJsonArray venueArray = venuesValue.toArray();
for (int venueIndex = 0; venueIndex < venueArray.size(); ++venueIndex) {
const QJsonObject venueObject = venueArray[venueIndex].toObject();
const QJsonObject firstCategoryObject = venueObject.value(QStringLiteral("categories")).toArray().at(0).toObject();
const QString id = venueObject.value(QStringLiteral("id")).toString();
const QString name = venueObject.value(QStringLiteral("name")).toString();
const QString category = firstCategoryObject.value(QStringLiteral("name")).toString();
const QJsonObject locationObject = venueObject.value(QStringLiteral("location")).toObject();
const QString address = locationObject.value(QStringLiteral("address")).toString();
const QString city = locationObject.value(QStringLiteral("city")).toString();
const QString country = locationObject.value(QStringLiteral("country")).toString();
const double latitude = locationObject.value(QStringLiteral("lat")).toString().toDouble();
const double longitude = locationObject.value(QStringLiteral("lng")).toString().toDouble();
const int usersCount = venueObject.value(QStringLiteral("stats")).toObject().value(QStringLiteral("usersCount")).toInt();
const QJsonValue categoryIconValue = firstCategoryObject.value(QStringLiteral("icon"));
QString iconUrl;
QString largeIconUrl;
if ( categoryIcon.isValid() ) {
iconUrl = categoryIcon.property( "prefix" ).toString()
if (categoryIconValue.isObject()) {
const QJsonObject categoryIconObject = categoryIconValue.toObject();
const QString iconPrefix = categoryIconObject.value(QStringLiteral("prefix")).toString();
const QString iconName = categoryIconObject.value(QStringLiteral("name")).toString();
iconUrl = iconPrefix
+ QLatin1String("32") // That's the icon size hardcoded
+ categoryIcon.property( "name" ).toString();
largeIconUrl = categoryIcon.property( "prefix" ).toString()
+ iconName;
largeIconUrl = iconPrefix
+ QLatin1String("64") // Larger icon
+ categoryIcon.property( "name" ).toString();
+ iconName;
}
if( !itemExists( id ) ) {
......@@ -124,7 +130,6 @@ void FoursquareModel::parseFile( const QByteArray& file )
items << item;
}
}
while ( iterator.hasNext() );
addItemsToList( items );
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment