3500/CopyDatFile.cpp

208 lines
6.9 KiB
C++
Raw Permalink Normal View History

2023-10-19 14:15:31 +08:00
#include "CopyDatFile.h"
#include "ui_CopyDatFile.h"
#include <QMenu>
#include "NetMgr.h"
#include <QNetworkRequest>
2026-02-04 17:58:54 +08:00
2023-10-19 14:15:31 +08:00
CCopyDatFile::CCopyDatFile(QWidget *parent) :
QWidget(parent),
ui(new Ui::CCopyDatFile)
{
ui->setupUi(this);
ui -> treeWidget_fileList -> setRootIsDecorated(false);
2026-02-04 17:58:54 +08:00
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("时间"));
2023-10-19 14:15:31 +08:00
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 &)));
2026-02-03 20:31:37 +08:00
connect(ui->treeWidget_fileList, &QTreeWidget::itemDoubleClicked,
this, &CCopyDatFile::onItemDoubleClicked);
2026-02-04 17:58:54 +08:00
connect(ui->treeWidget_dist, &QTreeWidget::itemDoubleClicked,
this, &CCopyDatFile::onItemDoubleClickedDist);
2026-02-03 20:31:37 +08:00
2023-10-19 14:15:31 +08:00
ui->label_path->setText("/run/media/");
connectToFtp();
}
CCopyDatFile::~CCopyDatFile()
{
2026-02-03 20:31:37 +08:00
scp.disconnectFromHost();
2023-10-19 14:15:31 +08:00
delete ui;
}
void CCopyDatFile::connectToFtp()
{
2026-02-04 17:58:54 +08:00
bool res = scp.connectToHost(IP, 22, "root", "@#cidw!@123456");
2026-02-03 20:31:37 +08:00
if(!res){
QMessageBox::information(this, QStringLiteral("提示"), "连接失败!");
return;
}
2026-02-04 17:58:54 +08:00
QString path = "/run/media/";
2026-02-03 20:31:37 +08:00
QVector<RemoteFileInfo> files ;
scp.listRemoteDir(path,files);
fillTreeWidget(ui->treeWidget_fileList, files, path);
2023-10-19 14:15:31 +08:00
}
2026-02-03 20:31:37 +08:00
static QString permToString(uint perm)
2023-10-19 14:15:31 +08:00
{
2026-02-03 20:31:37 +08:00
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;
2023-10-19 14:15:31 +08:00
}
2026-02-03 20:31:37 +08:00
void CCopyDatFile::fillTreeWidget(
QTreeWidget *tree,
const QVector<RemoteFileInfo> &files,
const QString &currentPath)
2023-10-19 14:15:31 +08:00
{
2026-02-03 20:31:37 +08:00
tree->clear();
for (const auto &f : files) {
QTreeWidgetItem *item = new QTreeWidgetItem(tree);
item->setText(ColName, f.name);
2026-02-04 17:58:54 +08:00
if (f.isDir) {
item->setText(ColSize, "");
} else {
item->setText(ColSize, QString::number((f.size + 1023) / 1024));
item->setTextAlignment(1, Qt::AlignRight | Qt::AlignVCenter);
}
2026-02-03 20:31:37 +08:00
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"));
2026-02-04 17:58:54 +08:00
item->setData(ColName, Qt::UserRole + 1, f.name);
2026-02-03 20:31:37 +08:00
// 📁 目录加图标
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);
2023-10-19 14:15:31 +08:00
}
}
2026-02-03 20:31:37 +08:00
2023-10-19 14:15:31 +08:00
void CCopyDatFile::on_pushButton_refresh_clicked()
{
2026-02-03 20:31:37 +08:00
QString path_dist = "/run/media";
2026-02-04 17:58:54 +08:00
QVector<RemoteFileInfo> files ;
scp.listRemoteDir(path_dist,files);
fillTreeWidget(ui->treeWidget_dist, files, path_dist);
m_strDistPath = "/run/media";
2026-02-03 20:31:37 +08:00
}
void CCopyDatFile::onItemDoubleClicked(QTreeWidgetItem *item, int column)
{
if (item->text(ColType) != "Dir")
2026-02-04 17:58:54 +08:00
return;
2026-02-03 20:31:37 +08:00
QString path = item->data(ColName, Qt::UserRole).toString();
QVector<RemoteFileInfo> files ;
scp.listRemoteDir(path,files);
fillTreeWidget(ui->treeWidget_fileList, files, path);
ui->treeWidget_fileList->setProperty("currentPath", path);
ui->label_path->setText(path);
2026-02-04 17:58:54 +08:00
m_strFilePath = path;
2023-10-19 14:15:31 +08:00
}
2026-02-04 17:58:54 +08:00
void CCopyDatFile::onItemDoubleClickedDist(QTreeWidgetItem *item, int column){
if (item->text(ColType) != "Dir")
return;
2023-10-19 14:15:31 +08:00
2026-02-04 17:58:54 +08:00
QString path = item->data(ColName, Qt::UserRole).toString();
QVector<RemoteFileInfo> 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;
2023-10-19 14:15:31 +08:00
}
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);
2024-10-24 13:49:00 +08:00
cmdBody.insert("dstdirectory",m_strDistPath);
2023-10-19 14:15:31 +08:00
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);
2024-10-24 13:49:00 +08:00
cmdBody.insert("dstdirectory",m_strDistPath);
2023-10-19 14:15:31 +08:00
allObj["cmdBody"] = cmdBody;
QNetworkRequest req;
QString sUrl = QString("http://%1/cgi-bin/General.cgi/").arg(IP);
req.setUrl(sUrl);
g_NetMgr->PostJson(req,allObj);
}
2026-02-04 17:58:54 +08:00
void CCopyDatFile::on_pushButton_up_1_clicked()
{
QVector<RemoteFileInfo> 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<RemoteFileInfo> 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);
}