File indexing completed on 2025-01-05 04:55:52

0001 /*
0002     utils/cryptoconfig.cpp
0003 
0004     This file is part of libkleopatra, the KDE keymanagement library
0005     SPDX-FileCopyrightText: 2021 g10 Code GmbH
0006     SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include <config-libkleo.h>
0012 
0013 #include "cryptoconfig.h"
0014 #include "cryptoconfig_p.h"
0015 
0016 #include "compat.h"
0017 
0018 #include <QGpgME/CryptoConfig>
0019 #include <QGpgME/Protocol>
0020 
0021 #include <unordered_map>
0022 
0023 using namespace QGpgME;
0024 
0025 static std::unordered_map<std::string, std::unordered_map<std::string, int>> fakeCryptoConfigIntValues;
0026 static std::unordered_map<std::string, std::unordered_map<std::string, QString>> fakeCryptoConfigStringValues;
0027 
0028 int Kleo::getCryptoConfigIntValue(const char *componentName, const char *entryName, int defaultValue)
0029 {
0030     if (!fakeCryptoConfigIntValues.empty()) {
0031         const auto componentIt = fakeCryptoConfigIntValues.find(componentName);
0032         if (componentIt != std::end(fakeCryptoConfigIntValues)) {
0033             const auto entryIt = componentIt->second.find(entryName);
0034             if (entryIt != std::end(componentIt->second)) {
0035                 return entryIt->second;
0036             }
0037         }
0038     }
0039 
0040     const CryptoConfig *const config = cryptoConfig();
0041     if (!config) {
0042         return defaultValue;
0043     }
0044     const CryptoConfigEntry *const entry = getCryptoConfigEntry(config, componentName, entryName);
0045     if (entry && entry->argType() == CryptoConfigEntry::ArgType_Int) {
0046         return entry->intValue();
0047     }
0048     return defaultValue;
0049 }
0050 
0051 QString Kleo::getCryptoConfigStringValue(const char *componentName, const char *entryName)
0052 {
0053     if (!fakeCryptoConfigStringValues.empty()) {
0054         const auto componentIt = fakeCryptoConfigStringValues.find(componentName);
0055         if (componentIt != std::end(fakeCryptoConfigStringValues)) {
0056             const auto entryIt = componentIt->second.find(entryName);
0057             if (entryIt != std::end(componentIt->second)) {
0058                 return entryIt->second;
0059             }
0060         }
0061     }
0062 
0063     const CryptoConfig *const config = cryptoConfig();
0064     if (!config) {
0065         return {};
0066     }
0067     const CryptoConfigEntry *const entry = getCryptoConfigEntry(config, componentName, entryName);
0068     if (entry && entry->argType() == CryptoConfigEntry::ArgType_String) {
0069         return entry->stringValue();
0070     }
0071     return {};
0072 }
0073 
0074 QList<QUrl> Kleo::getCryptoConfigUrlList(const char *componentName, const char *entryName)
0075 {
0076     const CryptoConfig *const config = cryptoConfig();
0077     if (!config) {
0078         return {};
0079     }
0080     const CryptoConfigEntry *const entry = getCryptoConfigEntry(config, componentName, entryName);
0081     if (entry && entry->isList() && (entry->argType() == CryptoConfigEntry::ArgType_LDAPURL || entry->argType() == CryptoConfigEntry::ArgType_Path)) {
0082         return entry->urlValueList();
0083     }
0084     return {};
0085 }
0086 
0087 void Kleo::Private::setFakeCryptoConfigIntValue(const std::string &componentName, const std::string &entryName, int fakeValue)
0088 {
0089     fakeCryptoConfigIntValues[componentName][entryName] = fakeValue;
0090 }
0091 
0092 void Kleo::Private::clearFakeCryptoConfigIntValue(const std::string &componentName, const std::string &entryName)
0093 {
0094     auto &entryMap = fakeCryptoConfigIntValues[componentName];
0095     entryMap.erase(entryName);
0096     if (entryMap.empty()) {
0097         fakeCryptoConfigIntValues.erase(componentName);
0098     }
0099 }
0100 
0101 void Kleo::Private::setFakeCryptoConfigStringValue(const std::string &componentName, const std::string &entryName, const QString &fakeValue)
0102 {
0103     fakeCryptoConfigStringValues[componentName][entryName] = fakeValue;
0104 }
0105 
0106 void Kleo::Private::clearFakeCryptoConfigStringValue(const std::string &componentName, const std::string &entryName)
0107 {
0108     auto &entryMap = fakeCryptoConfigStringValues[componentName];
0109     entryMap.erase(entryName);
0110     if (entryMap.empty()) {
0111         fakeCryptoConfigStringValues.erase(componentName);
0112     }
0113 }