From 76438ba95d16c7d007fc16ffc194889f937a19f7 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Thu, 19 Oct 2017 19:36:10 +0900 Subject: Introduce device_id and initial_device_name support; switch to generated LoginJob This is _almost_ a backwards-compatible change, except that connect*() and other relevant methods in Connection are no more virtual (that wasn't much useful anyway). Otherwise it's a matter of passing initial_device_name to connectToServer(), saving device_id (along with access_token) from the result of LoginJob and then passing device_id (along with access_token, again) to connectWithToken() upon the next run. --- connection.cpp | 50 ++++++++++++++++++++++---------------------------- 1 file changed, 22 insertions(+), 28 deletions(-) (limited to 'connection.cpp') diff --git a/connection.cpp b/connection.cpp index 84a52149..427118c9 100644 --- a/connection.cpp +++ b/connection.cpp @@ -21,8 +21,8 @@ #include "user.h" #include "events/event.h" #include "room.h" +#include "jobs/generated/login.h" #include "jobs/generated/logout.h" -#include "jobs/passwordlogin.h" #include "jobs/sendeventjob.h" #include "jobs/postreceiptjob.h" #include "jobs/joinroomjob.h" @@ -61,8 +61,6 @@ class Connection::Private // Leave state of the same room. QHash, Room*> roomMap; QHash userMap; - QString username; - QString password; QString userId; SyncJob* syncJob; @@ -113,42 +111,33 @@ void Connection::resolveServer(const QString& domain) }); } -void Connection::connectToServer(const QString& user, const QString& password) +void Connection::connectToServer(const QString& user, const QString& password, + const QString& initialDeviceName, + const QString& deviceId) { - auto loginJob = callApi(user, password); - connect( loginJob, &PasswordLogin::success, [=] () { - connectWithToken(loginJob->id(), loginJob->token()); + auto loginJob = callApi(QStringLiteral("m.login.password"), user, + /*medium*/ "", /*address*/ "", password, /*token*/ "", + deviceId, initialDeviceName); + connect( loginJob, &BaseJob::success, [=] () { + connectWithToken(loginJob->user_id(), loginJob->access_token(), + loginJob->device_id()); }); - connect( loginJob, &PasswordLogin::failure, [=] () { + connect( loginJob, &BaseJob::failure, [=] () { emit loginError(loginJob->errorString()); }); - d->username = user; // to be able to reconnect - d->password = password; } -void Connection::connectWithToken(const QString& userId, const QString& token) +void Connection::connectWithToken(const QString& userId, + const QString& accessToken, const QString& deviceId) { d->userId = userId; - d->data->setToken(token); - qCDebug(MAIN) << "Accessing" << d->data->baseUrl() - << "by user" << userId - << "with the following access token:"; - qCDebug(MAIN) << token; + d->data->setToken(accessToken); + d->data->setDeviceId(deviceId); + qCDebug(MAIN) << "Using server" << d->data->baseUrl() << "by user" << userId + << "from device" << deviceId; emit connected(); } -void Connection::reconnect() -{ - auto loginJob = callApi(d->username, d->password); - connect( loginJob, &PasswordLogin::success, [=] () { - d->userId = loginJob->id(); - emit reconnected(); - }); - connect( loginJob, &PasswordLogin::failure, [=] () { - emit loginError(loginJob->errorString()); - }); -} - void Connection::logout() { auto job = callApi(); @@ -301,6 +290,11 @@ QString Connection::userId() const return d->userId; } +const QString& Connection::deviceId() const +{ + return d->data->deviceId(); +} + QString Connection::token() const { return accessToken(); -- cgit v1.2.3