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
28bool Kleo::getCryptoConfigBoolValue(const char *componentName, const char *entryName)
29{
30 const CryptoConfig *const config = cryptoConfig();
31 if (!config) {
32 return false;
33 }
34 const CryptoConfigEntry *const entry = getCryptoConfigEntry(config, componentName, entryName);
35 if (entry && entry->argType() == CryptoConfigEntry::ArgType_None && !entry->isList()) {
36 return entry->boolValue();
37 }
38 return false;
39}
40
41int Kleo::getCryptoConfigIntValue(const char *componentName, const char *entryName, int defaultValue)
42{
43 if (!fakeCryptoConfigIntValues.empty()) {
44 const auto componentIt = fakeCryptoConfigIntValues.find(componentName);
45 if (componentIt != std::end(fakeCryptoConfigIntValues)) {
46 const auto entryIt = componentIt->second.find(entryName);
47 if (entryIt != std::end(componentIt->second)) {
48 return entryIt->second;
49 }
50 }
51 }
52
53 const CryptoConfig *const config = cryptoConfig();
54 if (!config) {
55 return defaultValue;
56 }
57 const CryptoConfigEntry *const entry = getCryptoConfigEntry(config, componentName, entryName);
58 if (entry && entry->argType() == CryptoConfigEntry::ArgType_Int) {
59 return entry->intValue();
60 }
61 return defaultValue;
62}
63
64QString Kleo::getCryptoConfigStringValue(const char *componentName, const char *entryName)
65{
66 if (!fakeCryptoConfigStringValues.empty()) {
67 const auto componentIt = fakeCryptoConfigStringValues.find(componentName);
68 if (componentIt != std::end(fakeCryptoConfigStringValues)) {
69 const auto entryIt = componentIt->second.find(entryName);
70 if (entryIt != std::end(componentIt->second)) {
71 return entryIt->second;
72 }
73 }
74 }
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->argType() == CryptoConfigEntry::ArgType_String) {
82 return entry->stringValue();
83 }
84 return {};
85}
86
87QList<QUrl> Kleo::getCryptoConfigUrlList(const char *componentName, const char *entryName)
88{
89 const CryptoConfig *const config = cryptoConfig();
90 if (!config) {
91 return {};
92 }
93 const CryptoConfigEntry *const entry = getCryptoConfigEntry(config, componentName, entryName);
94 if (entry && entry->isList() && (entry->argType() == CryptoConfigEntry::ArgType_LDAPURL || entry->argType() == CryptoConfigEntry::ArgType_Path)) {
95 return entry->urlValueList();
96 }
97 return {};
98}
99
100void Kleo::Private::setFakeCryptoConfigIntValue(const std::string &componentName, const std::string &entryName, int fakeValue)
101{
102 fakeCryptoConfigIntValues[componentName][entryName] = fakeValue;
103}
104
105void Kleo::Private::clearFakeCryptoConfigIntValue(const std::string &componentName, const std::string &entryName)
106{
107 auto &entryMap = fakeCryptoConfigIntValues[componentName];
108 entryMap.erase(entryName);
109 if (entryMap.empty()) {
110 fakeCryptoConfigIntValues.erase(componentName);
111 }
112}
113
114void Kleo::Private::setFakeCryptoConfigStringValue(const std::string &componentName, const std::string &entryName, const QString &fakeValue)
115{
116 fakeCryptoConfigStringValues[componentName][entryName] = fakeValue;
117}
118
119void Kleo::Private::clearFakeCryptoConfigStringValue(const std::string &componentName, const std::string &entryName)
120{
121 auto &entryMap = fakeCryptoConfigStringValues[componentName];
122 entryMap.erase(entryName);
123 if (entryMap.empty()) {
124 fakeCryptoConfigStringValues.erase(componentName);
125 }
126}
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:14 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.