QGIS API Documentation 3.41.0-Master (af5edcb665c)
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(), txtHost->text(), txtDatabase->text(), txtPort->text(), mAuthSettingsDatabase->configId(), mAuthSettingsDatabase->username(), mAuthSettingsDatabase->password(), true );
102 QgsDebugMsgLevel( "Connecting using uri = " + uri, 2 );
103 OGRRegisterAll();
104 OGRDataSourceH poDS;
105 OGRSFDriverH pahDriver;
106 CPLErrorReset();
107 poDS = OGROpen( uri.toUtf8().constData(), false, &pahDriver );
108 if ( !poDS )
109 {
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() ) ) );
111 }
112 else
113 {
114 QMessageBox::information( this, tr( "Test Connection" ), tr( "Connection to %1 was successful." ).arg( uri ) );
115 OGRReleaseDataSource( poDS );
116 }
117}
118
120{
121 QgsHelp::openHelp( QStringLiteral( "managing_data_source/opening_data.html#creating-a-stored-connection" ) );
122}
123
124void QgsNewOgrConnection::updateOkButtonState()
125{
126 const bool enabled = !txtName->text().isEmpty();
127 buttonBox->button( QDialogButtonBox::Ok )->setEnabled( enabled );
128}
129
130
133{
134 QgsSettings settings;
135 QString baseKey = '/' + cmbDatabaseTypes->currentText() + "/connections/";
136 settings.setValue( baseKey + "selected", txtName->text() );
137
138 // warn if entry was renamed to an existing connection
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 )
140 {
141 return;
142 }
143
144 // on rename delete original entry first
145 if ( !mOriginalConnName.isNull() && mOriginalConnName != txtName->text() )
146 {
147 settings.remove( baseKey + mOriginalConnName );
148 }
149
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() );
159
160 QDialog::accept();
161}
162
163void QgsNewOgrConnection::btnConnect_clicked()
164{
166}
167
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:210
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:6643
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6642
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39