QGIS API Documentation  2.0.1-Dufour
 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 
25  QWidget *parent, const QString& baseKey, const QString& connName, Qt::WFlags fl ):
26  QDialog( parent, fl ),
27  mBaseKey( baseKey ),
28  mOriginalConnName( connName )
29 {
30  setupUi( this );
31 
32  QString service = baseKey.mid( 18, 3 ).toUpper();
33  setWindowTitle( tr( "Create a new %1 connection" ).arg( service ) );
34 
35  // It would be obviously much better to use mBaseKey also for credentials,
36  // but for some strange reason a different hardcoded key was used instead.
37  // WFS and WMS credentials were mixed with the same key WMS.
38  // Only WMS and WFS providers are using QgsNewHttpConnection at this moment
39  // using connection-wms and connection-wfs -> parse credential key fro it.
40  mCredentialsBaseKey = mBaseKey.split( '-' ).last().toUpper();
41 
42  if ( !connName.isEmpty() )
43  {
44  // populate the dialog with the information stored for the connection
45  // populate the fields with the stored setting parameters
46 
47  QSettings settings;
48 
49  QString key = mBaseKey + connName;
50  QString credentialsKey = "/Qgis/" + mCredentialsBaseKey + "/" + connName;
51  txtName->setText( connName );
52  txtUrl->setText( settings.value( key + "/url" ).toString() );
53 
54  cbxIgnoreGetMapURI->setChecked( settings.value( key + "/ignoreGetMapURI", false ).toBool() );
55  cbxIgnoreAxisOrientation->setChecked( settings.value( key + "/ignoreAxisOrientation", false ).toBool() );
56  cbxInvertAxisOrientation->setChecked( settings.value( key + "/invertAxisOrientation", false ).toBool() );
57  cbxIgnoreGetFeatureInfoURI->setChecked( settings.value( key + "/ignoreGetFeatureInfoURI", false ).toBool() );
58  cbxSmoothPixmapTransform->setChecked( settings.value( key + "/smoothPixmapTransform", false ).toBool() );
59 
60  txtReferer->setText( settings.value( key + "/referer" ).toString() );
61 
62  txtUserName->setText( settings.value( credentialsKey + "/username" ).toString() );
63  txtPassword->setText( settings.value( credentialsKey + "/password" ).toString() );
64  }
65 
66  if ( mBaseKey != "/Qgis/connections-wms/" )
67  {
68  if ( mBaseKey == "/Qgis/connections-wcs/" )
69  {
70  cbxIgnoreGetMapURI->setText( tr( "Ignore GetCoverage URI reported in capabilities" ) );
71  cbxIgnoreAxisOrientation->setText( tr( "Ignore axis orientation" ) );
72  }
73  else
74  {
75  cbxIgnoreGetMapURI->setVisible( false );
76  cbxIgnoreAxisOrientation->setVisible( false );
77  cbxInvertAxisOrientation->setVisible( false );
78  cbxSmoothPixmapTransform->setVisible( false );
79  mGroupBox->layout()->removeWidget( cbxIgnoreGetMapURI );
80  mGroupBox->layout()->removeWidget( cbxIgnoreAxisOrientation );
81  mGroupBox->layout()->removeWidget( cbxInvertAxisOrientation );
82  mGroupBox->layout()->removeWidget( cbxSmoothPixmapTransform );
83  }
84 
85  cbxIgnoreGetFeatureInfoURI->setVisible( false );
86  mGroupBox->layout()->removeWidget( cbxIgnoreGetFeatureInfoURI );
87 
88  txtReferer->setVisible( false );
89  mGroupBox->layout()->removeWidget( txtReferer );
90  lblReferer->setVisible( false );
91  mGroupBox->layout()->removeWidget( lblReferer );
92 
93  // Adjust height
94  int w = width();
95  adjustSize();
96  resize( w, height() );
97  }
98 
99  on_txtName_textChanged( connName );
100 }
101 
103 {
104 }
105 
107 {
108  buttonBox->button( QDialogButtonBox::Ok )->setDisabled( text.isEmpty() );
109 }
110 
112 {
113  QSettings settings;
114  QString key = mBaseKey + txtName->text();
115  QString credentialsKey = "/Qgis/" + mCredentialsBaseKey + "/" + txtName->text();
116 
117  // warn if entry was renamed to an existing connection
118  if (( mOriginalConnName.isNull() || mOriginalConnName != txtName->text() ) &&
119  settings.contains( key + "/url" ) &&
120  QMessageBox::question( this,
121  tr( "Save connection" ),
122  tr( "Should the existing connection %1 be overwritten?" ).arg( txtName->text() ),
123  QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
124  {
125  return;
126  }
127 
128  if ( !txtPassword->text().isEmpty() &&
129  QMessageBox::question( this,
130  tr( "Saving passwords" ),
131  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." ),
132  QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
133  {
134  return;
135  }
136 
137  // on rename delete original entry first
138  if ( !mOriginalConnName.isNull() && mOriginalConnName != key )
139  {
140  settings.remove( mBaseKey + mOriginalConnName );
141  settings.remove( "/Qgis/" + mCredentialsBaseKey + "/" + mOriginalConnName );
142  }
143 
144  QUrl url( txtUrl->text().trimmed() );
145  const QList< QPair<QByteArray, QByteArray> > &items = url.encodedQueryItems();
146  QHash< QString, QPair<QByteArray, QByteArray> > params;
147  for ( QList< QPair<QByteArray, QByteArray> >::const_iterator it = items.constBegin(); it != items.constEnd(); ++it )
148  {
149  params.insert( QString( it->first ).toUpper(), *it );
150  }
151 
152  if ( params["SERVICE"].second.toUpper() == "WMS" ||
153  params["SERVICE"].second.toUpper() == "WFS" ||
154  params["SERVICE"].second.toUpper() == "WCS" )
155  {
156  url.removeEncodedQueryItem( params["SERVICE"].first );
157  url.removeEncodedQueryItem( params["REQUEST"].first );
158  url.removeEncodedQueryItem( params["FORMAT"].first );
159  }
160 
161  if ( url.encodedPath().isEmpty() )
162  {
163  url.setEncodedPath( "/" );
164  }
165 
166  settings.setValue( key + "/url", url.toString() );
167  if ( mBaseKey == "/Qgis/connections-wms/" || mBaseKey == "/Qgis/connections-wcs/" )
168  {
169  settings.setValue( key + "/ignoreGetMapURI", cbxIgnoreGetMapURI->isChecked() );
170  settings.setValue( key + "/ignoreAxisOrientation", cbxIgnoreAxisOrientation->isChecked() );
171  settings.setValue( key + "/invertAxisOrientation", cbxInvertAxisOrientation->isChecked() );
172  settings.setValue( key + "/smoothPixmapTransform", cbxSmoothPixmapTransform->isChecked() );
173  }
174  if ( mBaseKey == "/Qgis/connections-wms/" )
175  {
176  settings.setValue( key + "/ignoreGetFeatureInfoURI", cbxIgnoreGetFeatureInfoURI->isChecked() );
177  }
178 
179  settings.setValue( key + "/referer", txtReferer->text() );
180 
181  settings.setValue( credentialsKey + "/username", txtUserName->text() );
182  settings.setValue( credentialsKey + "/password", txtPassword->text() );
183 
184  settings.setValue( mBaseKey + "/selected", txtName->text() );
185 
186  QDialog::accept();
187 }