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

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

#include <memory>

#include <Swiften/Base/foreach.h>
#include <Swiften/Serializer/PayloadSerializers/FormSerializer.h>
#include <Swiften/Serializer/XML/XMLElement.h>
#include <Swiften/Serializer/XML/XMLRawTextNode.h>

namespace Swift {

SearchPayloadSerializer::SearchPayloadSerializer() {
}

std::string SearchPayloadSerializer::serializePayload(std::shared_ptr<SearchPayload> searchPayload)    const {
    XMLElement searchElement("query", "jabber:iq:search");

    if (searchPayload->getInstructions()) {
        searchElement.addNode(XMLElement::ref(new XMLElement("instructions", "", *searchPayload->getInstructions())));
    }

    if (searchPayload->getNick()) {
        searchElement.addNode(XMLElement::ref(new XMLElement("nick", "", *searchPayload->getNick())));
    }

    if (searchPayload->getFirst()) {
        searchElement.addNode(XMLElement::ref(new XMLElement("first", "", *searchPayload->getFirst())));
    }

    if (searchPayload->getLast()) {
        searchElement.addNode(XMLElement::ref(new XMLElement("last", "", *searchPayload->getLast())));
    }

    if (searchPayload->getEMail()) {
        searchElement.addNode(XMLElement::ref(new XMLElement("email", "", *searchPayload->getEMail())));
    }

    foreach(const SearchPayload::Item& item, searchPayload->getItems()) {
        XMLElement::ref itemElement(new XMLElement("item"));
        itemElement->setAttribute("jid", item.jid);
        itemElement->addNode(XMLElement::ref(new XMLElement("first", "", item.first)));
        itemElement->addNode(XMLElement::ref(new XMLElement("last", "", item.last)));
        itemElement->addNode(XMLElement::ref(new XMLElement("nick", "", item.nick)));
        itemElement->addNode(XMLElement::ref(new XMLElement("email", "", item.email)));

        searchElement.addNode(itemElement);
    }

    if (Form::ref form = searchPayload->getForm()) {
        searchElement.addNode(std::make_shared<XMLRawTextNode>(FormSerializer().serialize(form)));
    }

    return searchElement.serialize();
}

}