Commit 5a3f30b0 authored by Sandro Knauß's avatar Sandro Knauß 🐝
Browse files

Make it possible to use crypto ContactPreference outside MessageComposer.

parent dc965474
......@@ -48,7 +48,6 @@ target_sources( KF5MessageComposer PRIVATE
job/emailaddressresolvejob.cpp
job/attachmentfrompublickeyjob.cpp
job/distributionlistexpandjob.cpp
job/savecontactpreferencejob.cpp
job/attachmentvcardfromaddressbookjob.cpp
job/attachmentclipboardjob.cpp
job/protectedheadersjob.cpp
......@@ -62,6 +61,8 @@ target_sources( KF5MessageComposer PRIVATE
composer/nearexpirychecker.cpp
composer/composerviewinterface.cpp
composer/composerattachmentinterface.cpp
contactpreference/contactpreference.cpp
contactpreference/savecontactpreferencejob.cpp
draftstatus/draftstatus.cpp
recipient/recipientspicker.cpp
recipient/recipient.cpp
......@@ -139,7 +140,6 @@ target_sources( KF5MessageComposer PRIVATE
job/signencryptjob.h
job/inserttextfilejob.h
job/emailaddressresolvejob.h
job/savecontactpreferencejob.h
job/singlepartjob.h
job/attachmentjob.h
job/jobbase_p.h
......@@ -424,6 +424,15 @@ ecm_generate_headers(MessageComposer_Camelcasecomposerng_HEADERS
RELATIVE composer-ng
)
ecm_generate_headers(MessageComposer_Camelcasecontactpreference_HEADERS
HEADER_NAMES
SaveContactPreferenceJob
ContactPreference
REQUIRED_HEADERS MessageComposer_contactpreference_HEADERS
PREFIX MessageComposer
RELATIVE contactpreference
)
ecm_generate_headers(MessageComposer_Camelcasesender_HEADERS
HEADER_NAMES
AkonadiSender
......@@ -570,6 +579,7 @@ install(FILES
${MessageComposer_Camelplugineditorcheckbeforesend_HEADERS}
${MessageComposer_Camelcasecomposer_HEADERS}
${MessageComposer_Camelcasecomposerng_HEADERS}
${MessageComposer_Camelcasecontactpreference_HEADERS}
${MessageComposer_Camelcasesender_HEADERS}
${MessageComposer_Camelcaseutils_HEADERS}
${MessageComposer_Camelcasehelper_HEADERS}
......@@ -602,6 +612,7 @@ install(FILES
${CMAKE_CURRENT_BINARY_DIR}/messagecomposer_debug.h
${MessageComposer_composer_HEADERS}
${MessageComposer_composerng_HEADERS}
${MessageComposer_contactpreference_HEADERS}
${MessageComposer_sender_HEADERS}
${MessageComposer_utils_HEADERS}
${MessageComposer_helper_HEADERS}
......
......@@ -13,7 +13,7 @@
#include "composer/keyresolver.h"
#include "composer/nearexpirychecker.h"
#include "job/savecontactpreferencejob.h"
#include "contactpreference/savecontactpreferencejob.h"
#include "utils/kleo_util.h"
#include <KCursorSaver>
......@@ -605,7 +605,7 @@ struct Q_DECL_HIDDEN Kleo::KeyResolver::KeyResolverPrivate {
std::map<CryptoMessageFormat, FormatInfo> mFormatInfoMap;
// key=email address, value=crypto preferences for this contact (from kabc)
using ContactPreferencesMap = std::map<QString, ContactPreferences>;
using ContactPreferencesMap = std::map<QString, MessageComposer::ContactPreference>;
ContactPreferencesMap mContactPreferencesMap;
std::map<QByteArray, QString> mAutocryptMap;
MessageComposer::NearExpiryChecker::Ptr nearExpiryChecker;
......@@ -733,7 +733,7 @@ std::vector<Kleo::KeyResolver::Item> Kleo::KeyResolver::getEncryptionItems(const
QStringList::const_iterator end(addresses.constEnd());
for (QStringList::const_iterator it = addresses.constBegin(); it != end; ++it) {
QString addr = canonicalAddress(*it).toLower();
const ContactPreferences pref = lookupContactPreferences(addr);
const auto pref = lookupContactPreferences(addr);
items.emplace_back(*it, /*getEncryptionKeys( *it, true ),*/
pref.encryptionPreference,
......@@ -894,7 +894,7 @@ Kleo::Result Kleo::KeyResolver::resolveEncryptionKeys(bool signingRequested, boo
return Kleo::Canceled;
}
QString addr = canonicalAddress(it->address).toLower();
const ContactPreferences pref = lookupContactPreferences(addr);
const auto pref = lookupContactPreferences(addr);
it->pref = pref.encryptionPreference;
it->signPref = pref.signingPreference;
it->format = pref.cryptoMessageFormat;
......@@ -910,7 +910,7 @@ Kleo::Result Kleo::KeyResolver::resolveEncryptionKeys(bool signingRequested, boo
return Kleo::Canceled;
}
QString addr = canonicalAddress(it->address).toLower();
const ContactPreferences pref = lookupContactPreferences(addr);
const auto pref = lookupContactPreferences(addr);
it->pref = pref.encryptionPreference;
it->signPref = pref.signingPreference;
it->format = pref.cryptoMessageFormat;
......@@ -1287,7 +1287,7 @@ Kleo::Result Kleo::KeyResolver::showKeyApprovalDialog(bool &finalySendUnencrypte
if (prefsChanged) {
for (uint i = 0, total = items.size(); i < total; ++i) {
ContactPreferences pref = lookupContactPreferences(items[i].address);
auto pref = lookupContactPreferences(items[i].address);
pref.encryptionPreference = items[i].pref;
pref.pgpKeyFingerprints.clear();
pref.smimeCertFingerprints.clear();
......@@ -1629,14 +1629,14 @@ void Kleo::KeyResolver::addKeys(const std::vector<Item> &items)
dump();
}
Kleo::KeyResolver::ContactPreferences Kleo::KeyResolver::lookupContactPreferences(const QString &address) const
MessageComposer::ContactPreference Kleo::KeyResolver::lookupContactPreferences(const QString &address) const
{
const auto it = d->mContactPreferencesMap.find(address);
if (it != d->mContactPreferencesMap.end()) {
return it->second;
}
ContactPreferences pref;
MessageComposer::ContactPreference pref;
if (!d->mAkonadiLookupEnabled) {
return pref;
......@@ -1650,14 +1650,7 @@ Kleo::KeyResolver::ContactPreferences Kleo::KeyResolver::lookupContactPreference
const KContacts::Addressee::List res = job->contacts();
if (!res.isEmpty()) {
KContacts::Addressee addr = res.at(0);
QString encryptPref = addr.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("CRYPTOENCRYPTPREF"));
pref.encryptionPreference = Kleo::stringToEncryptionPreference(encryptPref);
QString signPref = addr.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("CRYPTOSIGNPREF"));
pref.signingPreference = Kleo::stringToSigningPreference(signPref);
QString cryptoFormats = addr.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("CRYPTOPROTOPREF"));
pref.cryptoMessageFormat = Kleo::stringToCryptoMessageFormat(cryptoFormats);
pref.pgpKeyFingerprints = addr.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("OPENPGPFP")).split(QLatin1Char(','), Qt::SkipEmptyParts);
pref.smimeCertFingerprints = addr.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("SMIMEFP")).split(QLatin1Char(','), Qt::SkipEmptyParts);
pref.fillFromAddressee(addr);
}
// insert into map and grab resulting iterator
d->mContactPreferencesMap.insert(std::make_pair(address, pref));
......@@ -1665,27 +1658,20 @@ Kleo::KeyResolver::ContactPreferences Kleo::KeyResolver::lookupContactPreference
return pref;
}
void Kleo::KeyResolver::saveContactPreference(const QString &email, const ContactPreferences &pref) const
void Kleo::KeyResolver::saveContactPreference(const QString &email, const MessageComposer::ContactPreference &pref) const
{
d->mContactPreferencesMap.insert(std::make_pair(email, pref));
auto saveContactPreferencesJob = new MessageComposer::SaveContactPreferenceJob(email, pref);
saveContactPreferencesJob->start();
}
Kleo::KeyResolver::ContactPreferences::ContactPreferences()
: encryptionPreference(UnknownPreference)
, signingPreference(UnknownSigningPreference)
, cryptoMessageFormat(AutoFormat)
{
}
QStringList Kleo::KeyResolver::keysForAddress(const QString &address) const
{
if (address.isEmpty()) {
return {};
}
const QString addr = canonicalAddress(address).toLower();
const ContactPreferences pref = lookupContactPreferences(addr);
const auto pref = lookupContactPreferences(addr);
return pref.pgpKeyFingerprints + pref.smimeCertFingerprints;
}
......@@ -1695,7 +1681,7 @@ void Kleo::KeyResolver::setKeysForAddress(const QString &address, const QStringL
return;
}
const QString addr = canonicalAddress(address).toLower();
ContactPreferences pref = lookupContactPreferences(addr);
auto pref = lookupContactPreferences(addr);
pref.pgpKeyFingerprints = pgpKeyFingerprints;
pref.smimeCertFingerprints = smimeCertFingerprints;
saveContactPreference(addr, pref);
......
......@@ -26,6 +26,7 @@
namespace MessageComposer
{
class ContactPreference;
class NearExpiryChecker;
using NearExpiryCheckerPtr = QSharedPointer<NearExpiryChecker>;
}
......@@ -117,15 +118,6 @@ public:
~KeyResolver();
struct ContactPreferences {
ContactPreferences();
Kleo::EncryptionPreference encryptionPreference;
Kleo::SigningPreference signingPreference;
Kleo::CryptoMessageFormat cryptoMessageFormat;
QStringList pgpKeyFingerprints;
QStringList smimeCertFingerprints;
};
struct Item : public KeyApprovalDialog::Item {
Item()
: KeyApprovalDialog::Item()
......@@ -270,8 +262,8 @@ private:
bool encryptToSelf() const;
bool showApprovalDialog() const;
ContactPreferences lookupContactPreferences(const QString &address) const;
void saveContactPreference(const QString &email, const ContactPreferences &pref) const;
MessageComposer::ContactPreference lookupContactPreferences(const QString &address) const;
void saveContactPreference(const QString &email, const MessageComposer::ContactPreference &pref) const;
private:
class EncryptionPreferenceCounter;
......
/*
SPDX-FileCopyrightText: 2022 Sandro Knauß <sknauss@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "contactpreference.h"
#include <KContacts/Addressee>
using namespace MessageComposer;
ContactPreference::ContactPreference()
: encryptionPreference(Kleo::UnknownPreference)
, signingPreference(Kleo::UnknownSigningPreference)
, cryptoMessageFormat(Kleo::AutoFormat)
{
}
void ContactPreference::fillFromAddressee(const KContacts::Addressee& contact)
{
QString encryptPref = contact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("CRYPTOENCRYPTPREF"));
encryptionPreference = Kleo::stringToEncryptionPreference(encryptPref);
QString signPref = contact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("CRYPTOSIGNPREF"));
signingPreference = Kleo::stringToSigningPreference(signPref);
QString cryptoFormats = contact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("CRYPTOPROTOPREF"));
cryptoMessageFormat = Kleo::stringToCryptoMessageFormat(cryptoFormats);
pgpKeyFingerprints = contact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("OPENPGPFP")).split(QLatin1Char(','), Qt::SkipEmptyParts);
smimeCertFingerprints = contact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("SMIMEFP")).split(QLatin1Char(','), Qt::SkipEmptyParts);
}
void ContactPreference::fillAddressee(KContacts::Addressee& contact) const
{
contact.insertCustom(QStringLiteral("KADDRESSBOOK"),
QStringLiteral("CRYPTOENCRYPTPREF"),
QLatin1String(Kleo::encryptionPreferenceToString(encryptionPreference)));
contact.insertCustom(QStringLiteral("KADDRESSBOOK"),
QStringLiteral("CRYPTOSIGNPREF"),
QLatin1String(Kleo::signingPreferenceToString(signingPreference)));
contact.insertCustom(QStringLiteral("KADDRESSBOOK"),
QStringLiteral("CRYPTOPROTOPREF"),
QLatin1String(cryptoMessageFormatToString(cryptoMessageFormat)));
contact.insertCustom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("OPENPGPFP"), pgpKeyFingerprints.join(QLatin1Char(',')));
contact.insertCustom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("SMIMEFP"), smimeCertFingerprints.join(QLatin1Char(',')));
}
/*
SPDX-FileCopyrightText: 2022 Sandro Knauß <sknauss@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#pragma once
#include <Libkleo/Enum>
#include "messagecomposer_export.h"
namespace KContacts
{
class Addressee;
}
namespace MessageComposer
{
class MESSAGECOMPOSER_EXPORT ContactPreference {
public:
ContactPreference();
void fillFromAddressee(const KContacts::Addressee &contact);
void fillAddressee(KContacts::Addressee &contact) const;
Kleo::EncryptionPreference encryptionPreference;
Kleo::SigningPreference signingPreference;
Kleo::CryptoMessageFormat cryptoMessageFormat;
QStringList pgpKeyFingerprints;
QStringList smimeCertFingerprints;
};
}
......@@ -21,10 +21,26 @@
using namespace MessageComposer;
SaveContactPreferenceJob::SaveContactPreferenceJob(const QString &email, const Kleo::KeyResolver::ContactPreferences &pref, QObject *parent)
class MessageComposer::SaveContactPreferenceJobPrivate
{
public:
explicit SaveContactPreferenceJobPrivate(SaveContactPreferenceJob *qq, const QString &email, const ContactPreference &pref)
: q_ptr(qq)
, mEmail(email)
, mPref(pref)
{
}
SaveContactPreferenceJob *q_ptr;
Q_DECLARE_PUBLIC(SaveContactPreferenceJob)
const QString mEmail;
const ContactPreference mPref;
};
SaveContactPreferenceJob::SaveContactPreferenceJob(const QString &email, const ContactPreference &pref, QObject *parent)
: QObject(parent)
, mEmail(email)
, mPref(pref)
, d_ptr(std::unique_ptr<SaveContactPreferenceJobPrivate>(new SaveContactPreferenceJobPrivate(this, email, pref)))
{
}
......@@ -32,15 +48,18 @@ SaveContactPreferenceJob::~SaveContactPreferenceJob() = default;
void SaveContactPreferenceJob::start()
{
Q_D(SaveContactPreferenceJob);
auto job = new Akonadi::ContactSearchJob(this);
connect(job, &Akonadi::ContactSearchJob::result, this, &SaveContactPreferenceJob::slotSearchContact);
job->setLimit(1);
job->setQuery(Akonadi::ContactSearchJob::Email, mEmail);
job->setQuery(Akonadi::ContactSearchJob::Email, d->mEmail);
job->start();
}
void SaveContactPreferenceJob::slotSearchContact(KJob *job)
{
Q_D(SaveContactPreferenceJob);
auto contactSearchJob = qobject_cast<Akonadi::ContactSearchJob *>(job);
const Akonadi::Item::List items = contactSearchJob->items();
......@@ -49,7 +68,7 @@ void SaveContactPreferenceJob::slotSearchContact(KJob *job)
bool ok = true;
const QString fullName = QInputDialog::getText(nullptr,
i18n("Name Selection"),
i18n("Which name shall the contact '%1' have in your address book?", mEmail),
i18n("Which name shall the contact '%1' have in your address book?", d->mEmail),
QLineEdit::Normal,
QString(),
&ok);
......@@ -73,10 +92,10 @@ void SaveContactPreferenceJob::slotSearchContact(KJob *job)
KContacts::Addressee contact;
contact.setNameFromString(fullName);
KContacts::Email email(mEmail);
KContacts::Email email(d->mEmail);
email.setPreferred(true);
contact.addEmail(email);
writeCustomContactProperties(contact, mPref);
d->mPref.fillAddressee(contact);
Akonadi::Item item(KContacts::Addressee::mimeType());
item.setPayload<KContacts::Addressee>(contact);
......@@ -87,7 +106,7 @@ void SaveContactPreferenceJob::slotSearchContact(KJob *job)
Akonadi::Item item = items.first();
auto contact = item.payload<KContacts::Addressee>();
writeCustomContactProperties(contact, mPref);
d->mPref.fillAddressee(contact);
item.setPayload<KContacts::Addressee>(contact);
......@@ -96,20 +115,6 @@ void SaveContactPreferenceJob::slotSearchContact(KJob *job)
}
}
void SaveContactPreferenceJob::writeCustomContactProperties(KContacts::Addressee &contact, const Kleo::KeyResolver::ContactPreferences &pref) const
{
contact.insertCustom(QStringLiteral("KADDRESSBOOK"),
QStringLiteral("CRYPTOENCRYPTPREF"),
QLatin1String(Kleo::encryptionPreferenceToString(pref.encryptionPreference)));
contact.insertCustom(QStringLiteral("KADDRESSBOOK"),
QStringLiteral("CRYPTOSIGNPREF"),
QLatin1String(Kleo::signingPreferenceToString(pref.signingPreference)));
contact.insertCustom(QStringLiteral("KADDRESSBOOK"),
QStringLiteral("CRYPTOPROTOPREF"),
QLatin1String(cryptoMessageFormatToString(pref.cryptoMessageFormat)));
contact.insertCustom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("OPENPGPFP"), pref.pgpKeyFingerprints.join(QLatin1Char(',')));
contact.insertCustom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("SMIMEFP"), pref.smimeCertFingerprints.join(QLatin1Char(',')));
}
void SaveContactPreferenceJob::slotModifyCreateItem(KJob *job)
{
......
......@@ -6,25 +6,33 @@
#pragma once
#include "composer/keyresolver.h"
#include <KContacts/Addressee>
#include <MessageComposer/ContactPreference>
#include <QObject>
#include <memory>
#include "messagecomposer_export.h"
class KJob;
namespace MessageComposer
{
class SaveContactPreferenceJob : public QObject
class SaveContactPreferenceJobPrivate;
class MESSAGECOMPOSER_EXPORT SaveContactPreferenceJob : public QObject
{
Q_OBJECT
public:
explicit SaveContactPreferenceJob(const QString &email, const Kleo::KeyResolver::ContactPreferences &pref, QObject *parent = nullptr);
explicit SaveContactPreferenceJob(const QString &email, const ContactPreference &pref, QObject *parent = nullptr);
~SaveContactPreferenceJob() override;
void start();
private:
void slotSearchContact(KJob *job);
void slotModifyCreateItem(KJob *job);
void writeCustomContactProperties(KContacts::Addressee &contact, const Kleo::KeyResolver::ContactPreferences &pref) const;
const QString mEmail;
const Kleo::KeyResolver::ContactPreferences mPref;
std::unique_ptr<SaveContactPreferenceJobPrivate> d_ptr;
Q_DECLARE_PRIVATE(SaveContactPreferenceJob)
};
}
Supports Markdown
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