QGIS API Documentation  2.3.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 
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 
74 // QgsCredentialsConsole
75 
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 }
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)
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 setInstance(QgsCredentials *theInstance)
register instance