File indexing completed on 2024-11-24 04:52:57

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_MESSAGEMODEL_H
0025 #define CRYPTOGRAPHY_MESSAGEMODEL_H
0026 
0027 #include <QHash>
0028 #include <QPersistentModelIndex>
0029 
0030 #include "Cryptography/MessagePart.h"
0031 
0032 namespace Cryptography {
0033 
0034 class PartReplacer;
0035 class ProxyMessagePart;
0036 
0037 /** @short A model for a single message including raw and possibly decrypted message parts
0038  *
0039  * This model mimics the structure of a source model and has some provisions for adding custom
0040  * subtree items as replacements for arbitrary nodes (not just leaves). Internally, each node is
0041  * represented either by a LocalMessagePart (for those items which were added manually as
0042  * replacements) or ProxyMessagePart (for those items which are just proxies pointing back to the
0043  * original model, with unmodified position).
0044  *
0045  * This model makes a couple of assumptions. It doesn't support dynamic models at all; any removed
0046  * or added row/column/whatever in the source model will likely end up in a segfault.
0047  */
0048 class MessageModel: public QAbstractItemModel
0049 {
0050     Q_OBJECT
0051 
0052 public:
0053     MessageModel(QObject *parent, const QModelIndex &message);
0054 
0055     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
0056     QModelIndex parent(const QModelIndex &child) const override;
0057     int rowCount(const QModelIndex &parent) const override;
0058     int columnCount(const QModelIndex &parent) const override;
0059     QVariant data(const QModelIndex &index, int role) const override;
0060 
0061     QModelIndex message() const;
0062 
0063     /** @short Push a new complete subtree to become the only child of the indicated parent
0064 
0065     The @arg parent is required to not have any children at the time of call. The @arg child should
0066     form a standalone tree of MIME parts prepared in advance, and it will be pushed to the complete tree
0067     so that the root item of the added subtree becomes the first and only child of @arg parent. This
0068     means that @arg parent will have exactly one child when this function returns.
0069     */
0070     void insertSubtree(const QModelIndex &parent, Cryptography::MessagePart::Ptr tree);
0071 
0072     /** @short Overload, for taking several items and making them new children at the same level */
0073     void insertSubtree(const QModelIndex &parent, std::vector<MessagePart::Ptr> &&parts);
0074 
0075     void replaceMeWithSubtree(const QModelIndex &parent, MessagePart *partToReplace, MessagePart::Ptr tree);
0076 
0077     /** @short Activate a custom MIME part handler/replacer */
0078     void registerPartHandler(std::shared_ptr<PartReplacer> module);
0079 
0080 #ifdef MIME_TREE_DEBUG
0081     void debugDump();
0082 #endif
0083 
0084 private slots:
0085     void mapDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
0086 
0087 signals:
0088     void error(const QModelIndex &parent, const QString &error, const QString &details);
0089 
0090 private:
0091     MessagePart *translatePtr(const QModelIndex &part) const;
0092 
0093     const QPersistentModelIndex m_message;
0094     QHash<QPersistentModelIndex, MessagePart*> m_map;
0095     MessagePart::Ptr m_rootPart;
0096     std::vector<std::shared_ptr<PartReplacer>> m_partHandlers;
0097     QMetaObject::Connection m_insertRows;
0098 
0099     friend class TopLevelMessage;
0100     friend class ProxyMessagePart;
0101 };
0102 }
0103 #endif /* CRYPTOGRAPHY_MESSAGEMODEL_H */