-
Notifications
You must be signed in to change notification settings - Fork 0
/
TdClientWrapper.cpp
126 lines (107 loc) · 4.45 KB
/
TdClientWrapper.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
#include "TdClientWrapper.hpp"
#include <iostream>
#include <string>
#include <limits.h>
#include "td/telegram/td_json_client.h"
#include <QDebug>
#include <QtConcurrent/QtConcurrent>
using Object = td::td_api::object_ptr<td::td_api::Object>;
TdClientWrapper::TdClientWrapper(QObject *parent) : QObject(parent)
{
td_set_log_verbosity_level(1);
client = td_json_client_create();
//SEG FAULT means that json has error input variable names
std::string tdlibParameters = "{\"@type\":\"setTdlibParameters\",\"parameters\":{"
"\"use_message_database\":true,"
"\"use_secret_chats\":false,"
"\"api_id\":94575,"
"\"api_hash\":\"a3406de8d171bb422bb6ddf3bbd800e2\","
"\"system_language_code\":\"en\","
"\"device_model\":\"Desktop\","
"\"system_version\":\"Unknown\","
"\"application_version\":\"1.0\","
"\"enable_storage_optimizer\":true"
"}}";
td_json_client_send(client, tdlibParameters.c_str());
//answer is - {"@type":"updateAuthorizationState","authorization_state":{"@type":"authorizationStateWaitEncryptionKey","is_encrypted":false}}
std::string setDatabaseKey =
"{\"@type\":\"setDatabaseEncryptionKey\","
"\"new_encryption_key\":\"abcd\"}";
td_json_client_send(client, setDatabaseKey.c_str());
//Debug answer - Sending result for request 1: ok {}
}
void TdClientWrapper::loop()
{
receiveObject = new ReceiveObject(client);
parserObject = new ParserObject(this);
thread = new QThread;
parserThread = new QThread;
receiveObject->moveToThread(thread);
parserObject->moveToThread(parserThread);
// connect(receiveThread, &ReceiveThread::resultReady, this, &TdClientWrapper::logEmitted,
// Qt::QueuedConnection);
// connect(receiveThread, &ReceiveThread::resultReady, this, &TdClientWrapper::Log,
// Qt::QueuedConnection);
connect(thread, &QThread::started, receiveObject, &ReceiveObject::listen, Qt::QueuedConnection);
connect(receiveObject, &ReceiveObject::resultReady, parserObject, &ParserObject::parseResponse,
Qt::QueuedConnection);
connect(parserObject, &ParserObject::updateNewMessage, this, &TdClientWrapper::updateNewMessage);
thread->start();
parserThread->start();
// std::string str_start = td_json_client_receive(client, 10);
// std::cout << str_start << std::endl;
// timer.setInterval(100);
// connect(&timer, &QTimer::timeout, [this]() {
// QtConcurrent::run(this, &TdClientWrapper::getResponse);
// });
// timer.start();
}
std::string TdClientWrapper::getResponse()
{
std::string str = td_json_client_receive(client, 10);
if (!str.empty()) {
QString result = QString::fromStdString(str);
emit logEmitted(result);
}
}
void TdClientWrapper::setPhone(QString number)
{
qDebug() << number;
std::string setAuthenticationPhoneNumber =
"{\"@type\":\"setAuthenticationPhoneNumber\","
"\"phone_number\":\"" + number.toStdString() + "\","
"\"allow_flash_call\":false}";
td_json_client_send(client, setAuthenticationPhoneNumber.c_str());
}
void TdClientWrapper::setCode(QString code)
{
qDebug() << code;
std::string setAuthenticationCode =
"{\"@type\":\"checkAuthenticationCode\","
"\"code\":\"" + code.toStdString() + "\"}";
qDebug() << QString::fromStdString(setAuthenticationCode);
std::string setAuthenticationCodeIfRegistered =
"{\"@type\":\"checkAuthenticationCode\","
"\"code\":\"" + code.toStdString() + "\","
"\"first_name\":\"name\","
"\"last_name\":\"surname\"}";
td_json_client_send(client, setAuthenticationCode.c_str());
}
void TdClientWrapper::getChats()
{
auto max_order = std::to_string(std::numeric_limits<std::int64_t>::max());
std::string getChats =
"{\"@type\":\"getChats\","
"\"offset_order\":\"" + max_order + "\","
"\"offset_chat_id\":\"0\","
"\"limit\":20,"
"\"@extra\":12345}";
std::string getChat = "{\"@type\":\"getChat\","
"\"chat_id\":\"-1001107985716\""
"}";
td_json_client_send(client, getChats.c_str());
}
void TdClientWrapper::Log(const QString &str)
{
qDebug() << str;
}