-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
qgspgnewconnection.cpp
227 lines (192 loc) · 10.1 KB
/
qgspgnewconnection.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
/***************************************************************************
qgspgnewconnection.cpp - description
-------------------
begin : Sat Jun 22 2002
copyright : (C) 2002 by Gary E.Sherman
email : sherman at mrcc.com
***************************************************************************/
/***************************************************************************
* *
* 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 <QMessageBox>
#include <QInputDialog>
#include <QRegExpValidator>
#include "qgspgnewconnection.h"
#include "qgsauthmanager.h"
#include "qgsdatasourceuri.h"
#include "qgspostgresconn.h"
#include "qgssettings.h"
#include "qgsgui.h"
QgsPgNewConnection::QgsPgNewConnection( QWidget *parent, const QString &connName, Qt::WindowFlags fl )
: QDialog( parent, fl )
, mOriginalConnName( connName )
{
setupUi( this );
QgsGui::enableAutoGeometryRestore( this );
connect( btnConnect, &QPushButton::clicked, this, &QgsPgNewConnection::btnConnect_clicked );
connect( cb_geometryColumnsOnly, &QCheckBox::clicked, this, &QgsPgNewConnection::cb_geometryColumnsOnly_clicked );
connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsPgNewConnection::showHelp );
cbxSSLmode->addItem( tr( "disable" ), QgsDataSourceUri::SslDisable );
cbxSSLmode->addItem( tr( "allow" ), QgsDataSourceUri::SslAllow );
cbxSSLmode->addItem( tr( "prefer" ), QgsDataSourceUri::SslPrefer );
cbxSSLmode->addItem( tr( "require" ), QgsDataSourceUri::SslRequire );
cbxSSLmode->addItem( tr( "verify-ca" ), QgsDataSourceUri::SslVerifyCa );
cbxSSLmode->addItem( tr( "verify-full" ), QgsDataSourceUri::SslVerifyFull );
mAuthSettings->setDataprovider( QStringLiteral( "postgres" ) );
mAuthSettings->showStoreCheckboxes( true );
if ( !connName.isEmpty() )
{
// populate the dialog with the information stored for the connection
// populate the fields with the stored setting parameters
QgsSettings settings;
QString key = "/PostgreSQL/connections/" + connName;
txtService->setText( settings.value( key + "/service" ).toString() );
txtHost->setText( settings.value( key + "/host" ).toString() );
QString port = settings.value( key + "/port" ).toString();
if ( port.length() == 0 )
{
port = QStringLiteral( "5432" );
}
txtPort->setText( port );
txtDatabase->setText( settings.value( key + "/database" ).toString() );
cb_publicSchemaOnly->setChecked( settings.value( key + "/publicOnly", false ).toBool() );
cb_geometryColumnsOnly->setChecked( settings.value( key + "/geometryColumnsOnly", true ).toBool() );
cb_dontResolveType->setChecked( settings.value( key + "/dontResolveType", false ).toBool() );
cb_allowGeometrylessTables->setChecked( settings.value( key + "/allowGeometrylessTables", false ).toBool() );
// Ensure that cb_publicSchemaOnly is set correctly
cb_geometryColumnsOnly_clicked();
cb_useEstimatedMetadata->setChecked( settings.value( key + "/estimatedMetadata", false ).toBool() );
cb_projectsInDatabase->setChecked( settings.value( key + "/projectsInDatabase", false ).toBool() );
cbxSSLmode->setCurrentIndex( cbxSSLmode->findData( settings.enumValue( key + "/sslmode", QgsDataSourceUri::SslPrefer ) ) );
if ( settings.value( key + "/saveUsername" ).toString() == QLatin1String( "true" ) )
{
mAuthSettings->setUsername( settings.value( key + "/username" ).toString() );
mAuthSettings->setStoreUsernameChecked( true );
}
if ( settings.value( key + "/savePassword" ).toString() == QLatin1String( "true" ) )
{
mAuthSettings->setPassword( settings.value( key + "/password" ).toString() );
mAuthSettings->setStorePasswordChecked( true );
}
// Old save setting
if ( settings.contains( key + "/save" ) )
{
mAuthSettings->setUsername( settings.value( key + "/username" ).toString() );
mAuthSettings->setStoreUsernameChecked( ! mAuthSettings->username().isEmpty() );
if ( settings.value( key + "/save" ).toString() == QLatin1String( "true" ) )
mAuthSettings->setPassword( settings.value( key + "/password" ).toString() );
mAuthSettings->setStorePasswordChecked( true );
}
QString authcfg = settings.value( key + "/authcfg" ).toString();
mAuthSettings->setConfigId( authcfg );
txtName->setText( connName );
}
txtName->setValidator( new QRegExpValidator( QRegExp( "[^\\/]*" ), txtName ) );
}
//! Autoconnected SLOTS *
void QgsPgNewConnection::accept()
{
QgsSettings settings;
QString baseKey = QStringLiteral( "/PostgreSQL/connections/" );
settings.setValue( baseKey + "selected", txtName->text() );
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;
}
// warn if entry was renamed to an existing connection
if ( ( mOriginalConnName.isNull() || mOriginalConnName.compare( txtName->text(), Qt::CaseInsensitive ) != 0 ) &&
( settings.contains( baseKey + txtName->text() + "/service" ) ||
settings.contains( baseKey + txtName->text() + "/host" ) ) &&
QMessageBox::question( this,
tr( "Save Connection" ),
tr( "Should the existing connection %1 be overwritten?" ).arg( txtName->text() ),
QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
{
return;
}
// on rename delete the original entry first
if ( !mOriginalConnName.isNull() && mOriginalConnName != txtName->text() )
{
settings.remove( baseKey + mOriginalConnName );
settings.sync();
}
baseKey += txtName->text();
settings.setValue( baseKey + "/service", txtService->text() );
settings.setValue( baseKey + "/host", txtHost->text() );
settings.setValue( baseKey + "/port", txtPort->text() );
settings.setValue( baseKey + "/database", txtDatabase->text() );
settings.setValue( baseKey + "/username", mAuthSettings->storeUsernameIsChecked( ) ? mAuthSettings->username() : QString() );
settings.setValue( baseKey + "/password", mAuthSettings->storePasswordIsChecked( ) && !hasAuthConfigID ? mAuthSettings->password() : QString() );
settings.setValue( baseKey + "/authcfg", mAuthSettings->configId() );
settings.setValue( baseKey + "/publicOnly", cb_publicSchemaOnly->isChecked() );
settings.setValue( baseKey + "/geometryColumnsOnly", cb_geometryColumnsOnly->isChecked() );
settings.setValue( baseKey + "/dontResolveType", cb_dontResolveType->isChecked() );
settings.setValue( baseKey + "/allowGeometrylessTables", cb_allowGeometrylessTables->isChecked() );
settings.setValue( baseKey + "/sslmode", cbxSSLmode->currentData().toInt() );
settings.setValue( baseKey + "/saveUsername", mAuthSettings->storeUsernameIsChecked( ) ? "true" : "false" );
settings.setValue( baseKey + "/savePassword", mAuthSettings->storePasswordIsChecked( ) && !hasAuthConfigID ? "true" : "false" );
settings.setValue( baseKey + "/estimatedMetadata", cb_useEstimatedMetadata->isChecked() );
settings.setValue( baseKey + "/projectsInDatabase", cb_projectsInDatabase->isChecked() );
// remove old save setting
settings.remove( baseKey + "/save" );
QDialog::accept();
}
void QgsPgNewConnection::btnConnect_clicked()
{
testConnection();
}
void QgsPgNewConnection::cb_geometryColumnsOnly_clicked()
{
if ( cb_geometryColumnsOnly->checkState() == Qt::Checked )
cb_publicSchemaOnly->setEnabled( false );
else
cb_publicSchemaOnly->setEnabled( true );
}
//! End Autoconnected SLOTS *
void QgsPgNewConnection::testConnection()
{
QgsTemporaryCursorOverride cursorOverride( Qt::WaitCursor );
QgsDataSourceUri uri;
if ( !txtService->text().isEmpty() )
{
uri.setConnection( txtService->text(), txtDatabase->text(),
mAuthSettings->username(), mAuthSettings->password(),
( QgsDataSourceUri::SslMode ) cbxSSLmode->currentData().toInt(),
mAuthSettings->configId() );
}
else
{
uri.setConnection( txtHost->text(), txtPort->text(), txtDatabase->text(),
mAuthSettings->username(), mAuthSettings->password(),
( QgsDataSourceUri::SslMode ) cbxSSLmode->currentData().toInt(),
mAuthSettings->configId() );
}
QgsPostgresConn *conn = QgsPostgresConn::connectDb( uri.connectionInfo( false ), true );
if ( conn )
{
// Database successfully opened; we can now issue SQL commands.
bar->pushMessage( tr( "Connection to %1 was successful." ).arg( txtName->text() ),
Qgis::Info );
// free pg connection resources
conn->unref();
}
else
{
bar->pushMessage( tr( "Connection failed - consult message log for details." ),
Qgis::Warning );
}
}
void QgsPgNewConnection::showHelp()
{
QgsHelp::openHelp( QStringLiteral( "managing_data_source/opening_data.html#creating-a-stored-connection" ) );
}