File indexing completed on 2024-05-26 05:28:09

0001 /* Copyright (C) 2014 - 2015 Stephan Platz <trojita@paalsteek.de>
0002    Copyright (C) 2006 - 2016 Jan Kundrát <jkt@kde.org>
0003 
0004    This file is part of the Trojita Qt IMAP e-mail client,
0005    http://trojita.flaska.net/
0006 
0007    This program is free software; you can redistribute it and/or
0008    modify it under the terms of the GNU General Public License as
0009    published by the Free Software Foundation; either version 2 of
0010    the License or (at your option) version 3 or any later version
0011    accepted by the membership of KDE e.V. (or its successor approved
0012    by the membership of KDE e.V.), which shall act as a proxy
0013    defined in Section 14 of version 3 of the license.
0014 
0015    This program is distributed in the hope that it will be useful,
0016    but WITHOUT ANY WARRANTY; without even the implied warranty of
0017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018    GNU General Public License for more details.
0019 
0020    You should have received a copy of the GNU General Public License
0021    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022 */
0023 
0024 #ifndef CRYPTOGRAPHY_MESSAGEPART_H
0025 #define CRYPTOGRAPHY_MESSAGEPART_H
0026 
0027 #include <memory>
0028 #include <QModelIndex>
0029 #include <QUrl>
0030 #include <map>
0031 
0032 //#define MIME_TREE_DEBUG
0033 
0034 #ifdef MIME_TREE_DEBUG
0035 class QDebug;
0036 #endif
0037 
0038 namespace Imap {
0039 namespace Message {
0040 class Envelope;
0041 }
0042 }
0043 
0044 namespace Cryptography {
0045 class MessageModel;
0046 
0047 class MessagePart
0048 {
0049 protected:
0050     /** @short Availability of an item */
0051     enum class FetchingState {
0052         NONE, /**< @short No attempt to decrypt/load an item has been made yet */
0053         UNAVAILABLE, /**< @short Item could not be decrypted/loaded */
0054         LOADING, /**< @short Decryption/Loading of an item is already scheduled */
0055         DONE /**< @short Item is available right now */
0056     };
0057 
0058 public:
0059     using Ptr = std::unique_ptr<MessagePart>;
0060 
0061     MessagePart(MessagePart *parent, const int row);
0062     virtual ~MessagePart();
0063 
0064     virtual void fetchChildren(MessageModel *model) = 0;
0065 
0066     MessagePart* parent() const;
0067     int row() const;
0068     MessagePart *child(MessageModel *model, int row, int column) const;
0069     virtual int rowCount(MessageModel *model) const;
0070     virtual int columnCount(MessageModel *model) const;
0071 
0072     virtual QVariant data(int role) const = 0;
0073 
0074     void setSpecialParts(Ptr headerPart, Ptr textPart, Ptr mimePart, Ptr rawPart);
0075 
0076 protected:
0077 #ifdef MIME_TREE_DEBUG
0078     QByteArray dump(const int nestingLevel) const;
0079 #endif
0080 
0081     FetchingState m_childrenState;
0082     MessagePart *m_parent;
0083     std::map<int, MessagePart::Ptr> m_children;
0084     Ptr m_headerPart, m_textPart, m_mimePart, m_rawPart;
0085     int m_row;
0086 
0087     friend class MessageModel;
0088     friend class TopLevelMessage; // due to that lambda in TopLevelMessage::fetchChildren
0089 #ifdef MIME_TREE_DEBUG
0090     friend QDebug operator<<(QDebug dbg, const MessagePart &part);
0091 private:
0092     virtual QByteArray dumpLocalInfo() const = 0;
0093 #endif
0094 };
0095 
0096 #ifdef MIME_TREE_DEBUG
0097 QDebug operator<<(QDebug dbg, const MessagePart &part);
0098 #endif
0099 
0100 /** @short Represent the root of the message with a valid index
0101 
0102 This is a werid class, but the rest of the code really wants to operate on valid indexes, including the message's root.
0103 */
0104 class TopLevelMessage : public MessagePart
0105 {
0106 public:
0107     TopLevelMessage(const QModelIndex &messageRoot, MessageModel *model);
0108     ~TopLevelMessage();
0109 
0110     void fetchChildren(MessageModel *model) override;
0111     QVariant data(int role) const override;
0112 private:
0113 #ifdef MIME_TREE_DEBUG
0114     QByteArray dumpLocalInfo() const override;
0115 #endif
0116 
0117     QPersistentModelIndex m_root;
0118     MessageModel *m_model;
0119 };
0120 
0121 class ProxyMessagePart : public MessagePart
0122 {
0123 public:
0124     ProxyMessagePart(MessagePart *parent, const int row, const QModelIndex &sourceIndex, MessageModel *model);
0125     ~ProxyMessagePart();
0126 
0127     void fetchChildren(MessageModel *model) override;
0128 
0129     QVariant data(int role) const override;
0130 
0131 private:
0132 #ifdef MIME_TREE_DEBUG
0133     QByteArray dumpLocalInfo() const override;
0134 #endif
0135 
0136     QPersistentModelIndex m_sourceIndex;
0137     MessageModel *m_model;
0138 };
0139 
0140 class LocalMessagePart : public MessagePart
0141 {
0142 public:
0143     LocalMessagePart(MessagePart *parent, const int row, const QByteArray &mimetype);
0144     ~LocalMessagePart();
0145 
0146     void fetchChildren(MessageModel *model) override;
0147 
0148     QVariant data(int role) const override;
0149 
0150     void setData(const QByteArray &data);
0151 
0152     void setCharset(const QByteArray &charset);
0153     void setContentFormat(const QByteArray &format);
0154     void setDelSp(const QByteArray &delSp);
0155     void setFilename(const QString &filename);
0156     void setTransferEncoding(const QByteArray &transferEncoding);
0157     void setBodyFldId(const QByteArray &bodyFldId);
0158     void setBodyDisposition(const QByteArray &bodyDisposition);
0159     void setMultipartRelatedStartPart(const QByteArray &startPart);
0160     void setOctets(uint octets);
0161     void setEnvelope(std::unique_ptr<Imap::Message::Envelope> envelope);
0162     void setHdrReferences(const QList<QByteArray> &references);
0163     void setHdrListPost(const QList<QUrl> &listPost);
0164     void setHdrListPostNo(const bool listPostNo);
0165     void setBodyFldParam(const QMap<QByteArray, QByteArray> &bodyFldParam);
0166 
0167     void setChild(int row, Ptr part);
0168 
0169 private:
0170     bool isTopLevelMultipart() const;
0171     QByteArray partId() const;
0172     QByteArray pathToPart() const;
0173 #ifdef MIME_TREE_DEBUG
0174     QByteArray dumpLocalInfo() const override;
0175 #endif
0176 
0177 protected:
0178     FetchingState m_localState;
0179     std::unique_ptr<Imap::Message::Envelope> m_envelope;
0180     QList<QByteArray> m_hdrReferences;
0181     QList<QUrl> m_hdrListPost;
0182     bool m_hdrListPostNo;
0183     QByteArray m_charset;
0184     QByteArray m_contentFormat;
0185     QByteArray m_delSp;
0186     QByteArray m_data;
0187     QByteArray m_mimetype;
0188     QByteArray m_transferEncoding;
0189     QByteArray m_bodyFldId;
0190     QByteArray m_bodyDisposition;
0191     QByteArray m_multipartRelatedStartPart;
0192     QMap<QByteArray, QByteArray> m_bodyFldParam;
0193     QString m_filename;
0194     uint m_octets;
0195 
0196     friend class MessageModel;
0197 };
0198 
0199 }
0200 
0201 #endif /* CRYPTOGRAPHY_MESSAGEPART_H */