QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
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 "moc_qgsnewogrconnection.cpp"
23#include "qgslogger.h"
24#include "qgsproviderregistry.h"
25#include "qgsgdalguiutils.h"
26#include "qgsapplication.h"
27#include "qgssettings.h"
28#include "qgsgui.h"
29
30#include <ogr_api.h>
31#include <cpl_error.h>
32#include "qgshelp.h"
33
34QgsNewOgrConnection::QgsNewOgrConnection( QWidget *parent, const QString &connType, const QString &connName, Qt::WindowFlags fl )
35 : QDialog( parent, fl )
36 , mOriginalConnName( connName )
37{
38 setupUi( this );
40
41 connect( btnConnect, &QPushButton::clicked, this, &QgsNewOgrConnection::btnConnect_clicked );
43 connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsNewOgrConnection::showHelp );
45
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 );
51
52 const QgsSettings settings;
53
54 //add database drivers
55 const QStringList dbDrivers = QgsProviderRegistry::instance()->databaseDrivers().split( ';' );
56 for ( int i = 0; i < dbDrivers.count(); i++ )
57 {
58 const QString dbDriver = dbDrivers.at( i );
59 if ( !dbDriver.isEmpty() )
60 cmbDatabaseTypes->addItem( dbDriver.split( ',' ).at( 0 ) );
61 }
62 txtName->setEnabled( true );
63 cmbDatabaseTypes->setEnabled( true );
64
65 if ( !connType.isEmpty() )
66 {
67 cmbDatabaseTypes->setCurrentIndex( cmbDatabaseTypes->findText( connType ) );
68 }
69
70 if ( !connName.isEmpty() )
71 {
72 // populate the dialog with the information stored for the connection
73 // populate the fields with the stored setting parameters
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" ) )
80 {
81 mAuthSettingsDatabase->setUsername( settings.value( key + "/username" ).toString() );
82 mAuthSettingsDatabase->setStoreUsernameChecked( true );
83 }
84 if ( settings.value( key + "/store_password" ).toString() == QLatin1String( "true" ) )
85 {
86 mAuthSettingsDatabase->setPassword( settings.value( key + "/password" ).toString() );
87 mAuthSettingsDatabase->setStorePasswordChecked( true );
88 }
89 mAuthSettingsDatabase->setConfigId( settings.value( key + "/configid" ).toString() );
90 txtName->setText( connName );
91 txtName->setEnabled( false );
92 cmbDatabaseTypes->setEnabled( false );
93 }
94 txtName->setValidator( new QRegularExpressionValidator( QRegularExpression( "[^\\/]+" ), txtName ) );
95 mAuthSettingsDatabase->setDataprovider( QStringLiteral( "ogr" ) );
96 mAuthSettingsDatabase->showStoreCheckboxes( true );
97}
98
100{
101 QString uri = QgsGdalGuiUtils::createDatabaseURI( cmbDatabaseTypes->currentText(),
102 txtHost->text(),
103 txtDatabase->text(),
104 txtPort->text(),
105 mAuthSettingsDatabase->configId(),
106 mAuthSettingsDatabase->username(),
107 mAuthSettingsDatabase->password(),
108 true );
109 QgsDebugMsgLevel( "Connecting using uri = " + uri, 2 );
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 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.
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:209
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:39
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:64
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:6535
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6534
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39