Restructured signon database and added unit tests
[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     {
40     }
41
42     SignonIdentityInfo::SignonIdentityInfo(const quint32 id, const QString &userName,
43                 const QString &password, const QMap<QString, QVariant> &methods,
44                 const QString &caption, const QStringList &realms,
45                 const QStringList &accessControlList, const int type)
46         : m_id(id),
47           m_userName(userName),
48           m_password(password),
49           m_caption(caption),
50           m_realms(realms),
51           m_methods(mapVariantToMapList(methods)),
52           m_accessControlList(accessControlList),
53           m_type(type)
54     {
55     }
56
57     const QList<QVariant> SignonIdentityInfo::toVariantList()
58     {
59         QList<QVariant> list;
60         list << m_id
61              << m_userName
62              << m_password
63              << m_caption
64              << m_realms
65              << QVariant(mapListToMapVariant(m_methods))
66              << m_accessControlList
67              << m_type;
68
69         return list;
70     }
71
72     const QList<QVariant> SignonIdentityInfo::listToVariantList(
73             const QList<SignonIdentityInfo> &list)
74     {
75         QList<QVariant> variantList;
76         foreach(SignonIdentityInfo info, list)
77             variantList.append(QVariant(info.toVariantList())) ;
78         return variantList;
79     }
80
81     const QMap<QString, QVariant> SignonIdentityInfo::mapListToMapVariant(
82             const QMap<QString, QStringList> &mapList)
83     {
84         QMap<QString, QVariant> mapVariant;
85
86         QMapIterator<QString, QStringList> it(mapList);
87         while (it.hasNext()) {
88             it.next();
89             mapVariant.insert(it.key(), QVariant(it.value()));
90         }
91         return mapVariant;
92     }
93
94     const QMap<QString, QStringList> SignonIdentityInfo::mapVariantToMapList(
95             const QMap<QString, QVariant> &mapVariant)
96     {
97         QMap<QString, QStringList> mapList;
98
99         QMapIterator<QString, QVariant> it(mapVariant);
100         while (it.hasNext()) {
101             it.next();
102             mapList.insert(it.key(), it.value().toStringList());
103         }
104         return mapList;
105     }
106
107     const QString SignonIdentityInfo::serialize()
108     {
109         QString serialized;
110         QTextStream stream(&serialized);
111         stream << QString::fromLatin1("SignondIdentityInfo serialized:\nID = %1, ").arg(m_id);
112         stream << QString::fromLatin1("username = %1, ").arg(m_userName);
113         stream << QString::fromLatin1("password = %1, ").arg(m_password);
114         stream << QString::fromLatin1("caption = %1, ").arg(m_caption);
115         stream << QString::fromLatin1("realms = %1, \n").arg(m_realms.join(QLatin1String(" ")));
116         stream << QString::fromLatin1("acl = %1, \n").arg(m_accessControlList.join(QLatin1String(" ")));
117         stream << QString::fromLatin1("type = %1, \n").arg(m_type);
118
119         stream << "methods (";
120         for (QMap<QString, QStringList>::iterator it = m_methods.begin();
121              it != m_methods.end(); ++it) {
122             stream << QString::fromLatin1("(%1, (%2))").arg(it.key()).arg(it.value().join(QLatin1String(",")));
123         }
124         stream << ")";
125
126         return serialized;
127     }
128
129     bool SignonIdentityInfo::operator== (const SignonIdentityInfo &other) const
130     {
131         return (m_id == other.m_id)
132                 && (m_userName == other.m_userName)
133                 && (m_password == other.m_password)
134                 && (m_caption == other.m_caption)
135                 && (m_realms == other.m_realms)
136                 && (m_accessControlList == other.m_accessControlList)
137                 && (m_methods ==other.m_methods)
138                 && (m_type == other.m_type);
139     }
140
141 } //namespace SignonDaemonNS