File indexing completed on 2025-01-19 05:20:19
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 "bytearrayvaluesstreamencoder.hpp" 0010 0011 // lib 0012 #include <bytearrayview.hpp> 0013 // Okteta core 0014 #include <Okteta/AbstractByteArrayModel> 0015 #include <Okteta/ValueCodec> 0016 // KF 0017 #include <KConfigGroup> 0018 #include <KSharedConfig> 0019 #include <KLocalizedString> 0020 // Qt 0021 #include <QTextStream> 0022 0023 namespace Kasten { 0024 0025 const QString ValuesStreamEncoderSettings::DefaultSeparator = QStringLiteral(" "); 0026 0027 ValuesStreamEncoderSettings::ValuesStreamEncoderSettings() = default; 0028 0029 bool ValuesStreamEncoderSettings::operator==(const ValuesStreamEncoderSettings& other) const 0030 { 0031 // the only user-edited value 0032 return (separation == other.separation); 0033 } 0034 0035 void ValuesStreamEncoderSettings::loadConfig(const KConfigGroup& configGroup) 0036 { 0037 separation = configGroup.readEntry(SeparatorConfigKey, DefaultSeparator); 0038 } 0039 0040 void ValuesStreamEncoderSettings::saveConfig(KConfigGroup& configGroup) const 0041 { 0042 configGroup.writeEntry(SeparatorConfigKey, separation); 0043 } 0044 0045 0046 ByteArrayValuesStreamEncoder::ByteArrayValuesStreamEncoder() 0047 : AbstractByteArrayStreamEncoder(i18nc("name of the encoding target", "Values"), QStringLiteral("text/plain")) 0048 { 0049 const KConfigGroup configGroup(KSharedConfig::openConfig(), ConfigGroupId); 0050 mSettings.loadConfig(configGroup); 0051 } 0052 0053 ByteArrayValuesStreamEncoder::~ByteArrayValuesStreamEncoder() = default; 0054 0055 void ByteArrayValuesStreamEncoder::setSettings(const ValuesStreamEncoderSettings& settings) 0056 { 0057 if (mSettings == settings) { 0058 return; 0059 } 0060 0061 mSettings = settings; 0062 KConfigGroup configGroup(KSharedConfig::openConfig(), ConfigGroupId); 0063 mSettings.saveConfig(configGroup); 0064 Q_EMIT settingsChanged(); 0065 } 0066 0067 bool ByteArrayValuesStreamEncoder::encodeDataToStream(QIODevice* device, 0068 const ByteArrayView* byteArrayView, 0069 const Okteta::AbstractByteArrayModel* byteArrayModel, 0070 const Okteta::AddressRange& range) 0071 { 0072 bool success = true; 0073 0074 // settings 0075 mSettings.undefinedChar = byteArrayView->undefinedChar(); 0076 mSettings.substituteChar = byteArrayView->substituteChar(); 0077 mSettings.valueCoding = (Okteta::ValueCoding)byteArrayView->valueCoding(); 0078 0079 // encode 0080 QTextStream textStream(device); 0081 0082 Okteta::ValueCodec* valueCodec = Okteta::ValueCodec::createCodec(mSettings.valueCoding); 0083 0084 // prepare 0085 QString valueString; 0086 valueString.resize(valueCodec->encodingWidth()); 0087 0088 for (Okteta::Address i = range.start(); i <= range.end(); ++i) { 0089 if (i > range.start()) { 0090 textStream << mSettings.separation; 0091 } 0092 0093 valueCodec->encode(&valueString, 0, byteArrayModel->byte(i)); 0094 0095 textStream << valueString; 0096 } 0097 0098 // clean up 0099 delete valueCodec; 0100 0101 return success; 0102 } 0103 0104 } 0105 0106 #include "moc_bytearrayvaluesstreamencoder.cpp"