Skip to content

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

新增 SingletonApplication 类实现单实例应用; 改进错误提示和反馈机制; 支持语义化版本

XiaoYouChR66df06dd027e
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 197a13dc..ca73d2d0 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -4,41 +4,41 @@ on:
workflow_dispatch:
jobs:
-# build-windows:
-# strategy:
-# matrix:
-# architecture: [x86_64, arm64]
-# runs-on: windows-2019
-# steps:
-# - name: Checkout code
-# uses: actions/checkout@v2
-#
-# - name: Set up Python 3.11
-# uses: actions/setup-python@v4
-# with:
-# python-version: '3.11'
-#
-# - name: Install dependencies
-# run: |
-# pip install --timeout=300 -r requirements.txt
-# pip install nuitka==2.6.4
-#
-# - name: Build with Nuitka
-# run: |
-# python -c "from app.common.config import VERSION; f = open('version.txt', 'w'); f.write(VERSION); f.close()"
-# $VERSION = gc version.txt
-# Write-Host "VERSION=$VERSION"
-# echo "VERSION=$VERSION" >> $env:GITHUB_ENV
-# del version.txt
-# python deploy.py
-## nuitka --module plugins/jy_os_page.py
-## mv jy_os_page*.pyd output/
-#
-# - name: Upload artifacts
-# uses: actions/upload-artifact@v4
-# with:
-# name: Ghost-Downloader-v${{ env.VERSION }}-Windows-${{ matrix.architecture }}
-# path: dist/Ghost-Downloader-3.dist
+ build-windows:
+ strategy:
+ matrix:
+ architecture: [x86_64, arm64]
+ runs-on: windows-2019
+ steps:
+ - name: Checkout code
+ uses: actions/checkout@v2
+
+ - name: Set up Python 3.11
+ uses: actions/setup-python@v4
+ with:
+ python-version: '3.11'
+
+ - name: Install dependencies
+ run: |
+ pip install --timeout=300 -r requirements.txt
+ pip install nuitka==2.6.4
+
+ - name: Build with Nuitka
+ run: |
+ python -c "from app.common.config import VERSION; f = open('version.txt', 'w'); f.write(VERSION); f.close()"
+ $VERSION = gc version.txt
+ Write-Host "VERSION=$VERSION"
+ echo "VERSION=$VERSION" >> $env:GITHUB_ENV
+ del version.txt
+ python deploy.py
+# nuitka --module plugins/jy_os_page.py
+# mv jy_os_page*.pyd output/
+
+ - name: Upload artifacts
+ uses: actions/upload-artifact@v4
+ with:
+ name: Ghost-Downloader-v${{ env.VERSION }}-Windows-${{ matrix.architecture }}
+ path: dist/Ghost-Downloader-3.dist
build-macos-14-arm:
strategy:
@@ -137,40 +137,40 @@ jobs:
name: Ghost-Downloader-v${{ env.VERSION }}-macOS-${{ matrix.architecture }}
path: Ghost-Downloader-v${{ env.VERSION }}-macOS-${{ matrix.architecture }}.dmg
-# build-ubuntu:
-# strategy:
-# matrix:
-# architecture: [x86_64, arm64]
-# runs-on: ubuntu-20.04
-# steps:
-# - name: Checkout code
-# uses: actions/checkout@v2
-#
-# - name: Set up Python 3.11
-# uses: actions/setup-python@v4
-# with:
-# python-version: '3.11'
-#
-# - name: Install dependencies
-# run: |
-# sudo apt-get update
-# sudo apt-get install -y libegl1-mesa
-# pip install --timeout=300 -r requirements.txt
-# pip install nuitka==2.6.4
-#
-# - name: Build with Nuitka
-# run: |
-# python3 -c "import sys; sys.path.append('./app/common'); import config; print(config.VERSION)" > version.txt
-# version=$(sed -n '4p' version.txt)
-# echo "VERSION=${version}" >> "$GITHUB_ENV"
-# rm -rf version.txt
-# python deploy.py
-## nuitka --module plugins/jy_os_page.py
-## mkdir -p "output/Ghost-Downloader-3.dist/plugins/"
-## mv jy_os_page*.so "output/Ghost-Downloader-3.dist/plugins/"
-#
-# - name: Upload artifacts
-# uses: actions/upload-artifact@v4
-# with:
-# name: Ghost-Downloader-v${{ env.VERSION }}-Linux-${{ matrix.architecture }}
-# path: dist/Ghost-Downloader-3.dist
+ build-ubuntu:
+ strategy:
+ matrix:
+ architecture: [x86_64, arm64]
+ runs-on: ubuntu-20.04
+ steps:
+ - name: Checkout code
+ uses: actions/checkout@v2
+
+ - name: Set up Python 3.11
+ uses: actions/setup-python@v4
+ with:
+ python-version: '3.11'
+
+ - name: Install dependencies
+ run: |
+ sudo apt-get update
+ sudo apt-get install -y libegl1-mesa
+ pip install --timeout=300 -r requirements.txt
+ pip install nuitka==2.6.4
+
+ - name: Build with Nuitka
+ run: |
+ python3 -c "import sys; sys.path.append('./app/common'); import config; print(config.VERSION)" > version.txt
+ version=$(sed -n '4p' version.txt)
+ echo "VERSION=${version}" >> "$GITHUB_ENV"
+ rm -rf version.txt
+ python deploy.py
+# nuitka --module plugins/jy_os_page.py
+# mkdir -p "output/Ghost-Downloader-3.dist/plugins/"
+# mv jy_os_page*.so "output/Ghost-Downloader-3.dist/plugins/"
+
+ - name: Upload artifacts
+ uses: actions/upload-artifact@v4
+ with:
+ name: Ghost-Downloader-v${{ env.VERSION }}-Linux-${{ matrix.architecture }}
+ path: dist/Ghost-Downloader-3.dist
diff --git a/Ghost-Downloader-3.py b/Ghost-Downloader-3.py
index 4417db96..ac06bc9e 100644
--- a/Ghost-Downloader-3.py
+++ b/Ghost-Downloader-3.py
@@ -1,12 +1,12 @@
# coding:utf-8
import os
-# 创建 Application
import sys
-from PySide6.QtWidgets import QApplication
+from PySide6.QtCore import QTimer
from qfluentwidgets import qconfig
+from app.common.application import SingletonApplication
from app.common.config import cfg
# 设置程序运行路径, 便于调试
@@ -17,46 +17,12 @@ else: # 编译后
cfg.appPath = os.path.dirname(sys.executable)
qconfig.load('{}/Ghost Downloader 配置文件.json'.format(os.path.dirname(sys.executable)), cfg)
- def exceptionHandler(type, value, traceback): # 自定义错误捕捉函数
- logger.exception(f"意料之外的错误! {type}: {value}. Traceback: {traceback}")
-
- sys.excepthook = exceptionHandler
-
# 必须在 QApplication 创建前设置缩放比例
-if cfg.get(cfg.dpiScale) == 0:
- pass
-else:
+if cfg.get(cfg.dpiScale) != 0:
os.environ["QT_ENABLE_HIGHDPI_SCALING"] = "0"
os.environ["QT_SCALE_FACTOR"] = str(cfg.get(cfg.dpiScale))
-app = QApplication(sys.argv)
-
-# 检测程序重复运行
-from PySide6.QtCore import QSharedMemory, QTimer
-
-# 尝试访问
-sharedMemory = QSharedMemory()
-sharedMemory.setKey("Ghost Downloader")
-
-if sharedMemory.attach(): # 访问成功, 说明程序正在运行
-
- if sys.platform == "win32":
- import win32gui
- import win32con
-
- hWnd = win32gui.FindWindow(None, "Ghost Downloader")
- win32gui.ShowWindow(hWnd, 1)
-
- # 发送自定义信息唤醒窗口
- # WM_CUSTOM = win32con.WM_USER + 1
- # win32gui.SendMessage(hWnd, WM_CUSTOM, 0, 0)
- win32gui.SendMessage(hWnd, win32con.WM_USER + 1, 0, 0)
-
- win32gui.SetForegroundWindow(hWnd)
-
- sys.exit(-1)
-# 创建 SharedMemory
-sharedMemory.create(1)
+app = SingletonApplication(sys.argv, "Ghost Downloader")
# Starting Program
import time
diff --git a/app/common/application.py b/app/common/application.py
new file mode 100644
index 00000000..eb0ff7ca
--- /dev/null
+++ b/app/common/application.py
@@ -0,0 +1,52 @@
+# coding:utf-8
+import sys
+import traceback
+from typing import List
+
+from PySide6.QtCore import QSharedMemory
+from PySide6.QtWidgets import QApplication
+from loguru import logger
+
+from .signal_bus import signalBus
+
+
+class SingletonApplication(QApplication):
+ """ Singleton application """
+
+ def __init__(self, argv: List[str], key: str):
+ super().__init__(argv)
+ self.key = key
+
+ # cleanup (only needed for unix)
+ QSharedMemory(key).attach()
+ self.memory = QSharedMemory(self)
+ self.memory.setKey(key)
+
+ if self.memory.attach():
+ if sys.platform == "win32":
+ import win32gui
+
+ hWnd = win32gui.FindWindow(None, "Ghost Downloader")
+ win32gui.ShowWindow(hWnd, 1)
+
+ # 发送自定义信息唤醒窗口
+ # WM_CUSTOM = win32con.WM_USER + 1
+ # win32gui.SendMessage(hWnd, WM_CUSTOM, 0, 0)
+ win32gui.SendMessage(hWnd, 1024 + 1, 0, 0)
+
+ win32gui.SetForegroundWindow(hWnd)
+
+ sys.exit(-1)
+
+ if not self.memory.create(1):
+ logger.error(self.memory.errorString())
+ raise RuntimeError(self.memory.errorString())
+
+def exception_hook(exception: BaseException, value, tb):
+ """ exception callback function """
+ message = '\n'.join([''.join(traceback.format_tb(tb)),
+ '{0}: {1}'.format(exception.__name__, value)])
+ logger.exception(f"{message}")
+ signalBus.appErrorSig.emit(message)
+
+sys.excepthook = exception_hook
diff --git a/app/common/methods.py b/app/common/methods.py
index 0ed40b89..bd57fec0 100644
--- a/app/common/methods.py
+++ b/app/common/methods.py
@@ -15,6 +15,7 @@ from PySide6.QtCore import QUrl
from PySide6.QtGui import QDesktopServices
from PySide6.QtWidgets import QApplication
from loguru import logger
+from qfluentwidgets import MessageBox
from app.common.config import cfg, Headers
from app.common.plugin_base import PluginBase
@@ -293,3 +294,14 @@ def addDownloadTask(url: str, fileName: str = None, filePath: str = None,
headers = Headers
signalBus.addTaskSignal.emit(url, fileName, filePath, headers, status, preBlockNum, notCreateHistoryFile, str(fileSize))
+
+def showMessageBox(self, title: str, content: str, showYesButton=False, yesSlot=None):
+ """ show message box """
+ w = MessageBox(title, content, self)
+ if not showYesButton:
+ w.cancelButton.setText('关闭')
+ w.yesButton.hide()
+ w.buttonLayout.insertStretch(0, 1)
+
+ if w.exec() and yesSlot is not None:
+ yesSlot()
diff --git a/app/common/signal_bus.py b/app/common/signal_bus.py
index 96fb6652..4be53130 100644
--- a/app/common/signal_bus.py
+++ b/app/common/signal_bus.py
@@ -6,5 +6,6 @@ class SignalBus(QObject):
""" Signal bus """
addTaskSignal = Signal(str, str, str, dict, str, int, bool, str) # url, fileName, filePath, headers, status, preBlockNum, notCreateHistoryFile, fileSize
allTaskFinished = Signal()
+ appErrorSig = Signal(str)
signalBus = SignalBus()
diff --git a/app/components/update_dialog.py b/app/components/update_dialog.py
index d6ec1272..b8b663b0 100644
--- a/app/components/update_dialog.py
+++ b/app/components/update_dialog.py
@@ -1,5 +1,5 @@
import httpx
-from PySide6.QtCore import QThread, Signal
+from PySide6.QtCore import QThread, Signal, QVersionNumber
from PySide6.QtGui import QColor
from PySide6.QtWidgets import QTableWidgetItem
from loguru import logger
@@ -24,8 +24,8 @@ class GetUpdateThread(QThread):
tagName = content["tag_name"][1:]
- latestVersion = list(map(int, tagName.split(".")))
- currentVersion = list(map(int, VERSION.split(".")))
+ latestVersion = QVersionNumber.fromString(tagName)
+ currentVersion = QVersionNumber.fromString(VERSION)
if latestVersion > currentVersion:
self.gotResponse.emit(content)
diff --git a/app/view/main_window.py b/app/view/main_window.py
index 509b07fb..797afa6d 100644
--- a/app/view/main_window.py
+++ b/app/view/main_window.py
@@ -6,8 +6,8 @@ from ctypes import byref, c_int
from pathlib import Path
import darkdetect
-from PySide6.QtCore import QSize, QThread, Signal, QTimer, QPropertyAnimation, QRect
-from PySide6.QtGui import QIcon, QDragEnterEvent, QDropEvent, QKeySequence
+from PySide6.QtCore import QSize, QThread, Signal, QTimer, QPropertyAnimation, QRect, QUrl
+from PySide6.QtGui import QIcon, QDragEnterEvent, QDropEvent, QKeySequence, QDesktopServices
from PySide6.QtWidgets import QApplication, QGraphicsOpacityEffect
from loguru import logger
from qfluentwidgets import FluentIcon as FIF, setTheme, Theme
@@ -15,9 +15,10 @@ from qfluentwidgets import NavigationItemPosition, MSFluentWindow, SplashScreen
from .setting_interface import SettingInterface
from .task_interface import TaskInterface
-from ..common.config import cfg, Headers, attachmentTypes
+from ..common.config import cfg, Headers, attachmentTypes, FEEDBACK_URL
from ..common.custom_socket import GhostDownloaderSocketServer
-from ..common.methods import getLinkInfo, bringWindowToTop, addDownloadTask
+from ..common.methods import getLinkInfo, bringWindowToTop, addDownloadTask, showMessageBox
+from ..common.signal_bus import signalBus
from ..components.add_task_dialog import AddTaskOptionDialog
from ..components.custom_tray import CustomSystemTrayIcon
from ..components.update_dialog import checkUpdate
@@ -68,6 +69,7 @@ class MainWindow(MSFluentWindow):
self.themeChangedListener = None
self.__onCustomThemeModeChanged(cfg.customThemeMode.value)
cfg.customThemeMode.valueChanged.connect(self.__onCustomThemeModeChanged)
+ signalBus.appErrorSig.connect(self.onAppError)
# 设置背景特效
self.applyBackgroundEffectByCfg()
@@ -256,6 +258,17 @@ class MainWindow(MSFluentWindow):
QApplication.processEvents()
+ def onAppError(self, message: str):
+ """ app error slot """
+ QApplication.clipboard().setText(message)
+ showMessageBox(
+ self,
+ "意料之外的错误!",
+ "错误消息已写入粘贴板和日志。是否报告?",
+ True,
+ lambda: QDesktopServices.openUrl(QUrl(FEEDBACK_URL))
+ )
+
def showAddTaskBox(self):
w = AddTaskOptionDialog(self)
w.exec()