3500/CopyDatFile.cpp
2024-10-24 13:49:00 +08:00

190 lines
6.3 KiB
C++

#include "CopyDatFile.h"
#include "ui_CopyDatFile.h"
#include <QMenu>
#include "NetMgr.h"
#include <QNetworkRequest>
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("时间"));
ui -> treeWidget_dist -> setHeaderLabels(QStringList() << tr("名字") << tr("大小(KB)") << 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_dist, &QTreeWidget::itemActivated,this, &CCopyDatFile::processItem);
ui->label_path->setText("/run/media/");
connectToFtp();
}
CCopyDatFile::~CCopyDatFile()
{
delete ui;
}
void CCopyDatFile::connectToFtp()
{
ftp = new QFtp(this);
ftp -> connectToHost(IP,21);
ftp -> login("root","@#cidw!@123456");
connect(ftp, SIGNAL(commandFinished(int, bool)),this, SLOT(ftpCommandFinished(int, bool)));
connect(ftp, SIGNAL(listInfo(const QUrlInfo&)),this, SLOT(addToList(const QUrlInfo&)));
connect(ftp, SIGNAL(listInfo(const QUrlInfo&)),this, SLOT(addToDistList(const QUrlInfo&)));
}
void CCopyDatFile::ftpCommandFinished(int, bool error)
{
qDebug() << ftp->currentCommand() <<endl;
if (ftp->currentCommand() == QFtp::ConnectToHost)
{
if (error) {
return;
}
else {
return;
}
}
if (ftp->currentCommand() == QFtp::Login){
qDebug() << "连接成功!" << endl;
ftp -> cd("/var/www/html/cidwdat/");
ftp->list();
}
if (ftp->currentCommand() == QFtp::Get) {
if (error) {
} else {
}
} else if (ftp->currentCommand() == QFtp::List) {
qDebug() << "List" << endl;
}
}
void CCopyDatFile::addToList(const QUrlInfo &urlInfo)
{
QTreeWidgetItem *item = new QTreeWidgetItem;
item->setText(0, urlInfo.name());
item->setText(1, QString::number(urlInfo.size()));
item->setText(2, urlInfo.owner());
item->setText(3, urlInfo.group());
item->setText(4, urlInfo.lastModified().toString("yyyy-MM-dd HH:mm:ss").toLocal8Bit().data());
QPixmap pixmap(urlInfo.isDir() ? ":/images/images/dir.png" : ":/images/images/file.png");
item->setIcon(0, pixmap);
item->setData(0, Qt::UserRole,urlInfo.name());
isDirectory[urlInfo.name()] = urlInfo.isDir();
this -> ui -> treeWidget_fileList->addTopLevelItem(item);
if (!this -> ui -> treeWidget_fileList->currentItem()) {
this -> ui -> treeWidget_fileList->setCurrentItem(this -> ui -> treeWidget_fileList->topLevelItem(0));
this -> ui -> treeWidget_fileList->setEnabled(true);
}
}
void CCopyDatFile::addToDistList(const QUrlInfo &urlInfo)
{
QTreeWidgetItem *item = new QTreeWidgetItem;
item->setText(0, urlInfo.name());
item->setText(1, QString::number(urlInfo.size()));
item->setText(2, urlInfo.owner());
item->setText(3, urlInfo.group());
item->setText(4, urlInfo.lastModified().toString("yyyy-MM-dd HH:mm:ss").toLocal8Bit().data());
QPixmap pixmap(urlInfo.isDir() ? ":/images/images/dir.png" : ":/images/images/file.png");
item->setIcon(0, pixmap);
isDirectory[urlInfo.name()] = urlInfo.isDir();
this -> ui -> treeWidget_dist->addTopLevelItem(item);
if (!this -> ui -> treeWidget_dist->currentItem()) {
this -> ui -> treeWidget_dist->setCurrentItem(this -> ui -> treeWidget_dist->topLevelItem(0));
this -> ui -> treeWidget_dist->setEnabled(true);
}
}
void CCopyDatFile::on_pushButton_refresh_clicked()
{
disconnect(ftp, SIGNAL(listInfo(const QUrlInfo&)),this, SLOT(addToList(const QUrlInfo&)));
ui -> treeWidget_dist ->clear();
ftp -> cd("/run/media/");
ui->label_path->setText("/run/media/");
m_strDistPath = "";
m_strDistPath = "/run/media/";
ftp->list();
}
void CCopyDatFile::processItem(QTreeWidgetItem *item, int /*column*/)
{
QString name = item->text(0);
if (isDirectory.value(name)) {
this -> ui -> treeWidget_dist->clear();
isDirectory.clear();
currentPath += '/';
currentPath += name;
ftp->cd(name);
m_strDistPath = "";
m_strDistPath = "/run/media" + currentPath;
ui->label_path->setText(m_strDistPath);
currentPath = "";
ftp->list();
return;
}
}
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);
}