1
/***************************************************************************
2
qgspgnewconnection.cpp - description
4
begin : Sat Jun 22 2002
5
copyright : (C) 2002 by Gary E.Sherman
6
email : sherman at mrcc.com
7
***************************************************************************/
9
/***************************************************************************
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. *
16
***************************************************************************/
20
#include <QMessageBox>
21
#include <QInputDialog>
23
#include "qgspgnewconnection.h"
24
#include "qgscontexthelp.h"
25
#include "qgsdatasourceuri.h"
26
#include "qgslogger.h"
27
#include "qgscredentialdialog.h"
34
QgsPgNewConnection::QgsPgNewConnection( QWidget *parent, const QString& connName, Qt::WFlags fl )
35
: QDialog( parent, fl ), mOriginalConnName( connName )
39
cbxSSLmode->addItem( tr( "disable" ), QgsDataSourceURI::SSLdisable );
40
cbxSSLmode->addItem( tr( "allow" ), QgsDataSourceURI::SSLallow );
41
cbxSSLmode->addItem( tr( "prefer" ), QgsDataSourceURI::SSLprefer );
42
cbxSSLmode->addItem( tr( "require" ), QgsDataSourceURI::SSLrequire );
44
if ( !connName.isEmpty() )
46
// populate the dialog with the information stored for the connection
47
// populate the fields with the stored setting parameters
50
QString key = "/PostgreSQL/connections/" + connName;
51
txtService->setText( settings.value( key + "/service" ).toString() );
52
txtHost->setText( settings.value( key + "/host" ).toString() );
53
QString port = settings.value( key + "/port" ).toString();
54
if ( port.length() == 0 )
58
txtPort->setText( port );
59
txtDatabase->setText( settings.value( key + "/database" ).toString() );
60
cb_publicSchemaOnly->setChecked( settings.value( key + "/publicOnly", false ).toBool() );
61
cb_geometryColumnsOnly->setChecked( settings.value( key + "/geometrycolumnsOnly", false ).toBool() );
62
cb_allowGeometrylessTables->setChecked( settings.value( key + "/allowGeometrylessTables", false ).toBool() );
63
// Ensure that cb_publicSchemaOnly is set correctly
64
on_cb_geometryColumnsOnly_clicked();
66
cb_useEstimatedMetadata->setChecked( settings.value( key + "/estimatedMetadata", false ).toBool() );
68
cbxSSLmode->setCurrentIndex( cbxSSLmode->findData( settings.value( key + "/sslmode", QgsDataSourceURI::SSLprefer ).toInt() ) );
70
if ( settings.value( key + "/saveUsername" ).toString() == "true" )
72
txtUsername->setText( settings.value( key + "/username" ).toString() );
73
chkStoreUsername->setChecked( true );
76
if ( settings.value( key + "/savePassword" ).toString() == "true" )
78
txtPassword->setText( settings.value( key + "/password" ).toString() );
79
chkStorePassword->setChecked( true );
83
if ( settings.contains( key + "/save" ) )
85
txtUsername->setText( settings.value( key + "/username" ).toString() );
86
chkStoreUsername->setChecked( !txtUsername->text().isEmpty() );
88
if ( settings.value( key + "/save" ).toString() == "true" )
89
txtPassword->setText( settings.value( key + "/password" ).toString() );
91
chkStorePassword->setChecked( true );
94
txtName->setText( connName );
97
/** Autoconnected SLOTS **/
98
void QgsPgNewConnection::accept()
101
QString baseKey = "/PostgreSQL/connections/";
102
settings.setValue( baseKey + "selected", txtName->text() );
104
// warn if entry was renamed to an existing connection
105
if (( mOriginalConnName.isNull() || mOriginalConnName != txtName->text() ) &&
106
( settings.contains( baseKey + txtName->text() + "/service" ) ||
107
settings.contains( baseKey + txtName->text() + "/host" ) ) &&
108
QMessageBox::question( this,
109
tr( "Save connection" ),
110
tr( "Should the existing connection %1 be overwritten?" ).arg( txtName->text() ),
111
QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
116
// on rename delete the original entry first
117
if ( !mOriginalConnName.isNull() && mOriginalConnName != txtName->text() )
120
settings.remove( baseKey + mOriginalConnName );
123
baseKey += txtName->text();
124
settings.setValue( baseKey + "/service", txtService->text() );
125
settings.setValue( baseKey + "/host", txtHost->text() );
126
settings.setValue( baseKey + "/port", txtPort->text() );
127
settings.setValue( baseKey + "/database", txtDatabase->text() );
128
settings.setValue( baseKey + "/username", chkStoreUsername->isChecked() ? txtUsername->text() : "" );
129
settings.setValue( baseKey + "/password", chkStorePassword->isChecked() ? txtPassword->text() : "" );
130
settings.setValue( baseKey + "/publicOnly", cb_publicSchemaOnly->isChecked() );
131
settings.setValue( baseKey + "/geometryColumnsOnly", cb_geometryColumnsOnly->isChecked() );
132
settings.setValue( baseKey + "/allowGeometrylessTables", cb_allowGeometrylessTables->isChecked() );
133
settings.setValue( baseKey + "/sslmode", cbxSSLmode->itemData( cbxSSLmode->currentIndex() ).toInt() );
134
settings.setValue( baseKey + "/saveUsername", chkStoreUsername->isChecked() ? "true" : "false" );
135
settings.setValue( baseKey + "/savePassword", chkStorePassword->isChecked() ? "true" : "false" );
136
settings.setValue( baseKey + "/estimatedMetadata", cb_useEstimatedMetadata->isChecked() );
138
// remove old save setting
139
settings.remove( baseKey + "/save" );
144
void QgsPgNewConnection::on_btnConnect_clicked()
149
void QgsPgNewConnection::on_cb_geometryColumnsOnly_clicked()
151
if ( cb_geometryColumnsOnly->checkState() == Qt::Checked )
152
cb_publicSchemaOnly->setEnabled( false );
154
cb_publicSchemaOnly->setEnabled( true );
157
/** end Autoconnected SLOTS **/
159
QgsPgNewConnection::~QgsPgNewConnection()
163
void QgsPgNewConnection::testConnection()
165
QgsDataSourceURI uri;
166
if ( !txtService->text().isEmpty() )
168
uri.setConnection( txtService->text(), txtDatabase->text(),
169
txtUsername->text(), txtPassword->text(),
170
( QgsDataSourceURI::SSLmode ) cbxSSLmode->itemData( cbxSSLmode->currentIndex() ).toInt() );
174
uri.setConnection( txtHost->text(), txtPort->text(), txtDatabase->text(),
175
txtUsername->text(), txtPassword->text(),
176
( QgsDataSourceURI::SSLmode ) cbxSSLmode->itemData( cbxSSLmode->currentIndex() ).toInt() );
178
QString conninfo = uri.connectionInfo();
179
QgsDebugMsg( "PQconnectdb(\"" + conninfo + "\");" );
181
PGconn *pd = PQconnectdb( conninfo.toLocal8Bit() ); // use what is set based on locale; after connecting, use Utf8
182
// check the connection status
183
if ( PQstatus( pd ) != CONNECTION_OK )
185
QString username = txtUsername->text();
186
QString password = txtPassword->text();
188
uri.setUsername( "" );
189
uri.setPassword( "" );
191
while ( PQstatus( pd ) != CONNECTION_OK )
193
bool ok = QgsCredentials::instance()->get( conninfo, username, password, QString::fromUtf8( PQerrorMessage( pd ) ) );
199
QgsDataSourceURI uri( conninfo );
201
if ( !username.isEmpty() )
202
uri.setUsername( username );
204
if ( !password.isEmpty() )
205
uri.setPassword( password );
207
QgsDebugMsg( "PQconnectdb(\"" + uri.connectionInfo() + "\");" );
208
pd = PQconnectdb( uri.connectionInfo().toLocal8Bit() );
211
if ( PQstatus( pd ) == CONNECTION_OK )
213
if ( chkStoreUsername->isChecked() )
214
txtUsername->setText( username );
215
if ( chkStorePassword->isChecked() )
216
txtPassword->setText( password );
218
QgsCredentials::instance()->put( conninfo, username, password );
222
if ( PQstatus( pd ) == CONNECTION_OK )
224
// Database successfully opened; we can now issue SQL commands.
225
QMessageBox::information( this,
226
tr( "Test connection" ),
227
tr( "Connection to %1 was successful" ).arg( txtDatabase->text() ) );
231
QMessageBox::information( this,
232
tr( "Test connection" ),
233
tr( "Connection failed - Check settings and try again.\n\nExtended error information:\n%1" )
234
.arg( QString::fromUtf8( PQerrorMessage( pd ) ) ) );
236
// free pg connection resources