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.

452 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. m_lSelSrv.RemoveAll();
  82. return FALSE; // return TRUE unless you set the focus to a control
  83. // EXCEPTION: OCX Property Pages should return FALSE
  84. }
  85. void CAddServer::OnOK()
  86. {
  87. GetInfo();
  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. BOOL success;
  100. success = dlgGetComputer.GetComputer(::FindMMCMainWindow());
  101. // Return the focus back to this window
  102. SetFocus();
  103. if ( !success ) {
  104. return;
  105. }
  106. m_editServer.SetWindowText(dlgGetComputer.m_strComputerName);
  107. }
  108. void CAddServer::OnRadioAnyServer()
  109. {
  110. UpdateControls();
  111. }
  112. void CAddServer::OnRadioAuthorizedServers()
  113. {
  114. UpdateControls();
  115. }
  116. void CAddServer::OnChangeEditAddServerName()
  117. {
  118. UpdateControls();
  119. }
  120. void CAddServer::OnTimer(UINT nIDEvent)
  121. {
  122. if (m_pServerList->IsInitialized())
  123. {
  124. m_radioAuthorizedServer.EnableWindow(TRUE);
  125. CleanupTimer();
  126. FillListCtrl();
  127. }
  128. }
  129. void CAddServer::OnItemchangedListAuthorizedServers(NMHDR* pNMHDR, LRESULT* pResult)
  130. {
  131. NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
  132. UpdateControls();
  133. *pResult = 0;
  134. }
  135. void CAddServer::FillListCtrl()
  136. {
  137. CServerInfo ServerInfo;
  138. CString strIp;
  139. int nItem = 0;
  140. m_listctrlServers.DeleteAllItems();
  141. if (m_pServerList->IsInitialized())
  142. {
  143. // fill the list control with data
  144. POSITION pos = m_pServerList->GetHeadPosition();
  145. // walk the list and add items to the list control
  146. while (pos != NULL)
  147. {
  148. POSITION lastpos = pos;
  149. // get the next item
  150. ServerInfo = m_pServerList->GetNext(pos);
  151. UtilCvtIpAddrToWstr(ServerInfo.m_dwIp, &strIp);
  152. nItem = m_listctrlServers.InsertItem(nItem, ServerInfo.m_strName);
  153. m_listctrlServers.SetItemText(nItem, 1, strIp);
  154. // save off the position value for sorting later
  155. m_listctrlServers.SetItemData(nItem, (DWORD_PTR) lastpos);
  156. }
  157. if (m_listctrlServers.GetItemCount() > 0)
  158. {
  159. // select the first one by default
  160. m_listctrlServers.SetItemState(0, LVIS_SELECTED, LVIS_SELECTED);
  161. }
  162. Sort(COLUMN_NAME);
  163. if (m_listctrlServers.GetItemCount() > 0)
  164. {
  165. // select the first one by default
  166. m_listctrlServers.SetItemState(0, LVIS_SELECTED, LVIS_SELECTED);
  167. }
  168. }
  169. else
  170. {
  171. // put some text in the list control and start a timer
  172. // so we can periodically check to see if initialization completes
  173. CString strMessage;
  174. strMessage.LoadString(IDS_ADD_SERVER_LOADING);
  175. m_listctrlServers.InsertItem(0, strMessage);
  176. ::SetTimer(GetSafeHwnd(), ADD_SERVER_TIMER_ID, 500, NULL);
  177. }
  178. }
  179. void CAddServer::UpdateControls()
  180. {
  181. BOOL fAnyServer = TRUE;
  182. BOOL fAuthorizedServer = FALSE;
  183. BOOL fEnableOk = FALSE;
  184. if (!m_pServerList->IsInitialized())
  185. {
  186. m_radioAuthorizedServer.EnableWindow(FALSE);
  187. }
  188. if (!m_radioAnyServer.GetCheck())
  189. {
  190. // enable the auth server list
  191. fAnyServer = FALSE;
  192. fAuthorizedServer = TRUE;
  193. // check to see if something is selected
  194. fEnableOk = ( m_listctrlServers.GetSelectedCount() > 0 );
  195. }
  196. else
  197. {
  198. // check to see if the edit box is empty
  199. CString strText;
  200. m_editServer.GetWindowText(strText);
  201. if (!strText.IsEmpty())
  202. {
  203. fEnableOk = TRUE;
  204. }
  205. }
  206. m_editServer.EnableWindow(fAnyServer);
  207. m_buttonBrowse.EnableWindow(fAnyServer);
  208. m_listctrlServers.EnableWindow(fAuthorizedServer);
  209. m_buttonOk.EnableWindow(fEnableOk);
  210. }
  211. // BOOL CAddServer::GetInfo(CString & strName, CString & strIp)
  212. void CAddServer::GetInfo()
  213. {
  214. SelectedServer SelSrv;
  215. // Clear the list
  216. m_lSelSrv.RemoveAll();
  217. if (!m_radioAnyServer.GetCheck()) {
  218. // check to see if something is selected
  219. POSITION pos;
  220. int nItem;
  221. pos = m_listctrlServers.GetFirstSelectedItemPosition();
  222. if ( NULL == pos ) {
  223. return;
  224. }
  225. while ( NULL != pos ) {
  226. nItem = m_listctrlServers.GetNextSelectedItem( pos );
  227. SelSrv.strName = m_listctrlServers.GetItemText( nItem, 0 );
  228. SelSrv.strIp = m_listctrlServers.GetItemText( nItem, 1 );
  229. m_lSelSrv.AddTail( SelSrv );
  230. } // while
  231. } // if auth list
  232. else {
  233. m_editServer.GetWindowText( SelSrv.strName );
  234. DWORD dwIpAddress = 0;
  235. DWORD err = ERROR_SUCCESS;
  236. DHC_HOST_INFO_STRUCT hostInfo;
  237. BEGIN_WAIT_CURSOR
  238. switch (::UtilCategorizeName( SelSrv.strName )) {
  239. case HNM_TYPE_IP:
  240. dwIpAddress = ::UtilCvtWstrToIpAddr( SelSrv.strName ) ;
  241. SelSrv.strName.Empty();
  242. break ;
  243. case HNM_TYPE_NB:
  244. case HNM_TYPE_DNS:
  245. err = ::UtilGetHostAddress( SelSrv.strName, &dwIpAddress ) ;
  246. break ;
  247. default:
  248. err = IDS_ERR_BAD_HOST_NAME ;
  249. break;
  250. }
  251. END_WAIT_CURSOR
  252. if (err == ERROR_SUCCESS)
  253. {
  254. BEGIN_WAIT_CURSOR
  255. // get the FQDN for this machine and set it.
  256. err = ::UtilGetHostInfo( dwIpAddress, &hostInfo );
  257. // Make sure we do not use 127.0.0.1
  258. if (( INADDR_LOOPBACK == dwIpAddress ) &&
  259. ( NO_ERROR == err )) {
  260. ::UtilGetLocalHostAddress( &dwIpAddress );
  261. ::UtilGetHostInfo( dwIpAddress, &hostInfo );
  262. } // if
  263. END_WAIT_CURSOR
  264. CString strTemp = hostInfo._chHostName;
  265. if (!strTemp.IsEmpty())
  266. SelSrv.strName = hostInfo._chHostName;
  267. }
  268. ::UtilCvtIpAddrToWstr(dwIpAddress, &SelSrv.strIp);
  269. m_lSelSrv.AddTail( SelSrv );
  270. } // else specified server
  271. } // CAddServer::GetInfo()
  272. void CAddServer::CleanupTimer()
  273. {
  274. KillTimer(ADD_SERVER_TIMER_ID);
  275. }
  276. void CAddServer::OnColumnclickListAuthorizedServers(NMHDR* pNMHDR, LRESULT* pResult)
  277. {
  278. NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
  279. // sort depending on what column was clicked;
  280. Sort(pNMListView->iSubItem);
  281. *pResult = 0;
  282. }
  283. void CAddServer::Sort(int nCol)
  284. {
  285. if (m_nSortColumn == nCol)
  286. {
  287. // if the user is clicking the same column again, reverse the sort order
  288. m_aSortOrder[nCol] = m_aSortOrder[nCol] ? FALSE : TRUE;
  289. }
  290. else
  291. {
  292. m_nSortColumn = nCol;
  293. }
  294. m_listctrlServers.SortItems(AddServerCompareFunc, (LPARAM) this);
  295. }
  296. int CAddServer::HandleSort(LPARAM lParam1, LPARAM lParam2)
  297. {
  298. int nCompare = 0;
  299. CServerInfo ServerInfo1, ServerInfo2;
  300. ServerInfo1 = m_pServerList->GetAt((POSITION) lParam1);
  301. ServerInfo2 = m_pServerList->GetAt((POSITION) lParam2);
  302. switch (m_nSortColumn)
  303. {
  304. case COLUMN_NAME:
  305. {
  306. nCompare = ServerInfo1.m_strName.CompareNoCase(ServerInfo2.m_strName);
  307. }
  308. // if the names are the same, fall back to the IP address
  309. if (nCompare != 0)
  310. {
  311. break;
  312. }
  313. case COLUMN_IP:
  314. {
  315. if (ServerInfo1.m_dwIp > ServerInfo2.m_dwIp)
  316. nCompare = 1;
  317. else
  318. if (ServerInfo1.m_dwIp < ServerInfo2.m_dwIp)
  319. nCompare = -1;
  320. }
  321. break;
  322. }
  323. if (m_aSortOrder[m_nSortColumn] == FALSE)
  324. {
  325. // descending
  326. return -nCompare;
  327. }
  328. else
  329. {
  330. // ascending
  331. return nCompare;
  332. }
  333. }
  334. void CAddServer::ResetSort()
  335. {
  336. m_nSortColumn = -1;
  337. for (int i = 0; i < COLUMN_MAX; i++)
  338. {
  339. m_aSortOrder[i] = TRUE; // ascending
  340. }
  341. }