diff --git a/ClientCore/ClientCore.cpp b/ClientCore/ClientCore.cpp index badb2b1..439726a 100644 --- a/ClientCore/ClientCore.cpp +++ b/ClientCore/ClientCore.cpp @@ -132,6 +132,16 @@ void ClientCore::handleAsk(QSharedPointer frame) } return; } + if (msg.command == STRMSG_AC_NEW_DIR) { + msg.command = STRMSG_AC_ANSWER_NEW_DIR; + msg.msg = Util::NewDir(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 9529746..5477ae2 100644 --- a/Gui/Control/FileControl.cpp +++ b/Gui/Control/FileControl.cpp @@ -241,6 +241,72 @@ void FileManager::SortFileInfo(SortMethod method) } } +void FileManager::ShowFileItem(const DirFileInfo& f, int i) +{ + // *********************************************************************************** + auto* iconItem = new QTableWidgetItem(""); + iconItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter); + iconItem->setFlags(iconItem->flags() & ~Qt::ItemIsEditable); + ui->tableWidget->setItem(i, 0, iconItem); + + // *********************************************************************************** + auto* fileItem = new QTableWidgetItem(f.name); + fileItem->setFlags(fileItem->flags() & ~Qt::ItemIsEditable); + ui->tableWidget->setItem(i, 1, fileItem); + QDateTime modifyTime = QDateTime::fromMSecsSinceEpoch(f.lastModified); + + // *********************************************************************************** + QString timeStr = modifyTime.toString("yyyy-MM-dd hh:mm:ss"); + auto* timeItem = new QTableWidgetItem(timeStr); + timeItem->setFlags(timeItem->flags() & ~Qt::ItemIsEditable); + ui->tableWidget->setItem(i, 2, timeItem); + + // *********************************************************************************** + QString typeStr; + switch (f.type) { + case File: + typeStr = "File"; + iconItem->setIcon(QApplication::style()->standardIcon(QStyle::SP_FileIcon)); + break; + case Dir: + typeStr = "Dir"; + iconItem->setIcon(QApplication::style()->standardIcon(QStyle::SP_DirIcon)); + break; + case Link: + typeStr = "Link"; + break; + case Other: + typeStr = "Other"; + break; + default: + typeStr = "Unknown"; + break; + } + + // *********************************************************************************** + auto* typeItem = new QTableWidgetItem(typeStr); + typeItem->setFlags(typeItem->flags() & ~Qt::ItemIsEditable); + ui->tableWidget->setItem(i, 3, typeItem); + + // *********************************************************************************** + QString sizeStr; + if (f.size < 1024) { + sizeStr = QString::number(f.size) + " B"; + } else if (f.size < 1024 * 1024) { + sizeStr = QString::number(f.size / 1024.0, 'f', 2) + " KB"; + } else { + sizeStr = QString::number(f.size / (1024.0 * 1024.0), 'f', 2) + " MB"; + } + QTableWidgetItem* item = nullptr; + if (f.type == File) { + item = new QTableWidgetItem(sizeStr); + } else { + item = new QTableWidgetItem(""); + } + item->setFlags(item->flags() & ~Qt::ItemIsEditable); + ui->tableWidget->setItem(i, 4, item); +} + void FileManager::RefreshTab() { userScrol_ = false; @@ -251,70 +317,7 @@ void FileManager::RefreshTab() ui->tableWidget->insertRow(ui->tableWidget->rowCount()); const DirFileInfo& fileInfo = currentShowInfo_.vec[i]; - - // *********************************************************************************** - auto* iconItem = new QTableWidgetItem(""); - iconItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter); - iconItem->setFlags(iconItem->flags() & ~Qt::ItemIsEditable); - ui->tableWidget->setItem(i, 0, iconItem); - - // *********************************************************************************** - auto* fileItem = new QTableWidgetItem(fileInfo.name); - fileItem->setFlags(fileItem->flags() & ~Qt::ItemIsEditable); - ui->tableWidget->setItem(i, 1, fileItem); - QDateTime modifyTime = QDateTime::fromMSecsSinceEpoch(fileInfo.lastModified); - - // *********************************************************************************** - QString timeStr = modifyTime.toString("yyyy-MM-dd hh:mm:ss"); - auto* timeItem = new QTableWidgetItem(timeStr); - timeItem->setFlags(timeItem->flags() & ~Qt::ItemIsEditable); - ui->tableWidget->setItem(i, 2, timeItem); - - // *********************************************************************************** - QString typeStr; - switch (fileInfo.type) { - case File: - typeStr = "File"; - iconItem->setIcon(QApplication::style()->standardIcon(QStyle::SP_FileIcon)); - break; - case Dir: - typeStr = "Dir"; - iconItem->setIcon(QApplication::style()->standardIcon(QStyle::SP_DirIcon)); - break; - case Link: - typeStr = "Link"; - break; - case Other: - typeStr = "Other"; - break; - default: - typeStr = "Unknown"; - break; - } - - // *********************************************************************************** - auto* typeItem = new QTableWidgetItem(typeStr); - typeItem->setFlags(typeItem->flags() & ~Qt::ItemIsEditable); - ui->tableWidget->setItem(i, 3, typeItem); - - // *********************************************************************************** - QString sizeStr; - if (fileInfo.size < 1024) { - sizeStr = QString::number(fileInfo.size) + " B"; - } else if (fileInfo.size < 1024 * 1024) { - sizeStr = QString::number(fileInfo.size / 1024.0, 'f', 2) + " KB"; - } else { - sizeStr = QString::number(fileInfo.size / (1024.0 * 1024.0), 'f', 2) + " MB"; - } - QTableWidgetItem* item = nullptr; - if (fileInfo.type == File) { - item = new QTableWidgetItem(sizeStr); - } else { - item = new QTableWidgetItem(""); - } - item->setFlags(item->flags() & ~Qt::ItemIsEditable); - ui->tableWidget->setItem(i, 4, item); - + ShowFileItem(fileInfo, i); if (i % 50 == 0) { QGuiApplication::processEvents(); } @@ -541,6 +544,77 @@ void FileManager::UpDown() void FileManager::OperNewFolder() { + 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; + } + } else { + return; + } + + auto folder = Util::Join(GetRoot(), text); + if (!isRemote_) { + QString ret = Util::NewDir(folder); + if (ret.isEmpty()) { + QMessageBox::information(this, tr("提示"), tr("创建%1成功").arg(folder)); + DirFileInfo nf; + nf.size = 0; + nf.type = Dir; + nf.fullPath = folder; + nf.name = text; + nf.lastModified = QDateTime::currentDateTime().toMSecsSinceEpoch(); + ui->tableWidget->insertRow(0); + ShowFileItem(nf, 0); + } else { + QMessageBox::information(this, tr("提示"), ret); + } + return; + } + + WaitOper oper(this); + oper.SetClient(cliCore_); + oper.SetType(STRMSG_AC_NEW_DIR, STRMSG_AC_ANSWER_NEW_DIR); + oper.SetPath(folder, "", ""); + + 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("新建%1成功。")).arg(folder)); + DirFileInfo nf; + nf.size = 0; + nf.type = Dir; + nf.fullPath = folder; + nf.name = text; + nf.lastModified = QDateTime::currentDateTime().toMSecsSinceEpoch(); + ui->tableWidget->insertRow(0); + ShowFileItem(nf, 0); + } } void FileManager::OperDelete() @@ -640,8 +714,7 @@ void FileManager::OperRename() if (text.isEmpty()) { return; } - } - else { + } else { return; } diff --git a/Gui/Control/FileControl.h b/Gui/Control/FileControl.h index 5d05050..d75f7e8 100644 --- a/Gui/Control/FileControl.h +++ b/Gui/Control/FileControl.h @@ -5,9 +5,9 @@ #include #include #include -#include #include #include +#include #include #include #include @@ -72,6 +72,7 @@ private: void InitMenu(); void ShowPath(const QString& path, const QVector& drivers); void ShowFile(const DirFileInfoVec& info); + void ShowFileItem(const DirFileInfo& f, int i); void doubleClick(int row, int column); void SetRoot(const QString& path); void SortFileInfo(SortMethod method); diff --git a/Note/version.md b/Note/version.md index 85f03e7..f11dec5 100644 --- a/Note/version.md +++ b/Note/version.md @@ -16,7 +16,7 @@ | 37 | 问题 | 未连接对方客户端时,执行对照传输会崩溃。 | 待复现 | 0.2.3 | | | 36 | 功能 | 备份文件功能。 | | 0.2.3 | | | 35 | 功能 | 完善对照功能的界面其他内容。 | | 0.2.3 | | -| 34 | 功能 | 支持删除、重命名、新建文件(夹)。 | | 0.2.3 | | +| 34 | 功能 | 支持删除、重命名、新建文件(夹)。 | | 0.2.3 | 0.2.4 | | 33 | 功能 | 弹窗查看文件(夹)属性窗口。 | | 0.2.3 | 0.2.4 | | 32 | 功能 | 路径选择显示驱动器。 | | 0.2.2 | 0.2.3 | | 31 | 功能 | Server端buffer大小判断,有过多无效数据则踢出该客户端。 | | 0.2.2 | 0.2.4 | diff --git a/Util/Util.cpp b/Util/Util.cpp index 093ec6b..0f4bff0 100644 --- a/Util/Util.cpp +++ b/Util/Util.cpp @@ -279,4 +279,34 @@ QString Util::Delete(const QString& path) } else { return tr("不支持的文件类型: %1").arg(path); } +} + +QString Util::NewDir(const QString& path) +{ + if (path.isEmpty()) { + return tr("路径不能为空"); + } + + if (QDir(path).exists()) { + return tr("目录已存在: %1").arg(path); + } + + QFileInfo pathInfo(path); + QDir parentDir = pathInfo.absoluteDir(); + + if (!parentDir.exists()) { + return tr("父目录不存在: %1").arg(parentDir.absolutePath()); + } + + QFileInfo parentInfo(parentDir.absolutePath()); + if (!parentInfo.isWritable()) { + return tr("父目录无写入权限: %1").arg(parentDir.absolutePath()); + } + + QDir dir; + if (dir.mkpath(path)) { + return ""; + } else { + return tr("创建目录失败: %1").arg(path); + } } \ No newline at end of file diff --git a/Util/Util.h b/Util/Util.h index 24efff4..053ab44 100644 --- a/Util/Util.h +++ b/Util/Util.h @@ -53,6 +53,7 @@ public: 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 NewDir(const QString& path); static QString UUID(); static QVector GetLocalDrivers(); };