进行了部分逻辑上的改动
This commit is contained in:
parent
c3eb7638da
commit
03fa52da27
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE QtCreatorProject>
|
||||
<!-- Written by QtCreator 6.0.2, 2023-01-09T18:43:00. -->
|
||||
<!-- Written by QtCreator 6.0.2, 2023-01-09T23:21:25. -->
|
||||
<qtcreator>
|
||||
<data>
|
||||
<variable>EnvironmentId</variable>
|
||||
|
|
|
@ -160,19 +160,19 @@ 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, QString(file));
|
||||
data = protocol->data_maker(Protocol::FILE, sendname, filename, QString(file.toHex()));
|
||||
data_encrypt = protocol->data_encrypt(data);
|
||||
tcpclient->sendToserver(data_encrypt);
|
||||
qDebug() << "群发文件" << data;
|
||||
qDebug() << "群发文件";
|
||||
}
|
||||
|
||||
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));
|
||||
data = protocol->data_maker(Protocol::FILE, name, sendname, filename, QString(file.toHex()));
|
||||
data_encrypt = protocol->data_encrypt(data);
|
||||
tcpclient->sendToserver(data_encrypt);
|
||||
qDebug() << "私发文件" << data;
|
||||
qDebug() << "私发文件";
|
||||
}
|
||||
|
||||
void DataManager::account_cancellation(QString username)
|
||||
|
|
20
login.cpp
20
login.cpp
|
@ -9,9 +9,11 @@ Login::Login(QWidget *parent) :
|
|||
ui->setupUi(this);
|
||||
this->setWindowIcon(QIcon(":/icon.png"));
|
||||
this->setTabOrder(ui->lineEdit_username,ui->lineEdit_password);
|
||||
this->setTabOrder(ui->lineEdit_password,ui->btn_signin);
|
||||
this->setWindowFlags(Qt::FramelessWindowHint); //无边框
|
||||
this->setAttribute(Qt::WA_TranslucentBackground);
|
||||
new_closebutton(ui->close_btn,":/close.png", QSize(30, 30));
|
||||
ui->btn_signin->installEventFilter(this);
|
||||
|
||||
/********datamanager********/
|
||||
datamanager = DataManager::getInstance();
|
||||
|
@ -267,3 +269,21 @@ void Login::new_closebutton(QPushButton* btn, QString position,QSize size)
|
|||
//设置图标
|
||||
btn->setIcon(pixmap);
|
||||
}
|
||||
|
||||
bool Login::eventFilter(QObject *target, QEvent *event)
|
||||
{
|
||||
if(target == ui->btn_signin) //可替换
|
||||
{
|
||||
if(event->type() == QEvent::KeyPress)//回车键
|
||||
{
|
||||
QKeyEvent *k = static_cast<QKeyEvent *>(event);
|
||||
|
||||
if(k->key() == Qt::Key_Return || k->key() == Qt::Key_Enter)
|
||||
{
|
||||
on_btn_signin_clicked(); //替换为需要响应的函数事件,以这里的按钮为例
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return QWidget::eventFilter(target,event);
|
||||
}
|
||||
|
|
1
login.h
1
login.h
|
@ -63,6 +63,7 @@ private:
|
|||
void new_closebutton(QPushButton* btn, QString position,QSize size);
|
||||
void zoom_up(QPushButton* btn);
|
||||
void zoom_down(QPushButton* btn);
|
||||
bool eventFilter(QObject *target, QEvent *event);
|
||||
|
||||
};
|
||||
|
||||
|
|
20
main.cpp
20
main.cpp
|
@ -1,11 +1,31 @@
|
|||
#include "login.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QDir>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
Login w;
|
||||
w.show();
|
||||
QString document_name("Document");
|
||||
QString record_name("ChatRecord");
|
||||
QDir dir(QDir::currentPath());
|
||||
QFile file("./ChatRecord/memory.txt");
|
||||
|
||||
if(!dir.exists(document_name))
|
||||
{
|
||||
dir.mkdir(document_name);
|
||||
qDebug()<<QString("文件夹%1创建成功!").arg(document_name);
|
||||
}
|
||||
if(!dir.exists(record_name))
|
||||
{
|
||||
dir.mkdir(record_name);
|
||||
qDebug()<<QString("文件夹%1创建成功!").arg(record_name);
|
||||
}
|
||||
if(!file.exists())
|
||||
{
|
||||
file.open(QIODevice::WriteOnly | QIODevice::Append);
|
||||
}
|
||||
return a.exec();
|
||||
}
|
||||
|
|
130
mainwidget.cpp
130
mainwidget.cpp
|
@ -13,6 +13,22 @@ MainWidget::MainWidget(QString username,QWidget *parent) :
|
|||
ui->name_list->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
|
||||
ui->name_list->verticalScrollBar()->setStyleSheet("QScrollBar{width:16px;}");
|
||||
ui->textEdit->setReadOnly(true);
|
||||
ui->textEdit->setFocusPolicy(Qt::NoFocus);
|
||||
//防止QTextBrowser中的内容被重定向
|
||||
ui->textEdit->setOpenLinks(false);
|
||||
ui->textEdit->setOpenExternalLinks(false);
|
||||
connect(ui->textEdit, &QTextBrowser::anchorClicked, this, [=](const QUrl &link)
|
||||
{
|
||||
auto path = link.path();
|
||||
qDebug() << path;
|
||||
path.remove(0, 1);
|
||||
qDebug() << path;
|
||||
QFileInfo info(path);
|
||||
if(info.isDir())
|
||||
QDesktopServices::openUrl(link);
|
||||
else if(info.isFile())
|
||||
QDesktopServices::openUrl(QUrl::fromLocalFile(path));
|
||||
});
|
||||
ui->send_btn->setFocus();
|
||||
ui->send_btn->setDefault(true);
|
||||
ui->msg_edit->installEventFilter(this);//设置完后自动调用其eventFilter函数
|
||||
|
@ -45,7 +61,9 @@ void MainWidget::on_send_text_clicked()
|
|||
if(text.isEmpty()) //群发
|
||||
{
|
||||
QString currentTime = QTime::currentTime().toString("H:mm A");
|
||||
ui->textEdit->append("<font color=\"MediumBlue\"><b>" + username + " [" + currentTime + "] send a file \"" + filename + "\"</b></font>");
|
||||
QString html = "<a href=\"file:///" + filePath + "\">" + filename + "</a>";
|
||||
qDebug() << html;
|
||||
ui->textEdit->append("<font color=\"MediumBlue\"><b>" + username + " [" + currentTime + "] send a file \"" + html + "\"</b></font>");
|
||||
this->sendFile(filePath,filename);
|
||||
}
|
||||
else //私发文件
|
||||
|
@ -53,15 +71,29 @@ void MainWidget::on_send_text_clicked()
|
|||
ui->msg_edit->clear();
|
||||
int n = text.indexOf(' ');
|
||||
QString name = text.mid(1,n-1);
|
||||
if(name == username)
|
||||
if(username == name)
|
||||
{
|
||||
QMessageBox::critical(this,"错误","不能自己给自己发送文件!");
|
||||
return;
|
||||
}
|
||||
|
||||
int index = 0;
|
||||
for (int i = 0; i < ui->name_list->count(); i++)
|
||||
{
|
||||
if(name != ui->name_list->item(index)->text())
|
||||
index++;
|
||||
}
|
||||
if(index >= ui->name_list->count())
|
||||
{
|
||||
QMessageBox::critical(this,"错误","未找到指定用户!");
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
QString currentTime = QTime::currentTime().toString("H:mm A");
|
||||
ui->textEdit->append(QString("<font color=\"MediumBlue\"><b>" + username + " [" + currentTime + "] send a private file to %1\""
|
||||
+ filename + "\"</b></font>").arg(name));
|
||||
QString html = "<a href=\"file:///" + filePath + "\">" + filename + "</a>";
|
||||
qDebug() << html;
|
||||
ui->textEdit->append("<font color=\"MediumBlue\"><b>" + username + " [" + currentTime + "] send " + name + " a private file \"" + html + "\"</b></font>");
|
||||
this->sendFile(filePath,filename,name);
|
||||
}
|
||||
}
|
||||
|
@ -70,7 +102,7 @@ void MainWidget::on_send_text_clicked()
|
|||
|
||||
void MainWidget::on_save_btn_clicked()
|
||||
{
|
||||
QFile file("./memory.txt");
|
||||
QFile file("./ChatRecord/memory.txt");
|
||||
if (file.exists()) //文件存在
|
||||
{
|
||||
QMessageBox::StandardButton reply;
|
||||
|
@ -82,8 +114,10 @@ void MainWidget::on_save_btn_clicked()
|
|||
QTextStream stream(&file);
|
||||
//转换为字符串
|
||||
QString str = ui->textEdit->toPlainText();
|
||||
str = "\n\n" + str;
|
||||
//写入文本流
|
||||
QString date = QDate::currentDate().toString("yyyy-MM-dd ");
|
||||
QString currentTime = QTime::currentTime().toString("H:mm A\n");
|
||||
str = "\n\n" + date + currentTime + str;
|
||||
//加入时间戳并写入
|
||||
stream << str;
|
||||
//关闭文件
|
||||
file.close();
|
||||
|
@ -97,7 +131,9 @@ void MainWidget::on_save_btn_clicked()
|
|||
QTextStream stream(&file);
|
||||
//转换为字符串
|
||||
QString str = ui->textEdit->toPlainText();
|
||||
//写入文本流
|
||||
QString date = QDate::currentDate().toString("yyyy-MM-dd ");
|
||||
QString currentTime = QTime::currentTime().toString("H:mm A\n");
|
||||
str = date + currentTime + str + "\n";
|
||||
stream << str;
|
||||
//关闭文件
|
||||
file.close();
|
||||
|
@ -132,14 +168,17 @@ void MainWidget::on_send_btn_clicked()
|
|||
QString data_text = insert + text.mid(n,-1);
|
||||
QString text_self = QString("私发给%1:%2").arg(to_user).arg(text.mid(n));
|
||||
qDebug() << text_self;
|
||||
int index = 0;
|
||||
for (index = 0; index < ui->name_list->count(); index++)
|
||||
if(username == to_user)
|
||||
{
|
||||
if(username ==ui->name_list->item(index)->text())
|
||||
{
|
||||
QMessageBox::critical(this,"错误","不能自己给自己发送消息!");
|
||||
return;
|
||||
}
|
||||
QMessageBox::critical(this,"错误","不能自己给自己发送文件!");
|
||||
return;
|
||||
}
|
||||
|
||||
int index = 0;
|
||||
for (int i = 0; i < ui->name_list->count(); i++)
|
||||
{
|
||||
if(to_user != ui->name_list->item(index)->text())
|
||||
index++;
|
||||
}
|
||||
if(index >= ui->name_list->count())
|
||||
{
|
||||
|
@ -213,6 +252,24 @@ void MainWidget::update_text(QString sendname,QString data)
|
|||
ui->textEdit->append(sendname + " [" + currentTime + "] : " + data);
|
||||
}
|
||||
|
||||
void MainWidget::update_file(QString sendname,QString filename,QString file_data)
|
||||
{
|
||||
readFile(filename,QByteArray::fromHex(file_data.toUtf8()));
|
||||
QString currentTime = QTime::currentTime().toString("H:m A");
|
||||
QString html = "<a href=\"file:///./Document/" + filename + "\">" + filename + "</a>";
|
||||
QString msg = username + " [" + currentTime + "] : " + QString("收到了一个来自%1的群发文件:").arg(sendname) + html;
|
||||
ui->textEdit->append(msg);
|
||||
}
|
||||
|
||||
void MainWidget::update_file_private(QString sendname,QString filename,QString file_data)
|
||||
{
|
||||
readFile(filename,QByteArray::fromHex(file_data.toUtf8()));
|
||||
QString currentTime = QTime::currentTime().toString("H:m A");
|
||||
QString html = "<a href=\"file:///./Document/" + filename + "\">" + filename + "</a>";
|
||||
QString msg = username + " [" + currentTime + "] : " + QString("收到了一个来自%1的私发文件:").arg(sendname) + html;
|
||||
ui->textEdit->append(msg);
|
||||
}
|
||||
/*
|
||||
void MainWidget::update_file(QString sendname,QString filename,QString file_data)
|
||||
{
|
||||
QMessageBox::StandardButton response;
|
||||
|
@ -248,7 +305,7 @@ void MainWidget::update_file_private(QString sendname,QString filename,QString f
|
|||
QMessageBox::information(this,"消息","未接收该文件!");
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
void MainWidget::update_users(int count,QList<QString>online_users)
|
||||
{
|
||||
ui->name_list->clear();
|
||||
|
@ -271,25 +328,23 @@ void MainWidget::sendFile(QString filePath,QString filename,QString name)
|
|||
return;
|
||||
}
|
||||
|
||||
//打开文件流,以写的方式打开
|
||||
/*打开文件流,以写的方式打开
|
||||
QDataStream out(&block,QIODevice::WriteOnly);
|
||||
out.setVersion(QDataStream::Qt_5_8);
|
||||
out.setVersion(QDataStream::Qt_5_8);*/
|
||||
|
||||
//将数据读取到QByteArray
|
||||
QByteArray dataOfFile = file.readAll();
|
||||
//关闭文件
|
||||
file.close();
|
||||
//放入block
|
||||
block.append(dataOfFile);
|
||||
|
||||
if(name.isEmpty()) //群发文件
|
||||
{
|
||||
emit send_file_to_all(username,filename,block);
|
||||
emit send_file_to_all(username,filename,dataOfFile);
|
||||
}
|
||||
|
||||
else //私发文件
|
||||
{
|
||||
emit send_file_private(username,filename,block,name);
|
||||
emit send_file_private(username,filename,dataOfFile,name);
|
||||
}
|
||||
QProgressDialog *progressDlg = new QProgressDialog(this);
|
||||
progressDlg->setWindowModality(Qt::WindowModal);
|
||||
|
@ -313,21 +368,32 @@ void MainWidget::sendFile(QString filePath,QString filename,QString name)
|
|||
|
||||
void MainWidget::readFile(QString filename,QByteArray data)
|
||||
{
|
||||
|
||||
QString homePath = QDir::homePath();
|
||||
QString filePath = QFileDialog::getExistingDirectory(this,
|
||||
tr("Open Directory"),
|
||||
homePath,
|
||||
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
|
||||
QFile outFile(filePath + "/" +filename);
|
||||
/*QString homePath = QDir::homePath();
|
||||
QString filePath = QFileDialog::getExistingDirectory(this,tr("Open Directory"),homePath,
|
||||
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);*/
|
||||
QString filepath = "./Document";
|
||||
QDir dir(filepath);
|
||||
if(dir.exists())
|
||||
{
|
||||
QFile outFile(filepath + "/" +filename);
|
||||
if(!outFile.open(QIODevice::WriteOnly)){
|
||||
QMessageBox::warning(this,"","错误: 无法打开文件.");
|
||||
QMessageBox::warning(this,"错误","接收文件出错!");
|
||||
return;
|
||||
}
|
||||
outFile.write(data);
|
||||
outFile.close();
|
||||
QMessageBox::question(this,"接收文件","接收成功!");
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
dir.mkdir(filepath);
|
||||
QFile outFile(filepath + "/" +filename);
|
||||
if(!outFile.open(QIODevice::WriteOnly)){
|
||||
QMessageBox::warning(this,"错误","接收文件出错!");
|
||||
return;
|
||||
}
|
||||
outFile.write(data);
|
||||
outFile.close();
|
||||
}
|
||||
}
|
||||
|
||||
bool MainWidget::eventFilter(QObject *target, QEvent *event)
|
||||
|
|
|
@ -13,6 +13,8 @@
|
|||
#include <QFileDialog>
|
||||
#include <QScrollBar>
|
||||
#include <QKeyEvent>
|
||||
#include <QDate>
|
||||
#include <QDesktopServices>
|
||||
|
||||
namespace Ui {
|
||||
class MainWidget;
|
||||
|
|
|
@ -71,19 +71,6 @@
|
|||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QTextEdit" name="textEdit">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>60</x>
|
||||
<y>50</y>
|
||||
<width>361</width>
|
||||
<height>401</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="styleSheet">
|
||||
<string notr="true">border-radius:20px;</string>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QWidget" name="layoutWidget_3">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
|
@ -242,6 +229,20 @@ QScrollBar::add-page:vertical,QScrollBar::sub-page:vertical
|
|||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QTextBrowser" name="textEdit">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>55</x>
|
||||
<y>51</y>
|
||||
<width>371</width>
|
||||
<height>401</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="styleSheet">
|
||||
<string notr="true">border-radius:
|
||||
7px;padding:2px 4px;</string>
|
||||
</property>
|
||||
</widget>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
|
|
|
@ -155,7 +155,6 @@ QByteArray Protocol::data_encrypt(QByteArray src)
|
|||
|
||||
QByteArray Protocol::data_decrypt(QByteArray src)
|
||||
{
|
||||
qDebug() << QString(src);
|
||||
QByteArray result = XOR_En_Decrypt(src);
|
||||
//qDebug() << result;
|
||||
result = Upper_Lower_En_Decrypt(result);
|
||||
|
|
|
@ -35,7 +35,6 @@ void TCPClient::configAndrun()
|
|||
|
||||
void TCPClient::sendToserver(QByteArray &data)
|
||||
{
|
||||
qDebug() << data;
|
||||
if (socket.isOpen())
|
||||
{
|
||||
socket.write(data);
|
||||
|
@ -51,7 +50,6 @@ void TCPClient::setServer(QString IP, qint16 port)
|
|||
|
||||
void TCPClient::connected()
|
||||
{
|
||||
qDebug() << "1";
|
||||
emit TCPClientConnected();
|
||||
qDebug() << "emit TCPClientConnected successfully";
|
||||
QString addr_t = socket.peerAddress().toString();
|
||||
|
|
Loading…
Reference in New Issue