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.

471 lines
11 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows/NT **/
  3. /** Copyright(c) Microsoft Corporation, 1995 - 1999 **/
  4. /**********************************************************************/
  5. /*
  6. dlgdefop.cpp
  7. Default options dialog
  8. FILE HISTORY:
  9. */
  10. #include "stdafx.h"
  11. #include "scope.h"
  12. #include "dlgdefop.h"
  13. #ifdef _DEBUG
  14. #undef THIS_FILE
  15. static char BASED_CODE THIS_FILE[] = __FILE__;
  16. #endif
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CDhcpDefOptionDlg dialog
  19. CDhcpDefOptionDlg::CDhcpDefOptionDlg
  20. (
  21. COptionList * polValues,
  22. CDhcpOption * pdhcType,
  23. LPCTSTR pszVendor, // Vendor Name
  24. CWnd* pParent /*=NULL*/
  25. ) : CBaseDialog(CDhcpDefOptionDlg::IDD, pParent),
  26. m_pol_types( polValues ),
  27. m_p_type_base( pdhcType ),
  28. m_p_type( NULL )
  29. {
  30. //{{AFX_DATA_INIT(CDhcpDefOptionDlg)
  31. //}}AFX_DATA_INIT
  32. m_strVendor = pszVendor;
  33. }
  34. CDhcpDefOptionDlg::~CDhcpDefOptionDlg ()
  35. {
  36. delete m_p_type ;
  37. }
  38. CDhcpOption *
  39. CDhcpDefOptionDlg::RetrieveParamType()
  40. {
  41. CDhcpOption * pdhcParamType = m_p_type ;
  42. m_p_type = NULL ;
  43. return pdhcParamType ;
  44. }
  45. void
  46. CDhcpDefOptionDlg::DoDataExchange
  47. (
  48. CDataExchange* pDX
  49. )
  50. {
  51. CBaseDialog::DoDataExchange(pDX);
  52. //{{AFX_DATA_MAP(CDhcpDefOptionDlg)
  53. DDX_Control(pDX, IDC_STATIC_ID, m_static_id);
  54. DDX_Control(pDX, IDC_STATIC_DATATYPE, m_static_DataType);
  55. DDX_Control(pDX, IDC_CHECK_ARRAY, m_check_array);
  56. DDX_Control(pDX, IDC_EDIT_NAME, m_edit_name);
  57. DDX_Control(pDX, IDC_EDIT_TYPE_ID, m_edit_id);
  58. DDX_Control(pDX, IDC_EDIT_TYPE_COMMENT, m_edit_comment);
  59. DDX_Control(pDX, IDC_COMBO_DATA_TYPE, m_combo_data_type);
  60. //}}AFX_DATA_MAP
  61. }
  62. BEGIN_MESSAGE_MAP(CDhcpDefOptionDlg, CBaseDialog)
  63. //{{AFX_MSG_MAP(CDhcpDefOptionDlg)
  64. ON_WM_CREATE()
  65. ON_WM_DESTROY()
  66. ON_WM_KILLFOCUS()
  67. ON_WM_CLOSE()
  68. ON_CBN_SELCHANGE(IDC_COMBO_DATA_TYPE, OnSelchangeComboDataType)
  69. //}}AFX_MSG_MAP
  70. END_MESSAGE_MAP()
  71. /////////////////////////////////////////////////////////////////////////////
  72. // CDhcpDefOptionDlg message handlers
  73. int
  74. CDhcpDefOptionDlg::OnCreate
  75. (
  76. LPCREATESTRUCT lpCreateStruct
  77. )
  78. {
  79. if (CBaseDialog::OnCreate(lpCreateStruct) == -1)
  80. {
  81. return -1;
  82. }
  83. return 0;
  84. }
  85. void
  86. CDhcpDefOptionDlg::OnDestroy()
  87. {
  88. CBaseDialog::OnDestroy();
  89. }
  90. BOOL
  91. CDhcpDefOptionDlg::OnInitDialog()
  92. {
  93. CBaseDialog::OnInitDialog();
  94. DWORD err = 0 ;
  95. m_edit_name.LimitText( EDIT_STRING_MAX ) ;
  96. m_edit_id.LimitText( EDIT_ID_MAX ) ;
  97. m_edit_comment.LimitText( EDIT_STRING_MAX ) ;
  98. //
  99. // If this is an update of an existing type, don't allow editing
  100. // of the data type or id field.
  101. //
  102. if ( m_p_type_base )
  103. {
  104. m_edit_id.SetReadOnly() ;
  105. m_static_id.EnableWindow(FALSE);
  106. m_combo_data_type.EnableWindow( FALSE ) ;
  107. m_static_DataType.EnableWindow(FALSE);
  108. }
  109. CATCH_MEM_EXCEPTION
  110. {
  111. // update the vendor name info if necessary
  112. if (!m_strVendor.IsEmpty())
  113. GetDlgItem(IDC_STATIC_CLASS_NAME)->SetWindowText(m_strVendor);
  114. //
  115. // Load the data type names combo box
  116. // Set the dialog title properly.
  117. //
  118. CString strDataType ;
  119. CString strTitle ;
  120. strTitle.LoadString( m_p_type_base
  121. ? IDS_INFO_TITLE_CHANGE_OPT_TYPE
  122. : IDS_INFO_TITLE_ADD_OPTION_TYPES ) ;
  123. SetWindowText( strTitle ) ;
  124. for ( int iDataType = DhcpByteOption ;
  125. iDataType <= DhcpEncapsulatedDataOption ;
  126. iDataType++ )
  127. {
  128. strDataType.LoadString( IDS_INFO_TYPOPT_BYTE2 + iDataType ) ;
  129. int nIndex = m_combo_data_type.AddString( strDataType ) ;
  130. m_combo_data_type.SetItemData(nIndex, iDataType);
  131. }
  132. //
  133. // If this is "change" mode, create the working type by
  134. // copy-constructing the base option type object.
  135. //
  136. if ( m_p_type_base )
  137. {
  138. m_p_type = new CDhcpOption( *m_p_type_base ) ;
  139. //err = m_p_type->QueryError();
  140. //
  141. // Set the "array" checkbox state properly, but disable it.
  142. //
  143. m_check_array.SetCheck( m_p_type->IsArray() ? 1 : 0 ) ;
  144. m_check_array.EnableWindow( FALSE ) ;
  145. }
  146. }
  147. END_MEM_EXCEPTION( err )
  148. if ( err )
  149. {
  150. ::DhcpMessageBox( err ) ;
  151. EndDialog( -1 ) ;
  152. }
  153. else if ( m_p_type_base )
  154. {
  155. Set() ;
  156. }
  157. else
  158. {
  159. m_combo_data_type.SetCurSel(0) ;
  160. }
  161. return FALSE ; // return TRUE unless you set the focus to a control
  162. }
  163. void
  164. CDhcpDefOptionDlg::OnKillFocus
  165. (
  166. CWnd* pNewWnd
  167. )
  168. {
  169. CBaseDialog::OnKillFocus(pNewWnd);
  170. }
  171. void CDhcpDefOptionDlg::OnOK()
  172. {
  173. LONG err = m_p_type
  174. ? UpdateType()
  175. : AddType() ;
  176. //
  177. // Discard the OK click if there was an error.
  178. //
  179. if ( err == 0 )
  180. {
  181. CBaseDialog::OnOK();
  182. }
  183. else
  184. {
  185. ::DhcpMessageBox( err ) ;
  186. }
  187. }
  188. //
  189. // Set the data values for the controls based upon the current selection
  190. // in the combo box.
  191. //
  192. void
  193. CDhcpDefOptionDlg::Set()
  194. {
  195. if ( m_p_type == NULL )
  196. {
  197. return ;
  198. }
  199. DWORD err ;
  200. CATCH_MEM_EXCEPTION
  201. {
  202. //
  203. // Set the control values
  204. //
  205. CString strnumId;
  206. strnumId.Format(_T("%d"), m_p_type->QueryId() );
  207. CString strValue ;
  208. DHCP_OPTION_DATA_TYPE enType = m_p_type->QueryValue().QueryDataType() ;
  209. m_p_type->QueryValue().QueryDisplayString( strValue ) ;
  210. m_combo_data_type.SetCurSel( enType ) ;
  211. m_edit_name.SetWindowText( m_p_type->QueryName() ) ;
  212. m_edit_name.SetModify( FALSE ) ;
  213. m_edit_id.SetWindowText( strnumId ) ;
  214. m_edit_id.SetModify( FALSE ) ;
  215. m_edit_comment.SetWindowText( m_p_type->QueryComment() ) ;
  216. m_edit_comment.SetModify( FALSE ) ;
  217. }
  218. END_MEM_EXCEPTION(err)
  219. }
  220. DHCP_OPTION_DATA_TYPE
  221. CDhcpDefOptionDlg::QueryType() const
  222. {
  223. return (DHCP_OPTION_DATA_TYPE) m_combo_data_type.GetCurSel() ;
  224. }
  225. //
  226. // Update the displayed type based upon the current values of
  227. // the controls. Does nothing if the controls have not changed.
  228. // The Boolean parameter indicates that the user has requested an
  229. // update. This differentiates the other case where the controls
  230. // are dirty and the user has closed the dialog or changed primary
  231. // selection.
  232. //
  233. LONG
  234. CDhcpDefOptionDlg::UpdateType()
  235. {
  236. ASSERT( m_p_type != NULL ) ;
  237. //
  238. // If there isn't a current type object, return now.
  239. //
  240. if ( m_p_type == NULL )
  241. {
  242. return 0 ;
  243. }
  244. LONG err = 0 ;
  245. DHCP_OPTION_DATA_TYPE
  246. enType = m_p_type->QueryValue().QueryDataType(),
  247. enDlg = QueryType() ;
  248. CString str ;
  249. BOOL bChangedType = enType != enDlg,
  250. bChangedName = m_edit_name.GetModify() != 0,
  251. bChangedComment = m_edit_comment.GetModify() != 0,
  252. bChangedId = m_edit_id.GetModify() != 0,
  253. bChanged = bChangedType
  254. || bChangedName
  255. || bChangedComment
  256. || bChangedId ;
  257. CATCH_MEM_EXCEPTION
  258. {
  259. do
  260. {
  261. if ( ! bChanged )
  262. {
  263. break ;
  264. }
  265. if ( bChangedId && m_p_type_base )
  266. {
  267. //
  268. // Identifier of an existing option cannot be changed.
  269. //
  270. err = IDS_ERR_CANT_CHANGE_ID ;
  271. break ;
  272. }
  273. if ( bChangedType )
  274. {
  275. if ( err = m_p_type->QueryValue().SetDataType( enDlg ) )
  276. {
  277. break ;
  278. }
  279. }
  280. if ( bChangedName )
  281. {
  282. m_edit_name.GetWindowText( str ) ;
  283. m_p_type->SetName( str ) ;
  284. }
  285. if ( ::wcslen( m_p_type->QueryName() ) == 0 )
  286. {
  287. err = IDS_ERR_OPTION_NAME_REQUIRED ;
  288. break ;
  289. }
  290. if ( bChangedComment )
  291. {
  292. m_edit_comment.GetWindowText( str ) ;
  293. m_p_type->SetComment( str ) ;
  294. }
  295. }
  296. while ( FALSE ) ;
  297. }
  298. END_MEM_EXCEPTION(err)
  299. if ( bChanged && err == 0 )
  300. {
  301. m_p_type->SetDirty( TRUE ) ;
  302. }
  303. return err ;
  304. }
  305. LONG
  306. CDhcpDefOptionDlg::AddType()
  307. {
  308. ASSERT( m_p_type == NULL ) ;
  309. LONG err = 0 ;
  310. CDhcpOption * pdhcType = NULL ;
  311. TCHAR szT[32];
  312. DWORD dwId;
  313. CString strName, strComment ;
  314. DHCP_OPTION_TYPE dhcpOptType = m_check_array.GetCheck() & 1
  315. ? DhcpArrayTypeOption
  316. : DhcpUnaryElementTypeOption ;
  317. CATCH_MEM_EXCEPTION
  318. {
  319. do
  320. {
  321. m_edit_id.GetWindowText(szT, sizeof(szT)/sizeof(szT[0]));
  322. if (!FCvtAsciiToInteger(szT, OUT &dwId))
  323. {
  324. err = IDS_ERR_INVALID_NUMBER;
  325. m_edit_id.SetFocus();
  326. break;
  327. }
  328. ASSERT(dwId >= 0);
  329. if (( dwId < 2 ) ||
  330. ( dwId > 255))
  331. {
  332. err = IDS_ERR_INVALID_OPTION_ID;
  333. m_edit_id.SetFocus();
  334. break;
  335. }
  336. // only restrict options in the default vendor class
  337. if (m_strVendor.IsEmpty() &&
  338. (dwId == OPTION_DNS_REGISTATION) )
  339. {
  340. // this range is reserved
  341. err = IDS_ERR_RESERVED_OPTION_ID;
  342. m_edit_id.SetFocus();
  343. break;
  344. }
  345. if ( m_pol_types->FindId(dwId, m_strVendor.IsEmpty() ? NULL : (LPCTSTR) m_strVendor) )
  346. {
  347. err = IDS_ERR_ID_ALREADY_EXISTS ;
  348. break ;
  349. }
  350. m_edit_comment.GetWindowText( strComment ) ;
  351. m_edit_name.GetWindowText( strName ) ;
  352. if ( strName.GetLength() == 0 )
  353. {
  354. err = IDS_ERR_OPTION_NAME_REQUIRED ;
  355. break ;
  356. }
  357. pdhcType = new CDhcpOption( dwId,
  358. QueryType(),
  359. strName,
  360. strComment,
  361. dhcpOptType ) ;
  362. if ( pdhcType == NULL )
  363. {
  364. err = ERROR_NOT_ENOUGH_MEMORY ;
  365. break ;
  366. }
  367. pdhcType->SetVendor(m_strVendor);
  368. } while ( FALSE ) ;
  369. }
  370. END_MEM_EXCEPTION(err)
  371. if ( err )
  372. {
  373. delete pdhcType ;
  374. }
  375. else
  376. {
  377. m_p_type = pdhcType ;
  378. m_p_type->SetDirty() ;
  379. }
  380. return err ;
  381. }
  382. void
  383. CDhcpDefOptionDlg::OnClose()
  384. {
  385. CBaseDialog::OnClose();
  386. }
  387. void CDhcpDefOptionDlg::OnSelchangeComboDataType()
  388. {
  389. // presently the server doesn't support Encapsulated,
  390. // binary or string array options, so disable the array checkbox.
  391. BOOL bEnable = TRUE;
  392. int nCurSel = m_combo_data_type.GetCurSel();
  393. LRESULT nDataType = m_combo_data_type.GetItemData(nCurSel);
  394. if (nDataType == DhcpEncapsulatedDataOption ||
  395. nDataType == DhcpBinaryDataOption ) {
  396. bEnable = FALSE;
  397. }
  398. m_check_array.EnableWindow(bEnable);
  399. } // CDhcpDefOptionDlg::OnSelchangeComboDataType()