From 43fce94494307d1b3a866f5781528c94df7e5bca Mon Sep 17 00:00:00 2001
From: Richard Maudsley <richard.maudsley@isode.com>
Date: Thu, 3 Jul 2014 11:42:32 +0100
Subject: Honour 'show offline contacts' toggle when exiting roster search
 filter

Test-Information:

Enter search term and check that toggling "show offline contacts" resets the QtFilterWidget and that the roster list is representative of the "show offline contacts" state. Ensure that "show offline contacts" behaves normally when no search term has been entered. Toggle "show offline contacts" several times to make sure it remains in-sync.

Change-Id: Id92a6a65b8f38cf6806f22c175514283e05affa9

diff --git a/Swift/Controllers/Roster/Roster.cpp b/Swift/Controllers/Roster/Roster.cpp
index 44cf03d..4dbc453 100644
--- a/Swift/Controllers/Roster/Roster.cpp
+++ b/Swift/Controllers/Roster/Roster.cpp
@@ -197,7 +197,13 @@ void Roster::applyOnAllItems(const RosterItemOperation& operation) {
 	filterAll();
 }
 
-void Roster::removeFilter(RosterFilter *filter) {
+void Roster::addFilter(RosterFilter* filter) {
+	filters_.push_back(filter);
+	filterAll();
+	onFilterAdded(filter);
+}
+
+void Roster::removeFilter(RosterFilter* filter) {
 	for (unsigned int i = 0; i < filters_.size(); i++) {
 		if (filters_[i] == filter) {
 			filters_.erase(filters_.begin() + i);
@@ -205,6 +211,7 @@ void Roster::removeFilter(RosterFilter *filter) {
 		}
 	}
 	filterAll();
+	onFilterRemoved(filter);
 }
 
 void Roster::filterContact(ContactRosterItem* contact, GroupRosterItem* group) {
diff --git a/Swift/Controllers/Roster/Roster.h b/Swift/Controllers/Roster/Roster.h
index b60eafa..9956cb9 100644
--- a/Swift/Controllers/Roster/Roster.h
+++ b/Swift/Controllers/Roster/Roster.h
@@ -38,14 +38,16 @@ class Roster {
 		void applyOnItems(const RosterItemOperation& operation);
 		void applyOnAllItems(const RosterItemOperation& operation);
 		void applyOnItem(const RosterItemOperation& operation, const JID& jid);
-		void addFilter(RosterFilter *filter) {filters_.push_back(filter);filterAll();}
-		void removeFilter(RosterFilter *filter);
+		void addFilter(RosterFilter* filter);
+		void removeFilter(RosterFilter* filter);
 		GroupRosterItem* getRoot();
 		std::vector<RosterFilter*> getFilters() {return filters_;}
 		boost::signal<void (GroupRosterItem*)> onChildrenChanged;
 		boost::signal<void (GroupRosterItem*)> onGroupAdded;
 		boost::signal<void (RosterItem*)> onDataChanged;
 		boost::signal<void (JID&)> onVCardUpdateRequested;
+		boost::signal<void (RosterFilter* filter)> onFilterAdded;
+		boost::signal<void (RosterFilter* filter)> onFilterRemoved;
 		GroupRosterItem* getGroup(const std::string& groupName);
 		void setBlockingSupported(bool isSupported);
 
diff --git a/Swift/QtUI/Roster/QtFilterWidget.cpp b/Swift/QtUI/Roster/QtFilterWidget.cpp
index a4c9449..64eb312 100644
--- a/Swift/QtUI/Roster/QtFilterWidget.cpp
+++ b/Swift/QtUI/Roster/QtFilterWidget.cpp
@@ -86,7 +86,6 @@ bool QtFilterWidget::eventFilter(QObject*, QEvent* event) {
 		}
 
 		filterLineEdit_->event(event);
-		filterLineEdit_->setVisible(!filterLineEdit_->text().isEmpty());
 
 		if (event->type() == QEvent::KeyRelease) {
 			updateRosterFilters();
@@ -102,9 +101,13 @@ void QtFilterWidget::popAllFilters() {
 		filters_.push_back(filter);
 		treeView_->getRoster()->removeFilter(filter);
 	}
+	treeView_->getRoster()->onFilterAdded.connect(boost::bind(&QtFilterWidget::handleFilterAdded, this, _1));
+	treeView_->getRoster()->onFilterRemoved.connect(boost::bind(&QtFilterWidget::handleFilterRemoved, this, _1));
 }
 
 void QtFilterWidget::pushAllFilters() {
+	treeView_->getRoster()->onFilterAdded.disconnect(boost::bind(&QtFilterWidget::handleFilterAdded, this, _1));
+	treeView_->getRoster()->onFilterRemoved.disconnect(boost::bind(&QtFilterWidget::handleFilterRemoved, this, _1));
 	foreach(RosterFilter* filter, filters_) {
 		treeView_->getRoster()->addFilter(filter);
 	}
@@ -130,6 +133,7 @@ void QtFilterWidget::updateRosterFilters() {
 			updateSearchFilter();
 		}
 	}
+	filterLineEdit_->setVisible(!filterLineEdit_->text().isEmpty());
 }
 
 void QtFilterWidget::updateSearchFilter() {
@@ -143,4 +147,20 @@ void QtFilterWidget::updateSearchFilter() {
 	treeView_->setCurrentIndex(sourceModel_->index(0, 0, sourceModel_->index(0,0)));
 }
 
+void QtFilterWidget::handleFilterAdded(RosterFilter* filter) {
+	if (filter != fuzzyRosterFilter_) {
+		filterLineEdit_->setText("");
+		updateRosterFilters();
+	}
+}
+
+void QtFilterWidget::handleFilterRemoved(RosterFilter* filter) {
+	/* make sure we don't end up adding this one back in later */
+	filters_.erase(std::remove(filters_.begin(), filters_.end(), filter), filters_.end());
+	if (filter != fuzzyRosterFilter_) {
+		filterLineEdit_->setText("");
+		updateRosterFilters();
+	}
+}
+
 }
diff --git a/Swift/QtUI/Roster/QtFilterWidget.h b/Swift/QtUI/Roster/QtFilterWidget.h
index 3e17566..d0307ea 100644
--- a/Swift/QtUI/Roster/QtFilterWidget.h
+++ b/Swift/QtUI/Roster/QtFilterWidget.h
@@ -35,6 +35,9 @@ class QtFilterWidget : public QWidget {
 		void updateRosterFilters();
 		void updateSearchFilter();
 
+		void handleFilterAdded(RosterFilter* filter);
+		void handleFilterRemoved(RosterFilter* filter);
+
 	private:
 		QtClosableLineEdit* filterLineEdit_;
 		QtTreeWidget* treeView_;
-- 
cgit v0.10.2-6-g49f6