#include "PromangeDlg.h"
#include <QMessageBox>
#include <QTableWidgetItem>
#include <QStringList>
#include <QFileDialog>
#include <QTextCodec>
#include <QTimer>
#include <QDataStream>
QString ftpIP="192.168.99.22";
int ftpPort=21;
QString ftpUser="root";
QString ftpPassword="xuanjing";
extern QString ServerIp;
network *m_net;
/******************************************************
*函数名称:CRC32Le
*输 入:p 要校验的数据
len 校验数据的长
*输 出:校验值
*功 能:循环冗余校验-32
(0xedb88320)
*******************************************************/
#define CRCPOLY_LE 0xedb88320
int crc32_le(unsigned char const *p, unsigned long len)
{
int i;
int crc = 0;
while (len--) {
crc ^= *p++;
for (i = 0; i < 8; i++)
crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
}
return crc;
}
PromangeDlg::PromangeDlg(QWidget *parent)
: QMainWindow(parent), Shakehands(0), sendok(0),puttype(0), bitfilemark1(0), bitfilemark2(0)
{
ui.setupUi(this);
//ui.frame->setStyleSheet("QFrame{background-color:rgbk(150,150,150,100);}");
//this->installEventFilter(this);
setFixedSize(this->width(), this->height());
m_net = new network(this);
connect(m_net, SIGNAL(devslg(int)),this,SLOT(replaydevslg(int)));
ui.update_msg->setStyleSheet("QLabel{color:red;}");
m_ftp = new QFtp;
connect(m_ftp, SIGNAL(commandFinished(int, bool)), this, SLOT(replycommandFinished(int, bool)));
connect(m_ftp,SIGNAL(listInfo(const QUrlInfo&)),this,SLOT(filelist(const QUrlInfo&)));
connect(m_ftp, SIGNAL(dataTransferProgress(qint64, qint64)), this, SLOT(updatedataTransferProgress(qint64, qint64)));
getfilelist = false;
ui.tableWidget->setColumnWidth(0, 350);
ui.tableWidget->setColumnWidth(1, 150);
ui.tableWidget->setColumnWidth(2, 200);
ui.tableWidget->setColumnWidth(3, 150);
ui.tableWidget->setColumnWidth(4, 100);
//ui.tableWidget->setAlternatingRowColors(true);
ui.tableWidget->setEditTriggers(QTableWidget::NoEditTriggers);
ui.ftp_passwd->setEchoMode(QLineEdit::Password);
ui.ftp_url->setPlaceholderText("192.168.8.8");
ui.ftp_user->setPlaceholderText("root");
ui.ftp_passwd->setPlaceholderText("******");
m_movie = new QMovie("./img/mo4.gif");
m_lable = new QLabel(this);
m_lable->setGeometry(this->width() / 2 - 50, this->height() / 2 - 60, 100, 100);
m_lable->setAttribute(Qt::WA_TranslucentBackground);
m_lable->setMovie(m_movie);
m_timer = new QTimer;
connect(m_timer, SIGNAL(timeout()),this,SLOT(replaytimeout()));
}
PromangeDlg::~PromangeDlg()
{
delete m_ftp;
}
void PromangeDlg::replycommandFinished(int id,bool err)
{
if (m_ftp->currentCommand()==QFtp::ConnectToHost)
{
if (err)
{
QMessageBox msg(QMessageBox::NoIcon,"提示","无法连接文件服务器");
msg.exec();
}
}
else if (m_ftp->currentCommand() == QFtp::Login)
{
if (err)
{
QMessageBox msg(QMessageBox::NoIcon, "提示", "连接到文件服务器的账户信息错误");
msg.exec();
}
}
else if (m_ftp->currentCommand()==QFtp::Put)
{
ui.progressBar->setMaximum(100);
ui.progressBar->setValue(0);
if (err)
{
QMessageBox::information(NULL, "提示", "上传文件失败!");
}
else
{
if (puttype ==0) //上传视频
{
getfilelist = true;
m_ftp->list("/home/ftp/upload/");
sendok = true;
QByteArray ba;
ba.append("AAAAA1");
if (!m_net->sendcmd(ba))
{
//QMessageBox::information(this, tr("提示"), tr("发布失败,通信故障!"));
}
ui.btn_del->setEnabled(true);
ui.btn_export->setEnabled(true);
ui.btn_find->setEnabled(true);
ui.btn_import->setEnabled(true);
ui.groupBox_up->setEnabled(true);
}
else if (puttype == 2) //升级固件 fpga
{
m_movie->start();
m_lable->show();
m_timer->start(1000); //启动检测定时器
m_ftp->rename("/home/ftp/bin/updatebak.bin", "/home/ftp/bin/update.bin");
ui.update_msg->setText("等待设备固化升级文件,请稍后...");
}
else if (puttype == 1) //升级固件
{
m_ftp->rename("/home/ftp/bin/updatebak.bin", "/home/ftp/bin/update.bin");
ui.btn_del->setEnabled(true);
ui.btn_export->setEnabled(true);
ui.btn_find->setEnabled(true);
ui.btn_import->setEnabled(true);
ui.groupBox_up->setEnabled(true);
QMessageBox::information(this, tr("提示"), tr("固件1升级成功!"));
}
}
m_pFile->close();
}
else if (m_ftp->currentCommand()==QFtp::Get)
{
if (err)
{
QMessageBox::information(NULL, "提示", "下载失败!");
}
else
{
QMessageBox::information(NULL, "提示", "下载成功!");
}
m_pFileexport->close();
ui.progressBar->setMaximum(100);
ui.progressBar->setValue(0);
ui.btn_del->setEnabled(true);
ui.btn_export->setEnabled(true);
ui.btn_find->setEnabled(true);
}
else if (m_ftp->currentCommand() == QFtp::Remove)
{
if (err)
{
QMessageBox::information(NULL, "提示", "删除失败!");
}
else
{
QMessageBox::information(NULL, "提示", "删除成功!");
getfilelist = true;
puttype = 0;
m_ftp->list("/home/ftp/upload/");
}
}
if (m_ftp->currentCommand()==QFtp::List)
{
}
}
void PromangeDlg::filelist(const QUrlInfo& urlinfo)
{
if (puttype==0)
{
if (getfilelist)
{
ui.tableWidget->setRowCount(0);
ui.tableWidget->clearContents();
m_row = 0;
getfilelist = false;
}
QString filename = QString::fromUtf8(urlinfo.name().toLatin1());
ui.tableWidget->insertRow(m_row);
ui.tableWidget->setItem(m_row, 0,new QTableWidgetItem(filename));
ui.tableWidget->setItem(m_row, 1, new QTableWidgetItem(m_net->byteconvertmkb(urlinfo.size())));
ui.tableWidget->setItem(m_row, 2, new QTableWidgetItem(urlinfo.lastModified().toString("yyyy-MM-dd hh:mm:ss")));
QString filetype;
if (urlinfo.isDir())
{
filetype = "文件夹";
}
else
{
QStringList strlist = filename.split(".");
if (strlist.count()>=2)
{
int kk = strlist.count() - 1;
if (strlist[kk]=="bmp" || strlist[kk] == "gif"|| strlist[kk] == "jpg"|| strlist[kk] == "png")
{
filetype = "图片文件";
}
else if (strlist[kk] == "avi"|| strlist[kk] == "mpg"|| strlist[kk] == "mov"|| strlist[kk] == "mp4")
{
filetype = "视频文件";
}
else if (strlist[kk] == "txt" || strlist[kk] == "ini" )
{
filetype = "文本文件";
}
else
filetype = "文件";
}
else
{
filetype = "文件";
}
}
ui.tableWidget->setItem(m_row, 3, new QTableWidgetItem(filetype));
ui.tableWidget->setItem(m_row, 4, new QTableWidgetItem(QString::number(urlinfo.permissions())));
m_row++;
}
else if (puttype==2) //fpga
{
QString filename = QString::fromUtf8(urlinfo.name().toLatin1());
if (filename=="bitstream")
{
bitfilemark1 = 1;
bitfilemark2 = 1;
}
}
}
void PromangeDlg::updatedataTransferProgress(qint64 readbyte, qint64 totalbyte)
{
ui.progressBar->setMaximum(totalbyte);
ui.progressBar->setValue(readbyte);
}
void PromangeDlg::importfile()
{
static QString oldpath = "."; //*.avi *.mp4 *.mov *.rmvb *.rm *.asf *.divx *.mpg *.*.aac *.wmv *.jpg *.png
QString path = QFileDialog::getOpenFileName(this, "选择上传文件", oldpath, "files(*.*)");
oldpath = path;
if (path.length() != 0)
{
m_pFile = new QFile(path);
if (!m_pFile->open(QIODevice::ReadOnly))
{
m_pFile->close();
QMessageBox::information(this, tr("提示"), tr("文件打开失败!"));
return;
}
int i = path.lastIndexOf('/');
QString importfilename = path.right(path.length() - i - 1);
Shakehands = 0;
sendok = false;
QByteArray ba;
ba.append("AAAAA2");
if (m_net->sendcmd(ba))
{
if (AckWait(1000 * 3))
{
puttype = 0;
m_ftp->put(m_pFile, "/home/ftp/upload/" + QString::fromLatin1(importfilename.toUtf8()));
ui.btn_del->setEnabled(false);
ui.btn_export->setEnabled(