File indexing completed on 2025-01-19 05:20:21

0001 /*
0002     This file is part of the Okteta Kasten module, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2007 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "abstractbytearraystreamencoder.hpp"
0010 
0011 // lib
0012 #include <bytearrayview.hpp>
0013 // Okteta Kasten core
0014 #include <Kasten/Okteta/ByteArrayDocument>
0015 #include <Kasten/Okteta/ByteArraySelection>
0016 // Okteta core
0017 #include <Okteta/AbstractByteArrayModel>
0018 // Qt
0019 #include <QBuffer>
0020 
0021 namespace Kasten {
0022 
0023 AbstractByteArrayStreamEncoder::AbstractByteArrayStreamEncoder(const QString& remoteTypeName,
0024                                                                const QString& remoteMimeType,
0025                                                                const QString& remoteClipboardMimeType)
0026     : AbstractModelStreamEncoder(remoteTypeName, remoteMimeType,
0027                                  remoteClipboardMimeType.isEmpty() ? QStringLiteral("text/plain") : remoteClipboardMimeType)
0028 {}
0029 
0030 AbstractByteArrayStreamEncoder::~AbstractByteArrayStreamEncoder() = default;
0031 
0032 QString AbstractByteArrayStreamEncoder::modelTypeName(AbstractModel* model, const AbstractModelSelection* selection) const
0033 {
0034     Q_UNUSED(selection)
0035 
0036     const auto * byteArrayDocument = model->findBaseModel<const ByteArrayDocument*>();
0037 
0038     return byteArrayDocument ? byteArrayDocument->typeName() : QString();
0039 }
0040 
0041 bool AbstractByteArrayStreamEncoder::encodeToStream(QIODevice* device,
0042                                                     AbstractModel* model, const AbstractModelSelection* selection)
0043 {
0044     const auto* byteArrayView = qobject_cast<const ByteArrayView*>(model);
0045 
0046     const ByteArrayDocument* byteArrayDocument =
0047         byteArrayView ? qobject_cast<const ByteArrayDocument*>(byteArrayView->baseModel()) : nullptr;
0048     if (!byteArrayDocument) {
0049         return false;
0050     }
0051 
0052     const Okteta::AbstractByteArrayModel* byteArray = byteArrayDocument->content();
0053 
0054     const ByteArraySelection* byteArraySelection =
0055         selection ? static_cast<const ByteArraySelection*>(selection) : nullptr;
0056 
0057     const Okteta::AddressRange range = byteArraySelection && byteArraySelection->isValid() ?
0058                                        byteArraySelection->range() :
0059                                        Okteta::AddressRange::fromWidth(0, byteArray->size());
0060 
0061     const bool success = encodeDataToStream(device, byteArrayView, byteArray, range);
0062 
0063     return success;
0064 }
0065 
0066 QString AbstractByteArrayStreamEncoder::previewData(AbstractModel* model, const AbstractModelSelection* selection)
0067 {
0068     const auto* byteArrayView = qobject_cast<const ByteArrayView*>(model);
0069 
0070     const ByteArrayDocument* byteArrayDocument =
0071         byteArrayView ? qobject_cast<const ByteArrayDocument*>(byteArrayView->baseModel()) : nullptr;
0072     if (!byteArrayDocument) {
0073         return {};
0074     }
0075 
0076     const Okteta::AbstractByteArrayModel* byteArray = byteArrayDocument->content();
0077 
0078     const ByteArraySelection* byteArraySelection =
0079         selection ? static_cast<const ByteArraySelection*>(selection) : nullptr;
0080 
0081     Okteta::AddressRange range = byteArraySelection && byteArraySelection->isValid() ?
0082                                  byteArraySelection->range() :
0083                                  Okteta::AddressRange::fromWidth(0, byteArray->size());
0084     range.restrictEndByWidth(MaxPreviewSize);
0085 
0086     QByteArray data;
0087     QBuffer dataBuffer(&data);
0088     dataBuffer.open(QIODevice::WriteOnly);
0089 
0090     const bool success = encodeDataToStream(&dataBuffer, byteArrayView, byteArray, range);
0091     dataBuffer.close();
0092 
0093     return success ? QString::fromLatin1(data) : QString();
0094 }
0095 
0096 }
0097 
0098 #include "moc_abstractbytearraystreamencoder.cpp"