KWallet

kwalletsessionstore.cpp
1/*
2 This file is part of the KDE Wallet Daemon
3 SPDX-FileCopyrightText: 2008 Michael Leupold <lemma@confuego.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "kwalletsessionstore.h"
9
10class KWalletSessionStore::Session
11{
12public:
13 QString m_service; // client dbus service (or empty)
14 int m_handle; // backend handle
15};
16
17KWalletSessionStore::KWalletSessionStore()
18{
19}
20
21KWalletSessionStore::~KWalletSessionStore()
22{
23 for (const QList<Session *> &l : std::as_const(m_sessions)) {
24 qDeleteAll(l);
25 }
26}
27
28void KWalletSessionStore::addSession(const QString &appid, const QString &service, int handle)
29{
30 Session *sess = new Session();
31 sess->m_service = service;
32 sess->m_handle = handle;
33 m_sessions[appid].append(sess);
34}
35
36bool KWalletSessionStore::hasSession(const QString &appid, int handle) const
37{
38 if (!m_sessions.contains(appid)) {
39 return false;
40 } else if (handle == -1) {
41 return true;
42 }
43
45 QList<Session *>::const_iterator end = m_sessions[appid].constEnd();
46 for (it = m_sessions[appid].constBegin(); it != end; ++it) {
47 Q_ASSERT(*it);
48 if ((*it)->m_handle == handle) {
49 return true;
50 }
51 }
52
53 return false;
54}
55
56QList<KWalletAppHandlePair> KWalletSessionStore::findSessions(const QString &service) const
57{
59 const QList<QString> sessionKeys(m_sessions.keys());
60 for (const QString &appid : sessionKeys) {
61 const auto lst = m_sessions[appid];
62 for (const Session *sess : lst) {
63 Q_ASSERT(sess);
64 if (sess->m_service == service) {
65 rc.append(qMakePair(appid, sess->m_handle));
66 }
67 }
68 }
69 return rc;
70}
71
72bool KWalletSessionStore::removeSession(const QString &appid, const QString &service, int handle)
73{
74 if (!m_sessions.contains(appid)) {
75 return false;
76 }
77
79 QList<Session *>::const_iterator end = m_sessions[appid].constEnd();
80 for (it = m_sessions[appid].constBegin(); it != end; ++it) {
81 Q_ASSERT(*it);
82 if ((*it)->m_service == service && (*it)->m_handle == handle) {
83 Session *sess = *it;
84 m_sessions[appid].removeAll(sess);
85 delete sess;
86 if (m_sessions[appid].isEmpty()) {
87 m_sessions.remove(appid);
88 }
89 return true;
90 }
91 }
92
93 return false;
94}
95
96int KWalletSessionStore::removeAllSessions(const QString &appid, int handle)
97{
98 if (!m_sessions.contains(appid)) {
99 return false;
100 }
101
103 QList<Session *>::iterator end = m_sessions[appid].end();
104 for (it = m_sessions[appid].begin(); it != end; ++it) {
105 Q_ASSERT(*it);
106 if ((*it)->m_handle == handle) {
107 delete *it;
108 *it = nullptr;
109 }
110 }
111
112 int removed = m_sessions[appid].removeAll(nullptr);
113 if (m_sessions[appid].isEmpty()) {
114 m_sessions.remove(appid);
115 }
116
117 return removed;
118}
119
120int KWalletSessionStore::removeAllSessions(int handle)
121{
122 QList<QString> appremove;
123 int numrem = 0;
124 const QList<QString> sessionKeys(m_sessions.keys());
125 for (const QString &appid : sessionKeys) {
127 QList<Session *>::iterator end = m_sessions[appid].end();
128 for (it = m_sessions[appid].begin(); it != end; ++it) {
129 Q_ASSERT(*it);
130 if ((*it)->m_handle == handle) {
131 delete *it;
132 *it = nullptr;
133 numrem++;
134 }
135 }
136 // remove all zeroed sessions
137 m_sessions[appid].removeAll(nullptr);
138 if (m_sessions[appid].isEmpty()) {
139 appremove.append(appid);
140 }
141 }
142
143 // now remove all applications without sessions
144 for (const QString &appid : std::as_const(appremove)) {
145 m_sessions.remove(appid);
146 }
147
148 return numrem;
149}
150
151QStringList KWalletSessionStore::getApplications(int handle) const
152{
153 QStringList rc;
154 const auto lst = m_sessions.keys();
155 for (const QString &appid : lst) {
156 if (hasSession(appid, handle)) {
157 rc.append(appid);
158 }
159 }
160 return rc;
161}
const QList< QKeySequence > & begin()
const QList< QKeySequence > & end()
const_iterator constEnd() const const
bool contains(const Key &key) const const
iterator end()
QList< Key > keys() const const
bool remove(const Key &key)
void append(QList< T > &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:12:12 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.