Commit 1b9f9eb1 authored by Ingo Klöcker's avatar Ingo Klöcker
Browse files

Replace bool arguments needSign and needEncrypt with KeyUsage enum

GnuPG-bug-id: 5283
parent 01a950ff
......@@ -73,11 +73,6 @@ enum Action {
IgnoreKey,
};
enum class KeyUsage {
AnyUsage,
Sign,
Encrypt,
};
auto mapValidity(GpgME::UserID::Validity validity)
{
......
......@@ -22,6 +22,14 @@ class UserID;
namespace Kleo
{
enum class KeyUsage : char {
AnyUsage,
Sign,
Encrypt,
Certify,
Authenticate,
};
enum CryptoMessageFormat {
InlineOpenPGPFormat = 1,
OpenPGPMIMEFormat = 2,
......
......@@ -295,8 +295,7 @@ void KeyResolverCore::Private::resolveSign(Protocol proto)
// Explicitly set
return;
}
const auto keys = mCache->findBestByMailBox(mSender.toUtf8().constData(),
proto, true, false);
const auto keys = mCache->findBestByMailBox(mSender.toUtf8().constData(), proto, KeyUsage::Sign);
for (const auto &key: keys) {
if (key.isNull()) {
continue;
......@@ -331,7 +330,7 @@ void KeyResolverCore::Private::setSigningKeys(const QStringList &fingerprints)
std::vector<Key> KeyResolverCore::Private::resolveRecipient(const QString &address, Protocol protocol)
{
const auto keys = mCache->findBestByMailBox(address.toUtf8().constData(), protocol, false, true);
const auto keys = mCache->findBestByMailBox(address.toUtf8().constData(), protocol, KeyUsage::Encrypt);
if (keys.empty() || keys[0].isNull()) {
qCDebug(LIBKLEO_LOG) << "Failed to find any" << Formatting::displayName(protocol) << "key for: " << address;
return {};
......
......@@ -13,6 +13,7 @@
#include "keycache.h"
#include "keycache_p.h"
#include "kleo/enum.h"
#include "kleo/keygroup.h"
#include "kleo/predicates.h"
#include "kleo/stl_util.h"
......@@ -1634,17 +1635,17 @@ static bool subkeyIsOk(const Subkey &s)
namespace
{
time_t creationTimeOfNewestSuitableSubKey(const Key &key, bool needSign, bool needEncrypt)
time_t creationTimeOfNewestSuitableSubKey(const Key &key, KeyUsage usage)
{
time_t creationTime = 0;
for (const Subkey &s: key.subkeys()) {
if (!subkeyIsOk(s)) {
continue;
}
if (needSign && !s.canSign()) {
if (usage == KeyUsage::Sign && !s.canSign()) {
continue;
}
if (needEncrypt && !s.canEncrypt()) {
if (usage == KeyUsage::Encrypt && !s.canEncrypt()) {
continue;
}
if (s.creationTime() > creationTime) {
......@@ -1662,8 +1663,7 @@ struct BestMatch
};
}
std::vector<GpgME::Key> KeyCache::findBestByMailBox(const char *addr, GpgME::Protocol proto,
bool needSign, bool needEncrypt) const
std::vector<GpgME::Key> KeyCache::findBestByMailBox(const char *addr, GpgME::Protocol proto, KeyUsage usage) const
{
d->ensureCachePopulated();
std::vector<GpgME::Key> ret;
......@@ -1689,13 +1689,13 @@ std::vector<GpgME::Key> KeyCache::findBestByMailBox(const char *addr, GpgME::Pro
if (proto != Protocol::UnknownProtocol && k.protocol() != proto) {
continue;
}
if (needEncrypt && !k.canEncrypt()) {
if (usage == KeyUsage::Encrypt && !k.canEncrypt()) {
continue;
}
if (needSign && (!k.canSign() || !k.hasSecret())) {
if (usage == KeyUsage::Sign && (!k.canSign() || !k.hasSecret())) {
continue;
}
const time_t creationTime = creationTimeOfNewestSuitableSubKey(k, needSign, needEncrypt);
const time_t creationTime = creationTimeOfNewestSuitableSubKey(k, usage);
if (creationTime == 0) {
// key does not have a suitable (and usable) subkey
continue;
......
......@@ -41,6 +41,7 @@ namespace Kleo
class FileSystemWatcher;
class KeyGroup;
enum class KeyUsage : char;
class KLEO_EXPORT KeyCache : public QObject
{
......@@ -104,8 +105,7 @@ public:
* keys for a single mailbox. Only supported for OpenPGP currently.
*
* @returns only the "best" key for the mailbox, or a list if it is a group. */
std::vector<GpgME::Key> findBestByMailBox(const char *addr, GpgME::Protocol proto,
bool needSign, bool needEncrypt) const;
std::vector<GpgME::Key> findBestByMailBox(const char *addr, GpgME::Protocol proto, KeyUsage usage) const;
const GpgME::Key &findByShortKeyID(const char *id) const;
const GpgME::Key &findByShortKeyID(const std::string &id) const;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment