summaryrefslogtreecommitdiffstats
blob: c7fdc5b7f223a83d7fad2660176b3422367db644 (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
/*
 * Copyright (c) 2011 Jan Kaluza
 * Licensed under the Simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

/*
 * Copyright (c) 2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

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

#include <memory>

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

namespace Swift {

RosterItemExchangeSerializer::RosterItemExchangeSerializer() : GenericPayloadSerializer<RosterItemExchangePayload>() {
}

std::string RosterItemExchangeSerializer::serializePayload(std::shared_ptr<RosterItemExchangePayload> roster)  const {
    XMLElement queryElement("x", "http://jabber.org/protocol/rosterx");
    for (const auto& item : roster->getItems()) {
        std::shared_ptr<XMLElement> itemElement(new XMLElement("item"));
        itemElement->setAttribute("jid", item.getJID());
        itemElement->setAttribute("name", item.getName());

        switch (item.getAction()) {
            case RosterItemExchangePayload::Item::Add: itemElement->setAttribute("action", "add"); break;
            case RosterItemExchangePayload::Item::Modify: itemElement->setAttribute("action", "modify"); break;
            case RosterItemExchangePayload::Item::Delete: itemElement->setAttribute("action", "delete"); break;
        }

        for (const auto& group : item.getGroups()) {
            std::shared_ptr<XMLElement> groupElement(new XMLElement("group"));
            groupElement->addNode(std::make_shared<XMLTextNode>(group));
            itemElement->addNode(groupElement);
        }

        queryElement.addNode(itemElement);
    }

    return queryElement.serialize();
}

}