Skip to content

Commit ec7ce57

Browse files
committed
修复一点bug,优化使用体验
1 parent e5af713 commit ec7ce57

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+308
-4999
lines changed

DrCOM_JLU_Qt.pro

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#-------------------------------------------------
66

77
QT += core gui network widgets
8+
RC_ICONS = images/icon.ico
9+
10+
# translations
11+
TRANSLATIONS += ts/DrCOM_zh_CN.ts
812

913
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
1014

@@ -25,8 +29,8 @@ DEFINES += QT_DEPRECATED_WARNINGS
2529
CONFIG += c++11
2630

2731
SOURCES += \
28-
main.cpp \
29-
mainwindow.cpp \
32+
main.cpp \
33+
mainwindow.cpp \
3034
dogcomcontroller.cpp \
3135
interruptiblesleeper.cpp \
3236
dogcom.cpp \
@@ -35,7 +39,7 @@ SOURCES += \
3539
encrypt/sha1.cpp
3640

3741
HEADERS += \
38-
mainwindow.h \
42+
mainwindow.h \
3943
dogcomcontroller.h \
4044
constants.h \
4145
interruptiblesleeper.h \
@@ -45,7 +49,7 @@ HEADERS += \
4549
encrypt/sha1.h
4650

4751
FORMS += \
48-
mainwindow.ui
52+
mainwindow.ui
4953

5054
# Default rules for deployment.
5155
qnx: target.path = /tmp/$${TARGET}/bin
@@ -56,10 +60,5 @@ RESOURCES += \
5660
DrCOM_JLU_Qt.qrc
5761

5862
# Single Application implementation
59-
include(.\singleinstance\singleapplication.pri)
63+
include(singleinstance/singleapplication.pri)
6064
DEFINES += QAPPLICATION_CLASS=QApplication
61-
62-
# QtKeyChain inplementation
63-
include(.\qtkeychain\qt5keychain.pri)
64-
65-
DISTFILES +=

DrCOM_JLU_Qt.qrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
<qresource prefix="/">
33
<file>images/offline.png</file>
44
<file>images/online.png</file>
5+
<file>ts/DrCOM_zh_CN.qm</file>
56
</qresource>
67
</RCC>

