From 28c99a37d09a68ef993330b9ece2732ec03eba8f Mon Sep 17 00:00:00 2001
From: Tobias Markmann <tm@ayena.de>
Date: Thu, 17 Jan 2013 17:11:43 +0100
Subject: Fixing VCard related crash caused by wrong deletion of profile
 window.

Change-Id: Ifab6b304f0e25ab323b4a354f4682291e6aa1078
License: This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details.

diff --git a/Swift/Controllers/ProfileController.cpp b/Swift/Controllers/ProfileController.cpp
index e641988..241cc2e 100644
--- a/Swift/Controllers/ProfileController.cpp
+++ b/Swift/Controllers/ProfileController.cpp
@@ -25,7 +25,7 @@ ProfileController::~ProfileController() {
 	if (profileWindow) {
 		vcardManager->onOwnVCardChanged.disconnect(boost::bind(&ProfileController::handleOwnVCardChanged, this, _1));
 		profileWindow->onVCardChangeRequest.disconnect(boost::bind(&ProfileController::handleVCardChangeRequest, this, _1));
-		delete profileWindow;
+		profileWindow->onWindowAboutToBeClosed.disconnect(boost::bind(&ProfileController::handleProfileWindowAboutToBeClosed, this, _1));
 	}
 	uiEventStream->onUIEvent.disconnect(boost::bind(&ProfileController::handleUIEvent, this, _1));
 }
@@ -39,6 +39,7 @@ void ProfileController::handleUIEvent(UIEvent::ref event) {
 		profileWindow = profileWindowFactory->createProfileWindow();
 		profileWindow->setEditable(true);
 		profileWindow->onVCardChangeRequest.connect(boost::bind(&ProfileController::handleVCardChangeRequest, this, _1));
+		profileWindow->onWindowAboutToBeClosed.connect(boost::bind(&ProfileController::handleProfileWindowAboutToBeClosed, this, _1));
 		vcardManager->onOwnVCardChanged.connect(boost::bind(&ProfileController::handleOwnVCardChanged, this, _1));
 	}
 	gettingVCard = true;
@@ -76,6 +77,10 @@ void ProfileController::handleOwnVCardChanged(VCard::ref vcard) {
 	}
 }
 
