KMime::Content

Search for usage in LXR

#include <kmime_content.h>

Inheritance diagram for KMime::Content:

Public Types

typedef QVector< KMime::Content * > List
 

Public Member Functions

 Content (Content *parent=nullptr)
 
virtual ~Content ()
 
void addContent (Content *content, bool prepend=false)
 
void appendHeader (Headers::Base *h)
 
void assemble ()
 
QVector< Content * > attachments ()
 
QByteArray body () const
 
QSharedPointer< MessagebodyAsMessage () const
 
bool bodyIsMessage () const
 
void changeEncoding (Headers::contentEncoding e)
 
void clear ()
 
void clearContents (bool del=true)
 
Contentcontent (const ContentIndex &index) const
 
Headers::ContentDescriptioncontentDescription (bool create=true)
 
Headers::ContentDispositioncontentDisposition (bool create=true)
 
Headers::ContentIDcontentID (bool create=true)
 
Headers::ContentLocationcontentLocation (bool create=true)
 
QVector< Content * > contents () const
 
Headers::ContentTransferEncodingcontentTransferEncoding (bool create=true)
 
Headers::ContentTypecontentType (bool create=true)
 
QByteArray decodedContent ()
 
QString decodedText (bool trimText=false, bool removeTrailingNewlines=false)
 
QByteArray encodedBody ()
 
QByteArray encodedContent (bool useCrLf=false)
 
QByteArray epilogue () const
 
void fromUnicodeString (const QString &s)
 
bool hasContent () const
 
bool hasHeader (const char *type) const
 
QByteArray head () const
 
template<typename T >
T * header (bool create=false)
 
Headers::BaseheaderByType (const char *type) const
 
QVector< Headers::Base * > headers () const
 
QVector< Headers::Base * > headersByType (const char *type) const
 
ContentIndex index () const
 
ContentIndex indexForContent (Content *content) const
 
bool isFrozen () const
 
bool isTopLevel () const
 
int lineCount () const
 
Contentparent () const
 
void parse ()
 
QByteArray preamble () const
 
void removeContent (Content *content, bool del=false)
 
bool removeHeader (const char *type)
 
template<typename T >
bool removeHeader ()
 
void replaceContent (Content *oldContent, Content *newContent)
 
void setBody (const QByteArray &body)
 
void setContent (const QByteArray &s)
 
void setEpilogue (const QByteArray &epilogue)
 
void setFrozen (bool frozen=true)
 
void setHead (const QByteArray &head)
 
void setHeader (Headers::Base *h)
 
void setParent (Content *parent)
 
void setPreamble (const QByteArray &preamble)
 
int size ()
 
int storageSize () const
 
ContenttextContent ()
 
ContenttopLevel () const
 

Static Public Member Functions

static QByteArray defaultCharset ()
 

Protected Member Functions

virtual QByteArray assembleHeaders ()
 

Detailed Description

A class that encapsulates MIME encoded Content.

A Content object holds two representations of a content:

  • the string representation: This is the content encoded as a string ready for transport. Accessible through the encodedContent() method.
  • the broken-down representation: This is the tree of objects (headers, sub-Contents and (if present) the encapsulated message) that this Content is made of. Accessible through methods like header(), contents() and bodyAsMessage().

The parse() function updates the broken-down representation of the Content from its string representation. Calling it is necessary to access the headers, sub-Contents or the encapsulated message of this Content.

The assemble() function updates the string representation of the Content from its broken-down representation. Calling it is necessary for encodedContent() to reflect any changes made to the broken-down representation of the Content.

There are two basic types of a Content:

  • A leaf Content: This is a content that is neither a multipart content nor an encapsulated message. Because of this, it will not have any children, it has no sub-contents and is therefore a leaf content. Only leaf contents have a body that is not empty, i.e. functions that operate on the body, such as body(), size() and decodedContent(), will work only on leaf contents.
  • A non-leaf Content: This is a content that itself doesn't have any body, but that does have sub-contents. This is the case for contents that are of mimetype multipart/ or of mimetype message/rfc822. In case of a multipart content, contents() will return the multipart child contents. In case of an encapsulated message, the message can be accessed with bodyAsMessage(), and contents() will have one entry that is the message as well. On a non-leaf content, body() will have an empty return value and other functions working on the body will not work. A call to parse() is required before the child multipart contents or the encapsulated message is created.

