ebook-tweak: Add an option to open a specific file on startup

This commit is contained in:
Kovid Goyal 2013-12-04 16:40:23 +05:30
parent fcb09e8628
commit c344aaca7e
3 changed files with 29 additions and 3 deletions

View File

@ -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()

View File

@ -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):

View File

@ -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__':