Index: src/app/qgspgsourceselect.cpp =================================================================== --- src/app/qgspgsourceselect.cpp (revision 12601) +++ src/app/qgspgsourceselect.cpp (working copy) @@ -406,34 +406,30 @@ if ( pd != 0 ) PQfinish( pd ); - pd = PQconnectdb( m_connectionInfo.toLocal8Bit() ); // use what is set based on locale; after connecting, use Utf8 - -#if defined(PG_VERSION_NUM) && PG_VERSION_NUM >= 80300 - // if the connection needs a password ask for one - if ( PQstatus( pd ) == CONNECTION_BAD && PQconnectionNeedsPassword( pd ) ) -#else - // if the connection failed and we didn't have a password, ask for one and retry - if ( PQstatus( pd ) == CONNECTION_BAD && password.isEmpty() ) -#endif + pd = PQconnectdb( uri.connectionInfo().toLocal8Bit() ); // use what is set based on locale; after connecting, use Utf8 + // check the connection status + if ( PQstatus( pd ) != CONNECTION_OK && QString::fromUtf8( PQerrorMessage( pd ) ) == PQnoPasswordSupplied ) { - // get password from user - bool makeConnection = false; - password = QInputDialog::getText( this, tr( "Password for " ) + username, - tr( "Please enter your password:" ), - QLineEdit::Password, QString::null, &makeConnection ); - // allow null password entry in case its valid for the database - if ( makeConnection ) + QString password = QString::null; + + while ( PQstatus( pd ) != CONNECTION_OK ) { - uri.setConnection( settings.value( key + "/host" ).toString(), - settings.value( key + "/port" ).toString(), - database, - settings.value( key + "/username" ).toString(), - password, - ( QgsDataSourceURI::SSLmode ) settings.value( key + "/sslmode", QgsDataSourceURI::SSLprefer ).toInt() ); + bool ok = true; + password = QInputDialog::getText( this, + tr( "Enter password" ), + tr( "Error: %1\nEnter password for %2" ) + .arg( QString::fromUtf8( PQerrorMessage( pd ) ) ) + .arg( uri.connectionInfo() ), + QLineEdit::Password, + password, + &ok ); - m_connectionInfo = uri.connectionInfo(); - PQfinish( pd ); - pd = PQconnectdb( m_connectionInfo.toLocal8Bit() ); // use what is set based on locale; after connecting, use Utf8 + ::PQfinish( pd ); + + if ( !ok ) + break; + + pd = PQconnectdb( QString( "%1 password='%2'" ).arg( uri.connectionInfo() ).arg( password ).toLocal8Bit() ); } } Index: src/app/qgsnewconnection.cpp =================================================================== --- src/app/qgsnewconnection.cpp (revision 12601) +++ src/app/qgsnewconnection.cpp (working copy) @@ -18,6 +18,7 @@ #include #include +#include #include "qgsnewconnection.h" #include "qgscontexthelp.h" @@ -139,9 +140,35 @@ QgsDataSourceURI uri; uri.setConnection( txtHost->text(), txtPort->text(), txtDatabase->text(), txtUsername->text(), txtPassword->text(), ( QgsDataSourceURI::SSLmode ) cbxSSLmode->itemData( cbxSSLmode->currentIndex() ).toInt() ); - QgsDebugMsg( "PQconnectdb(" + uri.connectionInfo() + ");" ); + QgsDebugMsg( "PQconnectdb(\"" + uri.connectionInfo() + "\");" ); - PGconn *pd = PQconnectdb( uri.connectionInfo().toLocal8Bit().data() ); + PGconn *pd = PQconnectdb( uri.connectionInfo().toLocal8Bit() ); // use what is set based on locale; after connecting, use Utf8 + // check the connection status + if ( PQstatus( pd ) != CONNECTION_OK && QString::fromUtf8( PQerrorMessage( pd ) ) == PQnoPasswordSupplied ) + { + QString password = QString::null; + + while ( PQstatus( pd ) != CONNECTION_OK ) + { + bool ok = true; + password = QInputDialog::getText( this, + tr( "Enter password" ), + tr( "Error: %1\nEnter password for %2" ) + .arg( QString::fromUtf8( PQerrorMessage( pd ) ) ) + .arg( uri.connectionInfo() ), + QLineEdit::Password, + password, + &ok ); + + ::PQfinish( pd ); + + if ( !ok ) + break; + + pd = PQconnectdb( QString( "%1 password='%2'" ).arg( uri.connectionInfo() ).arg( password ).toLocal8Bit() ); + } + } + if ( PQstatus( pd ) == CONNECTION_OK ) { // Database successfully opened; we can now issue SQL commands. Index: src/providers/postgres/qgspostgresprovider.cpp =================================================================== --- src/providers/postgres/qgspostgresprovider.cpp (revision 12601) +++ src/providers/postgres/qgspostgresprovider.cpp (working copy) @@ -44,6 +44,8 @@ #include "qgspostgisbox3d.h" #include "qgslogger.h" +#include + const QString POSTGRES_KEY = "postgres"; const QString POSTGRES_DESCRIPTION = "PostgreSQL/PostGIS data provider"; @@ -304,8 +306,34 @@ PGconn *pd = PQconnectdb( conninfo.toLocal8Bit() ); // use what is set based on locale; after connecting, use Utf8 // check the connection status + if ( PQstatus( pd ) != CONNECTION_OK && QString::fromUtf8( PQerrorMessage( pd ) ) == PQnoPasswordSupplied ) + { + QString password = QString::null; + + while ( PQstatus( pd ) != CONNECTION_OK ) + { + bool ok = true; + password = QInputDialog::getText( 0, + tr( "Enter password" ), + tr( "Error: %1\nEnter password for %2" ) + .arg( QString::fromUtf8( PQerrorMessage( pd ) ) ) + .arg( conninfo ), + QLineEdit::Password, + password, + &ok ); + + ::PQfinish( pd ); + + if ( !ok ) + break; + + pd = PQconnectdb( QString( "%1 password='%2'" ).arg( conninfo ).arg( password ).toLocal8Bit() ); + } + } + if ( PQstatus( pd ) != CONNECTION_OK ) { + ::PQfinish( pd ); QgsDebugMsg( "Connection to database failed" ); return NULL; }