Definition at line 98 of file kmime_content.h.

Member Typedef Documentation

Describes a list of Content objects.

Definition at line 105 of file kmime_content.h.

Constructor & Destructor Documentation

KMime::Content::Content ( Content parent = nullptr)
explicit

Creates an empty Content object with a specified parent.

Parameters
parentthe parent Content object
Since
4.3

Definition at line 42 of file kmime_content.cpp.

KMime::Content::~Content ( )
virtual

Destroys this Content object.

Definition at line 48 of file kmime_content.cpp.

Member Function Documentation

void KMime::Content::addContent ( Content content,
bool  prepend = false 
)

Adds a new sub-Content.

If the sub-Content is already part of another Content object, it is removed from there and its parent is updated. If the current Content object is single-part, it is converted to multipart/mixed first.

Warning
If the single-part to multipart conversion happens, all pointers you may have into this object (such as headers) will become invalid!
Parameters
contentThe new sub-Content.
prependIf true, prepend to the Content list; otherwise append. to the Content list.
See also
removeContent().

Definition at line 479 of file kmime_content.cpp.

void KMime::Content::appendHeader ( Headers::Base h)

Appends the specified header to the headers of this Content.

Parameters
hThe header to append.
Since
4.4

Definition at line 646 of file kmime_content.cpp.

void KMime::Content::assemble ( )

Generates the MIME content.

This means the string representation of this Content is updated from the broken-down object representation. Call this if you have made changes to the content, and want encodedContent() to reflect those changes.

Note
assemble() has no effect if the Content isFrozen(). You may want to freeze, for instance, signed sub-Contents, to make sure they are kept unmodified.
If this content is an encapsulated message, i.e. bodyIsMessage() returns true, then calling assemble() will also assemble the message returned by bodyAsMessage().
Warning
assemble() may change the order of the headers, and other details such as where folding occurs. This may break things like signature verification, so you should ONLY call assemble() when you have actually modified the content.

Definition at line 182 of file kmime_content.cpp.

QByteArray KMime::Content::assembleHeaders ( )
protectedvirtual

Reimplement this method if you need to assemble additional headers in a derived class.

Don't forget to call the implementation of the base class.

Returns
The raw, assembled headers.

Reimplemented in KMime::Message, and KMime::NewsArticle.

Definition at line 196 of file kmime_content.cpp.

QVector< Content * > KMime::Content::attachments ( )

Returns all attachments below this node, recursively.

This does not include crypto parts, nodes of alternative or related multipart nodes, or the primary body part (see textContent()).

See also
KMime::isAttachment(), KMime::hasAttachment()

Definition at line 438 of file kmime_content.cpp.

QByteArray KMime::Content::body ( ) const

Returns the Content body raw data.

Note that this will be empty for multipart contents or for encapsulated messages, after parse() has been called.

See also
setBody().

Definition at line 81 of file kmime_content.cpp.

Message::Ptr KMime::Content::bodyAsMessage ( ) const

If this content is an encapsulated message, in which case bodyIsMessage() will return true, the message represented by the body of this content will be returned.

The returned message is already fully parsed. Calling this method is the aquivalent of calling contents().first() and casting the result to a KMime::Message*. bodyAsMessage() has the advantage that it will return a shared pointer that will not be destroyed when the container message is destroyed or re-parsed.

The message that is returned here is created when calling parse(), so make sure to call parse() first. Since each parse() creates a new message object, a different message object will be returned each time you call parse().

If you make changes to the returned message, you need to call assemble() on this content or on the message if you want that encodedContent() reflects these changes. This also means that calling assemble() on this content will assemble the returned message.

Since
4.5

Definition at line 846 of file kmime_content.cpp.

bool KMime::Content::bodyIsMessage ( ) const
Returns
true if this content is an encapsulated message, i.e. if it has the mimetype message/rfc822.
Since
4.5

Definition at line 855 of file kmime_content.cpp.

void KMime::Content::changeEncoding ( Headers::contentEncoding  e)

Changes the encoding of this Content to e.

If the Content is binary, this actually re-encodes the data to use the new encoding.

Parameters
eThe new encoding to use.

Definition at line 577 of file kmime_content.cpp.

void KMime::Content::clear ( )

Clears the content, deleting all headers and sub-Contents.

