File indexing completed on 2024-11-24 03:41:07

0001 /*
0002     This file is part of the KDE Wallet Daemon
0003     SPDX-FileCopyrightText: 2008 Michael Leupold <lemma@confuego.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "kwalletsessionstore.h"
0009 
0010 class KWalletSessionStore::Session
0011 {
0012 public:
0013     QString m_service; // client dbus service (or empty)
0014     int m_handle; // backend handle
0015 };
0016 
0017 KWalletSessionStore::KWalletSessionStore()
0018 {
0019 }
0020 
0021 KWalletSessionStore::~KWalletSessionStore()
0022 {
0023     for (const QList<Session *> &l : std::as_const(m_sessions)) {
0024         qDeleteAll(l);
0025     }
0026 }
0027 
0028 void KWalletSessionStore::addSession(const QString &appid, const QString &service, int handle)
0029 {
0030     Session *sess = new Session();
0031     sess->m_service = service;
0032     sess->m_handle = handle;
0033     m_sessions[appid].append(sess);
0034 }
0035 
0036 bool KWalletSessionStore::hasSession(const QString &appid, int handle) const
0037 {
0038     if (!m_sessions.contains(appid)) {
0039         return false;
0040     } else if (handle == -1) {
0041         return true;
0042     }
0043 
0044     QList<Session *>::const_iterator it;
0045     QList<Session *>::const_iterator end = m_sessions[appid].constEnd();
0046     for (it = m_sessions[appid].constBegin(); it != end; ++it) {
0047         Q_ASSERT(*it);
0048         if ((*it)->m_handle == handle) {
0049             return true;
0050         }
0051     }
0052 
0053     return false;
0054 }
0055 
0056 QList<KWalletAppHandlePair> KWalletSessionStore::findSessions(const QString &service) const
0057 {
0058     QList<KWalletAppHandlePair> rc;
0059     const QList<QString> sessionKeys(m_sessions.keys());
0060     for (const QString &appid : sessionKeys) {
0061         const auto lst = m_sessions[appid];
0062         for (const Session *sess : lst) {
0063             Q_ASSERT(sess);
0064             if (sess->m_service == service) {
0065                 rc.append(qMakePair(appid, sess->m_handle));
0066             }
0067         }
0068     }
0069     return rc;
0070 }
0071 
0072 bool KWalletSessionStore::removeSession(const QString &appid, const QString &service, int handle)
0073 {
0074     if (!m_sessions.contains(appid)) {
0075         return false;
0076     }
0077 
0078     QList<Session *>::const_iterator it;
0079     QList<Session *>::const_iterator end = m_sessions[appid].constEnd();
0080     for (it = m_sessions[appid].constBegin(); it != end; ++it) {
0081         Q_ASSERT(*it);
0082         if ((*it)->m_service == service && (*it)->m_handle == handle) {
0083             Session *sess = *it;
0084             m_sessions[appid].removeAll(sess);
0085             delete sess;
0086             if (m_sessions[appid].isEmpty()) {
0087                 m_sessions.remove(appid);
0088             }
0089             return true;
0090         }
0091     }
0092 
0093     return false;
0094 }
0095 
0096 int KWalletSessionStore::removeAllSessions(const QString &appid, int handle)
0097 {
0098     if (!m_sessions.contains(appid)) {
0099         return false;
0100     }
0101 
0102     QList<Session *>::iterator it;
0103     QList<Session *>::iterator end = m_sessions[appid].end();
0104     for (it = m_sessions[appid].begin(); it != end; ++it) {
0105         Q_ASSERT(*it);
0106         if ((*it)->m_handle == handle) {
0107             delete *it;
0108             *it = nullptr;
0109         }
0110     }
0111 
0112     int removed = m_sessions[appid].removeAll(nullptr);
0113     if (m_sessions[appid].isEmpty()) {
0114         m_sessions.remove(appid);
0115     }
0116 
0117     return removed;
0118 }
0119 
0120 int KWalletSessionStore::removeAllSessions(int handle)
0121 {
0122     QList<QString> appremove;
0123     int numrem = 0;
0124     const QList<QString> sessionKeys(m_sessions.keys());
0125     for (const QString &appid : sessionKeys) {
0126         QList<Session *>::iterator it;
0127         QList<Session *>::iterator end = m_sessions[appid].end();
0128         for (it = m_sessions[appid].begin(); it != end; ++it) {
0129             Q_ASSERT(*it);
0130             if ((*it)->m_handle == handle) {
0131                 delete *it;
0132                 *it = nullptr;
0133                 numrem++;
0134             }
0135         }
0136         // remove all zeroed sessions
0137         m_sessions[appid].removeAll(nullptr);
0138         if (m_sessions[appid].isEmpty()) {
0139             appremove.append(appid);
0140         }
0141     }
0142 
0143     // now remove all applications without sessions
0144     for (const QString &appid : std::as_const(appremove)) {
0145         m_sessions.remove(appid);
0146     }
0147 
0148     return numrem;
0149 }
0150 
0151 QStringList KWalletSessionStore::getApplications(int handle) const
0152 {
0153     QStringList rc;
0154     const auto lst = m_sessions.keys();
0155     for (const QString &appid : lst) {
0156         if (hasSession(appid, handle)) {
0157             rc.append(appid);
0158         }
0159     }
0160     return rc;
0161 }