#include "updatedlg.h" #include #include #include #include #include #include #include "qmovewindowmanager.h" #include "..\ZLibWrapLib\ZLibWrapLib.h" UpdateDlg::UpdateDlg(QString strContent, QString strVersion, QString url, bool ishaveExc, QDialog *parent) : QDialog(parent) { ui.setupUi(this); m_url = url; ui.widget_head->installEventFilter(new QMoveWindowManager(this)); setWindowFlags(Qt::FramelessWindowHint); ui.pushButton_2->setDefault(false); ui.pushButton_2->setFocusPolicy(Qt::NoFocus); connect(ui.pushButton_2, SIGNAL(clicked()), this, SLOT(closeMe())); connect(ui.pushButton_3, SIGNAL(clicked()), this, SLOT(jumpMe())); connect(ui.pushButton_4, SIGNAL(clicked()), this, SLOT(cancleMe())); connect(ui.pushButton_5, SIGNAL(clicked()), this, SLOT(updateMe())); QString strInfo = QString::fromStdWString(L"更新内容:\r\n%1").arg(strContent); QStringList litContent = strInfo.split("\r\n"); strContent.clear(); for (int i = 00; i < litContent.size(); i++) { litContent[i].append("\r\n\r\n"); strContent.append(litContent[i]); } ui.label->setText(strContent); m_newVersion = strVersion; QString _version = QString::fromStdWString(L"发现新版本:%1").arg(strVersion); ui.label_3->setText(_version); ui.pushButton_3->hide(); //ui.label_2->setText(QString::fromStdWString(L"升级提醒")); m_ishaveExc = ishaveExc; ui.progressBar->setValue(0); ui.progressBar->hide(); } UpdateDlg::~UpdateDlg() { } void UpdateDlg::hideMe() { this->showMinimized(); } void UpdateDlg::closeMe() { this->close(); } void UpdateDlg::jumpMe() { QDialog::done(10); } void UpdateDlg::cancleMe() { QDialog::reject(); } void UpdateDlg::updateMe() { if (m_ishaveExc) { QMessageBox box(QMessageBox::Warning, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("系统检测到还有异常记录未处理完成,请先处理完异常再进行更新")); box.setStandardButtons(QMessageBox::Ok); box.setButtonText(QMessageBox::Ok, QString::fromLocal8Bit("关闭并处理异常")); box.exec(); this->close(); return; } on_downloadButton_clicked(); //QDialog::accept(); } void UpdateDlg::on_uploadButton_clicked() //上传文件 { //file = new QFile("C:/Users/Administrator/Desktop/test.jpg"); //file->open(QIODevice::ReadOnly); //QNetworkAccessManager *accessManager1 = new QNetworkAccessManager(this); //在服务器上创建目录 //accessManager1->setNetworkAccessible(QNetworkAccessManager::Accessible); //QByteArray data; //QUrl url1("http://192.168.1.166:80/createFolder.php?foldername=upload"); //QNetworkRequest request1(url1); //request1.setHeader(QNetworkRequest::ContentTypeHeader, QLatin1String("application/octet-stream")); //accessManager1->post(request1, data); //QNetworkAccessManager *accessManager2 = new QNetworkAccessManager(this); //往该目录中上传文件 //accessManager2->setNetworkAccessible(QNetworkAccessManager::Accessible); //QByteArray byte_file = file->readAll(); //QUrl url2("http://192.168.1.166:80/upload.php?foldername=upload&filename=" + ui->lineEdit->text() + ".jpg"); //如这里指定的上传文件至HTTP服务器目录中的upload目录中 //QNetworkRequest request2(url2); //request2.setHeader(QNetworkRequest::ContentTypeHeader, "application/octet-stream"); //reply = accessManager2->post(request2, byte_file); //progressBar = new QProgressBar(); //progressBar->setValue(0); //progressBar->show(); //connect(accessManager2, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*))); //connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(loadError(QNetworkReply::NetworkError))); //connect(reply, SIGNAL(uploadProgress(qint64, qint64)), this, SLOT(loadProgress(qint64, qint64))); } void UpdateDlg::on_downloadButton_clicked() //下载文件 { QUrl url(m_url); file = new QFile(QApplication::applicationDirPath() + "/temp.zip"); file->open(QIODevice::WriteOnly); QNetworkAccessManager *accessManager = new QNetworkAccessManager(this); accessManager->setNetworkAccessible(QNetworkAccessManager::Accessible); //QUrl url("http://fastcvf.zhixinhuixue.com/zxhx_2.2.3_part.zip"); QNetworkRequest request(url); //request.setHeader(QNetworkRequest::ContentTypeHeader, "application/octet-stream"); reply = accessManager->get(request); ui.progressBar->setValue(0); ui.progressBar->show(); connect((QObject *)reply, SIGNAL(readyRead()), this, SLOT(readContent())); connect(accessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*))); connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(loadError(QNetworkReply::NetworkError))); connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(loadProgress(qint64, qint64))); } void UpdateDlg::readContent() //下载时向本地文件中写入数据 { file->write(reply->readAll()); } void UpdateDlg::replyFinished(QNetworkReply*) //删除指针,更新和关闭文件 { if (reply->error() == QNetworkReply::NoError) { reply->deleteLater(); file->flush(); file->close(); QString zip = QApplication::applicationDirPath() + "/temp.zip"; QString updir = QApplication::applicationDirPath() + "/update"; bool suc = ZipExtract(zip.toStdWString().c_str(), updir.toStdWString().c_str()); if (!suc){ QMessageBox::critical(NULL, tr("Error"), "unzip Failed!!!"); return; } QString fff; fff = QApplication::applicationDirPath() + QString::fromLocal8Bit("\\tempExe\\SmartCopyAndRun.exe"); QString src = QApplication::applicationDirPath() + QString::fromLocal8Bit("\\SmartCopyAndRun.exe"); QString target = QApplication::applicationDirPath() + QString::fromLocal8Bit("\\tempExe\\SmartCopyAndRun.exe"); QString dir = QApplication::applicationDirPath() + QString::fromLocal8Bit("\\tempExe"); ::CreateDirectory(dir.toStdWString().c_str(),0); CopyFile(src.toStdWString().c_str(),target.toStdWString().c_str() , FALSE); Sleep(2000); //CopyDirectory(modulDir+_T("/update"),modulDir); //DeleteDirectory(modulDir+_T("/update")); //UpdateRegistryVersion(m_new_version); //UpdateRegistryVersion(m_new_version); //清空结构 STARTUPINFO sInfo; PROCESS_INFORMATION pInfo; ZeroMemory(&sInfo, sizeof(sInfo)); sInfo.cb = sizeof(sInfo); sInfo.dwFlags = STARTF_USESHOWWINDOW; sInfo.wShowWindow = SW_SHOWNORMAL; ZeroMemory(&pInfo, sizeof(pInfo)); QString cmd = "version="; cmd += m_newVersion; if (!::CreateProcess(target.toStdWString().c_str(), (WCHAR*)cmd.toStdWString().c_str(), NULL, NULL, FALSE, 0, NULL, QApplication::applicationDirPath().toStdWString().c_str(), &sInfo, &pInfo)) { //PostMessage(WM_ERROR, ERROR_CANNOT_STARTAPP, 0); //PostMessage(WM_UPDATE_STATE_CHANGE, STATE_NONE, 0); } else{ //PostMessage(WM_UPDATE_STATE_CHANGE, STATE_NONE, 0); //AfxGetMainWnd()->PostMessage(WM_CLOSE); } } else { QMessageBox::critical(NULL, tr("Error"), "download Failed!!!"); } } void UpdateDlg::loadProgress(qint64 bytesSent, qint64 bytesTotal) //更新进度条 { qDebug() << "loaded" << bytesSent << "of" << bytesTotal; ui.progressBar->setMaximum(bytesTotal); //最大值 ui.progressBar->setValue(bytesSent); //当前值 } void UpdateDlg::loadError(QNetworkReply::NetworkError) //传输中的错误输出 { qDebug() << "Error: " << reply->error(); }