Add a bookmark sorting method

This commit is contained in:
Philipp Hörist 2018-09-04 23:00:29 +02:00 committed by Philipp Hörist
parent 39c187ada4
commit 4d43fc4db5
4 changed files with 30 additions and 22 deletions

View File

@ -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)

View File

@ -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(

View File

@ -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)

View File

@ -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)