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

Move KMail draft headers to own classes to share code.

To not implement the read and write of those headers in several places,
lets move those to one place. Additional advantage is that we can now
unittest those headers.

Also in kmail repo we need to use those classes.
parent 07d2d52a
Pipeline #83618 failed with stage
in 8 minutes and 19 seconds
cmake_minimum_required(VERSION 3.16 FATAL_ERROR)
set(PIM_VERSION "5.18.43")
set(PIM_VERSION "5.18.44")
project(Messagelib VERSION ${PIM_VERSION})
......
......@@ -69,6 +69,7 @@ target_sources( KF5MessageComposer PRIVATE
composer/keyresolver.cpp
composer/composerviewinterface.cpp
composer/composerattachmentinterface.cpp
draftstatus/draftstatus.cpp
recipient/recipientspicker.cpp
recipient/recipient.cpp
recipient/recipientline.cpp
......
......@@ -14,6 +14,7 @@
#include "composer.h"
#include "composer/keyresolver.h"
#include "composer/signaturecontroller.h"
#include "draftstatus/draftstatus.h"
#include "imagescaling/imagescalingutils.h"
#include "job/emailaddressresolvejob.h"
#include "part/globalpart.h"
......@@ -325,17 +326,11 @@ void ComposerViewBase::send(MessageComposer::MessageSender::SendMethod method, M
if (m_neverEncrypt && saveIn != MessageComposer::MessageSender::SaveInNone) {
// we can't use the state of the mail itself, to remember the
// signing and encryption state, so let's add a header instead
auto header = new KMime::Headers::Generic("X-KMail-SignatureActionEnabled");
header->fromUnicodeString(m_sign ? QStringLiteral("true") : QStringLiteral("false"), "utf-8");
m_msg->setHeader(header);
header = new KMime::Headers::Generic("X-KMail-EncryptActionEnabled");
header->fromUnicodeString(m_encrypt ? QStringLiteral("true") : QStringLiteral("false"), "utf-8");
m_msg->setHeader(header);
header = new KMime::Headers::Generic("X-KMail-CryptoMessageFormat");
header->fromUnicodeString(QString::number(m_cryptoMessageFormat), "utf-8");
m_msg->setHeader(header);
DraftSignatureState(m_msg).setState(m_sign);
DraftEncryptionState(m_msg).setState(m_sign);
DraftCryptoMessageFormatState(m_msg).setState(m_cryptoMessageFormat);
} else {
MessageComposer::Util::removeNotNecessaryHeaders(m_msg);
removeDraftCryptoHeaders(m_msg);
}
if (mSendMethod == MessageComposer::MessageSender::SendImmediate && checkMailDispatcher) {
......
/*
SPDX-FileCopyrightText: 2021 Sandro Knauß <sknauss@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "draftstatus.h"
#include <MessageCore/AutocryptUtils>
using namespace MessageComposer;
void MessageComposer::removeDraftCryptoHeaders(const KMime::Message::Ptr &msg)
{
DraftEncryptionState(msg).removeState();
DraftSignatureState(msg).removeState();
DraftCryptoMessageFormatState(msg).removeState();
}
DraftEncryptionState::DraftEncryptionState(const KMime::Message::Ptr &msg)
: mMsg(msg)
{
}
void DraftEncryptionState::setState(bool encrypt)
{
auto hdr = new KMime::Headers::Generic("X-KMail-EncryptActionEnabled");
hdr->fromUnicodeString(sign ? QStringLiteral("true") : QStringLiteral("false"), "utf-8");
mMsg->setHeader(hdr);
}
void DraftEncryptionState::removeState()
{
mMsg->removeHeader("X-KMail-EncryptActionEnabled");
}
bool DraftEncryptionState::encryptionState() const
{
if (isDefined()) {
auto hdr = mMsg->headerByType("X-KMail-EncryptActionEnabled");
return hdr->as7BitString(false).contains("true");
}
return false;
}
bool DraftEncryptionState::isDefined() const
{
return mMsg->hasHeader("X-KMail-EncryptActionEnabled");
}
DraftSignatureState::DraftSignatureState(const KMime::Message::Ptr &msg)
: mMsg(msg)
{
}
void DraftSignatureState::setState(bool sign)
{
auto hdr = new KMime::Headers::Generic("X-KMail-SignatureActionEnabled");
hdr->fromUnicodeString(sign ? QStringLiteral("true") : QStringLiteral("false"), "utf-8");
mMsg->setHeader(hdr);
}
void DraftSignatureState::removeState()
{
mMsg->removeHeader("X-KMail-SignatureActionEnabled");
}
bool DraftSignatureState::signState() const
{
if (isDefined()) {
auto hdr = mMsg->headerByType("X-KMail-SignatureActionEnabled");
return hdr->as7BitString(false).contains("true");
}
return false;
}
bool DraftSignatureState::isDefined() const
{
return mMsg->hasHeader("X-KMail-SignatureActionEnabled");
}
DraftCryptoMessageFormatState::DraftCryptoMessageFormatState(const KMime::Message::Ptr &msg)
: mMsg(msg)
{
}
void DraftCryptoMessageFormatState::setState(Kleo::CryptoMessageFormat cryptoMessageFormat)
{
auto hdr = new KMime::Headers::Generic("X-KMail-CryptoMessageFormat");
hdr->fromUnicodeString(QString::number(cryptoMessageFormat), "utf-8");
mMsg->setHeader(hdr);
}
void DraftCryptoMessageFormatState::removeState()
{
mMsg->removeHeader("X-KMail-CryptoMessageFormat");
}
Kleo::CryptoMessageFormat DraftCryptoMessageFormatState::cryptoMessageFormatState() const
{
if (isDefined()) {
auto hdr = mMsg->headerByType("X-KMail-CryptoMessageFormat");
return static_cast<Kleo::CryptoMessageFormat>(hdr->asUnicodeString().toInt());
}
return Kleo::AutoFormat;
}
bool DraftCryptoMessageFormatState::isDefined() const
{
return mMsg->hasHeader("X-KMail-CryptoMessageFormat");
}
/*
SPDX-FileCopyrightText: 2021 Sandro Knauß <sknauss@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#pragma once
#include "messagecomposer_export.h"
#include <KMime/Message>
#include <Libkleo/Enum>
namespace MessageComposer
{
class MESSAGECOMPOSER_EXPORT DraftEncryptionState
{
public:
DraftEncryptionState(const KMime::Message::Ptr &msg);
void setState(bool encrypt);
void removeState();
Q_REQUIRED_RESULT bool encryptionState() const;
Q_REQUIRED_RESULT bool isDefined() const;
private:
KMime::Message::Ptr mMsg;
};
class MESSAGECOMPOSER_EXPORT DraftSignatureState
{
public:
DraftSignatureState(const KMime::Message::Ptr &msg);
void setState(bool sign);
void removeState();
Q_REQUIRED_RESULT bool signState() const;
Q_REQUIRED_RESULT bool isDefined() const;
private:
KMime::Message::Ptr mMsg;
};
class MESSAGECOMPOSER_EXPORT DraftCryptoMessageFormatState
{
public:
DraftCryptoMessageFormatState(const KMime::Message::Ptr &msg);
void setState(Kleo::CryptoMessageFormat cryptoMessageFormat);
void removeState();
Q_REQUIRED_RESULT Kleo::CryptoMessageFormat cryptoMessageFormatState() const;
Q_REQUIRED_RESULT bool isDefined() const;
private:
KMime::Message::Ptr mMsg;
};
void MESSAGECOMPOSER_EXPORT removeDraftCryptoHeaders(const KMime::Message::Ptr &msg);
}
......@@ -11,6 +11,7 @@
#include "messagefactoryforwardjob.h"
#include "messagefactoryreplyjob.h"
#include "settings/messagecomposersettings.h"
#include "draftstatus/draftstatus.h"
#include <MessageComposer/Util>
#include <Akonadi/Item>
......@@ -108,12 +109,8 @@ void MessageFactoryNG::slotCreateReplyDone(const KMime::Message::Ptr &msg, bool
msg->setHeader(header);
}
if (auto hrd = mOrigMsg->headerByType("X-KMail-EncryptActionEnabled")) {
if (hrd->as7BitString(false).contains("true")) {
auto header = new KMime::Headers::Generic("X-KMail-EncryptActionEnabled");
header->fromUnicodeString(QStringLiteral("true"), "utf-8");
msg->setHeader(header);
}
if (DraftEncryptionState(mOrigMsg).encryptionState()) {
DraftEncryptionState(msg).setState(true);
}
msg->assemble();
......
......@@ -320,13 +320,6 @@ bool MessageComposer::Util::sendMailDispatcherIsOnline(QWidget *parent)
return false;
}
void MessageComposer::Util::removeNotNecessaryHeaders(const KMime::Message::Ptr &msg)
{
msg->removeHeader("X-KMail-SignatureActionEnabled");
msg->removeHeader("X-KMail-EncryptActionEnabled");
msg->removeHeader("X-KMail-CryptoMessageFormat");
}
KMime::Content *MessageComposer::Util::findTypeInMessage(KMime::Content *data, const QByteArray &mimeType, const QByteArray &subType)
{
if (!data->contentType()->isEmpty()) {
......
......@@ -31,7 +31,6 @@ Q_REQUIRED_RESULT MESSAGECOMPOSER_EXPORT QStringList AttachmentKeywords();
Q_REQUIRED_RESULT MESSAGECOMPOSER_EXPORT QString cleanedUpHeaderString(const QString &s);
Q_REQUIRED_RESULT MESSAGECOMPOSER_EXPORT bool sendMailDispatcherIsOnline(QWidget *parent = nullptr);
MESSAGECOMPOSER_EXPORT void removeNotNecessaryHeaders(const KMime::Message::Ptr &msg);
/**
* find mimetype in message
......
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