certmanager/lib

qgpgmeprogresstokenmapper.cpp

00001 /*  -*- mode: C++; c-file-style: "gnu" -*-
00002     qgpgmeprogresstokenmapper.cpp
00003 
00004     This file is part of libkleopatra, the KDE keymanagement library
00005     Copyright (c) 2004 Klarälvdalens Datakonsult AB
00006 
00007     Libkleopatra is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU General Public License as
00009     published by the Free Software Foundation; either version 2 of the
00010     License, or (at your option) any later version.
00011 
00012     Libkleopatra is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program; if not, write to the Free Software
00019     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021     In addition, as a special exception, the copyright holders give
00022     permission to link the code of this program with any edition of
00023     the Qt library by Trolltech AS, Norway (or with modified versions
00024     of Qt that use the same license as Qt), and distribute linked
00025     combinations including the two.  You must obey the GNU General
00026     Public License in all respects for all of the code used other than
00027     Qt.  If you modify this file, you may extend this exception to
00028     your version of the file, but you are not obligated to do so.  If
00029     you do not wish to do so, delete this exception statement from
00030     your version.
00031 */
00032 
00033 #ifdef HAVE_CONFIG_H
00034 #include <config.h>
00035 #endif
00036 
00037 #include "qgpgmeprogresstokenmapper.h"
00038 
00039 #include <klocale.h>
00040 
00041 #include <qstring.h>
00042 
00043 #include <assert.h>
00044 #include <map>
00045 
00046 struct Desc {
00047   int type; // 0 == fallback
00048   const char * display; // add %1 for useCur ^ useTot and %1 %2 for useCur == useTot == true
00049   bool useCur : 1;
00050   bool useTot : 1;
00051 };
00052 
00053 static const struct Desc pk_dsa[] = {
00054   { 0, I18N_NOOP("Generating DSA key..."), false, false }
00055 };
00056 
00057 static const struct Desc pk_elg[] = {
00058   { 0, I18N_NOOP("Generating ElGamal key..."), false, false }
00059 };
00060 
00061 static const struct Desc primegen[] = {
00062   // FIXME: add all type's?
00063   { 0, I18N_NOOP("Searching for a large prime number..."), false, false }
00064 };
00065 
00066 static const struct Desc need_entropy[] = {
00067   { 0, I18N_NOOP("Waiting for new entropy from random number generator (you might want to excercise the harddisks or move the mouse)..."), false, false }
00068 };
00069 
00070 static const struct Desc tick[] = {
00071   { 0, I18N_NOOP("Please wait..."), false, false }
00072 };
00073 
00074 static const struct Desc starting_agent[] = {
00075   { 0, I18N_NOOP("Starting gpg-agent (you should consider starting a global instance instead)..."), false, false }
00076 };
00077 
00078 static const struct {
00079   const char * token;
00080   const Desc * desc;
00081   unsigned int numDesc;
00082 } tokens[] = {
00083 #define make_token(x) { #x, x, sizeof(x) / sizeof(*x) }
00084   make_token(pk_dsa),
00085   make_token(pk_elg),
00086   make_token(primegen),
00087   make_token(need_entropy),
00088   make_token(tick),
00089   make_token(starting_agent)
00090 #undef make_token
00091 };
00092 
00093 
00094 
00095 Kleo::QGpgMEProgressTokenMapper * Kleo::QGpgMEProgressTokenMapper::mSelf = 0;
00096 
00097 const Kleo::QGpgMEProgressTokenMapper * Kleo::QGpgMEProgressTokenMapper::instance() {
00098   if ( !mSelf )
00099     (void) new QGpgMEProgressTokenMapper();
00100   return mSelf;
00101 }
00102 
00103 Kleo::QGpgMEProgressTokenMapper::QGpgMEProgressTokenMapper() {
00104   mSelf = this;
00105 }
00106 
00107 Kleo::QGpgMEProgressTokenMapper::~QGpgMEProgressTokenMapper() {
00108   mSelf = 0;
00109 }
00110 
00111 typedef std::map< QString, std::map<int,Desc> > Map;
00112 
00113 static const Map & makeMap() { // return a reference to a static to avoid copying
00114   static Map map;
00115   for ( unsigned int i = 0 ; i < sizeof tokens / sizeof *tokens ; ++i ) {
00116     assert( tokens[i].token );
00117     const QString token = QString::fromLatin1( tokens[i].token ).lower();
00118     for ( unsigned int j = 0 ; j < tokens[i].numDesc ; ++j ) {
00119       const Desc & desc = tokens[i].desc[j];
00120       assert( desc.display );
00121       map[ token ][ desc.type ] = desc;
00122     }
00123   }
00124   return map;
00125 }
00126 
00127 QString Kleo::QGpgMEProgressTokenMapper::map( const char * tokenUtf8, int subtoken, int cur, int tot ) const {
00128   if ( !tokenUtf8 || !*tokenUtf8 )
00129     return QString::null;
00130 
00131   if ( qstrcmp( tokenUtf8, "file:" ) == 0 )
00132     return QString::null; // gpgme's job
00133 
00134   return map( QString::fromUtf8( tokenUtf8 ), subtoken, cur, tot );
00135 }
00136 
00137 QString Kleo::QGpgMEProgressTokenMapper::map( const QString & token, int subtoken, int cur, int tot ) const {
00138   if ( token.startsWith( "file:" ) )
00139     return QString::null; // gpgme's job
00140 
00141   static const Map & tokenMap = makeMap();
00142 
00143   const Map::const_iterator it1 = tokenMap.find( token.lower() );
00144   if ( it1 == tokenMap.end() )
00145     return token;
00146   std::map<int,Desc>::const_iterator it2 = it1->second.find( subtoken );
00147   if ( it2 == it1->second.end() )
00148     it2 = it1->second.find( 0 );
00149   if ( it2 == it1->second.end() )
00150     return token;
00151   const Desc & desc = it2->second;
00152   QString result = i18n( desc.display );
00153   if ( desc.useCur )
00154     result = result.arg( cur );
00155   if ( desc.useTot )
00156     result = result.arg( tot );
00157   return result;
00158 }
00159 
KDE Home | KDE Accessibility Home | Description of Access Keys