From a719c9898cba1e5a618996ae506256a0c2556cc4 Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Sun, 3 Oct 2010 16:10:46 +0100
Subject: Show contacts as unavailable after subscription removal.

Resolves: #455

diff --git a/Swift/Controllers/RosterController.cpp b/Swift/Controllers/RosterController.cpp
index 5288df1..96056ca 100644
--- a/Swift/Controllers/RosterController.cpp
+++ b/Swift/Controllers/RosterController.cpp
@@ -146,7 +146,6 @@ void RosterController::handleOnJIDUpdated(const JID& jid, const String& oldName,
 			roster_->removeContactFromGroup(jid, group);
 		}
 	}
-	
 }
 
 void RosterController::handleUIEvent(boost::shared_ptr<UIEvent> event) {
@@ -225,7 +224,13 @@ void RosterController::handleRosterSetError(boost::optional<ErrorPayload> error,
 }
 
 void RosterController::handleIncomingPresence(boost::shared_ptr<Presence> newPresence) {
-	roster_->applyOnItems(SetPresence(newPresence));
+	boost::shared_ptr<Presence> appliedPresence(newPresence);
+	if (newPresence->getType() == Presence::Unsubscribe) {
+		/* In 3921bis, subscription removal isn't followed by a presence push of unavailable*/
+		appliedPresence = boost::shared_ptr<Presence>(new Presence());
+		appliedPresence->setFrom(newPresence->getFrom().toBare());
+	}
+	roster_->applyOnItems(SetPresence(appliedPresence));
 }
 
 void RosterController::handleSubscriptionRequest(const JID& jid, const String& message) {
diff --git a/Swiften/Roster/ContactRosterItem.cpp b/Swiften/Roster/ContactRosterItem.cpp
index 1c27395..9251fad 100644
--- a/Swiften/Roster/ContactRosterItem.cpp
+++ b/Swiften/Roster/ContactRosterItem.cpp
@@ -80,8 +80,13 @@ void ContactRosterItem::applyPresence(const String& resource, boost::shared_ptr<
 		offlinePresence_ = boost::shared_ptr<Presence>();
 	}
 	if (presence->getType() == Presence::Unavailable) {
-		if (presences_.find(resource) != presences_.end()) {
-			presences_.erase(resource);
+		if (resource.isEmpty()) {
+			/* Unavailable from the bare JID means all resources are offline.*/
+			presences_.clear();
+		} else {
+			if (presences_.find(resource) != presences_.end()) {
+				presences_.erase(resource);
+			}
 		}
 		if (presences_.size() > 0) {
 			offlinePresence_ = presence;
-- 
cgit v0.10.2-6-g49f6