mhtml: ask before overwriting dest

This commit is contained in:
Daniel 2015-10-02 13:50:19 +02:00
parent 420c087373
commit 6601df14a3
2 changed files with 28 additions and 4 deletions

View File

@ -1171,12 +1171,12 @@ class CommandDispatcher:
q.mode = usertypes.PromptMode.text q.mode = usertypes.PromptMode.text
q.completed.connect(q.deleteLater) q.completed.connect(q.deleteLater)
q.default = downloads.path_suggestion(suggested_fn) q.default = downloads.path_suggestion(suggested_fn)
q.answered.connect(mhtml.start_download) q.answered.connect(mhtml.start_download_checked)
message_bridge = objreg.get("message-bridge", scope="window", message_bridge = objreg.get("message-bridge", scope="window",
window=self._win_id) window=self._win_id)
message_bridge.ask(q, blocking=False) message_bridge.ask(q, blocking=False)
else: else:
mhtml.start_download(dest) mhtml.start_download_checked(dest)
@cmdutils.register(instance='command-dispatcher', scope='window', @cmdutils.register(instance='command-dispatcher', scope='window',
deprecated="Use :download instead.") deprecated="Use :download instead.")

View File

@ -33,7 +33,7 @@ import email.mime.multipart
from PyQt5.QtCore import QUrl from PyQt5.QtCore import QUrl
from qutebrowser.browser import webelem from qutebrowser.browser import webelem
from qutebrowser.utils import log, objreg, message from qutebrowser.utils import log, objreg, message, usertypes
try: try:
import cssutils import cssutils
@ -80,7 +80,10 @@ def _get_css_imports_cssutils(data, inline=False):
data: The content of the stylesheet to scan as string. data: The content of the stylesheet to scan as string.
inline: True if the argument is a inline HTML style attribute. inline: True if the argument is a inline HTML style attribute.
""" """
parser = cssutils.CSSParser(fetcher=lambda url: (None, ""), validate=False) # We don't care about invalid CSS data, this will only litter the log
# output with CSS errors
parser = cssutils.CSSParser(loglevel=100,
fetcher=lambda url: (None, ""), validate=False)
if not inline: if not inline:
sheet = parser.parseString(data) sheet = parser.parseString(data)
return list(cssutils.getUrls(sheet)) return list(cssutils.getUrls(sheet))
@ -420,6 +423,8 @@ class _NoCloseBytesIO(io.BytesIO): # pylint: disable=no-init
def start_download(dest): def start_download(dest):
"""Start downloading the current page and all assets to a MHTML file. """Start downloading the current page and all assets to a MHTML file.
This will overwrite dest if it already exists.
Args: Args:
dest: The filename where the resulting file should be saved. dest: The filename where the resulting file should be saved.
""" """
@ -427,3 +432,22 @@ def start_download(dest):
web_view = objreg.get('webview', scope='tab', tab='current') web_view = objreg.get('webview', scope='tab', tab='current')
loader = _Downloader(web_view, dest) loader = _Downloader(web_view, dest)
loader.run() loader.run()
def start_download_checked(dest):
"""First check if dest is already a file, then start the download.
Args:
dest: The filename where the resulting file should be saved.
"""
if not os.path.isfile(dest):
start_download(dest)
return
q = usertypes.Question()
q.mode = usertypes.PromptMode.yesno
q.text = "{} exists. Overwrite?".format(dest)
q.completed.connect(q.deleteLater)
q.answered_yes.connect(functools.partial(start_download, dest))
message_bridge = objreg.get('message-bridge', scope='window', window='current')
message_bridge.ask(q, blocking=False)