File indexing completed on 2024-06-23 05:48:54

0001 /*
0002     This file is part of the Okteta Kasten module, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2019 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 #ifndef KASTEN_REPLACEJOB_HPP
0010 #define KASTEN_REPLACEJOB_HPP
0011 
0012 // controller
0013 #include "replaceuserqueryable.hpp"
0014 // libfinddialog
0015 #include <finddirection.hpp>
0016 // Okteta core
0017 #include <Okteta/AddressRange>
0018 // Qt
0019 #include <QObject>
0020 #include <QByteArray>
0021 
0022 namespace Okteta {
0023 class AbstractByteArrayModel;
0024 }
0025 
0026 namespace Kasten {
0027 
0028 class ByteArrayView;
0029 
0030 class ReplaceJob : public QObject
0031 {
0032     Q_OBJECT
0033 
0034 public:
0035     /// @param userQueryAgent expected to implement If::ReplaceUserQueryable
0036     explicit ReplaceJob(ByteArrayView* byteArrayView, Okteta::AbstractByteArrayModel* byteArrayModel,
0037                         QObject* userQueryAgent,
0038                         QObject* parent = nullptr);
0039     ~ReplaceJob() override;
0040 
0041 public:
0042     void setSearchData(const QByteArray& searchData);
0043     void setReplaceData(const QByteArray& replaceData);
0044     void setCaseSensitivity(Qt::CaseSensitivity caseSensitivity);
0045     void setDoPrompt(bool doPrompt);
0046     void setRange(Okteta::Address replaceRangeStartIndex, Okteta::Address replaceRangeEndIndex,
0047                   FindDirection direction);
0048 
0049 public:
0050     void start();
0051 
0052 Q_SIGNALS:
0053     void finished(bool previousFound, int noOfReplacements);
0054 
0055 private Q_SLOTS:
0056     void handleSearchResult(Okteta::AddressRange matchRange);
0057     void handleReplaceCurrentFinished(Kasten::ReplaceBehaviour replaceBehaviour);
0058     void handleContinueFinished(bool result);
0059 
0060 private:
0061     void searchNextPosition();
0062     void wrapAndSearchNextPosition();
0063     void replaceCurrent();
0064     void handleEndReached();
0065     void finish();
0066 
0067 private: // settings
0068     QByteArray m_searchData;
0069     QByteArray m_replaceData;
0070     Okteta::Address m_replaceRangeStartIndex;
0071     Okteta::Address m_replaceRangeEndIndex;
0072     Qt::CaseSensitivity m_caseSensitivity = Qt::CaseSensitive;
0073     bool m_doPrompt = false;
0074 
0075 private: // status
0076     Okteta::Address m_currentIndex;
0077     Okteta::Address m_currentReplaceRangeStartIndex;
0078     Okteta::Address m_currentReplaceRangeEndIndex;
0079     Okteta::Size m_currentMatchWidth;
0080     int m_noOfReplacements = 0;
0081     FindDirection m_direction;
0082     bool m_previousFound : 1;
0083     bool m_doWrap : 1;
0084 
0085 private:
0086     // expected to implement If::ReplaceUserQueryable
0087     QObject* m_userQueryAgent = nullptr;
0088 
0089 private: // target
0090     ByteArrayView* m_byteArrayView = nullptr;
0091     Okteta::AbstractByteArrayModel* m_byteArrayModel = nullptr;
0092 };
0093 
0094 }
0095 
0096 #endif
0097