From 7de66dcdd76bfe7196ae310913b37fb1f15b4e30 Mon Sep 17 00:00:00 2001 From: Matt Date: Sun, 10 Apr 2022 02:00:50 +0300 Subject: [PATCH] Minimize State enum usage --- telegramapi.cpp | 15 ++++++++++----- telegramclient.cpp | 16 ++++++++-------- telegramclient.h | 45 ++++++++++++++++++++------------------------- telegramsession.cpp | 16 ++++++++++++++-- telegramsession.h | 3 +++ 5 files changed, 55 insertions(+), 40 deletions(-) diff --git a/telegramapi.cpp b/telegramapi.cpp index 76bf9b2..99c65e1 100644 --- a/telegramapi.cpp +++ b/telegramapi.cpp @@ -58,7 +58,8 @@ qint64 TelegramClient::sendCode(QString phone_number) TGOBJECT(codeSettings, TLType::CodeSettings); sendCode["settings"] = codeSettings; - lastPhoneNumber = phone_number; + session.lastPhoneNumber = phone_number; + sync(); return sendMTObject<&writeTLMethodAuthSendCode>(sendCode); } @@ -70,17 +71,21 @@ void TelegramClient::handleSentCode(QByteArray data, qint64 mtm) readTLAuthSentCode(packet, var); TelegramObject sentCode = var.toMap(); - lastPhoneCodeHash = sentCode["phone_code_hash"].toString(); - emit gotSentCode(mtm, sentCode["phone_code_hash"].toString()); + if (!isLoggedIn()) { + session.lastPhoneCodeHash = sentCode["phone_code_hash"].toString(); + sync(); + + emit gotSentCode(mtm, sentCode["phone_code_hash"].toString()); + } } qint64 TelegramClient::signIn(QString phone_code, QString phone_code_hash, QString phone_number) { TGOBJECT(signIn, TLType::AuthSignInMethod); - if (phone_number.isEmpty()) phone_number = lastPhoneNumber; + if (phone_number.isEmpty()) phone_number = session.lastPhoneNumber; signIn["phone_number"] = phone_number; - if (phone_code_hash.isEmpty()) phone_code_hash = lastPhoneCodeHash; + if (phone_code_hash.isEmpty()) phone_code_hash = session.lastPhoneCodeHash; signIn["phone_code_hash"] = phone_code_hash; signIn["phone_code"] = phone_code; diff --git a/telegramclient.cpp b/telegramclient.cpp index 61385da..c71c52c 100644 --- a/telegramclient.cpp +++ b/telegramclient.cpp @@ -87,9 +87,7 @@ TelegramClient::TelegramClient(QObject *parent, QString sessionId) : updateSeq(), updatePts(), updateQts(), - timer(this), - lastPhoneNumber(), - lastPhoneCodeHash() + timer(this) { session.deserialize(sessionFile.value("session").toMap()); @@ -172,14 +170,16 @@ void TelegramClient::changeState(State s) } case LOGGED_IN: { - lastPhoneNumber = ""; - lastPhoneCodeHash = ""; + session.lastPhoneNumber = ""; + session.lastPhoneCodeHash = ""; + sync(); + getUpdatesState(); break; } } - emit stateChanged(s); + emit stateChanged((qint32) s); } bool TelegramClient::isLoggedIn() @@ -202,9 +202,9 @@ bool TelegramClient::isOpened() return socket.isOpen(); } -State TelegramClient::getState() +qint32 TelegramClient::getState() { - return state; + return (qint32) state; } bool TelegramClient::isConnected() diff --git a/telegramclient.h b/telegramclient.h index f33b205..49ee251 100644 --- a/telegramclient.h +++ b/telegramclient.h @@ -15,30 +15,28 @@ #endif #include -enum State -{ - STOPPED = 0, - CONNECTING, - DH_STEP_1, - DH_STEP_2, - DH_STEP_3, - DH_STEP_4, - DH_STEP_5, - DH_STEP_6, - DH_STEP_7, - DH_STEP_8, - DH_STEP_9, - AUTHORIZED, - INITED, - LOGGED_IN -}; - -Q_DECLARE_METATYPE(State) - class TelegramClient : public QObject { Q_OBJECT + Q_ENUMS(State) public: + enum State + { + STOPPED = 0, + CONNECTING, + DH_STEP_1, + DH_STEP_2, + DH_STEP_3, + DH_STEP_4, + DH_STEP_5, + DH_STEP_6, + DH_STEP_7, + DH_STEP_8, + DH_STEP_9, + AUTHORIZED, + INITED, + LOGGED_IN + }; explicit TelegramClient(QObject *parent = 0, QString sessionId = "kg"); private: //TODO support all MTProto service-messages. @@ -74,9 +72,6 @@ class TelegramClient : public QObject qint32 updatePts; qint32 updateQts; - QString lastPhoneNumber; - QString lastPhoneCodeHash; - template qint64 sendMTObject(QVariant obj, bool ignoreConfirm = false, bool binary = false); qint64 sendMTPacket(QByteArray raw, bool ignoreConfirm = false, bool binary = false); void sendPlainPacket(QByteArray raw); @@ -93,7 +88,7 @@ class TelegramClient : public QObject QByteArray gzipPacket(QByteArray data); signals: void handleResponse(qint64 mtm, QByteArray data, qint32 conId); - void stateChanged(State state); + void stateChanged(qint32 state); void gotSocketError(QAbstractSocket::SocketError error); void gotMTError(qint32 error_code); @@ -174,7 +169,7 @@ public slots: qint64 userId(); - State getState(); + qint32 getState(); qint64 pingDelayDisconnect(qint64 ping_id, qint32 delay); //TODO: handle pong diff --git a/telegramsession.cpp b/telegramsession.cpp index cea2251..b9b63d5 100644 --- a/telegramsession.cpp +++ b/telegramsession.cpp @@ -57,13 +57,21 @@ QVariantMap AuthKey::serialize() } TelegramSession::TelegramSession() : - authKey(), salt(), timeOffset(), id(), lastMessageId(), sequence(), userId(), + authKey(), + salt(), + timeOffset(), + id(), + lastMessageId(), + sequence(), + userId(), currentDc(DC_NUMBER), currentIp(DC_IP), currentPort(DC_PORT), migrateDc(), importId(), - importBytes() + importBytes(), + lastPhoneNumber(), + lastPhoneCodeHash() { } @@ -87,6 +95,8 @@ TelegramSession& TelegramSession::deserialize(QVariantMap obj) migrateDc = obj["migrateDc"].toInt(); importId = obj["importId"].toInt(); importBytes = obj["importBytes"].toByteArray(); + lastPhoneNumber = obj["lastPhoneNumber"].toString(); + lastPhoneCodeHash = obj["lastPhoneCodeHash"].toString(); return *this; } @@ -108,6 +118,8 @@ QVariantMap TelegramSession::serialize() obj["migrateDc"] = migrateDc; obj["importId"] = importId; obj["importBytes"] = importBytes; + obj["lastPhoneNumber"] = lastPhoneNumber; + obj["lastPhoneCodeHash"] = lastPhoneCodeHash; return obj; } diff --git a/telegramsession.h b/telegramsession.h index 0ae9273..e1e620a 100644 --- a/telegramsession.h +++ b/telegramsession.h @@ -39,6 +39,9 @@ struct TelegramSession qint32 importId; QByteArray importBytes; + QString lastPhoneNumber; + QString lastPhoneCodeHash; + TelegramSession& deserialize(QVariantMap obj); QVariantMap serialize(); };