Quantum GIS API Documentation  1.8
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   // It would be obviously much better to use mBaseKey also for credentials,
00033   // but for some strange reason a different hardcoded key was used instead.
00034   // WFS and WMS credentials were mixed with the same key WMS.
00035   // Only WMS and WFS providers are using QgsNewHttpConnection at this moment
00036   // using connection-wms and connection-wfs -> parse credential key fro it.
00037   mCredentialsBaseKey = mBaseKey.split( '-' ).last().toUpper();
00038 
00039   if ( !connName.isEmpty() )
00040   {
00041     // populate the dialog with the information stored for the connection
00042     // populate the fields with the stored setting parameters
00043 
00044     QSettings settings;
00045 
00046     QString key = mBaseKey + connName;
00047     QString credentialsKey = "/Qgis/" + mCredentialsBaseKey + "/" + connName;
00048     txtName->setText( connName );
00049     txtUrl->setText( settings.value( key + "/url" ).toString() );
00050 
00051     if ( mBaseKey == "/Qgis/connections-wms/" )
00052     {
00053       cbxIgnoreGetMapURI->setChecked( settings.value( key + "/ignoreGetMapURI", false ).toBool() );
00054       cbxIgnoreGetFeatureInfoURI->setChecked( settings.value( key + "/ignoreGetFeatureInfoURI", false ).toBool() );
00055     }
00056     else
00057     {
00058       cbxIgnoreGetMapURI->setVisible( false );
00059       cbxIgnoreGetFeatureInfoURI->setVisible( false );
00060     }
00061 
00062     txtUserName->setText( settings.value( credentialsKey + "/username" ).toString() );
00063     txtPassword->setText( settings.value( credentialsKey + "/password" ).toString() );
00064   }
00065 
00066   on_txtName_textChanged( connName );
00067 }
00068 
00069 QgsNewHttpConnection::~QgsNewHttpConnection()
00070 {
00071 }
00072 
00073 void QgsNewHttpConnection::on_txtName_textChanged( const QString &text )
00074 {
00075   buttonBox->button( QDialogButtonBox::Ok )->setDisabled( text.isEmpty() );
00076 }
00077 
00078 void QgsNewHttpConnection::accept()
00079 {
00080   QSettings settings;
00081   QString key = mBaseKey + txtName->text();
00082   QString credentialsKey = "/Qgis/" + mCredentialsBaseKey + "/" + txtName->text();
00083 
00084   // warn if entry was renamed to an existing connection
00085   if (( mOriginalConnName.isNull() || mOriginalConnName != txtName->text() ) &&
00086       settings.contains( key + "/url" ) &&
00087       QMessageBox::question( this,
00088                              tr( "Save connection" ),
00089                              tr( "Should the existing connection %1 be overwritten?" ).arg( txtName->text() ),
00090                              QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
00091   {
00092     return;
00093   }
00094 
00095   // on rename delete original entry first
00096   if ( !mOriginalConnName.isNull() && mOriginalConnName != key )
00097   {
00098     settings.remove( mBaseKey + mOriginalConnName );
00099     settings.remove( "/Qgis/" + mCredentialsBaseKey + "/" + mOriginalConnName );
00100   }
00101 
00102   QUrl url( txtUrl->text().trimmed() );
00103 
00104   QList< QPair<QByteArray, QByteArray> > params = url.encodedQueryItems();
00105   for ( int i = 0; i < params.size(); i++ )
00106   {
00107     if ( params[i].first.toUpper() == "SERVICE" ||
00108          params[i].first.toUpper() == "REQUEST" ||
00109          params[i].first.toUpper() == "FORMAT" )
00110     {
00111       params.removeAt( i-- );
00112     }
00113   }
00114   url.setEncodedQueryItems( params );
00115 
00116   settings.setValue( key + "/url", url.toString() );
00117   if ( mBaseKey == "/Qgis/connections-wms/" )
00118   {
00119     settings.setValue( key + "/ignoreGetMapURI", cbxIgnoreGetMapURI->isChecked() );
00120     settings.setValue( key + "/ignoreGetFeatureInfoURI", cbxIgnoreGetFeatureInfoURI->isChecked() );
00121   }
00122 
00123   settings.setValue( credentialsKey + "/username", txtUserName->text() );
00124   settings.setValue( credentialsKey + "/password", txtPassword->text() );
00125 
00126   settings.setValue( mBaseKey + "/selected", txtName->text() );
00127 
00128   QDialog::accept();
00129 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines