From 59be74ec6fc7bc495f2a261b8f274b8555aee306 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Sat, 30 Oct 2010 16:11:35 +0200
Subject: Changed CoreClient::onError to CoreClient::onDisconnected.

The error parameter is optional.

diff --git a/Swift/Controllers/MainController.cpp b/Swift/Controllers/MainController.cpp
index c9e6230..eb03e50 100644
--- a/Swift/Controllers/MainController.cpp
+++ b/Swift/Controllers/MainController.cpp
@@ -366,7 +366,7 @@ void MainController::performLoginFromCachedCredentials() {
 		client_ = new Swift::Client(eventLoop_, jid_, password_, storages_);
 		client_->onDataRead.connect(boost::bind(&XMLConsoleController::handleDataRead, xmlConsoleController_, _1));
 		client_->onDataWritten.connect(boost::bind(&XMLConsoleController::handleDataWritten, xmlConsoleController_, _1));
-		client_->onError.connect(boost::bind(&MainController::handleError, this, _1));
+		client_->onDisconnected.connect(boost::bind(&MainController::handleDisconnected, this, _1));
 		client_->onConnected.connect(boost::bind(&MainController::handleConnected, this));
 
 		client_->setSoftwareVersion(CLIENT_NAME, buildVersion);
@@ -400,42 +400,43 @@ void MainController::performLoginFromCachedCredentials() {
 	}
 }
 