constants.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ enum {
2121
OFF_NOT_ON_THIS_IP_MAC,
2222
OFF_MUST_USE_DHCP,
2323
OFF_TIMEOUT,
24-
OFF_DHCP_LOGIN_FAILED,
2524

2625
// challenge 成功 获取到服务器返回的ip地址
2726
OBTAIN_IP_ADDRESS,

dogcom.cpp

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ void DogCom::run()
8080
int keepalive_try_counter = 0;
8181
int first = 1;
8282
while (true) {
83+
if(udp_sender.state()!=QUdpSocket::BoundState){
84+
emit ReportOffline(OFF_TIMEOUT);
85+
break;
86+
}
8387
if (!keepalive_1(udp_sender, auth_information)) {
8488
sleeper->Sleep(200); // 0.2 second
8589
if (keepalive_2(udp_sender, &keepalive_counter, &first)) {
@@ -92,7 +96,7 @@ void DogCom::run()
9296
break;
9397
}
9498
} else {
95-
if (keepalive_try_counter > 5) {
99+
if (keepalive_try_counter > 3) {
96100
// 清理操作
97101
emit ReportOffline(OFF_TIMEOUT);
98102
sleeper->Interrupt();
@@ -130,8 +134,12 @@ bool DogCom::dhcp_challenge(QUdpSocket &udp_sender, unsigned char seed[])
130134
print_packet("[Challenge sent]", challenge_packet, 20);
131135

132136
qDebug()<<"reading from dest...";
133-
udp_sender.waitForReadyRead();
134-
qDebug()<<"read result:"<<udp_sender.readDatagram((char*)recv_packet,1024,server_address,&port_dest);
137+
udp_sender.waitForReadyRead(1000);
138+
if(udp_sender.readDatagram((char*)recv_packet,1024,server_address,&port_dest)<=0){
139+
qDebug()<<"Failed to recv";
140+
qDebug()<<udp_sender.error();
141+
return false;
142+
}
135143
qDebug()<<"read done";
136144
print_packet("[Challenge recv]",recv_packet,76);
137145

@@ -319,13 +327,11 @@ int DogCom::dhcp_login(QUdpSocket &udp_sender, unsigned char seed[], unsigned ch
319327
udp_sender.writeDatagram((const char*)login_packet,login_packet_size,*server_address,port_dest);
320328
print_packet("[Login sent]",login_packet,login_packet_size);
321329

322-
udp_sender.waitForReadyRead();
323-
324-
udp_sender.waitForReadyRead();
330+
udp_sender.waitForReadyRead(1000);
325331
if(udp_sender.readDatagram((char*)recv_packet,1024,server_address,&port_dest)<=0){
326332
qDebug()<<udp_sender.error();
327333
qDebug()<<"Failed to recv data";
328-
return OFF_DHCP_LOGIN_FAILED;
334+
return OFF_TIMEOUT;
329335
}
330336

331337
if (recv_packet[0] != 0x04) {
@@ -341,7 +347,8 @@ int DogCom::dhcp_login(QUdpSocket &udp_sender, unsigned char seed[], unsigned ch
341347
case LOGIN_NOT_ON_THIS_IP:return OFF_NOT_ON_THIS_IP;
342348
case LOGIN_NOT_ON_THIS_MAC:return OFF_NOT_ON_THIS_MAC;
343349
case LOGIN_TOO_MUCH_IP:return OFF_TOO_MUCH_IP;
344-
case LOGIN_UPDATE_CLIENT:return OFF_UPDATE_CLIENT;
350+
// 升级客户端这个密码错了就会弹出俩
351+
case LOGIN_UPDATE_CLIENT:return OFF_WRONG_PASS;
345352
case LOGIN_NOT_ON_THIS_IP_MAC:return OFF_NOT_ON_THIS_IP_MAC;
346353
case LOGIN_MUST_USE_DHCP:return OFF_MUST_USE_DHCP;
347354
default:return OFF_UNKNOWN;
@@ -355,10 +362,6 @@ int DogCom::dhcp_login(QUdpSocket &udp_sender, unsigned char seed[], unsigned ch
355362

356363
memcpy(auth_information, &recv_packet[23], 16);
357364

358-
// udp_sender.waitForReadyRead();
359-
// if(udp_sender.readDatagram((char*)recv_packet,1024,server_address,&port_dest)>=0){
360-
// qDebug()<<"Get notice packet.";
361-
// }
362365
return -1;
363366
}
364367

@@ -439,7 +442,7 @@ int DogCom::keepalive_2(QUdpSocket &udp_sender, int *keepalive_counter, int *fir
439442
qDebug()<<"Failed to recv data";
440443
return 1;
441444
}
442-
print_packet("[Keepalive2_file recv",recv_packet,40);
445+
print_packet("[Keepalive2_file recv]",recv_packet,40);
443446

444447
if (recv_packet[0] == 0x07) {
445448
if (recv_packet[2] == 0x10) {
@@ -461,7 +464,7 @@ int DogCom::keepalive_2(QUdpSocket &udp_sender, int *keepalive_counter, int *fir
461464
(*keepalive_counter)++;
462465
udp_sender.writeDatagram((const char*)keepalive_2_packet,40,*server_address,port_dest);
463466

464-
print_packet("[Keepalive2_A sent",keepalive_2_packet,40);
467+
print_packet("[Keepalive2_A sent]",keepalive_2_packet,40);
465468

466469
udp_sender.waitForReadyRead();
467470
if(udp_sender.readDatagram((char*)recv_packet,1024,server_address,&port_dest)<=0){
@@ -487,15 +490,15 @@ int DogCom::keepalive_2(QUdpSocket &udp_sender, int *keepalive_counter, int *fir
487490
(*keepalive_counter)++;
488491
udp_sender.writeDatagram((const char*)keepalive_2_packet,40,*server_address,port_dest);
489492

490-
print_packet("[Keepalive2_C sent",keepalive_2_packet,40);
493+
print_packet("[Keepalive2_C sent]",keepalive_2_packet,40);
491494

492495
udp_sender.waitForReadyRead();
493496
if(udp_sender.readDatagram((char*)recv_packet,1024,server_address,&port_dest)<=0){
494497
qDebug()<<udp_sender.error();
495498
qDebug()<<"Failed to recv data";
496499
return 1;
497500
}
498-
print_packet("[Keepalive2_D recv",recv_packet,40);
501+
print_packet("[Keepalive2_D recv]",recv_packet,40);
499502

500503
if (recv_packet[0] == 0x07) {
501504
if (recv_packet[2] != 0x28) {

images/icon.ico

264 KB
Binary file not shown.

images/offline.png

-10.9 KB
Loading

images/online.png

528 Bytes
Loading

main.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
// 设定single application的继承类
2-
#define QAPPLICATION_CLASS QApplication
3-
#include "mainwindow.h"
1+
#include "mainwindow.h"
42
#include <singleapplication.h>
3+
#include <QTranslator>
54

65
int main(int argc, char *argv[])
76
{
87
Q_INIT_RESOURCE(DrCOM_JLU_Qt);
98
SingleApplication a(argc,argv);
109
SingleApplication::setQuitOnLastWindowClosed(false);
10+
11+
QTranslator translator;
12+
translator.load(":/ts/DrCOM_zh_CN.qm");
13+
a.installTranslator(&translator);
14+
1115
MainWindow w;
1216
QObject::connect(&a,&SingleApplication::instanceStarted,[&w](){
1317
w.ShowLoginWindow();

mainwindow.cpp

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ MainWindow::MainWindow(QWidget *parent) :
2121
{
2222
ui->setupUi(this);
2323

24+
CURR_STATE=STATE_OFFLINE;
25+
2426
// 记住窗口大小功能
2527
QSettings settings;
2628
restoreGeometry(settings.value("mainWindowGeometry").toByteArray());
2729

2830
// 获取mac地址
2931
foreach(QNetworkInterface i, QNetworkInterface::allInterfaces()){
3032
if(!i.flags().testFlag(QNetworkInterface::IsLoopBack)){
31-
// qDebug()<<i<<endl;
3233
ui->comboBoxMAC->addItem(i.hardwareAddress()+i.name());
3334
}
3435
}
@@ -41,7 +42,7 @@ MainWindow::MainWindow(QWidget *parent) :
4142
logOutAction=new QAction(tr("&Logout"),this);
4243
connect(logOutAction,&QAction::triggered,this,&MainWindow::UserLogOut);
4344
quitAction=new QAction(tr("&Quit"),this);
44-
connect(quitAction,&QAction::triggered,qApp,&QCoreApplication::quit);
45+
connect(quitAction,&QAction::triggered,qApp,&QApplication::quit);
4546
// 新建菜单
4647
trayIconMenu=new QMenu(this);
4748
trayIconMenu->addAction(restoreAction);
@@ -59,6 +60,14 @@ MainWindow::MainWindow(QWidget *parent) :
5960
// 显示出来托盘图标
6061
trayIcon->show();
6162

63+
// 创建窗口菜单
64+
aboutAction=new QAction(tr("&About"),this);
65+
connect(aboutAction,&QAction::triggered,this,&MainWindow::AboutDrcom);
66+
windowMenu=new QMenu(tr("&Help"),this);
67+
windowMenu->addAction(aboutAction);
68+
windowMenu->addAction(logOutAction);
69+
ui->menuBar->addMenu(windowMenu);
70+
6271
// 读取配置文件
6372
LoadSettings();
6473

@@ -68,6 +77,8 @@ MainWindow::MainWindow(QWidget *parent) :
6877
this, &MainWindow::HandleOffline);
6978
connect(dogcomController, &DogcomController::HaveLoggedIn,
7079
this, &MainWindow::HandleLoggedIn);
80+
connect(dogcomController,&DogcomController::HaveObtainedIp,
81+
this,&MainWindow::HandleIpAddress);
7182

7283
// 验证手动输入的mac地址
7384
macValidator=new QRegExpValidator(QRegExp("[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}"));
@@ -78,13 +89,21 @@ MainWindow::MainWindow(QWidget *parent) :
7889

7990
// 自动登录功能
8091
if(auto_login){
81-
ui->pushButtonLogin->click();
92+
emit ui->pushButtonLogin->click();
8293
}
8394
}
8495

96+
void MainWindow::AboutDrcom(){
97+
QDesktopServices::openUrl(QUrl("https://github.com/code4lala/drcom-jlu-qt"));
98+
}
99+
85100
void MainWindow::closeEvent(QCloseEvent *event){
86101
QSettings settings;
87102
settings.setValue("mainWindowGeometry", saveGeometry());
103+
// 未登录时直接关闭窗口就退出
104+
if(CURR_STATE==STATE_OFFLINE){
105+
QApplication::quit();
106+
}
88107
}
89108

90109
void MainWindow::ShowLoginWindow(){
@@ -188,16 +207,17 @@ void MainWindow::on_pushButtonLogin_clicked()
188207
if(!account.compare("")
189208
||!password.compare("")
190209
||!mac_addr.compare("")){
191-
QMessageBox::warning(this, APP_NAME, "Input can not be empty!");
210+
QMessageBox::warning(this, APP_NAME, tr("Input can not be empty!"));
192211
return;
193212
}
194213
if(mac_addr.length()!=17){
195-
QMessageBox::warning(this, APP_NAME, "Illegal MAC address!");
214+
QMessageBox::warning(this, APP_NAME, tr("Illegal MAC address!"));
196215
return;
197216
}
198217
// 输入无误,执行登录操作
199218
// 先禁用输入框和按钮
200219
SetDisableInput(true);
220+
CURR_STATE=STATE_LOGGING;
201221
dogcomController->Login(account,password,mac_addr);
202222
}
203223

@@ -252,10 +272,11 @@ void MainWindow::GetInputs(){
252272

253273
void MainWindow::HandleOffline(int reason)
254274
{
255-
ui->pushButtonLogin->setText("Login");
275+
CURR_STATE=STATE_OFFLINE;
276+
ui->pushButtonLogin->setText(tr("Login"));
256277
switch (reason) {
257278
case OFF_USER_LOGOUT:{
258-
QMessageBox::critical(this,tr("Logout succeed"),tr("Logout succeed"));
279+
QMessageBox::information(this,tr("Logout succeed"),tr("Logout succeed"));
259280
break;
260281
}
261282
case OFF_BIND_FAILED:{
@@ -314,10 +335,6 @@ void MainWindow::HandleOffline(int reason)
314335
QMessageBox::critical(this,tr("You have been offline"),tr("Time out, please check your connection"));
315336
break;
316337
}
317-
case OFF_DHCP_LOGIN_FAILED:{
318-
QMessageBox::critical(this,tr("Login failed"),tr("Qt reported that it can not receive packets from the server but I don't think so"));
319-
break;
320-
}
321338
case OFF_UNKNOWN:
322339
default:
323340
QMessageBox::critical(this, tr("You have been offline"), tr("Unknow reason"));
@@ -340,6 +357,7 @@ void MainWindow::HandleOffline(int reason)
340357

341358
void MainWindow::HandleLoggedIn()
342359
{
360+
CURR_STATE=STATE_ONLINE;
343361
// 显示欢迎页
344362
QDesktopServices::openUrl(QUrl("http://login.jlu.edu.cn/notice.php"));
345363
// 登录成功,保存密码
@@ -357,7 +375,7 @@ void MainWindow::HandleLoggedIn()
357375

358376
void MainWindow::HandleIpAddress(const QString &ip)
359377
{
360-
ui->pushButtonLogin->setText(ip);
378+
ui->labelIp->setText(ip);
361379
}
362380

363381
void MainWindow::UserLogOut()

mainwindow.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public slots:
4343
private:
4444
Ui::MainWindow *ui;
4545
QString SETTINGS_FILE_NAME="DrCOM_JLU_Qt.ini";
46-
const QString CUSTOM_MAC="custom (format: 1A:2B:3C:4D:5E:6F case insensitive)";
47-
const QString APP_NAME="DrCOM JLU version";
46+
const QString CUSTOM_MAC=tr("custom (format: 1A:2B:3C:4D:5E:6F case insensitive)");
47+
const QString APP_NAME=tr("DrCOM JLU Qt version");
4848
const QString
4949
idAccount="account",
5050
idPassword="password",
@@ -56,12 +56,20 @@ public slots:
5656
QString account,password,mac_addr;
5757
bool remember,auto_login;
5858

59+
// 用于在未登录时关闭窗口就退出
60+
int CURR_STATE;
61+
5962
QRegExpValidator *macValidator;
6063
DogcomController *dogcomController;
6164

6265
// 设置托盘中的注销按钮的可用性
6366
void DisableLogOutButton(bool yes);
6467

68+
// 窗口菜单
69+
QAction *aboutAction;
70+
QMenu *windowMenu;
71+
void AboutDrcom();
72+
6573
// 托盘图标
6674
QAction *restoreAction;
6775
QAction *logOutAction;

0 commit comments

Comments
 (0)