File indexing completed on 2024-04-28 09:12:11

0001 /*
0002     kmime_message.h
0003 
0004     KMime, the KDE Internet mail/usenet news message library.
0005     SPDX-FileCopyrightText: 2001 the KMime authors.
0006     See file AUTHORS for details
0007 
0008     SPDX-License-Identifier: LGPL-2.0-or-later
0009 */
0010 #pragma once
0011 
0012 #include "kmime_export.h"
0013 #include "kmime_content.h"
0014 #include "kmime_headers.h"
0015 
0016 #include <QMetaType>
0017 #include <QSharedPointer>
0018 
0019 namespace KMime
0020 {
0021 
0022 class MessagePrivate;
0023 
0024 /**
0025  * Represents a (email) message.
0026  *
0027  * Sample how to create a multipart message:
0028  * \code
0029  * // Set the multipart message.
0030  * Message *m = new Message;
0031  * Headers::ContentType *ct = m->contentType();
0032  * ct->setMimeType( "multipart/mixed" );
0033  * ct->setBoundary( multiPartBoundary() );
0034  * ct->setCategory( Headers::CCcontainer );
0035  * Headers::ContentTransferEncoding *cte = m->contentTransferEncoding();
0036  * cte->setEncoding(Headers::CE7Bit);
0037  * cte->setDecoded(true);
0038  *
0039  * // Set the headers.
0040  * m->from()->fromUnicodeString( "some@mailaddy.com", "utf-8" );
0041  * m->to()->fromUnicodeString( "someother@mailaddy.com", "utf-8" );
0042  * m->cc()->fromUnicodeString( "some@mailaddy.com", "utf-8" );
0043  * m->date()->setDateTime( QDateTime::currentLocalDateTime() );
0044  * m->subject()->fromUnicodeString( "My Subject", "utf-8" );
0045  *
0046  * // Set the first multipart, the body message.
0047  * KMime::Content *b = new KMime::Content;
0048  * b->contentType()->setMimeType( "text/plain" );
0049  * b->setBody( "Some text..." );
0050  *
0051  * // Set the second multipart, the attachment.
0052  * KMime::Content *a = new KMime::Content;
0053  * KMime::Headers::ContentDisposition *d = new KMime::Headers::ContentDisposition( attachMessage );
0054  * d->setFilename( "cal.ics" );
0055  * d->setDisposition( KMime::Headers::CDattachment );
0056  * a->contentType()->setMimeType( "text/plain" );
0057  * a->setHeader( d );
0058  * a->setBody( "Some text in the attachment..." );
0059  *
0060  * // Attach the both multiparts and assemble the message.
0061  * m->appendContent( b );
0062  * m->appendContent( a );
0063  * m->assemble();
0064  * \endcode
0065  */
0066 class KMIME_EXPORT Message : public Content
0067 {
0068 public:
0069     /**
0070       A shared pointer to a message object.
0071     */
0072     typedef QSharedPointer<Message> Ptr;
0073     /**
0074       Creates an empty Message.
0075     */
0076     Message();
0077 
0078     /**
0079       Destroys this Message.
0080     */
0081     ~Message() override;
0082 
0083     /**
0084       Returns the Message-ID header.
0085       @param create If true, create the header if it doesn't exist yet.
0086     */
0087     KMime::Headers::MessageID *messageID(bool create = true);
0088 
0089     /**
0090       Returns the Subject header.
0091       @param create If true, create the header if it doesn't exist yet.
0092     */
0093     KMime::Headers::Subject *subject(bool create = true);
0094 
0095     /**
0096       Returns the Date header.
0097       @param create If true, create the header if it doesn't exist yet.
0098     */
0099     KMime::Headers::Date *date(bool create = true);
0100 
0101     /**
0102       Returns the From header.
0103       @param create If true, create the header if it doesn't exist yet.
0104     */
0105     KMime::Headers::From *from(bool create = true);
0106 
0107     /**
0108       Returns the Organization header.
0109       @param create If true, create the header if it doesn't exist yet.
0110     */
0111     KMime::Headers::Organization *organization(bool create = true);
0112 
0113     /**
0114       Returns the Reply-To header.
0115       @param create If true, create the header if it doesn't exist yet.
0116     */
0117     KMime::Headers::ReplyTo *replyTo(bool create = true);
0118 
0119     /**
0120       Returns the To header.
0121       @param create If true, create the header if it doesn't exist yet.
0122     */
0123     KMime::Headers::To *to(bool create = true);
0124 
0125     /**
0126       Returns the Cc header.
0127       @param create If true, create the header if it doesn't exist yet.
0128     */
0129     KMime::Headers::Cc *cc(bool create = true);
0130 
0131     /**
0132       Returns the Bcc header.
0133       @param create If true, create the header if it doesn't exist yet.
0134     */
0135     KMime::Headers::Bcc *bcc(bool create = true);
0136 
0137     /**
0138       Returns the References header.
0139       @param create If true, create the header if it doesn't exist yet.
0140     */
0141     KMime::Headers::References *references(bool create = true);
0142 
0143     /**
0144       Returns the User-Agent header.
0145       @param create If true, create the header if it doesn't exist yet.
0146     */
0147     KMime::Headers::UserAgent *userAgent(bool create = true);
0148 
0149     /**
0150       Returns the In-Reply-To header.
0151       @param create If true, create the header if it doesn't exist yet.
0152     */
0153     KMime::Headers::InReplyTo *inReplyTo(bool create = true);
0154 
0155     /**
0156       Returns the Sender header.
0157       @param create If true, create the header if it doesn't exist yet.
0158     */
0159     KMime::Headers::Sender *sender(bool create = true);
0160 
0161     /**
0162       Returns the first main body part of a given type, taking multipart/mixed
0163       and multipart/alternative nodes into consideration.
0164       Eg. \c bodyPart("text/html") will return a html content object if that is
0165       provided in a multipart/alternative node, but not if it's the non-first
0166       child node of a multipart/mixed node (ie. an attachment).
0167       @param type The mimetype of the body part, if not given, the first
0168       body part will be returned, regardless of it's type.
0169     */
0170     Content *mainBodyPart(const QByteArray &type = QByteArray());
0171 
0172     /**
0173       Returns the MIME type used for Messages
0174     */
0175     static QString mimeType();
0176 
0177 protected:
0178     QByteArray assembleHeaders() override;
0179 
0180 private:
0181     Q_DECLARE_PRIVATE(Message)
0182 
0183 }; // class Message
0184 
0185 } // namespace KMime
0186 
0187 Q_DECLARE_METATYPE(KMime::Message*)
0188 Q_DECLARE_METATYPE(KMime::Message::Ptr)
0189