download-open: add cmdline parameter

This commit is contained in:
Daniel Schadt 2016-08-02 19:57:45 +02:00
parent fa6c552d7b
commit a85227fa25

View File

@ -530,8 +530,13 @@ class DownloadItem(QObject):
self.cancel() self.cancel()
@pyqtSlot() @pyqtSlot()
def open_file(self): def open_file(self, cmdline=None):
"""Open the downloaded file.""" """Open the downloaded file.
Args:
cmdline: The command to use, or None for the system's default
program.
"""
assert self.successful assert self.successful
filename = self._filename filename = self._filename
if filename is None: if filename is None:
@ -539,8 +544,20 @@ class DownloadItem(QObject):
if filename is None: if filename is None:
log.downloads.error("No filename to open the download!") log.downloads.error("No filename to open the download!")
return return
url = QUrl.fromLocalFile(filename)
QDesktopServices.openUrl(url) if cmdline is None:
log.downloads.debug("Opening {} with the system application"
.format(filename))
url = QUrl.fromLocalFile(filename)
QDesktopServices.openUrl(url)
return
cmd, *args = shlex.split(cmdline)
args = [filename if arg == '{}' else arg for arg in args]
log.downloads.debug("Opening {} with {}"
.format(filename, [cmd] + args))
proc = guiprocess.GUIProcess(self._win_id, what='download')
proc.start_detached(cmd, args)
def set_filename(self, filename): def set_filename(self, filename):
"""Set the filename to save the download to. """Set the filename to save the download to.
@ -975,20 +992,8 @@ class DownloadManager(QAbstractListModel):
if not download.successful: if not download.successful:
log.downloads.debug("{} finished but not successful, not opening!" log.downloads.debug("{} finished but not successful, not opening!"
.format(download)) .format(download))
if cmdline is None:
download.open_file()
return return
download.open_file(cmdline)
cmd, *args = shlex.split(cmdline)
filename = download._filename
if filename is None:
filename = getattr(download.fileobj, 'name', None)
if filename is None:
raise ValueError("Download has no filename to open")
args = [filename if arg == '{}' else arg for arg in args]
proc = guiprocess.GUIProcess(self._win_id, what='process')
proc.start_detached(cmd, args)
def raise_no_download(self, count): def raise_no_download(self, count):
@ -1048,12 +1053,14 @@ class DownloadManager(QAbstractListModel):
self.remove_item(download) self.remove_item(download)
log.downloads.debug("deleted download {}".format(download)) log.downloads.debug("deleted download {}".format(download))
@cmdutils.register(instance='download-manager', scope='window') @cmdutils.register(instance='download-manager', scope='window', maxsplit=0)
@cmdutils.argument('count', count=True) @cmdutils.argument('count', count=True)
def download_open(self, count=0): def download_open(self, cmdline: str=None, count=0):
"""Open the last/[count]th download. """Open the last/[count]th download.
Args: Args:
cmdline: The command which should be used to open the file. A {} as
argument will be replaced by the download's filename.
count: The index of the download to open. count: The index of the download to open.
""" """
try: try:
@ -1064,7 +1071,7 @@ class DownloadManager(QAbstractListModel):
if not count: if not count:
count = len(self.downloads) count = len(self.downloads)
raise cmdexc.CommandError("Download {} is not done!".format(count)) raise cmdexc.CommandError("Download {} is not done!".format(count))
download.open_file() download.open_file(cmdline)
@cmdutils.register(instance='download-manager', scope='window') @cmdutils.register(instance='download-manager', scope='window')
@cmdutils.argument('count', count=True) @cmdutils.argument('count', count=True)