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

Move all method implementations to the private class

The implementations only use variables and methods of the private class.

GnuPG-bug-id: 5283
parent 6ec029a0
......@@ -67,11 +67,15 @@ public:
bool isAcceptableSigningKey(const Key &key);
bool isAcceptableEncryptionKey(const Key &key, const QString &address = QString());
void setSender(const QString &address);
void addRecipients(const QStringList &addresses);
void setOverrideKeys(const QMap<Protocol, QMap<QString, QStringList>> &overrides);
void resolveOverrides();
void resolveSign(Protocol proto);
void setSigningKeys(const QStringList &fingerprints);
void resolveEnc(Protocol proto);
QStringList unresolvedRecipients(GpgME::Protocol protocol) const;
bool resolve();
KeyResolverCore *const q;
QString mSender;
......@@ -134,6 +138,22 @@ bool KeyResolverCore::Private::isAcceptableEncryptionKey(const Key &key, const Q
return false;
}
void KeyResolverCore::Private::setSender(const QString &address)
{
const auto normalized = UserID::addrSpecFromString (address.toUtf8().constData());
if (normalized.empty()) {
// should not happen bug in the caller, non localized
// error for bug reporting.
mFatalErrors << QStringLiteral("The sender address '%1' could not be extracted").arg(address);
return;
}
const auto normStr = QString::fromUtf8(normalized.c_str());
if (mSign) {
mSender = normStr;
}
addRecipients({address});
}
void KeyResolverCore::Private::addRecipients(const QStringList &addresses)
{
if (!mEncrypt) {
......@@ -161,6 +181,20 @@ void KeyResolverCore::Private::addRecipients(const QStringList &addresses)
}
}
void KeyResolverCore::Private::setOverrideKeys(const QMap<Protocol, QMap<QString, QStringList>> &overrides)
{
QMap<QString, QStringList> normalizedOverrides;
for (const auto fmt: overrides.keys()) {
for (const auto &addr: overrides[fmt].keys()) {
const auto normalized = QString::fromUtf8(
UserID::addrSpecFromString (addr.toUtf8().constData()).c_str());
const auto fingerprints = overrides[fmt][addr];
normalizedOverrides.insert(addr, fingerprints);
}
mOverrides.insert(fmt, normalizedOverrides);
}
}
// Apply the overrides this is also where specific formats come in
void KeyResolverCore::Private::resolveOverrides()
{
......@@ -303,93 +337,44 @@ void KeyResolverCore::Private::resolveEnc(Protocol proto)
mEncKeys.insert(proto, encMap);
}
KeyResolverCore::KeyResolverCore(bool encrypt, bool sign, Protocol fmt)
: d(new Private(this, encrypt, sign, fmt))
QStringList KeyResolverCore::Private::unresolvedRecipients(GpgME::Protocol protocol) const
{
}
KeyResolverCore::~KeyResolverCore() = default;
void KeyResolverCore::setSender(const QString &address)
{
const auto normalized = UserID::addrSpecFromString (address.toUtf8().constData());
if (normalized.empty()) {
// should not happen bug in the caller, non localized
// error for bug reporting.
d->mFatalErrors << QStringLiteral("The sender address '%1' could not be extracted").arg(address);
return;
}
const auto normStr = QString::fromUtf8(normalized.c_str());
if (d->mSign) {
d->mSender = normStr;
}
d->addRecipients({address});
}
QString KeyResolverCore::normalizedSender() const
{
return d->mSender;
}
void KeyResolverCore::setRecipients(const QStringList &addresses)
{
d->addRecipients(addresses);
}
void KeyResolverCore::setSigningKeys(const QStringList &fingerprints)
{
d->setSigningKeys(fingerprints);
}
void KeyResolverCore::setOverrideKeys(const QMap<Protocol, QMap<QString, QStringList> > &overrides)
{
QMap<QString, QStringList> normalizedOverrides;
for (const auto fmt: overrides.keys()) {
for (const auto &addr: overrides[fmt].keys()) {
const auto normalized = QString::fromUtf8(
UserID::addrSpecFromString (addr.toUtf8().constData()).c_str());
const auto fingerprints = overrides[fmt][addr];
normalizedOverrides.insert(addr, fingerprints);
QStringList result;
const auto encMap = mEncKeys.value(protocol);
result.reserve(encMap.size());
for (auto it = encMap.cbegin(); it != encMap.cend(); ++it) {
if (it.value().empty()) {
result.push_back(it.key());
}
d->mOverrides.insert(fmt, normalizedOverrides);
}
return result;
}
void KeyResolverCore::setPreferredProtocol(Protocol proto)
{
d->mPreferredProtocol = proto;
}
void KeyResolverCore::setMinimumValidity(int validity)
{
d->mMinimumValidity = validity;
}
bool KeyResolverCore::resolve()
bool KeyResolverCore::Private::resolve()
{
qCDebug(LIBKLEO_LOG) << "Starting ";
if (!d->mSign && !d->mEncrypt) {
if (!mSign && !mEncrypt) {
// nothing to do
return true;
}
// First resolve through overrides
d->resolveOverrides();
resolveOverrides();
// Then look for signing / encryption keys
if (d->mFormat != CMS) {
d->resolveSign(OpenPGP);
d->resolveEnc(OpenPGP);
if (mFormat != CMS) {
resolveSign(OpenPGP);
resolveEnc(OpenPGP);
}
const QStringList unresolvedPGP = unresolvedRecipients(OpenPGP);
bool pgpOnly = unresolvedPGP.empty() && (!d->mSign || d->mSigKeys.contains(OpenPGP));
bool pgpOnly = unresolvedPGP.empty() && (!mSign || mSigKeys.contains(OpenPGP));
if (d->mFormat != OpenPGP) {
d->resolveSign(CMS);
d->resolveEnc(CMS);
if (mFormat != OpenPGP) {
resolveSign(CMS);
resolveEnc(CMS);
}
const QStringList unresolvedCMS = unresolvedRecipients(CMS);
bool cmsOnly = unresolvedCMS.empty() && (!d->mSign || d->mSigKeys.contains(CMS));
bool cmsOnly = unresolvedCMS.empty() && (!mSign || mSigKeys.contains(CMS));
// Check if we need the user to select different keys.
bool needsUser = false;
......@@ -401,30 +386,30 @@ bool KeyResolverCore::resolve()
break;
}
}
if (d->mSign) {
if (mSign) {
// So every recipient could be resolved through
// a combination of PGP and S/MIME do we also
// have signing keys for both?
needsUser |= !(d->mSigKeys.contains(OpenPGP) &&
d->mSigKeys.contains(CMS));
needsUser |= !(mSigKeys.contains(OpenPGP) &&
mSigKeys.contains(CMS));
}
}
if (!needsUser) {
if (pgpOnly && cmsOnly) {
if (d->mPreferredProtocol == CMS) {
d->mSigKeys.remove(OpenPGP);
d->mEncKeys.remove(OpenPGP);
if (mPreferredProtocol == CMS) {
mSigKeys.remove(OpenPGP);
mEncKeys.remove(OpenPGP);
} else {
d->mSigKeys.remove(CMS);
d->mEncKeys.remove(CMS);
mSigKeys.remove(CMS);
mEncKeys.remove(CMS);
}
} else if (pgpOnly) {
d->mSigKeys.remove(CMS);
d->mEncKeys.remove(CMS);
mSigKeys.remove(CMS);
mEncKeys.remove(CMS);
} else if (cmsOnly) {
d->mSigKeys.remove(OpenPGP);
d->mEncKeys.remove(OpenPGP);
mSigKeys.remove(OpenPGP);
mEncKeys.remove(OpenPGP);
}
qCDebug(LIBKLEO_LOG) << "Automatic key resolution done.";
......@@ -434,6 +419,53 @@ bool KeyResolverCore::resolve()
return false;
}
KeyResolverCore::KeyResolverCore(bool encrypt, bool sign, Protocol fmt)
: d(new Private(this, encrypt, sign, fmt))
{
}
KeyResolverCore::~KeyResolverCore() = default;
void KeyResolverCore::setSender(const QString &address)
{
d->setSender(address);
}
QString KeyResolverCore::normalizedSender() const
{
return d->mSender;
}
void KeyResolverCore::setRecipients(const QStringList &addresses)
{
d->addRecipients(addresses);
}
void KeyResolverCore::setSigningKeys(const QStringList &fingerprints)
{
d->setSigningKeys(fingerprints);
}
void KeyResolverCore::setOverrideKeys(const QMap<Protocol, QMap<QString, QStringList>> &overrides)
{
d->setOverrideKeys(overrides);
}
void KeyResolverCore::setPreferredProtocol(Protocol proto)
{
d->mPreferredProtocol = proto;
}
void KeyResolverCore::setMinimumValidity(int validity)
{
d->mMinimumValidity = validity;
}
bool KeyResolverCore::resolve()
{
return d->resolve();
}
QMap <Protocol, std::vector<Key> > KeyResolverCore::signingKeys() const
{
return d->mSigKeys;
......@@ -446,13 +478,5 @@ QMap <Protocol, QMap<QString, std::vector<Key> > > KeyResolverCore::encryptionKe
QStringList KeyResolverCore::unresolvedRecipients(GpgME::Protocol protocol) const
{
QStringList result;
const auto encMap = d->mEncKeys.value(protocol);
result.reserve(encMap.size());
for (auto it = encMap.cbegin(); it != encMap.cend(); ++it) {
if (it.value().empty()) {
result.push_back(it.key());
}
}
return result;
return d->unresolvedRecipients(protocol);
}
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