#include "widget.h"
#include "ui_widget.h"
#include <QtWidgets>
#include <QtNetwork>
#include <QTextCodec>
#include <QStandardPaths>
//#include "../printAppServer/control_base/messageBox.h"
//using namespace AMSSY::gui;
#include "form.h"
//#include "ui_authenticationdialog.h"
int Widget::nCount = 0;
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
setWindowIcon(QIcon("ico.ico"));
fileLog = new QFile("update_error_log.txt");
if (!fileLog->open(QIODevice::ReadWrite|QIODevice::Append|QIODevice::Text))
{
qDebug()<<tr("Creat the logFile failured")<<endl;
}
QTextStream log(fileLog);
QString logTime = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
log<<logTime<<QString::fromUtf8(":开始检测")<<"\r\n";
log.flush();
QString fileName = "./AutoUpdate.exe.bak";
QFile::remove(fileName);
QString fileName2 = "./printAppServer.exe.bak";
QFile::remove(fileName2);
QString fileName3 = "./printview.dll.bak";
QFile::remove(fileName3);
bFinished = false;
qlFileName.clear();
qlFileExt.clear();
strTmpDownDir = "./tmp_down/";
strBakDir = ".tmp_bak/";
QSettings *configIniRead = new QSettings("config.ini", QSettings::IniFormat);
QString VERPATH = configIniRead->value("/Path/VERPATH").toString();
VERSION = configIniRead->value("/Path/VERSION").toString();
QString VERPATH_COMPANY = configIniRead->value("/Path/VERPATH_COMPANY").toString();
QString VERSION_COMPANY = configIniRead->value("/Path/VERSION_COMPANY").toString();
QNetworkAccessManager manager;
QEventLoop loop,loop2;
QNetworkReply *reply,*reply2;
bool bCdn,bCompany;
QByteArray qa,qa2;
reply = manager.get(QNetworkRequest(QUrl(VERPATH)));
qDebug()<<VERPATH<<endl;
//请求结束并下载完成后,退出子事件循环
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
//开启子事件循环
loop.exec();
QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
if(status_code == "200" && reply->error() == QNetworkReply::NoError)
{
qa = reply->readAll();
qDebug()<<"CDN:"<<qa<<endl;
bCdn = true;
}
else
{
bCdn = false;
}
reply2 = manager.get(QNetworkRequest(QUrl(VERPATH_COMPANY)));
//请求结束并下载完成后,退出子事件循环
QObject::connect(reply2, SIGNAL(finished()), &loop2, SLOT(quit()));
//开启子事件循环
loop2.exec();
QVariant status_code2 = reply2->attribute(QNetworkRequest::HttpStatusCodeAttribute);
if(status_code2 == "200" && reply2->error() == QNetworkReply::NoError)
{
qa2 = reply2->readAll();
qDebug()<<"COMPANY:"<<qa2<<endl<<VERPATH_COMPANY<<endl;
bCompany = true;
}
else
{
bCompany = false;
}
if(bCdn)
{
if(bCompany)
{
QXmlStreamReader xml(qa2);
if(qa2==qa)
{
}
else
{
VERPATH = VERPATH_COMPANY;
VERSION = VERSION_COMPANY;
}
}
else
{
VERPATH = VERPATH;
VERSION = VERSION;
}
}
else
{
if(bCompany)
{
VERPATH = VERPATH_COMPANY;
VERSION = VERSION_COMPANY;
}
else
{
qDebug()<<tr("No file needed update")<<endl;
logTime = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
log<<logTime<<QString::fromUtf8(":版本文件下载失败,下载地址%1和%2")
.arg(VERPATH_COMPANY).arg(VERPATH)<<"\r\n";
qApp->quit();
return;
}
}
logTime = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
log<<logTime<<QString::fromUtf8(":更新包下载地址%1").arg(VERSION)<<"\r\n";
log.flush();
// QTextStream out(fileLog);
// out<<tr("Unable to save the file\n")<<endl;
// out.flush();
// QTextStream out2(fileLog);
// out2<<tr("Unable to save the file\n")<<endl;
// out2.flush();
startup_update(VERPATH);
}
Widget::~Widget()
{
QTextStream log(fileLog);
log<<"\r\n";
fileLog->close();
delete ui;
}
void Widget::startRequest(QUrl url)
{
bFinished = false;
QNetworkRequest request;
request.setUrl(QUrl(url));
// QSslConfiguration config;
// config.setPeerVerifyMode(QSslSocket::VerifyNone);
// config.setProtocol(QSsl::TlsV1_0);
// request.setSslConfiguration(config);
reply = qnam.get(request);
connect(reply, SIGNAL(finished()),
this, SLOT(httpFinished()));
connect(reply, SIGNAL(readyRead()),
this, SLOT(httpReadyRead()));
}
void Widget::downloadFile(QUrl url)
{
QFileInfo fileInfo(url.path());
QString fileName = fileInfo.fileName();
if (fileName.isEmpty())
//fileName = "index.html";
return;
QTextStream log(fileLog);
QString logTime = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
log<<logTime<<QString::fromUtf8(":下载文件")<<url.url()<<"\r\n";
log.flush();
QString fileExt = fileInfo.suffix();
qlFileName.append(fileName);
fileName = "./tmp_down/" + fileName;
if(fileExt == tr("zip"))
{
qlFileExt.append(fileName);
}
if (QFile::exists(fileName))
{
// if (QMessageBox::question(this, tr("HTTP"),
// tr("There already exists a file called %1 in "
// "the current directory. Overwrite?").arg(fileName),
// QMessageBox::Yes|QMessageBox::No, QMessageBox::No)
// == QMessageBox::No)
// return;
QFile::remove(fileName);
}
file = new QFile(fileName);
if (!file->open(QIODevice::WriteOnly)) {
// QMessageBox::information(this, tr("HTTP"),
// tr("Unable to save the file %1: %2.")
// .arg(fileName).arg(file->errorString()));
Form *f = new Form;
f->show();
delete file;
file = 0;
QTextStream out(fileLog);
out<<tr("Unable to save the file %1: %2.\n").arg(fileName).arg(file->errorString()).toStdString().c_str()<<endl;
out.flush();
return;
}
// schedule the request
httpRequestAborted = false;
startRequest(url);
}
void Widget::httpFinished()
{
if (httpRequestAborted)
{
if (file) {
file->close();
file->remove();
delete file;
file = 0;
}
reply->deleteLater();
return;
}
file->flush();
file->close();
QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
if (reply->error())
{
file->remove();
// QMessageBox::information(this, tr("HTTP"),
// tr("Download failed: %1.")
// .arg(reply->errorString()));
}
else if (!redirectionTarget.isNull())
{
QUrl newUrl = url.resolved(redirectionTarget.toUrl());
//if (QMessageBox::question(this, tr("HTTP"),
// tr("Redirect to %1 ?").arg(newUrl.toString()),
// QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
{
url = newUrl;
reply->deleteLater();
file->open(QIODevice::WriteOnly);
file->resize(0);
startRequest(url);
return;
}
}
else
{
}
reply
- 1
- 2
前往页