summaryrefslogtreecommitdiff
path: root/src/roster_sorting.py
diff options
context:
space:
mode:
authormathieui <mathieui@mathieui.net>2012-08-01 14:42:02 +0200
committermathieui <mathieui@mathieui.net>2012-08-01 14:42:02 +0200
commit9905646184361dba98ad742b39e946bb74580c7a (patch)
tree7fe279ec255d7d1aa4a0aea2e7c60cfea36c067f /src/roster_sorting.py
parentc890fefbc40377fd63f1d69011ed12a863e11043 (diff)
downloadpoezio-9905646184361dba98ad742b39e946bb74580c7a.tar.gz
poezio-9905646184361dba98ad742b39e946bb74580c7a.tar.bz2
poezio-9905646184361dba98ad742b39e946bb74580c7a.tar.xz
poezio-9905646184361dba98ad742b39e946bb74580c7a.zip
Various changes to the roster sorting
- Change the separator from _ to : - Move the functions away in another module to avoir cluttering the roster code - Add a case-sensitive sort (“sname”)
Diffstat (limited to 'src/roster_sorting.py')
-rw-r--r--src/roster_sorting.py90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/roster_sorting.py b/src/roster_sorting.py
new file mode 100644
index 00000000..c57f0dce
--- /dev/null
+++ b/src/roster_sorting.py
@@ -0,0 +1,90 @@
+"""
+Defines the roster sorting methods used in roster.py
+(for contacts/groups)
+"""
+
+########################### Contacts sorting ############################
+
+PRESENCE_PRIORITY = {'unavailable': 5,
+ 'xa': 4,
+ 'away': 3,
+ 'dnd': 2,
+ '': 1,
+ 'available': 1}
+
+def sort_jid(contact):
+ """Sort by contact JID"""
+ return contact.bare_jid
+
+def sort_show(contact):
+ """Sort by show (from high availability to low)"""
+ res = contact.get_highest_priority_resource()
+ if not res:
+ return 5
+ show = res.presence
+ if show not in PRESENCE_PRIORITY:
+ return 0
+ return PRESENCE_PRIORITY[show]
+
+def sort_resource_nb(contact):
+ """Sort by number of connected resources"""
+ return - len(contact)
+
+def sort_name(contact):
+ """Sort by name (case insensitive)"""
+ return contact.name.lower() or contact.bare_jid
+
+def sort_sname(contact):
+ """Sort by name (case sensitive)"""
+ return contact.name or contact.bare_jid
+
+def sort_online(contact):
+ """Sort by connected/disconnected"""
+ result = sort_show(contact)
+ return 0 if result < 5 else 1
+
+SORTING_METHODS = {
+ 'jid': sort_jid,
+ 'sname': sort_sname,
+ 'show': sort_show,
+ 'resource': sort_resource_nb,
+ 'name': sort_name,
+ 'online': sort_online,
+}
+
+
+######################## Roster Groups sorting ##########################
+
+def sort_group_name(group):
+ """Sort by name (case insensitive)"""
+ return group.name.lower()
+
+def sort_group_sname(group):
+ """Sort by name (case-sensitive)"""
+ return group.name
+
+def sort_group_folded(group):
+ """Sort by folded/unfolded"""
+ return group.folded
+
+def sort_group_connected(group):
+ """Sort by number of connected contacts"""
+ return - group.get_nb_connected_contacts()
+
+def sort_group_size(group):
+ """Sort by group size"""
+ return - len(group)
+
+def sort_group_none(group):
+ """Put the none group at the end, if any"""
+ return 0 if group.name != 'none' else 1
+
+GROUP_SORTING_METHODS = {
+ 'name': sort_group_name,
+ 'fold': sort_group_folded,
+ 'connected': sort_group_connected,
+ 'size': sort_group_size,
+ 'none': sort_group_none,
+ 'sname': sort_group_sname,
+}
+