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 if ( geoParts.size() >= 2 ) {
00453 geo.setLatitude( geoParts[ 0 ].toFloat() );
00454 geo.setLongitude( geoParts[ 1 ].toFloat() );
00455 addr.setGeo( geo );
00456 }
00457 }
00458
00459
00460 else if ( identifier == "key" ) {
00461 addr.insertKey( parseKey( *lineIt ) );
00462 }
00463
00464
00465 else if ( identifier == "label" ) {
00466 Address::Type type;
00467
00468 const QStringList types = (*lineIt).parameters( "type" );
00469 for ( QStringList::ConstIterator it = types.begin(); it != types.end(); ++it ) {
00470 type |= mAddressTypeMap[ (*it).toLower() ];
00471 }
00472
00473 bool available = false;
00474 KABC::Address::List addressList = addr.addresses();
00475 for ( KABC::Address::List::Iterator it = addressList.begin();
00476 it != addressList.end(); ++it ) {
00477 if ( (*it).type() == type ) {
00478 (*it).setLabel( (*lineIt).value().toString() );
00479 addr.insertAddress( *it );
00480 available = true;
00481 break;
00482 }
00483 }
00484
00485 if ( !available ) {
00486 KABC::Address address( type );
00487 address.setLabel( (*lineIt).value().toString() );
00488 addr.insertAddress( address );
00489 }
00490 }
00491
00492
00493 else if ( identifier == "logo" ) {
00494 addr.setLogo( parsePicture( *lineIt ) );
00495 }
00496
00497
00498 else if ( identifier == "mailer" ) {
00499 addr.setMailer( (*lineIt).value().toString() );
00500 }
00501
00502
00503 else if ( identifier == "n" ) {
00504 const QStringList nameParts = splitString( semicolonSep, (*lineIt).value().toString() );
00505 if ( nameParts.count() > 0 ) {
00506 addr.setFamilyName( nameParts[ 0 ] );
00507 }
00508 if ( nameParts.count() > 1 ) {
00509 addr.setGivenName( nameParts[ 1 ] );
00510 }
00511 if ( nameParts.count() > 2 ) {
00512 addr.setAdditionalName( nameParts[ 2 ] );
00513 }
00514 if ( nameParts.count() > 3 ) {
00515 addr.setPrefix( nameParts[ 3 ] );
00516 }
00517 if ( nameParts.count() > 4 ) {
00518 addr.setSuffix( nameParts[ 4 ] );
00519 }
00520 }
00521
00522
00523 else if ( identifier == "name" ) {
00524 addr.setName( (*lineIt).value().toString() );
00525 }
00526
00527
00528 else if ( identifier == "nickname" ) {
00529 addr.setNickName( (*lineIt).value().toString() );
00530 }
00531
00532
00533 else if ( identifier == "note" ) {
00534 addr.setNote( (*lineIt).value().toString() );
00535 }
00536
00537
00538 else if ( identifier == "org" ) {
00539 const QStringList orgParts = splitString( semicolonSep, (*lineIt).value().toString() );
00540 if ( orgParts.count() > 0 ) {
00541 addr.setOrganization( orgParts[ 0 ] );
00542 }
00543 if ( orgParts.count() > 1 ) {
00544 addr.setDepartment( orgParts[ 1 ] );
00545 }
00546 }
00547
00548
00549 else if ( identifier == "photo" ) {
00550 addr.setPhoto( parsePicture( *lineIt ) );
00551 }
00552
00553
00554 else if ( identifier == "prodid" ) {
00555 addr.setProductId( (*lineIt).value().toString() );
00556 }
00557
00558
00559 else if ( identifier == "rev" ) {
00560 addr.setRevision( parseDateTime( (*lineIt).value().toString() ) );
00561 }
00562
00563
00564 else if ( identifier == "role" ) {
00565 addr.setRole( (*lineIt).value().toString() );
00566 }
00567
00568
00569 else if ( identifier == "sort-string" ) {
00570 addr.setSortString( (*lineIt).value().toString() );
00571 }
00572
00573
00574 else if ( identifier == "sound" ) {
00575 addr.setSound( parseSound( *lineIt ) );
00576 }
00577
00578
00579 else if ( identifier == "tel" ) {
00580 PhoneNumber phone;
00581 phone.setNumber( (*lineIt).value().toString() );
00582
00583 PhoneNumber::Type type;
00584
00585 const QStringList types = (*lineIt).parameters( "type" );
00586 for ( QStringList::ConstIterator it = types.begin(); it != types.end(); ++it ) {
00587 type |= mPhoneTypeMap[(*it).toUpper()];
00588 }
00589
00590 phone.setType( type );
00591
00592 addr.insertPhoneNumber( phone );
00593 }
00594
00595
00596 else if ( identifier == "title" ) {
00597 addr.setTitle( (*lineIt).value().toString() );
00598 }
00599
00600
00601 else if ( identifier == "tz" ) {
00602 TimeZone tz;
00603 const QString date = (*lineIt).value().toString();
00604
00605 int hours = date.mid( 1, 2 ).toInt();
00606 int minutes = date.mid( 4, 2 ).toInt();
00607 int offset = ( hours * 60 ) + minutes;
00608 offset = offset * ( date[ 0 ] == '+' ? 1 : -1 );
00609
00610 tz.setOffset( offset );
00611 addr.setTimeZone( tz );
00612 }
00613
00614
00615 else if ( identifier == "uid" ) {
00616 addr.setUid( (*lineIt).value().toString() );
00617 }
00618
00619
00620 else if ( identifier == "url" ) {
00621 addr.setUrl( KUrl( (*lineIt).value().toString() ) );
00622 }
00623
00624
00625 else if ( identifier.startsWith( "x-" ) ) {
00626 const QString key = (*lineIt).identifier().mid( 2 );
00627 int dash = key.indexOf( "-" );
00628 addr.insertCustom( key.left( dash ), key.mid( dash + 1 ), (*lineIt).value().toString() );
00629 }
00630 }
00631 }
00632
00633 addrList.append( addr );
00634 }
00635
00636 return addrList;
00637 }
00638
00639 QDateTime VCardTool::parseDateTime( const QString &str ) const
00640 {
00641 QDateTime dateTime;
00642
00643 if ( str.indexOf( '-' ) == -1 ) {
00644 dateTime.setDate( QDate( str.left( 4 ).toInt(), str.mid( 4, 2 ).toInt(),
00645 str.mid( 6, 2 ).toInt() ) );
00646
00647 if ( str.indexOf( 'T' ) ) {
00648 dateTime.setTime( QTime( str.mid( 11, 2 ).toInt(), str.mid( 14, 2 ).toInt(),
00649 str.mid( 17, 2 ).toInt() ) );
00650 }
00651 } else {
00652 dateTime.setDate( QDate( str.left( 4 ).toInt(), str.mid( 5, 2 ).toInt(),
00653 str.mid( 8, 2 ).toInt() ) );
00654
00655 if ( str.indexOf( 'T' ) ) {
00656 dateTime.setTime( QTime( str.mid( 11, 2 ).toInt(), str.mid( 14, 2 ).toInt(),
00657 str.mid( 17, 2 ).toInt() ) );
00658 }
00659 }
00660
00661 return dateTime;
00662 }
00663
00664 QString VCardTool::createDateTime( const QDateTime &dateTime ) const
00665 {
00666 QString str;
00667
00668 if ( dateTime.date().isValid() ) {
00669 str.sprintf( "%4d-%02d-%02d", dateTime.date().year(), dateTime.date().month(),
00670 dateTime.date().day() );
00671 if ( dateTime.time().isValid() ) {
00672 QString tmp;
00673 tmp.sprintf( "T%02d:%02d:%02dZ", dateTime.time().hour(), dateTime.time().minute(),
00674 dateTime.time().second() );
00675 str += tmp;
00676 }
00677 }
00678
00679 return str;
00680 }
00681
00682 Picture VCardTool::parsePicture( const VCardLine &line ) const
00683 {
00684 Picture pic;
00685
00686 const QStringList params = line.parameterList();
00687 if ( params.contains( "encoding" ) ) {
00688 QImage img;
00689 img.loadFromData( line.value().toByteArray() );
00690 pic.setData( img );
00691 } else if ( params.contains( "value" ) ) {
00692 if ( line.parameter( "value" ).toLower() == "uri" ) {
00693 pic.setUrl( line.value().toString() );
00694 }
00695 }
00696
00697 if ( params.contains( "type" ) ) {
00698 pic.setType( line.parameter( "type" ) );
00699 }
00700
00701 return pic;
00702 }
00703
00704 VCardLine VCardTool::createPicture( const QString &identifier, const Picture &pic ) const
00705 {
00706 VCardLine line( identifier );
00707
00708 if ( pic.isIntern() ) {
00709 if ( !pic.data().isNull() ) {
00710 QByteArray input;
00711 QBuffer buffer( &input );
00712 buffer.open( QIODevice::WriteOnly );
00713 pic.data().save( &buffer, "JPEG" );
00714
00715 line.setValue( input );
00716 line.addParameter( "encoding", "b" );
00717 line.addParameter( "type", "image/jpeg" );
00718 }
00719 } else if ( !pic.url().isEmpty() ) {
00720 line.setValue( pic.url() );
00721 line.addParameter( "value", "URI" );
00722 }
00723
00724 return line;
00725 }
00726
00727 Sound VCardTool::parseSound( const VCardLine &line ) const
00728 {
00729 Sound snd;
00730
00731 const QStringList params = line.parameterList();
00732 if ( params.contains( "encoding" ) ) {
00733 snd.setData( line.value().toByteArray() );
00734 } else if ( params.contains( "value" ) ) {
00735 if ( line.parameter( "value" ).toLower() == "uri" ) {
00736 snd.setUrl( line.value().toString() );
00737 }
00738 }
00739
00740
00741
00742
00743
00744
00745 return snd;
00746 }
00747
00748 VCardLine VCardTool::createSound( const Sound &snd ) const
00749 {
00750 VCardLine line( "SOUND" );
00751
00752 if ( snd.isIntern() ) {
00753 if ( !snd.data().isEmpty() ) {
00754 line.setValue( snd.data() );
00755 line.addParameter( "encoding", "b" );
00756
00757 }
00758 } else if ( !snd.url().isEmpty() ) {
00759 line.setValue( snd.url() );
00760 line.addParameter( "value", "URI" );
00761 }
00762
00763 return line;
00764 }
00765
00766 Key VCardTool::parseKey( const VCardLine &line ) const
00767 {
00768 Key key;
00769
00770 const QStringList params = line.parameterList();
00771 if ( params.contains( "encoding" ) ) {
00772 key.setBinaryData( line.value().toByteArray() );
00773 } else {
00774 key.setTextData( line.value().toString() );
00775 }
00776
00777 if ( params.contains( "type" ) ) {
00778 if ( line.parameter( "type" ).toLower() == "x509" ) {
00779 key.setType( Key::X509 );
00780 } else if ( line.parameter( "type" ).toLower() == "pgp" ) {
00781 key.setType( Key::PGP );
00782 } else {
00783 key.setType( Key::Custom );
00784 key.setCustomTypeString( line.parameter( "type" ) );
00785 }
00786 }
00787
00788 return key;
00789 }
00790
00791 VCardLine VCardTool::createKey( const Key &key ) const
00792 {
00793 VCardLine line( "KEY" );
00794
00795 if ( key.isBinary() ) {
00796 if ( !key.binaryData().isEmpty() ) {
00797 line.setValue( key.binaryData() );
00798 line.addParameter( "encoding", "b" );
00799 }
00800 } else if ( !key.textData().isEmpty() ) {
00801 line.setValue( key.textData() );
00802 }
00803
00804 if ( key.type() == Key::X509 ) {
00805 line.addParameter( "type", "X509" );
00806 } else if ( key.type() == Key::PGP ) {
00807 line.addParameter( "type", "PGP" );
00808 } else if ( key.type() == Key::Custom ) {
00809 line.addParameter( "type", key.customTypeString() );
00810 }
00811
00812 return line;
00813 }
00814
00815 Secrecy VCardTool::parseSecrecy( const VCardLine &line ) const
00816 {
00817 Secrecy secrecy;
00818
00819 const QString value = line.value().toString().toLower();
00820 if ( value == "public" ) {
00821 secrecy.setType( Secrecy::Public );
00822 } else if ( value == "private" ) {
00823 secrecy.setType( Secrecy::Private );
00824 } else if ( value == "confidential" ) {
00825 secrecy.setType( Secrecy::Confidential );
00826 }
00827
00828 return secrecy;
00829 }
00830
00831 VCardLine VCardTool::createSecrecy( const Secrecy &secrecy ) const
00832 {
00833 VCardLine line( "CLASS" );
00834
00835 int type = secrecy.type();
00836
00837 if ( type == Secrecy::Public ) {
00838 line.setValue( QString::fromLatin1( "PUBLIC" ) );
00839 } else if ( type == Secrecy::Private ) {
00840 line.setValue( QString::fromLatin1( "PRIVATE" ) );
00841 } else if ( type == Secrecy::Confidential ) {
00842 line.setValue( QString::fromLatin1( "CONFIDENTIAL" ) );
00843 }
00844
00845 return line;
00846 }
00847
00848 QStringList VCardTool::splitString( const QChar &sep, const QString &str ) const
00849 {
00850 QStringList list;
00851 QString value( str );
00852
00853 int start = 0;
00854 int pos = value.indexOf( sep, start );
00855
00856 while ( pos != -1 ) {
00857 if ( pos == 0 || value[ pos - 1 ] != '\\' ) {
00858 if ( pos > start && pos <= (int)value.length() ) {
00859 list << value.mid( start, pos - start );
00860 } else {
00861 list << QString();
00862 }
00863
00864 start = pos + 1;
00865 pos = value.indexOf( sep, start );
00866 } else {
00867 value.replace( pos - 1, 2, sep );
00868 pos = value.indexOf( sep, pos );
00869 }
00870 }
00871
00872 int l = value.length() - 1;
00873 if ( value.mid( start, l - start + 1 ).length() > 0 ) {
00874 list << value.mid( start, l - start + 1 );
00875 } else {
00876 list << QString();
00877 }
00878
00879 return list;
00880 }