qgpgmeprogresstokenmapper.cpp
1 /*
2  qgpgmeprogresstokenmapper.cpp
3 
4  This file is part of libkleopatra, the KDE keymanagement library
5  Copyright (c) 2004 Klarälvdalens Datakonsult AB
6 
7  Libkleopatra is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of the
10  License, or (at your option) any later version.
11 
12  Libkleopatra is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  In addition, as a special exception, the copyright holders give
22  permission to link the code of this program with any edition of
23  the TQt library by Trolltech AS, Norway (or with modified versions
24  of TQt that use the same license as TQt), and distribute linked
25  combinations including the two. You must obey the GNU General
26  Public License in all respects for all of the code used other than
27  TQt. If you modify this file, you may extend this exception to
28  your version of the file, but you are not obligated to do so. If
29  you do not wish to do so, delete this exception statement from
30  your version.
31 */
32 
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36 
37 #include "qgpgmeprogresstokenmapper.h"
38 
39 #include <tdelocale.h>
40 
41 #include <tqstring.h>
42 
43 #include <assert.h>
44 #include <map>
45 
46 struct Desc {
47  int type; // 0 == fallback
48  const char * display; // add %1 for useCur ^ useTot and %1 %2 for useCur == useTot == true
49  bool useCur : 1;
50  bool useTot : 1;
51 };
52 
53 static const struct Desc pk_dsa[] = {
54  { 0, I18N_NOOP("Generating DSA key..."), false, false }
55 };
56 
57 static const struct Desc pk_elg[] = {
58  { 0, I18N_NOOP("Generating ElGamal key..."), false, false }
59 };
60 
61 static const struct Desc primegen[] = {
62  // FIXME: add all type's?
63  { 0, I18N_NOOP("Searching for a large prime number..."), false, false }
64 };
65 
66 static const struct Desc need_entropy[] = {
67  { 0, I18N_NOOP("Waiting for new entropy from random number generator (you might want to exercise the harddisks or move the mouse)..."), false, false }
68 };
69 
70 static const struct Desc tick[] = {
71  { 0, I18N_NOOP("Please wait..."), false, false }
72 };
73 
74 static const struct Desc starting_agent[] = {
75  { 0, I18N_NOOP("Starting gpg-agent (you should consider starting a global instance instead)..."), false, false }
76 };
77 
78 static const struct {
79  const char * token;
80  const Desc * desc;
81  unsigned int numDesc;
82 } tokens[] = {
83 #define make_token(x) { #x, x, sizeof(x) / sizeof(*x) }
84  make_token(pk_dsa),
85  make_token(pk_elg),
86  make_token(primegen),
87  make_token(need_entropy),
88  make_token(tick),
89  make_token(starting_agent)
90 #undef make_token
91 };
92 
93 
94 
95 Kleo::QGpgMEProgressTokenMapper * Kleo::QGpgMEProgressTokenMapper::mSelf = 0;
96 
97 const Kleo::QGpgMEProgressTokenMapper * Kleo::QGpgMEProgressTokenMapper::instance() {
98  if ( !mSelf )
99  (void) new QGpgMEProgressTokenMapper();
100  return mSelf;
101 }
102 
103 Kleo::QGpgMEProgressTokenMapper::QGpgMEProgressTokenMapper() {
104  mSelf = this;
105 }
106 
107 Kleo::QGpgMEProgressTokenMapper::~QGpgMEProgressTokenMapper() {
108  mSelf = 0;
109 }
110 
111 typedef std::map< TQString, std::map<int,Desc> > Map;
112 
113 static const Map & makeMap() { // return a reference to a static to avoid copying
114  static Map map;
115  for ( unsigned int i = 0 ; i < sizeof tokens / sizeof *tokens ; ++i ) {
116  assert( tokens[i].token );
117  const TQString token = TQString::fromLatin1( tokens[i].token ).lower();
118  for ( unsigned int j = 0 ; j < tokens[i].numDesc ; ++j ) {
119  const Desc & desc = tokens[i].desc[j];
120  assert( desc.display );
121  map[ token ][ desc.type ] = desc;
122  }
123  }
124  return map;
125 }
126 
127 TQString Kleo::QGpgMEProgressTokenMapper::map( const char * tokenUtf8, int subtoken, int cur, int tot ) const {
128  if ( !tokenUtf8 || !*tokenUtf8 )
129  return TQString();
130 
131  if ( qstrcmp( tokenUtf8, "file:" ) == 0 )
132  return TQString(); // gpgme's job
133 
134  return map( TQString::fromUtf8( tokenUtf8 ), subtoken, cur, tot );
135 }
136 
137 TQString Kleo::QGpgMEProgressTokenMapper::map( const TQString & token, int subtoken, int cur, int tot ) const {
138  if ( token.startsWith( "file:" ) )
139  return TQString(); // gpgme's job
140 
141  static const Map & tokenMap = makeMap();
142 
143  const Map::const_iterator it1 = tokenMap.find( token.lower() );
144  if ( it1 == tokenMap.end() )
145  return token;
146  std::map<int,Desc>::const_iterator it2 = it1->second.find( subtoken );
147  if ( it2 == it1->second.end() )
148  it2 = it1->second.find( 0 );
149  if ( it2 == it1->second.end() )
150  return token;
151  const Desc & desc = it2->second;
152  TQString result = i18n( desc.display );
153  if ( desc.useCur )
154  result = result.arg( cur );
155  if ( desc.useTot )
156  result = result.arg( tot );
157  return result;
158 }
159