Skip to content

Commit 3c5c55e

Browse files
author
jef
committedFeb 1, 2010
[FEATURE] allow postgres layers without saved username & password by asking for credentials
git-svn-id: http://svn.osgeo.org/qgis/trunk@12859 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 16983a8 commit 3c5c55e

18 files changed

+591
-345
lines changed
 

‎src/app/postgres/qgspgnewconnection.cpp

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "qgscontexthelp.h"
2525
#include "qgsdatasourceuri.h"
2626
#include "qgslogger.h"
27+
#include "qgscredentialdialog.h"
2728

2829
extern "C"
2930
{
@@ -55,7 +56,6 @@ QgsPgNewConnection::QgsPgNewConnection( QWidget *parent, const QString& connName
5556
port = "5432";
5657
}
5758
txtPort->setText( port );
58-
txtUsername->setText( settings.value( key + "/username" ).toString() );
5959
Qt::CheckState s = Qt::Checked;
6060
if ( ! settings.value( key + "/publicOnly", false ).toBool() )
6161
s = Qt::Unchecked;
@@ -69,11 +69,30 @@ QgsPgNewConnection::QgsPgNewConnection( QWidget *parent, const QString& connName
6969

7070
cbxSSLmode->setCurrentIndex( cbxSSLmode->findData( settings.value( key + "/sslmode", QgsDataSourceURI::SSLprefer ).toInt() ) );
7171

72-
if ( settings.value( key + "/save" ).toString() == "true" )
72+
if ( settings.value( key + "/saveUsername" ).toString() == "true" )
73+
{
74+
txtUsername->setText( settings.value( key + "/username" ).toString() );
75+
chkStoreUsername->setChecked( true );
76+
}
77+
78+
if ( settings.value( key + "/savePassword" ).toString() == "true" )
7379
{
7480
txtPassword->setText( settings.value( key + "/password" ).toString() );
7581
chkStorePassword->setChecked( true );
7682
}
83+
84+
// Old save setting
85+
if ( settings.contains( key + "/save" ) )
86+
{
87+
txtUsername->setText( settings.value( key + "/username" ).toString() );
88+
chkStoreUsername->setChecked( !txtUsername->text().isEmpty() );
89+
90+
if ( settings.value( key + "/save" ).toString() == "true" )
91+
txtPassword->setText( settings.value( key + "/password" ).toString() );
92+
93+
chkStorePassword->setChecked( true );
94+
}
95+
7796
txtName->setText( connName );
7897
}
7998
}
@@ -106,12 +125,16 @@ void QgsPgNewConnection::accept()
106125
settings.setValue( baseKey + "/host", txtHost->text() );
107126
settings.setValue( baseKey + "/database", txtDatabase->text() );
108127
settings.setValue( baseKey + "/port", txtPort->text() );
109-
settings.setValue( baseKey + "/username", txtUsername->text() );
128+
settings.setValue( baseKey + "/username", chkStoreUsername->isChecked() ? txtUsername->text() : "" );
110129
settings.setValue( baseKey + "/password", chkStorePassword->isChecked() ? txtPassword->text() : "" );
111130
settings.setValue( baseKey + "/publicOnly", cb_publicSchemaOnly->isChecked() );
112131
settings.setValue( baseKey + "/geometryColumnsOnly", cb_geometryColumnsOnly->isChecked() );
113-
settings.setValue( baseKey + "/save", chkStorePassword->isChecked() ? "true" : "false" );
114132
settings.setValue( baseKey + "/sslmode", cbxSSLmode->itemData( cbxSSLmode->currentIndex() ).toInt() );
133+
settings.setValue( baseKey + "/saveUsername", chkStoreUsername->isChecked() ? "true" : "false" );
134+
settings.setValue( baseKey + "/savePassword", chkStorePassword->isChecked() ? "true" : "false" );
135+
136+
// remove old save setting
137+
settings.remove( baseKey + "/save" );
115138

