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.

473 lines
14 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1999
  6. //
  7. // File: chooser.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. /////////////////////////////////////////////////////////////////////
  11. // Chooser.cpp
  12. //
  13. // Dialog to choose a machine name.
  14. //
  15. // PURPOSE
  16. // What you have to do is to copy all the files from the
  17. // snapin\chooser\ directory into your project (you may add
  18. // \nt\private\admin\snapin\chooser\ to your include directory if
  19. // you prefer not copying the code).
  20. // If you decide to copy the code to your project, please send mail
  21. // to Dan Morin (T-DanM) and cc to Jon Newman (JonN) so we can
  22. // mail you when we have updates available. The next update will
  23. // be the "Browse" button to select a machine name.
  24. //
  25. //
  26. // DYNALOADED LIBRARIES
  27. //
  28. // HISTORY
  29. // 13-May-1997 t-danm Creation.
  30. // 23-May-1997 t-danm Checkin into public tree. Comments updates.
  31. // 25-May-1997 t-danm Added MMCPropPageCallback().
  32. // 31-Oct-1997 mattt Added dynaload, fixed user <CANCEL> logic
  33. // 1-Oct-1998 mattt Removed reliance on MFC, changed default look to enable certsvr picker
  34. //
  35. /////////////////////////////////////////////////////////////////////
  36. #include <stdafx.h>
  37. #include "chooser.h"
  38. #include "csdisp.h" // certsrv picker
  39. #define __dwFILE__ __dwFILE_CERTMMC_CHOOSER_CPP__
  40. #ifdef _DEBUG
  41. #undef THIS_FILE
  42. #define THIS_FILE __FILE__
  43. #endif
  44. #ifndef INOUT
  45. // The following defines are found in \nt\private\admin\snapin\filemgmt\stdafx.h
  46. #define INOUT
  47. #define Endorse(f) // Dummy macro
  48. #define LENGTH(x) (sizeof(x)/sizeof(x[0]))
  49. #define Assert(f) ASSERT(f)
  50. #endif
  51. /////////////////////////////////////////////////////////////////////
  52. /////////////////////////////////////////////////////////////////////
  53. // Replacement for BEGIN_MESSAGE_MAP
  54. BOOL
  55. CAutoDeletePropPage::OnCommand(
  56. WPARAM, // wParam
  57. LPARAM /* lParam */ )
  58. {
  59. /*
  60. switch(LOWORD(wParam))
  61. {
  62. }
  63. */
  64. return FALSE;
  65. }
  66. /////////////////////////////////////////////////////////////////////
  67. // Constructor
  68. CAutoDeletePropPage::CAutoDeletePropPage(UINT uIDD) : PropertyPage(uIDD)
  69. {
  70. m_prgzHelpIDs = NULL;
  71. m_autodeleteStuff.cWizPages = 1; // Number of pages in wizard
  72. m_autodeleteStuff.pfnOriginalPropSheetPageProc = m_psp.pfnCallback;
  73. m_psp.dwFlags |= PSP_USECALLBACK;
  74. m_psp.pfnCallback = S_PropSheetPageProc;
  75. m_psp.lParam = reinterpret_cast<LPARAM>(this);
  76. }
  77. CAutoDeletePropPage::~CAutoDeletePropPage()
  78. {
  79. }
  80. /////////////////////////////////////////////////////////////////////
  81. void CAutoDeletePropPage::SetCaption(LPCTSTR pszCaption)
  82. {
  83. m_strCaption = pszCaption; // Copy the caption
  84. m_psp.pszTitle = m_strCaption; // Set the title
  85. m_psp.dwFlags |= PSP_USETITLE;
  86. }
  87. /////////////////////////////////////////////////////////////////////
  88. void CAutoDeletePropPage::SetCaption(UINT uStringID)
  89. {
  90. VERIFY(m_strCaption.LoadString(uStringID));
  91. SetCaption(m_strCaption);
  92. }
  93. /////////////////////////////////////////////////////////////////////
  94. void CAutoDeletePropPage::SetHelp(LPCTSTR szHelpFile, const DWORD rgzHelpIDs[])
  95. {
  96. m_strHelpFile = szHelpFile;
  97. m_prgzHelpIDs = rgzHelpIDs;
  98. }
  99. /////////////////////////////////////////////////////////////////////
  100. void CAutoDeletePropPage::EnableDlgItem(INT nIdDlgItem, BOOL fEnable)
  101. {
  102. Assert(IsWindow(::GetDlgItem(m_hWnd, nIdDlgItem)));
  103. ::EnableWindow(::GetDlgItem(m_hWnd, nIdDlgItem), fEnable);
  104. }
  105. /////////////////////////////////////////////////////////////////////
  106. BOOL CAutoDeletePropPage::OnSetActive()
  107. {
  108. HWND hwndParent = ::GetParent(m_hWnd);
  109. Assert(IsWindow(hwndParent));
  110. ::PropSheet_SetWizButtons(hwndParent, PSWIZB_FINISH);
  111. return PropertyPage::OnSetActive();
  112. }
  113. /////////////////////////////////////////////////////////////////////
  114. void CAutoDeletePropPage::OnHelp(LPHELPINFO pHelpInfo)
  115. {
  116. if (m_prgzHelpIDs == NULL || m_strHelpFile.IsEmpty())
  117. return;
  118. if (pHelpInfo != NULL &&
  119. pHelpInfo->iContextType == HELPINFO_WINDOW &&
  120. HasContextHelp(pHelpInfo->iCtrlId))
  121. {
  122. // Display context help for a control
  123. ::WinHelp((HWND)pHelpInfo->hItemHandle, m_strHelpFile,
  124. HELP_WM_HELP, (ULONG_PTR)(LPVOID)m_prgzHelpIDs);
  125. }
  126. return;
  127. }
  128. /////////////////////////////////////////////////////////////////////
  129. void CAutoDeletePropPage::OnContextHelp(HWND hwnd)
  130. {
  131. if (m_prgzHelpIDs == NULL || m_strHelpFile.IsEmpty())
  132. return;
  133. Assert(IsWindow(hwnd));
  134. if(HasContextHelp(GetDlgCtrlID(hwnd)))
  135. {
  136. ::WinHelp(
  137. hwnd,
  138. m_strHelpFile,
  139. HELP_CONTEXTMENU,
  140. (ULONG_PTR)(LPVOID)m_prgzHelpIDs);
  141. }
  142. return;
  143. }
  144. /////////////////////////////////////////////////////////////////////
  145. bool CAutoDeletePropPage::HasContextHelp(int nDlgItem)
  146. {
  147. const DWORD * pdwHelpIDs;
  148. for(pdwHelpIDs = m_prgzHelpIDs;
  149. *pdwHelpIDs;
  150. pdwHelpIDs += 2)
  151. {
  152. if(nDlgItem == (int) *pdwHelpIDs)
  153. return true;
  154. }
  155. return false;
  156. }
  157. /////////////////////////////////////////////////////////////////////
  158. // S_PropSheetPageProc()
  159. //
  160. // Static member function used to delete the CAutoDeletePropPage object
  161. // when wizard terminates
  162. //
  163. UINT CALLBACK
  164. CAutoDeletePropPage::S_PropSheetPageProc(
  165. HWND hwnd,
  166. UINT uMsg,
  167. LPPROPSHEETPAGE ppsp)
  168. {
  169. Assert(ppsp != NULL);
  170. CAutoDeletePropPage * pThis;
  171. pThis = reinterpret_cast<CAutoDeletePropPage*>(ppsp->lParam);
  172. Assert(pThis != NULL);
  173. BOOL fDefaultRet;
  174. fDefaultRet = FALSE;
  175. switch (uMsg)
  176. {
  177. case PSPCB_RELEASE:
  178. if (--(pThis->m_autodeleteStuff.cWizPages) <= 0)
  179. {
  180. // Remember callback on stack since "this" will be deleted
  181. LPFNPSPCALLBACK pfnOrig = pThis->m_autodeleteStuff.pfnOriginalPropSheetPageProc;
  182. delete pThis;
  183. if (pfnOrig)
  184. return (pfnOrig)(hwnd, uMsg, ppsp);
  185. else
  186. return fDefaultRet;
  187. }
  188. break;
  189. case PSPCB_CREATE:
  190. fDefaultRet = TRUE;
  191. // do not increase refcount, PSPCB_CREATE may or may not be called
  192. // depending on whether the page was created. PSPCB_RELEASE can be
  193. // depended upon to be called exactly once per page however.
  194. break;
  195. } // switch
  196. if (pThis->m_autodeleteStuff.pfnOriginalPropSheetPageProc)
  197. return (pThis->m_autodeleteStuff.pfnOriginalPropSheetPageProc)(hwnd, uMsg, ppsp);
  198. else
  199. return fDefaultRet;
  200. } // CAutoDeletePropPage::S_PropSheetPageProc()
  201. /////////////////////////////////////////////////////////////////////
  202. /////////////////////////////////////////////////////////////////////
  203. // Replacement for BEGIN_MESSAGE_MAP
  204. BOOL
  205. CChooseMachinePropPage::OnCommand(
  206. WPARAM wParam,
  207. LPARAM /* lParam */ )
  208. {
  209. switch(LOWORD(wParam))
  210. {
  211. case IDC_CHOOSER_RADIO_LOCAL_MACHINE:
  212. OnRadioLocalMachine();
  213. break;
  214. case IDC_CHOOSER_RADIO_SPECIFIC_MACHINE:
  215. OnRadioSpecificMachine();
  216. break;
  217. case IDC_CHOOSER_BUTTON_BROWSE_MACHINENAMES:
  218. OnBrowse();
  219. break;
  220. default:
  221. return FALSE;
  222. break;
  223. }
  224. return TRUE;
  225. }
  226. #ifdef _DEBUG
  227. static void AssertValidDialogTemplate(HWND hwnd)
  228. {
  229. ASSERT(::IsWindow(hwnd));
  230. // Mandatory controls for a valid dialog template
  231. static const UINT rgzidDialogControl[] =
  232. {
  233. IDC_CHOOSER_RADIO_LOCAL_MACHINE,
  234. IDC_CHOOSER_RADIO_SPECIFIC_MACHINE,
  235. IDC_CHOOSER_EDIT_MACHINE_NAME,
  236. 0
  237. };
  238. for (int i = 0; rgzidDialogControl[i] != 0; i++)
  239. {
  240. ASSERT(NULL != GetDlgItem(hwnd, rgzidDialogControl[i]) &&
  241. "Control ID not found in dialog template.");
  242. }
  243. } // AssertValidDialogTemplate()
  244. #else
  245. #define AssertValidDialogTemplate(hwnd)
  246. #endif // ~_DEBUG
  247. /////////////////////////////////////////////////////////////////////
  248. // Constructor
  249. CChooseMachinePropPage::CChooseMachinePropPage(UINT uIDD) : CAutoDeletePropPage(uIDD)
  250. {
  251. m_fIsRadioLocalMachine = TRUE;
  252. m_fEnableMachineBrowse = FALSE;
  253. m_pstrMachineNameOut = NULL;
  254. m_pstrMachineNameEffectiveOut = NULL;
  255. m_pdwFlags = NULL;
  256. }
  257. /////////////////////////////////////////////////////////////////////
  258. CChooseMachinePropPage::~CChooseMachinePropPage()
  259. {
  260. }
  261. /////////////////////////////////////////////////////////////////////
  262. // Load the initial state of CChooseMachinePropPage
  263. void CChooseMachinePropPage::InitMachineName(LPCTSTR pszMachineName)
  264. {
  265. m_strMachineName = pszMachineName;
  266. m_fIsRadioLocalMachine = m_strMachineName.IsEmpty();
  267. }
  268. /////////////////////////////////////////////////////////////////////
  269. // SetOutputBuffers()
  270. //
  271. // - Set the pointer to the CString object to store the machine name.
  272. // - Set the pointer to the boolean flag for command line override.
  273. // - Set the pointer pointer to store the overriden machine name.
  274. //
  275. void CChooseMachinePropPage::SetOutputBuffers(
  276. OUT CString * pstrMachineNamePersist, // Machine name the user typed. Empty string == local machine.
  277. OUT CString * pstrMachineNameEffective,
  278. OUT DWORD* pdwFlags)
  279. {
  280. Assert(pstrMachineNamePersist != NULL && "Invalid output buffer");
  281. // point members at params
  282. m_pstrMachineNameOut = pstrMachineNamePersist;
  283. m_pstrMachineNameEffectiveOut = pstrMachineNameEffective;
  284. m_pdwFlags = pdwFlags;
  285. *m_pdwFlags = 0;
  286. }
  287. /////////////////////////////////////////////////////////////////////
  288. // Replacement for DoDataExchange
  289. BOOL CChooseMachinePropPage::UpdateData(BOOL fSuckFromDlg /*= TRUE*/)
  290. {
  291. if (fSuckFromDlg)
  292. {
  293. m_strMachineName.FromWindow(GetDlgItem(m_hWnd, IDC_CHOOSER_EDIT_MACHINE_NAME));
  294. int iCheck = (int)SendMessage(GetDlgItem(m_hWnd, IDC_CHOOSER_MACHINE_OVERRIDE), BM_GETCHECK, 0, 0);
  295. if (iCheck == BST_CHECKED)
  296. *m_pdwFlags |= CCOMPDATAIMPL_FLAGS_ALLOW_MACHINE_OVERRIDE;
  297. else
  298. *m_pdwFlags &= ~CCOMPDATAIMPL_FLAGS_ALLOW_MACHINE_OVERRIDE;
  299. }
  300. else
  301. {
  302. m_strMachineName.ToWindow(GetDlgItem(m_hWnd, IDC_CHOOSER_EDIT_MACHINE_NAME));
  303. int iCheck;
  304. iCheck = (*m_pdwFlags & CCOMPDATAIMPL_FLAGS_ALLOW_MACHINE_OVERRIDE) ? BST_CHECKED : BST_UNCHECKED;
  305. SendMessage(GetDlgItem(m_hWnd, IDC_CHOOSER_MACHINE_OVERRIDE), BM_SETCHECK, iCheck, 0);
  306. }
  307. return TRUE;
  308. }
  309. /////////////////////////////////////////////////////////////////////
  310. BOOL CChooseMachinePropPage::OnInitDialog()
  311. {
  312. AssertValidDialogTemplate(m_hWnd);
  313. CAutoDeletePropPage::OnInitDialog();
  314. InitChooserControls();
  315. PropSheet_SetWizButtons(GetParent(), PSWIZB_FINISH);
  316. return TRUE;
  317. }
  318. /////////////////////////////////////////////////////////////////////
  319. BOOL CChooseMachinePropPage::OnWizardFinish()
  320. {
  321. if (!UpdateData()) // Do the data exchange to collect data
  322. return FALSE; // don't destroy on error
  323. if (m_fIsRadioLocalMachine)
  324. m_strMachineName.Empty();
  325. else
  326. if (m_strMachineName.IsEmpty())
  327. {
  328. DisplayCertSrvErrorWithContext(m_hWnd, S_OK, IDS_MUST_CHOOSE_MACHINE);
  329. return FALSE;
  330. }
  331. if (m_pstrMachineNameOut != NULL)
  332. {
  333. // Store the machine name into its output buffer
  334. *m_pstrMachineNameOut = m_strMachineName;
  335. if (m_pstrMachineNameEffectiveOut != NULL)
  336. {
  337. *m_pstrMachineNameEffectiveOut = m_strMachineName;
  338. } // if
  339. }
  340. else
  341. Assert(FALSE && "FYI: You have not specified any output buffer to store the machine name.");
  342. return CAutoDeletePropPage::OnWizardFinish();
  343. }
  344. void CChooseMachinePropPage::InitChooserControls()
  345. {
  346. SendDlgItemMessage(IDC_CHOOSER_RADIO_LOCAL_MACHINE, BM_SETCHECK, m_fIsRadioLocalMachine);
  347. SendDlgItemMessage(IDC_CHOOSER_RADIO_SPECIFIC_MACHINE, BM_SETCHECK, !m_fIsRadioLocalMachine);
  348. EnableDlgItem(IDC_CHOOSER_EDIT_MACHINE_NAME, !m_fIsRadioLocalMachine);
  349. PCCRYPTUI_CA_CONTEXT pCAContext = NULL;
  350. DWORD dwCACount;
  351. HRESULT hr = myGetConfigFromPicker(
  352. m_hWnd,
  353. NULL, //sub title
  354. NULL, //title
  355. NULL,
  356. GCFPF_USEDS,
  357. TRUE, // count only
  358. &dwCACount,
  359. &pCAContext);
  360. if (S_OK != hr && HRESULT_FROM_WIN32(ERROR_CANCELLED) != hr)
  361. {
  362. _PrintError(hr, "myGetConfigFromPicker");
  363. goto done;
  364. }
  365. m_fEnableMachineBrowse = (0 == dwCACount) ? FALSE : TRUE;
  366. if (NULL != pCAContext)
  367. {
  368. CryptUIDlgFreeCAContext(pCAContext);
  369. }
  370. done:
  371. EnableDlgItem(IDC_CHOOSER_BUTTON_BROWSE_MACHINENAMES,
  372. !m_fIsRadioLocalMachine && m_fEnableMachineBrowse);
  373. }
  374. void CChooseMachinePropPage::OnRadioLocalMachine()
  375. {
  376. m_fIsRadioLocalMachine = TRUE;
  377. EnableDlgItem(IDC_CHOOSER_EDIT_MACHINE_NAME, FALSE);
  378. EnableDlgItem(IDC_CHOOSER_BUTTON_BROWSE_MACHINENAMES, FALSE);
  379. }
  380. void CChooseMachinePropPage::OnRadioSpecificMachine()
  381. {
  382. m_fIsRadioLocalMachine = FALSE;
  383. EnableDlgItem(IDC_CHOOSER_EDIT_MACHINE_NAME, TRUE);
  384. EnableDlgItem(IDC_CHOOSER_BUTTON_BROWSE_MACHINENAMES, m_fEnableMachineBrowse);
  385. }
  386. void CChooseMachinePropPage::OnBrowse()
  387. {
  388. HRESULT hr;
  389. WCHAR *szConfig = NULL;
  390. CWaitCursor cwait;
  391. // UNDONE: expand config picker to non-published (DS chooser dlg)
  392. hr = myGetConfigStringFromPicker(m_hWnd,
  393. NULL, //use default prompt
  394. NULL, //use default title
  395. NULL, //use default shared folder
  396. GCFPF_USEDS,
  397. &szConfig);
  398. if (hr == S_OK)
  399. {
  400. LPWSTR szWhack = wcschr(szConfig, L'\\');
  401. if (szWhack != NULL)
  402. szWhack[0] = L'\0';
  403. m_strMachineName = szConfig;
  404. LocalFree(szConfig);
  405. }
  406. // push result back to ui
  407. UpdateData(FALSE);
  408. }