17 #include <QMessageBox> 18 #include <QRegExpValidator> 29 #include <cpl_error.h> 33 : QDialog( parent, fl )
34 , mOriginalConnName( connName )
39 connect( btnConnect, &QPushButton::clicked,
this, &QgsNewOgrConnection::btnConnect_clicked );
48 for (
int i = 0; i < dbDrivers.count(); i++ )
50 QString dbDrive = dbDrivers.at( i );
51 cmbDatabaseTypes->addItem( dbDrive.split(
',' ).at( 0 ) );
53 txtName->setEnabled(
true );
54 cmbDatabaseTypes->setEnabled(
true );
55 if ( !connName.isEmpty() )
59 QString key =
'/' + connType +
"/connections/" + connName;
60 txtHost->setText( settings.value( key +
"/host" ).toString() );
61 txtDatabase->setText( settings.value( key +
"/database" ).toString() );
62 QString port = settings.value( key +
"/port" ).toString();
63 txtPort->setText( port );
64 if ( settings.value( key +
"/store_username" ).toString() == QLatin1String(
"true" ) )
66 mAuthSettingsDatabase->setUsername( settings.value( key +
"/username" ).toString() );
67 mAuthSettingsDatabase->setStoreUsernameChecked(
true );
69 if ( settings.value( key +
"/store_password" ).toString() == QLatin1String(
"true" ) )
71 mAuthSettingsDatabase->setPassword( settings.value( key +
"/password" ).toString() );
72 mAuthSettingsDatabase->setStorePasswordChecked(
true );
74 mAuthSettingsDatabase->setConfigId( settings.value( key +
"/configid" ).toString() );
75 cmbDatabaseTypes->setCurrentIndex( cmbDatabaseTypes->findText( connType ) );
76 txtName->setText( connName );
77 txtName->setEnabled(
false );
78 cmbDatabaseTypes->setEnabled(
false );
80 txtName->setValidator(
new QRegExpValidator( QRegExp(
"[^\\/]+" ), txtName ) );
81 mAuthSettingsDatabase->setDataprovider( QStringLiteral(
"ogr" ) );
82 mAuthSettingsDatabase->showStoreCheckboxes(
true );
92 mAuthSettingsDatabase->configId(),
93 mAuthSettingsDatabase->username(),
94 mAuthSettingsDatabase->password(),
99 OGRSFDriverH pahDriver;
101 poDS = OGROpen( uri.toUtf8().constData(),
false, &pahDriver );
104 QMessageBox::information(
this, tr(
"Test Connection" ), tr(
"Connection failed - Check settings and try again.\n\nExtended error information:\n%1" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
108 QMessageBox::information(
this, tr(
"Test Connection" ), tr(
"Connection to %1 was successful." ).arg( uri ) );
109 OGRReleaseDataSource( poDS );
115 QgsHelp::openHelp( QStringLiteral(
"managing_data_source/opening_data.html#creating-a-stored-connection" ) );
122 QString baseKey =
'/' + cmbDatabaseTypes->currentText() +
"/connections/";
123 settings.
setValue( baseKey +
"selected", txtName->text() );
126 if ( ( mOriginalConnName.isNull() || mOriginalConnName != txtName->text() ) &&
127 settings.
contains( baseKey + txtName->text() +
"/host" ) &&
128 QMessageBox::question(
this,
129 tr(
"Save Connection" ),
130 tr(
"Should the existing connection %1 be overwritten?" ).arg( txtName->text() ),
131 QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
137 if ( !mOriginalConnName.isNull() && mOriginalConnName != txtName->text() )
139 settings.
remove( baseKey + mOriginalConnName );
142 baseKey += txtName->text();
143 settings.
setValue( baseKey +
"/host", txtHost->text() );
144 settings.
setValue( baseKey +
"/database", txtDatabase->text() );
145 settings.
setValue( baseKey +
"/port", txtPort->text() );
146 settings.
setValue( baseKey +
"/username", mAuthSettingsDatabase->storeUsernameIsChecked() ? mAuthSettingsDatabase->username() : QString() );
147 settings.
setValue( baseKey +
"/password", mAuthSettingsDatabase->storePasswordIsChecked() ? mAuthSettingsDatabase->password() : QString() );
148 settings.
setValue( baseKey +
"/store_username", mAuthSettingsDatabase->storeUsernameIsChecked() ?
"true" :
"false" );
149 settings.
setValue( baseKey +
"/store_password", mAuthSettingsDatabase->storePasswordIsChecked() ?
"true" :
"false" );
150 settings.
setValue( baseKey +
"/configid", mAuthSettingsDatabase->configId() );
155 void QgsNewOgrConnection::btnConnect_clicked()
This class is a composition of two QSettings instances:
#define Q_NOWARN_DEPRECATED_PUSH
void remove(const QString &key, QgsSettings::Section section=QgsSettings::NoSection)
Removes the setting key and any sub-settings of key in a section.
virtual QString databaseDrivers() const
Returns a string containing the available database drivers.
void testConnection()
Tests the connection using the parameters supplied.
QgsNewOgrConnection(QWidget *parent=nullptr, const QString &connType=QString(), const QString &connName=QString(), Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags)
Constructor.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
Q_DECL_DEPRECATED void showHelp() SIP_DEPRECATED
Show the help.
#define Q_NOWARN_DEPRECATED_POP
QString createDatabaseURI(const QString &connectionType, const QString &host, const QString &database, QString port, const QString &configId, QString username, QString password, bool expandAuthConfig)
CreateDatabaseURI.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
void accept() override
Autoconnected SLOTS *.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
bool contains(const QString &key, QgsSettings::Section section=QgsSettings::NoSection) const
Returns true if there exists a setting called key; returns false otherwise.