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