summaryrefslogtreecommitdiffstats
path: root/src/Common/UserLists/Util.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Common/UserLists/Util.cpp')
-rw-r--r--src/Common/UserLists/Util.cpp111
1 files changed, 111 insertions, 0 deletions
diff --git a/src/Common/UserLists/Util.cpp b/src/Common/UserLists/Util.cpp
new file mode 100644
index 0000000..812c2c7
--- /dev/null
+++ b/src/Common/UserLists/Util.cpp
@@ -0,0 +1,111 @@
+/*
+ * 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"
+
+namespace Mad {
+namespace Common {
+namespace UserLists {
+
+void Util::serializeUserListEntry(const UserListEntry *user, 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(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, XmlData *data) {
+ 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 XmlData *data) {
+ boost::shared_ptr<UserList> users(new UserList);
+
+ const XmlData::List *userList = data->getList("users");
+
+ if(userList) {
+ for(XmlData::List::const_iterator user = userList->begin(); user != userList->end(); ++user)
+ users->push_back(deserializeUserListEntry(user));
+ }
+
+ return users;
+}
+
+void Util::serializeUserListDiff(const UserListDiff *diff, XmlData *data) {
+ 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 XmlData *data) {
+ boost::shared_ptr<UserListDiff> diff(new UserListDiff);
+
+ const XmlData::List *userList = data->getList("addedUsers");
+ if(userList) {
+ for(XmlData::List::const_iterator user = userList->begin(); user != userList->end(); ++user)
+ diff->insertAddedUser(deserializeUserListEntry(user));
+ }
+
+ userList = data->getList("deletedUsers");
+ if(userList) {
+ for(XmlData::List::const_iterator user = userList->begin(); user != userList->end(); ++user)
+ diff->insertDeletedUser(deserializeUserListEntry(user));
+ }
+
+ userList = data->getList("unchangedUsers");
+ if(userList) {
+ for(XmlData::List::const_iterator user = userList->begin(); user != userList->end(); ++user)
+ diff->insertUnchangedUser(deserializeUserListEntry(user));
+ }
+
+ return diff;
+}
+
+}
+}
+}