signon  8.41
signonsessioncoretools.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of signon
3  *
4  * Copyright (C) 2009-2011 Nokia Corporation.
5  *
6  * Contact: Aurel Popirtac <ext-aurel.popirtac@nokia.com>
7  * Contact: Alberto Mardegan <alberto.mardegan@canonical.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 
24 #include "signonsessioncoretools.h"
25 
26 #include <QDebug>
27 #include "signond-common.h"
28 
29 using namespace SignonDaemonNS;
30 
31 QVariantMap SignonDaemonNS::mergeVariantMaps(const QVariantMap &map1,
32  const QVariantMap &map2)
33 {
34  if (map1.isEmpty()) return map2;
35  if (map2.isEmpty()) return map1;
36 
37  QVariantMap map = map1;
38  //map2 values will overwrite map1 values for the same keys.
39  QMapIterator<QString, QVariant> it(map2);
40  while (it.hasNext()) {
41  it.next();
42  if (map.contains(it.key()))
43  map.remove(it.key());
44  }
45  return map.unite(map2);
46 }
47 
48 /* --------------------- StoreOperation ---------------------- */
49 
51  m_storeType(type)
52 {
53 }
54 
56  m_storeType(src.m_storeType),
57  m_info(src.m_info),
58  m_authMethod(src.m_authMethod),
59  m_blobData(src.m_blobData)
60 {
61 }
62 
64 {
65 }
66 
67 /* --------------------- RequestData ---------------------- */
68 
69 RequestData::RequestData(const QDBusConnection &conn,
70  const QDBusMessage &msg,
71  const QVariantMap &params,
72  const QString &mechanism,
73  const QString &cancelKey):
74  m_conn(conn),
75  m_msg(msg),
76  m_params(params),
77  m_mechanism(mechanism),
78  m_cancelKey(cancelKey)
79 {
80 }
81 
83  m_conn(other.m_conn),
84  m_msg(other.m_msg),
85  m_params(other.m_params),
86  m_mechanism(other.m_mechanism),
87  m_cancelKey(other.m_cancelKey)
88 {
89 }
90 
92 {
93 }
94 
95 /* --------------------- AuthCoreCache ---------------------- */
96 
97 AuthCoreCache *AuthCoreCache::m_instance = 0;
98 
100 {
101 }
102 
103 AuthCoreCache::AuthCache::~AuthCache()
104 {
105 }
106 
108 {
109  return (m_password.isEmpty() && m_blobData.isEmpty());
110 }
111 
112 AuthCoreCache::AuthCoreCache(QObject *parent):
113  QObject(parent)
114 {
115 }
116 
118 {
119  clear();
120  m_instance = 0;
121 }
122 
124 {
125  if (m_instance == 0)
126  m_instance = new AuthCoreCache(parent);
127 
128  return m_instance;
129 }
130 
132 {
133  return m_cache.value(id, 0);
134 }
135 
136 void AuthCoreCache::insert(const CacheId &id, AuthCache *cache)
137 {
138  if (cache == 0) return;
139 
140  AuthCache *data = m_cache.take(id.first);
141 
142  if ((data != 0) && data->isEmpty()) {
143  delete data;
144  data = 0;
145  }
146 
147  if (data == 0) {
148  m_cache.insert(id.first, cache);
149  m_cachingSessionsMethods[id.first] = AuthMethods() << id.second;
150  } else {
151  if (cache->m_username.isEmpty())
152  cache->m_username = data->m_username;
153  if (cache->m_password.isEmpty())
154  cache->m_password = data->m_password;
155 
156  cache->m_blobData =
157  mergeVariantMaps(data->m_blobData, cache->m_blobData);
158 
159  delete data;
160  m_cache.insert(id.first, cache);
161 
162  AuthMethods cachingSessionsMethods = m_cachingSessionsMethods[id.first];
163  if (!cachingSessionsMethods.contains(id.second))
164  cachingSessionsMethods.append(id.second);
165  }
166 }
167 
169 {
170  AuthCache *data = m_cache.value(id.first, 0);
171  if (data != 0) {
172  AuthMethods authMethods = m_cachingSessionsMethods[id.first];
173  authMethods.removeOne(id.second);
174  if (authMethods.isEmpty()) {
175  delete m_cache.take(id.first);
176  (void)m_cachingSessionsMethods.take(id.first);
177  }
178  }
179 }
180 
182 {
183  QList<IdentityId> keys = m_cache.keys();
184  foreach (IdentityId key, keys)
185  delete m_cache.take(key);
186 
187  m_cachingSessionsMethods.clear();
188 }