-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxboxsdk.cpp
More file actions
239 lines (178 loc) · 6.52 KB
/
xboxsdk.cpp
File metadata and controls
239 lines (178 loc) · 6.52 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "xboxsdk.h"
#include "ui_xboxsdk.h"
/**
* construct
*/
XboxSDK::XboxSDK(QWidget *parent) : QMainWindow(parent), ui(new Ui::XboxSDK), m_manager(new QNetworkAccessManager(this)), m_parser(new QJson::Parser)
{
ui->setupUi(this);
ui->grpLoginInfo->hide();
// gui setup
QList<QString> headers;
headers << "ID" << "Profile ID";
ui->tblProfiles->setHorizontalHeaderLabels(headers);
// connect our signal for our finished slot
connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinsihed(QNetworkReply*)));
}
// -------------------------------------------------------------------------------
/**
* destruct
* pick up after yourself!
*/
XboxSDK::~XboxSDK()
{
delete m_parser;
delete m_manager;
delete ui;
}
// -------------------------------------------------------------------------------
// ===============================================================================
// PUBLIC SLOTS
// ===============================================================================
/**
* requestFinished()
*/
void XboxSDK::requestFinsihed(QNetworkReply *reply)
{
// read our reply data
//QString json_data( reply->readAll() );
QByteArray json_data = reply->readAll();
// debug output
ui->dbgOut->addItem( QString("response: %1").arg( QString(json_data) ) );
// parse our json XboxSDK API response
parse_jsondata(json_data);
}
// -------------------------------------------------------------------------------
// ===============================================================================
// PUBLIC FUNCTIONS
// ===============================================================================
/**
* query_api()
* function sets up the API URI and makes the call
*/
void XboxSDK::query_api(QString uri_string)
{
// build our query uri
QUrl uri( QString("%1%2").arg(ui->comboBox->currentText(), uri_string) );
// debug output
ui->dbgOut->addItem(QString("API Call: %1").arg(uri_string));
// run our web request
m_reply = m_manager->get(QNetworkRequest(uri));
m_reply->ignoreSslErrors();
}
// -------------------------------------------------------------------------------
/**
* parse_jsondata()
* function will do two main things, first it will make sure the data we have gotten back can be parsed as json
* next it will then break the data into the first pass "success", "data" and/or "error"
*
* parsed data is stored in m_res a public variable of type QVariantMap
*/
void XboxSDK::parse_jsondata(QByteArray jsondata)
{
// bool to make sure parse was successfull
bool parse_ok;
// try and parse our json data
m_res = m_parser->parse(jsondata, &parse_ok).toMap();
// check to make sure parse was successfull
if (!parse_ok)
{
QMessageBox::critical(this, "Error", "Unable to parse json data.");
exit(1);
}
// now that data is checked we ca see what the API said!
if (!(m_res.value("success").toBool()))
{
QMessageBox::warning(this, "Error", m_res.value("error").toString());
}
// debug output
ui->dbgOut->addItem("Json response from API parsed successfully.");
ui->dbgOut->addItem("-------------------------------------------");
ui->dbgOut->addItem("");
}
// -------------------------------------------------------------------------------
// ===============================================================================
// PRIVATE SLOTS
// ===============================================================================
/**
* on_btnLogin_clicked()
*/
void XboxSDK::on_btnLogin_clicked()
{
// uri for this via the api is
// https://xboxsdk.com/api/login/<username>/<password>/[type]/[hashed]
QString username = ui->txtUsername->text();
QString password = QString( QCryptographicHash::hash(ui->txtPassword->text().toAscii(), QCryptographicHash::Sha1) ).toAscii().toHex();
// query the API
query_api( QString("login/%1/%2/xboxsdk/true").arg(username, password) );
// sit tight while we process that request.
QEventLoop eventLoop;
connect(m_reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
eventLoop.exec();
// was the login successful?
if (m_res.value("success").toBool())
{
// map our user data
QVariantMap userdata = m_res.value("data").toMap();
ui->lblUsername->setText( QString("Username: %1").arg(userdata.value("user_name").toString()) );
ui->lblEmail->setText( QString("Email: %1").arg(userdata.value("user_email").toString()) );
ui->grpLoginInfo->show();
}
}
// -------------------------------------------------------------------------------
/**
* on_btnLogout_clicked()
*/
void XboxSDK::on_btnLogout_clicked()
{
// clear input and labels
ui->lblUsername->clear();
ui->lblEmail->clear();
ui->txtUsername->clear();
ui->txtPassword->clear();
// hide the user box
ui->grpLoginInfo->hide();
}
// -------------------------------------------------------------------------------
/**
* on_btnReset_clicked()
*/
void XboxSDK::on_btnReset_clicked()
{
ui->txtUsername->clear();
ui->txtPassword->clear();
}
// -------------------------------------------------------------------------------
/**
* on_btnFetchProfiles_clicked()
*/
void XboxSDK::on_btnFetchProfiles_clicked()
{
// uri for this via the api is
// profiles/<API Key>
QString apikey = ui->txtAPIKey->text();
// query the API
query_api( QString("profiles/%1/").arg(apikey) );
// sit tight while we process that request.
QEventLoop eventLoop;
connect(m_reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
eventLoop.exec();
// was the login successful?
if (m_res.value("success").toBool())
{
// map our users profiles
foreach(QVariant profile, m_res.value("data").toList())
{
// map our profile data
QVariantMap p_data = profile.toMap();
ui->tblProfiles->setRowCount(ui->tblProfiles->rowCount() + 1);
// insert it into our table
ui->tblProfiles->setItem(ui->tblProfiles->rowCount() - 1, 0, new QTableWidgetItem(p_data.value("id").toString()));
ui->tblProfiles->setItem(ui->tblProfiles->rowCount() - 1, 1, new QTableWidgetItem(p_data.value("profile_id").toString()));
}
}
}
// -------------------------------------------------------------------------------
// ===============================================================================
// PRIVATE FUNCTIONS
// ===============================================================================