From 013725b6ac01ee0351cd61701588099dc1ec1258 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Wed, 18 Nov 2009 09:40:30 +0100
Subject: Wait until WebKit is ready loading the initial template before
 appending messages.

Fixes bug where first messages were missing from the view.

diff --git a/Swift/QtUI/QtChatView.cpp b/Swift/QtUI/QtChatView.cpp
index 12f6beb..62e04ce 100644
--- a/Swift/QtUI/QtChatView.cpp
+++ b/Swift/QtUI/QtChatView.cpp
@@ -20,6 +20,7 @@ QtChatView::QtChatView(QWidget* parent) : QWidget(parent) {
 	webView_ = new QWebView(this);
 	webView_->setFocusPolicy(Qt::NoFocus);
 	connect(webView_, SIGNAL(linkClicked(const QUrl&)), SLOT(handleLinkClicked(const QUrl&)));
+	connect(webView_, SIGNAL(loadFinished(bool)), SLOT(handleViewLoadFinished(bool)));
 #ifdef Q_WS_X11
 	/* To give a border on Linux, where it looks bad without */
 	QStackedWidget* stack = new QStackedWidget(this);
@@ -50,6 +51,8 @@ QtChatView::QtChatView(QWidget* parent) : QWidget(parent) {
 	pageHTML.replace(pageHTML.indexOf("%@"), 2, "");
 	pageHTML.replace(pageHTML.indexOf("%@"), 2, "");
 	file.close();
+
+	viewReady_ = false;
 	webPage_->mainFrame()->setHtml(pageHTML);
 }
 
@@ -61,11 +64,18 @@ void QtChatView::addMessage(const ChatSnippet& snippet) {
 	content.replace("\"", "\\\"");
 	content.replace("\n", "\\n");
 	content.replace("\r", "");
+	QString command;
 	if (previousContinuationElementID_.isEmpty() || !snippet.getAppendToPrevious()) {
-		webPage_->mainFrame()->evaluateJavaScript("appendMessage(\"" + content + "\");");
+		command = "appendMessage(\"" + content + "\");";
+	}
+	else {
+		command = "appendNextMessage(\"" + content + "\");";
+	}
+	if (viewReady_) {
+		webPage_->mainFrame()->evaluateJavaScript(command);
 	}
 	else {
-		webPage_->mainFrame()->evaluateJavaScript("appendNextMessage(\"" + content + "\");");
+		queuedMessages_ += command;
 	}
 
 	//qDebug() << webPage_->mainFrame()->toHtml();
@@ -94,4 +104,11 @@ void QtChatView::handleLinkClicked(const QUrl& url) {
 	QDesktopServices::openUrl(url);
 }
 
+void QtChatView::handleViewLoadFinished(bool ok) {
+	Q_ASSERT(ok);
+	viewReady_ = true;
+	webPage_->mainFrame()->evaluateJavaScript(queuedMessages_);
+	queuedMessages_.clear();
+}
+
 }
diff --git a/Swift/QtUI/QtChatView.h b/Swift/QtUI/QtChatView.h
index 7340e00..d3e997a 100644
--- a/Swift/QtUI/QtChatView.h
+++ b/Swift/QtUI/QtChatView.h
@@ -24,10 +24,15 @@ namespace Swift {
 			void scrollToBottom();
 			void handleLinkClicked(const QUrl&);
 
+		private slots:
+			void handleViewLoadFinished(bool);
+
 		private:
+			bool viewReady_;
 			QWebView* webView_;
 			QWebPage* webPage_;
 			QString previousContinuationElementID_;
+			QString queuedMessages_;
 	};
 }
 
-- 
cgit v0.10.2-6-g49f6