LeechCraft 0.6.70-14794-g33744ae6ce
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
customnetworkreply.cpp
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
10#include <cstring>
11#include <QTimer>
12
13namespace LC::Util
14{
15 CustomNetworkReply::CustomNetworkReply (const QUrl& url, QObject *parent)
16 : QNetworkReply (parent)
17 {
18 setUrl (url);
19 }
20
21 void CustomNetworkReply::SetContentType (const QByteArray& ct)
22 {
23 setHeader (QNetworkRequest::ContentTypeHeader, ct);
24 }
25
26 void CustomNetworkReply::SetContent (const QString& content)
27 {
28 SetContent (content.toUtf8 ());
29 }
30
31 void CustomNetworkReply::SetContent (const QByteArray& content)
32 {
33 Content_ = content;
34 Offset_ = 0;
35
36 open (ReadOnly | Unbuffered);
37
38 setHeader (QNetworkRequest::ContentLengthHeader, Content_.size ());
39
40 QTimer::singleShot (0,
41 this,
42 &CustomNetworkReply::readyRead);
43 QTimer::singleShot (0,
44 this,
45 &CustomNetworkReply::finished);
46 }
47
49 {
50 }
51
53 {
54 return Content_.size () - Offset_;
55 }
56
58 {
59 return true;
60 }
61
62 qint64 CustomNetworkReply::readData (char *data, qint64 maxSize)
63 {
64 if (Offset_ >= Content_.size ())
65 return -1;
66
67 qint64 number = std::min (maxSize, bytesAvailable ());
68 std::memcpy (data, Content_.constData () + Offset_, number);
69 Offset_ += number;
70
71 return number;
72 }
73}
void SetContentType(const QByteArray &type)
Sets the content type of this reply.
qint64 bytesAvailable() const override
Reimplemented from QNetworkReply::bytesAvailable().
void abort() override
Reimplemented from QNetworkReply::abort().
CustomNetworkReply(const QUrl &url, QObject *parent=nullptr)
Creates the reply with the given url and parent.
void SetContent(const QString &string)
Sets content of this reply to the given string.
qint64 readData(char *, qint64) override
bool isSequential() const override
Reimplemented from QNetworkReply::isSequential().