File indexing completed on 2024-11-24 04:43:52

0001 /*
0002     SPDX-FileCopyrightText: 2015-2017 Krzysztof Nowicki <krissn@op.pl>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <functional>
0010 
0011 #include <QQueue>
0012 
0013 #include <KIO/SpecialJob>
0014 
0015 namespace KIO
0016 {
0017 class Job;
0018 }
0019 
0020 template<typename F>
0021 struct Finally {
0022     Finally(F f)
0023         : cleanupf{f}
0024     {
0025     }
0026 
0027     ~Finally()
0028     {
0029         cleanupf();
0030     }
0031 
0032     F cleanupf;
0033 };
0034 
0035 template<typename F>
0036 Finally<F> finally(F f)
0037 {
0038     return Finally<F>(f);
0039 }
0040 
0041 class FakeTransferJob : public KIO::SpecialJob
0042 {
0043     Q_OBJECT
0044 public:
0045     using VerifierFn = std::function<void(FakeTransferJob *, const QByteArray &)>;
0046 
0047     struct Verifier {
0048         QObject *object;
0049         VerifierFn fn;
0050     };
0051 
0052     FakeTransferJob(const QByteArray &postData, const VerifierFn &fn, QObject *parent = nullptr);
0053     ~FakeTransferJob() override;
0054 
0055     static void addVerifier(QObject *obj, const VerifierFn &fn);
0056     static Verifier getVerifier();
0057 public Q_SLOTS:
0058     void postResponse(const QByteArray &resp);
0059 private Q_SLOTS:
0060     void callVerifier();
0061     void doEmitResult();
0062     void doData(const QByteArray &resp);
0063 Q_SIGNALS:
0064     void requestReceived(FakeTransferJob *job, const QByteArray &req);
0065 
0066 private:
0067     QByteArray mPostData;
0068     QByteArray mResponse;
0069     VerifierFn mVerifier;
0070     static QQueue<Verifier> mVerifierQueue;
0071 };