diff options
author | Kevin Smith <git@kismith.co.uk> | 2010-05-07 09:19:08 (GMT) |
---|---|---|
committer | Kevin Smith <git@kismith.co.uk> | 2010-05-07 09:19:08 (GMT) |
commit | 2dd59db4ae815e2e6e487e4c0078d3dc495e95f0 (patch) | |
tree | ccc8110e80071e743f5c536e4910886d2feef345 /Swiften | |
parent | 5409f27bbc3b32a65b2feed3259b1cc15da888a3 (diff) | |
download | swift-contrib-2dd59db4ae815e2e6e487e4c0078d3dc495e95f0.zip swift-contrib-2dd59db4ae815e2e6e487e4c0078d3dc495e95f0.tar.bz2 |
Sort the roster
Diffstat (limited to 'Swiften')
-rw-r--r-- | Swiften/Roster/GroupRosterItem.cpp | 23 | ||||
-rw-r--r-- | Swiften/Roster/GroupRosterItem.h | 3 | ||||
-rw-r--r-- | Swiften/Roster/RosterItem.cpp | 2 |
3 files changed, 24 insertions, 4 deletions
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); |