Definition at line 209 of file kmime_content.cpp.

void KMime::Content::clearContents ( bool  del = true)

Removes all sub-Contents from this content.

Deletes them if del is true. This is different from calling removeContent() on each sub-Content, because removeContent() will convert this to a single-part Content if only one sub-Content is left. Calling clearContents() does NOT make this Content single-part.

Parameters
delWhether to delete the sub-Contents.
See also
removeContent()
Since
4.4

Definition at line 219 of file kmime_content.cpp.

Content * KMime::Content::content ( const ContentIndex index) const

Returns the Content specified by the given index.

If the index does not point to a Content, 0 is returned. If the index is invalid (empty), this Content is returned.

Parameters
indexThe Content index.

Definition at line 762 of file kmime_content.cpp.

Headers::ContentDescription* KMime::Content::contentDescription ( bool  create = true)

Returns the Content-Description header.

Parameters
createIf true, create the header if it doesn't exist yet.
Headers::ContentDisposition* KMime::Content::contentDisposition ( bool  create = true)

Returns the Content-Disposition header.

Parameters
createIf true, create the header if it doesn't exist yet.
Headers::ContentID* KMime::Content::contentID ( bool  create = true)

Returns the Content-ID header.

Parameters
createif true, create the header if it does not exist yet.
Since
4.4
Headers::ContentLocation* KMime::Content::contentLocation ( bool  create = true)

Returns the Content-Location header.

Parameters
createIf true, create the header if it doesn't exist yet.
Since
4.2
QVector< Content * > KMime::Content::contents ( ) const

For multipart contents, this will return a list of all multipart child contents.

For contents that are of mimetype message/rfc822, this will return a list with one entry, and that entry is the encapsulated message, as it would be returned by bodyAsMessage().

Definition at line 457 of file kmime_content.cpp.

Headers::ContentTransferEncoding* KMime::Content::contentTransferEncoding ( bool  create = true)

Returns the Content-Transfer-Encoding header.

Parameters
createIf true, create the header if it doesn't exist yet.
Headers::ContentType* KMime::Content::contentType ( bool  create = true)

Returns the Content-Type header.

Parameters
createIf true, create the header if it doesn't exist yet.
QByteArray KMime::Content::decodedContent ( )

Returns the decoded Content body.

Note that this will be empty for multipart contents or for encapsulated messages, after parse() has been called.

Definition at line 313 of file kmime_content.cpp.

QString KMime::Content::decodedText ( bool  trimText = false,
bool  removeTrailingNewlines = false 
)

Returns the decoded text.

Additional to decodedContent(), this also applies charset decoding. If this is not a text Content, decodedText() returns an empty QString.

Parameters
trimTextIf true, then the decoded text will have all trailing whitespace removed.
removeTrailingNewlinesIf true, then the decoded text will have all consecutive trailing newlines removed.

The last trailing new line of the decoded text is always removed.

Definition at line 364 of file kmime_content.cpp.

QByteArray KMime::Content::defaultCharset ( )
static

Returns the charset that is used to decode RFC2047 strings in all headers and to decode the body if the charset is not declared explicitly.

It is also used as the charset when encoding RFC2047 strings in headers.

Definition at line 757 of file kmime_content.cpp.

QByteArray KMime::Content::encodedBody ( )

Like encodedContent(), with the difference that only the body will be returned, i.e.

the headers are excluded.

Since
4.6

Definition at line 252 of file kmime_content.cpp.

QByteArray KMime::Content::encodedContent ( bool  useCrLf = false)

Returns a QByteArray containing the encoded Content, including the Content header and all sub-Contents.

If you make changes to the broken-down representation of the message, be sure to first call assemble() before calling encodedContent(), otherwise the result will not be up-to-date.

If this content is an encapsulated message, i.e. bodyIsMessage() returns true, then encodedContent() will use the message returned by bodyAsMessage() as the body of the result, calling encodedContent() on the message.

Parameters
useCrLfIf true, use CRLF instead of LF for linefeeds.

Definition at line 229 of file kmime_content.cpp.

QByteArray KMime::Content::epilogue ( ) const

Returns the MIME preamble.

Returns
a QByteArray containing the MIME epilogue.
Since
4.9

Definition at line 101 of file kmime_content.cpp.

void KMime::Content::fromUnicodeString ( const QString s)

