add more spacing
[personal-kdebase.git] / runtime / kwalletd / kwalletsessionstore.cpp
blob1bc582e595d4e1f550907954ac8313d1adebb0b9
1 // -*- indent-tabs-mode: t; tab-width: 4; c-basic-offset: 4; -*-
2 /*
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
27 public:
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) {
39 qDeleteAll(l);
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)) {
54 return false;
55 } else if (handle == -1) {
56 return true;
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;
68 return false;
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]) {
76 Q_ASSERT(sess);
77 if (sess->m_service == service) {
78 rc.append(qMakePair(appid, sess->m_handle));
82 return rc;
85 bool KWalletSessionStore::removeSession(const QString &appid, const QString &service, int handle)
87 if (!m_sessions.contains(appid)) {
88 return false;
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) {
94 Q_ASSERT(*it);
95 if ((*it)->m_service == service && (*it)->m_handle == handle) {
96 Session *sess = *it;
97 m_sessions[appid].removeAll(sess);
98 delete sess;
99 if (m_sessions[appid].isEmpty()) {
100 m_sessions.remove(appid);
102 return true;
106 return false;
110 int KWalletSessionStore::removeAllSessions(const QString &appid, int handle)
112 if (!m_sessions.contains(appid)) {
113 return false;
116 QList<Session*>::iterator it;
117 QList<Session*>::iterator end = m_sessions[appid].end();
118 for (it = m_sessions[appid].begin(); it != end; ++it) {
119 Q_ASSERT(*it);
120 if ((*it)->m_handle == handle) {
121 delete *it;
122 *it = 0;
126 int removed = m_sessions[appid].removeAll(0);
127 if (m_sessions[appid].isEmpty()) {
128 m_sessions.remove(appid);
131 return removed;
134 int KWalletSessionStore::removeAllSessions(int handle) {
135 QList<QString> appremove;
136 int numrem = 0;
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) {
141 Q_ASSERT(*it);
142 if ((*it)->m_handle == handle) {
143 delete *it;
144 *it = 0;
145 numrem++;
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);
160 return numrem;
163 QStringList KWalletSessionStore::getApplications(int handle) const {
164 QStringList rc;
165 Q_FOREACH(const QString &appid, m_sessions.uniqueKeys()) {
166 if (hasSession(appid, handle)) {
167 rc.append(appid);
170 return rc;