kabc Library API Documentation

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