File indexing completed on 2024-04-28 16:54:24

0001 /*
0002     SPDX-FileCopyrightText: 2004 Esben Mose Hansen <kde@mosehansen.dk>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #include "historyitem.h"
0007 
0008 #include "klipper_debug.h"
0009 #include <QMap>
0010 
0011 #include <kurlmimedata.h>
0012 
0013 #include "historyimageitem.h"
0014 #include "historymodel.h"
0015 #include "historystringitem.h"
0016 #include "historyurlitem.h"
0017 
0018 HistoryItem::HistoryItem(const QByteArray &uuid)
0019     : m_model(nullptr)
0020     , m_uuid(uuid)
0021 {
0022 }
0023 
0024 HistoryItem::~HistoryItem()
0025 {
0026 }
0027 
0028 HistoryItemPtr HistoryItem::create(const QMimeData *data)
0029 {
0030     if (data->hasUrls()) {
0031         KUrlMimeData::MetaDataMap metaData;
0032         QList<QUrl> urls = KUrlMimeData::urlsFromMimeData(data, KUrlMimeData::PreferKdeUrls, &metaData);
0033         if (urls.isEmpty()) {
0034             return HistoryItemPtr();
0035         }
0036         QByteArray bytes = data->data(QStringLiteral("application/x-kde-cutselection"));
0037         bool cut = !bytes.isEmpty() && (bytes.at(0) == '1'); // true if 1
0038         return HistoryItemPtr(new HistoryURLItem(urls, metaData, cut));
0039     }
0040     if (data->hasText()) {
0041         const QString text = data->text();
0042         if (text.isEmpty()) { // reading mime data can fail. Avoid ghost entries
0043             return HistoryItemPtr();
0044         }
0045         return HistoryItemPtr(new HistoryStringItem(data->text()));
0046     }
0047     if (data->hasImage()) {
0048         const QImage image = qvariant_cast<QImage>(data->imageData());
0049         if (image.isNull()) {
0050             return HistoryItemPtr();
0051         }
0052         return HistoryItemPtr(new HistoryImageItem(image));
0053     }
0054 
0055     return HistoryItemPtr(); // Failed.
0056 }
0057 
0058 HistoryItemPtr HistoryItem::create(QDataStream &dataStream)
0059 {
0060     if (dataStream.atEnd()) {
0061         return HistoryItemPtr();
0062     }
0063     QString type;
0064     dataStream >> type;
0065     if (type == QLatin1String("url")) {
0066         QList<QUrl> urls;
0067         QMap<QString, QString> metaData;
0068         int cut;
0069         dataStream >> urls;
0070         dataStream >> metaData;
0071         dataStream >> cut;
0072         return HistoryItemPtr(new HistoryURLItem(urls, metaData, cut));
0073     }
0074     if (type == QLatin1String("string")) {
0075         QString text;
0076         dataStream >> text;
0077         return HistoryItemPtr(new HistoryStringItem(text));
0078     }
0079     if (type == QLatin1String("image")) {
0080         QImage image;
0081         dataStream >> image;
0082         return HistoryItemPtr(new HistoryImageItem(image));
0083     }
0084     qCWarning(KLIPPER_LOG) << "Failed to restore history item: Unknown type \"" << type << "\"";
0085     return HistoryItemPtr();
0086 }
0087 
0088 QByteArray HistoryItem::next_uuid() const
0089 {
0090     if (!m_model) {
0091         return m_uuid;
0092     }
0093     // go via the model to the next
0094     const QModelIndex ownIndex = m_model->indexOf(m_uuid);
0095     if (!ownIndex.isValid()) {
0096         // that was wrong, model doesn't contain our item, so there is no chain
0097         return m_uuid;
0098     }
0099     const int nextRow = (ownIndex.row() + 1) % m_model->rowCount();
0100     return m_model->index(nextRow, 0).data(HistoryModel::UuidRole).toByteArray();
0101 }
0102 
0103 QByteArray HistoryItem::previous_uuid() const
0104 {
0105     if (!m_model) {
0106         return m_uuid;
0107     }
0108     // go via the model to the next
0109     const QModelIndex ownIndex = m_model->indexOf(m_uuid);
0110     if (!ownIndex.isValid()) {
0111         // that was wrong, model doesn't contain our item, so there is no chain
0112         return m_uuid;
0113     }
0114     const int nextRow = ((ownIndex.row() == 0) ? m_model->rowCount() : ownIndex.row()) - 1;
0115     return m_model->index(nextRow, 0).data(HistoryModel::UuidRole).toByteArray();
0116 }
0117 
0118 void HistoryItem::setModel(HistoryModel *model)
0119 {
0120     m_model = model;
0121 }