Quantum GIS API Documentation  1.7.4
src/core/qgscredentials.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002     qgscredentials.cpp -  interface for requesting credentials
00003     ----------------------
00004     begin                : February 2010
00005     copyright            : (C) 2010 by Juergen E. Fischer
00006     email                : jef at norbit dot de
00007  ***************************************************************************
00008  *                                                                         *
00009  *   This program is free software; you can redistribute it and/or modify  *
00010  *   it under the terms of the GNU General Public License as published by  *
00011  *   the Free Software Foundation; either version 2 of the License, or     *
00012  *   (at your option) any later version.                                   *
00013  *                                                                         *
00014  ***************************************************************************/
00015 /* $Id$ */
00016 
00017 #include "qgscredentials.h"
00018 #include "qgslogger.h"
00019 
00020 #include <QTextIStream>
00021 #include <QTextOStream>
00022 
00023 QgsCredentials *QgsCredentials::smInstance = 0;
00024 
00025 void QgsCredentials::setInstance( QgsCredentials *theInstance )
00026 {
00027   if ( smInstance )
00028     QgsDebugMsg( "already registered an instance of QgsCredentials" );
00029 
00030   smInstance = theInstance;
00031 }
00032 
00033 QgsCredentials *QgsCredentials::instance()
00034 {
00035   if ( smInstance )
00036     return smInstance;
00037 
00038   return new QgsCredentialsConsole();
00039 }
00040 
00041 QgsCredentials::~QgsCredentials()
00042 {
00043 }
00044 
00045 bool QgsCredentials::get( QString realm, QString &username, QString &password, QString message )
00046 {
00047   if ( mCredentialCache.contains( realm ) )
00048   {
00049     QPair<QString, QString> credentials = mCredentialCache.take( realm );
00050     username = credentials.first;
00051     password = credentials.second;
00052     QgsDebugMsg( QString( "retrieved realm:%1 username:%2 password:%3" ).arg( realm ).arg( username ).arg( password ) );
00053     return true;
00054   }
00055   else if ( request( realm, username, password, message ) )
00056   {
00057     QgsDebugMsg( QString( "requested realm:%1 username:%2 password:%3" ).arg( realm ).arg( username ).arg( password ) );
00058     return true;
00059   }
00060   else
00061   {
00062     QgsDebugMsg( QString( "unset realm:%1" ).arg( realm ) );
00063     return false;
00064   }
00065 }
00066 
00067 void QgsCredentials::put( QString realm, QString username, QString password )
00068 {
00069   QgsDebugMsg( QString( "inserting realm:%1 username:%2 password:%3" ).arg( realm ).arg( username ).arg( password ) );
00070   mCredentialCache.insert( realm, QPair<QString, QString>( username, password ) );
00071 }
00072 
00074 // QgsCredentialsConsole
00075 
00076 QgsCredentialsConsole::QgsCredentialsConsole()
00077 {
00078   setInstance( this );
00079 }
00080 
00081 bool QgsCredentialsConsole::request( QString realm, QString &username, QString &password, QString message )
00082 {
00083   QTextStream in( stdin, QIODevice::ReadOnly );
00084   QTextStream out( stdout, QIODevice::WriteOnly );
00085 
00086   out << "credentials for " << realm << endl;
00087   if ( !message.isEmpty() )
00088     out << "message: " << message << endl;
00089   out << "username: ";
00090   in >> username;
00091   out << "password: ";
00092   in >> password;
00093 
00094   return true;
00095 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines