-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
qgshananewconnection.cpp
383 lines (336 loc) · 14.3 KB
/
qgshananewconnection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/***************************************************************************
qgshananewconnection.cpp
--------------------------------------
Date : 31-05-2019
Copyright : (C) SAP SE
Author : Maxim Rylov
***************************************************************************/
/***************************************************************************
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
***************************************************************************/
#include "qgsauthmanager.h"
#include "qgshanadriver.h"
#include "qgshanaconnection.h"
#include "qgshananewconnection.h"
#include "qgshanasettings.h"
#include "qgssettings.h"
#include <QFileInfo>
#include <QInputDialog>
#include <QMessageBox>
using namespace std;
namespace
{
bool isStringEmpty( const QString &input )
{
return ( input.isEmpty() || QString( input ).replace( ' ', QString() ).isEmpty() );
}
}
QgsHanaNewConnection::QgsHanaNewConnection(
QWidget *parent,
const QString &connName,
Qt::WindowFlags fl )
: QDialog( parent, fl )
, mOriginalConnName( connName )
{
setupUi( this );
cmbIdentifierType_changed( cmbIdentifierType->currentIndex() );
connect( cmbIdentifierType, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsHanaNewConnection::cmbIdentifierType_changed );
connect( rbtnSingleContainer, &QRadioButton::clicked, this, &QgsHanaNewConnection::rbtnSingleContainer_clicked );
connect( rbtnMultipleContainers, &QRadioButton::clicked, this, &QgsHanaNewConnection::rbtnMultipleContainers_clicked );
connect( rbtnTenantDatabase, &QRadioButton::clicked, this, &QgsHanaNewConnection::rbtnTenantDatabase_clicked );
connect( rbtnSystemDatabase, &QRadioButton::clicked, this, &QgsHanaNewConnection::rbtnSystemDatabase_clicked );
connect( chkEnableSSL, &QCheckBox::clicked, this, &QgsHanaNewConnection::chkEnableSSL_clicked );
connect( chkValidateCertificate, &QCheckBox::clicked, this, &QgsHanaNewConnection::chkValidateCertificate_clicked );
connect( btnConnect, &QPushButton::clicked, this, &QgsHanaNewConnection::btnConnect_clicked );
connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsHanaNewConnection::showHelp );
txtDriver->setText( QgsHanaDriver::instance()->driver() );
#if defined(Q_OS_WIN)
txtDriver->setToolTip( tr( "The name of the HANA ODBC driver.\r\n\r\n"
"The HANA ODBC driver is a part of the SAP HANA Client,\r\n"
"which can be found at https://tools.hana.ondemand.com/#hanatools." ) );
#else
txtDriver->setToolTip( tr( "The name or path to the HANA ODBC driver.\r\n\r\n"
"If the driver is registered in odbcinst.ini, enter the driver's name.\r\n"
"Otherwise, enter the path to the driver (libodbcHDB.so).\r\n\r\n"
"The HANA ODBC driver is a part of the SAP HANA Client,\r\n"
"which can be found at https://tools.hana.ondemand.com/#hanatools." ) );
#endif
cbxCryptoProvider->addItem( QStringLiteral( "openssl" ), QStringLiteral( "openssl" ) );
cbxCryptoProvider->addItem( QStringLiteral( "commoncrypto" ), QStringLiteral( "commoncrypto" ) );
cbxCryptoProvider->addItem( QStringLiteral( "sapcrypto" ), QStringLiteral( "sapcrypto" ) );
cbxCryptoProvider->addItem( QStringLiteral( "mscrypto" ), QStringLiteral( "mscrypto" ) );
mAuthSettings->setDataprovider( QStringLiteral( "hana" ) );
mAuthSettings->showStoreCheckboxes( true );
if ( connName.isEmpty() )
{
rbtnSingleContainer->setChecked( true );
frmMultitenantSettings->setEnabled( false );
}
else
{
QgsHanaSettings settings( connName, true );
updateControlsFromSettings( settings );
}
txtName->setValidator( new QRegExpValidator( QRegExp( "[^\\/]*" ), txtName ) );
chkEnableSSL_clicked();
}
void QgsHanaNewConnection::accept()
{
if ( isStringEmpty( txtName->text() ) )
{
QMessageBox::warning( this,
tr( "Save Connection" ), tr( "Connection name cannot be empty." ), QMessageBox::Ok );
return;
}
if ( isStringEmpty( txtDriver->text() ) )
{
QMessageBox::warning( this,
tr( "Save Connection" ), tr( "Driver field cannot be empty." ), QMessageBox::Ok );
return;
}
if ( isStringEmpty( txtHost->text() ) )
{
QMessageBox::warning( this,
tr( "Save Connection" ), tr( "Host field cannot be empty." ), QMessageBox::Ok );
return;
}
if ( rbtnMultipleContainers->isChecked() && rbtnTenantDatabase->isChecked() )
{
if ( isStringEmpty( txtTenantDatabaseName->text() ) )
{
QMessageBox::warning( this,
tr( "Save Connection" ), tr( "Tenant database name cannot be empty." ), QMessageBox::Ok );
return;
}
}
QString connName = txtName->text();
QgsHanaSettings::setSelectedConnection( connName );
bool hasAuthConfigID = !mAuthSettings->configId().isEmpty();
if ( !hasAuthConfigID && mAuthSettings->storePasswordIsChecked() &&
QMessageBox::question( this,
tr( "Saving Passwords" ),
tr( "WARNING: You have opted to save your password. It will be stored in unsecured "
"plain text in your project files and in your home directory (Unix-like OS) or user profile (Windows). "
"If you want to avoid this, press Cancel and either:\n\na) Don't save a password in the connection "
"settings — it will be requested interactively when needed;\nb) Use the Configuration tab to add your "
"credentials in an HTTP Basic Authentication method and store them in an encrypted database." ),
QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
{
return;
}
QgsHanaSettings settings( connName, true );
// warn if entry was renamed to an existing connection
if ( ( !mOriginalConnName.isNull() && mOriginalConnName.compare( connName, Qt::CaseInsensitive ) != 0 ) &&
QMessageBox::question( this, tr( "Save Connection" ), tr( "Should the existing connection %1 be overwritten?" ).arg( connName ),
QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
{
return;
}
// on rename delete the original entry first
if ( !mOriginalConnName.isNull() && mOriginalConnName != connName )
QgsHanaSettings::removeConnection( mOriginalConnName );
readSettingsFromControls( settings );
if ( !mAuthSettings->storeUsernameIsChecked() )
settings.setUserName( QString( ) );
if ( !( mAuthSettings->storePasswordIsChecked() && !hasAuthConfigID ) )
settings.setPassword( QString( ) );
settings.setSaveUserName( mAuthSettings->storeUsernameIsChecked() );
settings.setSavePassword( mAuthSettings->storePasswordIsChecked() && !hasAuthConfigID );
settings.save();
QDialog::accept();
}
void QgsHanaNewConnection::btnConnect_clicked()
{
testConnection();
}
void QgsHanaNewConnection::cmbIdentifierType_changed( int index )
{
if ( QgsHanaIdentifierType::fromInt( static_cast<uint>( index ) ) == QgsHanaIdentifierType::INSTANCE_NUMBER )
{
txtIdentifier->setMaxLength( 2 );
txtIdentifier->setValidator( new QIntValidator( 0, 99, this ) );
txtIdentifier->setText( QStringLiteral( "00" ) );
}
else
{
txtIdentifier->setMaxLength( 5 );
txtIdentifier->setValidator( new QIntValidator( 1, 65535, this ) );
txtIdentifier->setText( QStringLiteral( "00000" ) );
}
}
void QgsHanaNewConnection::rbtnSingleContainer_clicked()
{
frmMultitenantSettings->setEnabled( false );
}
void QgsHanaNewConnection::rbtnMultipleContainers_clicked()
{
frmMultitenantSettings->setEnabled( true );
}
void QgsHanaNewConnection::rbtnTenantDatabase_clicked()
{
lblTenantDatabaseName->setEnabled( true );
txtTenantDatabaseName->setEnabled( true );
}
void QgsHanaNewConnection::rbtnSystemDatabase_clicked()
{
lblTenantDatabaseName->setEnabled( false );
txtTenantDatabaseName->setEnabled( false );
}
void QgsHanaNewConnection::chkEnableSSL_clicked()
{
bool enabled = chkEnableSSL->isChecked();
lblCryptoProvider->setEnabled( enabled );
cbxCryptoProvider->setEnabled( enabled );
chkValidateCertificate->setEnabled( enabled );
lblOverrideHostName->setEnabled( enabled );
txtOverrideHostName->setEnabled( enabled && chkValidateCertificate->isChecked() );
lblKeyStore->setEnabled( enabled );
txtKeyStore->setEnabled( enabled );
lblTrustStore->setEnabled( enabled );
txtTrustStore->setEnabled( enabled );
}
void QgsHanaNewConnection::chkValidateCertificate_clicked()
{
txtOverrideHostName->setEnabled( chkValidateCertificate->isChecked() );
}
void QgsHanaNewConnection::readSettingsFromControls( QgsHanaSettings &settings )
{
settings.setDriver( txtDriver->text() );
settings.setHost( txtHost->text() );
settings.setIdentifierType( static_cast<uint>( cmbIdentifierType->currentIndex() ) );
settings.setIdentifier( txtIdentifier->text() );
settings.setDatabase( getDatabaseName() );
settings.setMultitenant( rbtnMultipleContainers->isChecked() );
settings.setSchema( txtSchema->text() );
settings.setAuthCfg( mAuthSettings->configId() );
settings.setUserName( mAuthSettings->username() );
settings.setPassword( mAuthSettings->password() );
settings.setSaveUserName( mAuthSettings->storeUsernameIsChecked() );
settings.setSavePassword( mAuthSettings->storePasswordIsChecked() );
settings.setUserTablesOnly( chkUserTablesOnly->isChecked() );
settings.setAllowGeometrylessTables( chkAllowGeometrylessTables->isChecked() );
settings.setEnableSsl( chkEnableSSL->isChecked() );
settings.setSslCryptoProvider( cbxCryptoProvider->currentData().toString() );
settings.setSslKeyStore( txtKeyStore->text() );
settings.setSslTrustStore( txtTrustStore->text() );
settings.setSslValidateCertificate( chkValidateCertificate->isChecked() );
settings.setSslHostNameInCertificate( txtOverrideHostName->text() );
}
void QgsHanaNewConnection::updateControlsFromSettings( const QgsHanaSettings &settings )
{
txtDriver->setText( settings.driver() );
txtHost->setText( settings.host() );
cmbIdentifierType->setCurrentIndex( QgsHanaIdentifierType::INSTANCE_NUMBER );
cmbIdentifierType->setCurrentIndex( static_cast<int>( settings.identifierType() ) );
txtIdentifier->setText( settings.identifier() );
if ( !settings.multitenant() )
{
rbtnSingleContainer->setChecked( true );
frmMultitenantSettings->setEnabled( false );
}
else
{
rbtnMultipleContainers->setChecked( true );
if ( settings.database() == QLatin1String( "SYSTEMDB" ) )
rbtnSystemDatabase->setChecked( true );
else
txtTenantDatabaseName->setText( settings.database() );
}
txtSchema->setText( settings.schema() );
chkUserTablesOnly->setChecked( settings.userTablesOnly() );
chkAllowGeometrylessTables->setChecked( settings.allowGeometrylessTables() );
chkEnableSSL->setChecked( settings.enableSsl() );
int idx = cbxCryptoProvider->findData( settings.sslCryptoProvider() );
if ( idx >= 0 )
cbxCryptoProvider->setCurrentIndex( idx );
chkValidateCertificate->setChecked( settings.sslValidateCertificate() );
txtOverrideHostName->setText( settings.sslHostNameInCertificate() );
txtKeyStore->setText( settings.sslKeyStore() );
txtTrustStore->setText( settings.sslTrustStore() );
if ( settings.saveUserName() )
{
mAuthSettings->setUsername( settings.userName() );
mAuthSettings->setStoreUsernameChecked( true );
}
if ( settings.savePassword() )
{
mAuthSettings->setPassword( settings.password() );
mAuthSettings->setStorePasswordChecked( true );
}
mAuthSettings->setConfigId( settings.authCfg() );
txtName->setText( settings.name() );
}
void QgsHanaNewConnection::testConnection()
{
QString warningMsg;
if ( txtHost->text().isEmpty() )
warningMsg = tr( "Host name has not been specified." );
else if ( rbtnMultipleContainers->isChecked() && rbtnTenantDatabase->isChecked() &&
txtTenantDatabaseName->text().isEmpty() )
warningMsg = tr( "Database has not been specified." );
else if ( mAuthSettings->username().isEmpty() )
warningMsg = tr( "User name has not been specified." );
else if ( mAuthSettings->password().isEmpty() )
warningMsg = tr( "Password has not been specified." );
else if ( txtIdentifier->text().isEmpty() )
warningMsg = tr( "Identifier has not been specified." );
else
{
QString driver = txtDriver->text();
if ( driver.isEmpty() )
warningMsg = tr( "Driver name/path has not been specified." );
else
{
if ( !QgsHanaDriver::isInstalled( driver ) )
{
#if defined(Q_OS_WIN)
warningMsg = tr( "Driver with name '%1' is not installed." ).arg( driver );
#else
if ( !QgsHanaDriver::isValidPath( driver ) )
{
if ( QFileInfo::exists( driver ) )
warningMsg = tr( "Specified driver '%1' cannot be used to connect to HANA." ).arg( driver );
else
warningMsg = tr( "Driver with name/path '%1' was not found." ).arg( driver );
}
#endif
}
}
}
if ( !warningMsg.isEmpty() )
{
bar->clearWidgets();
bar->pushWarning( tr( "Connection failed" ), warningMsg );
return;
}
QgsTemporaryCursorOverride cursorOverride( Qt::WaitCursor );
QgsHanaSettings settings( txtName->text() );
readSettingsFromControls( settings );
QString errorMsg;
unique_ptr<QgsHanaConnection> conn( QgsHanaConnection::createConnection( settings.toDataSourceUri(), nullptr, &errorMsg ) );
if ( conn )
bar->pushMessage( tr( "Connection to the server was successful." ), Qgis::Info );
else
bar->pushMessage( tr( "Connection failed: %1." ).arg( errorMsg ), Qgis::Warning );
}
QString QgsHanaNewConnection::getDatabaseName() const
{
if ( rbtnMultipleContainers->isChecked() )
{
if ( rbtnTenantDatabase->isChecked() )
return QString( txtTenantDatabaseName->text() );
else
return QStringLiteral( "SYSTEMDB" );
}
else
return QString( );
}
void QgsHanaNewConnection::showHelp()
{
QgsHelp::openHelp( QStringLiteral( "managing_data_source/opening_data.html#creating-a-stored-connection" ) );
}