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.

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