Skip to content
Permalink
Browse files

apply #2322

git-svn-id: http://svn.osgeo.org/qgis/trunk@12641 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef
jef committed Dec 28, 2009
1 parent a13c99d commit 9b7a041be714a135e43be38f9017b970dfbcc887
Showing with 80 additions and 29 deletions.
  1. +30 −3 src/app/qgsnewconnection.cpp
  2. +22 −26 src/app/qgspgsourceselect.cpp
  3. +28 −0 src/providers/postgres/qgspostgresprovider.cpp
@@ -18,6 +18,7 @@

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

#include "qgsnewconnection.h"
#include "qgscontexthelp.h"
@@ -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() ) );
@@ -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() );
}
}

@@ -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";

@@ -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;
}

0 comments on commit 9b7a041

Please sign in to comment.
You can’t perform that action at this time.