Cleanup passing of --open-at data to show_book

This commit is contained in:
Kovid Goyal 2019-12-09 10:31:12 +05:30
parent f495ffc16c
commit 6a540b8b98
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 22 additions and 18 deletions

View File

@ -403,19 +403,22 @@ class EbookViewer(MainWindow):
self.load_book_data() self.load_book_data()
self.update_window_title() self.update_window_title()
initial_cfi = self.initial_cfi_for_current_book() initial_cfi = self.initial_cfi_for_current_book()
initial_toc_node = initial_bookpos = None initial_position = {'type': 'cfi', 'data': initial_cfi} if initial_cfi else None
if open_at: if open_at:
if open_at.startswith('toc:'): if open_at.startswith('toc:'):
initial_toc_node = self.toc_model.node_id_for_text(open_at[len('toc:'):]) initial_toc_node = self.toc_model.node_id_for_text(open_at[len('toc:'):])
initial_position = {'type': 'toc', 'data': initial_toc_node}
elif open_at.startswith('toc-href:'): elif open_at.startswith('toc-href:'):
initial_toc_node = self.toc_model.node_id_for_href(open_at[len('toc-href:'):], exact=True) initial_toc_node = self.toc_model.node_id_for_href(open_at[len('toc-href:'):], exact=True)
initial_position = {'type': 'toc', 'data': initial_toc_node}
elif open_at.startswith('toc-href-contains:'): elif open_at.startswith('toc-href-contains:'):
initial_toc_node = self.toc_model.node_id_for_href(open_at[len('toc-href-contains:'):], exact=False) initial_toc_node = self.toc_model.node_id_for_href(open_at[len('toc-href-contains:'):], exact=False)
initial_position = {'type': 'toc', 'data': initial_toc_node}
elif open_at.startswith('epubcfi(/'): elif open_at.startswith('epubcfi(/'):
initial_cfi = open_at initial_position = {'type': 'cfi', 'data': open_at}
elif is_float(open_at): elif is_float(open_at):
initial_bookpos = float(open_at) initial_position = {'type': 'bookpos', 'data': float(open_at)}
self.web_view.start_book_load(initial_cfi=initial_cfi, initial_toc_node=initial_toc_node, initial_bookpos=initial_bookpos) self.web_view.start_book_load(initial_position=initial_position)
def load_book_data(self): def load_book_data(self):
self.load_book_annotations() self.load_book_annotations()

View File

@ -544,9 +544,9 @@ class WebView(RestartingWebEngineView):
for func, args in iteritems(self.pending_bridge_ready_actions): for func, args in iteritems(self.pending_bridge_ready_actions):
getattr(self.bridge, func)(*args) getattr(self.bridge, func)(*args)
def start_book_load(self, initial_cfi=None, initial_toc_node=None, initial_bookpos=None): def start_book_load(self, initial_position=None):
key = (set_book_path.path,) key = (set_book_path.path,)
self.execute_when_ready('start_book_load', key, initial_cfi, initial_toc_node, initial_bookpos, set_book_path.pathtoebook) self.execute_when_ready('start_book_load', key, initial_position, set_book_path.pathtoebook)
def execute_when_ready(self, action, *args): def execute_when_ready(self, action, *args):
if self.bridge.ready: if self.bridge.ready:

View File

@ -616,7 +616,7 @@ class View:
cfi = '/' + rest cfi = '/' + rest
return name, cfi return name, cfi
def display_book(self, book, initial_cfi, initial_toc_node, initial_bookpos): def display_book(self, book, initial_position):
self.hide_overlays() self.hide_overlays()
self.iframe.focus() self.iframe.focus()
is_current_book = self.book and self.book.key == book.key is_current_book = self.book and self.book.key == book.key
@ -636,8 +636,8 @@ class View:
unkey = username_key(get_interface_data().username) unkey = username_key(get_interface_data().username)
name = book.manifest.spine[0] name = book.manifest.spine[0]
cfi = None cfi = None
if initial_cfi and initial_cfi.startswith('epubcfi(/'): if initial_position and initial_position.type is 'cfi' and initial_position.data.startswith('epubcfi(/'):
cfi = initial_cfi cfi = initial_position.data
else: else:
q = parse_url_params() q = parse_url_params()
if q.bookpos and q.bookpos.startswith('epubcfi(/'): if q.bookpos and q.bookpos.startswith('epubcfi(/'):
@ -649,11 +649,12 @@ class View:
name = cfiname name = cfiname
pos.type, pos.cfi = 'cfi', internal_cfi pos.type, pos.cfi = 'cfi', internal_cfi
navigated = False navigated = False
if jstype(initial_toc_node) is 'number': if initial_position:
navigated = self.goto_toc_node(initial_toc_node) if initial_position.type is 'toc':
if not navigated and jstype(initial_bookpos) is 'number': navigated = self.goto_toc_node(initial_position.data)
navigated = True elif initial_position.type is 'bookpos':
self.goto_book_position(initial_bookpos) navigated = True
self.goto_book_position(initial_position.data)
if navigated: if navigated:
self.hide_loading() self.hide_loading()
else: else:

View File

@ -138,7 +138,7 @@ def show_error(title, msg, details):
to_python.show_error(title, msg, details) to_python.show_error(title, msg, details)
def manifest_received(key, initial_cfi, initial_toc_node, initial_bookpos, pathtoebook, end_type, xhr, ev): def manifest_received(key, initial_position, pathtoebook, end_type, xhr, ev):
nonlocal book nonlocal book
end_type = workaround_qt_bug(xhr, end_type) end_type = workaround_qt_bug(xhr, end_type)
if end_type is 'load': if end_type is 'load':
@ -151,7 +151,7 @@ def manifest_received(key, initial_cfi, initial_toc_node, initial_bookpos, patht
book.is_complete = True book.is_complete = True
v'delete book.manifest["metadata"]' v'delete book.manifest["metadata"]'
v'delete book.manifest["last_read_positions"]' v'delete book.manifest["last_read_positions"]'
view.display_book(book, initial_cfi, initial_toc_node, initial_bookpos) view.display_book(book, initial_position)
else: else:
show_error(_('Could not open book'), _( show_error(_('Could not open book'), _(
'Failed to load book manifest, click "Show details" for more info'), 'Failed to load book manifest, click "Show details" for more info'),
@ -241,8 +241,8 @@ def show_home_page():
@from_python @from_python
def start_book_load(key, initial_cfi, initial_toc_node, initial_bookpos, pathtoebook): def start_book_load(key, initial_position, pathtoebook):
xhr = ajax('manifest', manifest_received.bind(None, key, initial_cfi, initial_toc_node, initial_bookpos, pathtoebook), ok_code=0) xhr = ajax('manifest', manifest_received.bind(None, key, initial_position, pathtoebook), ok_code=0)
xhr.responseType = 'json' xhr.responseType = 'json'
xhr.send() xhr.send()