Skip to content

Commit

Permalink
apply #2322
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12641 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Dec 28, 2009
1 parent d2d9cf9 commit 80c4825
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 29 deletions.
33 changes: 30 additions & 3 deletions src/app/qgsnewconnection.cpp
Expand Up @@ -18,6 +18,7 @@

#include <QSettings>
#include <QMessageBox>
#include <QInputDialog>

#include "qgsnewconnection.h"
#include "qgscontexthelp.h"
Expand Down Expand Up @@ -139,10 +140,36 @@ void QgsNewConnection::testConnection()
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() );
if ( PQstatus( pd ) == CONNECTION_OK )
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: %1Enter 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.
QMessageBox::information( this, tr( "Test connection" ), tr( "Connection to %1 was successful" ).arg( txtDatabase->text() ) );
Expand Down
48 changes: 22 additions & 26 deletions src/app/qgspgsourceselect.cpp
Expand Up @@ -406,34 +406,30 @@ void QgsPgSourceSelect::on_btnConnect_clicked()
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() );

m_connectionInfo = uri.connectionInfo();
PQfinish( pd );
pd = PQconnectdb( m_connectionInfo.toLocal8Bit() ); // use what is set based on locale; after connecting, use Utf8
bool ok = true;
password = QInputDialog::getText( this,
tr( "Enter password" ),
tr( "Error: %1Enter 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() );
}
}

Expand Down
28 changes: 28 additions & 0 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -44,6 +44,8 @@
#include "qgspostgisbox3d.h"
#include "qgslogger.h"

#include <QInputDialog>

const QString POSTGRES_KEY = "postgres";
const QString POSTGRES_DESCRIPTION = "PostgreSQL/PostGIS data provider";

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

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: %1Enter 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;
}
Expand Down

0 comments on commit 80c4825

Please sign in to comment.