From ef9b2346985907e8e95f9b7f7b506ba523722dea Mon Sep 17 00:00:00 2001 From: "Sebastian J. Wolf" Date: Mon, 10 Aug 2020 20:17:13 +0200 Subject: [PATCH] Add TDLib message receiver --- harbour-fernschreiber.pro | 2 ++ src/tdlibreceiver.cpp | 27 +++++++++++++++++++++++++++ src/tdlibreceiver.h | 25 +++++++++++++++++++++++++ src/tdlibwrapper.cpp | 13 ++++++------- src/tdlibwrapper.h | 5 +++-- 5 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 src/tdlibreceiver.cpp create mode 100644 src/tdlibreceiver.h diff --git a/harbour-fernschreiber.pro b/harbour-fernschreiber.pro index f1fb621..16e9acb 100644 --- a/harbour-fernschreiber.pro +++ b/harbour-fernschreiber.pro @@ -15,6 +15,7 @@ TARGET = harbour-fernschreiber CONFIG += sailfishapp sailfishapp_i18n SOURCES += src/harbour-fernschreiber.cpp \ + src/tdlibreceiver.cpp \ src/tdlibwrapper.cpp DISTFILES += qml/harbour-fernschreiber.qml \ @@ -70,5 +71,6 @@ INSTALLS += telegram 86.png 108.png 128.png 172.png 256.png \ fernschreiber.desktop gui images HEADERS += \ + src/tdlibreceiver.h \ src/tdlibsecrets.h \ src/tdlibwrapper.h diff --git a/src/tdlibreceiver.cpp b/src/tdlibreceiver.cpp new file mode 100644 index 0000000..fe63de1 --- /dev/null +++ b/src/tdlibreceiver.cpp @@ -0,0 +1,27 @@ +#include "tdlibreceiver.h" + +TDLibReceiver::TDLibReceiver(void *tdLibClient, QObject *parent) : QThread(parent) +{ + this->tdLibClient = tdLibClient; + this->isActive = true; +} + +void TDLibReceiver::setActive(const bool &active) +{ + qDebug() << "[TDLibReceiver] setActive " << active; + this->isActive = active; +} + +void TDLibReceiver::receiverLoop() +{ + qDebug() << "[TDLibReceiver] Starting receiver loop"; + const double WAIT_TIMEOUT = 5.0; + while (this->isActive) { + const char *result = td_json_client_receive(this->tdLibClient, WAIT_TIMEOUT); + if (result) { + qDebug() << "[TDLibReceiver] Raw result: " << result; + // parse the result as JSON object and process it as an incoming update or an answer to a previously sent request + } + } + qDebug() << "[TDLibReceiver] Stopping receiver loop"; +} diff --git a/src/tdlibreceiver.h b/src/tdlibreceiver.h new file mode 100644 index 0000000..d070283 --- /dev/null +++ b/src/tdlibreceiver.h @@ -0,0 +1,25 @@ +#ifndef TDLIBRECEIVER_H +#define TDLIBRECEIVER_H + +#include +#include +#include + +class TDLibReceiver : public QThread +{ + Q_OBJECT + void run() Q_DECL_OVERRIDE { + receiverLoop(); + } +public: + explicit TDLibReceiver(void *tdLibClient, QObject *parent = nullptr); + void setActive(const bool &active); + +private: + void *tdLibClient; + bool isActive; + + void receiverLoop(); +}; + +#endif // TDLIBRECEIVER_H diff --git a/src/tdlibwrapper.cpp b/src/tdlibwrapper.cpp index 241303a..84788b4 100644 --- a/src/tdlibwrapper.cpp +++ b/src/tdlibwrapper.cpp @@ -23,18 +23,17 @@ TDLibWrapper::TDLibWrapper(QObject *parent) : QObject(parent) { qDebug() << "[TDLibWrapper] Initializing TD Lib..."; this->tdLibClient = td_json_client_create(); - //this->testIt(); + this->tdLibReceiver = new TDLibReceiver(this->tdLibClient, this); + this->tdLibReceiver->start(); } TDLibWrapper::~TDLibWrapper() { qDebug() << "[TDLibWrapper] Destroying TD Lib..."; + this->tdLibReceiver->setActive(false); + while (this->tdLibReceiver->isRunning()) { + QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); + } td_json_client_destroy(this->tdLibClient); } -void TDLibWrapper::testIt() -{ - qDebug() << "[TDLibWrapper] Test it!"; - td_json_client_send(this->tdLibClient, "BLUBB"); -} - diff --git a/src/tdlibwrapper.h b/src/tdlibwrapper.h index fa9d021..9b8160a 100644 --- a/src/tdlibwrapper.h +++ b/src/tdlibwrapper.h @@ -1,9 +1,11 @@ #ifndef TDLIBWRAPPER_H #define TDLIBWRAPPER_H +#include #include #include #include +#include "tdlibreceiver.h" class TDLibWrapper : public QObject { @@ -12,8 +14,6 @@ public: explicit TDLibWrapper(QObject *parent = nullptr); ~TDLibWrapper(); - Q_INVOKABLE void testIt(); - signals: public slots: @@ -21,6 +21,7 @@ public slots: private: void *tdLibClient; + TDLibReceiver *tdLibReceiver; };