116139
QDialog::accept();
117140
}
@@ -138,45 +161,67 @@ QgsPgNewConnection::~QgsPgNewConnection()
138161
void QgsPgNewConnection::testConnection()
139162
{
140163
QgsDataSourceURI uri;
141-
uri.setConnection( txtHost->text(), txtPort->text(), txtDatabase->text(), txtUsername->text(), txtPassword->text(), ( QgsDataSourceURI::SSLmode ) cbxSSLmode->itemData( cbxSSLmode->currentIndex() ).toInt() );
142-
143-
QgsDebugMsg( "PQconnectdb(\"" + uri.connectionInfo() + "\");" );
144-
145-
PGconn *pd = PQconnectdb( uri.connectionInfo().toLocal8Bit() ); // use what is set based on locale; after connecting, use Utf8
164+
uri.setConnection( txtHost->text(), txtPort->text(), txtDatabase->text(),
165+
chkStoreUsername->isChecked() ? txtUsername->text() : "",
166+
chkStorePassword->isChecked() ? txtPassword->text() : "",
167+
( QgsDataSourceURI::SSLmode ) cbxSSLmode->itemData( cbxSSLmode->currentIndex() ).toInt() );
168+
QString conninfo = uri.connectionInfo();
169+
QgsDebugMsg( "PQconnectdb(\"" + conninfo + "\");" );
170+
171+
PGconn *pd = PQconnectdb( conninfo.toLocal8Bit() ); // use what is set based on locale; after connecting, use Utf8
146172
// check the connection status
147173
if ( PQstatus( pd ) != CONNECTION_OK && QString::fromUtf8( PQerrorMessage( pd ) ) == PQnoPasswordSupplied )
148174
{
149-
QString password = QString::null;
175+
QString username = txtUsername->text();
176+
QString password = txtPassword->text();
177+
178+
uri.setUsername( "" );
179+
uri.setPassword( "" );
150180

151181
while ( PQstatus( pd ) != CONNECTION_OK )
152182
{
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 );
183+
bool ok = QgsCredentials::instance()->get( conninfo, username, password, QString::fromUtf8( PQerrorMessage( pd ) ) );
184+
if ( !ok )
185+
break;
162186

163187
::PQfinish( pd );
164188

165-
if ( !ok )
166-
break;
189+
QgsDataSourceURI uri( conninfo );
190+
191+
if ( !username.isEmpty() )
192+
uri.setUsername( username );
193+
194+
if ( !password.isEmpty() )
195+
uri.setPassword( password );
196+
197+
QgsDebugMsg( "PQconnectdb(\"" + uri.connectionInfo() + "\");" );
198+
pd = PQconnectdb( uri.connectionInfo().toLocal8Bit() );
199+
}
200+
201+
if ( PQstatus( pd ) == CONNECTION_OK )
202+
{
203+
if ( chkStoreUsername->isChecked() )
204+
txtUsername->setText( username );
205+
if ( chkStorePassword->isChecked() )
206+
txtPassword->setText( password );
167207

168-
pd = PQconnectdb( QString( "%1 password='%2'" ).arg( uri.connectionInfo() ).arg( password ).toLocal8Bit() );
208+
QgsCredentials::instance()->put( conninfo, username, password );
169209
}
170210
}
171211

172212
if ( PQstatus( pd ) == CONNECTION_OK )
173213
{
174214
// Database successfully opened; we can now issue SQL commands.
175-
QMessageBox::information( this, tr( "Test connection" ), tr( "Connection to %1 was successful" ).arg( txtDatabase->text() ) );
215+
QMessageBox::information( this,
216+
tr( "Test connection" ),
217+
tr( "Connection to %1 was successful" ).arg( txtDatabase->text() ) );
176218
}
177219
else
178220
{
179-
QMessageBox::information( this, tr( "Test connection" ), tr( "Connection failed - Check settings and try again.\n\nExtended error information:\n%1" ).arg( QString::fromUtf8( PQerrorMessage( pd ) ) ) );
221+
QMessageBox::information( this,
222+
tr( "Test connection" ),
223+
tr( "Connection failed - Check settings and try again.\n\nExtended error information:\n%1" )
224+
.arg( QString::fromUtf8( PQerrorMessage( pd ) ) ) );
180225
}
181226
// free pg connection resources
182227
PQfinish( pd );

‎src/app/postgres/qgspgsourceselect.cpp

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ email : sherman at mrcc.com
2828
#include "qgsquerybuilder.h"
2929
#include "qgsdatasourceuri.h"
3030
#include "qgsvectorlayer.h"
31+
#include "qgscredentials.h"
3132

3233
#include <QInputDialog>
3334
#include <QMessageBox>
@@ -122,6 +123,8 @@ void QgsPgSourceSelect::on_btnDelete_clicked()
122123
settings.remove( key + "/sslmode" );
123124
settings.remove( key + "/publicOnly" );
124125
settings.remove( key + "/geometryColumnsOnly" );
126+
settings.remove( key + "/saveUsername" );
127+
settings.remove( key + "/savePassword" );
125128
settings.remove( key + "/save" );
126129

127130
populateConnectionList();
@@ -403,7 +406,7 @@ void QgsPgSourceSelect::on_btnConnect_clicked()
403406
uri.setConnection( settings.value( key + "/host" ).toString(),
404407
settings.value( key + "/port" ).toString(),
405408
database,
406-
settings.value( key + "/username" ).toString(),
409+
username,
407410
password,
408411
( QgsDataSourceURI::SSLmode ) settings.value( key + "/sslmode", QgsDataSourceURI::SSLprefer ).toInt() );
409412

