mirror of
https://github.com/andwati/kget-capture.git
synced 2026-11-09 13:35:23 +00:00
Get correct filenames, skip KGet's dialog, and add a finished notification
Switch auto-capture from onCreated to onDeterminingFilename, which fires once Chrome has already resolved the real filename (from Content-Disposition or the URL). That name is now passed through to the native host and used to build a full dir+filename destination for addTransfer, replacing showNewTransferDialog. A non-empty destDir makes KGet skip its interactive dialog and start immediately, which also fixes it showing an opaque URL path segment as the filename for signed/obfuscated URLs (e.g. cloud storage links) instead of the real name. Also enable KGet's existing (but disabled-by-default) "Download Finished" KNotify popup via install.sh, so there's still feedback now that captures no longer show an interactive dialog.
This commit is contained in:
@@ -2,11 +2,28 @@
|
||||
"""Native messaging host bridging Chrome to KGet's D-Bus service."""
|
||||
|
||||
import json
|
||||
import os
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
|
||||
def default_download_dir():
|
||||
home = os.path.expanduser("~")
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["xdg-user-dir", "DOWNLOAD"], capture_output=True, text=True, timeout=5
|
||||
)
|
||||
downloads = result.stdout.strip()
|
||||
except (FileNotFoundError, subprocess.TimeoutExpired):
|
||||
downloads = ""
|
||||
# xdg-user-dir falls back to bare $HOME (not $HOME/Downloads) when
|
||||
# there's no ~/.config/user-dirs.dirs at all -- treat that as not found.
|
||||
if not downloads or downloads.rstrip("/") == home.rstrip("/"):
|
||||
return os.path.join(home, "Downloads")
|
||||
return downloads
|
||||
|
||||
|
||||
def read_message():
|
||||
raw_length = sys.stdin.buffer.read(4)
|
||||
if len(raw_length) < 4:
|
||||
@@ -23,20 +40,24 @@ def write_message(message):
|
||||
sys.stdout.buffer.flush()
|
||||
|
||||
|
||||
def add_transfer(url):
|
||||
# showNewTransferDialog opens KGet's "New Download" dialog (editable name
|
||||
# and destination) and returns immediately without waiting for the user
|
||||
# to interact with it. addTransfer, by contrast, only returns once the
|
||||
# dialog is dismissed when destDir is empty -- it blocks the D-Bus call
|
||||
# on human input, which is why we don't use it here.
|
||||
def add_transfer(url, filename):
|
||||
# A non-empty destDir makes KGet add+start the transfer immediately with
|
||||
# no interactive dialog. Passing dir+filename (when we know the real
|
||||
# filename, e.g. from Chrome's own Content-Disposition resolution) also
|
||||
# sidesteps KGet's own filename guess, which is derived purely from the
|
||||
# URL path and gets it wrong for opaque/signed URLs.
|
||||
dest_dir = default_download_dir()
|
||||
dest = os.path.join(dest_dir, filename) if filename else dest_dir
|
||||
cmd = [
|
||||
"dbus-send",
|
||||
"--print-reply",
|
||||
"--reply-timeout=10000",
|
||||
"--dest=org.kde.kget",
|
||||
"/KGet",
|
||||
"org.kde.kget.main.showNewTransferDialog",
|
||||
f"array:string:{url}",
|
||||
"org.kde.kget.main.addTransfer",
|
||||
f"string:{url}",
|
||||
f"string:{dest}",
|
||||
"boolean:true",
|
||||
]
|
||||
try:
|
||||
result = subprocess.run(cmd, capture_output=True, text=True, timeout=15)
|
||||
@@ -57,7 +78,7 @@ def main():
|
||||
write_message({"ok": False, "error": "invalid request"})
|
||||
return
|
||||
|
||||
ok, error = add_transfer(message["url"])
|
||||
ok, error = add_transfer(message["url"], message.get("filename"))
|
||||
write_message({"ok": True} if ok else {"ok": False, "error": error})
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user