Messagelib

mimetreeparser/src/messagepart.h
1 /*
2  SPDX-FileCopyrightText: 2015 Sandro KnauƟ <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #ifndef MIMETREEPARSER_MESSAGEPART_H
8 #define MIMETREEPARSER_MESSAGEPART_H
9 
10 #include "mimetreeparser_export.h"
11 
12 #include "mimetreeparser/bodypartformatter.h"
13 #include "mimetreeparser/util.h"
14 
15 #include <KMime/Message>
16 
17 #include <gpgme++/verificationresult.h>
18 #include <gpgme++/decryptionresult.h>
19 #include <gpgme++/importresult.h>
20 #include <gpgme++/key.h>
21 
22 #include <QString>
23 #include <QSharedPointer>
24 
25 #include <memory>
26 
27 class QTextCodec;
28 
29 namespace GpgME {
30 class ImportResult;
31 }
32 
33 namespace QGpgME {
34 class Protocol;
35 }
36 
37 namespace KMime {
38 class Content;
39 }
40 
41 namespace MimeTreeParser {
42 class CryptoBodyPartMemento;
43 class MessagePartPrivate;
44 namespace Interface {
45 class ObjectTreeSource;
46 }
50 class MIMETREEPARSER_EXPORT MessagePart : public QObject
51 {
52  Q_OBJECT
53  Q_PROPERTY(QString plaintextContent READ plaintextContent)
54  Q_PROPERTY(QString htmlContent READ htmlContent)
55  Q_PROPERTY(bool isAttachment READ isAttachment)
56  Q_PROPERTY(bool root READ isRoot)
57  Q_PROPERTY(bool isHtml READ isHtml)
58  Q_PROPERTY(bool isImage READ isImage CONSTANT)
59  Q_PROPERTY(bool neverDisplayInline READ neverDisplayInline CONSTANT)
60  Q_PROPERTY(QString attachmentIndex READ attachmentIndex CONSTANT)
61  Q_PROPERTY(QString link READ attachmentLink CONSTANT)
62 public:
64  MessagePart(ObjectTreeParser *otp, const QString &text);
65  ~MessagePart();
66 
67  void setParentPart(MessagePart *parentPart);
68  MessagePart *parentPart() const;
69 
70  virtual Q_REQUIRED_RESULT QString text() const;
71  void setText(const QString &text);
72 
73  virtual Q_REQUIRED_RESULT QString plaintextContent() const;
74  virtual Q_REQUIRED_RESULT QString htmlContent() const;
75 
79  KMime::Content *content() const;
80  void setContent(KMime::Content *node);
81 
86  KMime::Content *attachmentContent() const;
87  void setAttachmentContent(KMime::Content *node);
88  Q_REQUIRED_RESULT bool isAttachment() const;
90  Q_REQUIRED_RESULT QString attachmentIndex() const;
92  Q_REQUIRED_RESULT QString attachmentLink() const;
93 
97  Q_REQUIRED_RESULT QString makeLink(const QString &path) const;
98 
99  void setIsRoot(bool root);
100  Q_REQUIRED_RESULT bool isRoot() const;
101 
102  virtual bool isHtml() const;
103 
104  Q_REQUIRED_RESULT bool neverDisplayInline() const;
105  void setNeverDisplayInline(bool displayInline);
106  Q_REQUIRED_RESULT bool isImage() const;
107  void setIsImage(bool image);
108 
109  PartMetaData *partMetaData() const;
110 
111  Interface::BodyPartMemento *memento() const;
112  void setMemento(Interface::BodyPartMemento *memento);
113 
114  /* only a function that should be removed if the refactoring is over */
115  virtual void fix() const;
116 
117  void appendSubPart(const MessagePart::Ptr &messagePart);
118  const QVector<MessagePart::Ptr> &subParts() const;
119  Q_REQUIRED_RESULT bool hasSubParts() const;
120  void clearSubParts();
121 
122  Interface::ObjectTreeSource *source() const;
123  NodeHelper *nodeHelper() const;
124 
125  virtual Q_REQUIRED_RESULT bool hasHeader(const char *header) const;
126  virtual KMime::Headers::Base *header(const char *header) const;
127 
128 protected:
129  void parseInternal(KMime::Content *node, bool onlyOneMimePart);
130  QString renderInternalText() const;
131 
132  ObjectTreeParser *mOtp = nullptr;
133 
134 private:
135  std::unique_ptr<MessagePartPrivate> d;
136 };
140 class MIMETREEPARSER_EXPORT MimeMessagePart : public MessagePart
141 {
142  Q_OBJECT
143 public:
145  MimeMessagePart(MimeTreeParser::ObjectTreeParser *otp, KMime::Content *node, bool onlyOneMimePart);
146  ~MimeMessagePart() override;
147 
148  Q_REQUIRED_RESULT QString text() const override;
149 
150  Q_REQUIRED_RESULT QString plaintextContent() const override;
151  Q_REQUIRED_RESULT QString htmlContent() const override;
152 private:
153  bool mOnlyOneMimePart;
154 };
158 class MIMETREEPARSER_EXPORT MessagePartList : public MessagePart
159 {
160  Q_OBJECT
161 public:
164  ~MessagePartList() override;
165 
166  Q_REQUIRED_RESULT QString text() const override;
167 
168  Q_REQUIRED_RESULT QString plaintextContent() const override;
169  Q_REQUIRED_RESULT QString htmlContent() const override;
170 };
171 
172 enum IconType {
173  NoIcon = 0,
174  IconExternal,
175  IconInline
176 };
180 class MIMETREEPARSER_EXPORT TextMessagePart : public MessagePartList
181 {
182  Q_OBJECT
183  Q_PROPERTY(bool showLink READ showLink CONSTANT)
184  Q_PROPERTY(bool isFirstTextPart READ isFirstTextPart CONSTANT)
185  Q_PROPERTY(bool hasLabel READ hasLabel CONSTANT)
186  Q_PROPERTY(QString label READ label CONSTANT)
187  Q_PROPERTY(QString comment READ comment CONSTANT)
188 public:
190  TextMessagePart(MimeTreeParser::ObjectTreeParser *otp, KMime::Content *node, bool decryptMessage);
191  ~TextMessagePart() override;
192 
193  KMMsgSignatureState signatureState() const;
194  KMMsgEncryptionState encryptionState() const;
195 
196  Q_REQUIRED_RESULT bool decryptMessage() const;
197 
198  Q_REQUIRED_RESULT bool showLink() const;
199  Q_REQUIRED_RESULT bool isFirstTextPart() const;
200  Q_REQUIRED_RESULT bool hasLabel() const;
201 
203  Q_REQUIRED_RESULT QString label() const;
205  Q_REQUIRED_RESULT QString comment() const;
207  Q_REQUIRED_RESULT QString temporaryFilePath() const;
208 
209 private:
210  void parseContent();
211 
212  KMMsgSignatureState mSignatureState;
213  KMMsgEncryptionState mEncryptionState;
214  bool mDecryptMessage;
215 };
219 class MIMETREEPARSER_EXPORT AttachmentMessagePart : public TextMessagePart
220 {
221  Q_OBJECT
222 public:
225  ~AttachmentMessagePart() override;
226 };
230 class MIMETREEPARSER_EXPORT HtmlMessagePart : public MessagePart
231 {
232  Q_OBJECT
233 public:
236  ~HtmlMessagePart() override;
237 
238  Q_REQUIRED_RESULT QString text() const override;
239  Q_REQUIRED_RESULT QString plaintextContent() const override;
240 
241  void fix() const override;
242  Q_REQUIRED_RESULT bool isHtml() const override;
243 
244  Q_REQUIRED_RESULT QString bodyHtml() const;
245 
246 private:
248  QString mBodyHTML;
249  QByteArray mCharset;
250 };
254 class MIMETREEPARSER_EXPORT AlternativeMessagePart : public MessagePart
255 {
256  Q_OBJECT
257 public:
260  ~AlternativeMessagePart() override;
261 
262  Q_REQUIRED_RESULT QString text() const override;
263 
264  Q_REQUIRED_RESULT Util::HtmlMode preferredMode() const;
265  void setPreferredMode(Util::HtmlMode preferredMode);
266 
267  Q_REQUIRED_RESULT bool isHtml() const override;
268 
269  Q_REQUIRED_RESULT QString plaintextContent() const override;
270  Q_REQUIRED_RESULT QString htmlContent() const override;
271 
272  Q_REQUIRED_RESULT QList<Util::HtmlMode> availableModes();
273 
274  void fix() const override;
275 
276  const QMap<Util::HtmlMode, MimeMessagePart::Ptr> &childParts() const;
277 
278 private:
279  Util::HtmlMode mPreferredMode;
280 
283 };
287 class MIMETREEPARSER_EXPORT CertMessagePart : public MessagePart
288 {
289  Q_OBJECT
290 public:
292  CertMessagePart(MimeTreeParser::ObjectTreeParser *otp, KMime::Content *node, const QGpgME::Protocol *cryptoProto, bool autoImport);
293  ~CertMessagePart() override;
294 
295  Q_REQUIRED_RESULT QString text() const override;
296 
297  const GpgME::ImportResult &importResult() const;
298 
299 private:
300  bool mAutoImport;
301  GpgME::ImportResult mImportResult;
302  const QGpgME::Protocol *mCryptoProto;
303 };
307 class MIMETREEPARSER_EXPORT EncapsulatedRfc822MessagePart : public MessagePart
308 {
309  Q_OBJECT
310 public:
313  ~EncapsulatedRfc822MessagePart() override;
314 
315  Q_REQUIRED_RESULT QString text() const override;
316 
317  void fix() const override;
318 
319  const KMime::Message::Ptr message() const;
320 
321 private:
322  const KMime::Message::Ptr mMessage;
323 };
327 class MIMETREEPARSER_EXPORT EncryptedMessagePart : public MessagePart
328 {
329  Q_OBJECT
330  Q_PROPERTY(bool decryptMessage READ decryptMessage WRITE setDecryptMessage)
331  Q_PROPERTY(bool isEncrypted READ isEncrypted)
332  Q_PROPERTY(bool isNoSecKey READ isNoSecKey)
333  Q_PROPERTY(bool passphraseError READ passphraseError)
334 public:
336  EncryptedMessagePart(ObjectTreeParser *otp, const QString &text, const QGpgME::Protocol *cryptoProto, const QString &fromAddress, KMime::Content *node);
337 
338  ~EncryptedMessagePart() override;
339 
340  Q_REQUIRED_RESULT QString text() const override;
341 
342  void setDecryptMessage(bool decrypt);
343  Q_REQUIRED_RESULT bool decryptMessage() const;
344 
345  void setIsEncrypted(bool encrypted);
346  Q_REQUIRED_RESULT bool isEncrypted() const;
347 
348  Q_REQUIRED_RESULT bool isDecryptable() const;
349 
350  Q_REQUIRED_RESULT bool isNoSecKey() const;
351  Q_REQUIRED_RESULT bool passphraseError() const;
352 
353  void startDecryption(const QByteArray &text, const QTextCodec *aCodec);
354  void startDecryption(KMime::Content *data = nullptr);
355 
356  void setMementoName(const QByteArray &name);
357  Q_REQUIRED_RESULT QByteArray mementoName() const;
358 
359 
360  Q_REQUIRED_RESULT QString plaintextContent() const override;
361  Q_REQUIRED_RESULT QString htmlContent() const override;
362 
363  const QGpgME::Protocol *cryptoProto() const;
364  Q_REQUIRED_RESULT QString fromAddress() const;
365 
366  const std::vector<std::pair<GpgME::DecryptionResult::Recipient, GpgME::Key> > &decryptRecipients() const;
367 
368  Q_REQUIRED_RESULT bool hasHeader(const char *header) const override;
369  KMime::Headers::Base *header(const char *header) const override;
370 
371  QByteArray mDecryptedData;
372 private:
377  Q_REQUIRED_RESULT bool okDecryptMIME(KMime::Content &data);
378 
379 protected:
380  bool mPassphraseError;
381  bool mNoSecKey;
382  bool mDecryptMessage;
383  const QGpgME::Protocol *mCryptoProto;
384  QString mFromAddress;
385  QByteArray mVerifiedText;
386  QByteArray mMementoName;
387  std::vector<std::pair<GpgME::DecryptionResult::Recipient, GpgME::Key> > mDecryptRecipients;
388 
389  friend class EncryptedBodyPartFormatter;
390 };
394 class MIMETREEPARSER_EXPORT SignedMessagePart : public MessagePart
395 {
396  Q_OBJECT
397  Q_PROPERTY(bool isSigned READ isSigned)
398 public:
400  SignedMessagePart(ObjectTreeParser *otp, const QString &text, const QGpgME::Protocol *cryptoProto, const QString &fromAddress, KMime::Content *node);
401 
402  ~SignedMessagePart() override;
403 
404  void setIsSigned(bool isSigned);
405  Q_REQUIRED_RESULT bool isSigned() const;
406 
407  void startVerification(const QByteArray &text, const QTextCodec *aCodec);
408  void startVerificationDetached(const QByteArray &text, KMime::Content *textNode, const QByteArray &signature);
409 
410  void setMementoName(const QByteArray &name);
411  Q_REQUIRED_RESULT QByteArray mementoName() const;
412 
413  QByteArray mDecryptedData;
414  std::vector<GpgME::Signature> mSignatures;
415 
416  Q_REQUIRED_RESULT QString plaintextContent() const override;
417  Q_REQUIRED_RESULT QString htmlContent() const override;
418 
419  const QGpgME::Protocol *cryptoProto() const;
420  Q_REQUIRED_RESULT QString fromAddress() const;
421 
422  Q_REQUIRED_RESULT bool hasHeader(const char *header) const override;
423  KMime::Headers::Base *header(const char *header) const override;
424 private:
430  Q_REQUIRED_RESULT bool okVerify(const QByteArray &data, const QByteArray &signature, KMime::Content *textNode);
431 
432  void sigStatusToMetaData();
433 
434  void setVerificationResult(const CryptoBodyPartMemento *m, KMime::Content *textNode);
435 protected:
436  const QGpgME::Protocol *mCryptoProto;
437  QString mFromAddress;
438  QByteArray mVerifiedText;
439  QByteArray mMementoName;
440 
441  friend EncryptedMessagePart;
442 };
443 }
444 
445 #endif //__MIMETREEPARSER_MESSAGEPART_H
HtmlMode
Describes the type of the displayed message.
Interface for object tree sources.
interface of classes that implement status for BodyPartFormatters.
Definition: bodypart.h:31
Parses messages and generates HTML display code out of them.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat Sep 19 2020 23:12:50 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.