@@ -426,28 +429,30 @@ void QgsPgSourceSelect::on_btnConnect_clicked()
426429
// check the connection status
427430
if ( PQstatus( pd ) != CONNECTION_OK && QString::fromUtf8( PQerrorMessage( pd ) ) == PQnoPasswordSupplied )
428431
{
429-
QString password = QString::null;
430-
431432
while ( PQstatus( pd ) != CONNECTION_OK )
432433
{
433-
bool ok = true;
434-
password = QInputDialog::getText( this,
435-
tr( "Enter password" ),
436-
tr( "Error: %1Enter password for %2" )
437-
.arg( QString::fromUtf8( PQerrorMessage( pd ) ) )
438-
.arg( m_connInfo ),
439-
QLineEdit::Password,
440-
password,
441-
&ok );
434+
bool ok = QgsCredentials::instance()->get( m_connInfo, username, password, QString::fromUtf8( PQerrorMessage( pd ) ) );
435+
if ( !ok )
436+
break;
442437

443438
::PQfinish( pd );
444439

445-
if ( !ok )
446-
break;
440+
QgsDataSourceURI uri( m_connInfo );
441+
if ( !username.isEmpty() )
442+
uri.setUsername( username );
447443

448-
m_privConnInfo = QString( "%1 password='%2'" ).arg( m_connInfo ).arg( password );
444+
if ( !password.isEmpty() )
445+
uri.setPassword( password );
446+
447+
m_privConnInfo = uri.connectionInfo();
448+
QgsDebugMsg( "connecting " + m_privConnInfo );
449449
pd = PQconnectdb( m_privConnInfo.toLocal8Bit() );
450450
}
451+
452+
if ( PQstatus( pd ) == CONNECTION_OK )
453+
{
454+
QgsCredentials::instance()->put( m_connInfo, username, password );
455+
}
451456
}
452457

453458
if ( PQstatus( pd ) == CONNECTION_OK )

‎src/app/qgisapp.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
#include "ogr/qgsopenvectorlayerdialog.h"
143143
#include "qgsattributetabledialog.h"
144144
#include "qgsvectorfilewriter.h"
145+
#include "qgscredentialdialog.h"
145146

146147
#ifdef HAVE_QWT
147148
#include "qgsgpsinformationwidget.h"
@@ -265,12 +266,11 @@ static void setTitleBarText_( QWidget & qgisApp )
265266
/**
266267
Creator function for output viewer
267268
*/
268-
static QgsMessageOutput* messageOutputViewer_()
269+
static QgsMessageOutput *messageOutputViewer_()
269270
{
270-
return new QgsMessageViewer();
271+
return new QgsMessageViewer( QgisApp::instance() );
271272
}
272273

