Skip to content

Commit

Permalink
Rework QgsGeoNodeNewConnection to inherit from QgsNewHttpConnection
Browse files Browse the repository at this point in the history
Avoids a lot of duplicate code.

Note that while this refactoring was done to allow WFS and WMS
settings to be simultaneously visible, I've removed the settings
from the GeoNode connection for now. Looking into this they were
being stored, but not used at all when loading the layers from
the GeoNode instance.
  • Loading branch information
nyalldawson committed Sep 12, 2017
1 parent 90237fa commit ffaafa6
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 600 deletions.
31 changes: 31 additions & 0 deletions python/gui/qgsnewhttpconnection.sip
Expand Up @@ -55,11 +55,42 @@ class QgsNewHttpConnection : QDialog
and appearance.
%End

QString name() const;
%Docstring
Returns the current connection name.
.. versionadded:: 3.0
:rtype: str
%End

QString url() const;
%Docstring
Returns the current connection url.
.. versionadded:: 3.0
:rtype: str
%End

public slots:

virtual void accept();


protected:

virtual bool validate();
%Docstring
Returns true if dialog settings are valid, or false if current
settings are not valid and the dialog should not be acceptable.
.. versionadded:: 3.0
:rtype: bool
%End

QPushButton *testConnectButton();
%Docstring
Returns the "test connection" button.
.. versionadded:: 3.0
:rtype: QPushButton
%End

};

QFlags<QgsNewHttpConnection::ConnectionType> operator|(QgsNewHttpConnection::ConnectionType f1, QFlags<QgsNewHttpConnection::ConnectionType> f2);
Expand Down
246 changes: 19 additions & 227 deletions src/app/geocms/geonode/qgsgeonodenewconnection.cpp
Expand Up @@ -16,243 +16,22 @@
***************************************************************************/

#include <QMessageBox>
#include <QUrl>
#include "qgslogger.h"

#include "qgsgeonodenewconnection.h"
#include "qgsauthmanager.h"
#include "qgsdatasourceuri.h"
#include "qgsgeonodeconnection.h"
#include "qgssettings.h"
#include "qgsgeonoderequest.h"

QgsGeoNodeNewConnection::QgsGeoNodeNewConnection( QWidget *parent, const QString &connName, Qt::WindowFlags fl )
: QDialog( parent, fl )
, mOriginalConnName( connName )
, mAuthConfigSelect( nullptr )
: QgsNewHttpConnection( parent, 0, QgsGeoNodeConnectionUtils::pathGeoNodeConnection(), connName, QgsNewHttpConnection::FlagShowTestConnection, fl )
{
setupUi( this );

mBaseKey = QgsGeoNodeConnectionUtils::pathGeoNodeConnection();
mCredentialsBaseKey = QgsGeoNodeConnectionUtils::pathGeoNodeConnection();

mAuthConfigSelect = new QgsAuthConfigSelect( this );
tabAuth->insertTab( 1, mAuthConfigSelect, tr( "Configurations" ) );

cmbDpiMode->clear();
cmbDpiMode->addItem( tr( "all" ) );
cmbDpiMode->addItem( tr( "off" ) );
cmbDpiMode->addItem( tr( "QGIS" ) );
cmbDpiMode->addItem( tr( "UMN" ) );
cmbDpiMode->addItem( tr( "GeoServer" ) );

cmbVersion->clear();
cmbVersion->addItem( tr( "Auto-detect" ) );
cmbVersion->addItem( tr( "1.0" ) );
cmbVersion->addItem( tr( "1.1" ) );
cmbVersion->addItem( tr( "2.0" ) );

if ( !connName.isEmpty() )
{
// populate the dialog with the information stored for the connection
// populate the fields with the stored setting parameters
QgsSettings settings;

QString key = mBaseKey + '/' + connName;
QString credentialsKey = mCredentialsBaseKey + '/' + connName;
txtName->setText( connName );
txtUrl->setText( settings.value( key + "/url", "", QgsSettings::Providers ).toString() );

cbxIgnoreGetMapURI->setChecked( settings.value( key + "/wms/ignoreGetMapURI", false, QgsSettings::Providers ).toBool() );
cbxWfsIgnoreAxisOrientation->setChecked( settings.value( key + "/wfs/ignoreAxisOrientation", false, QgsSettings::Providers ).toBool() );
cbxWmsIgnoreAxisOrientation->setChecked( settings.value( key + "/wms/ignoreAxisOrientation", false, QgsSettings::Providers ).toBool() );
cbxWfsInvertAxisOrientation->setChecked( settings.value( key + "/wfs/invertAxisOrientation", false, QgsSettings::Providers ).toBool() );
cbxWmsInvertAxisOrientation->setChecked( settings.value( key + "/wms/invertAxisOrientation", false, QgsSettings::Providers ).toBool() );
cbxIgnoreGetFeatureInfoURI->setChecked( settings.value( key + "/wms/ignoreGetFeatureInfoURI", false, QgsSettings::Providers ).toBool() );
cbxSmoothPixmapTransform->setChecked( settings.value( key + "/wms/smoothPixmapTransform", false, QgsSettings::Providers ).toBool() );

int dpiIdx;
switch ( settings.value( key + "/dpiMode", 7, QgsSettings::Providers ).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 );

QString version = settings.value( key + "/version", QLatin1String( "1.0.0" ), QgsSettings::Providers ).toString();
int versionIdx = 0; // AUTO
if ( version == QLatin1String( "1.0.0" ) )
versionIdx = 1;
else if ( version == QLatin1String( "1.1.0" ) )
versionIdx = 2;
else if ( version == QLatin1String( "2.0.0" ) )
versionIdx = 3;
cmbVersion->setCurrentIndex( versionIdx );

txtReferer->setText( settings.value( key + "/referer", "", QgsSettings::Providers ).toString() );
txtMaxNumFeatures->setText( settings.value( key + "/maxnumfeatures", QgsSettings::Providers ).toString() );

txtUserName->setText( settings.value( credentialsKey + "/username", "", QgsSettings::Providers ).toString() );
txtPassword->setText( settings.value( credentialsKey + "/password", "", QgsSettings::Providers ).toString() );

QString authcfg = settings.value( credentialsKey + "/authcfg", "", QgsSettings::Providers ).toString();
mAuthConfigSelect->setConfigId( authcfg );
if ( !authcfg.isEmpty() )
{
tabAuth->setCurrentIndex( tabAuth->indexOf( mAuthConfigSelect ) );
}
}
setWindowTitle( tr( "Create a New GeoNode Connection" ) );

// Adjust height
int w = width();
adjustSize();
resize( w, height() );

buttonBox->button( QDialogButtonBox::Ok )->setDisabled( true );
connect( txtName, &QLineEdit::textChanged, this, &QgsGeoNodeNewConnection::okButtonBehavior );
connect( txtUrl, &QLineEdit::textChanged, this, &QgsGeoNodeNewConnection::okButtonBehavior );
connect( btnConnect, &QPushButton::clicked, this, &QgsGeoNodeNewConnection::testConnection );
}

