mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
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.
This commit is contained in:
parent
65e2fcce8e
commit
90b902ae90
@ -310,8 +310,8 @@ class Container(object): # {{{
|
|||||||
for elem in self.parsed(name).xpath('//*[@src]'):
|
for elem in self.parsed(name).xpath('//*[@src]'):
|
||||||
yield (elem.get('src'), elem.sourceline, 0) if get_line_numbers else elem.get('src')
|
yield (elem.get('src'), elem.sourceline, 0) if get_line_numbers else elem.get('src')
|
||||||
|
|
||||||
def abspath_to_name(self, fullpath):
|
def abspath_to_name(self, fullpath, root=None):
|
||||||
return self.relpath(os.path.abspath(fullpath)).replace(os.sep, '/')
|
return self.relpath(os.path.abspath(fullpath), base=root).replace(os.sep, '/')
|
||||||
|
|
||||||
def name_to_abspath(self, name):
|
def name_to_abspath(self, name):
|
||||||
return os.path.abspath(join(self.root, *name.split('/')))
|
return os.path.abspath(join(self.root, *name.split('/')))
|
||||||
|
@ -238,6 +238,7 @@ class NetworkAccessManager(QNetworkAccessManager):
|
|||||||
|
|
||||||
def __init__(self, *args):
|
def __init__(self, *args):
|
||||||
QNetworkAccessManager.__init__(self, *args)
|
QNetworkAccessManager.__init__(self, *args)
|
||||||
|
self.current_root = None
|
||||||
self.cache = QNetworkDiskCache(self)
|
self.cache = QNetworkDiskCache(self)
|
||||||
self.setCache(self.cache)
|
self.setCache(self.cache)
|
||||||
self.cache.setCacheDirectory(PersistentTemporaryDirectory(prefix='disk_cache_'))
|
self.cache.setCacheDirectory(PersistentTemporaryDirectory(prefix='disk_cache_'))
|
||||||
@ -251,7 +252,7 @@ class NetworkAccessManager(QNetworkAccessManager):
|
|||||||
path = path[1:]
|
path = path[1:]
|
||||||
c = current_container()
|
c = current_container()
|
||||||
try:
|
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
|
except ValueError: # Happens on windows with absolute paths on different drives
|
||||||
name = None
|
name = None
|
||||||
if c.has_name(name):
|
if c.has_name(name):
|
||||||
@ -306,6 +307,14 @@ class WebPage(QWebPage):
|
|||||||
self.mainFrame().javaScriptWindowObjectCleared.connect(self.init_javascript)
|
self.mainFrame().javaScriptWindowObjectCleared.connect(self.init_javascript)
|
||||||
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):
|
def javaScriptConsoleMessage(self, msg, lineno, source_id):
|
||||||
prints('preview js:%s:%s:'%(unicode(source_id), lineno), unicode(msg))
|
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.
|
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):
|
def inspect(self):
|
||||||
self.inspector.parent().show()
|
self.inspector.parent().show()
|
||||||
@ -565,6 +579,10 @@ class Preview(QWidget):
|
|||||||
self.view.clear()
|
self.view.clear()
|
||||||
self.current_name = None
|
self.current_name = None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def current_root(self):
|
||||||
|
return self.view.page().current_root
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_visible(self):
|
def is_visible(self):
|
||||||
return actions['preview-dock'].isChecked()
|
return actions['preview-dock'].isChecked()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user