From 90c44a10fec26d2a0935b2d62e82b6a5be028373 Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Sun, 21 Nov 2010 19:43:42 +0000
Subject: Clearing the chat window won't break continuation elements.

Resolves: #642
Resolves: #643
Release-Notes: Clearing the chat windows should now work correctly.

diff --git a/Swift/QtUI/ChatSnippet.h b/Swift/QtUI/ChatSnippet.h
index 9786518..f323295 100644
--- a/Swift/QtUI/ChatSnippet.h
+++ b/Swift/QtUI/ChatSnippet.h
@@ -6,6 +6,8 @@
 
 #pragma once
 
+#include <boost/shared_ptr.hpp>
+
 #include <QString>
 #include <QDateTime>
 #include "QtChatTheme.h"
@@ -13,12 +15,14 @@
 namespace Swift {
 	class ChatSnippet {
 		public:
-			ChatSnippet(bool appendToPrevious = false);
+			ChatSnippet(bool appendToPrevious);
 			virtual ~ChatSnippet();
 			
 			virtual const QString& getContent() const = 0;
 			virtual QString getContinuationElementID() const { return ""; }
 
+			boost::shared_ptr<ChatSnippet> getContinuationFallbackSnippet() {return continuationFallback_;}
+
 			bool getAppendToPrevious() const {
 				return appendToPrevious_;
 			}
@@ -26,9 +30,13 @@ namespace Swift {
 			static QString escape(const QString&);
 
 		protected:
+			void setContinuationFallbackSnippet(boost::shared_ptr<ChatSnippet> continuationFallback) {
+				continuationFallback_ = continuationFallback;
+			}
 			static QString timeToEscapedString(const QDateTime& time);
 		private:
 			bool appendToPrevious_;
+			boost::shared_ptr<ChatSnippet> continuationFallback_;
 	};
 }
 
diff --git a/Swift/QtUI/MessageSnippet.cpp b/Swift/QtUI/MessageSnippet.cpp
index 995a0db..47cb1a0 100644
--- a/Swift/QtUI/MessageSnippet.cpp
+++ b/Swift/QtUI/MessageSnippet.cpp
@@ -12,6 +12,9 @@
 namespace Swift {
 
 MessageSnippet::MessageSnippet(const QString& message, const QString& sender, const QDateTime& time, const QString& iconURI, bool isIncoming, bool appendToPrevious, QtChatTheme* theme, const QString& id) : ChatSnippet(appendToPrevious) {
+	if (appendToPrevious) {
+		setContinuationFallbackSnippet(boost::shared_ptr<ChatSnippet>(new MessageSnippet(message, sender, time, iconURI, isIncoming, false, theme, id)));
+	}
 	if (isIncoming) {
 		if (appendToPrevious) {
 			content_ = theme->getIncomingNextContent();
diff --git a/Swift/QtUI/QtChatView.cpp b/Swift/QtUI/QtChatView.cpp
index 2a66fc0..4738da3 100644
--- a/Swift/QtUI/QtChatView.cpp
+++ b/Swift/QtUI/QtChatView.cpp
@@ -75,9 +75,12 @@ QWebElement QtChatView::snippetToDOM(boost::shared_ptr<ChatSnippet> snippet) {
 
 void QtChatView::addToDOM(boost::shared_ptr<ChatSnippet> snippet) {
 	rememberScrolledToBottom();
-	QWebElement newElement = snippetToDOM(snippet);
+	bool insert = snippet->getAppendToPrevious();
 	QWebElement continuationElement = lastElement_.findFirst("#insert");
-	if (snippet->getAppendToPrevious()) {
+	bool fallback = insert && continuationElement.isNull();
+	boost::shared_ptr<ChatSnippet> newSnippet = (insert && fallback) ? snippet->getContinuationFallbackSnippet() : snippet;
+	QWebElement newElement = snippetToDOM(newSnippet);
+	if (insert && !fallback) {
 		Q_ASSERT(!continuationElement.isNull());
 		continuationElement.replace(newElement);
 	} else {
@@ -155,6 +158,7 @@ void QtChatView::handleViewLoadFinished(bool ok) {
 }
 
 void QtChatView::resetView() {
+	lastElement_ = QWebElement();
 	QString pageHTML = theme_->getTemplate();
 	pageHTML.replace("==bodyBackground==", "background-color:#e3e3e3");
 	pageHTML.replace(pageHTML.indexOf("%@"), 2, theme_->getBase());
diff --git a/Swift/QtUI/SystemMessageSnippet.cpp b/Swift/QtUI/SystemMessageSnippet.cpp
index 13919b2..2e0aec4 100644
--- a/Swift/QtUI/SystemMessageSnippet.cpp
+++ b/Swift/QtUI/SystemMessageSnippet.cpp
@@ -11,6 +11,9 @@
 namespace Swift {
 
 SystemMessageSnippet::SystemMessageSnippet(const QString& message, const QDateTime& time, bool appendToPrevious, QtChatTheme* theme) : ChatSnippet(appendToPrevious) {
+	if (appendToPrevious) {
+		setContinuationFallbackSnippet(boost::shared_ptr<ChatSnippet>(new SystemMessageSnippet(message, time, false, theme)));
+	}
 	content_ = theme->getStatus();
 
 	content_.replace("%message%", "<span class='swift_message'>" + escape(message) + "</span>");
-- 
cgit v0.10.2-6-g49f6