summaryrefslogtreecommitdiffstats
blob: 36401a6c2eda920d7ddc1f90dcc4d5fa36e72a8f (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
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

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

#include <memory>

#include <Swiften/Serializer/PayloadSerializers/SecurityLabelSerializer.h>
#include <Swiften/Serializer/XML/XMLElement.h>
#include <Swiften/Serializer/XML/XMLRawTextNode.h>

namespace Swift {

SecurityLabelsCatalogSerializer::SecurityLabelsCatalogSerializer() : GenericPayloadSerializer<SecurityLabelsCatalog>() {
}

std::string SecurityLabelsCatalogSerializer::serializePayload(std::shared_ptr<SecurityLabelsCatalog> catalog)  const {
    XMLElement element("catalog", "urn:xmpp:sec-label:catalog:2");
    if (!catalog->getName().empty()) {
        element.setAttribute("name", catalog->getName());
    }
    if (catalog->getTo().isValid()) {
        element.setAttribute("to", catalog->getTo());
    }
    if (!catalog->getDescription().empty()) {
        element.setAttribute("desc", catalog->getDescription());
    }
    for (const auto& item : catalog->getItems()) {
        std::shared_ptr<XMLElement> itemElement(new XMLElement("item"));
        itemElement->setAttribute("selector", item.getSelector());
        if (item.getIsDefault()) {
            itemElement->setAttribute("default", "true");
        }
        if (item.getLabel()) {
            std::string serializedLabel = SecurityLabelSerializer().serialize(item.getLabel());
            itemElement->addNode(std::make_shared<XMLRawTextNode>(serializedLabel));
        }
        element.addNode(itemElement);
    }
    return element.serialize();
}

}