Add a bookmark sorting method
This commit is contained in:
parent
a9a115d359
commit
b62335eaf5
|
@ -15,6 +15,8 @@
|
||||||
# XEP-0048: Bookmarks
|
# XEP-0048: Bookmarks
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import copy
|
||||||
|
from collections import OrderedDict
|
||||||
|
|
||||||
import nbxmpp
|
import nbxmpp
|
||||||
|
|
||||||
|
@ -35,6 +37,27 @@ class Bookmarks:
|
||||||
|
|
||||||
self.handlers = []
|
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):
|
def _pubsub_support(self):
|
||||||
return (self._con.get_module('PEP').supported and
|
return (self._con.get_module('PEP').supported and
|
||||||
self._con.get_module('PubSub').publish_options)
|
self._con.get_module('PubSub').publish_options)
|
||||||
|
|
|
@ -45,13 +45,9 @@ class ManageBookmarksWindow:
|
||||||
None, None, None, None, account_label])
|
None, None, None, None, account_label])
|
||||||
|
|
||||||
con = app.connections[account]
|
con = app.connections[account]
|
||||||
bookmarks = con.get_module('Bookmarks').bookmarks
|
bookmarks = con.get_module('Bookmarks').get_sorted_bookmarks()
|
||||||
|
|
||||||
for jid, bookmark in bookmarks.items():
|
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
|
# make '1', '0', 'true', 'false' (or other) to True/False
|
||||||
autojoin = helpers.from_xs_boolean_to_python_boolean(
|
autojoin = helpers.from_xs_boolean_to_python_boolean(
|
||||||
|
|
|
@ -679,7 +679,8 @@ def get_groupchat_menu(control_id):
|
||||||
|
|
||||||
def get_bookmarks_menu(account, rebuild=False):
|
def get_bookmarks_menu(account, rebuild=False):
|
||||||
con = app.connections[account]
|
con = app.connections[account]
|
||||||
boomarks = con.get_module('Bookmarks').bookmarks
|
boomarks = con.get_module('Bookmarks').get_sorted_bookmarks(
|
||||||
|
short_name=True)
|
||||||
if not boomarks:
|
if not boomarks:
|
||||||
return None
|
return None
|
||||||
menu = Gio.Menu()
|
menu = Gio.Menu()
|
||||||
|
@ -695,13 +696,6 @@ def get_bookmarks_menu(account, rebuild=False):
|
||||||
section = Gio.Menu()
|
section = Gio.Menu()
|
||||||
for jid, bookmark in boomarks.items():
|
for jid, bookmark in boomarks.items():
|
||||||
name = bookmark['name']
|
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)
|
action = 'app.{}-activate-bookmark'.format(account)
|
||||||
menuitem = Gio.MenuItem.new(name, action)
|
menuitem = Gio.MenuItem.new(name, action)
|
||||||
|
|
|
@ -5488,15 +5488,10 @@ class RosterWindow:
|
||||||
item = Gtk.SeparatorMenuItem.new()
|
item = Gtk.SeparatorMenuItem.new()
|
||||||
gc_sub_menu.append(item)
|
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']
|
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.
|
# Do not use underline.
|
||||||
item = Gtk.MenuItem.new_with_label(name)
|
item = Gtk.MenuItem.new_with_label(name)
|
||||||
|
|
Loading…
Reference in New Issue