QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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"
24 #include "qgsogrhelperfunctions.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 
33 QgsNewOgrConnection::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 dbDrive = dbDrivers.at( i );
58  cmbDatabaseTypes->addItem( dbDrive.split( ',' ).at( 0 ) );
59  }
60  txtName->setEnabled( true );
61  cmbDatabaseTypes->setEnabled( true );
62  if ( !connName.isEmpty() )
63  {
64  // populate the dialog with the information stored for the connection
65  // populate the fields with the stored setting parameters
66  const QString key = '/' + connType + "/connections/" + connName;
67  txtHost->setText( settings.value( key + "/host" ).toString() );
68  txtDatabase->setText( settings.value( key + "/database" ).toString() );
69  const QString port = settings.value( key + "/port" ).toString();
70  txtPort->setText( port );
71  if ( settings.value( key + "/store_username" ).toString() == QLatin1String( "true" ) )
72  {
73  mAuthSettingsDatabase->setUsername( settings.value( key + "/username" ).toString() );
74  mAuthSettingsDatabase->setStoreUsernameChecked( true );
75  }
76  if ( settings.value( key + "/store_password" ).toString() == QLatin1String( "true" ) )
77  {
78  mAuthSettingsDatabase->setPassword( settings.value( key + "/password" ).toString() );
79  mAuthSettingsDatabase->setStorePasswordChecked( true );
80  }
81  mAuthSettingsDatabase->setConfigId( settings.value( key + "/configid" ).toString() );
82  cmbDatabaseTypes->setCurrentIndex( cmbDatabaseTypes->findText( connType ) );
83  txtName->setText( connName );
84  txtName->setEnabled( false );
85  cmbDatabaseTypes->setEnabled( false );
86  }
87  txtName->setValidator( new QRegularExpressionValidator( QRegularExpression( "[^\\/]+" ), txtName ) );
88  mAuthSettingsDatabase->setDataprovider( QStringLiteral( "ogr" ) );
89  mAuthSettingsDatabase->showStoreCheckboxes( true );
90 }
91 
93 {
94  QString uri;
95  uri = createDatabaseURI( cmbDatabaseTypes->currentText(),
96  txtHost->text(),
97  txtDatabase->text(),
98  txtPort->text(),
99  mAuthSettingsDatabase->configId(),
100  mAuthSettingsDatabase->username(),
101  mAuthSettingsDatabase->password(),
102  true );
103  QgsDebugMsg( "Connecting using uri = " + uri );
104  OGRRegisterAll();
105  OGRDataSourceH poDS;
106  OGRSFDriverH pahDriver;
107  CPLErrorReset();
108  poDS = OGROpen( uri.toUtf8().constData(), false, &pahDriver );
109  if ( !poDS )
110  {
111  QMessageBox::information( this, tr( "Test Connection" ), tr( "Connection failed - Check settings and try again.\n\nExtended error information:\n%1" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
112  }
113  else
114  {
115  QMessageBox::information( this, tr( "Test Connection" ), tr( "Connection to %1 was successful." ).arg( uri ) );
116  OGRReleaseDataSource( poDS );
117  }
118 }
119 
121 {
122  QgsHelp::openHelp( QStringLiteral( "managing_data_source/opening_data.html#creating-a-stored-connection" ) );
123 }
124 
125 void QgsNewOgrConnection::updateOkButtonState()
126 {
127  const bool enabled = !txtName->text().isEmpty();
128  buttonBox->button( QDialogButtonBox::Ok )->setEnabled( enabled );
129 }
130 
131 
134 {
135  QgsSettings settings;
136  QString baseKey = '/' + cmbDatabaseTypes->currentText() + "/connections/";
137  settings.setValue( baseKey + "selected", txtName->text() );
138 
139  // warn if entry was renamed to an existing connection
140  if ( ( mOriginalConnName.isNull() || mOriginalConnName != txtName->text() ) &&
141  settings.contains( baseKey + txtName->text() + "/host" ) &&
142  QMessageBox::question( this,
143  tr( "Save Connection" ),
144  tr( "Should the existing connection %1 be overwritten?" ).arg( txtName->text() ),
145  QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
146  {
147  return;
148  }
149 
150  // on rename delete original entry first
151  if ( !mOriginalConnName.isNull() && mOriginalConnName != txtName->text() )
152  {
153  settings.remove( baseKey + mOriginalConnName );
154  }
155 
156  baseKey += txtName->text();
157  settings.setValue( baseKey + "/host", txtHost->text() );
158  settings.setValue( baseKey + "/database", txtDatabase->text() );
159  settings.setValue( baseKey + "/port", txtPort->text() );
160  settings.setValue( baseKey + "/username", mAuthSettingsDatabase->storeUsernameIsChecked() ? mAuthSettingsDatabase->username() : QString() );
161  settings.setValue( baseKey + "/password", mAuthSettingsDatabase->storePasswordIsChecked() ? mAuthSettingsDatabase->password() : QString() );
162  settings.setValue( baseKey + "/store_username", mAuthSettingsDatabase->storeUsernameIsChecked() ? "true" : "false" );
163  settings.setValue( baseKey + "/store_password", mAuthSettingsDatabase->storePasswordIsChecked() ? "true" : "false" );
164  settings.setValue( baseKey + "/configid", mAuthSettingsDatabase->configId() );
165 
166  QDialog::accept();
167 }
168 
169 void QgsNewOgrConnection::btnConnect_clicked()
170 {
171  testConnection();
172 }
173 
QgsSettings::remove
void remove(const QString &key, QgsSettings::Section section=QgsSettings::NoSection)
Removes the setting key and any sub-settings of key in a section.
Definition: qgssettings.cpp:192
qgsnewogrconnection.h
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition: qgssettings.cpp:161
qgsgui.h
QgsNewOgrConnection::QgsNewOgrConnection
QgsNewOgrConnection(QWidget *parent=nullptr, const QString &connType=QString(), const QString &connName=QString(), Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags)
Constructor.
Definition: qgsnewogrconnection.cpp:33
QgsSettings
This class is a composition of two QSettings instances:
Definition: qgssettings.h:61
QgsDebugMsg
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
qgsapplication.h
QgsGui::enableAutoGeometryRestore
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:180
Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:2820
qgsproviderregistry.h
createDatabaseURI
QString createDatabaseURI(const QString &connectionType, const QString &host, const QString &database, QString port, const QString &configId, QString username, QString password, bool expandAuthConfig)
CreateDatabaseURI.
Definition: qgsogrhelperfunctions.cpp:25
QgsNewOgrConnection::testConnection
void testConnection()
Tests the connection using the parameters supplied.
Definition: qgsnewogrconnection.cpp:92
qgsogrhelperfunctions.h
QgsSettings::setValue
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
Definition: qgssettings.cpp:279
QgsNewOgrConnection::accept
void accept() override
Autoconnected SLOTS.
Definition: qgsnewogrconnection.cpp:133
QgsProviderRegistry::databaseDrivers
QString databaseDrivers() const
Returns a string containing the available database drivers.
Definition: qgsproviderregistry.cpp:848
QgsHelp::openHelp
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:36
qgssettings.h
QgsSettings::contains
bool contains(const QString &key, QgsSettings::Section section=QgsSettings::NoSection) const
Returns true if there exists a setting called key; returns false otherwise.
Definition: qgssettings.cpp:175
qgslogger.h
Q_NOWARN_DEPRECATED_PUSH
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:2819
QgsProviderRegistry::instance
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
Definition: qgsproviderregistry.cpp:73
qgshelp.h
QgsNewOgrConnection::showHelp
Q_DECL_DEPRECATED void showHelp() SIP_DEPRECATED
Show the help.
Definition: qgsnewogrconnection.cpp:120