From a65372d1a0a85d04329cd91bcf6bdff2de492c80 Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Mon, 12 Apr 2010 13:47:07 +0000
Subject: Show ErrorEvent when roster push fails.

Resolves: #293

diff --git a/Swift/Controllers/RosterController.cpp b/Swift/Controllers/RosterController.cpp
index 6ddabc2..ab1f1fb 100644
--- a/Swift/Controllers/RosterController.cpp
+++ b/Swift/Controllers/RosterController.cpp
@@ -15,6 +15,7 @@
 #include "Swiften/Queries/Requests/GetRosterRequest.h"
 #include "Swiften/Queries/Requests/SetRosterRequest.h"
 #include "Swiften/Events/SubscriptionRequestEvent.h"
+#include "Swiften/Events/ErrorEvent.h"
 #include "Swiften/Presence/PresenceOracle.h"
 #include "Swift/Controllers/EventController.h"
 #include "Swiften/Queries/IQRouter.h"
@@ -180,7 +181,12 @@ void RosterController::handleUIEvent(boost::shared_ptr<UIEvent> event) {
 }
 
 void RosterController::handleRosterSetError(boost::optional<ErrorPayload> error, boost::shared_ptr<RosterPayload> rosterPayload) {
-	//FIXME: Create error events.
+	String text = "Server " + myJID_.getDomain() + " rejected roster change to item '" + rosterPayload->getItems()[0].getJID() + "'";
+	if (!error->getText().isEmpty()) {
+		text += ": " + error->getText();
+	}
+	boost::shared_ptr<ErrorEvent> errorEvent(new ErrorEvent(JID(myJID_.getDomain()), text));
+	eventController_->handleIncomingEvent(errorEvent);
 }
 
 void RosterController::handleIncomingPresence(boost::shared_ptr<Presence> newPresence, boost::shared_ptr<Presence> /*oldPresence*/) {
diff --git a/Swift/QtUI/EventViewer/QtEvent.cpp b/Swift/QtUI/EventViewer/QtEvent.cpp
index 91df26f..76875ae 100644
--- a/Swift/QtUI/EventViewer/QtEvent.cpp
+++ b/Swift/QtUI/EventViewer/QtEvent.cpp
@@ -7,6 +7,7 @@
 #include "Swift/QtUI/EventViewer/QtEvent.h"
 
 #include "Swiften/Events/MessageEvent.h"
+#include "Swiften/Events/ErrorEvent.h"
 #include "Swiften/Events/SubscriptionRequestEvent.h"
 
 #include "Swift/QtUI/QtSwiftUtil.h"
@@ -41,6 +42,10 @@ QString QtEvent::text() {
 		String message = subscriptionRequestEvent->getJID().toBare().toString() + " would like to add you to their roster" + (reason.isEmpty() ? "." : ", saying '" + reason + "'.");
 		return P2QSTRING(message);
 	}
+	boost::shared_ptr<ErrorEvent> errorEvent = boost::dynamic_pointer_cast<ErrorEvent>(event_);
+	if (errorEvent) {
+		return P2QSTRING(errorEvent->getText());
+	}
 	return "";
 }
 
diff --git a/Swift/QtUI/EventViewer/QtEventWindow.cpp b/Swift/QtUI/EventViewer/QtEventWindow.cpp
index 84758ae..65b946a 100644
--- a/Swift/QtUI/EventViewer/QtEventWindow.cpp
+++ b/Swift/QtUI/EventViewer/QtEventWindow.cpp
@@ -10,6 +10,7 @@
 #include <QtDebug>
 
 #include "Swiften/Events/MessageEvent.h"
+#include "Swiften/Events/ErrorEvent.h"
 #include "Swift/QtUI/QtSubscriptionRequestWindow.h"
 #include "Swiften/Events/SubscriptionRequestEvent.h"
 #include "Swift/Controllers/UIEvents/RequestChatUIEvent.h"
@@ -44,12 +45,15 @@ void QtEventWindow::handleItemActivated(const QModelIndex& item) {
 	QtEvent* event = model_->getItem(item.row());
 	boost::shared_ptr<MessageEvent> messageEvent = boost::dynamic_pointer_cast<MessageEvent>(event->getEvent());
 	boost::shared_ptr<SubscriptionRequestEvent> subscriptionEvent = boost::dynamic_pointer_cast<SubscriptionRequestEvent>(event->getEvent());
+	boost::shared_ptr<ErrorEvent> errorEvent = boost::dynamic_pointer_cast<ErrorEvent>(event->getEvent());
 	
 	if (messageEvent) {
 		eventStream_->send(boost::shared_ptr<UIEvent>(new RequestChatUIEvent(messageEvent->getStanza()->getFrom())));
 	} else if (subscriptionEvent) {
 		QtSubscriptionRequestWindow* window = QtSubscriptionRequestWindow::getWindow(subscriptionEvent, this);
 		window->show();
+	} else if (errorEvent) {
+		errorEvent->onConclusion();
 	} else {
 		qWarning() << "Trying to activate an unexpected event";
 	}
diff --git a/Swiften/Events/ErrorEvent.h b/Swiften/Events/ErrorEvent.h
new file mode 100644
index 0000000..9a0d163
--- /dev/null
+++ b/Swiften/Events/ErrorEvent.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2010 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include <cassert>
+
+#include <boost/signals.hpp>
+#include <boost/shared_ptr.hpp>
+
+#include "Swiften/Events/StanzaEvent.h"
+#include "Swiften/Base/String.h"
+#include "Swiften/JID/JID.h"
+
+namespace Swift {
+	class ErrorEvent : public StanzaEvent {
+		public:
+			ErrorEvent(const JID& jid, const String& text) : jid_(jid), text_(text){};
+			virtual ~ErrorEvent(){};
+			const JID& getJID() const {return jid_;};
+			const String& getText() const {return text_;};
+
+		private:
+			JID jid_;
+			String text_;
+	};
+}
+
-- 
cgit v0.10.2-6-g49f6