10#include <config-libkleo.h>
14#include <libkleo/keycache.h>
16#include <libkleo_debug.h>
18#include <KLazyLocalizedString>
19#include <KLocalizedString>
24#include <gpgme++/key.h>
25#include <gpgme++/tofuinfo.h>
30 Kleo::CryptoMessageFormat format;
32 const char *configName;
33} cryptoMessageFormats[] = {
35 {Kleo::InlineOpenPGPFormat, kli18n(
"Inline OpenPGP (deprecated)"),
"inline openpgp"},
36 {Kleo::OpenPGPMIMEFormat, kli18n(
"OpenPGP/MIME"),
"openpgp/mime" },
37 {Kleo::SMIMEFormat, kli18n(
"S/MIME"),
"s/mime" },
38 {Kleo::SMIMEOpaqueFormat, kli18n(
"S/MIME Opaque"),
"s/mime opaque" },
39 {Kleo::AnySMIME, kli18n(
"Any S/MIME"),
"any s/mime" },
40 {Kleo::AnyOpenPGP, kli18n(
"Any OpenPGP"),
"any openpgp" },
43static const unsigned int numCryptoMessageFormats =
sizeof cryptoMessageFormats /
sizeof *cryptoMessageFormats;
45const char *Kleo::cryptoMessageFormatToString(Kleo::CryptoMessageFormat f)
47 if (f == AutoFormat) {
50 for (
unsigned int i = 0; i < numCryptoMessageFormats; ++i) {
51 if (f == cryptoMessageFormats[i].format) {
52 return cryptoMessageFormats[i].configName;
58QStringList Kleo::cryptoMessageFormatsToStringList(
unsigned int f)
61 for (
unsigned int i = 0; i < numCryptoMessageFormats; ++i) {
62 if (f & cryptoMessageFormats[i].format) {
69QString Kleo::cryptoMessageFormatToLabel(Kleo::CryptoMessageFormat f)
71 if (f == AutoFormat) {
74 for (
unsigned int i = 0; i < numCryptoMessageFormats; ++i) {
75 if (f == cryptoMessageFormats[i].format) {
82Kleo::CryptoMessageFormat Kleo::stringToCryptoMessageFormat(
const QString &s)
85 for (
unsigned int i = 0; i < numCryptoMessageFormats; ++i) {
87 return cryptoMessageFormats[i].format;
93unsigned int Kleo::stringListToCryptoMessageFormats(
const QStringList &sl)
95 unsigned int result = 0;
97 result |= stringToCryptoMessageFormat(*it);
104const char *Kleo::encryptionPreferenceToString(EncryptionPreference pref)
107 case UnknownPreference:
113 case AlwaysEncryptIfPossible:
114 return "alwaysIfPossible";
115 case AlwaysAskForEncryption:
117 case AskWheneverPossible:
118 return "askWhenPossible";
123Kleo::EncryptionPreference Kleo::stringToEncryptionPreference(
const QString &str)
129 return AlwaysEncrypt;
132 return AlwaysEncryptIfPossible;
135 return AlwaysAskForEncryption;
138 return AskWheneverPossible;
140 return UnknownPreference;
143QString Kleo::encryptionPreferenceToLabel(EncryptionPreference pref)
147 return i18n(
"Never Encrypt");
149 return i18n(
"Always Encrypt");
150 case AlwaysEncryptIfPossible:
151 return i18n(
"Always Encrypt If Possible");
152 case AlwaysAskForEncryption:
154 case AskWheneverPossible:
155 return i18n(
"Ask Whenever Possible");
157 return xi18nc(
"no specific preference",
"<placeholder>none</placeholder>");
161const char *Kleo::signingPreferenceToString(SigningPreference pref)
164 case UnknownSigningPreference:
170 case AlwaysSignIfPossible:
171 return "alwaysIfPossible";
172 case AlwaysAskForSigning:
174 case AskSigningWheneverPossible:
175 return "askWhenPossible";
180Kleo::SigningPreference Kleo::stringToSigningPreference(
const QString &str)
189 return AlwaysSignIfPossible;
192 return AlwaysAskForSigning;
195 return AskSigningWheneverPossible;
197 return UnknownSigningPreference;
200QString Kleo::signingPreferenceToLabel(SigningPreference pref)
204 return i18n(
"Never Sign");
206 return i18n(
"Always Sign");
207 case AlwaysSignIfPossible:
208 return i18n(
"Always Sign If Possible");
209 case AlwaysAskForSigning:
211 case AskSigningWheneverPossible:
212 return i18n(
"Ask Whenever Possible");
214 return i18nc(
"no specific preference",
"<none>");
218Kleo::TrustLevel Kleo::trustLevel(
const GpgME::Key &key)
220 TrustLevel maxTl = Level0;
221 for (
int i = 0, c = key.numUserIDs(); i < c; ++i) {
222 const auto tl = trustLevel(key.userID(i));
223 maxTl = qMax(maxTl, tl);
224 if (maxTl == Level4) {
235bool hasTrustedSignature(
const GpgME::UserID &uid)
238 static std::shared_ptr<const Kleo::KeyCache> keyCache;
240 keyCache = Kleo::KeyCache::instance();
243 const auto signatures = uid.signatures();
244 std::vector<std::string> sigKeyIDs;
245 std::transform(signatures.cbegin(),
247 std::back_inserter(sigKeyIDs),
248 std::bind(&GpgME::UserID::Signature::signerKeyID, std::placeholders::_1));
250 const auto keys = keyCache->findByKeyIDOrFingerprint(sigKeyIDs);
251 return std::any_of(keys.cbegin(), keys.cend(), [](
const GpgME::Key &key) {
252 return key.ownerTrust() == GpgME::Key::Ultimate;
258Kleo::TrustLevel Kleo::trustLevel(
const GpgME::UserID &uid)
265 switch (uid.validity()) {
266 case GpgME::UserID::Unknown:
267 case GpgME::UserID::Undefined:
268 case GpgME::UserID::Never:
272 case GpgME::UserID::Marginal:
275 if (uid.tofuInfo().isNull()) {
279 switch (uid.tofuInfo().validity()) {
280 case GpgME::TofuInfo::ValidityUnknown:
281 case GpgME::TofuInfo::Conflict:
282 case GpgME::TofuInfo::NoHistory:
285 case GpgME::TofuInfo::LittleHistory:
288 case GpgME::TofuInfo::BasicHistory:
289 case GpgME::TofuInfo::LargeHistory:
294 case GpgME::UserID::Full:
297 return hasTrustedSignature(uid) ? Level4 : Level3;
299 case GpgME::UserID::Ultimate:
QString xi18nc(const char *context, const char *text, const TYPE &arg...)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void push_back(parameter_type value)
QString toLower() const const