@@ -6,13 +6,20 @@ aria2Launcher::aria2Launcher(QWidget *parent)
6
6
{
7
7
ui->setupUi (this );
8
8
9
+ // 检测是否已开启aria2 launcher
10
+ static QSharedMemory* shareMem = new QSharedMemory (" SingleApp" ); // 创建“SingleApp”的共享内存块
11
+ if (!shareMem->create (1 ))// 创建大小1b的内存
12
+ {
13
+ QApplication::beep ();
14
+ QMessageBox::warning (this , " 警告" , " Aria2 Launcher已在运行" );
15
+ exit (-1 ); // 创建失败,说明已经有一个程序运行,退出当前程序
16
+ }// https://blog.csdn.net/qianlixiaomage/article/details/125851086
17
+
9
18
checkFile ();// 检测所需文件是否存在
10
19
checkAria2Status ();// 检测是否已经有aria2c运行,可选择新建进程或kill已存在的进程再新建进程
11
20
uiInitialize ();// ui初始化
12
21
showWindow ();// 根据设置决定是否开启webui
13
22
showHide ();// 根据设置决定是显示窗口还是启动到托盘
14
-
15
- // this->resize((this->width())+1,(this->height())+1);//界面刷新
16
23
17
24
connect (ui->aboutqt , &QAction::triggered, [this ]() {QMessageBox::aboutQt (this , tr (" About Qt" )); });// 弹出qt版本
18
25
connect (ui->aboutthis , &QAction::triggered, [this ]() {aboutWin->exec (); });// 弹出关于窗口
@@ -72,20 +79,24 @@ void aria2Launcher::checkFile() { //检测所需文件是否存在
72
79
QFileInfo file3 (" aria2.session" );
73
80
QFileInfo file4 (" yaaw" );
74
81
if (!file1.isFile ()) {
75
- QMessageBox::critical (this , " 缺少文件" , " aria2c.exe不存在,无法启动" );
76
- exit (0 );
82
+ QApplication::beep ();
83
+ QMessageBox::warning (this , " 缺少文件" , " aria2c.exe不存在,无法启动" );
84
+ exit (-1 );
77
85
}
78
86
else if (!file2.isFile ()) {
79
- QMessageBox::critical (this , " 缺少文件" , " aria2.conf不存在,无法启动" );
80
- exit (0 );
87
+ QApplication::beep ();
88
+ QMessageBox::warning (this , " 缺少文件" , " aria2.conf不存在,无法启动" );
89
+ exit (-1 );
81
90
}
82
91
else if (!file3.isFile ()) {
83
- QMessageBox::critical (this , " 缺少文件" , " aria2.session不存在,无法启动" );
84
- exit (0 );
92
+ QApplication::beep ();
93
+ QMessageBox::warning (this , " 缺少文件" , " aria2.session不存在,无法启动" );
94
+ exit (-1 );
85
95
}
86
96
else if (!file4.isDir ()) {
87
- QMessageBox::critical (this , " 缺少文件夹" , " /yaaw/不存在,无法启动" );
88
- exit (0 );
97
+ QApplication::beep ();
98
+ QMessageBox::warning (this , " 缺少文件夹" , " /yaaw/不存在,无法启动" );
99
+ exit (-1 );
89
100
}
90
101
}
91
102
@@ -94,6 +105,7 @@ void aria2Launcher::checkAria2Status() { //检测是否已经有aria2c运行,可
94
105
bool flag = true ;
95
106
int findPid = (int )FindProcessIDByName (" aria2c.exe" ); // 检测aria2c是否已经存在
96
107
while (findPid != 0 && flag == true ) { // 如果aria2c.exe已存在
108
+ QApplication::beep ();
97
109
int id = QMessageBox::information (this , " 提示" , QString (" 第%1次检测\n aria2c.exe(PID:%2)已在运行(造成这种情况的原因可能是以下几点)\n 1:有正在使用aria2c的应用\n 2:您未正确关闭此软件或其他使用aria2c的软件\n\n 继续:创建一个新的aria2c进程,不影响已存在的进程\n 结束进程:结束已存在的aria2c进程(请确保您没有使用此进程下载文件中)\n\n 如果您不知道如何选择,请点击<继续>" ).arg (n).arg (findPid), QString (" 继续" ), QString (" 结束进程(PID:%1)" ).arg (findPid), 0 );
98
110
switch (id) {
99
111
case 0 : { // 继续,选择此选项时退出循环,不再检测,直接创建新进程
@@ -150,7 +162,7 @@ void aria2Launcher::uiInitialize() { //ui初始化
150
162
151
163
// 菜单初始化
152
164
connect (SOH, &QAction::triggered, this , &aria2Launcher::showOrHide);
153
- connect (Close, &QAction::triggered, this , &aria2Launcher::quitApp );
165
+ connect (Close, &QAction::triggered, [ this ]() {qApp-> quit (); } );
154
166
connect (trayIcon, &QSystemTrayIcon::activated, this , &aria2Launcher::on_activatedSysTrayIcon);
155
167
156
168
QActionGroup* group = new QActionGroup (this );
@@ -159,31 +171,27 @@ void aria2Launcher::uiInitialize() { //ui初始化
159
171
}
160
172
161
173
void aria2Launcher::closeEvent (QCloseEvent* event) { // 关闭事件
162
- if (!quitApp ()) {
163
- event->ignore ();
164
- }
165
- }
166
-
167
- bool aria2Launcher::quitApp () { // 退出询问
174
+ QApplication::beep ();
168
175
int id = QMessageBox::information (this , " 二次确认" , " 是否退出程序?\n 退出请确保没有文件正在下载中" , QString (" 退出" ), QString (" 最小化到系统托盘" ), QString (" 取消" ), 2 );
169
176
switch (id) {
170
177
case 0 : {
171
- qApp-> quit ();
178
+ event-> accept ();
172
179
break ;
173
180
}
174
181
case 1 : {
175
182
this ->hide ();
176
183
showWindowsMessage ();// 提示程序已进入托盘
177
- return false ;
184
+ event-> ignore () ;
178
185
break ;
179
186
}
180
187
case 2 : {
181
- return false ;
188
+ event-> ignore () ;
182
189
break ;
183
190
}
184
191
}
185
192
}
186
193
194
+
187
195
void aria2Launcher::showWindowsMessage () {// 提示程序已进入托盘
188
196
if (settingWin->ui ->radioButton_3 ->isChecked ()) {
189
197
trayIcon->showMessage (" Aria2 Launcher已最小化到托盘" , " 如果您希望不再看到这个提示,请前往设置关闭" );
0 commit comments