QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsvectortileconnection.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectortileconnection.cpp
3  ---------------------
4  begin : March 2020
5  copyright : (C) 2020 by Martin Dobias
6  email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
17 
18 #include "qgslogger.h"
19 #include "qgsdatasourceuri.h"
20 #include "qgssettings.h"
21 #include "qgshttpheaders.h"
22 
23 #include <QFileInfo>
24 
26 
27 QString QgsVectorTileProviderConnection::encodedUri( const QgsVectorTileProviderConnection::Data &conn )
28 {
29  QgsDataSourceUri uri;
30 
31  const QFileInfo info( conn.url );
32  QString suffix = info.suffix().toLower();
33  if ( suffix.startsWith( QLatin1String( "mbtiles" ) ) )
34  {
35  uri.setParam( QStringLiteral( "type" ), QStringLiteral( "mbtiles" ) );
36  }
37  else
38  {
39  uri.setParam( QStringLiteral( "type" ), QStringLiteral( "xyz" ) );
40  }
41 
42  uri.setParam( QStringLiteral( "url" ), conn.url );
43  if ( conn.zMin != -1 )
44  uri.setParam( QStringLiteral( "zmin" ), QString::number( conn.zMin ) );
45  if ( conn.zMax != -1 )
46  uri.setParam( QStringLiteral( "zmax" ), QString::number( conn.zMax ) );
47  if ( !conn.authCfg.isEmpty() )
48  uri.setAuthConfigId( conn.authCfg );
49  if ( !conn.username.isEmpty() )
50  uri.setUsername( conn.username );
51  if ( !conn.password.isEmpty() )
52  uri.setPassword( conn.password );
53  if ( !conn.styleUrl.isEmpty() )
54  uri.setParam( QStringLiteral( "styleUrl" ), conn.styleUrl );
55 
56  uri.setHttpHeaders( conn.httpHeaders );
57 
58  switch ( conn.serviceType )
59  {
60  case Generic:
61  break;
62 
63  case ArcgisVectorTileService:
64  uri.setParam( QStringLiteral( "serviceType" ), QStringLiteral( "arcgis" ) );
65  break;
66  }
67 
68  return uri.encodedUri();
69 }
70 
71 QgsVectorTileProviderConnection::Data QgsVectorTileProviderConnection::decodedUri( const QString &uri )
72 {
73  QgsDataSourceUri dsUri;
74  dsUri.setEncodedUri( uri );
75 
76  QgsVectorTileProviderConnection::Data conn;
77  conn.url = dsUri.param( QStringLiteral( "url" ) );
78  conn.zMin = dsUri.hasParam( QStringLiteral( "zmin" ) ) ? dsUri.param( QStringLiteral( "zmin" ) ).toInt() : -1;
79  conn.zMax = dsUri.hasParam( QStringLiteral( "zmax" ) ) ? dsUri.param( QStringLiteral( "zmax" ) ).toInt() : -1;
80  conn.authCfg = dsUri.authConfigId();
81  conn.username = dsUri.username();
82  conn.password = dsUri.password();
83  conn.styleUrl = dsUri.param( QStringLiteral( "styleUrl" ) );
84 
85  conn.httpHeaders = dsUri.httpHeaders();
86 
87  if ( dsUri.hasParam( QStringLiteral( "serviceType" ) ) )
88  {
89  if ( dsUri.param( QStringLiteral( "serviceType" ) ) == QLatin1String( "arcgis" ) )
90  conn.serviceType = ArcgisVectorTileService;
91  }
92  return conn;
93 }
94 
95 QString QgsVectorTileProviderConnection::encodedLayerUri( const QgsVectorTileProviderConnection::Data &conn )
96 {
97  // compared to encodedUri() this one also adds type=xyz to the URI
98  QgsDataSourceUri uri;
99 
100  const QFileInfo info( conn.url );
101  QString suffix = info.suffix().toLower();
102  if ( suffix.startsWith( QLatin1String( "mbtiles" ) ) )
103  {
104  uri.setParam( QStringLiteral( "type" ), QStringLiteral( "mbtiles" ) );
105  }
106  else
107  {
108  uri.setParam( QStringLiteral( "type" ), QStringLiteral( "xyz" ) );
109  }
110 
111  uri.setParam( QStringLiteral( "url" ), conn.url );
112  if ( conn.zMin != -1 )
113  uri.setParam( QStringLiteral( "zmin" ), QString::number( conn.zMin ) );
114  if ( conn.zMax != -1 )
115  uri.setParam( QStringLiteral( "zmax" ), QString::number( conn.zMax ) );
116  if ( !conn.authCfg.isEmpty() )
117  uri.setAuthConfigId( conn.authCfg );
118  if ( !conn.username.isEmpty() )
119  uri.setUsername( conn.username );
120  if ( !conn.password.isEmpty() )
121  uri.setPassword( conn.password );
122  if ( !conn.styleUrl.isEmpty() )
123  uri.setParam( QStringLiteral( "styleUrl" ), conn.styleUrl );
124 
125  uri.setHttpHeaders( conn.httpHeaders );
126 
127  switch ( conn.serviceType )
128  {
129  case Generic:
130  break;
131 
132  case ArcgisVectorTileService:
133  uri.setParam( QStringLiteral( "serviceType" ), QStringLiteral( "arcgis" ) );
134  break;
135  }
136 
137  return uri.encodedUri();
138 }
139 
140 QStringList QgsVectorTileProviderConnection::connectionList()
141 {
142  QgsSettings settings;
143  settings.beginGroup( QStringLiteral( "qgis/connections-vector-tile" ) );
144  QStringList connList = settings.childGroups();
145 
146  return connList;
147 }
148 
149 QgsVectorTileProviderConnection::Data QgsVectorTileProviderConnection::connection( const QString &name )
150 {
151  QgsSettings settings;
152  settings.beginGroup( "qgis/connections-vector-tile/" + name );
153 
154  if ( settings.value( "url" ).toString().isEmpty() )
155  return QgsVectorTileProviderConnection::Data();
156 
157  QgsVectorTileProviderConnection::Data conn;
158  conn.url = settings.value( QStringLiteral( "url" ) ).toString();
159  conn.zMin = settings.value( QStringLiteral( "zmin" ), -1 ).toInt();
160  conn.zMax = settings.value( QStringLiteral( "zmax" ), -1 ).toInt();
161  conn.authCfg = settings.value( QStringLiteral( "authcfg" ) ).toString();
162  conn.username = settings.value( QStringLiteral( "username" ) ).toString();
163  conn.password = settings.value( QStringLiteral( "password" ) ).toString();
164  conn.styleUrl = settings.value( QStringLiteral( "styleUrl" ) ).toString();
165 
166  conn.httpHeaders = QgsHttpHeaders( settings );
167 
168  if ( settings.contains( QStringLiteral( "serviceType" ) ) )
169  {
170  if ( settings.value( QStringLiteral( "serviceType" ) ) == QLatin1String( "arcgis" ) )
171  conn.serviceType = ArcgisVectorTileService;
172  }
173 
174  return conn;
175 }
176 
177 void QgsVectorTileProviderConnection::deleteConnection( const QString &name )
178 {
179  QgsSettings settings;
180  settings.remove( "qgis/connections-vector-tile/" + name );
181 }
182 
183 void QgsVectorTileProviderConnection::addConnection( const QString &name, QgsVectorTileProviderConnection::Data conn )
184 {
185  QgsSettings settings;
186 
187  settings.beginGroup( "qgis/connections-vector-tile/" + name );
188  settings.setValue( QStringLiteral( "url" ), conn.url );
189  settings.setValue( QStringLiteral( "zmin" ), conn.zMin );
190  settings.setValue( QStringLiteral( "zmax" ), conn.zMax );
191  settings.setValue( QStringLiteral( "authcfg" ), conn.authCfg );
192  settings.setValue( QStringLiteral( "username" ), conn.username );
193  settings.setValue( QStringLiteral( "password" ), conn.password );
194  settings.setValue( QStringLiteral( "styleUrl" ), conn.styleUrl );
195 
196  conn.httpHeaders.updateSettings( settings );
197 
198  switch ( conn.serviceType )
199  {
200  case Generic:
201  break;
202 
203  case ArcgisVectorTileService:
204  settings.setValue( QStringLiteral( "serviceType" ), QStringLiteral( "arcgis" ) );
205  break;
206  }
207 }
208 
209 QString QgsVectorTileProviderConnection::selectedConnection()
210 {
211  const QgsSettings settings;
212  return settings.value( QStringLiteral( "qgis/connections-vector-tile/selected" ) ).toString();
213 }
214 
215 void QgsVectorTileProviderConnection::setSelectedConnection( const QString &name )
216 {
217  QgsSettings settings;
218  return settings.setValue( QStringLiteral( "qgis/connections-vector-tile/selected" ), name );
219 }
220 
221 //
222 
223 QgsVectorTileProviderConnection::QgsVectorTileProviderConnection( const QString &name )
225 {
226  setUri( encodedUri( connection( name ) ) );
227 }
228 
229 QgsVectorTileProviderConnection::QgsVectorTileProviderConnection( const QString &uri, const QVariantMap &configuration )
230  : QgsAbstractProviderConnection( uri, configuration )
231 {
232 }
233 
234 void QgsVectorTileProviderConnection::store( const QString &name ) const
235 {
236  addConnection( name, decodedUri( uri() ) );
237 }
238 
239 void QgsVectorTileProviderConnection::remove( const QString &name ) const
240 {
241  deleteConnection( name );
242 }
243 
QgsSettings::remove
void remove(const QString &key, QgsSettings::Section section=QgsSettings::NoSection)
Removes the setting key and any sub-settings of key in a section.
Definition: qgssettings.cpp:192
QgsDataSourceUri
Class for storing the component parts of a RDBMS data source URI (e.g. a Postgres data source).
Definition: qgsdatasourceuri.h:37
QgsDataSourceUri::password
QString password() const
Returns the password stored in the URI.
Definition: qgsdatasourceuri.cpp:285
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition: qgssettings.cpp:161
QgsSettings
This class is a composition of two QSettings instances:
Definition: qgssettings.h:61
QgsDataSourceUri::param
QString param(const QString &key) const
Returns a generic parameter value corresponding to the specified key.
Definition: qgsdatasourceuri.cpp:834
QgsDataSourceUri::setUsername
void setUsername(const QString &username)
Sets the username for the URI.
Definition: qgsdatasourceuri.cpp:265
qgshttpheaders.h
QgsDataSourceUri::setHttpHeaders
void setHttpHeaders(const QgsHttpHeaders &headers)
Sets headers to headers.
Definition: qgsdatasourceuri.h:364
QgsDataSourceUri::username
QString username() const
Returns the username stored in the URI.
Definition: qgsdatasourceuri.cpp:260
qgsdatasourceuri.h
QgsDataSourceUri::setAuthConfigId
void setAuthConfigId(const QString &authcfg)
Sets the authentication configuration ID for the URI.
Definition: qgsdatasourceuri.cpp:727
QgsDataSourceUri::setParam
void setParam(const QString &key, const QString &value)
Sets a generic parameter value on the URI.
Definition: qgsdatasourceuri.cpp:789
qgsvectortileconnection.h
QgsSettings::setValue
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
Definition: qgssettings.cpp:279
QgsDataSourceUri::hasParam
bool hasParam(const QString &key) const
Returns true if a parameter with the specified key exists.
Definition: qgsdatasourceuri.cpp:860
QgsDataSourceUri::encodedUri
QByteArray encodedUri() const
Returns the complete encoded URI as a byte array.
Definition: qgsdatasourceuri.cpp:614
QgsSettings::beginGroup
void beginGroup(const QString &prefix, QgsSettings::Section section=QgsSettings::NoSection)
Appends prefix to the current group.
Definition: qgssettings.cpp:89
QgsDataSourceUri::setEncodedUri
void setEncodedUri(const QByteArray &uri)
Sets the complete encoded uri.
Definition: qgsdatasourceuri.cpp:636
QgsHttpHeaders
This class implements simple http header management.
Definition: qgshttpheaders.h:38
qgssettings.h
QgsSettings::contains
bool contains(const QString &key, QgsSettings::Section section=QgsSettings::NoSection) const
Returns true if there exists a setting called key; returns false otherwise.
Definition: qgssettings.cpp:175
QgsAbstractProviderConnection
The QgsAbstractProviderConnection provides an interface for data provider connections.
Definition: qgsabstractproviderconnection.h:44
QgsDataSourceUri::authConfigId
QString authConfigId() const
Returns any associated authentication configuration ID stored in the URI.
Definition: qgsdatasourceuri.cpp:255
qgslogger.h
QgsDataSourceUri::setPassword
void setPassword(const QString &password)
Sets the password for the URI.
Definition: qgsdatasourceuri.cpp:290
QgsSettings::childGroups
QStringList childGroups() const
Returns a list of all key top-level groups that contain keys that can be read using the QSettings obj...
Definition: qgssettings.cpp:136
QgsDataSourceUri::httpHeaders
QgsHttpHeaders httpHeaders() const
Returns http headers.
Definition: qgsdatasourceuri.h:345