From 9bd0d4220e8b8e7a1714536d9e4d6c6e61727d85 Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Sun, 22 Aug 2010 14:11:51 +0100
Subject: Don't incorrectly mark all messages read in most recently opened chat
 when a different chat is selected


diff --git a/Swift/QtUI/QtChatTabs.cpp b/Swift/QtUI/QtChatTabs.cpp
index 48c882f..c14f9ad 100644
--- a/Swift/QtUI/QtChatTabs.cpp
+++ b/Swift/QtUI/QtChatTabs.cpp
@@ -52,6 +52,10 @@ void QtChatTabs::closeEvent(QCloseEvent* event) {
 	event->accept();
 }
 
+QtTabbable* QtChatTabs::getCurrentTab() {
+	return qobject_cast<QtTabbable*>(tabs_->currentWidget());
+}
+
 void QtChatTabs::addTab(QtTabbable* tab) {
 	QSizePolicy policy = sizePolicy();
 	/* Chat windows like to grow - don't let them */
diff --git a/Swift/QtUI/QtChatTabs.h b/Swift/QtUI/QtChatTabs.h
index 69dd8d1..807b3c1 100644
--- a/Swift/QtUI/QtChatTabs.h
+++ b/Swift/QtUI/QtChatTabs.h
@@ -20,6 +20,7 @@ namespace Swift {
 			QtChatTabs();
 			void addTab(QtTabbable* tab);
 			void minimise();
+			QtTabbable* getCurrentTab();
 		signals:
 			void geometryChanged();
 
diff --git a/Swift/QtUI/QtChatWindow.cpp b/Swift/QtUI/QtChatWindow.cpp
index 5b73fec..de681d5 100644
--- a/Swift/QtUI/QtChatWindow.cpp
+++ b/Swift/QtUI/QtChatWindow.cpp
@@ -82,7 +82,6 @@ QtChatWindow::QtChatWindow(const QString &contact, UIEventStream* eventStream) :
 	connect(qApp, SIGNAL(focusChanged(QWidget*, QWidget*)), this, SLOT(qAppFocusChanged(QWidget*, QWidget*)));
 	connect(messageLog_, SIGNAL(gotFocus()), input_, SLOT(setFocus()));
 	resize(400,300);
-	input_->setFocus();
 }
 
 QtChatWindow::~QtChatWindow() {
@@ -194,6 +193,7 @@ void QtChatWindow::qAppFocusChanged(QWidget *old, QWidget *now) {
 	Q_UNUSED(old);
 	Q_UNUSED(now);
 	if (isWidgetSelected()) {
+		input_->setFocus();
 		onAllMessagesRead();
 	}
 	
@@ -351,7 +351,6 @@ void QtChatWindow::handleInputChanged() {
 void QtChatWindow::show() {
 	QWidget::show();
 	emit windowOpening();
-	input_->setFocus();
 }
 
 void QtChatWindow::activate() {
diff --git a/Swift/QtUI/QtTabbable.cpp b/Swift/QtUI/QtTabbable.cpp
index 227a975..07a17c1 100644
--- a/Swift/QtUI/QtTabbable.cpp
+++ b/Swift/QtUI/QtTabbable.cpp
@@ -8,6 +8,8 @@
 
 #include <QApplication>
 
+#include "QtChatTabs.h"
+
 namespace Swift {
 
 QtTabbable::~QtTabbable() {
@@ -16,7 +18,11 @@ QtTabbable::~QtTabbable() {
 
 bool QtTabbable::isWidgetSelected() {
 	/*isActiveWindow() shouldn't be necessary, but I don't trust it as far as I can throw it*/
-	return isActiveWindow() && isAncestorOf(QApplication::focusWidget());
+	if (!isActiveWindow()) {
+		return false;
+	}
+	QtChatTabs* parent = qobject_cast<QtChatTabs*>(window());
+	return parent ? parent->getCurrentTab() == this : isAncestorOf(QApplication::focusWidget());
 }
 
 }
-- 
cgit v0.10.2-6-g49f6