QGIS API Documentation  3.2.0-Bonn (bc43194)
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 
22 #include <QMessageBox>
23 #include <QUrl>
24 #include <QPushButton>
25 #include <QRegExp>
26 #include <QRegExpValidator>
27 
28 QgsNewHttpConnection::QgsNewHttpConnection( QWidget *parent, ConnectionTypes types, const QString &baseKey, const QString &connectionName, QgsNewHttpConnection::Flags flags, Qt::WindowFlags fl )
29  : QDialog( parent, fl )
30  , mTypes( types )
31  , mBaseKey( baseKey )
32  , mOriginalConnName( connectionName )
33 {
34  setupUi( this );
35  connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsNewHttpConnection::showHelp );
36 
37  QRegExp rx( "/connections-([^/]+)/" );
38  if ( rx.indexIn( baseKey ) != -1 )
39  {
40  QString connectionType( rx.cap( 1 ).toUpper() );
41  if ( connectionType == QLatin1String( "WMS" ) )
42  {
43  connectionType = QStringLiteral( "WMS/WMTS" );
44  }
45  setWindowTitle( tr( "Create a New %1 Connection" ).arg( connectionType ) );
46  }
47 
48  // It would be obviously much better to use mBaseKey also for credentials,
49  // but for some strange reason a different hardcoded key was used instead.
50  // WFS and WMS credentials were mixed with the same key WMS.
51  // Only WMS and WFS providers are using QgsNewHttpConnection at this moment
52  // using connection-wms and connection-wfs -> parse credential key fro it.
53  mCredentialsBaseKey = mBaseKey.split( '-' ).last().toUpper();
54 
55  txtName->setValidator( new QRegExpValidator( QRegExp( "[^\\/]+" ), txtName ) );
56 
57  cmbDpiMode->clear();
58  cmbDpiMode->addItem( tr( "all" ) );
59  cmbDpiMode->addItem( tr( "off" ) );
60  cmbDpiMode->addItem( tr( "QGIS" ) );
61  cmbDpiMode->addItem( tr( "UMN" ) );
62  cmbDpiMode->addItem( tr( "GeoServer" ) );
63 
64  cmbVersion->clear();
65  cmbVersion->addItem( tr( "Auto-detect" ) );
66  cmbVersion->addItem( tr( "1.0" ) );
67  cmbVersion->addItem( tr( "1.1" ) );
68  cmbVersion->addItem( tr( "2.0" ) );
69 
70  if ( !connectionName.isEmpty() )
71  {
72  // populate the dialog with the information stored for the connection
73  // populate the fields with the stored setting parameters
74 
75  QgsSettings settings;
76 
77  QString key = mBaseKey + connectionName;
78  QString credentialsKey = "qgis/" + mCredentialsBaseKey + '/' + connectionName;
79  txtName->setText( connectionName );
80  txtUrl->setText( settings.value( key + "/url" ).toString() );
81 
83 
84  // Authentication
85  mAuthSettings->setUsername( settings.value( credentialsKey + "/username" ).toString() );
86  mAuthSettings->setPassword( settings.value( credentialsKey + "/password" ).toString() );
87  mAuthSettings->setConfigId( settings.value( credentialsKey + "/authcfg" ).toString() );
88  }
89 
90  if ( !( mTypes & ConnectionWms ) && !( mTypes & ConnectionWcs ) )
91  {
92  mWmsOptionsGroupBox->setVisible( false );
93  mGroupBox->layout()->removeWidget( mWmsOptionsGroupBox );
94  }
95  if ( !( mTypes & ConnectionWfs ) )
96  {
97  mWfsOptionsGroupBox->setVisible( false );
98  mGroupBox->layout()->removeWidget( mWfsOptionsGroupBox );
99  }
100 
101  if ( mTypes & ConnectionWcs )
102  {
103  cbxIgnoreGetMapURI->setText( tr( "Ignore GetCoverage URI reported in capabilities" ) );
104  cbxWmsIgnoreAxisOrientation->setText( tr( "Ignore axis orientation" ) );
105  if ( !( mTypes & ConnectionWms ) )
106  {
107  mWmsOptionsGroupBox->setTitle( tr( "WCS Options" ) );
108 
109  cbxIgnoreGetFeatureInfoURI->setVisible( false );
110  mGroupBox->layout()->removeWidget( cbxIgnoreGetFeatureInfoURI );
111 
112  cmbDpiMode->setVisible( false );
113  mGroupBox->layout()->removeWidget( cmbDpiMode );
114  lblDpiMode->setVisible( false );
115  mGroupBox->layout()->removeWidget( lblDpiMode );
116 
117  txtReferer->setVisible( false );
118  mGroupBox->layout()->removeWidget( txtReferer );
119  lblReferer->setVisible( false );
120  mGroupBox->layout()->removeWidget( lblReferer );
121  }
122  }
123 
124 
125  if ( !( flags & FlagShowTestConnection ) )
126  {
127  mTestConnectionButton->hide();
128  mGroupBox->layout()->removeWidget( mTestConnectionButton );
129  }
130 
131  if ( flags & FlagHideAuthenticationGroup )
132  {
133  mAuthGroupBox->hide();
134  mGroupBox->layout()->removeWidget( mAuthGroupBox );
135  }
136  // Adjust height
137  int w = width();
138  adjustSize();
139  resize( w, height() );
140 
141  connect( txtName, &QLineEdit::textChanged, this, &QgsNewHttpConnection::nameChanged );
142  connect( txtUrl, &QLineEdit::textChanged, this, &QgsNewHttpConnection::urlChanged );
143 
144  buttonBox->button( QDialogButtonBox::Ok )->setDisabled( true );
145  connect( txtName, &QLineEdit::textChanged, this, &QgsNewHttpConnection::updateOkButtonState );
146  connect( txtUrl, &QLineEdit::textChanged, this, &QgsNewHttpConnection::updateOkButtonState );
147 
148  nameChanged( connectionName );
149 }
150 
152 {
153  return txtName->text();
154 }
155 
157 {
158  return txtUrl->text();
159 }
160 
161 void QgsNewHttpConnection::nameChanged( const QString &text )
162 {
163  Q_UNUSED( text );
164  buttonBox->button( QDialogButtonBox::Ok )->setDisabled( txtName->text().isEmpty() || txtUrl->text().isEmpty() );
165 }
166 
167 void QgsNewHttpConnection::urlChanged( const QString &text )
168 {
169  Q_UNUSED( text );
170  buttonBox->button( QDialogButtonBox::Ok )->setDisabled( txtName->text().isEmpty() || txtUrl->text().isEmpty() );
171 }
172 
173 void QgsNewHttpConnection::updateOkButtonState()
174 {
175  bool enabled = !txtName->text().isEmpty() && !txtUrl->text().isEmpty();
176  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( enabled );
177 }
178 
180 {
181  QgsSettings settings;
182  QString key = mBaseKey + txtName->text();
183 
184  // warn if entry was renamed to an existing connection
185  if ( ( mOriginalConnName.isNull() || mOriginalConnName.compare( txtName->text(), Qt::CaseInsensitive ) != 0 ) &&
186  settings.contains( key + "/url" ) &&
187  QMessageBox::question( this,
188  tr( "Save Connection" ),
189  tr( "Should the existing connection %1 be overwritten?" ).arg( txtName->text() ),
190  QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
191  {
192  return false;
193  }
194 
195  if ( ! mAuthSettings->password().isEmpty() &&
196  QMessageBox::question( this,
197  tr( "Saving Passwords" ),
198  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." ),
199  QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
200  {
201  return false;
202  }
203 
204  return true;
205 }
206 
208 {
209  return mTestConnectionButton;
210 }
211 
212 QString QgsNewHttpConnection::wfsSettingsKey( const QString &base, const QString &connectionName ) const
213 {
214  return base + connectionName;
215 }
216 
217 QString QgsNewHttpConnection::wmsSettingsKey( const QString &base, const QString &connectionName ) const
218 {
219  return base + connectionName;
220 }
221 
223 {
224  QgsSettings settings;
225  QString wfsKey = wfsSettingsKey( mBaseKey, mOriginalConnName );
226  QString wmsKey = wmsSettingsKey( mBaseKey, mOriginalConnName );
227 
228  cbxIgnoreGetMapURI->setChecked( settings.value( wmsKey + "/ignoreGetMapURI", false ).toBool() );
229  cbxWfsIgnoreAxisOrientation->setChecked( settings.value( wfsKey + "/ignoreAxisOrientation", false ).toBool() );
230  cbxWfsInvertAxisOrientation->setChecked( settings.value( wfsKey + "/invertAxisOrientation", false ).toBool() );
231  cbxWmsIgnoreAxisOrientation->setChecked( settings.value( wmsKey + "/ignoreAxisOrientation", false ).toBool() );
232  cbxWmsInvertAxisOrientation->setChecked( settings.value( wmsKey + "/invertAxisOrientation", false ).toBool() );
233  cbxIgnoreGetFeatureInfoURI->setChecked( settings.value( wmsKey + "/ignoreGetFeatureInfoURI", false ).toBool() );
234  cbxSmoothPixmapTransform->setChecked( settings.value( wmsKey + "/smoothPixmapTransform", false ).toBool() );
235 
236  int dpiIdx;
237  switch ( settings.value( wmsKey + "/dpiMode", 7 ).toInt() )
238  {
239  case 0: // off
240  dpiIdx = 1;
241  break;
242  case 1: // QGIS
243  dpiIdx = 2;
244  break;
245  case 2: // UMN
246  dpiIdx = 3;
247  break;
248  case 4: // GeoServer
249  dpiIdx = 4;
250  break;
251  default: // other => all
252  dpiIdx = 0;
253  break;
254  }
255  cmbDpiMode->setCurrentIndex( dpiIdx );
256 
257  QString version = settings.value( wfsKey + "/version" ).toString();
258  int versionIdx = 0; // AUTO
259  if ( version == QLatin1String( "1.0.0" ) )
260  versionIdx = 1;
261  else if ( version == QLatin1String( "1.1.0" ) )
262  versionIdx = 2;
263  else if ( version == QLatin1String( "2.0.0" ) )
264  versionIdx = 3;
265  cmbVersion->setCurrentIndex( versionIdx );
266 
267  txtReferer->setText( settings.value( wmsKey + "/referer" ).toString() );
268  txtMaxNumFeatures->setText( settings.value( wfsKey + "/maxnumfeatures" ).toString() );
269 }
270 
272 {
273  QgsSettings settings;
274  QString key = mBaseKey + txtName->text();
275  QString credentialsKey = "qgis/" + mCredentialsBaseKey + '/' + txtName->text();
276 
277  if ( !validate() )
278  return;
279 
280  // on rename delete original entry first
281  if ( !mOriginalConnName.isNull() && mOriginalConnName != key )
282  {
283  settings.remove( mBaseKey + mOriginalConnName );
284  settings.remove( "qgis/" + mCredentialsBaseKey + '/' + mOriginalConnName );
285  settings.sync();
286  }
287 
288  QUrl url( txtUrl->text().trimmed() );
289  const QList< QPair<QByteArray, QByteArray> > &items = url.encodedQueryItems();
290  QHash< QString, QPair<QByteArray, QByteArray> > params;
291  for ( QList< QPair<QByteArray, QByteArray> >::const_iterator it = items.constBegin(); it != items.constEnd(); ++it )
292  {
293  params.insert( QString( it->first ).toUpper(), *it );
294  }
295 
296  if ( params[QStringLiteral( "SERVICE" )].second.toUpper() == "WMS" ||
297  params[QStringLiteral( "SERVICE" )].second.toUpper() == "WFS" ||
298  params[QStringLiteral( "SERVICE" )].second.toUpper() == "WCS" )
299  {
300  url.removeEncodedQueryItem( params[QStringLiteral( "SERVICE" )].first );
301  url.removeEncodedQueryItem( params[QStringLiteral( "REQUEST" )].first );
302  url.removeEncodedQueryItem( params[QStringLiteral( "FORMAT" )].first );
303  }
304 
305  if ( url.encodedPath().isEmpty() )
306  {
307  url.setEncodedPath( "/" );
308  }
309 
310  settings.setValue( key + "/url", url.toString() );
311 
312  QString wfsKey = wfsSettingsKey( mBaseKey, txtName->text() );
313  QString wmsKey = wmsSettingsKey( mBaseKey, txtName->text() );
314 
315  if ( mTypes & ConnectionWfs )
316  {
317  settings.setValue( wfsKey + "/ignoreAxisOrientation", cbxWfsIgnoreAxisOrientation->isChecked() );
318  settings.setValue( wfsKey + "/invertAxisOrientation", cbxWfsInvertAxisOrientation->isChecked() );
319  }
320  if ( mTypes & ConnectionWms || mTypes & ConnectionWcs )
321  {
322  settings.setValue( wmsKey + "/ignoreAxisOrientation", cbxWmsIgnoreAxisOrientation->isChecked() );
323  settings.setValue( wmsKey + "/invertAxisOrientation", cbxWmsInvertAxisOrientation->isChecked() );
324 
325  settings.setValue( wmsKey + "/ignoreGetMapURI", cbxIgnoreGetMapURI->isChecked() );
326  settings.setValue( wmsKey + "/smoothPixmapTransform", cbxSmoothPixmapTransform->isChecked() );
327 
328  int dpiMode = 0;
329  switch ( cmbDpiMode->currentIndex() )
330  {
331  case 0: // all => QGIS|UMN|GeoServer
332  dpiMode = 7;
333  break;
334  case 1: // off
335  dpiMode = 0;
336  break;
337  case 2: // QGIS
338  dpiMode = 1;
339  break;
340  case 3: // UMN
341  dpiMode = 2;
342  break;
343  case 4: // GeoServer
344  dpiMode = 4;
345  break;
346  }
347 
348  settings.setValue( wmsKey + "/dpiMode", dpiMode );
349 
350  settings.setValue( wmsKey + "/referer", txtReferer->text() );
351  }
352  if ( mTypes & ConnectionWms )
353  {
354  settings.setValue( wmsKey + "/ignoreGetFeatureInfoURI", cbxIgnoreGetFeatureInfoURI->isChecked() );
355  }
356  if ( mTypes & ConnectionWfs )
357  {
358  QString version = QStringLiteral( "auto" );
359  switch ( cmbVersion->currentIndex() )
360  {
361  case 0:
362  version = QStringLiteral( "auto" );
363  break;
364  case 1:
365  version = QStringLiteral( "1.0.0" );
366  break;
367  case 2:
368  version = QStringLiteral( "1.1.0" );
369  break;
370  case 3:
371  version = QStringLiteral( "2.0.0" );
372  break;
373  }
374  settings.setValue( wfsKey + "/version", version );
375 
376  settings.setValue( wfsKey + "/maxnumfeatures", txtMaxNumFeatures->text() );
377  }
378 
379  settings.setValue( credentialsKey + "/username", mAuthSettings->username() );
380  settings.setValue( credentialsKey + "/password", mAuthSettings->password() );
381 
382  settings.setValue( credentialsKey + "/authcfg", mAuthSettings->configId() );
383 
384  settings.setValue( mBaseKey + "/selected", txtName->text() );
385 
386  QDialog::accept();
387 }
388 
389 void QgsNewHttpConnection::showHelp()
390 {
391  QgsHelp::openHelp( QStringLiteral( "working_with_ogc/index.html" ) );
392 }
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.
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 openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:35
bool contains(const QString &key, QgsSettings::Section section=QgsSettings::NoSection) const
Returns true if there exists a setting called key; returns false otherwise.
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.
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 ...