updatedlg.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include "updatedlg.h"
  2. #include <QSettings>
  3. #include <QTimer>
  4. #include <QJsonArray>
  5. #include <QJsonDocument>
  6. #include <QJsonObject>
  7. #include <QMessageBox>
  8. #include "qmovewindowmanager.h"
  9. #include "..\ZLibWrapLib\ZLibWrapLib.h"
  10. UpdateDlg::UpdateDlg(QString strContent, QString strVersion, QString url, bool ishaveExc, QDialog *parent)
  11. : QDialog(parent)
  12. {
  13. ui.setupUi(this);
  14. m_url = url;
  15. ui.widget_head->installEventFilter(new QMoveWindowManager(this));
  16. setWindowFlags(Qt::FramelessWindowHint);
  17. ui.pushButton_2->setDefault(false);
  18. ui.pushButton_2->setFocusPolicy(Qt::NoFocus);
  19. connect(ui.pushButton_2, SIGNAL(clicked()), this, SLOT(closeMe()));
  20. connect(ui.pushButton_3, SIGNAL(clicked()), this, SLOT(jumpMe()));
  21. connect(ui.pushButton_4, SIGNAL(clicked()), this, SLOT(cancleMe()));
  22. connect(ui.pushButton_5, SIGNAL(clicked()), this, SLOT(updateMe()));
  23. QString strInfo = QString::fromStdWString(L"更新内容:\r\n%1").arg(strContent);
  24. QStringList litContent = strInfo.split("\r\n");
  25. strContent.clear();
  26. for (int i = 00; i < litContent.size(); i++)
  27. {
  28. litContent[i].append("\r\n\r\n");
  29. strContent.append(litContent[i]);
  30. }
  31. ui.label->setText(strContent);
  32. m_newVersion = strVersion;
  33. QString _version = QString::fromStdWString(L"发现新版本:%1").arg(strVersion);
  34. ui.label_3->setText(_version);
  35. ui.pushButton_3->hide();
  36. //ui.label_2->setText(QString::fromStdWString(L"升级提醒"));
  37. m_ishaveExc = ishaveExc;
  38. ui.progressBar->setValue(0);
  39. ui.progressBar->hide();
  40. }
  41. UpdateDlg::~UpdateDlg()
  42. {
  43. }
  44. void UpdateDlg::hideMe()
  45. {
  46. this->showMinimized();
  47. }
  48. void UpdateDlg::closeMe()
  49. {
  50. this->close();
  51. }
  52. void UpdateDlg::jumpMe()
  53. {
  54. QDialog::done(10);
  55. }
  56. void UpdateDlg::cancleMe()
  57. {
  58. QDialog::reject();
  59. }
  60. void UpdateDlg::updateMe()
  61. {
  62. if (m_ishaveExc)
  63. {
  64. QMessageBox box(QMessageBox::Warning, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("系统检测到还有异常记录未处理完成,请先处理完异常再进行更新"));
  65. box.setStandardButtons(QMessageBox::Ok);
  66. box.setButtonText(QMessageBox::Ok, QString::fromLocal8Bit("关闭并处理异常"));
  67. box.exec();
  68. this->close();
  69. return;
  70. }
  71. on_downloadButton_clicked();
  72. //QDialog::accept();
  73. }
  74. void UpdateDlg::on_uploadButton_clicked() //上传文件
  75. {
  76. //file = new QFile("C:/Users/Administrator/Desktop/test.jpg");
  77. //file->open(QIODevice::ReadOnly);
  78. //QNetworkAccessManager *accessManager1 = new QNetworkAccessManager(this); //在服务器上创建目录
  79. //accessManager1->setNetworkAccessible(QNetworkAccessManager::Accessible);
  80. //QByteArray data;
  81. //QUrl url1("http://192.168.1.166:80/createFolder.php?foldername=upload");
  82. //QNetworkRequest request1(url1);
  83. //request1.setHeader(QNetworkRequest::ContentTypeHeader, QLatin1String("application/octet-stream"));
  84. //accessManager1->post(request1, data);
  85. //QNetworkAccessManager *accessManager2 = new QNetworkAccessManager(this); //往该目录中上传文件
  86. //accessManager2->setNetworkAccessible(QNetworkAccessManager::Accessible);
  87. //QByteArray byte_file = file->readAll();
  88. //QUrl url2("http://192.168.1.166:80/upload.php?foldername=upload&filename=" + ui->lineEdit->text() + ".jpg"); //如这里指定的上传文件至HTTP服务器目录中的upload目录中
  89. //QNetworkRequest request2(url2);
  90. //request2.setHeader(QNetworkRequest::ContentTypeHeader, "application/octet-stream");
  91. //reply = accessManager2->post(request2, byte_file);
  92. //progressBar = new QProgressBar();
  93. //progressBar->setValue(0);
  94. //progressBar->show();
  95. //connect(accessManager2, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
  96. //connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(loadError(QNetworkReply::NetworkError)));
  97. //connect(reply, SIGNAL(uploadProgress(qint64, qint64)), this, SLOT(loadProgress(qint64, qint64)));
  98. }
  99. void UpdateDlg::on_downloadButton_clicked() //下载文件
  100. {
  101. QUrl url(m_url);
  102. file = new QFile(QApplication::applicationDirPath() + "/temp.zip");
  103. file->open(QIODevice::WriteOnly);
  104. QNetworkAccessManager *accessManager = new QNetworkAccessManager(this);
  105. accessManager->setNetworkAccessible(QNetworkAccessManager::Accessible);
  106. //QUrl url("http://fastcvf.zhixinhuixue.com/zxhx_2.2.3_part.zip");
  107. QNetworkRequest request(url);
  108. //request.setHeader(QNetworkRequest::ContentTypeHeader, "application/octet-stream");
  109. reply = accessManager->get(request);
  110. ui.progressBar->setValue(0);
  111. ui.progressBar->show();
  112. connect((QObject *)reply, SIGNAL(readyRead()), this, SLOT(readContent()));
  113. connect(accessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
  114. connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(loadError(QNetworkReply::NetworkError)));
  115. connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(loadProgress(qint64, qint64)));
  116. }
  117. void UpdateDlg::readContent() //下载时向本地文件中写入数据
  118. {
  119. file->write(reply->readAll());
  120. }
  121. void UpdateDlg::replyFinished(QNetworkReply*) //删除指针,更新和关闭文件
  122. {
  123. if (reply->error() == QNetworkReply::NoError)
  124. {
  125. reply->deleteLater();
  126. file->flush();
  127. file->close();
  128. QString zip = QApplication::applicationDirPath() + "/temp.zip";
  129. QString updir = QApplication::applicationDirPath() + "/update";
  130. bool suc = ZipExtract(zip.toStdWString().c_str(), updir.toStdWString().c_str());
  131. if (!suc){
  132. QMessageBox::critical(NULL, tr("Error"), "unzip Failed!!!");
  133. return;
  134. }
  135. QString fff;
  136. fff = QApplication::applicationDirPath() + QString::fromLocal8Bit("\\tempExe\\SmartCopyAndRun.exe");
  137. QString src = QApplication::applicationDirPath() + QString::fromLocal8Bit("\\SmartCopyAndRun.exe");
  138. QString target = QApplication::applicationDirPath() + QString::fromLocal8Bit("\\tempExe\\SmartCopyAndRun.exe");
  139. QString dir = QApplication::applicationDirPath() + QString::fromLocal8Bit("\\tempExe");
  140. ::CreateDirectory(dir.toStdWString().c_str(),0);
  141. CopyFile(src.toStdWString().c_str(),target.toStdWString().c_str() , FALSE);
  142. Sleep(2000);
  143. //CopyDirectory(modulDir+_T("/update"),modulDir);
  144. //DeleteDirectory(modulDir+_T("/update"));
  145. //UpdateRegistryVersion(m_new_version);
  146. //UpdateRegistryVersion(m_new_version);
  147. //清空结构
  148. STARTUPINFO sInfo;
  149. PROCESS_INFORMATION pInfo;
  150. ZeroMemory(&sInfo, sizeof(sInfo));
  151. sInfo.cb = sizeof(sInfo);
  152. sInfo.dwFlags = STARTF_USESHOWWINDOW;
  153. sInfo.wShowWindow = SW_SHOWNORMAL;
  154. ZeroMemory(&pInfo, sizeof(pInfo));
  155. QString cmd = "version=";
  156. cmd += m_newVersion;
  157. if (!::CreateProcess(target.toStdWString().c_str(), (WCHAR*)cmd.toStdWString().c_str(),
  158. NULL,
  159. NULL,
  160. FALSE,
  161. 0,
  162. NULL,
  163. QApplication::applicationDirPath().toStdWString().c_str(),
  164. &sInfo,
  165. &pInfo))
  166. {
  167. //PostMessage(WM_ERROR, ERROR_CANNOT_STARTAPP, 0);
  168. //PostMessage(WM_UPDATE_STATE_CHANGE, STATE_NONE, 0);
  169. }
  170. else{
  171. //PostMessage(WM_UPDATE_STATE_CHANGE, STATE_NONE, 0);
  172. //AfxGetMainWnd()->PostMessage(WM_CLOSE);
  173. }
  174. }
  175. else
  176. {
  177. QMessageBox::critical(NULL, tr("Error"), "download Failed!!!");
  178. }
  179. }
  180. void UpdateDlg::loadProgress(qint64 bytesSent, qint64 bytesTotal) //更新进度条
  181. {
  182. qDebug() << "loaded" << bytesSent << "of" << bytesTotal;
  183. ui.progressBar->setMaximum(bytesTotal); //最大值
  184. ui.progressBar->setValue(bytesSent); //当前值
  185. }
  186. void UpdateDlg::loadError(QNetworkReply::NetworkError) //传输中的错误输出
  187. {
  188. qDebug() << "Error: " << reply->error();
  189. }