2005-05-28 23:23:13 +00:00
/***************************************************************************
qgsnewhttpconnection . cpp - selector for a new HTTP server for WMS , etc .
- - - - - - - - - - - - - - - - - - -
begin : 3 April 2005
copyright : ( C ) 2005 by Brendan Morley
email : morb at ozemail dot com dot au
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/***************************************************************************
* *
* This program is free software ; you can redistribute it and / or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation ; either version 2 of the License , or *
* ( at your option ) any later version . *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include "qgsnewhttpconnection.h"
2006-01-15 20:57:38 +00:00
# include "qgscontexthelp.h"
2005-12-30 04:56:31 +00:00
# include <QSettings>
2009-12-09 21:35:43 +00:00
# include <QMessageBox>
2011-02-17 12:57:38 +00:00
# include <QUrl>
2011-03-14 19:49:53 +00:00
# include <QPushButton>
2013-11-27 15:31:23 +01:00
# include <QRegExpValidator>
2005-05-28 23:23:13 +00:00
2008-06-15 22:35:03 +00:00
QgsNewHttpConnection : : QgsNewHttpConnection (
2014-03-26 22:46:18 -05:00
QWidget * parent , const QString & baseKey , const QString & connName , Qt : : WindowFlags fl ) :
2008-08-23 21:37:31 +00:00
QDialog ( parent , fl ) ,
mBaseKey ( baseKey ) ,
mOriginalConnName ( connName )
2005-05-28 23:23:13 +00:00
{
2008-08-23 21:37:31 +00:00
setupUi ( this ) ;
2005-12-30 04:56:31 +00:00
2012-04-28 12:18:17 +02:00
QString service = baseKey . mid ( 18 , 3 ) . toUpper ( ) ;
setWindowTitle ( tr ( " Create a new %1 connection " ) . arg ( service ) ) ;
2012-02-15 16:01:39 +01:00
// It would be obviously much better to use mBaseKey also for credentials,
// but for some strange reason a different hardcoded key was used instead.
// WFS and WMS credentials were mixed with the same key WMS.
// Only WMS and WFS providers are using QgsNewHttpConnection at this moment
// using connection-wms and connection-wfs -> parse credential key fro it.
mCredentialsBaseKey = mBaseKey . split ( ' - ' ) . last ( ) . toUpper ( ) ;
2014-02-14 13:03:07 +01:00
txtName - > setValidator ( new QRegExpValidator ( QRegExp ( " [^ \\ /]+ " ) , txtName ) ) ;
2013-11-27 15:31:23 +01:00
2013-12-11 14:15:04 +01:00
cmbDpiMode - > clear ( ) ;
cmbDpiMode - > addItem ( tr ( " all " ) ) ;
cmbDpiMode - > addItem ( tr ( " off " ) ) ;
cmbDpiMode - > addItem ( tr ( " QGIS " ) ) ;
cmbDpiMode - > addItem ( tr ( " UMN " ) ) ;
cmbDpiMode - > addItem ( tr ( " GeoServer " ) ) ;
2008-08-23 21:37:31 +00:00
if ( ! connName . isEmpty ( ) )
2008-06-15 22:35:03 +00:00
{
// populate the dialog with the information stored for the connection
// populate the fields with the stored setting parameters
2005-05-28 23:23:13 +00:00
2008-06-15 22:35:03 +00:00
QSettings settings ;
2005-05-28 23:23:13 +00:00
2008-06-15 22:35:03 +00:00
QString key = mBaseKey + connName ;
2012-02-15 16:01:39 +01:00
QString credentialsKey = " /Qgis/ " + mCredentialsBaseKey + " / " + connName ;
2008-08-23 21:37:31 +00:00
txtName - > setText ( connName ) ;
txtUrl - > setText ( settings . value ( key + " /url " ) . toString ( ) ) ;
2011-03-22 08:47:46 +00:00
2012-07-12 17:28:33 +02:00
cbxIgnoreGetMapURI - > setChecked ( settings . value ( key + " /ignoreGetMapURI " , false ) . toBool ( ) ) ;
cbxIgnoreAxisOrientation - > setChecked ( settings . value ( key + " /ignoreAxisOrientation " , false ) . toBool ( ) ) ;
cbxInvertAxisOrientation - > setChecked ( settings . value ( key + " /invertAxisOrientation " , false ) . toBool ( ) ) ;
cbxIgnoreGetFeatureInfoURI - > setChecked ( settings . value ( key + " /ignoreGetFeatureInfoURI " , false ) . toBool ( ) ) ;
2013-04-18 11:33:53 +02:00
cbxSmoothPixmapTransform - > setChecked ( settings . value ( key + " /smoothPixmapTransform " , false ) . toBool ( ) ) ;
2013-04-02 22:34:21 +02:00
2013-10-22 21:16:33 +02:00
int dpiIdx ;
switch ( settings . value ( key + " /dpiMode " , 7 ) . toInt ( ) )
{
case 0 : // off
dpiIdx = 1 ;
break ;
case 1 : // QGIS
dpiIdx = 2 ;
break ;
case 2 : // UMN
dpiIdx = 3 ;
break ;
case 4 : // GeoServer
dpiIdx = 4 ;
break ;
default : // other => all
dpiIdx = 0 ;
break ;
}
cmbDpiMode - > setCurrentIndex ( dpiIdx ) ;
2013-03-29 15:46:39 +01:00
txtReferer - > setText ( settings . value ( key + " /referer " ) . toString ( ) ) ;
2012-07-12 17:28:33 +02:00
txtUserName - > setText ( settings . value ( credentialsKey + " /username " ) . toString ( ) ) ;
txtPassword - > setText ( settings . value ( credentialsKey + " /password " ) . toString ( ) ) ;
}
if ( mBaseKey ! = " /Qgis/connections-wms/ " )
{
if ( mBaseKey = = " /Qgis/connections-wcs/ " )
2011-03-22 08:47:46 +00:00
{
2012-07-12 17:28:33 +02:00
cbxIgnoreGetMapURI - > setText ( tr ( " Ignore GetCoverage URI reported in capabilities " ) ) ;
cbxIgnoreAxisOrientation - > setText ( tr ( " Ignore axis orientation " ) ) ;
2011-03-22 08:47:46 +00:00
}
else
{
cbxIgnoreGetMapURI - > setVisible ( false ) ;
2012-05-17 21:00:01 +02:00
cbxIgnoreAxisOrientation - > setVisible ( false ) ;
2012-06-04 20:43:30 +02:00
cbxInvertAxisOrientation - > setVisible ( false ) ;
2013-06-30 10:42:24 +02:00
cbxSmoothPixmapTransform - > setVisible ( false ) ;
2012-07-12 17:28:33 +02:00
mGroupBox - > layout ( ) - > removeWidget ( cbxIgnoreGetMapURI ) ;
mGroupBox - > layout ( ) - > removeWidget ( cbxIgnoreAxisOrientation ) ;
mGroupBox - > layout ( ) - > removeWidget ( cbxInvertAxisOrientation ) ;
2013-06-30 10:42:24 +02:00
mGroupBox - > layout ( ) - > removeWidget ( cbxSmoothPixmapTransform ) ;
2011-03-22 08:47:46 +00:00
}
2012-04-26 11:00:17 +02:00
cbxIgnoreGetFeatureInfoURI - > setVisible ( false ) ;
mGroupBox - > layout ( ) - > removeWidget ( cbxIgnoreGetFeatureInfoURI ) ;
2013-04-02 22:34:21 +02:00
2013-10-22 21:16:33 +02:00
cmbDpiMode - > setVisible ( false ) ;
mGroupBox - > layout ( ) - > removeWidget ( cmbDpiMode ) ;
2013-11-07 00:04:24 +01:00
lblDpiMode - > setVisible ( false ) ;
mGroupBox - > layout ( ) - > removeWidget ( lblDpiMode ) ;
2013-10-22 21:16:33 +02:00
2013-03-29 16:03:15 +01:00
txtReferer - > setVisible ( false ) ;
mGroupBox - > layout ( ) - > removeWidget ( txtReferer ) ;
lblReferer - > setVisible ( false ) ;
mGroupBox - > layout ( ) - > removeWidget ( lblReferer ) ;
2012-07-12 17:28:33 +02:00
2012-04-26 11:00:17 +02:00
// Adjust height
int w = width ( ) ;
adjustSize ( ) ;
2012-04-28 12:18:17 +02:00
resize ( w , height ( ) ) ;
2012-04-26 11:00:17 +02:00
}
2011-03-14 19:49:53 +00:00
on_txtName_textChanged ( connName ) ;
2005-05-28 23:23:13 +00:00
}
2008-06-15 22:35:03 +00:00
QgsNewHttpConnection : : ~ QgsNewHttpConnection ( )
2005-05-28 23:23:13 +00:00
{
}
2011-03-14 19:49:53 +00:00
void QgsNewHttpConnection : : on_txtName_textChanged ( const QString & text )
{
2013-12-18 00:46:57 +01:00
Q_UNUSED ( text ) ;
2013-12-16 00:23:17 +01:00
buttonBox - > button ( QDialogButtonBox : : Ok ) - > setDisabled ( txtName - > text ( ) . isEmpty ( ) | | txtUrl - > text ( ) . isEmpty ( ) ) ;
}
void QgsNewHttpConnection : : on_txtUrl_textChanged ( const QString & text )
{
2013-12-18 00:46:57 +01:00
Q_UNUSED ( text ) ;
2013-12-16 00:23:17 +01:00
buttonBox - > button ( QDialogButtonBox : : Ok ) - > setDisabled ( txtName - > text ( ) . isEmpty ( ) | | txtUrl - > text ( ) . isEmpty ( ) ) ;
2011-03-14 19:49:53 +00:00
}
2008-06-15 22:35:03 +00:00
void QgsNewHttpConnection : : accept ( )
2005-05-28 23:23:13 +00:00
{
2008-08-23 21:37:31 +00:00
QSettings settings ;
2006-08-28 14:48:57 +00:00
QString key = mBaseKey + txtName - > text ( ) ;
2012-02-15 16:01:39 +01:00
QString credentialsKey = " /Qgis/ " + mCredentialsBaseKey + " / " + txtName - > text ( ) ;
2008-06-15 22:35:03 +00:00
2009-12-09 21:35:43 +00:00
// warn if entry was renamed to an existing connection
if ( ( mOriginalConnName . isNull ( ) | | mOriginalConnName ! = txtName - > text ( ) ) & &
settings . contains ( key + " /url " ) & &
QMessageBox : : question ( this ,
tr ( " Save connection " ) ,
tr ( " Should the existing connection %1 be overwritten? " ) . arg ( txtName - > text ( ) ) ,
QMessageBox : : Ok | QMessageBox : : Cancel ) = = QMessageBox : : Cancel )
{
return ;
}
2012-07-08 21:51:07 +02:00
if ( ! txtPassword - > text ( ) . isEmpty ( ) & &
QMessageBox : : question ( this ,
tr ( " Saving passwords " ) ,
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. \n Note: giving the password is optional. It will be requested interactivly, when needed. " ) ,
QMessageBox : : Ok | QMessageBox : : Cancel ) = = QMessageBox : : Cancel )
{
return ;
}
2009-12-09 21:35:43 +00:00
// on rename delete original entry first
2008-08-23 21:37:31 +00:00
if ( ! mOriginalConnName . isNull ( ) & & mOriginalConnName ! = key )
2008-06-15 22:35:03 +00:00
{
2008-08-23 21:37:31 +00:00
settings . remove ( mBaseKey + mOriginalConnName ) ;
2012-02-15 16:01:39 +01:00
settings . remove ( " /Qgis/ " + mCredentialsBaseKey + " / " + mOriginalConnName ) ;
2008-06-15 22:35:03 +00:00
}
2009-12-09 21:35:43 +00:00
2011-02-17 12:57:38 +00:00
QUrl url ( txtUrl - > text ( ) . trimmed ( ) ) ;
2012-05-17 21:00:01 +02:00
const QList < QPair < QByteArray , QByteArray > > & items = url . encodedQueryItems ( ) ;
QHash < QString , QPair < QByteArray , QByteArray > > params ;
for ( QList < QPair < QByteArray , QByteArray > > : : const_iterator it = items . constBegin ( ) ; it ! = items . constEnd ( ) ; + + it )
{
params . insert ( QString ( it - > first ) . toUpper ( ) , * it ) ;
}
2011-02-17 12:57:38 +00:00
2013-07-15 15:43:03 +02:00
if ( params [ " SERVICE " ] . second . toUpper ( ) = = " WMS " | |
params [ " SERVICE " ] . second . toUpper ( ) = = " WFS " | |
params [ " SERVICE " ] . second . toUpper ( ) = = " WCS " )
2011-02-17 12:57:38 +00:00
{
2012-06-10 22:56:50 +02:00
url . removeEncodedQueryItem ( params [ " SERVICE " ] . first ) ;
url . removeEncodedQueryItem ( params [ " REQUEST " ] . first ) ;
url . removeEncodedQueryItem ( params [ " FORMAT " ] . first ) ;
2011-02-17 12:57:38 +00:00
}
2012-05-17 21:00:01 +02:00
2013-05-15 20:42:04 +02:00
if ( url . encodedPath ( ) . isEmpty ( ) )
2013-05-09 17:08:04 +02:00
{
url . setEncodedPath ( " / " ) ;
}
2011-02-17 12:57:38 +00:00
settings . setValue ( key + " /url " , url . toString ( ) ) ;
2012-07-12 17:28:33 +02:00
if ( mBaseKey = = " /Qgis/connections-wms/ " | | mBaseKey = = " /Qgis/connections-wcs/ " )
2011-03-22 08:47:46 +00:00
{
settings . setValue ( key + " /ignoreGetMapURI " , cbxIgnoreGetMapURI - > isChecked ( ) ) ;
2012-05-17 21:00:01 +02:00
settings . setValue ( key + " /ignoreAxisOrientation " , cbxIgnoreAxisOrientation - > isChecked ( ) ) ;
2012-06-04 20:43:30 +02:00
settings . setValue ( key + " /invertAxisOrientation " , cbxInvertAxisOrientation - > isChecked ( ) ) ;
2013-04-18 11:33:53 +02:00
settings . setValue ( key + " /smoothPixmapTransform " , cbxSmoothPixmapTransform - > isChecked ( ) ) ;
2013-10-22 21:16:33 +02:00
2013-12-11 18:46:56 +01:00
int dpiMode = 0 ;
2013-10-22 21:16:33 +02:00
switch ( cmbDpiMode - > currentIndex ( ) )
{
case 0 : // all => QGIS|UMN|GeoServer
dpiMode = 7 ;
break ;
case 1 : // off
dpiMode = 0 ;
break ;
case 2 : // QGIS
dpiMode = 1 ;
break ;
case 3 : // UMN
dpiMode = 2 ;
break ;
case 4 : // GeoServer
dpiMode = 4 ;
break ;
}
settings . setValue ( key + " /dpiMode " , dpiMode ) ;
2011-03-22 08:47:46 +00:00
}
2012-07-12 17:28:33 +02:00
if ( mBaseKey = = " /Qgis/connections-wms/ " )
{
settings . setValue ( key + " /ignoreGetFeatureInfoURI " , cbxIgnoreGetFeatureInfoURI - > isChecked ( ) ) ;
}
2011-03-22 08:47:46 +00:00
2013-03-29 15:46:39 +01:00
settings . setValue ( key + " /referer " , txtReferer - > text ( ) ) ;
2009-04-21 07:46:54 +00:00
settings . setValue ( credentialsKey + " /username " , txtUserName - > text ( ) ) ;
settings . setValue ( credentialsKey + " /password " , txtPassword - > text ( ) ) ;
2008-06-15 22:35:03 +00:00
2011-03-14 19:35:23 +00:00
settings . setValue ( mBaseKey + " /selected " , txtName - > text ( ) ) ;
2008-06-15 22:35:03 +00:00
QDialog : : accept ( ) ;
2005-05-28 23:23:13 +00:00
}