summary refs log tree commit diff
path: root/src/MatrixClient.cc
diff options
context:
space:
mode:
authorKonstantinos Sideris <sideris.konstantin@gmail.com>2017-04-09 02:17:04 +0300
committerKonstantinos Sideris <sideris.konstantin@gmail.com>2017-04-09 02:17:04 +0300
commit239780557f6641c225828e0cb6508f47c0bc15aa (patch)
tree1a064cfd3cdb11dd46aa7b04737e7a07c2333eba /src/MatrixClient.cc
parentImplement initial registration stage (diff)
downloadnheko-239780557f6641c225828e0cb6508f47c0bc15aa.tar.xz
Add logout button
Logout from the current session and invalidate the current token
Diffstat (limited to 'src/MatrixClient.cc')
-rw-r--r--src/MatrixClient.cc43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/MatrixClient.cc b/src/MatrixClient.cc

index c376a53f..f7c1b0d4 100644 --- a/src/MatrixClient.cc +++ b/src/MatrixClient.cc
@@ -43,6 +43,15 @@ MatrixClient::MatrixClient(QString server, QObject *parent) connect(this, SIGNAL(finished(QNetworkReply *)), this, SLOT(onResponse(QNetworkReply *))); } +void MatrixClient::reset() +{ + next_batch_ = ""; + server_ = ""; + token_ = ""; + + txn_id_ = 0; +} + void MatrixClient::onVersionsResponse(QNetworkReply *reply) { reply->deleteLater(); @@ -93,6 +102,20 @@ void MatrixClient::onLoginResponse(QNetworkReply *reply) } } +void MatrixClient::onLogoutResponse(QNetworkReply *reply) +{ + reply->deleteLater(); + + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (status != 200) { + qWarning() << "Logout error: " << reply->errorString(); + return; + } + + emit loggedOut(); +} + void MatrixClient::onRegisterResponse(QNetworkReply *reply) { reply->deleteLater(); @@ -249,6 +272,9 @@ void MatrixClient::onResponse(QNetworkReply *reply) case Endpoint::Login: onLoginResponse(reply); break; + case Endpoint::Logout: + onLogoutResponse(reply); + break; case Endpoint::Register: onRegisterResponse(reply); break; @@ -283,6 +309,23 @@ void MatrixClient::login(const QString &username, const QString &password) reply->setProperty("endpoint", Endpoint::Login); } +void MatrixClient::logout() +{ + QUrlQuery query; + query.addQueryItem("access_token", token_); + + QUrl endpoint(server_); + endpoint.setPath(api_url_ + "/logout"); + endpoint.setQuery(query); + + QNetworkRequest request(endpoint); + request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); + + QJsonObject body{}; + QNetworkReply *reply = post(request, QJsonDocument(body).toJson(QJsonDocument::Compact)); + reply->setProperty("endpoint", Endpoint::Logout); +} + void MatrixClient::registerUser(const QString &user, const QString &pass, const QString &server) { setServer(server);