InstallPacDlg.cpp
上传用户:zhanglf88
上传日期:2013-11-19
资源大小:6036k
文件大小:13k
源码类别:

金融证券系统

开发平台:

Visual C++

  1. // InstallPacDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "InstallPacDlg.h"
  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #undef THIS_FILE
  8. static char THIS_FILE[] = __FILE__;
  9. #endif
  10. /////////////////////////////////////////////////////////////////////////////
  11. // CInstallPacDlg dialog
  12. HANDLE CInstallPacDlg::m_hEventKillInstallThread  = NULL;
  13. HANDLE CInstallPacDlg::m_hEventInstallThreadKilled  = NULL;
  14. BOOL CALLBACK InstallCallback(DWORD dwCode, DWORD dwProgress,
  15. LPCTSTR lpszMsg, void *cookie)
  16. {
  17. HWND hMainWnd = (HWND)cookie;
  18. if (PROG_PROGRESS == dwCode && ::IsWindow(hMainWnd))
  19. {
  20. ::SendMessage(hMainWnd, WM_USER_INSTALL_PROGRESS, dwProgress, (LPARAM)lpszMsg);
  21. }
  22. else if (::IsWindow(hMainWnd))
  23. {
  24. ::SendMessage(hMainWnd, WM_USER_INSTALL_MESSAGE, dwCode, (LPARAM)lpszMsg);
  25. }
  26. // User wants to quit program
  27. if (WAIT_OBJECT_0 == WaitForSingleObject(CInstallPacDlg::m_hEventKillInstallThread,0))
  28. {
  29. ::PostMessage(hMainWnd, WM_USER_INSTALL_END, FALSE, 0);
  30. SetEvent(CInstallPacDlg::m_hEventInstallThreadKilled);
  31. AfxEndThread(0, TRUE);
  32. return FALSE;
  33. }
  34. return TRUE;
  35. }
  36. UINT InstallMain(LPVOID pParam)
  37. {
  38. LPINSTALL_INFO pInfo=(LPINSTALL_INFO)pParam;
  39. PostMessage(pInfo->hMainWnd, WM_USER_INSTALL_PROGRESS, 0, 0);
  40. for(int i=0; i<pInfo->pStringArray->GetSize(); i++)
  41. {
  42. CString sFileName = pInfo->pStringArray->ElementAt(i);
  43. ::SendMessage(pInfo->hMainWnd, WM_USER_INSTALL_MESSAGE, PROG_NEWGROUPMSG, (DWORD)sFileName.GetBuffer(sFileName.GetLength()+1));
  44. ::SendMessage(pInfo->hMainWnd, WM_USER_INSTALL_PROGRESS, 0, NULL);
  45. sFileName.ReleaseBuffer();
  46. CPackage pac;
  47. pac.m_nType  = pInfo->nType;
  48. pac.m_bIsZipped = (sFileName.GetLength() > 4 && 0 == sFileName.Right(4).CompareNoCase(".zip"));
  49. pac.m_strDescript = sFileName;
  50. pInfo->pNetDB->InstallPackageEx(pac, sFileName, InstallCallback, pInfo->hMainWnd);
  51. // User wants to quit program
  52. if (WAIT_OBJECT_0 == WaitForSingleObject(CInstallPacDlg::m_hEventKillInstallThread,0))
  53. {
  54. ::PostMessage(pInfo->hMainWnd, WM_USER_INSTALL_END, FALSE, 0);
  55. SetEvent(CInstallPacDlg::m_hEventInstallThreadKilled);
  56. AfxEndThread(0, TRUE);
  57. return 0;
  58. }
  59. }
  60. ::SendMessage(pInfo->hMainWnd, WM_USER_INSTALL_END, TRUE, 0);
  61. AfxEndThread(0, TRUE);
  62. return 0;
  63. }
  64. IMPLEMENT_DYNCREATE(CInstallPacDlg, CPropertyPageEx)
  65. CInstallPacDlg::CInstallPacDlg() : CPropertyPageEx(CInstallPacDlg::IDD)
  66. {
  67. m_pInstallInfo = NULL;
  68. m_bDBModified = FALSE;
  69. m_bCodetableInstalled = FALSE;
  70. CSPTime tmLatestOld = 0;
  71. if (m_netdb.SetRootPath(AfxGetProfile().GetSelfDBPath(), IStStore::dbtypeSelfDB))
  72. m_netdb.GetTimeLocalRange(&tmLatestOld, NULL, NULL);
  73. m_tmLatestOld = tmLatestOld.GetTime();
  74. }
  75. CInstallPacDlg::~CInstallPacDlg()
  76. {
  77. if (m_pInstallInfo)
  78. {
  79. delete m_pInstallInfo;
  80. m_pInstallInfo = NULL;
  81. }
  82. }
  83. void CInstallPacDlg::DoDataExchange(CDataExchange* pDX)
  84. {
  85. CPropertyPageEx::DoDataExchange(pDX);
  86. //{{AFX_DATA_MAP(CInstallPacDlg)
  87. DDX_Control(pDX, IDC_STATIC_PROGRESS, m_staticProgress);
  88. DDX_Control(pDX, IDC_START, m_btnStart);
  89. DDX_Control(pDX, IDC_STOP, m_btnStop);
  90. DDX_Control(pDX, IDC_REMOVE, m_btnRemove);
  91. DDX_Control(pDX, IDC_EXPLORER, m_btnExplorer);
  92. DDX_Control(pDX, IDC_LIST_FILES, m_listFiles);
  93. DDX_Control(pDX, IDC_PROGRESS, m_ctrlProgress);
  94. DDX_Control(pDX, IDC_STATIC_STATUS, m_staticStatus);
  95. //}}AFX_DATA_MAP
  96. }
  97. BEGIN_MESSAGE_MAP(CInstallPacDlg, CPropertyPageEx)
  98. //{{AFX_MSG_MAP(CInstallPacDlg)
  99. ON_BN_CLICKED(IDC_EXPLORER, OnExplorer)
  100. ON_BN_CLICKED(IDC_REMOVE, OnRemove)
  101. ON_NOTIFY(NM_DBLCLK, IDC_LIST_FILES, OnDblclkListFiles)
  102. ON_BN_CLICKED(IDC_STOP, OnStop)
  103. ON_BN_CLICKED(IDC_START, OnStart)
  104. //}}AFX_MSG_MAP
  105. ON_MESSAGE(WM_USER_INSTALL_MESSAGE, OnInstallMessage)
  106. ON_MESSAGE(WM_USER_INSTALL_PROGRESS, OnInstallProgress)
  107. ON_MESSAGE(WM_USER_INSTALL_END, OnInstallEnd)
  108. END_MESSAGE_MAP()
  109. /////////////////////////////////////////////////////////////////////////////
  110. // CInstallPacDlg message handlers
  111. BOOL CInstallPacDlg::OnInitDialog() 
  112. {
  113. CPropertyPageEx::OnInitDialog();
  114. CheckRadioButton(IDC_DAYPACKAGE, IDC_MIN5PACKAGE, IDC_DAYPACKAGE);
  115. m_staticStatus.SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_HIDEWINDOW);
  116. m_ctrlProgress.SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_HIDEWINDOW);
  117. m_btnStop.EnableWindow(FALSE);
  118. CRect rect;
  119. m_listFiles.GetWindowRect(&rect);
  120. m_listFiles.InsertColumn(0, "", LVCFMT_LEFT, rect.Width()-5);
  121. m_imageList.Create(IDB_PACKAGE, 16, 1, RGB(0, 255, 0));
  122. m_listFiles.SetImageList(&m_imageList, LVSIL_SMALL);
  123. m_ctrlProgress.SetRange(0, STKLIB_MAX_PROGRESS);
  124. m_ctrlProgress.SetPos(0);
  125. return TRUE;  // return TRUE unless you set the focus to a control
  126.               // EXCEPTION: OCX Property Pages should return FALSE
  127. }
  128. BOOL CInstallPacDlg::OnSetActive() 
  129. {
  130. CPropertySheetEx * pSheet = DYNAMIC_DOWNCAST(CPropertySheetEx, GetParent() );
  131. if (pSheet)
  132. {
  133. CString strFinish, strClose;
  134. strFinish.LoadString(IDS_FINISHBUTTON);
  135. strClose.LoadString(IDS_CLOSEBUTTON);
  136. CWnd * pWnd = pSheet->GetDlgItem(IDOK);
  137. if (pWnd && ::IsWindow(pWnd->GetSafeHwnd()))
  138. {
  139. pWnd->SetWindowText(strFinish);
  140. pWnd->EnableWindow(FALSE);
  141. }
  142. pWnd = pSheet->GetDlgItem(IDCANCEL);
  143. if (pWnd && ::IsWindow(pWnd->GetSafeHwnd()))
  144. pWnd->SetWindowText(strClose);
  145. }
  146. return CPropertyPageEx::OnSetActive();
  147. }
  148. void CInstallPacDlg::OnExplorer() 
  149. {
  150. // TODO: Add your control notification handler code here
  151. UpdateData(TRUE);
  152. CFileDialog dlg (TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_EXPLORER | OFN_ENABLESIZING | OFN_ALLOWMULTISELECT,
  153. "Package files (*.zip,*.stk,*.dad)|*.zip;*.stk;*.dad|All files (*.*)|*.*||", NULL);
  154. int nResult = dlg.DoModal();
  155. if (IDOK == nResult)
  156. {
  157. POSITION pos = dlg.GetStartPosition();
  158. while (pos)
  159. {
  160. CString sFile = dlg.GetNextPathName(pos);
  161. for(int i=0; i<m_listFiles.GetItemCount(); i++)
  162. {
  163. CString strTemp = m_listFiles.GetItemText(i, 0);
  164. if (0 == strTemp.CompareNoCase(sFile))
  165. break;
  166. }
  167. if (i == m_listFiles.GetItemCount())
  168. m_listFiles.InsertItem(m_listFiles.GetItemCount(), sFile, 0);
  169. }
  170. }
  171. }
  172. void CInstallPacDlg::OnRemove() 
  173. {
  174. // TODO: Add your control notification handler code here
  175. for(int i=m_listFiles.GetItemCount()-1; i>=0; i--)
  176. {
  177. if (LVIS_SELECTED == m_listFiles.GetItemState(i, LVIS_SELECTED))
  178. m_listFiles.DeleteItem(i);
  179. }
  180. m_listFiles.SetItemState(0, LVIS_SELECTED, LVIS_SELECTED);
  181. m_listFiles.RedrawItems(0, m_listFiles.GetItemCount()-1);
  182. }
  183. void CInstallPacDlg::OnDblclkListFiles(NMHDR* pNMHDR, LRESULT* pResult) 
  184. {
  185. // TODO: Add your control notification handler code here
  186. OnRemove();
  187. *pResult = 0;
  188. }
  189. void CInstallPacDlg::OnStart() 
  190. {
  191. // TODO: Add extra validation here
  192. if (0 == m_listFiles.GetItemCount())
  193. {
  194. AfxMessageBox(IDS_SELECTPACKAGE_FIRST, MB_OK | MB_ICONINFORMATION);
  195. return;
  196. }
  197. UpdateData();
  198. // clear error pac msg
  199. m_astrErrorPac.RemoveAll();
  200. m_btnStart.EnableWindow(FALSE);
  201. m_btnStop.EnableWindow(TRUE);
  202. BOOL bOK = TRUE;
  203. if (bOK && !m_netdb.SetRootPath(AfxGetProfile().GetSelfDBPath(), IStStore::dbtypeSelfDB))
  204. {
  205. AfxMessageBox(IDS_ROOTPATH_ERROR, MB_OK | MB_ICONINFORMATION);
  206. bOK = FALSE;
  207. }
  208. if (bOK)
  209. {
  210. m_staticStatus.SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_SHOWWINDOW);
  211. m_ctrlProgress.SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_SHOWWINDOW);
  212. CInstallPacDlg::m_hEventKillInstallThread = CreateEvent(NULL,FALSE,FALSE,NULL);
  213. CInstallPacDlg::m_hEventInstallThreadKilled = CreateEvent(NULL,FALSE,FALSE,NULL);
  214. m_astrPacFiles.RemoveAll();
  215. for(int i=0; i<m_listFiles.GetItemCount(); i++)
  216. m_astrPacFiles.Add(m_listFiles.GetItemText(i, 0));
  217. if (NULL == m_pInstallInfo)
  218. m_pInstallInfo = new INSTALL_INFO;
  219. memset(m_pInstallInfo, 0, sizeof(INSTALL_INFO));
  220. m_pInstallInfo->hMainWnd = GetSafeHwnd();
  221. m_pInstallInfo->pNetDB  = &m_netdb;
  222. m_pInstallInfo->pStringArray= &m_astrPacFiles;
  223. CButton * pbtnDay = (CButton *)GetDlgItem(IDC_DAYPACKAGE);
  224. CButton * pbtnMin5 = (CButton *)GetDlgItem(IDC_MIN5PACKAGE);
  225. m_pInstallInfo->nType = CPackage::packageUnknown;
  226. if (pbtnDay && pbtnDay->GetCheck())
  227. m_pInstallInfo->nType = CPackage::packageDay;
  228. if (pbtnMin5 && pbtnMin5->GetCheck())
  229. m_pInstallInfo->nType = CPackage::packageMin5;
  230. AfxBeginThread(InstallMain, (LPVOID)m_pInstallInfo,THREAD_PRIORITY_NORMAL);
  231. }
  232. else
  233. {
  234. m_btnStart.EnableWindow(TRUE);
  235. m_btnStop.EnableWindow(FALSE);
  236. }
  237. }
  238. void CInstallPacDlg::OnStop() 
  239. {
  240. // TODO: Add your control notification handler code here
  241. if (m_hEventKillInstallThread)
  242. SetEvent(m_hEventKillInstallThread);
  243. // if (m_hEventInstallThreadKilled)
  244. // WaitForSingleObject(m_hEventInstallThreadKilled, INFINITE);
  245. }
  246. LRESULT CInstallPacDlg::OnInstallMessage(WPARAM wParam, LPARAM lParam)
  247. {
  248. // set message and enable buttons
  249. CString strNewMsg;
  250. CString strOldMsg;
  251. m_staticStatus.GetWindowText(strOldMsg);
  252. if (PROG_NEWGROUP == wParam)
  253. {
  254. strNewMsg.LoadString(lParam);
  255. }
  256. else if (PROG_NEWGROUPMSG == wParam)
  257. {
  258. strNewMsg = (LPCTSTR)lParam;
  259. }
  260. else if (PROG_ERRORPAC == wParam)
  261. {
  262. for(int i=0; i<m_astrErrorPac.GetSize(); i++)
  263. {
  264. if (0 == m_astrErrorPac.ElementAt(i).Compare((LPCTSTR)lParam))
  265. break;
  266. }
  267. if (i == m_astrErrorPac.GetSize())
  268. m_astrErrorPac.Add((LPCTSTR)lParam);
  269. CString strMsg = CNetDatabase::GetProgressMessage(wParam);
  270. strMsg += (LPCTSTR)lParam;
  271. if (strMsg.GetLength() > 0)
  272. {
  273. strNewMsg = strOldMsg;
  274. int nIndex = strOldMsg.Find(" -- ");
  275. if (-1 != nIndex)
  276. strNewMsg = strOldMsg.Left(nIndex);
  277. strNewMsg += " -- " + strMsg;
  278. }
  279. }
  280. else
  281. {
  282. CString strMsg = CNetDatabase::GetProgressMessage(wParam);
  283. if (strMsg.GetLength() > 0)
  284. {
  285. strNewMsg = strOldMsg;
  286. int nIndex = strOldMsg.Find(" -- ");
  287. if (-1 != nIndex)
  288. strNewMsg = strOldMsg.Left(nIndex);
  289. strNewMsg += " -- " + strMsg;
  290. }
  291. }
  292. if (strNewMsg.GetLength() > 0)
  293. {
  294. if (0 != strNewMsg.Compare(strOldMsg))
  295. m_staticStatus.SetWindowText(strNewMsg);
  296. }
  297. return 1;
  298. }
  299. LRESULT CInstallPacDlg::OnInstallProgress(WPARAM wParam, LPARAM lParam)
  300. {
  301. // set message and enable buttons
  302. if (wParam < 0)
  303. wParam = 0;
  304. if (wParam > STKLIB_MAX_PROGRESS)
  305. wParam = STKLIB_MAX_PROGRESS;
  306. m_ctrlProgress.SetPos(wParam);
  307. CString strProgressNew;
  308. strProgressNew.Format("%.2f%%", float(wParam)/100.);
  309. CString strProgressOld;
  310. m_staticProgress.GetWindowText(strProgressOld);
  311. if (0 != strProgressNew.Compare(strProgressOld))
  312. m_staticProgress.SetWindowText(strProgressNew);
  313. return 1;
  314. }
  315. LRESULT CInstallPacDlg::OnInstallEnd(WPARAM wParam, LPARAM lParam)
  316. {
  317. m_ctrlProgress.SetPos(STKLIB_MAX_PROGRESS);
  318. CString strProgressNew;
  319. strProgressNew.Format("%.2f%%", 100.);
  320. m_staticProgress.SetWindowText(strProgressNew);
  321. // set message and enable buttons
  322. CString strMsg;
  323. if (wParam)
  324. strMsg.LoadString(IDS_INSTALL_FINISHED);
  325. else
  326. strMsg.LoadString(IDS_INSTALL_PAUSED);
  327. CString strComma;
  328. strComma.LoadString(IDS_COMMA);
  329. CString strFullStop;
  330. strFullStop.LoadString(IDS_FULLSTOP);
  331. for(int i=0; i<m_astrErrorPac.GetSize(); i++)
  332. {
  333. if (0 == i)
  334. strMsg += CNetDatabase::GetProgressMessage(PROG_ERRORPAC);
  335. else
  336. strMsg += strComma;
  337. strMsg += m_astrErrorPac.ElementAt(i);
  338. }
  339. if (m_astrErrorPac.GetSize() > 0)
  340. strMsg += strFullStop;
  341. if (lParam)
  342. strMsg += (LPCTSTR)lParam;
  343. m_staticStatus.SetWindowText(strMsg);
  344. m_btnStart.EnableWindow(TRUE);
  345. m_btnStop.EnableWindow(FALSE);
  346. if (CInstallPacDlg::m_hEventKillInstallThread)
  347. CloseHandle(CInstallPacDlg::m_hEventKillInstallThread);
  348. if (CInstallPacDlg::m_hEventInstallThreadKilled)
  349. CloseHandle(CInstallPacDlg::m_hEventInstallThreadKilled);
  350. CInstallPacDlg::m_hEventKillInstallThread = NULL;
  351. CInstallPacDlg::m_hEventInstallThreadKilled = NULL;
  352. m_bDBModified = TRUE;
  353. return 1;
  354. }
  355. void CInstallPacDlg::OnCompleted()
  356. {
  357. AfxReleaseDB();
  358. char szErr[1024];
  359. if (!AfxInitializeDB(szErr, sizeof(szErr))) AfxMessageBox(szErr, MB_OK|MB_ICONINFORMATION);
  360. if (m_bDBModified)
  361. {
  362. BOOL bRecalculate = FALSE;
  363. CSPTime tmLatest = 0;
  364. if (m_bCodetableInstalled ||
  365. (m_netdb.SetRootPath(AfxGetProfile().GetSelfDBPath(), IStStore::dbtypeSelfDB)
  366. && m_netdb.GetTimeLocalRange(&tmLatest, NULL, NULL)
  367. && tmLatest.GetTime() > m_tmLatestOld.GetTime()))
  368. {
  369. bRecalculate = TRUE;
  370. }
  371. AfxGetStkUIApp()->OnDBChanged(FALSE /*bRecalculate*/);
  372. m_bDBModified = FALSE;
  373. }
  374. }
  375. void CInstallPacDlg::OnCancel() 
  376. {
  377. OnCompleted();
  378. CPropertyPageEx::OnCancel();
  379. }
  380. void CInstallPacDlg::OnOK() 
  381. {
  382. OnCompleted();
  383. CPropertyPageEx::OnOK();
  384. }
  385. BOOL CInstallPacDlg::OnKillActive() 
  386. {
  387. OnCompleted();
  388. return CPropertyPageEx::OnKillActive();
  389. }