QGIS API Documentation  2.3.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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, Qt::WindowFlags fl ):
27  QDialog( parent, fl ),
28  mBaseKey( baseKey ),
29  mOriginalConnName( connName )
30 {
31  setupUi( this );
32 
33  QString service = baseKey.mid( 18, 3 ).toUpper();
34  setWindowTitle( tr( "Create a new %1 connection" ).arg( service ) );
35 
36  // It would be obviously much better to use mBaseKey also for credentials,
37  // but for some strange reason a different hardcoded key was used instead.
38  // WFS and WMS credentials were mixed with the same key WMS.
39  // Only WMS and WFS providers are using QgsNewHttpConnection at this moment
40  // using connection-wms and connection-wfs -> parse credential key fro it.
41  mCredentialsBaseKey = mBaseKey.split( '-' ).last().toUpper();
42 
43  txtName->setValidator( new QRegExpValidator( QRegExp( "[^\\/]+" ), txtName ) );
44 
45  cmbDpiMode->clear();
46  cmbDpiMode->addItem( tr( "all" ) );
47  cmbDpiMode->addItem( tr( "off" ) );
48  cmbDpiMode->addItem( tr( "QGIS" ) );
49  cmbDpiMode->addItem( tr( "UMN" ) );
50  cmbDpiMode->addItem( tr( "GeoServer" ) );
51 
52  if ( !connName.isEmpty() )
53  {
54  // populate the dialog with the information stored for the connection
55  // populate the fields with the stored setting parameters
56 
57  QSettings settings;
58 
59  QString key = mBaseKey + connName;
60  QString credentialsKey = "/Qgis/" + mCredentialsBaseKey + "/" + connName;
61  txtName->setText( connName );
62  txtUrl->setText( settings.value( key + "/url" ).toString() );
63 
64  cbxIgnoreGetMapURI->setChecked( settings.value( key + "/ignoreGetMapURI", false ).toBool() );
65  cbxIgnoreAxisOrientation->setChecked( settings.value( key + "/ignoreAxisOrientation", false ).toBool() );
66  cbxInvertAxisOrientation->setChecked( settings.value( key + "/invertAxisOrientation", false ).toBool() );
67  cbxIgnoreGetFeatureInfoURI->setChecked( settings.value( key + "/ignoreGetFeatureInfoURI", false ).toBool() );
68  cbxSmoothPixmapTransform->setChecked( settings.value( key + "/smoothPixmapTransform", false ).toBool() );
69 
70  int dpiIdx;
71  switch ( settings.value( key + "/dpiMode", 7 ).toInt() )
72  {
73  case 0: // off
74  dpiIdx = 1;
75  break;
76  case 1: // QGIS
77  dpiIdx = 2;
78  break;
79  case 2: // UMN
80  dpiIdx = 3;
81  break;
82  case 4: // GeoServer
83  dpiIdx = 4;
84  break;
85  default: // other => all
86  dpiIdx = 0;
87  break;
88  }
89  cmbDpiMode->setCurrentIndex( dpiIdx );
90 
91  txtReferer->setText( settings.value( key + "/referer" ).toString() );
92 
93  txtUserName->setText( settings.value( credentialsKey + "/username" ).toString() );
94  txtPassword->setText( settings.value( credentialsKey + "/password" ).toString() );
95  }
96 
97  if ( mBaseKey != "/Qgis/connections-wms/" )
98  {
99  if ( mBaseKey == "/Qgis/connections-wcs/" )
100  {
101  cbxIgnoreGetMapURI->setText( tr( "Ignore GetCoverage URI reported in capabilities" ) );
102  cbxIgnoreAxisOrientation->setText( tr( "Ignore axis orientation" ) );
103  }
104  else
105  {
106  cbxIgnoreGetMapURI->setVisible( false );
107  cbxIgnoreAxisOrientation->setVisible( false );
108  cbxInvertAxisOrientation->setVisible( false );
109  cbxSmoothPixmapTransform->setVisible( false );
110  mGroupBox->layout()->removeWidget( cbxIgnoreGetMapURI );
111  mGroupBox->layout()->removeWidget( cbxIgnoreAxisOrientation );
112  mGroupBox->layout()->removeWidget( cbxInvertAxisOrientation );
113  mGroupBox->layout()->removeWidget( cbxSmoothPixmapTransform );
114  }
115 
116  cbxIgnoreGetFeatureInfoURI->setVisible( false );
117  mGroupBox->layout()->removeWidget( cbxIgnoreGetFeatureInfoURI );
118 
119  cmbDpiMode->setVisible( false );
120  mGroupBox->layout()->removeWidget( cmbDpiMode );
121  lblDpiMode->setVisible( false );
122  mGroupBox->layout()->removeWidget( lblDpiMode );
123 
124  txtReferer->setVisible( false );
125  mGroupBox->layout()->removeWidget( txtReferer );
126  lblReferer->setVisible( false );
127  mGroupBox->layout()->removeWidget( lblReferer );
128 
129  // Adjust height
130  int w = width();
131  adjustSize();
132  resize( w, height() );
133  }
134 
135  on_txtName_textChanged( connName );
136 }
137 
139 {
140 }
141 
143 {
144  Q_UNUSED( text );
145  buttonBox->button( QDialogButtonBox::Ok )->setDisabled( txtName->text().isEmpty() || txtUrl->text().isEmpty() );
146 }
147 
149 {
150  Q_UNUSED( text );
151  buttonBox->button( QDialogButtonBox::Ok )->setDisabled( txtName->text().isEmpty() || txtUrl->text().isEmpty() );
152 }
153 
155 {
156  QSettings settings;
157  QString key = mBaseKey + txtName->text();
158  QString credentialsKey = "/Qgis/" + mCredentialsBaseKey + "/" + txtName->text();
159 
160  // warn if entry was renamed to an existing connection
161  if (( mOriginalConnName.isNull() || mOriginalConnName != txtName->text() ) &&
162  settings.contains( key + "/url" ) &&
163  QMessageBox::question( this,
164  tr( "Save connection" ),
165  tr( "Should the existing connection %1 be overwritten?" ).arg( txtName->text() ),
166  QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
167  {
168  return;
169  }
170 
171  if ( !txtPassword->text().isEmpty() &&
172  QMessageBox::question( this,
173  tr( "Saving passwords" ),
174  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." ),
175  QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
176  {
177  return;
178  }
179 
180  // on rename delete original entry first
181  if ( !mOriginalConnName.isNull() && mOriginalConnName != key )
182  {
183  settings.remove( mBaseKey + mOriginalConnName );
184  settings.remove( "/Qgis/" + mCredentialsBaseKey + "/" + mOriginalConnName );
185  }
186 
187  QUrl url( txtUrl->text().trimmed() );
188  const QList< QPair<QByteArray, QByteArray> > &items = url.encodedQueryItems();
189  QHash< QString, QPair<QByteArray, QByteArray> > params;
190  for ( QList< QPair<QByteArray, QByteArray> >::const_iterator it = items.constBegin(); it != items.constEnd(); ++it )
191  {
192  params.insert( QString( it->first ).toUpper(), *it );
193  }
194 
195  if ( params["SERVICE"].second.toUpper() == "WMS" ||
196  params["SERVICE"].second.toUpper() == "WFS" ||
197  params["SERVICE"].second.toUpper() == "WCS" )
198  {
199  url.removeEncodedQueryItem( params["SERVICE"].first );
200  url.removeEncodedQueryItem( params["REQUEST"].first );
201  url.removeEncodedQueryItem( params["FORMAT"].first );
202  }
203 
204  if ( url.encodedPath().isEmpty() )
205  {
206  url.setEncodedPath( "/" );
207  }
208 
209  settings.setValue( key + "/url", url.toString() );
210  if ( mBaseKey == "/Qgis/connections-wms/" || mBaseKey == "/Qgis/connections-wcs/" )
211  {
212  settings.setValue( key + "/ignoreGetMapURI", cbxIgnoreGetMapURI->isChecked() );
213  settings.setValue( key + "/ignoreAxisOrientation", cbxIgnoreAxisOrientation->isChecked() );
214  settings.setValue( key + "/invertAxisOrientation", cbxInvertAxisOrientation->isChecked() );
215  settings.setValue( key + "/smoothPixmapTransform", cbxSmoothPixmapTransform->isChecked() );
216 
217  int dpiMode = 0;
218  switch ( cmbDpiMode->currentIndex() )
219  {
220  case 0: // all => QGIS|UMN|GeoServer
221  dpiMode = 7;
222  break;
223  case 1: // off
224  dpiMode = 0;
225  break;
226  case 2: // QGIS
227  dpiMode = 1;
228  break;
229  case 3: // UMN
230  dpiMode = 2;
231  break;
232  case 4: // GeoServer
233  dpiMode = 4;
234  break;
235  }
236 
237  settings.setValue( key + "/dpiMode", dpiMode );
238  }
239  if ( mBaseKey == "/Qgis/connections-wms/" )
240  {
241  settings.setValue( key + "/ignoreGetFeatureInfoURI", cbxIgnoreGetFeatureInfoURI->isChecked() );
242  }
243 
244  settings.setValue( key + "/referer", txtReferer->text() );
245 
246  settings.setValue( credentialsKey + "/username", txtUserName->text() );
247  settings.setValue( credentialsKey + "/password", txtPassword->text() );
248 
249  settings.setValue( mBaseKey + "/selected", txtName->text() );
250 
251  QDialog::accept();
252 }
void on_txtName_textChanged(const QString &)
QgsNewHttpConnection(QWidget *parent=0, const QString &baseKey="/Qgis/connections-wms/", const QString &connName=QString::null, Qt::WindowFlags fl=QgisGui::ModalDialogFlags)
Constructor.
void on_txtUrl_textChanged(const QString &)
void accept()
Saves the connection to ~/.qt/qgisrc.
#define tr(sourceText)