QGIS API Documentation  2.99.0-Master (40f86b2)
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 <QRegExpValidator>
26 
28  QWidget *parent, const QString &baseKey, const QString &connName, Qt::WindowFlags fl )
29  : QDialog( parent, fl )
30  , mBaseKey( baseKey )
31  , mOriginalConnName( connName )
32  , mAuthConfigSelect( nullptr )
33 {
34  setupUi( this );
35 
36  QString service = baseKey.mid( 18, 3 ).toUpper();
37  setWindowTitle( tr( "Create a new %1 connection" ).arg( service ) );
38 
39  // It would be obviously much better to use mBaseKey also for credentials,
40  // but for some strange reason a different hardcoded key was used instead.
41  // WFS and WMS credentials were mixed with the same key WMS.
42  // Only WMS and WFS providers are using QgsNewHttpConnection at this moment
43  // using connection-wms and connection-wfs -> parse credential key fro it.
44  mCredentialsBaseKey = mBaseKey.split( '-' ).last().toUpper();
45 
46  txtName->setValidator( new QRegExpValidator( QRegExp( "[^\\/]+" ), txtName ) );
47 
48  cmbDpiMode->clear();
49  cmbDpiMode->addItem( tr( "all" ) );
50  cmbDpiMode->addItem( tr( "off" ) );
51  cmbDpiMode->addItem( tr( "QGIS" ) );
52  cmbDpiMode->addItem( tr( "UMN" ) );
53  cmbDpiMode->addItem( tr( "GeoServer" ) );
54 
55  cmbVersion->clear();
56  cmbVersion->addItem( tr( "Auto-detect" ) );
57  cmbVersion->addItem( tr( "1.0" ) );
58  cmbVersion->addItem( tr( "1.1" ) );
59  cmbVersion->addItem( tr( "2.0" ) );
60 
61  mAuthConfigSelect = new QgsAuthConfigSelect( this );
62  tabAuth->insertTab( 1, mAuthConfigSelect, tr( "Configurations" ) );
63 
64  if ( !connName.isEmpty() )
65  {
66  // populate the dialog with the information stored for the connection
67  // populate the fields with the stored setting parameters
68 
69  QgsSettings settings;
70 
71  QString key = mBaseKey + connName;
72  QString credentialsKey = "qgis//" + mCredentialsBaseKey + '/' + connName;
73  txtName->setText( connName );
74  txtUrl->setText( settings.value( key + "/url" ).toString() );
75 
76  cbxIgnoreGetMapURI->setChecked( settings.value( key + "/ignoreGetMapURI", false ).toBool() );
77  cbxIgnoreAxisOrientation->setChecked( settings.value( key + "/ignoreAxisOrientation", false ).toBool() );
78  cbxInvertAxisOrientation->setChecked( settings.value( key + "/invertAxisOrientation", false ).toBool() );
79  cbxIgnoreGetFeatureInfoURI->setChecked( settings.value( key + "/ignoreGetFeatureInfoURI", false ).toBool() );
80  cbxSmoothPixmapTransform->setChecked( settings.value( key + "/smoothPixmapTransform", false ).toBool() );
81 
82  int dpiIdx;
83  switch ( settings.value( key + "/dpiMode", 7 ).toInt() )
84  {
85  case 0: // off
86  dpiIdx = 1;
87  break;
88  case 1: // QGIS
89  dpiIdx = 2;
90  break;
91  case 2: // UMN
92  dpiIdx = 3;
93  break;
94  case 4: // GeoServer
95  dpiIdx = 4;
96  break;
97  default: // other => all
98  dpiIdx = 0;
99  break;
100  }
101  cmbDpiMode->setCurrentIndex( dpiIdx );
102 
103  QString version = settings.value( key + "/version" ).toString();
104  int versionIdx = 0; // AUTO
105  if ( version == QLatin1String( "1.0.0" ) )
106  versionIdx = 1;
107  else if ( version == QLatin1String( "1.1.0" ) )
108  versionIdx = 2;
109  else if ( version == QLatin1String( "2.0.0" ) )
110  versionIdx = 3;
111  cmbVersion->setCurrentIndex( versionIdx );
112 
113  txtReferer->setText( settings.value( key + "/referer" ).toString() );
114  txtMaxNumFeatures->setText( settings.value( key + "/maxnumfeatures" ).toString() );
115 
116  txtUserName->setText( settings.value( credentialsKey + "/username" ).toString() );
117  txtPassword->setText( settings.value( credentialsKey + "/password" ).toString() );
118 
119  QString authcfg = settings.value( credentialsKey + "/authcfg" ).toString();
120  mAuthConfigSelect->setConfigId( authcfg );
121  if ( !authcfg.isEmpty() )
122  {
123  tabAuth->setCurrentIndex( tabAuth->indexOf( mAuthConfigSelect ) );
124  }
125  }
126 
127  if ( mBaseKey != QLatin1String( "qgis//connections-wms/" ) )
128  {
129  if ( mBaseKey != QLatin1String( "qgis//connections-wcs/" ) &&
130  mBaseKey != QLatin1String( "qgis//connections-wfs/" ) )
131  {
132  cbxIgnoreAxisOrientation->setVisible( false );
133  cbxInvertAxisOrientation->setVisible( false );
134  mGroupBox->layout()->removeWidget( cbxIgnoreAxisOrientation );
135  mGroupBox->layout()->removeWidget( cbxInvertAxisOrientation );
136  }
137 
138  if ( mBaseKey == QLatin1String( "qgis//connections-wfs/" ) )
139  {
140  cbxIgnoreAxisOrientation->setText( tr( "Ignore axis orientation (WFS 1.1/WFS 2.0)" ) );
141  }
142 
143  if ( mBaseKey == QLatin1String( "qgis//connections-wcs/" ) )
144  {
145  cbxIgnoreGetMapURI->setText( tr( "Ignore GetCoverage URI reported in capabilities" ) );
146  cbxIgnoreAxisOrientation->setText( tr( "Ignore axis orientation" ) );
147  }
148  else
149  {
150  cbxIgnoreGetMapURI->setVisible( false );
151  cbxSmoothPixmapTransform->setVisible( false );
152  mGroupBox->layout()->removeWidget( cbxIgnoreGetMapURI );
153  mGroupBox->layout()->removeWidget( cbxSmoothPixmapTransform );
154  }
155 
156  cbxIgnoreGetFeatureInfoURI->setVisible( false );
157  mGroupBox->layout()->removeWidget( cbxIgnoreGetFeatureInfoURI );
158 
159  cmbDpiMode->setVisible( false );
160  mGroupBox->layout()->removeWidget( cmbDpiMode );
161  lblDpiMode->setVisible( false );
162  mGroupBox->layout()->removeWidget( lblDpiMode );
163 
164  txtReferer->setVisible( false );
165  mGroupBox->layout()->removeWidget( txtReferer );
166  lblReferer->setVisible( false );
167  mGroupBox->layout()->removeWidget( lblReferer );
168  }
169 
170  if ( mBaseKey != QLatin1String( "qgis//connections-wfs/" ) )
171  {
172  cmbVersion->setVisible( false );
173  mGroupBox->layout()->removeWidget( cmbVersion );
174  lblMaxNumFeatures->setVisible( false );
175  mGroupBox->layout()->removeWidget( lblMaxNumFeatures );
176  txtMaxNumFeatures->setVisible( false );
177  mGroupBox->layout()->removeWidget( txtMaxNumFeatures );
178  }
179 
180  // Adjust height
181  int w = width();
182  adjustSize();
183  resize( w, height() );
184 
185  on_txtName_textChanged( connName );
186 }
187 
189 {
190  Q_UNUSED( text );
191  buttonBox->button( QDialogButtonBox::Ok )->setDisabled( txtName->text().isEmpty() || txtUrl->text().isEmpty() );
192 }
193 
195 {
196  Q_UNUSED( text );
197  buttonBox->button( QDialogButtonBox::Ok )->setDisabled( txtName->text().isEmpty() || txtUrl->text().isEmpty() );
198 }
199 
201 {
202  QgsSettings settings;
203  QString key = mBaseKey + txtName->text();
204  QString credentialsKey = "qgis//" + mCredentialsBaseKey + '/' + txtName->text();
205 
206  // warn if entry was renamed to an existing connection
207  if ( ( mOriginalConnName.isNull() || mOriginalConnName.compare( txtName->text(), Qt::CaseInsensitive ) != 0 ) &&
208  settings.contains( key + "/url" ) &&
209  QMessageBox::question( this,
210  tr( "Save connection" ),
211  tr( "Should the existing connection %1 be overwritten?" ).arg( txtName->text() ),
212  QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
213  {
214  return;
215  }
216 
217  if ( !txtPassword->text().isEmpty() &&
218  QMessageBox::question( this,
219  tr( "Saving passwords" ),
220  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." ),
221  QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
222  {
223  return;
224  }
225 
226  // on rename delete original entry first
227  if ( !mOriginalConnName.isNull() && mOriginalConnName != key )
228  {
229  settings.remove( mBaseKey + mOriginalConnName );
230  settings.remove( "qgis//" + mCredentialsBaseKey + '/' + mOriginalConnName );
231  settings.sync();
232  }
233 
234  QUrl url( txtUrl->text().trimmed() );
235  const QList< QPair<QByteArray, QByteArray> > &items = url.encodedQueryItems();
236  QHash< QString, QPair<QByteArray, QByteArray> > params;
237  for ( QList< QPair<QByteArray, QByteArray> >::const_iterator it = items.constBegin(); it != items.constEnd(); ++it )
238  {
239  params.insert( QString( it->first ).toUpper(), *it );
240  }
241 
242  if ( params[QStringLiteral( "SERVICE" )].second.toUpper() == "WMS" ||
243  params[QStringLiteral( "SERVICE" )].second.toUpper() == "WFS" ||
244  params[QStringLiteral( "SERVICE" )].second.toUpper() == "WCS" )
245  {
246  url.removeEncodedQueryItem( params[QStringLiteral( "SERVICE" )].first );
247  url.removeEncodedQueryItem( params[QStringLiteral( "REQUEST" )].first );
248  url.removeEncodedQueryItem( params[QStringLiteral( "FORMAT" )].first );
249  }
250 
251  if ( url.encodedPath().isEmpty() )
252  {
253  url.setEncodedPath( "/" );
254  }
255 
256  settings.setValue( key + "/url", url.toString() );
257 
258  if ( mBaseKey == QLatin1String( "qgis//connections-wms/" ) ||
259  mBaseKey == QLatin1String( "qgis//connections-wcs/" ) ||
260  mBaseKey == QLatin1String( "qgis//connections-wfs/" ) )
261  {
262  settings.setValue( key + "/ignoreAxisOrientation", cbxIgnoreAxisOrientation->isChecked() );
263  settings.setValue( key + "/invertAxisOrientation", cbxInvertAxisOrientation->isChecked() );
264  }
265 
266  if ( mBaseKey == QLatin1String( "qgis//connections-wms/" ) || mBaseKey == QLatin1String( "qgis//connections-wcs/" ) )
267  {
268  settings.setValue( key + "/ignoreGetMapURI", cbxIgnoreGetMapURI->isChecked() );
269  settings.setValue( key + "/smoothPixmapTransform", cbxSmoothPixmapTransform->isChecked() );
270 
271  int dpiMode = 0;
272  switch ( cmbDpiMode->currentIndex() )
273  {
274  case 0: // all => QGIS|UMN|GeoServer
275  dpiMode = 7;
276  break;
277  case 1: // off
278  dpiMode = 0;
279  break;
280  case 2: // QGIS
281  dpiMode = 1;
282  break;
283  case 3: // UMN
284  dpiMode = 2;
285  break;
286  case 4: // GeoServer
287  dpiMode = 4;
288  break;
289  }
290 
291  settings.setValue( key + "/dpiMode", dpiMode );
292  }
293  if ( mBaseKey == QLatin1String( "qgis//connections-wms/" ) )
294  {
295  settings.setValue( key + "/ignoreGetFeatureInfoURI", cbxIgnoreGetFeatureInfoURI->isChecked() );
296  }
297  if ( mBaseKey == QLatin1String( "qgis//connections-wfs/" ) )
298  {
299  QString version = QStringLiteral( "auto" );
300  switch ( cmbVersion->currentIndex() )
301  {
302  case 0:
303  version = QStringLiteral( "auto" );
304  break;
305  case 1:
306  version = QStringLiteral( "1.0.0" );
307  break;
308  case 2:
309  version = QStringLiteral( "1.1.0" );
310  break;
311  case 3:
312  version = QStringLiteral( "2.0.0" );
313  break;
314  }
315  settings.setValue( key + "/version", version );
316 
317  settings.setValue( key + "/maxnumfeatures", txtMaxNumFeatures->text() );
318  }
319 
320  settings.setValue( key + "/referer", txtReferer->text() );
321 
322  settings.setValue( credentialsKey + "/username", txtUserName->text() );
323  settings.setValue( credentialsKey + "/password", txtPassword->text() );
324 
325  settings.setValue( credentialsKey + "/authcfg", mAuthConfigSelect->configId() );
326 
327  settings.setValue( mBaseKey + "/selected", txtName->text() );
328 
329  QDialog::accept();
330 }
This class is a composition of two QSettings instances:
Definition: qgssettings.h:51
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::null, Qt::WindowFlags fl=QgisGui::ModalDialogFlags)
Constructor.
void setConfigId(const QString &authcfg)
Set the authentication config id for the resource.
bool contains(const QString &key, const Section section=Section::NoSection) const
Returns true if there exists a setting called key; returns false otherwise.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=Section::NoSection) const
Returns the value for setting key.
void on_txtUrl_textChanged(const QString &)
void setValue(const QString &key, const QVariant &value, const Section section=Section::NoSection)
Sets the value of setting key to value.
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.