QGIS API Documentation 3.29.0-Master (8c80f25a4f)
qgsnewogrconnection.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsnewogrconnection.cpp - description
3 -------------------
4 begin : Mon Jan 2 2009
5 copyright : (C) 2009 by Godofredo Contreras Nava
6 email : frdcn at hotmail.com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17#include <QMessageBox>
18#include <QRegularExpressionValidator>
19#include <QRegularExpression>
20
21#include "qgsnewogrconnection.h"
22#include "qgslogger.h"
23#include "qgsproviderregistry.h"
25#include "qgsapplication.h"
26#include "qgssettings.h"
27#include "qgsgui.h"
28
29#include <ogr_api.h>
30#include <cpl_error.h>
31#include "qgshelp.h"
32
33QgsNewOgrConnection::QgsNewOgrConnection( QWidget *parent, const QString &connType, const QString &connName, Qt::WindowFlags fl )
34 : QDialog( parent, fl )
35 , mOriginalConnName( connName )
36{
37 setupUi( this );
39
40 connect( btnConnect, &QPushButton::clicked, this, &QgsNewOgrConnection::btnConnect_clicked );
42 connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsNewOgrConnection::showHelp );
44
45 buttonBox->button( QDialogButtonBox::Ok )->setDisabled( true );
46 connect( txtName, &QLineEdit::textChanged, this, &QgsNewOgrConnection::updateOkButtonState );
47 connect( txtHost, &QLineEdit::textChanged, this, &QgsNewOgrConnection::updateOkButtonState );
48 connect( txtDatabase, &QLineEdit::textChanged, this, &QgsNewOgrConnection::updateOkButtonState );
49 connect( txtPort, &QLineEdit::textChanged, this, &QgsNewOgrConnection::updateOkButtonState );
50
51 const QgsSettings settings;
52
53 //add database drivers
54 const QStringList dbDrivers = QgsProviderRegistry::instance()->databaseDrivers().split( ';' );
55 for ( int i = 0; i < dbDrivers.count(); i++ )
56 {
57 const QString dbDriver = dbDrivers.at( i );
58 if ( !dbDriver.isEmpty() )
59 cmbDatabaseTypes->addItem( dbDriver.split( ',' ).at( 0 ) );
60 }
61 txtName->setEnabled( true );
62 cmbDatabaseTypes->setEnabled( true );
63
64 if ( !connType.isEmpty() )
65 {
66 cmbDatabaseTypes->setCurrentIndex( cmbDatabaseTypes->findText( connType ) );
67 }
68
69 if ( !connName.isEmpty() )
70 {
71 // populate the dialog with the information stored for the connection
72 // populate the fields with the stored setting parameters
73 const QString key = '/' + connType + "/connections/" + connName;
74 txtHost->setText( settings.value( key + "/host" ).toString() );
75 txtDatabase->setText( settings.value( key + "/database" ).toString() );
76 const QString port = settings.value( key + "/port" ).toString();
77 txtPort->setText( port );
78 if ( settings.value( key + "/store_username" ).toString() == QLatin1String( "true" ) )
79 {
80 mAuthSettingsDatabase->setUsername( settings.value( key + "/username" ).toString() );
81 mAuthSettingsDatabase->setStoreUsernameChecked( true );
82 }
83 if ( settings.value( key + "/store_password" ).toString() == QLatin1String( "true" ) )
84 {
85 mAuthSettingsDatabase->setPassword( settings.value( key + "/password" ).toString() );
86 mAuthSettingsDatabase->setStorePasswordChecked( true );
87 }
88 mAuthSettingsDatabase->setConfigId( settings.value( key + "/configid" ).toString() );
89 txtName->setText( connName );
90 txtName->setEnabled( false );
91 cmbDatabaseTypes->setEnabled( false );
92 }
93 txtName->setValidator( new QRegularExpressionValidator( QRegularExpression( "[^\\/]+" ), txtName ) );
94 mAuthSettingsDatabase->setDataprovider( QStringLiteral( "ogr" ) );
95 mAuthSettingsDatabase->showStoreCheckboxes( true );
96}
97
99{
100 QString uri;
101 uri = createDatabaseURI( cmbDatabaseTypes->currentText(),
102 txtHost->text(),
103 txtDatabase->text(),
104 txtPort->text(),
105 mAuthSettingsDatabase->configId(),
106 mAuthSettingsDatabase->username(),
107 mAuthSettingsDatabase->password(),
108 true );
109 QgsDebugMsg( "Connecting using uri = " + uri );
110 OGRRegisterAll();
111 OGRDataSourceH poDS;
112 OGRSFDriverH pahDriver;
113 CPLErrorReset();
114 poDS = OGROpen( uri.toUtf8().constData(), false, &pahDriver );
115 if ( !poDS )
116 {
117 QMessageBox::information( this, tr( "Test Connection" ), tr( "Connection failed - Check settings and try again.\n\nExtended error information:\n%1" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
118 }
119 else
120 {
121 QMessageBox::information( this, tr( "Test Connection" ), tr( "Connection to %1 was successful." ).arg( uri ) );
122 OGRReleaseDataSource( poDS );
123 }
124}
125
127{
128 QgsHelp::openHelp( QStringLiteral( "managing_data_source/opening_data.html#creating-a-stored-connection" ) );
129}
130
131void QgsNewOgrConnection::updateOkButtonState()
132{
133 const bool enabled = !txtName->text().isEmpty();
134 buttonBox->button( QDialogButtonBox::Ok )->setEnabled( enabled );
135}
136
137
140{
141 QgsSettings settings;
142 QString baseKey = '/' + cmbDatabaseTypes->currentText() + "/connections/";
143 settings.setValue( baseKey + "selected", txtName->text() );
144
145 // warn if entry was renamed to an existing connection
146 if ( ( mOriginalConnName.isNull() || mOriginalConnName != txtName->text() ) &&
147 settings.contains( baseKey + txtName->text() + "/host" ) &&
148 QMessageBox::question( this,
149 tr( "Save Connection" ),
150 tr( "Should the existing connection %1 be overwritten?" ).arg( txtName->text() ),
151 QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
152 {
153 return;
154 }
155
156 // on rename delete original entry first
157 if ( !mOriginalConnName.isNull() && mOriginalConnName != txtName->text() )
158 {
159 settings.remove( baseKey + mOriginalConnName );
160 }
161
162 baseKey += txtName->text();
163 settings.setValue( baseKey + "/host", txtHost->text() );
164 settings.setValue( baseKey + "/database", txtDatabase->text() );
165 settings.setValue( baseKey + "/port", txtPort->text() );
166 settings.setValue( baseKey + "/username", mAuthSettingsDatabase->storeUsernameIsChecked() ? mAuthSettingsDatabase->username() : QString() );
167 settings.setValue( baseKey + "/password", mAuthSettingsDatabase->storePasswordIsChecked() ? mAuthSettingsDatabase->password() : QString() );
168 settings.setValue( baseKey + "/store_username", mAuthSettingsDatabase->storeUsernameIsChecked() ? "true" : "false" );
169 settings.setValue( baseKey + "/store_password", mAuthSettingsDatabase->storePasswordIsChecked() ? "true" : "false" );
170 settings.setValue( baseKey + "/configid", mAuthSettingsDatabase->configId() );
171
172 QDialog::accept();
173}
174
175void QgsNewOgrConnection::btnConnect_clicked()
176{
178}
179
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...
Definition: qgsgui.cpp:178
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:38
void testConnection()
Tests the connection using the parameters supplied.
void accept() override
Autoconnected SLOTS.
Q_DECL_DEPRECATED void showHelp() SIP_DEPRECATED
Show the help.
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.
QString databaseDrivers() const
Returns a string containing the available database drivers.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
bool contains(const QString &key, QgsSettings::Section section=QgsSettings::NoSection) const
Returns true if there exists a setting called key; returns false otherwise.
void remove(const QString &key, QgsSettings::Section section=QgsSettings::NoSection)
Removes the setting key and any sub-settings of key in a section.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:3499
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:3498
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QString createDatabaseURI(const QString &connectionType, const QString &host, const QString &database, QString port, const QString &configId, QString username, QString password, bool expandAuthConfig)
CreateDatabaseURI.