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

#include <Sluift/ElementConvertors/DiscoItemsConvertor.h>

#include <memory>

#include <boost/numeric/conversion/cast.hpp>

#include <lua.hpp>

#include <Sluift/Lua/LuaUtils.h>

using namespace Swift;

DiscoItemsConvertor::DiscoItemsConvertor() : GenericLuaElementConvertor<DiscoItems>("disco_items") {
}

DiscoItemsConvertor::~DiscoItemsConvertor() {
}

std::shared_ptr<DiscoItems> DiscoItemsConvertor::doConvertFromLua(lua_State* L) {
    std::shared_ptr<DiscoItems> result = std::make_shared<DiscoItems>();
    if (boost::optional<std::string> value = Lua::getStringField(L, -1, "node")) {
        result->setNode(*value);
    }
    lua_getfield(L, -1, "items");
    if (lua_istable(L, -1)) {
        for (lua_pushnil(L); lua_next(L, -2); ) {
            result->addItem(DiscoItems::Item(
                    Lua::getStringField(L, -1, "name").get_value_or(""),
                    JID(Lua::getStringField(L, -1, "jid").get_value_or("")),
                    Lua::getStringField(L, -1, "node").get_value_or("")));
            lua_pop(L, 1);
        }
    }
    lua_pop(L, 1);
    return result;
}

void DiscoItemsConvertor::doConvertToLua(lua_State* L, std::shared_ptr<DiscoItems> payload) {
    lua_newtable(L);
    if (!payload->getNode().empty()) {
        lua_pushstring(L, payload->getNode().c_str());
        lua_setfield(L, -2, "node");
    }
    const std::vector<DiscoItems::Item>& items = payload->getItems();
    if (!items.empty()) {
        lua_createtable(L, boost::numeric_cast<int>(items.size()), 0);
        for (size_t i = 0; i < items.size(); ++i) {
            lua_createtable(L, 0, 0);
            if (!items[i].getName().empty()) {
                lua_pushstring(L, items[i].getName().c_str());
                lua_setfield(L, -2, "name");
            }
            if (!items[i].getNode().empty()) {
                lua_pushstring(L, items[i].getNode().c_str());
                lua_setfield(L, -2, "node");
            }
            if (items[i].getJID().isValid()) {
                lua_pushstring(L, items[i].getJID().toString().c_str());
                lua_setfield(L, -2, "jid");
            }
            lua_rawseti(L, -2, boost::numeric_cast<int>(i+1));
        }
        lua_setfield(L, -2, "items");
    }
}