Commit f140496f authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Don't create header when it's not necessary

parent f0645324
Pipeline #25535 passed with stage
in 47 minutes and 26 seconds
......@@ -164,9 +164,9 @@ void setAutomaticFields(const KMime::Message::Ptr &msg, bool aIsMulti)
if (aIsMulti || msg->contents().size() > 1) {
// Set the type to 'Multipart' and the subtype to 'Mixed'
msg->contentType()->setMimeType("multipart/mixed");
msg->contentType(false)->setMimeType("multipart/mixed");
// Create a random printable string and set it as the boundary parameter
msg->contentType()->setBoundary(KMime::multiPartBoundary());
msg->contentType(false)->setBoundary(KMime::multiPartBoundary());
}
}
}
......@@ -89,9 +89,10 @@ void ProtectedHeadersJob::doStart()
if (d->obvoscate && subject) {
// Create protected header lagacy mimepart with replaced headers
SinglepartJob *cjob = new SinglepartJob;
cjob->contentType()->setMimeType("text/plain");
cjob->contentType()->setCharset(subject->rfc2047Charset());
cjob->contentType()->setParameter(QStringLiteral("protected-headers"), QStringLiteral("v1"));
auto ct = cjob->contentType();
ct->setMimeType("text/plain");
ct->setCharset(subject->rfc2047Charset());
ct->setParameter(QStringLiteral("protected-headers"), QStringLiteral("v1"));
cjob->contentDisposition()->setDisposition(KMime::Headers::contentDisposition::CDinline);
cjob->setData(subject->type() + QByteArray(": ") + subject->asUnicodeString().toUtf8());
......
......@@ -138,60 +138,69 @@ void MessageComposer::Util::makeToplevelContentType(KMime::Content *content, Kle
switch (format) {
default:
case Kleo::InlineOpenPGPFormat:
case Kleo::OpenPGPMIMEFormat:
case Kleo::OpenPGPMIMEFormat: {
auto ct = content->contentType(); //Create
if (sign) {
content->contentType()->setMimeType(QByteArrayLiteral("multipart/signed"));
content->contentType()->setParameter(QStringLiteral("protocol"), QStringLiteral("application/pgp-signature"));
content->contentType()->setParameter(QStringLiteral("micalg"), QString::fromLatin1(QByteArray(QByteArrayLiteral("pgp-") + hashAlgo)).toLower());
ct->setMimeType(QByteArrayLiteral("multipart/signed"));
ct->setParameter(QStringLiteral("protocol"), QStringLiteral("application/pgp-signature"));
ct->setParameter(QStringLiteral("micalg"), QString::fromLatin1(QByteArray(QByteArrayLiteral("pgp-") + hashAlgo)).toLower());
} else {
content->contentType()->setMimeType(QByteArrayLiteral("multipart/encrypted"));
content->contentType()->setParameter(QStringLiteral("protocol"), QStringLiteral("application/pgp-encrypted"));
ct->setMimeType(QByteArrayLiteral("multipart/encrypted"));
ct->setParameter(QStringLiteral("protocol"), QStringLiteral("application/pgp-encrypted"));
}
}
return;
case Kleo::SMIMEFormat:
case Kleo::SMIMEFormat: {
if (sign) {
auto ct = content->contentType(); //Create
qCDebug(MESSAGECOMPOSER_LOG) << "setting headers for SMIME";
content->contentType()->setMimeType(QByteArrayLiteral("multipart/signed"));
content->contentType()->setParameter(QStringLiteral("protocol"), QStringLiteral("application/pkcs7-signature"));
content->contentType()->setParameter(QStringLiteral("micalg"), QString::fromLatin1(hashAlgo).toLower());
ct->setMimeType(QByteArrayLiteral("multipart/signed"));
ct->setParameter(QStringLiteral("protocol"), QStringLiteral("application/pkcs7-signature"));
ct->setParameter(QStringLiteral("micalg"), QString::fromLatin1(hashAlgo).toLower());
return;
}
// fall through (for encryption, there's no difference between
// SMIME and SMIMEOpaque, since there is no mp/encrypted for
// S/MIME)
}
Q_FALLTHROUGH();
case Kleo::SMIMEOpaqueFormat:
qCDebug(MESSAGECOMPOSER_LOG) << "setting headers for SMIME/opaque";
content->contentType()->setMimeType(QByteArrayLiteral("application/pkcs7-mime"));
auto ct = content->contentType(); //Create
ct->setMimeType(QByteArrayLiteral("application/pkcs7-mime"));
if (sign) {
content->contentType()->setParameter(QStringLiteral("smime-type"), QStringLiteral("signed-data"));
ct->setParameter(QStringLiteral("smime-type"), QStringLiteral("signed-data"));
} else {
content->contentType()->setParameter(QStringLiteral("smime-type"), QStringLiteral("enveloped-data"));
ct->setParameter(QStringLiteral("smime-type"), QStringLiteral("enveloped-data"));
}
content->contentType()->setParameter(QStringLiteral("name"), QStringLiteral("smime.p7m"));
ct->setParameter(QStringLiteral("name"), QStringLiteral("smime.p7m"));
}
}
void MessageComposer::Util::setNestedContentType(KMime::Content *content, Kleo::CryptoMessageFormat format, bool sign)
{
switch (format) {
case Kleo::OpenPGPMIMEFormat:
case Kleo::OpenPGPMIMEFormat: {
auto ct = content->contentType(); //Create
if (sign) {
content->contentType()->setMimeType(QByteArrayLiteral("application/pgp-signature"));
content->contentType()->setParameter(QStringLiteral("name"), QStringLiteral("signature.asc"));
ct->setMimeType(QByteArrayLiteral("application/pgp-signature"));
ct->setParameter(QStringLiteral("name"), QStringLiteral("signature.asc"));
content->contentDescription()->from7BitString("This is a digitally signed message part.");
} else {
content->contentType()->setMimeType(QByteArrayLiteral("application/octet-stream"));
ct->setMimeType(QByteArrayLiteral("application/octet-stream"));
}
}
return;
case Kleo::SMIMEFormat:
case Kleo::SMIMEFormat: {
if (sign) {
content->contentType()->setMimeType(QByteArrayLiteral("application/pkcs7-signature"));
content->contentType()->setParameter(QStringLiteral("name"), QStringLiteral("smime.p7s"));
auto ct = content->contentType(); //Create
ct->setMimeType(QByteArrayLiteral("application/pkcs7-signature"));
ct->setParameter(QStringLiteral("name"), QStringLiteral("smime.p7s"));
return;
}
}
Q_FALLTHROUGH();
// fall through:
default:
......@@ -203,12 +212,13 @@ void MessageComposer::Util::setNestedContentType(KMime::Content *content, Kleo::
void MessageComposer::Util::setNestedContentDisposition(KMime::Content *content, Kleo::CryptoMessageFormat format, bool sign)
{
auto ct = content->contentDisposition();
if (!sign && format & Kleo::OpenPGPMIMEFormat) {
content->contentDisposition()->setDisposition(KMime::Headers::CDinline);
content->contentDisposition()->setFilename(QStringLiteral("msg.asc"));
ct->setDisposition(KMime::Headers::CDinline);
ct->setFilename(QStringLiteral("msg.asc"));
} else if (sign && format & Kleo::SMIMEFormat) {
content->contentDisposition()->setDisposition(KMime::Headers::CDattachment);
content->contentDisposition()->setFilename(QStringLiteral("smime.p7s"));
ct->setDisposition(KMime::Headers::CDattachment);
ct->setFilename(QStringLiteral("smime.p7s"));
}
}
......
......@@ -66,22 +66,22 @@ void AttachmentFromMimeContentJob::doStart()
part->setData(content->decodedContent());
// Get the details from the MIME headers.
if (content->contentType(false)) {
part->setMimeType(content->contentType()->mimeType());
part->setName(content->contentType()->name());
if (auto ct = content->contentType(false)) {
part->setMimeType(ct->mimeType());
part->setName(ct->name());
}
if (content->contentTransferEncoding(false)) {
part->setEncoding(content->contentTransferEncoding()->encoding());
if (auto ct = content->contentTransferEncoding(false)) {
part->setEncoding(ct->encoding());
}
if (content->contentDisposition(false)) {
part->setFileName(content->contentDisposition()->filename());
part->setInline(content->contentDisposition()->disposition() == KMime::Headers::CDinline);
if (auto ct = content->contentDisposition(false)) {
part->setFileName(ct->filename());
part->setInline(ct->disposition() == KMime::Headers::CDinline);
}
if (content->contentDescription(false)) {
part->setDescription(content->contentDescription()->asUnicodeString());
if (auto ct = content->contentDescription(false)) {
part->setDescription(ct->asUnicodeString());
}
setAttachmentPart(part);
......
......@@ -44,11 +44,12 @@ static bool isInExclusionList(KMime::Content *node)
return true;
}
if (node->contentType()->mediaType() != "image") {
auto ct = node->contentType(); //Create if necessary
if (ct->mediaType() != "image") {
return true;
}
if (node->contentType()->isMultipart()) {
if (ct->isMultipart()) {
return true;
}
......
......@@ -917,9 +917,9 @@ MimeTreeParser::IconType DefaultRendererPrivate::displayHint(const MimeTreeParse
bool isHtmlPreferred = (preferredMode == MimeTreeParser::Util::Html) || (preferredMode == MimeTreeParser::Util::MultipartHtml);
QByteArray mediaType("text");
if (content->contentType(false) && !content->contentType()->mediaType().isEmpty()
&& !content->contentType()->subType().isEmpty()) {
mediaType = content->contentType()->mediaType();
if (content->contentType(false) && !content->contentType(false)->mediaType().isEmpty()
&& !content->contentType(false)->subType().isEmpty()) {
mediaType = content->contentType(false)->mediaType();
}
const bool isTextPart = (mediaType == QByteArrayLiteral("text"));
......
......@@ -76,8 +76,8 @@ public:
QString mimeTypeForContent(KMime::Content *content)
{
if (content->contentType(false)) {
return QString::fromLatin1(content->contentType()->mimeType());
if (auto ct = content->contentType(false)) {
return QString::fromLatin1(ct->mimeType());
}
return QString();
}
......@@ -106,13 +106,12 @@ public:
QIcon iconForContent(KMime::Content *content)
{
if (content->contentType(false)) {
if (auto ct = content->contentType(false)) {
auto iconName
= MimeTreeParser::Util::iconNameForMimetype(QLatin1String(content->contentType()->
mimeType()));
= MimeTreeParser::Util::iconNameForMimetype(QLatin1String(ct->mimeType()));
auto mimeType
= m_mimeDb.mimeTypeForName(QString::fromLatin1(content->contentType()->mimeType()));
= m_mimeDb.mimeTypeForName(QString::fromLatin1(ct->mimeType()));
if (!mimeType.isValid()
|| mimeType.name() == QLatin1String("application/octet-stream")) {
const QString name = descriptionForContent(content);
......
......@@ -283,11 +283,11 @@ void ViewerPrivate::openAttachment(KMime::Content *node, const QUrl &url)
return;
}
if (node->contentType(false)) {
if (node->contentType()->mimeType() == "text/x-moz-deleted") {
if (auto ct = node->contentType(false)) {
if (ct->mimeType() == "text/x-moz-deleted") {
return;
}
if (node->contentType()->mimeType() == "message/external-body") {
if (ct->mimeType() == "message/external-body") {
if (node->contentType()->hasParameter(QStringLiteral("url"))) {
KIO::OpenUrlJob *job = new KIO::OpenUrlJob(url, QStringLiteral("text/html"));
job->setUiDelegate(new KIO::JobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, q));
......@@ -401,9 +401,9 @@ bool ViewerPrivate::deleteAttachment(KMime::Content *node, bool showWarning)
filename = node->contentDisposition()->filename();
}
if (node->contentType(false)) {
name = node->contentType()->name();
mimetype = node->contentType()->mimeType();
if (auto ct = node->contentType(false)) {
name = ct->name();
mimetype = ct->mimeType();
}
// text/plain part:
......@@ -1777,8 +1777,8 @@ void ViewerPrivate::showContextMenu(KMime::Content *content, const QPoint &pos)
return;
}
if (content->contentType(false)) {
if (content->contentType()->mimeType() == "text/x-moz-deleted") {
if (auto ct = content->contentType(false)) {
if (ct->mimeType() == "text/x-moz-deleted") {
return;
}
}
......
......@@ -68,7 +68,7 @@ MessagePart::Ptr ApplicationPkcs7MimeBodyPartFormatter::process(Interface::BodyP
return {};
}
const QString smimeType = node->contentType()->parameter(QStringLiteral("smime-type")).toLower();
const QString smimeType = node->contentType(false)->parameter(QStringLiteral("smime-type")).toLower();
if (smimeType == QLatin1String("certs-only")) {
part.processResult()->setNeverDisplayInline(true);
......
......@@ -353,7 +353,7 @@ bool NodeHelper::isInEncapsulatedMessage(KMime::Content *node)
const KMime::Content *cur = node;
while (cur && cur != topLevel) {
const bool parentIsMessage = cur->parent() && cur->parent()->contentType(false)
&& cur->parent()->contentType()->mimeType().toLower() == "message/rfc822";
&& cur->parent()->contentType(false)->mimeType().toLower() == "message/rfc822";
if (parentIsMessage && cur->parent() != topLevel) {
return true;
}
......@@ -957,9 +957,9 @@ bool NodeHelper::unencryptedMessage_helper(KMime::Content *node, QByteArray &res
if (node) {
KMime::Content *curNode = node;
KMime::Content *decryptedNode = nullptr;
const QByteArray type = node->contentType(false) ? QByteArray(node->contentType()->mediaType()).toLower() : "text";
const QByteArray subType = node->contentType(false) ? node->contentType()->subType().toLower() : "plain";
const bool isMultipart = node->contentType(false) && node->contentType()->isMultipart();
const QByteArray type = node->contentType(false) ? QByteArray(node->contentType(false)->mediaType()).toLower() : "text";
const QByteArray subType = node->contentType(false) ? node->contentType(false)->subType().toLower() : "plain";
const bool isMultipart = node->contentType(false) && node->contentType(false)->isMultipart();
bool isSignature = false;
qCDebug(MIMETREEPARSER_LOG) << "(" << recursionLevel << ") Looking at" << type << "/" << subType;
......@@ -993,22 +993,22 @@ bool NodeHelper::unencryptedMessage_helper(KMime::Content *node, QByteArray &res
headers.setHead(curNode->head());
headers.parse();
if (decryptedNode->contentType(false)) {
headers.contentType()->from7BitString(decryptedNode->contentType()->as7BitString(false));
headers.contentType()->from7BitString(decryptedNode->contentType(false)->as7BitString(false));
} else {
headers.removeHeader<KMime::Headers::ContentType>();
}
if (decryptedNode->contentTransferEncoding(false)) {
headers.contentTransferEncoding()->from7BitString(decryptedNode->contentTransferEncoding()->as7BitString(false));
headers.contentTransferEncoding()->from7BitString(decryptedNode->contentTransferEncoding(false)->as7BitString(false));
} else {
headers.removeHeader<KMime::Headers::ContentTransferEncoding>();
}
if (decryptedNode->contentDisposition(false)) {
headers.contentDisposition()->from7BitString(decryptedNode->contentDisposition()->as7BitString(false));
headers.contentDisposition()->from7BitString(decryptedNode->contentDisposition(false)->as7BitString(false));
} else {
headers.removeHeader<KMime::Headers::ContentDisposition>();
}
if (decryptedNode->contentDescription(false)) {
headers.contentDescription()->from7BitString(decryptedNode->contentDescription()->as7BitString(false));
headers.contentDescription()->from7BitString(decryptedNode->contentDescription(false)->as7BitString(false));
} else {
headers.removeHeader<KMime::Headers::ContentDescription>();
}
......
......@@ -1195,8 +1195,9 @@ KMime::Content *TemplateParserJob::createMultipartMixed(const QVector<KMime::Con
{
KMime::Content *mixedPart = new KMime::Content(d->mMsg.data());
const QByteArray boundary = KMime::multiPartBoundary();
mixedPart->contentType()->setMimeType("multipart/mixed");
mixedPart->contentType()->setBoundary(boundary);
auto contentType = mixedPart->contentType();
contentType->setMimeType("multipart/mixed");
contentType->setBoundary(boundary);
mixedPart->contentTransferEncoding()->setEncoding(KMime::Headers::CE7Bit);
mixedPart->addContent(textPart);
......@@ -1205,10 +1206,10 @@ KMime::Content *TemplateParserJob::createMultipartMixed(const QVector<KMime::Con
mixedPart->addContent(attachment);
// If the content type has no name or filename parameter, add one, since otherwise the name
// would be empty in the attachment view of the composer, which looks confusing
if (attachment->contentType(false)) {
if (!attachment->contentType()->hasParameter(QStringLiteral("name"))
&& !attachment->contentType()->hasParameter(QStringLiteral("filename"))) {
attachment->contentType()->setParameter(
if (auto ct = attachment->contentType(false)) {
if (!ct->hasParameter(QStringLiteral("name"))
&& !ct->hasParameter(QStringLiteral("filename"))) {
ct->setParameter(
QStringLiteral("name"), i18nc("@item:intext", "Attachment %1", attachmentNumber));
}
}
......@@ -1221,8 +1222,9 @@ KMime::Content *TemplateParserJob::createMultipartRelated(const MessageCore::Ima
{
KMime::Content *relatedPart = new KMime::Content(d->mMsg.data());
const QByteArray boundary = KMime::multiPartBoundary();
relatedPart->contentType()->setMimeType("multipart/related");
relatedPart->contentType()->setBoundary(boundary);
auto contentType = relatedPart->contentType();
contentType->setMimeType("multipart/related");
contentType->setBoundary(boundary);
relatedPart->contentTransferEncoding()->setEncoding(KMime::Headers::CE7Bit);
relatedPart->addContent(mainTextPart);
for (KMime::Content *image : ic.images()) {
......
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