uiEraseMedium.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #include "./uiEraseMedium.h"
  2. #include "api.h"
  3. #include <api/service/waservicefactory.h>
  4. #include "./resource.h"
  5. #include <strsafe.h>
  6. #include <commctrl.h>
  7. #include "./uiCheckMedium.h"
  8. #define ERASETIME_QUICKMODE 50 // in seconds
  9. #define ERASETIME_FULLMODE 510 // in seconds
  10. #define TIMER_CLOCK_ID 1979
  11. #define TIMER_CLOCK_INTERVAL 1000
  12. EraseMediumUI::EraseMediumUI(void)
  13. {
  14. eraseMedium = NULL;
  15. }
  16. EraseMediumUI::~EraseMediumUI(void)
  17. {
  18. if (eraseMedium)
  19. {
  20. delete(eraseMedium);
  21. eraseMedium = NULL;
  22. }
  23. }
  24. DWORD EraseMediumUI::SetEject(int ejectmode)
  25. {
  26. return (eraseMedium) ? eraseMedium->SetEject(ejectmode) : 0;
  27. }
  28. DWORD EraseMediumUI::Erase(DWORD drive, BOOL discCheck, HWND ownerWnd)
  29. {
  30. this->drive = drive;
  31. this->discCheck = discCheck;
  32. DWORD retCode;
  33. LPCDLGTEMPLATE templ = NULL;
  34. HRSRC hres = FindResourceExW(hResource, MAKEINTRESOURCEW(5), MAKEINTRESOURCEW(IDD_DLG_ERASEMEDIUMPREPARE), MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL));
  35. templ = (hres) ? (LPCDLGTEMPLATE)LoadResource(hResource, hres) : NULL;
  36. retCode = (DWORD)DialogBoxIndirectParamW(dllInstance, templ, ownerWnd, (DLGPROC)PrepareWndProc, (LPARAM)this);
  37. if (ERASEMEDIUMUI_OK != retCode) return retCode;
  38. hres = FindResourceExW(hResource, MAKEINTRESOURCEW(5), MAKEINTRESOURCEW(IDD_DLG_ERASEMEDIUMSTATUS), MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL));
  39. templ = (hres) ? (LPCDLGTEMPLATE)LoadResource(hResource, hres) : NULL;
  40. retCode = (DWORD)DialogBoxIndirectParamW(dllInstance, templ, ownerWnd, (DLGPROC)EraseWndProc, (LPARAM)this);
  41. if (ERASEMEDIUM_ABORTED == retCode) return ERASEMEDIUMUI_CANCELED;
  42. else if (retCode > ERASEMEDIUM_ERROR) return ERASEMEDIUMUI_ERROR;
  43. return ERASEMEDIUMUI_OK;
  44. }
  45. void EraseMediumUI::OnPrepareInit(HWND hwndDlg)
  46. {
  47. prepareWnd = hwndDlg;
  48. wchar_t format[96] = {0}, buffer[98] = {0};
  49. LoadStringW(hResource, IDS_SELECTERASEMETHOD, format, 96);
  50. StringCchPrintfW(buffer, 98, format, drive);
  51. SetDlgItemTextW(prepareWnd, IDC_CAPTION, buffer);
  52. HWND cmbWnd = GetDlgItem(prepareWnd, IDC_CMB_ERASEMETHOD);
  53. LoadStringW(hResource, IDS_QUICKERASE, buffer, 98);
  54. SendMessageW(cmbWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
  55. LoadStringW(hResource, IDS_COMPLETEERASE, buffer, 98);
  56. SendMessageW(cmbWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
  57. SendMessageW(cmbWnd, CB_SETCURSEL, 0, 0);
  58. PostMessageW(prepareWnd, WM_COMMAND, MAKEWPARAM(IDC_CMB_ERASEMETHOD, CBN_SELCHANGE), 0);
  59. ShowWindow(prepareWnd, SW_SHOWNORMAL);
  60. SetForegroundWindow(prepareWnd);
  61. BringWindowToTop(prepareWnd);
  62. }
  63. void EraseMediumUI::OnPrepareOk()
  64. {
  65. switch(SendMessage(GetDlgItem(prepareWnd, IDC_CMB_ERASEMETHOD), CB_GETCURSEL, 0,0))
  66. {
  67. case 1: eraseMode = PRIMOSDK_ERASEFULL; break;
  68. default: eraseMode = PRIMOSDK_ERASEQUICK; break;
  69. }
  70. if (discCheck)
  71. {
  72. // check that disc is erasable
  73. obj_primo *primo=0;
  74. waServiceFactory *sf = WASABI_API_SVC->service_getServiceByGuid(obj_primo::getServiceGuid());
  75. if (sf) primo = reinterpret_cast<obj_primo *>(sf->getInterface());
  76. if (primo)
  77. {
  78. WAMEDIUMINFO mi;
  79. FillMemory(&mi, sizeof(WAMEDIUMINFO), 0xFF);
  80. mi.erasable = TRUE;
  81. CheckMediumUI cm;
  82. wchar_t buffer[256] = {0};
  83. LoadStringW(hResource, IDS_ERASEREQDISC, buffer, 256);
  84. DWORD retCode = cm.Check(primo, &drive, &mi, buffer, TRUE, FALSE, prepareWnd);
  85. switch(retCode)
  86. {
  87. case CHECKMEDIUMUI_MATCH: eraseCode = ERASEMEDIUMUI_OK; break;
  88. case CHECKMEDIUMUI_CANCELED: eraseCode = ERASEMEDIUMUI_CANCELED; break;
  89. default: eraseCode = ERASEMEDIUMUI_PRIMOSDKERROR; break;
  90. }
  91. sf->releaseInterface(primo);
  92. }
  93. else
  94. eraseCode = ERASEMEDIUMUI_PRIMOSDKERROR;
  95. }
  96. else
  97. {
  98. eraseCode = ERASEMEDIUMUI_OK;
  99. }
  100. EndDialog(prepareWnd, eraseCode);
  101. }
  102. void EraseMediumUI::OnEraseTimerClock(void)
  103. {
  104. actualTime = (GetTickCount() - startTick) / 1000;
  105. wchar_t time[32], *current;
  106. current = time;
  107. GetTimeString(current, 32, actualTime);
  108. unsigned int len = lstrlenW(current);
  109. current[len] = L'/';
  110. len++;
  111. current += len;
  112. if (estimateTime < actualTime) estimateTime = actualTime;
  113. GetTimeString(current, 32 - len, estimateTime);
  114. SetWindowTextW(GetDlgItem(eraseWnd, IDC_LBL_TIME), time);
  115. SendMessage(GetDlgItem(eraseWnd, IDC_PRG_PROGRESS), PBM_SETPOS, (int)(((float)actualTime/(float)estimateTime)*100), 0);
  116. }
  117. void EraseMediumUI::OnEraseInit(HWND hwndDlg)
  118. {
  119. eraseWnd = hwndDlg;
  120. CheckDlgButton(eraseWnd, IDC_CHECK_EJECT, BST_CHECKED);
  121. EnableWindow(GetDlgItem(eraseWnd, IDC_BTN_EJECT), FALSE);
  122. eraseMedium = new(EraseMedium);
  123. if (!eraseMedium) EndDialog(eraseWnd, ERASEMEDIUMUI_UNABLETOCREATEOBJECT);
  124. startTick = GetTickCount();
  125. OnEraseTimerClock();
  126. ShowWindow(eraseWnd, SW_SHOWNORMAL);
  127. SetForegroundWindow(eraseWnd);
  128. BringWindowToTop(eraseWnd);
  129. UpdateWindow(eraseWnd);
  130. SetTimer(eraseWnd, TIMER_CLOCK_ID, TIMER_CLOCK_INTERVAL, NULL);
  131. SendMessage(GetDlgItem(eraseWnd, IDC_PRG_PROGRESS), PBM_SETRANGE, 0, MAKELPARAM(0, 100));
  132. eraseMedium->Start(drive, eraseMode, OnEraseNotify, this, FALSE); // will catch all errors in calback
  133. EnableWindow(GetDlgItem(eraseWnd, IDC_BTN_EJECT), TRUE);
  134. }
  135. void EraseMediumUI::OnEraseClose(DWORD exitCode)
  136. {
  137. EnableWindow(GetDlgItem(eraseWnd, IDC_BTN_EJECT), FALSE);
  138. if (eraseMedium)
  139. {
  140. HWND btnWnd = GetDlgItem(eraseWnd, IDCANCEL);
  141. EnableWindow(btnWnd, FALSE);
  142. eraseMedium->Stop();
  143. wchar_t buffer[24] = {0};
  144. LoadStringW(hResource, IDS_CLOSE, buffer, 24);
  145. SetWindowTextW(btnWnd, buffer);
  146. EnableWindow(btnWnd, TRUE);
  147. primoCode = eraseMedium->GetErrorCode();
  148. delete(eraseMedium);
  149. eraseMedium = NULL;
  150. }
  151. eraseCode = exitCode;
  152. KillTimer(eraseWnd, TIMER_CLOCK_ID);
  153. estimateTime = 0; // will force it to be the same with actual
  154. OnEraseTimerClock();
  155. }
  156. LRESULT EraseMediumUI::PrepareWndProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  157. {
  158. static EraseMediumUI *object = NULL;
  159. switch(uMsg)
  160. {
  161. case WM_INITDIALOG:
  162. object = (EraseMediumUI*)lParam;
  163. object->OnPrepareInit(hwndDlg);
  164. break;
  165. case WM_COMMAND:
  166. switch(LOWORD(wParam))
  167. {
  168. case IDOK:
  169. object->OnPrepareOk();
  170. break;
  171. case IDCANCEL:
  172. {
  173. wchar_t msg[256] = {0}, caption[64] = {0};
  174. LoadStringW(hResource, IDS_MB_CANCELOPERATION, msg, 256);
  175. LoadStringW(hResource, IDS_CONFIRMATION, caption, 64);
  176. if (MessageBoxW(hwndDlg, msg, caption, MB_YESNO | MB_ICONQUESTION) == IDYES)
  177. {
  178. EndDialog(hwndDlg, LOWORD(wParam));
  179. }
  180. break;
  181. }
  182. case IDC_CMB_ERASEMETHOD:
  183. switch(HIWORD(wParam))
  184. {
  185. case CBN_SELCHANGE:
  186. switch(SendMessage(GetDlgItem(hwndDlg, IDC_CMB_ERASEMETHOD), CB_GETCURSEL, 0,0))
  187. {
  188. case 0:
  189. object->estimateTime= ERASETIME_QUICKMODE;
  190. break;
  191. case 1:
  192. object->estimateTime = ERASETIME_FULLMODE;
  193. break;
  194. default:
  195. object->estimateTime =0;
  196. break;
  197. }
  198. wchar_t time[16] = {0};
  199. SetWindowTextW(GetDlgItem(hwndDlg, IDC_LBL_TIME), GetTimeString(time, 16, object->estimateTime));
  200. break;
  201. }
  202. break;
  203. }
  204. break;
  205. }
  206. return 0;
  207. }
  208. LRESULT EraseMediumUI::EraseWndProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  209. {
  210. static EraseMediumUI *object = NULL;
  211. switch(uMsg)
  212. {
  213. case WM_INITDIALOG:
  214. object = (EraseMediumUI*)lParam;
  215. object->OnEraseInit(hwndDlg);
  216. break;
  217. case WM_CLOSE:
  218. object->OnEraseClose((DWORD)wParam);
  219. return 1;
  220. case WM_COMMAND:
  221. switch(LOWORD(wParam))
  222. {
  223. case IDCANCEL:
  224. if (object->eraseMedium)
  225. {
  226. wchar_t msg[256] = {0}, caption[64] = {0};
  227. LoadStringW(hResource, IDS_MB_CANCELOPERATION, msg, 256);
  228. LoadStringW(hResource, IDS_CONFIRMATION, caption, 64);
  229. if (MessageBoxW(hwndDlg, msg, caption, MB_YESNO | MB_ICONQUESTION) == IDYES) object->OnEraseClose(ERASEMEDIUM_ABORTED);
  230. }
  231. EndDialog(hwndDlg, object->eraseCode);
  232. break;
  233. case IDC_BTN_EJECT:
  234. if (BN_CLICKED == HIWORD(wParam) && object->eraseMedium)
  235. {
  236. object->eraseMedium->SetEject(BST_CHECKED == IsDlgButtonChecked (hwndDlg,IDC_BTN_EJECT));
  237. }
  238. break;
  239. }
  240. break;
  241. case WM_TIMER:
  242. switch(wParam)
  243. {
  244. case TIMER_CLOCK_ID:
  245. object->OnEraseTimerClock();
  246. break;
  247. }
  248. break;
  249. }
  250. return 0;
  251. }
  252. DWORD EraseMediumUI::OnEraseNotify(void *sender, void *param, DWORD eraseCode, DWORD primoCode)
  253. {
  254. EraseMediumUI *object = (EraseMediumUI*)param;
  255. unsigned int strcode;
  256. BOOL finished = FALSE;
  257. if (ERASEMEDIUM_ERROR < eraseCode)
  258. {// some error happened;
  259. finished = TRUE;
  260. switch(eraseCode)
  261. {
  262. case ERASEMEDIUM_ALREADYSTARTED: strcode = IDS_ALREADYERASING; break;
  263. case ERASEMEDIUM_UNABLEINITPRIMO: strcode = IDS_PRIMOINITFAILED; break;
  264. case ERASEMEDIUM_BEGINBURNFAILED: strcode = IDS_UNABLEINITERASE; break;
  265. case ERASEMEDIUM_ENDBURNFAILED: strcode = IDS_UNABLEFINERASE; break;
  266. case ERASEMEDIUM_ERASEMEDIUMFAILED: strcode = IDS_MEDIUMERASEFAILED; break;
  267. case ERASEMEDIUM_DEVICENOTREADY: strcode = IDS_DRIVENOTREADY; break;
  268. case ERASEMEDIUM_DISCINFOERROR: strcode = IDS_UNABLEGETDISCINFO; break;
  269. case ERASEMEDIUM_DISCNOTERASABLE: strcode = IDS_DISCNONERASABLE; break;
  270. default: strcode = IDS_UNKNOWNERROR; break;
  271. }
  272. MessageBeep(MB_ICONHAND);
  273. }
  274. else
  275. {
  276. switch(eraseCode)
  277. {
  278. case ERASEMEDIUM_READY: strcode = IDS_READY; break;
  279. case ERASEMEDIUM_INITIALIZING: strcode = IDS_INITIALIZING; break;
  280. case ERASEMEDIUM_ERASING: strcode = IDS_ERASING; break;
  281. case ERASEMEDIUM_FINISHING: strcode = IDS_FINISHING; break;
  282. case ERASEMEDIUM_CANCELING: strcode = IDS_CANCELING; break;
  283. case ERASEMEDIUM_COMPLETED: strcode = IDS_COMPLETED; finished = TRUE; MessageBeep(MB_OK); break;
  284. case ERASEMEDIUM_ABORTED: strcode = IDS_ABORTED; finished = TRUE; MessageBeep(MB_OK); break;
  285. }
  286. }
  287. wchar_t buffer[224] = {0};
  288. LoadStringW(hResource, strcode, buffer, 224);
  289. if (ERASEMEDIUM_ERROR < eraseCode)
  290. {
  291. wchar_t txtStatus[256] = {0}, error[24] = {0};
  292. LoadStringW(hResource, IDS_ERROR, error, 24);
  293. StringCchPrintfW(txtStatus, 256, L"%s! %s", error, buffer);
  294. }
  295. else
  296. {
  297. SetWindowTextW(GetDlgItem(object->eraseWnd, IDC_LBL_STATUS), buffer);
  298. }
  299. if (finished) PostMessage(object->eraseWnd, WM_CLOSE, eraseCode, 0);
  300. return ERASEMEDIUM_CONTINUE;
  301. }