40 #include "ui/dnattributeorderconfigwidget.h"
42 #include <tdeapplication.h>
43 #include <tdeconfig.h>
44 #include <tdelocale.h>
46 #include <tqstringlist.h>
47 #include <tqvaluevector.h>
58 struct Kleo::DN::Private {
59 Private() : mRefCount( 0 ) {}
60 Private(
const Private & other )
61 : attributes( other.attributes ),
62 reorderedAttributes( other.reorderedAttributes ),
73 if ( --mRefCount <= 0 ) {
80 int refCount()
const {
return mRefCount; }
82 DN::Attribute::List attributes;
83 DN::Attribute::List reorderedAttributes;
97 #define digitp(p) (*(p) >= '0' && *(p) <= '9')
98 #define hexdigitp(a) (digitp (a) \
99 || (*(a) >= 'A' && *(a) <= 'F') \
100 || (*(a) >= 'a' && *(a) <= 'f'))
101 #define xtoi_1(p) (*(p) <= '9'? (*(p)- '0'): \
102 *(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
103 #define xtoi_2(p) ((xtoi_1(p) * 16) + xtoi_1((p)+1))
106 trim_trailing_spaces(
char *
string )
110 for( mark = NULL, p =
string; *p; p++ ) {
111 if( isspace( *p ) ) {
127 static const unsigned char *
128 parse_dn_part (DnPair *array,
const unsigned char *
string)
130 const unsigned char *s, *s1;
135 for (s =
string+1; *s && *s !=
'='; s++)
142 p = (
char*)malloc (n+1);
145 memcpy (p,
string, n);
147 trim_trailing_spaces ((
char*)p);
149 for (
unsigned int i = 0 ; i < numOidMaps ; ++i )
150 if ( !strcasecmp ((
char*)p, oidmap[i].oid) ) {
152 p = strdup( oidmap[i].name );
161 for (s=
string; hexdigitp (s); s++)
167 array->value = p = (
char*)malloc (n+1);
170 for (s1=
string; n; s1 += 2, n--)
176 for (n=0, s=
string; *s; s++)
181 if (*s ==
',' || *s ==
'=' || *s ==
'+'
182 || *s ==
'<' || *s ==
'>' || *s ==
'#' || *s ==
';'
183 || *s ==
'\\' || *s ==
'\"' || *s ==
' ')
185 else if (hexdigitp (s) && hexdigitp (s+1))
195 else if (*s ==
',' || *s ==
'=' || *s ==
'+'
196 || *s ==
'<' || *s ==
'>' || *s ==
'#' || *s ==
';' )
202 array->value = p = (
char*)malloc (n+1);
205 for (s=
string; n; s++, n--)
230 static Kleo::DN::Attribute::List
231 parse_dn(
const unsigned char *
string ) {
233 return TQValueVector<Kleo::DN::Attribute>();
235 TQValueVector<Kleo::DN::Attribute> result;
238 while (*
string ==
' ')
243 DnPair pair = { 0, 0 };
244 string = parse_dn_part (&pair,
string);
247 if ( pair.key && pair.value )
248 result.push_back( Kleo::DN::Attribute( TQString::fromUtf8( pair.key ),
249 TQString::fromUtf8( pair.value ) ) );
253 while (*
string ==
' ')
255 if (*
string && *
string !=
',' && *
string !=
';' && *
string !=
'+')
263 return TQValueVector<Kleo::DN::Attribute>();
266 static TQValueVector<Kleo::DN::Attribute>
267 parse_dn(
const TQString & dn ) {
268 return parse_dn( (
const unsigned char*)dn.utf8().data() );
271 static TQString dn_escape(
const TQString & s ) {
273 for (
unsigned int i = 0, end = s.length() ; i != end ; ++i ) {
274 const TQChar ch = s[i];
275 switch ( ch.unicode() ) {
293 serialise(
const TQValueVector<Kleo::DN::Attribute> & dn ) {
295 for ( TQValueVector<Kleo::DN::Attribute>::const_iterator it = dn.begin() ; it != dn.end() ; ++it )
296 if ( !(*it).name().isEmpty() && !(*it).value().isEmpty() )
297 result.push_back( (*it).name().stripWhiteSpace() +
'=' + dn_escape( (*it).value().stripWhiteSpace() ) );
298 return result.join(
"," );
301 static Kleo::DN::Attribute::List
302 reorder_dn(
const Kleo::DN::Attribute::List & dn ) {
303 const TQStringList & attrOrder = Kleo::DNAttributeMapper::instance()->attributeOrder();
305 Kleo::DN::Attribute::List unknownEntries;
306 Kleo::DN::Attribute::List result;
307 unknownEntries.reserve( dn.size() );
308 result.reserve( dn.size() );
311 for ( Kleo::DN::const_iterator it = dn.begin(); it != dn.end(); ++it )
312 if ( attrOrder.find( (*it).name() ) == attrOrder.end() )
313 unknownEntries.push_back( *it );
316 for ( TQStringList::const_iterator oit = attrOrder.begin() ; oit != attrOrder.end() ; ++oit )
317 if ( *oit ==
"_X_" ) {
319 std::copy( unknownEntries.begin(), unknownEntries.end(),
320 std::back_inserter( result ) );
321 unknownEntries.clear();
323 for ( Kleo::DN::const_iterator dnit = dn.begin() ; dnit != dn.end() ; ++dnit )
324 if ( (*dnit).name() == *oit )
325 result.push_back( *dnit );
342 Kleo::DN::DN(
const TQString & dn ) {
345 d->attributes = parse_dn( dn );
348 Kleo::DN::DN(
const char * utf8DN ) {
352 d->attributes = parse_dn( (
const unsigned char*)utf8DN );
355 Kleo::DN::DN(
const DN & other )
365 const Kleo::DN & Kleo::DN::operator=(
const DN & that ) {
366 if ( this->d == that.d )
382 if ( d->reorderedAttributes.empty() )
383 d->reorderedAttributes = reorder_dn( d->attributes );
384 return serialise( d->reorderedAttributes );
388 return d ? serialise( d->attributes ) : TQString() ;
393 return dn_escape( value );
396 void Kleo::DN::detach() {
398 d =
new Kleo::DN::Private();
400 }
else if ( d->refCount() > 1 ) {
401 Kleo::DN::Private * d_save = d;
402 d =
new Kleo::DN::Private( *d );
408 void Kleo::DN::append(
const Attribute & attr ) {
410 d->attributes.push_back( attr );
411 d->reorderedAttributes.clear();
414 TQString Kleo::DN::operator[](
const TQString & attr )
const {
417 const TQString attrUpper = attr.upper();
418 for ( TQValueVector<Attribute>::const_iterator it = d->attributes.begin() ;
419 it != d->attributes.end() ; ++it )
420 if ( (*it).name() == attrUpper )
421 return (*it).value();
425 static TQValueVector<Kleo::DN::Attribute> empty;
427 Kleo::DN::const_iterator Kleo::DN::begin()
const {
428 return d ? d->attributes.begin() : empty.begin() ;
431 Kleo::DN::const_iterator Kleo::DN::end()
const {
432 return d ? d->attributes.end() : empty.end() ;
440 bool operator()(
const char * s1,
const char * s2 )
const {
441 return qstrcmp( s1, s2 ) < 0 ;
446 static const char * defaultOrder[] = {
447 "CN",
"L",
"_X_",
"OU",
"O",
"C"
450 std::pair<const char*,const char*> attributeLabels[] = {
451 #define MAKE_PAIR(x,y) std::pair<const char*,const char*>( x, y )
452 MAKE_PAIR(
"CN", I18N_NOOP(
"Common name") ),
453 MAKE_PAIR(
"SN", I18N_NOOP(
"Surname") ),
454 MAKE_PAIR(
"GN", I18N_NOOP(
"Given name") ),
455 MAKE_PAIR(
"L", I18N_NOOP(
"Location") ),
456 MAKE_PAIR(
"T", I18N_NOOP(
"Title") ),
457 MAKE_PAIR(
"OU", I18N_NOOP(
"Organizational unit") ),
458 MAKE_PAIR(
"O", I18N_NOOP(
"Organization") ),
459 MAKE_PAIR(
"PC", I18N_NOOP(
"Postal code") ),
460 MAKE_PAIR(
"C", I18N_NOOP(
"Country code") ),
461 MAKE_PAIR(
"SP", I18N_NOOP(
"State or province") ),
462 MAKE_PAIR(
"DC", I18N_NOOP(
"Domain component") ),
463 MAKE_PAIR(
"BC", I18N_NOOP(
"Business category") ),
464 MAKE_PAIR(
"EMAIL", I18N_NOOP(
"Email address") ),
465 MAKE_PAIR(
"MAIL", I18N_NOOP(
"Mail address") ),
466 MAKE_PAIR(
"MOBILE", I18N_NOOP(
"Mobile phone number") ),
467 MAKE_PAIR(
"TEL", I18N_NOOP(
"Telephone number") ),
468 MAKE_PAIR(
"FAX", I18N_NOOP(
"Fax number") ),
469 MAKE_PAIR(
"STREET", I18N_NOOP(
"Street address") ),
470 MAKE_PAIR(
"UID", I18N_NOOP(
"Unique ID") )
473 static const unsigned int numAttributeLabels =
sizeof attributeLabels /
sizeof *attributeLabels ;
475 class Kleo::DNAttributeMapper::Private {
478 std::map<const char*,const char*,ltstr> map;
479 TQStringList attributeOrder;
482 Kleo::DNAttributeMapper::Private::Private()
483 : map( attributeLabels, attributeLabels + numAttributeLabels ) {}
485 Kleo::DNAttributeMapper::DNAttributeMapper() {
487 const TDEConfigGroup config( kapp->config(),
"DN" );
488 d->attributeOrder = config.readListEntry(
"AttributeOrder" );
489 if ( d->attributeOrder.empty() )
490 std::copy( defaultOrder, defaultOrder +
sizeof defaultOrder /
sizeof *defaultOrder,
491 std::back_inserter( d->attributeOrder ) );
495 Kleo::DNAttributeMapper::~DNAttributeMapper() {
504 (void)
new DNAttributeMapper();
508 TQString Kleo::DNAttributeMapper::name2label(
const TQString & s )
const {
509 const std::map<const char*,const char*,ltstr>::const_iterator it
510 = d->map.find( s.stripWhiteSpace().upper().latin1() );
511 if ( it == d->map.end() )
513 return i18n( it->second );
516 TQStringList Kleo::DNAttributeMapper::names()
const {
518 for ( std::map<const char*,const char*,ltstr>::const_iterator it = d->map.begin() ; it != d->map.end() ; ++it )
519 result.push_back( it->first );
523 const TQStringList & Kleo::DNAttributeMapper::attributeOrder()
const {
524 return d->attributeOrder;
527 void Kleo::DNAttributeMapper::setAttributeOrder(
const TQStringList & order ) {
528 d->attributeOrder = order;
530 std::copy( defaultOrder, defaultOrder +
sizeof defaultOrder /
sizeof *defaultOrder,
531 std::back_inserter( d->attributeOrder ) );
532 TDEConfigGroup config( kapp->config(),
"DN" );
533 config.writeEntry(
"AttributeOrder", order );
536 Kleo::DNAttributeOrderConfigWidget * Kleo::DNAttributeMapper::configWidget( TQWidget * parent,
const char * name )
const {
537 return new DNAttributeOrderConfigWidget( mSelf, parent, name );
static TQString escape(const TQString &value)
TQString prettyDN() const