mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
ebook-tweak: Add an option to open a specific file on startup
This commit is contained in:
parent
fcb09e8628
commit
c344aaca7e
@ -117,7 +117,7 @@ class Boss(QObject):
|
|||||||
self.container_count += 1
|
self.container_count += 1
|
||||||
return tempfile.mkdtemp(prefix='%s%05d-' % (prefix, self.container_count), dir=self.tdir)
|
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 self.gui.action_save.isEnabled():
|
||||||
if not question_dialog(self.gui, _('Unsaved changes'), _(
|
if not question_dialog(self.gui, _('Unsaved changes'), _(
|
||||||
'The current book has unsaved changes. If you open a new book, they will be lost'
|
'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:
|
if self.tdir:
|
||||||
shutil.rmtree(self.tdir, ignore_errors=True)
|
shutil.rmtree(self.tdir, ignore_errors=True)
|
||||||
self.tdir = PersistentTemporaryDirectory()
|
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())
|
self.gui.blocking_job('open_book', _('Opening book, please wait...'), self.book_opened, get_container, path, tdir=self.mkdtemp())
|
||||||
|
|
||||||
def book_opened(self, job):
|
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 job.traceback is not None:
|
||||||
if 'DRMError:' in job.traceback:
|
if 'DRMError:' in job.traceback:
|
||||||
from calibre.gui2.dialogs.drm_error import DRMErrorMessage
|
from calibre.gui2.dialogs.drm_error import DRMErrorMessage
|
||||||
@ -178,6 +182,8 @@ class Boss(QObject):
|
|||||||
recent_books.insert(0, path)
|
recent_books.insert(0, path)
|
||||||
tprefs['recent-books'] = recent_books[:10]
|
tprefs['recent-books'] = recent_books[:10]
|
||||||
self.gui.update_recent_books()
|
self.gui.update_recent_books()
|
||||||
|
if ef:
|
||||||
|
self.gui.file_list.request_edit(ef)
|
||||||
|
|
||||||
def update_editors_from_container(self, container=None):
|
def update_editors_from_container(self, container=None):
|
||||||
c = container or current_container()
|
c = container or current_container()
|
||||||
|
@ -155,6 +155,13 @@ class FileList(QTreeWidget):
|
|||||||
if name in state['selected']:
|
if name in state['selected']:
|
||||||
c.setSelected(True)
|
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):
|
def select_name(self, name):
|
||||||
for parent in self.categories.itervalues():
|
for parent in self.categories.itervalues():
|
||||||
for c in (parent.child(i) for i in xrange(parent.childCount())):
|
for c in (parent.child(i) for i in xrange(parent.childCount())):
|
||||||
@ -446,12 +453,23 @@ class FileList(QTreeWidget):
|
|||||||
self.reorder_spine.emit(order)
|
self.reorder_spine.emit(order)
|
||||||
|
|
||||||
def item_double_clicked(self, item, column):
|
def item_double_clicked(self, item, column):
|
||||||
|
self._request_edit(item)
|
||||||
|
|
||||||
|
def _request_edit(self, item):
|
||||||
category = unicode(item.data(0, CATEGORY_ROLE).toString())
|
category = unicode(item.data(0, CATEGORY_ROLE).toString())
|
||||||
mime = unicode(item.data(0, MIME_ROLE).toString())
|
mime = unicode(item.data(0, MIME_ROLE).toString())
|
||||||
name = unicode(item.data(0, NAME_ROLE).toString())
|
name = unicode(item.data(0, NAME_ROLE).toString())
|
||||||
syntax = {'text':'html', 'styles':'css'}.get(category, None)
|
syntax = {'text':'html', 'styles':'css'}.get(category, None)
|
||||||
self.edit_file.emit(name, syntax, mime)
|
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
|
@property
|
||||||
def all_files(self):
|
def all_files(self):
|
||||||
return (category.child(i) for category in self.categories.itervalues() for i in xrange(category.childCount()))
|
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',
|
'edit_file', 'merge_requested', 'mark_requested',
|
||||||
'export_requested', 'replace_requested'):
|
'export_requested', 'replace_requested'):
|
||||||
getattr(self.file_list, x).connect(getattr(self, x))
|
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))
|
setattr(self, x, getattr(self.file_list, x))
|
||||||
|
|
||||||
def build(self, container, preserve_state=True):
|
def build(self, container, preserve_state=True):
|
||||||
|
@ -22,6 +22,8 @@ def option_parser():
|
|||||||
Launch the calibre tweak book tool.
|
Launch the calibre tweak book tool.
|
||||||
''')
|
''')
|
||||||
setup_gui_option_parser(parser)
|
setup_gui_option_parser(parser)
|
||||||
|
parser.add_option('--edit-file', help=_(
|
||||||
|
'Edit the named file inside the book'))
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
|
||||||
@ -50,7 +52,7 @@ def main(args=sys.argv):
|
|||||||
sys.excepthook = main.unhandled_exception
|
sys.excepthook = main.unhandled_exception
|
||||||
main.show()
|
main.show()
|
||||||
if len(args) > 1:
|
if len(args) > 1:
|
||||||
main.boss.open_book(args[1])
|
main.boss.open_book(args[1], edit_file=opts.edit_file)
|
||||||
app.exec_()
|
app.exec_()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Loading…
x
Reference in New Issue
Block a user