From 32b206c2f56fab9ea643483a02e01ac7da4fcdc5 Mon Sep 17 00:00:00 2001 From: taynpg Date: Fri, 20 Jun 2025 09:45:39 +0800 Subject: [PATCH] fix: down remote file error. --- ClientCore/ClientCore.cpp | 4 +++ ClientCore/ClientCore.h | 1 + ClientCore/FileTrans.cpp | 50 +++++++++++++++++++++++++++++++---- ClientCore/FileTrans.h | 1 + Gui/Control/FileControl.cpp | 8 +++--- Gui/Control/cpTableWidget.cpp | 7 +++++ Gui/Control/cpTableWidget.h | 4 +-- Gui/Form/Transform.cpp | 29 +++++++++++++++++--- Gui/Form/Transform.h | 3 +++ Gui/frelayGUI.cpp | 8 +++--- Protocol/Protocol.h | 3 ++- 11 files changed, 99 insertions(+), 19 deletions(-) diff --git a/ClientCore/ClientCore.cpp b/ClientCore/ClientCore.cpp index 26a58a5..c01b522 100644 --- a/ClientCore/ClientCore.cpp +++ b/ClientCore/ClientCore.cpp @@ -139,6 +139,10 @@ void ClientCore::UseFrame(QSharedPointer frame) emit sigTransFailed(frame); break; } + case FBT_CLI_FILE_INFO: { + emit sigFileInfo(frame); + break; + } default: qCritical() << QString("unknown frame type: %1").arg(frame->type); break; diff --git a/ClientCore/ClientCore.h b/ClientCore/ClientCore.h index 1c8c121..e403c35 100644 --- a/ClientCore/ClientCore.h +++ b/ClientCore/ClientCore.h @@ -79,6 +79,7 @@ signals: void sigCanDown(QSharedPointer frame); void sigFileBuffer(QSharedPointer frame); void sigTransFailed(QSharedPointer frame); + void sigFileInfo(QSharedPointer frame); private: void onReadyRead(); diff --git a/ClientCore/FileTrans.cpp b/ClientCore/FileTrans.cpp index 85456c9..799a57d 100644 --- a/ClientCore/FileTrans.cpp +++ b/ClientCore/FileTrans.cpp @@ -64,6 +64,8 @@ void FileTrans::ReqDownFile(const TransTask& task) info.toPath = task.localPath; info.fromPath = task.remotePath; + downTask_->task = task; + downTask_->totalSize = 0; downTask_->file.setFileName(Util::Get2FilePath(task.remotePath, task.localPath)); if (!downTask_->file.open(QIODevice::WriteOnly)) { qCritical() << QString(tr("open file [%1] failed.")).arg(downTask_->file.fileName()); @@ -73,29 +75,38 @@ void FileTrans::ReqDownFile(const TransTask& task) auto frame = clientCore_->GetBuffer(info, FBT_CLI_REQ_DOWN, task.remoteId); if (!ClientCore::asyncInvoke(clientCore_, [this, frame]() { return clientCore_->Send(frame); })) { qCritical() << QString(tr("send req send failed: %1")).arg(info.msg); - sendTask_->state = TaskState::STATE_NONE; - sendTask_->file.close(); + downTask_->state = TaskState::STATE_NONE; + downTask_->file.close(); return; } - sendTask_->state = TaskState::STATE_RUNNING; + downTask_->state = TaskState::STATE_RUNNING; } qint32 FileTrans::GetSendProgress() { + if (sendTask_->state == TaskState::STATE_FINISH) { + downTask_->state == TaskState::STATE_NONE; + return 100; + } if (sendTask_->state != TaskState::STATE_RUNNING) { return -1; } - double per = (sendTask_->tranSize * 100.0) / sendTask_->totalSize; return per; } qint32 FileTrans::GetDownProgress() { + if (downTask_->state == TaskState::STATE_FINISH) { + downTask_->state == TaskState::STATE_NONE; + return 100; + } if (downTask_->state != TaskState::STATE_RUNNING) { return -1; } - + if (downTask_->totalSize == 0) { + return 0; + } double per = (downTask_->tranSize * 100.0) / downTask_->totalSize; return per; } @@ -110,6 +121,7 @@ void FileTrans::RegisterSignal() connect(clientCore_, &ClientCore::sigCanotDown, this, [this](QSharedPointer frame) { fbtCanotDown(frame); }); connect(clientCore_, &ClientCore::sigCanDown, this, [this](QSharedPointer frame) { fbtCanDown(frame); }); connect(clientCore_, &ClientCore::sigFileBuffer, this, [this](QSharedPointer frame) { fbtFileBuffer(frame); }); + connect(clientCore_, &ClientCore::sigFileInfo, this, [this](QSharedPointer frame) { fbtFileInfo(frame); }); connect(clientCore_, &ClientCore::sigTransFailed, this, [this](QSharedPointer frame) { fbtTransFailed(frame); }); } @@ -174,6 +186,25 @@ void FileTrans::fbtReqDown(QSharedPointer frame) qCritical() << QString(tr("open file failed: %1")).arg(info.fromPath); return; } + + QFileInfo fileInfo(info.fromPath); + if (fileInfo.exists()) { + qint64 size = fileInfo.size(); + info.permissions = static_cast(fileInfo.permissions()); + info.size = size; + } else { + qCritical() << QString(tr("File [%1] not exit.")).arg(info.fromPath); + doTask->file.close(); + return; + } + + // reply fileinfo + auto f = clientCore_->GetBuffer(info, FBT_CLI_FILE_INFO, frame->fid); + if (!ClientCore::asyncInvoke(clientCore_, [this, f]() { return clientCore_->Send(f); })) { + qCritical() << QString(tr("send file %1 info failed.")).arg(info.fromPath); + doTask->file.close(); + return; + } doTask->task.isUpload = true; doTask->task.localPath = info.fromPath; doTask->task.remoteId = frame->fid; @@ -230,6 +261,15 @@ void FileTrans::fbtFileBuffer(QSharedPointer frame) downTask_->tranSize += ws; } +void FileTrans::fbtFileInfo(QSharedPointer frame) +{ + InfoMsg info = infoUnpack(frame->data); + qInfo() << QString(tr("prepare downfile's size is:%1, perm:%2")).arg(info.size, info.permissions); + downTask_->totalSize = info.size; + downTask_->tranSize = 0; + downTask_->permission = info.permissions; +} + void FileTrans::fbtCanotSend(QSharedPointer frame) { InfoMsg info = infoUnpack(frame->data); diff --git a/ClientCore/FileTrans.h b/ClientCore/FileTrans.h index 25a89a1..5da2260 100644 --- a/ClientCore/FileTrans.h +++ b/ClientCore/FileTrans.h @@ -76,6 +76,7 @@ private: void fbtCanDown(QSharedPointer frame); void fbtCanotDown(QSharedPointer frame); void fbtFileBuffer(QSharedPointer frame); + void fbtFileInfo(QSharedPointer frame); void fbtCanotSend(QSharedPointer frame); void fbtCanSend(QSharedPointer frame); void fbtTransFailed(QSharedPointer frame); diff --git a/Gui/Control/FileControl.cpp b/Gui/Control/FileControl.cpp index a283523..e2ed055 100644 --- a/Gui/Control/FileControl.cpp +++ b/Gui/Control/FileControl.cpp @@ -23,18 +23,20 @@ FileManager::~FileManager() void FileManager::SetModeStr(const QString& modeStr, int type, ClientCore* clientCore) { + cliCore_ = clientCore; + ui->lbMode->setText(modeStr); if (type == 0) { fileHelper_ = std::make_shared(); } else { - cliCore_ = clientCore; auto remotePtr = std::make_shared(); remotePtr->setClientCore(clientCore); ui->tableWidget->setIsRemote(true); - ui->tableWidget->setOwnIDCall([this]() { return cliCore_->GetOwnID(); }); - ui->tableWidget->setRemoteIDCall([this]() { return cliCore_->GetRemoteID(); }); fileHelper_ = remotePtr; } + + ui->tableWidget->setOwnIDCall([this]() { return cliCore_->GetOwnID(); }); + ui->tableWidget->setRemoteIDCall([this]() { return cliCore_->GetRemoteID(); }); ui->tableWidget->setBasePathCall([this]() { return curRoot_; }); connect(fileHelper_.get(), &DirFileHelper::sigHome, this, &FileManager::ShowPath); diff --git a/Gui/Control/cpTableWidget.cpp b/Gui/Control/cpTableWidget.cpp index 68b698c..e6bf3c6 100644 --- a/Gui/Control/cpTableWidget.cpp +++ b/Gui/Control/cpTableWidget.cpp @@ -7,10 +7,17 @@ CpTableWidget::CpTableWidget(QWidget* parent) : QTableWidget(parent) { + } CpTableWidget::~CpTableWidget() { + +} + +void CpTableWidget::dropEvent(QDropEvent* event) +{ + } void CpTableWidget::dragEnterEvent(QDragEnterEvent* event) diff --git a/Gui/Control/cpTableWidget.h b/Gui/Control/cpTableWidget.h index ff40ac4..b1b36da 100644 --- a/Gui/Control/cpTableWidget.h +++ b/Gui/Control/cpTableWidget.h @@ -12,10 +12,8 @@ public: ~CpTableWidget() override; protected: + void dropEvent(QDropEvent* event) override; void dragEnterEvent(QDragEnterEvent* event); - -protected: - QPoint startPos_; }; #endif // CP_TABLEWIDET_H diff --git a/Gui/Form/Transform.cpp b/Gui/Form/Transform.cpp index 038feb6..3a19da8 100644 --- a/Gui/Form/Transform.cpp +++ b/Gui/Form/Transform.cpp @@ -8,10 +8,18 @@ TransForm::TransForm(QWidget* parent) : QDialog(parent), ui(new Ui::TransForm) { ui->setupUi(this); + setWindowTitle(tr("TransProgress")); + ui->edFrom->setReadOnly(true); + ui->edTo->setReadOnly(true); + ui->pedFrom->setReadOnly(true); + ui->pedTo->setReadOnly(true); + ui->edTask->setReadOnly(true); + connect(this, &TransForm::sigProgress, this, &TransForm::setProgress); connect(this, &TransForm::sigDone, this, &TransForm::handleDone); connect(this, &TransForm::sigFailed, this, &TransForm::handleFailed); connect(this, &TransForm::sigSetUi, this, &TransForm::handleUI); + connect(this, &TransForm::sigTaskNum, this, &TransForm::showNum); } TransForm::~TransForm() @@ -35,7 +43,13 @@ void TransForm::SetTasks(const QVector& tasks) void TransForm::startTask() { + qInfo() << "TransForm::startTask enter...."; + curTaskNum_ = 0; for (auto& task : tasks_) { + + QString str = QString(tr("%1/%2")).arg(curTaskNum_).arg(tasks_.size()); + emit sigTaskNum(str); + emit sigSetUi(task); if (task.isUpload) { fileTrans_->ReqSendFile(task); @@ -74,9 +88,13 @@ void TransForm::startTask() QThread::msleep(10); } } + ++curTaskNum_; + + str = QString(tr("%1/%2")).arg(curTaskNum_).arg(tasks_.size()); + emit sigTaskNum(str); } tasks_.clear(); - qDebug() << "TransForm::startTask exit...."; + qInfo() << "TransForm::startTask exit...."; } void TransForm::setProgress(double val) @@ -109,11 +127,16 @@ void TransForm::handleUI(const TransTask& task) } } +void TransForm::showNum(const QString& data) +{ + ui->edTask->setText(data); +} + void TransForm::showEvent(QShowEvent* event) { QDialog::showEvent(event); workTh_ = new TranFromTh(this, this); - //fileTrans_->moveToThread(workTh_); + // fileTrans_->moveToThread(workTh_); connect(workTh_, &QThread::finished, workTh_, &QObject::deleteLater); workTh_->start(); } @@ -121,5 +144,5 @@ void TransForm::showEvent(QShowEvent* event) void TransForm::closeEvent(QCloseEvent* event) { exis_ = true; - QDialog::closeEvent(event); + QDialog::closeEvent(event); } diff --git a/Gui/Form/Transform.h b/Gui/Form/Transform.h index 4ce30f8..f672979 100644 --- a/Gui/Form/Transform.h +++ b/Gui/Form/Transform.h @@ -30,12 +30,14 @@ signals: void sigFailed(); void sigDone(); void sigSetUi(const TransTask& task); + void sigTaskNum(const QString& data); private: void setProgress(double val); void handleFailed(); void handleDone(); void handleUI(const TransTask& task); + void showNum(const QString& data); protected: void showEvent(QShowEvent* event) override; @@ -44,6 +46,7 @@ protected: private: bool exis_{ false }; TranFromTh* workTh_{}; + qint32 curTaskNum_{ 0 }; QVector tasks_; FileTrans* fileTrans_{}; ClientCore* clientCore_{}; diff --git a/Gui/frelayGUI.cpp b/Gui/frelayGUI.cpp index cfd6e13..d281fc0 100644 --- a/Gui/frelayGUI.cpp +++ b/Gui/frelayGUI.cpp @@ -46,7 +46,7 @@ void frelayGUI::InitControl() localFile_ = new FileManager(this); remoteFile_ = new FileManager(this); - localFile_->SetModeStr(tr("Local:")); + localFile_->SetModeStr(tr("Local:"), 0, clientCore_); localFile_->SetOtherSideCall([this]() { return remoteFile_->GetCurRoot(); }); remoteFile_->SetModeStr(tr("Remote:"), 1, clientCore_); remoteFile_->SetOtherSideCall([this]() { return localFile_->GetCurRoot(); }); @@ -90,9 +90,9 @@ void frelayGUI::ControlMsgHander(QtMsgType type, const QMessageLogContext& conte { Q_UNUSED(context); - if (!qApp || !qobject_cast(qApp->activeWindow())) { - return; - } + //if (!qApp || !qobject_cast(qApp->activeWindow())) { + // return; + //} switch (type) { case QtDebugMsg: diff --git a/Protocol/Protocol.h b/Protocol/Protocol.h index e6a93de..766c9a1 100644 --- a/Protocol/Protocol.h +++ b/Protocol/Protocol.h @@ -31,7 +31,8 @@ enum FrameBufferType : uint16_t { FBT_CLI_CANOT_DOWN, FBT_CLI_FILE_BUFFER, FBT_CLI_TRANS_DONE, - FBT_CLI_TRANS_FAILED + FBT_CLI_TRANS_FAILED, + FBT_CLI_FILE_INFO }; struct FrameBuffer {