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.

741 lines
15 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows/NT **/
  3. /** Copyright(c) Microsoft Corporation, 1999 - 1999 **/
  4. /**********************************************************************/
  5. /*
  6. chkrgdlg.cpp.cpp
  7. The check registered names dialog
  8. FILE HISTORY:
  9. */
  10. #include "stdafx.h"
  11. #include <mbstring.h>
  12. #include "winssnap.h"
  13. #include "actreg.h"
  14. #include "ChkRgdlg.h"
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CCheckRegNames dialog
  22. #define NAME 0
  23. #define SERVER 1
  24. CCheckRegNames::CCheckRegNames(CWnd* pParent /*=NULL*/)
  25. : CBaseDialog(CCheckRegNames::IDD, pParent)
  26. {
  27. //{{AFX_DATA_INIT(CCheckRegNames)
  28. m_nFileName = 0;
  29. m_strName = _T("");
  30. m_strServer = _T("");
  31. m_nFileServer = 0;
  32. m_strRecNameForList = _T("");
  33. m_fVerifyWithPartners = FALSE;
  34. //}}AFX_DATA_INIT
  35. }
  36. void CCheckRegNames::DoDataExchange(CDataExchange* pDX)
  37. {
  38. CBaseDialog::DoDataExchange(pDX);
  39. //{{AFX_DATA_MAP(CCheckRegNames)
  40. DDX_Control(pDX, IDC_EDIT_NAME_LIST, m_editRecordNameForList);
  41. DDX_Control(pDX, IDC_SERVER_ADD, m_buttonAddServer);
  42. DDX_Control(pDX, IDC_SERVER_REMOVE, m_buttonRemoveServer);
  43. DDX_Control(pDX, IDC_SERVER_BROWSE, m_buttonBrowseServer);
  44. DDX_Control(pDX, IDC_NAME_REMOVE, m_buttonNameremove);
  45. DDX_Control(pDX, IDC_NAME_BROWSE, m_buttonBrowseName);
  46. DDX_Control(pDX, IDC_NAME_ADD, m_buttonAddName);
  47. DDX_Control(pDX, IDC_LIST_SERVER, m_listServer);
  48. DDX_Control(pDX, IDC_LIST_NAME, m_listName);
  49. DDX_Control(pDX, IDC_EDIT_SERVER, m_editServer);
  50. DDX_Control(pDX, IDC_EDIT_NAME, m_editName);
  51. DDX_Radio(pDX, IDC_RADIO_NAME_FILE, m_nFileName);
  52. DDX_Text(pDX, IDC_EDIT_NAME, m_strName);
  53. DDX_Text(pDX, IDC_EDIT_SERVER, m_strServer);
  54. DDX_Radio(pDX, IDC_RADIO_SERVER_FILE, m_nFileServer);
  55. DDX_Text(pDX, IDC_EDIT_NAME_LIST, m_strRecNameForList);
  56. DDX_Check(pDX, IDC_CHECK_PARTNERS, m_fVerifyWithPartners);
  57. //}}AFX_DATA_MAP
  58. DDX_Control(pDX, IDC_IPADDRESS, m_ipaServerIPAddress);
  59. }
  60. BEGIN_MESSAGE_MAP(CCheckRegNames, CBaseDialog)
  61. //{{AFX_MSG_MAP(CCheckRegNames)
  62. ON_BN_CLICKED(IDC_NAME_BROWSE, OnNameBrowse)
  63. ON_BN_CLICKED(IDC_SERVER_BROWSE, OnServerBrowse)
  64. ON_EN_CHANGE(IDC_EDIT_NAME, OnChangeEditName)
  65. ON_EN_CHANGE(IDC_EDIT_SERVER, OnChangeEditServer)
  66. ON_LBN_SELCHANGE(IDC_LIST_NAME, OnSelchangeListName)
  67. ON_LBN_SELCHANGE(IDC_LIST_SERVER, OnSelchangeListServer)
  68. ON_BN_CLICKED(IDC_NAME_ADD, OnNameAdd)
  69. ON_BN_CLICKED(IDC_NAME_REMOVE, OnNameRemove)
  70. ON_BN_CLICKED(IDC_SERVER_ADD, OnServerAdd)
  71. ON_BN_CLICKED(IDC_SERVER_REMOVE, OnServerRemove)
  72. ON_BN_CLICKED(IDC_RADIO_NAME_FILE, OnRadioNameFile)
  73. ON_BN_CLICKED(IDC_RADIO_NAME_LIST, OnRadioNameList)
  74. ON_BN_CLICKED(IDC_RADIO_SERVER_FILE, OnRadioServerFile)
  75. ON_BN_CLICKED(IDC_RADIO_SERVER_LIST, OnRadioServerList)
  76. ON_EN_CHANGE(IDC_EDIT_NAME_LIST, OnChangeEditNameList)
  77. //}}AFX_MSG_MAP
  78. ON_EN_CHANGE(IDC_IPADDRESS,OnChangeIpAddress)
  79. END_MESSAGE_MAP()
  80. /////////////////////////////////////////////////////////////////////////////
  81. // CCheckRegNames message handlers
  82. BOOL CCheckRegNames::OnInitDialog()
  83. {
  84. CBaseDialog::OnInitDialog();
  85. EnableControls(NAME, FALSE);
  86. EnableControls(SERVER, FALSE);
  87. return TRUE; // return TRUE unless you set the focus to a control
  88. // EXCEPTION: OCX Property Pages should return FALSE
  89. }
  90. void CCheckRegNames::OnNameBrowse()
  91. {
  92. CString strFilter;
  93. strFilter.LoadString(IDS_TEXT_FILES);
  94. CFileDialog cFileDlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, strFilter);
  95. CThemeContextActivator themeActivator;
  96. if (IDOK != cFileDlg.DoModal())
  97. return;
  98. CString strFile = cFileDlg.GetPathName();
  99. m_editName.EnableWindow(TRUE);
  100. m_editName.SetWindowText(strFile);
  101. m_editName.SetReadOnly(TRUE);
  102. ParseFile(strFile, NAME);
  103. }
  104. void CCheckRegNames::OnServerBrowse()
  105. {
  106. CString strFilter;
  107. strFilter.LoadString(IDS_TEXT_FILES);
  108. CFileDialog cFileDlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, strFilter);
  109. CThemeContextActivator themeActivator;
  110. if (IDOK != cFileDlg.DoModal())
  111. return ;
  112. CString strFile = cFileDlg.GetPathName();
  113. m_editServer.EnableWindow(TRUE);
  114. m_editServer.SetWindowText(strFile);
  115. m_editServer.SetReadOnly(TRUE);
  116. ParseFile(strFile, SERVER);
  117. }
  118. void
  119. CCheckRegNames::EnableControls(int nNameOrServer, BOOL bEnable)
  120. {
  121. switch (nNameOrServer)
  122. {
  123. case SERVER:
  124. m_buttonAddServer.EnableWindow(bEnable);
  125. m_buttonRemoveServer.EnableWindow(bEnable);
  126. //m_listServer.EnableWindow(bEnable);
  127. //m_editServerNameForList.EnableWindow(bEnable);
  128. m_ipaServerIPAddress.EnableWindow(bEnable);
  129. m_buttonBrowseServer.EnableWindow(!bEnable);
  130. m_editServer.EnableWindow(!bEnable);
  131. m_editServer.SetReadOnly(TRUE);
  132. break;
  133. case NAME:
  134. m_buttonAddName.EnableWindow(bEnable);
  135. m_buttonNameremove.EnableWindow(bEnable);
  136. //m_listName.EnableWindow(bEnable);
  137. m_editRecordNameForList.EnableWindow(bEnable);
  138. m_buttonBrowseName.EnableWindow(!bEnable);
  139. m_editName.EnableWindow(!bEnable);
  140. m_editName.SetReadOnly(TRUE);
  141. break;
  142. }
  143. }
  144. void
  145. CCheckRegNames::ParseFile(CString strFile, int nServerOrName)
  146. {
  147. TRY
  148. {
  149. #define MAX_SIZE 64000
  150. CFile cFile(strFile, CFile::modeRead );
  151. CString strContent;
  152. char szContent[MAX_SIZE];
  153. int nSize = cFile.GetLength();
  154. cFile.Read(szContent, nSize);
  155. szContent[nSize] = '\0';
  156. CString strTemp(szContent);
  157. strContent = strTemp;
  158. cFile.Close();
  159. if (strContent.IsEmpty())
  160. {
  161. CThemeContextActivator themeActivator;
  162. ::AfxMessageBox(IDS_ERR_FILE_EMPTY, MB_OK|MB_ICONINFORMATION);
  163. if (nServerOrName == SERVER)
  164. m_buttonBrowseServer.SetFocus();
  165. else
  166. if(nServerOrName == NAME)
  167. m_buttonBrowseName.SetFocus();
  168. return;
  169. }
  170. AddFileContent(strContent, nServerOrName);
  171. }
  172. CATCH( CFileException, e )
  173. {
  174. #ifdef _DEBUG
  175. afxDump << "File could not be opened " << e->m_cause << "\n";
  176. #endif
  177. return;
  178. }
  179. END_CATCH
  180. }
  181. void
  182. CCheckRegNames::SetControlState(int nNameOrServer)
  183. {
  184. UpdateData();
  185. DWORD dwIPAdd;
  186. switch (nNameOrServer)
  187. {
  188. case NAME:
  189. if ( (m_editRecordNameForList.GetWindowTextLength() > 0) &&
  190. (m_nFileName != 0) )
  191. {
  192. m_buttonAddName.EnableWindow(TRUE);
  193. }
  194. else
  195. {
  196. m_buttonAddName.EnableWindow(FALSE);
  197. }
  198. if ( (m_listName.GetCurSel() == LB_ERR ) ||
  199. (m_nFileName == 0) )
  200. {
  201. m_buttonNameremove.EnableWindow(FALSE);
  202. }
  203. else
  204. {
  205. m_buttonNameremove.EnableWindow(TRUE);
  206. }
  207. break;
  208. case SERVER:
  209. m_ipaServerIPAddress.GetAddress(&dwIPAdd);
  210. if ( (dwIPAdd != 0) &&
  211. (m_nFileServer != 0) )
  212. {
  213. m_buttonAddServer.EnableWindow(TRUE);
  214. }
  215. else
  216. {
  217. m_buttonAddServer.EnableWindow(FALSE);
  218. }
  219. if ( (m_listServer.GetCurSel() == LB_ERR) ||
  220. (m_nFileServer == 0) )
  221. {
  222. m_buttonRemoveServer.EnableWindow(FALSE);
  223. }
  224. else
  225. {
  226. m_buttonRemoveServer.EnableWindow(TRUE);
  227. }
  228. break;
  229. }
  230. }
  231. void CCheckRegNames::OnChangeEditName()
  232. {
  233. UpdateData();
  234. if(m_nFileName != 0)
  235. SetControlState(NAME);
  236. }
  237. void CCheckRegNames::OnChangeEditServer()
  238. {
  239. UpdateData();
  240. if(m_nFileServer != 0)
  241. SetControlState(SERVER);
  242. }
  243. void CCheckRegNames::OnSelchangeListName()
  244. {
  245. SetControlState(NAME);
  246. }
  247. void CCheckRegNames::OnSelchangeListServer()
  248. {
  249. SetControlState(SERVER);
  250. }
  251. void CCheckRegNames::OnOK()
  252. {
  253. CThemeContextActivator themeActivator;
  254. UpdateData();
  255. // if the list radio button is selected
  256. if (m_nFileServer)
  257. {
  258. Add(SERVER);
  259. }
  260. if (m_strServerArray.GetSize() == 0)
  261. {
  262. AfxMessageBox(IDS_ERR_NAME_CHECK_NO_SERVERS);
  263. return;
  264. }
  265. //if the list radio button is selected
  266. if (m_nFileName)
  267. {
  268. Add(NAME);
  269. }
  270. if (m_strNameArray.GetSize() == 0)
  271. {
  272. AfxMessageBox(IDS_ERR_NAME_CHECK_NO_NAMES);
  273. return;
  274. }
  275. // clean up the name array because of deletions
  276. for (int i = 0; i < m_strNameArray.GetSize(); i++)
  277. {
  278. CWinsName winsName = m_strNameArray.GetAt(i);
  279. if (winsName.strName.IsEmpty())
  280. {
  281. m_strNameArray.RemoveAt(i);
  282. i--;
  283. }
  284. }
  285. CBaseDialog::OnOK();
  286. }
  287. void CCheckRegNames::OnNameAdd()
  288. {
  289. UpdateData();
  290. CString strFormattedName;
  291. CWinsName winsName;
  292. CThemeContextActivator themeActivator;
  293. if (!ParseName(m_strRecNameForList, winsName, strFormattedName))
  294. {
  295. // invalid name
  296. CString strMessage;
  297. AfxFormatString1(strMessage, IDS_ERR_INVALID_NAME, m_strRecNameForList);
  298. AfxMessageBox(strMessage);
  299. return;
  300. }
  301. if (!CheckIfPresent(winsName, NAME))
  302. {
  303. int nItem = m_listName.AddString(strFormattedName);
  304. int nIndex = (int)m_strNameArray.Add(winsName);
  305. m_listName.SetItemData(nItem, nIndex);
  306. m_editRecordNameForList.SetWindowText(NULL);
  307. }
  308. else
  309. {
  310. ::AfxMessageBox(IDS_STRING_PRESENT);
  311. m_editRecordNameForList.SetFocus();
  312. m_editRecordNameForList.SetSel(0,-1);
  313. return;
  314. }
  315. }
  316. void CCheckRegNames::OnNameRemove()
  317. {
  318. int nSel = m_listName.GetCurSel();
  319. if (nSel == LB_ERR)
  320. return;
  321. DWORD_PTR dwIndex = m_listName.GetItemData(nSel);
  322. m_listName.DeleteString(nSel);
  323. m_strNameArray[(int) dwIndex].strName.Empty();
  324. SetControlState(NAME);
  325. }
  326. void CCheckRegNames::OnServerAdd()
  327. {
  328. UpdateData();
  329. DWORD dwIPAdd;
  330. m_ipaServerIPAddress.GetAddress(&dwIPAdd);
  331. if (dwIPAdd == 0)
  332. {
  333. m_ipaServerIPAddress.SetFocusField(0);
  334. return;
  335. }
  336. CString strIP;
  337. m_ipaServerIPAddress.GetWindowText(strIP);
  338. if (!CheckIfPresent(strIP, SERVER))
  339. {
  340. m_listServer.AddString(strIP);
  341. m_ipaServerIPAddress.ClearAddress();
  342. }
  343. else
  344. {
  345. CThemeContextActivator themeActivator;
  346. ::AfxMessageBox(IDS_STRING_PRESENT);
  347. m_ipaServerIPAddress.SetFocusField(0);
  348. return;
  349. }
  350. }
  351. void CCheckRegNames::OnServerRemove()
  352. {
  353. int nSel = m_listServer.GetCurSel();
  354. if (nSel == LB_ERR)
  355. return;
  356. m_listServer.DeleteString(nSel);
  357. SetControlState(SERVER);
  358. }
  359. void
  360. CCheckRegNames::AddFileContent(CString &strContent, int nNameOrServer)
  361. {
  362. int nCount;
  363. int i = 0;
  364. int nPos = 0;
  365. CString strPart;
  366. CThemeContextActivator themeActivator;
  367. while (i < strContent.GetLength())
  368. {
  369. strPart.Empty();
  370. while(strContent[i] != 10 && strContent[i] != 13)
  371. {
  372. strPart += strContent[i++];
  373. if (i > strContent.GetLength())
  374. break;
  375. }
  376. if (!strPart.IsEmpty())
  377. {
  378. if (nNameOrServer == NAME)
  379. {
  380. CString strFormattedName;
  381. CWinsName winsName;
  382. if (!ParseName(strPart, winsName, strFormattedName))
  383. {
  384. // invalid name
  385. CString strMessage;
  386. AfxFormatString1(strMessage, IDS_INVALID_NAME_IN_FILE, strPart);
  387. if (AfxMessageBox(strMessage, MB_YESNO) == IDNO)
  388. break;
  389. }
  390. else
  391. if (!CheckIfAdded(winsName, NAME))
  392. {
  393. // add to internal lists and UI
  394. int nItem = m_listName.AddString(strFormattedName);
  395. int nIndex = (int)m_strNameArray.Add(winsName);
  396. m_listName.SetItemData(nItem, nIndex);
  397. }
  398. }
  399. else
  400. if (nNameOrServer == SERVER)
  401. {
  402. if (!CheckIfAdded(strPart, SERVER) && !strPart.IsEmpty())
  403. {
  404. m_listServer.AddString(strPart);
  405. m_strServerArray.Add(strPart);
  406. }
  407. }
  408. }
  409. i++;
  410. }
  411. }
  412. void
  413. CCheckRegNames::Add(int nServerOrName)
  414. {
  415. int nCount;
  416. int i = 0;
  417. switch (nServerOrName)
  418. {
  419. case SERVER:
  420. m_strServerArray.RemoveAll();
  421. nCount = m_listServer.GetCount();
  422. for(i = 0; i < nCount; i++)
  423. {
  424. CString strText;
  425. m_listServer.GetText(i, strText);
  426. m_strServerArray.Add(strText);
  427. }
  428. break;
  429. case NAME:
  430. /*
  431. m_strNameArray.RemoveAll();
  432. nCount = m_listName.GetCount();
  433. for(i = 0; i < nCount; i++)
  434. {
  435. CString strText;
  436. m_listName.GetText(i, strText);
  437. strText.MakeUpper();
  438. m_strNameArray.Add(strText);
  439. }
  440. */
  441. break;
  442. }
  443. }
  444. void CCheckRegNames::OnRadioNameFile()
  445. {
  446. EnableControls(NAME, FALSE);
  447. }
  448. void CCheckRegNames::OnRadioNameList()
  449. {
  450. // set focus to the edit control
  451. m_editRecordNameForList.SetFocus();
  452. EnableControls(NAME, TRUE);
  453. SetControlState(NAME);
  454. }
  455. void CCheckRegNames::OnRadioServerFile()
  456. {
  457. EnableControls(SERVER, FALSE);
  458. }
  459. void CCheckRegNames::OnRadioServerList()
  460. {
  461. // set focus to the IP control meant for the list
  462. m_ipaServerIPAddress.SetFocus();
  463. EnableControls(SERVER, TRUE);
  464. SetControlState(SERVER);
  465. }
  466. BOOL
  467. CCheckRegNames::CheckIfPresent(CWinsName & winsName, int nNameOrServer)
  468. {
  469. BOOL bPresent = FALSE;
  470. if (nNameOrServer == NAME)
  471. {
  472. int nCount = (int)m_strNameArray.GetSize();
  473. for (int i = 0; i < nCount; i++)
  474. {
  475. CWinsName winsNameCur = m_strNameArray[i];
  476. if (winsName == winsNameCur)
  477. {
  478. bPresent = TRUE;
  479. break;
  480. }
  481. }
  482. }
  483. return bPresent;
  484. }
  485. BOOL
  486. CCheckRegNames::CheckIfPresent(CString & strText, int nNameOrServer)
  487. {
  488. BOOL bPresent = FALSE;
  489. if (nNameOrServer == SERVER)
  490. {
  491. int nCount = m_listServer.GetCount();
  492. for (int i = 0; i < nCount; i++)
  493. {
  494. CString strListItem;
  495. m_listServer.GetText(i, strListItem);
  496. if (strListItem.CompareNoCase(strText) == 0)
  497. {
  498. bPresent = TRUE;
  499. break;
  500. }
  501. }
  502. }
  503. return bPresent;
  504. }
  505. BOOL
  506. CCheckRegNames::CheckIfAdded(CWinsName winsName, int nNameOrServer)
  507. {
  508. BOOL bAdded = FALSE;
  509. int nCount;
  510. if (nNameOrServer == NAME)
  511. {
  512. nCount = (int)m_strNameArray.GetSize();
  513. for (int i = 0; i < nCount; i++)
  514. {
  515. CWinsName winsNameCur = m_strNameArray[i];
  516. if (winsName == winsNameCur)
  517. {
  518. bAdded = TRUE;
  519. break;
  520. }
  521. }
  522. }
  523. return bAdded;
  524. }
  525. BOOL
  526. CCheckRegNames::CheckIfAdded(CString & strText, int nNameOrServer)
  527. {
  528. BOOL bAdded = FALSE;
  529. int nCount;
  530. if (nNameOrServer == SERVER)
  531. {
  532. nCount = (int)m_strServerArray.GetSize();
  533. for (int i = 0; i < nCount; i++)
  534. {
  535. if (m_strServerArray[i].Compare(strText) == 0)
  536. {
  537. bAdded = TRUE;
  538. break;
  539. }
  540. }
  541. }
  542. return bAdded;
  543. }
  544. void CCheckRegNames::OnChangeEditNameList()
  545. {
  546. UpdateData();
  547. if (m_nFileName != 0)
  548. SetControlState(NAME);
  549. }
  550. void
  551. CCheckRegNames::OnChangeIpAddress()
  552. {
  553. SetControlState(SERVER);
  554. }
  555. BOOL
  556. CCheckRegNames::ParseName(CString & strNameIn, CWinsName & winsName, CString & strFormattedName)
  557. {
  558. int nSeparator = strNameIn.Find(_T("*"));
  559. if (nSeparator == -1)
  560. {
  561. // no * separator between name and type -- in valid name
  562. return FALSE;
  563. }
  564. CString strName, strType;
  565. strName = strNameIn.Left(nSeparator);
  566. strType = strNameIn.Right(strNameIn.GetLength() - nSeparator - 1);
  567. if (strName.GetLength() > 15 ||
  568. strName.IsEmpty())
  569. {
  570. // name is too long or too short
  571. return FALSE;
  572. }
  573. DWORD dwType = _tcstol(strType, NULL, 16);
  574. if (dwType > 255)
  575. {
  576. // invalid type specified
  577. return FALSE;
  578. }
  579. winsName.strName = strName;
  580. winsName.dwType = dwType;
  581. strFormattedName.Format(_T("%s[%02Xh]"), strName, dwType);
  582. return TRUE;
  583. }