Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
Minimize State enum usage
Browse files Browse the repository at this point in the history
  • Loading branch information
curoviyxru committed Apr 9, 2022
1 parent 297cf56 commit 7de66dc
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 40 deletions.
15 changes: 10 additions & 5 deletions telegramapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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;

Expand Down
16 changes: 8 additions & 8 deletions telegramclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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()
Expand All @@ -202,9 +202,9 @@ bool TelegramClient::isOpened()
return socket.isOpen();
}

State TelegramClient::getState()
qint32 TelegramClient::getState()
{
return state;
return (qint32) state;
}

bool TelegramClient::isConnected()
Expand Down
45 changes: 20 additions & 25 deletions telegramclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,28 @@
#endif
#include <QTimer>

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.
Expand Down Expand Up @@ -74,9 +72,6 @@ class TelegramClient : public QObject
qint32 updatePts;
qint32 updateQts;

QString lastPhoneNumber;
QString lastPhoneCodeHash;

template <WRITE_METHOD W> qint64 sendMTObject(QVariant obj, bool ignoreConfirm = false, bool binary = false);
qint64 sendMTPacket(QByteArray raw, bool ignoreConfirm = false, bool binary = false);
void sendPlainPacket(QByteArray raw);
Expand All @@ -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);
Expand Down Expand Up @@ -174,7 +169,7 @@ public slots:

qint64 userId();

State getState();
qint32 getState();

qint64 pingDelayDisconnect(qint64 ping_id, qint32 delay); //TODO: handle pong

Expand Down
16 changes: 14 additions & 2 deletions telegramsession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
}

Expand All @@ -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;
}
Expand All @@ -108,6 +118,8 @@ QVariantMap TelegramSession::serialize()
obj["migrateDc"] = migrateDc;
obj["importId"] = importId;
obj["importBytes"] = importBytes;
obj["lastPhoneNumber"] = lastPhoneNumber;
obj["lastPhoneCodeHash"] = lastPhoneCodeHash;

return obj;
}
3 changes: 3 additions & 0 deletions telegramsession.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ struct TelegramSession
qint32 importId;
QByteArray importBytes;

QString lastPhoneNumber;
QString lastPhoneCodeHash;

TelegramSession& deserialize(QVariantMap obj);
QVariantMap serialize();
};
Expand Down

0 comments on commit 7de66dc

Please sign in to comment.