QGIS API Documentation  2.0.1-Dufour
 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 <QTextIStream>
20 #include <QTextOStream>
21 
23 
25 {
26  if ( smInstance )
27  {
28  QgsDebugMsg( "already registered an instance of QgsCredentials" );
29  }
30 
31  smInstance = theInstance;
32 }
33 
35 {
36  if ( smInstance )
37  return smInstance;
38 
39  return new QgsCredentialsConsole();
40 }
41 
43 {
44 }
45 
46 bool QgsCredentials::get( QString realm, QString &username, QString &password, QString message )
47 {
48  if ( mCredentialCache.contains( realm ) )
49  {
50  QPair<QString, QString> credentials = mCredentialCache.take( realm );
51  username = credentials.first;
52  password = credentials.second;
53  QgsDebugMsg( QString( "retrieved realm:%1 username:%2 password:%3" ).arg( realm ).arg( username ).arg( password ) );
54  return true;
55  }
56  else if ( request( realm, username, password, message ) )
57  {
58  QgsDebugMsg( QString( "requested realm:%1 username:%2 password:%3" ).arg( realm ).arg( username ).arg( password ) );
59  return true;
60  }
61  else
62  {
63  QgsDebugMsg( QString( "unset realm:%1" ).arg( realm ) );
64  return false;
65  }
66 }
67 
68 void QgsCredentials::put( QString realm, QString username, QString password )
69 {
70  QgsDebugMsg( QString( "inserting realm:%1 username:%2 password:%3" ).arg( realm ).arg( username ).arg( password ) );
71  mCredentialCache.insert( realm, QPair<QString, QString>( username, password ) );
72 }
73 
75 // QgsCredentialsConsole
76 
78 {
79  setInstance( this );
80 }
81 
82 bool QgsCredentialsConsole::request( QString realm, QString &username, QString &password, QString message )
83 {
84  QTextStream in( stdin, QIODevice::ReadOnly );
85  QTextStream out( stdout, QIODevice::WriteOnly );
86 
87  out << "credentials for " << realm << endl;
88  if ( !message.isEmpty() )
89  out << "message: " << message << endl;
90  out << "username: ";
91  in >> username;
92  out << "password: ";
93  in >> password;
94 
95  return true;
96 }