summaryrefslogtreecommitdiffstats
blob: a1393cb2fe8627c610f67b4db517e74cbeb45771 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swiften/Serializer/PayloadSerializers/VCardSerializer.h>

#include <memory>

#include <Swiften/Base/DateTime.h>
#include <Swiften/Base/foreach.h>
#include <Swiften/Serializer/XML/XMLElement.h>
#include <Swiften/Serializer/XML/XMLRawTextNode.h>
#include <Swiften/Serializer/XML/XMLTextNode.h>
#include <Swiften/StringCodecs/Base64.h>

namespace Swift {

VCardSerializer::VCardSerializer() : GenericPayloadSerializer<VCard>() {
}

std::string VCardSerializer::serializePayload(std::shared_ptr<VCard> vcard)  const {
    XMLElement queryElement("vCard", "vcard-temp");
    if (!vcard->getVersion().empty()) {
        queryElement.addNode(std::make_shared<XMLElement>("VERSION", "", vcard->getVersion()));
    }
    if (!vcard->getFullName().empty()) {
        queryElement.addNode(std::make_shared<XMLElement>("FN", "", vcard->getFullName()));
    }
    if (!vcard->getGivenName().empty() || !vcard->getFamilyName().empty() || !vcard->getMiddleName().empty() || !vcard->getPrefix().empty() || !vcard->getSuffix().empty()) {
        std::shared_ptr<XMLElement> nameElement(new XMLElement("N"));
        if (!vcard->getFamilyName().empty()) {
            nameElement->addNode(std::make_shared<XMLElement>("FAMILY", "", vcard->getFamilyName()));
        }
        if (!vcard->getGivenName().empty()) {
            nameElement->addNode(std::make_shared<XMLElement>("GIVEN", "", vcard->getGivenName()));
        }
        if (!vcard->getMiddleName().empty()) {
            nameElement->addNode(std::make_shared<XMLElement>("MIDDLE", "", vcard->getMiddleName()));
        }
        if (!vcard->getPrefix().empty()) {
            nameElement->addNode(std::make_shared<XMLElement>("PREFIX", "", vcard->getPrefix()));
        }
        if (!vcard->getSuffix().empty()) {
            nameElement->addNode(std::make_shared<XMLElement>("SUFFIX", "", vcard->getSuffix()));
        }
        queryElement.addNode(nameElement);
    }
    foreach(const VCard::EMailAddress& emailAddress, vcard->getEMailAddresses()) {
        std::shared_ptr<XMLElement> emailElement(new XMLElement("EMAIL"));
        emailElement->addNode(std::make_shared<XMLElement>("USERID", "", emailAddress.address));
        if (emailAddress.isHome) {
            emailElement->addNode(std::make_shared<XMLElement>("HOME"));
        }
        if (emailAddress.isWork) {
            emailElement->addNode(std::make_shared<XMLElement>("WORK"));
        }
        if (emailAddress.isInternet) {
            emailElement->addNode(std::make_shared<XMLElement>("INTERNET"));
        }
        if (emailAddress.isPreferred) {
            emailElement->addNode(std::make_shared<XMLElement>("PREF"));
        }
        if (emailAddress.isX400) {
            emailElement->addNode(std::make_shared<XMLElement>("X400"));
        }
        queryElement.addNode(emailElement);
    }
    if (!vcard->getNickname().empty()) {
        queryElement.addNode(std::make_shared<XMLElement>("NICKNAME", "", vcard->getNickname()));
    }
    if (!vcard->getPhoto().empty() || !vcard->getPhotoType().empty()) {
        XMLElement::ref photoElement(new XMLElement("PHOTO"));
        if (!vcard->getPhotoType().empty()) {
            photoElement->addNode(std::make_shared<XMLElement>("TYPE", "", vcard->getPhotoType()));
        }
        if (!vcard->getPhoto().empty()) {
            photoElement->addNode(std::make_shared<XMLElement>("BINVAL", "", Base64::encode(vcard->getPhoto())));
        }
        queryElement.addNode(photoElement);
    }
    if (!vcard->getBirthday().is_not_a_date_time()) {
        queryElement.addNode(std::make_shared<XMLElement>("BDAY", "", dateTimeToString(vcard->getBirthday())));
    }

    foreach(const VCard::Telephone& telephone, vcard->getTelephones()) {
        std::shared_ptr<XMLElement> telElement(new XMLElement("TEL"));
        telElement->addNode(std::make_shared<XMLElement>("NUMBER", "", telephone.number));
        if (telephone.isHome) {
            telElement->addNode(std::make_shared<XMLElement>("HOME"));
        }
        if (telephone.isWork) {
            telElement->addNode(std::make_shared<XMLElement>("WORK"));
        }
        if (telephone.isVoice) {
            telElement->addNode(std::make_shared<XMLElement>("VOICE"));
        }
        if (telephone.isFax) {
            telElement->addNode(std::make_shared<XMLElement>("FAX"));
        }
        if (telephone.isPager) {
            telElement->addNode(std::make_shared<XMLElement>("PAGER"));
        }
        if (telephone.isMSG) {
            telElement->addNode(std::make_shared<XMLElement>("MSG"));
        }
        if (telephone.isCell) {
            telElement->addNode(std::make_shared<XMLElement>("CELL"));
        }
        if (telephone.isVideo) {
            telElement->addNode(std::make_shared<XMLElement>("VIDEO"));
        }
        if (telephone.isBBS) {
            telElement->addNode(std::make_shared<XMLElement>("BBS"));
        }
        if (telephone.isModem) {
            telElement->addNode(std::make_shared<XMLElement>("MODEM"));
        }
        if (telephone.isISDN) {
            telElement->addNode(std::make_shared<XMLElement>("ISDN"));
        }
        if (telephone.isPCS) {
            telElement->addNode(std::make_shared<XMLElement>("PCS"));
        }
        if (telephone.isPreferred) {
            telElement->addNode(std::make_shared<XMLElement>("PREF"));
        }
        queryElement.addNode(telElement);
    }

    foreach(const VCard::Address& address, vcard->getAddresses()) {
        std::shared_ptr<XMLElement> adrElement = std::make_shared<XMLElement>("ADR");
        if (!address.poBox.empty()) {
            adrElement->addNode(std::make_shared<XMLElement>("POBOX", "", address.poBox));
        }
        if (!address.addressExtension.empty()) {
            adrElement->addNode(std::make_shared<XMLElement>("EXTADD", "", address.addressExtension));
        }
        if (!address.street.empty()) {
            adrElement->addNode(std::make_shared<XMLElement>("STREET", "", address.street));
        }
        if (!address.locality.empty()) {
            adrElement->addNode(std::make_shared<XMLElement>("LOCALITY", "", address.locality));
        }
        if (!address.region.empty()) {
            adrElement->addNode(std::make_shared<XMLElement>("REGION", "", address.region));
        }
        if (!address.postalCode.empty()) {
            adrElement->addNode(std::make_shared<XMLElement>("PCODE", "", address.postalCode));
        }
        if (!address.country.empty()) {
            adrElement->addNode(std::make_shared<XMLElement>("CTRY", "", address.country));
        }

        if (address.isHome) {
            adrElement->addNode(std::make_shared<XMLElement>("HOME"));
        }
        if (address.isWork) {
            adrElement->addNode(std::make_shared<XMLElement>("WORK"));
        }
        if (address.isPostal) {
            adrElement->addNode(std::make_shared<XMLElement>("POSTAL"));
        }
        if (address.isParcel) {
            adrElement->addNode(std::make_shared<XMLElement>("PARCEL"));
        }
        if (address.deliveryType == VCard::DomesticDelivery) {
            adrElement->addNode(std::make_shared<XMLElement>("DOM"));
        }
        if (address.deliveryType == VCard::InternationalDelivery) {
            adrElement->addNode(std::make_shared<XMLElement>("INTL"));
        }
        if (address.isPreferred) {
            adrElement->addNode(std::make_shared<XMLElement>("PREF"));
        }
        queryElement.addNode(adrElement);
    }

    foreach(const VCard::AddressLabel& addressLabel, vcard->getAddressLabels()) {
        std::shared_ptr<XMLElement> labelElement = std::make_shared<XMLElement>("LABEL");

        foreach(const std::string& line, addressLabel.lines) {
            labelElement->addNode(std::make_shared<XMLElement>("LINE", "", line));
        }

        if (addressLabel.isHome) {
            labelElement->addNode(std::make_shared<XMLElement>("HOME"));
        }
        if (addressLabel.isWork) {
            labelElement->addNode(std::make_shared<XMLElement>("WORK"));
        }
        if (addressLabel.isPostal) {
            labelElement->addNode(std::make_shared<XMLElement>("POSTAL"));
        }
        if (addressLabel.isParcel) {
            labelElement->addNode(std::make_shared<XMLElement>("PARCEL"));
        }
        if (addressLabel.deliveryType == VCard::DomesticDelivery) {
            labelElement->addNode(std::make_shared<XMLElement>("DOM"));
        }
        if (addressLabel.deliveryType == VCard::InternationalDelivery) {
            labelElement->addNode(std::make_shared<XMLElement>("INTL"));
        }
        if (addressLabel.isPreferred) {
            labelElement->addNode(std::make_shared<XMLElement>("PREF"));
        }
        queryElement.addNode(labelElement);
    }

    foreach(const JID& jid, vcard->getJIDs()) {
        queryElement.addNode(std::make_shared<XMLElement>("JID", "", jid.toString()));
    }

    if (!vcard->getDescription().empty()) {
        queryElement.addNode(std::make_shared<XMLElement>("DESC", "", vcard->getDescription()));
    }

    foreach(const VCard::Organization& org, vcard->getOrganizations()) {
        std::shared_ptr<XMLElement> orgElement = std::make_shared<XMLElement>("ORG");
        if (!org.name.empty()) {
            orgElement->addNode(std::make_shared<XMLElement>("ORGNAME", "", org.name));
        }
        if (!org.units.empty()) {
            foreach(const std::string& unit, org.units) {
                orgElement->addNode(std::make_shared<XMLElement>("ORGUNIT", "", unit));
            }
        }
        queryElement.addNode(orgElement);
    }

    foreach(const std::string& title, vcard->getTitles()) {
        queryElement.addNode(std::make_shared<XMLElement>("TITLE", "", title));
    }

    foreach(const std::string& role, vcard->getRoles()) {
        queryElement.addNode(std::make_shared<XMLElement>("ROLE", "", role));
    }

    foreach(const std::string& url, vcard->getURLs()) {
        queryElement.addNode(std::make_shared<XMLElement>("URL", "", url));
    }

    if (!vcard->getUnknownContent().empty()) {
        queryElement.addNode(std::make_shared<XMLRawTextNode>(vcard->getUnknownContent()));
    }
    return queryElement.serialize();
}

}