1 | // -*- indent-tabs-mode: t; tab-width: 4; c-basic-offset: 4; -*- |
2 | /* |
3 | This file is part of the KDE Wallet Daemon |
4 | |
5 | Copyright (c) 2008 Michael Leupold <lemma@confuego.org> |
6 | |
7 | This library is free software; you can redistribute it and/or |
8 | modify it under the terms of the GNU Library General Public |
9 | License as published by the Free Software Foundation; either |
10 | version 2 of the License, or (at your option) any later version. |
11 | |
12 | This library is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | Library General Public License for more details. |
16 | |
17 | You should have received a copy of the GNU Library General Public License |
18 | along with this library; see the file COPYING.LIB. If not, write to |
19 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 | Boston, MA 02110-1301, USA. |
21 | */ |
22 | |
23 | #include "kwalletsessionstore.h" |
24 | |
25 | class KWalletSessionStore::Session |
26 | { |
27 | public: |
28 | QString m_service; // client dbus service (or empty) |
29 | int m_handle; // backend handle |
30 | }; |
31 | |
32 | KWalletSessionStore::KWalletSessionStore() |
33 | { |
34 | } |
35 | |
36 | KWalletSessionStore::~KWalletSessionStore() |
37 | { |
38 | Q_FOREACH(const QList<Session*> &l, m_sessions) { |
39 | qDeleteAll(l); |
40 | } |
41 | } |
42 | |
43 | void KWalletSessionStore::addSession(const QString &appid, const QString &service, int handle) |
44 | { |
45 | Session *sess = new Session(); |
46 | sess->m_service = service; |
47 | sess->m_handle = handle; |
48 | m_sessions[appid].append(sess); |
49 | } |
50 | |
51 | bool KWalletSessionStore::hasSession(const QString &appid, int handle) const |
52 | { |
53 | if (!m_sessions.contains(appid)) { |
54 | return false; |
55 | } else if (handle == -1) { |
56 | return true; |
57 | } |
58 | |
59 | QList<Session*>::const_iterator it; |
60 | QList<Session*>::const_iterator end = m_sessions[appid].constEnd(); |
61 | for (it = m_sessions[appid].constBegin(); it != end; ++it) { |
62 | Q_ASSERT(*it); |
63 | if ((*it)->m_handle == handle) { |
64 | return true; |
65 | } |
66 | } |
67 | |
68 | return false; |
69 | } |
70 | |
71 | QList<KWalletAppHandlePair> KWalletSessionStore::findSessions(const QString &service) const |
72 | { |
73 | QList<KWalletAppHandlePair> rc; |
74 | QList<QString> sessionKeys(m_sessions.keys()); |
75 | Q_FOREACH(const QString &appid, sessionKeys) { |
76 | Q_FOREACH(const Session *sess, m_sessions[appid]) { |
77 | Q_ASSERT(sess); |
78 | if (sess->m_service == service) { |
79 | rc.append(qMakePair(appid, sess->m_handle)); |
80 | } |
81 | } |
82 | } |
83 | return rc; |
84 | } |
85 | |
86 | bool KWalletSessionStore::removeSession(const QString &appid, const QString &service, int handle) |
87 | { |
88 | if (!m_sessions.contains(appid)) { |
89 | return false; |
90 | } |
91 | |
92 | QList<Session*>::const_iterator it; |
93 | QList<Session*>::const_iterator end = m_sessions[appid].constEnd(); |
94 | for (it = m_sessions[appid].constBegin(); it != end; ++it) { |
95 | Q_ASSERT(*it); |
96 | if ((*it)->m_service == service && (*it)->m_handle == handle) { |
97 | Session *sess = *it; |
98 | m_sessions[appid].removeAll(sess); |
99 | delete sess; |
100 | if (m_sessions[appid].isEmpty()) { |
101 | m_sessions.remove(appid); |
102 | } |
103 | return true; |
104 | } |
105 | } |
106 | |
107 | return false; |
108 | } |
109 | |
110 | |
111 | int KWalletSessionStore::removeAllSessions(const QString &appid, int handle) |
112 | { |
113 | if (!m_sessions.contains(appid)) { |
114 | return false; |
115 | } |
116 | |
117 | QList<Session*>::iterator it; |
118 | QList<Session*>::iterator end = m_sessions[appid].end(); |
119 | for (it = m_sessions[appid].begin(); it != end; ++it) { |
120 | Q_ASSERT(*it); |
121 | if ((*it)->m_handle == handle) { |
122 | delete *it; |
123 | *it = 0; |
124 | } |
125 | } |
126 | |
127 | int removed = m_sessions[appid].removeAll(0); |
128 | if (m_sessions[appid].isEmpty()) { |
129 | m_sessions.remove(appid); |
130 | } |
131 | |
132 | return removed; |
133 | } |
134 | |
135 | int KWalletSessionStore::removeAllSessions(int handle) { |
136 | QList<QString> appremove; |
137 | int numrem = 0; |
138 | QList<QString> sessionKeys(m_sessions.keys()); |
139 | Q_FOREACH(const QString &appid, sessionKeys) { |
140 | QList<Session*>::iterator it; |
141 | QList<Session*>::iterator end = m_sessions[appid].end(); |
142 | for (it = m_sessions[appid].begin(); it != end; ++it) { |
143 | Q_ASSERT(*it); |
144 | if ((*it)->m_handle == handle) { |
145 | delete *it; |
146 | *it = 0; |
147 | numrem++; |
148 | } |
149 | } |
150 | // remove all zeroed sessions |
151 | m_sessions[appid].removeAll(0); |
152 | if (m_sessions[appid].isEmpty()) { |
153 | appremove.append(appid); |
154 | } |
155 | } |
156 | |
157 | // now remove all applications without sessions |
158 | Q_FOREACH(const QString &appid, appremove) { |
159 | m_sessions.remove(appid); |
160 | } |
161 | |
162 | return numrem; |
163 | } |
164 | |
165 | QStringList KWalletSessionStore::getApplications(int handle) const { |
166 | QStringList rc; |
167 | Q_FOREACH(const QString &appid, m_sessions.uniqueKeys()) { |
168 | if (hasSession(appid, handle)) { |
169 | rc.append(appid); |
170 | } |
171 | } |
172 | return rc; |
173 | } |
174 | |