KMime

message.cpp
1/*
2 kmime_message.cpp
3
4 KMime, the KDE Internet mail/usenet news message library.
5 SPDX-FileCopyrightText: 2001 the KMime authors.
6 See file AUTHORS for details
7
8 SPDX-License-Identifier: LGPL-2.0-or-later
9*/
10
11#include "message.h"
12#include "util_p.h"
13
14using namespace KMime;
15
16namespace KMime
17{
18
19Message::Message() = default;
20Message::~Message() = default;
21
23{
24 // Create the mandatory fields (RFC5322) if they do not exist already.
25 date(true);
26 from(true);
27
28 // Make sure the mandatory MIME-Version field (RFC2045) is present and valid.
29 auto *mimeVersion = header<Headers::MIMEVersion>(true);
30 mimeVersion->from7BitString("1.0");
31
32 // Assemble all header fields.
34}
35
37{
38 // ugly, but given we start from a non-const this we know the result will be safely const_castable as well
39 return const_cast<Content*>(static_cast<const Message*>(this)->mainBodyPart(type));
40}
41
42const Content *Message::mainBodyPart(const QByteArray &type) const
43{
44 const KMime::Content *c = this;
45 while (c) {
46 // not a multipart message
49 if ((contentType && contentType->mimeType() == type) || type.isEmpty()) {
50 return c;
51 }
52 return nullptr;
53 }
54
55 // empty multipart
56 if (c->contents().empty()) {
57 return nullptr;
58 }
59
60 // multipart/alternative
61 if (contentType && contentType->subType() == "alternative") {
62 if (type.isEmpty()) {
63 return c->contents()[0];
64 }
65 const auto contents = c->contents();
66 for (auto c1 : contents) {
67 if (c1->contentType()->mimeType() == type) {
68 return c1;
69 }
70 }
71 return nullptr;
72 }
73
74 c = c->contents()[0];
75 }
76
77 return nullptr;
78}
79
81{
82 return QStringLiteral("message/rfc822");
83}
84
85// @cond PRIVATE
86#define kmime_mk_header_accessor( type, method ) \
87 Headers::type *Message::method( bool create ) { \
88 return header<Headers::type>( create ); \
89 } \
90 const Headers::type *Message::method() const { \
91 return header<Headers::type>(); \
92 }
93
94kmime_mk_header_accessor(MessageID, messageID)
95kmime_mk_header_accessor(Subject, subject)
96kmime_mk_header_accessor(Date, date)
97kmime_mk_header_accessor(Organization, organization)
98kmime_mk_header_accessor(From, from)
99kmime_mk_header_accessor(ReplyTo, replyTo)
100kmime_mk_header_accessor(To, to)
101kmime_mk_header_accessor(Cc, cc)
102kmime_mk_header_accessor(Bcc, bcc)
103kmime_mk_header_accessor(References, references)
104kmime_mk_header_accessor(UserAgent, userAgent)
105kmime_mk_header_accessor(InReplyTo, inReplyTo)
106kmime_mk_header_accessor(Sender, sender)
107
108#undef kmime_mk_header_accessor
109// @endcond
110
111}
112
A class that encapsulates MIME encoded Content.
Definition content.h:92
Headers::ContentType * contentType(bool create=true)
Returns the Content-Type header.
const Headers::ContentType * contentType() const
Returns the Content-Type header.
T * header() const
Returns the first header of type.
Definition content.h:803
virtual QByteArray assembleHeaders()
Reimplement this method if you need to assemble additional headers in a derived class.
Definition content.cpp:203
QList< Content * > contents()
For multipart contents, this will return a list of all multipart child contents.
Definition content.cpp:468
Represents a "Content-Type" header.
Definition headers.h:984
QByteArray subType() const
Returns the mime sub-type (second part of the mimetype).
Definition headers.cpp:1551
bool isMultipart() const
Returns true if the associated MIME entity is a multipart container.
Definition headers.cpp:1606
QByteArray mimeType() const
Returns the mimetype.
Definition headers.cpp:1536
Represents a (email) message.
Definition message.h:65
const KMime::Headers::Date * date() const
Returns the Date header.
QByteArray assembleHeaders() override
Reimplement this method if you need to assemble additional headers in a derived class.
Definition message.cpp:22
const KMime::Headers::From * from() const
Returns the From header.
Content * mainBodyPart(const QByteArray &type=QByteArray())
Returns the first main body part of a given type, taking multipart/mixed and multipart/alternative no...
Definition message.cpp:36
~Message() override
Destroys this Message.
static QString mimeType()
Returns the MIME type used for Messages.
Definition message.cpp:80
Message()
Creates an empty Message.
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Wed Nov 6 2024 12:05:01 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.