From 1d23fbe7acbfed25578958fb24aa75416f2ab737 Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Fri, 1 Oct 2010 15:08:08 +0100
Subject: Reuse server-generated resources.

Once a new method has been implemented in Client, this will re-use
a server-generated resource for re-connections within the same
Swift session. This provides a stable resource for the session,
preventing MUC ghosts and similar artifacts due to server or network
failures, without disclosing additional information like hostnames
in the resource.

Resolves: #563

diff --git a/Swift/Controllers/MainController.cpp b/Swift/Controllers/MainController.cpp
index fdfab98..7ff3ed0 100644
--- a/Swift/Controllers/MainController.cpp
+++ b/Swift/Controllers/MainController.cpp
@@ -247,6 +247,7 @@ void MainController::resetCurrentError() {
 }
 
 void MainController::handleConnected() {
+	boundJID_ = client_->getBoundJID();
 	loginWindow_->setIsLoggingIn(false);
 	resetCurrentError();
 	resetPendingReconnects();
@@ -419,7 +420,11 @@ void MainController::performLoginFromCachedCredentials() {
 		/* In case we're in the middle of another login, make sure they don't overlap */
 		client_->disconnect();
 	}
-	client_->connect();
+	if (boundJID_.isValid() && jid_.isBare()) {
+		client_->connect(boundJID_);
+	} else {
+		client_->connect();
+	}
 }
 
 void MainController::handleError(const ClientError& error) {
diff --git a/Swift/Controllers/MainController.h b/Swift/Controllers/MainController.h
index c0885b6..ed2cee2 100644
--- a/Swift/Controllers/MainController.h
+++ b/Swift/Controllers/MainController.h
@@ -157,6 +157,7 @@ namespace Swift {
 			boost::shared_ptr<DiscoInfo> serverDiscoInfo_;
 			XMPPRoster* xmppRoster_;;
 			JID jid_;
+			JID boundJID_;
 			PresenceOracle* presenceOracle_;
 			SystemTrayController* systemTrayController_;
 			SoundEventController* soundEventController_;
diff --git a/Swiften/Client/Client.cpp b/Swiften/Client/Client.cpp
index 13e7a9b..fe9bc60 100644
--- a/Swiften/Client/Client.cpp
+++ b/Swiften/Client/Client.cpp
@@ -42,10 +42,20 @@ bool Client::isAvailable() {
 	return session_ && session_->getState() == ClientSession::Initialized;
 }
 
+/** FIXME: implement */
+JID Client::getBoundJID() {
+	return JID();
+}
+
 void Client::connect() {
 	connect(jid_.getDomain());
 }
 
+void Client::connect(const JID& jid) {
+	jid_ = jid;
+	connect();
+}
+
 void Client::connect(const String& host) {
 	assert(!connector_); // Crash on reconnect is here.
 	connector_ = Connector::create(host, &resolver_, connectionFactory_, timerFactory_);
diff --git a/Swiften/Client/Client.h b/Swiften/Client/Client.h
index cb07036..ca729a7 100644
--- a/Swiften/Client/Client.h
+++ b/Swiften/Client/Client.h
@@ -39,6 +39,7 @@ namespace Swift {
 			void setCertificate(const String& certificate);
 
 			void connect();
+			void connect(const JID& jid);
 			void connect(const String& host);
 			void disconnect();
 			
@@ -54,6 +55,8 @@ namespace Swift {
 				return iqRouter_;
 			}
 
+			JID getBoundJID();
+
 		public:
 			boost::signal<void (const ClientError&)> onError;
 			boost::signal<void ()> onConnected;
-- 
cgit v0.10.2-6-g49f6