From 7df402e9319b3ee2ce3c4d6e07a978f853b7a02e Mon Sep 17 00:00:00 2001
From: HanzZ <hanzz.k@gmail.com>
Date: Sun, 23 Oct 2011 14:20:52 +0200
Subject: Emit onSessionFinished only in Session::handleDisconnected.

Copyright (c) 2011 Jan Kaluza
Licensed under the Simplified BSD license.
See Documentation/Licenses/BSD-simplified.txt for more information.

diff --git a/Swiften/Session/Session.cpp b/Swiften/Session/Session.cpp
index 661cb8d..0eef997 100644
--- a/Swiften/Session/Session.cpp
+++ b/Swiften/Session/Session.cpp
@@ -48,8 +48,6 @@ void Session::finishSession() {
 		xmppLayer->writeFooter();
 	}
 	connection->disconnect();
-	handleSessionFinished(boost::optional<SessionError>());
-	onSessionFinished(boost::optional<SessionError>());
 }
 
 void Session::finishSession(const SessionError& error) {
@@ -61,21 +59,19 @@ void Session::finishSession(const SessionError& error) {
 		xmppLayer->writeFooter();
 	}
 	connection->disconnect();
-	handleSessionFinished(boost::optional<SessionError>(error));
-	onSessionFinished(boost::optional<SessionError>(error));
 }
 
 void Session::initializeStreamStack() {
 	xmppLayer = new XMPPLayer(payloadParserFactories, payloadSerializers, xmlParserFactory, ClientStreamType);
 	xmppLayer->onStreamStart.connect(
-			boost::bind(&Session::handleStreamStart, shared_from_this(), _1));
-	xmppLayer->onElement.connect(boost::bind(&Session::handleElement, shared_from_this(), _1));
+			boost::bind(&Session::handleStreamStart, this, _1));
+	xmppLayer->onElement.connect(boost::bind(&Session::handleElement, this, _1));
 	xmppLayer->onError.connect(
-			boost::bind(&Session::finishSession, shared_from_this(), XMLError));
+			boost::bind(&Session::finishSession, this, XMLError));
 	xmppLayer->onDataRead.connect(boost::bind(boost::ref(onDataRead), _1));
 	xmppLayer->onWriteData.connect(boost::bind(boost::ref(onDataWritten), _1));
 	connection->onDisconnected.connect(
-			boost::bind(&Session::handleDisconnected, shared_from_this(), _1));
+			boost::bind(&Session::handleDisconnected, this, _1));
 	connectionLayer = new ConnectionLayer(connection);
 	streamStack = new StreamStack(xmppLayer, connectionLayer);
 }
@@ -85,18 +81,24 @@ void Session::sendElement(boost::shared_ptr<Element> stanza) {
 }
 
 void Session::handleDisconnected(const boost::optional<Connection::Error>& connectionError) {
+	connection->onDisconnected.disconnect(
+			boost::bind(&Session::handleDisconnected, this, _1));
 	if (connectionError) {
 		switch (*connectionError) {
 			case Connection::ReadError:
-				finishSession(ConnectionReadError);
+				handleSessionFinished(ConnectionReadError);
+				onSessionFinished(ConnectionReadError);
 				break;
 			case Connection::WriteError:
-				finishSession(ConnectionWriteError);
+				handleSessionFinished(ConnectionWriteError);
+				onSessionFinished(ConnectionWriteError);
 				break;
 		}
 	}
 	else {
-		finishSession();
+		boost::optional<SessionError> error;
+		handleSessionFinished(error);
+		onSessionFinished(error);
 	}
 }
 
-- 
cgit v0.10.2-6-g49f6