1 // -*- indent-tabs-mode: t; tab-width: 4; c-basic-offset: 4; -*-
3 This file is part of the KDE Wallet Daemon
5 Copyright (c) 2008 Michael Leupold <lemma@confuego.org>
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.
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.
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.
23 #include "kwalletsessionstore.h"
25 class KWalletSessionStore::Session
28 QString m_service
; // client dbus service (or empty)
29 int m_handle
; // backend handle
32 KWalletSessionStore::KWalletSessionStore()
36 KWalletSessionStore::~KWalletSessionStore()
38 Q_FOREACH(const QList
<Session
*> &l
, m_sessions
) {
43 void KWalletSessionStore::addSession(const QString
&appid
, const QString
&service
, int handle
)
45 Session
*sess
= new Session();
46 sess
->m_service
= service
;
47 sess
->m_handle
= handle
;
48 m_sessions
[appid
].append(sess
);
51 bool KWalletSessionStore::hasSession(const QString
&appid
, int handle
) const
53 if (!m_sessions
.contains(appid
)) {
55 } else if (handle
== -1) {
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
) {
63 if ((*it
)->m_handle
== handle
) {
71 QList
<KWalletAppHandlePair
> KWalletSessionStore::findSessions(const QString
&service
) const
73 QList
<KWalletAppHandlePair
> rc
;
74 Q_FOREACH(const QString
&appid
, m_sessions
.keys()) {
75 Q_FOREACH(const Session
*sess
, m_sessions
[appid
]) {
77 if (sess
->m_service
== service
) {
78 rc
.append(qMakePair(appid
, sess
->m_handle
));
85 bool KWalletSessionStore::removeSession(const QString
&appid
, const QString
&service
, int handle
)
87 if (!m_sessions
.contains(appid
)) {
91 QList
<Session
*>::const_iterator it
;
92 QList
<Session
*>::const_iterator end
= m_sessions
[appid
].constEnd();
93 for (it
= m_sessions
[appid
].constBegin(); it
!= end
; ++it
) {
95 if ((*it
)->m_service
== service
&& (*it
)->m_handle
== handle
) {
97 m_sessions
[appid
].removeAll(sess
);
99 if (m_sessions
[appid
].isEmpty()) {
100 m_sessions
.remove(appid
);
110 int KWalletSessionStore::removeAllSessions(const QString
&appid
, int handle
)
112 if (!m_sessions
.contains(appid
)) {
116 QList
<Session
*>::iterator it
;
117 QList
<Session
*>::iterator end
= m_sessions
[appid
].end();
118 for (it
= m_sessions
[appid
].begin(); it
!= end
; ++it
) {
120 if ((*it
)->m_handle
== handle
) {
126 int removed
= m_sessions
[appid
].removeAll(0);
127 if (m_sessions
[appid
].isEmpty()) {
128 m_sessions
.remove(appid
);
134 int KWalletSessionStore::removeAllSessions(int handle
) {
135 QList
<QString
> appremove
;
137 Q_FOREACH(const QString
&appid
, m_sessions
.keys()) {
138 QList
<Session
*>::iterator it
;
139 QList
<Session
*>::iterator end
= m_sessions
[appid
].end();
140 for (it
= m_sessions
[appid
].begin(); it
!= end
; ++it
) {
142 if ((*it
)->m_handle
== handle
) {
148 // remove all zeroed sessions
149 m_sessions
[appid
].removeAll(0);
150 if (m_sessions
[appid
].isEmpty()) {
151 appremove
.append(appid
);
155 // now remove all applications without sessions
156 Q_FOREACH(const QString
&appid
, appremove
) {
157 m_sessions
.remove(appid
);
163 QStringList
KWalletSessionStore::getApplications(int handle
) const {
165 Q_FOREACH(const QString
&appid
, m_sessions
.uniqueKeys()) {
166 if (hasSession(appid
, handle
)) {