From f77e2d693ddb4844710389391782f0572398b8f8 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 1 Nov 2022 08:30:57 +0530 Subject: [PATCH] When emailling to kindle set the filename based on the post plugboard title --- src/calibre/gui2/email.py | 23 ++++++++++------------- src/calibre/gui2/library/models.py | 14 +++++++++----- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/calibre/gui2/email.py b/src/calibre/gui2/email.py index a28c9dfe9c..bab33c28bd 100644 --- a/src/calibre/gui2/email.py +++ b/src/calibre/gui2/email.py @@ -401,12 +401,10 @@ class EmailMixin: # {{{ if not ids or len(ids) == 0: return - files, _auto_ids = self.library_view.model().get_preferred_formats_from_ids(ids, - fmts, set_metadata=True, - specific_format=specific_format, - exclude_auto=do_auto_convert, - use_plugboard=plugboard_email_value, - plugboard_formats=plugboard_email_formats) + modified_metadata = [] + files, _auto_ids = self.library_view.model().get_preferred_formats_from_ids( + ids, fmts, set_metadata=True, specific_format=specific_format, exclude_auto=do_auto_convert, + use_plugboard=plugboard_email_value, plugboard_formats=plugboard_email_formats, modified_metadata=modified_metadata) if do_auto_convert: nids = list(set(ids).difference(_auto_ids)) ids = [i for i in ids if i in nids] @@ -418,10 +416,10 @@ class EmailMixin: # {{{ bad, remove_ids, jobnames = [], [], [] texts, subjects, attachments, attachment_names = [], [], [], [] - for f, mi, id in zip(files, full_metadata, ids): - t = mi.title - if not t: - t = _('Unknown') + for f, mi, id, newmi in zip(files, full_metadata, ids, modified_metadata): + if not newmi: + newmi = mi + t = mi.title or _('Unknown') if f is None: bad.append(t) else: @@ -435,8 +433,7 @@ class EmailMixin: # {{{ if not components: components = [mi.title] subjects.append(os.path.join(*components)) - a = authors_to_string(mi.authors if mi.authors else - [_('Unknown')]) + a = authors_to_string(mi.authors or [_('Unknown')]) texts.append(_('Attached, you will find the e-book') + '\n\n' + t + '\n\t' + _('by') + ' ' + a + '\n\n' + _('in the %s format.') % @@ -445,7 +442,7 @@ class EmailMixin: # {{{ from calibre.utils.html2text import html2text texts[-1] += '\n\n' + _('About this book:') + '\n\n' + textwrap.fill(html2text(mi.comments)) if is_for_kindle(to): - prefix = str(t) + prefix = str(newmi.title or t) else: prefix = f'{t} - {a}' if not isinstance(prefix, str): diff --git a/src/calibre/gui2/library/models.py b/src/calibre/gui2/library/models.py index 0aa644d384..543eb8e40a 100644 --- a/src/calibre/gui2/library/models.py +++ b/src/calibre/gui2/library/models.py @@ -680,13 +680,15 @@ class BooksModel(QAbstractTableModel): # {{{ else: return metadata - def get_preferred_formats_from_ids(self, ids, formats, - set_metadata=False, specific_format=None, - exclude_auto=False, mode='r+b', - use_plugboard=None, plugboard_formats=None): + def get_preferred_formats_from_ids( + self, ids, formats, + set_metadata=False, specific_format=None, exclude_auto=False, mode='r+b', + use_plugboard=None, plugboard_formats=None, modified_metadata=None, + ): from calibre.ebooks.metadata.meta import set_metadata as _set_metadata ans = [] need_auto = [] + modified_metadata = [] if modified_metadata is None else modified_metadata if specific_format is not None: formats = [specific_format.lower()] for id in ids: @@ -705,12 +707,12 @@ class BooksModel(QAbstractTableModel): # {{{ pt = PersistentTemporaryFile(suffix='caltmpfmt.'+format) self.db.copy_format_to(id, format, pt, index_is_id=True) pt.seek(0) + newmi = None if set_metadata: try: mi = self.db.get_metadata(id, get_cover=True, index_is_id=True, cover_as_data=True) - newmi = None if use_plugboard and format.lower() in plugboard_formats: plugboards = self.db.new_api.pref('plugboards', {}) cpb = find_plugboard(use_plugboard, format.lower(), @@ -731,10 +733,12 @@ class BooksModel(QAbstractTableModel): # {{{ x = x.decode(filesystem_encoding) return x ans.append(to_uni(os.path.abspath(pt.name))) + modified_metadata.append(newmi) else: need_auto.append(id) if not exclude_auto: ans.append(None) + modified_metadata.append(None) return ans, need_auto def get_preferred_formats(self, rows, formats, paths=False,