// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL // KOutNet - TDLib, wrapped. #pragma once #include #include #include #include #include QT_BEGIN_NAMESPACE class QThread; QT_END_NAMESPACE namespace koutnet { class TelegramClient : public QObject { Q_OBJECT public: explicit TelegramClient(QObject *parent = nullptr); ~TelegramClient() override; // apiId and apiHash come from my.telegram.org and are the one thing TDLib // cannot start without; databaseDirectory is where the account is kept // between runs, which is what makes this a session rather than a login. void start(quint32 apiId, const QByteArray &apiHash, const QString &databaseDirectory); void stop(); bool isRunning() const { return m_client != nullptr; } // Sends a request and forgets about it. For everything that carries no // answer worth acting on - setTdlibParameters, checkAuthenticationCode. void send(const QJsonObject &request); // Sends a request and hands the answer to `callback`. A failure comes back // as a null document with the reason in `error`, because TDLib reports a // refused request that way rather than as an exception of any kind. using Response = std::function; void request(const QJsonObject &request, Response callback); Q_SIGNALS: // Anything TDLib pushes that is not the answer to a request: new messages, // chat list changes, the authorization state walking forward. void update(const QJsonObject &update); private: // The receive loop, which lives on the client's own thread and returns // nothing; everything it learns is turned into a signal from there. void pump(); // Looks a request id up in the table of callbacks, takes the callback out // and runs it on the thread that asked for it. void deliver(const QJsonObject &response); void *m_client = nullptr; QThread *m_thread = nullptr; std::atomic m_nextRequestId{0}; // The waiting callbacks, by request id. Only the pump thread reads or // writes it, so nothing here needs a lock of its own. QHash m_pending; }; } // namespace koutnet