/* * Copyright (c) 2010 Remko Tronçon * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. */ #include #include #include #include #include #include #include #include #include namespace Swift { VCardSerializer::VCardSerializer() : GenericPayloadSerializer() { } std::string VCardSerializer::serializePayload(boost::shared_ptr vcard) const { XMLElement queryElement("vCard", "vcard-temp"); if (!vcard->getVersion().empty()) { queryElement.addNode(boost::make_shared("VERSION", "", vcard->getVersion())); } if (!vcard->getFullName().empty()) { queryElement.addNode(boost::make_shared("FN", "", vcard->getFullName())); } if (!vcard->getGivenName().empty() || !vcard->getFamilyName().empty() || !vcard->getMiddleName().empty() || !vcard->getPrefix().empty() || !vcard->getSuffix().empty()) { boost::shared_ptr nameElement(new XMLElement("N")); if (!vcard->getFamilyName().empty()) { nameElement->addNode(boost::make_shared("FAMILY", "", vcard->getFamilyName())); } if (!vcard->getGivenName().empty()) { nameElement->addNode(boost::make_shared("GIVEN", "", vcard->getGivenName())); } if (!vcard->getMiddleName().empty()) { nameElement->addNode(boost::make_shared("MIDDLE", "", vcard->getMiddleName())); } if (!vcard->getPrefix().empty()) { nameElement->addNode(boost::make_shared("PREFIX", "", vcard->getPrefix())); } if (!vcard->getSuffix().empty()) { nameElement->addNode(boost::make_shared("SUFFIX", "", vcard->getSuffix())); } queryElement.addNode(nameElement); } foreach(const VCard::EMailAddress& emailAddress, vcard->getEMailAddresses()) { boost::shared_ptr emailElement(new XMLElement("EMAIL")); emailElement->addNode(boost::make_shared("USERID", "", emailAddress.address)); if (emailAddress.isHome) { emailElement->addNode(boost::make_shared("HOME")); } if (emailAddress.isWork) { emailElement->addNode(boost::make_shared("WORK")); } if (emailAddress.isInternet) { emailElement->addNode(boost::make_shared("INTERNET")); } if (emailAddress.isPreferred) { emailElement->addNode(boost::make_shared("PREF")); } if (emailAddress.isX400) { emailElement->addNode(boost::make_shared("X400")); } queryElement.addNode(emailElement); } if (!vcard->getNickname().empty()) { queryElement.addNode(boost::make_shared("NICKNAME", "", vcard->getNickname())); } if (!vcard->getPhoto().empty() || !vcard->getPhotoType().empty()) { XMLElement::ref photoElement(new XMLElement("PHOTO")); if (!vcard->getPhotoType().empty()) { photoElement->addNode(boost::make_shared("TYPE", "", vcard->getPhotoType())); } if (!vcard->getPhoto().empty()) { photoElement->addNode(boost::make_shared("BINVAL", "", Base64::encode(vcard->getPhoto()))); } queryElement.addNode(photoElement); } if (!vcard->getBirthday().is_not_a_date_time()) { queryElement.addNode(boost::make_shared("BDAY", "", dateTimeToString(vcard->getBirthday()))); } foreach(const VCard::Telephone& telephone, vcard->getTelephones()) { boost::shared_ptr telElement(new XMLElement("TEL")); telElement->addNode(boost::make_shared("NUMBER", "", telephone.number)); if (telephone.isHome) { telElement->addNode(boost::make_shared("HOME")); } if (telephone.isWork) { telElement->addNode(boost::make_shared("WORK")); } if (telephone.isVoice) { telElement->addNode(boost::make_shared("VOICE")); } if (telephone.isFax) { telElement->addNode(boost::make_shared("FAX")); } if (telephone.isPager) { telElement->addNode(boost::make_shared("PAGER")); } if (telephone.isMSG) { telElement->addNode(boost::make_shared("MSG")); } if (telephone.isCell) { telElement->addNode(boost::make_shared("CELL")); } if (telephone.isVideo) { telElement->addNode(boost::make_shared("VIDEO")); } if (telephone.isBBS) { telElement->addNode(boost::make_shared("BBS")); } if (telephone.isModem) { telElement->addNode(boost::make_shared("MODEM")); } if (telephone.isISDN) { telElement->addNode(boost::make_shared("ISDN")); } if (telephone.isPCS) { telElement->addNode(boost::make_shared("PCS")); } if (telephone.isPreferred) { telElement->addNode(boost::make_shared("PREF")); } queryElement.addNode(telElement); } foreach(const VCard::Address& address, vcard->getAddresses()) { boost::shared_ptr adrElement = boost::make_shared("ADR"); if (!address.poBox.empty()) { adrElement->addNode(boost::make_shared("POBOX", "", address.poBox)); } if (!address.addressExtension.empty()) { adrElement->addNode(boost::make_shared("EXTADD", "", address.addressExtension)); } if (!address.street.empty()) { adrElement->addNode(boost::make_shared("STREET", "", address.street)); } if (!address.locality.empty()) { adrElement->addNode(boost::make_shared("LOCALITY", "", address.locality)); } if (!address.region.empty()) { adrElement->addNode(boost::make_shared("REGION", "", address.region)); } if (!address.postalCode.empty()) { adrElement->addNode(boost::make_shared("PCODE", "", address.postalCode)); } if (!address.country.empty()) { adrElement->addNode(boost::make_shared("CTRY", "", address.country)); } if (address.isHome) { adrElement->addNode(boost::make_shared("HOME")); } if (address.isWork) { adrElement->addNode(boost::make_shared("WORK")); } if (address.isPostal) { adrElement->addNode(boost::make_shared("POSTAL")); } if (address.isParcel) { adrElement->addNode(boost::make_shared("PARCEL")); } if (address.deliveryType == VCard::DomesticDelivery) { adrElement->addNode(boost::make_shared("DOM")); } if (address.deliveryType == VCard::InternationalDelivery) { adrElement->addNode(boost::make_shared("INTL")); } if (address.isPreferred) { adrElement->addNode(boost::make_shared("PREF")); } queryElement.addNode(adrElement); } foreach(const VCard::AddressLabel& addressLabel, vcard->getAddressLabels()) { boost::shared_ptr labelElement = boost::make_shared("LABEL"); foreach(const std::string& line, addressLabel.lines) { labelElement->addNode(boost::make_shared("LINE", "", line)); } if (addressLabel.isHome) { labelElement->addNode(boost::make_shared("HOME")); } if (addressLabel.isWork) { labelElement->addNode(boost::make_shared("WORK")); } if (addressLabel.isPostal) { labelElement->addNode(boost::make_shared("POSTAL")); } if (addressLabel.isParcel) { labelElement->addNode(boost::make_shared("PARCEL")); } if (addressLabel.deliveryType == VCard::DomesticDelivery) { labelElement->addNode(boost::make_shared("DOM")); } if (addressLabel.deliveryType == VCard::InternationalDelivery) { labelElement->addNode(boost::make_shared("INTL")); } if (addressLabel.isPreferred) { labelElement->addNode(boost::make_shared("PREF")); } queryElement.addNode(labelElement); } foreach(const JID& jid, vcard->getJIDs()) { queryElement.addNode(boost::make_shared("JID", "", jid.toString())); } if (!vcard->getDescription().empty()) { queryElement.addNode(boost::make_shared("DESC", "", vcard->getDescription())); } foreach(const VCard::Organization& org, vcard->getOrganizations()) { boost::shared_ptr orgElement = boost::make_shared("ORG"); if (!org.name.empty()) { orgElement->addNode(boost::make_shared("ORGNAME", "", org.name)); } if (!org.units.empty()) { foreach(const std::string& unit, org.units) { orgElement->addNode(boost::make_shared("ORGUNIT", "", unit)); } } queryElement.addNode(orgElement); } foreach(const std::string& title, vcard->getTitles()) { queryElement.addNode(boost::make_shared("TITLE", "", title)); } foreach(const std::string& role, vcard->getRoles()) { queryElement.addNode(boost::make_shared("ROLE", "", role)); } foreach(const std::string& url, vcard->getURLs()) { queryElement.addNode(boost::make_shared("URL", "", url)); } if (!vcard->getUnknownContent().empty()) { queryElement.addNode(boost::make_shared(vcard->getUnknownContent())); } return queryElement.serialize(); } }