LeechCraft  0.6.70-15082-g543737046d
Modular cross-platform feature rich live environment.
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 
9 #include "customnetworkreply.h"
10 #include <cstring>
11 #include <QTimer>
12 
13 namespace 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 }
LC::Util::CustomNetworkReply::SetContent
void SetContent(const QString &string)
Sets content of this reply to the given string.
Definition: customnetworkreply.cpp:32
LC::Util::CustomNetworkReply::isSequential
bool isSequential() const override
Reimplemented from QNetworkReply::isSequential().
Definition: customnetworkreply.cpp:63
LC::Util
Definition: icoreproxy.h:33
LC::Util::oral::sph::min
constexpr detail::AggregateType< detail::AggregateFunction::Min, Ptr > min
Definition: oral.h:969
LC::Util::CustomNetworkReply::abort
void abort() override
Reimplemented from QNetworkReply::abort().
Definition: customnetworkreply.cpp:54
LC::Util::CustomNetworkReply::readData
qint64 readData(char *, qint64) override
Definition: customnetworkreply.cpp:68
LC::Util::CustomNetworkReply::bytesAvailable
qint64 bytesAvailable() const override
Reimplemented from QNetworkReply::bytesAvailable().
Definition: customnetworkreply.cpp:58
LC::Util::CustomNetworkReply::SetContentType
void SetContentType(const QByteArray &type)
Sets the content type of this reply.
Definition: customnetworkreply.cpp:27
customnetworkreply.h
LC::Util::CustomNetworkReply::CustomNetworkReply
CustomNetworkReply(const QUrl &url, QObject *parent=nullptr)
Creates the reply with the given url and parent.
Definition: customnetworkreply.cpp:21