QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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  QgsSettings settings;
45 
46  //add database drivers
47  QStringList dbDrivers = QgsProviderRegistry::instance()->databaseDrivers().split( ';' );
48  for ( int i = 0; i < dbDrivers.count(); i++ )
49  {
50  QString dbDrive = dbDrivers.at( i );
51  cmbDatabaseTypes->addItem( dbDrive.split( ',' ).at( 0 ) );
52  }
53  txtName->setEnabled( true );
54  cmbDatabaseTypes->setEnabled( true );
55  if ( !connName.isEmpty() )
56  {
57  // populate the dialog with the information stored for the connection
58  // populate the fields with the stored setting parameters
59  QString key = '/' + connType + "/connections/" + connName;
60  txtHost->setText( settings.value( key + "/host" ).toString() );
61  txtDatabase->setText( settings.value( key + "/database" ).toString() );
62  QString port = settings.value( key + "/port" ).toString();
63  txtPort->setText( port );
64  if ( settings.value( key + "/store_username" ).toString() == QLatin1String( "true" ) )
65  {
66  mAuthSettingsDatabase->setUsername( settings.value( key + "/username" ).toString() );
67  mAuthSettingsDatabase->setStoreUsernameChecked( true );
68  }
69  if ( settings.value( key + "/store_password" ).toString() == QLatin1String( "true" ) )
70  {
71  mAuthSettingsDatabase->setPassword( settings.value( key + "/password" ).toString() );
72  mAuthSettingsDatabase->setStorePasswordChecked( true );
73  }
74  mAuthSettingsDatabase->setConfigId( settings.value( key + "/configid" ).toString() );
75  cmbDatabaseTypes->setCurrentIndex( cmbDatabaseTypes->findText( connType ) );
76  txtName->setText( connName );
77  txtName->setEnabled( false );
78  cmbDatabaseTypes->setEnabled( false );
79  }
80  txtName->setValidator( new QRegExpValidator( QRegExp( "[^\\/]+" ), txtName ) );
81  mAuthSettingsDatabase->setDataprovider( QStringLiteral( "ogr" ) );
82  mAuthSettingsDatabase->showStoreCheckboxes( true );
83 }
84 
86 {
87  QString uri;
88  uri = createDatabaseURI( cmbDatabaseTypes->currentText(),
89  txtHost->text(),
90  txtDatabase->text(),
91  txtPort->text(),
92  mAuthSettingsDatabase->configId(),
93  mAuthSettingsDatabase->username(),
94  mAuthSettingsDatabase->password(),
95  true );
96  QgsDebugMsg( "Connecting using uri = " + uri );
97  OGRRegisterAll();
98  OGRDataSourceH poDS;
99  OGRSFDriverH pahDriver;
100  CPLErrorReset();
101  poDS = OGROpen( uri.toUtf8().constData(), false, &pahDriver );
102  if ( !poDS )
103  {
104  QMessageBox::information( this, tr( "Test Connection" ), tr( "Connection failed - Check settings and try again.\n\nExtended error information:\n%1" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
105  }
106  else
107  {
108  QMessageBox::information( this, tr( "Test Connection" ), tr( "Connection to %1 was successful." ).arg( uri ) );
109  OGRReleaseDataSource( poDS );
110  }
111 }
112 
114 {
115  QgsHelp::openHelp( QStringLiteral( "managing_data_source/opening_data.html#creating-a-stored-connection" ) );
116 }
117 
120 {
121  QgsSettings settings;
122  QString baseKey = '/' + cmbDatabaseTypes->currentText() + "/connections/";
123  settings.setValue( baseKey + "selected", txtName->text() );
124 
125  // warn if entry was renamed to an existing connection
126  if ( ( mOriginalConnName.isNull() || mOriginalConnName != txtName->text() ) &&
127  settings.contains( baseKey + txtName->text() + "/host" ) &&
128  QMessageBox::question( this,
129  tr( "Save Connection" ),
130  tr( "Should the existing connection %1 be overwritten?" ).arg( txtName->text() ),
131  QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
132  {
133  return;
134  }
135 
136  // on rename delete original entry first
137  if ( !mOriginalConnName.isNull() && mOriginalConnName != txtName->text() )
138  {
139  settings.remove( baseKey + mOriginalConnName );
140  }
141 
142  baseKey += txtName->text();
143  settings.setValue( baseKey + "/host", txtHost->text() );
144  settings.setValue( baseKey + "/database", txtDatabase->text() );
145  settings.setValue( baseKey + "/port", txtPort->text() );
146  settings.setValue( baseKey + "/username", mAuthSettingsDatabase->storeUsernameIsChecked() ? mAuthSettingsDatabase->username() : QString() );
147  settings.setValue( baseKey + "/password", mAuthSettingsDatabase->storePasswordIsChecked() ? mAuthSettingsDatabase->password() : QString() );
148  settings.setValue( baseKey + "/store_username", mAuthSettingsDatabase->storeUsernameIsChecked() ? "true" : "false" );
149  settings.setValue( baseKey + "/store_password", mAuthSettingsDatabase->storePasswordIsChecked() ? "true" : "false" );
150  settings.setValue( baseKey + "/configid", mAuthSettingsDatabase->configId() );
151 
152  QDialog::accept();
153 }
154 
155 void QgsNewOgrConnection::btnConnect_clicked()
156 {
157  testConnection();
158 }
159 
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:649
void remove(const QString &key, QgsSettings::Section section=QgsSettings::NoSection)
Removes the setting key and any sub-settings of key in a section.
virtual QString databaseDrivers() const
Returns a string containing the available database drivers.
void testConnection()
Tests the connection using the parameters supplied.
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.
Q_DECL_DEPRECATED void showHelp() SIP_DEPRECATED
Show the help.
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:650
QString createDatabaseURI(const QString &connectionType, const QString &host, const QString &database, QString port, const QString &configId, QString username, QString password, bool expandAuthConfig)
CreateDatabaseURI.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
void accept() override
Autoconnected SLOTS *.
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:127
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:36
bool contains(const QString &key, QgsSettings::Section section=QgsSettings::NoSection) const
Returns true if there exists a setting called key; returns false otherwise.