QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 <QRegExpValidator>
19 
20 #include "qgsnewogrconnection.h"
21 #include "qgslogger.h"
22 #include "qgsproviderregistry.h"
23 #include "qgsogrhelperfunctions.h"
24 #include "qgsapplication.h"
25 #include "qgssettings.h"
26 #include "qgsgui.h"
27 
28 #include <ogr_api.h>
29 #include <cpl_error.h>
30 #include "qgshelp.h"
31 
32 QgsNewOgrConnection::QgsNewOgrConnection( QWidget *parent, const QString &connType, const QString &connName, Qt::WindowFlags fl )
33  : QDialog( parent, fl )
34  , mOriginalConnName( connName )
35 {
36  setupUi( this );
38 
39  connect( btnConnect, &QPushButton::clicked, this, &QgsNewOgrConnection::btnConnect_clicked );
41  connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsNewOgrConnection::showHelp );
43 
44  buttonBox->button( QDialogButtonBox::Ok )->setDisabled( true );
45  connect( txtName, &QLineEdit::textChanged, this, &QgsNewOgrConnection::updateOkButtonState );
46  connect( txtHost, &QLineEdit::textChanged, this, &QgsNewOgrConnection::updateOkButtonState );
47  connect( txtDatabase, &QLineEdit::textChanged, this, &QgsNewOgrConnection::updateOkButtonState );
48  connect( txtPort, &QLineEdit::textChanged, this, &QgsNewOgrConnection::updateOkButtonState );
49 
50  QgsSettings settings;
51 
52  //add database drivers
53  QStringList dbDrivers = QgsProviderRegistry::instance()->databaseDrivers().split( ';' );
54  for ( int i = 0; i < dbDrivers.count(); i++ )
55  {
56  QString dbDrive = dbDrivers.at( i );
57  cmbDatabaseTypes->addItem( dbDrive.split( ',' ).at( 0 ) );
58  }
59  txtName->setEnabled( true );
60  cmbDatabaseTypes->setEnabled( true );
61  if ( !connName.isEmpty() )
62  {
63  // populate the dialog with the information stored for the connection
64  // populate the fields with the stored setting parameters
65  QString key = '/' + connType + "/connections/" + connName;
66  txtHost->setText( settings.value( key + "/host" ).toString() );
67  txtDatabase->setText( settings.value( key + "/database" ).toString() );
68  QString port = settings.value( key + "/port" ).toString();
69  txtPort->setText( port );
70  if ( settings.value( key + "/store_username" ).toString() == QLatin1String( "true" ) )
71  {
72  mAuthSettingsDatabase->setUsername( settings.value( key + "/username" ).toString() );
73  mAuthSettingsDatabase->setStoreUsernameChecked( true );
74  }
75  if ( settings.value( key + "/store_password" ).toString() == QLatin1String( "true" ) )
76  {
77  mAuthSettingsDatabase->setPassword( settings.value( key + "/password" ).toString() );
78  mAuthSettingsDatabase->setStorePasswordChecked( true );
79  }
80  mAuthSettingsDatabase->setConfigId( settings.value( key + "/configid" ).toString() );
81  cmbDatabaseTypes->setCurrentIndex( cmbDatabaseTypes->findText( connType ) );
82  txtName->setText( connName );
83  txtName->setEnabled( false );
84  cmbDatabaseTypes->setEnabled( false );
85  }
86  txtName->setValidator( new QRegExpValidator( QRegExp( "[^\\/]+" ), txtName ) );
87  mAuthSettingsDatabase->setDataprovider( QStringLiteral( "ogr" ) );
88  mAuthSettingsDatabase->showStoreCheckboxes( true );
89 }
90 
92 {
93  QString uri;
94  uri = createDatabaseURI( cmbDatabaseTypes->currentText(),
95  txtHost->text(),
96  txtDatabase->text(),
97  txtPort->text(),
98  mAuthSettingsDatabase->configId(),
99  mAuthSettingsDatabase->username(),
100  mAuthSettingsDatabase->password(),
101  true );
102  QgsDebugMsg( "Connecting using uri = " + uri );
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 
124 void QgsNewOgrConnection::updateOkButtonState()
125 {
126  bool enabled = !txtName->text().isEmpty() && !txtHost->text().isEmpty() && !txtDatabase->text().isEmpty() && !txtPort->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() ) &&
140  settings.contains( baseKey + txtName->text() + "/host" ) &&
141  QMessageBox::question( this,
142  tr( "Save Connection" ),
143  tr( "Should the existing connection %1 be overwritten?" ).arg( txtName->text() ),
144  QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
145  {
146  return;
147  }
148 
149  // on rename delete original entry first
150  if ( !mOriginalConnName.isNull() && mOriginalConnName != txtName->text() )
151  {
152  settings.remove( baseKey + mOriginalConnName );
153  }
154 
155  baseKey += txtName->text();
156  settings.setValue( baseKey + "/host", txtHost->text() );
157  settings.setValue( baseKey + "/database", txtDatabase->text() );
158  settings.setValue( baseKey + "/port", txtPort->text() );
159  settings.setValue( baseKey + "/username", mAuthSettingsDatabase->storeUsernameIsChecked() ? mAuthSettingsDatabase->username() : QString() );
160  settings.setValue( baseKey + "/password", mAuthSettingsDatabase->storePasswordIsChecked() ? mAuthSettingsDatabase->password() : QString() );
161  settings.setValue( baseKey + "/store_username", mAuthSettingsDatabase->storeUsernameIsChecked() ? "true" : "false" );
162  settings.setValue( baseKey + "/store_password", mAuthSettingsDatabase->storePasswordIsChecked() ? "true" : "false" );
163  settings.setValue( baseKey + "/configid", mAuthSettingsDatabase->configId() );
164 
165  QDialog::accept();
166 }
167 
168 void QgsNewOgrConnection::btnConnect_clicked()
169 {
170  testConnection();
171 }
172 
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:205
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:174
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:32
QgsSettings
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:133
Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:752
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:91
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:289
QgsNewOgrConnection::accept
void accept() override
Autoconnected SLOTS *.
Definition: qgsnewogrconnection.cpp:132
QgsProviderRegistry::databaseDrivers
virtual QString databaseDrivers() const
Returns a string containing the available database drivers.
Definition: qgsproviderregistry.cpp:697
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:188
qgslogger.h
Q_NOWARN_DEPRECATED_PUSH
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:751
QgsProviderRegistry::instance
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
Definition: qgsproviderregistry.cpp:48
qgshelp.h
QgsNewOgrConnection::showHelp
Q_DECL_DEPRECATED void showHelp() SIP_DEPRECATED
Show the help.
Definition: qgsnewogrconnection.cpp:119