QGIS API Documentation  3.6.0-Noosa (5873452)
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 "qgsauthsettingswidget.h"
19 #include "qgssettings.h"
20 #include "qgshelp.h"
21 #include "qgsgui.h"
22 
23 #include <QMessageBox>
24 #include <QUrl>
25 #include <QPushButton>
26 #include <QRegExp>
27 #include <QRegExpValidator>
28 
29 QgsNewHttpConnection::QgsNewHttpConnection( QWidget *parent, ConnectionTypes types, const QString &baseKey, const QString &connectionName, QgsNewHttpConnection::Flags flags, Qt::WindowFlags fl )
30  : QDialog( parent, fl )
31  , mTypes( types )
32  , mBaseKey( baseKey )
33  , mOriginalConnName( connectionName )
34 {
35  setupUi( this );
36 
37  if ( !( flags & FlagShowHttpSettings ) )
38  mHttpGroupBox->hide();
39 
41 
42  connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsNewHttpConnection::showHelp );
43 
44  QRegExp rx( "/connections-([^/]+)/" );
45  if ( rx.indexIn( baseKey ) != -1 )
46  {
47  QString connectionType( rx.cap( 1 ).toUpper() );
48  if ( connectionType == QLatin1String( "WMS" ) )
49  {
50  connectionType = QStringLiteral( "WMS/WMTS" );
51  }
52  setWindowTitle( tr( "Create a New %1 Connection" ).arg( connectionType ) );
53  }
54 
55  // It would be obviously much better to use mBaseKey also for credentials,
56  // but for some strange reason a different hardcoded key was used instead.
57  // WFS and WMS credentials were mixed with the same key WMS.
58  // Only WMS and WFS providers are using QgsNewHttpConnection at this moment
59  // using connection-wms and connection-wfs -> parse credential key from it.
60  mCredentialsBaseKey = mBaseKey.split( '-' ).last().toUpper();
61 
62  txtName->setValidator( new QRegExpValidator( QRegExp( "[^\\/]+" ), txtName ) );
63 
64  cmbDpiMode->clear();
65  cmbDpiMode->addItem( tr( "all" ) );
66  cmbDpiMode->addItem( tr( "off" ) );
67  cmbDpiMode->addItem( tr( "QGIS" ) );
68  cmbDpiMode->addItem( tr( "UMN" ) );
69  cmbDpiMode->addItem( tr( "GeoServer" ) );
70 
71  cmbVersion->clear();
72  cmbVersion->addItem( tr( "Maximum" ) );
73  cmbVersion->addItem( tr( "1.0" ) );
74  cmbVersion->addItem( tr( "1.1" ) );
75  cmbVersion->addItem( tr( "2.0" ) );
76  connect( cmbVersion,
77  static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ),
78  this, &QgsNewHttpConnection::wfsVersionCurrentIndexChanged );
79 
80  connect( cbxWfsFeaturePaging, &QCheckBox::stateChanged,
81  this, &QgsNewHttpConnection::wfsFeaturePagingStateChanged );
82 
83  if ( !connectionName.isEmpty() )
84  {
85  // populate the dialog with the information stored for the connection
86  // populate the fields with the stored setting parameters
87 
88  QgsSettings settings;
89 
90  QString key = mBaseKey + connectionName;
91  QString credentialsKey = "qgis/" + mCredentialsBaseKey + '/' + connectionName;
92  txtName->setText( connectionName );
93  txtUrl->setText( settings.value( key + "/url" ).toString() );
94  mRefererLineEdit->setText( settings.value( key + "/referer" ).toString() );
95 
97 
98  // Authentication
99  mAuthSettings->setUsername( settings.value( credentialsKey + "/username" ).toString() );
100  mAuthSettings->setPassword( settings.value( credentialsKey + "/password" ).toString() );
101  mAuthSettings->setConfigId( settings.value( credentialsKey + "/authcfg" ).toString() );
102  }
103  mWfsVersionDetectButton->setDisabled( txtUrl->text().isEmpty() );
104 
105  if ( !( mTypes & ConnectionWms ) && !( mTypes & ConnectionWcs ) )
106  {
107  mWmsOptionsGroupBox->setVisible( false );
108  mGroupBox->layout()->removeWidget( mWmsOptionsGroupBox );
109  }
110  if ( !( mTypes & ConnectionWfs ) )
111  {
112  mWfsOptionsGroupBox->setVisible( false );
113  mGroupBox->layout()->removeWidget( mWfsOptionsGroupBox );
114  }
115 
116  if ( mTypes & ConnectionWcs )
117  {
118  cbxIgnoreGetMapURI->setText( tr( "Ignore GetCoverage URI reported in capabilities" ) );
119  cbxWmsIgnoreAxisOrientation->setText( tr( "Ignore axis orientation" ) );
120  if ( !( mTypes & ConnectionWms ) )
121  {
122  mWmsOptionsGroupBox->setTitle( tr( "WCS Options" ) );
123 
124  cbxIgnoreGetFeatureInfoURI->setVisible( false );
125  mGroupBox->layout()->removeWidget( cbxIgnoreGetFeatureInfoURI );
126 
127  cmbDpiMode->setVisible( false );
128  mGroupBox->layout()->removeWidget( cmbDpiMode );
129  lblDpiMode->setVisible( false );
130  mGroupBox->layout()->removeWidget( lblDpiMode );
131 
132  txtReferer->setVisible( false );
133  mGroupBox->layout()->removeWidget( txtReferer );
134  lblReferer->setVisible( false );
135  mGroupBox->layout()->removeWidget( lblReferer );
136  }
137  }
138 
139 
140  if ( !( flags & FlagShowTestConnection ) )
141  {
142  mTestConnectionButton->hide();
143  mGroupBox->layout()->removeWidget( mTestConnectionButton );
144  }
145 
146  if ( flags & FlagHideAuthenticationGroup )
147  {
148  mAuthGroupBox->hide();
149  mGroupBox->layout()->removeWidget( mAuthGroupBox );
150  }
151  // Adjust height
152  int w = width();
153  adjustSize();
154  resize( w, height() );
155 
156  connect( txtName, &QLineEdit::textChanged, this, &QgsNewHttpConnection::nameChanged );
157  connect( txtUrl, &QLineEdit::textChanged, this, &QgsNewHttpConnection::urlChanged );
158 
159  buttonBox->button( QDialogButtonBox::Ok )->setDisabled( true );
160  connect( txtName, &QLineEdit::textChanged, this, &QgsNewHttpConnection::updateOkButtonState );
161  connect( txtUrl, &QLineEdit::textChanged, this, &QgsNewHttpConnection::updateOkButtonState );
162 
163  nameChanged( connectionName );
164 }
165 
166 void QgsNewHttpConnection::wfsVersionCurrentIndexChanged( int index )
167 {
168  cbxWfsFeaturePaging->setEnabled( index == 0 || index == 3 );
169  lblPageSize->setEnabled( index == 0 || index == 3 );
170  txtPageSize->setEnabled( index == 0 || index == 3 );
171  cbxWfsIgnoreAxisOrientation->setEnabled( index != 1 );
172 }
173 
174 void QgsNewHttpConnection::wfsFeaturePagingStateChanged( int state )
175 {
176  lblPageSize->setEnabled( state == Qt::Checked );
177  txtPageSize->setEnabled( state == Qt::Checked );
178 }
179 
181 {
182  return txtName->text();
183 }
184 
186 {
187  return txtUrl->text();
188 }
189 
190 void QgsNewHttpConnection::nameChanged( const QString &text )
191 {
192  Q_UNUSED( text );
193  buttonBox->button( QDialogButtonBox::Ok )->setDisabled( txtName->text().isEmpty() || txtUrl->text().isEmpty() );
194 }
195 
196 void QgsNewHttpConnection::urlChanged( const QString &text )
197 {
198  Q_UNUSED( text );
199  buttonBox->button( QDialogButtonBox::Ok )->setDisabled( txtName->text().isEmpty() || txtUrl->text().isEmpty() );
200  mWfsVersionDetectButton->setDisabled( txtUrl->text().isEmpty() );
201 }
202 
203 void QgsNewHttpConnection::updateOkButtonState()
204 {
205  bool enabled = !txtName->text().isEmpty() && !txtUrl->text().isEmpty();
206  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( enabled );
207 }
208 
210 {
211  QgsSettings settings;
212  QString key = mBaseKey + txtName->text();
213 
214  // warn if entry was renamed to an existing connection
215  if ( ( mOriginalConnName.isNull() || mOriginalConnName.compare( txtName->text(), Qt::CaseInsensitive ) != 0 ) &&
216  settings.contains( key + "/url" ) &&
217  QMessageBox::question( this,
218  tr( "Save Connection" ),
219  tr( "Should the existing connection %1 be overwritten?" ).arg( txtName->text() ),
220  QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
221  {
222  return false;
223  }
224 
225  if ( ! mAuthSettings->password().isEmpty() &&
226  QMessageBox::question( this,
227  tr( "Saving Passwords" ),
228  tr( "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." ),
229  QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
230  {
231  return false;
232  }
233 
234  return true;
235 }
236 
238 {
239  return mTestConnectionButton;
240 }
241 
243 {
244  return mWfsVersionDetectButton;
245 }
246 
248 {
249  return cmbVersion;
250 }
251 
253 {
254  return cbxWfsFeaturePaging;
255 }
256 
258 {
259  return txtPageSize;
260 }
261 
262 QString QgsNewHttpConnection::wfsSettingsKey( const QString &base, const QString &connectionName ) const
263 {
264  return base + connectionName;
265 }
266 
267 QString QgsNewHttpConnection::wmsSettingsKey( const QString &base, const QString &connectionName ) const
268 {
269  return base + connectionName;
270 }
271 
273 {
274  QgsSettings settings;
275  QString wfsKey = wfsSettingsKey( mBaseKey, mOriginalConnName );
276  QString wmsKey = wmsSettingsKey( mBaseKey, mOriginalConnName );
277 
278  cbxIgnoreGetMapURI->setChecked( settings.value( wmsKey + "/ignoreGetMapURI", false ).toBool() );
279  cbxWfsIgnoreAxisOrientation->setChecked( settings.value( wfsKey + "/ignoreAxisOrientation", false ).toBool() );
280  cbxWfsInvertAxisOrientation->setChecked( settings.value( wfsKey + "/invertAxisOrientation", false ).toBool() );
281  cbxWmsIgnoreAxisOrientation->setChecked( settings.value( wmsKey + "/ignoreAxisOrientation", false ).toBool() );
282  cbxWmsInvertAxisOrientation->setChecked( settings.value( wmsKey + "/invertAxisOrientation", false ).toBool() );
283  cbxIgnoreGetFeatureInfoURI->setChecked( settings.value( wmsKey + "/ignoreGetFeatureInfoURI", false ).toBool() );
284  cbxSmoothPixmapTransform->setChecked( settings.value( wmsKey + "/smoothPixmapTransform", false ).toBool() );
285 
286  int dpiIdx;
287  switch ( settings.value( wmsKey + "/dpiMode", 7 ).toInt() )
288  {
289  case 0: // off
290  dpiIdx = 1;
291  break;
292  case 1: // QGIS
293  dpiIdx = 2;
294  break;
295  case 2: // UMN
296  dpiIdx = 3;
297  break;
298  case 4: // GeoServer
299  dpiIdx = 4;
300  break;
301  default: // other => all
302  dpiIdx = 0;
303  break;
304  }
305  cmbDpiMode->setCurrentIndex( dpiIdx );
306 
307  QString version = settings.value( wfsKey + "/version" ).toString();
308  int versionIdx = 0; // AUTO
309  if ( version == QLatin1String( "1.0.0" ) )
310  versionIdx = 1;
311  else if ( version == QLatin1String( "1.1.0" ) )
312  versionIdx = 2;
313  else if ( version == QLatin1String( "2.0.0" ) )
314  versionIdx = 3;
315  cmbVersion->setCurrentIndex( versionIdx );
316 
317  txtReferer->setText( settings.value( wmsKey + "/referer" ).toString() );
318  txtMaxNumFeatures->setText( settings.value( wfsKey + "/maxnumfeatures" ).toString() );
319 
320  bool pagingEnabled = settings.value( wfsKey + "/pagingenabled", true ).toBool();
321  txtPageSize->setText( settings.value( wfsKey + "/pagesize" ).toString() );
322  cbxWfsFeaturePaging->setChecked( pagingEnabled );
323 
324  txtPageSize->setEnabled( pagingEnabled );
325  lblPageSize->setEnabled( pagingEnabled );
326  cbxWfsFeaturePaging->setEnabled( pagingEnabled );
327 }
328 
330 {
331 
332  QUrl url( txtUrl->text().trimmed() );
333  const QList< QPair<QByteArray, QByteArray> > &items = url.encodedQueryItems();
334  QHash< QString, QPair<QByteArray, QByteArray> > params;
335  for ( QList< QPair<QByteArray, QByteArray> >::const_iterator it = items.constBegin(); it != items.constEnd(); ++it )
336  {
337  params.insert( QString( it->first ).toUpper(), *it );
338  }
339 
340  if ( params[QStringLiteral( "SERVICE" )].second.toUpper() == "WMS" ||
341  params[QStringLiteral( "SERVICE" )].second.toUpper() == "WFS" ||
342  params[QStringLiteral( "SERVICE" )].second.toUpper() == "WCS" )
343  {
344  url.removeEncodedQueryItem( params[QStringLiteral( "SERVICE" )].first );
345  url.removeEncodedQueryItem( params[QStringLiteral( "REQUEST" )].first );
346  url.removeEncodedQueryItem( params[QStringLiteral( "FORMAT" )].first );
347  }
348 
349  if ( url.encodedPath().isEmpty() )
350  {
351  url.setEncodedPath( "/" );
352  }
353  return url;
354 }
355 
357 {
358  QgsSettings settings;
359  QString key = mBaseKey + txtName->text();
360  QString credentialsKey = "qgis/" + mCredentialsBaseKey + '/' + txtName->text();
361 
362  if ( !validate() )
363  return;
364 
365  // on rename delete original entry first
366  if ( !mOriginalConnName.isNull() && mOriginalConnName != key )
367  {
368  settings.remove( mBaseKey + mOriginalConnName );
369  settings.remove( "qgis/" + mCredentialsBaseKey + '/' + mOriginalConnName );
370  settings.sync();
371  }
372 
373  QUrl url( urlTrimmed() );
374  settings.setValue( key + "/url", url.toString() );
375 
376  QString wfsKey = wfsSettingsKey( mBaseKey, txtName->text() );
377  QString wmsKey = wmsSettingsKey( mBaseKey, txtName->text() );
378 
379  if ( mTypes & ConnectionWfs )
380  {
381  settings.setValue( wfsKey + "/ignoreAxisOrientation", cbxWfsIgnoreAxisOrientation->isChecked() );
382  settings.setValue( wfsKey + "/invertAxisOrientation", cbxWfsInvertAxisOrientation->isChecked() );
383  }
384  if ( mTypes & ConnectionWms || mTypes & ConnectionWcs )
385  {
386  settings.setValue( wmsKey + "/ignoreAxisOrientation", cbxWmsIgnoreAxisOrientation->isChecked() );
387  settings.setValue( wmsKey + "/invertAxisOrientation", cbxWmsInvertAxisOrientation->isChecked() );
388 
389  settings.setValue( wmsKey + "/ignoreGetMapURI", cbxIgnoreGetMapURI->isChecked() );
390  settings.setValue( wmsKey + "/smoothPixmapTransform", cbxSmoothPixmapTransform->isChecked() );
391 
392  int dpiMode = 0;
393  switch ( cmbDpiMode->currentIndex() )
394  {
395  case 0: // all => QGIS|UMN|GeoServer
396  dpiMode = 7;
397  break;
398  case 1: // off
399  dpiMode = 0;
400  break;
401  case 2: // QGIS
402  dpiMode = 1;
403  break;
404  case 3: // UMN
405  dpiMode = 2;
406  break;
407  case 4: // GeoServer
408  dpiMode = 4;
409  break;
410  }
411 
412  settings.setValue( wmsKey + "/dpiMode", dpiMode );
413 
414  settings.setValue( wmsKey + "/referer", txtReferer->text() );
415  }
416  if ( mTypes & ConnectionWms )
417  {
418  settings.setValue( wmsKey + "/ignoreGetFeatureInfoURI", cbxIgnoreGetFeatureInfoURI->isChecked() );
419  }
420  if ( mTypes & ConnectionWfs )
421  {
422  QString version = QStringLiteral( "auto" );
423  switch ( cmbVersion->currentIndex() )
424  {
425  case 0:
426  version = QStringLiteral( "auto" );
427  break;
428  case 1:
429  version = QStringLiteral( "1.0.0" );
430  break;
431  case 2:
432  version = QStringLiteral( "1.1.0" );
433  break;
434  case 3:
435  version = QStringLiteral( "2.0.0" );
436  break;
437  }
438  settings.setValue( wfsKey + "/version", version );
439 
440  settings.setValue( wfsKey + "/maxnumfeatures", txtMaxNumFeatures->text() );
441 
442  settings.setValue( wfsKey + "/pagesize", txtPageSize->text() );
443  settings.setValue( wfsKey + "/pagingenabled", cbxWfsFeaturePaging->isChecked() );
444  }
445 
446  settings.setValue( credentialsKey + "/username", mAuthSettings->username() );
447  settings.setValue( credentialsKey + "/password", mAuthSettings->password() );
448 
449  settings.setValue( credentialsKey + "/authcfg", mAuthSettings->configId() );
450 
451  if ( mHttpGroupBox->isVisible() )
452  settings.setValue( key + "/referer", mRefererLineEdit->text() );
453 
454  settings.setValue( mBaseKey + "/selected", txtName->text() );
455 
456  QDialog::accept();
457 }
458 
459 void QgsNewHttpConnection::showHelp()
460 {
461  QgsHelp::openHelp( QStringLiteral( "working_with_ogc/index.html" ) );
462 }
Display the &#39;http&#39; group.
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.
void sync()
Writes any unsaved changes to permanent storage, and reloads any settings that have been changed in t...
void updateServiceSpecificSettings()
Triggers a resync of the GUI widgets for the service specific settings (i.e.
QString url() const
Returns the current connection url.
virtual QString wmsSettingsKey(const QString &base, const QString &connectionName) const
Returns the QSettings key for WMS related settings for the connection.
QString name() const
Returns the current connection name.
QComboBox * wfsVersionComboBox()
Returns the "WFS version" combobox.
QUrl urlTrimmed() const
Returns the url.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
virtual QString wfsSettingsKey(const QString &base, const QString &connectionName) const
Returns the QSettings key for WFS related settings for the connection.
QPushButton * testConnectButton()
Returns the "test connection" button.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition: qgsgui.cpp:104
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:36
bool contains(const QString &key, QgsSettings::Section section=QgsSettings::NoSection) const
Returns true if there exists a setting called key; returns false otherwise.
QCheckBox * wfsPagingEnabledCheckBox()
Returns the "WFS paging enabled" checkbox.
QgsNewHttpConnection(QWidget *parent=nullptr, QgsNewHttpConnection::ConnectionTypes types=ConnectionWms, const QString &baseKey="qgis/connections-wms/", const QString &connectionName=QString(), QgsNewHttpConnection::Flags flags=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags)
Constructor for QgsNewHttpConnection.
QLineEdit * wfsPageSizeLineEdit()
Returns the "WFS page size" edit.
QPushButton * wfsVersionDetectButton()
Returns the "WFS version detect" button.
Display the &#39;test connection&#39; button.
virtual bool validate()
Returns true if dialog settings are valid, or false if current settings are not valid and the dialog ...