QGIS API Documentation  3.6.0-Noosa (5873452)
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 
27 #include <ogr_api.h>
28 #include <cpl_error.h>
29 #include "qgshelp.h"
30 
31 QgsNewOgrConnection::QgsNewOgrConnection( QWidget *parent, const QString &connType, const QString &connName, Qt::WindowFlags fl )
32  : QDialog( parent, fl )
33  , mOriginalConnName( connName )
34 {
35  setupUi( this );
36  connect( btnConnect, &QPushButton::clicked, this, &QgsNewOgrConnection::btnConnect_clicked );
38  connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsNewOgrConnection::showHelp );
40 
41  QgsSettings settings;
42  restoreGeometry( settings.value( QStringLiteral( "Windows/OGRDatabaseConnection/geometry" ) ).toByteArray() );
43 
44  //add database drivers
45  QStringList dbDrivers = QgsProviderRegistry::instance()->databaseDrivers().split( ';' );
46  for ( int i = 0; i < dbDrivers.count(); i++ )
47  {
48  QString dbDrive = dbDrivers.at( i );
49  cmbDatabaseTypes->addItem( dbDrive.split( ',' ).at( 0 ) );
50  }
51  txtName->setEnabled( true );
52  cmbDatabaseTypes->setEnabled( true );
53  if ( !connName.isEmpty() )
54  {
55  // populate the dialog with the information stored for the connection
56  // populate the fields with the stored setting parameters
57  QString key = '/' + connType + "/connections/" + connName;
58  txtHost->setText( settings.value( key + "/host" ).toString() );
59  txtDatabase->setText( settings.value( key + "/database" ).toString() );
60  QString port = settings.value( key + "/port" ).toString();
61  txtPort->setText( port );
62  if ( settings.value( key + "/store_username" ).toString() == QLatin1String( "true" ) )
63  {
64  mAuthSettingsDatabase->setUsername( settings.value( key + "/username" ).toString() );
65  mAuthSettingsDatabase->setStoreUsernameChecked( true );
66  }
67  if ( settings.value( key + "/store_password" ).toString() == QLatin1String( "true" ) )
68  {
69  mAuthSettingsDatabase->setPassword( settings.value( key + "/password" ).toString() );
70  mAuthSettingsDatabase->setStorePasswordChecked( true );
71  }
72  mAuthSettingsDatabase->setConfigId( settings.value( key + "/configid" ).toString() );
73  cmbDatabaseTypes->setCurrentIndex( cmbDatabaseTypes->findText( connType ) );
74  txtName->setText( connName );
75  txtName->setEnabled( false );
76  cmbDatabaseTypes->setEnabled( false );
77  }
78  txtName->setValidator( new QRegExpValidator( QRegExp( "[^\\/]+" ), txtName ) );
79  mAuthSettingsDatabase->setDataprovider( QStringLiteral( "ogr" ) );
80  mAuthSettingsDatabase->showStoreCheckboxes( true );
81 }
82 
84 {
85  QgsSettings settings;
86  settings.setValue( QStringLiteral( "Windows/OGRDatabaseConnection/geometry" ), saveGeometry() );
87 }
88 
90 {
91  QString uri;
92  uri = createDatabaseURI( cmbDatabaseTypes->currentText(),
93  txtHost->text(),
94  txtDatabase->text(),
95  txtPort->text(),
96  mAuthSettingsDatabase->configId(),
97  mAuthSettingsDatabase->username(),
98  mAuthSettingsDatabase->password(),
99  true );
100  QgsDebugMsg( "Connecting using uri = " + uri );
101  OGRRegisterAll();
102  OGRDataSourceH poDS;
103  OGRSFDriverH pahDriver;
104  CPLErrorReset();
105  poDS = OGROpen( uri.toUtf8().constData(), false, &pahDriver );
106  if ( !poDS )
107  {
108  QMessageBox::information( this, tr( "Test Connection" ), tr( "Connection failed - Check settings and try again.\n\nExtended error information:\n%1" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
109  }
110  else
111  {
112  QMessageBox::information( this, tr( "Test Connection" ), tr( "Connection to %1 was successful." ).arg( uri ) );
113  OGRReleaseDataSource( poDS );
114  }
115 }
116 
118 {
119  QgsHelp::openHelp( QStringLiteral( "managing_data_source/opening_data.html#creating-a-stored-connection" ) );
120 }
121 
124 {
125  QgsSettings settings;
126  QString baseKey = '/' + cmbDatabaseTypes->currentText() + "/connections/";
127  settings.setValue( baseKey + "selected", txtName->text() );
128 
129  // warn if entry was renamed to an existing connection
130  if ( ( mOriginalConnName.isNull() || mOriginalConnName != txtName->text() ) &&
131  settings.contains( baseKey + txtName->text() + "/host" ) &&
132  QMessageBox::question( this,
133  tr( "Save Connection" ),
134  tr( "Should the existing connection %1 be overwritten?" ).arg( txtName->text() ),
135  QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
136  {
137  return;
138  }
139 
140  // on rename delete original entry first
141  if ( !mOriginalConnName.isNull() && mOriginalConnName != txtName->text() )
142  {
143  settings.remove( baseKey + mOriginalConnName );
144  }
145 
146  baseKey += txtName->text();
147  settings.setValue( baseKey + "/host", txtHost->text() );
148  settings.setValue( baseKey + "/database", txtDatabase->text() );
149  settings.setValue( baseKey + "/port", txtPort->text() );
150  settings.setValue( baseKey + "/username", mAuthSettingsDatabase->storeUsernameIsChecked() ? mAuthSettingsDatabase->username() : QString() );
151  settings.setValue( baseKey + "/password", mAuthSettingsDatabase->storePasswordIsChecked() ? mAuthSettingsDatabase->password() : QString() );
152  settings.setValue( baseKey + "/store_username", mAuthSettingsDatabase->storeUsernameIsChecked() ? "true" : "false" );
153  settings.setValue( baseKey + "/store_password", mAuthSettingsDatabase->storePasswordIsChecked() ? "true" : "false" );
154  settings.setValue( baseKey + "/configid", mAuthSettingsDatabase->configId() );
155 
156  QDialog::accept();
157 }
158 
159 void QgsNewOgrConnection::btnConnect_clicked()
160 {
161  testConnection();
162 }
163 
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:624
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.
void saveGeometry(QWidget *widget, const QString &keyName)
Save the wigget geometry into settings.
bool restoreGeometry(QWidget *widget, const QString &keyName)
Restore the wigget geometry from settings.
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:625
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 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.