diff options
Diffstat (limited to 'src/net')
-rw-r--r-- | src/net/instance.cpp | 6 | ||||
-rw-r--r-- | src/net/instance.h | 11 | ||||
-rw-r--r-- | src/net/mastodon_instance.cpp | 15 | ||||
-rw-r--r-- | src/net/mastodon_instance.h | 2 |
4 files changed, 20 insertions, 14 deletions
diff --git a/src/net/instance.cpp b/src/net/instance.cpp index 6982f64..8b2ad2d 100644 --- a/src/net/instance.cpp +++ b/src/net/instance.cpp @@ -4,15 +4,15 @@ #endif #include "src/settings_interface.h" -Instance* Instance::create_instance() { +InstancePtr Instance::create_instance() { return create_instance(SettingsInterface::quick_read_setting<AppSettingsTypes::InstanceType>("net/instance/type")); } -Instance* Instance::create_instance(AppSettingsTypes::InstanceType type) { +InstancePtr Instance::create_instance(AppSettingsTypes::InstanceType type) { switch (type) { #ifdef MASTODONPP_BUILD case AppSettingsTypes::InstanceType::MASTODON: - return new MastodonInstance; + return std::make_shared<MastodonInstance>(); #endif default: return nullptr; diff --git a/src/net/instance.h b/src/net/instance.h index 2b7eec7..79c0b12 100644 --- a/src/net/instance.h +++ b/src/net/instance.h @@ -1,6 +1,11 @@ #pragma once #include "src/activitypub/appost.h" #include "src/settings_interface.h" +#include <QMetaType> +#include <memory> + +class Instance; +typedef std::shared_ptr<Instance> InstancePtr; class Instance { public: @@ -10,7 +15,7 @@ public: }; virtual ~Instance() {}; - virtual APPost* get_post_from_url(const QString &url) = 0; + virtual APPostPtr get_post_from_url(const QString &url) = 0; // Returns the URI the user has to visit in their browser virtual QString oauth2_step1(); // auth_code is the code given by the user through the URI @@ -18,7 +23,7 @@ public: virtual OAuth2Step2 oauth2_step2(const QString &auth_code); bool supports_oauth2 = false; // Creates an instance by reading the type from settings - static Instance* create_instance(); + static InstancePtr create_instance(); // Creates a specific type of instance, for use with SettingsDialog - static Instance* create_instance(AppSettingsTypes::InstanceType type); + static InstancePtr create_instance(AppSettingsTypes::InstanceType type); }; diff --git a/src/net/mastodon_instance.cpp b/src/net/mastodon_instance.cpp index 8b88221..17d23ca 100644 --- a/src/net/mastodon_instance.cpp +++ b/src/net/mastodon_instance.cpp @@ -1,6 +1,7 @@ #include "mastodon_instance.h" #include "src/activitypub/appost.h" #include "src/activitypub/apquestion.h" +#include "src/activitypub/fields.h" #include "src/settings_interface.h" #include "src/types.h" @@ -10,7 +11,7 @@ #include <QUrl> #include <QDebug> -APPost* post_from_json(const QJsonObject &status); +APPostPtr post_from_json(const QJsonObject &status); MastodonInstance::MastodonInstance() : instance(SettingsInterface::quick_read_setting<QString>("net/instance/address").toStdString(), SettingsInterface::quick_read_setting<QString>("net/instance/token").toStdString()), connection(instance) { @@ -43,7 +44,7 @@ Instance::OAuth2Step2 MastodonInstance::oauth2_step2(const QString &auth_code) { return {(bool)answer, token}; } -APPost* MastodonInstance::get_post_from_url(const QString &url) { +APPostPtr MastodonInstance::get_post_from_url(const QString &url) { auto answer{connection.get(mastodonpp::API::v2::search, { {"q", QUrl::toPercentEncoding(url).toStdString()}, {"type", "statuses"}, @@ -57,18 +58,18 @@ APPost* MastodonInstance::get_post_from_url(const QString &url) { qDebug() << doc; if (obj.contains("statuses")) { QJsonObject status = obj["statuses"].toArray().first().toObject(); - if (status.isEmpty()) return new APPost({.content="invalid"}); // Invalid + if (status.isEmpty()) return std::make_shared<APPost>(APObjectFields {.content="invalid"}); // Invalid post return post_from_json(status); } } - return new APPost({.content="connection error"}); + return std::make_shared<APPost>(APObjectFields {.content="connection error"}); } // Create a filled APPost object from a Status entity (https://docs.joinmastodon.org/entities/Status/) // We have to return a pointer as else I can't get derived classes of APPost to render properly: the additional methods are ignored. // Furthermore, using pointers removes unecessary copying, even if that can be reduced by return value optimization. -APPost* post_from_json(const QJsonObject &status) { +APPostPtr post_from_json(const QJsonObject &status) { APObjectFields fields; bool is_question = status.contains("poll"); @@ -128,7 +129,7 @@ APPost* post_from_json(const QJsonObject &status) { } if (is_question) - return new APQuestion(fields); + return std::make_shared<APQuestion>(fields); else - return new APPost(fields); + return std::make_shared<APPost>(fields); } diff --git a/src/net/mastodon_instance.h b/src/net/mastodon_instance.h index 983bef6..7f9b058 100644 --- a/src/net/mastodon_instance.h +++ b/src/net/mastodon_instance.h @@ -7,7 +7,7 @@ class MastodonInstance : public Instance { public: MastodonInstance(); ~MastodonInstance(); - APPost* get_post_from_url(const QString &url); + APPostPtr get_post_from_url(const QString &url); QString oauth2_step1(); OAuth2Step2 oauth2_step2(const QString &auth_code); bool supports_oauth2 = true; |