-
Notifications
You must be signed in to change notification settings - Fork 1
/
secure.cpp
106 lines (84 loc) · 2.02 KB
/
secure.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
#include "secure.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QNetworkRequest>
// Useless destructor definition
SecureKeyCallback::~SecureKeyCallback(){}
class SecurePrivate {
public:
Secure *self;
QNetworkAccessManager m_network;
SecureKeyCallback *m_delegate;
QUrl m_url;
SecureKeyCallback* delegate() const
{
return m_delegate;
}
void setDelegate(SecureKeyCallback* delegate)
{
if (m_delegate != delegate) {
m_delegate = delegate;
emit self->delegateChanged(delegate);
}
}
QUrl url() const
{
return m_url;
}
void setUrl(QUrl url)
{
if (m_url != url) {
m_url = url;
emit self->urlChanged(url);
}
}
void request() {
QUrl requestUrl = url();
//requestUrl.addQueryItem();
QNetworkRequest request(requestUrl);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
m_network.get(request);
}
void responseReceived(QNetworkReply *reply) {
QByteArray bytes(reply->readAll());
QString text(QString::fromUtf8(bytes));
QJsonDocument doc = QJsonDocument::fromJson(bytes);
QByteArray key(QByteArray::fromBase64(doc.object().value("key").toVariant().toString().toUtf8()));
m_delegate->acceptKey(key);
}
};
Secure::Secure(QObject *parent) :
QObject(parent),
p(new SecurePrivate)
{
p->self = this;
QObject::connect(&p->m_network, SIGNAL(finished(QNetworkReply*)), this, SLOT(responseReceived(QNetworkReply*)));
}
Secure::~Secure()
{
}
SecureKeyCallback *Secure::delegate() const
{
return p->delegate();
}
QUrl Secure::url() const
{
return p->url();
}
void Secure::request()
{
p->request();
}
void Secure::responseReceived(QNetworkReply *reply)
{
p->responseReceived(reply);
}
void Secure::setDelegate(SecureKeyCallback *delegate)
{
p->setDelegate(delegate);
}
void Secure::setUrl(QUrl url)
{
p->setUrl(url);
}