From 941b7cf9745c0b160d3f5357bc082c581b60df00 Mon Sep 17 00:00:00 2001
From: Yann Leboulanger <asterix@lagaule.org>
Date: Tue, 23 Sep 2008 14:39:04 +0000
Subject: [PATCH] sort group chats by status if option is checked. Fixes #3823

---
 src/config.py            | 17 ++++++++------
 src/groupchat_control.py | 48 +++++++++++++++++++++++++++++++++++++++-
 2 files changed, 57 insertions(+), 8 deletions(-)

diff --git a/src/config.py b/src/config.py
index c40f80b332..6cebc1e148 100644
--- a/src/config.py
+++ b/src/config.py
@@ -532,6 +532,13 @@ class PreferencesWindow:
 	def on_sort_by_show_checkbutton_toggled(self, widget):
 		self.on_checkbutton_toggled(widget, 'sort_by_show')
 		gajim.interface.roster.setup_and_draw_roster()
+		# Redraw connected groupchats
+		for account in gajim.connections:
+			if gajim.connections[account].connected:
+				for gc_control in gajim.interface.msg_win_mgr.get_controls(
+				message_control.TYPE_GC) + \
+				gajim.interface.minimized_controls[account].values():
+					gc_control.draw_roster()
 
 	def on_show_avatars_in_roster_checkbutton_toggled(self, widget):
 		self.on_checkbutton_toggled(widget, 'show_avatars_in_roster')
@@ -540,9 +547,9 @@ class PreferencesWindow:
 		for account in gajim.connections:
 			if gajim.connections[account].connected:
 				for gc_control in gajim.interface.msg_win_mgr.get_controls(
-					message_control.TYPE_GC) + \
-					gajim.interface.minimized_controls[account].values():
-						gc_control.draw_roster()
+				message_control.TYPE_GC) + \
+				gajim.interface.minimized_controls[account].values():
+					gc_control.draw_roster()
 
 	def on_show_status_msgs_in_roster_checkbutton_toggled(self, widget):
 		self.on_checkbutton_toggled(widget, 'show_status_msgs_in_roster')
@@ -563,10 +570,6 @@ class PreferencesWindow:
 		self.on_checkbutton_toggled(widget, 'show_tunes_in_roster')
 		gajim.interface.roster.setup_and_draw_roster()
 
-	def on_sort_by_show_checkbutton_toggled(self, widget):
-		self.on_checkbutton_toggled(widget, 'sort_by_show')
-		gajim.interface.roster.setup_and_draw_roster()
-
 	def on_emoticons_combobox_changed(self, widget):
 		active = widget.get_active()
 		model = widget.get_model()
diff --git a/src/groupchat_control.py b/src/groupchat_control.py
index 94c336d884..992dc8d503 100644
--- a/src/groupchat_control.py
+++ b/src/groupchat_control.py
@@ -320,7 +320,8 @@ class GroupchatControl(ChatControlBase):
 		self.handlers[id] = self.list_treeview
 		#status_image, shown_nick, type, nickname, avatar
 		store = gtk.TreeStore(gtk.Image, str, str, str, gtk.gdk.Pixbuf)
-		store.set_sort_column_id(C_TEXT, gtk.SORT_ASCENDING)
+		store.set_sort_func(C_NICK, self.tree_compare_iters)
+		store.set_sort_column_id(C_NICK, gtk.SORT_ASCENDING)
 		self.list_treeview.set_model(store)
 
 		# columns
@@ -374,6 +375,51 @@ class GroupchatControl(ChatControlBase):
 		self.conv_textview.tv.grab_focus()
 		self.widget.show_all()
 
+	def tree_compare_iters(self, model, iter1, iter2):
+		'''Compare two iters to sort them'''
+		type1 = model[iter1][C_TYPE]
+		type2 = model[iter2][C_TYPE]
+		if not type1 or not type2:
+			return 0
+		nick1 = model[iter1][C_NICK]
+		nick2 = model[iter2][C_NICK]
+		if not nick1 or not nick2:
+			return 0
+		nick1 = nick1.decode('utf-8')
+		nick2 = nick2.decode('utf-8')
+		if type1 == 'role':
+			if nick1 < nick2:
+				return -1
+			return 1
+		if type1 == 'contact':
+			gc_contact1 = gajim.contacts.get_gc_contact(self.account, self.room_jid,
+				nick1)
+			if not gc_contact1:
+				return 0
+		if type2 == 'contact':
+			gc_contact2 = gajim.contacts.get_gc_contact(self.account, self.room_jid,
+				nick2)
+			if not gc_contact2:
+				return 0
+		if type1 == 'contact' and type2 == 'contact' and \
+		gajim.config.get('sort_by_show'):
+			cshow = {'online':0, 'chat': 1, 'away': 2, 'xa': 3, 'dnd': 4,
+				'invisible': 5, 'offline': 6, 'error': 7}
+			show1 = cshow[gc_contact1.show]
+			show2 = cshow[gc_contact2.show]
+			if show1 < show2:
+				return -1
+			elif show1 > show2:
+				return 1
+		# We compare names
+		name1 = gc_contact1.get_shown_name()
+		name2 = gc_contact2.get_shown_name()
+		if name1.lower() < name2.lower():
+			return -1
+		if name2.lower() < name1.lower():
+			return 1
+		return 0
+
 	def on_msg_textview_populate_popup(self, textview, menu):
 		'''we override the default context menu and we prepend Clear
 		and the ability to insert a nick'''
-- 
GitLab