-void MainController::handleError(const ClientError& error) {
-	String message;
-	switch(error.getType()) {
-		case ClientError::UnknownError: message = "Unknown Error"; break;
-		case ClientError::DomainNameResolveError: message = "Unable to find server"; break;
-		case ClientError::ConnectionError: message = "Error connecting to server"; break;
-		case ClientError::ConnectionReadError: message = "Error while receiving server data"; break;
-		case ClientError::ConnectionWriteError: message = "Error while sending data to the server"; break;
-		case ClientError::XMLError: message = "Error parsing server data"; break;
-		case ClientError::AuthenticationFailedError: message = "Login/password invalid"; break;
-		case ClientError::CompressionFailedError: message = "Error while compressing stream"; break;
-		case ClientError::ServerVerificationFailedError: message = "Server verification failed"; break;
-		case ClientError::NoSupportedAuthMechanismsError: message = "Authentication mechanisms not supported"; break;
-		case ClientError::UnexpectedElementError: message = "Unexpected response"; break;
-		case ClientError::ResourceBindError: message = "Error binding resource"; break;
-		case ClientError::SessionStartError: message = "Error starting session"; break;
-		case ClientError::TLSError: message = "Encryption error"; break;
-		case ClientError::ClientCertificateLoadError: message = "Error loading certificate (Invalid password?)"; break;
-		case ClientError::ClientCertificateError: message = "Certificate not authorized"; break;
-	}
-	if (!rosterController_) { //hasn't been logged in yet
-		signOut();
-		loginWindow_->setMessage(message);
-	} else {
-		logout();
-		setReconnectTimer();
-		if (lastDisconnectError_) {
-			message = "Reconnect to " + jid_.getDomain() + " failed: " + message + ". Will retry in " + boost::lexical_cast<std::string>(timeBeforeNextReconnect_) + " seconds.";
-			lastDisconnectError_->conclude();
+void MainController::handleDisconnected(const boost::optional<ClientError>& error) {
+	if (error) {
+		String message;
+		switch(error->getType()) {
+			case ClientError::UnknownError: message = "Unknown Error"; break;
+			case ClientError::DomainNameResolveError: message = "Unable to find server"; break;
+			case ClientError::ConnectionError: message = "Error connecting to server"; break;
+			case ClientError::ConnectionReadError: message = "Error while receiving server data"; break;
+			case ClientError::ConnectionWriteError: message = "Error while sending data to the server"; break;
+			case ClientError::XMLError: message = "Error parsing server data"; break;
+			case ClientError::AuthenticationFailedError: message = "Login/password invalid"; break;
+			case ClientError::CompressionFailedError: message = "Error while compressing stream"; break;
+			case ClientError::ServerVerificationFailedError: message = "Server verification failed"; break;
+			case ClientError::NoSupportedAuthMechanismsError: message = "Authentication mechanisms not supported"; break;
+			case ClientError::UnexpectedElementError: message = "Unexpected response"; break;
+			case ClientError::ResourceBindError: message = "Error binding resource"; break;
+			case ClientError::SessionStartError: message = "Error starting session"; break;
+			case ClientError::TLSError: message = "Encryption error"; break;
+			case ClientError::ClientCertificateLoadError: message = "Error loading certificate (Invalid password?)"; break;
+			case ClientError::ClientCertificateError: message = "Certificate not authorized"; break;
+		}
+		if (!rosterController_) { //hasn't been logged in yet
+			signOut();
+			loginWindow_->setMessage(message);
 		} else {
-			message = "Disconnected from " + jid_.getDomain() + ": " + message;
+			logout();
+			setReconnectTimer();
+			if (lastDisconnectError_) {
+				message = "Reconnect to " + jid_.getDomain() + " failed: " + message + ". Will retry in " + boost::lexical_cast<std::string>(timeBeforeNextReconnect_) + " seconds.";
+				lastDisconnectError_->conclude();
+			} else {
+				message = "Disconnected from " + jid_.getDomain() + ": " + message;
+			}
+			lastDisconnectError_ = boost::shared_ptr<ErrorEvent>(new ErrorEvent(JID(jid_.getDomain()), message));
+			eventController_->handleIncomingEvent(lastDisconnectError_);
 		}
-		lastDisconnectError_ = boost::shared_ptr<ErrorEvent>(new ErrorEvent(JID(jid_.getDomain()), message));
-		eventController_->handleIncomingEvent(lastDisconnectError_);
 	}
-
 }
 
 void MainController::setReconnectTimer() {
diff --git a/Swift/Controllers/MainController.h b/Swift/Controllers/MainController.h
index 709bacf..da65567 100644
--- a/Swift/Controllers/MainController.h
+++ b/Swift/Controllers/MainController.h
@@ -90,7 +90,7 @@ namespace Swift {
 			void handleLoginRequest(const String& username, const String& password, const String& certificateFile, bool remember, bool loginAutomatically);
 			void handleCancelLoginRequest();
 			void handleChangeStatusRequest(StatusShow::Type show, const String &statusText);
-			void handleError(const ClientError& error);
+			void handleDisconnected(const boost::optional<ClientError>& error);
 			void handleServerDiscoInfoResponse(boost::shared_ptr<DiscoInfo>, const boost::optional<ErrorPayload>&);
 			void handleEventQueueLengthChange(int count);
 			void handleVCardReceived(const JID& j, VCard::ref vCard);
diff --git a/Swiften/Client/CoreClient.cpp b/Swiften/Client/CoreClient.cpp
index fa9dca0..9511dfb 100644
--- a/Swiften/Client/CoreClient.cpp
+++ b/Swiften/Client/CoreClient.cpp
@@ -73,9 +73,7 @@ void CoreClient::handleConnectorFinished(boost::shared_ptr<Connection> connectio
 	connector_->onConnectFinished.disconnect(boost::bind(&CoreClient::handleConnectorFinished, this, _1));
 	connector_.reset();
 	if (!connection) {
-		if (!disconnectRequested_) {
-			onError(ClientError::ConnectionError);
-		}
+		onDisconnected(disconnectRequested_ ? boost::optional<ClientError>() : boost::optional<ClientError>(ClientError::ConnectionError));
 	}
 	else {
 		assert(!connection_);
@@ -131,6 +129,7 @@ void CoreClient::handleSessionFinished(boost::shared_ptr<Error> error) {
 	connection_->disconnect();
 	connection_.reset();
 
+	boost::optional<ClientError> actualError;
 	if (error) {
 		ClientError clientError;
 		if (boost::shared_ptr<ClientSession::Error> actualError = boost::dynamic_pointer_cast<ClientSession::Error>(error)) {
@@ -183,8 +182,9 @@ void CoreClient::handleSessionFinished(boost::shared_ptr<Error> error) {
 					break;
 			}
 		}
-		onError(clientError);
+		actualError = boost::optional<ClientError>(clientError);
 	}
+	onDisconnected(actualError);
 }
 
 void CoreClient::handleNeedCredentials() {
diff --git a/Swiften/Client/CoreClient.h b/Swiften/Client/CoreClient.h
index f6e0b6d..e9e81ec 100644
--- a/Swiften/Client/CoreClient.h
+++ b/Swiften/Client/CoreClient.h
@@ -126,9 +126,12 @@ namespace Swift {
 
 		public:
 			/**
-			 * Emitted when a non-recoverable error occurs.
+			 * Emitted when the client was disconnected from the network.
+			 *
+			 * If the connection was due to a non-recoverable error, the type
+			 * of error will be passed as a parameter.
 			 */
-			boost::signal<void (const ClientError&)> onError;
+			boost::signal<void (const boost::optional<ClientError>&)> onDisconnected;
 
 			/**
 			 * Emitted when the client is connected and authenticated,
diff --git a/Swiften/Examples/ConnectivityTest/ConnectivityTest.cpp b/Swiften/Examples/ConnectivityTest/ConnectivityTest.cpp
index e870d83..9da5cdf 100644
--- a/Swiften/Examples/ConnectivityTest/ConnectivityTest.cpp
+++ b/Swiften/Examples/ConnectivityTest/ConnectivityTest.cpp
@@ -46,7 +46,7 @@ void handleConnected() {
 	discoInfoRequest->send();
 }
 
-void handleError(const ClientError&) {
+void handleDisconnected(const boost::optional<ClientError>&) {
 	exitCode = CANNOT_AUTH;
 	eventLoop.stop();
 }
@@ -73,7 +73,7 @@ int main(int argc, char* argv[]) {
 	timeout = (timeout ? timeout : 30) * 1000;
 	ClientXMLTracer* tracer = new ClientXMLTracer(client);
 	client->onConnected.connect(&handleConnected);
-	errorConnection = client->onError.connect(&handleError);
+	errorConnection = client->onDisconnected.connect(&handleDisconnected);
 	std::cout << "Connecting to JID " << jid << " with timeout " << timeout << "ms on host: "; ;
 	if (!connectHost.isEmpty()) {
 		std::cout << connectHost << std::endl;
diff --git a/Swiften/Examples/SendFile/SendFile.cpp b/Swiften/Examples/SendFile/SendFile.cpp
index 6f72480..f0a2d59 100644
--- a/Swiften/Examples/SendFile/SendFile.cpp
+++ b/Swiften/Examples/SendFile/SendFile.cpp
@@ -32,13 +32,13 @@ class FileSender {
 
 			client = new Swift::Client(&eventLoop, jid, password);
 			client->onConnected.connect(boost::bind(&FileSender::handleConnected, this));
-			client->onError.connect(boost::bind(&FileSender::handleError, this, _1));
+			client->onDisconnected.connect(boost::bind(&FileSender::handleDisconnected, this, _1));
 			//tracer = new ClientXMLTracer(client);
 		}
 
 		~FileSender() {
 			//delete tracer;
-			client->onError.disconnect(boost::bind(&FileSender::handleError, this, _1));
+			client->onDisconnected.disconnect(boost::bind(&FileSender::handleDisconnected, this, _1));
 			client->onConnected.disconnect(boost::bind(&FileSender::handleConnected, this));
 			delete client;
 			delete socksBytestreamServer;
@@ -67,7 +67,7 @@ class FileSender {
 			transfer->start();
 		}
 
-		void handleError(const ClientError&) {
+		void handleDisconnected(const boost::optional<ClientError>&) {
 			std::cerr << "Error!" << std::endl;
 			exit(-1);
 		}
diff --git a/Swiften/Examples/SendMessage/SendMessage.cpp b/Swiften/Examples/SendMessage/SendMessage.cpp
index d9ed923..fe020aa 100644
--- a/Swiften/Examples/SendMessage/SendMessage.cpp
+++ b/Swiften/Examples/SendMessage/SendMessage.cpp
@@ -37,7 +37,7 @@ void handleConnected() {
 	eventLoop.stop();
 }
 
-void handleError(const ClientError&) {
+void handleDisconnected(const boost::optional<ClientError>&) {
 	std::cerr << "Error!" << std::endl;
 	exitCode = 1;
 	eventLoop.stop();
@@ -64,7 +64,7 @@ int main(int argc, char* argv[]) {
 
 	ClientXMLTracer* tracer = new ClientXMLTracer(client);
 	client->onConnected.connect(&handleConnected);
-	errorConnection = client->onError.connect(&handleError);
+	errorConnection = client->onDisconnected.connect(&handleDisconnected);
 	if (!connectHost.isEmpty()) {
 		client->connect(connectHost);
 	} else {
-- 
cgit v0.10.2-6-g49f6