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