File indexing completed on 2024-04-28 05:35:28

0001 /*
0002     SPDX-FileCopyrightText: 2004 Esben Mose Hansen <kde@mosehansen.dk>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #pragma once
0007 
0008 #include <QPixmap>
0009 
0010 class HistoryModel;
0011 class QString;
0012 class QMimeData;
0013 class QDataStream;
0014 
0015 class HistoryItem;
0016 typedef std::shared_ptr<HistoryItem> HistoryItemPtr;
0017 typedef std::shared_ptr<const HistoryItem> HistoryItemConstPtr;
0018 
0019 enum class HistoryItemType {
0020     Text,
0021     Image,
0022     Url,
0023 };
0024 
0025 /**
0026  * An entry in the clipboard history.
0027  */
0028 class HistoryItem
0029 {
0030 public:
0031     explicit HistoryItem(const QByteArray &uuid);
0032     virtual ~HistoryItem();
0033 
0034     /**
0035      * Returns the item type.
0036      */
0037     virtual HistoryItemType type() const = 0;
0038 
0039     /**
0040      * Return the current item as text
0041      * An image would be returned as a descriptive
0042      * text, such as 32x43 image.
0043      */
0044     virtual QString text() const = 0;
0045 
0046     /**
0047      * @return uuid of current item.
0048      */
0049     const QByteArray &uuid() const
0050     {
0051         return m_uuid;
0052     }
0053 
0054     /**
0055      * Return the current item as \QImage
0056      * A text would be returned as a null \QImage,
0057      * which is also the default implementation
0058      */
0059     virtual QImage image() const
0060     {
0061         return {};
0062     }
0063 
0064     /**
0065      * Returns a pointer to a QMimeData suitable for QClipboard::setMimeData().
0066      */
0067     virtual QMimeData *mimeData() const = 0;
0068 
0069     /**
0070      * Write object on datastream
0071      */
0072     virtual void write(QDataStream &stream) const = 0;
0073 
0074     /**
0075      * Equality.
0076      */
0077     virtual bool operator==(const HistoryItem &rhs) const = 0;
0078 
0079     /**
0080      * Create an HistoryItem from MimeSources (i.e., clipboard data)
0081      * returns null if create fails (e.g, unsupported mimetype)
0082      */
0083     static HistoryItemPtr create(const QMimeData *data);
0084 
0085     /**
0086      * Create an HistoryItem from data stream (i.e., disk file)
0087      * returns null if creation fails. In this case, the datastream
0088      * is left in an undefined state.
0089      */
0090     static HistoryItemPtr create(QDataStream &dataStream);
0091 
0092     /**
0093      * previous item's uuid
0094      * TODO: drop, only used in unit test now
0095      */
0096     QByteArray previous_uuid() const;
0097 
0098     /**
0099      * next item's uuid
0100      * TODO: drop, only used in unit test now
0101      */
0102     QByteArray next_uuid() const;
0103 
0104     void setModel(HistoryModel *model);
0105 
0106 protected:
0107     HistoryModel *m_model;
0108 
0109 private:
0110     QByteArray m_uuid;
0111 };
0112 
0113 inline QDataStream &operator<<(QDataStream &lhs, HistoryItem const *const rhs)
0114 {
0115     if (rhs) {
0116         rhs->write(lhs);
0117     }
0118     return lhs;
0119 }