+void ProfileController::handleProfileWindowAboutToBeClosed(const JID&) {
+	profileWindow = NULL;
+}
+
 void ProfileController::setAvailable(bool b) {
 	available = b;
 	if (!available) {
diff --git a/Swift/Controllers/ProfileController.h b/Swift/Controllers/ProfileController.h
index c1afcf9..538df36 100644
--- a/Swift/Controllers/ProfileController.h
+++ b/Swift/Controllers/ProfileController.h
@@ -29,6 +29,7 @@ namespace Swift {
 			void handleVCardChangeRequest(VCard::ref vcard);
 			void handleSetVCardResponse(ErrorPayload::ref);
 			void handleOwnVCardChanged(VCard::ref vcard);
+			void handleProfileWindowAboutToBeClosed(const JID&);
 			void updateDialogStatus();
 
 		private:
diff --git a/Swift/Controllers/ShowProfileController.cpp b/Swift/Controllers/ShowProfileController.cpp
index ee0854b..15b7b26 100644
--- a/Swift/Controllers/ShowProfileController.cpp
+++ b/Swift/Controllers/ShowProfileController.cpp
@@ -25,7 +25,7 @@ ShowProfileController::ShowProfileController(VCardManager* vcardManager, Profile
 ShowProfileController::~ShowProfileController() {
 	typedef std::pair<JID, ProfileWindow*> JIDProfileWindowPair;
 	foreach(const JIDProfileWindowPair& jidWndPair, openedProfileWindows) {
-		jidWndPair.second->onWindowClosed.disconnect(boost::bind(&ShowProfileController::handleProfileWindowClosed, this, _1));
+		jidWndPair.second->onWindowAboutToBeClosed.disconnect(boost::bind(&ShowProfileController::handleProfileWindowAboutToBeClosed, this, _1));
 		delete jidWndPair.second;
 	}
 
@@ -42,7 +42,7 @@ void ShowProfileController::handleUIEvent(UIEvent::ref event) {
 	if (openedProfileWindows.find(showProfileEvent->getJID()) == openedProfileWindows.end()) {
 		ProfileWindow* newProfileWindow = profileWindowFactory->createProfileWindow();
 		newProfileWindow->setJID(showProfileEvent->getJID());
-		newProfileWindow->onWindowClosed.connect(boost::bind(&ShowProfileController::handleProfileWindowClosed, this, _1));
+		newProfileWindow->onWindowAboutToBeClosed.connect(boost::bind(&ShowProfileController::handleProfileWindowAboutToBeClosed, this, _1));
 		openedProfileWindows[showProfileEvent->getJID()] = newProfileWindow;
 		VCard::ref vcard = vcardManager->getVCardAndRequestWhenNeeded(showProfileEvent->getJID());
 		if (vcard) {
@@ -67,10 +67,8 @@ void ShowProfileController::handleVCardChanged(const JID& jid, VCard::ref vcard)
 	profileWindow->show();
 }
 
-void ShowProfileController::handleProfileWindowClosed(const JID& profileJid) {
-	ProfileWindow* profileWindow = openedProfileWindows[profileJid];
+void ShowProfileController::handleProfileWindowAboutToBeClosed(const JID& profileJid) {
 	openedProfileWindows.erase(profileJid);
-	delete profileWindow;
 }
 
 }
diff --git a/Swift/Controllers/ShowProfileController.h b/Swift/Controllers/ShowProfileController.h
index 5646f5e..27a0cf4 100644
--- a/Swift/Controllers/ShowProfileController.h
+++ b/Swift/Controllers/ShowProfileController.h
@@ -25,7 +25,7 @@ namespace Swift {
 		private:
 			void handleUIEvent(UIEvent::ref event);
 			void handleVCardChanged(const JID&, VCard::ref);
-			void handleProfileWindowClosed(const JID& profileJid);
+			void handleProfileWindowAboutToBeClosed(const JID& profileJid);
 
 		private:
 			VCardManager* vcardManager;
diff --git a/Swift/Controllers/UIInterfaces/ProfileWindow.h b/Swift/Controllers/UIInterfaces/ProfileWindow.h
index 0ce2ccf..5c158e1 100644
--- a/Swift/Controllers/UIInterfaces/ProfileWindow.h
+++ b/Swift/Controllers/UIInterfaces/ProfileWindow.h
@@ -30,6 +30,6 @@ namespace Swift {
 			virtual void hide() = 0;
 
 			boost::signal<void (VCard::ref)> onVCardChangeRequest;
-			boost::signal<void (const JID&)> onWindowClosed;
+			boost::signal<void (const JID&)> onWindowAboutToBeClosed;
 	};
 }
diff --git a/Swift/QtUI/QtProfileWindow.cpp b/Swift/QtUI/QtProfileWindow.cpp
index ccc6ae9..26b160a 100644
--- a/Swift/QtUI/QtProfileWindow.cpp
+++ b/Swift/QtUI/QtProfileWindow.cpp
@@ -22,14 +22,15 @@
 
 namespace Swift {
 
-QtProfileWindow::QtProfileWindow(QWidget* parent) :
-	QWidget(parent),
+QtProfileWindow::QtProfileWindow() :
+	QWidget(),
 	ui(new Ui::QtProfileWindow) {
 	ui->setupUi(this);
 	new QShortcut(QKeySequence::Close, this, SLOT(close()));
 	ui->throbberLabel->setMovie(new QMovie(":/icons/throbber.gif", QByteArray(), this));
 	connect(ui->savePushButton, SIGNAL(clicked()), SLOT(handleSave()));
 	setEditable(false);
+	setAttribute(Qt::WA_DeleteOnClose);
 }
 
 QtProfileWindow::~QtProfileWindow() {
@@ -104,8 +105,8 @@ void QtProfileWindow::updateTitle() {
 }
 
 void QtProfileWindow::closeEvent(QCloseEvent* event) {
-	onWindowClosed(jid);
 	event->accept();
+	onWindowAboutToBeClosed(jid);
 }
 
 void QtProfileWindow::handleSave() {
diff --git a/Swift/QtUI/QtProfileWindow.h b/Swift/QtUI/QtProfileWindow.h
index 1dbc0fb..a2af63a 100644
--- a/Swift/QtUI/QtProfileWindow.h
+++ b/Swift/QtUI/QtProfileWindow.h
@@ -28,7 +28,7 @@ class QtProfileWindow : public QWidget, public ProfileWindow {
 	Q_OBJECT
 
 	public:
-		explicit QtProfileWindow(QWidget* parent = 0);
+		QtProfileWindow();
 		virtual ~QtProfileWindow();
 
 		virtual void setJID(const JID& jid);
-- 
cgit v0.10.2-6-g49f6