2016-05-28 23:24:17 +05:30
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Sink.h"
|
|
|
|
|
|
|
|
namespace Net {
|
2022-04-22 06:42:14 +05:30
|
|
|
|
2016-05-28 23:24:17 +05:30
|
|
|
/*
|
|
|
|
* Sink object for downloads that uses an external QByteArray it doesn't own as a target.
|
|
|
|
*/
|
2022-04-22 06:42:14 +05:30
|
|
|
class ByteArraySink : public Sink {
|
|
|
|
public:
|
|
|
|
ByteArraySink(QByteArray* output) : m_output(output){};
|
2016-05-28 23:24:17 +05:30
|
|
|
|
2022-04-22 06:42:14 +05:30
|
|
|
virtual ~ByteArraySink() = default;
|
2016-05-28 23:24:17 +05:30
|
|
|
|
2022-04-22 06:42:14 +05:30
|
|
|
public:
|
|
|
|
auto init(QNetworkRequest& request) -> Task::State override
|
2018-07-15 18:21:05 +05:30
|
|
|
{
|
2022-04-22 06:42:14 +05:30
|
|
|
if(!m_output)
|
|
|
|
return Task::State::Failed;
|
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
m_output->clear();
|
2022-04-22 06:42:14 +05:30
|
|
|
if (initAllValidators(request))
|
|
|
|
return Task::State::Running;
|
|
|
|
return Task::State::Failed;
|
2018-07-15 18:21:05 +05:30
|
|
|
};
|
|
|
|
|
2022-04-22 06:42:14 +05:30
|
|
|
auto write(QByteArray& data) -> Task::State override
|
2018-07-15 18:21:05 +05:30
|
|
|
{
|
2022-04-22 06:42:14 +05:30
|
|
|
if(!m_output)
|
|
|
|
return Task::State::Failed;
|
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
m_output->append(data);
|
2022-04-22 06:42:14 +05:30
|
|
|
if (writeAllValidators(data))
|
|
|
|
return Task::State::Running;
|
|
|
|
return Task::State::Failed;
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
|
2022-04-22 06:42:14 +05:30
|
|
|
auto abort() -> Task::State override
|
2018-07-15 18:21:05 +05:30
|
|
|
{
|
2022-04-22 06:42:14 +05:30
|
|
|
if(!m_output)
|
|
|
|
return Task::State::Failed;
|
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
m_output->clear();
|
|
|
|
failAllValidators();
|
2022-04-22 06:42:14 +05:30
|
|
|
return Task::State::Failed;
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
|
2022-04-22 06:42:14 +05:30
|
|
|
auto finalize(QNetworkReply& reply) -> Task::State override
|
2018-07-15 18:21:05 +05:30
|
|
|
{
|
2022-04-22 06:42:14 +05:30
|
|
|
if (finalizeAllValidators(reply))
|
|
|
|
return Task::State::Succeeded;
|
|
|
|
return Task::State::Failed;
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
|
2022-04-22 06:42:14 +05:30
|
|
|
auto hasLocalData() -> bool override { return false; }
|
2016-10-28 05:49:19 +05:30
|
|
|
|
2022-04-22 06:42:14 +05:30
|
|
|
private:
|
|
|
|
QByteArray* m_output;
|
2016-05-28 23:24:17 +05:30
|
|
|
};
|
2022-04-22 06:42:14 +05:30
|
|
|
} // namespace Net
|