From a95439400467e3ca2681554e603b8a9a3c2c9b1d Mon Sep 17 00:00:00 2001 From: Msy-msu <2200456405@qq.com> Date: Sun, 8 Jan 2023 00:28:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=9F=BA=E6=9C=AC=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TcpClient.pro.user | 185 ++++++++++++------------------------------- datamanager.cpp | 193 +++++++++++++++++++++++++-------------------- login.cpp | 5 +- login.ui | 24 ++++-- mainwidget.cpp | 85 +++++++++++++------- 5 files changed, 233 insertions(+), 259 deletions(-) diff --git a/TcpClient.pro.user b/TcpClient.pro.user index 72e5c64..3980f8d 100644 --- a/TcpClient.pro.user +++ b/TcpClient.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -85,9 +85,6 @@ true - - true - @@ -97,52 +94,12 @@ Desktop Qt 6.2.3 MinGW 64-bit Desktop Qt 6.2.3 MinGW 64-bit qt.qt6.623.win64_mingw_kit - 0 + 2 0 0 - 0 - C:\Users\25761\Desktop\build-TcpClient-Desktop_Qt_6_2_3_MinGW_64_bit-Debug - C:/Users/25761/Desktop/build-TcpClient-Desktop_Qt_6_2_3_MinGW_64_bit-Debug - - - true - QtProjectManager.QMakeBuildStep - false - - - - true - Qt4ProjectManager.MakeStep - - 2 - Build - Build - ProjectExplorer.BuildSteps.Build - - - - true - Qt4ProjectManager.MakeStep - clean - - 1 - Clean - Clean - ProjectExplorer.BuildSteps.Clean - - 2 - false - - false - - Debug - Qt4ProjectManager.Qt4BuildConfiguration - 2 - - - C:\Users\25761\Desktop\build-TcpClient-Desktop_Qt_6_2_3_MinGW_64_bit-Release - C:/Users/25761/Desktop/build-TcpClient-Desktop_Qt_6_2_3_MinGW_64_bit-Release + C:\Users\25761\Desktop\Error-in-"-Util.asciify("build-C:\Users\25761\Desktop\tcpclient\TcpClient-Desktop_Qt_6_2_3_MinGW_64_bit-Release")":-SyntaxError:-Expected-token-`)' + C:/Users/25761/Desktop/Error-in-"-Util.asciify("build-C:/Users/25761/Desktop/tcpclient/TcpClient-Desktop_Qt_6_2_3_MinGW_64_bit-Release")":-SyntaxError:-Expected-token-`)' true @@ -180,10 +137,10 @@ 0 0 - + 0 - C:\Users\25761\Desktop\build-TcpClient-Desktop_Qt_6_2_3_MinGW_64_bit-Profile - C:/Users/25761/Desktop/build-TcpClient-Desktop_Qt_6_2_3_MinGW_64_bit-Profile + C:\Users\25761\Desktop\Error-in-"-Util.asciify("build-C:\Users\25761\Desktop\tcpclient\TcpClient-Desktop_Qt_6_2_3_MinGW_64_bit-Profile")":-SyntaxError:-Expected-token-`)' + C:/Users/25761/Desktop/Error-in-"-Util.asciify("build-C:/Users/25761/Desktop/tcpclient/TcpClient-Desktop_Qt_6_2_3_MinGW_64_bit-Profile")":-SyntaxError:-Expected-token-`)' true @@ -222,6 +179,46 @@ 0 0 + + 0 + C:\Users\25761\Desktop\build-TCPClient-Desktop_Qt_6_2_3_MinGW_64_bit-111 + C:/Users/25761/Desktop/build-TCPClient-Desktop_Qt_6_2_3_MinGW_64_bit-111 + + + true + QtProjectManager.QMakeBuildStep + false + + + + true + Qt4ProjectManager.MakeStep + + 2 + Build + Build + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + clean + + 1 + Clean + Clean + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + 111 + Qt4ProjectManager.Qt4BuildConfiguration + 2 + 3 @@ -250,98 +247,14 @@ true false true - C:/Users/25761/Desktop/build-TcpClient-Desktop_Qt_6_2_3_MinGW_64_bit-Debug - - 1 - - - - ProjectExplorer.Project.Target.1 - - Desktop - Desktop Qt 6.2.3 MSVC2019 64bit - Desktop Qt 6.2.3 MSVC2019 64bit - qt.qt6.623.win64_msvc2019_64_kit - 0 - 0 - 0 - - 0 - C:\Users\25761\Desktop\Error-in-"-Util.asciify("build-C:\Users\25761\Desktop\tcpclient\TcpClient-Desktop_Qt_6_2_3_MSVC2019_64bit-Profile")":-SyntaxError:-Expected-token-`)' - C:/Users/25761/Desktop/Error-in-"-Util.asciify("build-C:/Users/25761/Desktop/tcpclient/TcpClient-Desktop_Qt_6_2_3_MSVC2019_64bit-Profile")":-SyntaxError:-Expected-token-`)' - - - true - QtProjectManager.QMakeBuildStep - false - - - - true - Qt4ProjectManager.MakeStep - - 2 - Build - Build - ProjectExplorer.BuildSteps.Build - - - - true - Qt4ProjectManager.MakeStep - clean - - 1 - Clean - Clean - ProjectExplorer.BuildSteps.Clean - - 2 - false - - false - - Profile - Qt4ProjectManager.Qt4BuildConfiguration - 0 - 0 - 0 - - 1 - - - 0 - Deploy - Deploy - ProjectExplorer.BuildSteps.Deploy - - 1 - - false - ProjectExplorer.DefaultDeployConfiguration - - 1 - - true - true - true - - 2 - - Qt4ProjectManager.Qt4RunConfiguration:C:/Users/25761/Desktop/tcpclient/TcpClient.pro - C:/Users/25761/Desktop/tcpclient/TcpClient.pro - false - true - true - false - true + C:/Users/25761/Desktop/build-TCPClient-Desktop_Qt_6_2_3_MinGW_64_bit-111 1 ProjectExplorer.Project.TargetCount - 2 + 1 ProjectExplorer.Project.Updater.FileVersion diff --git a/datamanager.cpp b/datamanager.cpp index 132da68..95e8bc9 100644 --- a/datamanager.cpp +++ b/datamanager.cpp @@ -1,21 +1,20 @@ #include "datamanager.h" -//DataManager* DataManager::p_this; +// DataManager* DataManager::p_this; DataManager::DataManager(QObject *parent) : QObject{parent} { qDebug() << "进入DataManager的构造函数"; tcpclient = TCPClient::getInstance(); - connect(tcpclient,&TCPClient::TCPClientConnected,this,&DataManager::TCPClientConnected); - connect(tcpclient,&TCPClient::TCPClientDisconnected,this,&DataManager::TCPClientDisconnected); - connect(tcpclient,&TCPClient::recv_data,this,&DataManager::recv_from_server); + connect(tcpclient, &TCPClient::TCPClientConnected, this, &DataManager::TCPClientConnected); + connect(tcpclient, &TCPClient::TCPClientDisconnected, this, &DataManager::TCPClientDisconnected); + connect(tcpclient, &TCPClient::recv_data, this, &DataManager::recv_from_server); tcpclient->configAndrun(); - //tcpclient->setCallBack(recv_from_server); + // tcpclient->setCallBack(recv_from_server); protocol = Protocol::getInstance(); - //p_this = this; - + // p_this = this; } -DataManager* DataManager::getInstance() +DataManager *DataManager::getInstance() { static DataManager datamanager_instance; return &datamanager_instance; @@ -32,7 +31,7 @@ void DataManager::TCPClientDisconnected() { connect_flag = false; qDebug() << "emit disconnect_from_server successfully"; - //emit p_this->disconnect_from_server(); + // emit p_this->disconnect_from_server(); emit disconnect_from_server(); } @@ -40,114 +39,136 @@ void DataManager::recv_from_server(QByteArray data) { QByteArray data_decrypt; data_decrypt = protocol->data_decrypt(data); - QJsonDocument json_data = QJsonDocument::fromJson(data_decrypt); - if (json_data["type"] == "message") + QList list = data_decrypt.split('\n'); + for (int i = 0; i < list.count(); i++) { - QString data,sendname; - sendname = json_data["sendname"].toString(); - data = json_data["data"].toString(); - emit update_text(sendname,data); - qDebug() << "收到message类型的信息" << sendname; - } - else if (json_data["type"] == "file") - { - QString filename,file_data,sendname; - sendname = json_data["sendname"].toString(); - filename = json_data["name"].toString(); - file_data = json_data["data"].toString(); - if(json_data["to_user"].toString().isEmpty()) + QJsonDocument json_data = QJsonDocument::fromJson(list[i]); + if (json_data["type"] == "message") { - emit update_file(sendname,filename,file_data); - qDebug() << "收到群发的文件" << sendname; + QString data, sendname; + sendname = json_data["sendname"].toString(); + data = json_data["data"].toString(); + emit update_text(sendname, data); + qDebug() << "收到message类型的信息" << sendname; + } + else if (json_data["type"] == "file") + { + QString filename, file_data, sendname; + sendname = json_data["sendname"].toString(); + filename = json_data["name"].toString(); + file_data = json_data["data"].toString(); + if (json_data["to_user"].toString().isEmpty()) + { + emit update_file(sendname, filename, file_data); + qDebug() << "收到群发的文件" << sendname; + } + else + { + emit update_file_private(sendname, filename, file_data); + qDebug() << "收到私发的文件" << sendname; + } + } + else if (json_data["type"] == "online_users") + { + int count; + QList online_users; + count = json_data["count"].toInt(); + online_users = json_data["users"].toVariant().toStringList(); + emit update_users(count, online_users); + qDebug() << "收到更新在线用户列表的信息" << online_users; + } + else if (json_data["type"] == "response") + { + if (json_data["action"] == "register") + { + bool register_flag; + QJsonObject json = json_data["content"].toObject(); + QString result = json["result"].toString(); + if(result == "true") + { + register_flag = true; + } + else + { + register_flag = false; + } + emit signup_response(register_flag); + qDebug() << "收到服务器响应的注册信息" << register_flag; + } + else if (json_data["action"] == "signin") + { + bool signin_flag; + QJsonObject json = json_data["content"].toObject(); + QString result = json["result"].toString(); + if(result == "true") + { + signin_flag = true; + } + else + { + signin_flag = false; + } + emit signin_response(signin_flag); + qDebug() << "收到服务器响应的登录信息" << signin_flag; + } } else { - emit update_file_private(sendname,filename,file_data); - qDebug() << "收到私发的文件" << sendname; + qDebug() << "收到未知类型的数据" << list[i]; + return; } } - else if (json_data["type"] == "online_users") - { - int count; - QList online_users; - count = json_data["count"].toInt(); - online_users = json_data["users"].toVariant().toStringList(); - emit update_users(count,online_users); - qDebug() << "收到更新在线用户列表的信息" << online_users; - } - else if (json_data["type"] == "response") - { - if(json_data["action"] == "register") - { - bool register_flag; - register_flag = json_data["content"].toBool(); - emit signup_response(register_flag); - qDebug() << "收到服务器响应的注册信息" << register_flag; - } - else if(json_data["action"] == "signin") - { - bool signin_flag; - signin_flag = json_data["content"].toBool(); - emit signin_response(signin_flag); - qDebug() << "收到服务器响应的登录信息" << signin_flag; - } - } - else - { - qDebug() << "收到未知类型的数据"; - return; - } } -void DataManager::Register(QString username,QString password) +void DataManager::Register(QString username, QString password) { - QByteArray data,data_encrypt; - data = protocol->data_maker(Protocol::REQUEST,QString("register"),QList{username,password}); + QByteArray data, data_encrypt; + data = protocol->data_maker(Protocol::REQUEST, QString("register"), QList{"user", username, "password", password}); data_encrypt = protocol->data_encrypt(data); tcpclient->sendToserver(data_encrypt); - qDebug() << "发送注册信息" <data_maker(Protocol::REQUEST,QString("signin"),QList{username,password}); + QByteArray data, data_encrypt; + data = protocol->data_maker(Protocol::REQUEST, QString("signin"), QList{"user", username, "password", password}); data_encrypt = protocol->data_encrypt(data); tcpclient->sendToserver(data_encrypt); - qDebug() << "发送登录信息" <data_maker(Protocol::MESSAGE,sendname,text); + QByteArray data, data_encrypt; + data = protocol->data_maker(Protocol::MESSAGE, sendname, text); data_encrypt = protocol->data_encrypt(data); tcpclient->sendToserver(data_encrypt); - qDebug() << "群发文本信息" <data_maker(Protocol::MESSAGE,sendname,to_user,text); + QByteArray data, data_encrypt; + data = protocol->data_maker(Protocol::MESSAGE, sendname, to_user, text); data_encrypt = protocol->data_encrypt(data); tcpclient->sendToserver(data_encrypt); - qDebug() << "私发文本信息" << sendname <data_maker(Protocol::FILE,sendname,filename,QString(file)); + QByteArray data, data_encrypt; + data = protocol->data_maker(Protocol::FILE, sendname, filename, QString(file)); data_encrypt = protocol->data_encrypt(data); tcpclient->sendToserver(data_encrypt); qDebug() << "群发文件" << sendname; } -void DataManager::send_file_private(QString sendname,QString filename,QByteArray file,QString name) +void DataManager::send_file_private(QString sendname, QString filename, QByteArray file, QString name) { - QByteArray data,data_encrypt; - data = protocol->data_maker(Protocol::FILE,name,sendname,filename,QString(file)); + QByteArray data, data_encrypt; + data = protocol->data_maker(Protocol::FILE, name, sendname, filename, QString(file)); data_encrypt = protocol->data_encrypt(data); tcpclient->sendToserver(data_encrypt); qDebug() << "私发文件" << sendname << name; @@ -155,17 +176,17 @@ void DataManager::send_file_private(QString sendname,QString filename,QByteArray void DataManager::account_cancellation(QString username) { - QByteArray data,data_encrypt; - data = protocol->data_maker(Protocol::REQUEST,QString("cancellation"),username); + QByteArray data, data_encrypt; + data = protocol->data_maker(Protocol::REQUEST, QString("cancellation"), QList{"name",username}); data_encrypt = protocol->data_encrypt(data); tcpclient->sendToserver(data_encrypt); - qDebug() << "发送注销信息" << username; + qDebug() << "发送注销信息" << data; } void DataManager::update_namelist() { - QByteArray data,data_encrypt; - data = protocol->data_maker(Protocol::REQUEST,QString("update_namelist")); + QByteArray data, data_encrypt; + data = protocol->data_maker(Protocol::REQUEST, QString("update_namelist")); data_encrypt = protocol->data_encrypt(data); tcpclient->sendToserver(data_encrypt); qDebug() << "发送更新在线用户列表信息"; diff --git a/login.cpp b/login.cpp index 56cdff0..0cd4b60 100644 --- a/login.cpp +++ b/login.cpp @@ -170,7 +170,7 @@ void Login::signin_response(bool result) connect(mainwidget,&MainWidget::send_to_user,datamanager,&DataManager::send_to_user); connect(mainwidget,&MainWidget::send_file_private,datamanager,&DataManager::send_file_private); connect(mainwidget,&MainWidget::send_file_to_all,datamanager,&DataManager::send_file_to_all); - connect(mainwidget,&MainWidget::exit_client,datamanager,&DataManager::update_namelist); + //connect(mainwidget,&MainWidget::exit_client,datamanager,&DataManager::update_namelist); connect(mainwidget,&MainWidget::account_cancellation,datamanager,&DataManager::account_cancellation); connect(datamanager,&DataManager::update_text,mainwidget,&MainWidget::update_text); connect(datamanager,&DataManager::update_file,mainwidget,&MainWidget::update_file); @@ -193,6 +193,9 @@ void Login::signup_response(bool result) { QString information = QString("请妥善保存您的用户名和密码!"); QMessageBox::information(signup,tr("注册成功"),information); + signup->close(); + this->show(); + } else { diff --git a/login.ui b/login.ui index 02b9b22..a585552 100644 --- a/login.ui +++ b/login.ui @@ -30,14 +30,18 @@ - background-color: qlineargradient(spread:pad, x1:0, y1:1, x2:0, y2:0, + QPushButton +{background-color:qlineargradient(spread:pad, x1:0, y1:1, x2:0, y2:0, stop:1 rgba(64, 145, 252, 255), stop:0 rgba(220, 255, 255, 255)); color: rgb(255, 255, 255); +border:0px groove gray; +border-radius:7px; +padding:2px 4px; +font: 14pt "Candara";} -border:0px groove gray;border-radius: -7px;padding:2px 4px; -font: 14pt "Candara"; +QPushButton:hover +{background-color:#999999;} Sign in @@ -84,14 +88,18 @@ font: 12pt "Candara"; - background-color: qlineargradient(spread:pad, x1:0, y1:1, x2:0, y2:0, + QPushButton +{background-color:qlineargradient(spread:pad, x1:0, y1:1, x2:0, y2:0, stop:1 rgba(64, 145, 252, 255), stop:0 rgba(220, 255, 255, 255)); color: rgb(255, 255, 255); +border:0px groove gray; +border-radius:7px; +padding:2px 4px; +font: 14pt "Candara";} -border:0px groove gray;border-radius: -7px;padding:2px 4px; -font: 14pt "Candara"; +QPushButton:hover +{background-color:#999999;} Sign up diff --git a/mainwidget.cpp b/mainwidget.cpp index 7f855ea..0b00c73 100644 --- a/mainwidget.cpp +++ b/mainwidget.cpp @@ -40,41 +40,36 @@ void MainWidget::on_send_text_clicked() return; else { - QString currentTime = QTime::currentTime().toString("H:mm A"); - ui->textEdit->append("" + username + " [" + currentTime + "] send a file \"" + filename + "\""); - - QString name = ui->msg_edit->text(); - if(name.isEmpty()) //群发 + QString text = ui->msg_edit->text(); + if(text.isEmpty()) //群发 { + QString currentTime = QTime::currentTime().toString("H:mm A"); + ui->textEdit->append("" + username + " [" + currentTime + "] send a file \"" + filename + "\""); this->sendFile(filePath,filename); } else //私发文件 { - this->sendFile(filePath,filename,name); - } - QProgressDialog *progressDlg = new QProgressDialog(this); - progressDlg->setWindowModality(Qt::WindowModal); - progressDlg->setMinimumDuration(0); - progressDlg->setAttribute(Qt::WA_DeleteOnClose, true); - progressDlg->setWindowTitle(tr("上传文件")); - progressDlg->setLabelText(tr("正在上传......")); - progressDlg->setCancelButtonText(tr("取消")); - progressDlg->setRange(0, 8000); - for (int i = 1; i < 8000; i++) - { - progressDlg->setValue(i); - if (progressDlg->wasCanceled()) + ui->msg_edit->clear(); + int n = text.indexOf(' '); + QString name = text.mid(1,n-1); + if(name == username) { - break; + QMessageBox::critical(this,"错误","不能自己给自己发送文件!"); + } + else + { + QString currentTime = QTime::currentTime().toString("H:mm A"); + ui->textEdit->append(QString("" + username + " [" + currentTime + "] send a private file to %1\"" + + filename + "\"").arg(name)); + this->sendFile(filePath,filename,name); } } - progressDlg->close(); } } void MainWidget::on_save_btn_clicked() { - QFile file("memory.txt"); + QFile file("./txt/memory.txt"); if (file.exists()) //文件存在 { QMessageBox::StandardButton reply; @@ -125,18 +120,30 @@ void MainWidget::on_send_btn_clicked() return; } ui->msg_edit->clear(); - QString currentTime = QTime::currentTime().toString("H:m A"); - ui->textEdit->append("" + username + " [" + currentTime + "] : " + text + ""); + if(text[0] == '@') { int n = text.indexOf(' '); QString to_user = text.mid(1,n-1); QString insert = "tell you quietly:"; - text = insert + text.mid(n,-1); - emit send_to_user(username,to_user,text); + QString data_text = insert + text.mid(n,-1); + QString text_self = QString("私发给%1:%2").arg(to_user).arg(text.mid(n)); + qDebug() << text_self; + if(to_user == username) + { + QMessageBox::critical(this,"错误","不能自己给自己发送消息!"); + } + else + { + QString currentTime = QTime::currentTime().toString("H:m A"); + ui->textEdit->append("" + username + " [" + currentTime + "] : " + text_self + ""); + emit send_to_user(username,to_user,data_text); + } } else { + QString currentTime = QTime::currentTime().toString("H:m A"); + ui->textEdit->append("" + username + " [" + currentTime + "] : " + text + ""); emit send_text(username,text); } } @@ -168,7 +175,6 @@ void MainWidget::on_exit_btn_clicked() } } - void MainWidget::on_stop_clicked() { QMessageBox::StandardButton response; @@ -176,6 +182,7 @@ void MainWidget::on_stop_clicked() if(response == QMessageBox::Yes) { + qDebug() << "点击注销账户按钮"; emit exit_client(); emit account_cancellation(username); this->close(); @@ -200,10 +207,11 @@ void MainWidget::update_file(QString sendname,QString filename,QString file_data if(response == QMessageBox::Yes) { readFile(filename,file_data.toUtf8()); + response = QMessageBox::question(this,"接收文件","接收成功!"); } else { - QMessageBox::information(this,"消息","未接收该文件!"); + QMessageBox::information(this,"接收文件","未接收该文件!"); } } @@ -261,6 +269,27 @@ void MainWidget::sendFile(QString filePath,QString filename,QString name) QMessageBox::warning(this,"错误","无法打开文件!"); return; } + else + { + QProgressDialog *progressDlg = new QProgressDialog(this); + progressDlg->setWindowModality(Qt::WindowModal); + progressDlg->setMinimumDuration(0); + progressDlg->setAttribute(Qt::WA_DeleteOnClose, true); + progressDlg->setWindowTitle(tr("上传文件")); + progressDlg->setLabelText(tr("正在上传......")); + progressDlg->setCancelButtonText(tr("取消")); + progressDlg->setRange(0, 12000); + for (int i = 1; i < 12000; i++) + { + progressDlg->setValue(i); + if (progressDlg->wasCanceled()) + { + break; + } + } + progressDlg->close(); + QMessageBox::information(this,"发送文件","发送成功!"); + } //打开文件流,以写的方式打开 QDataStream out(&block,QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_5_8);