1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtLocation module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL3$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or later as published by the Free
28 ** Software Foundation and appearing in the file LICENSE.GPL included in
29 ** the packaging of this file. Please review the following information to
30 ** ensure the GNU General Public License version 2.0 requirements will be
31 ** met: http://www.gnu.org/licenses/gpl-2.0.html.
32 **
33 ** $QT_END_LICENSE$
34 **
35 ****************************************************************************/
36 
37 #include "qgeoserviceproviderplugin_nokia.h"
38 
39 #include "qgeocodingmanagerengine_nokia.h"
40 #include "qgeoroutingmanagerengine_nokia.h"
41 #include "qgeotiledmappingmanagerengine_nokia.h"
42 #include "qplacemanagerengine_nokiav2.h"
43 #include "qgeointrinsicnetworkaccessmanager.h"
44 #include "qgeoerror_messages.h"
45 
46 #include <QtPlugin>
47 #include <QNetworkProxy>
48 #include <QCoreApplication>
49 
50 QT_BEGIN_NAMESPACE
51 
52 namespace
53 {
isValidParameter(const QString & param)54     bool isValidParameter(const QString &param)
55     {
56         if (param.isEmpty())
57             return false;
58 
59         if (param.length() > 512)
60             return false;
61 
62         foreach (QChar c, param) {
63             if (!c.isLetterOrNumber() && c.toLatin1() != '%' && c.toLatin1() != '-' &&
64                 c.toLatin1() != '+' && c.toLatin1() != '_') {
65                 return false;
66             }
67         }
68         return true;
69     }
70 
tryGetNetworkAccessManager(const QVariantMap & parameters)71     QGeoNetworkAccessManager *tryGetNetworkAccessManager(const QVariantMap &parameters)
72     {
73         return static_cast<QGeoNetworkAccessManager *>(qvariant_cast<void *>(parameters.value(QStringLiteral("nam"))));
74     }
75 
checkUsageTerms(const QVariantMap & parameters,QGeoServiceProvider::Error * error,QString * errorString)76     void checkUsageTerms(const QVariantMap &parameters, QGeoServiceProvider::Error *error, QString *errorString)
77     {
78         QString appId, token;
79 
80         appId = parameters.value(QStringLiteral("here.app_id")).toString();
81         token = parameters.value(QStringLiteral("here.token")).toString();
82 
83         if (isValidParameter(appId) && isValidParameter(token))
84              return;
85         else if (!isValidParameter(appId))
86             qWarning() << "Invalid here.app_id";
87         else
88             qWarning() << "Invalid here.token";
89 
90         if (parameters.contains(QStringLiteral("app_id")) || parameters.contains(QStringLiteral("token")))
91             qWarning() << QStringLiteral("Please prefix 'app_id' and 'token' with prefix 'here' (e.g.: 'here.app_id')");
92 
93         *error = QGeoServiceProvider::MissingRequiredParameterError;
94         *errorString = QCoreApplication::translate(NOKIA_PLUGIN_CONTEXT_NAME, MISSED_CREDENTIALS);
95     }
96 
97     template<class TInstance>
CreateInstanceOf(const QVariantMap & parameters,QGeoServiceProvider::Error * error,QString * errorString)98     TInstance * CreateInstanceOf(const QVariantMap &parameters, QGeoServiceProvider::Error *error, QString *errorString)
99     {
100         checkUsageTerms(parameters, error, errorString);
101 
102         if (*error != QGeoServiceProvider::NoError)
103             return 0;
104 
105         QGeoNetworkAccessManager *networkManager = tryGetNetworkAccessManager(parameters);
106         if (!networkManager)
107             networkManager = new QGeoIntrinsicNetworkAccessManager(parameters);
108 
109         return new TInstance(networkManager, parameters, error, errorString);
110     }
111 }
112 
QGeoServiceProviderFactoryNokia()113 QGeoServiceProviderFactoryNokia::QGeoServiceProviderFactoryNokia()
114 {
115 }
116 
createGeocodingManagerEngine(const QVariantMap & parameters,QGeoServiceProvider::Error * error,QString * errorString) const117 QGeoCodingManagerEngine *QGeoServiceProviderFactoryNokia::createGeocodingManagerEngine(
118         const QVariantMap &parameters,
119         QGeoServiceProvider::Error *error,
120         QString *errorString) const
121 {
122     return CreateInstanceOf<QGeoCodingManagerEngineNokia>(parameters, error, errorString);
123 }
124 
createMappingManagerEngine(const QVariantMap & parameters,QGeoServiceProvider::Error * error,QString * errorString) const125 QGeoMappingManagerEngine *QGeoServiceProviderFactoryNokia::createMappingManagerEngine(
126         const QVariantMap &parameters,
127         QGeoServiceProvider::Error *error,
128         QString *errorString) const
129 {
130     return CreateInstanceOf<QGeoTiledMappingManagerEngineNokia>(parameters, error, errorString);
131 }
132 
createRoutingManagerEngine(const QVariantMap & parameters,QGeoServiceProvider::Error * error,QString * errorString) const133 QGeoRoutingManagerEngine *QGeoServiceProviderFactoryNokia::createRoutingManagerEngine(
134         const QVariantMap &parameters,
135         QGeoServiceProvider::Error *error,
136         QString *errorString) const
137 {
138     return CreateInstanceOf<QGeoRoutingManagerEngineNokia>(parameters, error, errorString);
139 }
140 
createPlaceManagerEngine(const QVariantMap & parameters,QGeoServiceProvider::Error * error,QString * errorString) const141 QPlaceManagerEngine *QGeoServiceProviderFactoryNokia::createPlaceManagerEngine(
142         const QVariantMap &parameters,
143         QGeoServiceProvider::Error *error,
144         QString *errorString) const
145 {
146     return CreateInstanceOf<QPlaceManagerEngineNokiaV2>(parameters, error, errorString);
147 }
148 
149 QT_END_NAMESPACE
150