diff options
author | Konstantinos Sideris <sideris.konstantin@gmail.com> | 2017-12-03 02:47:37 +0200 |
---|---|---|
committer | Konstantinos Sideris <sideris.konstantin@gmail.com> | 2017-12-03 02:47:37 +0200 |
commit | 914bdecc0b8e57a3b4e75b218b02909172ada7e3 (patch) | |
tree | 115c7d85758e503921cd5443af9e007b07849625 /src | |
parent | f4f78b1d8a0e5f7065b50ae40766acb6521131b1 (diff) |
Initial integration with matrix-structs
Diffstat (limited to 'src')
-rw-r--r-- | src/ChatPage.cc | 4 | ||||
-rw-r--r-- | src/Login.cc | 22 | ||||
-rw-r--r-- | src/MatrixClient.cc | 48 | ||||
-rw-r--r-- | src/Profile.cc | 35 | ||||
-rw-r--r-- | src/Versions.cc | 62 |
5 files changed, 20 insertions, 151 deletions
diff --git a/src/ChatPage.cc b/src/ChatPage.cc index f773ff2c..bbed7359 100644 --- a/src/ChatPage.cc +++ b/src/ChatPage.cc @@ -260,9 +260,9 @@ ChatPage::ChatPage(QSharedPointer<MatrixClient> client, QWidget *parent) this, SLOT(syncFailed(const QString &))); connect(client_.data(), - SIGNAL(getOwnProfileResponse(const QUrl &, const QString &)), + &MatrixClient::getOwnProfileResponse, this, - SLOT(updateOwnProfileInfo(const QUrl &, const QString &))); + &ChatPage::updateOwnProfileInfo); connect(client_.data(), SIGNAL(ownAvatarRetrieved(const QPixmap &)), this, diff --git a/src/Login.cc b/src/Login.cc index ca41d019..3e681a76 100644 --- a/src/Login.cc +++ b/src/Login.cc @@ -47,25 +47,3 @@ LoginRequest::serialize() noexcept return QJsonDocument(body).toJson(QJsonDocument::Compact); } - -void -LoginResponse::deserialize(const QJsonDocument &data) -{ - if (!data.isObject()) - throw DeserializationException("Login response is not a JSON object"); - - QJsonObject object = data.object(); - - if (object.value("access_token") == QJsonValue::Undefined) - throw DeserializationException("Login: missing access_token param"); - - if (object.value("home_server") == QJsonValue::Undefined) - throw DeserializationException("Login: missing home_server param"); - - if (object.value("user_id") == QJsonValue::Undefined) - throw DeserializationException("Login: missing user_id param"); - - access_token_ = object.value("access_token").toString(); - home_server_ = object.value("home_server").toString(); - user_id_ = object.value("user_id").toString(); -} diff --git a/src/MatrixClient.cc b/src/MatrixClient.cc index 3326a47f..1887eb95 100644 --- a/src/MatrixClient.cc +++ b/src/MatrixClient.cc @@ -31,11 +31,11 @@ #include "Login.h" #include "MatrixClient.h" #include "MessageEvent.h" -#include "Profile.h" #include "Register.h" #include "RoomMessages.h" #include "Sync.h" -#include "Versions.h" + +#include "mtx.hpp" MatrixClient::MatrixClient(QString server, QObject *parent) : QNetworkAccessManager(parent) @@ -99,21 +99,18 @@ MatrixClient::login(const QString &username, const QString &password) noexcept return; } - auto data = reply->readAll(); - auto json = QJsonDocument::fromJson(data); - - LoginResponse response; - try { - response.deserialize(json); + mtx::responses::Login login = + nlohmann::json::parse(reply->readAll().data()); auto hostname = server_.host(); if (server_.port() > 0) hostname = QString("%1:%2").arg(server_.host()).arg(server_.port()); - emit loginSuccess( - response.getUserId(), hostname, response.getAccessToken()); + emit loginSuccess(QString::fromStdString(login.user_id.toString()), + hostname, + QString::fromStdString(login.access_token)); } catch (DeserializationException &e) { qWarning() << "Malformed JSON response" << e.what(); emit loginError(tr("Malformed response. Possibly not a Matrix server")); @@ -420,18 +417,12 @@ MatrixClient::versions() noexcept return; } - auto data = reply->readAll(); - auto json = QJsonDocument::fromJson(data); - - VersionsResponse response; - try { - response.deserialize(json); - if (!response.isVersionSupported(0, 2, 0)) - emit versionError("Server does not support required API version."); - else - emit versionSuccess(); - } catch (DeserializationException &e) { + mtx::responses::Versions versions = + nlohmann::json::parse(reply->readAll().data()); + + emit versionSuccess(); + } catch (std::exception &e) { emit versionError("Malformed response. Possibly not a Matrix server"); } }); @@ -465,16 +456,13 @@ MatrixClient::getOwnProfile() noexcept return; } - auto data = reply->readAll(); - auto json = QJsonDocument::fromJson(data); - - ProfileResponse response; - try { - response.deserialize(json); - emit getOwnProfileResponse(response.getAvatarUrl(), - response.getDisplayName()); - } catch (DeserializationException &e) { + mtx::responses::Profile profile = + nlohmann::json::parse(reply->readAll().data()); + + emit getOwnProfileResponse(QUrl(QString::fromStdString(profile.avatar_url)), + QString::fromStdString(profile.display_name)); + } catch (std::exception &e) { qWarning() << "Profile:" << e.what(); } }); diff --git a/src/Profile.cc b/src/Profile.cc deleted file mode 100644 index 8eaafe07..00000000 --- a/src/Profile.cc +++ /dev/null @@ -1,35 +0,0 @@ -/* - * nheko Copyright (C) 2017 Konstantinos Sideris <siderisk@auth.gr> - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -#include "Deserializable.h" - -#include "Profile.h" - -void -ProfileResponse::deserialize(const QJsonDocument &data) -{ - if (!data.isObject()) - throw DeserializationException("Response is not a JSON object"); - - QJsonObject object = data.object(); - - if (object.contains("avatar_url")) - avatar_url_ = QUrl(object.value("avatar_url").toString()); - - if (object.contains("displayname")) - display_name_ = object.value("displayname").toString(); -} diff --git a/src/Versions.cc b/src/Versions.cc deleted file mode 100644 index 9de1aafe..00000000 --- a/src/Versions.cc +++ /dev/null @@ -1,62 +0,0 @@ -/* - * nheko Copyright (C) 2017 Jan Solanti <jhs@psonet.com> - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -#include <QJsonArray> -#include <QRegExp> - -#include "Deserializable.h" -#include "Versions.h" - -void -VersionsResponse::deserialize(const QJsonDocument &data) -{ - if (!data.isObject()) - throw DeserializationException("Versions response is not a JSON object"); - - QJsonObject object = data.object(); - - if (object.value("versions") == QJsonValue::Undefined) - throw DeserializationException("Versions: missing version list"); - - auto versions = object.value("versions").toArray(); - for (auto const &elem : versions) { - QString str = elem.toString(); - QRegExp rx("r(\\d+)\\.(\\d+)\\.(\\d+)"); - - if (rx.indexIn(str) == -1) - throw DeserializationException( - "Invalid version string in versions response"); - - struct Version_ v; - v.major_ = rx.cap(1).toUInt(); - v.minor_ = rx.cap(2).toUInt(); - v.patch_ = rx.cap(3).toUInt(); - - supported_versions_.push_back(v); - } -} - -bool -VersionsResponse::isVersionSupported(unsigned int major, unsigned int minor, unsigned int patch) -{ - for (auto &v : supported_versions_) { - if (v.major_ == major && v.minor_ == minor && v.patch_ >= patch) - return true; - } - - return false; -} |