From 0c4887461ea3009f2f716ab0965f87471d5ac099 Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Sat, 30 Apr 2011 13:37:13 +0100
Subject: Allow groups to be resorted based on changing names.

Resolves: #794

diff --git a/Swift/Controllers/Chat/MUCController.cpp b/Swift/Controllers/Chat/MUCController.cpp
index 19489d3..3c6f965 100644
--- a/Swift/Controllers/Chat/MUCController.cpp
+++ b/Swift/Controllers/Chat/MUCController.cpp
@@ -322,7 +322,9 @@ void MUCController::handleOccupantRoleChanged(const std::string& nick, const MUC
 	if (occupant.getRealJID()) {
 		realJID = occupant.getRealJID().get();
 	}
-	roster_->addContact(jid, realJID, nick, roleToGroupName(occupant.getRole()), avatarManager_->getAvatarPath(jid).string());
+	std::string group(roleToGroupName(occupant.getRole()));
+	roster_->addContact(jid, realJID, nick, group, avatarManager_->getAvatarPath(jid).string());
+	roster_->getGroup(group)->setManualSort(roleToSortName(occupant.getRole()));
 	chatWindow_->addSystemMessage(str(format(QT_TRANSLATE_NOOP("", "%1% is now a %2%")) % nick % roleToFriendlyName(occupant.getRole())));
 }
 
diff --git a/Swift/Controllers/Roster/GroupRosterItem.cpp b/Swift/Controllers/Roster/GroupRosterItem.cpp
index a1c1156..2b56d1f 100644
--- a/Swift/Controllers/Roster/GroupRosterItem.cpp
+++ b/Swift/Controllers/Roster/GroupRosterItem.cpp
@@ -224,7 +224,8 @@ void GroupRosterItem::handleChildrenChanged(GroupRosterItem* group) {
 	} else {
 		displayedChildren_.erase(std::remove(displayedChildren_.begin(), displayedChildren_.end(), group), displayedChildren_.end());
 	}
-	if (oldSize != getDisplayedChildren().size()) {
+
+	if (oldSize != getDisplayedChildren().size() || sortDisplayed()) {
 		onChildrenChanged();
 		onDataChanged();
 	}
diff --git a/Swift/Controllers/Roster/UnitTest/RosterTest.cpp b/Swift/Controllers/Roster/UnitTest/RosterTest.cpp
index cbef787..4444e8a 100644
--- a/Swift/Controllers/Roster/UnitTest/RosterTest.cpp
+++ b/Swift/Controllers/Roster/UnitTest/RosterTest.cpp
@@ -21,6 +21,7 @@ class RosterTest : public CppUnit::TestFixture {
 		CPPUNIT_TEST(testRemoveSecondContact);
 		CPPUNIT_TEST(testRemoveSecondContactSameBare);
 		CPPUNIT_TEST(testApplyPresenceLikeMUC);
+		CPPUNIT_TEST(testReSortLikeMUC);
 		CPPUNIT_TEST_SUITE_END();
 
 	public:
@@ -117,6 +118,22 @@ class RosterTest : public CppUnit::TestFixture {
 
 		}
 
+		void testReSortLikeMUC() {
+			JID jid4a("a@b/c");
+			JID jid4b("a@b/d");
+			JID jid4c("a@b/e");
+			roster_->addContact(jid4a, JID(), "Bird", "group1", "");
+			roster_->addContact(jid4b, JID(), "Cookie", "group2", "");
+			roster_->addContact(jid4b, JID(), "Ernie", "group1", "");
+			roster_->getGroup("group1")->setManualSort("2");
+			roster_->getGroup("group2")->setManualSort("1");
+			GroupRosterItem* root = roster_->getRoot();
+			const std::vector<RosterItem*> kids = root->getDisplayedChildren();
+			CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), kids.size());
+			CPPUNIT_ASSERT_EQUAL(std::string("group2"), kids[0]->getDisplayName());
+			CPPUNIT_ASSERT_EQUAL(std::string("group1"), kids[1]->getDisplayName());
+		}
+
 	private:
 		Roster *roster_;
 		JID jid1_;
-- 
cgit v0.10.2-6-g49f6