21 #include "vcardtool.h"
27 #include <QtCore/QString>
28 #include <QtCore/QBuffer>
32 static bool needsEncoding(
const QString &value )
34 uint length = value.length();
35 for ( uint i = 0; i < length; ++i ) {
36 char c = value.at( i ).toLatin1();
37 if ( ( c < 33 || c > 126 ) && c !=
' ' && c !=
'=' ) {
45 VCardTool::VCardTool()
47 mAddressTypeMap.insert( QLatin1String(
"dom" ),
Address::Dom );
48 mAddressTypeMap.insert( QLatin1String(
"intl" ),
Address::Intl );
51 mAddressTypeMap.insert( QLatin1String(
"home" ),
Address::Home );
52 mAddressTypeMap.insert( QLatin1String(
"work" ),
Address::Work );
53 mAddressTypeMap.insert( QLatin1String(
"pref" ),
Address::Pref );
71 VCardTool::~VCardTool()
75 QByteArray VCardTool::exportVCards(
const Addressee::List &list, VCard::Version version )
const
77 return createVCards(list,version,
true );
80 QByteArray VCardTool::createVCards(
const Addressee::List &list, VCard::Version version )
const
82 return createVCards(list,version,
false);
85 QByteArray VCardTool::createVCards(
const Addressee::List &list, VCard::Version version,
bool exportVcard )
const
87 VCard::List vCardList;
89 Addressee::List::ConstIterator addrIt;
90 Addressee::List::ConstIterator listEnd( list.constEnd() );
91 for ( addrIt = list.constBegin(); addrIt != listEnd; ++addrIt ) {
93 QStringList::ConstIterator strIt;
97 for ( Address::List::ConstIterator it = addresses.begin(); it != addresses.end(); ++it ) {
100 const bool isEmpty = ( (*it).postOfficeBox().isEmpty() &&
101 (*it).extended().isEmpty() &&
102 (*it).street().isEmpty() &&
103 (*it).locality().isEmpty() &&
104 (*it).region().isEmpty() &&
105 (*it).postalCode().isEmpty() &&
106 (*it).country().isEmpty() );
108 address.append( (*it).postOfficeBox().replace( QLatin1Char(
';' ), QLatin1String(
"\\;" ) ) );
109 address.append( (*it).extended().replace( QLatin1Char(
';' ), QLatin1String(
"\\;" ) ) );
110 address.append( (*it).street().replace( QLatin1Char(
';' ), QLatin1String(
"\\;" ) ) );
111 address.append( (*it).locality().replace( QLatin1Char(
';' ), QLatin1String(
"\\;" ) ) );
112 address.append( (*it).region().replace( QLatin1Char(
';' ), QLatin1String(
"\\;" ) ) );
113 address.append( (*it).postalCode().replace( QLatin1Char(
';' ), QLatin1String(
"\\;" ) ) );
114 address.append( (*it).country().replace( QLatin1Char(
';' ), QLatin1String(
"\\;" ) ) );
116 VCardLine adrLine( QLatin1String(
"ADR" ), address.join( QLatin1String(
";" ) ) );
117 if ( version == VCard::v2_1 && needsEncoding( address.join( QLatin1String(
";" ) ) ) ) {
118 adrLine.addParameter( QLatin1String(
"charset" ), QLatin1String(
"UTF-8" ) );
119 adrLine.addParameter( QLatin1String(
"encoding" ), QLatin1String(
"QUOTED-PRINTABLE" ) );
122 VCardLine labelLine( QLatin1String(
"LABEL" ), (*it).label() );
123 if ( version == VCard::v2_1 && needsEncoding( (*it).label() ) ) {
124 labelLine.addParameter( QLatin1String(
"charset" ), QLatin1String(
"UTF-8" ) );
125 labelLine.addParameter( QLatin1String(
"encoding" ), QLatin1String(
"QUOTED-PRINTABLE" ) );
128 const bool hasLabel = !(*it).label().isEmpty();
129 QMap<QString, Address::TypeFlag>::ConstIterator typeIt;
130 for ( typeIt = mAddressTypeMap.constBegin();
131 typeIt != mAddressTypeMap.constEnd(); ++typeIt ) {
132 if ( typeIt.value() & (*it).type() ) {
133 adrLine.addParameter( QLatin1String(
"TYPE" ), typeIt.key() );
135 labelLine.addParameter( QLatin1String(
"TYPE" ), typeIt.key() );
141 card.addLine( adrLine );
144 card.addLine( labelLine );
149 card.addLine( VCardLine( QLatin1String(
"BDAY" ), createDateTime( (*addrIt).birthday() ) ) );
152 if ( version == VCard::v3_0 ) {
153 QStringList categories = (*addrIt).categories();
154 QStringList::Iterator catIt;
155 for ( catIt = categories.begin(); catIt != categories.end(); ++catIt ) {
156 (*catIt).replace( QLatin1Char(
',' ), QLatin1String(
"\\," ) );
159 VCardLine catLine( QLatin1String(
"CATEGORIES" ), categories.join( QLatin1String(
"," ) ) );
160 card.addLine( catLine );
164 if ( version == VCard::v3_0 ) {
165 card.addLine( createSecrecy( (*addrIt).secrecy() ) );
169 const QStringList emails = (*addrIt).emails();
171 for ( strIt = emails.begin(); strIt != emails.end(); ++strIt ) {
172 VCardLine line( QLatin1String(
"EMAIL" ), *strIt );
173 if ( pref ==
true && emails.count() > 1 ) {
174 line.addParameter( QLatin1String(
"TYPE" ), QLatin1String(
"PREF" ) );
177 card.addLine( line );
181 VCardLine fnLine( QLatin1String(
"FN" ), (*addrIt).formattedName() );
182 if ( version == VCard::v2_1 && needsEncoding( (*addrIt).formattedName() ) ) {
183 fnLine.addParameter( QLatin1String(
"charset" ), QLatin1String(
"UTF-8" ) );
184 fnLine.addParameter( QLatin1String(
"encoding" ), QLatin1String(
"QUOTED-PRINTABLE" ) );
186 card.addLine( fnLine );
189 const Geo geo = (*addrIt).geo();
193 card.addLine( VCardLine( QLatin1String(
"GEO" ), str ) );
198 Key::List::ConstIterator keyIt;
199 for ( keyIt = keys.begin(); keyIt != keys.end(); ++keyIt ) {
200 card.addLine( createKey( *keyIt ) );
204 card.addLine( createPicture( QLatin1String(
"LOGO" ), (*addrIt).logo() ) );
207 VCardLine mailerLine( QLatin1String(
"MAILER" ), (*addrIt).mailer() );
208 if ( version == VCard::v2_1 && needsEncoding( (*addrIt).mailer() ) ) {
209 mailerLine.addParameter( QLatin1String(
"charset" ), QLatin1String(
"UTF-8" ) );
210 mailerLine.addParameter( QLatin1String(
"encoding" ), QLatin1String(
"QUOTED-PRINTABLE" ) );
212 card.addLine( mailerLine );
216 name.append( (*addrIt).familyName().replace( QLatin1Char(
';' ), QLatin1String(
"\\;" ) ) );
217 name.append( (*addrIt).givenName().replace( QLatin1Char(
';' ), QLatin1String(
"\\;" ) ) );
218 name.append( (*addrIt).additionalName().replace( QLatin1Char(
';' ), QLatin1String(
"\\;" ) ) );
219 name.append( (*addrIt).prefix().replace( QLatin1Char(
';' ), QLatin1String(
"\\;" ) ) );
220 name.append( (*addrIt).suffix().replace( QLatin1Char(
';' ), QLatin1String(
"\\;" ) ) );
222 VCardLine nLine( QLatin1String(
"N" ), name.join( QLatin1String(
";" ) ) );
223 if ( version == VCard::v2_1 && needsEncoding( name.join( QLatin1String(
";" ) ) ) ) {
224 nLine.addParameter( QLatin1String(
"charset" ), QLatin1String(
"UTF-8" ) );
225 nLine.addParameter( QLatin1String(
"encoding" ), QLatin1String(
"QUOTED-PRINTABLE" ) );
227 card.addLine( nLine );
230 VCardLine nameLine( QLatin1String(
"NAME" ), (*addrIt).name() );
231 if ( version == VCard::v2_1 && needsEncoding( (*addrIt).name() ) ) {
232 nameLine.addParameter( QLatin1String(
"charset" ), QLatin1String(
"UTF-8" ) );
233 nameLine.addParameter( QLatin1String(
"encoding" ), QLatin1String(
"QUOTED-PRINTABLE" ) );
235 card.addLine( nameLine );
238 if ( version == VCard::v3_0 ) {
239 card.addLine( VCardLine( QLatin1String(
"NICKNAME" ), (*addrIt).nickName() ) );
243 VCardLine noteLine( QLatin1String(
"NOTE" ), (*addrIt).note() );
244 if ( version == VCard::v2_1 && needsEncoding( (*addrIt).note() ) ) {
245 noteLine.addParameter( QLatin1String(
"charset" ), QLatin1String(
"UTF-8" ) );
246 noteLine.addParameter( QLatin1String(
"encoding" ), QLatin1String(
"QUOTED-PRINTABLE" ) );
248 card.addLine( noteLine );
251 QStringList organization;
252 organization.append( ( *addrIt ).organization().replace( QLatin1Char(
';' ),
253 QLatin1String(
"\\;" ) ) );
254 if ( !( *addrIt ).department().isEmpty() ) {
255 organization.append( ( *addrIt ).department().replace( QLatin1Char(
';' ),
256 QLatin1String(
"\\;" ) ) );
258 VCardLine orgLine( QLatin1String(
"ORG" ), organization.join( QLatin1String(
";" ) ) );
259 if ( version == VCard::v2_1 && needsEncoding( organization.join( QLatin1String(
";" ) ) ) ) {
260 orgLine.addParameter( QLatin1String(
"charset" ), QLatin1String(
"UTF-8" ) );
261 orgLine.addParameter( QLatin1String(
"encoding" ), QLatin1String(
"QUOTED-PRINTABLE" ) );
263 card.addLine( orgLine );
266 card.addLine( createPicture( QLatin1String(
"PHOTO" ), (*addrIt).photo() ) );
269 if ( version == VCard::v3_0 ) {
270 card.addLine( VCardLine( QLatin1String(
"PRODID" ), (*addrIt).productId() ) );
274 card.addLine( VCardLine( QLatin1String(
"REV" ), createDateTime( (*addrIt).revision() ) ) );
277 VCardLine roleLine( QLatin1String(
"ROLE" ), (*addrIt).role() );
278 if ( version == VCard::v2_1 && needsEncoding( (*addrIt).role() ) ) {
279 roleLine.addParameter( QLatin1String(
"charset" ), QLatin1String(
"UTF-8" ) );
280 roleLine.addParameter( QLatin1String(
"encoding" ), QLatin1String(
"QUOTED-PRINTABLE" ) );
282 card.addLine( roleLine );
285 if ( version == VCard::v3_0 ) {
286 card.addLine( VCardLine( QLatin1String(
"SORT-STRING" ), (*addrIt).sortString() ) );
290 card.addLine( createSound( (*addrIt).sound() ) );
294 PhoneNumber::List::ConstIterator phoneIt;
295 for ( phoneIt = phoneNumbers.begin(); phoneIt != phoneNumbers.end(); ++phoneIt ) {
296 VCardLine line( QLatin1String(
"TEL" ), (*phoneIt).number() );
298 QMap<QString, PhoneNumber::TypeFlag>::ConstIterator typeIt;
299 for ( typeIt = mPhoneTypeMap.constBegin(); typeIt != mPhoneTypeMap.constEnd(); ++typeIt ) {
300 if ( typeIt.value() & (*phoneIt).type() ) {
301 line.addParameter( QLatin1String(
"TYPE" ), typeIt.key() );
305 card.addLine( line );
309 VCardLine titleLine( QLatin1String(
"TITLE" ), (*addrIt).title() );
310 if ( version == VCard::v2_1 && needsEncoding( (*addrIt).title() ) ) {
311 titleLine.addParameter( QLatin1String(
"charset" ), QLatin1String(
"UTF-8" ) );
312 titleLine.addParameter( QLatin1String(
"encoding" ), QLatin1String(
"QUOTED-PRINTABLE" ) );
314 card.addLine( titleLine );
317 const TimeZone timeZone = (*addrIt).timeZone();
322 if ( timeZone.
offset() < 0 ) {
326 str.sprintf(
"%c%02d:%02d", ( timeZone.
offset() >= 0 ?
'+' :
'-' ),
327 ( timeZone.
offset() / 60 ) * neg,
328 ( timeZone.
offset() % 60 ) * neg );
330 card.addLine( VCardLine( QLatin1String(
"TZ" ), str ) );
334 card.addLine( VCardLine( QLatin1String(
"UID" ), (*addrIt).uid() ) );
337 card.addLine( VCardLine( QLatin1String(
"URL" ), (*addrIt).url().url() ) );
340 if ( version == VCard::v2_1 ) {
341 card.addLine( VCardLine( QLatin1String(
"VERSION" ), QLatin1String(
"2.1" ) ) );
343 if ( version == VCard::v3_0 ) {
344 card.addLine( VCardLine( QLatin1String(
"VERSION" ), QLatin1String(
"3.0" ) ) );
348 const QStringList customs = (*addrIt).customs();
349 for ( strIt = customs.begin(); strIt != customs.end(); ++strIt ) {
350 QString identifier = QLatin1String(
"X-" ) +
351 (*strIt).left( (*strIt).indexOf( QLatin1Char(
':' ) ) );
352 const QString value = (*strIt).mid( (*strIt).indexOf( QLatin1Char(
':' ) ) + 1 );
353 if ( value.isEmpty() ) {
358 if(identifier == QLatin1String(
"X-messaging/aim-All")) {
359 identifier = QLatin1String(
"X-AIM");
360 }
else if(identifier == QLatin1String(
"X-messaging/icq-All")) {
361 identifier = QLatin1String(
"X-ICQ");
362 }
else if(identifier == QLatin1String(
"X-messaging/xmpp-All")) {
363 identifier = QLatin1String(
"X-JABBER");
364 }
else if(identifier == QLatin1String(
"X-messaging/msn-All")) {
365 identifier = QLatin1String(
"X-MSN");
366 }
else if(identifier == QLatin1String(
"X-messaging/yahoo-All")) {
367 identifier = QLatin1String(
"X-YAHOO");
368 }
else if(identifier == QLatin1String(
"X-messaging/gadu-All")) {
369 identifier = QLatin1String(
"X-GADUGADU");
370 }
else if(identifier == QLatin1String(
"X-messaging/skype-All")) {
371 identifier = QLatin1String(
"X-SKYPE");
372 }
else if(identifier == QLatin1String(
"X-messaging/groupwise-All")) {
373 identifier = QLatin1String(
"X-GROUPWISE");
374 }
else if(identifier == QLatin1String(
"X-messaging/sms-All")) {
375 identifier = QLatin1String(
"X-SMS");
376 }
else if(identifier == QLatin1String(
"X-messaging/meanwhile-All")) {
377 identifier = QLatin1String(
"X-MEANWHILE");
381 VCardLine line( identifier, value );
382 if ( version == VCard::v2_1 && needsEncoding( value ) ) {
383 line.addParameter( QLatin1String(
"charset" ), QLatin1String(
"UTF-8" ) );
384 line.addParameter( QLatin1String(
"encoding" ), QLatin1String(
"QUOTED-PRINTABLE" ) );
386 card.addLine( line );
389 vCardList.append( card );
392 return VCardParser::createVCards( vCardList );
395 Addressee::List VCardTool::parseVCards(
const QByteArray &vcard )
const
397 static const QLatin1Char semicolonSep(
';' );
398 static const QLatin1Char commaSep(
',' );
401 Addressee::List addrList;
402 const VCard::List vCardList = VCardParser::parseVCards( vcard );
404 VCard::List::ConstIterator cardIt;
405 VCard::List::ConstIterator listEnd( vCardList.end() );
406 for ( cardIt = vCardList.begin(); cardIt != listEnd; ++cardIt ) {
409 const QStringList idents = (*cardIt).identifiers();
410 QStringList::ConstIterator identIt;
411 QStringList::ConstIterator identEnd( idents.end() );
412 for ( identIt = idents.begin(); identIt != identEnd; ++identIt ) {
413 const VCardLine::List lines = (*cardIt).lines( (*identIt) );
414 VCardLine::List::ConstIterator lineIt;
417 for ( lineIt = lines.begin(); lineIt != lines.end(); ++lineIt ) {
418 identifier = (*lineIt).identifier().toLower();
420 if ( identifier == QLatin1String(
"adr" ) ) {
422 const QStringList addrParts = splitString( semicolonSep, (*lineIt).value().toString() );
423 if ( addrParts.count() > 0 ) {
426 if ( addrParts.count() > 1 ) {
429 if ( addrParts.count() > 2 ) {
432 if ( addrParts.count() > 3 ) {
435 if ( addrParts.count() > 4 ) {
438 if ( addrParts.count() > 5 ) {
441 if ( addrParts.count() > 6 ) {
447 const QStringList types = (*lineIt).parameters( QLatin1String(
"type" ) );
448 for ( QStringList::ConstIterator it = types.begin(); it != types.end(); ++it ) {
449 type |= mAddressTypeMap[ (*it).toLower() ];
453 addr.insertAddress( address );
457 else if ( identifier == QLatin1String(
"bday" ) ) {
458 addr.setBirthday( parseDateTime( (*lineIt).value().toString() ) );
462 else if ( identifier == QLatin1String(
"categories" ) ) {
463 const QStringList categories = splitString( commaSep, (*lineIt).value().toString() );
464 addr.setCategories( categories );
468 else if ( identifier == QLatin1String(
"class" ) ) {
469 addr.setSecrecy( parseSecrecy( *lineIt ) );
473 else if ( identifier == QLatin1String(
"email" ) ) {
474 const QStringList types = (*lineIt).parameters( QLatin1String(
"type" ) );
475 addr.insertEmail( (*lineIt).value().toString(),
476 types.contains( QLatin1String(
"PREF" ) ) );
480 else if ( identifier == QLatin1String(
"fn" ) ) {
481 addr.setFormattedName( (*lineIt).value().toString() );
485 else if ( identifier == QLatin1String(
"geo" ) ) {
488 const QStringList geoParts =
489 (*lineIt).value().
toString().split( QLatin1Char(
';' ), QString::KeepEmptyParts );
490 if ( geoParts.size() >= 2 ) {
498 else if ( identifier == QLatin1String(
"key" ) ) {
499 addr.insertKey( parseKey( *lineIt ) );
503 else if ( identifier == QLatin1String(
"label" ) ) {
506 const QStringList types = (*lineIt).parameters( QLatin1String(
"type" ) );
507 for ( QStringList::ConstIterator it = types.begin(); it != types.end(); ++it ) {
508 type |= mAddressTypeMap[ (*it).toLower() ];
511 bool available =
false;
513 for ( KABC::Address::List::Iterator it = addressList.begin();
514 it != addressList.end(); ++it ) {
515 if ( (*it).type() == type ) {
516 (*it).setLabel( (*lineIt).value().toString() );
517 addr.insertAddress( *it );
525 address.
setLabel( (*lineIt).value().toString() );
526 addr.insertAddress( address );
531 else if ( identifier == QLatin1String(
"logo" ) ) {
532 addr.setLogo( parsePicture( *lineIt ) );
536 else if ( identifier == QLatin1String(
"mailer" ) ) {
537 addr.setMailer( (*lineIt).value().toString() );
541 else if ( identifier == QLatin1String(
"n" ) ) {
542 const QStringList nameParts = splitString( semicolonSep, (*lineIt).value().toString() );
543 const int numberOfParts(nameParts.count());
544 if ( numberOfParts > 0 ) {
545 addr.setFamilyName( nameParts[ 0 ] );
547 if ( numberOfParts > 1 ) {
548 addr.setGivenName( nameParts[ 1 ] );
550 if ( numberOfParts > 2 ) {
551 addr.setAdditionalName( nameParts[ 2 ] );
553 if ( numberOfParts > 3 ) {
554 addr.setPrefix( nameParts[ 3 ] );
556 if ( numberOfParts > 4 ) {
557 addr.setSuffix( nameParts[ 4 ] );
562 else if ( identifier == QLatin1String(
"name" ) ) {
563 addr.setName( (*lineIt).value().toString() );
567 else if ( identifier == QLatin1String(
"nickname" ) ) {
568 addr.setNickName( (*lineIt).value().toString() );
572 else if ( identifier == QLatin1String(
"note" ) ) {
573 addr.setNote( (*lineIt).value().toString() );
577 else if ( identifier == QLatin1String(
"org" ) ) {
578 const QStringList orgParts = splitString( semicolonSep, (*lineIt).value().toString() );
579 if ( orgParts.count() > 0 ) {
580 addr.setOrganization( orgParts[ 0 ] );
582 if ( orgParts.count() > 1 ) {
583 addr.setDepartment( orgParts[ 1 ] );
588 else if ( identifier == QLatin1String(
"photo" ) ) {
589 addr.setPhoto( parsePicture( *lineIt ) );
593 else if ( identifier == QLatin1String(
"prodid" ) ) {
594 addr.setProductId( (*lineIt).value().toString() );
598 else if ( identifier == QLatin1String(
"rev" ) ) {
599 addr.setRevision( parseDateTime( (*lineIt).value().toString() ) );
603 else if ( identifier == QLatin1String(
"role" ) ) {
604 addr.setRole( (*lineIt).value().toString() );
608 else if ( identifier == QLatin1String(
"sort-string" ) ) {
609 addr.setSortString( (*lineIt).value().toString() );
613 else if ( identifier == QLatin1String(
"sound" ) ) {
614 addr.setSound( parseSound( *lineIt ) );
618 else if ( identifier == QLatin1String(
"tel" ) ) {
620 phone.
setNumber( (*lineIt).value().toString() );
622 PhoneNumber::Type type;
624 const QStringList types = (*lineIt).parameters( QLatin1String(
"type" ) );
625 QStringList::ConstIterator typeEnd(types.end());
626 for ( QStringList::ConstIterator it = types.begin(); it != typeEnd; ++it ) {
627 type |= mPhoneTypeMap[(*it).toUpper()];
632 addr.insertPhoneNumber( phone );
636 else if ( identifier == QLatin1String(
"title" ) ) {
637 addr.setTitle( (*lineIt).value().toString() );
641 else if ( identifier == QLatin1String(
"tz" ) ) {
643 const QString date = (*lineIt).value().
toString();
645 if ( !date.isEmpty() ) {
646 int hours = date.mid( 1, 2 ).toInt();
647 int minutes = date.mid( 4, 2 ).toInt();
648 int offset = ( hours * 60 ) + minutes;
649 offset = offset * ( date[ 0 ] == QLatin1Char(
'+' ) ? 1 : -1 );
652 addr.setTimeZone( tz );
657 else if ( identifier == QLatin1String(
"uid" ) ) {
658 addr.setUid( (*lineIt).value().toString() );
662 else if ( identifier == QLatin1String(
"url" ) ) {
663 addr.setUrl( KUrl( (*lineIt).value().toString() ) );
667 else if ( identifier.startsWith( QLatin1String(
"x-" ) ) ) {
668 QString ident = ( *lineIt ).identifier();
670 if(identifier==QLatin1String(
"x-evolution-spouse") || identifier == QLatin1String(
"x-spouse")) {
671 ident = QLatin1String(
"X-KADDRESSBOOK-X-SpousesName");
672 }
else if(identifier == QLatin1String(
"x-evolution-blog-url")) {
673 ident = QLatin1String(
"X-KADDRESSBOOK-BlogFeed");
674 }
else if(identifier == QLatin1String(
"x-evolution-assistant") || identifier == QLatin1String(
"x-assistant")) {
675 ident = QLatin1String(
"X-KADDRESSBOOK-X-AssistantsName");
676 }
else if(identifier == QLatin1String(
"x-evolution-anniversary") || identifier == QLatin1String(
"x-anniversary")) {
677 ident = QLatin1String(
"X-KADDRESSBOOK-X-Anniversary");
678 }
else if(identifier == QLatin1String(
"x-evolution-manager") || identifier == QLatin1String(
"x-manager")) {
679 ident = QLatin1String(
"X-KADDRESSBOOK-X-ManagersName");
680 }
else if(identifier == QLatin1String(
"x-aim")) {
681 ident = QLatin1String(
"X-messaging/aim-All");
682 }
else if(identifier == QLatin1String(
"x-icq")) {
683 ident = QLatin1String(
"X-messaging/icq-All");
684 }
else if(identifier == QLatin1String(
"x-jabber")) {
685 ident = QLatin1String(
"X-messaging/xmpp-All");
686 }
else if(identifier == QLatin1String(
"x-jabber")) {
687 ident = QLatin1String(
"X-messaging/xmpp-All");
688 }
else if(identifier == QLatin1String(
"x-msn")) {
689 ident = QLatin1String(
"X-messaging/msn-All");
690 }
else if(identifier == QLatin1String(
"x-yahoo")) {
691 ident = QLatin1String(
"X-messaging/yahoo-All");
692 }
else if(identifier == QLatin1String(
"x-gadugadu")) {
693 ident = QLatin1String(
"X-messaging/gadu-All");
694 }
else if(identifier == QLatin1String(
"x-skype")) {
695 ident = QLatin1String(
"X-messaging/skype-All");
696 }
else if(identifier == QLatin1String(
"x-groupwise")) {
697 ident = QLatin1String(
"X-messaging/groupwise-All");
698 }
else if(identifier == QLatin1String(
"x-sms")) {
699 ident = QLatin1String(
"X-messaging/sms-All");
700 }
else if(identifier == QLatin1String(
"x-meanwhile")) {
701 ident = QLatin1String(
"X-messaging/meanwhile-All");
704 const QString key = ident.mid( 2 );
705 const int dash = key.indexOf( QLatin1Char(
'-' ) );
706 addr.insertCustom( key.left( dash ), key.mid( dash + 1 ), ( *lineIt ).value().toString() );
711 addrList.append( addr );
717 QDateTime VCardTool::parseDateTime(
const QString &str )
const
722 if ( str.indexOf( QLatin1Char(
'-' ) ) == -1 ) {
723 date = QDate( str.left( 4 ).toInt(), str.mid( 4, 2 ).toInt(),
724 str.mid( 6, 2 ).toInt() );
726 date = QDate( str.left( 4 ).toInt(), str.mid( 5, 2 ).toInt(),
727 str.mid( 8, 2 ).toInt() );
731 int timeStart = str.indexOf( QLatin1Char(
'T' ) );
732 if ( timeStart >= 0 ) {
733 int hour = 0, minute = 0, second = 0;
735 hour = str.mid( timeStart + 1, 2 ).toInt();
737 if ( str.indexOf( QLatin1Char(
':' ), timeStart + 1 ) > 0 ) {
738 if ( str.length() >= ( timeStart + 5 ) ) {
739 minute = str.mid( timeStart + 4, 2 ).toInt();
740 if ( str.length() >= ( timeStart + 8 ) ) {
741 second = str.mid( timeStart + 7, 2 ).toInt();
745 if ( str.length() >= ( timeStart + 4 ) ) {
746 minute = str.mid( timeStart + 3, 2 ).toInt();
747 if ( str.length() >= ( timeStart + 6 ) ) {
748 second = str.mid( timeStart + 5, 2 ).toInt();
753 time = QTime( hour, minute, second );
756 Qt::TimeSpec spec = ( str.right( 1 ) == QLatin1String(
"Z" ) ) ? Qt::UTC : Qt::LocalTime;
758 QDateTime dateTime(date);
766 dateTime.setTime(time);
768 dateTime.setTimeSpec(spec);
772 QString VCardTool::createDateTime(
const QDateTime &dateTime )
const
776 if ( dateTime.date().isValid() ) {
777 str.sprintf(
"%4d-%02d-%02d", dateTime.date().year(), dateTime.date().month(),
778 dateTime.date().day() );
779 if ( dateTime.time().isValid() ) {
781 tmp.sprintf(
"T%02d:%02d:%02d", dateTime.time().hour(), dateTime.time().minute(),
782 dateTime.time().second() );
785 if ( dateTime.timeSpec() == Qt::UTC ) {
786 str += QLatin1Char(
'Z' );
794 Picture VCardTool::parsePicture(
const VCardLine &line )
const
798 const QStringList params = line.parameterList();
799 if ( params.contains( QLatin1String(
"encoding" ) ) ) {
801 img.loadFromData( line.value().toByteArray() );
803 }
else if ( params.contains( QLatin1String(
"value" ) ) ) {
804 if ( line.parameter( QLatin1String(
"value" ) ).toLower() == QLatin1String(
"uri" ) ) {
805 pic.
setUrl( line.value().toString() );
809 if ( params.contains( QLatin1String(
"type" ) ) ) {
810 pic.
setType( line.parameter( QLatin1String(
"type" ) ) );
816 VCardLine VCardTool::createPicture(
const QString &identifier,
const Picture &pic )
const
818 VCardLine line( identifier );
821 if ( !pic.
data().isNull() ) {
823 QBuffer buffer( &input );
824 buffer.open( QIODevice::WriteOnly );
826 if ( !pic.
data().hasAlphaChannel() ) {
827 pic.
data().save( &buffer,
"JPEG" );
829 line.setValue( input );
830 line.addParameter( QLatin1String(
"encoding" ), QLatin1String(
"b" ) );
831 line.addParameter( QLatin1String(
"type" ), QLatin1String(
"image/jpeg" ) );
833 pic.
data().save( &buffer,
"PNG" );
835 line.setValue( input );
836 line.addParameter( QLatin1String(
"encoding" ), QLatin1String(
"b" ) );
837 line.addParameter( QLatin1String(
"type" ), QLatin1String(
"image/png" ) );
840 }
else if ( !pic.
url().isEmpty() ) {
841 line.setValue( pic.
url() );
842 line.addParameter( QLatin1String(
"value" ), QLatin1String(
"URI" ) );
848 Sound VCardTool::parseSound(
const VCardLine &line )
const
852 const QStringList params = line.parameterList();
853 if ( params.contains( QLatin1String(
"encoding" ) ) ) {
854 snd.
setData( line.value().toByteArray() );
855 }
else if ( params.contains( QLatin1String(
"value" ) ) ) {
856 if ( line.parameter( QLatin1String(
"value" ) ).toLower() == QLatin1String(
"uri" ) ) {
857 snd.
setUrl( line.value().toString() );
869 VCardLine VCardTool::createSound(
const Sound &snd )
const
871 VCardLine line( QLatin1String(
"SOUND" ) );
874 if ( !snd.
data().isEmpty() ) {
875 line.setValue( snd.
data() );
876 line.addParameter( QLatin1String(
"encoding" ), QLatin1String(
"b" ) );
879 }
else if ( !snd.
url().isEmpty() ) {
880 line.setValue( snd.
url() );
881 line.addParameter( QLatin1String(
"value" ), QLatin1String(
"URI" ) );
887 Key VCardTool::parseKey(
const VCardLine &line )
const
891 const QStringList params = line.parameterList();
892 if ( params.contains( QLatin1String(
"encoding" ) ) ) {
898 if ( params.contains( QLatin1String(
"type" ) ) ) {
899 if ( line.parameter( QLatin1String(
"type" ) ).toLower() == QLatin1String(
"x509" ) ) {
901 }
else if ( line.parameter( QLatin1String(
"type" ) ).toLower() == QLatin1String(
"pgp" ) ) {
912 VCardLine VCardTool::createKey(
const Key &key )
const
914 VCardLine line( QLatin1String(
"KEY" ) );
919 line.addParameter( QLatin1String(
"encoding" ), QLatin1String(
"b" ) );
921 }
else if ( !key.
textData().isEmpty() ) {
926 line.addParameter( QLatin1String(
"type" ), QLatin1String(
"X509" ) );
928 line.addParameter( QLatin1String(
"type" ), QLatin1String(
"PGP" ) );
936 Secrecy VCardTool::parseSecrecy(
const VCardLine &line )
const
940 const QString value = line.value().toString().toLower();
941 if ( value == QLatin1String(
"public" ) ) {
942 secrecy.setType( Secrecy::Public );
943 }
else if ( value == QLatin1String(
"private" ) ) {
944 secrecy.setType( Secrecy::Private );
945 }
else if ( value == QLatin1String(
"confidential" ) ) {
946 secrecy.setType( Secrecy::Confidential );
952 VCardLine VCardTool::createSecrecy(
const Secrecy &secrecy )
const
954 VCardLine line( QLatin1String(
"CLASS" ) );
956 int type = secrecy.type();
958 if ( type == Secrecy::Public ) {
959 line.setValue( QLatin1String(
"PUBLIC" ) );
960 }
else if ( type == Secrecy::Private ) {
961 line.setValue( QLatin1String(
"PRIVATE" ) );
962 }
else if ( type == Secrecy::Confidential ) {
963 line.setValue( QLatin1String(
"CONFIDENTIAL" ) );
969 QStringList VCardTool::splitString(
const QChar &sep,
const QString &str )
const
972 QString value( str );
975 int pos = value.indexOf( sep, start );
977 while ( pos != -1 ) {
978 if ( pos == 0 || value[ pos - 1 ] != QLatin1Char(
'\\' ) ) {
979 if ( pos > start && pos <= (
int)value.length() ) {
980 list << value.mid( start, pos - start );
986 pos = value.indexOf( sep, start );
988 value.replace( pos - 1, 2, sep );
989 pos = value.indexOf( sep, pos );
993 int l = value.length() - 1;
994 if ( value.mid( start, l - start + 1 ).length() > 0 ) {
995 list << value.mid( start, l - start + 1 );