From c344aaca7e3543980e08c766528e233935bcec20 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 4 Dec 2013 16:40:23 +0530 Subject: [PATCH] ebook-tweak: Add an option to open a specific file on startup --- src/calibre/gui2/tweak_book/boss.py | 8 +++++++- src/calibre/gui2/tweak_book/file_list.py | 20 +++++++++++++++++++- src/calibre/gui2/tweak_book/main.py | 4 +++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/calibre/gui2/tweak_book/boss.py b/src/calibre/gui2/tweak_book/boss.py index 9542ceb7ae..2d3fae1b6a 100644 --- a/src/calibre/gui2/tweak_book/boss.py +++ b/src/calibre/gui2/tweak_book/boss.py @@ -117,7 +117,7 @@ class Boss(QObject): self.container_count += 1 return tempfile.mkdtemp(prefix='%s%05d-' % (prefix, self.container_count), dir=self.tdir) - def open_book(self, path=None): + def open_book(self, path=None, edit_file=None): if self.gui.action_save.isEnabled(): if not question_dialog(self.gui, _('Unsaved changes'), _( 'The current book has unsaved changes. If you open a new book, they will be lost' @@ -152,9 +152,13 @@ class Boss(QObject): if self.tdir: shutil.rmtree(self.tdir, ignore_errors=True) self.tdir = PersistentTemporaryDirectory() + self._edit_file_on_open = edit_file self.gui.blocking_job('open_book', _('Opening book, please wait...'), self.book_opened, get_container, path, tdir=self.mkdtemp()) def book_opened(self, job): + ef = getattr(self, '_edit_file_on_open', None) + self._edit_file_on_open = None + if job.traceback is not None: if 'DRMError:' in job.traceback: from calibre.gui2.dialogs.drm_error import DRMErrorMessage @@ -178,6 +182,8 @@ class Boss(QObject): recent_books.insert(0, path) tprefs['recent-books'] = recent_books[:10] self.gui.update_recent_books() + if ef: + self.gui.file_list.request_edit(ef) def update_editors_from_container(self, container=None): c = container or current_container() diff --git a/src/calibre/gui2/tweak_book/file_list.py b/src/calibre/gui2/tweak_book/file_list.py index 31729e8f18..730134f684 100644 --- a/src/calibre/gui2/tweak_book/file_list.py +++ b/src/calibre/gui2/tweak_book/file_list.py @@ -155,6 +155,13 @@ class FileList(QTreeWidget): if name in state['selected']: c.setSelected(True) + def item_from_name(self, name): + for parent in self.categories.itervalues(): + for c in (parent.child(i) for i in xrange(parent.childCount())): + q = unicode(c.data(0, NAME_ROLE).toString()) + if q == name: + return c + def select_name(self, name): for parent in self.categories.itervalues(): for c in (parent.child(i) for i in xrange(parent.childCount())): @@ -446,12 +453,23 @@ class FileList(QTreeWidget): self.reorder_spine.emit(order) def item_double_clicked(self, item, column): + self._request_edit(item) + + def _request_edit(self, item): category = unicode(item.data(0, CATEGORY_ROLE).toString()) mime = unicode(item.data(0, MIME_ROLE).toString()) name = unicode(item.data(0, NAME_ROLE).toString()) syntax = {'text':'html', 'styles':'css'}.get(category, None) self.edit_file.emit(name, syntax, mime) + def request_edit(self, name): + item = self.item_from_name(name) + if item is not None: + self._request_edit(item) + else: + error_dialog(self, _('Cannot edit'), + _('No item with the name: %s was found') % name, show=True) + @property def all_files(self): return (category.child(i) for category in self.categories.itervalues() for i in xrange(category.childCount())) @@ -641,7 +659,7 @@ class FileListWidget(QWidget): 'edit_file', 'merge_requested', 'mark_requested', 'export_requested', 'replace_requested'): getattr(self.file_list, x).connect(getattr(self, x)) - for x in ('delete_done', 'select_name'): + for x in ('delete_done', 'select_name', 'request_edit'): setattr(self, x, getattr(self.file_list, x)) def build(self, container, preserve_state=True): diff --git a/src/calibre/gui2/tweak_book/main.py b/src/calibre/gui2/tweak_book/main.py index 2a50e99fe1..abbdac43c0 100644 --- a/src/calibre/gui2/tweak_book/main.py +++ b/src/calibre/gui2/tweak_book/main.py @@ -22,6 +22,8 @@ def option_parser(): Launch the calibre tweak book tool. ''') setup_gui_option_parser(parser) + parser.add_option('--edit-file', help=_( + 'Edit the named file inside the book')) return parser @@ -50,7 +52,7 @@ def main(args=sys.argv): sys.excepthook = main.unhandled_exception main.show() if len(args) > 1: - main.boss.open_book(args[1]) + main.boss.open_book(args[1], edit_file=opts.edit_file) app.exec_() if __name__ == '__main__':