ifndef VK_H define VK_H include QString include QObject include QJsonD

  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
214
215
216
217
218
219
220
221
222
223
224
#ifndef VK_H
#define VK_H
#include <QString>
#include <QObject>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkReply>
#include <map>
class VK;
class VKAnswer;
class VKError;
class VKResponse;
class VK: public QObject {
Q_OBJECT
public:
VK(QString token);
void request(QString method, std::map<QString, QString> data);
void request(QString method, std::map<QString, QVariant> data);
void post(QString method, std::map<QString, QVariant> data);
virtual ~VK();
private:
QString token;
QString method;
private slots:
void _finished(QNetworkReply *reply);
signals:
void requestFinished(VKAnswer *answer, QString method);
};
class VKAnswer {
public:
VKAnswer(VKError *error, VKResponse *response);
VKError *error;
VKResponse *response;
~VKAnswer();
};
class VKResponse {
public:
VKResponse(QString json);
QJsonObject data;
~VKResponse();
};
class VKError {
public:
VKError(QString json);
VKError(int custom, QString error);
QString text();
int error();
~VKError();
static const unsigned int NoError = 0,
UnknownError = 1,
AppError = 2,
MethodError = 3,
ProtocolError = 4,
AuthorizeError = 5,
SpeedError = 6,
AccessError = 7,
SyntaxError = 8,
ManyError = 9,
ServerError = 10,
ModeError = 11,
CaptchaError = 12,
ValidateError = 13,
ParamError = 100,
NetError = 1400,
AnotherError = 65535;
private:
QString _text;
int _error;
};
struct VKMessage {
public:
QString body, id, user_id, chat_id, title, time;
bool isChat = false, readed, out;
};
struct VKMessages {
public:
VKMessages() {
this->list = new VKMessage[500];
}
VKMessage *list;
int count;
};
struct UserInfo {
public:
int id;
QString name, surname, avatar_50, avatar_100, avatar_200;
};
struct UsersInfo {
public:
UsersInfo() {
this->list = new UserInfo[500];
}
UserInfo *list;
int count;
};
#endif // VK_H
#include "vk.h"
#include "messenger.h"
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QJsonDocument>
#include <QObject>
VK::VK(QString token) {
this->token = token;
}
VK::~VK() {
}
void VK::request(QString method, std::map<QString, QString> data) {
QNetworkAccessManager *manager = new QNetworkAccessManager;
QObject::connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(_finished(QNetworkReply*)));
QString urlString("https://api.vk.com/method/" + method + "?v=5.26&");
std::map<QString, QString>::iterator i;
for (i = data.begin(); i != data.end(); i++) {
urlString += i->first + "=" + i->second + "&";
}
urlString += "&access_token=" + this->token;
QUrl url(urlString);
this->method = method;
manager->get(QNetworkRequest(url));
}
void VK::request(QString method, std::map<QString, QVariant> data) {
QNetworkAccessManager *manager = new QNetworkAccessManager();
QObject::connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(_finished(QNetworkReply*)));
QString urlString("https://api.vk.com/method/" + method + "?v=5.26&");
std::map<QString, QVariant>::iterator i;
for (i = data.begin(); i != data.end(); i++) {
urlString += i->first + "=" + i->second.toString() + "&";
}
urlString += "&access_token=" + this->token;
QUrl url(urlString);
this->method = method;
manager->get(QNetworkRequest(url));
}
void VK::post(QString method, std::map<QString, QVariant> data) {
QNetworkAccessManager *manager = new QNetworkAccessManager();
QObject::connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(_finished(QNetworkReply*)));
QString urlString("https://api.vk.com/method/" + method + "?v=5.26&access_token=" + this->token);
QByteArray postData;
std::map<QString, QVariant>::iterator i;
for (i = data.begin(); i != data.end(); i++) {
postData.append(i->first + "=" + i->second.toString() + "&");
}
QUrl url(urlString);
this->method = method;
manager->post(QNetworkRequest(url), postData);
}
void VK::_finished(QNetworkReply *reply) {
if (reply->error() != QNetworkReply::NoError) {
QString json("");
VKResponse *response = new VKResponse(json);
VKError *error = new VKError(VKError::NetError, "NetError");
VKAnswer *answer = new VKAnswer(error, response);
emit requestFinished(answer, method);
} else {
QString json(reply->readAll());
VKResponse *response = new VKResponse(json);
VKError *error = new VKError(json);
VKAnswer *answer = new VKAnswer(error, response);
emit requestFinished(answer, method);
}
}
VKResponse::VKResponse(QString json) {
QJsonDocument jsonResponse = QJsonDocument::fromJson(json.toUtf8());
this->data = jsonResponse.object();
}
VKError::VKError(int custom, QString error) {
if (custom || !custom) {
this->_error = custom;
this->_text = error;
}
}
VKError::VKError(QString json) {
QJsonDocument jsonResponse = QJsonDocument::fromJson(json.toUtf8());
QJsonObject jsonObject = jsonResponse.object();
if (jsonObject["error"].isNull()) {
this->_error = 0;
this->_text = "";
} else {
this->_text = jsonObject["error"].toObject()["error_msg"].toString();
this->_error = jsonObject["error"].toObject()["error_code"].toInt();
}
}
int VKError::error() {
return this->_error;
}
QString VKError::text() {
return this->_text;
}
VKAnswer::VKAnswer(VKError *error, VKResponse *response) {
this->error = error;
this->response = response;
}