From 2dd59db4ae815e2e6e487e4c0078d3dc495e95f0 Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Fri, 7 May 2010 09:19:08 +0000
Subject: Sort the roster


diff --git a/Swift/QtUI/Roster/RosterModel.cpp b/Swift/QtUI/Roster/RosterModel.cpp
index 408cc3e..d5b4002 100644
--- a/Swift/QtUI/Roster/RosterModel.cpp
+++ b/Swift/QtUI/Roster/RosterModel.cpp
@@ -182,7 +182,7 @@ int RosterModel::rowCount(const QModelIndex& parent) const {
 	Q_ASSERT(item);
 	GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(item);
 	int count = group ? group->getDisplayedChildren().size() : 0;
-	qDebug() << "rowCount = " << count << " where parent.isValid() == " << parent.isValid() << ", group == " << (group ? P2QSTRING(group->getDisplayName()) : "*contact*");
+//	qDebug() << "rowCount = " << count << " where parent.isValid() == " << parent.isValid() << ", group == " << (group ? P2QSTRING(group->getDisplayName()) : "*contact*");
 	return count;
 }
 
diff --git a/Swiften/Roster/GroupRosterItem.cpp b/Swiften/Roster/GroupRosterItem.cpp
index b55ce17..2632444 100644
--- a/Swiften/Roster/GroupRosterItem.cpp
+++ b/Swiften/Roster/GroupRosterItem.cpp
@@ -7,6 +7,7 @@
 #include "Swiften/Roster/GroupRosterItem.h"
 
 #include <boost/bind.hpp>
+//#include <boost/algorithm.hpp>
 #include <iostream>
 
 namespace Swift {
@@ -33,6 +34,8 @@ void GroupRosterItem::addChild(RosterItem* item) {
 	GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(item);
 	if (group) {
 		group->onChildrenChanged.connect(boost::bind(&GroupRosterItem::handleChildrenChanged, this, group));
+	} else {
+		item->onDataChanged.connect(boost::bind(&GroupRosterItem::handleDataChanged, this, item));
 	}
 	onChildrenChanged();
 	onDataChanged();
@@ -68,8 +71,17 @@ ContactRosterItem* GroupRosterItem::removeChild(const JID& jid) {
 	return removed;
 }
 
-void GroupRosterItem::sortDisplayed() {
-	std::stable_sort(displayedChildren_.begin(), displayedChildren_.end(), itemLessThan);
+/**
+ * Returns false if the list didn't need a resort
+ */
+bool GroupRosterItem::sortDisplayed() {
+	/* Not doing this until we import boost::algorithm*/
+//	if (boost::is_sorted(displayedChildren_begin(), displayedChildren_.end(), itemLessThan)) {
+//		return false;
+//	}
+	//Sholudn't need stable_sort here
+	std::sort(displayedChildren_.begin(), displayedChildren_.end(), itemLessThan);
+	return true;
 }
 
 bool GroupRosterItem::itemLessThan(const RosterItem* left, const RosterItem* right) {
@@ -90,6 +102,7 @@ bool GroupRosterItem::itemLessThan(const RosterItem* left, const RosterItem* rig
 		if (rightContact) {
 			return true;
 		}
+//		std::cout << "Comparing groups " << left->getSortableDisplayName() << " and " << right->getSortableDisplayName() << std::endl;
 		return left->getSortableDisplayName() < right->getSortableDisplayName();
 	}
 }
@@ -114,6 +127,12 @@ void GroupRosterItem::setDisplayed(RosterItem* item, bool displayed) {
 	onChildrenChanged();
 }
 
+void GroupRosterItem::handleDataChanged(RosterItem* /*item*/) {
+	if (sortDisplayed()) {
+		onChildrenChanged();
+	}
+}
+
 void GroupRosterItem::handleChildrenChanged(GroupRosterItem* group) {
 	size_t oldSize = getDisplayedChildren().size();
 	if (group->getDisplayedChildren().size() > 0) {
diff --git a/Swiften/Roster/GroupRosterItem.h b/Swiften/Roster/GroupRosterItem.h
index 5e16b2b..0d230e2 100644
--- a/Swiften/Roster/GroupRosterItem.h
+++ b/Swiften/Roster/GroupRosterItem.h
@@ -27,7 +27,8 @@ class GroupRosterItem : public RosterItem {
 		static bool itemLessThan(const RosterItem* left, const RosterItem* right);
 	private:
 		void handleChildrenChanged(GroupRosterItem* group);
-		void sortDisplayed();
+		void handleDataChanged(RosterItem* item);
+		bool sortDisplayed();
 		String name_;
 		std::vector<RosterItem*> children_;
 		std::vector<RosterItem*> displayedChildren_;
diff --git a/Swiften/Roster/RosterItem.cpp b/Swiften/Roster/RosterItem.cpp
index 7229199..3ce13b4 100644
--- a/Swiften/Roster/RosterItem.cpp
+++ b/Swiften/Roster/RosterItem.cpp
@@ -10,7 +10,7 @@
 
 namespace Swift {
 
-RosterItem::RosterItem(const String& name, GroupRosterItem* parent) : name_(name), parent_(parent) {
+RosterItem::RosterItem(const String& name, GroupRosterItem* parent) : name_(name), sortableDisplayName_(name_.getLowerCase()), parent_(parent) {
 	/* The following would be good, but because of C++'s inheritance not working in constructors, it's not going to work. */
 	//if (parent) {
 	//	parent_->addChild(this);
-- 
cgit v0.10.2-6-g49f6