Skip to content

Commit 80c4825

Browse files
author
jef
committedDec 28, 2009

File tree

3 files changed

+80
-29
lines changed

3 files changed

+80
-29
lines changed
 

‎src/app/qgsnewconnection.cpp

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <QSettings>
2020
#include <QMessageBox>
21+
#include <QInputDialog>
2122

2223
#include "qgsnewconnection.h"
2324
#include "qgscontexthelp.h"
@@ -139,10 +140,36 @@ void QgsNewConnection::testConnection()
139140
QgsDataSourceURI uri;
140141
uri.setConnection( txtHost->text(), txtPort->text(), txtDatabase->text(), txtUsername->text(), txtPassword->text(), ( QgsDataSourceURI::SSLmode ) cbxSSLmode->itemData( cbxSSLmode->currentIndex() ).toInt() );
141142

142-
QgsDebugMsg( "PQconnectdb(" + uri.connectionInfo() + ");" );
143+
QgsDebugMsg( "PQconnectdb(\"" + uri.connectionInfo() + "\");" );
143144

144-
PGconn *pd = PQconnectdb( uri.connectionInfo().toLocal8Bit().data() );
145-
if ( PQstatus( pd ) == CONNECTION_OK )
145+
PGconn *pd = PQconnectdb( uri.connectionInfo().toLocal8Bit() ); // use what is set based on locale; after connecting, use Utf8
146+
// check the connection status
147+
if ( PQstatus( pd ) != CONNECTION_OK && QString::fromUtf8( PQerrorMessage( pd ) ) == PQnoPasswordSupplied )
148+
{
149+
QString password = QString::null;
150+
151+
while( PQstatus( pd ) != CONNECTION_OK )
152+
{
153+
bool ok = true;
154+
password = QInputDialog::getText( this,
155+
tr( "Enter password" ),
156+
tr( "Error: %1Enter password for %2")
157+
.arg( QString::fromUtf8( PQerrorMessage( pd ) ) )
158+
.arg( uri.connectionInfo() ),
159+
QLineEdit::Password,
160+
password,
161+
&ok );
162+
163+
::PQfinish( pd );
164+
165+
if( !ok )
166+
break;
167+
168+
pd = PQconnectdb( QString( "%1 password='%2'" ).arg( uri.connectionInfo() ).arg( password ).toLocal8Bit() );
169+
}
170+
}
171+
172+
if ( PQstatus( pd ) == CONNECTION_OK )
146173
{
147174
// Database successfully opened; we can now issue SQL commands.
148175
QMessageBox::information( this, tr( "Test connection" ), tr( "Connection to %1 was successful" ).arg( txtDatabase->text() ) );

‎src/app/qgspgsourceselect.cpp

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -406,34 +406,30 @@ void QgsPgSourceSelect::on_btnConnect_clicked()
406406
if ( pd != 0 )
407407
PQfinish( pd );
408408

409-
pd = PQconnectdb( m_connectionInfo.toLocal8Bit() ); // use what is set based on locale; after connecting, use Utf8
410-
411-
#if defined(PG_VERSION_NUM) && PG_VERSION_NUM >= 80300
412-
// if the connection needs a password ask for one
413-
if ( PQstatus( pd ) == CONNECTION_BAD && PQconnectionNeedsPassword( pd ) )
414-
#else
415-
// if the connection failed and we didn't have a password, ask for one and retry
416-
if ( PQstatus( pd ) == CONNECTION_BAD && password.isEmpty() )
417-
#endif
409+
pd = PQconnectdb( uri.connectionInfo().toLocal8Bit() ); // use what is set based on locale; after connecting, use Utf8
410+
// check the connection status
411+
if ( PQstatus( pd ) != CONNECTION_OK && QString::fromUtf8( PQerrorMessage( pd ) ) == PQnoPasswordSupplied )
418412
{
419-
// get password from user
420-
bool makeConnection = false;
421-
password = QInputDialog::getText( this, tr( "Password for " ) + username,
422-
tr( "Please enter your password:" ),
423-
QLineEdit::Password, QString::null, &makeConnection );
424-
// allow null password entry in case its valid for the database
425-
if ( makeConnection )
413+
QString password = QString::null;
414+
415+
while( PQstatus( pd ) != CONNECTION_OK )
426416
{
427-
uri.setConnection( settings.value( key + "/host" ).toString(),
428-
settings.value( key + "/port" ).toString(),
429-
database,
430-
settings.value( key + "/username" ).toString(),
431-
password,
432-
( QgsDataSourceURI::SSLmode ) settings.value( key + "/sslmode", QgsDataSourceURI::SSLprefer ).toInt() );
433-
434-
m_connectionInfo = uri.connectionInfo();
435-
PQfinish( pd );
436-
pd = PQconnectdb( m_connectionInfo.toLocal8Bit() ); // use what is set based on locale; after connecting, use Utf8
417+
bool ok = true;
418+
password = QInputDialog::getText( this,
419+
tr( "Enter password" ),
420+
tr( "Error: %1Enter password for %2")
421+
.arg( QString::fromUtf8( PQerrorMessage( pd ) ) )
422+
.arg( uri.connectionInfo() ),
423+
QLineEdit::Password,
424+
password,
425+
&ok );
426+
427+
::PQfinish( pd );
428+
429+
if( !ok )
430+
break;
431+
432+
pd = PQconnectdb( QString( "%1 password='%2'" ).arg( uri.connectionInfo() ).arg( password ).toLocal8Bit() );
437433
}
438434
}
439435

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
#include "qgspostgisbox3d.h"
4545
#include "qgslogger.h"
4646

47+
#include <QInputDialog>
48+
4749
const QString POSTGRES_KEY = "postgres";
4850
const QString POSTGRES_DESCRIPTION = "PostgreSQL/PostGIS data provider";
4951

@@ -304,8 +306,34 @@ QgsPostgresProvider::Conn *QgsPostgresProvider::Conn::connectDb( const QString &
304306

305307
PGconn *pd = PQconnectdb( conninfo.toLocal8Bit() ); // use what is set based on locale; after connecting, use Utf8
306308
// check the connection status
309+
if ( PQstatus( pd ) != CONNECTION_OK && QString::fromUtf8( PQerrorMessage( pd ) ) == PQnoPasswordSupplied )
310+
{
311+
QString password = QString::null;
312+
313+
while( PQstatus( pd ) != CONNECTION_OK )
314+
{
315+
bool ok = true;
316+
password = QInputDialog::getText( 0,
317+
tr( "Enter password" ),
318+
tr( "Error: %1Enter password for %2")
319+
.arg( QString::fromUtf8( PQerrorMessage( pd ) ) )
320+
.arg( conninfo ),
321+
QLineEdit::Password,
322+
password,
323+
&ok );
324+
325+
::PQfinish( pd );
326+
327+
if( !ok )
328+
break;
329+
330+
pd = PQconnectdb( QString( "%1 password='%2'" ).arg( conninfo ).arg( password ).toLocal8Bit() );
331+
}
332+
}
333+
307334
if ( PQstatus( pd ) != CONNECTION_OK )
308335
{
336+
::PQfinish( pd );
309337
QgsDebugMsg( "Connection to database failed" );
310338
return NULL;
311339
}

0 commit comments

Comments
 (0)
Please sign in to comment.