Sets the Content body to the given string using charset of the content type.

If the charset can not be found, the system charset is taken and the content type header is changed to that charset. The charset of the content type header should be set to a charset that can encode the given string before calling this method.

This method does not set the content transfer encoding automatically, it needs to be set to a suitable value that can encode the given string before calling this method.

This method only makes sense for single-part contents, do not try to pass a multipart body or an encapsulated message here, that wouldn't work.

Parameters
sUnicode-encoded string.

Definition at line 404 of file kmime_content.cpp.

bool KMime::Content::hasContent ( ) const

Returns true if this Content object is not empty.

Definition at line 57 of file kmime_content.cpp.

bool KMime::Content::hasHeader ( const char *  type) const
Returns
true if this Content has a header of type type.
Parameters
typeThe type of the header to look for.

Definition at line 666 of file kmime_content.cpp.

QByteArray KMime::Content::head ( ) const

Returns the Content header raw data.

See also
setHead().

Definition at line 68 of file kmime_content.cpp.

template<typename T >
T * KMime::Content::header ( bool  create = false)

Returns the first header of type T, if it exists.

If the header does not exist and create is true, creates an empty header and returns it. Otherwise returns 0. Note that the returned header may be empty.

Parameters
createWhether to create the header if it does not exist.
Since
4.4.

KDE5: BIC: FIXME: Why is the default argument false here? That is inconsistent with the methods in KMime::Message!

Definition at line 679 of file kmime_content.h.

Headers::Base * KMime::Content::headerByType ( const char *  type) const

Returns the first header of type type, if it exists.

Otherwise returns 0. Note that the returned header may be empty.

Parameters
typethe header type to find
Since
4.2

Definition at line 611 of file kmime_content.cpp.

QVector< Headers::Base * > KMime::Content::headers ( ) const

Returns all headers.

Since
5.7

Definition at line 606 of file kmime_content.cpp.

QVector< Headers::Base * > KMime::Content::headersByType ( const char *  type) const

Returns all type headers in the Content.

Take care that this result is not cached, so could be slow.

Parameters
typethe header type to find
Since
4.2

Definition at line 624 of file kmime_content.cpp.

ContentIndex KMime::Content::index ( ) const

Returns the index of this Content based on the topLevel() object.

Since
4.3

Definition at line 836 of file kmime_content.cpp.

ContentIndex KMime::Content::indexForContent ( Content content) const

Returns the ContentIndex for the given Content, or an invalid index if the Content is not found within the hierarchy.

Parameters
contentthe Content object to search.

Definition at line 776 of file kmime_content.cpp.

bool KMime::Content::isFrozen ( ) const

Returns whether this Content is frozen.

A frozen content is immutable, i.e. calling assemble() will never modify its head or body, and encodedContent() will return the same data before and after parsing.

Since
4.4.
See also
setFrozen().

Definition at line 172 of file kmime_content.cpp.

bool KMime::Content::isTopLevel ( ) const

Returns true if this is the top-level node in the MIME tree.

The top-level node is always a Message or NewsArticle. However, a node can be a Message without being a top-level node when it is an encapsulated message.

Definition at line 796 of file kmime_content.cpp.

int KMime::Content::lineCount ( ) const

Line count of this Content and all sub-Contents.

Definition at line 707 of file kmime_content.cpp.

Content * KMime::Content::parent ( ) const

Returns the parent content object, or 0 if the content doesn't have a parent.

Since
4.3

Definition at line 819 of file kmime_content.cpp.

void KMime::Content::parse ( )

Parses the Content.

This means the broken-down object representation of the Content is updated from the string representation of the Content.

Call this if you want to access or change headers, sub-Contents or the encapsulated message.

Note
Calling parse() twice will not work for multipart contents or for contents of which the body is an encapsulated message. The reason is that the first parse() will delete the body, so there is no body to work on for the second call of parse().
Calling this will reset the message returned by bodyAsMessage(), as the message is re-parsed as well. Also, all old sub-contents will be deleted, so any old Content pointer will become invalid.

Definition at line 111 of file kmime_content.cpp.

QByteArray KMime::Content::preamble ( ) const

Returns the MIME preamble.

Returns
a QByteArray containing the MIME preamble.
Since
4.9

Definition at line 91 of file kmime_content.cpp.

