diff --git a/TcpClient.pro.user b/TcpClient.pro.user index 585b555..ec9c96d 100644 --- a/TcpClient.pro.user +++ b/TcpClient.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -8,7 +8,7 @@ ProjectExplorer.Project.ActiveTarget - 0 + 1 ProjectExplorer.Project.EditorSettings @@ -102,8 +102,93 @@ 0 0 - C:\Users\25761\Desktop\Error-in-"-Util.asciify("build-C:\Users\25761\Desktop\tcpclient\TcpClient-Desktop_Qt_6_2_3_MSVC2019_64bit-Debug")":-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-Debug")":-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-`)' + 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/Error-in-"-Util.asciify("build-C:/Users/25761/Desktop/tcpclient/TcpClient-Desktop_Qt_6_2_3_MSVC2019_64bit-Profile")":-SyntaxError:-Expected-token-`)' + + 1 + + + + ProjectExplorer.Project.Target.1 + + Desktop + Desktop Qt 6.2.3 MinGW 64-bit + Desktop Qt 6.2.3 MinGW 64-bit + qt.qt6.623.win64_mingw_kit + 0 + 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 @@ -141,8 +226,8 @@ 2 - C:\Users\25761\Desktop\Error-in-"-Util.asciify("build-C:\Users\25761\Desktop\tcpclient\TcpClient-Desktop_Qt_6_2_3_MSVC2019_64bit-Release")":-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-Release")":-SyntaxError:-Expected-token-`)' + 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 true @@ -182,8 +267,8 @@ 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-`)' + 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 true @@ -243,21 +328,21 @@ 2 - Qt4ProjectManager.Qt4RunConfiguration:C:\Users\25761\Desktop\tcpclient\TcpClient.pro - C:\Users\25761\Desktop\tcpclient\TcpClient.pro + 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/Error-in-"-Util.asciify("build-C:/Users/25761/Desktop/tcpclient/TcpClient-Desktop_Qt_6_2_3_MSVC2019_64bit-Debug")":-SyntaxError:-Expected-token-`)' + C:/Users/25761/Desktop/build-TcpClient-Desktop_Qt_6_2_3_MinGW_64_bit-Debug 1 ProjectExplorer.Project.TargetCount - 1 + 2 ProjectExplorer.Project.Updater.FileVersion diff --git a/datamanager.cpp b/datamanager.cpp index c6d81f5..b8bd081 100644 --- a/datamanager.cpp +++ b/datamanager.cpp @@ -36,7 +36,7 @@ void DataManager::TCPClientDisconnected() emit disconnect_from_server(); } -void DataManager::recv_from_server(QByteArray &data) +void DataManager::recv_from_server(QByteArray data) { QByteArray data_decrypt; data_decrypt = protocol->data_decrypt(data); @@ -54,7 +54,14 @@ void DataManager::recv_from_server(QByteArray &data) sendname = json_data["sendname"].toString(); filename = json_data["name"].toString(); file_data = json_data["data"].toString(); - emit update_file(sendname,filename,file_data); + if(json_data["to_user"].toString().isEmpty()) + { + emit update_file(sendname,filename,file_data); + } + else + { + emit update_file_private(sendname,filename,file_data); + } } else if (json_data["type"] == "online_users") { @@ -120,7 +127,7 @@ void DataManager::send_to_user(QString sendname,QString to_user,QString text) void DataManager::send_file_to_all(QString sendname,QString filename,QByteArray file) { QByteArray data,data_encrypt; - data = protocol->data_maker(Protocol::FILE,sendname,filename,file); + data = protocol->data_maker(Protocol::FILE,sendname,filename,QString(file)); data_encrypt = protocol->data_encrypt(data); tcpclient->sendToserver(data_encrypt); } @@ -128,7 +135,7 @@ void DataManager::send_file_to_all(QString sendname,QString filename,QByteArray 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,file); + data = protocol->data_maker(Protocol::FILE,name,sendname,filename,QString(file)); data_encrypt = protocol->data_encrypt(data); tcpclient->sendToserver(data_encrypt); } @@ -140,3 +147,11 @@ void DataManager::account_cancellation(QString username) data_encrypt = protocol->data_encrypt(data); tcpclient->sendToserver(data_encrypt); } + +void DataManager::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); +} diff --git a/datamanager.h b/datamanager.h index eef86fe..f569727 100644 --- a/datamanager.h +++ b/datamanager.h @@ -30,6 +30,7 @@ signals: void signup_response(bool result); void update_text(QString sendname,QString data); void update_file(QString sendname,QString filename,QString file_data); + void update_file_private(QString sendname,QString filename,QString file_data); void update_users(int count,QListonline_users); @@ -43,12 +44,13 @@ public slots: void send_to_user(QString sendname,QString to_user,QString text); void send_file_to_all(QString sendname,QString filename,QByteArray file); void send_file_private(QString sendname,QString filename,QByteArray file,QString name); + void update_namelist(); void account_cancellation(QString username); /******TCPClient******/ void TCPClientConnected(); void TCPClientDisconnected(); - void recv_from_server(QByteArray &data); + void recv_from_server(QByteArray data); }; diff --git a/login.cpp b/login.cpp index 0774a14..56cdff0 100644 --- a/login.cpp +++ b/login.cpp @@ -170,9 +170,11 @@ 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::account_cancellation,datamanager,&DataManager::account_cancellation); connect(datamanager,&DataManager::update_text,mainwidget,&MainWidget::update_text); connect(datamanager,&DataManager::update_file,mainwidget,&MainWidget::update_file); + connect(datamanager,&DataManager::update_file_private,mainwidget,&MainWidget::update_file_private); connect(datamanager,&DataManager::update_users,mainwidget,&MainWidget::update_users); connect(mainwidget,&MainWidget::exit_client,this,&Login::exit_client); this->hide(); diff --git a/mainwidget.cpp b/mainwidget.cpp index 56a0dc7..7f855ea 100644 --- a/mainwidget.cpp +++ b/mainwidget.cpp @@ -186,16 +186,79 @@ void MainWidget::on_stop_clicked() } } +void MainWidget::update_text(QString sendname,QString data) +{ + QString currentTime = QTime::currentTime().toString("H:m A"); + ui->textEdit->append(sendname + " [" + currentTime + "] : " + data); +} + +void MainWidget::update_file(QString sendname,QString filename,QString file_data) +{ + QMessageBox::StandardButton response; + QString content = QString("你收到了一个来自%1的群发文件%2,是否接收?").arg(sendname).arg(filename); + response = QMessageBox::question(this,"提示",content); + if(response == QMessageBox::Yes) + { + readFile(filename,file_data.toUtf8()); + } + else + { + QMessageBox::information(this,"消息","未接收该文件!"); + } +} + +void MainWidget::update_file_private(QString sendname,QString filename,QString file_data) +{ + QMessageBox::StandardButton response; + QString content = QString("你收到了%1私发给你的文件%2,是否接收?").arg(sendname).arg(filename); + response = QMessageBox::question(this,"提示",content); + if(response == QMessageBox::Yes) + { + readFile(filename,file_data.toUtf8()); + } + else + { + QMessageBox::information(this,"消息","未接收该文件!"); + } +} + +void MainWidget::update_users(int count,QListonline_users) +{ + ui->name_list->clear(); + QList::iterator it = online_users.begin(); + for(; it!=online_users.end(); ++it) + { + ui->name_list->addItem(*it); + } + +} + +bool MainWidget::eventFilter(QObject *target, QEvent *event) +{ + if(target == ui->msg_edit) //可替换 + { + if(event->type() == QEvent::KeyPress)//回车键 + { + QKeyEvent *k = static_cast(event); + + if(k->key() == Qt::Key_Return || k->key() == Qt::Key_Enter) + { + on_send_btn_clicked(); //替换为需要响应的函数事件,以这里的按钮为例 + return true; + } + } + } + return QWidget::eventFilter(target,event); +} + void MainWidget::sendFile(QString filePath,QString filename,QString name) { - //通过fileTcpSocket发送文件 - //发送的数据存入block中 QByteArray block; QFile file(filePath); //打开文件,定义错误提醒 if(!file.open(QIODevice::ReadOnly)){ - QMessageBox::warning(this,"","无法打开文件!"); + QMessageBox::warning(this,"错误","无法打开文件!"); return; } //打开文件流,以写的方式打开 @@ -220,39 +283,22 @@ void MainWidget::sendFile(QString filePath,QString filename,QString name) } } -void MainWidget::update_text(QString sendname,QString data) -{ - QString currentTime = QTime::currentTime().toString("H:m A"); - ui->textEdit->append(sendname + " [" + currentTime + "] : " + data); -} - -void MainWidget::update_file(QString sendname,QString filename,QString file_data) +void MainWidget::readFile(QString filename,QByteArray data) { -} - -void MainWidget::update_users(int count,QListonline_users) -{ - -} - -bool MainWidget::eventFilter(QObject *target, QEvent *event) -{ - if(target == ui->msg_edit) //可替换 - { - if(event->type() == QEvent::KeyPress)//回车键 - { - QKeyEvent *k = static_cast(event); - - if(k->key() == Qt::Key_Return || k->key() == Qt::Key_Enter) - { - on_send_btn_clicked(); //替换为需要响应的函数事件,以这里的按钮为例 - return true; - } - } + QString homePath = QDir::homePath(); + QString filePath = QFileDialog::getExistingDirectory(this, + tr("Open Directory"), + homePath, + QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); + QFile outFile(filePath + "/" +filename); + if(!outFile.open(QIODevice::WriteOnly)){ + QMessageBox::warning(this,"","错误: 无法打开文件."); + return; } - return QWidget::eventFilter(target,event); + outFile.write(data); + outFile.close(); + } - diff --git a/mainwidget.h b/mainwidget.h index eebd3f6..cab7a99 100644 --- a/mainwidget.h +++ b/mainwidget.h @@ -25,6 +25,7 @@ public: explicit MainWidget(QString username,QWidget *parent = nullptr); ~MainWidget(); void sendFile(QString filePath,QString filename,QString name = ""); + void readFile(QString filename,QByteArray data); bool eventFilter(QObject *target, QEvent *event); signals: @@ -45,6 +46,7 @@ private slots: public slots: void update_text(QString sendname,QString data); void update_file(QString sendname,QString filename,QString file_data); + void update_file_private(QString sendname,QString filename,QString file_data); void update_users(int count,QListonline_users); private: