35 : QDialog( parent, fl )
36 , mOriginalConnName( connName )
41 connect( btnConnect, &QPushButton::clicked,
this, &QgsNewOgrConnection::btnConnect_clicked );
46 buttonBox->button( QDialogButtonBox::Ok )->setDisabled(
true );
47 connect( txtName, &QLineEdit::textChanged,
this, &QgsNewOgrConnection::updateOkButtonState );
48 connect( txtHost, &QLineEdit::textChanged,
this, &QgsNewOgrConnection::updateOkButtonState );
49 connect( txtDatabase, &QLineEdit::textChanged,
this, &QgsNewOgrConnection::updateOkButtonState );
50 connect( txtPort, &QLineEdit::textChanged,
this, &QgsNewOgrConnection::updateOkButtonState );
56 for (
int i = 0; i < dbDrivers.count(); i++ )
58 const QString dbDriver = dbDrivers.at( i );
59 if ( !dbDriver.isEmpty() )
60 cmbDatabaseTypes->addItem( dbDriver.split(
',' ).at( 0 ) );
62 txtName->setEnabled(
true );
63 cmbDatabaseTypes->setEnabled(
true );
65 if ( !connType.isEmpty() )
67 cmbDatabaseTypes->setCurrentIndex( cmbDatabaseTypes->findText( connType ) );
70 if ( !connName.isEmpty() )
74 const QString key =
'/' + connType +
"/connections/" + connName;
75 txtHost->setText( settings.
value( key +
"/host" ).toString() );
76 txtDatabase->setText( settings.
value( key +
"/database" ).toString() );
77 const QString port = settings.
value( key +
"/port" ).toString();
78 txtPort->setText( port );
79 if ( settings.
value( key +
"/store_username" ).toString() == QLatin1String(
"true" ) )
81 mAuthSettingsDatabase->setUsername( settings.
value( key +
"/username" ).toString() );
82 mAuthSettingsDatabase->setStoreUsernameChecked(
true );
84 if ( settings.
value( key +
"/store_password" ).toString() == QLatin1String(
"true" ) )
86 mAuthSettingsDatabase->setPassword( settings.
value( key +
"/password" ).toString() );
87 mAuthSettingsDatabase->setStorePasswordChecked(
true );
89 mAuthSettingsDatabase->setConfigId( settings.
value( key +
"/configid" ).toString() );
90 txtName->setText( connName );
91 txtName->setEnabled(
false );
92 cmbDatabaseTypes->setEnabled(
false );
94 txtName->setValidator(
new QRegularExpressionValidator( QRegularExpression(
"[^\\/]+" ), txtName ) );
95 mAuthSettingsDatabase->setDataprovider( QStringLiteral(
"ogr" ) );
96 mAuthSettingsDatabase->showStoreCheckboxes(
true );
101 QString uri =
QgsGdalGuiUtils::createDatabaseURI( cmbDatabaseTypes->currentText(), txtHost->text(), txtDatabase->text(), txtPort->text(), mAuthSettingsDatabase->configId(), mAuthSettingsDatabase->username(), mAuthSettingsDatabase->password(),
true );
105 OGRSFDriverH pahDriver;
107 poDS = OGROpen( uri.toUtf8().constData(),
false, &pahDriver );
110 QMessageBox::information(
this, tr(
"Test Connection" ), tr(
"Connection failed - Check settings and try again.\n\nExtended error information:\n%1" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
114 QMessageBox::information(
this, tr(
"Test Connection" ), tr(
"Connection to %1 was successful." ).arg( uri ) );
115 OGRReleaseDataSource( poDS );
135 QString baseKey =
'/' + cmbDatabaseTypes->currentText() +
"/connections/";
136 settings.
setValue( baseKey +
"selected", txtName->text() );
139 if ( ( mOriginalConnName.isNull() || mOriginalConnName != txtName->text() ) && settings.
contains( baseKey + txtName->text() +
"/host" ) && QMessageBox::question(
this, tr(
"Save Connection" ), tr(
"Should the existing connection %1 be overwritten?" ).arg( txtName->text() ), QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
145 if ( !mOriginalConnName.isNull() && mOriginalConnName != txtName->text() )
147 settings.
remove( baseKey + mOriginalConnName );
150 baseKey += txtName->text();
151 settings.
setValue( baseKey +
"/host", txtHost->text() );
152 settings.
setValue( baseKey +
"/database", txtDatabase->text() );
153 settings.
setValue( baseKey +
"/port", txtPort->text() );
154 settings.
setValue( baseKey +
"/username", mAuthSettingsDatabase->storeUsernameIsChecked() ? mAuthSettingsDatabase->username() : QString() );
155 settings.
setValue( baseKey +
"/password", mAuthSettingsDatabase->storePasswordIsChecked() ? mAuthSettingsDatabase->password() : QString() );
156 settings.
setValue( baseKey +
"/store_username", mAuthSettingsDatabase->storeUsernameIsChecked() ?
"true" :
"false" );
157 settings.
setValue( baseKey +
"/store_password", mAuthSettingsDatabase->storePasswordIsChecked() ?
"true" :
"false" );
158 settings.
setValue( baseKey +
"/configid", mAuthSettingsDatabase->configId() );
static QString createDatabaseURI(const QString &connectionType, const QString &host, const QString &database, QString port, const QString &configId, QString username, QString password, bool expandAuthConfig=false)
Create database uri from connection parameters.