From 154113afc436ee30eb702e49e9af77b16e14e824 Mon Sep 17 00:00:00 2001 From: "Sebastian J. Wolf" Date: Wed, 12 Aug 2020 23:51:09 +0200 Subject: [PATCH] Initial ping-pong with TDLib :) --- src/tdlibreceiver.cpp | 4 ++ src/tdlibreceiver.h | 1 + src/tdlibwrapper.cpp | 107 ++++++++++++++++++++++++++++++++++++++++-- src/tdlibwrapper.h | 30 ++++++++++-- 4 files changed, 135 insertions(+), 7 deletions(-) diff --git a/src/tdlibreceiver.cpp b/src/tdlibreceiver.cpp index 01c032f..d21c354 100644 --- a/src/tdlibreceiver.cpp +++ b/src/tdlibreceiver.cpp @@ -52,6 +52,10 @@ void TDLibReceiver::processUpdateOption(const QVariantMap &receivedInformation) QString detectedVersion = receivedInformation.value("value").toMap().value("value").toString(); qDebug() << "[TDLibReceiver] TD Lib version detected: " << detectedVersion; emit versionDetected(detectedVersion); + } else { + QVariant currentValue = receivedInformation.value("value").toMap().value("value"); + qDebug() << "[TDLibReceiver] Option updated: " << currentOption << currentValue; + emit optionUpdated(currentOption, currentValue); } } diff --git a/src/tdlibreceiver.h b/src/tdlibreceiver.h index 3a628e1..e1fed92 100644 --- a/src/tdlibreceiver.h +++ b/src/tdlibreceiver.h @@ -20,6 +20,7 @@ public: signals: void versionDetected(const QString &version); void authorizationStateChanged(const QString &authorizationState); + void optionUpdated(const QString &optionName, const QVariant &optionValue); private: void *tdLibClient; diff --git a/src/tdlibwrapper.cpp b/src/tdlibwrapper.cpp index f8f99c6..f1e824c 100644 --- a/src/tdlibwrapper.cpp +++ b/src/tdlibwrapper.cpp @@ -18,6 +18,11 @@ */ #include "tdlibwrapper.h" +#include "tdlibsecrets.h" +#include +#include +#include +#include TDLibWrapper::TDLibWrapper(QObject *parent) : QObject(parent) { @@ -25,8 +30,15 @@ TDLibWrapper::TDLibWrapper(QObject *parent) : QObject(parent) this->tdLibClient = td_json_client_create(); this->tdLibReceiver = new TDLibReceiver(this->tdLibClient, this); + QString tdLibDatabaseDirectoryPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/tdlib"; + QDir tdLibDatabaseDirectory(tdLibDatabaseDirectoryPath); + if (!tdLibDatabaseDirectory.exists()) { + tdLibDatabaseDirectory.mkpath(tdLibDatabaseDirectoryPath); + } + connect(this->tdLibReceiver, SIGNAL(versionDetected(QString)), this, SLOT(handleVersionDetected(QString))); connect(this->tdLibReceiver, SIGNAL(authorizationStateChanged(QString)), this, SLOT(handleAuthorizationStateChanged(QString))); + connect(this->tdLibReceiver, SIGNAL(optionUpdated(QString, QVariant)), this, SLOT(handleOptionUpdated(QString, QVariant))); this->tdLibReceiver->start(); } @@ -41,12 +53,19 @@ TDLibWrapper::~TDLibWrapper() td_json_client_destroy(this->tdLibClient); } +void TDLibWrapper::sendRequest(const QVariantMap &requestObject) +{ + qDebug() << "[TDLibWrapper] Sending request to TD Lib, object type name: " << requestObject.value("@type").toString(); + QJsonDocument requestDocument = QJsonDocument::fromVariant(requestObject); + td_json_client_send(this->tdLibClient, requestDocument.toJson().constData()); +} + QString TDLibWrapper::getVersion() { return this->version; } -QString TDLibWrapper::getAuthorizationState() +TDLibWrapper::AuthorizationState TDLibWrapper::getAuthorizationState() { return this->authorizationState; } @@ -59,7 +78,89 @@ void TDLibWrapper::handleVersionDetected(const QString &version) void TDLibWrapper::handleAuthorizationStateChanged(const QString &authorizationState) { - this->authorizationState = authorizationState; - emit authorizationStateChanged(authorizationState); + if (authorizationState == "authorizationStateClosed") { + this->authorizationState = AuthorizationState::Closed; + } + + if (authorizationState == "authorizationStateClosing") { + this->authorizationState = AuthorizationState::Closing; + } + + if (authorizationState == "authorizationStateLoggingOut") { + this->authorizationState = AuthorizationState::LoggingOut; + } + + if (authorizationState == "authorizationStateReady") { + this->authorizationState = AuthorizationState::Ready; + } + + if (authorizationState == "authorizationStateWaitCode") { + this->authorizationState = AuthorizationState::WaitCode; + } + + if (authorizationState == "authorizationStateWaitEncryptionKey") { + this->setEncryptionKey(); + this->authorizationState = AuthorizationState::WaitEncryptionKey; + } + + if (authorizationState == "authorizationStateWaitOtherDeviceConfirmation") { + this->authorizationState = AuthorizationState::WaitOtherDeviceConfirmation; + } + + if (authorizationState == "authorizationStateWaitPassword") { + this->authorizationState = AuthorizationState::WaitPassword; + } + + if (authorizationState == "authorizationStateWaitPhoneNumber") { + this->authorizationState = AuthorizationState::WaitPhoneNumber; + } + + if (authorizationState == "authorizationStateWaitRegistration") { + this->authorizationState = AuthorizationState::WaitRegistration; + } + + if (authorizationState == "authorizationStateWaitTdlibParameters") { + this->setInitialParameters(); + this->authorizationState = AuthorizationState::WaitTdlibParameters; + } + + emit authorizationStateChanged(this->authorizationState); + +} + +void TDLibWrapper::handleOptionUpdated(const QString &optionName, const QVariant &optionValue) +{ + this->options.insert(optionName, optionValue); + emit optionUpdated(optionName, optionValue); +} + +void TDLibWrapper::setInitialParameters() +{ + qDebug() << "[TDLibWrapper] Sending initial parameters to TD Lib"; + QVariantMap requestObject; + requestObject.insert("@type", "setTdlibParameters"); + QVariantMap initialParameters; + initialParameters.insert("api_id", TDLIB_API_ID); + initialParameters.insert("api_hash", TDLIB_API_HASH); + initialParameters.insert("database_directory", QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/tdlib"); + initialParameters.insert("use_message_database", true); + initialParameters.insert("use_secret_chats", false); + initialParameters.insert("system_language_code", QLocale::system().name()); + QSettings hardwareSettings("/etc/hw-release", QSettings::NativeFormat); + initialParameters.insert("device_model", hardwareSettings.value("NAME", "Unknown Mobile Device").toString()); + initialParameters.insert("system_version", QSysInfo::prettyProductName()); + initialParameters.insert("application_version", "0.1"); + requestObject.insert("parameters", initialParameters); + this->sendRequest(requestObject); +} + +void TDLibWrapper::setEncryptionKey() +{ + qDebug() << "[TDLibWrapper] Setting database encryption key"; + QVariantMap requestObject; + requestObject.insert("@type", "checkDatabaseEncryptionKey"); + // see https://github.com/tdlib/td/issues/188#issuecomment-379536139 + requestObject.insert("encryption_key", ""); + this->sendRequest(requestObject); } diff --git a/src/tdlibwrapper.h b/src/tdlibwrapper.h index aba7a3b..e8ad3bf 100644 --- a/src/tdlibwrapper.h +++ b/src/tdlibwrapper.h @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include #include "tdlibreceiver.h" @@ -14,23 +16,43 @@ public: explicit TDLibWrapper(QObject *parent = nullptr); ~TDLibWrapper(); + enum AuthorizationState { + Closed, + Closing, + LoggingOut, + Ready, + WaitCode, + WaitEncryptionKey, + WaitOtherDeviceConfirmation, + WaitPassword, + WaitPhoneNumber, + WaitRegistration, + WaitTdlibParameters + }; + + Q_INVOKABLE void sendRequest(const QVariantMap &requestObject); Q_INVOKABLE QString getVersion(); - Q_INVOKABLE QString getAuthorizationState(); + Q_INVOKABLE TDLibWrapper::AuthorizationState getAuthorizationState(); signals: void versionDetected(const QString &version); - void authorizationStateChanged(const QString &authorizationState); + void authorizationStateChanged(const TDLibWrapper::AuthorizationState &authorizationState); + void optionUpdated(const QString &optionName, const QVariant &optionValue); public slots: void handleVersionDetected(const QString &version); void handleAuthorizationStateChanged(const QString &authorizationState); + void handleOptionUpdated(const QString &optionName, const QVariant &optionValue); private: void *tdLibClient; TDLibReceiver *tdLibReceiver; - QString version; - QString authorizationState; + TDLibWrapper::AuthorizationState authorizationState; + QVariantMap options; + + void setInitialParameters(); + void setEncryptionKey(); };