From 56c0102d20adcaf9ff278126536f4275ef35aac4 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 22 Feb 2020 09:25:45 +0530 Subject: [PATCH] When running Edit book on a book with no formats ask if an empty book should be created. Fixes #1864234 [[Enhancement] Add option to open Edit book even if the selected book is not an EPUB or AZW3](https://bugs.launchpad.net/calibre/+bug/1864234) --- src/calibre/gui2/actions/add.py | 24 ++++++++++++++---------- src/calibre/gui2/actions/tweak_epub.py | 22 ++++++++++++++++------ 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/calibre/gui2/actions/add.py b/src/calibre/gui2/actions/add.py index 97f23c3e5f..c22d5d2bf5 100644 --- a/src/calibre/gui2/actions/add.py +++ b/src/calibre/gui2/actions/add.py @@ -232,21 +232,25 @@ class AddAction(InterfaceAction): return for id_ in ids: - from calibre.ebooks.oeb.polish.create import create_book - pt = PersistentTemporaryFile(suffix='.' + format_) - pt.close() - try: - mi = db.new_api.get_metadata(id_, get_cover=False, - get_user_categories=False, cover_as_data=False) - create_book(mi, pt.name, fmt=format_) - db.add_format_with_hooks(id_, format_, pt.name, index_is_id=True, notify=True) - finally: - os.remove(pt.name) + self.add_empty_format_to_book(id_, format_) current_idx = self.gui.library_view.currentIndex() if current_idx.isValid(): view.model().current_changed(current_idx, current_idx) + def add_empty_format_to_book(self, book_id, fmt): + from calibre.ebooks.oeb.polish.create import create_book + db = self.gui.current_db + pt = PersistentTemporaryFile(suffix='.' + fmt.lower()) + pt.close() + try: + mi = db.new_api.get_metadata(book_id, get_cover=False, + get_user_categories=False, cover_as_data=False) + create_book(mi, pt.name, fmt=fmt.lower()) + db.add_format_with_hooks(book_id, fmt, pt.name, index_is_id=True, notify=True) + finally: + os.remove(pt.name) + def add_archive(self, single): paths = choose_files( self.gui, 'recursive-archive-add', _('Choose archive file'), diff --git a/src/calibre/gui2/actions/tweak_epub.py b/src/calibre/gui2/actions/tweak_epub.py index b8cfd8b1ce..d4c343cf6f 100644 --- a/src/calibre/gui2/actions/tweak_epub.py +++ b/src/calibre/gui2/actions/tweak_epub.py @@ -10,7 +10,7 @@ import time from PyQt5.Qt import QTimer, QDialog, QDialogButtonBox, QCheckBox, QVBoxLayout, QLabel, Qt -from calibre.gui2 import error_dialog +from calibre.gui2 import error_dialog, question_dialog from calibre.gui2.actions import InterfaceAction @@ -105,13 +105,23 @@ class TweakEpubAction(InterfaceAction): from calibre.ebooks.oeb.polish.main import SUPPORTED db = self.gui.library_view.model().db fmts = db.formats(book_id, index_is_id=True) or '' - fmts = [x.upper().strip() for x in fmts.split(',')] + fmts = [x.upper().strip() for x in fmts.split(',') if x] tweakable_fmts = set(fmts).intersection(SUPPORTED) if not tweakable_fmts: - return error_dialog(self.gui, _('Cannot edit book'), - _('The book must be in the %s formats to edit.' - '\n\nFirst convert the book to one of these formats.') % (_(' or ').join(SUPPORTED)), - show=True) + if not fmts: + if not question_dialog(self.gui, _('No editable formats'), + _('Do you want to create an empty EPUB file to edit?')): + return + tweakable_fmts = {'EPUB'} + self.gui.iactions['Add Books'].add_empty_format_to_book(book_id, 'EPUB') + current_idx = self.gui.library_view.currentIndex() + if current_idx.isValid(): + self.gui.library_view.model().current_changed(current_idx, current_idx) + else: + return error_dialog(self.gui, _('Cannot edit book'), _( + 'The book must be in the %s formats to edit.' + '\n\nFirst convert the book to one of these formats.' + ) % (_(' or ').join(SUPPORTED)), show=True) from calibre.gui2.tweak_book import tprefs tprefs.refresh() # In case they were changed in a Tweak Book process if len(tweakable_fmts) > 1: