From a608bedd11a7786d6a8433a985fd33f043974c72 Mon Sep 17 00:00:00 2001 From: taynpg Date: Sun, 16 Nov 2025 20:26:08 +0800 Subject: [PATCH] =?UTF-8?q?func=EF=BC=9A=E5=88=A0=E9=99=A4=E6=96=87?= =?UTF-8?q?=E4=BB=B6/=E5=A4=B9=E6=88=90=E5=8A=9F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ClientCore/ClientCore.cpp | 10 +++++ Gui/Control/FileControl.cpp | 89 ++++++++++++++++++++++++++++++++----- Gui/Control/FileControl.h | 4 +- Protocol/Protocol.h | 6 +-- Util/Util.cpp | 29 +++++++++++- Util/Util.h | 1 + 6 files changed, 121 insertions(+), 18 deletions(-) diff --git a/ClientCore/ClientCore.cpp b/ClientCore/ClientCore.cpp index d3a3d72..badb2b1 100644 --- a/ClientCore/ClientCore.cpp +++ b/ClientCore/ClientCore.cpp @@ -122,6 +122,16 @@ void ClientCore::handleAsk(QSharedPointer frame) } return; } + if (msg.command == STRMSG_AC_DEL_FILEDIR) { + msg.command = STRMSG_AC_ANSWER_DEL_FILEDIR; + msg.msg = Util::Delete(msg.fromPath); + if (!Send(msg, FBT_MSGINFO_ANSWER, frame->fid)) { + auto logMsg = tr("给") + frame->fid + tr("返回删除结果消息失败。"); + qCritical() << logMsg; + return; + } + return; + } // 未知信息 qWarning() << QString(tr("未知询问信息类型:%1")).arg(msg.command); } diff --git a/Gui/Control/FileControl.cpp b/Gui/Control/FileControl.cpp index e39cb15..9529746 100644 --- a/Gui/Control/FileControl.cpp +++ b/Gui/Control/FileControl.cpp @@ -545,18 +545,85 @@ void FileManager::OperNewFolder() void FileManager::OperDelete() { + QList datas; + if (!CheckSelect(datas)) { + return; + } + + // 确认是否删除 + int ret = QMessageBox::question(this, tr("确认删除"), tr("确定要删除%1吗?").arg(datas[1]->text()), + QMessageBox::Yes | QMessageBox::No, QMessageBox::No); + + if (ret != QMessageBox::Yes) { + return; + } + + auto name = Util::Join(GetRoot(), datas[1]->text()); + auto type = datas[3]->text(); + + if (!isRemote_) { + QString ret = Util::Delete(name); + if (ret.isEmpty()) { + QMessageBox::information(this, tr("提示"), tr("删除成功")); + int row = datas[0]->row(); + ui->tableWidget->removeRow(row); + } else { + QMessageBox::information(this, tr("提示"), ret); + } + return; + } + + WaitOper oper(this); + oper.SetClient(cliCore_); + oper.SetType(STRMSG_AC_DEL_FILEDIR, STRMSG_AC_ANSWER_DEL_FILEDIR); + oper.SetPath(name, "", ""); + + LoadingDialog checking(this); + checking.setTipsText("正在删除..."); + connect(&oper, &WaitOper::sigCheckOver, &checking, &LoadingDialog::cancelBtnClicked); + connect(cliCore_, &ClientCore::sigMsgAnswer, &oper, &WaitOper::recvFrame); + + oper.start(); + checking.exec(); + + std::shared_ptr recv(nullptr, [&oper](void*) { oper.wait(); }); + + if (checking.isUserCancel()) { + oper.interrupCheck(); + return; + } + + // 检查结果 + auto msg = oper.GetMsg(); + if (msg.msg == STR_NONE || !msg.msg.isEmpty()) { + QMessageBox::information(this, tr("提示"), QString(tr("删除失败=>%1")).arg(msg.msg)); + } else { + QMessageBox::information(this, tr("提示"), QString(tr("删除成功。"))); + ui->tableWidget->removeRow(datas[0]->row()); + } +} + +bool FileManager::CheckSelect(QList& ret) +{ + ret = ui->tableWidget->selectedItems(); + if (ret.isEmpty()) { + QMessageBox::information(this, tr("提示"), tr("请选择一项。")); + return false; + } + if (ret.size() % 5 != 0) { + QMessageBox::information(this, tr("提示"), tr("请选择单行。")); + return false; + } + return true; } void FileManager::OperRename() { - auto datas = ui->tableWidget->selectedItems(); - if (datas.isEmpty()) { - return; - } - if (datas.size() % 5 != 0) { - QMessageBox::information(this, tr("提示"), tr("请选择单行进行重命名。")); + QList datas; + if (!CheckSelect(datas)) { return; } + auto curName = datas[1]->text(); auto curType = datas[3]->text(); @@ -574,6 +641,9 @@ void FileManager::OperRename() return; } } + else { + return; + } QString oldName = Util::Join(GetRoot(), curName); QString newName = Util::Join(GetRoot(), text); @@ -612,13 +682,8 @@ void FileManager::OperRename() auto msg = oper.GetMsg(); if (msg.msg == STR_NONE || !msg.msg.isEmpty()) { QMessageBox::information(this, tr("提示"), QString(tr("重命名失败=>%1")).arg(msg.msg)); - return; - } - if (msg.msg.isEmpty()) { - datas[1]->setText(text); - return; } else { - QMessageBox::information(this, tr("提示"), QString(tr("重命名失败=>%1")).arg(msg.msg)); + datas[1]->setText(text); } } diff --git a/Gui/Control/FileControl.h b/Gui/Control/FileControl.h index 102835c..5d05050 100644 --- a/Gui/Control/FileControl.h +++ b/Gui/Control/FileControl.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -86,7 +87,8 @@ private: void OperNewFolder(); void OperDelete(); void OperRename(); - + bool CheckSelect(QList& ret); + public slots: void evtHome(); void evtFile(); diff --git a/Protocol/Protocol.h b/Protocol/Protocol.h index 2c9ada9..9265ead 100644 --- a/Protocol/Protocol.h +++ b/Protocol/Protocol.h @@ -91,10 +91,8 @@ enum FileCheckState { // 字符串标识 #define STRMSG_AC_CHECK_FILE_EXIST "requestCheckFileExist" #define STRMSG_AC_ANSWER_FILE_EXIST "answerCheckFileExist" -#define STRMSG_AC_DEL_FILE "requestDelFile" -#define STRMSG_AC_ANSWER_DEL_FILE "answerDelFile" -#define STRMSG_AC_DEL_DIR "requestDelDir" -#define STRMSG_AC_ANSWER_DEL_DIR "answerDelDir" +#define STRMSG_AC_DEL_FILEDIR "requestDelFileDir" +#define STRMSG_AC_ANSWER_DEL_FILEDIR "answerDelFileDir" #define STRMSG_AC_RENAME_FILEDIR "requestRenameFileDir" #define STRMSG_AC_ANSWER_RENAME_FILEDIR "answerRenameFileDir" #define STRMSG_AC_NEW_DIR "requestNewDir" diff --git a/Util/Util.cpp b/Util/Util.cpp index 0e06d25..093ec6b 100644 --- a/Util/Util.cpp +++ b/Util/Util.cpp @@ -158,7 +158,7 @@ QString Util::Rename(const QString& from, const QString& to, bool isDir) if (dir.rename(from, to)) { return ""; } - return tr("请确认是否有权限或者被占用。"); + return tr("请确认是否无权限操作或者被占用。"); } else { QFile f(from); if (f.rename(to)) { @@ -253,3 +253,30 @@ void GlobalData::SetConfigPath(const std::string& path) { ConfigPath_ = path; } + +QString Util::Delete(const QString& path) +{ + QFileInfo fileInfo(path); + + if (!fileInfo.exists()) { + return tr("文件或目录不存在: %1").arg(path); + } + + if (fileInfo.isFile()) { + QFile file(path); + if (file.remove()) { + return ""; + } else { + return tr("删除文件失败: %1").arg(file.errorString()); + } + } else if (fileInfo.isDir()) { + QDir dir(path); + if (dir.removeRecursively()) { + return ""; + } else { + return tr("删除目录失败,确认是否占用或者无权限操作。"); + } + } else { + return tr("不支持的文件类型: %1").arg(path); + } +} \ No newline at end of file diff --git a/Util/Util.h b/Util/Util.h index 7c36be6..24efff4 100644 --- a/Util/Util.h +++ b/Util/Util.h @@ -52,6 +52,7 @@ public: static bool FileExist(const QString& path); static bool DirExist(const QString& path, bool isFilePath); static QString Rename(const QString& from, const QString& to, bool isDir); + static QString Delete(const QString& path); static QString UUID(); static QVector GetLocalDrivers(); };