00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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;
00048
const char * display;
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
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() {
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;
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;
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