upgrade.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include "upgrade.h"
  2. #include "ui_upgrade.h"
  3. #include "CAppInfo.h"
  4. #include <QFile>
  5. #include "logproc.h"
  6. #include "popMsgBox.h"
  7. #include "CCommonTools.h"
  8. #include "CSqlite3DBProc.h"
  9. upgrade::upgrade(QWidget *parent) :
  10. QWidget(parent),
  11. ui(new Ui::upgrade)
  12. {
  13. ui->setupUi(this);
  14. setStyleSheet(g_appInfoPtr->m_sQssStr);
  15. initUI();
  16. qRegisterMetaType<CDownLoadFileInfo>("CDownLoadFileInfo");
  17. connect(g_httpBllPtr.get(), SIGNAL(sgnDownLoadFile(CDownLoadFileInfo)), this, SLOT(onDownLoadFile(CDownLoadFileInfo)));
  18. }
  19. upgrade::~upgrade()
  20. {
  21. disconnect(g_httpBllPtr.get(), SIGNAL(sgnDownLoadFile(CDownLoadFileInfo)), this, SLOT(onDownLoadFile(CDownLoadFileInfo)));
  22. delete ui;
  23. }
  24. void upgrade::initUI()
  25. {
  26. setGeometry(0, 0, g_appInfoPtr->m_fRate*(600 - 24), g_appInfoPtr->m_fRate*340);
  27. ui->label_upgradeTitle->adjustSize();
  28. ui->label_upgradeTitle->setGeometry(g_appInfoPtr->m_fRate*80, g_appInfoPtr->m_fRate*86, ui->label_upgradeTitle->width(), ui->label_upgradeTitle->height());
  29. ui->label_upgradePregress->adjustSize();
  30. ui->label_upgradePregress->setGeometry(ui->label_upgradeTitle->x(), g_appInfoPtr->m_fRate*184, g_appInfoPtr->m_fRate*200, ui->label_upgradePregress->height());
  31. ui->pgb_upgrade->setGeometry(ui->label_upgradeTitle->x(), ui->label_upgradePregress->y() + ui->label_upgradePregress->height() + g_appInfoPtr->m_fRate*16,
  32. g_appInfoPtr->m_fRate*440, g_appInfoPtr->m_fRate*8);
  33. ui->label_upgradeHint->adjustSize();
  34. ui->label_upgradeHint->setGeometry(ui->label_upgradeTitle->x(), ui->pgb_upgrade->y() + ui->pgb_upgrade->height() + g_appInfoPtr->m_fRate*10,
  35. g_appInfoPtr->m_fRate*500, ui->label_upgradeHint->height());
  36. }
  37. void upgrade::startUpgrade(QList<UpdateInfo> sList)
  38. {
  39. m_sList = sList;
  40. if(m_sList.count() > 0)
  41. {
  42. m_curInfo = m_sList[0];
  43. m_sList.pop_front();
  44. if(m_curInfo.nType == 2)
  45. {
  46. ui->label_upgradeHint->setText(QString::fromLocal8Bit("正在获取升级信息…"));
  47. QApplication::processEvents();
  48. QString sFileName = m_curInfo.sUrl.right(m_curInfo.sUrl.length() - m_curInfo.sUrl.lastIndexOf("/") - 1);
  49. sFileName = g_appInfoPtr->m_sCacheFileDir + "uptemp/" + sFileName;
  50. CHttpRequestPackage hrp;
  51. hrp.sUri = m_curInfo.sUrl;
  52. hrp.sCommonStr = sFileName;
  53. hrp.nRequestType = RequestType::rtDownLoadFile;
  54. g_httpBllPtr->downLoad(hrp);
  55. }
  56. }
  57. }
  58. void upgrade::retry()
  59. {
  60. QString sFileName = m_curInfo.sUrl.right(m_curInfo.sUrl.length() - m_curInfo.sUrl.lastIndexOf("/") - 1);
  61. sFileName = g_appInfoPtr->m_sCacheFileDir + "uptemp/" + sFileName;
  62. CHttpRequestPackage hrp;
  63. hrp.sUri = m_curInfo.sUrl;
  64. hrp.sCommonStr = sFileName;
  65. hrp.nRequestType = RequestType::rtDownLoadFile;
  66. g_httpBllPtr->downLoad(hrp);
  67. }
  68. void upgrade::onDownLoadFile(CDownLoadFileInfo downLoadFileInfo)
  69. {
  70. QString sFileName = m_curInfo.sUrl.right(m_curInfo.sUrl.length() - m_curInfo.sUrl.lastIndexOf("/") - 1);
  71. sFileName = g_appInfoPtr->m_sCacheFileDir + "uptemp/" + sFileName;
  72. if (downLoadFileInfo.sFileName == sFileName)
  73. {
  74. if (downLoadFileInfo.nCode == 200)
  75. {
  76. if(m_curInfo.nType == 2)
  77. {
  78. //解析升级文件
  79. QString sUrlPrefix = m_curInfo.sUrl.left(m_curInfo.sUrl.lastIndexOf("/")+1);
  80. QString sFileName = m_curInfo.sUrl.right(m_curInfo.sUrl.length() - m_curInfo.sUrl.lastIndexOf("/") - 1);
  81. sFileName = g_appInfoPtr->m_sCacheFileDir + "uptemp/" + sFileName;
  82. QFile sFile(sFileName);
  83. if(!sFile.open(QIODevice::ReadOnly))
  84. {
  85. popMsg(QString::fromLocal8Bit("加载升级文件失败!"), QString::fromLocal8Bit("系统提示"), this);
  86. myDebug()<<QString::fromLocal8Bit("加载升级文件失败!");
  87. return;
  88. }
  89. QString upStr = sFile.readAll();
  90. Json::Reader reader;
  91. Json::Value rootJson = Json::Value::null;
  92. if(!reader.parse(upStr.toStdString(), rootJson))
  93. {
  94. popMsg(QString::fromLocal8Bit("解析升级文件失败!"), QString::fromLocal8Bit("系统提示"), this);
  95. myDebug()<<QString::fromLocal8Bit("解析升级文件失败!");
  96. return;
  97. }
  98. __int64 versionValue = rootJson["versionValue"].asInt64();
  99. if(versionValue != g_appInfoPtr->m_nVersionId)
  100. {
  101. popMsg(QString::fromLocal8Bit("升级文件版本不一致!"), QString::fromLocal8Bit("系统提示"), this);
  102. myDebug()<<QString::fromLocal8Bit("升级文件版本不一致!");
  103. return;
  104. }
  105. int nCount = 0;
  106. int nSize = rootJson["files"].size();
  107. ui->pgb_upgrade->setValue(0);
  108. ui->pgb_upgrade->setMaximum(nSize*2);
  109. ui->label_upgradePregress->setText(ui->pgb_upgrade->text());
  110. QApplication::processEvents();
  111. QApplication::processEvents();
  112. for(int i = 0; i < nSize; i++)
  113. {
  114. QString sName = rootJson["files"][i]["name"].asString().c_str();
  115. QString sMd5 = rootJson["files"][i]["md5"].asString().c_str();
  116. QString sFileMd5 = CCommonTools::fileMd5(sName);
  117. if(sFileMd5 != sMd5)
  118. {
  119. UpdateInfo upInfo;
  120. upInfo.nType = 3;
  121. upInfo.sUrl = sUrlPrefix + rootJson["files"][i]["url"].asString().c_str();
  122. upInfo.sMd5 = sMd5;
  123. upInfo.sDestFileName = sName;
  124. m_sList.push_back(upInfo);
  125. myDebug()<<sName;
  126. nCount++;
  127. }
  128. if(ui->pgb_upgrade->value() < ui->pgb_upgrade->maximum())
  129. {
  130. ui->pgb_upgrade->setValue(ui->pgb_upgrade->value()+1);
  131. ui->label_upgradePregress->setText(ui->pgb_upgrade->text());
  132. }
  133. QApplication::processEvents();
  134. }
  135. ui->pgb_upgrade->setMaximum(2*nCount);
  136. ui->pgb_upgrade->setValue(nCount);
  137. ui->label_upgradeHint->setText(QString::fromLocal8Bit("正在配置升级项…"));
  138. QApplication::processEvents();
  139. QFile::remove(sFileName);
  140. }
  141. if(m_curInfo.nType == 3)
  142. {
  143. QString sFileName = m_curInfo.sUrl.right(m_curInfo.sUrl.length() - m_curInfo.sUrl.lastIndexOf("/") - 1);
  144. QString sName = sFileName;
  145. sFileName = g_appInfoPtr->m_sCacheFileDir + "uptemp/" + sFileName;
  146. QFile file(sFileName);
  147. if(!file.exists())
  148. {
  149. popMsg(QString::fromLocal8Bit("下载文件不存在"), QString::fromLocal8Bit("系统提示"), this);
  150. myDebug()<<QString::fromLocal8Bit("下载文件不存在");
  151. return;
  152. }
  153. QString sFileMd5 = CCommonTools::fileMd5(sFileName);
  154. if(sFileMd5 != m_curInfo.sMd5)
  155. {
  156. popMsg(QString::fromLocal8Bit("下载文件MD5校验不通过"), QString::fromLocal8Bit("系统提示"), this);
  157. myDebug()<<QString::fromLocal8Bit("下载文件MD5校验不通过");
  158. return;
  159. }
  160. if(sName != "launcher.exe")
  161. {
  162. QFile::remove(m_curInfo.sDestFileName);
  163. CCommonTools::createDir(m_curInfo.sDestFileName);
  164. if(!file.copy(m_curInfo.sDestFileName))
  165. {
  166. popMsg(QString::fromLocal8Bit("拷贝升级文件失败!"), QString::fromLocal8Bit("系统提示"), this);
  167. myDebug()<<QString::fromLocal8Bit("拷贝升级文件失败!")<<m_curInfo.sDestFileName;
  168. return;
  169. }
  170. QFile::remove(sFileName);
  171. }
  172. else
  173. {
  174. m_bUpdateOeLauncher = true;
  175. }
  176. if(ui->pgb_upgrade->value() < ui->pgb_upgrade->maximum())
  177. {
  178. ui->pgb_upgrade->setValue(ui->pgb_upgrade->value()+1);
  179. ui->label_upgradePregress->setText(ui->pgb_upgrade->text());
  180. }
  181. QApplication::processEvents();
  182. }
  183. if(m_sList.count() > 0)
  184. {
  185. m_curInfo = m_sList[0];
  186. m_sList.pop_front();
  187. QString sFileName = m_curInfo.sUrl.right(m_curInfo.sUrl.length() - m_curInfo.sUrl.lastIndexOf("/") - 1);
  188. sFileName = g_appInfoPtr->m_sCacheFileDir + "uptemp/" + sFileName;
  189. CHttpRequestPackage hrp;
  190. hrp.sUri = m_curInfo.sUrl;
  191. hrp.sCommonStr = sFileName;
  192. hrp.nRequestType = RequestType::rtDownLoadFile;
  193. g_httpBllPtr->downLoad(hrp);
  194. }
  195. else
  196. {
  197. // if(m_curInfo.nType == 3)
  198. {
  199. QString sql = QString("update t_sysinfo set version_id='%1',version_code='%2'")
  200. .arg(g_appInfoPtr->m_nVersionId).arg(g_appInfoPtr->m_sVersionCode);
  201. if(!g_sysDBProcPtr->ExcuteSql(sql.toStdString()))
  202. {
  203. popMsg(QString::fromLocal8Bit("更新版本信息失败!"), QString::fromLocal8Bit("系统提示"), this);
  204. myDebug()<<QString::fromLocal8Bit("更新版本信息失败!");
  205. return;
  206. }
  207. }
  208. emit updateSucceed(m_bUpdateOeLauncher);
  209. }
  210. }
  211. else
  212. {
  213. // if(downLoadFileInfo.sMessage.isEmpty())
  214. {
  215. int nRet = popMsg(QString::fromLocal8Bit("下载失败,是否重试?"), QString::fromLocal8Bit("系统提示"), this);
  216. if(nRet == QDialog::Accepted)
  217. {
  218. retry();
  219. }
  220. else
  221. {
  222. emit exitUpgrade();
  223. }
  224. }
  225. // else
  226. // {
  227. // popMsg(downLoadFileInfo.sMessage, QString::fromLocal8Bit("系统提示"), this);
  228. // }
  229. }
  230. }
  231. }