-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubject.cpp
More file actions
76 lines (65 loc) · 1.53 KB
/
Copy pathsubject.cpp
File metadata and controls
76 lines (65 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "subject.h"
#include <QNetworkAccessManager>
void Subject::finishRequest()
{
setIn_progress(false);
}
Subject::Subject(DataType tp, QObject *parent):
QObject(parent),
dataType_(tp)
{
}
const QByteArray Subject::data() const
{
std::lock_guard _(mu_);
return data_;
}
void Subject::setReply(QNetworkReply *reply)
{
std::lock_guard _(mu_);
reply_=reply;
}
bool Subject::in_progress() const
{
return in_progress_.load();
}
void Subject::setIn_progress(bool newIn_progress)
{
in_progress_.store(newIn_progress);
}
void Subject::operator()(QNetworkAccessManager *nm,
QNetworkRequest rq, QByteArray payload)
{
if(in_progress())
return;
setIn_progress(true);
if(payload.isEmpty())
setReply(nm->get(rq));
else
setReply(nm->post(rq,payload));
//Singleshot
connect(reply_, &QNetworkReply::finished,
this, &Subject::processData,
Qt::QueuedConnection);
}
void Subject::processData()
{
{
std::lock_guard _(mu_);
if(reply_->error()==QNetworkReply::NoError)
{
data_=reply_->readAll();
emit data_ready(data_, dataType_);
}
else
{
emit error_occured(reply_->error(), reply_->errorString());
}
disconnect(this, nullptr, nullptr, nullptr);
connect(reply_, &QNetworkReply::destroyed,
this, &Subject::finishRequest);
reply_->close();
reply_->disconnect();
reply_->deleteLater();
}
}