Quantum GIS API Documentation  1.8
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 
00016 #include "qgscredentials.h"
00017 #include "qgslogger.h"
00018 
00019 #include <QTextIStream>
00020 #include <QTextOStream>
00021 
00022 QgsCredentials *QgsCredentials::smInstance = 0;
00023 
00024 void QgsCredentials::setInstance( QgsCredentials *theInstance )
00025 {
00026   if ( smInstance )
00027   {
00028     QgsDebugMsg( "already registered an instance of QgsCredentials" );
00029   }
00030 
00031   smInstance = theInstance;
00032 }
00033 
00034 QgsCredentials *QgsCredentials::instance()
00035 {
00036   if ( smInstance )
00037     return smInstance;
00038 
00039   return new QgsCredentialsConsole();
00040 }
00041 
00042 QgsCredentials::~QgsCredentials()
00043 {
00044 }
00045 
00046 bool QgsCredentials::get( QString realm, QString &username, QString &password, QString message )
00047 {
00048   if ( mCredentialCache.contains( realm ) )
00049   {
00050     QPair<QString, QString> credentials = mCredentialCache.take( realm );
00051     username = credentials.first;
00052     password = credentials.second;
00053     QgsDebugMsg( QString( "retrieved realm:%1 username:%2 password:%3" ).arg( realm ).arg( username ).arg( password ) );
00054     return true;
00055   }
00056   else if ( request( realm, username, password, message ) )
00057   {
00058     QgsDebugMsg( QString( "requested realm:%1 username:%2 password:%3" ).arg( realm ).arg( username ).arg( password ) );
00059     return true;
00060   }
00061   else
00062   {
00063     QgsDebugMsg( QString( "unset realm:%1" ).arg( realm ) );
00064     return false;
00065   }
00066 }
00067 
00068 void QgsCredentials::put( QString realm, QString username, QString password )
00069 {
00070   QgsDebugMsg( QString( "inserting realm:%1 username:%2 password:%3" ).arg( realm ).arg( username ).arg( password ) );
00071   mCredentialCache.insert( realm, QPair<QString, QString>( username, password ) );
00072 }
00073 
00075 // QgsCredentialsConsole
00076 
00077 QgsCredentialsConsole::QgsCredentialsConsole()
00078 {
00079   setInstance( this );
00080 }
00081 
00082 bool QgsCredentialsConsole::request( QString realm, QString &username, QString &password, QString message )
00083 {
00084   QTextStream in( stdin, QIODevice::ReadOnly );
00085   QTextStream out( stdout, QIODevice::WriteOnly );
00086 
00087   out << "credentials for " << realm << endl;
00088   if ( !message.isEmpty() )
00089     out << "message: " << message << endl;
00090   out << "username: ";
00091   in >> username;
00092   out << "password: ";
00093   in >> password;
00094 
00095   return true;
00096 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines