-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathqRestResult.cpp
163 lines (136 loc) · 4.56 KB
/
qRestResult.cpp
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*==============================================================================
Library: qRestAPI
Copyright (c) 2010 Kitware Inc.
See Doc/copyright/copyright.txt
or http://www.slicer.org/copyright/copyright.txt for details.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
This file was originally developed by Jean-Christophe Fillion-Robin, Kitware Inc.
and was partially funded by NIH grant 3P41RR013218-12S1
==============================================================================*/
#include "qRestResult.h"
#include <QDebug>
#include <QEventLoop>
#include <QNetworkReply>
#include <QMetaProperty>
// --------------------------------------------------------------------------
// qRestAPIResult methods
// --------------------------------------------------------------------------
qRestResult::qRestResult(const QUuid& queryId, QObject* parent)
: QObject(parent)
, QueryId(queryId)
, ErrorCode(qRestAPI::UnknownError)
, done(false)
{
}
// --------------------------------------------------------------------------
qRestResult::~qRestResult()
{
}
// --------------------------------------------------------------------------
const QUuid& qRestResult::queryId() const
{
return this->QueryId;
}
// --------------------------------------------------------------------------
const QList<QVariantMap>& qRestResult::results() const
{
return this->Result;
}
// --------------------------------------------------------------------------
const QVariantMap qRestResult::result() const
{
return this->Result.isEmpty() ? QVariantMap() : this->Result[0];
}
// --------------------------------------------------------------------------
const QString& qRestResult::error() const
{
return this->Error;
}
// --------------------------------------------------------------------------
qRestAPI::ErrorType qRestResult::errorType() const
{
return this->ErrorCode;
}
// --------------------------------------------------------------------------
QByteArray qRestResult::rawHeader(const QByteArray& name) const
{
if (this->RawHeaders.contains(name))
{
return this->RawHeaders[name];
}
return QByteArray();
}
// --------------------------------------------------------------------------
QMap<QByteArray, QByteArray> qRestResult::rawHeaders() const
{
return this->RawHeaders;
}
// --------------------------------------------------------------------------
QByteArray qRestResult::response()const
{
return this->Reponse;
}
// --------------------------------------------------------------------------
void qRestResult::setResult()
{
this->done = true;
emit ready();
}
// --------------------------------------------------------------------------
void qRestResult::setResult(const QList<QVariantMap>& result)
{
this->Result.clear();
for (int i = 0; i < result.size(); ++i)
this->Result.push_back(result[i]);
// this->Result = result;
this->done = true;
emit ready();
}
// --------------------------------------------------------------------------
void qRestResult::setError(const QString& error, qRestAPI::ErrorType errorType)
{
this->ErrorCode = errorType;
this->Error = error;
this->done = true;
emit ready();
}
// --------------------------------------------------------------------------
bool qRestResult::waitForDone()
{
if (!done)
{
QEventLoop eventLoop;
// Time out will fire an error which will quit the event loop.
QObject::connect(this, SIGNAL(ready()),
&eventLoop, SLOT(quit()));
eventLoop.exec();
}
// We require that the error code in case of an actual error is
// never UnknownError for qRestResult objects.
return this->ErrorCode == qRestAPI::UnknownError;
}
// --------------------------------------------------------------------------
void qRestResult::downloadReadyRead()
{
QNetworkReply* reply = qobject_cast<QNetworkReply*>(this->sender());
ioDevice->write(reply->readAll());
}
// --------------------------------------------------------------------------
void qRestResult::downloadFinished()
{
ioDevice->close();
}
// --------------------------------------------------------------------------
void qRestResult::uploadFinished()
{
ioDevice->close();
}
// --------------------------------------------------------------------------
void qRestResult::setRawHeader(const QByteArray& name, const QByteArray& value)
{
this->RawHeaders[name] = value;
}