vcard.cpp

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

Generated on Sat Nov 10 08:50:27 2007 for gloox by  doxygen 1.5.3-20071008