File indexing completed on 2024-05-12 05:56:13

0001 /*
0002     This file is part of the Okteta Kasten module, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2006-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 "bytearraydocumentfactory.hpp"
0010 
0011 // lib
0012 #include "bytearraydocument.hpp"
0013 // Okteta core
0014 #include <Okteta/PieceTableByteArrayModel>
0015 // Kasten core
0016 // #include <Kasten/Person>
0017 // KF
0018 #include <KLocalizedString>
0019 // Qt
0020 #include <QMimeData>
0021 
0022 namespace Kasten {
0023 
0024 static int newByteArrayDocumentCounter = 0;
0025 
0026 bool ByteArrayDocumentFactory::canCreateFromData(const QMimeData* mimeData)
0027 {
0028     Q_UNUSED(mimeData);
0029 
0030     // we currently take everything, see createFromData()
0031     return true;
0032 }
0033 
0034 AbstractDocument* ByteArrayDocumentFactory::create()
0035 {
0036     auto* document = new ByteArrayDocument(i18nc("The byte array was new created.", "New created."));
0037 
0038     ++newByteArrayDocumentCounter;
0039 
0040     // TODO: use document->typeName() ?
0041     document->setTitle(
0042         i18ncp("numbered title for a created document without a filename",
0043                "[New Byte Array]", "[New Byte Array %1]", newByteArrayDocumentCounter));
0044 
0045 //     document->setOwner(Person::createEgo());
0046 
0047     return document;
0048 }
0049 
0050 AbstractDocument* ByteArrayDocumentFactory::createFromData(const QMimeData* mimeData, bool setModified)
0051 {
0052     if (!mimeData || mimeData->formats().isEmpty()) {
0053         return create();
0054     }
0055 
0056     // SYNC: with abstractbytearrayview_p.cpp
0057     // if there is a octet stream, use it, otherwise take the dump of the format
0058     // with the highest priority
0059     // TODO: this may not be, what is expected, think about it, if we just
0060     // take byte array descriptions, like encodings in chars or values
0061     // would need the movement of the encoders into the core library
0062     const QString octetStreamFormatName = QStringLiteral("application/octet-stream");
0063     const QString dataFormatName = (mimeData->hasFormat(octetStreamFormatName)) ?
0064                                    octetStreamFormatName :
0065                                    mimeData->formats()[0];
0066 
0067     const QByteArray data = mimeData->data(dataFormatName);
0068 
0069     auto* byteArray = new Okteta::PieceTableByteArrayModel(data);
0070     byteArray->setModified(setModified);
0071 
0072     // TODO: pass name of generator
0073     auto* document = new ByteArrayDocument(byteArray, i18nc("origin of the byte array", "Created from data."));
0074 
0075     ++newByteArrayDocumentCounter;
0076 
0077     // TODO: use document->typeName() ?
0078     document->setTitle(
0079         i18ncp("numbered title for a created document without a filename",
0080                "[New Byte Array]", "[New Byte Array %1]", newByteArrayDocumentCounter));
0081 
0082 //     document->setOwner(Person::createEgo());
0083 
0084     return document;
0085 }
0086 
0087 }
0088 
0089 #include "moc_bytearraydocumentfactory.cpp"