Add TDLib message receiver
This commit is contained in:
parent
6d864b3623
commit
ef9b234698
5 changed files with 63 additions and 9 deletions
|
@ -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
|
||||
|
|
27
src/tdlibreceiver.cpp
Normal file
27
src/tdlibreceiver.cpp
Normal file
|
@ -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";
|
||||
}
|
25
src/tdlibreceiver.h
Normal file
25
src/tdlibreceiver.h
Normal file
|
@ -0,0 +1,25 @@
|
|||
#ifndef TDLIBRECEIVER_H
|
||||
#define TDLIBRECEIVER_H
|
||||
|
||||
#include <QDebug>
|
||||
#include <QThread>
|
||||
#include <td/telegram/td_json_client.h>
|
||||
|
||||
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
|
|
@ -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");
|
||||
}
|
||||
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
#ifndef TDLIBWRAPPER_H
|
||||
#define TDLIBWRAPPER_H
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QObject>
|
||||
#include <QDebug>
|
||||
#include <td/telegram/td_json_client.h>
|
||||
#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;
|
||||
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue