diff options
Diffstat (limited to 'Swiften/Queries')
-rw-r--r-- | Swiften/Queries/GenericRequest.h | 5 | ||||
-rw-r--r-- | Swiften/Queries/PubSubRequest.h | 90 | ||||
-rw-r--r-- | Swiften/Queries/Request.cpp | 3 | ||||
-rw-r--r-- | Swiften/Queries/Request.h | 11 |
4 files changed, 106 insertions, 3 deletions
diff --git a/Swiften/Queries/GenericRequest.h b/Swiften/Queries/GenericRequest.h index 68c86c5..b1b7f8a 100644 --- a/Swiften/Queries/GenericRequest.h +++ b/Swiften/Queries/GenericRequest.h @@ -22,6 +22,9 @@ namespace Swift { template<typename PAYLOAD_TYPE> class GenericRequest : public Request { public: + typedef boost::shared_ptr<GenericRequest<PAYLOAD_TYPE> > ref; + + public: /** * Create a request suitable for client use. * @param type Iq type - Get or Set. @@ -61,7 +64,7 @@ namespace Swift { onResponse(boost::dynamic_pointer_cast<PAYLOAD_TYPE>(payload), error); } - protected: + public: boost::shared_ptr<PAYLOAD_TYPE> getPayloadGeneric() const { return boost::dynamic_pointer_cast<PAYLOAD_TYPE>(getPayload()); } diff --git a/Swiften/Queries/PubSubRequest.h b/Swiften/Queries/PubSubRequest.h new file mode 100644 index 0000000..55bf9e3 --- /dev/null +++ b/Swiften/Queries/PubSubRequest.h @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2013 Remko Tronçon + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#pragma once + +#include <boost/smart_ptr/make_shared.hpp> + +#include <Swiften/Base/boost_bsignals.h> +#include <Swiften/Queries/Request.h> +#include <Swiften/Elements/ContainerPayload.h> +#include <Swiften/PubSub/PubSubUtil.h> +#include <Swiften/Elements/PubSub.h> +#include <Swiften/Elements/PubSubOwnerPubSub.h> +#include <Swiften/Elements/PubSubCreate.h> +#include <Swiften/Elements/PubSubSubscribe.h> +#include <Swiften/Elements/PubSubAffiliations.h> +#include <Swiften/Elements/PubSubDefault.h> +#include <Swiften/Elements/PubSubItems.h> +#include <Swiften/Elements/PubSubPublish.h> +#include <Swiften/Elements/PubSubRetract.h> +#include <Swiften/Elements/PubSubSubscription.h> +#include <Swiften/Elements/PubSubSubscriptions.h> +#include <Swiften/Elements/PubSubUnsubscribe.h> +#include <Swiften/Elements/PubSubOwnerAffiliations.h> +#include <Swiften/Elements/PubSubOwnerConfigure.h> +#include <Swiften/Elements/PubSubOwnerDefault.h> +#include <Swiften/Elements/PubSubOwnerDelete.h> +#include <Swiften/Elements/PubSubOwnerPurge.h> +#include <Swiften/Elements/PubSubOwnerSubscriptions.h> + +namespace Swift { + namespace Detail { + template<typename T> + struct PubSubPayloadTraits; + +#define SWIFTEN_PUBSUB_DECLARE_PAYLOAD_TRAITS(PAYLOAD, CONTAINER, RESPONSE) \ + template<> struct PubSubPayloadTraits< PAYLOAD > { \ + typedef CONTAINER ContainerType; \ + typedef RESPONSE ResponseType; \ + }; + + SWIFTEN_PUBSUB_FOREACH_PUBSUB_PAYLOAD_TYPE( + SWIFTEN_PUBSUB_DECLARE_PAYLOAD_TRAITS) + } + + template<typename T> + class PubSubRequest : public Request { + typedef typename Detail::PubSubPayloadTraits<T>::ContainerType ContainerType; + typedef typename Detail::PubSubPayloadTraits<T>::ResponseType ResponseType; + + public: + PubSubRequest( + IQ::Type type, + const JID& receiver, + boost::shared_ptr<T> payload, + IQRouter* router) : + Request(type, receiver, router) { + boost::shared_ptr<ContainerType> wrapper = boost::make_shared<ContainerType>(); + wrapper->setPayload(payload); + setPayload(wrapper); + } + + PubSubRequest( + IQ::Type type, + const JID& sender, + const JID& receiver, + boost::shared_ptr<T> payload, + IQRouter* router) : + Request(type, sender, receiver, router) { + boost::shared_ptr<ContainerType> wrapper = boost::make_shared<ContainerType>(); + wrapper->setPayload(payload); + setPayload(wrapper); + } + + virtual void handleResponse( + boost::shared_ptr<Payload> payload, ErrorPayload::ref error) { + boost::shared_ptr<ResponseType> result; + if (boost::shared_ptr<ContainerType> container = boost::dynamic_pointer_cast<ContainerType>(payload)) { + result = boost::dynamic_pointer_cast<ResponseType>(container->getPayload()); + } + onResponse(result, error); + } + + public: + boost::signal<void (boost::shared_ptr<ResponseType>, ErrorPayload::ref)> onResponse; + }; +} diff --git a/Swiften/Queries/Request.cpp b/Swiften/Queries/Request.cpp index 422f36c..40c8f60 100644 --- a/Swiften/Queries/Request.cpp +++ b/Swiften/Queries/Request.cpp @@ -23,7 +23,7 @@ Request::Request(IQ::Type type, const JID& sender, const JID& receiver, boost::s Request::Request(IQ::Type type, const JID& sender, const JID& receiver, IQRouter* router) : router_(router), type_(type), sender_(sender), receiver_(receiver), sent_(false) { } -void Request::send() { +std::string Request::send() { assert(payload_); assert(!sent_); sent_ = true; @@ -43,6 +43,7 @@ void Request::send() { } router_->sendIQ(iq); + return id_; } bool Request::handleIQ(boost::shared_ptr<IQ> iq) { diff --git a/Swiften/Queries/Request.h b/Swiften/Queries/Request.h index 5e3a4b8..f17cc11 100644 --- a/Swiften/Queries/Request.h +++ b/Swiften/Queries/Request.h @@ -24,12 +24,21 @@ namespace Swift { */ class SWIFTEN_API Request : public IQHandler, public boost::enable_shared_from_this<Request> { public: - void send(); + std::string send(); const JID& getReceiver() const { return receiver_; } + /** + * Returns the ID of this request. + * This will only be set after send() is called. + */ + const std::string& getID() const { + return id_; + } + + protected: /** * Constructs a request of a certain type to a specific receiver, and attaches the given |