273-
274274
/**
275275
* This function contains forced validation of CRS used in QGIS.
276276
* There are 3 options depending on the settings:
@@ -400,9 +400,13 @@ QgisApp::QgisApp( QSplashScreen *splash, QWidget * parent, Qt::WFlags fl )
400400

401401
// set QGIS specific srs validation
402402
QgsCoordinateReferenceSystem::setCustomSrsValidation( customSrsValidation_ );
403+
403404
// set graphical message output
404405
QgsMessageOutput::setMessageOutputCreator( messageOutputViewer_ );
405406

407+
// set graphical credential requester
408+
new QgsCredentialDialog( this );
409+
406410
fileNew(); // prepare empty project
407411
qApp->processEvents();
408412

‎src/app/qgsmanageconnectionsdialog.cpp

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,23 @@ QDomDocument QgsManageConnectionsDialog::savePgConnections( const QStringList &c
289289
el.setAttribute( "name", connections[ i ] );
290290
el.setAttribute( "host", settings.value( path + "/host", "" ).toString() );
291291
el.setAttribute( "port", settings.value( path + "/port", "" ).toString() );
292-
el.setAttribute( "db", settings.value( path + "/database", "" ).toString() );
293-
el.setAttribute( "username", settings.value( path + "/username", "" ).toString() );
294-
el.setAttribute( "password", settings.value( path + "/password", "" ).toString() );
292+
el.setAttribute( "database", settings.value( path + "/database", "" ).toString() );
295293
el.setAttribute( "sslmode", settings.value( path + "/sslmode", "1" ).toString() );
294+
295+
el.setAttribute( "saveUsername", settings.value( path + "/saveUsername", "false" ).toString() );
296+
297+
if ( settings.value( path + "/saveUsername", "false" ).toString() == "true" )
298+
{
299+
el.setAttribute( "username", settings.value( path + "/username", "" ).toString() );
300+
}
301+
302+
el.setAttribute( "savePassword", settings.value( path + "/savePassword", "false" ).toString() );
303+
304+
if ( settings.value( path + "/savePassword", "false" ).toString() == "true" )
305+
{
306+
el.setAttribute( "password", settings.value( path + "/password", "" ).toString() );
307+
}
308+
296309
root.appendChild( el );
297310
}
298311

@@ -359,7 +372,8 @@ void QgsManageConnectionsDialog::loadPgConnections( const QDomDocument &doc, con
359372
QDomElement root = doc.documentElement();
360373
if ( root.tagName() != "qgsPgConnections" )
361374
{
362-
QMessageBox::information( this, tr( "Loading connections" ),
375+
QMessageBox::information( this,
376+
tr( "Loading connections" ),
363377
tr( "The file is not an PostGIS connections exchange file." ) );
364378
return;
365379
}
@@ -382,7 +396,8 @@ void QgsManageConnectionsDialog::loadPgConnections( const QDomDocument &doc, con
382396
// check for duplicates
383397
if ( keys.contains( connectionName ) )
384398
{
385-
int res = QMessageBox::warning( this, tr( "Loading conections" ),
399+
int res = QMessageBox::warning( this,
400+
tr( "Loading connections" ),
386401
tr( "Connection with name %1 already exists. Overwrite?" )
387402
.arg( connectionName ),
388403
QMessageBox::Yes | QMessageBox::No );
@@ -395,19 +410,17 @@ void QgsManageConnectionsDialog::loadPgConnections( const QDomDocument &doc, con
395410

396411
//no dups detected or overwrite is allowed
397412
settings.beginGroup( "/PostgreSQL/connections/" + connectionName );
413+
398414
settings.setValue( "/host", child.attribute( "host" ) );
399415
settings.setValue( "/port", child.attribute( "port" ) );
400-
settings.setValue( "/database", child.attribute( "db" ) );
416+
settings.setValue( "/database", child.attribute( "database" ) );
401417
settings.setValue( "/sslmode", child.attribute( "sslmode" ) );
402-
if ( !child.attribute( "username" ).isEmpty() )
403-
{
404-
settings.setValue( "/username", child.attribute( "username" ) );
405-
settings.setValue( "/password", child.attribute( "password" ) );
406-
settings.setValue( "/save", "true" );
407-
}
418+
settings.setValue( "/saveUsername", child.attribute( "saveUsername" ) );
419+
settings.setValue( "/username", child.attribute( "username" ) );
420+
settings.setValue( "/savePassword", child.attribute( "savePassword" ) );
421+
settings.setValue( "/password", child.attribute( "password" ) );
408422
settings.endGroup();
409423

410424
child = child.nextSiblingElement();
411425
}
412426
}
413-

‎src/core/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ SET(QGIS_CORE_SRCS
5757
qgsmaprenderer.cpp
5858
qgsmaptopixel.cpp
5959
qgsmessageoutput.cpp
60+
qgscredentials.cpp
6061
qgsoverlayobject.cpp
6162
qgspalgeometry.cpp
6263
qgspalobjectpositionmanager.cpp
@@ -223,6 +224,7 @@ SET(QGIS_CORE_MOC_HDRS
223224
qgsmaplayerregistry.h
224225
qgsmaprenderer.h
225226
qgsmessageoutput.h
227+
qgscredentials.h
226228
qgspluginlayer.h
227229
qgsproject.h
228230
qgsrunprocess.h
@@ -389,6 +391,7 @@ SET(QGIS_CORE_HDRS
389391
qgsmaprenderer.h
390392
qgsmaptopixel.h
391393
qgsmessageoutput.h
394+
qgscredentials.h
392395
qgsoverlayobjectpositionmanager.h
393396
qgspalobjectpositionmanager.h
394397
qgspluginlayer.h

‎src/core/qgscredentials.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/***************************************************************************
2+
qgscredentials.cpp - interface for requesting credentials
3+
----------------------
4+
begin : February 2010
5+
copyright : (C) 2010 by Juergen E. Fischer
6+
email : jef at norbit dot de
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
/* $Id$ */
16+
17+
#include "qgscredentials.h"
18+
#include "qgslogger.h"
19+
20+
#include <QTextIStream>
21+
#include <QTextOStream>
22+
23+
QgsCredentials *QgsCredentials::smInstance = 0;
24+
25+
void QgsCredentials::setInstance( QgsCredentials *theInstance )
26+
{
27+
if ( smInstance )
28+
QgsDebugMsg( "already registered an instance of QgsCredentials" );
29+
30+
smInstance = theInstance;
31+
}
32+
33+
QgsCredentials *QgsCredentials::instance()
34+
{
35+
if ( smInstance )
36+
return smInstance;
37+
38+
return new QgsCredentialsConsole();
39+
}
40+
41+
QgsCredentials::~QgsCredentials()
42+
{
43+
}
44+
45+
bool QgsCredentials::get( QString realm, QString &username, QString &password, QString message )
46+
{
47+
if ( mCredentialCache.contains( realm ) )
48+
{
49+
QPair<QString, QString> credentials = mCredentialCache.take( realm );
50+
username = credentials.first;
51+
password = credentials.second;
52+
QgsDebugMsg( QString( "retrieved realm:%1 username:%2 password:%3" ).arg( realm ).arg( username ).arg( password ) );
53+
return true;
54+
}
55+
else if ( request( realm, username, password, message ) )
56+
{
57+
QgsDebugMsg( QString( "requested realm:%1 username:%2 password:%3" ).arg( realm ).arg( username ).arg( password ) );
58+
return true;
59+
}
60+
else
61+
{
62+
QgsDebugMsg( QString( "unset realm:%1" ).arg( realm ) );
63+
return false;
64+
}
65+
}
66+
67+
void QgsCredentials::put( QString realm, QString username, QString password )
68+
{
69+
QgsDebugMsg( QString( "inserting realm:%1 username:%2 password:%3" ).arg( realm ).arg( username ).arg( password ) );
70+
mCredentialCache.insert( realm, QPair<QString, QString>( username, password ) );
71+
}
72+
73+
////////////////////////////////
74+
// QgsCredentialsConsole
75+
76+
QgsCredentialsConsole::QgsCredentialsConsole()
77+
{
78+
setInstance( this );
79+
}
80+
81+
bool QgsCredentialsConsole::request( QString realm, QString &username, QString &password, QString message )
82+
{
83+
QTextStream in( stdin, QIODevice::ReadOnly );
84+
QTextStream out( stdout, QIODevice::WriteOnly );
85+
86+
out << "credentials for " << realm << endl;
87+
if ( !message.isEmpty() )
88+
out << "message: " << message << endl;
89+
out << "username: ";
90+
in >> username;
91+
out << "password: ";
92+
in >> password;
93+
94+
return true;
95+
}

