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

KDE's Doxygen guidelines are available online.