QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsowsconnection.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsowsconnection.cpp - selector for WMS servers, etc.
3  -------------------
4  begin : 3 April 2005
5  copyright :
6  original : (C) 2005 by Brendan Morley email : morb at ozemail dot com dot au
7  wms search : (C) 2009 Mathias Walker <mwa at sourcepole.ch>, Sourcepole AG
8  wms-c support : (C) 2010 Juergen E. Fischer < jef at norbit dot de >, norBIT GmbH
9  generalized : (C) 2012 Radim Blazek, based on qgswmsconnection.cpp
10 
11  ***************************************************************************/
12 
13 /***************************************************************************
14  * *
15  * This program is free software; you can redistribute it and/or modify *
16  * it under the terms of the GNU General Public License as published by *
17  * the Free Software Foundation; either version 2 of the License, or *
18  * (at your option) any later version. *
19  * *
20  ***************************************************************************/
21 
22 #include "qgis.h" // GEO_EPSG_CRS_ID
23 #include "qgsdatasourceuri.h"
24 #include "qgslogger.h"
25 #include "qgsproject.h"
26 #include "qgsproviderregistry.h"
27 #include "qgsowsconnection.h"
28 #include "qgssettings.h"
29 #include "qgshttpheaders.h"
30 
31 #include <QPicture>
32 #include <QUrl>
33 #include <QNetworkRequest>
34 #include <QNetworkReply>
35 
36 
37 QgsOwsConnection::QgsOwsConnection( const QString &service, const QString &connName )
38  : mConnName( connName )
39  , mService( service )
40 {
41  QgsDebugMsgLevel( "theConnName = " + connName, 4 );
42 
43  const QString url = settingsConnectionUrl.value( {mService.toLower(), mConnName} );
44  mConnectionInfo = url;
45  mUri.setParam( QStringLiteral( "url" ), url );
46 
47  // Check for credentials and prepend to the connection info
48  const QString username = settingsConnectionUsername.value( {mService, mConnName} );
49  const QString password = settingsConnectionPassword.value( {mService, mConnName} );
50  if ( !username.isEmpty() )
51  {
52  // check for a password, if none prompt to get it
53  mUri.setUsername( username );
54  mUri.setPassword( password );
55  }
56 
57  const QString authcfg = settingsConnectionAuthCfg.value( {mService, mConnName} );
58  if ( !authcfg.isEmpty() )
59  {
60  mUri.setAuthConfigId( authcfg );
61  }
62  mConnectionInfo.append( ",authcfg=" + authcfg );
63 
64  QgsHttpHeaders httpHeaders( QString( "%3/connections-%1/%2/" ).arg( mService.toLower(), mConnName, QgsSettings::Prefix::QGIS ) );
65  mUri.setHttpHeaders( httpHeaders );
66  const QString referer = httpHeaders[QgsHttpHeaders::KEY_REFERER].toString();
67  if ( !referer.isEmpty() )
68  {
69  mConnectionInfo.append( ",referer=" + referer );
70  }
71 
72  if ( mService.compare( QLatin1String( "WMS" ), Qt::CaseInsensitive ) == 0
73  || mService.compare( QLatin1String( "WCS" ), Qt::CaseInsensitive ) == 0 )
74  {
76  }
77  else if ( mService.compare( QLatin1String( "WFS" ), Qt::CaseInsensitive ) == 0 )
78  {
80  }
81 
82  QgsDebugMsgLevel( QStringLiteral( "encoded uri: '%1'." ).arg( QString( mUri.encodedUri() ) ), 4 );
83 }
84 
86 {
87  return mConnName;
88 }
89 
91 {
92  return mConnectionInfo;
93 }
94 
96 {
97  return mService;
98 }
99 
101 {
102  return mUri;
103 }
104 
106 {
108  addCommonConnectionSettings( uri, settingsKey );
110 
111  const QgsSettings settings;
112  uri.httpHeaders().setFromSettings( settings, settingsKey );
113 
114  if ( settings.value( settingsKey + QStringLiteral( "/ignoreGetMapURI" ), false ).toBool() )
115  {
116  uri.setParam( QStringLiteral( "IgnoreGetMapUrl" ), QStringLiteral( "1" ) );
117  }
118  if ( settings.value( settingsKey + QStringLiteral( "/ignoreGetFeatureInfoURI" ), false ).toBool() )
119  {
120  uri.setParam( QStringLiteral( "IgnoreGetFeatureInfoUrl" ), QStringLiteral( "1" ) );
121  }
122  if ( settings.value( settingsKey + QStringLiteral( "/smoothPixmapTransform" ), false ).toBool() )
123  {
124  uri.setParam( QStringLiteral( "SmoothPixmapTransform" ), QStringLiteral( "1" ) );
125  }
126  if ( settings.value( settingsKey + QStringLiteral( "/ignoreReportedLayerExtents" ), false ).toBool() )
127  {
128  uri.setParam( QStringLiteral( "IgnoreReportedLayerExtents" ), QStringLiteral( "1" ) );
129  }
130  const QString dpiMode = settings.value( settingsKey + QStringLiteral( "/dpiMode" ), QStringLiteral( "all" ) ).toString();
131  if ( !dpiMode.isEmpty() )
132  {
133  uri.setParam( QStringLiteral( "dpiMode" ), dpiMode );
134  }
135 
136  return uri;
137 }
138 
139 QgsDataSourceUri &QgsOwsConnection::addWmsWcsConnectionSettings( QgsDataSourceUri &uri, const QString &service, const QString &connName )
140 {
141  addCommonConnectionSettings( uri, service, connName );
142 
143  QString settingsKey = QString( "%3/connections-%1/%2/" ).arg( service.toLower(), connName, QgsSettings::Prefix::QGIS );
144  const QgsSettings settings;
145  uri.httpHeaders().setFromSettings( settings, settingsKey );
146 
147  if ( settingsConnectionIgnoreGetMapURI.value( {service.toLower(), connName} ) )
148  {
149  uri.setParam( QStringLiteral( "IgnoreGetMapUrl" ), QStringLiteral( "1" ) );
150  }
151  if ( settingsConnectionIgnoreGetFeatureInfoURI.value( {service.toLower(), connName} ) )
152  {
153  uri.setParam( QStringLiteral( "IgnoreGetFeatureInfoUrl" ), QStringLiteral( "1" ) );
154  }
155  if ( settingsConnectionSmoothPixmapTransform.value( {service.toLower(), connName} ) )
156  {
157  uri.setParam( QStringLiteral( "SmoothPixmapTransform" ), QStringLiteral( "1" ) );
158  }
159  if ( settingsConnectionReportedLayerExtents.value( {service.toLower(), connName} ) )
160  {
161  uri.setParam( QStringLiteral( "IgnoreReportedLayerExtents" ), QStringLiteral( "1" ) );
162  }
163  if ( settingsConnectionDpiMode.exists( {service.toLower(), connName} ) )
164  {
165  uri.setParam( QStringLiteral( "dpiMode" ), QString::number( static_cast<int>( settingsConnectionDpiMode.value( {service.toLower(), connName} ) ) ) );
166  }
167 
168  return uri;
169 }
170 
172 {
174  addCommonConnectionSettings( uri, settingsKey );
176 
177  const QgsSettings settings;
178  const QString version = settings.value( settingsKey + "/version" ).toString();
179  if ( !version.isEmpty() )
180  {
181  uri.setParam( QStringLiteral( "version" ), version );
182  }
183 
184  const QString maxnumfeatures = settings.value( settingsKey + QStringLiteral( "/maxnumfeatures" ) ).toString();
185  if ( !maxnumfeatures.isEmpty() )
186  {
187  uri.setParam( QStringLiteral( "maxNumFeatures" ), maxnumfeatures );
188  }
189 
190  return uri;
191 }
192 
193 QgsDataSourceUri &QgsOwsConnection::addWfsConnectionSettings( QgsDataSourceUri &uri, const QString &service, const QString &connName )
194 {
195  addCommonConnectionSettings( uri, service, connName );
196 
197  const QString version = settingsConnectionVersion.value( {service.toLower(), connName} );
198  if ( !version.isEmpty() )
199  {
200  uri.setParam( QStringLiteral( "version" ), version );
201  }
202 
203  const QString maxnumFeatures = settingsConnectionMaxNumFeatures.value( {service.toLower(), connName} );
204  if ( !maxnumFeatures.isEmpty() )
205  {
206  uri.setParam( QStringLiteral( "maxNumFeatures" ), maxnumFeatures );
207  }
208 
209  return uri;
210 }
211 
212 QStringList QgsOwsConnection::connectionList( const QString &service )
213 {
214  QgsSettings settings;
215  settings.beginGroup( QString( "%1/connections-%2" ).arg( QgsSettings::Prefix::QGIS, service.toLower() ) );
216  return settings.childGroups();
217 }
218 
219 QString QgsOwsConnection::selectedConnection( const QString &service )
220 {
221  const QgsSettings settings;
222  return settings.value( QString( "%1/connections-%2/selected" ).arg( QgsSettings::Prefix::QGIS, service.toLower() ) ).toString();
223 }
224 
225 void QgsOwsConnection::setSelectedConnection( const QString &service, const QString &name )
226 {
227  QgsSettings settings;
228  settings.setValue( QString( "%1/connections-%2/selected" ).arg( QgsSettings::Prefix::QGIS, service.toLower() ), name );
229 }
230 
231 void QgsOwsConnection::addCommonConnectionSettings( QgsDataSourceUri &uri, const QString &key )
232 {
233  const QgsSettings settings;
234 
235  if ( settings.value( key + QStringLiteral( "/ignoreAxisOrientation" ), false ).toBool() )
236  {
237  uri.setParam( QStringLiteral( "IgnoreAxisOrientation" ), QStringLiteral( "1" ) );
238  }
239  if ( settings.value( key + QStringLiteral( "/invertAxisOrientation" ), false ).toBool() )
240  {
241  uri.setParam( QStringLiteral( "InvertAxisOrientation" ), QStringLiteral( "1" ) );
242  }
243 }
244 
245 void QgsOwsConnection::addCommonConnectionSettings( QgsDataSourceUri &uri, const QString &service, const QString &connectionName )
246 {
247 
248  if ( settingsConnectionIgnoreAxisOrientation.value( {service.toLower(), connectionName} ) )
249  {
250  uri.setParam( QStringLiteral( "IgnoreAxisOrientation" ), QStringLiteral( "1" ) );
251  }
252  if ( settingsConnectionInvertAxisOrientation.value( {service.toLower(), connectionName} ) )
253  {
254  uri.setParam( QStringLiteral( "InvertAxisOrientation" ), QStringLiteral( "1" ) );
255  }
256 }
257 
258 void QgsOwsConnection::deleteConnection( const QString &service, const QString &name )
259 {
262 }
QgsOwsConnection::service
QString service() const
Returns a string representing the service type, e.g.
Definition: qgsowsconnection.cpp:95
QgsOwsConnection::settingsConnectionIgnoreGetFeatureInfoURI
static const QgsSettingsEntryBool settingsConnectionIgnoreGetFeatureInfoURI
Definition: qgsowsconnection.h:66
QgsOwsConnection::settingsConnectionVersion
static const QgsSettingsEntryString settingsConnectionVersion
Definition: qgsowsconnection.h:64
QgsDataSourceUri
Class for storing the component parts of a RDBMS data source URI (e.g. a Postgres data source).
Definition: qgsdatasourceuri.h:37
QgsOwsConnection::settingsServiceConnectionDetailsGroup
static const QgsSettingsEntryGroup settingsServiceConnectionDetailsGroup
Definition: qgsowsconnection.h:81
QgsSettings::Prefix::QGIS
static const char * QGIS
Definition: qgssettings.h:101
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
QgsOwsConnection::settingsConnectionUsername
static const QgsSettingsEntryString settingsConnectionUsername
Definition: qgsowsconnection.h:77
QgsOwsConnection::mUri
QgsDataSourceUri mUri
Definition: qgsowsconnection.h:156
QgsDebugMsgLevel
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
QgsHttpHeaders::setFromSettings
void setFromSettings(const QgsSettings &settings, const QString &key=QString())
Loads headers from the settings.
Definition: qgshttpheaders.cpp:140
QgsSettingsEntryGroup::removeAllSettingsAtBaseKey
void removeAllSettingsAtBaseKey(const QStringList &dynamicKeyPartList=QStringList()) const
Removes all the settings at the base key for the given dynamicKeyPartList This means it might remove ...
Definition: qgssettingsentry.cpp:76
QgsOwsConnection::settingsConnectionAuthCfg
static const QgsSettingsEntryString settingsConnectionAuthCfg
Definition: qgsowsconnection.h:79
QgsOwsConnection::settingsConnectionUrl
static const QgsSettingsEntryString settingsConnectionUrl
Definition: qgsowsconnection.h:62
qgis.h
qgsowsconnection.h
QgsSettings
This class is a composition of two QSettings instances:
Definition: qgssettings.h:61
QgsOwsConnection::settingsConnectionInvertAxisOrientation
static const QgsSettingsEntryBool settingsConnectionInvertAxisOrientation
Definition: qgsowsconnection.h:75
QgsSettingsEntryBase::exists
bool exists(const QString &dynamicKeyPart=QString()) const
Returns true if the settings is contained in the underlying QSettings.
Definition: qgssettingsentry.cpp:173
QgsOwsConnection::addWfsConnectionSettings
static Q_DECL_DEPRECATED QgsDataSourceUri & addWfsConnectionSettings(QgsDataSourceUri &uri, const QString &settingsKey)
Adds uri parameters relating to the settings for a WFS connection to a QgsDataSourceUri uri.
Definition: qgsowsconnection.cpp:171
QgsOwsConnection::settingsConnectionIgnoreAxisOrientation
static const QgsSettingsEntryBool settingsConnectionIgnoreAxisOrientation
Definition: qgsowsconnection.h:74
QgsOwsConnection::settingsConnectionMaxNumFeatures
static const QgsSettingsEntryString settingsConnectionMaxNumFeatures
Definition: qgsowsconnection.h:70
Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:2820
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
QgsOwsConnection::selectedConnection
static QString selectedConnection(const QString &service)
Retrieves the selected connection for the specified service.
Definition: qgsowsconnection.cpp:219
qgsproviderregistry.h
qgsdatasourceuri.h
QgsOwsConnection::deleteConnection
static void deleteConnection(const QString &service, const QString &name)
Deletes the connection for the specified service with the specified name.
Definition: qgsowsconnection.cpp:258
QgsDataSourceUri::setAuthConfigId
void setAuthConfigId(const QString &authcfg)
Sets the authentication configuration ID for the URI.
Definition: qgsdatasourceuri.cpp:727
QgsOwsConnection::settingsConnectionDpiMode
static const QgsSettingsEntryEnumFlag< Qgis::DpiMode > settingsConnectionDpiMode
Definition: qgsowsconnection.h:69
QgsHttpHeaders::KEY_REFERER
static const QString KEY_REFERER
Used in settings as the referer key.
Definition: qgshttpheaders.h:48
QgsOwsConnection::addWmsWcsConnectionSettings
static Q_DECL_DEPRECATED QgsDataSourceUri & addWmsWcsConnectionSettings(QgsDataSourceUri &uri, const QString &settingsKey)
Adds uri parameters relating to the settings for a WMS or WCS connection to a QgsDataSourceUri uri.
Definition: qgsowsconnection.cpp:105
QgsDataSourceUri::setParam
void setParam(const QString &key, const QString &value)
Sets a generic parameter value on the URI.
Definition: qgsdatasourceuri.cpp:789
QgsOwsConnection::settingsConnectionPassword
static const QgsSettingsEntryString settingsConnectionPassword
Definition: qgsowsconnection.h:78
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
QgsOwsConnection::settingsServiceConnectionCredentialsGroup
static const QgsSettingsEntryGroup settingsServiceConnectionCredentialsGroup
Definition: qgsowsconnection.h:82
QgsOwsConnection::connectionName
QString connectionName() const
Returns the connection name.
Definition: qgsowsconnection.cpp:85
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
QgsSettingsEntryByReference::value
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
Definition: qgssettingsentry.h:379
QgsHttpHeaders
This class implements simple http header management.
Definition: qgshttpheaders.h:38
QgsOwsConnection::settingsConnectionIgnoreGetMapURI
static const QgsSettingsEntryBool settingsConnectionIgnoreGetMapURI
Definition: qgsowsconnection.h:65
QgsOwsConnection::QgsOwsConnection
QgsOwsConnection(const QString &service, const QString &connName)
Constructor.
Definition: qgsowsconnection.cpp:37
qgssettings.h
QgsOwsConnection::connectionList
static QStringList connectionList(const QString &service)
Returns the list of connections for the specified service.
Definition: qgsowsconnection.cpp:212
QgsOwsConnection::settingsConnectionSmoothPixmapTransform
static const QgsSettingsEntryBool settingsConnectionSmoothPixmapTransform
Definition: qgsowsconnection.h:67
qgslogger.h
QgsOwsConnection::settingsConnectionReportedLayerExtents
static const QgsSettingsEntryBool settingsConnectionReportedLayerExtents
Definition: qgsowsconnection.h:68
QgsOwsConnection::connectionInfo
QString connectionInfo() const
Returns connection info string.
Definition: qgsowsconnection.cpp:90
Q_NOWARN_DEPRECATED_PUSH
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:2819
QgsDataSourceUri::setPassword
void setPassword(const QString &password)
Sets the password for the URI.
Definition: qgsdatasourceuri.cpp:290
qgsproject.h
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
QgsOwsConnection::setSelectedConnection
static void setSelectedConnection(const QString &service, const QString &name)
Marks the specified connection for the specified service as selected.
Definition: qgsowsconnection.cpp:225
QgsSettingsEntryByValue::value
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
Definition: qgssettingsentry.h:520
QgsDataSourceUri::httpHeaders
QgsHttpHeaders httpHeaders() const
Returns http headers.
Definition: qgsdatasourceuri.h:345
QgsOwsConnection::uri
QgsDataSourceUri uri() const
Returns the connection uri.
Definition: qgsowsconnection.cpp:100