diff options
author | abcd <amos.choy@nokia.com> | 2011-10-20 20:28:15 +1000 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2011-10-25 11:21:52 +0200 |
commit | eff2b2d022e45d18ca3263dbb6cd9bceeb99f798 (patch) | |
tree | b8951527c273510035b3c876bb091cd744c2c39c /src/plugins | |
parent | ba22abc92a69b024027217c3dfcd947c0f70920b (diff) | |
download | qtlocation-eff2b2d022e45d18ca3263dbb6cd9bceeb99f798.tar.gz |
Implement uncompleted category related functions in rest plugin
Also do some refactoring for the cateogories. With the new
approach we don't have to keep traversing the category tree
to get a category's parent's or children or category itself.
Change-Id: I732a108249da178bc1d45e17ae201cbd3317b59d
Reviewed-by: Aaron McCarthy <aaron.mccarthy@nokia.com>
Diffstat (limited to 'src/plugins')
9 files changed, 97 insertions, 260 deletions
diff --git a/src/plugins/geoservices/nokia/places/places.pri b/src/plugins/geoservices/nokia/places/places.pri index ecb9827e..8c3ee909 100644 --- a/src/plugins/geoservices/nokia/places/places.pri +++ b/src/plugins/geoservices/nokia/places/places.pri @@ -26,7 +26,6 @@ HEADERS += \ places/qplacerestreply.h \ places/qplacerestmanager.h \ places/qplacesuppliersrepository.h \ - places/qplacecategorytree.h \ #misc places/unsupportedreplies.h @@ -55,8 +54,7 @@ SOURCES += \ places/qplacecategoriesrepository.cpp \ places/qplacerestreply.cpp \ places/qplacerestmanager.cpp \ - places/qplacesuppliersrepository.cpp \ - places/qplacecategorytree.cpp + places/qplacesuppliersrepository.cpp diff --git a/src/plugins/geoservices/nokia/places/qplacecategoriesreplyimpl.cpp b/src/plugins/geoservices/nokia/places/qplacecategoriesreplyimpl.cpp index a0f460c7..465e4684 100644 --- a/src/plugins/geoservices/nokia/places/qplacecategoriesreplyimpl.cpp +++ b/src/plugins/geoservices/nokia/places/qplacecategoriesreplyimpl.cpp @@ -79,7 +79,10 @@ QPlaceCategoryTree QPlaceCategoriesReplyImpl::categories() const QList<QPlaceCategory> QPlaceCategoriesReplyImpl::categoriesFlat() const { - return m_categoryTree.toList(); + QList<QPlaceCategory> categories; + foreach (const PlaceCategoryNode node, m_categoryTree.values()) + categories.append(node.category); + return categories; } void QPlaceCategoriesReplyImpl::abort() diff --git a/src/plugins/geoservices/nokia/places/qplacecategoriesrepository.cpp b/src/plugins/geoservices/nokia/places/qplacecategoriesrepository.cpp index 5c04f076..06e59d5c 100644 --- a/src/plugins/geoservices/nokia/places/qplacecategoriesrepository.cpp +++ b/src/plugins/geoservices/nokia/places/qplacecategoriesrepository.cpp @@ -118,7 +118,7 @@ QString QPlaceCategoriesRepository::getCategoryTagId(const QPlaceCategory &categ QPlaceCategory QPlaceCategoriesRepository::findCategoryById(const QString &id) { - return m_categoryTree.findCategoryById(id); + return m_categoryTree.value(id).category; } void QPlaceCategoriesRepository::replyFinished() diff --git a/src/plugins/geoservices/nokia/places/qplacecategorytree.cpp b/src/plugins/geoservices/nokia/places/qplacecategorytree.cpp deleted file mode 100644 index e8870b9d..00000000 --- a/src/plugins/geoservices/nokia/places/qplacecategorytree.cpp +++ /dev/null @@ -1,114 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** 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 "qplacecategorytree.h" - -QPlaceCategoryTree::QPlaceCategoryTree() -{ -} - -QPlaceCategoryTree::~QPlaceCategoryTree() -{ -} - -void QPlaceCategoryTree::clear() -{ - category = QPlaceCategory(); - subCategories.clear(); -} - -QPlaceCategory QPlaceCategoryTree::findCategoryById(const QString &id) const -{ - if (category.categoryId() == id) - return category; - - if (subCategories.contains(id)) - return subCategories.value(id).category; - - QHashIterator<QString, QPlaceCategoryTree> it(subCategories); - while (it.hasNext()) { - it.next(); - - QPlaceCategory cat = it.value().findCategoryById(id); - if (cat.categoryId() == id) - return cat; - } - - return QPlaceCategory(); -} - -QPlaceCategoryTree QPlaceCategoryTree::findCategoryTreeById(const QString &id) const -{ - if (category.categoryId() == id) - return *this; - - QHashIterator<QString, QPlaceCategoryTree> it(subCategories); - while (it.hasNext()) { - it.next(); - - if (it.value().category.categoryId() == id) - return it.value(); - - QPlaceCategoryTree t = it.value().findCategoryTreeById(id); - if (t.category.categoryId() == id) - return t; - } - - return QPlaceCategoryTree(); -} - -QList<QPlaceCategory> QPlaceCategoryTree::toList() const -{ - QList<QPlaceCategory> results; - - QHashIterator<QString, QPlaceCategoryTree> it(subCategories); - while (it.hasNext()) { - it.next(); - - QPlaceCategory cat = it.value().category; - if (!cat.categoryId().isEmpty()) - results.append(cat); - - results.append(it.value().toList()); - } - - return results; -} diff --git a/src/plugins/geoservices/nokia/places/qplacecategorytree.h b/src/plugins/geoservices/nokia/places/qplacecategorytree.h deleted file mode 100644 index 0d876747..00000000 --- a/src/plugins/geoservices/nokia/places/qplacecategorytree.h +++ /dev/null @@ -1,64 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** 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$ -** -****************************************************************************/ - -#ifndef QPLACECATEGORYTREE_H -#define QPLACECATEGORYTREE_H - -#include <QtCore/QHash> -#include <QtLocation/QPlaceCategory> - -class QPlaceCategoryTree -{ -public: - QPlaceCategoryTree(); - ~QPlaceCategoryTree(); - - void clear(); - - QPlaceCategory findCategoryById(const QString &id) const; - QPlaceCategoryTree findCategoryTreeById(const QString &id) const; - QList<QPlaceCategory> toList() const; - - QPlaceCategory category; - QHash<QString, QPlaceCategoryTree> subCategories; -}; - -#endif // QPLACECATEGORYTREE_H diff --git a/src/plugins/geoservices/nokia/places/qplacejsoncategoriesparser.cpp b/src/plugins/geoservices/nokia/places/qplacejsoncategoriesparser.cpp index 691c8729..cb3d5dbb 100644 --- a/src/plugins/geoservices/nokia/places/qplacejsoncategoriesparser.cpp +++ b/src/plugins/geoservices/nokia/places/qplacejsoncategoriesparser.cpp @@ -75,29 +75,42 @@ QPlaceJSonCategoriesParser::~QPlaceJSonCategoriesParser() { } -QList<QPlaceCategory> QPlaceJSonCategoriesParser::parseFlatCategoryList(const QScriptValue &categories) +QPlaceCategoryTree QPlaceJSonCategoriesParser::resultCategories() const { - return processCategories(categories).toList(); + return m_tree; } -QPlaceCategoryTree QPlaceJSonCategoriesParser::resultCategories() const +QList<QPlaceCategory> QPlaceJSonCategoriesParser::resultCategoriesFlat() const { - return m_categoryTree; + QList<QPlaceCategory> result; + foreach (const PlaceCategoryNode node, m_tree.values()) + result.append(node.category); + return result; } -QList<QPlaceCategory> QPlaceJSonCategoriesParser::resultCategoriesFlat() const +QList<QPlaceCategory> QPlaceJSonCategoriesParser::parseFlatCategoryList(const QScriptValue &categories) { - return m_categoryTree.toList(); + QPlaceCategoryTree tree; + PlaceCategoryNode rootNode; + tree.insert(QString(), rootNode); + processCategories(categories, QString(), &tree); + + QList<QPlaceCategory> result; + foreach (const PlaceCategoryNode &node, tree.values()) + result.append(node.category); + return result; } void QPlaceJSonCategoriesParser::processJSonData(const QScriptValue &sv) { - m_categoryTree.clear(); + m_tree.clear(); + PlaceCategoryNode rootNode; + m_tree.insert(QString(), rootNode); if (sv.isValid()) { QScriptValue sv2 = sv.property(place_categories_element); if (sv2.isValid()) { - m_categoryTree = processCategories(sv2); + processCategories(sv2, QString(), &m_tree); emit finished(NoError, QString()); } else { emit finished(ParsingError, QString("JSON data are invalid")); @@ -107,9 +120,10 @@ void QPlaceJSonCategoriesParser::processJSonData(const QScriptValue &sv) } } -QPlaceCategoryTree QPlaceJSonCategoriesParser::processCategories(const QScriptValue &categories) +void QPlaceJSonCategoriesParser::processCategories(const QScriptValue &categories, const QString &parentId, QPlaceCategoryTree *tree) { - QPlaceCategoryTree results; + + Q_ASSERT(tree->contains(parentId)); QScriptValue value = categories.property(place_category_element); if (value.isValid()) { @@ -119,20 +133,21 @@ QPlaceCategoryTree QPlaceJSonCategoriesParser::processCategories(const QScriptVa it.next(); // array contains count as last element if (it.name() != QLatin1String("length")) { - QPlaceCategoryTree catTree; - catTree.category = processCategory(it.value()); - if (!catTree.category.categoryId().isEmpty() && - !results.subCategories.contains(catTree.category.categoryId())) { - results.subCategories.insert(catTree.category.categoryId(), catTree); + PlaceCategoryNode catNode = processCategory(it.value(), parentId); + + if (!catNode.category.categoryId().isEmpty() && + !tree->contains(catNode.category.categoryId())) { + tree->insert(catNode.category.categoryId(), catNode); + (*tree)[parentId].childIds.append(catNode.category.categoryId()); } } } } else { - QPlaceCategoryTree catTree; - catTree.category = processCategory(value); - if (!catTree.category.categoryId().isEmpty() && - !results.subCategories.contains(catTree.category.categoryId())) { - results.subCategories.insert(catTree.category.categoryId(), catTree); + PlaceCategoryNode catNode = processCategory(value, parentId); + if (!catNode.category.categoryId().isEmpty() && + !tree->contains(catNode.category.categoryId())) { + tree->insert(catNode.category.categoryId(), catNode); + (*tree)[parentId].childIds.append(catNode.category.categoryId()); } } } @@ -144,64 +159,43 @@ QPlaceCategoryTree QPlaceJSonCategoriesParser::processCategories(const QScriptVa while (it.hasNext()) { it.next(); // array contains count as last element - if (it.name() != QLatin1String("length")) { - QPlaceCategoryTree catTree = processGroup(it.value()); - if (!results.subCategories.contains(catTree.category.categoryId())) { - results.subCategories.insert(catTree.category.categoryId(), catTree); - } else { - QHashIterator<QString, QPlaceCategoryTree> treeIt(catTree.subCategories); - while (treeIt.hasNext()) { - treeIt.next(); - - results.subCategories[catTree.category.categoryId()].subCategories.insert(treeIt.key(), treeIt.value()); - } - } - } + if (it.name() != QLatin1String("length")) + processGroup(it.value(), parentId, tree); } } else { - QPlaceCategoryTree catTree = processGroup(value); - if (!results.subCategories.contains(catTree.category.categoryId())) { - results.subCategories.insert(catTree.category.categoryId(), catTree); - } else { - QHashIterator<QString, QPlaceCategoryTree> treeIt(catTree.subCategories); - while (treeIt.hasNext()) { - treeIt.next(); - - results.subCategories[catTree.category.categoryId()].subCategories.insert(treeIt.key(), treeIt.value()); - } - } + processGroup(value, parentId, tree); } } - - return results; } -QPlaceCategory QPlaceJSonCategoriesParser::processCategory(const QScriptValue &categoryValue) +PlaceCategoryNode QPlaceJSonCategoriesParser::processCategory(const QScriptValue &categoryValue, const QString &parentId) { - QPlaceCategory category; + PlaceCategoryNode categoryNode; + categoryNode.parentId = parentId; QScriptValue value = categoryValue.property(place_category_id_element); if (value.isValid() && !value.toString().isEmpty()) { - category.setCategoryId(value.toString()); + categoryNode.category.setCategoryId(value.toString()); value = categoryValue.property(place_category_name_element); if (value.isValid() && !value.toString().isEmpty()) { - category.setName(value.toString()); + categoryNode.category.setName(value.toString()); } } - return category; + return categoryNode; } -QPlaceCategoryTree QPlaceJSonCategoriesParser::processGroup(const QScriptValue &group) +void QPlaceJSonCategoriesParser::processGroup(const QScriptValue &group, const QString &parentId, QPlaceCategoryTree *tree) { QScriptValue value = group.property(place_groupingcategory_element); if (!value.isValid()) - return QPlaceCategoryTree(); - - QPlaceCategoryTree results; - results.category = processCategory(value); - if (results.category.categoryId().isEmpty()) - return QPlaceCategoryTree(); + return; - results.subCategories = processCategories(group).subCategories; + PlaceCategoryNode catNode = processCategory(value, parentId); + if (catNode.category.categoryId().isEmpty()) { + return; + } else { + tree->insert(catNode.category.categoryId(), catNode); + (*tree)[parentId].childIds.append(catNode.category.categoryId()); + } - return results; + processCategories(group, catNode.category.categoryId(), tree); } diff --git a/src/plugins/geoservices/nokia/places/qplacejsoncategoriesparser.h b/src/plugins/geoservices/nokia/places/qplacejsoncategoriesparser.h index cd938ac2..5c02d875 100644 --- a/src/plugins/geoservices/nokia/places/qplacejsoncategoriesparser.h +++ b/src/plugins/geoservices/nokia/places/qplacejsoncategoriesparser.h @@ -55,13 +55,22 @@ #include <qplacecategory.h> #include "qplacejsonparser_p.h" -#include "qplacecategorytree.h" class QScriptEngine; class QScriptValue; QT_BEGIN_NAMESPACE +struct PlaceCategoryNode +{ +public: + QString parentId; + QStringList childIds; + QPlaceCategory category; +}; + +typedef QMap<QString, PlaceCategoryNode> QPlaceCategoryTree; + class QPlaceJSonCategoriesParser : public QPlaceJSonParser { Q_OBJECT @@ -78,12 +87,12 @@ public: private: void processJSonData(const QScriptValue &sv); - static QPlaceCategoryTree processCategories(const QScriptValue &categories); - static QPlaceCategoryTree processGroup(const QScriptValue &group); - static QPlaceCategory processCategory(const QScriptValue &categoryValue); + static void processCategories(const QScriptValue &categories, const QString &parentId, QPlaceCategoryTree *tree); + static void processGroup(const QScriptValue &group, const QString &parentId, QPlaceCategoryTree *tree); + static PlaceCategoryNode processCategory(const QScriptValue &categoryValue, const QString &parentId); private: - QPlaceCategoryTree m_categoryTree; + QPlaceCategoryTree m_tree; }; QT_END_NAMESPACE diff --git a/src/plugins/geoservices/nokia/qplacemanagerengine_nokia.cpp b/src/plugins/geoservices/nokia/qplacemanagerengine_nokia.cpp index e074c157..57035515 100644 --- a/src/plugins/geoservices/nokia/qplacemanagerengine_nokia.cpp +++ b/src/plugins/geoservices/nokia/qplacemanagerengine_nokia.cpp @@ -276,20 +276,31 @@ QPlaceReply *QPlaceManagerEngineNokia::initializeCategories() return reply; } -QList<QPlaceCategory> QPlaceManagerEngineNokia::childCategories(const QString &parentId) const +QString QPlaceManagerEngineNokia::parentCategoryId(const QString &categoryId) const { QPlaceCategoryTree tree = QPlaceCategoriesRepository::instance()->categories(); - tree = tree.findCategoryTreeById(parentId); - - QList<QPlaceCategory> results; + return tree.value(categoryId).parentId; +} - QHashIterator<QString, QPlaceCategoryTree> it(tree.subCategories); - while (it.hasNext()) { - it.next(); +QStringList QPlaceManagerEngineNokia::childrenCategoryIds(const QString &categoryId) const +{ + QPlaceCategoryTree tree = QPlaceCategoriesRepository::instance()->categories(); + return tree.value(categoryId).childIds; +} - results.append(it.value().category); - } +QPlaceCategory QPlaceManagerEngineNokia::category(const QString &categoryId) const +{ + QPlaceCategoryTree tree = QPlaceCategoriesRepository::instance()->categories(); + return tree.value(categoryId).category; +} +QList<QPlaceCategory> QPlaceManagerEngineNokia::childCategories(const QString &parentId) const +{ + QList<QPlaceCategory> results; + QPlaceCategoryTree tree = QPlaceCategoriesRepository::instance()->categories(); + PlaceCategoryNode node = tree.value(parentId); + foreach (const QString &childId, node.childIds) + results.append(tree.value(childId).category); return results; } diff --git a/src/plugins/geoservices/nokia/qplacemanagerengine_nokia.h b/src/plugins/geoservices/nokia/qplacemanagerengine_nokia.h index 06ea1fe1..edd29bf0 100644 --- a/src/plugins/geoservices/nokia/qplacemanagerengine_nokia.h +++ b/src/plugins/geoservices/nokia/qplacemanagerengine_nokia.h @@ -82,9 +82,9 @@ public: QPlaceIdReply *removeCategory(const QString &categoryId); QPlaceReply *initializeCategories(); - QString parentCategoryId(const QString &/*categoryId*/) const {return QString();} - virtual QStringList childrenCategoryIds(const QString &/*categoryId*/) const {return QStringList();} - virtual QPlaceCategory category(const QString &/*categoryId*/) const {return QPlaceCategory();} + QString parentCategoryId(const QString &categoryId) const; + QStringList childrenCategoryIds(const QString &categoryId) const; + QPlaceCategory category(const QString &categoryId) const; QList<QPlaceCategory> childCategories(const QString &parentId) const; QLocale locale() const; |