Skip to content

Commit d209771

Browse files
committed
feat: 在 Windows 中注册 AppID 以实现在系统设置中关闭 GD3 Toast 通知
- 添加 attemptRegisterAppID 函数以注册 AppID - 在弹出通知时调用 attemptRegisterAppID 函数 - 添加 logo.ico 文件到资源文件 - Fix #167
1 parent b334068 commit d209771

File tree

4 files changed

+15275
-15
lines changed

4 files changed

+15275
-15
lines changed

app/common/methods.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from urllib.parse import unquote, parse_qs, urlparse
1414

1515
import httpx
16-
from PySide6.QtCore import QUrl, QOperatingSystemVersion
16+
from PySide6.QtCore import QUrl, QOperatingSystemVersion, QResource
1717
from PySide6.QtGui import QDesktopServices
1818
from PySide6.QtWidgets import QApplication
1919
from loguru import logger
@@ -201,6 +201,25 @@ def getLocalTimeFromGithubApiTime(gmtTimeStr:str):
201201

202202
return localTimeNaive
203203

204+
def attemptRegisterAppID(appId: str = "GD3", appName: str = "Ghost Downloader", iconPath: Path = Path("{}/logo.ico".format(cfg.appPath))):
205+
import winreg
206+
keyPath = f"SOFTWARE\\Classes\\AppUserModelId\\{appId}"
207+
208+
try:
209+
reg_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, keyPath)
210+
winreg.CloseKey(reg_key)
211+
return
212+
except FileNotFoundError:
213+
with open(iconPath, "wb") as f:
214+
f.write(QResource(":/image/logo.ico").data())
215+
216+
winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER)
217+
with winreg.CreateKeyEx(winreg.HKEY_CURRENT_USER, keyPath) as masterKey:
218+
winreg.SetValueEx(masterKey, "DisplayName", 0, winreg.REG_SZ, appName)
219+
if iconPath is not None:
220+
winreg.SetValueEx(masterKey, "IconUri", 0, winreg.REG_SZ, str(iconPath.resolve()))
221+
except Exception as e:
222+
logger.error(f"Could not register the application: {e}")
204223

205224
def getLinkInfo(url: str, headers: dict, fileName: str = "", verify: bool = cfg.SSLVerify.value, proxy: str = "", followRedirects: bool = True) -> tuple:
206225
if not proxy:

app/view/pop_up_window.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
from qfluentwidgets import FluentIcon as FIF
88
from qfluentwidgets.common.screen import getCurrentScreenGeometry
99
from qframelesswindow import WindowEffect
10-
from app.common.methods import isGreaterEqualWin10
1110

11+
from app.common.methods import isGreaterEqualWin10, attemptRegisterAppID
1212
from app.common.methods import openFile, bringWindowToTop, isAbleToShowToast
1313
from app.view.Ui_PopUpWindow import Ui_PopUpWindow
1414

@@ -252,8 +252,10 @@ def showPopUpWindow(cls, fileResolvePath:str, mainWindow=None):
252252
{'activationType': 'protocol', 'arguments': fileResolvePath, 'content': cls.tr('打开文件')},
253253
{'activationType': 'protocol', 'arguments': dirname(fileResolvePath), 'content': cls.tr('打开目录')}
254254
]
255-
256-
return TaskExecutor.run(toast, cls.tr("下载完成"), fileResolvePath, icon=icon, buttons=buttons)
255+
256+
attemptRegisterAppID()
257+
258+
return TaskExecutor.run(toast, cls.tr("下载完成"), fileResolvePath, icon=icon, buttons=buttons, app_id="GD3")
257259
else:
258260
w = FinishedPopUpWindow(fileResolvePath, mainWindow)
259261
w.show()
@@ -294,7 +296,10 @@ def showPopUpWindow(cls, receiveContent:str, mainWindow=None):
294296
'src': f"file://{logoTempFile}",
295297
'placement': 'appLogoOverride'
296298
}
297-
return TaskExecutor.run(toast, cls.tr("接收到来自浏览器的下载任务:"), receiveContent, icon=icon) # TODO 点击后 bringWindowToTop(mainWindow), 需要信号
299+
300+
attemptRegisterAppID()
301+
302+
return TaskExecutor.run(toast, cls.tr("接收到来自浏览器的下载任务:"), receiveContent, icon=icon, app_id="GD3") # TODO 点击后 bringWindowToTop(mainWindow), 需要信号
298303
else:
299304
w = ReceivedPopUpWindow(receiveContent, mainWindow)
300305
w.show()

0 commit comments

Comments
 (0)