certmanager/lib

enum.cpp
1 /*
2  kleo/enum.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 #include "enum.h"
34 
35 #include <tdelocale.h>
36 
37 #include <tqstring.h>
38 #include <tqstringlist.h>
39 
40 static const struct {
41  Kleo::CryptoMessageFormat format;
42  const char * displayName;
43  const char * configName;
44 } cryptoMessageFormats[] = {
45  { Kleo::InlineOpenPGPFormat,
46  I18N_NOOP("Inline OpenPGP (deprecated)"),
47  "inline openpgp" },
48  { Kleo::OpenPGPMIMEFormat,
49  I18N_NOOP("OpenPGP/MIME"),
50  "openpgp/mime" },
51  { Kleo::SMIMEFormat,
52  I18N_NOOP("S/MIME"),
53  "s/mime" },
54  { Kleo::SMIMEOpaqueFormat,
55  I18N_NOOP("S/MIME Opaque"),
56  "s/mime opaque" },
57 };
58 static const unsigned int numCryptoMessageFormats
59  = sizeof cryptoMessageFormats / sizeof *cryptoMessageFormats ;
60 
61 const char * Kleo::cryptoMessageFormatToString( Kleo::CryptoMessageFormat f ) {
62  if ( f == AutoFormat )
63  return "auto";
64  for ( unsigned int i = 0 ; i < numCryptoMessageFormats ; ++i )
65  if ( f == cryptoMessageFormats[i].format )
66  return cryptoMessageFormats[i].configName;
67  return 0;
68 }
69 
70 TQStringList Kleo::cryptoMessageFormatsToStringList( unsigned int f ) {
71  TQStringList result;
72  for ( unsigned int i = 0 ; i < numCryptoMessageFormats ; ++i )
73  if ( f & cryptoMessageFormats[i].format )
74  result.push_back( cryptoMessageFormats[i].configName );
75  return result;
76 }
77 
78 TQString Kleo::cryptoMessageFormatToLabel( Kleo::CryptoMessageFormat f ) {
79  if ( f == AutoFormat )
80  return i18n("Any");
81  for ( unsigned int i = 0 ; i < numCryptoMessageFormats ; ++i )
82  if ( f == cryptoMessageFormats[i].format )
83  return i18n( cryptoMessageFormats[i].displayName );
84  return TQString();
85 }
86 
87 Kleo::CryptoMessageFormat Kleo::stringToCryptoMessageFormat( const TQString & s ) {
88  const TQString t = s.lower();
89  for ( unsigned int i = 0 ; i < numCryptoMessageFormats ; ++i )
90  if ( t == cryptoMessageFormats[i].configName )
91  return cryptoMessageFormats[i].format;
92  return AutoFormat;
93 }
94 
95 unsigned int Kleo::stringListToCryptoMessageFormats( const TQStringList & sl ) {
96  unsigned int result = 0;
97  for ( TQStringList::const_iterator it = sl.begin() ; it != sl.end() ; ++it )
98  result |= stringToCryptoMessageFormat( *it );
99  return result;
100 }
101 
102 // For the config values used below, see also kaddressbook/editors/cryptowidget.cpp
103 
104 const char* Kleo::encryptionPreferenceToString( EncryptionPreference pref )
105 {
106  switch( pref ) {
107  case UnknownPreference:
108  return 0;
109  case NeverEncrypt:
110  return "never";
111  case AlwaysEncrypt:
112  return "always";
113  case AlwaysEncryptIfPossible:
114  return "alwaysIfPossible";
115  case AlwaysAskForEncryption:
116  return "askAlways";
117  case AskWheneverPossible:
118  return "askWhenPossible";
119  }
120  return 0; // keep the compiler happy
121 }
122 
123 Kleo::EncryptionPreference Kleo::stringToEncryptionPreference( const TQString& str )
124 {
125  if ( str == "never" )
126  return NeverEncrypt;
127  if ( str == "always" )
128  return AlwaysEncrypt;
129  if ( str == "alwaysIfPossible" )
130  return AlwaysEncryptIfPossible;
131  if ( str == "askAlways" )
132  return AlwaysAskForEncryption;
133  if ( str == "askWhenPossible" )
134  return AskWheneverPossible;
135  return UnknownPreference;
136 }
137 
138 TQString Kleo::encryptionPreferenceToLabel( EncryptionPreference pref )
139 {
140  switch( pref ) {
141  case NeverEncrypt:
142  return i18n( "Never Encrypt" );
143  case AlwaysEncrypt:
144  return i18n( "Always Encrypt" );
145  case AlwaysEncryptIfPossible:
146  return i18n( "Always Encrypt If Possible" );
147  case AlwaysAskForEncryption:
148  return i18n( "Ask" );
149  case AskWheneverPossible:
150  return i18n( "Ask Whenever Possible" );
151  default:
152  return i18n( "no specific preference", "<none>" );
153  }
154 }
155 
156 const char* Kleo::signingPreferenceToString( SigningPreference pref )
157 {
158  switch( pref ) {
159  case UnknownSigningPreference:
160  return 0;
161  case NeverSign:
162  return "never";
163  case AlwaysSign:
164  return "always";
165  case AlwaysSignIfPossible:
166  return "alwaysIfPossible";
167  case AlwaysAskForSigning:
168  return "askAlways";
169  case AskSigningWheneverPossible:
170  return "askWhenPossible";
171  }
172  return 0; // keep the compiler happy
173 }
174 
175 Kleo::SigningPreference Kleo::stringToSigningPreference( const TQString& str )
176 {
177  if ( str == "never" )
178  return NeverSign;
179  if ( str == "always" )
180  return AlwaysSign;
181  if ( str == "alwaysIfPossible" )
182  return AlwaysSignIfPossible;
183  if ( str == "askAlways" )
184  return AlwaysAskForSigning;
185  if ( str == "askWhenPossible" )
186  return AskSigningWheneverPossible;
187  return UnknownSigningPreference;
188 }
189 
190 TQString Kleo::signingPreferenceToLabel( SigningPreference pref )
191 {
192  switch( pref ) {
193  case NeverSign:
194  return i18n( "Never Sign" );
195  case AlwaysSign:
196  return i18n( "Always Sign" );
197  case AlwaysSignIfPossible:
198  return i18n( "Always Sign If Possible" );
199  case AlwaysAskForSigning:
200  return i18n( "Ask" );
201  case AskSigningWheneverPossible:
202  return i18n( "Ask Whenever Possible" );
203  default:
204  return i18n( "no specific preference", "<none>" );
205  }
206 }