From dfea757fd085ca9386b96db58c5d2bcd6173a917 Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Sun, 20 Jun 2010 18:38:19 +0100
Subject: Show subjects on MUC join.

Resolves: #472

diff --git a/Swift/Controllers/Chat/ChatsManager.cpp b/Swift/Controllers/Chat/ChatsManager.cpp
index 824c98d..ea38f1f 100644
--- a/Swift/Controllers/Chat/ChatsManager.cpp
+++ b/Swift/Controllers/Chat/ChatsManager.cpp
@@ -215,7 +215,7 @@ void ChatsManager::handleJoinMUCRequest(const JID &muc, const boost::optional<St
 void ChatsManager::handleIncomingMessage(boost::shared_ptr<Message> message) {
 	JID jid = message->getFrom();
 	boost::shared_ptr<MessageEvent> event(new MessageEvent(message));
-	if (!event->isReadable() && !message->getPayload<ChatState>()) {
+	if (!event->isReadable() && !message->getPayload<ChatState>() && message->getSubject().isEmpty()) {
 		return;
 	}
 
diff --git a/Swift/Controllers/Chat/MUCController.cpp b/Swift/Controllers/Chat/MUCController.cpp
index 2f74713..6029a14 100644
--- a/Swift/Controllers/Chat/MUCController.cpp
+++ b/Swift/Controllers/Chat/MUCController.cpp
@@ -194,6 +194,10 @@ void MUCController::preHandleIncomingMessage(boost::shared_ptr<Message> message)
 	/*Buggy implementations never send the status code, so use an incoming message as a hint that joining's done (e.g. the old ejabberd on psi-im.org).*/
 	receivedActivity();
 	joined_ = true;
+
+	if (!message->getSubject().isEmpty() && message->getBody().isEmpty()) {
+		chatWindow_->addSystemMessage("The room subject is now: " + message->getSubject());
+	}
 }
 
 void MUCController::handleOccupantRoleChanged(const String& nick, const MUCOccupant& occupant, const MUCOccupant::Role& oldRole) {
diff --git a/Swiften/Elements/Message.h b/Swiften/Elements/Message.h
index b1fea56..f42aec6 100644
--- a/Swiften/Elements/Message.h
+++ b/Swiften/Elements/Message.h
@@ -10,6 +10,7 @@
 
 #include "Swiften/Base/String.h"
 #include "Swiften/Elements/Body.h"
+#include "Swiften/Elements/Subject.h"
 #include "Swiften/Elements/ErrorPayload.h"
 #include "Swiften/Elements/Stanza.h"
 
@@ -22,6 +23,18 @@ namespace Swift
 
 			Message() : type_(Chat) { }
 
+			String getSubject() const { 
+				boost::shared_ptr<Subject> subject(getPayload<Subject>());
+				if (subject) {
+					return subject->getText();
+				}
+				return "";
+			}
+
+			void setSubject(const String& subject) { 
+				updatePayload(boost::shared_ptr<Subject>(new Subject(subject)));
+			}
+
 			String getBody() const { 
 				boost::shared_ptr<Body> body(getPayload<Body>());
 				if (body) {
@@ -43,7 +56,6 @@ namespace Swift
 			void setType(Type type) { type_ = type; }
 
 		private:
-			String body_;
 			Type type_;
 	};
 }
diff --git a/Swiften/Elements/Subject.h b/Swiften/Elements/Subject.h
new file mode 100644
index 0000000..745ddb5
--- /dev/null
+++ b/Swiften/Elements/Subject.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2010 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "Swiften/Elements/Payload.h"
+#include "Swiften/Base/String.h"
+
+namespace Swift {
+	class Subject : public Payload {
+		public:
+			Subject(const String& text = "") : text_(text) {
+			}
+
+			void setText(const String& text) {
+				text_ = text;
+			}
+
+			const String& getText() const {
+				return text_;
+			}
+
+		private:
+			String text_;
+	};
+}
diff --git a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp
index 1a45ed2..eace3d1 100644
--- a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp
+++ b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp
@@ -10,6 +10,7 @@
 #include "Swiften/Parser/PayloadParserFactory.h"
 #include "Swiften/Parser/PayloadParsers/ErrorParserFactory.h"
 #include "Swiften/Parser/PayloadParsers/BodyParserFactory.h"
+#include "Swiften/Parser/PayloadParsers/SubjectParserFactory.h"
 #include "Swiften/Parser/PayloadParsers/ChatStateParserFactory.h"
 #include "Swiften/Parser/PayloadParsers/PriorityParserFactory.h"
 #include "Swiften/Parser/PayloadParsers/ResourceBindParserFactory.h"
@@ -38,6 +39,7 @@ FullPayloadParserFactoryCollection::FullPayloadParserFactoryCollection() {
 	factories_.push_back(shared_ptr<PayloadParserFactory>(new StatusParserFactory()));
 	factories_.push_back(shared_ptr<PayloadParserFactory>(new StatusShowParserFactory()));
 	factories_.push_back(shared_ptr<PayloadParserFactory>(new BodyParserFactory()));
+	factories_.push_back(shared_ptr<PayloadParserFactory>(new SubjectParserFactory()));
 	factories_.push_back(shared_ptr<PayloadParserFactory>(new PriorityParserFactory()));
 	factories_.push_back(shared_ptr<PayloadParserFactory>(new ErrorParserFactory()));
 	factories_.push_back(shared_ptr<PayloadParserFactory>(new SoftwareVersionParserFactory()));
diff --git a/Swiften/Parser/PayloadParsers/SubjectParser.cpp b/Swiften/Parser/PayloadParsers/SubjectParser.cpp
new file mode 100644
index 0000000..d28fb7d
--- /dev/null
+++ b/Swiften/Parser/PayloadParsers/SubjectParser.cpp
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2010 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include "Swiften/Parser/PayloadParsers/SubjectParser.h"
+
+namespace Swift {
+
+SubjectParser::SubjectParser() : level_(0) {
+}
+
+void SubjectParser::handleStartElement(const String&, const String&, const AttributeMap&) {
+	++level_;
+}
+
+void SubjectParser::handleEndElement(const String&, const String&) {
+	--level_;
+	if (level_ == 0) {
+		getPayloadInternal()->setText(text_);
+	}
+}
+
+void SubjectParser::handleCharacterData(const String& data) {
+	text_ += data;
+}
+
+}
diff --git a/Swiften/Parser/PayloadParsers/SubjectParser.h b/Swiften/Parser/PayloadParsers/SubjectParser.h
new file mode 100644
index 0000000..a7b8a89
--- /dev/null
+++ b/Swiften/Parser/PayloadParsers/SubjectParser.h
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2010 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "Swiften/Elements/Subject.h"
+#include "Swiften/Parser/GenericPayloadParser.h"
+
+namespace Swift {
+	class SubjectParser : public GenericPayloadParser<Subject> {
+		public:
+			SubjectParser();
+
+			virtual void handleStartElement(const String& element, const String&, const AttributeMap& attributes);
+			virtual void handleEndElement(const String& element, const String&);
+			virtual void handleCharacterData(const String& data);
+
+		private:
+			int level_;
+			String text_;
+	};
+}
diff --git a/Swiften/Parser/PayloadParsers/SubjectParserFactory.h b/Swiften/Parser/PayloadParsers/SubjectParserFactory.h
new file mode 100644
index 0000000..8ac45c6
--- /dev/null
+++ b/Swiften/Parser/PayloadParsers/SubjectParserFactory.h
@@ -0,0 +1,18 @@
+/*
+ * Copyright (c) 2010 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "Swiften/Parser/GenericPayloadParserFactory.h"
+#include "Swiften/Parser/PayloadParsers/SubjectParser.h"
+
+namespace Swift {
+	class SubjectParserFactory : public GenericPayloadParserFactory<SubjectParser> {
+		public:
+			SubjectParserFactory() : GenericPayloadParserFactory<SubjectParser>("subject") {}
+	};
+}
+
diff --git a/Swiften/Parser/SConscript b/Swiften/Parser/SConscript
index 430d827..7b64ee3 100644
--- a/Swiften/Parser/SConscript
+++ b/Swiften/Parser/SConscript
@@ -18,6 +18,7 @@ sources = [
 		"PayloadParserFactory.cpp",
 		"PayloadParserFactoryCollection.cpp",
 		"PayloadParsers/BodyParser.cpp",
+		"PayloadParsers/SubjectParser.cpp",
 		"PayloadParsers/ChatStateParser.cpp",
 		"PayloadParsers/DiscoInfoParser.cpp",
 		"PayloadParsers/DiscoItemsParser.cpp",
diff --git a/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp b/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp
index e407876..39e7d5b 100644
--- a/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp
+++ b/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp
@@ -8,6 +8,7 @@
 #include "Swiften/Base/foreach.h"
 #include "Swiften/Serializer/PayloadSerializer.h"
 #include "Swiften/Serializer/PayloadSerializers/BodySerializer.h"
+#include "Swiften/Serializer/PayloadSerializers/SubjectSerializer.h"
 #include "Swiften/Serializer/PayloadSerializers/ChatStateSerializer.h"
 #include "Swiften/Serializer/PayloadSerializers/PrioritySerializer.h"
 #include "Swiften/Serializer/PayloadSerializers/ErrorSerializer.h"
@@ -35,6 +36,7 @@ namespace Swift {
 
 FullPayloadSerializerCollection::FullPayloadSerializerCollection() {
 	serializers_.push_back(new BodySerializer());
+	serializers_.push_back(new SubjectSerializer());
 	serializers_.push_back(new ChatStateSerializer());
 	serializers_.push_back(new PrioritySerializer());
 	serializers_.push_back(new ErrorSerializer());
diff --git a/Swiften/Serializer/PayloadSerializers/SubjectSerializer.h b/Swiften/Serializer/PayloadSerializers/SubjectSerializer.h
new file mode 100644
index 0000000..fcd82a5
--- /dev/null
+++ b/Swiften/Serializer/PayloadSerializers/SubjectSerializer.h
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2010 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "Swiften/Serializer/GenericPayloadSerializer.h"
+#include "Swiften/Serializer/XML/XMLTextNode.h"
+#include "Swiften/Elements/Subject.h"
+
+namespace Swift {
+	class SubjectSerializer : public GenericPayloadSerializer<Subject> {
+		public:
+			SubjectSerializer() : GenericPayloadSerializer<Subject>() {}
+
+			virtual String serializePayload(boost::shared_ptr<Subject> subject)  const {
+				XMLTextNode textNode(subject->getText());
+				return "<subject>" + textNode.serialize() + "</subject>";
+			}
+	};
+}
-- 
cgit v0.10.2-6-g49f6