23 #include <QMessageBox>
25 #include <QPushButton>
27 #include <QRegExpValidator>
30 : QDialog( parent, fl )
33 , mOriginalConnName( connectionName )
38 mHttpGroupBox->hide();
42 connect( buttonBox, &QDialogButtonBox::helpRequested,
this, &QgsNewHttpConnection::showHelp );
44 QRegExp rx(
"/connections-([^/]+)/" );
45 if ( rx.indexIn( baseKey ) != -1 )
47 QString connectionType( rx.cap( 1 ).toUpper() );
48 if ( connectionType == QLatin1String(
"WMS" ) )
50 connectionType = QStringLiteral(
"WMS/WMTS" );
52 setWindowTitle( tr(
"Create a New %1 Connection" ).arg( connectionType ) );
60 mCredentialsBaseKey = mBaseKey.split(
'-' ).last().toUpper();
62 txtName->setValidator(
new QRegExpValidator( QRegExp(
"[^\\/]+" ), txtName ) );
65 cmbDpiMode->addItem( tr(
"all" ) );
66 cmbDpiMode->addItem( tr(
"off" ) );
67 cmbDpiMode->addItem( tr(
"QGIS" ) );
68 cmbDpiMode->addItem( tr(
"UMN" ) );
69 cmbDpiMode->addItem( tr(
"GeoServer" ) );
72 cmbVersion->addItem( tr(
"Maximum" ) );
73 cmbVersion->addItem( tr(
"1.0" ) );
74 cmbVersion->addItem( tr(
"1.1" ) );
75 cmbVersion->addItem( tr(
"2.0" ) );
76 cmbVersion->addItem( tr(
"OGC API - Features" ) );
78 static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
79 this, &QgsNewHttpConnection::wfsVersionCurrentIndexChanged );
81 connect( cbxWfsFeaturePaging, &QCheckBox::stateChanged,
82 this, &QgsNewHttpConnection::wfsFeaturePagingStateChanged );
84 if ( !connectionName.isEmpty() )
91 QString key = mBaseKey + connectionName;
92 QString credentialsKey =
"qgis/" + mCredentialsBaseKey +
'/' + connectionName;
93 txtName->setText( connectionName );
94 txtUrl->setText( settings.
value( key +
"/url" ).toString() );
95 mRefererLineEdit->setText( settings.
value( key +
"/referer" ).toString() );
100 mAuthSettings->setUsername( settings.
value( credentialsKey +
"/username" ).toString() );
101 mAuthSettings->setPassword( settings.
value( credentialsKey +
"/password" ).toString() );
102 mAuthSettings->setConfigId( settings.
value( credentialsKey +
"/authcfg" ).toString() );
104 mWfsVersionDetectButton->setDisabled( txtUrl->text().isEmpty() );
108 mWmsOptionsGroupBox->setVisible(
false );
109 mGroupBox->layout()->removeWidget( mWmsOptionsGroupBox );
113 mWfsOptionsGroupBox->setVisible(
false );
114 mGroupBox->layout()->removeWidget( mWfsOptionsGroupBox );
119 cbxIgnoreGetMapURI->setText( tr(
"Ignore GetCoverage URI reported in capabilities" ) );
120 cbxWmsIgnoreAxisOrientation->setText( tr(
"Ignore axis orientation" ) );
123 mWmsOptionsGroupBox->setTitle( tr(
"WCS Options" ) );
125 cbxIgnoreGetFeatureInfoURI->setVisible(
false );
126 mGroupBox->layout()->removeWidget( cbxIgnoreGetFeatureInfoURI );
128 cmbDpiMode->setVisible(
false );
129 mGroupBox->layout()->removeWidget( cmbDpiMode );
130 lblDpiMode->setVisible(
false );
131 mGroupBox->layout()->removeWidget( lblDpiMode );
137 mTestConnectionButton->hide();
138 mGroupBox->layout()->removeWidget( mTestConnectionButton );
143 mAuthGroupBox->hide();
144 mGroupBox->layout()->removeWidget( mAuthGroupBox );
149 resize( w, height() );
151 connect( txtName, &QLineEdit::textChanged,
this, &QgsNewHttpConnection::nameChanged );
152 connect( txtUrl, &QLineEdit::textChanged,
this, &QgsNewHttpConnection::urlChanged );
154 buttonBox->button( QDialogButtonBox::Ok )->setDisabled(
true );
155 connect( txtName, &QLineEdit::textChanged,
this, &QgsNewHttpConnection::updateOkButtonState );
156 connect( txtUrl, &QLineEdit::textChanged,
this, &QgsNewHttpConnection::updateOkButtonState );
158 nameChanged( connectionName );
161 void QgsNewHttpConnection::wfsVersionCurrentIndexChanged(
int index )
171 void QgsNewHttpConnection::wfsFeaturePagingStateChanged(
int state )
173 lblPageSize->setEnabled( state == Qt::Checked );
174 txtPageSize->setEnabled( state == Qt::Checked );
179 return txtName->text();
184 return txtUrl->text();
187 void QgsNewHttpConnection::nameChanged(
const QString &text )
190 buttonBox->button( QDialogButtonBox::Ok )->setDisabled( txtName->text().isEmpty() || txtUrl->text().isEmpty() );
193 void QgsNewHttpConnection::urlChanged(
const QString &text )
196 buttonBox->button( QDialogButtonBox::Ok )->setDisabled( txtName->text().isEmpty() || txtUrl->text().isEmpty() );
197 mWfsVersionDetectButton->setDisabled( txtUrl->text().isEmpty() );
200 void QgsNewHttpConnection::updateOkButtonState()
202 bool enabled = !txtName->text().isEmpty() && !txtUrl->text().isEmpty();
203 buttonBox->button( QDialogButtonBox::Ok )->setEnabled( enabled );
209 QString key = mBaseKey + txtName->text();
212 if ( ( mOriginalConnName.isNull() || mOriginalConnName.compare( txtName->text(), Qt::CaseInsensitive ) != 0 ) &&
213 settings.
contains( key +
"/url" ) &&
214 QMessageBox::question(
this,
215 tr(
"Save Connection" ),
216 tr(
"Should the existing connection %1 be overwritten?" ).arg( txtName->text() ),
217 QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
222 if ( ! mAuthSettings->password().isEmpty() &&
223 QMessageBox::question(
this,
224 tr(
"Saving Passwords" ),
225 tr(
"WARNING: You have entered a password. It will be stored in unsecured plain text in your project files and your home directory (Unix-like OS) or user profile (Windows). If you want to avoid this, press Cancel and either:\n\na) Don't provide a password in the connection settings — it will be requested interactively when needed;\nb) Use the Configuration tab to add your credentials in an HTTP Basic Authentication method and store them in an encrypted database." ),
226 QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
236 return mTestConnectionButton;
241 return mAuthSettings;
246 return mWfsVersionDetectButton;
256 return cbxWfsFeaturePaging;
266 return base + connectionName;
271 return base + connectionName;
280 cbxIgnoreGetMapURI->setChecked( settings.
value( wmsKey +
"/ignoreGetMapURI",
false ).toBool() );
281 cbxWmsIgnoreReportedLayerExtents->setChecked( settings.
value( wmsKey + QStringLiteral(
"/ignoreReportedLayerExtents" ),
false ).toBool() );
282 cbxWfsIgnoreAxisOrientation->setChecked( settings.
value( wfsKey +
"/ignoreAxisOrientation",
false ).toBool() );
283 cbxWfsInvertAxisOrientation->setChecked( settings.
value( wfsKey +
"/invertAxisOrientation",
false ).toBool() );
284 cbxWmsIgnoreAxisOrientation->setChecked( settings.
value( wmsKey +
"/ignoreAxisOrientation",
false ).toBool() );
285 cbxWmsInvertAxisOrientation->setChecked( settings.
value( wmsKey +
"/invertAxisOrientation",
false ).toBool() );
286 cbxIgnoreGetFeatureInfoURI->setChecked( settings.
value( wmsKey +
"/ignoreGetFeatureInfoURI",
false ).toBool() );
287 cbxSmoothPixmapTransform->setChecked( settings.
value( wmsKey +
"/smoothPixmapTransform",
false ).toBool() );
290 switch ( settings.
value( wmsKey +
"/dpiMode", 7 ).toInt() )
308 cmbDpiMode->setCurrentIndex( dpiIdx );
310 QString version = settings.
value( wfsKey +
"/version" ).toString();
312 if ( version == QLatin1String(
"1.0.0" ) )
314 else if ( version == QLatin1String(
"1.1.0" ) )
316 else if ( version == QLatin1String(
"2.0.0" ) )
318 else if ( version == QLatin1String(
"OGC_API_FEATURES" ) )
320 cmbVersion->setCurrentIndex( versionIdx );
323 wfsVersionCurrentIndexChanged( versionIdx );
325 mRefererLineEdit->setText( settings.
value( wmsKey +
"/referer" ).toString() );
326 txtMaxNumFeatures->setText( settings.
value( wfsKey +
"/maxnumfeatures" ).toString() );
330 txtPageSize->setText( settings.
value( wfsKey +
"/pagesize" ).toString() );
331 cbxWfsFeaturePaging->setChecked( pagingEnabled );
339 return "0123456789ABCDEF"[value & 0xF];
342 static inline ushort encodeNibble( ushort
c )
349 while ( ptr + 4 <= end )
351 quint32 data = qFromUnaligned<quint32>( ptr );
352 if ( data &= 0x80808080U )
354 #if Q_BYTE_ORDER == Q_BIG_ENDIAN
355 uint idx = qCountLeadingZeroBits( data );
357 uint idx = qCountTrailingZeroBits( data );
366 if ( quint8( *ptr ) & 0x80 )
379 const char *in = ba.constData();
380 const char *
const end = ba.constEnd();
384 return QString::fromLatin1( ba, ba.size() );
387 QByteArray intermediate = ba;
388 intermediate.resize( ba.size() * 3 - ( in - ba.constData() ) );
389 uchar *out =
reinterpret_cast<uchar *
>( intermediate.data() + ( in - ba.constData() ) );
390 for ( ; in < end; ++in )
396 *out++ = encodeNibble( uchar( *in ) >> 4 );
397 *out++ = encodeNibble( uchar( *in ) & 0xf );
402 *out++ = uchar( *in );
406 return QString::fromLatin1( intermediate, out -
reinterpret_cast<uchar *
>( intermediate.data() ) );
412 QUrl
url( txtUrl->text().trimmed() );
413 QUrlQuery query(
url );
414 const QList<QPair<QString, QString> > items = query.queryItems( QUrl::FullyEncoded );
415 QHash< QString, QPair<QString, QString> > params;
416 for (
const QPair<QString, QString> &it : items )
418 params.insert( it.first.toUpper(), it );
421 if ( params[QStringLiteral(
"SERVICE" )].second.toUpper() ==
"WMS" ||
422 params[QStringLiteral(
"SERVICE" )].second.toUpper() ==
"WFS" ||
423 params[QStringLiteral(
"SERVICE" )].second.toUpper() ==
"WCS" )
425 query.removeQueryItem( params.value( QStringLiteral(
"SERVICE" ) ).first );
426 query.removeQueryItem( params.value( QStringLiteral(
"REQUEST" ) ).first );
427 query.removeQueryItem( params.value( QStringLiteral(
"FORMAT" ) ).first );
430 url.setQuery( query );
432 if (
url.path( QUrl::FullyEncoded ).isEmpty() )
442 QString key = mBaseKey + txtName->text();
443 QString credentialsKey =
"qgis/" + mCredentialsBaseKey +
'/' + txtName->text();
449 if ( !mOriginalConnName.isNull() && mOriginalConnName != key )
451 settings.
remove( mBaseKey + mOriginalConnName );
452 settings.
remove(
"qgis/" + mCredentialsBaseKey +
'/' + mOriginalConnName );
464 settings.
setValue( wfsKey +
"/ignoreAxisOrientation", cbxWfsIgnoreAxisOrientation->isChecked() );
465 settings.
setValue( wfsKey +
"/invertAxisOrientation", cbxWfsInvertAxisOrientation->isChecked() );
469 settings.
setValue( wmsKey +
"/ignoreAxisOrientation", cbxWmsIgnoreAxisOrientation->isChecked() );
470 settings.
setValue( wmsKey +
"/invertAxisOrientation", cbxWmsInvertAxisOrientation->isChecked() );
472 settings.
setValue( wmsKey + QStringLiteral(
"/ignoreReportedLayerExtents" ), cbxWmsIgnoreReportedLayerExtents->isChecked() );
473 settings.
setValue( wmsKey +
"/ignoreGetMapURI", cbxIgnoreGetMapURI->isChecked() );
474 settings.
setValue( wmsKey +
"/smoothPixmapTransform", cbxSmoothPixmapTransform->isChecked() );
477 switch ( cmbDpiMode->currentIndex() )
496 settings.
setValue( wmsKey +
"/dpiMode", dpiMode );
498 settings.
setValue( wmsKey +
"/referer", mRefererLineEdit->text() );
502 settings.
setValue( wmsKey +
"/ignoreGetFeatureInfoURI", cbxIgnoreGetFeatureInfoURI->isChecked() );
506 QString version = QStringLiteral(
"auto" );
507 switch ( cmbVersion->currentIndex() )
510 version = QStringLiteral(
"auto" );
513 version = QStringLiteral(
"1.0.0" );
516 version = QStringLiteral(
"1.1.0" );
519 version = QStringLiteral(
"2.0.0" );
522 version = QStringLiteral(
"OGC_API_FEATURES" );
525 settings.
setValue( wfsKey +
"/version", version );
527 settings.
setValue( wfsKey +
"/maxnumfeatures", txtMaxNumFeatures->text() );
529 settings.
setValue( wfsKey +
"/pagesize", txtPageSize->text() );
530 settings.
setValue( wfsKey +
"/pagingenabled", cbxWfsFeaturePaging->isChecked() );
533 settings.
setValue( credentialsKey +
"/username", mAuthSettings->username() );
534 settings.
setValue( credentialsKey +
"/password", mAuthSettings->password() );
536 settings.
setValue( credentialsKey +
"/authcfg", mAuthSettings->configId() );
538 if ( mHttpGroupBox->isVisible() )
539 settings.
setValue( key +
"/referer", mRefererLineEdit->text() );
541 settings.
setValue( mBaseKey +
"/selected", txtName->text() );
546 void QgsNewHttpConnection::showHelp()