From 5c2567847c73f6095206dfbd451fe2f26f4056aa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Sat, 28 Nov 2009 21:07:57 +0100
Subject: Implemented XML console.

Resolves: #135

diff --git a/Swift/Controllers/MainController.cpp b/Swift/Controllers/MainController.cpp
index 2e640e0..8800da0 100644
--- a/Swift/Controllers/MainController.cpp
+++ b/Swift/Controllers/MainController.cpp
@@ -45,16 +45,6 @@
 #include "Swiften/StringCodecs/SHA1.h"
 #include "Swiften/StringCodecs/Hexify.h"
 
-namespace {
-	void printIncomingData(const Swift::String& data) {
-		std::cout << "<- " << data << std::endl;
-	}
-
-	void printOutgoingData(const Swift::String& data) {
-		std::cout << "-> " << data << std::endl;
-	}
-}
-
 namespace Swift {
 
 static const String CLIENT_NAME = "Swift";
@@ -273,8 +263,10 @@ void MainController::performLoginFromCachedCredentials() {
 	if (!client_) {
 		client_ = new Swift::Client(jid_, password_);
 		presenceSender_ = new PresenceSender(client_);
-		//client_->onDataRead.connect(&printIncomingData);
-		//client_->onDataWritten.connect(&printOutgoingData);
+		client_->onDataRead.connect(boost::bind(
+				&XMLConsoleController::handleDataRead, xmlConsoleController_, _1));
+		client_->onDataWritten.connect(boost::bind(
+				&XMLConsoleController::handleDataWritten, xmlConsoleController_, _1));
 		if (!certificateFile_.isEmpty()) {
 			client_->setCertificate(certificateFile_);
 		}
diff --git a/Swift/Controllers/UIInterfaces/XMLConsoleWidget.h b/Swift/Controllers/UIInterfaces/XMLConsoleWidget.h
index e333d97..eab29a8 100644
--- a/Swift/Controllers/UIInterfaces/XMLConsoleWidget.h
+++ b/Swift/Controllers/UIInterfaces/XMLConsoleWidget.h
@@ -1,10 +1,15 @@
 #pragma once
 
 namespace Swift {
+	class String;
+
 	class XMLConsoleWidget {
 		public:
 			virtual ~XMLConsoleWidget();
 
+			virtual void handleDataRead(const String& data) = 0;
+			virtual void handleDataWritten(const String& data) = 0;
+
 			virtual void show() = 0;
 			virtual void activate() = 0;
 	};
diff --git a/Swift/Controllers/XMLConsoleController.cpp b/Swift/Controllers/XMLConsoleController.cpp
index 4629573..7de5b94 100644
--- a/Swift/Controllers/XMLConsoleController.cpp
+++ b/Swift/Controllers/XMLConsoleController.cpp
@@ -24,4 +24,16 @@ void XMLConsoleController::handleUIEvent(boost::shared_ptr<UIEvent> rawEvent) {
 	}
 }
 
+void XMLConsoleController::handleDataRead(const String& data) {
+	if (xmlConsoleWidget) {
+		xmlConsoleWidget->handleDataRead(data);
+	}
+}
+
+void XMLConsoleController::handleDataWritten(const String& data) {
+	if (xmlConsoleWidget) {
+		xmlConsoleWidget->handleDataWritten(data);
+	}
+}
+
 }
diff --git a/Swift/Controllers/XMLConsoleController.h b/Swift/Controllers/XMLConsoleController.h
index 52a8269..134bf0d 100644
--- a/Swift/Controllers/XMLConsoleController.h
+++ b/Swift/Controllers/XMLConsoleController.h
@@ -7,13 +7,19 @@
 #include "Swift/Controllers/UIEvents/UIEventStream.h"
 
 namespace Swift {
+	class String;
 	class XMLConsoleWidgetFactory;
 	class XMLConsoleWidget;
+
 	class XMLConsoleController {
 		public:
 			XMLConsoleController(UIEventStream* uiEventStream, XMLConsoleWidgetFactory* xmlConsoleWidgetFactory);
 			~XMLConsoleController();
 
+		public:
+			void handleDataRead(const String& data);
+			void handleDataWritten(const String& data);
+
 		private:
 			void handleUIEvent(boost::shared_ptr<UIEvent> event);
 
diff --git a/Swift/QtUI/QtXMLConsoleWidget.cpp b/Swift/QtUI/QtXMLConsoleWidget.cpp
index 7553d06..662f070 100644
--- a/Swift/QtUI/QtXMLConsoleWidget.cpp
+++ b/Swift/QtUI/QtXMLConsoleWidget.cpp
@@ -1,10 +1,37 @@
 #include "QtXMLConsoleWidget.h"
 
 #include <QCloseEvent>
+#include <QTextEdit>
+#include <QVBoxLayout>
+#include <QPushButton>
+#include <QScrollBar>
+
+#include "QtSwiftUtil.h"
+#include "Swiften/Base/String.h"
 
 namespace Swift {
+
 QtXMLConsoleWidget::QtXMLConsoleWidget() {
+	setWindowTitle("Console");
+
+	QVBoxLayout* layout = new QVBoxLayout(this);
+	layout->setSpacing(0);
+	layout->setContentsMargins(0,0,0,0);
 
+	textEdit = new QTextEdit(this);
+	textEdit->setReadOnly(true);
+	layout->addWidget(textEdit);
+
+	QWidget* bottom = new QWidget(this);
+	layout->addWidget(bottom);
+
+	QHBoxLayout* buttonLayout = new QHBoxLayout(bottom);
+	buttonLayout->setContentsMargins(0,0,20,0);
+	buttonLayout->setSpacing(0);
+	buttonLayout->addStretch();
+	QPushButton* clearButton = new QPushButton("Clear", bottom);
+	connect(clearButton, SIGNAL(clicked()), textEdit, SLOT(clear()));
+	buttonLayout->addWidget(clearButton);
 }
 
 void QtXMLConsoleWidget::showEvent(QShowEvent* event) {
@@ -26,4 +53,23 @@ void QtXMLConsoleWidget::closeEvent(QCloseEvent* event) {
 	event->accept();
 }
 
+void QtXMLConsoleWidget::handleDataRead(const String& data) {
+	textEdit->setTextColor(QColor(33,98,33));
+	appendText(data);
+}
+
+void QtXMLConsoleWidget::handleDataWritten(const String& data) {
+	textEdit->setTextColor(QColor(155,1,0));
+	appendText(data);
+}
+
+void QtXMLConsoleWidget::appendText(const String& data) {
+	QScrollBar* scrollBar = textEdit->verticalScrollBar();
+	bool scrollToBottom = (!scrollBar || scrollBar->value() == scrollBar->maximum());
+	textEdit->append(P2QSTRING(data));
+	if (scrollToBottom) {
+		textEdit->ensureCursorVisible();
+	}
+}
+
 }
diff --git a/Swift/QtUI/QtXMLConsoleWidget.h b/Swift/QtUI/QtXMLConsoleWidget.h
index 50b53f2..0d5a10d 100644
--- a/Swift/QtUI/QtXMLConsoleWidget.h
+++ b/Swift/QtUI/QtXMLConsoleWidget.h
@@ -3,16 +3,28 @@
 #include "Swift/Controllers/UIInterfaces/XMLConsoleWidget.h"
 #include "QtTabbable.h"
 
+class QTextEdit;
+
 namespace Swift {
 	class QtXMLConsoleWidget : public QtTabbable, public XMLConsoleWidget {
-		Q_OBJECT
+			Q_OBJECT
+
 		public:
 			QtXMLConsoleWidget();
+
 			void show();
 			void activate();
-		protected slots:
-			void closeEvent(QCloseEvent* event);
-		protected:
-			void showEvent(QShowEvent* event);
+
+			virtual void handleDataRead(const String& data);
+			virtual void handleDataWritten(const String& data);
+
+		private:
+			virtual void closeEvent(QCloseEvent* event);
+			virtual void showEvent(QShowEvent* event);
+
+			void appendText(const String& data);
+
+		private:
+			QTextEdit* textEdit;
 	};
 }
-- 
cgit v0.10.2-6-g49f6