2016-05-28 18:54:17 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Sink.h"
|
|
|
|
|
|
|
|
namespace Net {
|
2022-04-22 02:12:14 +01:00
|
|
|
|
2016-05-28 18:54:17 +01:00
|
|
|
/*
|
|
|
|
* Sink object for downloads that uses an external QByteArray it doesn't own as a target.
|
2022-04-27 22:36:11 +01:00
|
|
|
* FIXME: It is possible that the QByteArray is freed while we're doing some operation on it,
|
|
|
|
* causing a segmentation fault.
|
2016-05-28 18:54:17 +01:00
|
|
|
*/
|
2022-04-22 02:12:14 +01:00
|
|
|
class ByteArraySink : public Sink {
|
|
|
|
public:
|
|
|
|
ByteArraySink(QByteArray* output) : m_output(output){};
|
2016-05-28 18:54:17 +01:00
|
|
|
|
2022-04-22 02:12:14 +01:00
|
|
|
virtual ~ByteArraySink() = default;
|
2016-05-28 18:54:17 +01:00
|
|
|
|
2022-04-22 02:12:14 +01:00
|
|
|
public:
|
|
|
|
auto init(QNetworkRequest& request) -> Task::State override
|
2018-07-15 13:51:05 +01:00
|
|
|
{
|
|
|
|
m_output->clear();
|
2022-04-22 02:12:14 +01:00
|
|
|
if (initAllValidators(request))
|
|
|
|
return Task::State::Running;
|
|
|
|
return Task::State::Failed;
|
2018-07-15 13:51:05 +01:00
|
|
|
};
|
|
|
|
|
2022-04-22 02:12:14 +01:00
|
|
|
auto write(QByteArray& data) -> Task::State override
|
2018-07-15 13:51:05 +01:00
|
|
|
{
|
|
|
|
m_output->append(data);
|
2022-04-22 02:12:14 +01:00
|
|
|
if (writeAllValidators(data))
|
|
|
|
return Task::State::Running;
|
|
|
|
return Task::State::Failed;
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
|
|
|
|
2022-04-22 02:12:14 +01:00
|
|
|
auto abort() -> Task::State override
|
2018-07-15 13:51:05 +01:00
|
|
|
{
|
|
|
|
m_output->clear();
|
|
|
|
failAllValidators();
|
2022-04-22 02:12:14 +01:00
|
|
|
return Task::State::Failed;
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
|
|
|
|
2022-04-22 02:12:14 +01:00
|
|
|
auto finalize(QNetworkReply& reply) -> Task::State override
|
2018-07-15 13:51:05 +01:00
|
|
|
{
|
2022-04-22 02:12:14 +01:00
|
|
|
if (finalizeAllValidators(reply))
|
|
|
|
return Task::State::Succeeded;
|
|
|
|
return Task::State::Failed;
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
|
|
|
|
2022-04-22 02:12:14 +01:00
|
|
|
auto hasLocalData() -> bool override { return false; }
|
2016-10-28 01:19:19 +01:00
|
|
|
|
2022-04-22 02:12:14 +01:00
|
|
|
private:
|
|
|
|
QByteArray* m_output;
|
2016-05-28 18:54:17 +01:00
|
|
|
};
|
2022-04-22 02:12:14 +01:00
|
|
|
} // namespace Net
|