代码拉取完成,页面将自动刷新
#include "serverfilesystem.h"
#include "client_socket.h"
serverFileSystem::serverFileSystem(QWidget *parent)
: QWidget{parent}
{
m_ItemModel = new QStandardItemModel(this);
m_TreeView=new QTreeView(this);
m_pUpload= new QPushButton("upload",this);
m_pDownload= new QPushButton("download",this);
m_pShare= new QPushButton("share",this);
m_pCreate= new QPushButton("create",this);
m_pMove= new QPushButton("move",this);
m_pDelete= new QPushButton("delete",this);
m_pUpdate = new QPushButton("update file");
m_pChangeWidget = new QPushButton("change interface");
m_pMoveDes = new QPushButton("destination file dir");
m_pMoveDes->setEnabled(false);
m_TreeView->setModel(m_ItemModel);
connect(m_pUpload,&QPushButton::clicked,this,&serverFileSystem::UpLoad);
connect(m_pDownload,&QPushButton::clicked,this,&serverFileSystem::DownLoad);
connect(m_pShare,&QPushButton::clicked,this,&serverFileSystem::Share);
connect(m_pCreate,&QPushButton::clicked,this,&serverFileSystem::Create);
connect(m_pMove,&QPushButton::clicked,this,&serverFileSystem::Move);
connect(m_pDelete,&QPushButton::clicked,this,&serverFileSystem::Delete);
connect(m_pUpdate,&QPushButton::clicked,this,&serverFileSystem::UpdateFileInfo);
connect(m_pMoveDes,&QPushButton::clicked,this,&serverFileSystem::MoveDes);
QVBoxLayout* VB_Layout = new QVBoxLayout();
VB_Layout->addWidget(m_pUpload);
VB_Layout->addWidget(m_pDownload);
VB_Layout->addWidget(m_pShare);
VB_Layout->addWidget(m_pCreate);
VB_Layout->addWidget(m_pMove);
VB_Layout->addWidget(m_pMoveDes);
VB_Layout->addWidget(m_pDelete);
VB_Layout->addWidget(m_pUpdate);
VB_Layout->addWidget(m_pChangeWidget);
QHBoxLayout* HB_Layout = new QHBoxLayout(this);
HB_Layout->addWidget(m_TreeView);
HB_Layout->addLayout(VB_Layout);
this->setLayout(HB_Layout);
}
QTreeView *&serverFileSystem::getTreeView()
{
return m_TreeView;
}
QStandardItemModel *&serverFileSystem::getItemModel()
{
return m_ItemModel;
}
QString serverFileSystem::getTreeRelativePath(QString* type)
{
QModelIndex currentIndex = m_TreeView->currentIndex();
int row = currentIndex.row();
QModelIndex parent = currentIndex.parent(); // 对于顶级项,这通常是 QModelIndex()
// 访问同一行但不同列的数据
QModelIndex index = m_ItemModel->index(row,0,parent);
QStandardItem* item = m_ItemModel->itemFromIndex(index);
if(type!=NULL)
{
QModelIndex index1 = m_ItemModel->index(row,1,parent);
QStandardItem* item1 = m_ItemModel->itemFromIndex(index);
*type = item1->text();
}
if(item)
{
QStringList path;
while(item)
{
path.prepend(item->text());
item = item->parent();
}
return QString(path.join("/"));
}
else
{
return QString();
}
}
void serverFileSystem::flushTreeView(PDU *pdu)
{
m_ItemModel->clear();
// 使用 pdu 填充新内容到 model
mkTreeItem(pdu, m_ItemModel);
}
void serverFileSystem::mkTreeItem(PDU *pdu, QStandardItemModel *mItemModel)
{
m_ItemModel->setColumnCount(4);
m_ItemModel->setHeaderData(0, Qt::Horizontal, QObject::tr("name"));
m_ItemModel->setHeaderData(1, Qt::Horizontal, QObject::tr("type"));
m_ItemModel->setHeaderData(2, Qt::Horizontal, QObject::tr("size"));
m_ItemModel->setHeaderData(3, Qt::Horizontal, QObject::tr("modify_time"));
QStandardItem* rootItem = mItemModel->invisibleRootItem();
QString name;
QString size;
QString modify_time;
uint AllFileCount = pdu->uiMsgLen / sizeof(FileInfo);
FileInfo* fileInfo = new FileInfo;
QIcon folderIcon("://resource/dir.png");
QIcon fileIcon("://resource/file.png");
for (int i = 0; i < AllFileCount; i++) {
memcpy((char*)fileInfo, (FileInfo*)pdu->msg + i, sizeof(FileInfo));
name = fileInfo->caName;
size = QString::number(fileInfo->uiSize);
modify_time = fileInfo->caTime;
int depth = fileInfo->depth;
QStandardItem* name_item = new QStandardItem(name);
QStandardItem* size_item = new QStandardItem(size);
QStandardItem* type_item = new QStandardItem(fileInfo->bIsDir ? "dir" : "file");
QStandardItem* time_item = new QStandardItem(modify_time);
if (fileInfo->bIsDir) {
name_item->setIcon(folderIcon);
}
else {
name_item->setIcon(fileIcon);
}
QStandardItem* parentItem = rootItem;
for (int j = 0; j < depth; j++) {
if (parentItem->rowCount() > 0) {
parentItem = parentItem->child(parentItem->rowCount() - 1);
}
}
parentItem->appendRow(QList<QStandardItem*>() << name_item << type_item << size_item << time_item);
}
delete fileInfo;
}
void serverFileSystem::startThread()
{
fileThread->start();
}
// my_tcpsocket *serverFileSystem::getSocket()
// {
// return filesocket;
// }
QThread *serverFileSystem::get_fileThread()
{
return fileThread;
}
fileUpload *serverFileSystem::get_uploadFile_OB()
{
return uploadFile_OB;
}
void serverFileSystem::handleThread()
{
QTimer::singleShot(2000, [&]() {
fileThread->requestInterruption();
fileThread->quit();
fileThread->deleteLater();
});
}
void serverFileSystem::UpLoad()
{
QString dirPath = getTreeRelativePath(NULL);
uploadFilePath = QFileDialog::getOpenFileName();
if(uploadFilePath.isEmpty())
{
return;
}
int index = uploadFilePath.lastIndexOf('/');
QString fileName = uploadFilePath.right(uploadFilePath.size()-index -1);
QFile file(uploadFilePath);
qint64 fileSize = file.size();
QString relativePath =client_socket::getInstance().getM_userID() + "/" + dirPath + "/" + fileName;
PDU* pdu = mkPDU(relativePath.size()+21+2+30);
pdu->uiMsgType = MSG_TYPE::MSG_TYPE_FILE_UPLOAD_REQUEST;
sprintf(pdu->msg,"%lld %s",fileSize,relativePath.toStdString().c_str());
uploadFile_OB = new fileUpload;
uploadFile_OB->setPdu(pdu,pdu->uiMsgLen);
uploadFile_OB->setFilePath(uploadFilePath);
my_tcpsocket* tcp = new my_tcpsocket;
uploadFile_OB->setTcpSocket(tcp);
QString strIP= client_socket::getInstance().getStrIP();
quint16 usPort = client_socket::getInstance().getUSPort() ;
uploadFile_OB->get_uploadSocket()->connectToHost(QHostAddress(strIP),usPort);
fileThread = new QThread;
uploadFile_OB->moveToThread(fileThread);
uploadFile_OB->get_uploadSocket()->moveToThread(fileThread);
connect(fileThread,&QThread::started,uploadFile_OB,&fileUpload::init_update);
connect(uploadFile_OB->get_uploadSocket(),&my_tcpsocket::readyRead,uploadFile_OB->get_uploadSocket(),&my_tcpsocket::receiveMsg);
free(pdu);
pdu = NULL;
fileThread->start();
}
void serverFileSystem::DownLoad()
{
QString type;
QString filePath = getTreeRelativePath(&type);
if(filePath.isEmpty())return;
filePath = client_socket::getInstance().getM_userID() + "/" + filePath;
if(type == QString("dir"))
{
QMessageBox::warning(NULL,"下载文件","暂不支持直接下载目录");
return;
}
QString strDownloadFilePath = QFileDialog::getSaveFileName();
if(strDownloadFilePath.isEmpty())
{
QMessageBox::warning(NULL, "下载文件", "请指定下载文件的位置!");
client_socket::getInstance().getTcpSocket().get_downloadFileInfo()->file.setFileName(""); // 清空
return ;
}
PDU* pdu = mkPDU(filePath.size()+9);
pdu->uiMsgType=MSG_TYPE::MSG_TYPE_FILE_DOWNLOAD_REQUEST;
sprintf(pdu->msg,"%s",filePath.toStdString().c_str());
uploadFile_OB = new fileUpload;
uploadFile_OB->setPdu(pdu,pdu->uiMsgLen);
my_tcpsocket* tcp = new my_tcpsocket;
uploadFile_OB->setTcpSocket(tcp);
tcp->get_downloadFileInfo()->file.setFileName(strDownloadFilePath);
QString strIP= client_socket::getInstance().getStrIP();
quint16 usPort = client_socket::getInstance().getUSPort() ;
uploadFile_OB->get_uploadSocket()->connectToHost(QHostAddress(strIP),usPort);
fileThread = new QThread;
uploadFile_OB->moveToThread(fileThread);
uploadFile_OB->get_uploadSocket()->moveToThread(fileThread);
connect(fileThread,&QThread::started,uploadFile_OB,&fileUpload::init_download);
connect(uploadFile_OB->get_uploadSocket(),&my_tcpsocket::readyRead,uploadFile_OB->get_uploadSocket(),&my_tcpsocket::receiveMsg);
free(pdu);
pdu =NULL;
fileThread->start();
}
void serverFileSystem::Share()
{
}
void serverFileSystem::Create()
{
CreateFileDiaog* dialog = new CreateFileDiaog;
dialog->exec();
QString fileName = dialog->getFileName_LE()->text();
QString file_suffix = dialog->getSuffix_LE()->text();
QString type;
QString relativePath = getTreeRelativePath(&type);
QString strPathName;
bool mkFile =0;
if(type == QString("file"))
{
QMessageBox::warning(NULL,"创建文件","不能在文件下创建文件或目录");
return;
}
if(fileName.isEmpty()&&file_suffix.isEmpty())
{
QMessageBox::warning(NULL,"创建文件","未选择任何文件或文件夹");
return;
}
else if(file_suffix.isEmpty()&& !fileName.isEmpty())
{
strPathName=fileName;
mkFile=0;
}
else if(!fileName.isEmpty()&&!file_suffix.isEmpty())
{
strPathName=fileName+"."+file_suffix;
mkFile = 1;
}
QString mkDirPath = relativePath + "/" + strPathName;
PDU* pdu = mkPDU(mkDirPath.size());
if(mkFile)
{
pdu->uiMsgType=MSG_TYPE::MSG_TYPE_FILE_CREATE_FILE_REQUEST;
}
else
{
pdu->uiMsgType=MSG_TYPE::MSG_TYPE_FILE_CREATE_DIR_REQUEST;
}
memcpy(pdu->msg,mkDirPath.toStdString().c_str(),mkDirPath.size());
client_socket::getInstance().getTcpSocket().write((char*)pdu,pdu->uiPduLen);
free(pdu);
free(dialog);
dialog=NULL;
pdu=NULL;
}
void serverFileSystem::Move()
{
QString relativePath = getTreeRelativePath(NULL);
if(relativePath.isEmpty())
{
QMessageBox::warning(NULL,"移动文件","未选择任何文件或文件夹");
return;
}
m_pMoveOldPath=relativePath;
m_pMoveDes->setEnabled(true);
QMessageBox::information(this, "移动文件", "请跳转到需要移动到的目录,\n然后点击“目标目录”按钮。");
}
void serverFileSystem::MoveDes()
{
QString strDesDir = getTreeRelativePath(NULL);
if(strDesDir.isEmpty())
{
QMessageBox::warning(NULL,"移动文件","未选择任何文件");
return;
}
QMessageBox::StandardButton sbMoveAffirm;
QString strMoveAffirm = QString("你确认将文件%1移动到%2目录下吗?").arg(m_pMoveOldPath).arg(strDesDir);
sbMoveAffirm = QMessageBox::question(this, "移动文件", strMoveAffirm);
uint DesSize = strDesDir.size()+1;
uint OldSize = m_pMoveOldPath.size() + 1;
if(sbMoveAffirm == QMessageBox::No) // 不移动
{
m_pMoveOldPath.clear();
m_pMoveDes->setEnabled(false);
return ;
}
PDU* pdu = mkPDU(4+m_pMoveOldPath.size()+strDesDir.size()+5);
pdu->uiMsgType=MSG_TYPE::MSG_TYPE_FILE_MOVE_REQUEST;
sprintf(pdu->msg,"%u %u %s %s",OldSize,DesSize,m_pMoveOldPath.toStdString().c_str(),strDesDir.toStdString().c_str());
client_socket::getInstance().getTcpSocket().write((char*)pdu,pdu->uiPduLen);
free(pdu);
pdu=NULL;
m_pMoveOldPath.clear();
m_pMoveDes->setEnabled(false);
}
void serverFileSystem::Delete()
{
QString relativePath = getTreeRelativePath(NULL);
if(relativePath.isEmpty())
{
QMessageBox::warning(NULL,"删除文件","未选择任何文件或文件夹");
return;
}
PDU* pdu = mkPDU(relativePath.size());
pdu->uiMsgType = MSG_TYPE::MSG_TYPE_FILE_DELETE_REQUEST;
memcpy(pdu->msg,relativePath.toStdString().c_str(),relativePath.size());
client_socket::getInstance().getTcpSocket().write((char*)pdu,pdu->uiPduLen);
free(pdu);
pdu=NULL;
}
void serverFileSystem::UpdateFileInfo()
{
// qDebug()<<"UpdateFileInfo";
QString ID= client_socket::getInstance().getM_userID();
PDU* pdu = mkPDU(32);
pdu->uiMsgType = MSG_TYPE::MSG_TYPE_FILE_UPDATEFILEINFO_REQUEST;
memcpy(pdu->msg,ID.toStdString().c_str(),ID.size());
uint len =client_socket::getInstance().getTcpSocket().write((char*)pdu,pdu->uiPduLen);
if(len==-1)
{
qDebug()<<"消息发送失败";
}
else if(len != pdu->uiPduLen)
{
qDebug()<<"消息发送失败";
}
// client_socket::getInstance().getTcpSocket().getTimer()->stop();
free(pdu);
pdu=NULL;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。