diff options
author | Remko Tronçon <git@el-tramo.be> | 2010-09-15 21:10:25 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2010-09-15 21:17:47 (GMT) |
commit | 127b96597bcb1b5d5a823222f3dd28f4b7aa354a (patch) | |
tree | 1e87148e7d45fcd46e3fee3904eeb9046a834d83 /Swift/Controllers/XMPPEvents | |
parent | e215caa4811f4c80c9f6767c523e649d5058cc61 (diff) | |
download | swift-contrib-127b96597bcb1b5d5a823222f3dd28f4b7aa354a.zip swift-contrib-127b96597bcb1b5d5a823222f3dd28f4b7aa354a.tar.bz2 |
Moved events from Swiften to Swift.
Diffstat (limited to 'Swift/Controllers/XMPPEvents')
-rw-r--r-- | Swift/Controllers/XMPPEvents/ErrorEvent.h | 31 | ||||
-rw-r--r-- | Swift/Controllers/XMPPEvents/EventController.cpp | 38 | ||||
-rw-r--r-- | Swift/Controllers/XMPPEvents/EventController.h | 33 | ||||
-rw-r--r-- | Swift/Controllers/XMPPEvents/MessageEvent.h | 39 | ||||
-rw-r--r-- | Swift/Controllers/XMPPEvents/StanzaEvent.h | 28 | ||||
-rw-r--r-- | Swift/Controllers/XMPPEvents/SubscriptionRequestEvent.h | 46 |
6 files changed, 215 insertions, 0 deletions
diff --git a/Swift/Controllers/XMPPEvents/ErrorEvent.h b/Swift/Controllers/XMPPEvents/ErrorEvent.h new file mode 100644 index 0000000..3f78109 --- /dev/null +++ b/Swift/Controllers/XMPPEvents/ErrorEvent.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2010 Kevin Smith + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#pragma once + +#include <cassert> + +#include "Swiften/Base/boost_bsignals.h" +#include <boost/shared_ptr.hpp> + +#include "Swift/Controllers/XMPPEvents/StanzaEvent.h" +#include "Swiften/Base/String.h" +#include "Swiften/JID/JID.h" + +namespace Swift { + class ErrorEvent : public StanzaEvent { + public: + ErrorEvent(const JID& jid, const String& text) : jid_(jid), text_(text){}; + virtual ~ErrorEvent(){}; + const JID& getJID() const {return jid_;}; + const String& getText() const {return text_;}; + + private: + JID jid_; + String text_; + }; +} + diff --git a/Swift/Controllers/XMPPEvents/EventController.cpp b/Swift/Controllers/XMPPEvents/EventController.cpp new file mode 100644 index 0000000..b14ff46 --- /dev/null +++ b/Swift/Controllers/XMPPEvents/EventController.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2010 Kevin Smith + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#include "Swift/Controllers/XMPPEvents/EventController.h" + +#include <boost/bind.hpp> +#include <algorithm> + +#include "Swift/Controllers/XMPPEvents/MessageEvent.h" +#include "Swift/Controllers/XMPPEvents/ErrorEvent.h" +#include "Swift/Controllers/XMPPEvents/SubscriptionRequestEvent.h" + +namespace Swift { + +EventController::EventController() { +} + +void EventController::handleIncomingEvent(boost::shared_ptr<StanzaEvent> sourceEvent) { + boost::shared_ptr<MessageEvent> messageEvent = boost::dynamic_pointer_cast<MessageEvent>(sourceEvent); + boost::shared_ptr<SubscriptionRequestEvent> subscriptionEvent = boost::dynamic_pointer_cast<SubscriptionRequestEvent>(sourceEvent); + boost::shared_ptr<ErrorEvent> errorEvent = boost::dynamic_pointer_cast<ErrorEvent>(sourceEvent); + if ((messageEvent && messageEvent->isReadable()) || subscriptionEvent || errorEvent) { + events_.push_back(sourceEvent); + sourceEvent->onConclusion.connect(boost::bind(&EventController::handleEventConcluded, this, sourceEvent)); + onEventQueueLengthChange(events_.size()); + onEventQueueEventAdded(sourceEvent); + } +} + +void EventController::handleEventConcluded(boost::shared_ptr<StanzaEvent> event) { + events_.erase(std::remove(events_.begin(), events_.end(), event), events_.end()); + onEventQueueLengthChange(events_.size()); +} + +} diff --git a/Swift/Controllers/XMPPEvents/EventController.h b/Swift/Controllers/XMPPEvents/EventController.h new file mode 100644 index 0000000..b405556 --- /dev/null +++ b/Swift/Controllers/XMPPEvents/EventController.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2010 Kevin Smith + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#ifndef SWIFTEN_EventController_H +#define SWIFTEN_EventController_H + + +#include "Swiften/Base/boost_bsignals.h" +#include <boost/shared_ptr.hpp> +#include <vector> + +#include "Swift/Controllers/XMPPEvents/StanzaEvent.h" +#include "Swift/Controllers/XMPPEvents/MessageEvent.h" + +namespace Swift { + class EventController { + public: + EventController(); + void handleIncomingEvent(boost::shared_ptr<StanzaEvent> sourceEvent); + boost::signal<void (int)> onEventQueueLengthChange; + boost::signal<void (boost::shared_ptr<StanzaEvent>)> onEventQueueEventAdded; + + private: + void handleEventConcluded(boost::shared_ptr<StanzaEvent> event); + std::vector<boost::shared_ptr<StanzaEvent> > events_; + }; +} +#endif + + diff --git a/Swift/Controllers/XMPPEvents/MessageEvent.h b/Swift/Controllers/XMPPEvents/MessageEvent.h new file mode 100644 index 0000000..313ad78 --- /dev/null +++ b/Swift/Controllers/XMPPEvents/MessageEvent.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2010 Kevin Smith + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#ifndef SWIFTEN_MessageEvent_H +#define SWIFTEN_MessageEvent_H + +#include <cassert> + +#include "Swiften/Base/boost_bsignals.h" +#include <boost/shared_ptr.hpp> + +#include "Swift/Controllers/XMPPEvents/StanzaEvent.h" +#include "Swiften/Elements/Message.h" + +namespace Swift { + class MessageEvent : public StanzaEvent { + public: + MessageEvent(boost::shared_ptr<Message> stanza) : stanza_(stanza){}; + + boost::shared_ptr<Message> getStanza() {return stanza_;} + + bool isReadable() { + return getStanza()->isError() || !getStanza()->getBody().isEmpty(); + } + + void read() { + assert (isReadable()); + conclude(); + } + + private: + boost::shared_ptr<Message> stanza_; + }; +} + +#endif diff --git a/Swift/Controllers/XMPPEvents/StanzaEvent.h b/Swift/Controllers/XMPPEvents/StanzaEvent.h new file mode 100644 index 0000000..78dd4dc --- /dev/null +++ b/Swift/Controllers/XMPPEvents/StanzaEvent.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2010 Kevin Smith + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#pragma once + +#include <boost/shared_ptr.hpp> +#include <boost/date_time/posix_time/posix_time.hpp> + +#include "Swiften/Base/boost_bsignals.h" + +namespace Swift { + class StanzaEvent { + public: + StanzaEvent() : time_(boost::posix_time::microsec_clock::universal_time()) {concluded_ = false;}; + virtual ~StanzaEvent() {}; + void conclude() {concluded_ = true; onConclusion();}; + /** Do not call this directly from outside the class */ + boost::signal<void()> onConclusion; + bool getConcluded() {return concluded_;}; + boost::posix_time::ptime getTime() {return time_;} + private: + bool concluded_; + boost::posix_time::ptime time_; + }; +} diff --git a/Swift/Controllers/XMPPEvents/SubscriptionRequestEvent.h b/Swift/Controllers/XMPPEvents/SubscriptionRequestEvent.h new file mode 100644 index 0000000..704a86c --- /dev/null +++ b/Swift/Controllers/XMPPEvents/SubscriptionRequestEvent.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2010 Kevin Smith + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#pragma once + +#include <cassert> + +#include "Swiften/Base/boost_bsignals.h" +#include <boost/shared_ptr.hpp> + +#include "Swift/Controllers/XMPPEvents/StanzaEvent.h" +#include "Swiften/Base/String.h" +#include "Swiften/JID/JID.h" + +namespace Swift { + class SubscriptionRequestEvent : public StanzaEvent { + public: + SubscriptionRequestEvent(const JID& jid, const String& reason) : jid_(jid), reason_(reason){}; + virtual ~SubscriptionRequestEvent(){}; + const JID& getJID() const {return jid_;}; + const String& getReason() const {return reason_;}; + boost::signal<void()> onAccept; + boost::signal<void()> onDecline; + void accept() { + onAccept(); + conclude(); + }; + + void decline() { + onDecline(); + conclude(); + }; + + void defer() { + conclude(); + } + + private: + JID jid_; + String reason_; + }; +} + |