From 29a83617e0a066ee0692d2bbcecc9335f73b8138 Mon Sep 17 00:00:00 2001
From: Vlad Voicu <vladvoic@gmail.com>
Date: Wed, 1 Jun 2011 23:36:29 +0300
Subject: Replace last message feature for MUC

License: This patch is BSD-licensed, see
http://www.opensource.org/licenses/bsd-license.php

diff --git a/Swift/Controllers/Chat/ChatController.cpp b/Swift/Controllers/Chat/ChatController.cpp
index d8c9e31..ab273a2 100644
--- a/Swift/Controllers/Chat/ChatController.cpp
+++ b/Swift/Controllers/Chat/ChatController.cpp
@@ -103,21 +103,6 @@ void ChatController::preHandleIncomingMessage(boost::shared_ptr<MessageEvent> me
 			setToJID(from);
 		}
 	}
-	boost::shared_ptr<Replace> replace = message->getPayload<Replace>();
-	if (replace) {
-		// Determine the timestamp
-		boost::posix_time::ptime timeStamp = boost::posix_time::microsec_clock::universal_time();
-		boost::optional<boost::posix_time::ptime> messageTimeStamp = getMessageTimestamp(message);
-		if (messageTimeStamp) {
-			timeStamp = *messageTimeStamp;
-		}
-		std::string body = message->getBody();
-		chatWindow_->replaceMessage(body, lastMessageUIID_, timeStamp);
-		replacedMessage_ = true;
-	}
-	else {
-		replacedMessage_ = false;
-	}
 	chatStateTracker_->handleMessageReceived(message);
 	chatStateNotifier_->receivedMessageFromContact(message->getPayload<ChatState>());
 }
@@ -134,7 +119,6 @@ void ChatController::preSendMessageRequest(boost::shared_ptr<Message> message) {
 void ChatController::postSendMessage(const std::string& body, boost::shared_ptr<Stanza> sentStanza) {
 	boost::shared_ptr<Replace> replace = sentStanza->getPayload<Replace>();
 	if (replace) {
-		chatWindow_->replaceMessage(body, myLastMessageUIID_, boost::posix_time::microsec_clock::universal_time());
 		eraseIf(unackedStanzas_, PairSecondEquals<boost::shared_ptr<Stanza>, std::string>(myLastMessageUIID_));
 	} else {
 		myLastMessageUIID_ = addMessage(body, QT_TRANSLATE_NOOP("", "me"), true, labelsEnabled_ ? chatWindow_->getSelectedSecurityLabel().getLabel() : boost::shared_ptr<SecurityLabel>(), std::string(avatarManager_->getAvatarPath(selfJID_).string()), boost::posix_time::microsec_clock::universal_time());
diff --git a/Swift/Controllers/Chat/ChatControllerBase.cpp b/Swift/Controllers/Chat/ChatControllerBase.cpp
index 0fcf901..fcaf4ce 100644
--- a/Swift/Controllers/Chat/ChatControllerBase.cpp
+++ b/Swift/Controllers/Chat/ChatControllerBase.cpp
@@ -7,6 +7,7 @@
 #include "Swift/Controllers/Chat/ChatControllerBase.h"
 
 #include <sstream>
+#include <map>
 
 #include <boost/bind.hpp>
 #include <boost/shared_ptr.hpp>
@@ -33,7 +34,6 @@ ChatControllerBase::ChatControllerBase(const JID& self, StanzaChannel* stanzaCha
 	chatWindow_->onSendMessageRequest.connect(boost::bind(&ChatControllerBase::handleSendMessageRequest, this, _1, _2));
 	setOnline(stanzaChannel->isAvailable() && iqRouter->isAvailable());
 	createDayChangeTimer();
-	replacedMessage_ = false;
 }
 
 ChatControllerBase::~ChatControllerBase() {
@@ -186,8 +186,19 @@ void ChatControllerBase::handleIncomingMessage(boost::shared_ptr<MessageEvent> m
 			timeStamp = *messageTimeStamp;
 		}
 		onActivity(body);
-		if (!replacedMessage_) {
-			lastMessageUIID_ = addMessage(body, senderDisplayNameFromMessage(from), isIncomingMessageFromMe(message), label, std::string(avatarManager_->getAvatarPath(from).string()), timeStamp);
+
+ 		boost::shared_ptr<Replace> replace = message->getPayload<Replace>();
+		if (replace) {
+			std::string body = message->getBody();
+			// Should check if the user has a previous message
+			std::map<JID, std::string>::iterator lastMessage;
+			lastMessage = lastMessagesUIID_.find(from);
+			if (lastMessage != lastMessagesUIID_.end()) {
+				chatWindow_->replaceMessage(body, lastMessagesUIID_[from], timeStamp);
+			}
+		}
+		else {
+			lastMessagesUIID_[from] = addMessage(body, senderDisplayNameFromMessage(from), isIncomingMessageFromMe(message), label, std::string(avatarManager_->getAvatarPath(from).string()), timeStamp);
 		}
 	}
 	chatWindow_->show();
diff --git a/Swift/Controllers/Chat/ChatControllerBase.h b/Swift/Controllers/Chat/ChatControllerBase.h
index e0f1b94..3364ccb 100644
--- a/Swift/Controllers/Chat/ChatControllerBase.h
+++ b/Swift/Controllers/Chat/ChatControllerBase.h
@@ -4,8 +4,7 @@
  * See Documentation/Licenses/GPLv3.txt for more information.
  */
 
-#ifndef SWIFTEN_ChatControllerBase_H
-#define SWIFTEN_ChatControllerBase_H
+#pragma once
 
 #include <map>
 #include <vector>
@@ -85,8 +84,7 @@ namespace Swift {
 			ChatWindow* chatWindow_;
 			JID toJID_;
 			bool labelsEnabled_;
-			bool replacedMessage_;
-			std::string lastMessageUIID_;
+			std::map<JID, std::string> lastMessagesUIID_;
 			PresenceOracle* presenceOracle_;
 			AvatarManager* avatarManager_;
 			bool useDelayForLatency_;
@@ -95,5 +93,3 @@ namespace Swift {
 			TimerFactory* timerFactory_;
 	};
 }
-
-#endif
-- 
cgit v0.10.2-6-g49f6