Skip to content

The only downloader you need. 下载器的集大成者。

feat(clipboard): 剪贴板监听可按链接类型关闭

XiaoYouChR5f8afdc14f69
diff --git a/CONTEXT.md b/CONTEXT.md
index a3a23e0b..0402b50c 100644
--- a/CONTEXT.md
+++ b/CONTEXT.md
@@ -105,6 +105,10 @@ _Avoid_: URL 注册、协议注册
让系统把某类本地文件(.torrent、.m3u8 等)交给 Ghost Downloader 打开,进入任务创建流程。文件类型由 FeaturePack 声明;平台差异同 Scheme Association。
_Avoid_: 文件注册
+**Passive Capture**:
+用户没有主动添加任务时(目前只有剪贴板),把一条链接识别为可下载链接。由最终负责解析它的 FeaturePack 判断;用户可以按 FeaturePack 关掉。
+_Avoid_: 自动识别、协议过滤
+
**Wake Link**:
浏览器扩展拉起 Ghost Downloader 的 `ghostdownloader://` 链接。launch 打开界面;wake 只让 Browser Service、Aria2 RPC Service 和任务运行起来,不打断用户。
_Avoid_: URL Scheme(会和 Scheme Association 混淆)
@@ -192,7 +196,7 @@ Port Listener 是否在监听的唯一事实:关闭、启动中、监听中、
_Avoid_: 用绑定端口或任务句柄推断是否在监听
**Clipboard Listener**:
-剪贴板监听器。监控剪贴板变化,过滤出 URL 后发出通知。
+剪贴板监听器。监控剪贴板变化,把其中符合 Passive Capture 的链接通知出去。
**Category**:
下载分类和目标目录规则。将文件扩展名匹配到分类并解析下载目录。
diff --git a/app/config/cfg.py b/app/config/cfg.py
index 663c6a31..81ab1174 100644
--- a/app/config/cfg.py
+++ b/app/config/cfg.py
@@ -309,6 +309,7 @@ class Config(QConfig):
OptionsValidator(CloseMode), EnumSerializer(CloseMode),
)
isClipboardListenerEnabled = ConfigItem("Software", "ClipboardListener", True, BoolValidator())
+ clipboardIgnoredPacks = ConfigItem("Software", "ClipboardIgnoredPacks", [], StringListValidator())
geometry = ConfigItem(
"Software", "Geometry", (0, 0, 0, 0), serializer=GeometrySerializer(),
)
diff --git a/app/models/pack.py b/app/models/pack.py
index 01ecda38..10052548 100644
--- a/app/models/pack.py
+++ b/app/models/pack.py
@@ -267,6 +267,7 @@ class FeaturePack:
manifest: PackManifest | None = None
config: PackConfig | None = None
proxySchemes: frozenset[str] | None = None
+ capturedLinks: str | None = None
parsers: list[type[TaskParser]] = []
parse: Callable[[TaskOptions], Awaitable[Task]] | None = None
diff --git a/app/services/feature_service.py b/app/services/feature_service.py
index edad5b56..5dd9e746 100644
--- a/app/services/feature_service.py
+++ b/app/services/feature_service.py
@@ -5,6 +5,7 @@ from threading import Event
from typing import TYPE_CHECKING
from urllib.parse import urlparse
+from app.config.cfg import cfg
from app.config.paths import FEATURES_DIR
from app.models.task import TaskOptions
from app.platform import file_association
@@ -100,7 +101,10 @@ class FeatureService:
def matchPassive(self, url: str) -> bool:
options = TaskOptions(url=url)
- return any(parser.matchPassive(options) for parser in self._parsers)
+ parser = next((parser for parser in self._parsers if parser.match(options)), None)
+ return (parser is not None
+ and parser.pack.packId not in cfg.clipboardIgnoredPacks.value
+ and parser.matchPassive(options))
# ── Card construction (the seam) ──
@@ -198,7 +202,6 @@ class FeatureService:
)
def setFileAssociation(self, isEnabled: bool) -> None:
- from app.config.cfg import cfg
for pack in self._packs:
if pack.config and pack.config.associateFileTypes is not None:
cfg.set(pack.config.associateFileTypes, isEnabled)
diff --git a/app/view/components/setting_cards.py b/app/view/components/setting_cards.py
index d21b329c..31e2880f 100644
--- a/app/view/components/setting_cards.py
+++ b/app/view/components/setting_cards.py
@@ -3,7 +3,7 @@ from __future__ import annotations
from re import compile
from urllib.parse import urlsplit
-from PySide6.QtCore import Qt, QEvent, QRectF, Signal
+from PySide6.QtCore import QCoreApplication, Qt, QEvent, QRectF, Signal
from PySide6.QtGui import QPainter, QPainterPath
from PySide6.QtWidgets import (
QWidget, QVBoxLayout, QButtonGroup, QHBoxLayout, QLineEdit,
@@ -14,7 +14,7 @@ from qfluentwidgets import (
ConfigItem, FluentIcon, BodyLabel, CaptionLabel,
RadioButton, ComboBox, LineEdit, ToolButton, ToolTipFilter,
PrimaryPushButton, InfoBar, InfoBarPosition,
- IconWidget,
+ IconWidget, CheckBox, SwitchButton, IndicatorPosition,
)
from app.view.components.setting_card_group import CollapsibleSettingCard
@@ -339,6 +339,58 @@ class ProxySettingCard(CollapsibleSettingCard):
cfg.set(self._configItem, url)
+class ClipboardSettingCard(CollapsibleSettingCard):
+
+ def __init__(self, packs: list, parent=None):
+ super().__init__(FluentIcon.PASTE, self.tr("剪贴板监听"),
+ self.tr("剪贴板监听器将自动检测剪贴板中的链接并添加下载任务"), parent=parent)
+ self._packs = [pack for pack in packs if pack.capturedLinks]
+ self._initWidget()
+ self._initLayout()
+ self._bind()
+
+ def _initWidget(self) -> None:
+ self.switchButton = SwitchButton(self, IndicatorPosition.RIGHT)
+ self.switchButton.setOnText(QCoreApplication.translate("SwitchSettingCard", "On"))
+ self.switchButton.setOffText(QCoreApplication.translate("SwitchSettingCard", "Off"))
+ self.switchButton.setChecked(cfg.isClipboardListenerEnabled.value)
+
+ self.packWidget = QWidget(self.view)
+ self.packWidget.setEnabled(cfg.isClipboardListenerEnabled.value)
+ self.packLayout = QVBoxLayout(self.packWidget)
+ self.checkBoxes = {}
+ for pack in self._packs:
+ checkBox = CheckBox(QCoreApplication.translate(type(pack).__name__, pack.capturedLinks), self.packWidget)
+ checkBox.setChecked(pack.packId not in cfg.clipboardIgnoredPacks.value)
+ self.checkBoxes[pack.packId] = checkBox
+
+ def _initLayout(self) -> None:
+ self.addWidget(self.switchButton)
+ self.packLayout.setSpacing(19)
+ self.packLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
+ self.packLayout.setContentsMargins(48, 18, 0, 18)
+ for checkBox in self.checkBoxes.values():
+ self.packLayout.addWidget(checkBox)
+ self.addGroupWidget(self.packWidget)
+
+ def _bind(self) -> None:
+ self.switchButton.checkedChanged.connect(self._onSwitchCheckedChanged)
+ cfg.isClipboardListenerEnabled.valueChanged.connect(self._onEnabledChanged)
+ for checkBox in self.checkBoxes.values():
+ checkBox.toggled.connect(self._onPackToggled)
+
+ def _onSwitchCheckedChanged(self, isChecked: bool) -> None:
+ cfg.set(cfg.isClipboardListenerEnabled, isChecked)
+
+ def _onEnabledChanged(self, isEnabled: bool) -> None:
+ self.switchButton.setChecked(isEnabled)
+ self.packWidget.setEnabled(isEnabled)
+
+ def _onPackToggled(self) -> None:
+ cfg.set(cfg.clipboardIgnoredPacks,
+ [packId for packId, checkBox in self.checkBoxes.items() if not checkBox.isChecked()])
+
+
class SelectFolderSettingCard(SettingCard):
pathChanged = Signal(str)
diff --git a/app/view/pages/setting_page.py b/app/view/pages/setting_page.py
index dbf0d7fc..361ceb47 100644
--- a/app/view/pages/setting_page.py
+++ b/app/view/pages/setting_page.py
@@ -25,7 +25,7 @@ from app.config.constants import (
from app.view.components.category_settings import CategoryRulesCard
from app.view.components.setting_card_group import CollapsibleSettingCardGroup
from app.view.components.setting_cards import (
- HeadersPresetSettingCard, IdentitySettingCard, LineEditSettingCard,
+ ClipboardSettingCard, HeadersPresetSettingCard, IdentitySettingCard, LineEditSettingCard,
PercentSpinBoxSettingCard, ProxySettingCard, SpinBoxSettingCard,
)
from app.view.components.editors import FolderPicker, addRecentFolder
@@ -343,9 +343,7 @@ class SettingPage(FastScrollArea):
),
)
softwareCards.append(
- SwitchSettingCard(FluentIcon.PASTE, self.tr("剪贴板监听"),
- self.tr("剪贴板监听器将自动检测剪贴板中的链接并添加下载任务"),
- cfg.isClipboardListenerEnabled),
+ ClipboardSettingCard(self._featureService.packs),
)
if not IS_ANDROID:
softwareCards.append(self.migrateCard)
diff --git a/features/bili_pack/pack.py b/features/bili_pack/pack.py
index 1bcb62e7..d106a5eb 100644
--- a/features/bili_pack/pack.py
+++ b/features/bili_pack/pack.py
@@ -3,6 +3,7 @@ from __future__ import annotations
from urllib.parse import urlparse
from app.client import buildClient
+from app.i18n import N
from app.models.pack import FeaturePack, TaskParser
from app.models.task import Task, TaskOptions
from app.platform.filesystem import toSafeFilename
@@ -154,6 +155,7 @@ async def fetchView(client, biliUrl) -> dict:
class BilibiliPack(FeaturePack):
packId = "bili"
+ capturedLinks = N("BilibiliPack", "哔哩哔哩视频、番剧")
config = bilibiliConfig
parsers = [BilibiliParser]
diff --git a/features/bittorrent_pack/pack.py b/features/bittorrent_pack/pack.py
index bb50dc36..1da16ff1 100644
--- a/features/bittorrent_pack/pack.py
+++ b/features/bittorrent_pack/pack.py
@@ -101,6 +101,7 @@ class TorrentParser(TaskParser):
class BitTorrentPack(FeaturePack):
packId = "bt"
+ capturedLinks = N("BitTorrentPack", "磁力链接")
config = bittorrentConfig
proxySchemes = PROXY_SCHEMES
parsers = [TorrentParser]
diff --git a/features/ed2k_pack/pack.py b/features/ed2k_pack/pack.py
index 748a8b6a..1effd4b3 100644
--- a/features/ed2k_pack/pack.py
+++ b/features/ed2k_pack/pack.py
@@ -5,6 +5,7 @@ from collections.abc import Callable
from pathlib import Path
from app.config.paths import APP_DATA_DIR
+from app.i18n import N
from app.models.pack import FeaturePack, TaskParser, UriScheme
from app.models.task import Task, TaskError, TaskOptions
from app.platform.filesystem import toSafeFilename
@@ -46,6 +47,7 @@ class ED2kParser(TaskParser):
class ED2kPack(FeaturePack):
packId = "ed2k"
+ capturedLinks = N("ED2kPack", "eD2k 链接")
config = ed2kConfig
parsers = [ED2kParser]
proxySchemes = PROXY_SCHEMES
diff --git a/features/ftp_pack/pack.py b/features/ftp_pack/pack.py
index dee562c0..0927b012 100644
--- a/features/ftp_pack/pack.py
+++ b/features/ftp_pack/pack.py
@@ -5,6 +5,7 @@ from urllib.parse import unquote, urlparse
from loguru import logger
+from app.i18n import N
from app.models.pack import FeaturePack, TaskParser, UriScheme
from .config import ftpConfig
from app.models.task import Task, TaskOptions, SpecialFileSize
@@ -122,6 +123,7 @@ class FtpParser(TaskParser):
class FtpPack(FeaturePack):
packId = "ftp"
+ capturedLinks = N("FtpPack", "FTP / FTPS 链接")
config = ftpConfig
proxySchemes = PROXY_SCHEMES
parsers = [FtpParser]
diff --git a/features/github_pack/pack.py b/features/github_pack/pack.py
index c278544a..ac68af1e 100644
--- a/features/github_pack/pack.py
+++ b/features/github_pack/pack.py
@@ -4,6 +4,7 @@ from urllib.parse import urlparse
from loguru import logger
+from app.i18n import N
from app.models.pack import FeaturePack, TaskParser
from app.models.task import Task, TaskOptions
from http_pack.pack import buildTask
@@ -117,6 +118,7 @@ class GitHubParser(TaskParser):
class GitHubPack(FeaturePack):
packId = "github"
+ capturedLinks = N("GitHubPack", "GitHub 发布文件与仓库文件")
config = githubConfig
parsers = [GitHubParser]
diff --git a/features/http_pack/pack.py b/features/http_pack/pack.py
index 5936bf9b..09becf89 100644
--- a/features/http_pack/pack.py
+++ b/features/http_pack/pack.py
@@ -12,6 +12,7 @@ from loguru import logger
from app.client import buildClient, toEmulation
from app.config.cfg import cfg
+from app.i18n import N
from app.models.pack import FeaturePack, TaskParser
from app.models.task import (
Task, TaskOptions, ResourceTaskOptions, SpecialFileSize,
@@ -229,6 +230,7 @@ class HttpParser(TaskParser):
class HttpPack(FeaturePack):
packId = "http"
+ capturedLinks = N("HttpPack", "文件直链(视频、音频、压缩包、安装包、镜像、文档)")
parsers = [HttpParser]
def taskCardClass(self, task: Task) -> type | None:
diff --git a/features/huggingface_pack/pack.py b/features/huggingface_pack/pack.py
index a1e68948..2764198d 100644
--- a/features/huggingface_pack/pack.py
+++ b/features/huggingface_pack/pack.py
@@ -6,6 +6,7 @@ from pathlib import PurePosixPath
from urllib.parse import urlparse
from app.client import buildClient
+from app.i18n import N
from app.models.pack import FeaturePack, TaskParser
from app.models.task import Task, TaskFile, TaskOptions
from app.platform.filesystem import toSafeFilename
@@ -199,6 +200,7 @@ class HuggingFaceParser(TaskParser):
class HuggingFacePack(FeaturePack):
packId = "huggingface"
+ capturedLinks = N("HuggingFacePack", "Hugging Face 模型与数据集文件")
config = huggingFaceConfig
parsers = [HuggingFaceParser]
diff --git a/features/m3u8_pack/pack.py b/features/m3u8_pack/pack.py
index 96982bbe..80c36303 100644
--- a/features/m3u8_pack/pack.py
+++ b/features/m3u8_pack/pack.py
@@ -251,6 +251,7 @@ class M3U8Parser(TaskParser):
class M3U8Pack(FeaturePack):
packId = "m3u8"
+ capturedLinks = N("M3U8Pack", "M3U8 / DASH 流媒体")
parsers = [M3U8Parser]
def taskCardClass(self, task: Task) -> type | None:
diff --git a/features/yt_dlp_pack/pack.py b/features/yt_dlp_pack/pack.py
index 70347745..3ccce4cf 100644
--- a/features/yt_dlp_pack/pack.py
+++ b/features/yt_dlp_pack/pack.py
@@ -2,6 +2,7 @@ from __future__ import annotations
from urllib.parse import urlparse, parse_qs, quote
+from app.i18n import N
from app.models.pack import FeaturePack, TaskParser
from app.models.task import Task, TaskOptions, SpecialFileSize
from app.platform.filesystem import toSafeFilename
@@ -59,6 +60,7 @@ class YouTubeParser(TaskParser):
class YouTubePack(FeaturePack):
packId = "ytdlp"
+ capturedLinks = N("YouTubePack", "YouTube 视频")
parsers = [YouTubeParser]
def taskCardClass(self, task: Task) -> type | None:
diff --git a/tests/test_android.py b/tests/test_android.py
index 56c887b5..7e83a8d1 100644
--- a/tests/test_android.py
+++ b/tests/test_android.py
@@ -33,6 +33,7 @@ DESKTOP_ONLY = {
("Personalization", "ShowMenuBarSpeed"),
("Personalization", "ThemeMode"),
("Software", "AutoRun"),
+ ("Software", "ClipboardIgnoredPacks"),
("Software", "ClipboardListener"),
("Software", "CloseMode"),
("Software", "Geometry"),
diff --git a/tests/test_passive_capture.py b/tests/test_passive_capture.py
new file mode 100644
index 00000000..1256624c
--- /dev/null
+++ b/tests/test_passive_capture.py
@@ -0,0 +1,26 @@
+from __future__ import annotations
+
+import json
+
+from app.config.cfg import cfg
+from app.config.paths import APP_DATA_DIR
+from app.models.pack import PackServices
+from http_pack.pack import HttpPack
+from huggingface_pack.pack import HuggingFacePack
+
+MODEL_URL = "https://huggingface.co/org/repo/resolve/main/model.zip"
+
+
+def test_match_skips_ignored_pack(engine):
+ services = PackServices(coroutineRunner=engine.coroutineRunner, speedMeter=engine.speedMeter)
+ engine.featureService.register(HuggingFacePack(services))
+ engine.featureService.register(HttpPack(services))
+ assert engine.featureService.matchPassive(MODEL_URL)
+
+ configFile = APP_DATA_DIR / "UserConfig.json"
+ config = json.loads(configFile.read_text(encoding="utf-8"))
+ config["Software"] = {"ClipboardIgnoredPacks": ["huggingface"]}
+ configFile.write_text(json.dumps(config), encoding="utf-8")
+ cfg.load(configFile)
+
+ assert not engine.featureService.matchPassive(MODEL_URL)