‎src/core/qgscredentials.h

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/***************************************************************************
2+
qgscredentials.h - interface for requesting credentials
3+
----------------------
4+
begin : Feburary 2010
5+
copyright : (C) 2010 by Juergen E. Fischer
6+
email : jef at norbit dot de
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
/* $Id$ */
16+
17+
18+
#ifndef QGSCREDENTIALS_H
19+
#define QGSCREDENTIALS_H
20+
21+
#include <QString>
22+
#include <QObject>
23+
#include <QPair>
24+
#include <QMap>
25+
26+
/** \ingroup core
27+
* Interface for requesting credentials in QGIS in GUI independent way.
28+
* This class provides abstraction of a dialog for requesting credentials to the user.
29+
* By default QgsCredentials will be used if not overridden with other
30+
* credential creator function.
31+
32+
* QGIS application uses QgsCredentialDialog class for displaying a dialog to the user.
33+
34+
* Object deletes itself when it's not needed anymore. Children should use
35+
* signal destroyed() to be notified of the deletion
36+
*/
37+
class CORE_EXPORT QgsCredentials
38+
{
39+
public:
40+
//! virtual destructor
41+
virtual ~QgsCredentials();
42+
43+
bool get( QString realm, QString &username, QString &password, QString message = QString::null );
44+
void put( QString realm, QString username, QString password );
45+
46+
//! retrieves instance
47+
static QgsCredentials *instance();
48+
49+
protected:
50+
//! request a password
51+
virtual bool request( QString realm, QString &username, QString &password, QString message = QString::null ) = 0;
52+
53+
//! register instance
54+
void setInstance( QgsCredentials *theInstance );
55+
56+
private:
57+
//! cache for already requested credentials in this session
58+
QMap< QString, QPair<QString, QString> > mCredentialCache;
59+
60+
//! Pointer to the credential instance
61+
static QgsCredentials *smInstance;
62+
};
63+
64+
65+
/**
66+
\brief Default implementation of credentials interface
67+
68+
This class outputs message to the standard output and retrieves input from
69+
standard input. Therefore it won't be the right choice for apps without
70+
GUI.
71+
*/
72+
class CORE_EXPORT QgsCredentialsConsole : public QObject, public QgsCredentials
73+
{
74+
Q_OBJECT
75+
76+
public:
77+
QgsCredentialsConsole();
78+
79+
signals:
80+
//! signals that object will be destroyed and shouldn't be used anymore
81+
void destroyed();
82+
83+
protected:
84+
virtual bool request( QString realm, QString &username, QString &password, QString message = QString::null );
85+
};
86+
87+
#endif

‎src/core/qgsdatasourceuri.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,11 @@ QString QgsDataSourceURI::username() const
216216
return mUsername;
217217
}
218218

219+
void QgsDataSourceURI::setUsername( QString username )
220+
{
221+
mUsername = username;
222+
}
223+
219224
QString QgsDataSourceURI::host() const
220225
{
221226
return mHost;
@@ -231,6 +236,11 @@ QString QgsDataSourceURI::password() const
231236
return mPassword;
232237
}
233238

239+
void QgsDataSourceURI::setPassword( QString password )
240+
{
241+
mPassword = password;
242+
}
243+
234244
QString QgsDataSourceURI::port() const
235245
{
236246
return mPort;

‎src/core/qgsdatasourceuri.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ class CORE_EXPORT QgsDataSourceURI
6868
const QString& aSql = QString(),
6969
const QString& aKeyColumn = QString() );
7070

71+
//! set username
72+
// added in 1.5
73+
void setUsername( QString username );
74+
75+
//! set password
76+
// added in 1.5
77+
void setPassword( QString password );
78+
7179
//! Removes password element from uris
7280
static QString removePassword( const QString& aUri );
7381

‎src/core/qgspoint.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class CORE_EXPORT QgsPoint
121121
//! String representation of the point (x,y)
122122
QString toString() const;
123123

124-
//! As above but with precision for string representaiton of a point
124+
//! As above but with precision for string representation of a point
125125
QString toString( int thePrecision ) const;
126126

