diff --git a/ClientCore/ClientCore.cpp b/ClientCore/ClientCore.cpp index 2e601ed..d3a3d72 100644 --- a/ClientCore/ClientCore.cpp +++ b/ClientCore/ClientCore.cpp @@ -112,6 +112,16 @@ void ClientCore::handleAsk(QSharedPointer frame) } return; } + if (msg.command == STRMSG_AC_RENAME_FILEDIR) { + msg.command = STRMSG_AC_ANSWER_RENAME_FILEDIR; + msg.msg = Util::Rename(msg.fromPath, msg.toPath, msg.type == STR_DIR); + 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 d3acc68..e39cb15 100644 --- a/Gui/Control/FileControl.cpp +++ b/Gui/Control/FileControl.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -15,6 +16,7 @@ #include #include "Form/FileInfoForm.h" +#include "Form/Loading.h" #include "GuiUtil/Public.h" #include "ui_FileControl.h" @@ -313,7 +315,7 @@ void FileManager::RefreshTab() item->setFlags(item->flags() & ~Qt::ItemIsEditable); ui->tableWidget->setItem(i, 4, item); - if (i % 10 == 0) { + if (i % 50 == 0) { QGuiApplication::processEvents(); } } @@ -547,10 +549,77 @@ void FileManager::OperDelete() void FileManager::OperRename() { -} + auto datas = ui->tableWidget->selectedItems(); + if (datas.isEmpty()) { + return; + } + if (datas.size() % 5 != 0) { + QMessageBox::information(this, tr("提示"), tr("请选择单行进行重命名。")); + return; + } + auto curName = datas[1]->text(); + auto curType = datas[3]->text(); -void FileManager::WaitMsg() -{ + QInputDialog dialog(this); + dialog.setWindowTitle("输入"); + dialog.setLabelText("请输入新名称:"); + dialog.setOkButtonText("确定"); + dialog.setCancelButtonText("取消"); + dialog.setFixedSize(dialog.minimumSizeHint()); + + QString text; + if (dialog.exec() == QDialog::Accepted) { + text = dialog.textValue().trimmed(); + if (text.isEmpty()) { + return; + } + } + + QString oldName = Util::Join(GetRoot(), curName); + QString newName = Util::Join(GetRoot(), text); + + if (!isRemote_) { + QString ret = Util::Rename(oldName, newName, curType == STR_DIR); + if (!ret.isEmpty()) { + QMessageBox::information(this, tr("提示"), ret); + } else { + datas[1]->setText(text); + } + return; + } + + WaitOper oper(this); + oper.SetClient(cliCore_); + oper.SetType(STRMSG_AC_RENAME_FILEDIR, STRMSG_AC_ANSWER_RENAME_FILEDIR); + oper.SetPath(oldName, newName, curType); + + 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)); + return; + } + if (msg.msg.isEmpty()) { + datas[1]->setText(text); + return; + } else { + QMessageBox::information(this, tr("提示"), QString(tr("重命名失败=>%1")).arg(msg.msg)); + } } QString FileManager::GetRoot() @@ -603,7 +672,7 @@ void FileManager::doubleClick(int row, int column) } auto type = ui->tableWidget->item(row, 3)->text(); - if (type != "Dir") { + if (type != STR_DIR) { return; } @@ -611,3 +680,80 @@ void FileManager::doubleClick(int row, int column) QString np = dir.filePath(item->text()); fileHelper_->GetDirFile(np); } + +WaitOper::WaitOper(QObject* parent) : WaitThread(parent) +{ +} + +void WaitOper::run() +{ + isAlreadyInter_ = false; + infoMsg_.msg = STR_NONE; + isRun_ = true; + recvMsg_ = false; + + InfoMsg msg; + msg.command = sendStrType_; + msg.fromPath = stra_; + msg.toPath = strb_; + msg.type = type_; + + auto f = cli_->GetBuffer(msg, FBT_MSGINFO_ASK, cli_->GetRemoteID()); + if (!ClientCore::syncInvoke(cli_, f)) { + auto errMsg = QString(tr("向%1发送%2请求失败。")).arg(cli_->GetRemoteID()).arg(sendStrType_); + emit sigCheckOver(); + qCritical() << errMsg; + return; + } + while (isRun_) { + QThread::msleep(1); + if (isAlreadyInter_) { + qInfo() << tr("线程中断文件操作等待......"); + return; + } + if (!recvMsg_) { + continue; + } + break; + } + isAlreadyInter_ = true; + emit sigCheckOver(); + auto n = QString(tr("向%1的请求%2处理结束。")).arg(cli_->GetRemoteID()).arg(sendStrType_); + qInfo() << n; +} + +void WaitOper::SetType(const QString& sendType, const QString& ansType) +{ + sendStrType_ = sendType; + ansStrType_ = ansType; +} + +void WaitOper::SetPath(const QString& stra, const QString& strb, const QString& type) +{ + stra_ = stra; + strb_ = strb; + type_ = type; +} + +InfoMsg WaitOper::GetMsg() const +{ + return infoMsg_; +} + +void WaitOper::interrupCheck() +{ + qWarning() << QString(tr("中断请求处理%1......")).arg(sendStrType_); + WaitThread::interrupCheck(); +} + +void WaitOper::recvFrame(QSharedPointer frame) +{ + InfoMsg info = infoUnpack(frame->data); + if (info.command == ansStrType_) { + infoMsg_ = info; + recvMsg_ = true; + return; + } + auto n = tr("收到未知Oper的回复信息:") + info.command; + qInfo() << n; +} diff --git a/Gui/Control/FileControl.h b/Gui/Control/FileControl.h index 010d921..102835c 100644 --- a/Gui/Control/FileControl.h +++ b/Gui/Control/FileControl.h @@ -27,6 +27,29 @@ enum class SortMethod { SMD_BY_SIZE_ASC, }; +class WaitOper : public WaitThread +{ +public: + WaitOper(QObject* parent = nullptr); + +public: + void run() override; + void SetType(const QString& sendType, const QString& ansType); + void SetPath(const QString& stra, const QString& strb, const QString& type); + InfoMsg GetMsg() const; + virtual void interrupCheck(); + virtual void recvFrame(QSharedPointer frame); + +private: + bool recvMsg_{}; + InfoMsg infoMsg_{}; + QString sendStrType_{}; + QString ansStrType_{}; + QString stra_; + QString strb_; + QString type_; +}; + class FileManager : public QWidget { Q_OBJECT @@ -58,13 +81,12 @@ private: void CopyFullPath(); void ShowProperties(); void UpDown(); - + private: void OperNewFolder(); void OperDelete(); void OperRename(); - void WaitMsg(); - + public slots: void evtHome(); void evtFile(); diff --git a/Gui/frelayGUI.cpp b/Gui/frelayGUI.cpp index febde63..434b71d 100644 --- a/Gui/frelayGUI.cpp +++ b/Gui/frelayGUI.cpp @@ -165,10 +165,11 @@ void frelayGUI::HandleTask(const QVector& tasks) return; } + std::shared_ptr recv(nullptr, [&cond](void*) { cond.wait(); }); + // 检查结果 auto reTasks = cond.GetTasks(); if (!CheckTaskResult(reTasks)) { - cond.wait(); return; } if (reTasks.empty()) { diff --git a/Protocol/Protocol.h b/Protocol/Protocol.h index 22a6878..2c9ada9 100644 --- a/Protocol/Protocol.h +++ b/Protocol/Protocol.h @@ -96,20 +96,21 @@ enum FileCheckState { #define STRMSG_AC_DEL_DIR "requestDelDir" #define STRMSG_AC_ANSWER_DEL_DIR "answerDelDir" #define STRMSG_AC_RENAME_FILEDIR "requestRenameFileDir" -#define STRMSG_AC_ANSWER_FILEDIR "answerRenameFileDir" +#define STRMSG_AC_ANSWER_RENAME_FILEDIR "answerRenameFileDir" #define STRMSG_AC_NEW_DIR "requestNewDir" #define STRMSG_AC_ANSWER_NEW_DIR "answerNewDir" #define STRMSG_AC_ASK_FILEINFO "requestFileInfo" #define STRMSG_AC_ANSWER_FILEINFO "answerFileInfo" #define STRMSG_AC_UP "upAction" #define STRMSG_AC_DOWN "downAction" -#define STRMSG_AC_ACCEPT "acceptAction" -#define STRMSG_AC_REJECT "rejectAction" -#define STRMSG_AC_CANCEL "cancelAction" #define STRMSG_ST_FILEEXIT "fileExist" #define STRMSG_ST_FILENOEXIT "fileNotExist" #define STRMSG_ST_DIREXIT "dirExist" #define STRMSG_ST_DIRNOEXIT "dirNotExist" +#define STR_FILE "File" +#define STR_DIR "Dir" +#define STR_NONE "None" + #endif // PROTOCOL_H \ No newline at end of file diff --git a/Struct/InfoMsg.h b/Struct/InfoMsg.h index 93f156d..1be431b 100644 --- a/Struct/InfoMsg.h +++ b/Struct/InfoMsg.h @@ -24,6 +24,7 @@ struct InfoMsg { QString msg; QString fromPath; QString toPath; + QString type; quint64 size{}; quint32 permissions{}; QVector list; @@ -31,7 +32,7 @@ struct InfoMsg { void serialize(QDataStream& data) const { - data << mark << command << msg << fromPath << toPath << size << permissions; + data << mark << command << msg << fromPath << toPath << type << size << permissions; data << static_cast(list.size()); for (const auto& item : list) { data << item; @@ -46,7 +47,7 @@ struct InfoMsg { void deserialize(QDataStream& data) { - data >> mark >> command >> msg >> fromPath >> toPath >> size >> permissions; + data >> mark >> command >> msg >> fromPath >> toPath >> type >> size >> permissions; qint32 listSize; data >> listSize; diff --git a/Util/Util.cpp b/Util/Util.cpp index 61f5f7b..0e06d25 100644 --- a/Util/Util.cpp +++ b/Util/Util.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -150,6 +151,23 @@ bool Util::DirExist(const QString& path, bool isFilePath) return dir.exists(); } +QString Util::Rename(const QString& from, const QString& to, bool isDir) +{ + if (isDir) { + QDir dir; + if (dir.rename(from, to)) { + return ""; + } + return tr("请确认是否有权限或者被占用。"); + } else { + QFile f(from); + if (f.rename(to)) { + return ""; + } + return f.errorString(); + } +} + QString Util::UUID() { return QUuid::createUuid().toString().remove("{").remove("}"); @@ -235,4 +253,3 @@ void GlobalData::SetConfigPath(const std::string& path) { ConfigPath_ = path; } - diff --git a/Util/Util.h b/Util/Util.h index b51ac4d..7c36be6 100644 --- a/Util/Util.h +++ b/Util/Util.h @@ -51,6 +51,7 @@ public: static QString GetVersion(); 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 UUID(); static QVector GetLocalDrivers(); };