QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 <QSettings>
20 #include <QMessageBox>
21 #include <QUrl>
22 #include <QPushButton>
23 #include <QRegExpValidator>
24 
26  QWidget *parent, const QString& baseKey, const QString& connName, const Qt::WindowFlags& fl )
27  : QDialog( parent, fl )
28  , mBaseKey( baseKey )
29  , mOriginalConnName( connName )
30  , mAuthConfigSelect( nullptr )
31 {
32  setupUi( this );
33 
34  QString service = baseKey.mid( 18, 3 ).toUpper();
35  setWindowTitle( tr( "Create a new %1 connection" ).arg( service ) );
36 
37  // It would be obviously much better to use mBaseKey also for credentials,
38  // but for some strange reason a different hardcoded key was used instead.
39  // WFS and WMS credentials were mixed with the same key WMS.
40  // Only WMS and WFS providers are using QgsNewHttpConnection at this moment
41  // using connection-wms and connection-wfs -> parse credential key fro it.
42  mCredentialsBaseKey = mBaseKey.split( '-' ).last().toUpper();
43 
44  txtName->setValidator( new QRegExpValidator( QRegExp( "[^\\/]+" ), txtName ) );
45 
46  cmbDpiMode->clear();
47  cmbDpiMode->addItem( tr( "all" ) );
48  cmbDpiMode->addItem( tr( "off" ) );
49  cmbDpiMode->addItem( tr( "QGIS" ) );
50  cmbDpiMode->addItem( tr( "UMN" ) );
51  cmbDpiMode->addItem( tr( "GeoServer" ) );
52 
53  cmbVersion->clear();
54  cmbVersion->addItem( tr( "Auto-detect" ) );
55  cmbVersion->addItem( tr( "1.0" ) );
56  cmbVersion->addItem( tr( "1.1" ) );
57  cmbVersion->addItem( tr( "2.0" ) );
58 
59  mAuthConfigSelect = new QgsAuthConfigSelect( this );
60  tabAuth->insertTab( 1, mAuthConfigSelect, tr( "Configurations" ) );
61 
62  if ( !connName.isEmpty() )
63  {
64  // populate the dialog with the information stored for the connection
65  // populate the fields with the stored setting parameters
66 
67  QSettings settings;
68 
69  QString key = mBaseKey + connName;
70  QString credentialsKey = "/Qgis/" + mCredentialsBaseKey + '/' + connName;
71  txtName->setText( connName );
72  txtUrl->setText( settings.value( key + "/url" ).toString() );
73 
74  cbxIgnoreGetMapURI->setChecked( settings.value( key + "/ignoreGetMapURI", false ).toBool() );
75  cbxIgnoreAxisOrientation->setChecked( settings.value( key + "/ignoreAxisOrientation", false ).toBool() );
76  cbxInvertAxisOrientation->setChecked( settings.value( key + "/invertAxisOrientation", false ).toBool() );
77  cbxIgnoreGetFeatureInfoURI->setChecked( settings.value( key + "/ignoreGetFeatureInfoURI", false ).toBool() );
78  cbxSmoothPixmapTransform->setChecked( settings.value( key + "/smoothPixmapTransform", false ).toBool() );
79 
80  int dpiIdx;
81  switch ( settings.value( key + "/dpiMode", 7 ).toInt() )
82  {
83  case 0: // off
84  dpiIdx = 1;
85  break;
86  case 1: // QGIS
87  dpiIdx = 2;
88  break;
89  case 2: // UMN
90  dpiIdx = 3;
91  break;
92  case 4: // GeoServer
93  dpiIdx = 4;
94  break;
95  default: // other => all
96  dpiIdx = 0;
97  break;
98  }
99  cmbDpiMode->setCurrentIndex( dpiIdx );
100 
101  QString version = settings.value( key + "/version" ).toString();
102  int versionIdx = 0; // AUTO
103  if ( version == "1.0.0" )
104  versionIdx = 1;
105  else if ( version == "1.1.0" )
106  versionIdx = 2;
107  else if ( version == "2.0.0" )
108  versionIdx = 3;
109  cmbVersion->setCurrentIndex( versionIdx );
110 
111  txtReferer->setText( settings.value( key + "/referer" ).toString() );
112  txtMaxNumFeatures->setText( settings.value( key + "/maxnumfeatures" ).toString() );
113 
114  txtUserName->setText( settings.value( credentialsKey + "/username" ).toString() );
115  txtPassword->setText( settings.value( credentialsKey + "/password" ).toString() );
116 
117  QString authcfg = settings.value( credentialsKey + "/authcfg" ).toString();
118  mAuthConfigSelect->setConfigId( authcfg );
119  if ( !authcfg.isEmpty() )
120  {
121  tabAuth->setCurrentIndex( tabAuth->indexOf( mAuthConfigSelect ) );
122  }
123  }
124 
125  if ( mBaseKey != "/Qgis/connections-wms/" )
126  {
127  if ( mBaseKey != "/Qgis/connections-wcs/" &&
128  mBaseKey != "/Qgis/connections-wfs/" )
129  {
130  cbxIgnoreAxisOrientation->setVisible( false );
131  cbxInvertAxisOrientation->setVisible( false );
132  mGroupBox->layout()->removeWidget( cbxIgnoreAxisOrientation );
133  mGroupBox->layout()->removeWidget( cbxInvertAxisOrientation );
134  }
135 
136  if ( mBaseKey == "/Qgis/connections-wfs/" )
137  {
138  cbxIgnoreAxisOrientation->setText( tr( "Ignore axis orientation (WFS 1.1/WFS 2.0)" ) );
139  }
140 
141  if ( mBaseKey == "/Qgis/connections-wcs/" )
142  {
143  cbxIgnoreGetMapURI->setText( tr( "Ignore GetCoverage URI reported in capabilities" ) );
144  cbxIgnoreAxisOrientation->setText( tr( "Ignore axis orientation" ) );
145  }
146  else
147  {
148  cbxIgnoreGetMapURI->setVisible( false );
149  cbxSmoothPixmapTransform->setVisible( false );
150  mGroupBox->layout()->removeWidget( cbxIgnoreGetMapURI );
151  mGroupBox->layout()->removeWidget( cbxSmoothPixmapTransform );
152  }
153 
154  cbxIgnoreGetFeatureInfoURI->setVisible( false );
155  mGroupBox->layout()->removeWidget( cbxIgnoreGetFeatureInfoURI );
156 
157  cmbDpiMode->setVisible( false );
158  mGroupBox->layout()->removeWidget( cmbDpiMode );
159  lblDpiMode->setVisible( false );
160  mGroupBox->layout()->removeWidget( lblDpiMode );
161 
162  txtReferer->setVisible( false );
163  mGroupBox->layout()->removeWidget( txtReferer );
164  lblReferer->setVisible( false );
165  mGroupBox->layout()->removeWidget( lblReferer );
166  }
167 
168  if ( mBaseKey != "/Qgis/connections-wfs/" )
169  {
170  cmbVersion->setVisible( false );
171  mGroupBox->layout()->removeWidget( cmbVersion );
172  lblMaxNumFeatures->setVisible( false );
173  mGroupBox->layout()->removeWidget( lblMaxNumFeatures );
174  txtMaxNumFeatures->setVisible( false );
175  mGroupBox->layout()->removeWidget( txtMaxNumFeatures );
176  }
177 
178  // Adjust height
179  int w = width();
180  adjustSize();
181  resize( w, height() );
182 
183  on_txtName_textChanged( connName );
184 }
185 
187 {
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  QSettings 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  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." ),
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();
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["SERVICE"].second.toUpper() == "WMS" ||
245  params["SERVICE"].second.toUpper() == "WFS" ||
246  params["SERVICE"].second.toUpper() == "WCS" )
247  {
248  url.removeEncodedQueryItem( params["SERVICE"].first );
249  url.removeEncodedQueryItem( params["REQUEST"].first );
250  url.removeEncodedQueryItem( params["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 == "/Qgis/connections-wms/" ||
261  mBaseKey == "/Qgis/connections-wcs/" ||
262  mBaseKey == "/Qgis/connections-wfs/" )
263  {
264  settings.setValue( key + "/ignoreAxisOrientation", cbxIgnoreAxisOrientation->isChecked() );
265  settings.setValue( key + "/invertAxisOrientation", cbxInvertAxisOrientation->isChecked() );
266  }
267 
268  if ( mBaseKey == "/Qgis/connections-wms/" || mBaseKey == "/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 == "/Qgis/connections-wms/" )
296  {
297  settings.setValue( key + "/ignoreGetFeatureInfoURI", cbxIgnoreGetFeatureInfoURI->isChecked() );
298  }
299  if ( mBaseKey == "/Qgis/connections-wfs/" )
300  {
301  QString version = "auto";
302  switch ( cmbVersion->currentIndex() )
303  {
304  case 0:
305  version = "auto";
306  break;
307  case 1:
308  version = "1.0.0";
309  break;
310  case 2:
311  version = "1.1.0";
312  break;
313  case 3:
314  version = "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 }
iterator insert(const Key &key, const T &value)
void sync()
void setupUi(QWidget *widget)
QString toUpper() const
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
void adjustSize()
QString tr(const char *sourceText, const char *disambiguation, int n)
void on_txtName_textChanged(const QString &)
bool isNull() const
int width() const
void setValue(const QString &key, const QVariant &value)
void resize(int w, int h)
int toInt(bool *ok) const
void setConfigId(const QString &authcfg)
Set the authentication config id for the resource.
StandardButton question(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
void accept() override
Saves the connection to ~/.qt/qgisrc.
bool contains(const QString &key) const
bool isEmpty() const
virtual void accept()
void remove(const QString &key)
QVariant value(const QString &key, const QVariant &defaultValue) const
QString mid(int position, int n) const
void setWindowTitle(const QString &)
T & last()
bool toBool() const
typedef WindowFlags
void on_txtUrl_textChanged(const QString &)
QgsNewHttpConnection(QWidget *parent=nullptr, const QString &baseKey="/Qgis/connections-wms/", const QString &connName=QString::null, const Qt::WindowFlags &fl=QgisGui::ModalDialogFlags)
Constructor.
int compare(const QString &other) const
QString toString() const
int height() const
Selector widget for authentication configs.
const QString configId() const
Get the authentication config id for the resource.