From 2598ef4efcd74d5e9e701c62337bf84829572508 Mon Sep 17 00:00:00 2001
From: Tobias Markmann <tm@ayena.de>
Date: Tue, 9 Jun 2015 23:29:20 +0200
Subject: Show old vCard in UI if vCard update is rejected by server

Previously QtVCardWidget would work on a shared copy of a VCard and
modify the shared version. Now when setting the vCard on the widget
it creates its own copy and works on that.

The ProfileController holds a reference to the VCard it sets on the UI.
If the server rejects an update it can reset the UI to this VCard.

Test-Information:

Tested against a XMPP server that rejects all vCard updates
(mod_readonly).

Change-Id: Iaa8eed6406f2dc6cc7ac5434194a7dae4f879b33

diff --git a/Swift/Controllers/ProfileController.cpp b/Swift/Controllers/ProfileController.cpp
index be7b72c..7df8036 100644
--- a/Swift/Controllers/ProfileController.cpp
+++ b/Swift/Controllers/ProfileController.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010 Isode Limited.
+ * Copyright (c) 2010-2015 Isode Limited.
  * All rights reserved.
  * See the COPYING file for more information.
  */
@@ -8,12 +8,12 @@
 
 #include <boost/bind.hpp>
 
+#include <Swiften/VCards/VCardManager.h>
+
+#include <Swift/Controllers/Intl.h>
 #include <Swift/Controllers/UIEvents/RequestProfileEditorUIEvent.h>
 #include <Swift/Controllers/UIEvents/UIEventStream.h>
 #include <Swift/Controllers/UIInterfaces/ProfileWindowFactory.h>
-#include <Swiften/VCards/VCardManager.h>
-#include <Swift/Controllers/Intl.h>
-
 
 namespace Swift {
 
@@ -61,6 +61,7 @@ void ProfileController::handleSetVCardResponse(ErrorPayload::ref error) {
 	pendingSetVCardRequest.reset();
 	updateDialogStatus();
 	if (error) {
+		profileWindow->setVCard(vcardBeforeEdit);
 		profileWindow->setError(QT_TRANSLATE_NOOP("", "There was an error publishing your profile data"));
 	}
 	else {
@@ -72,6 +73,7 @@ void ProfileController::handleSetVCardResponse(ErrorPayload::ref error) {
 void ProfileController::handleOwnVCardChanged(VCard::ref vcard) {
 	if (profileWindow) {
 		profileWindow->setVCard(vcard);
+		vcardBeforeEdit = vcard;
 		gettingVCard = false;
 		updateDialogStatus();
 	}
diff --git a/Swift/Controllers/ProfileController.h b/Swift/Controllers/ProfileController.h
index 466834f..c6f5420 100644
--- a/Swift/Controllers/ProfileController.h
+++ b/Swift/Controllers/ProfileController.h
@@ -1,16 +1,17 @@
 /*
- * Copyright (c) 2010 Isode Limited.
+ * Copyright (c) 2010-2015 Isode Limited.
  * All rights reserved.
  * See the COPYING file for more information.
  */
 
 #pragma once
 
-#include <Swift/Controllers/UIEvents/UIEvent.h>
-#include <Swiften/Elements/VCard.h>
 #include <Swiften/Elements/ErrorPayload.h>
+#include <Swiften/Elements/VCard.h>
 #include <Swiften/VCards/SetVCardRequest.h>
 
+#include <Swift/Controllers/UIEvents/UIEvent.h>
+
 namespace Swift {
 	class UIEventStream;
 	class ProfileWindowFactory;
@@ -40,6 +41,7 @@ namespace Swift {
 			SetVCardRequest::ref pendingSetVCardRequest;
 			ProfileWindow* profileWindow;
 			bool gettingVCard;
+			VCard::ref vcardBeforeEdit;
 	};
 }
 
diff --git a/Swift/QtUI/QtVCardWidget/QtVCardWidget.cpp b/Swift/QtUI/QtVCardWidget/QtVCardWidget.cpp
index 79360d8..ac087ed 100644
--- a/Swift/QtUI/QtVCardWidget/QtVCardWidget.cpp
+++ b/Swift/QtUI/QtVCardWidget/QtVCardWidget.cpp
@@ -13,7 +13,6 @@
 #include <Swift/QtUI/QtVCardWidget/QtVCardWidget.h>
 #include <Swift/QtUI/QtVCardWidget/ui_QtVCardWidget.h>
 
-#include <QDebug>
 #include <QLineEdit>
 #include <QMenu>
 
@@ -112,7 +111,7 @@ void QtVCardWidget::setEditable(bool editable) {
 
 void QtVCardWidget::setVCard(VCard::ref vcard) {
 	clearFields();
-	this->vcard = vcard;
+	this->vcard = boost::make_shared<VCard>(*vcard);
 	ui->photoAndName->setFormattedName(P2QSTRING(vcard->getFullName()));
 	ui->photoAndName->setNickname(P2QSTRING(vcard->getNickname()));
 	ui->photoAndName->setPrefix(P2QSTRING(vcard->getPrefix()));
-- 
cgit v0.10.2-6-g49f6