void KMime::Content::removeContent ( Content content,
bool  del = false 
)

Removes the given sub-Content.

If only one sub-Content is left, the current Content object is converted into a single-part Content.

Warning
If the multipart to single-part conversion happens, the head and body of the single remaining sub-Content are copied over, and the sub-Content is deleted. All pointers to it or into it (such as headers) will become invalid!
Parameters
contentThe Content to remove.
delIf true, delete the removed Content object. Otherwise set its parent to 0.
See also
addContent().
clearContents().

Definition at line 538 of file kmime_content.cpp.

bool KMime::Content::removeHeader ( const char *  type)

Searches for the first header of type type, and deletes it, removing it from this Content.

Parameters
typeThe type of the header to look for.
Returns
true if a header was found and removed.

Definition at line 652 of file kmime_content.cpp.

template<typename T >
bool KMime::Content::removeHeader ( )

Searches for the first header of type T, and deletes it, removing it from this Content.

Template Parameters
TThe type of the header to look for.
Returns
true if a header was found and removed.

Definition at line 692 of file kmime_content.h.

void KMime::Content::setBody ( const QByteArray body)

Sets the Content body raw data.

This method operates on the string representation of the Content. Call parse() if you want to access individual sub-Contents or the encapsulated message.

Parameters
bodyis a QByteArray containing the body data.
See also
body().

Definition at line 86 of file kmime_content.cpp.

void KMime::Content::setContent ( const QByteArray s)

Sets the Content to the given raw data, containing the Content head and body separated by two linefeeds.

This method operates on the string representation of the Content. Call parse() if you want to access individual headers, sub-Contents or the encapsulated message.

Note
The passed data must not contain any CRLF sequences, only LF. Use CRLFtoLF for conversion before passing in the data.
Parameters
sis a QByteArray containing the raw Content data.

Definition at line 62 of file kmime_content.cpp.

void KMime::Content::setEpilogue ( const QByteArray epilogue)

Sets the MIME preamble.

Parameters
epiloguea QByteArray containing what will be used as the MIME epilogue.
Since
4.9

Definition at line 106 of file kmime_content.cpp.

void KMime::Content::setFrozen ( bool  frozen = true)

Freezes this Content if frozen is true; otherwise unfreezes it.

Parameters
frozenfreeze content if true, otherwise unfreeze
Since
4.4
See also
isFrozen().

Definition at line 177 of file kmime_content.cpp.

void KMime::Content::setHead ( const QByteArray head)

Sets the Content header raw data.

This method operates on the string representation of the Content. Call parse() if you want to access individual headers.

Parameters
headis a QByteArray containing the header data.
See also
head().

Definition at line 73 of file kmime_content.cpp.

void KMime::Content::setHeader ( Headers::Base h)

Sets the specified header to this Content.

Any previous header of the same type is removed. If you need multiple headers of the same type, use appendHeader() or prependHeader().

Parameters
hThe header to set.
See also
appendHeader()
removeHeader()
Since
4.4

Definition at line 639 of file kmime_content.cpp.

void KMime::Content::setParent ( Content parent)

Sets a new parent to the Content and add to its contents list.

If it already had a parent, it is removed from the old parents contents list.

Parameters
parentthe new parent
Since
4.3

Definition at line 801 of file kmime_content.cpp.

void KMime::Content::setPreamble ( const QByteArray preamble)

Sets the MIME preamble.

Parameters
preamblea QByteArray containing what will be used as the MIME preamble.
Since
4.9

Definition at line 96 of file kmime_content.cpp.

int KMime::Content::size ( )

Returns the size of the Content body after encoding.

(If the encoding is quoted-printable, this is only an approximate size.) This will return 0 for multipart contents or for encapsulated messages.

Definition at line 671 of file kmime_content.cpp.

int KMime::Content::storageSize ( ) const

Returns the size of this Content and all sub-Contents.

Definition at line 687 of file kmime_content.cpp.

Content * KMime::Content::textContent ( )

Returns the first Content with mimetype text/.

Definition at line 420 of file kmime_content.cpp.

Content * KMime::Content::topLevel ( ) const

Returns the toplevel content object, 0 if there is no such object.

Since
4.3

Definition at line 824 of file kmime_content.cpp.


The documentation for this class was generated from the following files:
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Jul 26 2021 23:13:35 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.