Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

573 lines
14 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows/NT **/
  3. /** Copyright (c) 1997-1999 Microsoft Corporation
  4. /**********************************************************************/
  5. #include "precomp.h"
  6. #include "LogPage.h"
  7. #include "CHString1.h"
  8. #include "resource.h"
  9. #include <shlobj.h>
  10. #include "WMIHelp.h"
  11. #include <errno.h>
  12. #include "ShlWapi.h"
  13. const static DWORD logPageHelpIDs[] = { // Context Help IDs
  14. IDC_LOG_PARA, -1,
  15. IDC_STATUS_FRAME, -1,
  16. IDC_DISABLELOGGING, IDH_WMI_CTRL_LOGGING_LOGGING_LEVEL,
  17. IDC_ERRORLOGGING, IDH_WMI_CTRL_LOGGING_LOGGING_LEVEL,
  18. IDC_VERBOSELOGGING, IDH_WMI_CTRL_LOGGING_LOGGING_LEVEL,
  19. IDC_MAXFILESIZE_LABEL, IDH_WMI_CTRL_LOGGING_MAX_SIZE,
  20. IDC_MAXFILESIZE, IDH_WMI_CTRL_LOGGING_MAX_SIZE,
  21. IDC_LOGGINGDIRECTORY_LABEL, IDH_WMI_CTRL_LOGGING_LOCATION,
  22. IDC_LOGGINGDIRECTORY, IDH_WMI_CTRL_LOGGING_LOCATION,
  23. IDC_BROWSE, IDH_WMI_CTRL_LOGGING_BROWSE,
  24. 0, 0
  25. };
  26. CLogPage::~CLogPage(void)
  27. {
  28. }
  29. //-------------------------------------------------------------------------
  30. void CLogPage::InitDlg(HWND hDlg)
  31. {
  32. m_hDlg = hDlg;
  33. ::SendMessage(GetDlgItem(hDlg, IDC_MAXFILESIZE),
  34. EM_LIMITTEXT, 10, 0);
  35. ::SendMessage(GetDlgItem(hDlg, IDC_LOGGINGDIRECTORY),
  36. EM_LIMITTEXT, _MAX_PATH, 0);
  37. }
  38. //---------------------------------------------------------------------------
  39. void CLogPage::Refresh(HWND hDlg)
  40. {
  41. if(m_DS && m_DS->IsNewConnection(&m_sessionID))
  42. {
  43. CHString1 temp;
  44. ULONG iTemp;
  45. CHString1 szNotRemoteable, szUnavailable;
  46. HRESULT hr = S_OK;
  47. BOOL enable = TRUE;
  48. szNotRemoteable.LoadString(IDS_NOT_REMOTEABLE);
  49. szUnavailable.LoadString(IDS_UNAVAILABLE);
  50. PageChanged(PB_LOGGING, false);
  51. // - - - - - - - - - - - - - -
  52. // logging status:
  53. UINT ID = IDC_DISABLELOGGING;
  54. hr = m_DS->GetLoggingStatus(m_oldStatus);
  55. if(SUCCEEDED(hr))
  56. {
  57. switch(m_oldStatus)
  58. {
  59. case DataSource::Disabled: ID = IDC_DISABLELOGGING; break;
  60. case DataSource::ErrorsOnly: ID = IDC_ERRORLOGGING; break;
  61. case DataSource::Verbose: ID = IDC_VERBOSELOGGING; break;
  62. }
  63. enable = TRUE;
  64. CheckRadioButton(hDlg, IDC_DISABLELOGGING, IDC_VERBOSELOGGING,
  65. ID);
  66. }
  67. else //failed
  68. {
  69. enable = FALSE;
  70. }
  71. ::EnableWindow(GetDlgItem(hDlg, IDC_STATUS_FRAME), enable);
  72. ::EnableWindow(GetDlgItem(hDlg, IDC_DISABLELOGGING), enable);
  73. ::EnableWindow(GetDlgItem(hDlg, IDC_ERRORLOGGING), enable);
  74. ::EnableWindow(GetDlgItem(hDlg, IDC_VERBOSELOGGING), enable);
  75. // - - - - - - - - - - - - - -
  76. // max file size:
  77. hr = m_DS->GetLoggingSize(iTemp);
  78. if(SUCCEEDED(hr))
  79. {
  80. enable = TRUE;
  81. temp.Format(_T("%u"), iTemp);
  82. SetWindowText(GetDlgItem(hDlg, IDC_MAXFILESIZE),
  83. temp);
  84. }
  85. else //failed
  86. {
  87. enable = FALSE;
  88. SetWindowText(GetDlgItem(hDlg, IDC_MAXFILESIZE),
  89. _T(""));
  90. }
  91. ::EnableWindow(GetDlgItem(hDlg, IDC_MAXFILESIZE),
  92. enable);
  93. ::EnableWindow(GetDlgItem(hDlg, IDC_MAXFILESIZE_LABEL),
  94. enable);
  95. // - - - - - - - - - - - - - -
  96. // Location:
  97. hr = m_DS->GetLoggingLocation(temp);
  98. if(SUCCEEDED(hr))
  99. {
  100. enable = TRUE;
  101. SetWindowText(GetDlgItem(hDlg, IDC_LOGGINGDIRECTORY),
  102. temp);
  103. }
  104. else
  105. {
  106. enable = FALSE;
  107. SetWindowText(GetDlgItem(hDlg, IDC_LOGGINGDIRECTORY),
  108. _T(""));
  109. }
  110. ::EnableWindow(GetDlgItem(hDlg, IDC_LOGGINGDIRECTORY),
  111. enable);
  112. // browse only works for local connections.
  113. ::EnableWindow(GetDlgItem(hDlg, IDC_BROWSE),
  114. ((BOOL)m_DS->IsLocal() && enable) );
  115. }
  116. }
  117. //------------------------------------------------------------------------
  118. bool CLogPage::GoodPathSyntax(LPCTSTR path)
  119. {
  120. bool retval = true;
  121. if(PathIsUNC(path))
  122. {
  123. TCHAR caption[50] = {0}, threat[256] = {0};
  124. ::LoadString(_Module.GetResourceInstance(),
  125. IDS_SHORT_NAME, caption, ARRAYSIZE(caption));
  126. ::LoadString(_Module.GetResourceInstance(),
  127. IDS_NO_UNC, threat, ARRAYSIZE(threat));
  128. MessageBox(m_hDlg, threat, caption,
  129. MB_OK|MB_DEFBUTTON1|MB_ICONEXCLAMATION);
  130. retval = false;
  131. }
  132. else
  133. {
  134. TCHAR pth[_MAX_PATH] = {0},
  135. file[_MAX_FNAME] = {0}, ext[_MAX_EXT] = {0};
  136. TCHAR drive[_MAX_DRIVE] = {0};
  137. // rip it apart.
  138. _tsplitpath(path, drive, pth, file, ext);
  139. // missing a letter?
  140. if((!_istalpha(drive[0])) ||
  141. (drive[1] != _T(':')))
  142. {
  143. TCHAR caption[50] = {0}, threat[100] = {0};
  144. ::LoadString(_Module.GetResourceInstance(),
  145. IDS_SHORT_NAME, caption, ARRAYSIZE(caption));
  146. ::LoadString(_Module.GetResourceInstance(),
  147. IDS_NO_DRIVE_LTR, threat, ARRAYSIZE(threat));
  148. MessageBox(m_hDlg, (LPCTSTR)threat, (LPCTSTR)caption,
  149. MB_OK|MB_DEFBUTTON1|MB_ICONEXCLAMATION);
  150. retval = false;
  151. }
  152. // has a filename?
  153. else if((_tcslen(file) != 0) || (_tcslen(ext) != 0))
  154. {
  155. TCHAR caption[50] = {0}, threat[256] = {0};
  156. ::LoadString(_Module.GetResourceInstance(),
  157. IDS_SHORT_NAME, caption, ARRAYSIZE(caption));
  158. ::LoadString(_Module.GetResourceInstance(),
  159. IDS_DIRS_ONLY, threat, ARRAYSIZE(threat));
  160. MessageBox(m_hDlg, (LPCTSTR)threat, (LPCTSTR)caption,
  161. MB_OK|MB_DEFBUTTON1|MB_ICONEXCLAMATION);
  162. retval = false;
  163. }
  164. // what kind of drive?
  165. CWbemClassObject inst;
  166. TCHAR drvRoot[40] = {0};
  167. _tcscpy(drvRoot, _T("Win32_LogicalDisk=\""));
  168. _tcsncat(drvRoot, path, 2);
  169. _tcscat(drvRoot, _T("\""));
  170. inst = m_DS->m_cimv2NS.GetObject(drvRoot);
  171. if(inst)
  172. {
  173. DWORD driveType = inst.GetLong(_T("DriveType"));
  174. if(driveType != 3)
  175. {
  176. // cant use removeables.
  177. TCHAR caption[50] = {0}, threat[100] = {0};
  178. ::LoadString(_Module.GetResourceInstance(),
  179. IDS_SHORT_NAME, caption, ARRAYSIZE(caption));
  180. ::LoadString(_Module.GetResourceInstance(),
  181. IDS_LOGS_WRONG_DRIVETYPE, threat, ARRAYSIZE(threat));
  182. MessageBox(m_hDlg, (LPCTSTR)threat, (LPCTSTR)caption,
  183. MB_OK|MB_DEFBUTTON1|MB_ICONEXCLAMATION);
  184. retval = false;
  185. }
  186. }
  187. else
  188. {
  189. }
  190. }
  191. return retval;
  192. }
  193. //------------------------------------------------------------------------
  194. BOOL CLogPage::OnValidate(HWND hDlg)
  195. {
  196. BOOL keepFocus = FALSE;
  197. HWND hwnd = GetDlgItem(hDlg, IDC_LOGGINGDIRECTORY);
  198. if(Edit_GetModify(hwnd))
  199. {
  200. TCHAR buf[_MAX_PATH] = {0};
  201. ::GetWindowText(hwnd, buf, _MAX_PATH);
  202. if(_tcslen(buf) == 0)
  203. {
  204. CHString1 caption, threat;
  205. caption.LoadString(IDS_SHORT_NAME);
  206. threat.LoadString(IDS_DIR_EMPTY);
  207. MessageBox(hDlg, (LPCTSTR)threat, (LPCTSTR)caption,
  208. MB_OK|MB_DEFBUTTON1|MB_ICONEXCLAMATION);
  209. CHString1 temp;
  210. HRESULT hr = m_DS->GetLoggingLocation(temp);
  211. if(SUCCEEDED(hr))
  212. {
  213. SetWindowText(GetDlgItem(hDlg, IDC_LOGGINGDIRECTORY),
  214. temp);
  215. }
  216. // send him back to fix it.
  217. Edit_SetModify(hwnd, FALSE);
  218. ::SetFocus(hwnd);
  219. keepFocus = TRUE;
  220. }
  221. else if(!GoodPathSyntax(buf))
  222. {
  223. ::SetFocus(hwnd);
  224. keepFocus = TRUE;
  225. }
  226. else if(m_DS->IsValidDir(CHString1(buf)))
  227. {
  228. keepFocus = FALSE;
  229. }
  230. else
  231. {
  232. CHString1 strLogDir;
  233. m_DS->GetLoggingLocation(strLogDir);
  234. if(CHString1(buf) != strLogDir)
  235. {
  236. // msg box here.
  237. CHString1 caption, threat;
  238. caption.LoadString(IDS_SHORT_NAME);
  239. threat.LoadString(IDS_DIR_DOESNT_EXIST);
  240. if(MessageBox(hDlg, (LPCTSTR)threat, (LPCTSTR)caption,
  241. MB_YESNO|MB_DEFBUTTON2|MB_ICONEXCLAMATION) == IDYES)
  242. {
  243. // let it go through then.
  244. Edit_SetModify(hwnd, TRUE);
  245. keepFocus = FALSE;
  246. }
  247. else
  248. {
  249. // send him back to fix it.
  250. Edit_SetModify(hwnd, FALSE);
  251. ::SetFocus(hwnd);
  252. keepFocus = TRUE;
  253. }
  254. }
  255. else
  256. {
  257. keepFocus = FALSE;
  258. }
  259. }
  260. }
  261. // check the logsize.
  262. hwnd = GetDlgItem(hDlg, IDC_MAXFILESIZE);
  263. if((keepFocus == FALSE) && Edit_GetModify(hwnd))
  264. {
  265. TCHAR buf[16] = {0};
  266. ULONG temp = 0;
  267. ::GetWindowText(hwnd, buf, 16);
  268. errno = 0;
  269. temp = _tcstoul(buf, NULL, 10);
  270. if(errno == ERANGE)
  271. {
  272. TCHAR caption[50] = {0}, threat[256] = {0};
  273. ::LoadString(_Module.GetResourceInstance(),
  274. IDS_SHORT_NAME, caption, ARRAYSIZE(caption));
  275. ::LoadString(_Module.GetResourceInstance(),
  276. IDS_BAD_LOGSIZE, threat, ARRAYSIZE(threat));
  277. MessageBox(m_hDlg, threat, caption,
  278. MB_OK|MB_DEFBUTTON1|MB_ICONEXCLAMATION);
  279. ::SetFocus(hwnd);
  280. keepFocus = TRUE;
  281. }
  282. }
  283. return keepFocus;
  284. }
  285. //------------------------------------------------------------------------
  286. void CLogPage::OnApply(HWND hDlg, bool bClose)
  287. {
  288. DataSource::LOGSTATUS status = DataSource::Disabled;
  289. bool needToPut = false;
  290. if(IsDlgButtonChecked(hDlg, IDC_DISABLELOGGING) == BST_CHECKED)
  291. status = DataSource::Disabled;
  292. else if(IsDlgButtonChecked(hDlg, IDC_ERRORLOGGING) == BST_CHECKED)
  293. status = DataSource::ErrorsOnly;
  294. else if(IsDlgButtonChecked(hDlg, IDC_VERBOSELOGGING) == BST_CHECKED)
  295. status = DataSource::Verbose;
  296. if(m_oldStatus != status)
  297. {
  298. m_DS->SetLoggingStatus(status);
  299. m_oldStatus = status;
  300. needToPut = true;
  301. }
  302. HWND hwnd = GetDlgItem(hDlg, IDC_MAXFILESIZE);
  303. TCHAR buf[_MAX_PATH] = {0};
  304. ULONG temp = 0;
  305. if(Edit_GetModify(hwnd))
  306. {
  307. ::GetWindowText(hwnd, buf, ARRAYSIZE(buf));
  308. temp = _tcstoul(buf, NULL, 10);
  309. if(SUCCEEDED(m_DS->SetLoggingSize(temp)))
  310. {
  311. needToPut = true;
  312. }
  313. }
  314. hwnd = GetDlgItem(hDlg, IDC_LOGGINGDIRECTORY);
  315. if(Edit_GetModify(hwnd))
  316. {
  317. ::GetWindowText(hwnd, buf, ARRAYSIZE(buf));
  318. if(SUCCEEDED(m_DS->SetLoggingLocation(buf)))
  319. {
  320. needToPut = true;
  321. }
  322. }
  323. if(needToPut)
  324. {
  325. NeedToPut(PB_LOGGING, !bClose);
  326. if(!bClose)
  327. Refresh(hDlg);
  328. }
  329. }
  330. //------------------------------------------------------------------------
  331. BOOL CLogPage::DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  332. {
  333. switch(uMsg)
  334. {
  335. case WM_INITDIALOG:
  336. InitDlg(hDlg);
  337. break;
  338. case WM_NOTIFY:
  339. {
  340. switch (((LPNMHDR)lParam)->code)
  341. {
  342. case PSN_SETACTIVE:
  343. Refresh(hDlg);
  344. break;
  345. case PSN_HELP:
  346. HTMLHelper(hDlg);
  347. break;
  348. case PSN_APPLY:
  349. OnApply(hDlg, (((LPPSHNOTIFY)lParam)->lParam == 1));
  350. break;
  351. case PSN_KILLACTIVE:
  352. {
  353. BOOL retval = OnValidate(hDlg);
  354. ::SetWindowLongPtr(hDlg, DWLP_MSGRESULT, retval);
  355. return retval;
  356. }
  357. break;
  358. }
  359. }
  360. break;
  361. case WM_COMMAND:
  362. switch(LOWORD(wParam))
  363. {
  364. case IDC_DISABLELOGGING:
  365. case IDC_ERRORLOGGING:
  366. case IDC_VERBOSELOGGING:
  367. if(HIWORD(wParam) == BN_CLICKED)
  368. {
  369. PageChanged(PB_LOGGING, true);
  370. return TRUE;
  371. }
  372. break;
  373. case IDC_MAXFILESIZE:
  374. if(HIWORD(wParam) == EN_UPDATE)
  375. {
  376. bool valid = true;
  377. HWND hwnd = GetDlgItem(hDlg, IDC_MAXFILESIZE);
  378. TCHAR buf[_MAX_PATH] = {0};
  379. ::GetWindowText(hwnd, buf, _MAX_PATH);
  380. for(UINT x = 0; valid && x < _tcslen(buf); x++)
  381. {
  382. switch(buf[x])
  383. {
  384. case '0':
  385. case '1':
  386. case '2':
  387. case '3':
  388. case '4':
  389. case '5':
  390. case '6':
  391. case '7':
  392. case '8':
  393. case '9':
  394. valid = true;
  395. break;
  396. default:
  397. valid = false;
  398. break;
  399. } //endswitch
  400. } //endfor
  401. if(!valid)
  402. {
  403. buf[_tcslen(buf) - 1] = _T('\0');
  404. SetWindowText(hwnd, buf);
  405. SendMessage(hwnd, EM_SETSEL, 0, -1);
  406. MessageBeep(MB_ICONASTERISK);
  407. } // endif
  408. }
  409. else if((HIWORD(wParam) == EN_CHANGE) &&
  410. Edit_GetModify((HWND)lParam))
  411. {
  412. PageChanged(PB_LOGGING, true);
  413. return TRUE;
  414. }
  415. break;
  416. case IDC_LOGGINGDIRECTORY:
  417. if((HIWORD(wParam) == EN_CHANGE) && Edit_GetModify((HWND)lParam))
  418. {
  419. PageChanged(PB_LOGGING, true);
  420. return TRUE;
  421. }
  422. break;
  423. case IDC_BROWSE:
  424. if(HIWORD(wParam) == BN_CLICKED)
  425. {
  426. LPMALLOC pMalloc; /* Gets the Shell's default allocator */
  427. if(::SHGetMalloc(&pMalloc) == NOERROR)
  428. {
  429. BROWSEINFO bi;
  430. TCHAR pszBuffer[MAX_PATH] = {0};
  431. LPITEMIDLIST pidl;
  432. ITEMIDLIST *root;
  433. // Get the PIDL for the Programs folder.
  434. if(SUCCEEDED(SHGetSpecialFolderLocation(hDlg, CSIDL_DRIVES,
  435. &root)))
  436. {
  437. bi.pidlRoot = root;
  438. }
  439. else
  440. {
  441. bi.pidlRoot = NULL;
  442. }
  443. // Get help on BROWSEINFO struct - it's got all the bit settings.
  444. TCHAR title[100] = {0};
  445. ::LoadString(_Module.GetModuleInstance(), IDS_LOG_SELECT_FDR,
  446. title, 100);
  447. bi.hwndOwner = hDlg;
  448. bi.pszDisplayName = pszBuffer;
  449. bi.lpszTitle = title;
  450. bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
  451. bi.lpfn = NULL;
  452. bi.lParam = 0;
  453. // This next call issues the dialog box.
  454. if((pidl = ::SHBrowseForFolder(&bi)) != NULL)
  455. {
  456. if(::SHGetPathFromIDList(pidl, pszBuffer))
  457. {
  458. // At this point pszBuffer contains the selected path */.
  459. HWND hwnd = GetDlgItem(hDlg, IDC_LOGGINGDIRECTORY);
  460. PathAddBackslash(pszBuffer);
  461. SetWindowText(hwnd, pszBuffer);
  462. Edit_SetModify(hwnd, TRUE);
  463. PageChanged(PB_LOGGING, true);
  464. }
  465. // Free the PIDL allocated by SHBrowseForFolder.
  466. pMalloc->Free(pidl);
  467. }
  468. // Release the shell's allocator.
  469. pMalloc->Release();
  470. }
  471. }
  472. break;
  473. default: break;
  474. } //endswitch(LOWORD(wParam))
  475. break;
  476. case WM_HELP:
  477. if (IsWindowEnabled(hDlg))
  478. {
  479. WinHelp((HWND)((LPHELPINFO)lParam)->hItemHandle,
  480. c_HelpFile,
  481. HELP_WM_HELP,
  482. (ULONG_PTR)logPageHelpIDs);
  483. }
  484. break;
  485. case WM_CONTEXTMENU:
  486. if (IsWindowEnabled(hDlg))
  487. {
  488. WinHelp(hDlg, c_HelpFile,
  489. HELP_CONTEXTMENU,
  490. (ULONG_PTR)logPageHelpIDs);
  491. }
  492. break;
  493. default:
  494. return FALSE;
  495. }
  496. return TRUE;
  497. }