Libkleo

cryptoconfig.cpp
1/*
2 utils/cryptoconfig.cpp
3
4 This file is part of libkleopatra, the KDE keymanagement library
5 SPDX-FileCopyrightText: 2021 g10 Code GmbH
6 SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
7
8 SPDX-License-Identifier: GPL-2.0-or-later
9*/
10
11#include <config-libkleo.h>
12
13#include "cryptoconfig.h"
14#include "cryptoconfig_p.h"
15
16#include "compat.h"
17
18#include <QGpgME/CryptoConfig>
19#include <QGpgME/Protocol>
20
21#include <unordered_map>
22
23using namespace QGpgME;
24
25static std::unordered_map<std::string, std::unordered_map<std::string, int>> fakeCryptoConfigIntValues;
26static std::unordered_map<std::string, std::unordered_map<std::string, QString>> fakeCryptoConfigStringValues;
27
28int Kleo::getCryptoConfigIntValue(const char *componentName, const char *entryName, int defaultValue)
29{
30 if (!fakeCryptoConfigIntValues.empty()) {
31 const auto componentIt = fakeCryptoConfigIntValues.find(componentName);
32 if (componentIt != std::end(fakeCryptoConfigIntValues)) {
33 const auto entryIt = componentIt->second.find(entryName);
34 if (entryIt != std::end(componentIt->second)) {
35 return entryIt->second;
36 }
37 }
38 }
39
40 const CryptoConfig *const config = cryptoConfig();
41 if (!config) {
42 return defaultValue;
43 }
44 const CryptoConfigEntry *const entry = getCryptoConfigEntry(config, componentName, entryName);
45 if (entry && entry->argType() == CryptoConfigEntry::ArgType_Int) {
46 return entry->intValue();
47 }
48 return defaultValue;
49}
50
51QString Kleo::getCryptoConfigStringValue(const char *componentName, const char *entryName)
52{
53 if (!fakeCryptoConfigStringValues.empty()) {
54 const auto componentIt = fakeCryptoConfigStringValues.find(componentName);
55 if (componentIt != std::end(fakeCryptoConfigStringValues)) {
56 const auto entryIt = componentIt->second.find(entryName);
57 if (entryIt != std::end(componentIt->second)) {
58 return entryIt->second;
59 }
60 }
61 }
62
63 const CryptoConfig *const config = cryptoConfig();
64 if (!config) {
65 return {};
66 }
67 const CryptoConfigEntry *const entry = getCryptoConfigEntry(config, componentName, entryName);
68 if (entry && entry->argType() == CryptoConfigEntry::ArgType_String) {
69 return entry->stringValue();
70 }
71 return {};
72}
73
74QList<QUrl> Kleo::getCryptoConfigUrlList(const char *componentName, const char *entryName)
75{
76 const CryptoConfig *const config = cryptoConfig();
77 if (!config) {
78 return {};
79 }
80 const CryptoConfigEntry *const entry = getCryptoConfigEntry(config, componentName, entryName);
81 if (entry && entry->isList() && (entry->argType() == CryptoConfigEntry::ArgType_LDAPURL || entry->argType() == CryptoConfigEntry::ArgType_Path)) {
82 return entry->urlValueList();
83 }
84 return {};
85}
86
87void Kleo::Private::setFakeCryptoConfigIntValue(const std::string &componentName, const std::string &entryName, int fakeValue)
88{
89 fakeCryptoConfigIntValues[componentName][entryName] = fakeValue;
90}
91
92void Kleo::Private::clearFakeCryptoConfigIntValue(const std::string &componentName, const std::string &entryName)
93{
94 auto &entryMap = fakeCryptoConfigIntValues[componentName];
95 entryMap.erase(entryName);
96 if (entryMap.empty()) {
97 fakeCryptoConfigIntValues.erase(componentName);
98 }
99}
100
101void Kleo::Private::setFakeCryptoConfigStringValue(const std::string &componentName, const std::string &entryName, const QString &fakeValue)
102{
103 fakeCryptoConfigStringValues[componentName][entryName] = fakeValue;
104}
105
106void Kleo::Private::clearFakeCryptoConfigStringValue(const std::string &componentName, const std::string &entryName)
107{
108 auto &entryMap = fakeCryptoConfigStringValues[componentName];
109 entryMap.erase(entryName);
110 if (entryMap.empty()) {
111 fakeCryptoConfigStringValues.erase(componentName);
112 }
113}
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 14 2024 11:51:59 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.