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