1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
|
/*
This file is part of libkabc.
Copyright (c) 2003 Helge Deller <deller@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
/*
Useful links:
- http://tldp.org/HOWTO/LDAP-Implementation-HOWTO/schemas.html
- http://www.faqs.org/rfcs/rfc2849.html
Not yet handled items:
- objectclass microsoftaddressbook
- info,
- initials,
- otherfacsimiletelephonenumber,
- otherpager,
- physicaldeliveryofficename,
*/
#include <tqstring.h>
#include <tqstringlist.h>
#include <tqregexp.h>
#include <tqtextstream.h>
#include <klocale.h>
#include <kdebug.h>
#include <kmdcodec.h>
#include "addressee.h"
#include "address.h"
#include "ldif.h"
#include "ldifconverter.h"
#include "vcardconverter.h"
using namespace KABC;
/* generate LDIF stream */
bool LDIFConverter::addresseeToLDIF( const AddresseeList &addrList, TQString &str )
{
AddresseeList::ConstIterator it;
for ( it = addrList.begin(); it != addrList.end(); ++it ) {
addresseeToLDIF( *it, str );
}
return true;
}
static void ldif_out( TQTextStream &t, TQString formatStr, TQString value )
{
if ( value.isEmpty() )
return;
TQCString txt = LDIF::assembleLine( formatStr, value, 72 );
// write the string
t << TQString::fromUtf8(txt) << "\n";
}
bool LDIFConverter::addresseeToLDIF( const Addressee &addr, TQString &str )
{
if ( addr.isEmpty() )
return false;
TQTextStream t( str, IO_WriteOnly|IO_Append );
t.setEncoding( TQTextStream::UnicodeUTF8 );
const Address homeAddr = addr.address( Address::Home );
const Address workAddr = addr.address( Address::Work );
ldif_out( t, "dn", TQString( "cn=%1,mail=%2" )
.arg( addr.formattedName().simplifyWhiteSpace() )
.arg( addr.preferredEmail() ) );
ldif_out( t, "givenname", addr.givenName() );
ldif_out( t, "sn", addr.familyName() );
ldif_out( t, "cn", addr.formattedName().simplifyWhiteSpace() );
ldif_out( t, "uid", addr.uid() );
ldif_out( t, "nickname", addr.nickName() );
ldif_out( t, "xmozillanickname", addr.nickName() );
ldif_out( t, "mail", addr.preferredEmail() );
if ( addr.emails().count() > 1 )
ldif_out( t, "mozillasecondemail", addr.emails()[ 1 ] );
//ldif_out( t, "mozilla_AIMScreenName: %1\n", "screen_name" );
ldif_out( t, "telephonenumber", addr.phoneNumber( PhoneNumber::Work ).number() );
ldif_out( t, "facsimiletelephonenumber", addr.phoneNumber( PhoneNumber::Fax ).number() );
ldif_out( t, "homephone", addr.phoneNumber( PhoneNumber::Home ).number() );
ldif_out( t, "mobile", addr.phoneNumber( PhoneNumber::Cell ).number() ); // Netscape 7
ldif_out( t, "cellphone", addr.phoneNumber( PhoneNumber::Cell ).number() ); // Netscape 4.x
ldif_out( t, "pager", addr.phoneNumber( PhoneNumber::Pager ).number() );
ldif_out( t, "pagerphone", addr.phoneNumber( PhoneNumber::Pager ).number() );
ldif_out( t, "streethomeaddress", homeAddr.street() );
ldif_out( t, "postalcode", workAddr.postalCode() );
ldif_out( t, "postofficebox", workAddr.postOfficeBox() );
TQStringList streets = TQStringList::split( '\n', homeAddr.street() );
if ( streets.count() > 0 )
ldif_out( t, "homepostaladdress", streets[ 0 ] ); // Netscape 7
if ( streets.count() > 1 )
ldif_out( t, "mozillahomepostaladdress2", streets[ 1 ] ); // Netscape 7
ldif_out( t, "mozillahomelocalityname", homeAddr.locality() ); // Netscape 7
ldif_out( t, "mozillahomestate", homeAddr.region() );
ldif_out( t, "mozillahomepostalcode", homeAddr.postalCode() );
ldif_out( t, "mozillahomecountryname", Address::ISOtoCountry(homeAddr.country()) );
ldif_out( t, "locality", workAddr.locality() );
ldif_out( t, "streetaddress", workAddr.street() ); // Netscape 4.x
streets = TQStringList::split( '\n', workAddr.street() );
if ( streets.count() > 0 )
ldif_out( t, "postaladdress", streets[ 0 ] );
if ( streets.count() > 1 )
ldif_out( t, "mozillapostaladdress2", streets[ 1 ] );
ldif_out( t, "countryname", Address::ISOtoCountry(workAddr.country()) );
ldif_out( t, "l", workAddr.locality() );
ldif_out( t, "c", Address::ISOtoCountry(workAddr.country()) );
ldif_out( t, "st", workAddr.region() );
ldif_out( t, "title", addr.title() );
ldif_out( t, "vocation", addr.prefix() );
ldif_out( t, "ou", addr.role() );
ldif_out( t, "o", addr.organization() );
ldif_out( t, "organization", addr.organization() );
ldif_out( t, "organizationname", addr.organization() );
// Compatibility with older kabc versions.
if ( !addr.department().isEmpty() )
ldif_out( t, "department", addr.department() );
else
ldif_out( t, "department", addr.custom("KADDRESSBOOK", "X-Department") );
ldif_out( t, "workurl", addr.url().prettyURL() );
ldif_out( t, "homeurl", addr.url().prettyURL() );
ldif_out( t, "description", addr.note() );
if (addr.revision().isValid())
ldif_out(t, "modifytimestamp", dateToVCardString( addr.revision()) );
t << "objectclass: top\n";
t << "objectclass: person\n";
t << "objectclass: organizationalPerson\n";
t << "\n";
return true;
}
/* convert from LDIF stream */
bool LDIFConverter::LDIFToAddressee( const TQString &str, AddresseeList &addrList, TQDateTime dt )
{
if (str.isEmpty())
return true;
bool endldif = false, end = false;
LDIF ldif;
LDIF::ParseVal ret;
const char *latinstr = str.latin1();
int latinstrlen = tqstrlen( latinstr );
TQByteArray data;
Addressee a;
Address homeAddr, workAddr;
data.setRawData( latinstr, latinstrlen );
ldif.setLDIF( data );
if (!dt.isValid())
dt = TQDateTime::tqcurrentDateTime();
a.setRevision(dt);
homeAddr = Address( Address::Home );
workAddr = Address( Address::Work );
do {
ret = ldif.nextItem();
switch ( ret ) {
case LDIF::Item: {
TQString fieldname = ldif.attr().lower();
TQString value = TQString::fromUtf8( ldif.val(), ldif.val().size() );
evaluatePair( a, homeAddr, workAddr, fieldname, value );
break;
}
case LDIF::EndEntry:
// if the new address is not empty, append it
if ( !a.formattedName().isEmpty() || !a.name().isEmpty() ||
!a.familyName().isEmpty() ) {
if ( !homeAddr.isEmpty() )
a.insertAddress( homeAddr );
if ( !workAddr.isEmpty() )
a.insertAddress( workAddr );
addrList.append( a );
}
a = Addressee();
a.setRevision(dt);
homeAddr = Address( Address::Home );
workAddr = Address( Address::Work );
break;
case LDIF::MoreData: {
if ( endldif )
end = true;
else {
ldif.endLDIF();
endldif = true;
break;
}
}
default:
break;
}
} while ( !end );
data.resetRawData( latinstr, latinstrlen );
return true;
}
bool LDIFConverter::evaluatePair( Addressee &a, Address &homeAddr,
Address &workAddr,
TQString &fieldname, TQString &value )
{
if ( fieldname == TQString::tqfromLatin1( "dn" ) ) // ignore & return false!
return false;
if ( fieldname.startsWith("#") ) {
return true;
}
if ( fieldname.isEmpty() && !a.note().isEmpty() ) {
// some LDIF export filters are borken and add additional
// comments on stand-alone lines. Just add them to the notes for now.
a.setNote( a.note() + "\n" + value );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "givenname" ) ) {
a.setGivenName( value );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "xmozillanickname") ||
fieldname == TQString::tqfromLatin1( "nickname") ) {
a.setNickName( value );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "sn" ) ) {
a.setFamilyName( value );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "uid" ) ) {
a.setUid( value );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "mail" ) ||
fieldname == TQString::tqfromLatin1( "mozillasecondemail" ) ) { // mozilla
if ( a.emails().findIndex( value ) == -1 )
a.insertEmail( value );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "title" ) ) {
a.setTitle( value );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "vocation" ) ) {
a.setPrefix( value );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "cn" ) ) {
a.setFormattedName( value );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "o" ) ||
fieldname == TQString::tqfromLatin1( "organization" ) || // Exchange
fieldname == TQString::tqfromLatin1( "organizationname" ) ) { // Exchange
a.setOrganization( value );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "description" ) ) {
addComment:
if ( !a.note().isEmpty() )
a.setNote( a.note() + "\n" );
a.setNote( a.note() + value );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "custom1" ) ||
fieldname == TQString::tqfromLatin1( "custom2" ) ||
fieldname == TQString::tqfromLatin1( "custom3" ) ||
fieldname == TQString::tqfromLatin1( "custom4" ) ) {
goto addComment;
}
if ( fieldname == TQString::tqfromLatin1( "homeurl" ) ||
fieldname == TQString::tqfromLatin1( "workurl" ) ) {
if (a.url().isEmpty()) {
a.setUrl( KURL( value ) );
return true;
}
if ( a.url().prettyURL() == KURL(value).prettyURL() )
return true;
// TODO: current version of kabc only supports one URL.
// TODO: change this with KDE 4
}
if ( fieldname == TQString::tqfromLatin1( "homephone" ) ) {
a.insertPhoneNumber( PhoneNumber( value, PhoneNumber::Home ) );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "telephonenumber" ) ) {
a.insertPhoneNumber( PhoneNumber( value, PhoneNumber::Work ) );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "mobile" ) ) { // mozilla/Netscape 7
a.insertPhoneNumber( PhoneNumber( value, PhoneNumber::Cell ) );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "cellphone" ) ) {
a.insertPhoneNumber( PhoneNumber( value, PhoneNumber::Cell ) );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "pager" ) || // mozilla
fieldname == TQString::tqfromLatin1( "pagerphone" ) ) { // mozilla
a.insertPhoneNumber( PhoneNumber( value, PhoneNumber::Pager ) );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "facsimiletelephonenumber" ) ) {
a.insertPhoneNumber( PhoneNumber( value, PhoneNumber::Fax ) );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "xmozillaanyphone" ) ) { // mozilla
a.insertPhoneNumber( PhoneNumber( value, PhoneNumber::Work ) );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "street" ) ||
fieldname == TQString::tqfromLatin1( "streethomeaddress" ) ) {
homeAddr.setStreet( value );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "postaladdress" ) ) { // mozilla
workAddr.setStreet( value );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "mozillapostaladdress2" ) ) { // mozilla
workAddr.setStreet( workAddr.street() + TQString::tqfromLatin1( "\n" ) + value );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "postalcode" ) ) {
workAddr.setPostalCode( value );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "postofficebox" ) ) {
workAddr.setPostOfficeBox( value );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "homepostaladdress" ) ) { // Netscape 7
homeAddr.setStreet( value );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "mozillahomepostaladdress2" ) ) { // mozilla
homeAddr.setStreet( homeAddr.street() + TQString::tqfromLatin1( "\n" ) + value );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "mozillahomelocalityname" ) ) { // mozilla
homeAddr.setLocality( value );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "mozillahomestate" ) ) { // mozilla
homeAddr.setRegion( value );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "mozillahomepostalcode" ) ) { // mozilla
homeAddr.setPostalCode( value );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "mozillahomecountryname" ) ) { // mozilla
if ( value.length() <= 2 )
value = Address::ISOtoCountry(value);
homeAddr.setCountry( value );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "locality" ) ) {
workAddr.setLocality( value );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "streetaddress" ) ) { // Netscape 4.x
workAddr.setStreet( value );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "countryname" ) ||
fieldname == TQString::tqfromLatin1( "c" ) ) { // mozilla
if ( value.length() <= 2 )
value = Address::ISOtoCountry(value);
workAddr.setCountry( value );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "l" ) ) { // mozilla
workAddr.setLocality( value );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "st" ) ) {
workAddr.setRegion( value );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "ou" ) ) {
a.setRole( value );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "department" ) ) {
a.setDepartment( value );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "member" ) ) {
// this is a mozilla list member (cn=xxx, mail=yyy)
TQStringList list( TQStringList::split( ',', value ) );
TQString name, email;
TQStringList::Iterator it;
for ( it = list.begin(); it != list.end(); ++it ) {
if ( (*it).startsWith( "cn=" ) )
name = (*it).mid( 3 ).stripWhiteSpace();
if ( (*it).startsWith( "mail=" ) )
email = (*it).mid( 5 ).stripWhiteSpace();
}
if ( !name.isEmpty() && !email.isEmpty() )
email = " <" + email + ">";
a.insertEmail( name + email );
a.insertCategory( i18n( "List of Emails" ) );
return true;
}
if ( fieldname == TQString::tqfromLatin1( "modifytimestamp" ) ) {
if (value == TQString::tqfromLatin1("0Z")) // ignore
return true;
TQDateTime dt = VCardStringToDate( value );
if ( dt.isValid() ) {
a.setRevision(dt);
return true;
}
}
if ( fieldname == TQString::tqfromLatin1( "objectclass" ) ) // ignore
return true;
kdWarning() << TQString("LDIFConverter: Unknown field for '%1': '%2=%3'\n")
.arg(a.formattedName()).arg(fieldname).arg(value);
return true;
}
/* The following functions are obsoleted. Similar functionality can be found
* in the LDIF class */
bool LDIFConverter::parseSingleLine( Addressee &a, Address &homeAddr,
Address &workAddr, TQString &line )
{
if ( line.isEmpty() )
return true;
TQString fieldname, value;
TQByteArray val;
LDIF::splitLine( line.latin1(), fieldname, val );
value = TQString::fromUtf8( val.data(), val.size() );
return evaluatePair( a, homeAddr, workAddr, fieldname, value);
}
bool LDIFConverter::splitLine( TQString &line, TQString &fieldname, TQString &value)
{
TQByteArray val;
bool ret = LDIF::splitLine( line.latin1(), fieldname, val );
value = TQString::fromUtf8( val.data(), val.size() );
return ret;
}
TQString LDIFConverter::makeLDIFfieldString( TQString formatStr, TQString value, bool allowEncode )
{
if ( value.isEmpty() )
return TQString();
// append format if not given
if (formatStr.tqfind(':') == -1)
formatStr.append(": %1\n");
// check if base64-encoding is needed
bool printable = true;
unsigned int i, len;
len = value.length();
for (i = 0; i<len; ++i ) {
if (!value[i].isPrint()) {
printable = false;
break;
}
}
if (printable) // always encode if we find special chars...
printable = (value.tqfind('\n') == -1);
if (!printable && allowEncode) {
// encode to base64
value = KCodecs::base64Encode( value.utf8() );
int p = formatStr.tqfind(':');
if (p>=0)
formatStr.insert(p, ':');
}
// generate the new string and split it to 72 chars/line
TQCString txt = (formatStr.arg(value)).utf8();
if (allowEncode) {
len = txt.length();
if (len && txt[len-1] == '\n')
--len;
i = 72;
while (i < len) {
txt.insert(i, "\n ");
i += 72+1;
len += 2;
}
}
return TQString::fromUtf8(txt);
}
|