QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 QgsOwsConnection::QgsOwsConnection( const QString &service, const QString &connName )
36  : mConnName( connName )
37  , mService( service )
38 {
39  QgsDebugMsgLevel( "theConnName = " + connName, 4 );
40 
41  QgsSettings settings;
42 
43  QString key = "qgis/connections-" + mService.toLower() + '/' + mConnName;
44  QString credentialsKey = "qgis/" + mService + '/' + mConnName;
45 
46  mConnectionInfo = settings.value( key + "/url" ).toString();
47  mUri.setParam( QStringLiteral( "url" ), settings.value( key + "/url" ).toString() );
48 
49  // Check for credentials and prepend to the connection info
50  QString username = settings.value( credentialsKey + "/username" ).toString();
51  QString password = settings.value( credentialsKey + "/password" ).toString();
52  if ( !username.isEmpty() )
53  {
54  // check for a password, if none prompt to get it
55  mUri.setUsername( username );
56  mUri.setPassword( password );
57  }
58 
59  QString authcfg = settings.value( credentialsKey + "/authcfg" ).toString();
60  if ( !authcfg.isEmpty() )
61  {
62  mUri.setAuthConfigId( authcfg );
63  }
64  mConnectionInfo.append( ",authcfg=" + authcfg );
65 
66  const QString referer = settings.value( key + "/referer" ).toString();
67  if ( !referer.isEmpty() )
68  {
69  mUri.setParam( QStringLiteral( "referer" ), referer );
70  mConnectionInfo.append( ",referer=" + referer );
71  }
72 
73  if ( mService.compare( QLatin1String( "WMS" ), Qt::CaseInsensitive ) == 0
74  || mService.compare( QLatin1String( "WCS" ), Qt::CaseInsensitive ) == 0 )
75  {
77  }
78  else if ( mService.compare( QLatin1String( "WFS" ), Qt::CaseInsensitive ) == 0 )
79  {
81  }
82 
83  QgsDebugMsgLevel( QStringLiteral( "encoded uri: '%1'." ).arg( QString( mUri.encodedUri() ) ), 4 );
84 }
85 
87 {
88  return mConnName;
89 }
90 
92 {
93  return mConnectionInfo;
94 }
95 
97 {
98  return mService;
99 }
100 
102 {
103  return mUri;
104 }
105 
107 {
108  addCommonConnectionSettings( uri, settingsKey );
109 
110  QgsSettings settings;
111  QString referer = settings.value( settingsKey + "/referer" ).toString();
112  if ( !referer.isEmpty() )
113  {
114  uri.setParam( QStringLiteral( "referer" ), referer );
115  }
116  if ( settings.value( settingsKey + QStringLiteral( "/ignoreGetMapURI" ), false ).toBool() )
117  {
118  uri.setParam( QStringLiteral( "IgnoreGetMapUrl" ), QStringLiteral( "1" ) );
119  }
120  if ( settings.value( settingsKey + QStringLiteral( "/ignoreGetFeatureInfoURI" ), false ).toBool() )
121  {
122  uri.setParam( QStringLiteral( "IgnoreGetFeatureInfoUrl" ), QStringLiteral( "1" ) );
123  }
124  if ( settings.value( settingsKey + QStringLiteral( "/smoothPixmapTransform" ), false ).toBool() )
125  {
126  uri.setParam( QStringLiteral( "SmoothPixmapTransform" ), QStringLiteral( "1" ) );
127  }
128  if ( settings.value( settingsKey + QStringLiteral( "/ignoreReportedLayerExtents" ), false ).toBool() )
129  {
130  uri.setParam( QStringLiteral( "IgnoreReportedLayerExtents" ), QStringLiteral( "1" ) );
131  }
132  QString dpiMode = settings.value( settingsKey + QStringLiteral( "/dpiMode" ), QStringLiteral( "all" ) ).toString();
133  if ( !dpiMode.isEmpty() )
134  {
135  uri.setParam( QStringLiteral( "dpiMode" ), dpiMode );
136  }
137 
138  return uri;
139 }
140 
142 {
143  addCommonConnectionSettings( uri, settingsKey );
144 
145  QgsSettings settings;
146  QString version = settings.value( settingsKey + "/version" ).toString();
147  if ( !version.isEmpty() )
148  {
149  uri.setParam( QStringLiteral( "version" ), version );
150  }
151 
152  QString maxnumfeatures = settings.value( settingsKey + QStringLiteral( "/maxnumfeatures" ) ).toString();
153  if ( !maxnumfeatures.isEmpty() )
154  {
155  uri.setParam( QStringLiteral( "maxNumFeatures" ), maxnumfeatures );
156  }
157 
158  return uri;
159 }
160 
161 QStringList QgsOwsConnection::connectionList( const QString &service )
162 {
163  QgsSettings settings;
164  settings.beginGroup( "qgis/connections-" + service.toLower() );
165  return settings.childGroups();
166 }
167 
168 QString QgsOwsConnection::selectedConnection( const QString &service )
169 {
170  QgsSettings settings;
171  return settings.value( "qgis/connections-" + service.toLower() + "/selected" ).toString();
172 }
173 
174 void QgsOwsConnection::setSelectedConnection( const QString &service, const QString &name )
175 {
176  QgsSettings settings;
177  settings.setValue( "qgis/connections-" + service.toLower() + "/selected", name );
178 }
179 
180 void QgsOwsConnection::addCommonConnectionSettings( QgsDataSourceUri &uri, const QString &key )
181 {
182  QgsSettings settings;
183 
184  if ( settings.value( key + QStringLiteral( "/ignoreAxisOrientation" ), false ).toBool() )
185  {
186  uri.setParam( QStringLiteral( "IgnoreAxisOrientation" ), QStringLiteral( "1" ) );
187  }
188  if ( settings.value( key + QStringLiteral( "/invertAxisOrientation" ), false ).toBool() )
189  {
190  uri.setParam( QStringLiteral( "InvertAxisOrientation" ), QStringLiteral( "1" ) );
191  }
192 }
193 
194 void QgsOwsConnection::deleteConnection( const QString &service, const QString &name )
195 {
196  QgsSettings settings;
197  settings.remove( "qgis/connections-" + service.toLower() + '/' + name );
198  settings.remove( "qgis/" + service + '/' + name );
199 }
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 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 void setSelectedConnection(const QString &service, const QString &name)
Marks the specified connection for the specified service as selected.
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 QgsDataSourceUri & addWfsConnectionSettings(QgsDataSourceUri &uri, const QString &settingsKey)
Adds uri parameters relating to the settings for a WFS connection to a QgsDataSourceUri uri.
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.
QgsDataSourceUri uri() const
Returns the connection uri.
QString service() const
Returns a string representing the service type, e.g.
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39