Messagelib

attachmentjob.cpp
1/*
2 SPDX-FileCopyrightText: 2009 Constantin Berzan <exit3219@gmail.com>
3
4 Parts based on KMail code by:
5 Various authors.
6
7 SPDX-License-Identifier: LGPL-2.0-or-later
8*/
9
10#include "job/attachmentjob.h"
11#include "contentjobbase_p.h"
12#include "job/singlepartjob.h"
13#include "part/globalpart.h"
14#include "utils/util.h"
15
16#include "messagecomposer_debug.h"
17
18using namespace MessageComposer;
19using namespace MessageCore;
20
21class MessageComposer::AttachmentJobPrivate : public ContentJobBasePrivate
22{
23public:
24 AttachmentJobPrivate(AttachmentJob *qq)
25 : ContentJobBasePrivate(qq)
26 {
27 }
28
30
31 Q_DECLARE_PUBLIC(AttachmentJob)
32};
33
34AttachmentJob::AttachmentJob(AttachmentPart::Ptr part, QObject *parent)
35 : ContentJobBase(*new AttachmentJobPrivate(this), parent)
36{
38 d->part = part;
39}
40
41AttachmentJob::~AttachmentJob() = default;
42
43AttachmentPart::Ptr AttachmentJob::attachmentPart() const
44{
45 Q_D(const AttachmentJob);
46 return d->part;
47}
48
49void AttachmentJob::setAttachmentPart(const AttachmentPart::Ptr &part)
50{
52 d->part = part;
53}
54
55void AttachmentJob::doStart()
56{
58 Q_ASSERT(d->part);
59
60 if (d->part->mimeType() == "multipart/digest" || d->part->mimeType() == "message/rfc822") {
61 // this is actually a digest, so we don't want any additional headers
62 // the attachment is really a complete multipart/digest subtype
63 // and us adding our own headers would break it. so copy over the content
64 // and leave it alone
65 auto part = new KMime::Content;
66 part->setContent(d->part->data());
67 part->parse();
68 d->subjobContents << part;
69 process();
70 return;
71 }
72
73 // Set up a subjob to generate the attachment content.
74 auto sjob = new SinglepartJob(this);
75 sjob->setData(d->part->data());
76
77 // Set up the headers.
78 // rfc822 forwarded messages have 7bit CTE, the message itself will have
79 // its own CTE for the content
80 if (d->part->mimeType() == "message/rfc822") {
81 sjob->contentTransferEncoding()->setEncoding(KMime::Headers::CE7Bit);
82 } else {
83 sjob->contentTransferEncoding()->setEncoding(d->part->encoding());
84 }
85
86 auto ct = sjob->contentType();
87 ct->setMimeType(d->part->mimeType()); // setMimeType() clears all other params.
88 ct->setName(d->part->name());
89 if (ct->isText()) {
90 // If it is a text file, detect its charset.
91 // sjob->contentType()->setCharset( d->detectCharset( d->part->data() ) );
92
93 // From my few tests, this is *very* unreliable.
94 // Therefore, if we do not know which charset to use, just use UTF-8.
95 // (cberzan)
96 QByteArray textCharset = d->part->charset();
97 if (textCharset.isEmpty()) {
98 qCWarning(MESSAGECOMPOSER_LOG) << "No charset specified. Using UTF-8.";
99 textCharset = "utf-8";
100 }
101 ct->setCharset(textCharset);
102 }
103
104 sjob->contentDescription()->fromUnicodeString(d->part->description());
105
106 auto contentDisposition = sjob->contentDisposition();
107 contentDisposition->setFilename(d->part->fileName());
108 contentDisposition->setRFC2047Charset("utf-8");
109 if (d->part->isInline()) {
110 contentDisposition->setDisposition(KMime::Headers::CDinline);
111 } else {
112 contentDisposition->setDisposition(KMime::Headers::CDattachment);
113 }
114
116}
117
118void AttachmentJob::process()
119{
121 // The content has been created by our subjob.
122 Q_ASSERT(d->subjobContents.count() == 1);
123 d->resultContent = d->subjobContents.constFirst();
124 emitResult();
125}
126
127#include "moc_attachmentjob.cpp"
void emitResult()
void setContent(const QByteArray &s)
The AttachmentJob class.
The ContentJobBase class.
virtual void doStart()
Reimplement to do additional stuff before processing children, such as adding more subjobs.
The SinglepartJob class.
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
bool isEmpty() const const
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:33:25 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.