uiUnitReady.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #include "./uiUnitReady.h"
  2. #include "./resource.h"
  3. #include <strsafe.h>
  4. #define TIMER_REFRESH_ID 1979
  5. #define TIMER_REFRESH_INTERVAL 300
  6. UnitReadyUI::UnitReadyUI(void)
  7. {
  8. hwnd = NULL;
  9. drive = NULL;
  10. primoSDK = NULL;
  11. errPrimo = 0;
  12. errReady = 0;
  13. updateDlg = NULL;
  14. }
  15. UnitReadyUI::~UnitReadyUI(void)
  16. {
  17. if (updateDlg) delete(updateDlg);
  18. updateDlg = NULL;
  19. if (hwnd) DestroyWindow(hwnd);
  20. }
  21. DWORD UnitReadyUI::Check(obj_primo *primoSDK, DWORD *drive, BOOL showRetry, HWND ownerWnd)
  22. {
  23. if (!drive) return UNITREADYUI_DRIVENOTSET;
  24. if (!primoSDK) return UNITREADYUI_PRIMOSDKNOTSET;
  25. this->drive = drive;
  26. this->primoSDK = primoSDK;
  27. hwnd = NULL;
  28. errPrimo = PRIMOSDK_OK;
  29. errReady = UNITREADYUI_NOTREADY;
  30. statSense = MAXDWORD;
  31. statAsc = MAXDWORD;
  32. statAscQ = MAXDWORD;
  33. if (updateDlg) delete(updateDlg);
  34. updateDlg = NULL;
  35. Rescan();
  36. if (UNITREADYUI_DRIVEREADY == errReady || UNITREADYUI_CANCELED == errReady || UNITREADYUI_PRIMOSDKERROR == errReady) return errReady;
  37. LPCDLGTEMPLATE templ = NULL;
  38. HRSRC hres = FindResourceExW(hResource, MAKEINTRESOURCEW(5), MAKEINTRESOURCEW(IDD_DLG_UNITNOTREADY), MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL));
  39. if (hres) templ = (LPCDLGTEMPLATE)LoadResource(hResource, hres);
  40. HWND dlgWnd = CreateDialogIndirectParamW(dllInstance, templ, ownerWnd, (DLGPROC)WndProc, (LPARAM)this);
  41. if (!dlgWnd) return UNITREADYUI_UNABLETOCREATEDIALOG;
  42. wchar_t caption[64] = {0}, buffer[48] = {0};
  43. LoadStringW(hResource, IDS_UNITNOTREADY, buffer, 48);
  44. StringCchPrintfW(caption, 64, buffer, (char)*drive);
  45. SetWindowTextW(GetDlgItem(hwnd, IDC_CAPTION), caption);
  46. ShowWindow(GetDlgItem(hwnd, IDOK), showRetry);
  47. MSG msg;
  48. BOOL ret;
  49. while( 0 != (ret = GetMessageW(&msg, NULL, 0, 0)))
  50. {
  51. if (ret == -1)
  52. {
  53. errReady = UNITREADYUI_MESSAGEPUMPERROR;
  54. break;
  55. }
  56. if (IsDialogMessage(hwnd, &msg)) continue;
  57. TranslateMessage(&msg);
  58. DispatchMessageW(&msg);
  59. }
  60. return errReady;
  61. }
  62. DWORD UnitReadyUI::Rescan(void)
  63. {
  64. if (!drive) return UNITREADYUI_DRIVENOTSET;
  65. if (!primoSDK) return UNITREADYUI_PRIMOSDKNOTSET;
  66. if (hwnd) KillTimer(hwnd, TIMER_REFRESH_ID);
  67. errPrimo = primoSDK->UnitReady(drive);
  68. switch(errPrimo)
  69. {
  70. case PRIMOSDK_NOTREADY:
  71. if (hwnd)
  72. {
  73. DWORD cmd(0), sense(0), asc(0), ascq(0);
  74. primoSDK->UnitStatus(drive, &cmd, &sense, &asc, &ascq);
  75. if (sense != statSense || asc != statAsc || ascq != statAscQ)
  76. {
  77. statSense = sense;
  78. statAsc = asc;
  79. statAscQ = ascq;
  80. if ((sense == 0x02 && asc == 0x04 && ascq == 0x01) ||
  81. (sense == 0x06 && asc == 0x28 && ascq == 0x00))
  82. {
  83. if (!updateDlg)
  84. {
  85. // ShowWindow(hwnd, SW_HIDE);
  86. UpdateWindow(GetParent(hwnd));
  87. updateDlg = new UpdatingDataUI;
  88. wchar_t buffer[64] = {0};
  89. LoadStringW(hResource, IDS_WAITINGFORDRIVE, buffer, 64);
  90. updateDlg->Show(0, buffer, TRUE, hwnd);
  91. }
  92. }
  93. else
  94. {
  95. if(updateDlg)
  96. {
  97. updateDlg->Hide();
  98. delete(updateDlg);
  99. updateDlg = NULL;
  100. }
  101. wchar_t buffer[256] = {0}, pe[512] = {0};
  102. StringCchPrintfW(buffer, 256, L"%s.", GetUnitStatusText(pe, 512, sense, asc, ascq));
  103. SetWindowTextW(GetDlgItem(hwnd, IDC_LBL_REASON_VAL), buffer);
  104. ShowWindow(hwnd, SW_SHOW);
  105. SetForegroundWindow(hwnd);
  106. BringWindowToTop(hwnd);
  107. UpdateWindow(hwnd);
  108. MessageBeep(MB_ICONEXCLAMATION);
  109. }
  110. }
  111. }
  112. errReady = UNITREADYUI_NOTREADY;
  113. SetTimer(hwnd, TIMER_REFRESH_ID, TIMER_REFRESH_INTERVAL, NULL);
  114. break;
  115. case PRIMOSDK_OK:
  116. if (updateDlg)
  117. {
  118. updateDlg->Hide();
  119. delete(updateDlg);
  120. updateDlg = NULL;
  121. }
  122. errReady = UNITREADYUI_DRIVEREADY;
  123. if(hwnd) PostMessage(hwnd, WM_DESTROY, 0, 0);
  124. break;
  125. default:
  126. if (updateDlg)
  127. {
  128. updateDlg->Hide();
  129. delete(updateDlg);
  130. updateDlg = NULL;
  131. }
  132. errReady = UNITREADYUI_PRIMOSDKERROR;
  133. if(hwnd) PostMessage(hwnd, WM_DESTROY, 0, 0);
  134. hwnd = NULL;
  135. }
  136. return errReady;
  137. }
  138. void UnitReadyUI::OnInitDialog(HWND hwndDlg)
  139. {
  140. hwnd = hwndDlg;
  141. HANDLE hImage =LoadBitmapW(hResource, MAKEINTRESOURCEW(IDB_DRIVE1));
  142. if(hImage==NULL){
  143. hImage = LoadBitmapW(dllInstance, MAKEINTRESOURCEW(IDB_DRIVE1));
  144. }
  145. SendDlgItemMessage(hwnd, IDC_PIC, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hImage);
  146. Rescan();
  147. }
  148. void UnitReadyUI::OnCancel(void)
  149. {
  150. KillTimer(hwnd, TIMER_REFRESH_ID);
  151. wchar_t msg[256] = {0}, caption[64] = {0};
  152. LoadStringW(hResource, IDS_MB_CANCELOPERATION, msg, 256);
  153. LoadStringW(hResource, IDS_CONFIRMATION, caption, 64);
  154. if (MessageBoxW(hwnd, msg, caption, MB_YESNO | MB_ICONQUESTION) == IDYES)
  155. {
  156. errReady = UNITREADYUI_CANCELED;
  157. if(hwnd) DestroyWindow(hwnd);
  158. hwnd = NULL;
  159. }
  160. else
  161. {
  162. SetTimer(hwnd, TIMER_REFRESH_ID, TIMER_REFRESH_INTERVAL, NULL);
  163. }
  164. }
  165. void UnitReadyUI::OnDestroy(void)
  166. {
  167. ShowWindow(hwnd, SW_HIDE);
  168. hwnd = NULL;
  169. drive = NULL;
  170. primoSDK = NULL;
  171. }
  172. LRESULT UnitReadyUI::WndProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  173. {
  174. static UnitReadyUI *object = NULL;
  175. switch(uMsg)
  176. {
  177. case WM_INITDIALOG:
  178. object = (UnitReadyUI*)lParam;
  179. object->OnInitDialog(hwndDlg);
  180. break;
  181. case WM_DESTROY:
  182. object->OnDestroy();
  183. PostQuitMessage(object->errReady);
  184. break;
  185. case WM_COMMAND:
  186. switch(LOWORD(wParam))
  187. {
  188. case IDOK:
  189. object->Rescan();
  190. break;
  191. case IDCANCEL:
  192. object->OnCancel();
  193. break;
  194. }
  195. break;
  196. case WM_TIMER:
  197. switch(wParam)
  198. {
  199. case TIMER_REFRESH_ID:
  200. object->Rescan();
  201. break;
  202. }
  203. break;
  204. }
  205. return 0;
  206. }