From 9bc8ddaf40d73647944592385bf56ece41046846 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Fri, 12 Jun 2009 12:26:41 +0200
Subject: Removing auto delete stuff from Request.

This should now be handled properly by using shared_ptrs.

diff --git a/Swift/Controllers/ChatControllerBase.cpp b/Swift/Controllers/ChatControllerBase.cpp
index 3b3ddb7..0d22571 100644
--- a/Swift/Controllers/ChatControllerBase.cpp
+++ b/Swift/Controllers/ChatControllerBase.cpp
@@ -26,7 +26,7 @@ void ChatControllerBase::setAvailableServerFeatures(boost::shared_ptr<DiscoInfo>
 	if (info->hasFeature(DiscoInfo::SecurityLabels)) {
 		chatWindow_->setSecurityLabelsEnabled(true);
 		chatWindow_->setSecurityLabelsError();
-		GetSecurityLabelsCatalogRequest* request = new GetSecurityLabelsCatalogRequest(JID(toJID_.toBare()), iqRouter_, Request::AutoDeleteAfterResponse);
+		boost::shared_ptr<GetSecurityLabelsCatalogRequest> request(new GetSecurityLabelsCatalogRequest(JID(toJID_.toBare()), iqRouter_));
 		request->onResponse.connect(boost::bind(&ChatControllerBase::handleSecurityLabelsCatalogResponse, this, _1, _2));
 		request->send();
 		labelsEnabled_ = true;
diff --git a/Swift/Controllers/MainController.cpp b/Swift/Controllers/MainController.cpp
index e5036a5..ff53450 100644
--- a/Swift/Controllers/MainController.cpp
+++ b/Swift/Controllers/MainController.cpp
@@ -101,7 +101,7 @@ void MainController::handleConnected() {
 	discoResponder_->setDiscoInfo(capsInfo_->getNode() + "#" + capsInfo_->getVersion(), discoInfo);
 
 	serverDiscoInfo_ = boost::shared_ptr<DiscoInfo>(new DiscoInfo());
-	GetDiscoInfoRequest* discoInfoRequest = new GetDiscoInfoRequest(JID(), client_, Request::AutoDeleteAfterResponse);
+	boost::shared_ptr<GetDiscoInfoRequest> discoInfoRequest(new GetDiscoInfoRequest(JID(), client_));
 	discoInfoRequest->onResponse.connect(boost::bind(&MainController::handleServerDiscoInfoResponse, this, _1, _2));
 	discoInfoRequest->send();
 	
diff --git a/Swift/Controllers/XMPPRosterController.cpp b/Swift/Controllers/XMPPRosterController.cpp
index 46fb40c..e1716e6 100644
--- a/Swift/Controllers/XMPPRosterController.cpp
+++ b/Swift/Controllers/XMPPRosterController.cpp
@@ -26,7 +26,7 @@ XMPPRosterController::XMPPRosterController(IQRouter* iqRouter, boost::shared_ptr
 }
 
 void XMPPRosterController::requestRoster() {
-	GetRosterRequest* rosterRequest = new GetRosterRequest(iqRouter_, Request::AutoDeleteAfterResponse);
+	boost::shared_ptr<GetRosterRequest> rosterRequest(new GetRosterRequest(iqRouter_));
 	rosterRequest->onResponse.connect(boost::bind(&XMPPRosterController::handleRosterReceived, this, _1));
 	rosterRequest->send();
 }
diff --git a/Swiften/QA/ClientTest/ClientTest.cpp b/Swiften/QA/ClientTest/ClientTest.cpp
index 20a03a4..d3e8c67 100644
--- a/Swiften/QA/ClientTest/ClientTest.cpp
+++ b/Swiften/QA/ClientTest/ClientTest.cpp
@@ -28,7 +28,7 @@ void handleRosterReceived(boost::shared_ptr<Payload>) {
 }
 
 void handleConnected() {
-	GetRosterRequest* rosterRequest = new GetRosterRequest(client, Request::AutoDeleteAfterResponse);
+	boost::shared_ptr<GetRosterRequest> rosterRequest(new GetRosterRequest(client));
 	rosterRequest->onResponse.connect(boost::bind(&handleRosterReceived, _1));
 	rosterRequest->send();
 }
diff --git a/Swiften/Queries/GenericRequest.h b/Swiften/Queries/GenericRequest.h
index c81760f..b4a1918 100644
--- a/Swiften/Queries/GenericRequest.h
+++ b/Swiften/Queries/GenericRequest.h
@@ -13,9 +13,8 @@ namespace Swift {
 					IQ::Type type, 
 					const JID& receiver, 
 					boost::shared_ptr<Payload> payload, 
-					IQRouter* router,
-					AutoDeleteBehavior autoDeleteBehavior = DoNotAutoDelete) : 
-						Request(type, receiver, payload, router, autoDeleteBehavior) {
+					IQRouter* router) :
+						Request(type, receiver, payload, router) {
 			}
 
 			virtual void handleResponse(boost::shared_ptr<Payload> payload, boost::optional<Error> error) {
diff --git a/Swiften/Queries/Request.cpp b/Swiften/Queries/Request.cpp
index a788e91..4235c6f 100644
--- a/Swiften/Queries/Request.cpp
+++ b/Swiften/Queries/Request.cpp
@@ -4,7 +4,7 @@
 
 namespace Swift {
 
-Request::Request(IQ::Type type, const JID& receiver, boost::shared_ptr<Payload> payload, IQRouter* router, AutoDeleteBehavior autoDeleteBehavior) : router_(router), type_(type), receiver_(receiver), payload_(payload), autoDeleteBehavior_(autoDeleteBehavior), sent_(false) {
+Request::Request(IQ::Type type, const JID& receiver, boost::shared_ptr<Payload> payload, IQRouter* router) : router_(router), type_(type), receiver_(receiver), payload_(payload), sent_(false) {
 }
 
 void Request::send() {
@@ -16,7 +16,14 @@ void Request::send() {
 	iq->addPayload(payload_);
 	id_ = router_->getNewIQID();
 	iq->setID(id_);
-	router_->addHandler(this);
+
+	try {
+		router_->addHandler(shared_from_this());
+	}
+	catch (const std::exception&) {
+		router_->addHandler(this);
+	}
+
 	router_->sendIQ(iq);
 }
 
@@ -31,9 +38,6 @@ bool Request::handleIQ(boost::shared_ptr<IQ> iq) {
 			handleResponse(boost::shared_ptr<Payload>(), boost::optional<Error>(Error::UndefinedCondition));
 		}
 		router_->removeHandler(this);
-		if (autoDeleteBehavior_ == AutoDeleteAfterResponse) {
-			MainEventLoop::deleteLater(this);
-		}
 		handled = true;
 	}
 	return handled;
diff --git a/Swiften/Queries/Request.h b/Swiften/Queries/Request.h
index 52d1d15..1c972dc 100644
--- a/Swiften/Queries/Request.h
+++ b/Swiften/Queries/Request.h
@@ -3,6 +3,7 @@
 
 #include <boost/shared_ptr.hpp>
 #include <boost/optional.hpp>
+#include <boost/enable_shared_from_this.hpp>
 
 #include "Swiften/Base/String.h"
 #include "Swiften/Queries/IQHandler.h"
@@ -12,19 +13,13 @@
 #include "Swiften/JID/JID.h"
 
 namespace Swift {
-	class Request : public IQHandler {
+	class Request : public IQHandler, public boost::enable_shared_from_this<Request> {
 		public:
-			enum AutoDeleteBehavior {
-				DoNotAutoDelete,
-				AutoDeleteAfterResponse
-			};
-
 			Request(
 					IQ::Type type, 
 					const JID& receiver, 
 					boost::shared_ptr<Payload> payload, 
-					IQRouter* router, 
-					AutoDeleteBehavior = DoNotAutoDelete);
+					IQRouter* router);
 
 			void send();
 
@@ -39,7 +34,6 @@ namespace Swift {
 			IQ::Type type_;
 			JID receiver_;
 			boost::shared_ptr<Payload> payload_;
-			AutoDeleteBehavior autoDeleteBehavior_;
 			String id_;
 			bool sent_;
 	};
diff --git a/Swiften/Queries/Requests/GetDiscoInfoRequest.h b/Swiften/Queries/Requests/GetDiscoInfoRequest.h
index 0e8508e..386dd10 100644
--- a/Swiften/Queries/Requests/GetDiscoInfoRequest.h
+++ b/Swiften/Queries/Requests/GetDiscoInfoRequest.h
@@ -7,8 +7,8 @@
 namespace Swift {
 	class GetDiscoInfoRequest : public GenericRequest<DiscoInfo> {
 		public:
-			GetDiscoInfoRequest(const JID& jid, IQRouter* router, AutoDeleteBehavior autoDeleteBehavior = DoNotAutoDelete) : 
-					GenericRequest<DiscoInfo>(IQ::Get, jid, boost::shared_ptr<DiscoInfo>(new DiscoInfo()), router, autoDeleteBehavior) {
+			GetDiscoInfoRequest(const JID& jid, IQRouter* router) :
+					GenericRequest<DiscoInfo>(IQ::Get, jid, boost::shared_ptr<DiscoInfo>(new DiscoInfo()), router) {
 			}
 	};
 }
diff --git a/Swiften/Queries/Requests/GetRosterRequest.h b/Swiften/Queries/Requests/GetRosterRequest.h
index 2364d81..4ebb25e 100644
--- a/Swiften/Queries/Requests/GetRosterRequest.h
+++ b/Swiften/Queries/Requests/GetRosterRequest.h
@@ -7,8 +7,8 @@
 namespace Swift {
 	class GetRosterRequest : public GenericRequest<RosterPayload> {
 		public:
-			GetRosterRequest(IQRouter* router, AutoDeleteBehavior autoDeleteBehavior = DoNotAutoDelete) : 
-					GenericRequest<RosterPayload>(IQ::Get, JID(), boost::shared_ptr<Payload>(new RosterPayload()), router, autoDeleteBehavior) {
+			GetRosterRequest(IQRouter* router) :
+					GenericRequest<RosterPayload>(IQ::Get, JID(), boost::shared_ptr<Payload>(new RosterPayload()), router) {
 			}
 	};
 }
diff --git a/Swiften/Queries/Requests/GetSecurityLabelsCatalogRequest.h b/Swiften/Queries/Requests/GetSecurityLabelsCatalogRequest.h
index 53ca3eb..3971c9d 100644
--- a/Swiften/Queries/Requests/GetSecurityLabelsCatalogRequest.h
+++ b/Swiften/Queries/Requests/GetSecurityLabelsCatalogRequest.h
@@ -9,10 +9,9 @@ namespace Swift {
 		public:
 			GetSecurityLabelsCatalogRequest(
 					const JID& recipient, 
-					IQRouter* router, 
-					AutoDeleteBehavior autoDeleteBehavior = DoNotAutoDelete) : 
+					IQRouter* router) :
 						GenericRequest<SecurityLabelsCatalog>(
-							IQ::Get, JID(), boost::shared_ptr<SecurityLabelsCatalog>(new SecurityLabelsCatalog(recipient)), router, autoDeleteBehavior) {
+							IQ::Get, JID(), boost::shared_ptr<SecurityLabelsCatalog>(new SecurityLabelsCatalog(recipient)), router) {
 			}
 	};
 }
-- 
cgit v0.10.2-6-g49f6