QGIS API Documentation  2.13.0-Master
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  mAuthConfigSelect = new QgsAuthConfigSelect( this );
54  tabAuth->insertTab( 1, mAuthConfigSelect, tr( "Configurations" ) );
55 
56  if ( !connName.isEmpty() )
57  {
58  // populate the dialog with the information stored for the connection
59  // populate the fields with the stored setting parameters
60 
61  QSettings settings;
62 
63  QString key = mBaseKey + connName;
64  QString credentialsKey = "/Qgis/" + mCredentialsBaseKey + '/' + connName;
65  txtName->setText( connName );
66  txtUrl->setText( settings.value( key + "/url" ).toString() );
67 
68  cbxIgnoreGetMapURI->setChecked( settings.value( key + "/ignoreGetMapURI", false ).toBool() );
69  cbxIgnoreAxisOrientation->setChecked( settings.value( key + "/ignoreAxisOrientation", false ).toBool() );
70  cbxInvertAxisOrientation->setChecked( settings.value( key + "/invertAxisOrientation", false ).toBool() );
71  cbxIgnoreGetFeatureInfoURI->setChecked( settings.value( key + "/ignoreGetFeatureInfoURI", false ).toBool() );
72  cbxSmoothPixmapTransform->setChecked( settings.value( key + "/smoothPixmapTransform", false ).toBool() );
73 
74  int dpiIdx;
75  switch ( settings.value( key + "/dpiMode", 7 ).toInt() )
76  {
77  case 0: // off
78  dpiIdx = 1;
79  break;
80  case 1: // QGIS
81  dpiIdx = 2;
82  break;
83  case 2: // UMN
84  dpiIdx = 3;
85  break;
86  case 4: // GeoServer
87  dpiIdx = 4;
88  break;
89  default: // other => all
90  dpiIdx = 0;
91  break;
92  }
93  cmbDpiMode->setCurrentIndex( dpiIdx );
94 
95  txtReferer->setText( settings.value( key + "/referer" ).toString() );
96 
97  txtUserName->setText( settings.value( credentialsKey + "/username" ).toString() );
98  txtPassword->setText( settings.value( credentialsKey + "/password" ).toString() );
99 
100  QString authcfg = settings.value( credentialsKey + "/authcfg" ).toString();
101  mAuthConfigSelect->setConfigId( authcfg );
102  if ( !authcfg.isEmpty() )
103  {
104  tabAuth->setCurrentIndex( tabAuth->indexOf( mAuthConfigSelect ) );
105  }
106  }
107 
108  if ( mBaseKey != "/Qgis/connections-wms/" )
109  {
110  if ( mBaseKey == "/Qgis/connections-wcs/" )
111  {
112  cbxIgnoreGetMapURI->setText( tr( "Ignore GetCoverage URI reported in capabilities" ) );
113  cbxIgnoreAxisOrientation->setText( tr( "Ignore axis orientation" ) );
114  }
115  else
116  {
117  cbxIgnoreGetMapURI->setVisible( false );
118  cbxIgnoreAxisOrientation->setVisible( false );
119  cbxInvertAxisOrientation->setVisible( false );
120  cbxSmoothPixmapTransform->setVisible( false );
121  mGroupBox->layout()->removeWidget( cbxIgnoreGetMapURI );
122  mGroupBox->layout()->removeWidget( cbxIgnoreAxisOrientation );
123  mGroupBox->layout()->removeWidget( cbxInvertAxisOrientation );
124  mGroupBox->layout()->removeWidget( cbxSmoothPixmapTransform );
125  }
126 
127  cbxIgnoreGetFeatureInfoURI->setVisible( false );
128  mGroupBox->layout()->removeWidget( cbxIgnoreGetFeatureInfoURI );
129 
130  cmbDpiMode->setVisible( false );
131  mGroupBox->layout()->removeWidget( cmbDpiMode );
132  lblDpiMode->setVisible( false );
133  mGroupBox->layout()->removeWidget( lblDpiMode );
134 
135  txtReferer->setVisible( false );
136  mGroupBox->layout()->removeWidget( txtReferer );
137  lblReferer->setVisible( false );
138  mGroupBox->layout()->removeWidget( lblReferer );
139 
140  // Adjust height
141  int w = width();
142  adjustSize();
143  resize( w, height() );
144  }
145 
146  on_txtName_textChanged( connName );
147 }
148 
150 {
151 }
152 
154 {
155  Q_UNUSED( text );
156  buttonBox->button( QDialogButtonBox::Ok )->setDisabled( txtName->text().isEmpty() || txtUrl->text().isEmpty() );
157 }
158 
160 {
161  Q_UNUSED( text );
162  buttonBox->button( QDialogButtonBox::Ok )->setDisabled( txtName->text().isEmpty() || txtUrl->text().isEmpty() );
163 }
164 
166 {
167  QSettings settings;
168  QString key = mBaseKey + txtName->text();
169  QString credentialsKey = "/Qgis/" + mCredentialsBaseKey + '/' + txtName->text();
170 
171  // warn if entry was renamed to an existing connection
172  if (( mOriginalConnName.isNull() || mOriginalConnName.compare( txtName->text(), Qt::CaseInsensitive ) != 0 ) &&
173  settings.contains( key + "/url" ) &&
174  QMessageBox::question( this,
175  tr( "Save connection" ),
176  tr( "Should the existing connection %1 be overwritten?" ).arg( txtName->text() ),
177  QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
178  {
179  return;
180  }
181 
182  if ( !txtPassword->text().isEmpty() &&
183  QMessageBox::question( this,
184  tr( "Saving passwords" ),
185  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." ),
186  QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
187  {
188  return;
189  }
190 
191  // on rename delete original entry first
192  if ( !mOriginalConnName.isNull() && mOriginalConnName != key )
193  {
194  settings.remove( mBaseKey + mOriginalConnName );
195  settings.remove( "/Qgis/" + mCredentialsBaseKey + '/' + mOriginalConnName );
196  settings.sync();
197  }
198 
199  QUrl url( txtUrl->text().trimmed() );
200  const QList< QPair<QByteArray, QByteArray> > &items = url.encodedQueryItems();
202  for ( QList< QPair<QByteArray, QByteArray> >::const_iterator it = items.constBegin(); it != items.constEnd(); ++it )
203  {
204  params.insert( QString( it->first ).toUpper(), *it );
205  }
206 
207  if ( params["SERVICE"].second.toUpper() == "WMS" ||
208  params["SERVICE"].second.toUpper() == "WFS" ||
209  params["SERVICE"].second.toUpper() == "WCS" )
210  {
211  url.removeEncodedQueryItem( params["SERVICE"].first );
212  url.removeEncodedQueryItem( params["REQUEST"].first );
213  url.removeEncodedQueryItem( params["FORMAT"].first );
214  }
215 
216  if ( url.encodedPath().isEmpty() )
217  {
218  url.setEncodedPath( "/" );
219  }
220 
221  settings.setValue( key + "/url", url.toString() );
222  if ( mBaseKey == "/Qgis/connections-wms/" || mBaseKey == "/Qgis/connections-wcs/" )
223  {
224  settings.setValue( key + "/ignoreGetMapURI", cbxIgnoreGetMapURI->isChecked() );
225  settings.setValue( key + "/ignoreAxisOrientation", cbxIgnoreAxisOrientation->isChecked() );
226  settings.setValue( key + "/invertAxisOrientation", cbxInvertAxisOrientation->isChecked() );
227  settings.setValue( key + "/smoothPixmapTransform", cbxSmoothPixmapTransform->isChecked() );
228 
229  int dpiMode = 0;
230  switch ( cmbDpiMode->currentIndex() )
231  {
232  case 0: // all => QGIS|UMN|GeoServer
233  dpiMode = 7;
234  break;
235  case 1: // off
236  dpiMode = 0;
237  break;
238  case 2: // QGIS
239  dpiMode = 1;
240  break;
241  case 3: // UMN
242  dpiMode = 2;
243  break;
244  case 4: // GeoServer
245  dpiMode = 4;
246  break;
247  }
248 
249  settings.setValue( key + "/dpiMode", dpiMode );
250  }
251  if ( mBaseKey == "/Qgis/connections-wms/" )
252  {
253  settings.setValue( key + "/ignoreGetFeatureInfoURI", cbxIgnoreGetFeatureInfoURI->isChecked() );
254  }
255 
256  settings.setValue( key + "/referer", txtReferer->text() );
257 
258  settings.setValue( credentialsKey + "/username", txtUserName->text() );
259  settings.setValue( credentialsKey + "/password", txtPassword->text() );
260 
261  settings.setValue( credentialsKey + "/authcfg", mAuthConfigSelect->configId() );
262 
263  settings.setValue( mBaseKey + "/selected", txtName->text() );
264 
265  QDialog::accept();
266 }
const QString configId() const
Get the authentication config id for the resource.
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.