Source code of Windows XP (NT5)
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.

347 lines
10 KiB

  1. #include "stdafx.h"
  2. #include "grpinfo.h"
  3. #pragma hdrstop
  4. // Names of groups retrieved by SID
  5. WCHAR g_szPowerUsers[MAX_GROUP + 1];
  6. WCHAR g_szUsers[MAX_GROUP + 1];
  7. /**************************************************************
  8. CGroupPageBase Implementation
  9. Functions common to both the group prop page and the group
  10. wizard page.
  11. **************************************************************/
  12. CGroupPageBase::CGroupPageBase(CUserInfo* pUserInfo, CDPA<CGroupInfo>* pGroupList)
  13. {
  14. m_pUserInfo = pUserInfo;
  15. m_pGroupList = pGroupList;
  16. m_hBoldFont = NULL;
  17. // Load names for local groups based on SID
  18. if (FAILED(LookupLocalGroupName(DOMAIN_ALIAS_RID_POWER_USERS, g_szPowerUsers, ARRAYSIZE(g_szPowerUsers))))
  19. {
  20. *g_szPowerUsers = L'\0';
  21. }
  22. if (FAILED(LookupLocalGroupName(DOMAIN_ALIAS_RID_USERS, g_szUsers, ARRAYSIZE(g_szUsers))))
  23. {
  24. *g_szUsers = L'\0';
  25. }
  26. }
  27. INT_PTR CGroupPageBase::HandleGroupMessage(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  28. {
  29. switch (uMsg)
  30. {
  31. HANDLE_MSG(hwndDlg, WM_INITDIALOG, OnInitDialog);
  32. HANDLE_MSG(hwndDlg, WM_COMMAND, OnCommand);
  33. };
  34. return FALSE;
  35. }
  36. void CGroupPageBase::InitializeLocalGroupCombo(HWND hwndCombo)
  37. {
  38. ComboBox_ResetContent(hwndCombo);
  39. // Add all of the groups in the list to the box
  40. for(int i = 0; i < m_pGroupList->GetPtrCount(); i ++)
  41. {
  42. CGroupInfo* pGroupInfo = m_pGroupList->GetPtr(i);
  43. int index = ComboBox_AddString(hwndCombo, pGroupInfo->m_szGroup);
  44. ComboBox_SetItemData(hwndCombo, index, pGroupInfo->m_szComment);
  45. }
  46. TCHAR szSelectGroup[MAX_GROUP + 1];
  47. // Load a local group name from the resources to select by default
  48. LoadString(g_hinst, IDS_USR_DEFAULTGROUP, szSelectGroup, ARRAYSIZE(szSelectGroup));
  49. if (ComboBox_SelectString(hwndCombo, 0, szSelectGroup) == CB_ERR)
  50. ComboBox_SetCurSel(hwndCombo, 0);
  51. }
  52. void CGroupPageBase::SetGroupDescription(HWND hwndCombo, HWND hwndEdit)
  53. {
  54. int iItem = ComboBox_GetCurSel(hwndCombo);
  55. TCHAR* pszDescription = (TCHAR*) ComboBox_GetItemData(hwndCombo, iItem);
  56. SetWindowText(hwndEdit, pszDescription);
  57. }
  58. BOOL CGroupPageBase::OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
  59. {
  60. // Fill in the local group combo box
  61. HWND hwndCombo = GetDlgItem(hwnd, IDC_GROUPS);
  62. InitializeLocalGroupCombo(hwndCombo);
  63. HWND hwndEdit = GetDlgItem(hwnd, IDC_GROUPDESC);
  64. if ((NULL != m_pUserInfo) && (m_pUserInfo->m_szGroups[0] != TEXT('\0')))
  65. {
  66. // Select the local group corresponding to the first one in the user's groups
  67. // string
  68. TCHAR szSelect[MAX_GROUP + 1];
  69. // Copy the string since we might shorten our copy
  70. lstrcpyn(szSelect, m_pUserInfo->m_szGroups, ARRAYSIZE(szSelect));
  71. TCHAR* pchEndOfFirst = StrChr(szSelect, TEXT(';'));
  72. if (pchEndOfFirst)
  73. {
  74. // More than one group; we'll fix that!
  75. *pchEndOfFirst = TEXT('\0');
  76. }
  77. SelectGroup(hwnd, szSelect);
  78. }
  79. else
  80. {
  81. // Select the power user group by default
  82. SendDlgItemMessage(hwnd, IDC_POWERUSERS, BM_SETCHECK,
  83. (WPARAM) BST_CHECKED, 0);
  84. OnRadioChanged(hwnd, IDC_POWERUSERS);
  85. }
  86. SetGroupDescription(hwndCombo, hwndEdit);
  87. // Bold the group names
  88. BoldGroupNames(hwnd);
  89. return TRUE;
  90. }
  91. BOOL CGroupPageBase::GetSelectedGroup(HWND hwnd, LPTSTR pszGroupOut, DWORD cchGroup, CUserInfo::GROUPPSEUDONYM* pgsOut)
  92. {
  93. *pgsOut = CUserInfo::USEGROUPNAME;
  94. UINT idString = 0;
  95. if (BST_CHECKED == Button_GetCheck(GetDlgItem(hwnd, IDC_POWERUSERS)))
  96. {
  97. StrCpyN(pszGroupOut, g_szPowerUsers, cchGroup);
  98. *pgsOut = CUserInfo::STANDARD;
  99. }
  100. else if (BST_CHECKED == Button_GetCheck(GetDlgItem(hwnd, IDC_USERS)))
  101. {
  102. StrCpyN(pszGroupOut, g_szUsers, cchGroup);
  103. *pgsOut = CUserInfo::RESTRICTED;
  104. }
  105. else
  106. {
  107. // 'other' must be selected; get the string from the dropdown
  108. GetWindowText(GetDlgItem(hwnd, IDC_GROUPS), pszGroupOut, cchGroup);
  109. }
  110. return TRUE;
  111. }
  112. // Returns IDC_OTHER if no radio button id corresponds to the group
  113. UINT CGroupPageBase::RadioIdForGroup(LPCTSTR pszGroup)
  114. {
  115. UINT uiRadio = IDC_OTHER; // Assume IDC_OTHER to start
  116. if (0 == StrCmpI(pszGroup, g_szPowerUsers))
  117. {
  118. uiRadio = IDC_POWERUSERS;
  119. }
  120. else if (0 == StrCmpI(pszGroup, g_szUsers))
  121. {
  122. uiRadio = IDC_USERS;
  123. }
  124. return uiRadio;
  125. }
  126. // Disable/update as appropriate when radio selection changes
  127. void CGroupPageBase::OnRadioChanged(HWND hwnd, UINT idRadio)
  128. {
  129. BOOL fEnableGroupDropdown = (IDC_OTHER == idRadio);
  130. EnableWindow(GetDlgItem(hwnd, IDC_GROUPS), fEnableGroupDropdown);
  131. EnableWindow(GetDlgItem(hwnd, IDC_OTHER_STATIC), fEnableGroupDropdown);
  132. ShowWindow(GetDlgItem(hwnd, IDC_GROUPDESC),
  133. fEnableGroupDropdown ? SW_SHOW : SW_HIDE);
  134. }
  135. void CGroupPageBase::SelectGroup(HWND hwnd, LPCTSTR pszSelect)
  136. {
  137. // Always select the group in the 'other' dropdown
  138. ComboBox_SelectString(GetDlgItem(hwnd, IDC_GROUPS),
  139. -1, pszSelect);
  140. // Check the appropriate radio button
  141. UINT idRadio = RadioIdForGroup(pszSelect);
  142. Button_SetCheck(GetDlgItem(hwnd, idRadio), BST_CHECKED);
  143. OnRadioChanged(hwnd, idRadio);
  144. }
  145. void CGroupPageBase::BoldGroupNames(HWND hwnd)
  146. {
  147. HWND hwndPowerUsers = GetDlgItem(hwnd, IDC_POWERUSERS);
  148. HFONT hfont = (HFONT) SendMessage(hwndPowerUsers, WM_GETFONT, 0, 0);
  149. if (hfont)
  150. {
  151. LOGFONT lf;
  152. if (FALSE != GetObject((HGDIOBJ) hfont, sizeof(lf), &lf))
  153. {
  154. lf.lfWeight = FW_BOLD;
  155. m_hBoldFont = CreateFontIndirect(&lf);
  156. if (NULL != m_hBoldFont)
  157. {
  158. // Set the font
  159. SendMessage(hwndPowerUsers, WM_SETFONT,
  160. (WPARAM) m_hBoldFont, 0);
  161. SendDlgItemMessage(hwnd, IDC_USERS,
  162. WM_SETFONT, (WPARAM) m_hBoldFont, 0);
  163. SendDlgItemMessage(hwnd, IDC_OTHER,
  164. WM_SETFONT, (WPARAM) m_hBoldFont, 0);
  165. }
  166. }
  167. }
  168. }
  169. BOOL CGroupPageBase::OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
  170. {
  171. switch(codeNotify)
  172. {
  173. case CBN_SELCHANGE:
  174. SetGroupDescription(hwndCtl, GetDlgItem(hwnd, IDC_GROUPDESC));
  175. PropSheet_Changed(GetParent(hwnd), hwnd);
  176. break;
  177. case BN_CLICKED:
  178. // Handle radio clicks
  179. switch (id)
  180. {
  181. case IDC_POWERUSERS:
  182. case IDC_USERS:
  183. case IDC_OTHER:
  184. PropSheet_Changed(GetParent(hwnd), hwnd);
  185. OnRadioChanged(hwnd, id);
  186. }
  187. break;
  188. }
  189. return FALSE;
  190. }
  191. /**************************************************************
  192. CGroupWizardPage Implementation
  193. **************************************************************/
  194. INT_PTR CGroupWizardPage::DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  195. {
  196. switch (uMsg)
  197. {
  198. HANDLE_MSG(hwndDlg, WM_INITDIALOG, OnInitDialog);
  199. HANDLE_MSG(hwndDlg, WM_NOTIFY, OnNotify);
  200. HANDLE_MSG(hwndDlg, WM_COMMAND, OnCommand);
  201. }
  202. return FALSE;
  203. }
  204. BOOL CGroupWizardPage::OnNotify(HWND hwnd, int idCtrl, LPNMHDR pnmh)
  205. {
  206. switch (pnmh->code)
  207. {
  208. case PSN_SETACTIVE:
  209. {
  210. PropSheet_SetWizButtons(pnmh->hwndFrom, PSWIZB_BACK | PSWIZB_FINISH);
  211. SetWindowLongPtr(hwnd, DWLP_MSGRESULT, 0);
  212. return TRUE;
  213. }
  214. case PSN_WIZFINISH:
  215. {
  216. // Read in the local group name
  217. CUserInfo::GROUPPSEUDONYM gs;
  218. GetSelectedGroup(hwnd, m_pUserInfo->m_szGroups,
  219. ARRAYSIZE(m_pUserInfo->m_szGroups), &gs);
  220. // Don't close wizard by default
  221. LONG_PTR finishResult = (LONG_PTR) hwnd;
  222. CWaitCursor cur;
  223. if (SUCCEEDED(m_pUserInfo->Create(hwnd, gs)))
  224. {
  225. m_pUserInfo->m_fHaveExtraUserInfo = FALSE;
  226. // Close wizard
  227. finishResult = 0;
  228. }
  229. SetWindowLongPtr(hwnd, DWLP_MSGRESULT, finishResult);
  230. return TRUE;
  231. }
  232. }
  233. return FALSE;
  234. }
  235. /**************************************************************
  236. CGroupPropertyPage Implementation
  237. **************************************************************/
  238. INT_PTR CGroupPropertyPage::DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  239. {
  240. switch (uMsg)
  241. {
  242. HANDLE_MSG(hwndDlg, WM_INITDIALOG, OnInitDialog);
  243. HANDLE_MSG(hwndDlg, WM_NOTIFY, OnNotify);
  244. HANDLE_MSG(hwndDlg, WM_COMMAND, OnCommand);
  245. }
  246. return FALSE;
  247. }
  248. BOOL CGroupPropertyPage::OnNotify(HWND hwnd, int idCtrl, LPNMHDR pnmh)
  249. {
  250. switch(pnmh->code)
  251. {
  252. case PSN_APPLY:
  253. {
  254. // Check to see if the group needs updating on Apply
  255. TCHAR szTemp[MAX_GROUP + 1];
  256. // Read in the local group name
  257. CUserInfo::GROUPPSEUDONYM gs;
  258. GetSelectedGroup(hwnd, szTemp,
  259. ARRAYSIZE(szTemp), &gs);
  260. if (StrCmp(szTemp, m_pUserInfo->m_szGroups) != 0)
  261. {
  262. HRESULT hr = m_pUserInfo->UpdateGroup(hwnd, szTemp, gs);
  263. if (SUCCEEDED(hr))
  264. {
  265. SetWindowLongPtr(hwnd, DWLP_MSGRESULT, PSNRET_NOERROR);
  266. }
  267. else
  268. {
  269. TCHAR szDomainUser[MAX_DOMAIN + MAX_USER + 2];
  270. MakeDomainUserString(m_pUserInfo->m_szDomain, m_pUserInfo->m_szUsername,
  271. szDomainUser, ARRAYSIZE(szDomainUser));
  272. ::DisplayFormatMessage(hwnd, IDS_USR_APPLET_CAPTION,
  273. IDS_USR_UPDATE_GROUP_ERROR, MB_ICONERROR | MB_OK,
  274. szDomainUser);
  275. SetWindowLongPtr(hwnd, DWLP_MSGRESULT, PSNRET_INVALID_NOCHANGEPAGE);
  276. }
  277. }
  278. }
  279. break;
  280. default:
  281. return FALSE;
  282. }
  283. return TRUE;
  284. }