QGIS API Documentation  master-3f58142
src/gui/qgsnewhttpconnection.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002     qgsnewhttpconnection.cpp -  selector for a new HTTP server for WMS, etc.
00003                              -------------------
00004     begin                : 3 April 2005
00005     copyright            : (C) 2005 by Brendan Morley
00006     email                : morb at ozemail dot com dot au
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 #include "qgsnewhttpconnection.h"
00018 #include "qgscontexthelp.h"
00019 #include <QSettings>
00020 #include <QMessageBox>
00021 #include <QUrl>
00022 #include <QPushButton>
00023 
00024 QgsNewHttpConnection::QgsNewHttpConnection(
00025   QWidget *parent, const QString& baseKey, const QString& connName, Qt::WFlags fl ):
00026     QDialog( parent, fl ),
00027     mBaseKey( baseKey ),
00028     mOriginalConnName( connName )
00029 {
00030   setupUi( this );
00031 
00032   QString service = baseKey.mid( 18, 3 ).toUpper();
00033   setWindowTitle( tr( "Create a new %1 connection" ).arg( service ) );
00034 
00035   // It would be obviously much better to use mBaseKey also for credentials,
00036   // but for some strange reason a different hardcoded key was used instead.
00037   // WFS and WMS credentials were mixed with the same key WMS.
00038   // Only WMS and WFS providers are using QgsNewHttpConnection at this moment
00039   // using connection-wms and connection-wfs -> parse credential key fro it.
00040   mCredentialsBaseKey = mBaseKey.split( '-' ).last().toUpper();
00041 
00042   if ( !connName.isEmpty() )
00043   {
00044     // populate the dialog with the information stored for the connection
00045     // populate the fields with the stored setting parameters
00046 
00047     QSettings settings;
00048 
00049     QString key = mBaseKey + connName;
00050     QString credentialsKey = "/Qgis/" + mCredentialsBaseKey + "/" + connName;
00051     txtName->setText( connName );
00052     txtUrl->setText( settings.value( key + "/url" ).toString() );
00053 
00054     cbxIgnoreGetMapURI->setChecked( settings.value( key + "/ignoreGetMapURI", false ).toBool() );
00055     cbxIgnoreAxisOrientation->setChecked( settings.value( key + "/ignoreAxisOrientation", false ).toBool() );
00056     cbxInvertAxisOrientation->setChecked( settings.value( key + "/invertAxisOrientation", false ).toBool() );
00057     cbxIgnoreGetFeatureInfoURI->setChecked( settings.value( key + "/ignoreGetFeatureInfoURI", false ).toBool() );
00058     cbxSmoothPixmapTransform->setChecked( settings.value( key + "/smoothPixmapTransform", false ).toBool() );
00059 
00060     txtReferer->setText( settings.value( key + "/referer" ).toString() );
00061 
00062     txtUserName->setText( settings.value( credentialsKey + "/username" ).toString() );
00063     txtPassword->setText( settings.value( credentialsKey + "/password" ).toString() );
00064   }
00065 
00066   if ( mBaseKey != "/Qgis/connections-wms/" )
00067   {
00068     if ( mBaseKey == "/Qgis/connections-wcs/" )
00069     {
00070       cbxIgnoreGetMapURI->setText( tr( "Ignore GetCoverage URI reported in capabilities" ) );
00071       cbxIgnoreAxisOrientation->setText( tr( "Ignore axis orientation" ) );
00072     }
00073     else
00074     {
00075       cbxIgnoreGetMapURI->setVisible( false );
00076       cbxIgnoreAxisOrientation->setVisible( false );
00077       cbxInvertAxisOrientation->setVisible( false );
00078       mGroupBox->layout()->removeWidget( cbxIgnoreGetMapURI );
00079       mGroupBox->layout()->removeWidget( cbxIgnoreAxisOrientation );
00080       mGroupBox->layout()->removeWidget( cbxInvertAxisOrientation );
00081     }
00082 
00083     cbxIgnoreGetFeatureInfoURI->setVisible( false );
00084     mGroupBox->layout()->removeWidget( cbxIgnoreGetFeatureInfoURI );
00085 
00086     txtReferer->setVisible( false );
00087     mGroupBox->layout()->removeWidget( txtReferer );
00088     lblReferer->setVisible( false );
00089     mGroupBox->layout()->removeWidget( lblReferer );
00090 
00091     // Adjust height
00092     int w = width();
00093     adjustSize();
00094     resize( w, height() );
00095   }
00096 
00097   on_txtName_textChanged( connName );
00098 }
00099 
00100 QgsNewHttpConnection::~QgsNewHttpConnection()
00101 {
00102 }
00103 
00104 void QgsNewHttpConnection::on_txtName_textChanged( const QString &text )
00105 {
00106   buttonBox->button( QDialogButtonBox::Ok )->setDisabled( text.isEmpty() );
00107 }
00108 
00109 void QgsNewHttpConnection::accept()
00110 {
00111   QSettings settings;
00112   QString key = mBaseKey + txtName->text();
00113   QString credentialsKey = "/Qgis/" + mCredentialsBaseKey + "/" + txtName->text();
00114 
00115   // warn if entry was renamed to an existing connection
00116   if (( mOriginalConnName.isNull() || mOriginalConnName != txtName->text() ) &&
00117       settings.contains( key + "/url" ) &&
00118       QMessageBox::question( this,
00119                              tr( "Save connection" ),
00120                              tr( "Should the existing connection %1 be overwritten?" ).arg( txtName->text() ),
00121                              QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
00122   {
00123     return;
00124   }
00125 
00126   if ( !txtPassword->text().isEmpty() &&
00127        QMessageBox::question( this,
00128                               tr( "Saving passwords" ),
00129                               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." ),
00130                               QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
00131   {
00132     return;
00133   }
00134 
00135   // on rename delete original entry first
00136   if ( !mOriginalConnName.isNull() && mOriginalConnName != key )
00137   {
00138     settings.remove( mBaseKey + mOriginalConnName );
00139     settings.remove( "/Qgis/" + mCredentialsBaseKey + "/" + mOriginalConnName );
00140   }
00141 
00142   QUrl url( txtUrl->text().trimmed() );
00143   const QList< QPair<QByteArray, QByteArray> > &items = url.encodedQueryItems();
00144   QHash< QString, QPair<QByteArray, QByteArray> > params;
00145   for ( QList< QPair<QByteArray, QByteArray> >::const_iterator it = items.constBegin(); it != items.constEnd(); ++it )
00146   {
00147     params.insert( QString( it->first ).toUpper(), *it );
00148   }
00149 
00150   if ( params["SERVICE"].second.toUpper() == "WMS" )
00151   {
00152     url.removeEncodedQueryItem( params["SERVICE"].first );
00153     url.removeEncodedQueryItem( params["REQUEST"].first );
00154     url.removeEncodedQueryItem( params["FORMAT"].first );
00155   }
00156 
00157   if ( url.encodedPath().isEmpty() )
00158   {
00159     url.setEncodedPath( "/" );
00160   }
00161 
00162   settings.setValue( key + "/url", url.toString() );
00163   if ( mBaseKey == "/Qgis/connections-wms/" || mBaseKey == "/Qgis/connections-wcs/" )
00164   {
00165     settings.setValue( key + "/ignoreGetMapURI", cbxIgnoreGetMapURI->isChecked() );
00166     settings.setValue( key + "/ignoreAxisOrientation", cbxIgnoreAxisOrientation->isChecked() );
00167     settings.setValue( key + "/invertAxisOrientation", cbxInvertAxisOrientation->isChecked() );
00168     settings.setValue( key + "/smoothPixmapTransform", cbxSmoothPixmapTransform->isChecked() );
00169   }
00170   if ( mBaseKey == "/Qgis/connections-wms/" )
00171   {
00172     settings.setValue( key + "/ignoreGetFeatureInfoURI", cbxIgnoreGetFeatureInfoURI->isChecked() );
00173   }
00174 
00175   settings.setValue( key + "/referer", txtReferer->text() );
00176 
00177   settings.setValue( credentialsKey + "/username", txtUserName->text() );
00178   settings.setValue( credentialsKey + "/password", txtPassword->text() );
00179 
00180   settings.setValue( mBaseKey + "/selected", txtName->text() );
00181 
00182   QDialog::accept();
00183 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines