summaryrefslogtreecommitdiffstats
path: root/src/modules/UserListManager/Util.cpp
blob: f9b6a5591922d59340d1bd49d78a3f062491b008 (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
/*
 * Util.cpp
 *
 * Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "Util.h"
#include "UserList.h"

#include <Common/XmlData.h>

namespace Mad {
namespace Modules {
namespace UserListManager {

void Util::serializeUserList(const UserList *list, Common::XmlData *data) {
  Common::XmlData::List *userList = data->createList("users");

  for(UserList::const_iterator user = list->begin(); user != list->end(); ++user) {
    Common::XmlData::List::iterator entry = userList->addEntry();

    std::set<std::string> details = user->getDetailList();
    for(std::set<std::string>::iterator detail = details.begin(); detail != details.end(); ++detail)
      entry->set(*detail, user->getDetail(*detail));

    entry->set("name", user->getName());
    entry->set("group", user->getGroup());
  }
}

boost::shared_ptr<UserList> Util::deserializeUserList(const Common::XmlData *data) {
  boost::shared_ptr<UserList> users(new UserList);

  const Common::XmlData::List *userList = data->getList("users");

  if(userList) {
    for(Common::XmlData::List::const_iterator user = userList->begin(); user != userList->end(); ++user) {
      UserListEntry entry(user->get<const std::string&>("name"), user->get<const std::string&>("group"));

      std::set<std::string> details = user->getChildren();
      for(std::set<std::string>::iterator detail = details.begin(); detail != details.end(); ++detail) {
        if(*detail == "user" || *detail == "group")
          continue;

        entry.setDetail(*detail, user->get<const std::string&>(*detail));
      }

      users->push_back(entry);
    }
  }

  return users;
}

}
}
}