Skip to content

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

feat(notification): 添加磁盘空间不足通知功能并实现多语言支持

XiaoYouChR0f316e29ac84
diff --git a/Ghost-Downloader-3.py b/Ghost-Downloader-3.py
index 419064f4..98d862b5 100644
--- a/Ghost-Downloader-3.py
+++ b/Ghost-Downloader-3.py
@@ -175,11 +175,12 @@ def startApp(application, isSilent=False):
from app.platform.android import IS_ANDROID
if IS_ANDROID:
- from app.platform.android_notification import notifyTaskCompleted
+ from app.platform.android_notification import notifyTaskCompleted, notifyDiskSpaceInsufficient
else:
- from app.platform.desktop_notification import init, notifyTaskCompleted
+ from app.platform.desktop_notification import init, notifyTaskCompleted, notifyDiskSpaceInsufficient
coroutineRunner.submit(init())
taskService.taskCompleted.connect(notifyTaskCompleted)
+ taskService.diskSpaceInsufficient.connect(notifyDiskSpaceInsufficient)
from app.services.plan import plan
taskService.tasksAllCompleted.connect(plan.trigger)
diff --git a/android/main.py b/android/main.py
index af20d09a..dcfd9a6e 100644
--- a/android/main.py
+++ b/android/main.py
@@ -47,7 +47,10 @@ def startApp(application):
from app.config.cfg import cfg
from app.models.pack import PackConfig
from app.platform.android_keepalive import keepAlive, REASON_DOWNLOAD, REASON_BROWSER, requestIgnoreBatteryOptimizations
- from app.platform.android_notification import notifyBrowserPaired, notifyBrowserTaskAdded, notifyTaskCompleted, requestNotificationPermission
+ from app.platform.android_notification import (
+ notifyBrowserPaired, notifyBrowserTaskAdded, notifyDiskSpaceInsufficient,
+ notifyTaskCompleted, requestNotificationPermission,
+ )
from app.services.browser_service import browserService
from app.services.coroutine_runner import coroutineRunner
from app.services.feature_service import featureService
@@ -86,6 +89,7 @@ def startApp(application):
requestIgnoreBatteryOptimizations()
taskService.taskCompleted.connect(notifyTaskCompleted)
+ taskService.diskSpaceInsufficient.connect(notifyDiskSpaceInsufficient)
taskService.taskStarted.connect(lambda _: keepAlive.holdFor(REASON_DOWNLOAD))
taskService.tasksAllCompleted.connect(lambda: keepAlive.release(REASON_DOWNLOAD))
taskService.taskStarted.connect(lambda _: speedMeter.start())
diff --git a/app/platform/android_notification.py b/app/platform/android_notification.py
index 45292117..dc26b3f4 100644
--- a/app/platform/android_notification.py
+++ b/app/platform/android_notification.py
@@ -2,9 +2,13 @@ from __future__ import annotations
from typing import TYPE_CHECKING
+from PySide6.QtCore import QCoreApplication
+
if TYPE_CHECKING:
from app.models.task import Task
+tr = QCoreApplication.translate
+
DOWNLOAD_CHANNEL = "gd3_downloads"
@@ -36,8 +40,23 @@ def notify(channelId: str, channelName: str, notificationId: int,
def notifyTaskCompleted(task: Task) -> None:
- notify(DOWNLOAD_CHANNEL, "下载", hash(task.taskId) & 0x7FFFFFFF,
- "下载完成", task.name, ongoing=False, lowImportance=False)
+ notify(DOWNLOAD_CHANNEL, tr("Notifications", "Downloads"),
+ hash(task.taskId) & 0x7FFFFFFF,
+ tr("Notifications", "Download completed"), task.name,
+ ongoing=False, lowImportance=False)
+
+
+DISK_SPACE_NOTIFICATION_ID = 0x6764_0003
+
+
+def notifyDiskSpaceInsufficient(free: int, needed: int) -> None:
+ from app.format import toReadableSize
+ notify(DOWNLOAD_CHANNEL, tr("Notifications", "Downloads"),
+ DISK_SPACE_NOTIFICATION_ID,
+ tr("Notifications", "Disk space insufficient"),
+ tr("Notifications", "Remaining {0}, need {1}, task not auto-started").format(
+ toReadableSize(free), toReadableSize(needed)),
+ ongoing=False, lowImportance=False)
BROWSER_PUSH_NOTIFICATION_ID = 0x6764_0001
@@ -48,17 +67,21 @@ def notifyBrowserTaskAdded(tasks: list[Task]) -> None:
if not tasks:
return
count = len(tasks)
- title = "浏览器推送" if count == 1 else f"浏览器推送 ({count})"
+ title = tr("Notifications", "Browser push") if count == 1 \
+ else tr("Notifications", "Browser push ({count})").format(count=count)
text = tasks[0].name if count == 1 else "、".join(t.name for t in tasks[:3])
if count > 3:
- text += f" 等 {count} 项"
- notify(DOWNLOAD_CHANNEL, "下载", BROWSER_PUSH_NOTIFICATION_ID,
+ text += tr("Notifications", " and {count} more").format(count=count)
+ notify(DOWNLOAD_CHANNEL, tr("Notifications", "Downloads"),
+ BROWSER_PUSH_NOTIFICATION_ID,
title, text, ongoing=False, lowImportance=False)
def notifyBrowserPaired(peerAddress: str) -> None:
- notify(DOWNLOAD_CHANNEL, "下载", BROWSER_PAIR_NOTIFICATION_ID,
- "浏览器扩展已连接", peerAddress, ongoing=False, lowImportance=True)
+ notify(DOWNLOAD_CHANNEL, tr("Notifications", "Downloads"),
+ BROWSER_PAIR_NOTIFICATION_ID,
+ tr("Notifications", "Browser extension connected"), peerAddress,
+ ongoing=False, lowImportance=True)
def requestNotificationPermission() -> None:
diff --git a/app/platform/desktop_notification.py b/app/platform/desktop_notification.py
index 9c197684..2cdbc875 100644
--- a/app/platform/desktop_notification.py
+++ b/app/platform/desktop_notification.py
@@ -3,8 +3,10 @@ from __future__ import annotations
from pathlib import Path
from typing import TYPE_CHECKING
-from PySide6.QtCore import QFileInfo, QStandardPaths, Qt
+from PySide6.QtCore import QCoreApplication, QFileInfo, QStandardPaths, Qt
from PySide6.QtWidgets import QFileIconProvider
+
+tr = QCoreApplication.translate
from loguru import logger
from app.platform.desktop import openFile, revealInFolder
@@ -31,6 +33,18 @@ async def init() -> None:
notifier = DN(app_name="Ghost Downloader", app_icon=Icon(path=iconPath))
+def notifyDiskSpaceInsufficient(free: int, needed: int) -> None:
+ if notifier is None:
+ return
+ from app.format import toReadableSize
+ from app.services.coroutine_runner import coroutineRunner
+ coroutineRunner.submit(notifier.send(
+ title=tr("Notifications", "Disk space insufficient"),
+ message=tr("Notifications", "Remaining {0}, need {1}, task not auto-started").format(
+ toReadableSize(free), toReadableSize(needed)),
+ ))
+
+
def notifyTaskCompleted(task: Task) -> None:
if notifier is None:
return
@@ -56,11 +70,11 @@ def notifyTaskCompleted(task: Task) -> None:
from app.services.coroutine_runner import coroutineRunner
coroutineRunner.submit(notifier.send(
- title="下载完成",
+ title=tr("Notifications", "Download completed"),
message=task.name,
buttons=[
- Button(title="打开文件", on_pressed=lambda: openFile(outputPath)),
- Button(title="打开目录", on_pressed=lambda: revealInFolder(outputPath)),
+ Button(title=tr("Notifications", "Open file"), on_pressed=lambda: openFile(outputPath)),
+ Button(title=tr("Notifications", "Open folder"), on_pressed=lambda: revealInFolder(outputPath)),
],
on_clicked=lambda: openFile(outputPath),
icon=Icon(path=iconPath) if iconPath.exists() else None,
diff --git a/app/view/pages/task_page.py b/app/view/pages/task_page.py
index ce878a84..15692c58 100644
--- a/app/view/pages/task_page.py
+++ b/app/view/pages/task_page.py
@@ -263,7 +263,6 @@ class TaskPage(QWidget):
taskService.taskCompleted, taskService.taskFailed):
signal.connect(self._refreshVisibleCards)
taskService.fileDisappeared.connect(self._onFileDisappeared)
- taskService.diskSpaceInsufficient.connect(self._onDiskSpaceInsufficient)
speedMeter.speedChanged.connect(self._onSpeedChanged)
self.scrollArea.verticalScrollBar().valueChanged.connect(self._refreshViewport)
@@ -421,17 +420,6 @@ class TaskPage(QWidget):
else:
plan.clear()
- def _onDiskSpaceInsufficient(self, free: int, needed: int) -> None:
- from qfluentwidgets import InfoBar, InfoBarPosition
- InfoBar.warning(
- self.tr("磁盘空间不足"),
- self.tr("剩余 {},需要 {},任务未自动开始").format(
- toReadableSize(free), toReadableSize(needed)),
- duration=5000,
- position=InfoBarPosition.TOP,
- parent=self.window(),
- )
-
def _onCategoryEnabledChanged(self, value) -> None:
self.categoryFilterButton.setVisible(bool(value))