aboutsummaryrefslogtreecommitdiff
path: root/connection.cpp
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2017-10-19 19:36:10 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2017-10-19 19:36:10 +0900
commitba620ee07989aff134fec6b5d6f058cab3377ecc (patch)
treec9e6f0e434acf36efb61d5a28824f583f2c048e2 /connection.cpp
parente4fabf6e618b5045efec2629cb5d7b5bf73677e8 (diff)
downloadlibquotient-ba620ee07989aff134fec6b5d6f058cab3377ecc.tar.gz
libquotient-ba620ee07989aff134fec6b5d6f058cab3377ecc.zip
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.
Diffstat (limited to 'connection.cpp')
-rw-r--r--connection.cpp50
1 files changed, 22 insertions, 28 deletions
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<QPair<QString, bool>, Room*> roomMap;
QHash<QString, User*> 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<PasswordLogin>(user, password);
- connect( loginJob, &PasswordLogin::success, [=] () {
- connectWithToken(loginJob->id(), loginJob->token());
+ auto loginJob = callApi<LoginJob>(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<PasswordLogin>(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<LogoutJob>();
@@ -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();