From b62335eaf5a551421a76420e46e2cd9d3242ad47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=B6rist?= Date: Tue, 4 Sep 2018 23:00:29 +0200 Subject: [PATCH] Add a bookmark sorting method --- gajim/common/modules/bookmarks.py | 23 +++++++++++++++++++++++ gajim/gtk/bookmarks.py | 8 ++------ gajim/gui_menu_builder.py | 10 ++-------- gajim/roster_window.py | 11 +++-------- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/gajim/common/modules/bookmarks.py b/gajim/common/modules/bookmarks.py index 1e9fbeb2a..7f1da15c4 100644 --- a/gajim/common/modules/bookmarks.py +++ b/gajim/common/modules/bookmarks.py @@ -15,6 +15,8 @@ # XEP-0048: Bookmarks import logging +import copy +from collections import OrderedDict import nbxmpp @@ -35,6 +37,27 @@ class Bookmarks: self.handlers = [] + def get_sorted_bookmarks(self, short_name=False): + # This returns a sorted by name copy of the bookmarks + sorted_bookmarks = {} + for jid, bookmarks in self.bookmarks.items(): + bookmark_copy = copy.deepcopy(bookmarks) + if not bookmark_copy['name']: + # No name was given for this bookmark + # Use the first part of JID instead + name = jid.split("@")[0] + bookmark_copy['name'] = name + + if short_name: + name = bookmark_copy['name'] + name = (name[:42] + '..') if len(name) > 42 else name + bookmark_copy['name'] = name + + sorted_bookmarks[jid] = bookmark_copy + return OrderedDict( + sorted(sorted_bookmarks.items(), + key=lambda bookmark: bookmark[1]['name'].lower())) + def _pubsub_support(self): return (self._con.get_module('PEP').supported and self._con.get_module('PubSub').publish_options) diff --git a/gajim/gtk/bookmarks.py b/gajim/gtk/bookmarks.py index d6e36f3a9..808aaf9c6 100644 --- a/gajim/gtk/bookmarks.py +++ b/gajim/gtk/bookmarks.py @@ -45,13 +45,9 @@ class ManageBookmarksWindow: None, None, None, None, account_label]) con = app.connections[account] - bookmarks = con.get_module('Bookmarks').bookmarks + bookmarks = con.get_module('Bookmarks').get_sorted_bookmarks() + for jid, bookmark in bookmarks.items(): - if not bookmark['name']: - # No name was given for this bookmark. - # Use the first part of JID instead... - name = jid.split("@")[0] - bookmark['name'] = name # make '1', '0', 'true', 'false' (or other) to True/False autojoin = helpers.from_xs_boolean_to_python_boolean( diff --git a/gajim/gui_menu_builder.py b/gajim/gui_menu_builder.py index d293ac5de..8a8ec2013 100644 --- a/gajim/gui_menu_builder.py +++ b/gajim/gui_menu_builder.py @@ -679,7 +679,8 @@ def get_groupchat_menu(control_id): def get_bookmarks_menu(account, rebuild=False): con = app.connections[account] - boomarks = con.get_module('Bookmarks').bookmarks + boomarks = con.get_module('Bookmarks').get_sorted_bookmarks( + short_name=True) if not boomarks: return None menu = Gio.Menu() @@ -695,13 +696,6 @@ def get_bookmarks_menu(account, rebuild=False): section = Gio.Menu() for jid, bookmark in boomarks.items(): name = bookmark['name'] - if not name: - # No name was given for this bookmark. - # Use the first part of JID instead... - name = jid.split("@")[0] - - # Shorten long names - name = (name[:42] + '..') if len(name) > 42 else name action = 'app.{}-activate-bookmark'.format(account) menuitem = Gio.MenuItem.new(name, action) diff --git a/gajim/roster_window.py b/gajim/roster_window.py index 2b3155134..a2199f18e 100644 --- a/gajim/roster_window.py +++ b/gajim/roster_window.py @@ -5488,15 +5488,10 @@ class RosterWindow: item = Gtk.SeparatorMenuItem.new() gc_sub_menu.append(item) - for jid, bookmark in con.get_module('Bookmarks').bookmarks.items(): + bookmarks = con.get_module('Bookmarks').get_sorted_bookmarks( + short_name=True) + for jid, bookmark in bookmarks.items(): name = bookmark['name'] - if not name: - # No name was given for this bookmark. - # Use the first part of JID instead... - name = jid.split("@")[0] - - # Shorten long names - name = (name[:42] + '..') if len(name) > 42 else name # Do not use underline. item = Gtk.MenuItem.new_with_label(name)