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

0001 /* Copyright (C) 2006 - 2014 Jan Kundrát <jkt@flaska.net>
0002 
0003    This file is part of the Trojita Qt IMAP e-mail client,
0004    http://trojita.flaska.net/
0005 
0006    This program is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU General Public License as
0008    published by the Free Software Foundation; either version 2 of
0009    the License or (at your option) version 3 or any later version
0010    accepted by the membership of KDE e.V. (or its successor approved
0011    by the membership of KDE e.V.), which shall act as a proxy
0012    defined in Section 14 of version 3 of the license.
0013 
0014    This program is distributed in the hope that it will be useful,
0015    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017    GNU General Public License for more details.
0018 
0019    You should have received a copy of the GNU General Public License
0020    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 
0023 #ifndef COMPOSER_COMPOSERATTACHMENTS_H
0024 #define COMPOSER_COMPOSERATTACHMENTS_H
0025 
0026 #include <QIODevice>
0027 #include <QPersistentModelIndex>
0028 #include <QPointer>
0029 #include <QSharedPointer>
0030 #include "Composer/ContentDisposition.h"
0031 
0032 namespace Imap {
0033 namespace Mailbox {
0034 
0035 class Model;
0036 class FullMessageCombiner;
0037 class TreeItemMessage;
0038 class TreeItemPart;
0039 
0040 }
0041 }
0042 
0043 namespace Composer {
0044 
0045 /** @short A generic item to be used as an attachment */
0046 class AttachmentItem {
0047 public:
0048     AttachmentItem();
0049     virtual ~AttachmentItem();
0050 
0051     virtual QString caption() const = 0;
0052     virtual QString tooltip() const = 0;
0053     virtual QByteArray mimeType() const = 0;
0054     virtual QByteArray contentDispositionHeader() const;
0055     virtual ContentDisposition contentDispositionMode() const;
0056     virtual bool setContentDispositionMode(const ContentDisposition contentDisposition);
0057     virtual QString contentDispositionFilename() const = 0;
0058     virtual bool setPreferredFileName(const QString &name) = 0;
0059 
0060     /** @short Return shared pointer to QIODevice which is ready to return data for this part
0061 
0062     The underlying QIODevice MUST NOT be stored for future use.  It is no longer valid after the source
0063     AttachmentItem is destroyed.
0064 
0065     The QIODevice MAY support only a single reading pass.  If the caller wants to read data multiple times, they should
0066     obtain another copy through calling rawData again.
0067 
0068     This function MAY return a null pointer if the data is not ready yet. Always use isAvailable() to make sure that
0069     the function will return correct data AND check the return value due to a possible TOCTOU issue.
0070 
0071     When the event loop is renetered, the QIODevice MAY become invalid and MUST NOT be used anymore.
0072 
0073     (I really, really like the RFC way of expression constraints :). )
0074     */
0075     virtual QSharedPointer<QIODevice> rawData() const = 0;
0076 
0077     /** @short Return true if the data are already available from a local cache */
0078     virtual bool isAvailableLocally() const = 0;
0079 
0080     enum class ContentTransferEncoding {
0081         SevenBit,
0082         EightBit,
0083         Binary,
0084         Base64,
0085         QuotedPrintable,
0086     };
0087     virtual ContentTransferEncoding suggestedCTE() const = 0;
0088 
0089     /** @short Returns a valid IMAP URL or an empty QByteArray if the data are not available from an IMAP server */
0090     virtual QByteArray imapUrl() const = 0;
0091 
0092     /** @short Express interest in having the attachment data available locally */
0093     virtual void preload() const = 0;
0094 
0095     /** @short Different sorts of possible attachments */
0096     typedef enum {
0097         ATTACHMENT_IMAP_MESSAGE,
0098         ATTACHMENT_IMAP_PART,
0099         ATTACHMENT_FILE
0100     } AttachmentKind;
0101 
0102     /** @short Describe the data for use in a drag-and-drop operation */
0103     virtual void asDroppableMimeData(QDataStream &stream) const = 0;
0104 
0105 protected:
0106     ContentDisposition m_contentDisposition;
0107 };
0108 
0109 /** @short Part of a message stored in an IMAP server */
0110 class ImapMessageAttachmentItem: public AttachmentItem {
0111 public:
0112     ImapMessageAttachmentItem(Imap::Mailbox::Model *model, const QString &mailbox, const uint uidValidity, const uint uid);
0113     ~ImapMessageAttachmentItem();
0114 
0115     QString caption() const override;
0116     QString tooltip() const override;
0117     QByteArray mimeType() const override;
0118     QString contentDispositionFilename() const override;
0119     bool setPreferredFileName(const QString &name) override;
0120     QSharedPointer<QIODevice> rawData() const override;
0121     bool isAvailableLocally() const override;
0122     ContentTransferEncoding suggestedCTE() const override;
0123     QByteArray imapUrl() const override;
0124     void preload() const override;
0125     void asDroppableMimeData(QDataStream &stream) const override;
0126 private:
0127     Imap::Mailbox::FullMessageCombiner *fullMessageCombiner;
0128     QPersistentModelIndex index;
0129     QString preferredName;
0130 };
0131 
0132 /** @short Part of a message stored in an IMAP server */
0133 class ImapPartAttachmentItem: public AttachmentItem {
0134 public:
0135     ImapPartAttachmentItem(Imap::Mailbox::Model *model, const QString &mailbox, const uint uidValidity, const uint uid, const QByteArray &trojitaPath);
0136     ~ImapPartAttachmentItem();
0137 
0138     QString caption() const override;
0139     QString tooltip() const override;
0140     QByteArray mimeType() const override;
0141     QString contentDispositionFilename() const override;
0142     bool setPreferredFileName(const QString &name) override;
0143     QSharedPointer<QIODevice> rawData() const override;
0144     bool isAvailableLocally() const override;
0145     ContentTransferEncoding suggestedCTE() const override;
0146     QByteArray imapUrl() const override;
0147     void preload() const override;
0148     void asDroppableMimeData(QDataStream &stream) const override;
0149 private:
0150     QPersistentModelIndex index;
0151     QString preferredName;
0152 };
0153 
0154 /** @short On-disk file */
0155 class FileAttachmentItem: public AttachmentItem {
0156 public:
0157     explicit FileAttachmentItem(const QString &fileName);
0158     ~FileAttachmentItem();
0159 
0160     QString caption() const override;
0161     QString tooltip() const override;
0162     QByteArray mimeType() const override;
0163     QString contentDispositionFilename() const override;
0164     bool setPreferredFileName(const QString &name) override;
0165     QSharedPointer<QIODevice> rawData() const override;
0166     bool isAvailableLocally() const override;
0167     ContentTransferEncoding suggestedCTE() const override;
0168     QByteArray imapUrl() const override;
0169     void preload() const override;
0170     void asDroppableMimeData(QDataStream &stream) const override;
0171 private:
0172     QString fileName;
0173     QString preferredName;
0174     mutable QByteArray m_cachedMime;
0175 };
0176 
0177 }
0178 
0179 #endif // COMPOSER_COMPOSERATTACHMENTS_H