From 90b902ae90d288e2789e6067082fa4921cf599e8 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 18 May 2014 17:30:55 +0530 Subject: [PATCH] Preview panel: Improve robustness of resource loading from the book Ensure that resources are always loaded from the current container, even if the current container was changed since the base URL was specified. While this is unlikely to happen, it is best to be robust. --- src/calibre/ebooks/oeb/polish/container.py | 4 ++-- src/calibre/gui2/tweak_book/preview.py | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/calibre/ebooks/oeb/polish/container.py b/src/calibre/ebooks/oeb/polish/container.py index b8fd8e2c18..61a645badf 100644 --- a/src/calibre/ebooks/oeb/polish/container.py +++ b/src/calibre/ebooks/oeb/polish/container.py @@ -310,8 +310,8 @@ class Container(object): # {{{ for elem in self.parsed(name).xpath('//*[@src]'): yield (elem.get('src'), elem.sourceline, 0) if get_line_numbers else elem.get('src') - def abspath_to_name(self, fullpath): - return self.relpath(os.path.abspath(fullpath)).replace(os.sep, '/') + def abspath_to_name(self, fullpath, root=None): + return self.relpath(os.path.abspath(fullpath), base=root).replace(os.sep, '/') def name_to_abspath(self, name): return os.path.abspath(join(self.root, *name.split('/'))) diff --git a/src/calibre/gui2/tweak_book/preview.py b/src/calibre/gui2/tweak_book/preview.py index 5e0c7fb11f..a6df907c63 100644 --- a/src/calibre/gui2/tweak_book/preview.py +++ b/src/calibre/gui2/tweak_book/preview.py @@ -238,6 +238,7 @@ class NetworkAccessManager(QNetworkAccessManager): def __init__(self, *args): QNetworkAccessManager.__init__(self, *args) + self.current_root = None self.cache = QNetworkDiskCache(self) self.setCache(self.cache) self.cache.setCacheDirectory(PersistentTemporaryDirectory(prefix='disk_cache_')) @@ -251,7 +252,7 @@ class NetworkAccessManager(QNetworkAccessManager): path = path[1:] c = current_container() try: - name = c.abspath_to_name(path) + name = c.abspath_to_name(path, root=self.current_root) except ValueError: # Happens on windows with absolute paths on different drives name = None if c.has_name(name): @@ -306,6 +307,14 @@ class WebPage(QWebPage): self.mainFrame().javaScriptWindowObjectCleared.connect(self.init_javascript) self.init_javascript() + @dynamic_property + def current_root(self): + def fget(self): + return self.networkAccessManager().current_root + def fset(self, val): + self.networkAccessManager().current_root = val + return property(fget=fget, fset=fset) + def javaScriptConsoleMessage(self, msg, lineno, source_id): prints('preview js:%s:%s:'%(unicode(source_id), lineno), unicode(msg)) @@ -408,6 +417,11 @@ class WebView(QWebView): page margins and embedded fonts that use font name aliasing. ''')) + self.page().current_root = None + + def setUrl(self, qurl): + self.page().current_root = current_container().root + return QWebView.setUrl(self, qurl) def inspect(self): self.inspector.parent().show() @@ -565,6 +579,10 @@ class Preview(QWidget): self.view.clear() self.current_name = None + @property + def current_root(self): + return self.view.page().current_root + @property def is_visible(self): return actions['preview-dock'].isChecked()