QGIS API Documentation  2.99.0-Master (314842d)
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  QgsDebugMsg( "theConnName = " + connName );
42 
43  QgsSettings settings;
44 
45  QString key = "qgis//connections-" + mService.toLower() + '/' + mConnName;
46  QString credentialsKey = "qgis//" + mService + '/' + mConnName;
47 
48  QStringList connStringParts;
49 
50  mConnectionInfo = settings.value( key + "/url" ).toString();
51  mUri.setParam( QStringLiteral( "url" ), settings.value( key + "/url" ).toString() );
52 
53  // Check for credentials and prepend to the connection info
54  QString username = settings.value( credentialsKey + "/username" ).toString();
55  QString password = settings.value( credentialsKey + "/password" ).toString();
56  if ( !username.isEmpty() )
57  {
58  // check for a password, if none prompt to get it
59  mUri.setParam( QStringLiteral( "username" ), username );
60  mUri.setParam( QStringLiteral( "password" ), password );
61  }
62 
63  QString authcfg = settings.value( credentialsKey + "/authcfg" ).toString();
64  if ( !authcfg.isEmpty() )
65  {
66  mUri.setParam( QStringLiteral( "authcfg" ), authcfg );
67  }
68  mConnectionInfo.append( ",authcfg=" + authcfg );
69 
70  bool ignoreGetMap = settings.value( key + "/ignoreGetMapURI", false ).toBool();
71  bool ignoreGetFeatureInfo = settings.value( key + "/ignoreGetFeatureInfoURI", false ).toBool();
72  bool ignoreAxisOrientation = settings.value( key + "/ignoreAxisOrientation", false ).toBool();
73  bool invertAxisOrientation = settings.value( key + "/invertAxisOrientation", false ).toBool();
74  if ( ignoreGetMap )
75  {
76  mUri.setParam( QStringLiteral( "IgnoreGetMapUrl" ), QStringLiteral( "1" ) );
77  }
78  if ( ignoreGetFeatureInfo )
79  {
80  mUri.setParam( QStringLiteral( "IgnoreGetFeatureInfoUrl" ), QStringLiteral( "1" ) );
81  }
82  if ( ignoreAxisOrientation )
83  {
84  mUri.setParam( QStringLiteral( "IgnoreAxisOrientation" ), QStringLiteral( "1" ) );
85  }
86  if ( invertAxisOrientation )
87  {
88  mUri.setParam( QStringLiteral( "InvertAxisOrientation" ), QStringLiteral( "1" ) );
89  }
90 
91  QgsDebugMsg( QString( "encoded uri: '%1'." ).arg( QString( mUri.encodedUri() ) ) );
92 }
93 
95 {
96  return mUri;
97 }
98 
99 QStringList QgsOwsConnection::connectionList( const QString &service )
100 {
101  QgsSettings settings;
102  settings.beginGroup( "qgis//connections-" + service.toLower() );
103  return settings.childGroups();
104 }
105 
106 QString QgsOwsConnection::selectedConnection( const QString &service )
107 {
108  QgsSettings settings;
109  return settings.value( "qgis//connections-" + service.toLower() + "/selected" ).toString();
110 }
111 
112 void QgsOwsConnection::setSelectedConnection( const QString &service, const QString &name )
113 {
114  QgsSettings settings;
115  settings.setValue( "qgis//connections-" + service.toLower() + "/selected", name );
116 }
117 
118 void QgsOwsConnection::deleteConnection( const QString &service, const QString &name )
119 {
120  QgsSettings settings;
121  settings.remove( "qgis//connections-" + service.toLower() + '/' + name );
122  settings.remove( "qgis//" + service + '/' + name );
123 }
QStringList childGroups() const
Returns a list of all key top-level groups that contain keys that can be read using the QSettings obj...
This class is a composition of two QSettings instances:
Definition: qgssettings.h:51
void beginGroup(const QString &prefix)
Appends prefix to the current group.
Definition: qgssettings.cpp:85
#define QgsDebugMsg(str)
Definition: qgslogger.h:36
QgsOwsConnection(const QString &service, const QString &connName)
Constructor.
static QStringList connectionList(const QString &service)
Returns the list of connections for the specified service.
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)
Set generic param (generic mode)
QgsDataSourceUri mUri
QByteArray encodedUri() const
return complete encoded uri (generic mode)
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=Section::NoSection) const
Returns the value for setting key.
Class for storing the component parts of a PostgreSQL/RDBMS datasource URI.
void setValue(const QString &key, const QVariant &value, const Section section=Section::NoSection)
Sets the value of setting key to value.
static QString selectedConnection(const QString &service)
Retrieves the selected connection for the specified service.
void remove(const QString &key)
Removes the setting key and any sub-settings of key.
static void deleteConnection(const QString &service, const QString &name)
Deletes the connection for the specified service with the specified name.