Initial parsing of received information

This commit is contained in:
Sebastian J. Wolf 2020-08-10 23:37:25 +02:00
parent ef9b234698
commit 0e7dc04fa6
2 changed files with 43 additions and 2 deletions

View file

@ -19,9 +19,41 @@ void TDLibReceiver::receiverLoop()
while (this->isActive) { while (this->isActive) {
const char *result = td_json_client_receive(this->tdLibClient, WAIT_TIMEOUT); const char *result = td_json_client_receive(this->tdLibClient, WAIT_TIMEOUT);
if (result) { if (result) {
qDebug() << "[TDLibReceiver] Raw result: " << result; QJsonDocument receivedJsonDocument = QJsonDocument::fromJson(QByteArray(result));
// parse the result as JSON object and process it as an incoming update or an answer to a previously sent request qDebug().noquote() << "[TDLibReceiver] Raw result: " << receivedJsonDocument.toJson(QJsonDocument::Indented);
handleReceivedDocument(receivedJsonDocument);
} }
} }
qDebug() << "[TDLibReceiver] Stopping receiver loop"; qDebug() << "[TDLibReceiver] Stopping receiver loop";
} }
void TDLibReceiver::handleReceivedDocument(const QJsonDocument &receivedJsonDocument)
{
QVariantMap receivedInformation = receivedJsonDocument.object().toVariantMap();
QString objectTypeName = receivedInformation.value("@type").toString();
if (objectTypeName == "updateOption") {
this->handleUpdateOption(receivedInformation);
}
if (objectTypeName == "updateAuthorizationState") {
this->handleUpdateAuthorizationState(receivedInformation);
}
}
void TDLibReceiver::handleUpdateOption(const QVariantMap &receivedInformation)
{
QString currentOption = receivedInformation.value("name").toString();
if (currentOption == "version") {
QString detectedVersion = receivedInformation.value("value").toMap().value("value").toString();
qDebug() << "[TDLibReceiver] TD Lib version detected: " << detectedVersion;
emit versionDetected(detectedVersion);
}
}
void TDLibReceiver::handleUpdateAuthorizationState(const QVariantMap &receivedInformation)
{
QString authorizationState = receivedInformation.value("authorization_state").toMap().value("@type").toString();
qDebug() << "[TDLibReceiver] Authorization state changed: " << authorizationState;
emit authorizationStateChanged(authorizationState);
}

View file

@ -3,6 +3,8 @@
#include <QDebug> #include <QDebug>
#include <QThread> #include <QThread>
#include <QJsonDocument>
#include <QJsonObject>
#include <td/telegram/td_json_client.h> #include <td/telegram/td_json_client.h>
class TDLibReceiver : public QThread class TDLibReceiver : public QThread
@ -15,11 +17,18 @@ public:
explicit TDLibReceiver(void *tdLibClient, QObject *parent = nullptr); explicit TDLibReceiver(void *tdLibClient, QObject *parent = nullptr);
void setActive(const bool &active); void setActive(const bool &active);
signals:
void versionDetected(const QString &version);
void authorizationStateChanged(const QString &authorizationState);
private: private:
void *tdLibClient; void *tdLibClient;
bool isActive; bool isActive;
void receiverLoop(); void receiverLoop();
void handleReceivedDocument(const QJsonDocument &receivedJsonDocument);
void handleUpdateOption(const QVariantMap &receivedInformation);
void handleUpdateAuthorizationState(const QVariantMap &receivedInformation);
}; };
#endif // TDLIBRECEIVER_H #endif // TDLIBRECEIVER_H