#include "CopyDatFile.h" #include "ui_CopyDatFile.h" #include #include "NetMgr.h" #include CCopyDatFile::CCopyDatFile(QWidget *parent) : QWidget(parent), ui(new Ui::CCopyDatFile) { ui->setupUi(this); ui -> treeWidget_fileList -> setRootIsDecorated(false); ui -> treeWidget_fileList -> setHeaderLabels(QStringList() << tr("名字") << tr("大小(KB)") << tr("类型")<< tr("权限") << tr("拥有者") << tr("所属组") << tr("时间")); ui -> treeWidget_dist -> setHeaderLabels(QStringList() << tr("名字") << tr("大小(KB)") << tr("类型")<< tr("权限") << tr("拥有者") << tr("所属组") << tr("时间")); ui->treeWidget_fileList->header()->setSectionResizeMode(QHeaderView::ResizeToContents); ui -> treeWidget_fileList -> header() ->setStretchLastSection(false); ui->treeWidget_dist->header()->setSectionResizeMode(QHeaderView::ResizeToContents); ui -> treeWidget_dist -> header() ->setStretchLastSection(false); ui->treeWidget_fileList->setContextMenuPolicy(Qt::CustomContextMenu); QObject::connect(ui->treeWidget_fileList ,SIGNAL(customContextMenuRequested(const QPoint &)),this,SLOT(on_treeWidget_customContextMenuRequested(const QPoint &))); connect(ui->treeWidget_fileList, &QTreeWidget::itemDoubleClicked, this, &CCopyDatFile::onItemDoubleClicked); connect(ui->treeWidget_dist, &QTreeWidget::itemDoubleClicked, this, &CCopyDatFile::onItemDoubleClickedDist); ui->label_path->setText("/run/media/"); connectToFtp(); } CCopyDatFile::~CCopyDatFile() { scp.disconnectFromHost(); delete ui; } void CCopyDatFile::connectToFtp() { bool res = scp.connectToHost(IP, 22, "root", "@#cidw!@123456"); if(!res){ QMessageBox::information(this, QStringLiteral("提示"), "连接失败!"); return; } QString path = "/run/media/"; QVector files ; scp.listRemoteDir(path,files); fillTreeWidget(ui->treeWidget_fileList, files, path); } static QString permToString(uint perm) { QString s; s += (perm & 0400) ? "r" : "-"; s += (perm & 0200) ? "w" : "-"; s += (perm & 0100) ? "x" : "-"; s += (perm & 0040) ? "r" : "-"; s += (perm & 0020) ? "w" : "-"; s += (perm & 0010) ? "x" : "-"; s += (perm & 0004) ? "r" : "-"; s += (perm & 0002) ? "w" : "-"; s += (perm & 0001) ? "x" : "-"; return s; } void CCopyDatFile::fillTreeWidget( QTreeWidget *tree, const QVector &files, const QString ¤tPath) { tree->clear(); for (const auto &f : files) { QTreeWidgetItem *item = new QTreeWidgetItem(tree); item->setText(ColName, f.name); if (f.isDir) { item->setText(ColSize, ""); } else { item->setText(ColSize, QString::number((f.size + 1023) / 1024)); item->setTextAlignment(1, Qt::AlignRight | Qt::AlignVCenter); } item->setText(ColType, f.isDir ? "Dir" : "File"); item->setText(ColPerm, permToString(f.permissions)); item->setText(ColOwner, f.owner); item->setText(ColGroup, f.group); item->setText(ColModified, f.lastModified.toString("yyyy-MM-dd HH:mm:ss")); item->setData(ColName, Qt::UserRole + 1, f.name); // 📁 目录加图标 if (f.isDir) { item->setIcon(ColName, QApplication::style()->standardIcon(QStyle::SP_DirIcon)); } else { item->setIcon(ColName, QApplication::style()->standardIcon(QStyle::SP_FileIcon)); } // 存远程完整路径(关键) QString fullPath = currentPath; if (!fullPath.endsWith('/')) fullPath += '/'; fullPath += f.name; item->setData(ColName, Qt::UserRole, fullPath); } } void CCopyDatFile::on_pushButton_refresh_clicked() { QString path_dist = "/run/media"; QVector files ; scp.listRemoteDir(path_dist,files); fillTreeWidget(ui->treeWidget_dist, files, path_dist); m_strDistPath = "/run/media"; } void CCopyDatFile::onItemDoubleClicked(QTreeWidgetItem *item, int column) { if (item->text(ColType) != "Dir") return; QString path = item->data(ColName, Qt::UserRole).toString(); QVector files ; scp.listRemoteDir(path,files); fillTreeWidget(ui->treeWidget_fileList, files, path); ui->treeWidget_fileList->setProperty("currentPath", path); ui->label_path->setText(path); m_strFilePath = path; } void CCopyDatFile::onItemDoubleClickedDist(QTreeWidgetItem *item, int column){ if (item->text(ColType) != "Dir") return; QString path = item->data(ColName, Qt::UserRole).toString(); QVector files ; scp.listRemoteDir(path,files); fillTreeWidget(ui->treeWidget_dist, files, path); ui->treeWidget_dist->setProperty("currentPath", path); ui->label_path->setText(path); m_strDistPath = path; } void CCopyDatFile::slotCopyItem() { QTreeWidgetItem *item = ui->treeWidget_fileList->currentItem(); //获取光标所在位置的Item QString str = item->data(0, Qt::UserRole).toString(); qDebug() << str << item << endl; QJsonObject allObj,cmdBody; allObj.insert("cmd", "100"); cmdBody.insert("subcmd",1); cmdBody.insert("srcfile",str); cmdBody.insert("dstdirectory",m_strDistPath); allObj["cmdBody"] = cmdBody; QNetworkRequest req; QString sUrl = QString("http://%1/cgi-bin/General.cgi/").arg(IP); req.setUrl(sUrl); g_NetMgr->PostJson(req,allObj); } void CCopyDatFile::on_treeWidget_customContextMenuRequested(const QPoint &pos) { int iCount = ui->treeWidget_dist->topLevelItemCount(); if(iCount <= 0) return; QMenu menu; copyAction = new QAction("复制"); connect(copyAction, &QAction::triggered, this, &CCopyDatFile::slotCopyItem, Qt::UniqueConnection); menu.addAction(copyAction); menu.exec(QCursor::pos()); //显示菜单 } void CCopyDatFile::on_pushButton_exit_clicked() { QJsonObject allObj,cmdBody; allObj.insert("cmd", "100"); cmdBody.insert("subcmd",3); cmdBody.insert("dstdirectory",m_strDistPath); allObj["cmdBody"] = cmdBody; QNetworkRequest req; QString sUrl = QString("http://%1/cgi-bin/General.cgi/").arg(IP); req.setUrl(sUrl); g_NetMgr->PostJson(req,allObj); } void CCopyDatFile::on_pushButton_up_1_clicked() { QVector files ; QString retPath; scp.goUp(m_strFilePath,files,retPath); fillTreeWidget(ui->treeWidget_fileList, files, retPath); ui->treeWidget_fileList->setProperty("currentPath", retPath); ui->label_path->setText(retPath); } void CCopyDatFile::on_pushButton_up_2_clicked() { QVector files ; QString retPath; scp.goUp(m_strDistPath,files,retPath); fillTreeWidget(ui->treeWidget_dist, files, retPath); ui->treeWidget_fileList->setProperty("currentPath", retPath); ui->label_path->setText(retPath); }