127127
/** Return a string representation as degrees minutes seconds.
@@ -177,7 +177,7 @@ class CORE_EXPORT QgsPoint
177177
double m_y;
178178

179179

180-
}; // class QgsPOint
180+
}; // class QgsPoint
181181

182182

183183
inline bool operator==( const QgsPoint &p1, const QgsPoint &p2 )

‎src/gui/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ qgsmaptoolemitpoint.cpp
4242
qgsmaptoolpan.cpp
4343
qgsmaptoolzoom.cpp
4444
qgsmessageviewer.cpp
45+
qgscredentialdialog.cpp
4546
qgsprojectbadlayerguihandler.cpp
4647
qgsprojectionselector.cpp
4748
qgsquickprint.cpp
@@ -78,6 +79,7 @@ qgsmapcanvas.h
7879
qgsmapoverviewcanvas.h
7980
qgsmaptoolemitpoint.h
8081
qgsmessageviewer.h
82+
qgscredentialdialog.h
8183
qgsprojectionselector.h
8284
qgsquickprint.h
8385
qgsludialog.h
@@ -155,6 +157,7 @@ qgsmaptoolemitpoint.h
155157
qgsmaptoolpan.h
156158
qgsmaptoolzoom.h
157159
qgsmessageviewer.h
160+
qgscredentialdialog.h
158161
qgsprojectionselector.h
159162
qgsrubberband.h
160163
qgsvertexmarker.h
@@ -163,6 +166,7 @@ qgsmaptip.h
163166
${CMAKE_CURRENT_BINARY_DIR}/../ui/ui_qgsdetaileditemwidgetbase.h
164167
${CMAKE_CURRENT_BINARY_DIR}/../ui/ui_qgsgenericprojectionselectorbase.h
165168
${CMAKE_CURRENT_BINARY_DIR}/../ui/ui_qgsmessageviewer.h
169+
${CMAKE_CURRENT_BINARY_DIR}/../ui/ui_qgscredentialdialog.h
166170
${CMAKE_CURRENT_BINARY_DIR}/../ui/ui_qgsprojectionselectorbase.h
167171
)
168172

‎src/gui/qgscredentialdialog.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/***************************************************************************
2+
qgscredentialdialog.cpp - description
3+
-------------------
4+
begin : February 2010
5+
copyright : (C) 2010 by Juergen E. Fischer
6+
email : jef at norbit dot de
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
/* $Id$ */
18+
19+
#include "qgscredentialdialog.h"
20+
#include <QSettings>
21+
22+
QgsCredentialDialog::QgsCredentialDialog( QWidget *parent, Qt::WFlags fl )
23+
: QDialog( parent, fl )
24+
{
25+
setupUi( this );
26+
setInstance( this );
27+
}
28+
29+
QgsCredentialDialog::~QgsCredentialDialog()
30+
{
31+
}
32+
33+
bool QgsCredentialDialog::request( QString realm, QString &username, QString &password, QString message )
34+
{
35+
labelRealm->setText( realm );
36+
leUsername->setText( username );
37+
lePassword->setText( password );
38+
labelMessage->setText( message );
39+
labelMessage->setHidden( message.isEmpty() );
40+
41+
if ( exec() == QDialog::Accepted )
42+
{
43+
username = leUsername->text();
44+
password = lePassword->text();
45+
return true;
46+
}
47+
else
48+
{
49+
return false;
50+
}
51+
}

‎src/gui/qgscredentialdialog.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/***************************************************************************
2+
qgscredentialdialog.h - description
3+
-------------------
4+
begin : February 2010
5+
copyright : (C) 2010 by Juergen E. Fischer
6+
email : jef at norbit dot de
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
/* $Id$ */
18+
#ifndef QGSCREDENTIALDIALOG_H
19+
#define QGSCREDENTIALDIALOG_H
20+
21+
#include <ui_qgscredentialdialog.h>
22+
#include <qgisgui.h>
23+
#include "qgscredentialdialog.h"
24+
#include "qgscredentials.h"
25+
26+
#include <QString>
27+
28+
29+
/** \ingroup gui
30+
* A generic dialog for requesting credentials
31+
*/
32+
class GUI_EXPORT QgsCredentialDialog : public QDialog, public QgsCredentials, private Ui_QgsCredentialDialog
33+
{
34+
Q_OBJECT
35+
public:
36+
QgsCredentialDialog( QWidget *parent = 0, Qt::WFlags fl = QgisGui::ModalDialogFlags );
37+
~QgsCredentialDialog();
38+
39+
protected:
40+
virtual bool request( QString realm, QString &username, QString &password, QString message = QString::null );
41+
};
42+
43+
#endif

‎src/plugins/spit/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ SET (SPIT_UIS
2323
../../ui/qgspgnewconnectionbase.ui
2424
qgsspitbase.ui
2525
../../ui/qgsmessageviewer.ui
26+
../../ui/qgscredentialdialog.ui
2627
)
2728

2829
SET (SPIT_EXE_MOC_HDRS

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
#include "qgspostgisbox3d.h"
4545
#include "qgslogger.h"
4646

47-
#include <QInputDialog>
47+
#include "qgscredentials.h"
4848

4949
const QString POSTGRES_KEY = "postgres";
5050
const QString POSTGRES_DESCRIPTION = "PostgreSQL/PostGIS data provider";
@@ -291,7 +291,7 @@ QgsPostgresProvider::~QgsPostgresProvider()
291291
//pLog.flush();
292292
}
293293

