436 lines
11 KiB
C++
436 lines
11 KiB
C++
#include "Worker.h"
|
|
#include <QFileInfo>
|
|
#include <QDir>
|
|
#include <QProcess>
|
|
#include <QTextCodec>
|
|
#include <QtXml>
|
|
#include <QApplication>
|
|
#include "FileInfo.h"
|
|
#include "Utils.h"
|
|
#include <QEventLoop>
|
|
|
|
|
|
Worker::Worker()
|
|
{
|
|
qDebug() << "线程初始化";
|
|
// 初始化文件总大小
|
|
m_downloadSize=0;
|
|
m_filesSize = 0;
|
|
// 保存XML文件下载地址
|
|
m_url = "http://88.151.33.134:5666/Crown";
|
|
|
|
connect(this,&Worker::signalCheckUpdate,this,&Worker::slotCheckUpdate);
|
|
}
|
|
|
|
Worker::~Worker()
|
|
{
|
|
|
|
}
|
|
|
|
void Worker::slotCheckUpdate()
|
|
{
|
|
QList<FILE_INFO> files;
|
|
// 解析XML文件获取最新文件列表
|
|
this->parseXML(QCoreApplication::applicationDirPath() + "/Update.xml");
|
|
for (int i = 0; i < m_fileInfo.count(); i++)
|
|
{
|
|
QFileInfo fileInfo(QCoreApplication::applicationDirPath() + "/" + m_fileInfo.at(i).path);
|
|
QDir dir(QCoreApplication::applicationDirPath() + "/" + m_fileInfo.at(i).path);
|
|
// 如果文件不存在则直接下载文件或创建文件夹
|
|
if (!fileInfo.exists())
|
|
{
|
|
if (m_fileInfo[i].type == "File")
|
|
{
|
|
// 下载文件
|
|
qDebug() << "文件大小" << m_fileInfo[i].size;
|
|
m_filesSize += m_fileInfo[i].size;
|
|
files.append(m_fileInfo[i]);
|
|
}
|
|
else if (m_fileInfo[i].type == "Dir")
|
|
{
|
|
// 创建文件夹
|
|
dir.mkpath(QCoreApplication::applicationDirPath() + "/" + m_fileInfo.at(i).path);
|
|
}
|
|
continue;
|
|
}
|
|
|
|
// 存在版本信息则对比版本
|
|
if (!m_fileInfo[i].version.isEmpty())
|
|
{
|
|
// 如果是文件则获取版本对比
|
|
if (fileInfo.isFile())
|
|
{
|
|
QString version = this->getFileVertion(QCoreApplication::applicationDirPath() + "/" + m_fileInfo.at(i).path);
|
|
|
|
if (!version.contains(m_fileInfo[i].version))
|
|
{
|
|
qDebug() << m_fileInfo [i].path << "当前版本: " << version << "| 最新版本: " << m_fileInfo[i].version;
|
|
m_filesSize += m_fileInfo[i].size;
|
|
// 版本号不一致则下载文件
|
|
files.append(m_fileInfo[i]);
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
qDebug() << "更新文件数量:" << files.count();
|
|
qDebug() << "更新文件总大小:" << m_filesSize;
|
|
|
|
for (int i = 0; i < files.count(); i++)
|
|
{
|
|
QUrl url = QUrl::fromUserInput(m_url + "/Update/"+ files[i].path);
|
|
// 失败直接加上目标文件大小
|
|
if (FAILURE == download(&url, QCoreApplication::applicationDirPath() + "/" + files[i].path, "正在下载:" + files[i].name))
|
|
{
|
|
m_downloadSize += files[i].size;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
QString Worker::getFileVertion(QString fullName)
|
|
{
|
|
QString result = "";
|
|
char* pData = nullptr;
|
|
do
|
|
{
|
|
DWORD dwLen = GetFileVersionInfoSize(fullName.toStdWString().c_str(), 0);
|
|
if (0 == dwLen)
|
|
{
|
|
break;
|
|
}
|
|
pData = new char[dwLen + 1];
|
|
BOOL bSuccess = GetFileVersionInfo(fullName.toStdWString().c_str(), 0, dwLen, pData);
|
|
if (!bSuccess)
|
|
{
|
|
break;
|
|
}
|
|
struct LANGANDCODEPAGE
|
|
{
|
|
WORD wLanguage;
|
|
WORD wCodePage;
|
|
} *lpTranslate;
|
|
LPVOID lpBuffer = nullptr;
|
|
UINT uLen = 0;
|
|
bSuccess = VerQueryValue(pData, (TEXT("\\VarFileInfo\\Translation")), (LPVOID*)&lpTranslate, &uLen);
|
|
if (!bSuccess)
|
|
{
|
|
break;
|
|
}
|
|
QString str1, str2;
|
|
str1.setNum(lpTranslate->wLanguage, 16);
|
|
str2.setNum(lpTranslate->wCodePage, 16);
|
|
str1 = "000" + str1;
|
|
str2 = "000" + str2;
|
|
QString verPath = "\\StringFileInfo\\" + str1.right(4) + str2.right(4) + "\\FileVersion";//ProductVersion:产品版本号 FileVersion:文件版本号
|
|
bSuccess = VerQueryValue(pData, (verPath.toStdWString().c_str()), &lpBuffer, &uLen);
|
|
if (!bSuccess)
|
|
{
|
|
break;
|
|
}
|
|
result = QString::fromUtf16((const unsigned short int *)lpBuffer);
|
|
} while (false);
|
|
if (nullptr != pData)
|
|
{
|
|
delete pData;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
QList<FILE_INFO> Worker::FindFile(const QString& path)
|
|
{
|
|
QList<FILE_INFO> result;
|
|
|
|
// QDir的路径一定要是全路径,相对路径会有错误
|
|
QDir dir(path);
|
|
if (!dir.exists())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
|
|
// 取到所有的文件和文件名,去掉.和..文件夹
|
|
dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
|
|
// 设置排序方式
|
|
dir.setSorting(QDir::DirsFirst);
|
|
// 将其转化为一个list
|
|
QFileInfoList list = dir.entryInfoList();
|
|
if (list.size() < 1)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
|
|
int i = 0;
|
|
// 采用递归算法
|
|
do {
|
|
QFileInfo fileInfo = list.at(i);
|
|
FILE_INFO file;
|
|
// 判断是否为文件夹
|
|
bool isDirectory = fileInfo.isDir();
|
|
if (isDirectory)
|
|
{
|
|
qDebug() << "[Dir]" << fileInfo.filePath() << " -> " << fileInfo.size() << " -> " << file.version;
|
|
// 获取文件信息
|
|
file.path = fileInfo.filePath();
|
|
file.size = 0;
|
|
file.type = "Dir";
|
|
file.version.clear();
|
|
auto tmp = FindFile(fileInfo.filePath());
|
|
result = result + tmp;
|
|
}
|
|
else
|
|
{
|
|
// 获取文件信息
|
|
file.path = fileInfo.filePath();
|
|
file.size = fileInfo.size();
|
|
file.type = "File";
|
|
file.version = this->getFileVertion(file.path);
|
|
result.append(file);
|
|
// qDebug() << "[File]" << fileInfo.filePath() << " -> " << fileInfo.size() << " -> " << file.version;
|
|
}
|
|
++i;
|
|
} while (i < list.size());
|
|
|
|
return result;
|
|
}
|
|
|
|
void Worker::run()
|
|
{
|
|
// 开始工作
|
|
m_currentState = DownloadXML;
|
|
// 下载XML文件
|
|
QUrl xmlUrl = QUrl::fromUserInput(m_url + "/Update.xml");
|
|
qDebug() << "下载地址:" << m_url + "/Update.xml";
|
|
download(&xmlUrl, QCoreApplication::applicationDirPath() + "/Update.xml", "正在检查更新");
|
|
}
|
|
|
|
void Worker::updateProgress(qint64 dowloadedBytes, qint64 totalBytes)
|
|
{
|
|
|
|
|
|
int statusCode = m_networkReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
if (m_currentState!= DownloadXML)
|
|
{
|
|
m_downloadTempSize = dowloadedBytes;
|
|
if (dowloadedBytes == totalBytes)
|
|
{
|
|
// 下载完成一个文件清空临时大小并附加到总大小
|
|
m_downloadSize += m_downloadTempSize;
|
|
m_downloadTempSize = 0;
|
|
emit signalProgress(m_downloadSize, m_filesSize);
|
|
}
|
|
else
|
|
{
|
|
emit signalProgress(m_downloadSize + m_downloadTempSize, m_filesSize);
|
|
}
|
|
qDebug() << "下载进度: " << m_downloadSize << "|" << dowloadedBytes << "|"<< totalBytes << "|";
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
void Worker::slotReadyRead()
|
|
{
|
|
int statusCode = m_networkReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
if (statusCode == 200)
|
|
{
|
|
|
|
QByteArray bytes = m_networkReply->readAll();
|
|
m_downloadFile->write(bytes);
|
|
}
|
|
}
|
|
|
|
void Worker::slotFinished()
|
|
{
|
|
|
|
int statusCode = m_networkReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
qDebug() << "代码:" << statusCode;
|
|
switch (statusCode)
|
|
{
|
|
case 200:
|
|
// 关闭文件
|
|
m_downloadFile->close();
|
|
// 销毁文件类
|
|
delete m_downloadFile;
|
|
// 销毁网络请求类
|
|
m_networkReply->deleteLater();
|
|
// 销毁网络管理类
|
|
m_networkManager->deleteLater();
|
|
|
|
switch (m_currentState)
|
|
{
|
|
case DownloadXML:
|
|
connect(this, SIGNAL(sigCheckUpdate()), this, SLOT(checkUpdate()));
|
|
// 更新状态
|
|
m_currentState = DownloadUpdate;
|
|
// 发出检查更新信号
|
|
emit signalCheckUpdate();
|
|
break;
|
|
case DownloadUpdate:
|
|
qDebug() << "[结束]下载进度: "<< m_downloadSize ;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
break;
|
|
case 302:
|
|
{
|
|
qDebug() << "重定向";
|
|
// 重定向 再次尝试
|
|
QUrl url = m_networkReply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
|
|
m_networkReply = m_networkManager->get(QNetworkRequest(url));
|
|
connect(m_networkReply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
|
|
connect(m_networkReply, SIGNAL(finished()), this, SLOT(slotFinished()));
|
|
connect(m_networkReply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(updateProgress(qint64, qint64)));
|
|
break;
|
|
}
|
|
default:
|
|
qDebug() << "网络错误";
|
|
emit signalError("网络错误", "错误代码: " + QString::number(statusCode));
|
|
m_downloadFile->close();
|
|
delete m_downloadFile;
|
|
m_networkReply->deleteLater();
|
|
m_networkManager->deleteLater();
|
|
break;
|
|
}
|
|
}
|
|
|
|
// 下载文件
|
|
int Worker::download(QUrl *url, QString path, QString status)
|
|
{
|
|
if (!url->isValid())
|
|
{
|
|
qDebug() << "下载失败: 网址无效";
|
|
emit signalError("无效网址", "目标网址无效");
|
|
return FAILURE;
|
|
}
|
|
m_downloadFile = new QFile(path);
|
|
if (!m_downloadFile->open(QIODevice::WriteOnly))
|
|
{
|
|
qDebug() << "下载失败: 无法打开文件" << path;
|
|
emit signalError("文件操作失败", "无法打开临时文件!");
|
|
return FAILURE;
|
|
}
|
|
|
|
|
|
|
|
m_networkManager = new QNetworkAccessManager;
|
|
m_networkReply = m_networkManager->get(QNetworkRequest(*url));
|
|
connect(m_networkReply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
|
|
connect(m_networkReply, SIGNAL(finished()), this, SLOT(slotFinished()));
|
|
connect(m_networkReply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(updateProgress(qint64, qint64)));
|
|
|
|
emit sigShowStatus(status);
|
|
// 事件循环
|
|
QEventLoop eventLoop;
|
|
// 连接信号和槽
|
|
connect(m_networkManager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
|
|
// 开始循环
|
|
eventLoop.exec();
|
|
|
|
qDebug() << "开始下载";
|
|
// 更新状态
|
|
|
|
return SUCCESS;
|
|
}
|
|
|
|
|
|
|
|
// 解析XML文件
|
|
void Worker::parseXML(QString fileName)
|
|
{
|
|
// 避免中文乱码
|
|
QTextCodec *codec = QTextCodec::codecForName("UTF8");
|
|
QFile file(fileName);
|
|
|
|
// 打开XML文件
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
|
|
{
|
|
emit signalError("文件操作失败", "无法打开XML文件!");
|
|
return;
|
|
}
|
|
|
|
// 读取XML文件并解析
|
|
QDomDocument doc;
|
|
if (!doc.setContent(&file))
|
|
{
|
|
emit signalError("文件操作失败","无法解析XML文件!");
|
|
file.close();
|
|
return;
|
|
}
|
|
file.close();
|
|
|
|
// 获取根节点
|
|
QDomElement root = doc.documentElement();
|
|
// 获取第一个子节点
|
|
QDomNode node = root.firstChild();
|
|
// 遍历子节点
|
|
while (!node.isNull())
|
|
{
|
|
// 如果节点是元素
|
|
if (node.isElement())
|
|
{
|
|
// 转换为元素
|
|
QDomElement element = node.toElement();
|
|
if (!element.isNull())
|
|
{
|
|
FILE_INFO fileInfo;
|
|
fileInfo.name = element.attribute("name");
|
|
fileInfo.path = element.attribute("path");
|
|
fileInfo.size = element.attribute("size").toInt();
|
|
|
|
fileInfo.type = element.attribute("type");
|
|
fileInfo.version = element.attribute("version");
|
|
m_fileInfo.append(fileInfo);
|
|
}
|
|
}
|
|
// 下一个兄弟节点
|
|
node = node.nextSibling();
|
|
}
|
|
}
|
|
|
|
// 删除文件或目录
|
|
bool Worker::deleteFileOrDirectory(QString path)
|
|
{
|
|
// 判断路径是否为空
|
|
if (path.isEmpty() || !QDir().exists(path))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
QFileInfo fileInfo(path);
|
|
// 删除文件
|
|
if (fileInfo.isFile())
|
|
{
|
|
return QFile::remove(path);
|
|
}
|
|
// 删除目录
|
|
else if (fileInfo.isDir())
|
|
{
|
|
QDir dir(path);
|
|
dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);
|
|
QFileInfoList fileList = dir.entryInfoList();
|
|
foreach(QFileInfo fi, fileList)
|
|
{
|
|
if (fi.isFile())
|
|
{
|
|
fi.dir().remove(fi.fileName());
|
|
}
|
|
else
|
|
{
|
|
deleteFileOrDirectory(fi.absoluteFilePath());
|
|
}
|
|
}
|
|
dir.rmpath(dir.absolutePath());
|
|
}
|
|
return true;
|
|
}
|
|
|