Messagelib

messagefactoryng.h
1 /*
2  SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company, [email protected]
3  SPDX-FileCopyrightText: 2010 Leo Franchi <[email protected]>
4  SPDX-FileCopyrightText: 2017-2023 Laurent Montel <[email protected]>
5 
6  SPDX-License-Identifier: GPL-2.0-or-later
7 */
8 
9 #pragma once
10 
11 #include "messagecomposer_export.h"
12 #include <KMime/KMimeMessage>
13 #include <KMime/MDN>
14 
15 #include <Akonadi/Collection>
16 #include <Akonadi/Item>
17 #include <Akonadi/MessageStatus>
18 #include <QVector>
19 
20 namespace KIdentityManagement
21 {
22 class IdentityManager;
23 }
24 
25 namespace MessageComposer
26 {
27 /**
28  * Enumeration that defines the available reply "modes"
29  */
31  ReplySmart = 0, //< Attempt to automatically guess the best recipient for the reply
32  ReplyAuthor, //< Reply to the author of the message (possibly NOT the mailing list, if any)
33  ReplyList, //< Reply to the mailing list (and not the author of the message)
34  ReplyAll, //< Reply to author and all the recipients in CC
35  ReplyNone, //< Don't set reply addresses: they will be set manually
36 };
37 
38 enum MDNAdvice {
39  MDNIgnore,
40  MDNSendDenied,
41  MDNSend,
42 };
43 /**
44  * Contains various factory methods for creating new messages such as replies, MDNs, forwards, etc.
45  */
46 class MESSAGECOMPOSER_EXPORT MessageFactoryNG : public QObject
47 {
48  Q_OBJECT
49 public:
50  /// Small helper structure which encapsulates the KMime::Message created when creating a reply, and
51  /// the reply mode
52  struct MessageReply {
53  KMime::Message::Ptr msg = nullptr; ///< The actual reply message
54  bool replyAll = false; ///< If true, the "reply all" template was used, otherwise the normal reply
55  /// template
56  };
57 
58  explicit MessageFactoryNG(const KMime::Message::Ptr &origMsg,
61  QObject *parent = nullptr);
62  ~MessageFactoryNG() override;
63 
64  /**
65  * Create a new message that is a reply to this message, filling all
66  * required header fields with the proper values. The returned message
67  * is not stored in any folder. Marks this message as replied.
68  *
69  */
70  void createReplyAsync();
71 
72  /** Create a new message that is a forward of this message, filling all
73  required header fields with the proper values. The returned message
74  is not stored in any folder. Marks this message as forwarded. */
75  void createForwardAsync();
76 
77  /**
78  * Create a forward from the given list of messages, attaching each
79  * message to be forwarded to the new forwarded message.
80  *
81  * If no list is passed, use the original message passed in the MessageFactoryNG
82  * constructor.
83  */
84  Q_REQUIRED_RESULT QPair<KMime::Message::Ptr, QVector<KMime::Content *>> createAttachedForward(const Akonadi::Item::List &items = Akonadi::Item::List());
85 
86  /** Create a new message that is a redirect to this message, filling all
87  required header fields with the proper values. The returned message
88  is not stored in any folder. Marks this message as replied.
89  Redirects differ from forwards so they are forwarded to some other
90  user, mail is not changed and the reply-to field is set to
91  the email address of the original sender.
92  */
93  Q_REQUIRED_RESULT KMime::Message::Ptr createRedirect(const QString &toStr,
94  const QString &ccStr = QString(),
95  const QString &bccStr = QString(),
96  int transportId = -1,
97  const QString &fcc = QString(),
98  int identity = -1);
99 
100  Q_REQUIRED_RESULT KMime::Message::Ptr createResend();
101 
102  /** Create a new message that is a delivery receipt of this message,
103  filling required header fields with the proper values. The
104  returned message is not stored in any folder. */
105  Q_REQUIRED_RESULT KMime::Message::Ptr createDeliveryReceipt();
106 
107  /** Create a new message that is a MDN for this message, filling all
108  required fields with proper values. The returned message is not
109  stored in any folder.
110 
111  @param a Use AutomaticAction for filtering and ManualAction for
112  user-induced events.
113  @param d See docs for KMime::MDN::DispositionType
114  @param s See docs for KMime::MDN::SendingMode (in KMail, use MDNAdvideDialog to ask the user for this parameter)
115  @param m See docs for KMime::MDN::DispositionModifier
116 
117  @return The notification message or 0, if none should be sent, as well as the state of the MDN operation.
118  **/
119  Q_REQUIRED_RESULT KMime::Message::Ptr createMDN(KMime::MDN::ActionMode a,
120  KMime::MDN::DispositionType d,
121  KMime::MDN::SendingMode s,
122  int mdnQuoteOriginal = 0,
124 
125  /**
126  * Create a new forwarded MIME digest. If the user is trying to forward multiple messages
127  * at once all inline, they can choose to have them be compiled into a single digest
128  * message.
129  *
130  * This will return a header message and individual message parts to be set as
131  * attachments.
132  *
133  * @param msgs List of messages to be composed into a digest
134  */
135  Q_REQUIRED_RESULT QPair<KMime::Message::Ptr, KMime::Content *> createForwardDigestMIME(const Akonadi::Item::List &items);
136 
137  /**
138  * Set the identity manager to be used when creating messages.
139  * Required to be set before create* is called, otherwise the created messages
140  * might have the wrong identity data.
141  */
142  void setIdentityManager(KIdentityManagement::IdentityManager *ident);
143 
144  /**
145  * Set the reply strategy to use. Default is ReplySmart.
146  */
147  void setReplyStrategy(MessageComposer::ReplyStrategy replyStrategy);
148 
149  /**
150  * Set the selection to be used to base the reply on.
151  */
152  void setSelection(const QString &selection);
153 
154  /**
155  * Whether to quote the original message in the reply.
156  * Default is to quote.
157  */
158  void setQuote(bool quote);
159 
160  /**
161  * Set the template to be used when creating the reply. Default is to not
162  * use any template at all.
163  */
164  void setTemplate(const QString &templ);
165 
166  /**
167  * Set extra mailinglist addresses to send the created message to.
168  * Any mailing-list addresses specified in the original message
169  * itself will be added by MessageFactoryNG, so no need to add those manually.
170  */
171  void setMailingListAddresses(const KMime::Types::Mailbox::List &listAddresses);
172 
173  /**
174  * Set the identity that is set for the folder in which the given message is.
175  * It is used as a fallback when finding the identity if it can't be found in
176  * any other way.
177  * @param folderIdentityId an uoid of KIdentityManagement::Identity
178  */
179  void setFolderIdentity(uint folderIdentityId);
180 
181  /**
182  * Whether or not to put the reply to a message in the same folder as the message itself.
183  * If so, specify the folder id in which to put them. Default is -1, which means to not put
184  * replies in the same folder at all.
185  */
186  void putRepliesInSameFolder(Akonadi::Item::Id parentColId = -1);
187 
188  /**
189  * When creating MDNs, the user needs to be asked for confirmation in specific
190  * cases according to RFC 2298.
191  */
192  Q_REQUIRED_RESULT static bool MDNRequested(const KMime::Message::Ptr &msg);
193 
194  /**
195  * If sending an MDN requires confirmation due to multiple addresses.
196  *
197  * RFC 2298: [ Confirmation from the user SHOULD be obtained (or no
198  * MDN sent) ] if there is more than one distinct address in the
199  * Disposition-Notification-To header.
200  */
201  Q_REQUIRED_RESULT static bool MDNConfirmMultipleRecipients(const KMime::Message::Ptr &msg);
202 
203  /**
204  *
205  * If sending an MDN requires confirmation due to discrepancy between MDN
206  * header and Return-Path header.
207  *
208  * RFC 2298: MDNs SHOULD NOT be sent automatically if the address in
209  * the Disposition-Notification-To header differs from the address
210  * in the Return-Path header. [...] Confirmation from the user
211  * SHOULD be obtained (or no MDN sent) if there is no Return-Path
212  * header in the message [...]
213  */
214  Q_REQUIRED_RESULT static bool MDNReturnPathEmpty(const KMime::Message::Ptr &msg);
215  Q_REQUIRED_RESULT static bool MDNReturnPathNotInRecieptTo(const KMime::Message::Ptr &msg);
216 
217  /**
218  * If the MDN headers contain options that KMail can't parse
219  */
220  Q_REQUIRED_RESULT static bool MDNMDNUnknownOption(const KMime::Message::Ptr &msg);
221 
222  Q_REQUIRED_RESULT bool replyAsHtml() const;
223  void setReplyAsHtml(bool replyAsHtml);
224 
225 Q_SIGNALS:
226  void createReplyDone(const MessageComposer::MessageFactoryNG::MessageReply &reply);
227  void createForwardDone(const KMime::Message::Ptr &msg);
228 
229 private Q_SLOTS:
230  void slotCreateReplyDone(const KMime::Message::Ptr &msg, bool replyAll);
231  void slotCreateForwardDone(const KMime::Message::Ptr &msg);
232 
233 private:
234  /** @return the UOID of the identity for this message.
235  Searches the "x-kmail-identity" header and if that fails,
236  searches with KIdentityManagement::IdentityManager::identityForAddress()
237  **/
238  Q_REQUIRED_RESULT uint identityUoid(const KMime::Message::Ptr &msg);
239 
240  Q_REQUIRED_RESULT QString replaceHeadersInString(const KMime::Message::Ptr &msg, const QString &s);
241 
242  /*
243  * If force charset option is enabled, try to set the original charset
244  * in the newly created message. If unable to encode, fall back to
245  * preferred charsets, and if all fail, use UTF-8.
246  */
247  void applyCharset(const KMime::Message::Ptr msg);
248 
249  Q_REQUIRED_RESULT QByteArray getRefStr(const KMime::Message::Ptr &msg);
250  KMime::Content *createForwardAttachmentMessage(const KMime::Message::Ptr &fwdMsg);
251 
252  KIdentityManagement::IdentityManager *mIdentityManager = nullptr;
253  // Required parts to create messages
254  KMime::Message::Ptr mOrigMsg;
255  uint mFolderId;
256  Akonadi::Item::Id mParentFolderId;
257 
258  Akonadi::Collection mCollection;
259 
260  // Optional settings the calling class may set
261  MessageComposer::ReplyStrategy mReplyStrategy;
262  QString mSelection;
263  QString mTemplate;
264  bool mQuote = true;
265  bool mReplyAsHtml = false;
266  KMime::Types::Mailbox::List mMailingListAddresses;
267  Akonadi::Item::Id mId;
268 };
269 }
270 
271 Q_DECLARE_METATYPE(MessageComposer::ReplyStrategy)
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
Contains various factory methods for creating new messages such as replies, MDNs, forwards,...
ReplyStrategy
Enumeration that defines the available reply "modes".
Small helper structure which encapsulates the KMime::Message created when creating a reply,...
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Mar 22 2023 04:07:14 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.