00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <qdatastream.h>
00022 #include <qstring.h>
00023
00024 #include "agent.h"
00025 #include "key.h"
00026 #include "picture.h"
00027 #include "secrecy.h"
00028 #include "sound.h"
00029
00030 #include "vcardtool.h"
00031
00032 using namespace KABC;
00033
00034 VCardTool::VCardTool()
00035 {
00036 mAddressTypeMap.insert( "dom", Address::Dom );
00037 mAddressTypeMap.insert( "intl", Address::Intl );
00038 mAddressTypeMap.insert( "postal", Address::Postal );
00039 mAddressTypeMap.insert( "parcel", Address::Parcel );
00040 mAddressTypeMap.insert( "home", Address::Home );
00041 mAddressTypeMap.insert( "work", Address::Work );
00042 mAddressTypeMap.insert( "pref", Address::Pref );
00043
00044 mPhoneTypeMap.insert( "HOME", PhoneNumber::Home );
00045 mPhoneTypeMap.insert( "WORK", PhoneNumber::Work );
00046 mPhoneTypeMap.insert( "MSG", PhoneNumber::Msg );
00047 mPhoneTypeMap.insert( "PREF", PhoneNumber::Pref );
00048 mPhoneTypeMap.insert( "VOICE", PhoneNumber::Voice );
00049 mPhoneTypeMap.insert( "FAX", PhoneNumber::Fax );
00050 mPhoneTypeMap.insert( "CELL", PhoneNumber::Cell );
00051 mPhoneTypeMap.insert( "VIDEO", PhoneNumber::Video );
00052 mPhoneTypeMap.insert( "BBS", PhoneNumber::Bbs );
00053 mPhoneTypeMap.insert( "MODEM", PhoneNumber::Modem );
00054 mPhoneTypeMap.insert( "CAR", PhoneNumber::Car );
00055 mPhoneTypeMap.insert( "ISDN", PhoneNumber::Isdn );
00056 mPhoneTypeMap.insert( "PCS", PhoneNumber::Pcs );
00057 mPhoneTypeMap.insert( "PAGER", PhoneNumber::Pager );
00058 }
00059
00060 VCardTool::~VCardTool()
00061 {
00062 }
00063
00064 QString VCardTool::createVCards( Addressee::List list, VCard::Version version )
00065 {
00066 VCard::List vCardList;
00067
00068 Addressee::List::Iterator addrIt;
00069 for ( addrIt = list.begin(); addrIt != list.end(); ++addrIt ) {
00070 VCard card;
00071 QStringList::ConstIterator strIt;
00072
00073
00074 Address::List addresses = (*addrIt).addresses();
00075 for ( Address::List::Iterator it = addresses.begin(); it != addresses.end(); ++it ) {
00076 QStringList address;
00077
00078 bool isEmpty = ( (*it).postOfficeBox().isEmpty() &&
00079 (*it).extended().isEmpty() &&
00080 (*it).street().isEmpty() &&
00081 (*it).locality().isEmpty() &&
00082 (*it).region().isEmpty() &&
00083 (*it).postalCode().isEmpty() &&
00084 (*it).country().isEmpty() );
00085
00086 address.append( (*it).postOfficeBox().replace( ';', "\\;" ) );
00087 address.append( (*it).extended().replace( ';', "\\;" ) );
00088 address.append( (*it).street().replace( ';', "\\;" ) );
00089 address.append( (*it).locality().replace( ';', "\\;" ) );
00090 address.append( (*it).region().replace( ';', "\\;" ) );
00091 address.append( (*it).postalCode().replace( ';', "\\;" ) );
00092 address.append( (*it).country().replace( ';', "\\;" ) );
00093
00094 VCardLine adrLine( "ADR", address.join( ";" ) );
00095 if ( version == VCard::v2_1 ) {
00096 adrLine.addParameter( "CHARSET", "UTF-8" );
00097 adrLine.addParameter( "ENCODING", "8BIT" );
00098 }
00099
00100 VCardLine labelLine( "LABEL", (*it).label() );
00101 if ( version == VCard::v2_1 ) {
00102 labelLine.addParameter( "CHARSET", "UTF-8" );
00103 labelLine.addParameter( "ENCODING", "8BIT" );
00104 }
00105
00106 bool hasLabel = !(*it).label().isEmpty();
00107 QMap<QString, int>::Iterator typeIt;
00108 for ( typeIt = mAddressTypeMap.begin(); typeIt != mAddressTypeMap.end(); ++typeIt ) {
00109 if ( typeIt.data() & (*it).type() ) {
00110 adrLine.addParameter( "TYPE", typeIt.key() );
00111 if ( hasLabel )
00112 labelLine.addParameter( "TYPE", typeIt.key() );
00113 }
00114 }
00115
00116 if ( !isEmpty )
00117 card.addLine( adrLine );
00118 if ( hasLabel )
00119 card.addLine( labelLine );
00120 }
00121
00122
00123 card.addLine( createAgent( version, (*addrIt).agent() ) );
00124
00125
00126 card.addLine( VCardLine( "BDAY", createDateTime( (*addrIt).birthday() ) ) );
00127
00128
00129 if ( version == VCard::v3_0 ) {
00130 QStringList categories = (*addrIt).categories();
00131 QStringList::Iterator catIt;
00132 for ( catIt = categories.begin(); catIt != categories.end(); ++catIt )
00133 (*catIt).replace( ',', "\\," );
00134
00135 VCardLine catLine( "CATEGORIES", categories.join( "," ) );
00136 if ( version == VCard::v2_1 ) {
00137 catLine.addParameter( "CHARSET", "UTF-8" );
00138 catLine.addParameter( "ENCODING", "8BIT" );
00139 }
00140
00141 card.addLine( catLine );
00142 }
00143
00144
00145 if ( version == VCard::v3_0 ) {
00146 card.addLine( createSecrecy( (*addrIt).secrecy() ) );
00147 }
00148
00149
00150 QStringList emails = (*addrIt).emails();
00151 bool pref = true;
00152 for ( strIt = emails.begin(); strIt != emails.end(); ++strIt ) {
00153 VCardLine line( "EMAIL", *strIt );
00154 if ( version == VCard::v2_1 ) {
00155 line.addParameter( "CHARSET", "UTF-8" );
00156 line.addParameter( "ENCODING", "8BIT" );
00157 }
00158 if ( pref == true && emails.count() > 1 ) {
00159 line.addParameter( "TYPE", "PREF" );
00160 pref = false;
00161 }
00162 card.addLine( line );
00163 }
00164
00165
00166 VCardLine fnLine( "FN", (*addrIt).formattedName() );
00167 if ( version == VCard::v2_1 ) {
00168 fnLine.addParameter( "CHARSET", "UTF-8" );
00169 fnLine.addParameter( "ENCODING", "8BIT" );
00170 }
00171 card.addLine( fnLine );
00172
00173
00174 Geo geo = (*addrIt).geo();
00175 if ( geo.isValid() ) {
00176 QString str;
00177 str.sprintf( "%.6f;%.6f", geo.latitude(), geo.longitude() );
00178 card.addLine( VCardLine( "GEO", str ) );
00179 }
00180
00181
00182 Key::List keys = (*addrIt).keys();
00183 Key::List::ConstIterator keyIt;
00184 for ( keyIt = keys.begin(); keyIt != keys.end(); ++keyIt )
00185 card.addLine( createKey( *keyIt ) );
00186
00187
00188 card.addLine( createPicture( "LOGO", (*addrIt).logo() ) );
00189
00190
00191 VCardLine mailerLine( "MAILER", (*addrIt).mailer() );
00192 if ( version == VCard::v2_1 ) {
00193 mailerLine.addParameter( "CHARSET", "UTF-8" );
00194 mailerLine.addParameter( "ENCODING", "8BIT" );
00195 }
00196 card.addLine( mailerLine );
00197
00198
00199 QStringList name;
00200 name.append( (*addrIt).familyName().replace( ';', "\\;" ) );
00201 name.append( (*addrIt).givenName().replace( ';', "\\;" ) );
00202 name.append( (*addrIt).additionalName().replace( ';', "\\;" ) );
00203 name.append( (*addrIt).prefix().replace( ';', "\\;" ) );
00204 name.append( (*addrIt).suffix().replace( ';', "\\;" ) );
00205
00206 VCardLine nLine( "N", name.join( ";" ) );
00207 if ( version == VCard::v2_1 ) {
00208 nLine.addParameter( "CHARSET", "UTF-8" );
00209 nLine.addParameter( "ENCODING", "8BIT" );
00210 }
00211 card.addLine( nLine );
00212
00213
00214 VCardLine nameLine( "NAME", (*addrIt).name() );
00215 if ( version == VCard::v2_1 ) {
00216 nameLine.addParameter( "CHARSET", "UTF-8" );
00217 nameLine.addParameter( "ENCODING", "8BIT" );
00218 }
00219 card.addLine( nameLine );
00220
00221
00222 if ( version == VCard::v3_0 )
00223 card.addLine( VCardLine( "NICKNAME", (*addrIt).nickName() ) );
00224
00225
00226 VCardLine noteLine( "NOTE", (*addrIt).note() );
00227 if ( version == VCard::v2_1 ) {
00228 noteLine.addParameter( "CHARSET", "UTF-8" );
00229 noteLine.addParameter( "ENCODING", "8BIT" );
00230 }
00231 card.addLine( noteLine );
00232
00233
00234 VCardLine orgLine( "ORG", (*addrIt).organization() );
00235 if ( version == VCard::v2_1 ) {
00236 orgLine.addParameter( "CHARSET", "UTF-8" );
00237 orgLine.addParameter( "ENCODING", "8BIT" );
00238 }
00239 card.addLine( orgLine );
00240
00241
00242 card.addLine( createPicture( "PHOTO", (*addrIt).photo() ) );
00243
00244
00245 if ( version == VCard::v3_0 )
00246 card.addLine( VCardLine( "PRODID", (*addrIt).productId() ) );
00247
00248
00249 card.addLine( VCardLine( "REV", createDateTime( (*addrIt).revision() ) ) );
00250
00251
00252 VCardLine roleLine( "ROLE", (*addrIt).role() );
00253 if ( version == VCard::v2_1 ) {
00254 roleLine.addParameter( "CHARSET", "UTF-8" );
00255 roleLine.addParameter( "ENCODING", "8BIT" );
00256 }
00257 card.addLine( roleLine );
00258
00259
00260 if ( version == VCard::v3_0 )
00261 card.addLine( VCardLine( "SORT-STRING", (*addrIt).sortString() ) );
00262
00263
00264 card.addLine( createSound( (*addrIt).sound() ) );
00265
00266
00267 PhoneNumber::List phoneNumbers = (*addrIt).phoneNumbers();
00268 PhoneNumber::List::ConstIterator phoneIt;
00269 for ( phoneIt = phoneNumbers.begin(); phoneIt != phoneNumbers.end(); ++phoneIt ) {
00270 VCardLine line( "TEL", (*phoneIt).number() );
00271
00272 QMap<QString, int>::Iterator typeIt;
00273 for ( typeIt = mPhoneTypeMap.begin(); typeIt != mPhoneTypeMap.end(); ++typeIt ) {
00274 if ( typeIt.data() & (*phoneIt).type() )
00275 line.addParameter( "TYPE", typeIt.key() );
00276 }
00277
00278 card.addLine( line );
00279 }
00280
00281
00282 VCardLine titleLine( "TITLE", (*addrIt).title() );
00283 if ( version == VCard::v2_1 ) {
00284 titleLine.addParameter( "CHARSET", "UTF-8" );
00285 titleLine.addParameter( "ENCODING", "8BIT" );
00286 }
00287 card.addLine( titleLine );
00288
00289
00290 TimeZone timeZone = (*addrIt).timeZone();
00291 if ( timeZone.isValid() ) {
00292 QString str;
00293
00294 int neg = 1;
00295 if ( timeZone.offset() < 0 )
00296 neg = -1;
00297
00298 str.sprintf( "%c%02d:%02d", ( timeZone.offset() >= 0 ? '+' : '-' ),
00299 ( timeZone.offset() / 60 ) * neg,
00300 ( timeZone.offset() % 60 ) * neg );
00301
00302 card.addLine( VCardLine( "TZ", str ) );
00303 }
00304
00305
00306 card.addLine( VCardLine( "UID", (*addrIt).uid() ) );
00307
00308
00309 card.addLine( VCardLine( "URL", (*addrIt).url().url() ) );
00310
00311
00312 if ( version == VCard::v2_1 )
00313 card.addLine( VCardLine( "VERSION", "2.1" ) );
00314 if ( version == VCard::v3_0 )
00315 card.addLine( VCardLine( "VERSION", "3.0" ) );
00316
00317
00318 QStringList customs = (*addrIt).customs();
00319 for ( strIt = customs.begin(); strIt != customs.end(); ++strIt ) {
00320 QString identifier = "X-" + (*strIt).left( (*strIt).find( ":" ) );
00321 QString value = (*strIt).mid( (*strIt).find( ":" ) + 1 );
00322 if ( value.isEmpty() )
00323 continue;
00324
00325 VCardLine line( identifier, value );
00326 if ( version == VCard::v2_1 ) {
00327 line.addParameter( "CHARSET", "UTF-8" );
00328 line.addParameter( "ENCODING", "8BIT" );
00329 }
00330 card.addLine( line );
00331 }
00332
00333 vCardList.append( card );
00334 }
00335
00336 return VCardParser::createVCards( vCardList );
00337 }
00338
00339 Addressee::List VCardTool::parseVCards( const QString& vcard )
00340 {
00341 QChar semicolonSep( ';' );
00342 QChar commaSep( ',' );
00343 QString identifier;
00344
00345 Addressee::List addrList;
00346 VCard::List vCardList = VCardParser::parseVCards( vcard );
00347 VCard::List::Iterator cardIt;
00348 for ( cardIt = vCardList.begin(); cardIt != vCardList.end(); ++cardIt ) {
00349 Addressee addr;
00350 QStringList idents = (*cardIt).identifiers();
00351 QStringList::ConstIterator identIt;
00352 for ( identIt = idents.begin(); identIt != idents.end(); ++identIt ) {
00353 VCard card = (*cardIt);
00354 VCardLine::List lines = card.lines( (*identIt) );
00355 VCardLine::List::Iterator lineIt;
00356
00357
00358 for ( lineIt = lines.begin(); lineIt != lines.end(); ++lineIt ) {
00359 QStringList params = (*lineIt).parameterList();
00360
00361 identifier = (*lineIt).identifier().lower();
00362
00363 if ( identifier == "adr" ) {
00364 Address address;
00365 QStringList addrParts = splitString( semicolonSep, (*lineIt).value().asString() );
00366 if ( addrParts.count() > 0 )
00367 address.setPostOfficeBox( addrParts[ 0 ] );
00368 if ( addrParts.count() > 1 )
00369 address.setExtended( addrParts[ 1 ] );
00370 if ( addrParts.count() > 2 )
00371 address.setStreet( addrParts[ 2 ] );
00372 if ( addrParts.count() > 3 )
00373 address.setLocality( addrParts[ 3 ] );
00374 if ( addrParts.count() > 4 )
00375 address.setRegion( addrParts[ 4 ] );
00376 if ( addrParts.count() > 5 )
00377 address.setPostalCode( addrParts[ 5 ] );
00378 if ( addrParts.count() > 6 )
00379 address.setCountry( addrParts[ 6 ] );
00380
00381 int type = 0;
00382
00383 QStringList types = (*lineIt).parameters( "type" );
00384 for ( QStringList::Iterator it = types.begin(); it != types.end(); ++it )
00385 type += mAddressTypeMap[ (*it).lower() ];
00386
00387 address.setType( type );
00388 addr.insertAddress( address );
00389 }
00390
00391
00392 if ( identifier == "agent" )
00393 addr.setAgent( parseAgent( *lineIt ) );
00394
00395
00396 if ( identifier == "bday" )
00397 addr.setBirthday( parseDateTime( (*lineIt).value().asString() ) );
00398
00399
00400 if ( identifier == "categories" ) {
00401 QStringList categories = splitString( commaSep, (*lineIt).value().asString() );
00402 addr.setCategories( categories );
00403 }
00404
00405
00406 if ( identifier == "class" )
00407 addr.setSecrecy( parseSecrecy( *lineIt ) );
00408
00409
00410 if ( identifier == "email" ) {
00411 QStringList types = (*lineIt).parameters( "type" );
00412 addr.insertEmail( (*lineIt).value().asString(), types.findIndex( "PREF" ) != -1 );
00413 }
00414
00415
00416 if ( identifier == "fn" )
00417 addr.setFormattedName( (*lineIt).value().asString() );
00418
00419
00420 if ( identifier == "geo" ) {
00421 Geo geo;
00422
00423 QStringList geoParts = QStringList::split( ';', (*lineIt).value().asString(), true );
00424 geo.setLatitude( geoParts[ 0 ].toFloat() );
00425 geo.setLongitude( geoParts[ 1 ].toFloat() );
00426
00427 addr.setGeo( geo );
00428 }
00429
00430
00431 if ( identifier == "key" )
00432 addr.insertKey( parseKey( *lineIt ) );
00433
00434
00435 if ( identifier == "label" ) {
00436 int type = 0;
00437
00438 QStringList types = (*lineIt).parameters( "type" );
00439 for ( QStringList::Iterator it = types.begin(); it != types.end(); ++it )
00440 type += mAddressTypeMap[ (*it).lower() ];
00441
00442 bool available = false;
00443 KABC::Address::List addressList = addr.addresses();
00444 KABC::Address::List::Iterator it;
00445 for ( it = addressList.begin(); it != addressList.end(); ++it ) {
00446 if ( (*it).type() == type ) {
00447 (*it).setLabel( (*lineIt).value().asString() );
00448 addr.insertAddress( *it );
00449 available = true;
00450 break;
00451 }
00452 }
00453
00454 if ( !available ) {
00455 KABC::Address address( type );
00456 address.setLabel( (*lineIt).value().asString() );
00457 addr.insertAddress( address );
00458 }
00459 }
00460
00461
00462 if ( identifier == "logo" )
00463 addr.setLogo( parsePicture( *lineIt ) );
00464
00465
00466 if ( identifier == "mailer" )
00467 addr.setMailer( (*lineIt).value().asString() );
00468
00469
00470 if ( identifier == "n" ) {
00471 QStringList nameParts = splitString( semicolonSep, (*lineIt).value().asString() );
00472 if ( nameParts.count() > 0 )
00473 addr.setFamilyName( nameParts[ 0 ] );
00474 if ( nameParts.count() > 1 )
00475 addr.setGivenName( nameParts[ 1 ] );
00476 if ( nameParts.count() > 2 )
00477 addr.setAdditionalName( nameParts[ 2 ] );
00478 if ( nameParts.count() > 3 )
00479 addr.setPrefix( nameParts[ 3 ] );
00480 if ( nameParts.count() > 4 )
00481 addr.setSuffix( nameParts[ 4 ] );
00482 }
00483
00484
00485 if ( identifier == "nickname" )
00486 addr.setNickName( (*lineIt).value().asString() );
00487
00488
00489 if ( identifier == "note" )
00490 addr.setNote( (*lineIt).value().asString() );
00491
00492
00493 if ( identifier == "org" )
00494 addr.setOrganization( (*lineIt).value().asString() );
00495
00496
00497 if ( identifier == "photo" )
00498 addr.setPhoto( parsePicture( *lineIt ) );
00499
00500
00501 if ( identifier == "prodid" )
00502 addr.setProductId( (*lineIt).value().asString() );
00503
00504
00505 if ( identifier == "rev" )
00506 addr.setRevision( parseDateTime( (*lineIt).value().asString() ) );
00507
00508
00509 if ( identifier == "role" )
00510 addr.setRole( (*lineIt).value().asString() );
00511
00512
00513 if ( identifier == "sort-string" )
00514 addr.setSortString( (*lineIt).value().asString() );
00515
00516
00517 if ( identifier == "sound" )
00518 addr.setSound( parseSound( *lineIt ) );
00519
00520
00521 if ( identifier == "tel" ) {
00522 PhoneNumber phone;
00523 phone.setNumber( (*lineIt).value().asString() );
00524
00525 int type = 0;
00526
00527 QStringList types = (*lineIt).parameters( "type" );
00528 for ( QStringList::Iterator it = types.begin(); it != types.end(); ++it )
00529 type += mPhoneTypeMap[(*it).upper()];
00530
00531 phone.setType( type );
00532
00533 addr.insertPhoneNumber( phone );
00534 }
00535
00536
00537 if ( identifier == "title" )
00538 addr.setTitle( (*lineIt).value().asString() );
00539
00540
00541 if ( identifier == "tz" ) {
00542 TimeZone tz;
00543 QString date = (*lineIt).value().asString();
00544
00545 int hours = date.mid( 1, 2).toInt();
00546 int minutes = date.mid( 4, 2 ).toInt();
00547 int offset = ( hours * 60 ) + minutes;
00548 offset = offset * ( date[ 0 ] == '+' ? 1 : -1 );
00549
00550 tz.setOffset( offset );
00551 addr.setTimeZone( tz );
00552 }
00553
00554
00555 if ( identifier == "uid" )
00556 addr.setUid( (*lineIt).value().asString() );
00557
00558
00559 if ( identifier == "url" )
00560 addr.setUrl( KURL( (*lineIt).value().asString() ) );
00561
00562
00563 if ( identifier.startsWith( "x-" ) ) {
00564 QString key = (*lineIt).identifier().mid( 2 );
00565 int dash = key.find( "-" );
00566 addr.insertCustom( key.left( dash ), key.mid( dash + 1 ), (*lineIt).value().asString() );
00567 }
00568 }
00569 }
00570
00571 addrList.append( addr );
00572 }
00573
00574 return addrList;
00575 }
00576
00577 QDateTime VCardTool::parseDateTime( const QString &str )
00578 {
00579 QDateTime dateTime;
00580
00581 if ( str.find( '-' ) == -1 ) {
00582 dateTime.setDate( QDate( str.left( 4 ).toInt(), str.mid( 4, 2 ).toInt(),
00583 str.mid( 6, 2 ).toInt() ) );
00584
00585 if ( str.find( 'T' ) )
00586 dateTime.setTime( QTime( str.mid( 11, 2 ).toInt(), str.mid( 14, 2 ).toInt(),
00587 str.mid( 17, 2 ).toInt() ) );
00588
00589 } else {
00590 dateTime.setDate( QDate( str.left( 4 ).toInt(), str.mid( 5, 2 ).toInt(),
00591 str.mid( 8, 2 ).toInt() ) );
00592
00593 if ( str.find( 'T' ) )
00594 dateTime.setTime( QTime( str.mid( 11, 2 ).toInt(), str.mid( 14, 2 ).toInt(),
00595 str.mid( 17, 2 ).toInt() ) );
00596 }
00597
00598 return dateTime;
00599 }
00600
00601 QString VCardTool::createDateTime( const QDateTime &dateTime )
00602 {
00603 QString str;
00604
00605 if ( dateTime.date().isValid() ) {
00606 str.sprintf( "%4d-%02d-%02d", dateTime.date().year(), dateTime.date().month(),
00607 dateTime.date().day() );
00608 if ( dateTime.time().isValid() ) {
00609 QString tmp;
00610 tmp.sprintf( "T%02d:%02d:%02dZ", dateTime.time().hour(), dateTime.time().minute(),
00611 dateTime.time().second() );
00612 str += tmp;
00613 }
00614 }
00615
00616 return str;
00617 }
00618
00619 Picture VCardTool::parsePicture( const VCardLine &line )
00620 {
00621 Picture pic;
00622
00623 QStringList params = line.parameterList();
00624 if ( params.findIndex( "encoding" ) != -1 ) {
00625 QImage img;
00626 img.loadFromData( line.value().asByteArray() );
00627 pic.setData( img );
00628 } else if ( params.findIndex( "value" ) != -1 ) {
00629 if ( line.parameter( "value" ).lower() == "uri" )
00630 pic.setUrl( line.value().asString() );
00631 }
00632
00633 if ( params.findIndex( "type" ) != -1 )
00634 pic.setType( line.parameter( "type" ) );
00635
00636 return pic;
00637 }
00638
00639 VCardLine VCardTool::createPicture( const QString &identifier, const Picture &pic )
00640 {
00641 VCardLine line( identifier );
00642
00643 if ( pic.isIntern() ) {
00644 if ( !pic.data().isNull() ) {
00645 QByteArray input;
00646 QDataStream s( input, IO_WriteOnly );
00647 s.setVersion( 4 );
00648 s << pic.data();
00649 line.setValue( input );
00650 line.addParameter( "encoding", "b" );
00651 line.addParameter( "type", "image/png" );
00652 }
00653 } else if ( !pic.url().isEmpty() ) {
00654 line.setValue( pic.url() );
00655 line.addParameter( "value", "URI" );
00656 }
00657
00658 return line;
00659 }
00660
00661 Sound VCardTool::parseSound( const VCardLine &line )
00662 {
00663 Sound snd;
00664
00665 QStringList params = line.parameterList();
00666 if ( params.findIndex( "encoding" ) != -1 )
00667 snd.setData( line.value().asByteArray() );
00668 else if ( params.findIndex( "value" ) != -1 ) {
00669 if ( line.parameter( "value" ).lower() == "uri" )
00670 snd.setUrl( line.value().asString() );
00671 }
00672
00673
00674
00675
00676
00677
00678 return snd;
00679 }
00680
00681 VCardLine VCardTool::createSound( const Sound &snd )
00682 {
00683 VCardLine line( "SOUND" );
00684
00685 if ( snd.isIntern() ) {
00686 if ( !snd.data().isEmpty() ) {
00687 line.setValue( snd.data() );
00688 line.addParameter( "encoding", "b" );
00689
00690 }
00691 } else if ( !snd.url().isEmpty() ) {
00692 line.setValue( snd.url() );
00693 line.addParameter( "value", "URI" );
00694 }
00695
00696 return line;
00697 }
00698
00699 Key VCardTool::parseKey( const VCardLine &line )
00700 {
00701 Key key;
00702
00703 QStringList params = line.parameterList();
00704 if ( params.findIndex( "encoding" ) != -1 )
00705 key.setBinaryData( line.value().asByteArray() );
00706 else
00707 key.setTextData( line.value().asString() );
00708
00709 if ( params.findIndex( "type" ) != -1 ) {
00710 if ( line.parameter( "type" ).lower() == "x509" )
00711 key.setType( Key::X509 );
00712 else if ( line.parameter( "type" ).lower() == "pgp" )
00713 key.setType( Key::PGP );
00714 else {
00715 key.setType( Key::Custom );
00716 key.setCustomTypeString( line.parameter( "type" ) );
00717 }
00718 }
00719
00720 return key;
00721 }
00722
00723 VCardLine VCardTool::createKey( const Key &key )
00724 {
00725 VCardLine line( "KEY" );
00726
00727 if ( key.isBinary() ) {
00728 if ( !key.binaryData().isEmpty() ) {
00729 line.setValue( key.binaryData() );
00730 line.addParameter( "encoding", "b" );
00731 }
00732 } else if ( !key.textData().isEmpty() )
00733 line.setValue( key.textData() );
00734
00735 if ( key.type() == Key::X509 )
00736 line.addParameter( "type", "X509" );
00737 else if ( key.type() == Key::PGP )
00738 line.addParameter( "type", "PGP" );
00739 else if ( key.type() == Key::Custom )
00740 line.addParameter( "type", key.customTypeString() );
00741
00742 return line;
00743 }
00744
00745 Secrecy VCardTool::parseSecrecy( const VCardLine &line )
00746 {
00747 Secrecy secrecy;
00748
00749 if ( line.value().asString().lower() == "public" )
00750 secrecy.setType( Secrecy::Public );
00751 if ( line.value().asString().lower() == "private" )
00752 secrecy.setType( Secrecy::Private );
00753 if ( line.value().asString().lower() == "confidential" )
00754 secrecy.setType( Secrecy::Confidential );
00755
00756 return secrecy;
00757 }
00758
00759 VCardLine VCardTool::createSecrecy( const Secrecy &secrecy )
00760 {
00761 VCardLine line( "CLASS" );
00762
00763 int type = secrecy.type();
00764
00765 if ( type == Secrecy::Public )
00766 line.setValue( "PUBLIC" );
00767 else if ( type == Secrecy::Private )
00768 line.setValue( "PRIVATE" );
00769 else if ( type == Secrecy::Confidential )
00770 line.setValue( "CONFIDENTIAL" );
00771
00772 return line;
00773 }
00774
00775 Agent VCardTool::parseAgent( const VCardLine &line )
00776 {
00777 Agent agent;
00778
00779 QStringList params = line.parameterList();
00780 if ( params.findIndex( "value" ) != -1 ) {
00781 if ( line.parameter( "value" ).lower() == "uri" )
00782 agent.setUrl( line.value().asString() );
00783 } else {
00784 QString str = line.value().asString();
00785 str.replace( "\\n", "\r\n" );
00786 str.replace( "\\N", "\r\n" );
00787 str.replace( "\\;", ";" );
00788 str.replace( "\\:", ":" );
00789 str.replace( "\\,", "," );
00790
00791 Addressee::List list = parseVCards( str );
00792 if ( list.count() > 0 ) {
00793 Addressee *addr = new Addressee;
00794 *addr = list[ 0 ];
00795 agent.setAddressee( addr );
00796 }
00797 }
00798
00799 return agent;
00800 }
00801
00802 VCardLine VCardTool::createAgent( VCard::Version version, const Agent &agent )
00803 {
00804 VCardLine line( "AGENT" );
00805
00806 if ( agent.isIntern() ) {
00807 if ( agent.addressee() != 0 ) {
00808 Addressee::List list;
00809 list.append( *agent.addressee() );
00810
00811 QString str = createVCards( list, version );
00812 str.replace( "\r\n", "\\n" );
00813 str.replace( ";", "\\;" );
00814 str.replace( ":", "\\:" );
00815 str.replace( ",", "\\," );
00816 line.setValue( str );
00817 }
00818 } else if ( !agent.url().isEmpty() ) {
00819 line.setValue( agent.url() );
00820 line.addParameter( "value", "URI" );
00821 }
00822
00823 return line;
00824 }
00825
00826 QStringList VCardTool::splitString( const QChar &sep, const QString &str )
00827 {
00828 QStringList list;
00829 QString value( str );
00830
00831 int start = 0;
00832 int pos = value.find( sep, start );
00833
00834 while ( pos != -1 ) {
00835 if ( value[ pos - 1 ] != '\\' ) {
00836 if ( pos > start && pos <= (int)value.length() )
00837 list << value.mid( start, pos - start );
00838 else
00839 list << QString::null;
00840
00841 start = pos + 1;
00842 pos = value.find( sep, start );
00843 } else {
00844 if ( pos != 0 ) {
00845 value.replace( pos - 1, 2, sep );
00846 pos = value.find( sep, pos );
00847 } else
00848 pos = value.find( sep, pos + 1 );
00849 }
00850 }
00851
00852 int l = value.length() - 1;
00853 if ( value.mid( start, l - start + 1 ).length() > 0 )
00854 list << value.mid( start, l - start + 1 );
00855 else
00856 list << QString::null;
00857
00858 return list;
00859 }