QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgscredentials.cpp
Go to the documentation of this file.
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 
16 #include "qgscredentials.h"
17 #include "qgslogger.h"
18 
19 #include <QTextStream>
20 
22 
24 {
25  if ( smInstance )
26  {
27  QgsDebugMsg( "already registered an instance of QgsCredentials" );
28  }
29 
30  smInstance = theInstance;
31 }
32 
34 {
35  if ( smInstance )
36  return smInstance;
37 
38  return new QgsCredentialsConsole();
39 }
40 
42 {
43 }
44 
46 {
47 }
48 
49 bool QgsCredentials::get( QString realm, QString &username, QString &password, QString message )
50 {
51  if ( mCredentialCache.contains( realm ) )
52  {
53  QPair<QString, QString> credentials = mCredentialCache.take( realm );
54  username = credentials.first;
55  password = credentials.second;
56  QgsDebugMsg( QString( "retrieved realm:%1 username:%2 password:%3" ).arg( realm ).arg( username ).arg( password ) );
57 
58  if ( !password.isNull() )
59  return true;
60  }
61 
62  if ( request( realm, username, password, message ) )
63  {
64  QgsDebugMsg( QString( "requested realm:%1 username:%2 password:%3" ).arg( realm ).arg( username ).arg( password ) );
65  return true;
66  }
67  else
68  {
69  QgsDebugMsg( QString( "unset realm:%1" ).arg( realm ) );
70  return false;
71  }
72 }
73 
74 void QgsCredentials::put( QString realm, QString username, QString password )
75 {
76  QgsDebugMsg( QString( "inserting realm:%1 username:%2 password:%3" ).arg( realm ).arg( username ).arg( password ) );
77  mCredentialCache.insert( realm, QPair<QString, QString>( username, password ) );
78 }
79 
81 {
82  mMutex.lock();
83 }
84 
86 {
87  mMutex.unlock();
88 }
89 
90 
92 // QgsCredentialsConsole
93 
95 {
96  setInstance( this );
97 }
98 
99 bool QgsCredentialsConsole::request( QString realm, QString &username, QString &password, QString message )
100 {
101  QTextStream in( stdin, QIODevice::ReadOnly );
102  QTextStream out( stdout, QIODevice::WriteOnly );
103 
104  out << "credentials for " << realm << endl;
105  if ( !message.isEmpty() )
106  out << "message: " << message << endl;
107  out << "username: ";
108  in >> username;
109  out << "password: ";
110  in >> password;
111 
112  return true;
113 }
virtual bool request(QString realm, QString &username, QString &password, QString message=QString::null)
request a password
Interface for requesting credentials in QGIS in GUI independent way.
#define QgsDebugMsg(str)
Definition: qgslogger.h:36
static QgsCredentials * instance()
retrieves instance
bool get(QString realm, QString &username, QString &password, QString message=QString::null)
void lock()
Lock the instance against access from multiple threads.
static QgsCredentials * smInstance
Pointer to the credential instance.
virtual bool request(QString realm, QString &username, QString &password, QString message=QString::null)=0
request a password
QMap< QString, QPair< QString, QString > > mCredentialCache
cache for already requested credentials in this session
void put(QString realm, QString username, QString password)
Default implementation of credentials interface.
virtual ~QgsCredentials()
virtual destructor
void unlock()
Unlock the instance after being locked.
void setInstance(QgsCredentials *theInstance)
register instance