• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

kabc

vcardtool.cpp

00001 /*
00002     This file is part of libkabc.
00003     Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018     Boston, MA 02110-1301, USA.
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     // ADR + LABEL
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     // BDAY
00139     card.addLine( VCardLine( "BDAY", createDateTime( (*addrIt).birthday() ) ) );
00140 
00141     // CATEGORIES
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     // CLASS
00154     if ( version == VCard::v3_0 ) {
00155       card.addLine( createSecrecy( (*addrIt).secrecy() ) );
00156     }
00157 
00158     // EMAIL
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     // FN
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     // GEO
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     // KEY
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     // LOGO
00194     card.addLine( createPicture( "LOGO", (*addrIt).logo() ) );
00195 
00196     // MAILER
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     // N
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     // NAME
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     // NICKNAME
00228     if ( version == VCard::v3_0 ) {
00229       card.addLine( VCardLine( "NICKNAME", (*addrIt).nickName() ) );
00230     }
00231 
00232     // NOTE
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     // ORG
00241     QStringList organization;
00242     organization.append( ( *addrIt ).organization().replace( ';', "\\;" ) );
00243     if ( !( *addrIt ).department().isEmpty() )
00244       organization.append( ( *addrIt ).department().replace( ';', "\\;" ) );
00245     VCardLine orgLine( "ORG", organization.join( ";" ) );
00246     if ( version == VCard::v2_1 && needsEncoding( organization.join( ";" ) ) ) {
00247       orgLine.addParameter( "charset", "UTF-8" );
00248       orgLine.addParameter( "encoding", "QUOTED-PRINTABLE" );
00249     }
00250     card.addLine( orgLine );
00251 
00252     // PHOTO
00253     card.addLine( createPicture( "PHOTO", (*addrIt).photo() ) );
00254 
00255     // PROID
00256     if ( version == VCard::v3_0 ) {
00257       card.addLine( VCardLine( "PRODID", (*addrIt).productId() ) );
00258     }
00259 
00260     // REV
00261     card.addLine( VCardLine( "REV", createDateTime( (*addrIt).revision() ) ) );
00262 
00263     // ROLE
00264     VCardLine roleLine( "ROLE", (*addrIt).role() );
00265     if ( version == VCard::v2_1 && needsEncoding( (*addrIt).role() ) ) {
00266       roleLine.addParameter( "charset", "UTF-8" );
00267       roleLine.addParameter( "encoding", "QUOTED-PRINTABLE" );
00268     }
00269     card.addLine( roleLine );
00270 
00271     // SORT-STRING
00272     if ( version == VCard::v3_0 ) {
00273       card.addLine( VCardLine( "SORT-STRING", (*addrIt).sortString() ) );
00274     }
00275 
00276     // SOUND
00277     card.addLine( createSound( (*addrIt).sound() ) );
00278 
00279     // TEL
00280     const PhoneNumber::List phoneNumbers = (*addrIt).phoneNumbers();
00281     PhoneNumber::List::ConstIterator phoneIt;
00282     for ( phoneIt = phoneNumbers.begin(); phoneIt != phoneNumbers.end(); ++phoneIt ) {
00283       VCardLine line( "TEL", (*phoneIt).number() );
00284 
00285       QMap<QString, PhoneNumber::TypeFlag>::ConstIterator typeIt;
00286       for ( typeIt = mPhoneTypeMap.constBegin(); typeIt != mPhoneTypeMap.constEnd(); ++typeIt ) {
00287         if ( typeIt.value() & (*phoneIt).type() ) {
00288           line.addParameter( "TYPE", typeIt.key() );
00289         }
00290       }
00291 
00292       card.addLine( line );
00293     }
00294 
00295     // TITLE
00296     VCardLine titleLine( "TITLE", (*addrIt).title() );
00297     if ( version == VCard::v2_1 && needsEncoding( (*addrIt).title() ) ) {
00298       titleLine.addParameter( "charset", "UTF-8" );
00299       titleLine.addParameter( "encoding", "QUOTED-PRINTABLE" );
00300     }
00301     card.addLine( titleLine );
00302 
00303     // TZ
00304     TimeZone timeZone = (*addrIt).timeZone();
00305     if ( timeZone.isValid() ) {
00306       QString str;
00307 
00308       int neg = 1;
00309       if ( timeZone.offset() < 0 ) {
00310         neg = -1;
00311       }
00312 
00313       str.sprintf( "%c%02d:%02d", ( timeZone.offset() >= 0 ? '+' : '-' ),
00314                                   ( timeZone.offset() / 60 ) * neg,
00315                                   ( timeZone.offset() % 60 ) * neg );
00316 
00317       card.addLine( VCardLine( "TZ", str ) );
00318     }
00319 
00320     // UID
00321     card.addLine( VCardLine( "UID", (*addrIt).uid() ) );
00322 
00323     // URL
00324     card.addLine( VCardLine( "URL", (*addrIt).url().url() ) );
00325 
00326     // VERSION
00327     if ( version == VCard::v2_1 ) {
00328       card.addLine( VCardLine( "VERSION", QString::fromLatin1( "2.1" ) ) );
00329     }
00330     if ( version == VCard::v3_0 ) {
00331       card.addLine( VCardLine( "VERSION", QString::fromLatin1( "3.0" ) ) );
00332     }
00333 
00334     // X-
00335     const QStringList customs = (*addrIt).customs();
00336     for ( strIt = customs.begin(); strIt != customs.end(); ++strIt ) {
00337       QString identifier = "X-" + (*strIt).left( (*strIt).indexOf( ":" ) );
00338       QString value = (*strIt).mid( (*strIt).indexOf( ":" ) + 1 );
00339       if ( value.isEmpty() ) {
00340         continue;
00341       }
00342 
00343       VCardLine line( identifier, value );
00344       if ( version == VCard::v2_1 && needsEncoding( value ) ) {
00345         line.addParameter( "charset", "UTF-8" );
00346         line.addParameter( "encoding", "QUOTED-PRINTABLE" );
00347       }
00348       card.addLine( line );
00349     }
00350 
00351     vCardList.append( card );
00352   }
00353 
00354   return VCardParser::createVCards( vCardList );
00355 }
00356 
00357 Addressee::List VCardTool::parseVCards( const QByteArray &vcard ) const
00358 {
00359   static const QChar semicolonSep( ';' );
00360   static const QChar commaSep( ',' );
00361   QString identifier;
00362 
00363   Addressee::List addrList;
00364   const VCard::List vCardList = VCardParser::parseVCards( vcard );
00365 
00366   VCard::List::ConstIterator cardIt;
00367   VCard::List::ConstIterator listEnd( vCardList.end() );
00368   for ( cardIt = vCardList.begin(); cardIt != listEnd; ++cardIt ) {
00369     Addressee addr;
00370 
00371     const QStringList idents = (*cardIt).identifiers();
00372     QStringList::ConstIterator identIt;
00373     QStringList::ConstIterator identEnd( idents.end() );
00374     for ( identIt = idents.begin(); identIt != identEnd; ++identIt ) {
00375       const VCardLine::List lines = (*cardIt).lines( (*identIt) );
00376       VCardLine::List::ConstIterator lineIt;
00377 
00378       // iterate over the lines
00379       for ( lineIt = lines.begin(); lineIt != lines.end(); ++lineIt ) {
00380         identifier = (*lineIt).identifier().toLower();
00381         // ADR
00382         if ( identifier == "adr" ) {
00383           Address address;
00384           const QStringList addrParts = splitString( semicolonSep, (*lineIt).value().toString() );
00385           if ( addrParts.count() > 0 ) {
00386             address.setPostOfficeBox( addrParts[ 0 ] );
00387           }
00388           if ( addrParts.count() > 1 ) {
00389             address.setExtended( addrParts[ 1 ] );
00390           }
00391           if ( addrParts.count() > 2 ) {
00392             address.setStreet( addrParts[ 2 ] );
00393           }
00394           if ( addrParts.count() > 3 ) {
00395             address.setLocality( addrParts[ 3 ] );
00396           }
00397           if ( addrParts.count() > 4 ) {
00398             address.setRegion( addrParts[ 4 ] );
00399           }
00400           if ( addrParts.count() > 5 ) {
00401             address.setPostalCode( addrParts[ 5 ] );
00402           }
00403           if ( addrParts.count() > 6 ) {
00404             address.setCountry( addrParts[ 6 ] );
00405           }
00406 
00407           Address::Type type;
00408 
00409           const QStringList types = (*lineIt).parameters( "type" );
00410           for ( QStringList::ConstIterator it = types.begin(); it != types.end(); ++it ) {
00411             type |= mAddressTypeMap[ (*it).toLower() ];
00412           }
00413 
00414           address.setType( type );
00415           addr.insertAddress( address );
00416         }
00417 
00418         // BDAY
00419         else if ( identifier == "bday" ) {
00420           addr.setBirthday( parseDateTime( (*lineIt).value().toString() ) );
00421         }
00422 
00423         // CATEGORIES
00424         else if ( identifier == "categories" ) {
00425           const QStringList categories = splitString( commaSep, (*lineIt).value().toString() );
00426           addr.setCategories( categories );
00427         }
00428 
00429         // CLASS
00430         else if ( identifier == "class" ) {
00431           addr.setSecrecy( parseSecrecy( *lineIt ) );
00432         }
00433 
00434         // EMAIL
00435         else if ( identifier == "email" ) {
00436           const QStringList types = (*lineIt).parameters( "type" );
00437           addr.insertEmail( (*lineIt).value().toString(), types.contains( "PREF" ) );
00438         }
00439 
00440         // FN
00441         else if ( identifier == "fn" ) {
00442           addr.setFormattedName( (*lineIt).value().toString() );
00443         }
00444 
00445         // GEO
00446         else if ( identifier == "geo" ) {
00447           Geo geo;
00448 
00449           const QStringList geoParts =
00450             (*lineIt).value().toString().split( ';', QString::KeepEmptyParts );
00451           geo.setLatitude( geoParts[ 0 ].toFloat() );
00452           geo.setLongitude( geoParts[ 1 ].toFloat() );
00453 
00454           addr.setGeo( geo );
00455         }
00456 
00457         // KEY
00458         else if ( identifier == "key" ) {
00459           addr.insertKey( parseKey( *lineIt ) );
00460         }
00461 
00462         // LABEL
00463         else if ( identifier == "label" ) {
00464           Address::Type type;
00465 
00466           const QStringList types = (*lineIt).parameters( "type" );
00467           for ( QStringList::ConstIterator it = types.begin(); it != types.end(); ++it ) {
00468             type |= mAddressTypeMap[ (*it).toLower() ];
00469           }
00470 
00471           bool available = false;
00472           KABC::Address::List addressList = addr.addresses();
00473           for ( KABC::Address::List::Iterator it = addressList.begin(); it != addressList.end(); ++it ) {
00474             if ( (*it).type() == type ) {
00475               (*it).setLabel( (*lineIt).value().toString() );
00476               addr.insertAddress( *it );
00477               available = true;
00478               break;
00479             }
00480           }
00481 
00482           if ( !available ) { // a standalone LABEL tag
00483             KABC::Address address( type );
00484             address.setLabel( (*lineIt).value().toString() );
00485             addr.insertAddress( address );
00486           }
00487         }
00488 
00489         // LOGO
00490         else if ( identifier == "logo" ) {
00491           addr.setLogo( parsePicture( *lineIt ) );
00492         }
00493 
00494         // MAILER
00495         else if ( identifier == "mailer" ) {
00496           addr.setMailer( (*lineIt).value().toString() );
00497         }
00498 
00499         // N
00500         else if ( identifier == "n" ) {
00501           const QStringList nameParts = splitString( semicolonSep, (*lineIt).value().toString() );
00502           if ( nameParts.count() > 0 ) {
00503             addr.setFamilyName( nameParts[ 0 ] );
00504           }
00505           if ( nameParts.count() > 1 ) {
00506             addr.setGivenName( nameParts[ 1 ] );
00507           }
00508           if ( nameParts.count() > 2 ) {
00509             addr.setAdditionalName( nameParts[ 2 ] );
00510           }
00511           if ( nameParts.count() > 3 ) {
00512             addr.setPrefix( nameParts[ 3 ] );
00513           }
00514           if ( nameParts.count() > 4 ) {
00515             addr.setSuffix( nameParts[ 4 ] );
00516           }
00517         }
00518 
00519         // NAME
00520         else if ( identifier == "name" ) {
00521           addr.setName( (*lineIt).value().toString() );
00522         }
00523 
00524         // NICKNAME
00525         else if ( identifier == "nickname" ) {
00526           addr.setNickName( (*lineIt).value().toString() );
00527         }
00528 
00529         // NOTE
00530         else if ( identifier == "note" ) {
00531           addr.setNote( (*lineIt).value().toString() );
00532         }
00533 
00534         // ORGANIZATION
00535         else if ( identifier == "org" ) {
00536           const QStringList orgParts = splitString( semicolonSep, (*lineIt).value().toString() );
00537           if ( orgParts.count() > 0 )
00538             addr.setOrganization( orgParts[ 0 ] );
00539           if ( orgParts.count() > 1 )
00540             addr.setDepartment( orgParts[ 1 ] );
00541         }
00542  
00543         // PHOTO
00544         else if ( identifier == "photo" ) {
00545           addr.setPhoto( parsePicture( *lineIt ) );
00546         }
00547 
00548         // PROID
00549         else if ( identifier == "prodid" ) {
00550           addr.setProductId( (*lineIt).value().toString() );
00551         }
00552 
00553         // REV
00554         else if ( identifier == "rev" ) {
00555           addr.setRevision( parseDateTime( (*lineIt).value().toString() ) );
00556         }
00557 
00558         // ROLE
00559         else if ( identifier == "role" ) {
00560           addr.setRole( (*lineIt).value().toString() );
00561         }
00562 
00563         // SORT-STRING
00564         else if ( identifier == "sort-string" ) {
00565           addr.setSortString( (*lineIt).value().toString() );
00566         }
00567 
00568         // SOUND
00569         else if ( identifier == "sound" ) {
00570           addr.setSound( parseSound( *lineIt ) );
00571         }
00572 
00573         // TEL
00574         else if ( identifier == "tel" ) {
00575           PhoneNumber phone;
00576           phone.setNumber( (*lineIt).value().toString() );
00577 
00578           PhoneNumber::Type type;
00579 
00580           const QStringList types = (*lineIt).parameters( "type" );
00581           for ( QStringList::ConstIterator it = types.begin(); it != types.end(); ++it ) {
00582             type |= mPhoneTypeMap[(*it).toUpper()];
00583           }
00584 
00585           phone.setType( type );
00586 
00587           addr.insertPhoneNumber( phone );
00588         }
00589 
00590         // TITLE
00591         else if ( identifier == "title" ) {
00592           addr.setTitle( (*lineIt).value().toString() );
00593         }
00594 
00595         // TZ
00596         else if ( identifier == "tz" ) {
00597           TimeZone tz;
00598           const QString date = (*lineIt).value().toString();
00599 
00600           int hours = date.mid( 1, 2 ).toInt();
00601           int minutes = date.mid( 4, 2 ).toInt();
00602           int offset = ( hours * 60 ) + minutes;
00603           offset = offset * ( date[ 0 ] == '+' ? 1 : -1 );
00604 
00605           tz.setOffset( offset );
00606           addr.setTimeZone( tz );
00607         }
00608 
00609         // UID
00610         else if ( identifier == "uid" ) {
00611           addr.setUid( (*lineIt).value().toString() );
00612         }
00613 
00614         // URL
00615         else if ( identifier == "url" ) {
00616           addr.setUrl( KUrl( (*lineIt).value().toString() ) );
00617         }
00618 
00619         // X-
00620         else if ( identifier.startsWith( "x-" ) ) {
00621           const QString key = (*lineIt).identifier().mid( 2 );
00622           int dash = key.indexOf( "-" );
00623           addr.insertCustom( key.left( dash ), key.mid( dash + 1 ), (*lineIt).value().toString() );
00624         }
00625       }
00626     }
00627 
00628     addrList.append( addr );
00629   }
00630 
00631   return addrList;
00632 }
00633 
00634 QDateTime VCardTool::parseDateTime( const QString &str ) const
00635 {
00636   QDateTime dateTime;
00637 
00638   if ( str.indexOf( '-' ) == -1 ) { // is base format (yyyymmdd)
00639     dateTime.setDate( QDate( str.left( 4 ).toInt(), str.mid( 4, 2 ).toInt(),
00640                              str.mid( 6, 2 ).toInt() ) );
00641 
00642     if ( str.indexOf( 'T' ) ) { // has time information yyyymmddThh:mm:ss
00643       dateTime.setTime( QTime( str.mid( 11, 2 ).toInt(), str.mid( 14, 2 ).toInt(),
00644                                str.mid( 17, 2 ).toInt() ) );
00645     }
00646   } else { // is extended format yyyy-mm-dd
00647     dateTime.setDate( QDate( str.left( 4 ).toInt(), str.mid( 5, 2 ).toInt(),
00648                              str.mid( 8, 2 ).toInt() ) );
00649 
00650     if ( str.indexOf( 'T' ) ) { // has time information yyyy-mm-ddThh:mm:ss
00651       dateTime.setTime( QTime( str.mid( 11, 2 ).toInt(), str.mid( 14, 2 ).toInt(),
00652                                str.mid( 17, 2 ).toInt() ) );
00653     }
00654   }
00655 
00656   return dateTime;
00657 }
00658 
00659 QString VCardTool::createDateTime( const QDateTime &dateTime ) const
00660 {
00661   QString str;
00662 
00663   if ( dateTime.date().isValid() ) {
00664     str.sprintf( "%4d-%02d-%02d", dateTime.date().year(), dateTime.date().month(),
00665                  dateTime.date().day() );
00666     if ( dateTime.time().isValid() ) {
00667       QString tmp;
00668       tmp.sprintf( "T%02d:%02d:%02dZ", dateTime.time().hour(), dateTime.time().minute(),
00669                    dateTime.time().second() );
00670       str += tmp;
00671     }
00672   }
00673 
00674   return str;
00675 }
00676 
00677 Picture VCardTool::parsePicture( const VCardLine &line ) const
00678 {
00679   Picture pic;
00680 
00681   const QStringList params = line.parameterList();
00682   if ( params.contains( "encoding" ) ) {
00683     QImage img;
00684     img.loadFromData( line.value().toByteArray() );
00685     pic.setData( img );
00686   } else if ( params.contains( "value" ) ) {
00687     if ( line.parameter( "value" ).toLower() == "uri" ) {
00688       pic.setUrl( line.value().toString() );
00689     }
00690   }
00691 
00692   if ( params.contains( "type" ) ) {
00693     pic.setType( line.parameter( "type" ) );
00694   }
00695 
00696   return pic;
00697 }
00698 
00699 VCardLine VCardTool::createPicture( const QString &identifier, const Picture &pic ) const
00700 {
00701   VCardLine line( identifier );
00702 
00703   if ( pic.isIntern() ) {
00704     if ( !pic.data().isNull() ) {
00705       QByteArray input;
00706       QBuffer buffer( &input );
00707       buffer.open( QIODevice::WriteOnly );
00708       pic.data().save( &buffer, "JPEG" );
00709 
00710       line.setValue( input );
00711       line.addParameter( "encoding", "b" );
00712       line.addParameter( "type", "image/jpeg" );
00713     }
00714   } else if ( !pic.url().isEmpty() ) {
00715     line.setValue( pic.url() );
00716     line.addParameter( "value", "URI" );
00717   }
00718 
00719   return line;
00720 }
00721 
00722 Sound VCardTool::parseSound( const VCardLine &line ) const
00723 {
00724   Sound snd;
00725 
00726   const QStringList params = line.parameterList();
00727   if ( params.contains( "encoding" ) ) {
00728     snd.setData( line.value().toByteArray() );
00729   } else if ( params.contains( "value" ) ) {
00730     if ( line.parameter( "value" ).toLower() == "uri" ) {
00731       snd.setUrl( line.value().toString() );
00732     }
00733   }
00734 
00735 /* TODO: support sound types
00736   if ( params.contains( "type" ) )
00737     snd.setType( line.parameter( "type" ) );
00738 */
00739 
00740   return snd;
00741 }
00742 
00743 VCardLine VCardTool::createSound( const Sound &snd ) const
00744 {
00745   VCardLine line( "SOUND" );
00746 
00747   if ( snd.isIntern() ) {
00748     if ( !snd.data().isEmpty() ) {
00749       line.setValue( snd.data() );
00750       line.addParameter( "encoding", "b" );
00751       // TODO: need to store sound type!!!
00752     }
00753   } else if ( !snd.url().isEmpty() ) {
00754     line.setValue( snd.url() );
00755     line.addParameter( "value", "URI" );
00756   }
00757 
00758   return line;
00759 }
00760 
00761 Key VCardTool::parseKey( const VCardLine &line ) const
00762 {
00763   Key key;
00764 
00765   const QStringList params = line.parameterList();
00766   if ( params.contains( "encoding" ) ) {
00767     key.setBinaryData( line.value().toByteArray() );
00768   } else {
00769     key.setTextData( line.value().toString() );
00770   }
00771 
00772   if ( params.contains( "type" ) ) {
00773     if ( line.parameter( "type" ).toLower() == "x509" ) {
00774       key.setType( Key::X509 );
00775     } else if ( line.parameter( "type" ).toLower() == "pgp" ) {
00776       key.setType( Key::PGP );
00777     } else {
00778       key.setType( Key::Custom );
00779       key.setCustomTypeString( line.parameter( "type" ) );
00780     }
00781   }
00782 
00783   return key;
00784 }
00785 
00786 VCardLine VCardTool::createKey( const Key &key ) const
00787 {
00788   VCardLine line( "KEY" );
00789 
00790   if ( key.isBinary() ) {
00791     if ( !key.binaryData().isEmpty() ) {
00792       line.setValue( key.binaryData() );
00793       line.addParameter( "encoding", "b" );
00794     }
00795   } else if ( !key.textData().isEmpty() ) {
00796     line.setValue( key.textData() );
00797   }
00798 
00799   if ( key.type() == Key::X509 ) {
00800     line.addParameter( "type", "X509" );
00801   } else if ( key.type() == Key::PGP ) {
00802     line.addParameter( "type", "PGP" );
00803   } else if ( key.type() == Key::Custom ) {
00804     line.addParameter( "type", key.customTypeString() );
00805   }
00806 
00807   return line;
00808 }
00809 
00810 Secrecy VCardTool::parseSecrecy( const VCardLine &line ) const
00811 {
00812   Secrecy secrecy;
00813 
00814   const QString value = line.value().toString().toLower();
00815   if ( value == "public" ) {
00816     secrecy.setType( Secrecy::Public );
00817   } else if ( value == "private" ) {
00818     secrecy.setType( Secrecy::Private );
00819   } else if ( value == "confidential" ) {
00820     secrecy.setType( Secrecy::Confidential );
00821   }
00822 
00823   return secrecy;
00824 }
00825 
00826 VCardLine VCardTool::createSecrecy( const Secrecy &secrecy ) const
00827 {
00828   VCardLine line( "CLASS" );
00829 
00830   int type = secrecy.type();
00831 
00832   if ( type == Secrecy::Public ) {
00833     line.setValue( QString::fromLatin1( "PUBLIC" ) );
00834   } else if ( type == Secrecy::Private ) {
00835     line.setValue( QString::fromLatin1( "PRIVATE" ) );
00836   } else if ( type == Secrecy::Confidential ) {
00837     line.setValue( QString::fromLatin1( "CONFIDENTIAL" ) );
00838   }
00839 
00840   return line;
00841 }
00842 
00843 QStringList VCardTool::splitString( const QChar &sep, const QString &str ) const
00844 {
00845   QStringList list;
00846   QString value( str );
00847 
00848   int start = 0;
00849   int pos = value.indexOf( sep, start );
00850 
00851   while ( pos != -1 ) {
00852     if ( pos == 0 || value[ pos - 1 ] != '\\' ) {
00853       if ( pos > start && pos <= (int)value.length() ) {
00854         list << value.mid( start, pos - start );
00855       } else {
00856         list << QString();
00857       }
00858 
00859       start = pos + 1;
00860       pos = value.indexOf( sep, start );
00861     } else {
00862       value.replace( pos - 1, 2, sep );
00863       pos = value.indexOf( sep, pos );
00864     }
00865   }
00866 
00867   int l = value.length() - 1;
00868   if ( value.mid( start, l - start + 1 ).length() > 0 ) {
00869     list << value.mid( start, l - start + 1 );
00870   } else {
00871     list << QString();
00872   }
00873 
00874   return list;
00875 }

kabc

Skip menu "kabc"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.5
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal