QGIS API Documentation  3.25.0-Master (10b47c2603)
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 
30 #include <QPicture>
31 #include <QUrl>
32 #include <QNetworkRequest>
33 #include <QNetworkReply>
34 
35 
36 QgsOwsConnection::QgsOwsConnection( const QString &service, const QString &connName )
37  : mConnName( connName )
38  , mService( service )
39 {
40  QgsDebugMsgLevel( "theConnName = " + connName, 4 );
41 
42  const QString url = settingsConnectionUrl.value( {mService.toLower(), mConnName} );
43  mConnectionInfo = url;
44  mUri.setParam( QStringLiteral( "url" ), url );
45 
46  // Check for credentials and prepend to the connection info
47  const QString username = settingsConnectionUsername.value( {mService, mConnName} );
48  const QString password = settingsConnectionPassword.value( {mService, mConnName} );
49  if ( !username.isEmpty() )
50  {
51  // check for a password, if none prompt to get it
52  mUri.setUsername( username );
53  mUri.setPassword( password );
54  }
55 
56  const QString authcfg = settingsConnectionAuthCfg.value( {mService, mConnName} );
57  if ( !authcfg.isEmpty() )
58  {
59  mUri.setAuthConfigId( authcfg );
60  }
61  mConnectionInfo.append( ",authcfg=" + authcfg );
62 
63  const QString referer = settingsConnectionReferer.value( {mService.toLower(), mConnName} );
64  if ( !referer.isEmpty() )
65  {
66  mUri.setParam( QStringLiteral( "referer" ), referer );
67  mConnectionInfo.append( ",referer=" + referer );
68  }
69 
70  if ( mService.compare( QLatin1String( "WMS" ), Qt::CaseInsensitive ) == 0
71  || mService.compare( QLatin1String( "WCS" ), Qt::CaseInsensitive ) == 0 )
72  {
74  }
75  else if ( mService.compare( QLatin1String( "WFS" ), Qt::CaseInsensitive ) == 0 )
76  {
78  }
79 
80  QgsDebugMsgLevel( QStringLiteral( "encoded uri: '%1'." ).arg( QString( mUri.encodedUri() ) ), 4 );
81 }
82 
84 {
85  return mConnName;
86 }
87 
89 {
90  return mConnectionInfo;
91 }
92 
94 {
95  return mService;
96 }
97 
99 {
100  return mUri;
101 }
102 
104 {
106  addCommonConnectionSettings( uri, settingsKey );
108 
109  const QgsSettings settings;
110  const QString referer = settings.value( settingsKey + "/referer" ).toString();
111  if ( !referer.isEmpty() )
112  {
113  uri.setParam( QStringLiteral( "referer" ), referer );
114  }
115  if ( settings.value( settingsKey + QStringLiteral( "/ignoreGetMapURI" ), false ).toBool() )
116  {
117  uri.setParam( QStringLiteral( "IgnoreGetMapUrl" ), QStringLiteral( "1" ) );
118  }
119  if ( settings.value( settingsKey + QStringLiteral( "/ignoreGetFeatureInfoURI" ), false ).toBool() )
120  {
121  uri.setParam( QStringLiteral( "IgnoreGetFeatureInfoUrl" ), QStringLiteral( "1" ) );
122  }
123  if ( settings.value( settingsKey + QStringLiteral( "/smoothPixmapTransform" ), false ).toBool() )
124  {
125  uri.setParam( QStringLiteral( "SmoothPixmapTransform" ), QStringLiteral( "1" ) );
126  }
127  if ( settings.value( settingsKey + QStringLiteral( "/ignoreReportedLayerExtents" ), false ).toBool() )
128  {
129  uri.setParam( QStringLiteral( "IgnoreReportedLayerExtents" ), QStringLiteral( "1" ) );
130  }
131  const QString dpiMode = settings.value( settingsKey + QStringLiteral( "/dpiMode" ), QStringLiteral( "all" ) ).toString();
132  if ( !dpiMode.isEmpty() )
133  {
134  uri.setParam( QStringLiteral( "dpiMode" ), dpiMode );
135  }
136 
137  return uri;
138 }
139 
140 QgsDataSourceUri &QgsOwsConnection::addWmsWcsConnectionSettings( QgsDataSourceUri &uri, const QString &service, const QString &connName )
141 {
142  addCommonConnectionSettings( uri, service, connName );
143 
144  const QString referer = settingsConnectionReferer.value( {service.toLower(), connName} );
145  if ( !referer.isEmpty() )
146  {
147  uri.setParam( QStringLiteral( "referer" ), referer );
148  }
149  if ( settingsConnectionIgnoreGetMapURI.value( {service.toLower(), connName} ) )
150  {
151  uri.setParam( QStringLiteral( "IgnoreGetMapUrl" ), QStringLiteral( "1" ) );
152  }
153  if ( settingsConnectionIgnoreGetFeatureInfoURI.value( {service.toLower(), connName} ) )
154  {
155  uri.setParam( QStringLiteral( "IgnoreGetFeatureInfoUrl" ), QStringLiteral( "1" ) );
156  }
157  if ( settingsConnectionSmoothPixmapTransform.value( {service.toLower(), connName} ) )
158  {
159  uri.setParam( QStringLiteral( "SmoothPixmapTransform" ), QStringLiteral( "1" ) );
160  }
161  if ( settingsConnectionReportedLayerExtents.value( {service.toLower(), connName} ) )
162  {
163  uri.setParam( QStringLiteral( "IgnoreReportedLayerExtents" ), QStringLiteral( "1" ) );
164  }
165  if ( settingsConnectionDpiMode.exists( {service.toLower(), connName} ) )
166  {
167  uri.setParam( QStringLiteral( "dpiMode" ), QString::number( static_cast<int>( settingsConnectionDpiMode.value( {service.toLower(), connName} ) ) ) );
168  }
169 
170  return uri;
171 }
172 
174 {
176  addCommonConnectionSettings( uri, settingsKey );
178 
179  const QgsSettings settings;
180  const QString version = settings.value( settingsKey + "/version" ).toString();
181  if ( !version.isEmpty() )
182  {
183  uri.setParam( QStringLiteral( "version" ), version );
184  }
185 
186  const QString maxnumfeatures = settings.value( settingsKey + QStringLiteral( "/maxnumfeatures" ) ).toString();
187  if ( !maxnumfeatures.isEmpty() )
188  {
189  uri.setParam( QStringLiteral( "maxNumFeatures" ), maxnumfeatures );
190  }
191 
192  return uri;
193 }
194 
195 QgsDataSourceUri &QgsOwsConnection::addWfsConnectionSettings( QgsDataSourceUri &uri, const QString &service, const QString &connName )
196 {
197  addCommonConnectionSettings( uri, service, connName );
198 
199  const QString version = settingsConnectionVersion.value( {service.toLower(), connName} );
200  if ( !version.isEmpty() )
201  {
202  uri.setParam( QStringLiteral( "version" ), version );
203  }
204 
205  const QString maxnumFeatures = settingsConnectionMaxNumFeatures.value( {service.toLower(), connName} );
206  if ( !maxnumFeatures.isEmpty() )
207  {
208  uri.setParam( QStringLiteral( "maxNumFeatures" ), maxnumFeatures );
209  }
210 
211  return uri;
212 }
213 
214 QStringList QgsOwsConnection::connectionList( const QString &service )
215 {
216  QgsSettings settings;
217  settings.beginGroup( "qgis/connections-" + service.toLower() );
218  return settings.childGroups();
219 }
220 
221 QString QgsOwsConnection::selectedConnection( const QString &service )
222 {
223  const QgsSettings settings;
224  return settings.value( "qgis/connections-" + service.toLower() + "/selected" ).toString();
225 }
226 
227 void QgsOwsConnection::setSelectedConnection( const QString &service, const QString &name )
228 {
229  QgsSettings settings;
230  settings.setValue( "qgis/connections-" + service.toLower() + "/selected", name );
231 }
232 
233 void QgsOwsConnection::addCommonConnectionSettings( QgsDataSourceUri &uri, const QString &key )
234 {
235  const QgsSettings settings;
236 
237  if ( settings.value( key + QStringLiteral( "/ignoreAxisOrientation" ), false ).toBool() )
238  {
239  uri.setParam( QStringLiteral( "IgnoreAxisOrientation" ), QStringLiteral( "1" ) );
240  }
241  if ( settings.value( key + QStringLiteral( "/invertAxisOrientation" ), false ).toBool() )
242  {
243  uri.setParam( QStringLiteral( "InvertAxisOrientation" ), QStringLiteral( "1" ) );
244  }
245 }
246 
247 void QgsOwsConnection::addCommonConnectionSettings( QgsDataSourceUri &uri, const QString &service, const QString &connectionName )
248 {
249 
250  if ( settingsConnectionIgnoreAxisOrientation.value( {service.toLower(), connectionName} ) )
251  {
252  uri.setParam( QStringLiteral( "IgnoreAxisOrientation" ), QStringLiteral( "1" ) );
253  }
254  if ( settingsConnectionInvertAxisOrientation.value( {service.toLower(), connectionName} ) )
255  {
256  uri.setParam( QStringLiteral( "InvertAxisOrientation" ), QStringLiteral( "1" ) );
257  }
258 }
259 
260 void QgsOwsConnection::deleteConnection( const QString &service, const QString &name )
261 {
264 }
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.
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 setPassword(const QString &password)
Sets the password for the URI.
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 const QgsSettingsEntryString settingsConnectionReferer
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 ...
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:89
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:2617
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:2616
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39