From 03eea7f62a751ca3db01ad9a36493e921d894c48 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 9 Dec 2016 07:00:25 +0100 Subject: [PATCH] Remove download filename suffixes with QtWebEngine --- .../browser/webengine/webenginedownloads.py | 18 +++++++++- .../webengine/test_webenginedownloads.py | 34 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 tests/unit/browser/webengine/test_webenginedownloads.py diff --git a/qutebrowser/browser/webengine/webenginedownloads.py b/qutebrowser/browser/webengine/webenginedownloads.py index 5286a6298..8ce0cdc2e 100644 --- a/qutebrowser/browser/webengine/webenginedownloads.py +++ b/qutebrowser/browser/webengine/webenginedownloads.py @@ -19,6 +19,7 @@ """QtWebEngine specific code for downloads.""" +import re import os.path import functools @@ -122,6 +123,21 @@ class DownloadItem(downloads.AbstractDownloadItem): self._qt_item.accept() +def _get_suggested_filename(path): + """Convert a path we got from chromium to a suggested filename. + + Chromium thinks we want to download stuff to ~/Download, so even if we + don't, we get downloads with a suffix like (1) for files existing there. + + We simply strip the suffix off via regex. + + See https://bugreports.qt.io/browse/QTBUG-56978 + """ + filename = os.path.basename(path) + filename = re.sub(r'\([0-9]+\)$', '', filename) + return filename + + class DownloadManager(downloads.AbstractDownloadManager): """Manager for currently running downloads.""" @@ -134,7 +150,7 @@ class DownloadManager(downloads.AbstractDownloadManager): @pyqtSlot(QWebEngineDownloadItem) def handle_download(self, qt_item): """Start a download coming from a QWebEngineProfile.""" - suggested_filename = os.path.basename(qt_item.path()) + suggested_filename = _get_suggested_filename(qt_item.path()) download = DownloadItem(qt_item) self._init_item(download, auto_remove=False, diff --git a/tests/unit/browser/webengine/test_webenginedownloads.py b/tests/unit/browser/webengine/test_webenginedownloads.py new file mode 100644 index 000000000..3cead0e97 --- /dev/null +++ b/tests/unit/browser/webengine/test_webenginedownloads.py @@ -0,0 +1,34 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2016 Florian Bruhin (The Compiler) +# +# This file is part of qutebrowser. +# +# qutebrowser is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# qutebrowser is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with qutebrowser. If not, see . + +import os.path + +import pytest + +from qutebrowser.browser.webengine import webenginedownloads + + +@pytest.mark.parametrize('path, expected', [ + (os.path.join('subfolder', 'foo'), 'foo'), + ('foo(1)', 'foo'), + ('foo(a)', 'foo(a)'), + ('foo1', 'foo1'), +]) +def test_get_suggested_filename(path, expected): + assert webenginedownloads._get_suggested_filename(path) == expected