From 94ff6ec9e699a2db97775063eb7fa2f6d24b281a Mon Sep 17 00:00:00 2001
From: Catalin Badea <catalin.badea392@gmail.com>
Date: Sun, 29 Jul 2012 16:42:06 +0300
Subject: Fix adding messages at the top.


diff --git a/Swift/Controllers/HistoryViewController.cpp b/Swift/Controllers/HistoryViewController.cpp
index e5e2acc..c6e663b 100644
--- a/Swift/Controllers/HistoryViewController.cpp
+++ b/Swift/Controllers/HistoryViewController.cpp
@@ -158,9 +158,10 @@ void HistoryViewController::handleReturnPressed(const std::string& keyword) {
 void HistoryViewController::handleScrollReachedTop(const boost::gregorian::date& date) {
 	std::vector<HistoryMessage> messages = historyController_->getMessagesFromPreviousDate(selfJID_, selectedItem_->getJID(), selectedItemType_, date);
 
-	reverse_foreach (const HistoryMessage& message, messages) {
+	foreach (const HistoryMessage& message, messages) {
 		addNewMessage(message, true);
 	}
+	historyWindow_->resetConversationViewTopInsertPoint();
 }
 
 void HistoryViewController::handleScrollReachedBottom(const boost::gregorian::date& date) {
diff --git a/Swift/Controllers/UIInterfaces/HistoryWindow.h b/Swift/Controllers/UIInterfaces/HistoryWindow.h
index aa1a495..edc4af3 100644
--- a/Swift/Controllers/UIInterfaces/HistoryWindow.h
+++ b/Swift/Controllers/UIInterfaces/HistoryWindow.h
@@ -17,6 +17,7 @@ namespace Swift {
 			virtual void setRosterModel(Roster*) = 0;
 			virtual void addMessage(const std::string &message, const std::string &senderName, bool senderIsSelf, const std::string& avatarPath, const boost::posix_time::ptime& time, bool addAtTheTop) = 0;
 			virtual void resetConversationView() = 0;
+			virtual void resetConversationViewTopInsertPoint() = 0; // this is a temporary fix used in adding messages at the top
 			virtual void setDate(const boost::gregorian::date& date) = 0;
 
 			boost::signal<void (RosterItem*)> onSelectedContactChanged;
diff --git a/Swift/QtUI/QtChatView.cpp b/Swift/QtUI/QtChatView.cpp
index 0af1262..3ad957c 100644
--- a/Swift/QtUI/QtChatView.cpp
+++ b/Swift/QtUI/QtChatView.cpp
@@ -93,11 +93,8 @@ void QtChatView::addMessageBottom(boost::shared_ptr<ChatSnippet> snippet) {
 		/* If this asserts, the previous queuing code was necessary and should be reinstated */
 		assert(false);
 	}
-
-	if (firstElement_.isNull()) {
-		firstElement_ = lastElement_;
-	}
 }
+
 void QtChatView::addMessageTop(boost::shared_ptr<ChatSnippet> snippet) {
 	// save scrollbar maximum value
 	if (!topMessageAdded_) {
@@ -105,9 +102,22 @@ void QtChatView::addMessageTop(boost::shared_ptr<ChatSnippet> snippet) {
 	}
 	topMessageAdded_ = true;
 
-	QWebElement newElement = snippetToDOM(snippet);
+	QWebElement continuationElement = firstElement_.findFirst("#insert");
+
+	bool insert = snippet->getAppendToPrevious();
+	bool fallback = 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 {
+		continuationElement.removeFromDocument();
+		topInsertPoint_.appendOutside(newElement);
+	}
 
-	firstElement_.prependOutside(newElement);
 	firstElement_ = newElement;
 
 	if (lastElement_.isNull()) {
@@ -345,6 +355,8 @@ void QtChatView::resetView() {
 	QWebElement chatElement = document_.findFirst("#Chat");
 	newInsertPoint_ = chatElement.clone();
 	newInsertPoint_.setOuterXml("<div id='swift_insert'/>");
+	topInsertPoint_ = newInsertPoint_.clone();
+	chatElement.appendInside(topInsertPoint_);
 	chatElement.appendInside(newInsertPoint_);
 	Q_ASSERT(!newInsertPoint_.isNull());
 
@@ -444,4 +456,8 @@ int QtChatView::getSnippetPositionByDate(const QDate& date) {
 	return message.geometry().top();
 }
 
+void QtChatView::resetTopInsertPoint() {
+	firstElement_ = QWebElement();
+}
+
 }
diff --git a/Swift/QtUI/QtChatView.h b/Swift/QtUI/QtChatView.h
index d778f6f..dada46a 100644
--- a/Swift/QtUI/QtChatView.h
+++ b/Swift/QtUI/QtChatView.h
@@ -61,6 +61,7 @@ namespace Swift {
 			void handleLinkClicked(const QUrl&);
 			void handleKeyPressEvent(QKeyEvent* event);
 			void resetView();
+			void resetTopInsertPoint();
 			void increaseFontSize(int numSteps = 1);
 			void decreaseFontSize();
 			void resizeFont(int fontSizeSteps);
@@ -86,6 +87,7 @@ namespace Swift {
 			int fontSizeSteps_;
 			QtChatTheme* theme_;
 			QWebElement newInsertPoint_;
+			QWebElement topInsertPoint_;
 			QWebElement lineSeparator_;
 			QWebElement lastElement_;
 			QWebElement firstElement_;
diff --git a/Swift/QtUI/QtHistoryWindow.cpp b/Swift/QtUI/QtHistoryWindow.cpp
index a0ccbf9..d9b587f 100644
--- a/Swift/QtUI/QtHistoryWindow.cpp
+++ b/Swift/QtUI/QtHistoryWindow.cpp
@@ -117,8 +117,8 @@ void QtHistoryWindow::addMessage(const std::string &message, const std::string &
 	QString qAvatarPath =  scaledAvatarPath.isEmpty() ? "qrc:/icons/avatar.png" : QUrl::fromLocalFile(scaledAvatarPath).toEncoded();
 
 	if (addAtTheTop) {
-		// bool appendToPrevious = ((senderIsSelf && previousTopMessageWasSelf_) || (!senderIsSelf && !previousTopMessageWasSelf_&& previousTopSenderName_ == P2QSTRING(senderName)));
-		conversation_->addMessageTop(boost::shared_ptr<ChatSnippet>(new MessageSnippet(messageHTML, Qt::escape(P2QSTRING(senderName)), qTime, qAvatarPath, senderIsSelf, false, theme_, P2QSTRING(id))));
+		bool appendToPrevious = ((senderIsSelf && previousTopMessageWasSelf_) || (!senderIsSelf && !previousTopMessageWasSelf_&& previousTopSenderName_ == P2QSTRING(senderName)));
+		conversation_->addMessageTop(boost::shared_ptr<ChatSnippet>(new MessageSnippet(messageHTML, Qt::escape(P2QSTRING(senderName)), qTime, qAvatarPath, senderIsSelf, appendToPrevious, theme_, P2QSTRING(id))));
 
 		previousTopMessageWasSelf_ = senderIsSelf;
 		previousTopSenderName_ = P2QSTRING(senderName);
@@ -206,4 +206,10 @@ void QtHistoryWindow::handleFontResized(int fontSizeSteps) {
 	emit fontResized(fontSizeSteps);
 }
 
+void QtHistoryWindow::resetConversationViewTopInsertPoint() {
+	previousTopMessageWasSelf_ = false;
+	previousTopSenderName_ = QString();
+	conversation_->resetTopInsertPoint();
+}
+
 }
diff --git a/Swift/QtUI/QtHistoryWindow.h b/Swift/QtUI/QtHistoryWindow.h
index c813b82..13d3f14 100644
--- a/Swift/QtUI/QtHistoryWindow.h
+++ b/Swift/QtUI/QtHistoryWindow.h
@@ -23,8 +23,9 @@ namespace Swift {
 			~QtHistoryWindow();
 			void activate();
 			void setRosterModel(Roster*);
-			void addMessage(const std::string &message, const std::string &senderName, bool senderIsSelf, const std::string& avatarPath, const boost::posix_time::ptime& time, bool addAtTheTop);
+			void addMessage(const std::string& message, const std::string& senderName, bool senderIsSelf, const std::string& avatarPath, const boost::posix_time::ptime& time, bool addAtTheTop);
 			void resetConversationView();
+			void resetConversationViewTopInsertPoint();
 			void setDate(const boost::gregorian::date& date);
 
 			void closeEvent(QCloseEvent* event);
-- 
cgit v0.10.2-6-g49f6