QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 <QInputDialog>
31 #include <QMessageBox>
32 #include <QPicture>
33 #include <QUrl>
34 #include <QNetworkRequest>
35 #include <QNetworkReply>
36 
37 QgsOwsConnection::QgsOwsConnection( const QString &service, const QString &connName )
38  : mConnName( connName )
39  , mService( service )
40 {
41  QgsDebugMsgLevel( "theConnName = " + connName, 4 );
42 
43  QgsSettings settings;
44 
45  QString key = "qgis/connections-" + mService.toLower() + '/' + mConnName;
46  QString credentialsKey = "qgis/" + mService + '/' + mConnName;
47 
48  mConnectionInfo = settings.value( key + "/url" ).toString();
49  mUri.setParam( QStringLiteral( "url" ), settings.value( key + "/url" ).toString() );
50 
51  // Check for credentials and prepend to the connection info
52  QString username = settings.value( credentialsKey + "/username" ).toString();
53  QString password = settings.value( credentialsKey + "/password" ).toString();
54  if ( !username.isEmpty() )
55  {
56  // check for a password, if none prompt to get it
57  mUri.setParam( QStringLiteral( "username" ), username );
58  mUri.setParam( QStringLiteral( "password" ), password );
59  }
60 
61  QString authcfg = settings.value( credentialsKey + "/authcfg" ).toString();
62  if ( !authcfg.isEmpty() )
63  {
64  mUri.setParam( QStringLiteral( "authcfg" ), authcfg );
65  }
66  mConnectionInfo.append( ",authcfg=" + authcfg );
67 
68  const QString referer = settings.value( key + "/referer" ).toString();
69  if ( !referer.isEmpty() )
70  {
71  mUri.setParam( QStringLiteral( "referer" ), referer );
72  mConnectionInfo.append( ",referer=" + referer );
73  }
74 
75  if ( mService.compare( QLatin1String( "WMS" ), Qt::CaseInsensitive ) == 0
76  || mService.compare( QLatin1String( "WCS" ), Qt::CaseInsensitive ) == 0 )
77  {
79  }
80  else if ( mService.compare( QLatin1String( "WFS" ), Qt::CaseInsensitive ) == 0 )
81  {
83  }
84 
85  QgsDebugMsgLevel( QStringLiteral( "encoded uri: '%1'." ).arg( QString( mUri.encodedUri() ) ), 4 );
86 }
87 
89 {
90  return mConnName;
91 }
92 
94 {
95  return mConnectionInfo;
96 }
97 
99 {
100  return mService;
101 }
102 
104 {
105  return mUri;
106 }
107 
109 {
110  addCommonConnectionSettings( uri, settingsKey );
111 
112  QgsSettings settings;
113  QString referer = settings.value( settingsKey + "/referer" ).toString();
114  if ( !referer.isEmpty() )
115  {
116  uri.setParam( QStringLiteral( "referer" ), referer );
117  }
118  if ( settings.value( settingsKey + QStringLiteral( "/ignoreGetMapURI" ), false ).toBool() )
119  {
120  uri.setParam( QStringLiteral( "IgnoreGetMapUrl" ), QStringLiteral( "1" ) );
121  }
122  if ( settings.value( settingsKey + QStringLiteral( "/ignoreGetFeatureInfoURI" ), false ).toBool() )
123  {
124  uri.setParam( QStringLiteral( "IgnoreGetFeatureInfoUrl" ), QStringLiteral( "1" ) );
125  }
126  if ( settings.value( settingsKey + QStringLiteral( "/smoothPixmapTransform" ), false ).toBool() )
127  {
128  uri.setParam( QStringLiteral( "SmoothPixmapTransform" ), QStringLiteral( "1" ) );
129  }
130  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 
140 {
141  addCommonConnectionSettings( uri, settingsKey );
142 
143  QgsSettings settings;
144  QString version = settings.value( settingsKey + "/version" ).toString();
145  if ( !version.isEmpty() )
146  {
147  uri.setParam( QStringLiteral( "version" ), version );
148  }
149 
150  QString maxnumfeatures = settings.value( settingsKey + QStringLiteral( "/maxnumfeatures" ) ).toString();
151  if ( !maxnumfeatures.isEmpty() )
152  {
153  uri.setParam( QStringLiteral( "maxNumFeatures" ), maxnumfeatures );
154  }
155 
156  return uri;
157 }
158 
159 QStringList QgsOwsConnection::connectionList( const QString &service )
160 {
161  QgsSettings settings;
162  settings.beginGroup( "qgis/connections-" + service.toLower() );
163  return settings.childGroups();
164 }
165 
167 {
168  QgsSettings settings;
169  return settings.value( "qgis/connections-" + service.toLower() + "/selected" ).toString();
170 }
171 
172 void QgsOwsConnection::setSelectedConnection( const QString &service, const QString &name )
173 {
174  QgsSettings settings;
175  settings.setValue( "qgis/connections-" + service.toLower() + "/selected", name );
176 }
177 
178 void QgsOwsConnection::addCommonConnectionSettings( QgsDataSourceUri &uri, const QString &key )
179 {
180  QgsSettings settings;
181 
182  if ( settings.value( key + QStringLiteral( "/ignoreAxisOrientation" ), false ).toBool() )
183  {
184  uri.setParam( QStringLiteral( "IgnoreAxisOrientation" ), QStringLiteral( "1" ) );
185  }
186  if ( settings.value( key + QStringLiteral( "/invertAxisOrientation" ), false ).toBool() )
187  {
188  uri.setParam( QStringLiteral( "InvertAxisOrientation" ), QStringLiteral( "1" ) );
189  }
190 }
191 
192 void QgsOwsConnection::deleteConnection( const QString &service, const QString &name )
193 {
194  QgsSettings settings;
195  settings.remove( "qgis/connections-" + service.toLower() + '/' + name );
196  settings.remove( "qgis/" + service + '/' + name );
197 }
QStringList childGroups() const
Returns a list of all key top-level groups that contain keys that can be read using the QSettings obj...
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...
QString connectionName() const
Returns the connection name.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void remove(const QString &key, QgsSettings::Section section=QgsSettings::NoSection)
Removes the setting key and any sub-settings of key in a section.
QgsOwsConnection(const QString &service, const QString &connName)
Constructor.
static QStringList connectionList(const QString &service)
Returns the list of connections for the specified service.
QString connectionInfo() const
Returns connection info string.
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
QString service() const
Returns a string representing the service type, e.g.
static void setSelectedConnection(const QString &service, const QString &name)
Marks the specified connection for the specified service as selected.
QgsDataSourceUri uri() const
Returns the connection uri.
void setParam(const QString &key, const QString &value)
Sets a generic parameter value on the URI.
QgsDataSourceUri mUri
void beginGroup(const QString &prefix, QgsSettings::Section section=QgsSettings::NoSection)
Appends prefix to the current group.
Definition: qgssettings.cpp:87
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
static QgsDataSourceUri & addWfsConnectionSettings(QgsDataSourceUri &uri, const QString &settingsKey)
Adds uri parameters relating to the settings for a WFS connection to a QgsDataSourceUri uri...
QByteArray encodedUri() const
Returns the complete encoded URI as a byte array.
Class for storing the component parts of a RDBMS data source URI (e.g.
static QString selectedConnection(const QString &service)
Retrieves the selected connection for the specified service.
static void deleteConnection(const QString &service, const QString &name)
Deletes the connection for the specified service with the specified name.