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.

462 lines
11 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows/NT **/
  3. /** Copyright(c) Microsoft Corporation, 1999 - 1999 **/
  4. /**********************************************************************/
  5. /*
  6. addserv.cpp
  7. The add server dialog
  8. FILE HISTORY:
  9. */
  10. #include "stdafx.h"
  11. #include "AddServ.h"
  12. #include <objpick.h> // for CGetComputer
  13. #ifdef _DEBUG
  14. #define new DEBUG_NEW
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. int CALLBACK AddServerCompareFunc
  19. (
  20. LPARAM lParam1,
  21. LPARAM lParam2,
  22. LPARAM lParamSort
  23. )
  24. {
  25. return ((CAddServer *) lParamSort)->HandleSort(lParam1, lParam2);
  26. }
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CAddServer dialog
  29. CAddServer::CAddServer(CWnd* pParent /*=NULL*/)
  30. : CBaseDialog(CAddServer::IDD, pParent)
  31. {
  32. //{{AFX_DATA_INIT(CAddServer)
  33. //}}AFX_DATA_INIT
  34. ResetSort();
  35. }
  36. void CAddServer::DoDataExchange(CDataExchange* pDX)
  37. {
  38. CBaseDialog::DoDataExchange(pDX);
  39. //{{AFX_DATA_MAP(CAddServer)
  40. DDX_Control(pDX, IDC_RADIO_AUTHORIZED_SERVERS, m_radioAuthorizedServer);
  41. DDX_Control(pDX, IDOK, m_buttonOk);
  42. DDX_Control(pDX, IDC_RADIO_ANY_SERVER, m_radioAnyServer);
  43. DDX_Control(pDX, IDC_EDIT_ADD_SERVER_NAME, m_editServer);
  44. DDX_Control(pDX, IDC_BUTTON_BROWSE_SERVERS, m_buttonBrowse);
  45. DDX_Control(pDX, IDC_LIST_AUTHORIZED_SERVERS, m_listctrlServers);
  46. //}}AFX_DATA_MAP
  47. }
  48. BEGIN_MESSAGE_MAP(CAddServer, CBaseDialog)
  49. //{{AFX_MSG_MAP(CAddServer)
  50. ON_BN_CLICKED(IDC_BUTTON_BROWSE_SERVERS, OnButtonBrowseServers)
  51. ON_BN_CLICKED(IDC_RADIO_ANY_SERVER, OnRadioAnyServer)
  52. ON_BN_CLICKED(IDC_RADIO_AUTHORIZED_SERVERS, OnRadioAuthorizedServers)
  53. ON_EN_CHANGE(IDC_EDIT_ADD_SERVER_NAME, OnChangeEditAddServerName)
  54. ON_WM_TIMER()
  55. ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_AUTHORIZED_SERVERS, OnItemchangedListAuthorizedServers)
  56. ON_NOTIFY(LVN_COLUMNCLICK, IDC_LIST_AUTHORIZED_SERVERS, OnColumnclickListAuthorizedServers)
  57. //}}AFX_MSG_MAP
  58. END_MESSAGE_MAP()
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CAddServer message handlers
  61. BOOL CAddServer::OnInitDialog()
  62. {
  63. CBaseDialog::OnInitDialog();
  64. LV_COLUMN lvColumn;
  65. CString strText;
  66. strText.LoadString(IDS_NAME);
  67. ListView_SetExtendedListViewStyle(m_listctrlServers.GetSafeHwnd(), LVS_EX_FULLROWSELECT);
  68. lvColumn.mask = LVCF_TEXT | LVCF_FMT | LVCF_WIDTH;
  69. lvColumn.fmt = LVCFMT_LEFT;
  70. lvColumn.cx = 175;
  71. lvColumn.pszText = (LPTSTR) (LPCTSTR) strText;
  72. m_listctrlServers.InsertColumn(0, &lvColumn);
  73. strText.LoadString(IDS_IP_ADDRESS);
  74. lvColumn.pszText = (LPTSTR) (LPCTSTR) strText;
  75. lvColumn.cx = 100;
  76. m_listctrlServers.InsertColumn(1, &lvColumn);
  77. m_editServer.SetFocus();
  78. m_radioAnyServer.SetCheck(TRUE);
  79. UpdateControls();
  80. FillListCtrl();
  81. return FALSE; // return TRUE unless you set the focus to a control
  82. // EXCEPTION: OCX Property Pages should return FALSE
  83. }
  84. void CAddServer::OnOK()
  85. {
  86. if (!GetInfo(m_strName, m_strIp))
  87. return;
  88. CleanupTimer();
  89. CBaseDialog::OnOK();
  90. }
  91. void CAddServer::OnCancel()
  92. {
  93. CleanupTimer();
  94. CBaseDialog::OnCancel();
  95. }
  96. void CAddServer::OnButtonBrowseServers()
  97. {
  98. CGetComputer dlgGetComputer;
  99. if (!dlgGetComputer.GetComputer(::FindMMCMainWindow()))
  100. return;
  101. m_editServer.SetWindowText(dlgGetComputer.m_strComputerName);
  102. }
  103. void CAddServer::OnRadioAnyServer()
  104. {
  105. UpdateControls();
  106. }
  107. void CAddServer::OnRadioAuthorizedServers()
  108. {
  109. UpdateControls();
  110. }
  111. void CAddServer::OnChangeEditAddServerName()
  112. {
  113. UpdateControls();
  114. }
  115. void CAddServer::OnTimer(UINT nIDEvent)
  116. {
  117. if (m_pServerList->IsInitialized())
  118. {
  119. m_radioAuthorizedServer.EnableWindow(TRUE);
  120. CleanupTimer();
  121. FillListCtrl();
  122. }
  123. }
  124. void CAddServer::OnItemchangedListAuthorizedServers(NMHDR* pNMHDR, LRESULT* pResult)
  125. {
  126. NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
  127. UpdateControls();
  128. *pResult = 0;
  129. }
  130. void CAddServer::FillListCtrl()
  131. {
  132. CServerInfo ServerInfo;
  133. CString strIp;
  134. int nItem = 0;
  135. m_listctrlServers.DeleteAllItems();
  136. if (m_pServerList->IsInitialized())
  137. {
  138. // fill the list control with data
  139. POSITION pos = m_pServerList->GetHeadPosition();
  140. // walk the list and add items to the list control
  141. while (pos != NULL)
  142. {
  143. POSITION lastpos = pos;
  144. // get the next item
  145. ServerInfo = m_pServerList->GetNext(pos);
  146. UtilCvtIpAddrToWstr(ServerInfo.m_dwIp, &strIp);
  147. nItem = m_listctrlServers.InsertItem(nItem, ServerInfo.m_strName);
  148. m_listctrlServers.SetItemText(nItem, 1, strIp);
  149. // save off the position value for sorting later
  150. m_listctrlServers.SetItemData(nItem, (DWORD_PTR) lastpos);
  151. }
  152. if (m_listctrlServers.GetItemCount() > 0)
  153. {
  154. // select the first one by default
  155. m_listctrlServers.SetItemState(0, LVIS_SELECTED, LVIS_SELECTED);
  156. }
  157. Sort(COLUMN_NAME);
  158. }
  159. else
  160. {
  161. // put some text in the list control and start a timer
  162. // so we can periodically check to see if initialization completes
  163. CString strMessage;
  164. strMessage.LoadString(IDS_ADD_SERVER_LOADING);
  165. m_listctrlServers.InsertItem(0, strMessage);
  166. ::SetTimer(GetSafeHwnd(), ADD_SERVER_TIMER_ID, 500, NULL);
  167. }
  168. }
  169. void CAddServer::UpdateControls()
  170. {
  171. BOOL fAnyServer = TRUE;
  172. BOOL fAuthorizedServer = FALSE;
  173. BOOL fEnableOk = FALSE;
  174. if (!m_pServerList->IsInitialized())
  175. {
  176. m_radioAuthorizedServer.EnableWindow(FALSE);
  177. }
  178. if (!m_radioAnyServer.GetCheck())
  179. {
  180. // enable the auth server list
  181. fAnyServer = FALSE;
  182. fAuthorizedServer = TRUE;
  183. // check to see if something is selected
  184. CString strName, strIp;
  185. GetSelectedServer(strName, strIp);
  186. if (!strName.IsEmpty() ||
  187. !strIp.IsEmpty())
  188. {
  189. fEnableOk = TRUE;
  190. }
  191. }
  192. else
  193. {
  194. // check to see if the edit box is empty
  195. CString strText;
  196. m_editServer.GetWindowText(strText);
  197. if (!strText.IsEmpty())
  198. {
  199. fEnableOk = TRUE;
  200. }
  201. }
  202. m_editServer.EnableWindow(fAnyServer);
  203. m_buttonBrowse.EnableWindow(fAnyServer);
  204. m_listctrlServers.EnableWindow(fAuthorizedServer);
  205. m_buttonOk.EnableWindow(fEnableOk);
  206. }
  207. BOOL CAddServer::GetInfo(CString & strName, CString & strIp)
  208. {
  209. BOOL fSuccess = TRUE;
  210. if (!m_radioAnyServer.GetCheck())
  211. {
  212. // check to see if something is selected
  213. GetSelectedServer(strName, strIp);
  214. }
  215. else
  216. {
  217. m_editServer.GetWindowText(strName);
  218. DWORD dwIpAddress = 0;
  219. DWORD err = ERROR_SUCCESS;
  220. DHC_HOST_INFO_STRUCT hostInfo;
  221. BEGIN_WAIT_CURSOR
  222. switch (::UtilCategorizeName(strName))
  223. {
  224. case HNM_TYPE_IP:
  225. dwIpAddress = ::UtilCvtWstrToIpAddr( strName ) ;
  226. strName.Empty();
  227. break ;
  228. case HNM_TYPE_NB:
  229. case HNM_TYPE_DNS:
  230. err = ::UtilGetHostAddress( strName, &dwIpAddress ) ;
  231. break ;
  232. default:
  233. err = IDS_ERR_BAD_HOST_NAME ;
  234. break;
  235. }
  236. END_WAIT_CURSOR
  237. /*
  238. if (err != ERROR_SUCCESS)
  239. {
  240. DhcpMessageBox(err);
  241. fSuccess = FALSE;
  242. return fSuccess;
  243. }
  244. */
  245. if (err == ERROR_SUCCESS)
  246. {
  247. BEGIN_WAIT_CURSOR
  248. // get the FQDN for this machine and set it.
  249. err = ::UtilGetHostInfo(dwIpAddress, &hostInfo);
  250. // Make sure we do not use 127.0.0.1
  251. if (( INADDR_LOOPBACK == dwIpAddress ) &&
  252. ( NO_ERROR == err )) {
  253. ::UtilGetHostAddress( hostInfo._chHostName, &dwIpAddress );
  254. } // if
  255. END_WAIT_CURSOR
  256. // only put up this error if the user typed in an IP address
  257. // and we couldn't resolve it to a name.
  258. // if we have a name but just can't resolve it to a FQDN
  259. // then we'll try to use that.
  260. /*
  261. if (err != ERROR_SUCCESS &&
  262. strName.IsEmpty())
  263. {
  264. ::DhcpMessageBox(WIN32_FROM_HRESULT(err));
  265. fSuccess = FALSE;
  266. }
  267. */
  268. CString strTemp = hostInfo._chHostName;
  269. if (!strTemp.IsEmpty())
  270. strName = hostInfo._chHostName;
  271. }
  272. ::UtilCvtIpAddrToWstr(dwIpAddress, &strIp);
  273. }
  274. return fSuccess;
  275. }
  276. void CAddServer::GetSelectedServer(CString & strName, CString & strIp)
  277. {
  278. strName.Empty();
  279. strIp.Empty();
  280. int nSelectedItem = m_listctrlServers.GetNextItem(-1, LVNI_SELECTED);
  281. if (nSelectedItem != -1)
  282. {
  283. strName = m_listctrlServers.GetItemText(nSelectedItem, 0);
  284. strIp = m_listctrlServers.GetItemText(nSelectedItem, 1);
  285. }
  286. }
  287. void CAddServer::CleanupTimer()
  288. {
  289. KillTimer(ADD_SERVER_TIMER_ID);
  290. }
  291. void CAddServer::OnColumnclickListAuthorizedServers(NMHDR* pNMHDR, LRESULT* pResult)
  292. {
  293. NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
  294. // sort depending on what column was clicked;
  295. Sort(pNMListView->iSubItem);
  296. *pResult = 0;
  297. }
  298. void CAddServer::Sort(int nCol)
  299. {
  300. if (m_nSortColumn == nCol)
  301. {
  302. // if the user is clicking the same column again, reverse the sort order
  303. m_aSortOrder[nCol] = m_aSortOrder[nCol] ? FALSE : TRUE;
  304. }
  305. else
  306. {
  307. m_nSortColumn = nCol;
  308. }
  309. m_listctrlServers.SortItems(AddServerCompareFunc, (LPARAM) this);
  310. }
  311. int CAddServer::HandleSort(LPARAM lParam1, LPARAM lParam2)
  312. {
  313. int nCompare = 0;
  314. CServerInfo ServerInfo1, ServerInfo2;
  315. ServerInfo1 = m_pServerList->GetAt((POSITION) lParam1);
  316. ServerInfo2 = m_pServerList->GetAt((POSITION) lParam2);
  317. switch (m_nSortColumn)
  318. {
  319. case COLUMN_NAME:
  320. {
  321. nCompare = ServerInfo1.m_strName.CompareNoCase(ServerInfo2.m_strName);
  322. }
  323. // if the names are the same, fall back to the IP address
  324. if (nCompare != 0)
  325. {
  326. break;
  327. }
  328. case COLUMN_IP:
  329. {
  330. if (ServerInfo1.m_dwIp > ServerInfo2.m_dwIp)
  331. nCompare = 1;
  332. else
  333. if (ServerInfo1.m_dwIp < ServerInfo2.m_dwIp)
  334. nCompare = -1;
  335. }
  336. break;
  337. }
  338. if (m_aSortOrder[m_nSortColumn] == FALSE)
  339. {
  340. // descending
  341. return -nCompare;
  342. }
  343. else
  344. {
  345. // ascending
  346. return nCompare;
  347. }
  348. }
  349. void CAddServer::ResetSort()
  350. {
  351. m_nSortColumn = -1;
  352. for (int i = 0; i < COLUMN_MAX; i++)
  353. {
  354. m_aSortOrder[i] = TRUE; // ascending
  355. }
  356. }