Messagelib

composerviewbase.h
1 /*
2  SPDX-FileCopyrightText: 2010 Klaralvdalens Datakonsult AB, a KDAB Group company, [email protected]
3  SPDX-FileCopyrightText: 2010 Leo Franchi <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #pragma once
9 
10 #include "MessageComposer/Recipient"
11 #include "messagecomposer/messagesender.h"
12 #include "messagecomposer_export.h"
13 #include <Akonadi/Collection>
14 #include <Akonadi/Item>
15 #include <KMime/Message>
16 
17 #include <MessageComposer/NearExpiryChecker>
18 
19 #include <Libkleo/Enum>
20 #include <QObject>
21 #include <QUrl>
22 #include <QVector>
23 
24 class QTimer;
25 class KJob;
26 class QWidget;
27 
28 class ComposerViewBaseTest;
29 
30 namespace Sonnet
31 {
32 class DictionaryComboBox;
33 }
34 
35 namespace Akonadi
36 {
37 class CollectionComboBox;
38 }
39 
40 namespace MailTransport
41 {
42 class TransportComboBox;
43 }
44 
45 namespace KIdentityManagement
46 {
47 class IdentityCombo;
48 class Identity;
49 class IdentityManager;
50 }
51 
52 namespace Kleo
53 {
54 class KeyResolver;
55 }
56 
57 namespace MessageComposer
58 {
59 class RecipientsEditor;
60 class RichTextComposerNg;
61 class InfoPart;
62 class GlobalPart;
63 class Composer;
64 class AttachmentControllerBase;
65 class AttachmentModel;
66 class SignatureController;
67 class SendLaterInfo;
68 /**
69  * @brief The ComposerViewBase class
70  */
71 class MESSAGECOMPOSER_EXPORT ComposerViewBase : public QObject
72 {
73  Q_OBJECT
74 public:
75  explicit ComposerViewBase(QObject *parent = nullptr, QWidget *widget = nullptr);
76  ~ComposerViewBase() override;
77 
78  enum Confirmation {
79  LetUserConfirm,
80  NoConfirmationNeeded,
81  };
82  enum MissingAttachment {
83  NoMissingAttachmentFound,
84  FoundMissingAttachmentAndSending,
85  FoundMissingAttachmentAndAddedAttachment,
86  FoundMissingAttachmentAndCancel,
87  };
88 
89  enum FailedType {
90  Sending,
91  AutoSave,
92  };
93 
94  /**
95  * Set the message to be opened in the composer window, and set the internal data structures to
96  * keep track of it.
97  */
98  void setMessage(const KMime::Message::Ptr &newMsg, bool allowDecryption);
99 
100  void updateTemplate(const KMime::Message::Ptr &msg);
101 
102  /**
103  * Send the message with the specified method, saving it in the specified folder.
104  */
105  void send(MessageComposer::MessageSender::SendMethod method, MessageComposer::MessageSender::SaveIn saveIn, bool checkMailDispatcher = true);
106 
107  /**
108  * Returns true if there is at least one composer job running.
109  */
110  Q_REQUIRED_RESULT bool isComposing() const;
111 
112  /**
113  * Add the given attachment to the message.
114  */
115  void addAttachment(const QUrl &url, const QString &comment, bool sync);
116  void addAttachment(const QString &name, const QString &filename, const QString &charset, const QByteArray &data, const QByteArray &mimeType);
117  void addAttachmentPart(KMime::Content *part);
118 
119  void fillComposer(MessageComposer::Composer *composer);
120 
121  /**
122  * Header fields in recipients editor.
123  */
124  Q_REQUIRED_RESULT QString to() const;
125  Q_REQUIRED_RESULT QString cc() const;
126  Q_REQUIRED_RESULT QString bcc() const;
127  Q_REQUIRED_RESULT QString from() const;
128  Q_REQUIRED_RESULT QString replyTo() const;
129  Q_REQUIRED_RESULT QString subject() const;
130 
131  Q_REQUIRED_RESULT const KIdentityManagement::Identity &currentIdentity() const;
132  Q_REQUIRED_RESULT bool autocryptEnabled() const;
133 
134  /**
135  * The following are for setting the various options and widgets in the
136  * composer.
137  */
138  void setAttachmentModel(MessageComposer::AttachmentModel *model);
139  Q_REQUIRED_RESULT MessageComposer::AttachmentModel *attachmentModel();
140 
141  void setAttachmentController(MessageComposer::AttachmentControllerBase *controller);
142  Q_REQUIRED_RESULT MessageComposer::AttachmentControllerBase *attachmentController();
143 
144  void setRecipientsEditor(MessageComposer::RecipientsEditor *recEditor);
145  Q_REQUIRED_RESULT MessageComposer::RecipientsEditor *recipientsEditor();
146 
147  void setSignatureController(MessageComposer::SignatureController *sigController);
148  Q_REQUIRED_RESULT MessageComposer::SignatureController *signatureController();
149 
150  void setIdentityCombo(KIdentityManagement::IdentityCombo *identCombo);
151  Q_REQUIRED_RESULT KIdentityManagement::IdentityCombo *identityCombo();
152 
153  void setIdentityManager(KIdentityManagement::IdentityManager *identMan);
154  Q_REQUIRED_RESULT KIdentityManagement::IdentityManager *identityManager();
155 
156  void setEditor(MessageComposer::RichTextComposerNg *editor);
157  Q_REQUIRED_RESULT MessageComposer::RichTextComposerNg *editor() const;
158 
159  void setTransportCombo(MailTransport::TransportComboBox *transpCombo);
160  Q_REQUIRED_RESULT MailTransport::TransportComboBox *transportComboBox() const;
161 
162  void setFccCombo(Akonadi::CollectionComboBox *fcc);
163  Q_REQUIRED_RESULT Akonadi::CollectionComboBox *fccCombo() const;
164  void setFcc(const Akonadi::Collection &id);
165 
166  Q_REQUIRED_RESULT Sonnet::DictionaryComboBox *dictionary() const;
167  void setDictionary(Sonnet::DictionaryComboBox *dictionary);
168 
169  /**
170  * Widgets for editing differ in client classes, so
171  * values are set before sending.
172  */
173  void setFrom(const QString &from);
174  void setSubject(const QString &subject);
175 
176  /**
177  * The following are various settings the user can modify when composing a message. If they are not set,
178  * the default values will be used.
179  */
180  void setCryptoOptions(bool sign, bool encrypt, Kleo::CryptoMessageFormat format, bool neverEncryptDrafts = false);
181  void setCharsets(const QVector<QByteArray> &charsets);
182  void setMDNRequested(bool mdnRequested);
183  void setUrgent(bool urgent);
184 
185  void setAutoSaveInterval(int interval);
186  void setCustomHeader(const QMap<QByteArray, QString> &customHeader);
187 
188  /**
189  * Enables/disables autosaving depending on the value of the autosave
190  * interval.
191  */
192  void updateAutoSave();
193 
194  /**
195  * Sets the filename to use when autosaving something. This is used when the client recovers
196  * the autosave files: It calls this method, so that the composer uses the same filename again.
197  * That way, the recovered autosave file is properly cleaned up in cleanupAutoSave():
198  */
199  void setAutoSaveFileName(const QString &fileName);
200 
201  /**
202  * Stop autosaving and delete the autosaved message.
203  */
204  void cleanupAutoSave();
205 
206  void setParentWidgetForGui(QWidget *);
207 
208  /**
209  * Check if the mail has references to attachments, but no attachments are added to it.
210  * If missing attachments are found, a dialog to add new attachments is shown.
211  * @param attachmentKeywords a list with the keywords that indicate an attachment should be present
212  * @return NoMissingAttachmentFound, if there is attachment in email
213  * FoundMissingAttachmentAndCancelSending, if mail might miss attachment but sending
214  * FoundMissingAttachmentAndAddedAttachment, if mail might miss attachment and we added an attachment
215  * FoundMissingAttachmentAndCancel, if mail might miss attachment and cancel sending
216  */
217  Q_REQUIRED_RESULT ComposerViewBase::MissingAttachment checkForMissingAttachments(const QStringList &attachmentKeywords);
218 
219  Q_REQUIRED_RESULT bool hasMissingAttachments(const QStringList &attachmentKeywords);
220 
221  void setSendLaterInfo(SendLaterInfo *info);
222  Q_REQUIRED_RESULT SendLaterInfo *sendLaterInfo() const;
223  void saveMailSettings();
224 
225  Q_REQUIRED_RESULT QDate followUpDate() const;
226  void setFollowUpDate(const QDate &followUpDate);
227 
228  void clearFollowUp();
229 
230  Q_REQUIRED_RESULT Akonadi::Collection followUpCollection() const;
231  void setFollowUpCollection(const Akonadi::Collection &followUpCollection);
232 
233  Q_REQUIRED_RESULT KMime::Message::Ptr msg() const;
234 
235  Q_REQUIRED_RESULT bool requestDeleveryConfirmation() const;
236  void setRequestDeleveryConfirmation(bool requestDeleveryConfirmation);
237 
238  Q_REQUIRED_RESULT NearExpiryChecker::Ptr nearExpiryChecker();
239 
240 public Q_SLOTS:
241  void identityChanged(const KIdentityManagement::Identity &ident, const KIdentityManagement::Identity &oldIdent, bool msgCleared = false);
242 
243  /**
244  * Save the message.
245  */
246  void autoSaveMessage();
247 
248 Q_SIGNALS:
249  /**
250  * Message sending completed successfully.
251  */
252  void sentSuccessfully(Akonadi::Item::Id id);
253  /**
254  * Message sending failed with given error message.
255  */
256  void failed(const QString &errorMessage, MessageComposer::ComposerViewBase::FailedType type = Sending);
257 
258  /**
259  * The composer was modified. This can happen behind the users' back
260  * when, for example, and autosaved message was recovered.
261  */
262  void modified(bool isModified);
263 
264  /**
265  * Enabling or disabling HTML in the editor is affected
266  * by various client options, so when that would otherwise happen,
267  * hand it off to the client to enact it for real.
268  */
269  void disableHtml(MessageComposer::ComposerViewBase::Confirmation);
270  void enableHtml();
271  void tooManyRecipient(bool);
272 
273 private Q_SLOTS:
274  void slotEmailAddressResolved(KJob *);
275  void slotSendComposeResult(KJob *);
276  void slotQueueResult(KJob *job);
277  void slotCreateItemResult(KJob *);
278  void slotAutoSaveComposeResult(KJob *job);
279  void slotFccCollectionCheckResult(KJob *job);
280  void slotSaveMessage(KJob *job);
281 
282 private:
283  Q_REQUIRED_RESULT Akonadi::Collection defaultSpecialTarget() const;
284  /**
285  * Searches the mime tree, where root is the root node, for embedded images,
286  * extracts them froom the body and adds them to the editor.
287  */
288  void collectImages(KMime::Content *root);
289  Q_REQUIRED_RESULT bool inlineSigningEncryptionSelected() const;
290  /**
291  * Applies the user changes to the message object of the composer
292  * and signs/encrypts the message if activated.
293  * Disables the controls of the composer window.
294  */
295  void readyForSending();
296 
297  enum RecipientExpansion {
298  UseExpandedRecipients,
299  UseUnExpandedRecipients,
300  };
301  void fillComposer(MessageComposer::Composer *composer, ComposerViewBase::RecipientExpansion expansion, bool autoresize);
302  Q_REQUIRED_RESULT QVector<MessageComposer::Composer *> generateCryptoMessages(bool &wasCanceled);
303  void fillGlobalPart(MessageComposer::GlobalPart *globalPart);
304  void fillInfoPart(MessageComposer::InfoPart *part, RecipientExpansion expansion);
305  void queueMessage(const KMime::Message::Ptr &message, MessageComposer::Composer *composer);
306  void saveMessage(const KMime::Message::Ptr &message, MessageComposer::MessageSender::SaveIn saveIn);
307  void saveRecentAddresses(const KMime::Message::Ptr &ptr);
308  void updateRecipients(const KIdentityManagement::Identity &ident, const KIdentityManagement::Identity &oldIdent, MessageComposer::Recipient::Type type);
309 
310  void markAllAttachmentsForSigning(bool sign);
311  void markAllAttachmentsForEncryption(bool encrypt);
312  bool determineWhetherToSign(bool doSignCompletely, Kleo::KeyResolver *keyResolver, bool signSomething, bool &result, bool &canceled);
313  bool determineWhetherToEncrypt(bool doEncryptCompletely,
314  Kleo::KeyResolver *keyResolver,
315  bool encryptSomething,
316  bool signSomething,
317  bool &result,
318  bool &canceled);
319 
320  /**
321  * Writes out autosave data to the disk from the KMime::Message message.
322  * Also appends the msgNum to the filename as a message can have a number of
323  * KMime::Messages
324  */
325  void writeAutoSaveToDisk(const KMime::Message::Ptr &message);
326 
327  /**
328  * Returns the autosave interval in milliseconds (as needed for QTimer).
329  */
330  int autoSaveInterval() const;
331 
332  /**
333  * Initialize autosaving (timer and filename).
334  */
335  void initAutoSave();
336  void addFollowupReminder(const QString &messageId);
337  void addSendLaterItem(const Akonadi::Item &item);
338 
339  bool addKeysToContext(const QString &gnupgHome,
340  const QVector<QPair<QStringList, std::vector<GpgME::Key>>> &data,
341  const std::map<QByteArray, QString> &autocryptMap);
342 
343  void setAkonadiLookupEnabled(bool akonadiLookupEnabled);
344 
345  KMime::Message::Ptr m_msg;
346  MessageComposer::AttachmentControllerBase *m_attachmentController = nullptr;
347  MessageComposer::AttachmentModel *m_attachmentModel = nullptr;
348  MessageComposer::SignatureController *m_signatureController = nullptr;
349  MessageComposer::RecipientsEditor *m_recipientsEditor = nullptr;
350  KIdentityManagement::IdentityCombo *m_identityCombo = nullptr;
351  KIdentityManagement::IdentityManager *m_identMan = nullptr;
352  MessageComposer::RichTextComposerNg *m_editor = nullptr;
353  MailTransport::TransportComboBox *m_transport = nullptr;
354  Sonnet::DictionaryComboBox *m_dictionary = nullptr;
355  Akonadi::CollectionComboBox *m_fccCombo = nullptr;
356  Akonadi::Collection m_fccCollection;
357  QWidget *m_parentWidget = nullptr;
358 
359  // List of active composer jobs. For example, saving as draft, autosaving and printing
360  // all create a composer, which is added to this list as long as it is active.
361  // Used mainly to prevent closing the window if a composer is active
363 
364  bool m_sign = false;
365  bool m_encrypt = false;
366  bool m_neverEncrypt = false;
367  bool m_mdnRequested = false;
368  bool m_urgent = false;
369  bool m_requestDeleveryConfirmation = false;
370  bool m_akonadiLookupEnabled = true;
371  Kleo::CryptoMessageFormat m_cryptoMessageFormat;
372  QString mExpandedFrom;
373  QString m_from;
374  QString m_subject;
375  QStringList mExpandedTo, mExpandedCc, mExpandedBcc, mExpandedReplyTo;
376  QVector<QByteArray> m_charsets;
377  QMap<QByteArray, QString> m_customHeader;
378 
379  int m_pendingQueueJobs = 0;
380 
381  QTimer *m_autoSaveTimer = nullptr;
382  QString m_autoSaveUUID;
383  bool m_autoSaveErrorShown = false; // Stops an error message being shown every time autosave is executed.
384  int m_autoSaveInterval;
385 
386  MessageComposer::MessageSender::SendMethod mSendMethod;
387  MessageComposer::MessageSender::SaveIn mSaveIn;
388 
389  NearExpiryChecker::Ptr mNearExpiryChecker;
390 
391  QDate mFollowUpDate;
392  Akonadi::Collection mFollowUpCollection;
393 
394  std::unique_ptr<SendLaterInfo> mSendLaterInfo;
395 
396  friend ComposerViewBaseTest;
397 };
398 } // namespace
Send later information.
Definition: sendlaterinfo.h:17
Composer
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
The RecipientsEditor class.
The SignatureController class Controls signature (the footer thing, not the crypto thing) operations ...
The Composer class.
Definition: composer.h:33
The ComposerViewBase class.
The GlobalPart class.
Definition: globalpart.h:19
KCALUTILS_EXPORT QString errorMessage(const KCalendarCore::Exception &exception)
The InfoPart class contains the message header.
Definition: infopart.h:21
The RichTextComposerNg class.
The AttachmentControllerBase class.
The AttachmentModel class.
QString message
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.