diff options
author | Aaron McCarthy <aaron.mccarthy@nokia.com> | 2012-01-19 15:49:13 +1000 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-02-09 06:02:00 +0100 |
commit | d24206cf6d87966b32c8a1186c9fa682d66b8ee9 (patch) | |
tree | e860f08099c77f7a5b06f89a2931f6b0932f6208 /src/plugins/geoservices/nokia/placesv2/jsonparserhelpers.cpp | |
parent | 4f158d2920736553cc214a5ef4690e8e89a79324 (diff) | |
download | qtlocation-d24206cf6d87966b32c8a1186c9fa682d66b8ee9.tar.gz |
Add support for the new Nokia REST API to nokia plugin.
Support is added in such a way that both the v1 and v2 REST APIs are
supported by the plugin. By default the v1 API is used. The
application developer can explicitly select v2 by setting the
places.api_version plugin parameter to 2. At some point the default
will change to v2 and support for v1 and places.api_version may be
dropped entirely.
The new REST API is documented at
http://api.places.lbs.maps.nokia.com/places/static/doc/index.html
Change-Id: I643d10202ab387346fa4658f096a6ddbb5e80e34
Reviewed-by: Aaron McCarthy <aaron.mccarthy@nokia.com>
Diffstat (limited to 'src/plugins/geoservices/nokia/placesv2/jsonparserhelpers.cpp')
-rw-r--r-- | src/plugins/geoservices/nokia/placesv2/jsonparserhelpers.cpp | 219 |
1 files changed, 219 insertions, 0 deletions
diff --git a/src/plugins/geoservices/nokia/placesv2/jsonparserhelpers.cpp b/src/plugins/geoservices/nokia/placesv2/jsonparserhelpers.cpp new file mode 100644 index 00000000..a201bb73 --- /dev/null +++ b/src/plugins/geoservices/nokia/placesv2/jsonparserhelpers.cpp @@ -0,0 +1,219 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the QtLocation module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "jsonparserhelpers.h" + +#include <QtCore/QCoreApplication> +#include <QtCore/QDateTime> +#include <QtCore/QJsonObject> +#include <QtCore/QJsonArray> +#include <QtCore/QVariantMap> +#include <QtLocation/QGeoCoordinate> +#include <QtLocation/QPlaceImage> +#include <QtLocation/QPlaceReview> +#include <QtLocation/QPlaceEditorial> +#include <QtLocation/QPlaceUser> +#include <QtLocation/QPlaceContactDetail> +#include <QtLocation/QPlaceCategory> + +QT_BEGIN_NAMESPACE + +QGeoCoordinate parseCoordinate(const QJsonArray &coordinateArray) +{ + return QGeoCoordinate(coordinateArray.at(0).toDouble(), coordinateArray.at(1).toDouble()); +} + +QPlaceSupplier parseSupplier(const QJsonObject &supplierObject) +{ + QPlaceSupplier supplier; + supplier.setName(supplierObject.value(QLatin1String("title")).toString()); + supplier.setUrl(supplierObject.value(QLatin1String("href")).toString()); + + QVariantMap parameters; + parameters.insert(QPlaceIcon::SingleUrl, + QUrl(supplierObject.value(QLatin1String("icon")).toString())); + QPlaceIcon icon; + icon.setParameters(parameters); + supplier.setIcon(icon); + + return supplier; +} + +QPlaceCategory parseCategory(const QJsonObject &categoryObject) +{ + QPlaceCategory category; + + category.setName(categoryObject.value(QLatin1String("title")).toString()); + + const QUrl href(categoryObject.value(QLatin1String("href")).toString()); + category.setCategoryId(href.path().mid(34)); + + QVariantMap parameters; + parameters.insert(QPlaceIcon::SingleUrl, + QUrl(categoryObject.value(QLatin1String("icon")).toString())); + QPlaceIcon icon; + icon.setParameters(parameters); + category.setIcon(icon); + + return category; +} + +QList<QPlaceCategory> parseCategories(const QJsonArray &categoryArray) +{ + QList<QPlaceCategory> categoryList; + for (int i = 0; i < categoryArray.count(); ++i) + categoryList.append(parseCategory(categoryArray.at(i).toObject())); + + return categoryList; +} + +QList<QPlaceContactDetail> parseContactDetails(const QJsonArray &contacts) +{ + QList<QPlaceContactDetail> contactDetails; + + for (int i = 0; i < contacts.count(); ++i) { + QJsonObject contact = contacts.at(i).toObject(); + + QPlaceContactDetail detail; + detail.setLabel(contact.value(QLatin1String("label")).toString()); + detail.setValue(contact.value(QLatin1String("value")).toString()); + + contactDetails.append(detail); + } + + return contactDetails; +} + +QPlaceImage parseImage(const QJsonObject &imageObject) +{ + QPlaceImage image; + + image.setAttribution(imageObject.value(QLatin1String("attribution")).toString()); + image.setUrl(imageObject.value(QLatin1String("src")).toString()); + image.setSupplier(parseSupplier(imageObject.value(QLatin1String("supplier")).toObject())); + + return image; +} + +QPlaceReview parseReview(const QJsonObject &reviewObject) +{ + QPlaceReview review; + + review.setDateTime(QDateTime::fromString(reviewObject.value(QLatin1String("date")).toString())); + + if (reviewObject.contains(QLatin1String("title"))) + review.setTitle(reviewObject.value(QLatin1String("title")).toString()); + + if (reviewObject.contains(QLatin1String("rating"))) + review.setRating(reviewObject.value(QLatin1String("rating")).toDouble()); + + review.setText(reviewObject.value(QLatin1String("description")).toString()); + + QJsonObject userObject = reviewObject.value(QLatin1String("user")).toObject(); + + QPlaceUser user; + user.setUserId(userObject.value(QLatin1String("id")).toString()); + user.setName(userObject.value(QLatin1String("title")).toString()); + review.setUser(user); + + review.setAttribution(reviewObject.value(QLatin1String("attribution")).toString()); + + review.setLanguage(reviewObject.value(QLatin1String("language")).toString()); + + review.setSupplier(parseSupplier(reviewObject.value(QLatin1String("supplier")).toObject())); + + //if (reviewObject.contains(QLatin1String("via"))) { + // QJsonObject viaObject = reviewObject.value(QLatin1String("via")).toObject(); + //} + + return review; +} + +QPlaceEditorial parseEditorial(const QJsonObject &editorialObject) +{ + QPlaceEditorial editorial; + + editorial.setAttribution(editorialObject.value(QLatin1String("attribution")).toString()); + + //if (editorialObject.contains(QLatin1String("via"))) { + // QJsonObject viaObject = editorialObject.value(QLatin1String("via")).toObject(); + //} + + editorial.setSupplier(parseSupplier(editorialObject.value(QLatin1String("supplier")).toObject())); + editorial.setLanguage(editorialObject.value(QLatin1String("language")).toString()); + editorial.setText(editorialObject.value(QLatin1String("description")).toString()); + + return editorial; +} + +void parseCollection(QPlaceContent::Type type, const QJsonObject &object, + QPlaceContent::Collection *collection, int *totalCount) +{ + if (totalCount) + *totalCount = object.value(QLatin1String("available")).toDouble(); + + int offset = 0; + if (object.contains(QLatin1String("offset"))) + offset = object.value(QLatin1String("offset")).toDouble(); + + if (collection) { + QJsonArray items = object.value(QLatin1String("items")).toArray(); + for (int i = 0; i < items.count(); ++i) { + QJsonObject itemObject = items.at(i).toObject(); + + switch (type) { + case QPlaceContent::ImageType: + collection->insert(offset + i, parseImage(itemObject)); + break; + case QPlaceContent::ReviewType: + collection->insert(offset + i, parseReview(itemObject)); + break; + case QPlaceContent::EditorialType: + collection->insert(offset + i, parseEditorial(itemObject)); + break; + case QPlaceContent::NoType: + break; + } + } + } +} + +QT_END_NAMESPACE |