Add named referencies implementation
[accounts-sso:vitalyrepins-signon.git] / src / signond / signonidentityinfo.cpp
1 /*
2  * This file is part of signon
3  *
4  * Copyright (C) 2009-2010 Nokia Corporation.
5  *
6  * Contact: Aurel Popirtac <ext-aurel.popirtac@nokia.com>
7  * Contact: Alberto Mardegan <alberto.mardegan@nokia.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * version 2.1 as published by the Free Software Foundation.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  */
23 #include "signonidentityinfo.h"
24
25 #include <QBuffer>
26 #include <QDataStream>
27 #include <QDebug>
28
29 namespace SignonDaemonNS {
30
31     SignonIdentityInfo::SignonIdentityInfo()
32         : m_id(0),
33           m_userName(QString()),
34           m_password(QString()),
35           m_caption(QString()),
36           m_realms(QStringList()),
37           m_accessControlList(QStringList()),
38           m_type(0),
39           m_refCount(0),
40           m_validated(false)
41     {
42     }
43
44     SignonIdentityInfo::SignonIdentityInfo(const quint32 id, const QString &userName,
45                 const QString &password, const QMap<QString, QVariant> &methods,
46                 const QString &caption, const QStringList &realms,
47                 const QStringList &accessControlList,
48                 int type, int refCount, bool validated)
49         : m_id(id),
50           m_userName(userName),
51           m_password(password),
52           m_caption(caption),
53           m_realms(realms),
54           m_methods(mapVariantToMapList(methods)),
55           m_accessControlList(accessControlList),
56           m_type(type),
57           m_refCount(refCount),
58           m_validated(validated)
59     {
60     }
61
62     const QList<QVariant> SignonIdentityInfo::toVariantList()
63     {
64         QList<QVariant> list;
65         list << m_id
66              << m_userName
67              << m_password
68              << m_caption
69              << m_realms
70              << QVariant(mapListToMapVariant(m_methods))
71              << m_accessControlList
72              << m_type
73              << m_refCount
74              << m_validated;
75
76         return list;
77     }
78
79     const QList<QVariant> SignonIdentityInfo::listToVariantList(
80             const QList<SignonIdentityInfo> &list)
81     {
82         QList<QVariant> variantList;
83         foreach(SignonIdentityInfo info, list)
84             variantList.append(QVariant(info.toVariantList())) ;
85         return variantList;
86     }
87
88     const QMap<QString, QVariant> SignonIdentityInfo::mapListToMapVariant(
89             const QMap<QString, QStringList> &mapList)
90     {
91         QMap<QString, QVariant> mapVariant;
92
93         QMapIterator<QString, QStringList> it(mapList);
94         while (it.hasNext()) {
95             it.next();
96             mapVariant.insert(it.key(), QVariant(it.value()));
97         }
98         return mapVariant;
99     }
100
101     const QMap<QString, QStringList> SignonIdentityInfo::mapVariantToMapList(
102             const QMap<QString, QVariant> &mapVariant)
103     {
104         QMap<QString, QStringList> mapList;
105
106         QMapIterator<QString, QVariant> it(mapVariant);
107         while (it.hasNext()) {
108             it.next();
109             mapList.insert(it.key(), it.value().toStringList());
110         }
111         return mapList;
112     }
113
114     const QString SignonIdentityInfo::serialize()
115     {
116         QString serialized;
117         QTextStream stream(&serialized);
118         stream << QString::fromLatin1("SignondIdentityInfo serialized:\nID = %1, ").arg(m_id);
119         stream << QString::fromLatin1("username = %1, ").arg(m_userName);
120         stream << QString::fromLatin1("password = %1, ").arg(m_password);
121         stream << QString::fromLatin1("caption = %1, ").arg(m_caption);
122         stream << QString::fromLatin1("realms = %1, \n").arg(m_realms.join(QLatin1String(" ")));
123         stream << QString::fromLatin1("acl = %1, \n").arg(m_accessControlList.join(QLatin1String(" ")));
124         stream << QString::fromLatin1("type = %1, \n").arg(m_type);
125         stream << QString::fromLatin1("refcount = %1, \n").arg(m_refCount);
126         stream << QString::fromLatin1("validated = %1, \n").arg(m_validated);
127
128         stream << "methods (";
129         for (QMap<QString, QStringList>::iterator it = m_methods.begin();
130              it != m_methods.end(); ++it) {
131             stream << QString::fromLatin1("(%1, (%2))").arg(it.key()).arg(it.value().join(QLatin1String(",")));
132         }
133         stream << ")";
134
135         return serialized;
136     }
137
138     bool SignonIdentityInfo::operator== (const SignonIdentityInfo &other) const
139     {
140         //do not care about list element order
141         SignonIdentityInfo me = *this;
142         SignonIdentityInfo you = other;
143         me.m_realms.sort();
144         you.m_realms.sort();
145         me.m_accessControlList.sort();
146         you.m_accessControlList.sort();
147         QMapIterator<QString, QStringList> it(me.m_methods);
148         while (it.hasNext()) {
149             it.next();
150             QStringList list1 = it.value();
151             QStringList list2 = you.m_methods.value(it.key());
152             list1.sort();
153             list2.sort();
154             if (list1 != list2) return false;
155         }
156
157         return (m_id == other.m_id)
158                 && (m_userName == other.m_userName)
159                 && (m_password == other.m_password)
160                 && (m_caption == other.m_caption)
161                 && (me.m_realms ==you.m_realms)
162                 && (me.m_accessControlList == you.m_accessControlList)
163                 && (m_type == other.m_type)
164                 && (m_validated == other.m_validated);
165     }
166
167 } //namespace SignonDaemonNS