deduplicate download opening code
This commit is contained in:
parent
8c5ad7d46d
commit
4fdd3cd761
@ -20,7 +20,6 @@
|
||||
"""Shared QtWebKit/QtWebEngine code for downloads."""
|
||||
|
||||
import sys
|
||||
import shlex
|
||||
import html
|
||||
import os.path
|
||||
import collections
|
||||
@ -30,13 +29,11 @@ import tempfile
|
||||
import sip
|
||||
from PyQt5.QtCore import (pyqtSlot, pyqtSignal, Qt, QObject, QUrl, QModelIndex,
|
||||
QTimer, QAbstractListModel)
|
||||
from PyQt5.QtGui import QDesktopServices
|
||||
|
||||
from qutebrowser.commands import cmdexc, cmdutils
|
||||
from qutebrowser.config import config
|
||||
from qutebrowser.utils import (usertypes, standarddir, utils, message, log,
|
||||
qtutils)
|
||||
from qutebrowser.misc import guiprocess
|
||||
|
||||
|
||||
ModelRole = usertypes.enum('ModelRole', ['item'], start=Qt.UserRole,
|
||||
@ -532,31 +529,7 @@ class AbstractDownloadItem(QObject):
|
||||
if filename is None: # pragma: no cover
|
||||
log.downloads.error("No filename to open the download!")
|
||||
return
|
||||
|
||||
# the default program to open downloads with - will be empty string
|
||||
# if we want to use the default
|
||||
override = config.get('general', 'default-open-dispatcher')
|
||||
|
||||
# precedence order: cmdline > default-open-dispatcher > openUrl
|
||||
|
||||
if cmdline is None and not override:
|
||||
log.downloads.debug("Opening {} with the system application"
|
||||
.format(filename))
|
||||
url = QUrl.fromLocalFile(filename)
|
||||
QDesktopServices.openUrl(url)
|
||||
return
|
||||
|
||||
if cmdline is None and override:
|
||||
cmdline = override
|
||||
|
||||
cmd, *args = shlex.split(cmdline)
|
||||
args = [arg.replace('{}', filename) for arg in args]
|
||||
if '{}' not in cmdline:
|
||||
args.append(filename)
|
||||
log.downloads.debug("Opening {} with {}"
|
||||
.format(filename, [cmd] + args))
|
||||
proc = guiprocess.GUIProcess(what='download')
|
||||
proc.start_detached(cmd, args)
|
||||
utils.open_file(filename, cmdline)
|
||||
|
||||
def _ensure_can_set_filename(self, filename):
|
||||
"""Make sure we can still set a filename."""
|
||||
|
@ -35,12 +35,10 @@ import email.message
|
||||
import quopri
|
||||
|
||||
from PyQt5.QtCore import QUrl
|
||||
from PyQt5.QtGui import QDesktopServices
|
||||
|
||||
from qutebrowser.browser import downloads
|
||||
from qutebrowser.browser.webkit import webkitelem
|
||||
from qutebrowser.utils import log, objreg, message, usertypes, utils, urlutils
|
||||
from qutebrowser.config import config
|
||||
|
||||
_File = collections.namedtuple('_File',
|
||||
['content', 'content_type', 'content_location',
|
||||
@ -490,31 +488,7 @@ class _Downloader:
|
||||
message.info("Page saved as {}".format(self.target))
|
||||
|
||||
if isinstance(self.target, downloads.OpenFileDownloadTarget):
|
||||
filename = fobj.name
|
||||
# the default program to open downloads with - will be empty string
|
||||
# if we want to use the default
|
||||
override = config.get('general', 'default-open-dispatcher')
|
||||
cmdline = self.target.cmdline
|
||||
|
||||
# precedence order: cmdline > default-open-dispatcher > openUrl
|
||||
if cmdline is None and not override:
|
||||
log.downloads.debug("Opening {} with the system application"
|
||||
.format(filename))
|
||||
url = QUrl.fromLocalFile(filename)
|
||||
QDesktopServices.openUrl(url)
|
||||
return
|
||||
|
||||
if cmdline is None and override:
|
||||
cmdline = override
|
||||
|
||||
cmd, *args = shlex.split(cmdline)
|
||||
args = [arg.replace('{}', filename) for arg in args]
|
||||
if '{}' not in cmdline:
|
||||
args.append(filename)
|
||||
log.downloads.debug("Opening {} with {}"
|
||||
.format(filename, [cmd] + args))
|
||||
proc = guiprocess.GUIProcess(what='download')
|
||||
proc.start_detached(cmd, args)
|
||||
utils.open_file(fobj.name, self.target.cmdline)
|
||||
|
||||
def _collect_zombies(self):
|
||||
"""Collect done downloads and add their data to the MHTML file.
|
||||
|
@ -29,14 +29,17 @@ import functools
|
||||
import contextlib
|
||||
import itertools
|
||||
import socket
|
||||
import shlex
|
||||
|
||||
from PyQt5.QtCore import Qt
|
||||
from PyQt5.QtGui import QKeySequence, QColor, QClipboard
|
||||
from PyQt5.QtCore import Qt, QUrl
|
||||
from PyQt5.QtGui import QKeySequence, QColor, QClipboard, QDesktopServices
|
||||
from PyQt5.QtWidgets import QApplication
|
||||
import pkg_resources
|
||||
|
||||
import qutebrowser
|
||||
from qutebrowser.utils import qtutils, log
|
||||
from qutebrowser.misc import guiprocess
|
||||
from qutebrowser.config import config
|
||||
|
||||
|
||||
fake_clipboard = None
|
||||
@ -825,3 +828,43 @@ def random_port():
|
||||
port = sock.getsockname()[1]
|
||||
sock.close()
|
||||
return port
|
||||
|
||||
|
||||
def open_file(filename, cmdline=None):
|
||||
"""Open the given file.
|
||||
|
||||
If cmdline is not given, general->default-open-dispatcher is used.
|
||||
If default-open-dispatcher is unset, the system's default application is
|
||||
used.
|
||||
|
||||
Args:
|
||||
filename: The filename to open.
|
||||
cmdline: The command to use as string. A `{}` is expanded to the
|
||||
filename. None means to use the system's default application
|
||||
or `default-open-dispatcher` if set. If no `{}` is found, the
|
||||
filename is appended to the cmdline.
|
||||
"""
|
||||
# the default program to open downloads with - will be empty string
|
||||
# if we want to use the default
|
||||
override = config.get('general', 'default-open-dispatcher')
|
||||
|
||||
# precedence order: cmdline > default-open-dispatcher > openUrl
|
||||
|
||||
if cmdline is None and not override:
|
||||
log.misc.debug("Opening {} with the system application"
|
||||
.format(filename))
|
||||
url = QUrl.fromLocalFile(filename)
|
||||
QDesktopServices.openUrl(url)
|
||||
return
|
||||
|
||||
if cmdline is None and override:
|
||||
cmdline = override
|
||||
|
||||
cmd, *args = shlex.split(cmdline)
|
||||
args = [arg.replace('{}', filename) for arg in args]
|
||||
if '{}' not in cmdline:
|
||||
args.append(filename)
|
||||
log.misc.debug("Opening {} with {}"
|
||||
.format(filename, [cmd] + args))
|
||||
proc = guiprocess.GUIProcess(what='open-file')
|
||||
proc.start_detached(cmd, args)
|
||||
|
Loading…
Reference in New Issue
Block a user