File indexing completed on 2025-01-05 05:23:50

0001 /*
0002     This file is part of the Okteta Kasten module, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2008-2009, 2011-2012 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 "bytearrayrawfileloadthread.hpp"
0010 
0011 // lib
0012 #include "bytearraydocument.hpp"
0013 // Kasten core
0014 // #include <Kasten/Person>
0015 // Okteta core
0016 #include <Okteta/PieceTableByteArrayModel>
0017 // KF
0018 #include <KLocalizedString>
0019 // Qt
0020 #include <QCoreApplication>
0021 #include <QDataStream>
0022 #include <QFile>
0023 // C++
0024 #include <limits>
0025 
0026 namespace Kasten {
0027 
0028 ByteArrayRawFileLoadThread::~ByteArrayRawFileLoadThread() = default;
0029 
0030 void ByteArrayRawFileLoadThread::run()
0031 {
0032     const qint64 fileSize = mFile->size();
0033 
0034     // check if the file content can be addressed with Okteta::Address
0035     const Okteta::Address maxAddress = std::numeric_limits<Okteta::Address>::max();
0036 
0037     bool success = (fileSize <= maxAddress);
0038 
0039     if (success) {
0040         // allocate working memory
0041         QByteArray data;
0042         data.resize(fileSize);
0043         bool success = (data.size() == fileSize);
0044 
0045         if (success) {
0046             QDataStream inStream(mFile);
0047             inStream.readRawData(data.data(), fileSize);
0048 
0049             success = (inStream.status() == QDataStream::Ok);
0050 
0051             if (success) {
0052                 auto* byteArray = new Okteta::PieceTableByteArrayModel(data);
0053                 byteArray->setModified(false);
0054 
0055                 mDocument = new ByteArrayDocument(byteArray, i18nc("destination of the byte array", "Loaded from file."));
0056 //                 mDocument->setOwner(Person::createEgo());
0057                 // TODO: make PieceTableByteArrayModel a child by constructor argument parent
0058                 byteArray->moveToThread(QCoreApplication::instance()->thread());
0059                 mDocument->moveToThread(QCoreApplication::instance()->thread());
0060             } else {
0061                 mErrorString = mFile->errorString();
0062             }
0063         } else {
0064             mErrorString = i18n("There is not enough free working memory to load this file.");
0065         }
0066     } else {
0067         mErrorString = i18n("Support to load files larger than 2 GiB has not yet been implemented.");
0068     }
0069 
0070     if (!success) {
0071         mDocument = nullptr;
0072     }
0073 
0074     Q_EMIT documentRead(mDocument);
0075 }
0076 
0077 }
0078 
0079 #include "moc_bytearrayrawfileloadthread.cpp"