IdDsnPropEdBindingNET.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. unit IdDsnPropEdBindingNET;
  2. interface
  3. uses
  4. Classes,
  5. System.Drawing, System.Collections, System.ComponentModel,
  6. System.Windows.Forms, System.Data, IdSocketHandle;
  7. type
  8. TIdDsnPropEdBindingNET = class(System.Windows.Forms.Form)
  9. {$REGION 'Designer Managed Code'}
  10. strict private
  11. /// <summary>
  12. /// Required designer variable.
  13. /// </summary>
  14. Components: System.ComponentModel.Container;
  15. btnOk: System.Windows.Forms.Button;
  16. btnCancel: System.Windows.Forms.Button;
  17. lblBindings: System.Windows.Forms.Label;
  18. lbBindings: System.Windows.Forms.ListBox;
  19. btnNew: System.Windows.Forms.Button;
  20. btnDelete: System.Windows.Forms.Button;
  21. lblIPAddress: System.Windows.Forms.Label;
  22. edtIPAddress: System.Windows.Forms.ComboBox;
  23. lblPort: System.Windows.Forms.Label;
  24. edtPort: System.Windows.Forms.NumericUpDown;
  25. cboIPVersion: System.Windows.Forms.ComboBox;
  26. lblIPVersion: System.Windows.Forms.Label;
  27. /// <summary>
  28. /// Required method for Designer support - do not modify
  29. /// the contents of this method with the code editor.
  30. /// </summary>
  31. procedure InitializeComponent;
  32. procedure btnNew_Click(sender: System.Object; e: System.EventArgs);
  33. procedure btnDelete_Click(sender: System.Object; e: System.EventArgs);
  34. procedure edtPort_ValueChanged(sender: System.Object; e: System.EventArgs);
  35. procedure edtIPAddress_SelectedValueChanged(sender: System.Object; e: System.EventArgs);
  36. procedure cboIPVersion_SelectedValueChanged(sender: System.Object; e: System.EventArgs);
  37. procedure lbBindings_SelectedValueChanged(sender: System.Object; e: System.EventArgs);
  38. {$ENDREGION}
  39. strict protected
  40. /// <summary>
  41. /// Clean up any resources being used.
  42. /// </summary>
  43. procedure Dispose(Disposing: Boolean); override;
  44. private
  45. FHandles : TIdSocketHandles;
  46. FDefaultPort : Integer;
  47. FIPv4Addresses : TStrings;
  48. FIPv6Addresses : TStrings;
  49. FCurrentHandle : TIdSocketHandle;
  50. { Private Declarations }
  51. procedure SetHandles(const Value: TIdSocketHandles);
  52. procedure SetIPv4Addresses(const Value: TStrings);
  53. procedure SetIPv6Addresses(const Value: TStrings);
  54. procedure UpdateBindingList;
  55. procedure UpdateEditControls;
  56. procedure FillComboBox(ACombo : System.Windows.Forms.ComboBox; AStrings :TStrings);
  57. procedure SetCaption(const AValue : String);
  58. function GetCaption : String;
  59. public
  60. constructor Create;
  61. function Execute : Boolean;
  62. function GetList: string;
  63. procedure SetList(const AList: string);
  64. property Handles : TIdSocketHandles read FHandles write SetHandles;
  65. property DefaultPort : Integer read FDefaultPort write FDefaultPort;
  66. property IPv4Addresses : TStrings read FIPv4Addresses write SetIPv4Addresses;
  67. property IPv6Addresses : TStrings read FIPv6Addresses write SetIPv6Addresses;
  68. property Caption : String read GetCaption write SetCaption;
  69. end;
  70. [assembly: RuntimeRequiredAttribute(TypeOf(TIdDsnPropEdBindingNET))]
  71. procedure FillHandleList(const AList: string; ADest: TIdSocketHandles);
  72. function GetListValues(const ASocketHandles : TIdSocketHandles) : String;
  73. implementation
  74. uses
  75. IdGlobal,
  76. IdIPAddress,
  77. IdDsnCoreResourceStrings, IdStack, SysUtils;
  78. const
  79. IPv6Wildcard1 = '::'; {do not localize}
  80. IPv6Wildcard2 = '0:0:0:0:0:0:0:0'; {do not localize}
  81. IPv6Loopback = '::1'; {do not localize}
  82. IPv4Wildcard = '0.0.0.0'; {do not localize}
  83. IPv4Loopback = '127.0.0.1'; {do not localize}
  84. function IsValidIP(const AAddr : String): Boolean;
  85. var
  86. LIP: TIdIPAddress;
  87. begin
  88. LIP := TIdIPAddress.MakeAddressObject(AAddr);
  89. Result := Assigned(LIP);
  90. if Result then
  91. begin
  92. FreeAndNil(LIP);
  93. end;
  94. end;
  95. function StripAndSymbol(s : String) : String;
  96. begin
  97. Result := '';
  98. repeat
  99. if s='' then
  100. begin
  101. Break;
  102. end;
  103. Result := Result + Fetch(s,'&');
  104. until False;
  105. end;
  106. procedure FillHandleList(const AList: string; ADest: TIdSocketHandles);
  107. var
  108. LItems: TStringList;
  109. i: integer;
  110. LIPVersion: TIdIPVersion;
  111. LAddr, LText: string;
  112. LPort: integer;
  113. begin
  114. ADest.BeginUpdate;
  115. try
  116. ADest.Clear;
  117. LItems := TStringList.Create;
  118. try
  119. LItems.CommaText := AList;
  120. for i := 0 to LItems.Count-1 do begin
  121. if Length(LItems[i]) > 0 then begin
  122. if TextStartsWith(LItems[i], '[') then begin
  123. // ipv6
  124. LIPVersion := Id_IPv6;
  125. LText := Copy(LItems[i], 2, MaxInt);
  126. LAddr := Fetch(LText, ']:');
  127. LPort := IndyStrToInt(LText, -1);
  128. end else begin
  129. // ipv4
  130. LIPVersion := Id_IPv4;
  131. LText := LItems[i];
  132. LAddr := Fetch(LText, ':');
  133. LPort := IndyStrToInt(LText, -1);
  134. //Note that 0 is legal and indicates the server binds to a random port
  135. end;
  136. if IsValidIP(LAddr) and (LPort > -1) and (LPort < 65536) then begin
  137. with ADest.Add do begin
  138. IPVersion := LIPVersion;
  139. IP := LAddr;
  140. Port := LPort;
  141. end;
  142. end;
  143. end;
  144. end;
  145. finally
  146. LItems.Free;
  147. end;
  148. finally
  149. ADest.EndUpdate;
  150. end;
  151. end;
  152. function NumericOnly(const AText : String) : String;
  153. var
  154. i: Integer;
  155. begin
  156. Result := '';
  157. for i := 1 to Length(AText) do
  158. begin
  159. if IsNumeric(AText[i]) then
  160. begin
  161. Result := Result + AText[i];
  162. end
  163. else
  164. begin
  165. Break;
  166. end;
  167. end;
  168. if (Length(Result) = 0) then
  169. begin
  170. Result := '0';
  171. end;
  172. end;
  173. function IndexOfNo(const ANo : Integer; AItems : System.Windows.Forms.ComboBox.ObjectCollection) : Integer;
  174. begin
  175. for Result := 0 to AItems.Count -1 do
  176. begin
  177. if ANo = IndyStrToInt( NumericOnly(AItems[Result].ToString )) then
  178. begin
  179. Exit;
  180. end;
  181. end;
  182. Result := -1;
  183. end;
  184. function GetDisplayString(ASocketHandle: TIdSocketHandle): string;
  185. begin
  186. Result := '';
  187. case ASocketHandle.IPVersion of
  188. Id_IPv4 : Result := IndyFormat('%s:%d', [ASocketHandle.IP, ASocketHandle.Port]);
  189. Id_IPv6 : Result := IndyFormat('[%s]:%d', [ASocketHandle.IP, ASocketHandle.Port]);
  190. end;
  191. end;
  192. function GetListValues(const ASocketHandles : TIdSocketHandles) : String;
  193. var
  194. i: Integer;
  195. begin
  196. Result := '';
  197. for i := 0 to ASocketHandles.Count -1 do begin
  198. Result := Result + ',' + GetDisplayString(ASocketHandles[i]);
  199. end;
  200. Delete(Result,1,1);
  201. end;
  202. {$AUTOBOX ON}
  203. {$REGION 'Windows Form Designer generated code'}
  204. /// <summary>
  205. /// Required method for Designer support -- do not modify
  206. /// the contents of this method with the code editor.
  207. /// </summary>
  208. procedure TIdDsnPropEdBindingNET.InitializeComponent;
  209. type
  210. TArrayOfInteger = array of Integer;
  211. begin
  212. Self.btnOk := System.Windows.Forms.Button.Create;
  213. Self.btnCancel := System.Windows.Forms.Button.Create;
  214. Self.lblBindings := System.Windows.Forms.Label.Create;
  215. Self.lbBindings := System.Windows.Forms.ListBox.Create;
  216. Self.btnNew := System.Windows.Forms.Button.Create;
  217. Self.btnDelete := System.Windows.Forms.Button.Create;
  218. Self.lblIPAddress := System.Windows.Forms.Label.Create;
  219. Self.edtIPAddress := System.Windows.Forms.ComboBox.Create;
  220. Self.lblPort := System.Windows.Forms.Label.Create;
  221. Self.edtPort := System.Windows.Forms.NumericUpDown.Create;
  222. Self.cboIPVersion := System.Windows.Forms.ComboBox.Create;
  223. Self.lblIPVersion := System.Windows.Forms.Label.Create;
  224. (System.ComponentModel.ISupportInitialize(Self.edtPort)).BeginInit;
  225. Self.SuspendLayout;
  226. //
  227. // btnOk
  228. //
  229. Self.btnOk.Anchor := (System.Windows.Forms.AnchorStyles((System.Windows.Forms.AnchorStyles.Bottom
  230. or System.Windows.Forms.AnchorStyles.Right)));
  231. Self.btnOk.DialogResult := System.Windows.Forms.DialogResult.OK;
  232. Self.btnOk.Location := System.Drawing.Point.Create(312, 160);
  233. Self.btnOk.Name := 'btnOk';
  234. Self.btnOk.TabIndex := 0;
  235. //
  236. // btnCancel
  237. //
  238. Self.btnCancel.Anchor := (System.Windows.Forms.AnchorStyles((System.Windows.Forms.AnchorStyles.Bottom
  239. or System.Windows.Forms.AnchorStyles.Right)));
  240. Self.btnCancel.DialogResult := System.Windows.Forms.DialogResult.Cancel;
  241. Self.btnCancel.Location := System.Drawing.Point.Create(392, 160);
  242. Self.btnCancel.Name := 'btnCancel';
  243. Self.btnCancel.TabIndex := 1;
  244. //
  245. // lblBindings
  246. //
  247. Self.lblBindings.AutoSize := True;
  248. Self.lblBindings.Location := System.Drawing.Point.Create(8, 8);
  249. Self.lblBindings.Name := 'lblBindings';
  250. Self.lblBindings.Size := System.Drawing.Size.Create(42, 16);
  251. Self.lblBindings.TabIndex := 2;
  252. Self.lblBindings.Text := '&Binding';
  253. //
  254. // lbBindings
  255. //
  256. Self.lbBindings.Anchor := (System.Windows.Forms.AnchorStyles(((System.Windows.Forms.AnchorStyles.Top
  257. or System.Windows.Forms.AnchorStyles.Bottom) or System.Windows.Forms.AnchorStyles.Left)));
  258. Self.lbBindings.Location := System.Drawing.Point.Create(8, 24);
  259. Self.lbBindings.Name := 'lbBindings';
  260. Self.lbBindings.Size := System.Drawing.Size.Create(137, 121);
  261. Self.lbBindings.TabIndex := 3;
  262. Include(Self.lbBindings.SelectedValueChanged, Self.lbBindings_SelectedValueChanged);
  263. //
  264. // btnNew
  265. //
  266. Self.btnNew.Location := System.Drawing.Point.Create(152, 56);
  267. Self.btnNew.Name := 'btnNew';
  268. Self.btnNew.TabIndex := 4;
  269. Include(Self.btnNew.Click, Self.btnNew_Click);
  270. //
  271. // btnDelete
  272. //
  273. Self.btnDelete.Location := System.Drawing.Point.Create(152, 88);
  274. Self.btnDelete.Name := 'btnDelete';
  275. Self.btnDelete.TabIndex := 5;
  276. Include(Self.btnDelete.Click, Self.btnDelete_Click);
  277. //
  278. // lblIPAddress
  279. //
  280. Self.lblIPAddress.Location := System.Drawing.Point.Create(240, 8);
  281. Self.lblIPAddress.Name := 'lblIPAddress';
  282. Self.lblIPAddress.Size := System.Drawing.Size.Create(100, 16);
  283. Self.lblIPAddress.TabIndex := 6;
  284. Self.lblIPAddress.Text := 'Label1';
  285. //
  286. // edtIPAddress
  287. //
  288. Self.edtIPAddress.Anchor := (System.Windows.Forms.AnchorStyles(((System.Windows.Forms.AnchorStyles.Top
  289. or System.Windows.Forms.AnchorStyles.Left) or System.Windows.Forms.AnchorStyles.Right)));
  290. Self.edtIPAddress.Location := System.Drawing.Point.Create(240, 24);
  291. Self.edtIPAddress.Name := 'edtIPAddress';
  292. Self.edtIPAddress.Size := System.Drawing.Size.Create(224, 21);
  293. Self.edtIPAddress.TabIndex := 7;
  294. Include(Self.edtIPAddress.SelectedValueChanged, Self.edtIPAddress_SelectedValueChanged);
  295. //
  296. // lblPort
  297. //
  298. Self.lblPort.Location := System.Drawing.Point.Create(240, 58);
  299. Self.lblPort.Name := 'lblPort';
  300. Self.lblPort.Size := System.Drawing.Size.Create(100, 16);
  301. Self.lblPort.TabIndex := 8;
  302. Self.lblPort.Text := 'Label1';
  303. //
  304. // edtPort
  305. //
  306. Self.edtPort.Anchor := (System.Windows.Forms.AnchorStyles(((System.Windows.Forms.AnchorStyles.Top
  307. or System.Windows.Forms.AnchorStyles.Left) or System.Windows.Forms.AnchorStyles.Right)));
  308. Self.edtPort.Location := System.Drawing.Point.Create(240, 74);
  309. Self.edtPort.Maximum := System.Decimal.Create(TArrayOfInteger.Create(65535,
  310. 0, 0, 0));
  311. Self.edtPort.Name := 'edtPort';
  312. Self.edtPort.Size := System.Drawing.Size.Create(224, 20);
  313. Self.edtPort.TabIndex := 9;
  314. Include(Self.edtPort.ValueChanged, Self.edtPort_ValueChanged);
  315. //
  316. // cboIPVersion
  317. //
  318. Self.cboIPVersion.DropDownStyle := System.Windows.Forms.ComboBoxStyle.DropDownList;
  319. Self.cboIPVersion.Location := System.Drawing.Point.Create(240, 124);
  320. Self.cboIPVersion.Name := 'cboIPVersion';
  321. Self.cboIPVersion.Size := System.Drawing.Size.Create(224, 21);
  322. Self.cboIPVersion.TabIndex := 10;
  323. Include(Self.cboIPVersion.SelectedValueChanged, Self.cboIPVersion_SelectedValueChanged);
  324. //
  325. // lblIPVersion
  326. //
  327. Self.lblIPVersion.Location := System.Drawing.Point.Create(240, 108);
  328. Self.lblIPVersion.Name := 'lblIPVersion';
  329. Self.lblIPVersion.Size := System.Drawing.Size.Create(100, 16);
  330. Self.lblIPVersion.TabIndex := 11;
  331. Self.lblIPVersion.Text := 'Label1';
  332. //
  333. // TIdDsnPropEdBindingNET
  334. //
  335. Self.AcceptButton := Self.btnOk;
  336. Self.AutoScaleBaseSize := System.Drawing.Size.Create(5, 13);
  337. Self.CancelButton := Self.btnCancel;
  338. Self.ClientSize := System.Drawing.Size.Create(470, 189);
  339. Self.Controls.Add(Self.lblIPVersion);
  340. Self.Controls.Add(Self.cboIPVersion);
  341. Self.Controls.Add(Self.edtPort);
  342. Self.Controls.Add(Self.lblPort);
  343. Self.Controls.Add(Self.edtIPAddress);
  344. Self.Controls.Add(Self.lblIPAddress);
  345. Self.Controls.Add(Self.btnDelete);
  346. Self.Controls.Add(Self.btnNew);
  347. Self.Controls.Add(Self.lbBindings);
  348. Self.Controls.Add(Self.lblBindings);
  349. Self.Controls.Add(Self.btnCancel);
  350. Self.Controls.Add(Self.btnOk);
  351. Self.FormBorderStyle := System.Windows.Forms.FormBorderStyle.FixedDialog;
  352. Self.MaximizeBox := False;
  353. Self.MaximumSize := System.Drawing.Size.Create(480, 225);
  354. Self.MinimizeBox := False;
  355. Self.MinimumSize := System.Drawing.Size.Create(480, 225);
  356. Self.Name := 'TIdDsnPropEdBindingNET';
  357. Self.ShowInTaskbar := False;
  358. Self.StartPosition := System.Windows.Forms.FormStartPosition.CenterScreen;
  359. Self.Text := 'WinForm';
  360. (System.ComponentModel.ISupportInitialize(Self.edtPort)).EndInit;
  361. Self.ResumeLayout(False);
  362. end;
  363. {$ENDREGION}
  364. procedure TIdDsnPropEdBindingNET.Dispose(Disposing: Boolean);
  365. begin
  366. if Disposing then
  367. begin
  368. if Components <> nil then
  369. begin
  370. Components.Dispose();
  371. FreeAndNil(FHandles);
  372. FreeAndNil(FIPv4Addresses);
  373. FreeAndNil(FIPv6Addresses);
  374. //don't free FCurrentHandle; - it's in the handles collection
  375. TIdStack.DecUsage;
  376. end;
  377. end;
  378. inherited Dispose(Disposing);
  379. end;
  380. constructor TIdDsnPropEdBindingNET.Create;
  381. var
  382. i: Integer;
  383. LLocalAddresses: TIdStackLocalAddressList;
  384. begin
  385. inherited Create;
  386. //
  387. // Required for Windows Form Designer support
  388. //
  389. InitializeComponent;
  390. //
  391. // TODO: Add any constructor code after InitializeComponent call
  392. //
  393. FHandles := TIdSocketHandles.Create(nil);
  394. FIPv4Addresses := TStringList.Create;
  395. FIPv6Addresses := TStringList.Create;
  396. SetIPv4Addresses(nil);
  397. SetIPv6Addresses(nil);
  398. TIdStack.IncUsage;
  399. try
  400. LLocalAddresses := TIdStackLocalAddressList.Create;
  401. try
  402. GStack.GetLocalAddressList(LLocalAddresses);
  403. for i := 0 to LLocalAddresses.Count-1 do
  404. begin
  405. case LLocalAddresses[i].IPVersion of
  406. Id_IPv4: FIPv4Addresses.Add(LLocalAddresses[i].IPAddress);
  407. Id_IPv6: FIPv6Addresses.Add(LLocalAddresses[i].IPAddress);
  408. end;
  409. end;
  410. finally
  411. LLocalAddresses.Free;
  412. end;
  413. finally
  414. TIdStack.DecUsage;
  415. end;
  416. UpdateEditControls;
  417. //captions
  418. btnNew.Text := RSBindingNewCaption;
  419. btnDelete.Text := RSBindingDeleteCaption;
  420. lblIPAddress.Text := RSBindingHostnameLabel;
  421. lblPort.Text := RSBindingPortLabel;
  422. lblIPVersion.Text := RSBindingIPVerLabel;
  423. btnOk.Text := RSOk;
  424. btnCancel.Text := RSCancel;
  425. //IPVersion choices
  426. //we yhave to strip out the & symbol. In Win32, we use this
  427. //in a radio-box so a user could select by pressingg the 4 or 6
  428. //key. For this, we don't have a radio box and I'm too lazy
  429. //to use two Radio Buttons.
  430. cboIPVersion.Items.Add(StripAndSymbol(RSBindingIPV4Item));
  431. cboIPVersion.Items.Add(StripAndSymbol(RSBindingIPV6Item));
  432. end;
  433. procedure TIdDsnPropEdBindingNET.SetHandles(const Value: TIdSocketHandles);
  434. begin
  435. FHandles.Assign(Value);
  436. UpdateBindingList;
  437. end;
  438. function TIdDsnPropEdBindingNET.GetList: string;
  439. begin
  440. Result := GetListValues(Handles);
  441. end;
  442. procedure TIdDsnPropEdBindingNET.SetIPv6Addresses(const Value: TStrings);
  443. begin
  444. if Assigned(Value) then begin
  445. FIPv6Addresses.Assign(Value);
  446. end;
  447. // Ensure that these two are always present
  448. if FIPv6Addresses.IndexOf(IPv6Loopback) = -1 then begin
  449. FIPv6Addresses.Insert(0, IPv6Loopback);
  450. end;
  451. if FIPv6Addresses.IndexOf(IPv6Wildcard1) = -1 then begin
  452. FIPv6Addresses.Insert(0, IPv6Wildcard1);
  453. end;
  454. end;
  455. procedure TIdDsnPropEdBindingNET.SetIPv4Addresses(const Value: TStrings);
  456. begin
  457. if Assigned(Value) then begin
  458. FIPv4Addresses.Assign(Value);
  459. end;
  460. // Ensure that these two are always present
  461. if FIPv4Addresses.IndexOf(IPv6Loopback) = -1 then begin
  462. FIPv4Addresses.Insert(0, IPv4Loopback);
  463. end;
  464. if FIPv4Addresses.IndexOf(IPv4Wildcard) = -1 then begin
  465. FIPv4Addresses.Insert(0, IPv4Wildcard);
  466. end;
  467. end;
  468. procedure TIdDsnPropEdBindingNET.SetList(const AList: string);
  469. begin
  470. FCurrentHandle := nil;
  471. FillHandleList(AList, Handles);
  472. UpdateBindingList;
  473. UpdateEditControls;
  474. end;
  475. procedure TIdDsnPropEdBindingNET.lbBindings_SelectedValueChanged(sender: System.Object;
  476. e: System.EventArgs);
  477. begin
  478. if lbBindings.SelectedIndex >= 0 then begin
  479. btnDelete.Enabled := True;
  480. FCurrentHandle := FHandles[lbBindings.SelectedIndex];
  481. end else begin
  482. btnDelete.Enabled := False;
  483. FCurrentHandle := nil;
  484. end;
  485. UpdateEditControls;
  486. end;
  487. procedure TIdDsnPropEdBindingNET.cboIPVersion_SelectedValueChanged(sender: System.Object;
  488. e: System.EventArgs);
  489. begin
  490. case cboIPVersion.SelectedIndex of
  491. 0 :
  492. begin
  493. if FCurrentHandle.IPVersion <> Id_IPv4 then
  494. begin
  495. FCurrentHandle.IPVersion := Id_IPv4;
  496. FillComboBox(edtIPAddress,FIPv4Addresses);
  497. FCurrentHandle.IP := IPv4Wildcard;
  498. end;
  499. end;
  500. 1 :
  501. begin
  502. if FCurrentHandle.IPVersion <> Id_IPv6 then
  503. begin
  504. FCurrentHandle.IPVersion := Id_IPv6;
  505. FillComboBox(edtIPAddress,FIPv6Addresses);
  506. FCurrentHandle.IP := IPv6Wildcard1;
  507. end;
  508. end;
  509. end;
  510. UpdateEditControls;
  511. UpdateBindingList;
  512. end;
  513. procedure TIdDsnPropEdBindingNET.edtIPAddress_SelectedValueChanged(sender: System.Object;
  514. e: System.EventArgs);
  515. begin
  516. FCurrentHandle.IP := edtIPAddress.SelectedItem.ToString;
  517. UpdateBindingList;
  518. end;
  519. procedure TIdDsnPropEdBindingNET.edtPort_ValueChanged(sender: System.Object;
  520. e: System.EventArgs);
  521. begin
  522. if Assigned(FCurrentHandle) then begin
  523. FCurrentHandle.Port := edtPort.Value.ToInt16(edtPort.Value);
  524. end;
  525. UpdateBindingList;
  526. end;
  527. procedure TIdDsnPropEdBindingNET.btnDelete_Click(sender: System.Object; e: System.EventArgs);
  528. var LSH : TIdSocketHandle;
  529. i : Integer;
  530. begin
  531. if lbBindings.SelectedIndex >= 0 then
  532. begin
  533. // Delete is not available in D4's collection classes
  534. // This should work just as well.
  535. i := lbBindings.get_SelectedIndex;
  536. LSH := Handles[i];
  537. FreeAndNil(LSH);
  538. lbBindings.Items.Remove(i);
  539. FCurrentHandle := nil;
  540. UpdateBindingList;
  541. end;
  542. lbBindings_SelectedValueChanged(nil, nil);
  543. UpdateEditControls;
  544. end;
  545. procedure TIdDsnPropEdBindingNET.btnNew_Click(sender: System.Object; e: System.EventArgs);
  546. begin
  547. FCurrentHandle := FHandles.Add;
  548. case FCurrentHandle.IPVersion of
  549. Id_IPv4: FCurrentHandle.IP := IPv4Wildcard;
  550. Id_IPv6: FCurrentHandle.IP := IPv6Wildcard1;
  551. end;
  552. FCurrentHandle.Port := FDefaultPort;
  553. UpdateBindingList;
  554. FillComboBox(edtIPAddress, FIPv4Addresses);
  555. UpdateEditControls;
  556. end;
  557. procedure TIdDsnPropEdBindingNET.UpdateBindingList;
  558. var
  559. i: integer;
  560. selected: integer;
  561. s: string;
  562. begin
  563. selected := lbBindings.SelectedIndex;
  564. lbBindings.BeginUpdate;
  565. try
  566. if lbBindings.Items.Count = FHandles.Count then begin
  567. for i := 0 to FHandles.Count - 1 do begin
  568. s := GetDisplayString(FHandles[i]);
  569. if s <> lbBindings.Items[i].ToString then begin
  570. lbBindings.Items[i] := s;
  571. end;
  572. end;
  573. end else begin
  574. lbBindings.Items.Clear;
  575. for i := 0 to FHandles.Count-1 do begin
  576. lbBindings.Items.Add(GetDisplayString(FHandles[i]));
  577. end;
  578. end;
  579. finally
  580. lbBindings.EndUpdate;
  581. if Assigned(FCurrentHandle) then begin
  582. lbBindings.SelectedIndex := FCurrentHandle.Index;
  583. end else begin
  584. lbBindings.SelectedIndex := IndyMin(selected, lbBindings.Items.Count-1);
  585. end;
  586. end;
  587. { selected := lbBindings.SelectedItem;
  588. lbBindings.Items.BeginUpdate;
  589. try
  590. if lbBindings.Items.Count = FHandles.Count then begin
  591. for i := 0 to FHandles.Count - 1 do begin
  592. s := GetDisplayString(FHandles[i]);
  593. if s <> lbBindings.Items[i] then begin
  594. lbBindings.Items[i] := s;
  595. end;
  596. end;
  597. end else begin
  598. lbBindings.Items.Clear;
  599. for i := 0 to FHandles.Count-1 do begin
  600. lbBindings.Items.Add(GetDisplayString(FHandles[i]));
  601. end;
  602. end;
  603. finally
  604. lbBindings.Items.EndUpdate;
  605. if Assigned(FCurrentHandle) then begin
  606. lbBindings.ItemIndex := FCurrentHandle.Index;
  607. end else begin
  608. lbBindings.ItemIndex := IndyMin(selected, lbBindings.Items.Count-1);
  609. end;
  610. end; }
  611. end;
  612. procedure TIdDsnPropEdBindingNET.UpdateEditControls;
  613. begin
  614. if Assigned(FCurrentHandle) then
  615. begin
  616. edtPort.Text := '';
  617. edtPort.Value := FCurrentHandle.Port;
  618. case FCurrentHandle.IPVersion of
  619. Id_IPv4 :
  620. begin
  621. FillComboBox(edtIPAddress, FIPv4Addresses);
  622. edtIPAddress.SelectedItem := edtIPAddress.Items[0];
  623. cboIPVersion.SelectedItem := cboIPVersion.Items[0];
  624. end;
  625. Id_IPv6 :
  626. begin
  627. FillComboBox(edtIPAddress, FIPv6Addresses);
  628. edtIPAddress.SelectedItem := edtIPAddress.Items[0];
  629. cboIPVersion.SelectedItem := cboIPVersion.Items[1];
  630. end;
  631. end;
  632. if edtIPAddress.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown then begin
  633. edtIPAddress.Text := FCurrentHandle.IP;
  634. end else begin
  635. edtIPAddress.SelectedIndex := edtIPAddress.Items.IndexOf(FCurrentHandle.IP);
  636. end;
  637. end;
  638. lblIPAddress.Enabled := Assigned(FCurrentHandle);
  639. edtIPAddress.Enabled := Assigned(FCurrentHandle);
  640. lblPort.Enabled := Assigned(FCurrentHandle);
  641. edtPort.Enabled := Assigned(FCurrentHandle);
  642. lblIPVersion.Enabled := Assigned(FCurrentHandle);
  643. cboIPVersion.Enabled := Assigned(FCurrentHandle);
  644. end;
  645. procedure TIdDsnPropEdBindingNET.FillComboBox(
  646. ACombo: System.Windows.Forms.ComboBox; AStrings: TStrings);
  647. var
  648. i : Integer;
  649. begin
  650. ACombo.Items.Clear;
  651. for i := 0 to AStrings.Count-1 do begin
  652. ACombo.Items.Add(AStrings[i]);
  653. end;
  654. end;
  655. function TIdDsnPropEdBindingNET.Execute: Boolean;
  656. begin
  657. Result := Self.ShowDialog = System.Windows.Forms.DialogResult.OK;
  658. end;
  659. function TIdDsnPropEdBindingNET.GetCaption: String;
  660. begin
  661. Result := Text;
  662. end;
  663. procedure TIdDsnPropEdBindingNET.SetCaption(const AValue: String);
  664. begin
  665. Text := AValue;
  666. end;
  667. end.