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

KDE's Doxygen guidelines are available online.