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. --- jobs/generated/login.cpp | 89 ++++++++++++++++++++++++++++++++++++++++++++++++ jobs/generated/login.h | 38 +++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 jobs/generated/login.cpp create mode 100644 jobs/generated/login.h (limited to 'jobs/generated') diff --git a/jobs/generated/login.cpp b/jobs/generated/login.cpp new file mode 100644 index 00000000..6e8294e7 --- /dev/null +++ b/jobs/generated/login.cpp @@ -0,0 +1,89 @@ +/****************************************************************************** + * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN + */ + + +#include "login.h" + +#include "jobs/converters.h" +#include + +using namespace QMatrixClient; + +static const auto basePath = QStringLiteral("/_matrix/client/r0"); + +class LoginJob::Private +{ + public: + QString user_id; + QString access_token; + QString home_server; + QString device_id; + +}; + +LoginJob::LoginJob(QString type, QString user, QString medium, QString address, QString password, QString token, QString device_id, QString initial_device_display_name) + : BaseJob(HttpVerb::Post, "LoginJob", + basePath % "/login", + Query { }, Data { }, false + ), d(new Private) +{ + Data _data; + _data.insert("type", toJson(type)); + if (!user.isEmpty()) + _data.insert("user", toJson(user)); + if (!medium.isEmpty()) + _data.insert("medium", toJson(medium)); + if (!address.isEmpty()) + _data.insert("address", toJson(address)); + if (!password.isEmpty()) + _data.insert("password", toJson(password)); + if (!token.isEmpty()) + _data.insert("token", toJson(token)); + if (!device_id.isEmpty()) + _data.insert("device_id", toJson(device_id)); + if (!initial_device_display_name.isEmpty()) + _data.insert("initial_device_display_name", toJson(initial_device_display_name)); + setRequestData(_data); +} + +LoginJob::~LoginJob() +{ + delete d; +} + +const QString& LoginJob::user_id() const +{ + return d->user_id; +} + +const QString& LoginJob::access_token() const +{ + return d->access_token; +} + +const QString& LoginJob::home_server() const +{ + return d->home_server; +} + +const QString& LoginJob::device_id() const +{ + return d->device_id; +} + +BaseJob::Status LoginJob::parseJson(const QJsonDocument& data) +{ + auto json = data.object(); + + d->user_id = fromJson(json.value("user_id")); + + d->access_token = fromJson(json.value("access_token")); + + d->home_server = fromJson(json.value("home_server")); + + d->device_id = fromJson(json.value("device_id")); + + return Success; +} + diff --git a/jobs/generated/login.h b/jobs/generated/login.h new file mode 100644 index 00000000..dc89206d --- /dev/null +++ b/jobs/generated/login.h @@ -0,0 +1,38 @@ +/****************************************************************************** + * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN + */ + + +#pragma once + +#include "../basejob.h" + +#include + + +namespace QMatrixClient +{ + + // Operations + + class LoginJob : public BaseJob + { + public: + explicit LoginJob(QString type, QString user = {}, QString medium = {}, QString address = {}, QString password = {}, QString token = {}, QString device_id = {}, QString initial_device_display_name = {}); + + ~LoginJob() override; + + const QString& user_id() const; + const QString& access_token() const; + const QString& home_server() const; + const QString& device_id() const; + + protected: + Status parseJson(const QJsonDocument& data) override; + + private: + class Private; + Private* d; + }; + +} // namespace QMatrixClient -- cgit v1.2.3 From 1cc5d08159936e87f7c1dc791d6ab3c0e46e7035 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Wed, 1 Nov 2017 08:38:59 +0300 Subject: Move converters.h out of jobs/ Because they are applicable beyond jobs. --- converters.h | 121 ++++++++++++++++++++++++++++++++++++++++++++ jobs/converters.h | 121 -------------------------------------------- jobs/generated/banning.cpp | 2 +- jobs/generated/inviting.cpp | 2 +- jobs/generated/kicking.cpp | 2 +- jobs/generated/leaving.cpp | 2 +- jobs/generated/login.cpp | 2 +- jobs/generated/logout.cpp | 2 +- jobs/generated/profile.cpp | 2 +- 9 files changed, 128 insertions(+), 128 deletions(-) create mode 100644 converters.h delete mode 100644 jobs/converters.h (limited to 'jobs/generated') diff --git a/converters.h b/converters.h new file mode 100644 index 00000000..f6e850c6 --- /dev/null +++ b/converters.h @@ -0,0 +1,121 @@ +/****************************************************************************** +* Copyright (C) 2017 Kitsune Ral +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2.1 of the License, or (at your option) any later version. +* +* This library 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 +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, write to the Free Software +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#pragma once + +#include +#include // Includes +#include + +namespace QMatrixClient +{ + template + inline QJsonValue toJson(T val) + { + return QJsonValue(val); + } + + template + inline QJsonValue toJson(const QVector& vals) + { + QJsonArray ar; + for (const auto& v: vals) + ar.push_back(toJson(v)); + return ar; + } + + inline QJsonValue toJson(const QStringList& strings) + { + return QJsonArray::fromStringList(strings); + } + + template + struct FromJson + { + T operator()(QJsonValue jv) const { return static_cast(jv); } + }; + + template + inline T fromJson(const QJsonValue& jv) + { + return FromJson()(jv); + } + + template <> struct FromJson + { + bool operator()(QJsonValue jv) const { return jv.toBool(); } + }; + + template <> struct FromJson + { + int operator()(QJsonValue jv) const { return jv.toInt(); } + }; + + template <> struct FromJson + { + double operator()(QJsonValue jv) const { return jv.toDouble(); } + }; + + template <> struct FromJson + { + qint64 operator()(QJsonValue jv) const { return qint64(jv.toDouble()); } + }; + + template <> struct FromJson + { + QString operator()(QJsonValue jv) const { return jv.toString(); } + }; + + template <> struct FromJson + { + QDateTime operator()(QJsonValue jv) const + { + return QDateTime::fromMSecsSinceEpoch(fromJson(jv), Qt::UTC); + } + }; + + template <> struct FromJson + { + QDate operator()(QJsonValue jv) const + { + return fromJson(jv).date(); + } + }; + + template <> struct FromJson + { + QJsonObject operator()(QJsonValue jv) const { return jv.toObject(); } + }; + + template <> struct FromJson + { + QJsonArray operator()(QJsonValue jv) const { return jv.toArray(); } + }; + + template struct FromJson> + { + QVector operator()(QJsonValue jv) const + { + const auto jsonArray = jv.toArray(); + QVector vect; vect.resize(jsonArray.size()); + std::transform(jsonArray.begin(), jsonArray.end(), + vect.begin(), FromJson()); + return vect; + } + }; +} // namespace QMatrixClient diff --git a/jobs/converters.h b/jobs/converters.h deleted file mode 100644 index f6e850c6..00000000 --- a/jobs/converters.h +++ /dev/null @@ -1,121 +0,0 @@ -/****************************************************************************** -* Copyright (C) 2017 Kitsune Ral -* -* This library is free software; you can redistribute it and/or -* modify it under the terms of the GNU Lesser General Public -* License as published by the Free Software Foundation; either -* version 2.1 of the License, or (at your option) any later version. -* -* This library 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 -* Lesser General Public License for more details. -* -* You should have received a copy of the GNU Lesser General Public -* License along with this library; if not, write to the Free Software -* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -#pragma once - -#include -#include // Includes -#include - -namespace QMatrixClient -{ - template - inline QJsonValue toJson(T val) - { - return QJsonValue(val); - } - - template - inline QJsonValue toJson(const QVector& vals) - { - QJsonArray ar; - for (const auto& v: vals) - ar.push_back(toJson(v)); - return ar; - } - - inline QJsonValue toJson(const QStringList& strings) - { - return QJsonArray::fromStringList(strings); - } - - template - struct FromJson - { - T operator()(QJsonValue jv) const { return static_cast(jv); } - }; - - template - inline T fromJson(const QJsonValue& jv) - { - return FromJson()(jv); - } - - template <> struct FromJson - { - bool operator()(QJsonValue jv) const { return jv.toBool(); } - }; - - template <> struct FromJson - { - int operator()(QJsonValue jv) const { return jv.toInt(); } - }; - - template <> struct FromJson - { - double operator()(QJsonValue jv) const { return jv.toDouble(); } - }; - - template <> struct FromJson - { - qint64 operator()(QJsonValue jv) const { return qint64(jv.toDouble()); } - }; - - template <> struct FromJson - { - QString operator()(QJsonValue jv) const { return jv.toString(); } - }; - - template <> struct FromJson - { - QDateTime operator()(QJsonValue jv) const - { - return QDateTime::fromMSecsSinceEpoch(fromJson(jv), Qt::UTC); - } - }; - - template <> struct FromJson - { - QDate operator()(QJsonValue jv) const - { - return fromJson(jv).date(); - } - }; - - template <> struct FromJson - { - QJsonObject operator()(QJsonValue jv) const { return jv.toObject(); } - }; - - template <> struct FromJson - { - QJsonArray operator()(QJsonValue jv) const { return jv.toArray(); } - }; - - template struct FromJson> - { - QVector operator()(QJsonValue jv) const - { - const auto jsonArray = jv.toArray(); - QVector vect; vect.resize(jsonArray.size()); - std::transform(jsonArray.begin(), jsonArray.end(), - vect.begin(), FromJson()); - return vect; - } - }; -} // namespace QMatrixClient diff --git a/jobs/generated/banning.cpp b/jobs/generated/banning.cpp index 9fc5810a..c47d3419 100644 --- a/jobs/generated/banning.cpp +++ b/jobs/generated/banning.cpp @@ -5,7 +5,7 @@ #include "banning.h" -#include "jobs/converters.h" +#include "converters.h" #include using namespace QMatrixClient; diff --git a/jobs/generated/inviting.cpp b/jobs/generated/inviting.cpp index 95ba658d..11384c5e 100644 --- a/jobs/generated/inviting.cpp +++ b/jobs/generated/inviting.cpp @@ -5,7 +5,7 @@ #include "inviting.h" -#include "jobs/converters.h" +#include "converters.h" #include using namespace QMatrixClient; diff --git a/jobs/generated/kicking.cpp b/jobs/generated/kicking.cpp index 2e6797d6..e75b900a 100644 --- a/jobs/generated/kicking.cpp +++ b/jobs/generated/kicking.cpp @@ -5,7 +5,7 @@ #include "kicking.h" -#include "jobs/converters.h" +#include "converters.h" #include using namespace QMatrixClient; diff --git a/jobs/generated/leaving.cpp b/jobs/generated/leaving.cpp index 7fed347b..e443612e 100644 --- a/jobs/generated/leaving.cpp +++ b/jobs/generated/leaving.cpp @@ -5,7 +5,7 @@ #include "leaving.h" -#include "jobs/converters.h" +#include "converters.h" #include using namespace QMatrixClient; diff --git a/jobs/generated/login.cpp b/jobs/generated/login.cpp index 6e8294e7..0c57c684 100644 --- a/jobs/generated/login.cpp +++ b/jobs/generated/login.cpp @@ -5,7 +5,7 @@ #include "login.h" -#include "jobs/converters.h" +#include "converters.h" #include using namespace QMatrixClient; diff --git a/jobs/generated/logout.cpp b/jobs/generated/logout.cpp index b750efe2..a5848e7c 100644 --- a/jobs/generated/logout.cpp +++ b/jobs/generated/logout.cpp @@ -5,7 +5,7 @@ #include "logout.h" -#include "jobs/converters.h" +#include "converters.h" #include using namespace QMatrixClient; diff --git a/jobs/generated/profile.cpp b/jobs/generated/profile.cpp index 9d20a480..201bca79 100644 --- a/jobs/generated/profile.cpp +++ b/jobs/generated/profile.cpp @@ -5,7 +5,7 @@ #include "profile.h" -#include "jobs/converters.h" +#include "converters.h" #include using namespace QMatrixClient; -- cgit v1.2.3