[QGIS Commit] r12641 - in trunk/qgis/src: app providers/postgres

svn_qgis at osgeo.org svn_qgis at osgeo.org
Mon Dec 28 16:35:24 EST 2009


Author: jef
Date: 2009-12-28 16:35:24 -0500 (Mon, 28 Dec 2009)
New Revision: 12641

Modified:
   trunk/qgis/src/app/qgsnewconnection.cpp
   trunk/qgis/src/app/qgspgsourceselect.cpp
   trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp
Log:
apply #2322

Modified: trunk/qgis/src/app/qgsnewconnection.cpp
===================================================================
--- trunk/qgis/src/app/qgsnewconnection.cpp	2009-12-28 21:33:39 UTC (rev 12640)
+++ trunk/qgis/src/app/qgsnewconnection.cpp	2009-12-28 21:35:24 UTC (rev 12641)
@@ -18,6 +18,7 @@
 
 #include <QSettings>
 #include <QMessageBox>
+#include <QInputDialog>
 
 #include "qgsnewconnection.h"
 #include "qgscontexthelp.h"
@@ -139,11 +140,37 @@
   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() ) );
   }

Modified: trunk/qgis/src/app/qgspgsourceselect.cpp
===================================================================
--- trunk/qgis/src/app/qgspgsourceselect.cpp	2009-12-28 21:33:39 UTC (rev 12640)
+++ trunk/qgis/src/app/qgspgsourceselect.cpp	2009-12-28 21:35:24 UTC (rev 12641)
@@ -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: %1Enter 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() );
     }
   }
 

Modified: trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp
===================================================================
--- trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp	2009-12-28 21:33:39 UTC (rev 12640)
+++ trunk/qgis/src/providers/postgres/qgspostgresprovider.cpp	2009-12-28 21:35:24 UTC (rev 12641)
@@ -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 @@
 
   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;
   }



More information about the QGIS-commit mailing list