QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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
37QgsOwsConnection::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
139QgsDataSourceUri &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
193QgsDataSourceUri &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
212QStringList 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
219QString 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
225void 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
231void 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
245void 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
258void QgsOwsConnection::deleteConnection( const QString &service, const QString &name )
259{
262}
Class for storing the component parts of a RDBMS data source URI (e.g.
QByteArray encodedUri() const
Returns the complete encoded URI as a byte array.
void setAuthConfigId(const QString &authcfg)
Sets the authentication configuration ID for the URI.
QgsHttpHeaders httpHeaders() const
Returns http headers.
void setUsername(const QString &username)
Sets the username for the URI.
void setParam(const QString &key, const QString &value)
Sets a generic parameter value on the URI.
void setHttpHeaders(const QgsHttpHeaders &headers)
Sets headers to headers.
void setPassword(const QString &password)
Sets the password for the URI.
This class implements simple http header management.
static const QString KEY_REFERER
Used in settings as the referer key.
void setFromSettings(const QgsSettings &settings, const QString &key=QString())
Loads headers from the settings.
static const QgsSettingsEntryBool settingsConnectionIgnoreGetMapURI
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.
static QStringList connectionList(const QString &service)
Returns the list of connections for the specified service.
QgsDataSourceUri mUri
static const QgsSettingsEntryString settingsConnectionUrl
static const QgsSettingsEntryString settingsConnectionPassword
static const QgsSettingsEntryEnumFlag< Qgis::DpiMode > settingsConnectionDpiMode
static void setSelectedConnection(const QString &service, const QString &name)
Marks the specified connection for the specified service as selected.
static const QgsSettingsEntryBool settingsConnectionIgnoreGetFeatureInfoURI
static const QgsSettingsEntryString settingsConnectionVersion
static const QgsSettingsEntryString settingsConnectionUsername
static const QgsSettingsEntryGroup settingsServiceConnectionDetailsGroup
static const QgsSettingsEntryBool settingsConnectionReportedLayerExtents
QString connectionInfo() const
Returns connection info string.
static void deleteConnection(const QString &service, const QString &name)
Deletes the connection for the specified service with the specified name.
static const QgsSettingsEntryString settingsConnectionMaxNumFeatures
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.
static const QgsSettingsEntryBool settingsConnectionSmoothPixmapTransform
QString connectionName() const
Returns the connection name.
static QString selectedConnection(const QString &service)
Retrieves the selected connection for the specified service.
QgsOwsConnection(const QString &service, const QString &connName)
Constructor.
static const QgsSettingsEntryBool settingsConnectionIgnoreAxisOrientation
static const QgsSettingsEntryGroup settingsServiceConnectionCredentialsGroup
static const QgsSettingsEntryString settingsConnectionAuthCfg
static const QgsSettingsEntryBool settingsConnectionInvertAxisOrientation
QgsDataSourceUri uri() const
Returns the connection uri.
QString service() const
Returns a string representing the service type, e.g.
bool exists(const QString &dynamicKeyPart=QString()) const
Returns true if the settings is contained in the underlying QSettings.
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
void removeAllSettingsAtBaseKey(const QStringList &dynamicKeyPartList=QStringList()) const
Removes all the settings at the base key for the given dynamicKeyPartList This means it might remove ...
static const char * QGIS
Definition: qgssettings.h:101
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
QStringList childGroups() const
Returns a list of all key top-level groups that contain keys that can be read using the QSettings obj...
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void beginGroup(const QString &prefix, QgsSettings::Section section=QgsSettings::NoSection)
Appends prefix to the current group.
Definition: qgssettings.cpp:90
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:3061
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:3060
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39