summaryrefslogtreecommitdiffstats
path: root/src/modules/UserListManager/Util.cpp
blob: 9b986d81bc875baf9540052f37702ce852ef554f (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * 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 "UserListDiff.h"

#include <Common/XmlData.h>

namespace Mad {
namespace Modules {
namespace UserListManager {

void Util::serializeUserListEntry(const UserListEntry *user, Common::XmlData::List::iterator entry) {
  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());
}

UserListEntry Util::deserializeUserListEntry(Common::XmlData::List::const_iterator entry) {
  UserListEntry user(entry->get<const std::string&>("name"), entry->get<const std::string&>("group"));

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

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

  return user;
}

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)
    serializeUserListEntry(&*user, userList->addEntry());
}

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)
      users->push_back(deserializeUserListEntry(user));
  }

  return users;
}

void Util::serializeUserListDiff(const UserListDiff *diff, Common::XmlData *data) {
  Common::XmlData::List *userList = data->createList("addedUsers");
  for(std::set<UserListEntry>::const_iterator user = diff->getAddedUsers().begin(); user != diff->getAddedUsers().end(); ++user)
      serializeUserListEntry(&*user, userList->addEntry());

  userList = data->createList("deletedUsers");
  for(std::set<UserListEntry>::const_iterator user = diff->getDeletedUsers().begin(); user != diff->getDeletedUsers().end(); ++user)
    serializeUserListEntry(&*user, userList->addEntry());

  userList = data->createList("unchangedUsers");
  for(std::set<UserListEntry>::const_iterator user = diff->getUnchangedUsers().begin(); user != diff->getUnchangedUsers().end(); ++user)
    serializeUserListEntry(&*user, userList->addEntry());
}

boost::shared_ptr<UserListDiff> Util::deserializeUserListDiff(const Common::XmlData *data) {
  boost::shared_ptr<UserListDiff> diff(new UserListDiff);

  const Common::XmlData::List *userList = data->getList("addedUsers");
  if(userList) {
    for(Common::XmlData::List::const_iterator user = userList->begin(); user != userList->end(); ++user)
      diff->insertAddedUser(deserializeUserListEntry(user));
  }

  userList = data->getList("deletedUsers");
  if(userList) {
    for(Common::XmlData::List::const_iterator user = userList->begin(); user != userList->end(); ++user)
      diff->insertDeletedUser(deserializeUserListEntry(user));
  }

  userList = data->getList("unchangedUsers");
  if(userList) {
    for(Common::XmlData::List::const_iterator user = userList->begin(); user != userList->end(); ++user)
      diff->insertUnchangedUser(deserializeUserListEntry(user));
  }

  return diff;
}

}
}
}