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) 2013 Remko Tronçon
* Licensed under the GNU General Public License.
* See the COPYING file for more information.
*/
#include <Sluift/ElementConvertors/DiscoItemsConvertor.h>
#include <lua.hpp>
#include <boost/smart_ptr/make_shared.hpp>
#include <boost/numeric/conversion/cast.hpp>
#include <Sluift/Lua/LuaUtils.h>
using namespace Swift;
DiscoItemsConvertor::DiscoItemsConvertor() : GenericLuaElementConvertor<DiscoItems>("disco_items") {
}
DiscoItemsConvertor::~DiscoItemsConvertor() {
}
boost::shared_ptr<DiscoItems> DiscoItemsConvertor::doConvertFromLua(lua_State* L) {
boost::shared_ptr<DiscoItems> result = boost::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, boost::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);
lua_pushstring(L, items[i].getName().c_str());
lua_setfield(L, -2, "name");
lua_pushstring(L, items[i].getNode().c_str());
lua_setfield(L, -2, "node");
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");
}
}
|