294-
QgsPostgresProvider::Conn *QgsPostgresProvider::Conn::connectDb( const QString & conninfo, bool readonly )
294+
QgsPostgresProvider::Conn *QgsPostgresProvider::Conn::connectDb( const QString &conninfo, bool readonly )
295295
{
296296
QMap<QString, QgsPostgresProvider::Conn *> &connections =
297297
readonly ? QgsPostgresProvider::Conn::connectionsRO : QgsPostgresProvider::Conn::connectionsRW;
@@ -309,38 +309,30 @@ QgsPostgresProvider::Conn *QgsPostgresProvider::Conn::connectDb( const QString &
309309
// check the connection status
310310
if ( PQstatus( pd ) != CONNECTION_OK && QString::fromUtf8( PQerrorMessage( pd ) ) == PQnoPasswordSupplied )
311311
{
312-
QString password = QString::null;
312+
QgsDataSourceURI uri( conninfo );
313+
QString username = uri.username();
314+
QString password = uri.password();
313315

314316
while ( PQstatus( pd ) != CONNECTION_OK )
315317
{
316-
bool ok = true;
317-
318-
if ( passwordCache.contains( conninfo ) )
319-
{
320-
password = passwordCache.take( conninfo );
321-
}
322-
else
323-
{
324-
password = QInputDialog::getText( 0,
325-
tr( "Enter password" ),
326-
tr( "Error: %1Enter password for %2" )
327-
.arg( QString::fromUtf8( PQerrorMessage( pd ) ) )
328-
.arg( conninfo ),
329-
QLineEdit::Password,
330-
password,
331-
&ok );
332-
}
333-
318+
bool ok = QgsCredentials::instance()->get( conninfo, username, password, QString::fromUtf8( PQerrorMessage( pd ) ) );
334319
if ( !ok )
335320
break;
336321

337322
::PQfinish( pd );
338323

339-
pd = PQconnectdb( QString( "%1 password='%2'" ).arg( conninfo ).arg( password ).toLocal8Bit() );
324+
if ( !username.isEmpty() )
325+
uri.setUsername( username );
326+
327+
if ( !password.isEmpty() )
328+
uri.setPassword( password );
329+
330+
QgsDebugMsg( "Connecting to " + uri.connectionInfo() );
331+
pd = PQconnectdb( uri.connectionInfo().toLocal8Bit() );
340332
}
341333

342334
if ( PQstatus( pd ) == CONNECTION_OK )
343-
passwordCache[ conninfo ] = password;
335+
QgsCredentials::instance()->put( conninfo, username, password );
344336
}
345337

346338
if ( PQstatus( pd ) != CONNECTION_OK )

‎src/ui/qgscompositionwidget.h

Lines changed: 0 additions & 228 deletions
This file was deleted.

‎src/ui/qgscredentialdialog.ui

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>QgsCredentialDialog</class>
4+
<widget class="QDialog" name="QgsCredentialDialog">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>365</width>
10+
<height>156</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>Enter Credentials</string>
15+
</property>
16+
<layout class="QFormLayout" name="formLayout">
17+
<property name="fieldGrowthPolicy">
18+
<enum>QFormLayout::ExpandingFieldsGrow</enum>
19+
</property>
20+
<item row="2" column="0">
21+
<widget class="QLabel" name="label">
22+
<property name="text">
23+
<string>Username</string>
24+
</property>
25+
</widget>
26+
</item>
27+
<item row="2" column="1">
28+
<widget class="QLineEdit" name="leUsername"/>
29+
</item>
30+
<item row="3" column="0">
31+
<widget class="QLabel" name="label_2">
32+
<property name="text">
33+
<string>Password</string>
34+
</property>
35+
</widget>
36+
</item>
37+
<item row="3" column="1">
38+
<widget class="QLineEdit" name="lePassword">
39+
<property name="echoMode">
40+
<enum>QLineEdit::Password</enum>
41+
</property>
42+
</widget>
43+
</item>
44+
<item row="5" column="0" colspan="2">
45+
<widget class="QDialogButtonBox" name="buttonBox">
46+
<property name="orientation">
47+
<enum>Qt::Horizontal</enum>
48+
</property>
49+
<property name="standardButtons">
50+
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
51+
</property>
52+
</widget>
53+
</item>
54+
<item row="1" column="1">
55+
<widget class="QLabel" name="labelRealm">
56+
<property name="text">
57+
<string>TextLabel</string>
58+
</property>
59+
</widget>
60+
</item>
61+
<item row="4" column="0" colspan="2">
62+
<widget class="QLabel" name="labelMessage">
63+
<property name="text">
64+
<string>TextLabel</string>
65+
</property>
66+
</widget>
67+
</item>
68+
<item row="1" column="0">
69+
<widget class="QLabel" name="label_3">
70+
<property name="text">
71+
<string>Realm</string>
72+
</property>
73+
</widget>
74+
</item>
75+
</layout>
76+
</widget>
77+
<resources/>
78+
<connections>
79+
<connection>
80+
<sender>buttonBox</sender>
81+
<signal>accepted()</signal>
82+
<receiver>QgsCredentialDialog</receiver>
83+
<slot>accept()</slot>
84+
<hints>
85+
<hint type="sourcelabel">
86+
<x>248</x>
87+
<y>254</y>
88+
</hint>
89+
<hint type="destinationlabel">
90+
<x>157</x>
91+
<y>274</y>
92+
</hint>
93+
</hints>
94+
</connection>
95+
<connection>
96+
<sender>buttonBox</sender>
97+
<signal>rejected()</signal>
98+
<receiver>QgsCredentialDialog</receiver>
99+
<slot>reject()</slot>
100+
<hints>
101+
<hint type="sourcelabel">
102+
<x>316</x>
103+
<y>260</y>
104+
</hint>
105+
<hint type="destinationlabel">
106+
<x>286</x>
107+
<y>274</y>
108+
</hint>
109+
</hints>
110+
</connection>
111+
</connections>
112+
</ui>

‎src/ui/qgspgnewconnectionbase.ui

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@
100100
</property>
101101
</widget>
102102
</item>
103+
<item>
104+
<widget class="QLabel" name="TextLabel3_3">
105+
<property name="text">
106+
<string>SSL mode</string>
107+
</property>
108+
</widget>
109+
</item>
103110
<item>
104111
<widget class="QLabel" name="TextLabel3">
105112
<property name="text">
@@ -120,13 +127,6 @@
120127
</property>
121128
</widget>
122129
</item>
123-
<item>
124-
<widget class="QLabel" name="TextLabel3_3">
125-
<property name="text">
126-
<string>SSL mode</string>
127-
</property>
128-
</widget>
129-
</item>
130130
</layout>
131131
</item>
132132
<item>
@@ -157,6 +157,9 @@
157157
</property>
158158
</widget>
159159
</item>
160+
<item>
161+
<widget class="QComboBox" name="cbxSSLmode"/>
162+
</item>
160163
<item>
161164
<widget class="QLineEdit" name="txtUsername"/>
162165
</item>
@@ -167,37 +170,10 @@
167170
</property>
168171
</widget>
169172
</item>
170-
<item>
171-
<widget class="QComboBox" name="cbxSSLmode"/>
172-
</item>
173173
</layout>
174174
</item>
175175
</layout>
176176
</item>
177-
<item row="1" column="0">
178-
<layout class="QHBoxLayout">
179-
<property name="spacing">
180-
<number>6</number>
181-
</property>
182-
<property name="margin">
183-
<number>0</number>
184-
</property>
185-
<item>
186-
<widget class="QCheckBox" name="chkStorePassword">
187-
<property name="text">
188-
<string>Save Password</string>
189-
</property>
190-
</widget>
191-
</item>
192-
<item>
193-
<widget class="QPushButton" name="btnConnect">
194-
<property name="text">
195-
<string>&amp;Test Connect</string>
196-
</property>
197-
</widget>
198-
</item>
199-
</layout>
200-
</item>
201177
<item row="2" column="0">
202178
<widget class="QCheckBox" name="cb_geometryColumnsOnly">
203179
<property name="toolTip">
@@ -224,6 +200,34 @@
224200
</property>
225201
</widget>
226202
</item>
203+
<item row="1" column="0">
204+
<layout class="QGridLayout" name="gridLayout">
205+
<property name="margin">
206+
<number>0</number>
207+
</property>
208+
<item row="0" column="0">
209+
<widget class="QCheckBox" name="chkStoreUsername">
210+
<property name="text">
211+
<string>Save Username</string>
212+
</property>
213+
</widget>
214+
</item>
215+
<item row="0" column="1" rowspan="2">
216+
<widget class="QPushButton" name="btnConnect">
217+
<property name="text">
218+
<string>&amp;Test Connect</string>
219+
</property>
220+
</widget>
221+
</item>
222+
<item row="1" column="0">
223+
<widget class="QCheckBox" name="chkStorePassword">
224+
<property name="text">
225+
<string>Save Password</string>
226+
</property>
227+
</widget>
228+
</item>
229+
</layout>
230+
</item>
227231
</layout>
228232
</widget>
229233
</item>
@@ -244,11 +248,8 @@
244248
<tabstop>txtPort</tabstop>
245249
<tabstop>txtUsername</tabstop>
246250
<tabstop>txtPassword</tabstop>
247-
<tabstop>cbxSSLmode</tabstop>
248-
<tabstop>chkStorePassword</tabstop>
249251
<tabstop>cb_geometryColumnsOnly</tabstop>
250252
<tabstop>cb_publicSchemaOnly</tabstop>
251-
<tabstop>btnConnect</tabstop>
252253
<tabstop>buttonBox</tabstop>
253254
</tabstops>
254255
<resources/>

0 commit comments

Comments
 (0)
Please sign in to comment.