QGIS API Documentation  2.99.0-Master (7d4f81d)
qgsnewhttpconnection.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsnewhttpconnection.cpp - selector for a new HTTP server for WMS, etc.
3  -------------------
4  begin : 3 April 2005
5  copyright : (C) 2005 by Brendan Morley
6  email : morb at ozemail dot com dot au
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 #include "qgsnewhttpconnection.h"
18 #include "qgscontexthelp.h"
19 #include "qgsauthconfigselect.h"
20 #include "qgssettings.h"
21 
22 #include <QMessageBox>
23 #include <QUrl>
24 #include <QPushButton>
25 #include <QRegExp>
26 #include <QRegExpValidator>
27 
29  QWidget *parent, const QString &baseKey, const QString &connName, Qt::WindowFlags fl )
30  : QDialog( parent, fl )
31  , mBaseKey( baseKey )
32  , mOriginalConnName( connName )
33  , mAuthConfigSelect( nullptr )
34 {
35  setupUi( this );
36 
37  QRegExp rx( "/connections-([^/]+)/" );
38  rx.indexIn( baseKey );
39  setWindowTitle( tr( "Create a New %1 Connection" ).arg( rx.cap( 1 ).toUpper() ) );
40 
41  // It would be obviously much better to use mBaseKey also for credentials,
42  // but for some strange reason a different hardcoded key was used instead.
43  // WFS and WMS credentials were mixed with the same key WMS.
44  // Only WMS and WFS providers are using QgsNewHttpConnection at this moment
45  // using connection-wms and connection-wfs -> parse credential key fro it.
46  mCredentialsBaseKey = mBaseKey.split( '-' ).last().toUpper();
47 
48  txtName->setValidator( new QRegExpValidator( QRegExp( "[^\\/]+" ), txtName ) );
49 
50  cmbDpiMode->clear();
51  cmbDpiMode->addItem( tr( "all" ) );
52  cmbDpiMode->addItem( tr( "off" ) );
53  cmbDpiMode->addItem( tr( "QGIS" ) );
54  cmbDpiMode->addItem( tr( "UMN" ) );
55  cmbDpiMode->addItem( tr( "GeoServer" ) );
56 
57  cmbVersion->clear();
58  cmbVersion->addItem( tr( "Auto-detect" ) );
59  cmbVersion->addItem( tr( "1.0" ) );
60  cmbVersion->addItem( tr( "1.1" ) );
61  cmbVersion->addItem( tr( "2.0" ) );
62 
63  mAuthConfigSelect = new QgsAuthConfigSelect( this );
64  tabAuth->insertTab( 1, mAuthConfigSelect, tr( "Configurations" ) );
65 
66  if ( !connName.isEmpty() )
67  {
68  // populate the dialog with the information stored for the connection
69  // populate the fields with the stored setting parameters
70 
71  QgsSettings settings;
72 
73  QString key = mBaseKey + connName;
74  QString credentialsKey = "qgis/" + mCredentialsBaseKey + '/' + connName;
75  txtName->setText( connName );
76  txtUrl->setText( settings.value( key + "/url" ).toString() );
77 
78  cbxIgnoreGetMapURI->setChecked( settings.value( key + "/ignoreGetMapURI", false ).toBool() );
79  cbxIgnoreAxisOrientation->setChecked( settings.value( key + "/ignoreAxisOrientation", false ).toBool() );
80  cbxInvertAxisOrientation->setChecked( settings.value( key + "/invertAxisOrientation", false ).toBool() );
81  cbxIgnoreGetFeatureInfoURI->setChecked( settings.value( key + "/ignoreGetFeatureInfoURI", false ).toBool() );
82  cbxSmoothPixmapTransform->setChecked( settings.value( key + "/smoothPixmapTransform", false ).toBool() );
83 
84  int dpiIdx;
85  switch ( settings.value( key + "/dpiMode", 7 ).toInt() )
86  {
87  case 0: // off
88  dpiIdx = 1;
89  break;
90  case 1: // QGIS
91  dpiIdx = 2;
92  break;
93  case 2: // UMN
94  dpiIdx = 3;
95  break;
96  case 4: // GeoServer
97  dpiIdx = 4;
98  break;
99  default: // other => all
100  dpiIdx = 0;
101  break;
102  }
103  cmbDpiMode->setCurrentIndex( dpiIdx );
104 
105  QString version = settings.value( key + "/version" ).toString();
106  int versionIdx = 0; // AUTO
107  if ( version == QLatin1String( "1.0.0" ) )
108  versionIdx = 1;
109  else if ( version == QLatin1String( "1.1.0" ) )
110  versionIdx = 2;
111  else if ( version == QLatin1String( "2.0.0" ) )
112  versionIdx = 3;
113  cmbVersion->setCurrentIndex( versionIdx );
114 
115  txtReferer->setText( settings.value( key + "/referer" ).toString() );
116  txtMaxNumFeatures->setText( settings.value( key + "/maxnumfeatures" ).toString() );
117 
118  txtUserName->setText( settings.value( credentialsKey + "/username" ).toString() );
119  txtPassword->setText( settings.value( credentialsKey + "/password" ).toString() );
120 
121  QString authcfg = settings.value( credentialsKey + "/authcfg" ).toString();
122  mAuthConfigSelect->setConfigId( authcfg );
123  if ( !authcfg.isEmpty() )
124  {
125  tabAuth->setCurrentIndex( tabAuth->indexOf( mAuthConfigSelect ) );
126  }
127  }
128 
129  if ( mBaseKey != QLatin1String( "qgis/connections-wms/" ) )
130  {
131  if ( mBaseKey != QLatin1String( "qgis/connections-wcs/" ) &&
132  mBaseKey != QLatin1String( "qgis/connections-wfs/" ) )
133  {
134  cbxIgnoreAxisOrientation->setVisible( false );
135  cbxInvertAxisOrientation->setVisible( false );
136  mGroupBox->layout()->removeWidget( cbxIgnoreAxisOrientation );
137  mGroupBox->layout()->removeWidget( cbxInvertAxisOrientation );
138  }
139 
140  if ( mBaseKey == QLatin1String( "qgis/connections-wfs/" ) )
141  {
142  cbxIgnoreAxisOrientation->setText( tr( "Ignore axis orientation (WFS 1.1/WFS 2.0)" ) );
143  }
144 
145  if ( mBaseKey == QLatin1String( "qgis/connections-wcs/" ) )
146  {
147  cbxIgnoreGetMapURI->setText( tr( "Ignore GetCoverage URI reported in capabilities" ) );
148  cbxIgnoreAxisOrientation->setText( tr( "Ignore axis orientation" ) );
149  }
150  else
151  {
152  cbxIgnoreGetMapURI->setVisible( false );
153  cbxSmoothPixmapTransform->setVisible( false );
154  mGroupBox->layout()->removeWidget( cbxIgnoreGetMapURI );
155  mGroupBox->layout()->removeWidget( cbxSmoothPixmapTransform );
156  }
157 
158  cbxIgnoreGetFeatureInfoURI->setVisible( false );
159  mGroupBox->layout()->removeWidget( cbxIgnoreGetFeatureInfoURI );
160 
161  cmbDpiMode->setVisible( false );
162  mGroupBox->layout()->removeWidget( cmbDpiMode );
163  lblDpiMode->setVisible( false );
164  mGroupBox->layout()->removeWidget( lblDpiMode );
165 
166  txtReferer->setVisible( false );
167  mGroupBox->layout()->removeWidget( txtReferer );
168  lblReferer->setVisible( false );
169  mGroupBox->layout()->removeWidget( lblReferer );
170  }
171 
172  if ( mBaseKey != QLatin1String( "qgis/connections-wfs/" ) )
173  {
174  cmbVersion->setVisible( false );
175  mGroupBox->layout()->removeWidget( cmbVersion );
176  lblMaxNumFeatures->setVisible( false );
177  mGroupBox->layout()->removeWidget( lblMaxNumFeatures );
178  txtMaxNumFeatures->setVisible( false );
179  mGroupBox->layout()->removeWidget( txtMaxNumFeatures );
180  }
181 
182  // Adjust height
183  int w = width();
184  adjustSize();
185  resize( w, height() );
186 
187  on_txtName_textChanged( connName );
188 }
189 
191 {
192  Q_UNUSED( text );
193  buttonBox->button( QDialogButtonBox::Ok )->setDisabled( txtName->text().isEmpty() || txtUrl->text().isEmpty() );
194 }
195 
197 {
198  Q_UNUSED( text );
199  buttonBox->button( QDialogButtonBox::Ok )->setDisabled( txtName->text().isEmpty() || txtUrl->text().isEmpty() );
200 }
201 
203 {
204  QgsSettings settings;
205  QString key = mBaseKey + txtName->text();
206  QString credentialsKey = "qgis/" + mCredentialsBaseKey + '/' + txtName->text();
207 
208  // warn if entry was renamed to an existing connection
209  if ( ( mOriginalConnName.isNull() || mOriginalConnName.compare( txtName->text(), Qt::CaseInsensitive ) != 0 ) &&
210  settings.contains( key + "/url" ) &&
211  QMessageBox::question( this,
212  tr( "Save connection" ),
213  tr( "Should the existing connection %1 be overwritten?" ).arg( txtName->text() ),
214  QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
215  {
216  return;
217  }
218 
219  if ( !txtPassword->text().isEmpty() &&
220  QMessageBox::question( this,
221  tr( "Saving passwords" ),
222  trUtf8( "WARNING: You have entered a password. It will be stored in unsecured plain text in your project files and your home directory (Unix-like OS) or user profile (Windows). If you want to avoid this, press Cancel and either:\n\na) Don't provide a password in the connection settings — it will be requested interactively when needed;\nb) Use the Configuration tab to add your credentials in an HTTP Basic Authentication method and store them in an encrypted database." ),
223  QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
224  {
225  return;
226  }
227 
228  // on rename delete original entry first
229  if ( !mOriginalConnName.isNull() && mOriginalConnName != key )
230  {
231  settings.remove( mBaseKey + mOriginalConnName );
232  settings.remove( "qgis/" + mCredentialsBaseKey + '/' + mOriginalConnName );
233  settings.sync();
234  }
235 
236  QUrl url( txtUrl->text().trimmed() );
237  const QList< QPair<QByteArray, QByteArray> > &items = url.encodedQueryItems();
238  QHash< QString, QPair<QByteArray, QByteArray> > params;
239  for ( QList< QPair<QByteArray, QByteArray> >::const_iterator it = items.constBegin(); it != items.constEnd(); ++it )
240  {
241  params.insert( QString( it->first ).toUpper(), *it );
242  }
243 
244  if ( params[QStringLiteral( "SERVICE" )].second.toUpper() == "WMS" ||
245  params[QStringLiteral( "SERVICE" )].second.toUpper() == "WFS" ||
246  params[QStringLiteral( "SERVICE" )].second.toUpper() == "WCS" )
247  {
248  url.removeEncodedQueryItem( params[QStringLiteral( "SERVICE" )].first );
249  url.removeEncodedQueryItem( params[QStringLiteral( "REQUEST" )].first );
250  url.removeEncodedQueryItem( params[QStringLiteral( "FORMAT" )].first );
251  }
252 
253  if ( url.encodedPath().isEmpty() )
254  {
255  url.setEncodedPath( "/" );
256  }
257 
258  settings.setValue( key + "/url", url.toString() );
259 
260  if ( mBaseKey == QLatin1String( "qgis/connections-wms/" ) ||
261  mBaseKey == QLatin1String( "qgis/connections-wcs/" ) ||
262  mBaseKey == QLatin1String( "qgis/connections-wfs/" ) )
263  {
264  settings.setValue( key + "/ignoreAxisOrientation", cbxIgnoreAxisOrientation->isChecked() );
265  settings.setValue( key + "/invertAxisOrientation", cbxInvertAxisOrientation->isChecked() );
266  }
267 
268  if ( mBaseKey == QLatin1String( "qgis/connections-wms/" ) || mBaseKey == QLatin1String( "qgis/connections-wcs/" ) )
269  {
270  settings.setValue( key + "/ignoreGetMapURI", cbxIgnoreGetMapURI->isChecked() );
271  settings.setValue( key + "/smoothPixmapTransform", cbxSmoothPixmapTransform->isChecked() );
272 
273  int dpiMode = 0;
274  switch ( cmbDpiMode->currentIndex() )
275  {
276  case 0: // all => QGIS|UMN|GeoServer
277  dpiMode = 7;
278  break;
279  case 1: // off
280  dpiMode = 0;
281  break;
282  case 2: // QGIS
283  dpiMode = 1;
284  break;
285  case 3: // UMN
286  dpiMode = 2;
287  break;
288  case 4: // GeoServer
289  dpiMode = 4;
290  break;
291  }
292 
293  settings.setValue( key + "/dpiMode", dpiMode );
294  }
295  if ( mBaseKey == QLatin1String( "qgis/connections-wms/" ) )
296  {
297  settings.setValue( key + "/ignoreGetFeatureInfoURI", cbxIgnoreGetFeatureInfoURI->isChecked() );
298  }
299  if ( mBaseKey == QLatin1String( "qgis/connections-wfs/" ) )
300  {
301  QString version = QStringLiteral( "auto" );
302  switch ( cmbVersion->currentIndex() )
303  {
304  case 0:
305  version = QStringLiteral( "auto" );
306  break;
307  case 1:
308  version = QStringLiteral( "1.0.0" );
309  break;
310  case 2:
311  version = QStringLiteral( "1.1.0" );
312  break;
313  case 3:
314  version = QStringLiteral( "2.0.0" );
315  break;
316  }
317  settings.setValue( key + "/version", version );
318 
319  settings.setValue( key + "/maxnumfeatures", txtMaxNumFeatures->text() );
320  }
321 
322  settings.setValue( key + "/referer", txtReferer->text() );
323 
324  settings.setValue( credentialsKey + "/username", txtUserName->text() );
325  settings.setValue( credentialsKey + "/password", txtPassword->text() );
326 
327  settings.setValue( credentialsKey + "/authcfg", mAuthConfigSelect->configId() );
328 
329  settings.setValue( mBaseKey + "/selected", txtName->text() );
330 
331  QDialog::accept();
332 }
This class is a composition of two QSettings instances:
Definition: qgssettings.h:54
bool contains(const QString &key, const QgsSettings::Section section=QgsSettings::NoSection) const
Returns true if there exists a setting called key; returns false otherwise.
void sync()
Writes any unsaved changes to permanent storage, and reloads any settings that have been changed in t...
void on_txtName_textChanged(const QString &)
QgsNewHttpConnection(QWidget *parent=nullptr, const QString &baseKey="qgis/connections-wms/", const QString &connName=QString(), Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags)
Constructor.
void setValue(const QString &key, const QVariant &value, const QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
void setConfigId(const QString &authcfg)
Set the authentication config id for the resource.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
void on_txtUrl_textChanged(const QString &)
Selector widget for authentication configs.
void remove(const QString &key)
Removes the setting key and any sub-settings of key.
const QString configId() const
Get the authentication config id for the resource.