Skip to content

Commit

Permalink
DRYer
Browse files Browse the repository at this point in the history
  • Loading branch information
kovidgoyal committed Nov 27, 2015
1 parent 46d586b commit 4d554f8
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions src/calibre/gui2/tweak_book/boss.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,11 +418,8 @@ def reorder_spine(self, items):
completion_worker().clear_caches('names')

def add_file(self):
if current_container() is None:
return error_dialog(self.gui, _('No open book'), _(
'You must first open a book to tweak, before trying to create new files'
' in it.'), show=True)

if not self.ensure_book(_('You must first open a book to tweak, before trying to create new files in it.')):
return
self.commit_dirty_opf()
d = NewFileDialog(self.gui)
if d.exec_() != d.Accepted:
Expand Down Expand Up @@ -453,10 +450,8 @@ def do_add_file(self, file_name, data, using_template=False, edit_file=False):
completion_worker().clear_caches('names')

def add_files(self):
if current_container() is None:
return error_dialog(self.gui, _('No open book'), _(
'You must first open a book to tweak, before trying to create new files'
' in it.'), show=True)
if not self.ensure_book(_('You must first open a book to tweak, before trying to create new files in it.')):
return

files = choose_files(self.gui, 'tweak-book-bulk-import-files', _('Choose files'))
if files:
Expand Down Expand Up @@ -498,10 +493,15 @@ def add_cover(self):
finally:
d.import_requested.disconnect()

def edit_toc(self):
def ensure_book(self, msg):
if current_container() is None:
return error_dialog(self.gui, _('No book opened'), _(
'You must open a book before trying to edit the Table of Contents.'), show=True)
error_dialog(self.gui, _('No book open'), msg, show=True)
return False
return True

def edit_toc(self):
if not self.ensure_book(_('You must open a book before trying to edit the Table of Contents.')):
return
self.add_savepoint(_('Before: Edit Table of Contents'))
d = TOCEditor(title=self.current_metadata.title, parent=self.gui)
if d.exec_() != d.Accepted:
Expand Down Expand Up @@ -1193,9 +1193,8 @@ def browse_images(self):
self.gui.image_browser.raise_()

def show_reports(self):
if current_container() is None:
return error_dialog(self.gui, _('No book open'), _(
'You must first open a book in order to see the report.'), show=True)
if not self.ensure_book(_('You must first open a book in order to see the report.')):
return
self.gui.reports.refresh()
self.gui.reports.show()
self.gui.reports.raise_()
Expand All @@ -1209,10 +1208,8 @@ def image_activated(self, name):
self.edit_file_requested(name, None, mt)

def check_external_links(self):
if current_container() is None:
return error_dialog(self.gui, _('No book open'), _(
'You must first open a book in order to check links.'), show=True)
self.gui.check_external_links.show()
if self.ensure_book(_('You must first open a book in order to check links.')):
self.gui.check_external_links.show()

def sync_editor_to_preview(self, name, sourceline_address):
editor = self.edit_file(name, 'html')
Expand Down Expand Up @@ -1304,10 +1301,9 @@ def edit_file_requested(self, name, syntax=None, mime=None):
return self.edit_file(name, syntax)

def quick_open(self):
if not self.ensure_book(_('No book is currently open. You must first open a book to edit.')):
return
c = current_container()
if c is None:
return error_dialog(self.gui, _('No open book'), _(
'No book is currently open. You must first open a book to edit.'), show=True)
files = [name for name, mime in c.mime_map.iteritems() if c.exists(name) and syntax_from_mime(name, mime) is not None]
d = QuickOpen(files, parent=self.gui)
if d.exec_() == d.Accepted and d.selected_result is not None:
Expand Down

0 comments on commit 4d554f8

Please sign in to comment.