summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Smith <git@kismith.co.uk>2010-02-16 09:05:37 (GMT)
committerKevin Smith <git@kismith.co.uk>2010-02-17 08:05:08 (GMT)
commit5a334fd9b676564a8915baad312d92bd86358eec (patch)
treeaaecbbccd9cddcb843c126b8c022f1d1e667efde /Swiften/Chat
parent231c2cb6d00061e70860626467107f4c63f359a0 (diff)
downloadswift-contrib-5a334fd9b676564a8915baad312d92bd86358eec.zip
swift-contrib-5a334fd9b676564a8915baad312d92bd86358eec.tar.bz2
Preliminary Chat State Notifications support.
Only covers Active and Composing (Which is very possibly all we care about).
Diffstat (limited to 'Swiften/Chat')
-rw-r--r--Swiften/Chat/ChatStateMessageSender.cpp17
-rw-r--r--Swiften/Chat/ChatStateMessageSender.h15
-rw-r--r--Swiften/Chat/ChatStateNotifier.cpp4
-rw-r--r--Swiften/Chat/ChatStateNotifier.h5
-rw-r--r--Swiften/Chat/ChatStateTracker.cpp24
-rw-r--r--Swiften/Chat/ChatStateTracker.h16
-rw-r--r--Swiften/Chat/UnitTest/ChatStateNotifierTest.cpp10
7 files changed, 80 insertions, 11 deletions
diff --git a/Swiften/Chat/ChatStateMessageSender.cpp b/Swiften/Chat/ChatStateMessageSender.cpp
index d053cb5..ad1495f 100644
--- a/Swiften/Chat/ChatStateMessageSender.cpp
+++ b/Swiften/Chat/ChatStateMessageSender.cpp
@@ -1,5 +1,22 @@
#include "Swiften/Chat/ChatStateMessageSender.h"
+#include <boost/bind.hpp>
+
+#include "Swiften/Client/StanzaChannel.h"
+
namespace Swift {
+ChatStateMessageSender::ChatStateMessageSender(ChatStateNotifier* notifier, StanzaChannel* stanzaChannel, const JID& contact) : contact_(contact) {
+ notifier_ = notifier;
+ stanzaChannel_ = stanzaChannel;
+ notifier_->onChatStateChanged.connect(boost::bind(&ChatStateMessageSender::handleChatStateChanged, this, _1));
+}
+
+void ChatStateMessageSender::handleChatStateChanged(ChatState::ChatStateType state) {
+ boost::shared_ptr<Message> message(new Message());
+ message->setTo(contact_);
+ message->addPayload(boost::shared_ptr<Payload>(new ChatState(state)));
+ stanzaChannel_->sendMessage(message);
+}
+
}
diff --git a/Swiften/Chat/ChatStateMessageSender.h b/Swiften/Chat/ChatStateMessageSender.h
index d27973d..aff0791 100644
--- a/Swiften/Chat/ChatStateMessageSender.h
+++ b/Swiften/Chat/ChatStateMessageSender.h
@@ -1,7 +1,20 @@
#pragma once
+#include "Swiften/Chat/ChatStateNotifier.h"
+#include "Swiften/Elements/ChatState.h"
+#include "Swiften/JID/JID.h"
+
namespace Swift {
+ class StanzaChannel;
class ChatStateMessageSender {
-
+ public:
+ ChatStateMessageSender(ChatStateNotifier* notifier, StanzaChannel* stanzaChannel, const JID& contact);
+ void setContact(const JID& contact) {contact_ = contact;};
+
+ private:
+ void handleChatStateChanged(ChatState::ChatStateType state);
+ ChatStateNotifier* notifier_;
+ StanzaChannel* stanzaChannel_;
+ JID contact_;
};
}
diff --git a/Swiften/Chat/ChatStateNotifier.cpp b/Swiften/Chat/ChatStateNotifier.cpp
index 432f708..7c6560f 100644
--- a/Swiften/Chat/ChatStateNotifier.cpp
+++ b/Swiften/Chat/ChatStateNotifier.cpp
@@ -16,7 +16,7 @@ void ChatStateNotifier::setContactHas85Caps(bool hasCaps) {
void ChatStateNotifier::setUserIsTyping() {
if (contactShouldReceiveStates() && !userIsTyping_) {
userIsTyping_ = true;
- onChatStateChanged(Composing);
+ onChatStateChanged(ChatState::Composing);
}
}
@@ -27,7 +27,7 @@ void ChatStateNotifier::userSentMessage() {
void ChatStateNotifier::userCancelledNewMessage() {
if (userIsTyping_) {
userIsTyping_ = false;
- onChatStateChanged(Active);
+ onChatStateChanged(ChatState::Active);
}
}
diff --git a/Swiften/Chat/ChatStateNotifier.h b/Swiften/Chat/ChatStateNotifier.h
index 90228b7..0ef4255 100644
--- a/Swiften/Chat/ChatStateNotifier.h
+++ b/Swiften/Chat/ChatStateNotifier.h
@@ -3,10 +3,11 @@
#include <boost/signals.hpp>
#include <boost/shared_ptr.hpp>
+#include "Swiften/Elements/ChatState.h"
+
namespace Swift {
class ChatStateNotifier {
public:
- enum ChatState {Active, Composing, Paused, Inactive, Gone};
ChatStateNotifier();
void setContactHas85Caps(bool hasCaps);
void setUserIsTyping();
@@ -15,7 +16,7 @@ namespace Swift {
void receivedMessageFromContact(bool hasActiveElement);
bool contactShouldReceiveStates();
- boost::signal<void (ChatState)> onChatStateChanged;
+ boost::signal<void (ChatState::ChatStateType)> onChatStateChanged;
private:
bool contactHas85Caps_;
bool isInConversation_;
diff --git a/Swiften/Chat/ChatStateTracker.cpp b/Swiften/Chat/ChatStateTracker.cpp
index 553d2f4..94e01eb 100644
--- a/Swiften/Chat/ChatStateTracker.cpp
+++ b/Swiften/Chat/ChatStateTracker.cpp
@@ -1,5 +1,29 @@
#include "Swiften/Chat/ChatStateTracker.h"
namespace Swift {
+ChatStateTracker::ChatStateTracker() {
+ currentState_ = ChatState::Gone;
+}
+
+void ChatStateTracker::handleMessageReceived(boost::shared_ptr<Message> message) {
+ boost::shared_ptr<ChatState> statePayload = message->getPayload<ChatState>();
+ if (statePayload) {
+ changeState(statePayload->getChatState());;
+ }
+}
+
+void ChatStateTracker::handlePresenceChange(boost::shared_ptr<Presence> newPresence, boost::shared_ptr<Presence>) {
+ if (newPresence->getType() == Presence::Unavailable) {
+ onChatStateChange(ChatState::Gone);
+ }
+}
+
+void ChatStateTracker::changeState(ChatState::ChatStateType state) {
+ printf("Comparing state %d to old state %d\n", state, currentState_);
+ if (state != currentState_) {
+ currentState_ = state;
+ onChatStateChange(state);
+ }
+}
}
diff --git a/Swiften/Chat/ChatStateTracker.h b/Swiften/Chat/ChatStateTracker.h
index 005c479..e612852 100644
--- a/Swiften/Chat/ChatStateTracker.h
+++ b/Swiften/Chat/ChatStateTracker.h
@@ -1,7 +1,21 @@
#pragma once
+#include <boost/signal.hpp>
+#include <boost/shared_ptr.hpp>
+
+#include "Swiften/Elements/Message.h"
+#include "Swiften/Elements/Presence.h"
+#include "Swiften/ELements/ChatState.h"
+
namespace Swift {
class ChatStateTracker {
-
+ public:
+ ChatStateTracker();
+ void handleMessageReceived(boost::shared_ptr<Message> message);
+ void handlePresenceChange(boost::shared_ptr<Presence> newPresence, boost::shared_ptr<Presence> oldPresence);
+ boost::signal<void (ChatState::ChatStateType)> onChatStateChange;
+ private:
+ void changeState(ChatState::ChatStateType state);
+ ChatState::ChatStateType currentState_;
};
}
diff --git a/Swiften/Chat/UnitTest/ChatStateNotifierTest.cpp b/Swiften/Chat/UnitTest/ChatStateNotifierTest.cpp
index bacfc3a..44ec9d8 100644
--- a/Swiften/Chat/UnitTest/ChatStateNotifierTest.cpp
+++ b/Swiften/Chat/UnitTest/ChatStateNotifierTest.cpp
@@ -18,15 +18,15 @@ public:
int composingCallCount;
int activeCallCount;
- ChatStateNotifier::ChatState currentState;
+ ChatState::ChatStateType currentState;
private:
- void handleChatStateChanged(ChatStateNotifier::ChatState newState) {
+ void handleChatStateChanged(ChatState::ChatStateType newState) {
switch (newState) {
- case ChatStateNotifier::Composing:
+ case ChatState::Composing:
composingCallCount++;
break;
- case ChatStateNotifier::Active:
+ case ChatState::Active:
activeCallCount++;
break;
default:
@@ -86,7 +86,7 @@ public:
notifier_->userCancelledNewMessage();
CPPUNIT_ASSERT_EQUAL(1, monitor_->composingCallCount);
CPPUNIT_ASSERT_EQUAL(1, monitor_->activeCallCount);
- CPPUNIT_ASSERT_EQUAL(ChatStateNotifier::Active, monitor_->currentState);
+ CPPUNIT_ASSERT_EQUAL(ChatState::Active, monitor_->currentState);
}