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

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

#include <memory>

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

namespace Swift {

StorageSerializer::StorageSerializer() : GenericPayloadSerializer<Storage>() {
}

std::string StorageSerializer::serializePayload(std::shared_ptr<Storage> storage)    const {
    XMLElement storageElement("storage", "storage:bookmarks");

    foreach(const Storage::Room& room, storage->getRooms()) {
        std::shared_ptr<XMLElement> conferenceElement(new XMLElement("conference"));
        conferenceElement->setAttribute("name", room.name);
        conferenceElement->setAttribute("jid", room.jid);
        conferenceElement->setAttribute("autojoin", room.autoJoin ? "1" : "0");
        if (!room.nick.empty()) {
            std::shared_ptr<XMLElement> nickElement(new XMLElement("nick"));
            nickElement->addNode(std::make_shared<XMLTextNode>(room.nick));
            conferenceElement->addNode(nickElement);
        }
        if (room.password) {
            std::shared_ptr<XMLElement> passwordElement(new XMLElement("password"));
            passwordElement->addNode(std::make_shared<XMLTextNode>(*room.password));
            conferenceElement->addNode(passwordElement);
        }
        storageElement.addNode(conferenceElement);
    }

    foreach(const Storage::URL& url, storage->getURLs()) {
        std::shared_ptr<XMLElement> urlElement(new XMLElement("url"));
        urlElement->setAttribute("name", url.name);
        urlElement->setAttribute("url", url.url);
        storageElement.addNode(urlElement);
    }

    return storageElement.serialize();
}

}