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");
378 }
else if ( identifier == QLatin1String(
"X-messaging/irc-All" ) ) {
379 identifier = QLatin1String(
"X-IRC" );
383 VCardLine line( identifier, value );
384 if ( version == VCard::v2_1 && needsEncoding( value ) ) {
385 line.addParameter( QLatin1String(
"charset" ), QLatin1String(
"UTF-8" ) );
386 line.addParameter( QLatin1String(
"encoding" ), QLatin1String(
"QUOTED-PRINTABLE" ) );
388 card.addLine( line );
391 vCardList.append( card );
394 return VCardParser::createVCards( vCardList );
397 Addressee::List VCardTool::parseVCards(
const QByteArray &vcard )
const
399 static const QLatin1Char semicolonSep(
';' );
400 static const QLatin1Char commaSep(
',' );
403 Addressee::List addrList;
404 const VCard::List vCardList = VCardParser::parseVCards( vcard );
406 VCard::List::ConstIterator cardIt;
407 VCard::List::ConstIterator listEnd( vCardList.end() );
408 for ( cardIt = vCardList.begin(); cardIt != listEnd; ++cardIt ) {
411 const QStringList idents = (*cardIt).identifiers();
412 QStringList::ConstIterator identIt;
413 QStringList::ConstIterator identEnd( idents.end() );
414 for ( identIt = idents.begin(); identIt != identEnd; ++identIt ) {
415 const VCardLine::List lines = (*cardIt).lines( (*identIt) );
416 VCardLine::List::ConstIterator lineIt;
419 for ( lineIt = lines.begin(); lineIt != lines.end(); ++lineIt ) {
420 identifier = (*lineIt).identifier().toLower();
422 if ( identifier == QLatin1String(
"adr" ) ) {
424 const QStringList addrParts = splitString( semicolonSep, (*lineIt).value().toString() );
425 if ( addrParts.count() > 0 ) {
428 if ( addrParts.count() > 1 ) {
431 if ( addrParts.count() > 2 ) {
434 if ( addrParts.count() > 3 ) {
437 if ( addrParts.count() > 4 ) {
440 if ( addrParts.count() > 5 ) {
443 if ( addrParts.count() > 6 ) {
449 const QStringList types = (*lineIt).parameters( QLatin1String(
"type" ) );
450 for ( QStringList::ConstIterator it = types.begin(); it != types.end(); ++it ) {
451 type |= mAddressTypeMap[ (*it).toLower() ];
455 addr.insertAddress( address );
459 else if ( identifier == QLatin1String(
"bday" ) ) {
460 addr.setBirthday( parseDateTime( (*lineIt).value().toString() ) );
464 else if ( identifier == QLatin1String(
"categories" ) ) {
465 const QStringList categories = splitString( commaSep, (*lineIt).value().toString() );
466 addr.setCategories( categories );
470 else if ( identifier == QLatin1String(
"class" ) ) {
471 addr.setSecrecy( parseSecrecy( *lineIt ) );
475 else if ( identifier == QLatin1String(
"email" ) ) {
476 const QStringList types = (*lineIt).parameters( QLatin1String(
"type" ) );
477 addr.insertEmail( (*lineIt).value().toString(),
478 types.contains( QLatin1String(
"PREF" ) ) );
482 else if ( identifier == QLatin1String(
"fn" ) ) {
483 addr.setFormattedName( (*lineIt).value().toString() );
487 else if ( identifier == QLatin1String(
"geo" ) ) {
490 const QStringList geoParts =
491 (*lineIt).value().
toString().split( QLatin1Char(
';' ), QString::KeepEmptyParts );
492 if ( geoParts.size() >= 2 ) {
500 else if ( identifier == QLatin1String(
"key" ) ) {
501 addr.insertKey( parseKey( *lineIt ) );
505 else if ( identifier == QLatin1String(
"label" ) ) {
508 const QStringList types = (*lineIt).parameters( QLatin1String(
"type" ) );
509 for ( QStringList::ConstIterator it = types.begin(); it != types.end(); ++it ) {
510 type |= mAddressTypeMap[ (*it).toLower() ];
513 bool available =
false;
515 for ( KABC::Address::List::Iterator it = addressList.begin();
516 it != addressList.end(); ++it ) {
517 if ( (*it).type() == type ) {
518 (*it).setLabel( (*lineIt).value().toString() );
519 addr.insertAddress( *it );
527 address.
setLabel( (*lineIt).value().toString() );
528 addr.insertAddress( address );
533 else if ( identifier == QLatin1String(
"logo" ) ) {
534 addr.setLogo( parsePicture( *lineIt ) );
538 else if ( identifier == QLatin1String(
"mailer" ) ) {
539 addr.setMailer( (*lineIt).value().toString() );
543 else if ( identifier == QLatin1String(
"n" ) ) {
544 const QStringList nameParts = splitString( semicolonSep, (*lineIt).value().toString() );
545 const int numberOfParts(nameParts.count());
546 if ( numberOfParts > 0 ) {
547 addr.setFamilyName( nameParts[ 0 ] );
549 if ( numberOfParts > 1 ) {
550 addr.setGivenName( nameParts[ 1 ] );
552 if ( numberOfParts > 2 ) {
553 addr.setAdditionalName( nameParts[ 2 ] );
555 if ( numberOfParts > 3 ) {
556 addr.setPrefix( nameParts[ 3 ] );
558 if ( numberOfParts > 4 ) {
559 addr.setSuffix( nameParts[ 4 ] );
564 else if ( identifier == QLatin1String(
"name" ) ) {
565 addr.setName( (*lineIt).value().toString() );
569 else if ( identifier == QLatin1String(
"nickname" ) ) {
570 addr.setNickName( (*lineIt).value().toString() );
574 else if ( identifier == QLatin1String(
"note" ) ) {
575 addr.setNote( (*lineIt).value().toString() );
579 else if ( identifier == QLatin1String(
"org" ) ) {
580 const QStringList orgParts = splitString( semicolonSep, (*lineIt).value().toString() );
581 if ( orgParts.count() > 0 ) {
582 addr.setOrganization( orgParts[ 0 ] );
584 if ( orgParts.count() > 1 ) {
585 addr.setDepartment( orgParts[ 1 ] );
590 else if ( identifier == QLatin1String(
"photo" ) ) {
591 addr.setPhoto( parsePicture( *lineIt ) );
595 else if ( identifier == QLatin1String(
"prodid" ) ) {
596 addr.setProductId( (*lineIt).value().toString() );
600 else if ( identifier == QLatin1String(
"rev" ) ) {
601 addr.setRevision( parseDateTime( (*lineIt).value().toString() ) );
605 else if ( identifier == QLatin1String(
"role" ) ) {
606 addr.setRole( (*lineIt).value().toString() );
610 else if ( identifier == QLatin1String(
"sort-string" ) ) {
611 addr.setSortString( (*lineIt).value().toString() );
615 else if ( identifier == QLatin1String(
"sound" ) ) {
616 addr.setSound( parseSound( *lineIt ) );
620 else if ( identifier == QLatin1String(
"tel" ) ) {
622 phone.
setNumber( (*lineIt).value().toString() );
624 PhoneNumber::Type type;
626 const QStringList types = (*lineIt).parameters( QLatin1String(
"type" ) );
627 QStringList::ConstIterator typeEnd(types.end());
628 for ( QStringList::ConstIterator it = types.begin(); it != typeEnd; ++it ) {
629 type |= mPhoneTypeMap[(*it).toUpper()];
634 addr.insertPhoneNumber( phone );
638 else if ( identifier == QLatin1String(
"title" ) ) {
639 addr.setTitle( (*lineIt).value().toString() );
643 else if ( identifier == QLatin1String(
"tz" ) ) {
645 const QString date = (*lineIt).value().
toString();
647 if ( !date.isEmpty() ) {
648 int hours = date.mid( 1, 2 ).toInt();
649 int minutes = date.mid( 4, 2 ).toInt();
650 int offset = ( hours * 60 ) + minutes;
651 offset = offset * ( date[ 0 ] == QLatin1Char(
'+' ) ? 1 : -1 );
654 addr.setTimeZone( tz );
659 else if ( identifier == QLatin1String(
"uid" ) ) {
660 addr.setUid( (*lineIt).value().toString() );
664 else if ( identifier == QLatin1String(
"url" ) ) {
665 addr.setUrl( KUrl( (*lineIt).value().toString() ) );
669 else if ( identifier.startsWith( QLatin1String(
"x-" ) ) ) {
670 QString ident = ( *lineIt ).identifier();
672 if(identifier==QLatin1String(
"x-evolution-spouse") || identifier == QLatin1String(
"x-spouse")) {
673 ident = QLatin1String(
"X-KADDRESSBOOK-X-SpousesName");
674 }
else if(identifier == QLatin1String(
"x-evolution-blog-url")) {
675 ident = QLatin1String(
"X-KADDRESSBOOK-BlogFeed");
676 }
else if(identifier == QLatin1String(
"x-evolution-assistant") || identifier == QLatin1String(
"x-assistant")) {
677 ident = QLatin1String(
"X-KADDRESSBOOK-X-AssistantsName");
678 }
else if(identifier == QLatin1String(
"x-evolution-anniversary") || identifier == QLatin1String(
"x-anniversary")) {
679 ident = QLatin1String(
"X-KADDRESSBOOK-X-Anniversary");
680 }
else if(identifier == QLatin1String(
"x-evolution-manager") || identifier == QLatin1String(
"x-manager")) {
681 ident = QLatin1String(
"X-KADDRESSBOOK-X-ManagersName");
682 }
else if(identifier == QLatin1String(
"x-aim")) {
683 ident = QLatin1String(
"X-messaging/aim-All");
684 }
else if(identifier == QLatin1String(
"x-icq")) {
685 ident = QLatin1String(
"X-messaging/icq-All");
686 }
else if(identifier == QLatin1String(
"x-jabber")) {
687 ident = QLatin1String(
"X-messaging/xmpp-All");
688 }
else if(identifier == QLatin1String(
"x-jabber")) {
689 ident = QLatin1String(
"X-messaging/xmpp-All");
690 }
else if(identifier == QLatin1String(
"x-msn")) {
691 ident = QLatin1String(
"X-messaging/msn-All");
692 }
else if(identifier == QLatin1String(
"x-yahoo")) {
693 ident = QLatin1String(
"X-messaging/yahoo-All");
694 }
else if(identifier == QLatin1String(
"x-gadugadu")) {
695 ident = QLatin1String(
"X-messaging/gadu-All");
696 }
else if(identifier == QLatin1String(
"x-skype")) {
697 ident = QLatin1String(
"X-messaging/skype-All");
698 }
else if(identifier == QLatin1String(
"x-groupwise")) {
699 ident = QLatin1String(
"X-messaging/groupwise-All");
700 }
else if(identifier == QLatin1String(
"x-sms")) {
701 ident = QLatin1String(
"X-messaging/sms-All");
702 }
else if(identifier == QLatin1String(
"x-meanwhile")) {
703 ident = QLatin1String(
"X-messaging/meanwhile-All");
704 }
else if ( identifier == QLatin1String(
"x-irc" ) ) {
705 ident = QLatin1String(
"X-messaging/irc-All" );
708 const QString key = ident.mid( 2 );
709 const int dash = key.indexOf( QLatin1Char(
'-' ) );
710 addr.insertCustom( key.left( dash ), key.mid( dash + 1 ), ( *lineIt ).value().toString() );
715 addrList.append( addr );
721 QDateTime VCardTool::parseDateTime(
const QString &str )
const
726 if ( str.indexOf( QLatin1Char(
'-' ) ) == -1 ) {
727 date = QDate( str.left( 4 ).toInt(), str.mid( 4, 2 ).toInt(),
728 str.mid( 6, 2 ).toInt() );
730 date = QDate( str.left( 4 ).toInt(), str.mid( 5, 2 ).toInt(),
731 str.mid( 8, 2 ).toInt() );
735 int timeStart = str.indexOf( QLatin1Char(
'T' ) );
736 if ( timeStart >= 0 ) {
737 int hour = 0, minute = 0, second = 0;
739 hour = str.mid( timeStart + 1, 2 ).toInt();
741 if ( str.indexOf( QLatin1Char(
':' ), timeStart + 1 ) > 0 ) {
742 if ( str.length() >= ( timeStart + 5 ) ) {
743 minute = str.mid( timeStart + 4, 2 ).toInt();
744 if ( str.length() >= ( timeStart + 8 ) ) {
745 second = str.mid( timeStart + 7, 2 ).toInt();
749 if ( str.length() >= ( timeStart + 4 ) ) {
750 minute = str.mid( timeStart + 3, 2 ).toInt();
751 if ( str.length() >= ( timeStart + 6 ) ) {
752 second = str.mid( timeStart + 5, 2 ).toInt();
757 time = QTime( hour, minute, second );
760 Qt::TimeSpec spec = ( str.right( 1 ) == QLatin1String(
"Z" ) ) ? Qt::UTC : Qt::LocalTime;
762 QDateTime dateTime(date);
770 dateTime.setTime(time);
772 dateTime.setTimeSpec(spec);
776 QString VCardTool::createDateTime(
const QDateTime &dateTime )
const
780 if ( dateTime.date().isValid() ) {
781 str.sprintf(
"%4d-%02d-%02d", dateTime.date().year(), dateTime.date().month(),
782 dateTime.date().day() );
783 if ( dateTime.time().isValid() ) {
785 tmp.sprintf(
"T%02d:%02d:%02d", dateTime.time().hour(), dateTime.time().minute(),
786 dateTime.time().second() );
789 if ( dateTime.timeSpec() == Qt::UTC ) {
790 str += QLatin1Char(
'Z' );
798 Picture VCardTool::parsePicture(
const VCardLine &line )
const
802 const QStringList params = line.parameterList();
803 if ( params.contains( QLatin1String(
"encoding" ) ) ) {
805 img.loadFromData( line.value().toByteArray() );
807 }
else if ( params.contains( QLatin1String(
"value" ) ) ) {
808 if ( line.parameter( QLatin1String(
"value" ) ).toLower() == QLatin1String(
"uri" ) ) {
809 pic.
setUrl( line.value().toString() );
813 if ( params.contains( QLatin1String(
"type" ) ) ) {
814 pic.
setType( line.parameter( QLatin1String(
"type" ) ) );
820 VCardLine VCardTool::createPicture(
const QString &identifier,
const Picture &pic )
const
822 VCardLine line( identifier );
825 if ( !pic.
data().isNull() ) {
827 QBuffer buffer( &input );
828 buffer.open( QIODevice::WriteOnly );
830 if ( !pic.
data().hasAlphaChannel() ) {
831 pic.
data().save( &buffer,
"JPEG" );
833 line.setValue( input );
834 line.addParameter( QLatin1String(
"encoding" ), QLatin1String(
"b" ) );
835 line.addParameter( QLatin1String(
"type" ), QLatin1String(
"jpeg" ) );
837 pic.
data().save( &buffer,
"PNG" );
839 line.setValue( input );
840 line.addParameter( QLatin1String(
"encoding" ), QLatin1String(
"b" ) );
841 line.addParameter( QLatin1String(
"type" ), QLatin1String(
"png" ) );
844 }
else if ( !pic.
url().isEmpty() ) {
845 line.setValue( pic.
url() );
846 line.addParameter( QLatin1String(
"value" ), QLatin1String(
"URI" ) );
852 Sound VCardTool::parseSound(
const VCardLine &line )
const
856 const QStringList params = line.parameterList();
857 if ( params.contains( QLatin1String(
"encoding" ) ) ) {
858 snd.
setData( line.value().toByteArray() );
859 }
else if ( params.contains( QLatin1String(
"value" ) ) ) {
860 if ( line.parameter( QLatin1String(
"value" ) ).toLower() == QLatin1String(
"uri" ) ) {
861 snd.
setUrl( line.value().toString() );
873 VCardLine VCardTool::createSound(
const Sound &snd )
const
875 VCardLine line( QLatin1String(
"SOUND" ) );
878 if ( !snd.
data().isEmpty() ) {
879 line.setValue( snd.
data() );
880 line.addParameter( QLatin1String(
"encoding" ), QLatin1String(
"b" ) );
883 }
else if ( !snd.
url().isEmpty() ) {
884 line.setValue( snd.
url() );
885 line.addParameter( QLatin1String(
"value" ), QLatin1String(
"URI" ) );
891 Key VCardTool::parseKey(
const VCardLine &line )
const
895 const QStringList params = line.parameterList();
896 if ( params.contains( QLatin1String(
"encoding" ) ) ) {
902 if ( params.contains( QLatin1String(
"type" ) ) ) {
903 if ( line.parameter( QLatin1String(
"type" ) ).toLower() == QLatin1String(
"x509" ) ) {
905 }
else if ( line.parameter( QLatin1String(
"type" ) ).toLower() == QLatin1String(
"pgp" ) ) {
916 VCardLine VCardTool::createKey(
const Key &key )
const
918 VCardLine line( QLatin1String(
"KEY" ) );
923 line.addParameter( QLatin1String(
"encoding" ), QLatin1String(
"b" ) );
925 }
else if ( !key.
textData().isEmpty() ) {
930 line.addParameter( QLatin1String(
"type" ), QLatin1String(
"X509" ) );
932 line.addParameter( QLatin1String(
"type" ), QLatin1String(
"PGP" ) );
940 Secrecy VCardTool::parseSecrecy(
const VCardLine &line )
const
944 const QString value = line.value().toString().toLower();
945 if ( value == QLatin1String(
"public" ) ) {
946 secrecy.setType( Secrecy::Public );
947 }
else if ( value == QLatin1String(
"private" ) ) {
948 secrecy.setType( Secrecy::Private );
949 }
else if ( value == QLatin1String(
"confidential" ) ) {
950 secrecy.setType( Secrecy::Confidential );
956 VCardLine VCardTool::createSecrecy(
const Secrecy &secrecy )
const
958 VCardLine line( QLatin1String(
"CLASS" ) );
960 int type = secrecy.type();
962 if ( type == Secrecy::Public ) {
963 line.setValue( QLatin1String(
"PUBLIC" ) );
964 }
else if ( type == Secrecy::Private ) {
965 line.setValue( QLatin1String(
"PRIVATE" ) );
966 }
else if ( type == Secrecy::Confidential ) {
967 line.setValue( QLatin1String(
"CONFIDENTIAL" ) );
973 QStringList VCardTool::splitString(
const QChar &sep,
const QString &str )
const
976 QString value( str );
979 int pos = value.indexOf( sep, start );
981 while ( pos != -1 ) {
982 if ( pos == 0 || value[ pos - 1 ] != QLatin1Char(
'\\' ) ) {
983 if ( pos > start && pos <= (
int)value.length() ) {
984 list << value.mid( start, pos - start );
990 pos = value.indexOf( sep, start );
992 value.replace( pos - 1, 2, sep );
993 pos = value.indexOf( sep, pos );
997 int l = value.length() - 1;
998 if ( value.mid( start, l - start + 1 ).length() > 0 ) {
999 list << value.mid( start, l - start + 1 );