Edit book: When deleting files, automatically remove all entries that refer to them from the ToC

This commit is contained in:
Kovid Goyal 2013-12-20 15:01:16 +05:30
parent 873ebee3ed
commit 746ceb6c66
2 changed files with 30 additions and 1 deletions

View File

@ -45,6 +45,16 @@ class TOC(object):
self.children.remove(child) self.children.remove(child)
child.parent = None child.parent = None
def remove_from_parent(self):
if self.parent is None:
return
idx = self.parent.children.index(self)
for child in reversed(self.children):
child.parent = self.parent
self.parent.children.insert(idx, child)
self.parent.children.remove(self)
self.parent = None
def __iter__(self): def __iter__(self):
for c in self.children: for c in self.children:
yield c yield c
@ -407,3 +417,18 @@ def commit_toc(container, toc, lang=None, uid=None):
container.replace(tocname, root) container.replace(tocname, root)
container.pretty_print.add(tocname) container.pretty_print.add(tocname)
def remove_names_from_toc(container, names):
toc = get_toc(container)
if len(toc) == 0:
return False
remove = []
names = frozenset(names)
for node in toc.iterdescendants():
if node.dest in names:
remove.append(node)
if remove:
for node in reversed(remove):
node.remove_from_parent()
commit_toc(container, toc)
return True
return False

View File

@ -23,6 +23,7 @@ from calibre.ebooks.oeb.polish.cover import mark_as_cover, mark_as_titlepage
from calibre.ebooks.oeb.polish.pretty import fix_all_html, pretty_all from calibre.ebooks.oeb.polish.pretty import fix_all_html, pretty_all
from calibre.ebooks.oeb.polish.replace import rename_files, replace_file from calibre.ebooks.oeb.polish.replace import rename_files, replace_file
from calibre.ebooks.oeb.polish.split import split, merge, AbortError from calibre.ebooks.oeb.polish.split import split, merge, AbortError
from calibre.ebooks.oeb.polish.toc import remove_names_from_toc
from calibre.gui2 import error_dialog, choose_files, question_dialog, info_dialog, choose_save_file from calibre.gui2 import error_dialog, choose_files, question_dialog, info_dialog, choose_save_file
from calibre.gui2.dialogs.confirm_delete import confirm from calibre.gui2.dialogs.confirm_delete import confirm
from calibre.gui2.tweak_book import set_current_container, current_container, tprefs, actions, editors from calibre.gui2.tweak_book import set_current_container, current_container, tprefs, actions, editors
@ -257,11 +258,14 @@ class Boss(QObject):
c.remove_item(name) c.remove_item(name)
self.set_modified() self.set_modified()
self.gui.file_list.delete_done(spine_items, other_items) self.gui.file_list.delete_done(spine_items, other_items)
for name in [x for x, remove in spine_items if remove] + list(other_items): spine_names = [x for x, remove in spine_items if remove]
for name in spine_names + list(other_items):
if name in editors: if name in editors:
self.close_editor(name) self.close_editor(name)
if not editors: if not editors:
self.gui.preview.clear() self.gui.preview.clear()
if remove_names_from_toc(current_container(), spine_names + list(other_items)):
self.gui.toc_view.update_if_visible()
def commit_dirty_opf(self): def commit_dirty_opf(self):
c = current_container() c = current_container()