vcard.cpp

00001 /*
00002   Copyright (c) 2006 by Jakob Schroeter <js@camaya.net>
00003   This file is part of the gloox library. http://camaya.net/gloox
00004 
00005   This software is distributed under a license. The full license
00006   agreement can be found in the file LICENSE in this distribution.
00007   This software may not be copied, modified, sold or distributed
00008   other than expressed in the named license agreement.
00009 
00010   This software is distributed without any warranty.
00011 */
00012 
00013 
00014 #include "vcard.h"
00015 #include "tag.h"
00016 #include "base64.h"
00017 
00018 namespace gloox
00019 {
00020 
00021   VCard::VCard()
00022     : m_class( ClassNone ), m_prodid( "gloox" + GLOOX_VERSION ),
00023       m_N( false ), m_PHOTO( false ), m_LOGO( false )
00024   {
00025   }
00026 
00027   VCard::VCard( Tag *vcard )
00028     : m_class( ClassNone ), m_prodid( "gloox" + GLOOX_VERSION ),
00029       m_N( false ), m_PHOTO( false ), m_LOGO( false )
00030   {
00031     checkField( vcard, "FN", m_formattedname );
00032     checkField( vcard, "NICKNAME", m_nickname );
00033     checkField( vcard, "URL", m_url );
00034     checkField( vcard, "BDAY", m_bday );
00035     checkField( vcard, "JABBERID", m_jabberid );
00036     checkField( vcard, "TITLE", m_title );
00037     checkField( vcard, "ROLE", m_role );
00038     checkField( vcard, "NOTE", m_note );
00039     checkField( vcard, "DESC", m_desc );
00040     checkField( vcard, "MAILER", m_mailer );
00041     checkField( vcard, "TZ", m_tz );
00042     checkField( vcard, "PRODID", m_prodid );
00043     checkField( vcard, "REV", m_rev );
00044     checkField( vcard, "SORT-STRING", m_sortstring );
00045     checkField( vcard, "UID", m_uid );
00046 
00047     Tag::TagList::const_iterator it = vcard->children().begin();
00048     for( ; it != vcard->children().end(); ++it )
00049     {
00050       if( (*it)->name() == "N" )
00051       {
00052         m_N = true;
00053         if( (*it)->hasChild( "FAMILY" ) )
00054           m_name.family = (*it)->findChild( "FAMILY" )->cdata();
00055         if( (*it)->hasChild( "GIVEN" ) )
00056           m_name.given = (*it)->findChild( "GIVEN" )->cdata();
00057         if( (*it)->hasChild( "MIDDLE" ) )
00058           m_name.middle = (*it)->findChild( "MIDDLE" )->cdata();
00059         if( (*it)->hasChild( "PREFIX" ) )
00060           m_name.prefix = (*it)->findChild( "PREFIX" )->cdata();
00061         if( (*it)->hasChild( "SUFFIX" ) )
00062           m_name.suffix = (*it)->findChild( "SUFFIX" )->cdata();
00063       }
00064 
00065       if( (*it)->name() == "PHOTO" )
00066       {
00067         if( (*it)->hasChild( "EXTVAL" ) )
00068         {
00069           m_photo.extval = (*it)->findChild( "EXTVAL" )->cdata();
00070           m_PHOTO = true;
00071         }
00072         else if( (*it)->hasChild( "TYPE" ) && (*it)->hasChild( "BINVAL" ) )
00073         {
00074           m_photo.type = (*it)->findChild( "TYPE" )->cdata();
00075           m_photo.binval = Base64::decode64( (*it)->findChild( "BINVAL" )->cdata() );
00076           m_PHOTO = true;
00077         }
00078       }
00079 
00080       if( (*it)->name() == "LOGO" )
00081       {
00082         if( (*it)->hasChild( "EXTVAL" ) )
00083         {
00084           m_logo.extval = (*it)->findChild( "EXTVAL" )->cdata();
00085           m_LOGO = true;
00086         }
00087         else if( (*it)->hasChild( "TYPE" ) && (*it)->hasChild( "BINVAL" ) )
00088         {
00089           m_logo.type = (*it)->findChild( "TYPE" )->cdata();
00090           m_logo.binval = Base64::decode64( (*it)->findChild( "BINVAL" )->cdata() );
00091           m_LOGO = true;
00092         }
00093       }
00094 
00095       if( (*it)->name() == "EMAIL" && (*it)->hasChild( "USERID" ) )
00096       {
00097         Email item;
00098         item.userid = (*it)->findChild( "USERID" )->cdata();
00099         item.internet = ( (*it)->hasChild( "INTERNET" ) )?( true ):( false );
00100         item.x400 = ( (*it)->hasChild( "X400" ) )?( true ):( false );
00101         item.work = ( (*it)->hasChild( "WORK" ) )?( true ):( false );
00102         item.home = ( (*it)->hasChild( "HOME" ) )?( true ):( false );
00103         item.pref = ( (*it)->hasChild( "PREF" ) )?( true ):( false );
00104         m_emailList.push_back( item );
00105       }
00106 
00107       if( (*it)->name() == "ADR" )
00108       {
00109         Address item;
00110         checkField( (*it), "POBOX", item.pobox );
00111         checkField( (*it), "EXTADD", item.extadd );
00112         checkField( (*it), "STREET", item.street );
00113         checkField( (*it), "LOCALITY", item.locality );
00114         checkField( (*it), "REGION", item.region );
00115         checkField( (*it), "PCODE", item.pcode );
00116         checkField( (*it), "CTRY", item.ctry );
00117         item.postal = ( (*it)->hasChild( "POSTAL" ) )?( true ):( false );
00118         item.parcel = ( (*it)->hasChild( "PARCEL" ) )?( true ):( false );
00119         item.work = ( (*it)->hasChild( "WORK" ) )?( true ):( false );
00120         item.home = ( (*it)->hasChild( "HOME" ) )?( true ):( false );
00121         item.pref = ( (*it)->hasChild( "PREF" ) )?( true ):( false );
00122         item.dom = ( (*it)->hasChild( "DOM" ) )?( true ):( false );
00123         item.intl = ( !item.dom && (*it)->hasChild( "INTL" ) )?( true ):( false );
00124         m_addressList.push_back( item );
00125       }
00126 
00127       if( (*it)->name() == "TEL" && (*it)->hasChild( "NUMBER" ) )
00128       {
00129         Telephone item;
00130         item.number = (*it)->findChild( "NUMBER" )->cdata();
00131         item.work = ( (*it)->hasChild( "WORK" ) )?( true ):( false );
00132         item.home = ( (*it)->hasChild( "HOME" ) )?( true ):( false );
00133         item.voice = ( (*it)->hasChild( "VOICE" ) )?( true ):( false );
00134         item.fax = ( (*it)->hasChild( "FAX" ) )?( true ):( false );
00135         item.pager = ( (*it)->hasChild( "PAGER" ) )?( true ):( false );
00136         item.msg = ( (*it)->hasChild( "MSG" ) )?( true ):( false );
00137         item.cell = ( (*it)->hasChild( "CELL" ) )?( true ):( false );
00138         item.video = ( (*it)->hasChild( "VIDEO" ) )?( true ):( false );
00139         item.bbs = ( (*it)->hasChild( "BBS" ) )?( true ):( false );
00140         item.modem = ( (*it)->hasChild( "MODEM" ) )?( true ):( false );
00141         item.isdn = ( (*it)->hasChild( "ISDN" ) )?( true ):( false );
00142         item.pcs = ( (*it)->hasChild( "PCS" ) )?( true ):( false );
00143         item.pref = ( (*it)->hasChild( "PREF" ) )?( true ):( false );
00144         m_telephoneList.push_back( item );
00145       }
00146 
00147       if( (*it)->name() == "ORG" )
00148       {
00149         Tag::TagList::const_iterator ito = (*it)->children().begin();
00150         for( ; ito != (*it)->children().end(); ++ito )
00151         {
00152           if( (*ito)->name() == "ORGNAME" )
00153             m_org.name = (*ito)->cdata();
00154           else if( (*ito)->name() == "ORGUNIT" )
00155             m_org.units.push_back( (*ito)->cdata() );
00156         }
00157       }
00158 
00159       if( (*it)->name() == "GEO" )
00160       {
00161         checkField( (*it), "LON", m_geo.longitude );
00162         checkField( (*it), "LAT", m_geo.latitude );
00163       }
00164 
00165       if( (*it)->name() == "CLASS" )
00166       {
00167         if( (*it)->hasChild( "PRIVATE" ) )
00168           m_class = ClassPrivate;
00169         else if( (*it)->hasChild( "PUBLIC" ) )
00170           m_class = ClassPublic;
00171         else if( (*it)->hasChild( "CONFIDENTIAL" ) )
00172           m_class = ClassConfidential;
00173       }
00174 
00175     }
00176 
00177   }
00178 
00179   VCard::~VCard()
00180   {
00181   }
00182 
00183   void VCard::checkField( Tag *vcard, const std::string& field, std::string& var )
00184   {
00185     if( vcard->hasChild( field ) )
00186       var = vcard->findChild( field )->cdata();
00187   }
00188 
00189   void VCard::setName( const std::string& family, const std::string& given, const std::string& middle,
00190                        const std::string& prefix, const std::string& suffix )
00191   {
00192     m_name.family = family;
00193     m_name.given = given;
00194     m_name.middle = middle;
00195     m_name.prefix = prefix;
00196     m_name.suffix = suffix;
00197     m_N = true;
00198   }
00199 
00200   void VCard::setPhoto( const std::string& extval )
00201   {
00202     if( !extval.empty() )
00203     {
00204       m_photo.extval= extval;
00205       m_PHOTO = true;
00206     }
00207   }
00208 
00209   void VCard::setPhoto( const std::string& type, const std::string& binval )
00210   {
00211     if( !type.empty() && !binval.empty() )
00212     {
00213       m_photo.type = type;
00214       m_photo.binval = Base64::encode64( binval );
00215       m_PHOTO = true;
00216     }
00217   }
00218 
00219   void VCard::setLogo( const std::string& extval )
00220   {
00221     if( !extval.empty() )
00222     {
00223       m_logo.extval = extval;
00224       m_LOGO = true;
00225     }
00226   }
00227 
00228   void VCard::setLogo( const std::string& type, const std::string& binval )
00229   {
00230     if( !type.empty() && !binval.empty() )
00231     {
00232       m_logo.type = type;
00233       m_logo.binval = Base64::encode64( binval );
00234       m_LOGO = true;
00235     }
00236   }
00237 
00238   void VCard::addEmail( const std::string& userid, int type )
00239   {
00240     if( userid.empty() )
00241       return;
00242 
00243     Email item;
00244     item.userid = userid;
00245     item.internet = ( type & AddrTypeInet )?( true ):( false );
00246     item.x400 = ( type & AddrTypeX400 )?( true ):( false );
00247     item.work = ( type & AddrTypeWork )?( true ):( false );
00248     item.home = ( type & AddrTypeHome )?( true ):( false );
00249     item.pref = ( type & AddrTypePref )?( true ):( false );
00250 
00251     m_emailList.push_back( item );
00252   }
00253 
00254   void VCard::addAddress( const std::string& pobox, const std::string& extadd,
00255                           const std::string& street, const std::string& locality,
00256                           const std::string& region, const std::string& pcode,
00257                           const std::string& ctry, int type )
00258   {
00259     if( pobox.empty() && extadd.empty() && street.empty() &&
00260         locality.empty() && region.empty() && pcode.empty() && ctry.empty() )
00261       return;
00262 
00263     Address item;
00264     item.pobox = pobox;
00265     item.extadd = extadd;
00266     item.street = street;
00267     item.locality = locality;
00268     item.region = region;
00269     item.pcode = pcode;
00270     item.ctry = ctry;
00271     item.home = ( type & AddrTypeHome )?( true ):( false );
00272     item.work = ( type & AddrTypeWork )?( true ):( false );
00273     item.parcel = ( type & AddrTypeParcel )?( true ):( false );
00274     item.postal = ( type & AddrTypePostal )?( true ):( false );
00275     item.dom = ( type & AddrTypeDom )?( true ):( false );
00276     item.intl = ( !item.dom && type & AddrTypeDom )?( true ):( false );
00277     item.pref = ( type & AddrTypePref )?( true ):( false );
00278 
00279     m_addressList.push_back( item );
00280   }
00281 
00282   void VCard::addTelephone( const std::string& number, int type )
00283   {
00284     if( number.empty() )
00285       return;
00286 
00287     Telephone item;
00288     item.number = number;
00289     item.work = ( type & AddrTypeWork )?( true ):( false );
00290     item.home = ( type & AddrTypeHome )?( true ):( false );
00291     item.voice = ( type & AddrTypeVoice )?( true ):( false );
00292     item.fax = ( type & AddrTypeFax )?( true ):( false );
00293     item.pager = ( type & AddrTypePager )?( true ):( false );
00294     item.msg = ( type & AddrTypeMsg )?( true ):( false );
00295     item.cell = ( type & AddrTypeCell )?( true ):( false );
00296     item.video = ( type & AddrTypeVideo )?( true ):( false );
00297     item.bbs = ( type & AddrTypeBbs )?( true ):( false );
00298     item.modem = ( type & AddrTypeModem )?( true ):( false );
00299     item.isdn = ( type & AddrTypeIsdn )?( true ):( false );
00300     item.pcs = ( type & AddrTypePcs )?( true ):( false );
00301     item.pref = ( type & AddrTypePref )?( true ):( false );
00302 
00303     m_telephoneList.push_back( item );
00304   }
00305 
00306   void VCard::setGeo( const std::string& lat, const std::string& lon )
00307   {
00308     if( !lat.empty() && !lon.empty() )
00309     {
00310       m_geo.latitude = lat;
00311       m_geo.longitude = lon;
00312     }
00313   }
00314 
00315   void VCard::setOrganization( const std::string& orgname, const StringList& orgunits )
00316   {
00317     if( !orgname.empty() )
00318     {
00319       m_org.name = orgname;
00320       m_org.units = orgunits;
00321     }
00322   }
00323 
00324   Tag* VCard::tag() const
00325   {
00326     Tag *v = new Tag( "vCard" );
00327     v->addAttribute( "xmlns", XMLNS_VCARD_TEMP );
00328     v->addAttribute( "version", "3.0" );
00329 
00330     insertField( v, "FN", m_formattedname );
00331     insertField( v, "NICKNAME", m_nickname );
00332     insertField( v, "URL", m_url );
00333     insertField( v, "BDAY", m_bday );
00334     insertField( v, "JABBERID", m_jabberid );
00335     insertField( v, "TITLE", m_title );
00336     insertField( v, "ROLE", m_role );
00337     insertField( v, "NOTE", m_note );
00338     insertField( v, "DESC", m_desc );
00339     insertField( v, "MAILER", m_mailer );
00340     insertField( v, "TZ", m_tz );
00341     insertField( v, "REV", m_rev );
00342     insertField( v, "SORT_STRING", m_sortstring );
00343     insertField( v, "UID", m_uid );
00344 
00345     if( m_N )
00346     {
00347       Tag *n = new Tag( v, "N" );
00348       insertField( n, "FAMILY", m_name.family );
00349       insertField( n, "GIVEN", m_name.given );
00350       insertField( n, "MIDDLE", m_name.middle );
00351       insertField( n, "PREFIX", m_name.prefix );
00352       insertField( n, "SUFFIX", m_name.suffix );
00353     }
00354 
00355     if( m_PHOTO )
00356     {
00357       Tag *p = new Tag( v, "PHOTO" );
00358       if( !m_photo.extval.empty() )
00359       {
00360         new Tag( p, "EXTVAL", m_photo.extval );
00361       }
00362       else if( !m_photo.type.empty() && !m_photo.binval.empty() )
00363       {
00364         new Tag( p, "TYPE", m_photo.type );
00365         new Tag( p, "BINVAL", m_photo.binval );
00366       }
00367     }
00368 
00369     if( m_LOGO )
00370     {
00371       Tag *l = new Tag( v, "LOGO" );
00372       if( !m_logo.extval.empty() )
00373       {
00374         new Tag( l, "EXTVAL", m_logo.extval );
00375       }
00376       else if( !m_logo.type.empty() && !m_logo.binval.empty() )
00377       {
00378         new Tag( l, "TYPE", m_logo.type );
00379         new Tag( l, "BINVAL", m_logo.binval );
00380       }
00381     }
00382 
00383     EmailList::const_iterator ite = m_emailList.begin();
00384     for( ; ite != m_emailList.end(); ++ite )
00385     {
00386       Tag *e = new Tag( v, "EMAIL" );
00387       insertField( e, "INTERNET", (*ite).internet );
00388       insertField( e, "WORK", (*ite).work );
00389       insertField( e, "HOME", (*ite).home );
00390       insertField( e, "X400", (*ite).x400 );
00391       insertField( e, "PREF", (*ite).pref );
00392       insertField( e, "USERID", (*ite).userid );
00393     }
00394 
00395     AddressList::const_iterator ita = m_addressList.begin();
00396     for( ; ita != m_addressList.end(); ++ita )
00397     {
00398       Tag *a = new Tag( v, "ADR" );
00399       insertField( a, "POSTAL", (*ita).postal );
00400       insertField( a, "PARCEL", (*ita).parcel );
00401       insertField( a, "HOME", (*ita).home );
00402       insertField( a, "WORK", (*ita).work );
00403       insertField( a, "PREF", (*ita).pref );
00404       insertField( a, "DOM", (*ita).dom );
00405       if( !(*ita).dom )
00406         insertField( a, "INTL", (*ita).intl );
00407 
00408       insertField( a, "POBOX", (*ita).pobox );
00409       insertField( a, "EXTADD", (*ita).extadd );
00410       insertField( a, "STREET", (*ita).street );
00411       insertField( a, "LOCALITY", (*ita).locality );
00412       insertField( a, "REGION", (*ita).region );
00413       insertField( a, "PCODE", (*ita).pcode );
00414       insertField( a, "CTRY", (*ita).ctry );
00415     }
00416 
00417     TelephoneList::const_iterator itt = m_telephoneList.begin();
00418     for( ; itt != m_telephoneList.end(); ++itt )
00419     {
00420       Tag *t = new Tag( v, "TEL" );
00421       insertField( t, "NUMBER", (*itt).number );
00422       insertField( t, "HOME", (*itt).home );
00423       insertField( t, "WORK", (*itt).work );
00424       insertField( t, "VOICE", (*itt).voice );
00425       insertField( t, "FAX", (*itt).fax );
00426       insertField( t, "PAGER", (*itt).pager );
00427       insertField( t, "MSG", (*itt).msg );
00428       insertField( t, "CELL", (*itt).cell );
00429       insertField( t, "VIDEO", (*itt).video );
00430       insertField( t, "BBS", (*itt).bbs );
00431       insertField( t, "MODEM", (*itt).modem );
00432       insertField( t, "ISDN", (*itt).isdn );
00433       insertField( t, "PCS", (*itt).pcs );
00434       insertField( t, "PREF", (*itt).pref );
00435     }
00436 
00437     if( !m_geo.latitude.empty() && !m_geo.longitude.empty() )
00438     {
00439       Tag *g = new Tag( v, "GEO" );
00440       new Tag( g, "LAT", m_geo.latitude );
00441       new Tag( g, "LON", m_geo.longitude );
00442     }
00443 
00444     if( !m_org.name.empty() )
00445     {
00446       Tag *o = new Tag( v, "ORG" );
00447       new Tag( o, "ORGNAME", m_org.name );
00448       StringList::const_iterator ito = m_org.units.begin();
00449       for( ; ito != m_org.units.end(); ++ito )
00450         new Tag( o, "ORGUNITS", (*ito) );
00451     }
00452 
00453     if( m_class != ClassNone )
00454     {
00455       Tag *c = new Tag( v, "CLASS" );
00456       switch( m_class )
00457       {
00458         case ClassPublic:
00459           new Tag( c, "PUBLIC" );
00460           break;
00461         case ClassPrivate:
00462           new Tag( c, "PRIVATE" );
00463           break;
00464         case ClassConfidential:
00465           new Tag( c, "CONFIDENTIAL" );
00466           break;
00467         default:
00468           break;
00469       }
00470     }
00471 
00472     return v;
00473   }
00474 
00475   void VCard::insertField( Tag *vcard, const std::string& field, const std::string& var ) const
00476   {
00477     if( !var.empty() )
00478       new Tag( vcard, field, var );
00479   }
00480 
00481   void VCard::insertField( Tag *vcard, const std::string& field, bool var ) const
00482   {
00483     if( var )
00484       new Tag( vcard, field );
00485   }
00486 
00487 }

Generated on Tue May 1 14:20:20 2007 for gloox by  doxygen 1.5.1