1
0

uiCheckMedium.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #include "./uiCheckMedium.h"
  2. #include "./resource.h"
  3. #include <strsafe.h>
  4. #include "./uiEraseMedium.h"
  5. #include "./uiUnitReady.h"
  6. #define AINSTATE_DISABLE PRIMOSDK_LOCK
  7. #define AINSTATE_ENABLE PRIMOSDK_UNLOCK
  8. CheckMediumUI::CheckMediumUI(void)
  9. {
  10. hwnd = NULL;
  11. drive = NULL;
  12. primoSDK = NULL;
  13. errPrimo = 0;
  14. errReady = 0;
  15. }
  16. CheckMediumUI::~CheckMediumUI(void)
  17. {
  18. if (hwnd) DestroyWindow(hwnd);
  19. }
  20. DWORD CheckMediumUI::Check(obj_primo *primoSDK, DWORD *drive, WAMEDIUMINFO *medium, const wchar_t *description, BOOL disableAIN, BOOL showErase, HWND ownerWnd)
  21. {
  22. if (!medium) return CHECKMEDIUMUI_DISCNOTSET;
  23. if (!drive) return CHECKMEDIUMUI_DRIVENOTSET;
  24. if (!primoSDK) return CHECKMEDIUMUI_PRIMOSDKNOTSET;
  25. this->desiredMedium = medium;
  26. this->drive = drive;
  27. this->primoSDK = primoSDK;
  28. this->disableAIN = (disableAIN) ? AINSTATE_DISABLE : 0 ;
  29. this->showErase = showErase;
  30. this->description = description;
  31. this->ownerWnd = ownerWnd;
  32. hwnd = NULL;
  33. errPrimo = PRIMOSDK_OK;
  34. errReady = CHECKMEDIUMUI_NOMATCH;
  35. deadLoop = 0;
  36. LPCDLGTEMPLATE templ = NULL;
  37. HRSRC hres = FindResourceExW(hResource, MAKEINTRESOURCEW(5), MAKEINTRESOURCEW(IDD_DLG_WRONGMEDIUM), MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL));
  38. if (hres) templ = (LPCDLGTEMPLATE)LoadResource(hResource, hres);
  39. HWND dlgWnd = CreateDialogIndirectParamW(dllInstance, templ, ownerWnd, (DLGPROC)WndProc, (LPARAM)this);
  40. if (!dlgWnd) return CHECKMEDIUMUI_UNABLETOCREATEDIALOG;
  41. MSG msg;
  42. BOOL ret;
  43. while( 0 != (ret = GetMessageW(&msg, hwnd, 0, 0)))
  44. {
  45. if (ret == -1)
  46. {
  47. errReady = CHECKMEDIUMUI_MESSAGEPUMPERROR;
  48. break;
  49. }
  50. if (IsDialogMessage(hwnd, &msg)) continue;
  51. TranslateMessage(&msg);
  52. DispatchMessageW(&msg);
  53. }
  54. return errReady;
  55. }
  56. DWORD CheckMediumUI::Rescan(void)
  57. {
  58. wchar_t buffer[256] = {0};
  59. if (hwnd)
  60. {
  61. EnableWindow(GetDlgItem(hwnd, IDCANCEL), FALSE);
  62. LoadStringW(hResource, IDS_UPDATINGDATA, buffer, 256);
  63. SetWindowLong(GetDlgItem(hwnd, IDC_LBL_DETECTEDMEDIUM), GWL_STYLE, WS_CHILD | WS_VISIBLE | SS_CENTER | SS_CENTERIMAGE);
  64. SetDlgItemTextW(hwnd, IDC_LBL_DETECTEDMEDIUM, buffer);
  65. }
  66. if (!drive) return CHECKMEDIUMUI_DRIVENOTSET;
  67. if (!primoSDK) return CHECKMEDIUMUI_PRIMOSDKNOTSET;
  68. UpdatingDataUI updateDlg;
  69. LoadStringW(hResource, IDS_READINGDISCINFO, buffer, 256);
  70. updateDlg.Show(800, buffer, TRUE, hwnd);
  71. WAMEDIUMINFO detectedMedium;
  72. ZeroMemory(&detectedMedium, sizeof(WAMEDIUMINFO));
  73. errPrimo = GetMediumInfo(primoSDK, drive, &detectedMedium);
  74. updateDlg.Hide();
  75. if (PRIMOSDK_OK != errPrimo)
  76. {
  77. SetWindowLong(GetDlgItem(hwnd, IDC_LBL_DETECTEDMEDIUM), GWL_STYLE, WS_CHILD | WS_VISIBLE | SS_CENTER | SS_CENTERIMAGE);
  78. LoadStringW(hResource, IDS_UNABLEGETDISCINFO, buffer, 256);
  79. SetDlgItemTextW(hwnd, IDC_LBL_DETECTEDMEDIUM, buffer);
  80. EnableWindow(GetDlgItem(hwnd, IDCANCEL), FALSE);
  81. EnableWindow(GetDlgItem(hwnd, IDC_BTN_ERASE), FALSE);
  82. EnableWindow(hwnd, FALSE);
  83. if (AINSTATE_DISABLE == disableAIN && primoSDK)
  84. {
  85. primoSDK->UnitAIN(drive, disableAIN);
  86. disableAIN = AINSTATE_ENABLE;
  87. }
  88. UnitReadyUI ready;
  89. DWORD test = ready.Check(primoSDK, drive, FALSE, hwnd);
  90. if (UNITREADYUI_DRIVEREADY == test) deadLoop++;
  91. if (deadLoop == 5)
  92. { /// dead loop detected - PRIMOSDK broken - recomended restart of the computer
  93. errReady = CHECKMEDIUMUI_DEADLOOP;
  94. if(hwnd) PostMessage(hwnd, WM_DESTROY, 0, 0);
  95. return errReady;
  96. }
  97. EnableWindow(hwnd, TRUE);
  98. UpdateWindow(hwnd);
  99. switch(test)
  100. {
  101. case UNITREADYUI_DRIVEREADY:
  102. break;
  103. case UNITREADYUI_PRIMOSDKERROR:
  104. errReady = CHECKMEDIUMUI_PRIMOSDKERROR;
  105. break;
  106. case UNITREADYUI_CANCELED:
  107. errReady = CHECKMEDIUMUI_CANCELED;
  108. break;
  109. default:
  110. errReady = CHECKMEDIUMUI_PRIMOSDKERROR;
  111. break;
  112. }
  113. if (UNITREADYUI_DRIVEREADY != test)
  114. {
  115. if(hwnd) PostMessage(hwnd, WM_DESTROY, 0, 0);
  116. return errReady;
  117. }
  118. PostMessage(hwnd, WM_COMMAND, MAKEWPARAM(IDOK, BN_CLICKED), 0); // start other rescan
  119. errReady = CHECKMEDIUMUI_DRIVENOTREADY;
  120. return errReady;
  121. }
  122. if (hwnd)
  123. {
  124. EnableWindow(GetDlgItem(hwnd, IDCANCEL), TRUE);
  125. SetDlgItemTextW(hwnd, IDC_LBL_DETECTEDMEDIUM, L"");
  126. }
  127. errReady = CHECKMEDIUMUI_MATCH;
  128. if (CHECKMEDIUMUI_MATCH == errReady && MAXDWORD != desiredMedium->medium && desiredMedium->medium != detectedMedium.medium) errReady = CHECKMEDIUMUI_NOMATCH;
  129. if (CHECKMEDIUMUI_MATCH == errReady && MAXDWORD != desiredMedium->mediumType && desiredMedium->mediumType != detectedMedium.mediumType) errReady = CHECKMEDIUMUI_NOMATCH;
  130. if (CHECKMEDIUMUI_MATCH == errReady && MAXDWORD != desiredMedium->mediumFormat && desiredMedium->mediumFormat != detectedMedium.mediumFormat) errReady = CHECKMEDIUMUI_NOMATCH;
  131. if (CHECKMEDIUMUI_MATCH == errReady && MAXDWORD != desiredMedium->tracks && desiredMedium->tracks != detectedMedium.tracks) errReady = CHECKMEDIUMUI_NOMATCH;
  132. if (CHECKMEDIUMUI_MATCH == errReady && MAXDWORD != desiredMedium->erasable && desiredMedium->erasable != detectedMedium.erasable) errReady = CHECKMEDIUMUI_NOMATCH;
  133. if (CHECKMEDIUMUI_MATCH == errReady && MAXDWORD != desiredMedium->freeSectors && desiredMedium->freeSectors > detectedMedium.freeSectors) errReady = CHECKMEDIUMUI_NOMATCH;
  134. if (CHECKMEDIUMUI_MATCH == errReady && MAXDWORD != desiredMedium->usedSectors && desiredMedium->usedSectors < detectedMedium.usedSectors) errReady = CHECKMEDIUMUI_NOMATCH;
  135. if (CHECKMEDIUMUI_MATCH == errReady && MAXDWORD != desiredMedium->recordable && desiredMedium->recordable != detectedMedium.recordable) errReady = CHECKMEDIUMUI_NOMATCH;
  136. if (CHECKMEDIUMUI_MATCH == errReady && MAXDWORD != desiredMedium->isCD && desiredMedium->isCD != detectedMedium.isCD) errReady = CHECKMEDIUMUI_NOMATCH;
  137. if (CHECKMEDIUMUI_MATCH == errReady && MAXDWORD != desiredMedium->isDCD && desiredMedium->isDCD != detectedMedium.isDCD) errReady = CHECKMEDIUMUI_NOMATCH;
  138. if (CHECKMEDIUMUI_MATCH == errReady && MAXDWORD != desiredMedium->isDVD && desiredMedium->isDVD != detectedMedium.isDVD) errReady = CHECKMEDIUMUI_NOMATCH;
  139. if (CHECKMEDIUMUI_MATCH == errReady && MAXDWORD != desiredMedium->isDLDVD && desiredMedium->isDLDVD != detectedMedium.isDLDVD) errReady = CHECKMEDIUMUI_NOMATCH;
  140. if (CHECKMEDIUMUI_MATCH == errReady)
  141. {
  142. if(hwnd) PostMessage(hwnd, WM_DESTROY, 0, 0);
  143. }
  144. else if (hwnd)
  145. {
  146. wchar_t buffer[4096] = {0};
  147. SetWindowLong(GetDlgItem(hwnd, IDC_LBL_DETECTEDMEDIUM), GWL_STYLE, WS_CHILD | WS_VISIBLE | SS_LEFT);
  148. SetDlgItemTextW(hwnd, IDC_LBL_DETECTEDMEDIUM, GetMediumInfoText(buffer, 4096, &detectedMedium));
  149. if (showErase) EnableWindow(GetDlgItem(hwnd, IDC_BTN_ERASE), (detectedMedium.erasable && detectedMedium.usedSectors > 0));
  150. ShowWindow(hwnd, SW_SHOWNORMAL);
  151. SetForegroundWindow(hwnd);
  152. BringWindowToTop(hwnd);
  153. MessageBeep(MB_ICONEXCLAMATION);
  154. UpdateWindow(hwnd);
  155. }
  156. return errReady;
  157. }
  158. wchar_t* CheckMediumUI::GetMediumInfoText(wchar_t *buffer, unsigned int cchBuffer, WAMEDIUMINFO *info)
  159. {
  160. buffer[0] = 0x0000;
  161. wchar_t format[64] = {0}, freeSize[256] = {0}, usedSize[256] = {0}, medium[32] = {0}, mtype[128] = {0}, mformat[128] = {0}, erasable[32] = {0};
  162. LoadStringW(hResource, IDS_DISCINFOFORMAT, format, 64);
  163. LoadStringW(hResource, (info->erasable) ? IDS_DISCERASABLE : IDS_DISCNONERASABLE, erasable, 32);
  164. StringCchPrintfW(buffer, cchBuffer, format, GetMediumText(medium, 32, info->medium),
  165. erasable,
  166. GetSizeText(freeSize, 256, info->freeSectors),
  167. GetSizeText(usedSize, 256, info->usedSectors),
  168. GetMediumFormatText(mformat, 128,info->mediumFormat),
  169. GetMediumTypeText(mtype, 128, info->mediumType));
  170. return buffer;
  171. }
  172. wchar_t* CheckMediumUI::GetSizeText(wchar_t *buffer, unsigned int cchBuffer, unsigned int sectors)
  173. {
  174. wchar_t format[32] = {0},tmp[256] = {0};
  175. DWORD minutes = sectors/(75*60);
  176. DWORD kb = MulDiv(sectors, 2048,1024);
  177. DWORD mb = kb / 1024;
  178. LoadStringW(hResource, IDS_DISCSIZEFORMAT, format, 32);
  179. StringCchPrintfW(tmp, 256, format, minutes, mb, sectors);
  180. StringCchCatW(buffer, cchBuffer, tmp);
  181. return buffer;
  182. }
  183. void CheckMediumUI::OnInitDialog(HWND hwndDlg)
  184. {
  185. hwnd = hwndDlg;
  186. HANDLE hImage = LoadBitmapW(hResource, MAKEINTRESOURCEW(IDB_DISC1));
  187. if(hImage==NULL){
  188. hImage = LoadBitmapW(dllInstance, MAKEINTRESOURCEW(IDB_DISC1));
  189. }
  190. SendDlgItemMessage(hwnd, IDC_PIC, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hImage);
  191. wchar_t format[64] = {0}, buffer[66] = {0};
  192. LoadStringW(hResource, IDS_WRONGMEDIUM, format, 64);
  193. StringCchPrintfW(buffer, 64, format, (char)*drive);
  194. SetWindowTextW(GetDlgItem(hwnd, IDC_CAPTION), buffer);
  195. SetWindowTextW(GetDlgItem(hwnd, IDC_LBL_REQUESTEDMEDIUM), description);
  196. HWND btnWnd = GetDlgItem(hwnd, IDC_BTN_ERASE);
  197. EnableWindow(btnWnd, showErase);
  198. ShowWindow(btnWnd, showErase);
  199. Rescan();
  200. }
  201. void CheckMediumUI::OnCancel(void)
  202. {
  203. wchar_t msg[256] = {0}, caption[64] = {0};
  204. LoadStringW(hResource, IDS_MB_CANCELOPERATION, msg, 256);
  205. LoadStringW(hResource, IDS_CONFIRMATION, caption, 64);
  206. if (MessageBoxW(hwnd, msg, caption, MB_YESNO | MB_ICONQUESTION) == IDYES)
  207. {
  208. errReady = CHECKMEDIUMUI_CANCELED;
  209. if(hwnd) DestroyWindow(hwnd);
  210. hwnd = NULL;
  211. }
  212. }
  213. void CheckMediumUI::OnDestroy(void)
  214. {
  215. if (AINSTATE_ENABLE == disableAIN && primoSDK)
  216. {
  217. primoSDK->UnitAIN(drive, disableAIN);
  218. disableAIN = AINSTATE_DISABLE;
  219. }
  220. ShowWindow(hwnd, SW_HIDE);
  221. hwnd = NULL;
  222. drive = NULL;
  223. primoSDK = NULL;
  224. deadLoop = 0;
  225. }
  226. void CheckMediumUI::OnEraseClicked(void)
  227. {
  228. EraseMediumUI eraseMedium;
  229. eraseMedium.Erase(*drive, FALSE, hwnd); // do not perform disc check
  230. UpdateWindow(hwnd);
  231. Rescan();
  232. }
  233. LRESULT CheckMediumUI::WndProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  234. {
  235. static CheckMediumUI *object = NULL;
  236. switch(uMsg)
  237. {
  238. case WM_INITDIALOG:
  239. object = (CheckMediumUI*)lParam;
  240. object->OnInitDialog(hwndDlg);
  241. break;
  242. case WM_DESTROY:
  243. object->OnDestroy();
  244. PostQuitMessage(object->errReady);
  245. break;
  246. case WM_COMMAND:
  247. switch(LOWORD(wParam))
  248. {
  249. case IDOK:
  250. object->Rescan();
  251. break;
  252. case IDCANCEL:
  253. object->OnCancel();
  254. break;
  255. case IDC_BTN_ERASE:
  256. if (BN_CLICKED == HIWORD(wParam)) object->OnEraseClicked();
  257. break;
  258. }
  259. break;
  260. }
  261. return 0;
  262. }