QString QgsGeoNodeNewConnection::name() const
{
return txtName->text();
}

void QgsGeoNodeNewConnection::accept()
{
QgsSettings settings;
QString key = mBaseKey + '/' + txtName->text();
QString credentialsKey = mCredentialsBaseKey + '/' + txtName->text();

// warn if entry was renamed to an existing connection
if ( ( mOriginalConnName.isNull() || mOriginalConnName.compare( txtName->text(), Qt::CaseInsensitive ) != 0 ) &&
settings.contains( key + "/url", QgsSettings::Providers ) &&
QMessageBox::question( this,
tr( "Save connection" ),
tr( "Should the existing connection %1 be overwritten?" ).arg( txtName->text() ),
QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
{
return;
}

if ( !txtPassword->text().isEmpty() &&
QMessageBox::question( this,
tr( "Saving passwords" ),
trUtf8( "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." ),
QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
{
return;
}

// on rename delete original entry first
if ( !mOriginalConnName.isNull() && mOriginalConnName != key )
{
// Manually add Section here
settings.remove( "providers/" + mBaseKey + '/' + mOriginalConnName );
settings.remove( "providers/qgis//" + mCredentialsBaseKey + '/' + mOriginalConnName );
settings.sync();
}

if ( !txtUrl->text().contains( "://" ) &&
QMessageBox::information(
this,
tr( "Invalid URL" ),
tr( "Your URL doesn't contains protocol (e.g. http or https). Please add the protocol." ) ) == QMessageBox::Ok )
{
return;
}
QUrl url( txtUrl->text() );

settings.setValue( key + "/url", url.toString(), QgsSettings::Providers );

settings.setValue( key + "/wfs/ignoreAxisOrientation", cbxWfsIgnoreAxisOrientation->isChecked(), QgsSettings::Providers );
settings.setValue( key + "/wms/ignoreAxisOrientation", cbxWmsIgnoreAxisOrientation->isChecked(), QgsSettings::Providers );
settings.setValue( key + "/wfs/invertAxisOrientation", cbxWfsInvertAxisOrientation->isChecked(), QgsSettings::Providers );
settings.setValue( key + "/wms/invertAxisOrientation", cbxWmsInvertAxisOrientation->isChecked(), QgsSettings::Providers );

settings.setValue( key + "/wms/ignoreGetMapURI", cbxIgnoreGetMapURI->isChecked(), QgsSettings::Providers );
settings.setValue( key + "/wms/smoothPixmapTransform", cbxSmoothPixmapTransform->isChecked(), QgsSettings::Providers );
settings.setValue( key + "/wms/ignoreGetFeatureInfoURI", cbxIgnoreGetFeatureInfoURI->isChecked(), QgsSettings::Providers );

int dpiMode = 0;
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 + "/wms/dpiMode", dpiMode, QgsSettings::Providers );
settings.setValue( key + "/wms/referer", txtReferer->text(), QgsSettings::Providers );

QString version = QStringLiteral( "auto" );
switch ( cmbVersion->currentIndex() )
{
case 0:
version = QStringLiteral( "auto" );
break;
case 1:
version = QStringLiteral( "1.0.0" );
break;
case 2:
version = QStringLiteral( "1.1.0" );
break;
case 3:
version = QStringLiteral( "2.0.0" );
break;
}

settings.setValue( key + "/wfs/version", version, QgsSettings::Providers );
settings.setValue( key + "/wfs/maxnumfeatures", txtMaxNumFeatures->text(), QgsSettings::Providers );

settings.setValue( credentialsKey + "/username", txtUserName->text(), QgsSettings::Providers );
settings.setValue( credentialsKey + "/password", txtPassword->text(), QgsSettings::Providers );

settings.setValue( credentialsKey + "/authcfg", mAuthConfigSelect->configId(), QgsSettings::Providers );

settings.setValue( mBaseKey + "/selected", txtName->text(), QgsSettings::Providers );

QDialog::accept();
}

void QgsGeoNodeNewConnection::okButtonBehavior( const QString &text )
{
Q_UNUSED( text );
buttonBox->button( QDialogButtonBox::Ok )->setDisabled( txtName->text().isEmpty() || txtUrl->text().isEmpty() );
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( !txtName->text().isEmpty() && !txtUrl->text().isEmpty() );
connect( testConnectButton(), &QPushButton::clicked, this, &QgsGeoNodeNewConnection::testConnection );
}

void QgsGeoNodeNewConnection::testConnection()
{
QApplication::setOverrideCursor( Qt::BusyCursor );
QString url = txtUrl->text();
QgsGeoNodeRequest geonodeRequest( url, true );
QgsGeoNodeRequest geonodeRequest( url(), true );

QList<QgsGeoNodeRequest::ServiceLayerDetail> layers = geonodeRequest.fetchLayersBlocking();
QApplication::restoreOverrideCursor();
Expand All @@ -261,12 +40,25 @@ void QgsGeoNodeNewConnection::testConnection()
{
QMessageBox::information( this,
tr( "Test connection" ),
tr( "\nConnection to %1 was successful, \n\n%1 is a valid geonode instance.\n\n" ).arg( txtUrl->text() ) );
tr( "\nConnection to %1 was successful, \n\n%1 is a valid geonode instance.\n\n" ).arg( url() ) );
}
else
{
QMessageBox::information( this,
tr( "Test connection" ),
tr( "\nConnection failed, \n\nplease check whether %1 is a valid geonode instance.\n\n" ).arg( txtUrl->text() ) );
tr( "\nConnection failed, \n\nplease check whether %1 is a valid geonode instance.\n\n" ).arg( url() ) );
}
}

bool QgsGeoNodeNewConnection::validate()
{
if ( !url().contains( "://" ) )
{
QMessageBox::warning(
this,
tr( "Invalid URL" ),
tr( "Your URL doesn't contain a protocol (e.g. http or https). Please add the protocol." ) );
return false;
}
return QgsNewHttpConnection::validate();
}
21 changes: 7 additions & 14 deletions src/app/geocms/geonode/qgsgeonodenewconnection.h
Expand Up @@ -18,32 +18,25 @@
#ifndef QGSGEONODENEWCONNECTION_H
#define QGSGEONODENEWCONNECTION_H

#include "ui_qgsnewgeonodeconnectionbase.h"
#include "qgsguiutils.h"
#include "qgsauthconfigselect.h"
#include "qgsnewhttpconnection.h"

class QgsGeoNodeNewConnection : public QDialog, private Ui::QgsNewGeoNodeConnectionBase
class QgsGeoNodeNewConnection : public QgsNewHttpConnection
{
Q_OBJECT

public:
//! Constructor
QgsGeoNodeNewConnection( QWidget *parent = nullptr, const QString &connName = QString::null, Qt::WindowFlags fl = QgsGuiUtils::ModalDialogFlags );

//! Returns the new connection name
QString name() const;

public slots:
void accept() override;
void okButtonBehavior( const QString & );

//! Test the connection using the parameters supplied
void testConnection();

private:
QString mBaseKey;
QString mCredentialsBaseKey;
QString mOriginalConnName; //store initial name to delete entry in case of rename
QgsAuthConfigSelect *mAuthConfigSelect = nullptr;
protected:

bool validate() override;

};

#endif //QGSGEONODENEWCONNECTION_H

0 comments on commit ffaafa6

Please sign in to comment.