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 QDate date;
00642 QTime time;
00643
00644 if ( str.indexOf( '-' ) == -1 ) {
00645 date = QDate( str.left( 4 ).toInt(), str.mid( 4, 2 ).toInt(),
00646 str.mid( 6, 2 ).toInt() );
00647 } else {
00648 date = QDate( str.left( 4 ).toInt(), str.mid( 5, 2 ).toInt(),
00649 str.mid( 8, 2 ).toInt() );
00650 }
00651
00652
00653 int timeStart = str.indexOf( 'T' );
00654 if ( timeStart >= 0 ) {
00655 int hour = 0, minute = 0, second = 0;
00656
00657 hour = str.mid( timeStart + 1, 2 ).toInt();
00658
00659 if ( str.indexOf( ':', timeStart + 1 ) > 0 ) {
00660 if ( str.length() >= ( timeStart + 5 ) ) {
00661 minute = str.mid( timeStart + 4, 2 ).toInt();
00662 if ( str.length() >= ( timeStart + 8 ) ) {
00663 second = str.mid( timeStart + 7, 2 ).toInt();
00664 }
00665 }
00666 } else {
00667 if ( str.length() >= ( timeStart + 4 ) ) {
00668 minute = str.mid( timeStart + 3, 2 ).toInt();
00669 if ( str.length() >= ( timeStart + 6 ) ) {
00670 second = str.mid( timeStart + 5, 2 ).toInt();
00671 }
00672 }
00673 }
00674
00675 time = QTime( hour, minute, second );
00676 }
00677
00678 Qt::TimeSpec spec = ( str.right( 1 ) == "Z" ) ? Qt::UTC : Qt::LocalTime;
00679
00680 QDateTime dateTime(date);
00681
00682
00683
00684
00685
00686
00687
00688 dateTime.setTime(time);
00689
00690 dateTime.setTimeSpec(spec);
00691 return dateTime;
00692 }
00693
00694 QString VCardTool::createDateTime( const QDateTime &dateTime ) const
00695 {
00696 QString str;
00697
00698 if ( dateTime.date().isValid() ) {
00699 str.sprintf( "%4d-%02d-%02d", dateTime.date().year(), dateTime.date().month(),
00700 dateTime.date().day() );
00701 if ( dateTime.time().isValid() ) {
00702 QString tmp;
00703 tmp.sprintf( "T%02d:%02d:%02d", dateTime.time().hour(), dateTime.time().minute(),
00704 dateTime.time().second() );
00705 str += tmp;
00706
00707 if ( dateTime.timeSpec() == Qt::UTC ) {
00708 str += 'Z';
00709 }
00710 }
00711 }
00712
00713 return str;
00714 }
00715
00716 Picture VCardTool::parsePicture( const VCardLine &line ) const
00717 {
00718 Picture pic;
00719
00720 const QStringList params = line.parameterList();
00721 if ( params.contains( "encoding" ) ) {
00722 QImage img;
00723 img.loadFromData( line.value().toByteArray() );
00724 pic.setData( img );
00725 } else if ( params.contains( "value" ) ) {
00726 if ( line.parameter( "value" ).toLower() == "uri" ) {
00727 pic.setUrl( line.value().toString() );
00728 }
00729 }
00730
00731 if ( params.contains( "type" ) ) {
00732 pic.setType( line.parameter( "type" ) );
00733 }
00734
00735 return pic;
00736 }
00737
00738 VCardLine VCardTool::createPicture( const QString &identifier, const Picture &pic ) const
00739 {
00740 VCardLine line( identifier );
00741
00742 if ( pic.isIntern() ) {
00743 if ( !pic.data().isNull() ) {
00744 QByteArray input;
00745 QBuffer buffer( &input );
00746 buffer.open( QIODevice::WriteOnly );
00747
00748 if ( !pic.data().hasAlphaChannel() ) {
00749 pic.data().save( &buffer, "JPEG" );
00750
00751 line.setValue( input );
00752 line.addParameter( "encoding", "b" );
00753 line.addParameter( "type", "image/jpeg" );
00754 } else {
00755 pic.data().save( &buffer, "PNG" );
00756
00757 line.setValue( input );
00758 line.addParameter( "encoding", "b" );
00759 line.addParameter( "type", "image/png" );
00760 }
00761 }
00762 } else if ( !pic.url().isEmpty() ) {
00763 line.setValue( pic.url() );
00764 line.addParameter( "value", "URI" );
00765 }
00766
00767 return line;
00768 }
00769
00770 Sound VCardTool::parseSound( const VCardLine &line ) const
00771 {
00772 Sound snd;
00773
00774 const QStringList params = line.parameterList();
00775 if ( params.contains( "encoding" ) ) {
00776 snd.setData( line.value().toByteArray() );
00777 } else if ( params.contains( "value" ) ) {
00778 if ( line.parameter( "value" ).toLower() == "uri" ) {
00779 snd.setUrl( line.value().toString() );
00780 }
00781 }
00782
00783
00784
00785
00786
00787
00788 return snd;
00789 }
00790
00791 VCardLine VCardTool::createSound( const Sound &snd ) const
00792 {
00793 VCardLine line( "SOUND" );
00794
00795 if ( snd.isIntern() ) {
00796 if ( !snd.data().isEmpty() ) {
00797 line.setValue( snd.data() );
00798 line.addParameter( "encoding", "b" );
00799
00800 }
00801 } else if ( !snd.url().isEmpty() ) {
00802 line.setValue( snd.url() );
00803 line.addParameter( "value", "URI" );
00804 }
00805
00806 return line;
00807 }
00808
00809 Key VCardTool::parseKey( const VCardLine &line ) const
00810 {
00811 Key key;
00812
00813 const QStringList params = line.parameterList();
00814 if ( params.contains( "encoding" ) ) {
00815 key.setBinaryData( line.value().toByteArray() );
00816 } else {
00817 key.setTextData( line.value().toString() );
00818 }
00819
00820 if ( params.contains( "type" ) ) {
00821 if ( line.parameter( "type" ).toLower() == "x509" ) {
00822 key.setType( Key::X509 );
00823 } else if ( line.parameter( "type" ).toLower() == "pgp" ) {
00824 key.setType( Key::PGP );
00825 } else {
00826 key.setType( Key::Custom );
00827 key.setCustomTypeString( line.parameter( "type" ) );
00828 }
00829 }
00830
00831 return key;
00832 }
00833
00834 VCardLine VCardTool::createKey( const Key &key ) const
00835 {
00836 VCardLine line( "KEY" );
00837
00838 if ( key.isBinary() ) {
00839 if ( !key.binaryData().isEmpty() ) {
00840 line.setValue( key.binaryData() );
00841 line.addParameter( "encoding", "b" );
00842 }
00843 } else if ( !key.textData().isEmpty() ) {
00844 line.setValue( key.textData() );
00845 }
00846
00847 if ( key.type() == Key::X509 ) {
00848 line.addParameter( "type", "X509" );
00849 } else if ( key.type() == Key::PGP ) {
00850 line.addParameter( "type", "PGP" );
00851 } else if ( key.type() == Key::Custom ) {
00852 line.addParameter( "type", key.customTypeString() );
00853 }
00854
00855 return line;
00856 }
00857
00858 Secrecy VCardTool::parseSecrecy( const VCardLine &line ) const
00859 {
00860 Secrecy secrecy;
00861
00862 const QString value = line.value().toString().toLower();
00863 if ( value == "public" ) {
00864 secrecy.setType( Secrecy::Public );
00865 } else if ( value == "private" ) {
00866 secrecy.setType( Secrecy::Private );
00867 } else if ( value == "confidential" ) {
00868 secrecy.setType( Secrecy::Confidential );
00869 }
00870
00871 return secrecy;
00872 }
00873
00874 VCardLine VCardTool::createSecrecy( const Secrecy &secrecy ) const
00875 {
00876 VCardLine line( "CLASS" );
00877
00878 int type = secrecy.type();
00879
00880 if ( type == Secrecy::Public ) {
00881 line.setValue( QString::fromLatin1( "PUBLIC" ) );
00882 } else if ( type == Secrecy::Private ) {
00883 line.setValue( QString::fromLatin1( "PRIVATE" ) );
00884 } else if ( type == Secrecy::Confidential ) {
00885 line.setValue( QString::fromLatin1( "CONFIDENTIAL" ) );
00886 }
00887
00888 return line;
00889 }
00890
00891 QStringList VCardTool::splitString( const QChar &sep, const QString &str ) const
00892 {
00893 QStringList list;
00894 QString value( str );
00895
00896 int start = 0;
00897 int pos = value.indexOf( sep, start );
00898
00899 while ( pos != -1 ) {
00900 if ( pos == 0 || value[ pos - 1 ] != '\\' ) {
00901 if ( pos > start && pos <= (int)value.length() ) {
00902 list << value.mid( start, pos - start );
00903 } else {
00904 list << QString();
00905 }
00906
00907 start = pos + 1;
00908 pos = value.indexOf( sep, start );
00909 } else {
00910 value.replace( pos - 1, 2, sep );
00911 pos = value.indexOf( sep, pos );
00912 }
00913 }
00914
00915 int l = value.length() - 1;
00916 if ( value.mid( start, l - start + 1 ).length() > 0 ) {
00917 list << value.mid( start, l - start + 1 );
00918 } else {
00919 list << QString();
00920 }
00921
00922 return list;
00923 }