mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Book details: if an item has an associated link then offer that link in the item's context menu.
This commit is contained in:
parent
71c0450221
commit
798a74babf
@ -2367,18 +2367,22 @@ class Cache:
|
||||
@read_api
|
||||
def get_all_link_maps_for_book(self, book_id):
|
||||
'''
|
||||
Returns all links for all fields referenced by book identified by book_id
|
||||
Returns all links for all fields referenced by book identified by book_id.
|
||||
If book_id is None or doesn't exist then the method returns {}.
|
||||
|
||||
Example: Assume author A has link X, author B has link Y, tag S has link
|
||||
F, and tag T has link G. IF book 1 has author A and tag T,
|
||||
F, and tag T has link G. If book 1 has author A and tag T,
|
||||
this method returns {'authors':{'A':'X'}, 'tags':{'T', 'G'}}
|
||||
If book 2's author is neither A nor B and has no tags, this method returns {}
|
||||
|
||||
:param book_id: the book id in question.
|
||||
|
||||
:return: {field: {field_value, link_value}, ... for all fields that have a non-empty link value for that book
|
||||
:return: {field: {field_value, link_value}, ... for all fields with a field_value having a non-empty link value for that book
|
||||
|
||||
'''
|
||||
if not self.has_id(book_id):
|
||||
# Works for book_id is None.
|
||||
return {}
|
||||
cached = self.link_maps_cache.get(book_id)
|
||||
if cached is not None:
|
||||
return cached
|
||||
|
@ -183,14 +183,14 @@ def mi_to_html(
|
||||
else:
|
||||
if not metadata['is_multiple']:
|
||||
val = '<a href="{}" title="{}">{}</a>'.format(
|
||||
search_action(field, val),
|
||||
search_action(field, val, book_id=book_id),
|
||||
_('Click to see books with {0}: {1}').format(metadata['name'], a(val)), p(val))
|
||||
else:
|
||||
all_vals = [v.strip()
|
||||
for v in val.split(metadata['is_multiple']['cache_to_list']) if v.strip()]
|
||||
if show_links:
|
||||
links = ['<a href="{}" title="{}">{}</a>'.format(
|
||||
search_action(field, x), _('Click to see books with {0}: {1}').format(
|
||||
search_action(field, x, book_id=book_id), _('Click to see books with {0}: {1}').format(
|
||||
metadata['name'], a(x)), p(x)) for x in all_vals]
|
||||
else:
|
||||
links = all_vals
|
||||
@ -210,7 +210,7 @@ def mi_to_html(
|
||||
extra = '<br><span style="font-size:smaller">%s</span>'%(
|
||||
prepare_string_for_xml(durl))
|
||||
if show_links:
|
||||
link = '<a href="{}" title="{}">{}</a>{}'.format(action(scheme, loc=loc),
|
||||
link = '<a href="{}" title="{}">{}</a>{}'.format(action(scheme, book_id=book_id, loc=loc),
|
||||
prepare_string_for_xml(path, True), pathstr, extra)
|
||||
else:
|
||||
link = prepare_string_for_xml(path, True)
|
||||
@ -238,7 +238,7 @@ def mi_to_html(
|
||||
if show_links:
|
||||
links = [
|
||||
'<a href="{}" title="{}:{}">{}</a>'.format(
|
||||
action('identifier', url=url, name=namel, id_type=id_typ, value=id_val, field='identifiers', book_id=book_id),
|
||||
action('identifier', book_id=book_id, url=url, name=namel, id_type=id_typ, value=id_val, field='identifiers'),
|
||||
a(id_typ), a(id_val), p(namel))
|
||||
for namel, id_typ, id_val, url in urls]
|
||||
links = value_list(', ', links)
|
||||
@ -266,7 +266,8 @@ def mi_to_html(
|
||||
else:
|
||||
aut = p(aut)
|
||||
if link:
|
||||
val = '<a title="%s" href="%s">%s</a>'%(a(lt), action('author', url=link, name=aut, title=lt), aut)
|
||||
val = '<a title="%s" href="%s">%s</a>'%(a(lt), action('author', book_id=book_id,
|
||||
url=link, name=aut, title=lt), aut)
|
||||
else:
|
||||
val = aut
|
||||
val += add_other_link('authors', aut)
|
||||
|
@ -391,6 +391,12 @@ def add_item_specific_entries(menu, data, book_info, copy_menu, search_menu):
|
||||
ac.data = ('authors', author, book_id)
|
||||
ac.setText(_('Remove %s from this book') % escape_for_menu(author))
|
||||
menu.addAction(ac)
|
||||
# See if we need to add a click associated link menu line for the author
|
||||
link_map = get_gui().current_db.new_api.get_all_link_maps_for_book(data.get('book_id', None))
|
||||
link = link_map.get("authors", {}).get(author, None)
|
||||
if link:
|
||||
menu.addAction(QIcon.ic('external-link'), _('Open associated link'),
|
||||
lambda : book_info.link_clicked.emit(link))
|
||||
elif dt in ('path', 'devpath'):
|
||||
path = data['loc']
|
||||
ac = book_info.copy_link_action
|
||||
@ -435,6 +441,12 @@ def add_item_specific_entries(menu, data, book_info, copy_menu, search_menu):
|
||||
ac.data = (field, remove_value, book_id)
|
||||
ac.setText(_('Remove %s from this book') % escape_for_menu(remove_name or data.get('original_value') or value))
|
||||
menu.addAction(ac)
|
||||
# See if we need to add a click associated link menu line
|
||||
link_map = get_gui().current_db.new_api.get_all_link_maps_for_book(data.get('book_id', None))
|
||||
link = link_map.get(field, {}).get(value, None)
|
||||
if link:
|
||||
menu.addAction(QIcon.ic('external-link'), _('Open associated link'),
|
||||
lambda : book_info.link_clicked.emit(link))
|
||||
else:
|
||||
v = data.get('original_value') or data.get('value')
|
||||
copy_menu.addAction(QIcon.ic('edit-copy.png'), _('The text: {}').format(v),
|
||||
|
Loading…
x
Reference in New Issue
Block a user