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 <qbuffer.h>
00022 #include <qdatastream.h>
00023 #include <qstring.h>
00024 
00025 #include "agent.h"
00026 #include "key.h"
00027 #include "picture.h"
00028 #include "secrecy.h"
00029 #include "sound.h"
00030 
00031 #include "vcardtool.h"
00032 
00033 using namespace KABC;
00034 
00035 static bool needsEncoding( const QString &value )
00036 {
00037   uint length = value.length();
00038   for ( uint i = 0; i < length; ++i ) {
00039     char c = value.at( i ).latin1();
00040     if ( (c < 33 || c > 126) && c != ' ' && c != '=' )
00041       return true;
00042   }
00043 
00044   return false;
00045 }
00046 
00047 VCardTool::VCardTool()
00048 {
00049   mAddressTypeMap.insert( "dom", Address::Dom );
00050   mAddressTypeMap.insert( "intl", Address::Intl );
00051   mAddressTypeMap.insert( "postal", Address::Postal );
00052   mAddressTypeMap.insert( "parcel", Address::Parcel );
00053   mAddressTypeMap.insert( "home", Address::Home );
00054   mAddressTypeMap.insert( "work", Address::Work );
00055   mAddressTypeMap.insert( "pref", Address::Pref );
00056 
00057   mPhoneTypeMap.insert( "HOME", PhoneNumber::Home );
00058   mPhoneTypeMap.insert( "WORK", PhoneNumber::Work );
00059   mPhoneTypeMap.insert( "MSG", PhoneNumber::Msg );
00060   mPhoneTypeMap.insert( "PREF", PhoneNumber::Pref );
00061   mPhoneTypeMap.insert( "VOICE", PhoneNumber::Voice );
00062   mPhoneTypeMap.insert( "FAX", PhoneNumber::Fax );
00063   mPhoneTypeMap.insert( "CELL", PhoneNumber::Cell );
00064   mPhoneTypeMap.insert( "VIDEO", PhoneNumber::Video );
00065   mPhoneTypeMap.insert( "BBS", PhoneNumber::Bbs );
00066   mPhoneTypeMap.insert( "MODEM", PhoneNumber::Modem );
00067   mPhoneTypeMap.insert( "CAR", PhoneNumber::Car );
00068   mPhoneTypeMap.insert( "ISDN", PhoneNumber::Isdn );
00069   mPhoneTypeMap.insert( "PCS", PhoneNumber::Pcs );
00070   mPhoneTypeMap.insert( "PAGER", PhoneNumber::Pager );
00071 }
00072 
00073 VCardTool::~VCardTool()
00074 {
00075 }
00076 
00077 // TODO: make list a const&
00078 QString VCardTool::createVCards( Addressee::List list, VCard::Version version )
00079 {
00080   VCard::List vCardList;
00081 
00082   Addressee::List::ConstIterator addrIt;
00083   Addressee::List::ConstIterator listEnd( list.constEnd() );
00084   for ( addrIt = list.constBegin(); addrIt != listEnd; ++addrIt ) {
00085     VCard card;
00086     QStringList::ConstIterator strIt;
00087 
00088     // ADR + LABEL
00089     const Address::List addresses = (*addrIt).addresses();
00090     for ( Address::List::ConstIterator it = addresses.begin(); it != addresses.end(); ++it ) {
00091       QStringList address;
00092 
00093       bool isEmpty = ( (*it).postOfficeBox().isEmpty() &&
00094                      (*it).extended().isEmpty() &&
00095                      (*it).street().isEmpty() &&
00096                      (*it).locality().isEmpty() &&
00097                      (*it).region().isEmpty() &&
00098                      (*it).postalCode().isEmpty() &&
00099                      (*it).country().isEmpty() );
00100 
00101       address.append( (*it).postOfficeBox().replace( ';', "\\;" ) );
00102       address.append( (*it).extended().replace( ';', "\\;" ) );
00103       address.append( (*it).street().replace( ';', "\\;" ) );
00104       address.append( (*it).locality().replace( ';', "\\;" ) );
00105       address.append( (*it).region().replace( ';', "\\;" ) );
00106       address.append( (*it).postalCode().replace( ';', "\\;" ) );
00107       address.append( (*it).country().replace( ';', "\\;" ) );
00108 
00109       VCardLine adrLine( "ADR", address.join( ";" ) );
00110       if ( version == VCard::v2_1 && needsEncoding( address.join( ";" ) ) ) {
00111         adrLine.addParameter( "charset", "UTF-8" );
00112         adrLine.addParameter( "encoding", "QUOTED-PRINTABLE" );
00113       }
00114 
00115       VCardLine labelLine( "LABEL", (*it).label() );
00116       if ( version == VCard::v2_1 && needsEncoding( (*it).label() ) ) {
00117         labelLine.addParameter( "charset", "UTF-8" );
00118         labelLine.addParameter( "encoding", "QUOTED-PRINTABLE" );
00119       }
00120 
00121       bool hasLabel = !(*it).label().isEmpty();
00122       QMap<QString, int>::ConstIterator typeIt;
00123       for ( typeIt = mAddressTypeMap.constBegin(); typeIt != mAddressTypeMap.constEnd(); ++typeIt ) {
00124         if ( typeIt.data() & (*it).type() ) {
00125           adrLine.addParameter( "TYPE", typeIt.key() );
00126           if ( hasLabel )
00127             labelLine.addParameter( "TYPE",  typeIt.key() );
00128         }
00129       }
00130 
00131       if ( !isEmpty )
00132         card.addLine( adrLine );
00133       if ( hasLabel )
00134         card.addLine( labelLine );
00135     }
00136 
00137     // AGENT
00138     card.addLine( createAgent( version, (*addrIt).agent() ) );
00139 
00140     // BDAY
00141     card.addLine( VCardLine( "BDAY", createDateTime( (*addrIt).birthday() ) ) );
00142 
00143     // CATEGORIES
00144     if ( version == VCard::v3_0 ) {
00145       QStringList categories = (*addrIt).categories();
00146       QStringList::Iterator catIt;
00147       for ( catIt = categories.begin(); catIt != categories.end(); ++catIt )
00148         (*catIt).replace( ',', "\\," );
00149 
00150       VCardLine catLine( "CATEGORIES", categories.join( "," ) );
00151       if ( version == VCard::v2_1 && needsEncoding( categories.join( "," ) ) ) {
00152         catLine.addParameter( "charset", "UTF-8" );
00153         catLine.addParameter( "encoding", "QUOTED-PRINTABLE" );
00154       }
00155 
00156       card.addLine( catLine );
00157     }
00158 
00159     // CLASS
00160     if ( version == VCard::v3_0 ) {
00161       card.addLine( createSecrecy( (*addrIt).secrecy() ) );
00162     }
00163 
00164     // EMAIL
00165     const QStringList emails = (*addrIt).emails();
00166     bool pref = true;
00167     for ( strIt = emails.begin(); strIt != emails.end(); ++strIt ) {
00168       VCardLine line( "EMAIL", *strIt );
00169       if ( pref == true && emails.count() > 1 ) {
00170         line.addParameter( "TYPE", "PREF" );
00171         pref = false;
00172       }
00173       card.addLine( line );
00174     }
00175 
00176     // FN
00177     VCardLine fnLine( "FN", (*addrIt).formattedName() );
00178     if ( version == VCard::v2_1 && needsEncoding( (*addrIt).formattedName() ) ) {
00179       fnLine.addParameter( "charset", "UTF-8" );
00180       fnLine.addParameter( "encoding", "QUOTED-PRINTABLE" );
00181     }
00182     card.addLine( fnLine );
00183 
00184     // GEO
00185     Geo geo = (*addrIt).geo();
00186     if ( geo.isValid() ) {
00187       QString str;
00188       str.sprintf( "%.6f;%.6f", geo.latitude(), geo.longitude() );
00189       card.addLine( VCardLine( "GEO", str ) );
00190     }
00191 
00192     // KEY
00193     const Key::List keys = (*addrIt).keys();
00194     Key::List::ConstIterator keyIt;
00195     for ( keyIt = keys.begin(); keyIt != keys.end(); ++keyIt )
00196       card.addLine( createKey( *keyIt ) );
00197 
00198     // LOGO
00199     card.addLine( createPicture( "LOGO", (*addrIt).logo() ) );
00200 
00201     // MAILER
00202     VCardLine mailerLine( "MAILER", (*addrIt).mailer() );
00203     if ( version == VCard::v2_1 && needsEncoding( (*addrIt).mailer() ) ) {
00204       mailerLine.addParameter( "charset", "UTF-8" );
00205       mailerLine.addParameter( "encoding", "QUOTED-PRINTABLE" );
00206     }
00207     card.addLine( mailerLine );
00208 
00209     // N
00210     QStringList name;
00211     name.append( (*addrIt).familyName().replace( ';', "\\;" ) );
00212     name.append( (*addrIt).givenName().replace( ';', "\\;" ) );
00213     name.append( (*addrIt).additionalName().replace( ';', "\\;" ) );
00214     name.append( (*addrIt).prefix().replace( ';', "\\;" ) );
00215     name.append( (*addrIt).suffix().replace( ';', "\\;" ) );
00216 
00217     VCardLine nLine( "N", name.join( ";" ) );
00218     if ( version == VCard::v2_1 && needsEncoding( name.join( ";" ) ) ) {
00219       nLine.addParameter( "charset", "UTF-8" );
00220       nLine.addParameter( "encoding", "QUOTED-PRINTABLE" );
00221     }
00222     card.addLine( nLine );
00223 
00224     // NAME
00225     VCardLine nameLine( "NAME", (*addrIt).name() );
00226     if ( version == VCard::v2_1 && needsEncoding( (*addrIt).name() ) ) {
00227       nameLine.addParameter( "charset", "UTF-8" );
00228       nameLine.addParameter( "encoding", "QUOTED-PRINTABLE" );
00229     }
00230     card.addLine( nameLine );
00231 
00232     // NICKNAME
00233     if ( version == VCard::v3_0 )
00234       card.addLine( VCardLine( "NICKNAME", (*addrIt).nickName() ) );
00235 
00236     // NOTE
00237     VCardLine noteLine( "NOTE", (*addrIt).note() );
00238     if ( version == VCard::v2_1 && needsEncoding( (*addrIt).note() ) ) {
00239       noteLine.addParameter( "charset", "UTF-8" );
00240       noteLine.addParameter( "encoding", "QUOTED-PRINTABLE" );
00241     }
00242     card.addLine( noteLine );
00243 
00244     // ORG
00245     QStringList organization;
00246     organization.append( ( *addrIt ).organization().replace( ';', "\\;" ) );
00247     if ( !( *addrIt ).department().isEmpty() )
00248       organization.append( ( *addrIt ).department().replace( ';', "\\;" ) );
00249     VCardLine orgLine( "ORG", organization.join( ";" ) );
00250     if ( version == VCard::v2_1 && needsEncoding( organization.join( ";" ) ) ) {
00251       orgLine.addParameter( "charset", "UTF-8" );
00252       orgLine.addParameter( "encoding", "QUOTED-PRINTABLE" );
00253     }
00254     card.addLine( orgLine );
00255 
00256     // PHOTO
00257     card.addLine( createPicture( "PHOTO", (*addrIt).photo() ) );
00258 
00259     // PROID
00260     if ( version == VCard::v3_0 )
00261       card.addLine( VCardLine( "PRODID", (*addrIt).productId() ) );
00262 
00263     // REV
00264     card.addLine( VCardLine( "REV", createDateTime( (*addrIt).revision() ) ) );
00265 
00266     // ROLE
00267     VCardLine roleLine( "ROLE", (*addrIt).role() );
00268     if ( version == VCard::v2_1 && needsEncoding( (*addrIt).role() ) ) {
00269       roleLine.addParameter( "charset", "UTF-8" );
00270       roleLine.addParameter( "encoding", "QUOTED-PRINTABLE" );
00271     }
00272     card.addLine( roleLine );
00273 
00274     // SORT-STRING
00275     if ( version == VCard::v3_0 )
00276       card.addLine( VCardLine( "SORT-STRING", (*addrIt).sortString() ) );
00277 
00278     // SOUND
00279     card.addLine( createSound( (*addrIt).sound() ) );
00280 
00281     // TEL
00282     const PhoneNumber::List phoneNumbers = (*addrIt).phoneNumbers();
00283     PhoneNumber::List::ConstIterator phoneIt;
00284     for ( phoneIt = phoneNumbers.begin(); phoneIt != phoneNumbers.end(); ++phoneIt ) {
00285       VCardLine line( "TEL", (*phoneIt).number() );
00286 
00287       QMap<QString, int>::ConstIterator typeIt;
00288       for ( typeIt = mPhoneTypeMap.constBegin(); typeIt != mPhoneTypeMap.constEnd(); ++typeIt ) {
00289         if ( typeIt.data() & (*phoneIt).type() )
00290           line.addParameter( "TYPE", typeIt.key() );
00291       }
00292 
00293       card.addLine( line );
00294     }
00295 
00296     // TITLE
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     // TZ
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       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", "2.1" ) );
00329     if ( version == VCard::v3_0 )
00330       card.addLine( VCardLine( "VERSION", "3.0" ) );
00331 
00332     // X-
00333     const QStringList customs = (*addrIt).customs();
00334     for ( strIt = customs.begin(); strIt != customs.end(); ++strIt ) {
00335       QString identifier = "X-" + (*strIt).left( (*strIt).find( ":" ) );
00336       QString value = (*strIt).mid( (*strIt).find( ":" ) + 1 );
00337       if ( value.isEmpty() )
00338         continue;
00339 
00340       VCardLine line( identifier, value );
00341       if ( version == VCard::v2_1 && needsEncoding( value ) ) {
00342         line.addParameter( "charset", "UTF-8" );
00343         line.addParameter( "encoding", "QUOTED-PRINTABLE" );
00344       }
00345       card.addLine( line );
00346     }
00347 
00348     vCardList.append( card );
00349   }
00350 
00351   return VCardParser::createVCards( vCardList );
00352 }
00353 
00354 Addressee::List VCardTool::parseVCards( const QString& vcard )
00355 {
00356   static const QChar semicolonSep( ';' );
00357   static const QChar commaSep( ',' );
00358   QString identifier;
00359 
00360   Addressee::List addrList;
00361   const VCard::List vCardList = VCardParser::parseVCards( vcard );
00362 
00363   VCard::List::ConstIterator cardIt;
00364   VCard::List::ConstIterator listEnd( vCardList.end() );
00365   for ( cardIt = vCardList.begin(); cardIt != listEnd; ++cardIt ) {
00366     Addressee addr;
00367 
00368     const QStringList idents = (*cardIt).identifiers();
00369     QStringList::ConstIterator identIt;
00370     QStringList::ConstIterator identEnd( idents.end() );
00371     for ( identIt = idents.begin(); identIt != identEnd; ++identIt ) {
00372       const VCardLine::List lines = (*cardIt).lines( (*identIt) );
00373       VCardLine::List::ConstIterator lineIt;
00374 
00375       // iterate over the lines
00376       for ( lineIt = lines.begin(); lineIt != lines.end(); ++lineIt ) {
00377         identifier = (*lineIt).identifier().lower();
00378         // ADR
00379         if ( identifier == "adr" ) {
00380           Address address;
00381           const QStringList addrParts = splitString( semicolonSep, (*lineIt).value().asString() );
00382           if ( addrParts.count() > 0 )
00383             address.setPostOfficeBox( addrParts[ 0 ] );
00384           if ( addrParts.count() > 1 )
00385             address.setExtended( addrParts[ 1 ] );
00386           if ( addrParts.count() > 2 )
00387             address.setStreet( addrParts[ 2 ] );
00388           if ( addrParts.count() > 3 )
00389             address.setLocality( addrParts[ 3 ] );
00390           if ( addrParts.count() > 4 )
00391             address.setRegion( addrParts[ 4 ] );
00392           if ( addrParts.count() > 5 )
00393             address.setPostalCode( addrParts[ 5 ] );
00394           if ( addrParts.count() > 6 )
00395             address.setCountry( addrParts[ 6 ] );
00396 
00397           int type = 0;
00398 
00399           const QStringList types = (*lineIt).parameters( "type" );
00400           for ( QStringList::ConstIterator it = types.begin(); it != types.end(); ++it )
00401             type += mAddressTypeMap[ (*it).lower() ];
00402 
00403           address.setType( type );
00404           addr.insertAddress( address );
00405         }
00406 
00407         // AGENT
00408         else if ( identifier == "agent" )
00409           addr.setAgent( parseAgent( *lineIt ) );
00410 
00411         // BDAY
00412         else if ( identifier == "bday" )
00413           addr.setBirthday( parseDateTime( (*lineIt).value().asString() ) );
00414 
00415         // CATEGORIES
00416         else if ( identifier == "categories" ) {
00417           const QStringList categories = splitString( commaSep, (*lineIt).value().asString() );
00418           addr.setCategories( categories );
00419         }
00420 
00421         // CLASS
00422         else if ( identifier == "class" )
00423           addr.setSecrecy( parseSecrecy( *lineIt ) );
00424 
00425         // EMAIL
00426         else if ( identifier == "email" ) {
00427           const QStringList types = (*lineIt).parameters( "type" );
00428           addr.insertEmail( (*lineIt).value().asString(), types.findIndex( "PREF" ) != -1 );
00429         }
00430 
00431         // FN
00432         else if ( identifier == "fn" )
00433           addr.setFormattedName( (*lineIt).value().asString() );
00434 
00435         // GEO
00436         else if ( identifier == "geo" ) {
00437           Geo geo;
00438 
00439           const QStringList geoParts = QStringList::split( ';', (*lineIt).value().asString(), true );
00440           geo.setLatitude( geoParts[ 0 ].toFloat() );
00441           geo.setLongitude( geoParts[ 1 ].toFloat() );
00442 
00443           addr.setGeo( geo );
00444         }
00445 
00446         // KEY
00447         else if ( identifier == "key" )
00448           addr.insertKey( parseKey( *lineIt ) );
00449 
00450         // LABEL
00451         else if ( identifier == "label" ) {
00452           int type = 0;
00453 
00454           const QStringList types = (*lineIt).parameters( "type" );
00455           for ( QStringList::ConstIterator it = types.begin(); it != types.end(); ++it )
00456             type += mAddressTypeMap[ (*it).lower() ];
00457 
00458           bool available = false;
00459           KABC::Address::List addressList = addr.addresses();
00460           KABC::Address::List::Iterator it;
00461           for ( it = addressList.begin(); it != addressList.end(); ++it ) {
00462             if ( (*it).type() == type ) {
00463               (*it).setLabel( (*lineIt).value().asString() );
00464               addr.insertAddress( *it );
00465               available = true;
00466               break;
00467             }
00468           }
00469 
00470           if ( !available ) { // a standalone LABEL tag
00471             KABC::Address address( type );
00472             address.setLabel( (*lineIt).value().asString() );
00473             addr.insertAddress( address );
00474           }
00475         }
00476 
00477         // LOGO
00478         else if ( identifier == "logo" )
00479           addr.setLogo( parsePicture( *lineIt ) );
00480 
00481         // MAILER
00482         else if ( identifier == "mailer" )
00483           addr.setMailer( (*lineIt).value().asString() );
00484 
00485         // N
00486         else if ( identifier == "n" ) {
00487           const QStringList nameParts = splitString( semicolonSep, (*lineIt).value().asString() );
00488           if ( nameParts.count() > 0 )
00489             addr.setFamilyName( nameParts[ 0 ] );
00490           if ( nameParts.count() > 1 )
00491             addr.setGivenName( nameParts[ 1 ] );
00492           if ( nameParts.count() > 2 )
00493             addr.setAdditionalName( nameParts[ 2 ] );
00494           if ( nameParts.count() > 3 )
00495             addr.setPrefix( nameParts[ 3 ] );
00496           if ( nameParts.count() > 4 )
00497             addr.setSuffix( nameParts[ 4 ] );
00498         }
00499 
00500         // NAME
00501         else if ( identifier == "name" )
00502           addr.setName( (*lineIt).value().asString() );
00503 
00504         // NICKNAME
00505         else if ( identifier == "nickname" )
00506           addr.setNickName( (*lineIt).value().asString() );
00507 
00508         // NOTE
00509         else if ( identifier == "note" )
00510           addr.setNote( (*lineIt).value().asString() );
00511 
00512         // ORGANIZATION
00513         else if ( identifier == "org" ) {
00514           const QStringList orgParts = splitString( semicolonSep, (*lineIt).value().asString() );
00515           if ( orgParts.count() > 0 )
00516             addr.setOrganization( orgParts[ 0 ] );
00517           if ( orgParts.count() > 1 )
00518             addr.setDepartment( orgParts[ 1 ] );
00519         }
00520 
00521         // PHOTO
00522         else if ( identifier == "photo" )
00523           addr.setPhoto( parsePicture( *lineIt ) );
00524 
00525         // PROID
00526         else if ( identifier == "prodid" )
00527           addr.setProductId( (*lineIt).value().asString() );
00528 
00529         // REV
00530         else if ( identifier == "rev" )
00531           addr.setRevision( parseDateTime( (*lineIt).value().asString() ) );
00532 
00533         // ROLE
00534         else if ( identifier == "role" )
00535           addr.setRole( (*lineIt).value().asString() );
00536 
00537         // SORT-STRING
00538         else if ( identifier == "sort-string" )
00539           addr.setSortString( (*lineIt).value().asString() );
00540 
00541         // SOUND
00542         else if ( identifier == "sound" )
00543           addr.setSound( parseSound( *lineIt ) );
00544 
00545         // TEL
00546         else if ( identifier == "tel" ) {
00547           PhoneNumber phone;
00548           phone.setNumber( (*lineIt).value().asString() );
00549 
00550           int type = 0;
00551 
00552           const QStringList types = (*lineIt).parameters( "type" );
00553           for ( QStringList::ConstIterator it = types.begin(); it != types.end(); ++it )
00554             type += mPhoneTypeMap[(*it).upper()];
00555 
00556           phone.setType( type );
00557 
00558           addr.insertPhoneNumber( phone );
00559         }
00560 
00561         // TITLE
00562         else if ( identifier == "title" )
00563           addr.setTitle( (*lineIt).value().asString() );
00564 
00565         // TZ
00566         else if ( identifier == "tz" ) {
00567           TimeZone tz;
00568           const QString date = (*lineIt).value().asString();
00569 
00570           int hours = date.mid( 1, 2).toInt();
00571           int minutes = date.mid( 4, 2 ).toInt();
00572           int offset = ( hours * 60 ) + minutes;
00573           offset = offset * ( date[ 0 ] == '+' ? 1 : -1 );
00574 
00575           tz.setOffset( offset );
00576           addr.setTimeZone( tz );
00577         }
00578 
00579         // UID
00580         else if ( identifier == "uid" )
00581           addr.setUid( (*lineIt).value().asString() );
00582 
00583         // URL
00584         else if ( identifier == "url" )
00585           addr.setUrl( KURL( (*lineIt).value().asString() ) );
00586 
00587         // X-
00588         else if ( identifier.startsWith( "x-" ) ) {
00589           const QString key = (*lineIt).identifier().mid( 2 );
00590           int dash = key.find( "-" );
00591           addr.insertCustom( key.left( dash ), key.mid( dash + 1 ), (*lineIt).value().asString() );
00592         }
00593       }
00594     }
00595 
00596     addrList.append( addr );
00597   }
00598 
00599   return addrList;
00600 }
00601 
00602 QDateTime VCardTool::parseDateTime( const QString &str )
00603 {
00604   QDateTime dateTime;
00605 
00606   if ( str.find( '-' ) == -1 ) { // is base format (yyyymmdd)
00607     dateTime.setDate( QDate( str.left( 4 ).toInt(), str.mid( 4, 2 ).toInt(),
00608                              str.mid( 6, 2 ).toInt() ) );
00609 
00610     if ( str.find( 'T' ) ) // has time information yyyymmddThh:mm:ss
00611       dateTime.setTime( QTime( str.mid( 11, 2 ).toInt(), str.mid( 14, 2 ).toInt(),
00612                                str.mid( 17, 2 ).toInt() ) );
00613 
00614   } else { // is extended format yyyy-mm-dd
00615     dateTime.setDate( QDate( str.left( 4 ).toInt(), str.mid( 5, 2 ).toInt(),
00616                              str.mid( 8, 2 ).toInt() ) );
00617 
00618     if ( str.find( 'T' ) ) // has time information yyyy-mm-ddThh:mm:ss
00619       dateTime.setTime( QTime( str.mid( 11, 2 ).toInt(), str.mid( 14, 2 ).toInt(),
00620                                str.mid( 17, 2 ).toInt() ) );
00621   }
00622 
00623   return dateTime;
00624 }
00625 
00626 QString VCardTool::createDateTime( const QDateTime &dateTime )
00627 {
00628   QString str;
00629 
00630   if ( dateTime.date().isValid() ) {
00631     str.sprintf( "%4d-%02d-%02d", dateTime.date().year(), dateTime.date().month(),
00632                  dateTime.date().day() );
00633     if ( dateTime.time().isValid() ) {
00634       QString tmp;
00635       tmp.sprintf( "T%02d:%02d:%02dZ", dateTime.time().hour(), dateTime.time().minute(),
00636                    dateTime.time().second() );
00637       str += tmp;
00638     }
00639   }
00640 
00641   return str;
00642 }
00643 
00644 Picture VCardTool::parsePicture( const VCardLine &line )
00645 {
00646   Picture pic;
00647 
00648   const QStringList params = line.parameterList();
00649   if ( params.findIndex( "encoding" ) != -1 ) {
00650     QImage img;
00651     img.loadFromData( line.value().asByteArray() );
00652     pic.setData( img );
00653   } else if ( params.findIndex( "value" ) != -1 ) {
00654     if ( line.parameter( "value" ).lower() == "uri" )
00655       pic.setUrl( line.value().asString() );
00656   }
00657 
00658   if ( params.findIndex( "type" ) != -1 )
00659     pic.setType( line.parameter( "type" ) );
00660 
00661   return pic;
00662 }
00663 
00664 VCardLine VCardTool::createPicture( const QString &identifier, const Picture &pic )
00665 {
00666   VCardLine line( identifier );
00667 
00668   if ( pic.isIntern() ) {
00669     if ( !pic.data().isNull() ) {
00670       QByteArray input;
00671       QBuffer buffer( input );
00672       buffer.open( IO_WriteOnly );
00673 
00674       QImageIO iio( &buffer, "JPEG" );
00675       iio.setImage( pic.data() );
00676       iio.setQuality( 100 );
00677       iio.write();
00678 
00679       line.setValue( input );
00680       line.addParameter( "encoding", "b" );
00681       line.addParameter( "type", "image/jpeg" );
00682     }
00683   } else if ( !pic.url().isEmpty() ) {
00684     line.setValue( pic.url() );
00685     line.addParameter( "value", "URI" );
00686   }
00687 
00688   return line;
00689 }
00690 
00691 Sound VCardTool::parseSound( const VCardLine &line )
00692 {
00693   Sound snd;
00694 
00695   const QStringList params = line.parameterList();
00696   if ( params.findIndex( "encoding" ) != -1 )
00697     snd.setData( line.value().asByteArray() );
00698   else if ( params.findIndex( "value" ) != -1 ) {
00699     if ( line.parameter( "value" ).lower() == "uri" )
00700       snd.setUrl( line.value().asString() );
00701   }
00702 
00703 /* TODO: support sound types
00704   if ( params.contains( "type" ) )
00705     snd.setType( line.parameter( "type" ) );
00706 */
00707 
00708   return snd;
00709 }
00710 
00711 VCardLine VCardTool::createSound( const Sound &snd )
00712 {
00713   VCardLine line( "SOUND" );
00714 
00715   if ( snd.isIntern() ) {
00716     if ( !snd.data().isEmpty() ) {
00717       line.setValue( snd.data() );
00718       line.addParameter( "encoding", "b" );
00719       // TODO: need to store sound type!!!
00720     }
00721   } else if ( !snd.url().isEmpty() ) {
00722     line.setValue( snd.url() );
00723     line.addParameter( "value", "URI" );
00724   }
00725 
00726   return line;
00727 }
00728 
00729 Key VCardTool::parseKey( const VCardLine &line )
00730 {
00731   Key key;
00732 
00733   const QStringList params = line.parameterList();
00734   if ( params.findIndex( "encoding" ) != -1 )
00735     key.setBinaryData( line.value().asByteArray() );
00736   else
00737     key.setTextData( line.value().asString() );
00738 
00739   if ( params.findIndex( "type" ) != -1 ) {
00740     if ( line.parameter( "type" ).lower() == "x509" )
00741       key.setType( Key::X509 );
00742     else if ( line.parameter( "type" ).lower() == "pgp" )
00743       key.setType( Key::PGP );
00744     else {
00745       key.setType( Key::Custom );
00746       key.setCustomTypeString( line.parameter( "type" ) );
00747     }
00748   }
00749 
00750   return key;
00751 }
00752 
00753 VCardLine VCardTool::createKey( const Key &key )
00754 {
00755   VCardLine line( "KEY" );
00756 
00757   if ( key.isBinary() ) {
00758     if ( !key.binaryData().isEmpty() ) {
00759       line.setValue( key.binaryData() );
00760       line.addParameter( "encoding", "b" );
00761     }
00762   } else if ( !key.textData().isEmpty() )
00763     line.setValue( key.textData() );
00764 
00765   if ( key.type() == Key::X509 )
00766     line.addParameter( "type", "X509" );
00767   else if ( key.type() == Key::PGP )
00768     line.addParameter( "type", "PGP" );
00769   else if ( key.type() == Key::Custom )
00770     line.addParameter( "type", key.customTypeString() );
00771 
00772   return line;
00773 }
00774 
00775 Secrecy VCardTool::parseSecrecy( const VCardLine &line )
00776 {
00777   Secrecy secrecy;
00778 
00779   if ( line.value().asString().lower() == "public" )
00780     secrecy.setType( Secrecy::Public );
00781   if ( line.value().asString().lower() == "private" )
00782     secrecy.setType( Secrecy::Private );
00783   if ( line.value().asString().lower() == "confidential" )
00784     secrecy.setType( Secrecy::Confidential );
00785 
00786   return secrecy;
00787 }
00788 
00789 VCardLine VCardTool::createSecrecy( const Secrecy &secrecy )
00790 {
00791   VCardLine line( "CLASS" );
00792 
00793   int type = secrecy.type();
00794 
00795   if ( type == Secrecy::Public )
00796     line.setValue( "PUBLIC" );
00797   else if ( type == Secrecy::Private )
00798     line.setValue( "PRIVATE" );
00799   else if ( type == Secrecy::Confidential )
00800     line.setValue( "CONFIDENTIAL" );
00801 
00802   return line;
00803 }
00804 
00805 Agent VCardTool::parseAgent( const VCardLine &line )
00806 {
00807   Agent agent;
00808 
00809   const QStringList params = line.parameterList();
00810   if ( params.findIndex( "value" ) != -1 ) {
00811     if ( line.parameter( "value" ).lower() == "uri" )
00812       agent.setUrl( line.value().asString() );
00813   } else {
00814     QString str = line.value().asString();
00815     str.replace( "\\n", "\r\n" );
00816     str.replace( "\\N", "\r\n" );
00817     str.replace( "\\;", ";" );
00818     str.replace( "\\:", ":" );
00819     str.replace( "\\,", "," );
00820 
00821     const Addressee::List list = parseVCards( str );
00822     if ( list.count() > 0 ) {
00823       Addressee *addr = new Addressee;
00824       *addr = list[ 0 ];
00825       agent.setAddressee( addr );
00826     }
00827   }
00828 
00829   return agent;
00830 }
00831 
00832 VCardLine VCardTool::createAgent( VCard::Version version, const Agent &agent )
00833 {
00834   VCardLine line( "AGENT" );
00835 
00836   if ( agent.isIntern() ) {
00837     if ( agent.addressee() != 0 ) {
00838       Addressee::List list;
00839       list.append( *agent.addressee() );
00840 
00841       QString str = createVCards( list, version );
00842       str.replace( "\r\n", "\\n" );
00843       str.replace( ";", "\\;" );
00844       str.replace( ":", "\\:" );
00845       str.replace( ",", "\\," );
00846       line.setValue( str );
00847     }
00848   } else if ( !agent.url().isEmpty() ) {
00849     line.setValue( agent.url() );
00850     line.addParameter( "value", "URI" );
00851   }
00852 
00853   return line;
00854 }
00855 
00856 QStringList VCardTool::splitString( const QChar &sep, const QString &str )
00857 {
00858   QStringList list;
00859   QString value( str );
00860 
00861   int start = 0;
00862   int pos = value.find( sep, start );
00863 
00864   while ( pos != -1 ) {
00865     if ( value[ pos - 1 ] != '\\' ) {
00866       if ( pos > start && pos <= (int)value.length() )
00867         list << value.mid( start, pos - start );
00868       else
00869         list << QString::null;
00870 
00871       start = pos + 1;
00872       pos = value.find( sep, start );
00873     } else {
00874       if ( pos != 0 ) {
00875         value.replace( pos - 1, 2, sep );
00876         pos = value.find( sep, pos );
00877       } else
00878         pos = value.find( sep, pos + 1 );
00879     }
00880   }
00881 
00882   int l = value.length() - 1;
00883   if ( value.mid( start, l - start + 1 ).length() > 0 )
00884     list << value.mid( start, l - start + 1 );
00885   else
00886     list << QString::null;
00887 
00888   return list;
00889 }
KDE Home | KDE Accessibility Home | Description of Access Keys