source: trunk/UHostAddressList.pas

Last change on this file was 1, checked in by george, 15 years ago
  • Initial import.
File size: 4.7 KB
Line 
1unit UHostAddressList;
2
3interface
4
5uses
6 Classes, URegistry, Windows, SysUtils, Xmldom, XMLIntf, msxmldom, XMLDoc;
7
8type
9 THostAddress = class
10 Address: string;
11 User: string;
12 Note: string;
13 Password: string;
14 KeepPassword: Boolean;
15 SecureMode: Boolean;
16 procedure LoadFromXmlNode(Node: IXMLNode);
17 procedure SaveToXmlNode(Node: IXMLNode);
18 end;
19
20 THostAddressList = class(TList)
21 procedure LoadFromXML(FileName: string);
22 procedure SaveToXML(FileName: string);
23 procedure LoadFromRegistry(Path: string);
24 procedure SaveToRegistry(Path: string);
25 procedure Clear; override;
26 end;
27
28implementation
29
30{ THostAddressList }
31
32procedure THostAddressList.Clear;
33var
34 I: Integer;
35begin
36 for I := 0 to Count - 1 do
37 THostAddress(Items[I]).Free;
38 inherited;
39end;
40
41procedure THostAddressList.LoadFromRegistry(Path: string);
42var
43 I: Integer;
44begin
45 with TRegistryEx.Create do try
46 RootKey := HKEY_CURRENT_USER;
47 OpenKey(Path, True);
48 Count := ReadIntegerWithDefault('Count', 0);
49 for I := 0 to Count - 1 do begin
50 OpenKey(Path + '\Host' + IntToStr(I), True);
51 Items[I] := THostAddress.Create;
52 with THostAddress(Items[I]) do begin
53 Address := ReadStringWithDefault('Address', '');
54 User := ReadStringWithDefault('User', 'admin');
55 Note := ReadStringWithDefault('Note', '');
56 Password := ReadStringWithDefault('Password', '');
57 KeepPassword := ReadBoolWithDefault('KeepPassword', False);
58 SecureMode := ReadBoolWithDefault('SecureMode', True);
59 end;
60 end;
61 finally
62 Free;
63 end;
64end;
65
66procedure THostAddressList.LoadFromXML(FileName: string);
67var
68 XmlDocument: IXMLDocument;
69 I: Integer;
70 NewHostAddress: THostAddress;
71begin
72 if FileExists(FileName) then
73 try
74 XmlDocument := LoadXMLDocument(Filename);
75 XmlDocument.Active := True;
76 with XmlDocument do with DocumentElement do begin
77 with ChildNodes.Nodes['HostAddressList'] do
78 for I := 0 to ChildNodes.Count - 1 do begin
79 if ChildNodes.Nodes[I].NodeName = 'HostAddress' then begin
80 NewHostAddress := THostAddress.Create;
81 Add(NewHostAddress);
82 NewHostAddress.LoadFromXmlNode(ChildNodes.Nodes[I]);
83 end;
84 end;
85 end;
86 finally
87 XmlDocument := nil;
88 end;
89end;
90
91procedure THostAddressList.SaveToRegistry(Path: string);
92var
93 I: Integer;
94begin
95 with TRegistryEx.Create do try
96 RootKey := HKEY_CURRENT_USER;
97 OpenKey(Path, True);
98 WriteInteger('Count', Count);
99 for I := 0 to Count - 1 do begin
100 OpenKey(Path + '\Host' + IntToStr(I), True);
101 with THostAddress(Items[I]) do begin
102 WriteString('Address', Address);
103 WriteString('User', User);
104 WriteString('Note', Note);
105 if KeepPassword then WriteString('Password', Password)
106 else WriteString('Password', '');
107 WriteBool('KeepPassword', KeepPassword);
108 WriteBool('SecureMode', SecureMode);
109 end;
110 end;
111 finally
112 Free;
113 end;
114end;
115
116procedure THostAddressList.SaveToXML(FileName: string);
117var
118 XmlDocument: TXMLDocument;
119 NewNode: IXMLNode;
120 HostAddressList: IXMLNode;
121 I: Integer;
122begin
123 XmlDocument := TXMLDocument.Create(nil);
124 with XmlDocument do begin
125 Options := [doNodeAutoIndent, doNodeAutoCreate, doAttrNull, doAutoPrefix,
126 doNameSpaceDecl];
127 DOMVendor := DOMVendors.Find('Open XML');
128 Active := True;
129 NewNode := AddChild('Document');
130 with NewNode do begin
131 HostAddressList := AddChild('HostAddressList');
132 for I := 0 to Count - 1 do
133 THostAddress(Items[I]).SaveToXmlNode(HostAddressList);
134 end;
135 end;
136 XmlDocument.Version := '1.0';
137 XmlDocument.Encoding := 'utf-8';
138 XmlDocument.SaveToFile(FileName);
139end;
140
141{ THostAddress }
142
143procedure THostAddress.LoadFromXmlNode(Node: IXMLNode);
144begin
145 with Node do begin
146 Address := ChildNodes.Nodes['Address'].Text;
147 User := ChildNodes.Nodes['User'].Text;
148 Password := ChildNodes.Nodes['Password'].Text;
149 Note := ChildNodes.Nodes['Node'].Text;
150 KeepPassword := StrToBool(ChildNodes.Nodes['KeepPassword'].Text);
151 SecureMode := StrToBool(ChildNodes.Nodes['SecureMode'].Text);
152 end;
153end;
154
155procedure THostAddress.SaveToXmlNode(Node: IXMLNode);
156begin
157 Node := Node.AddChild('HostAddress');
158 with Node do begin
159 AddChild('Address').Text := Address;
160 AddChild('User').Text := User;
161 AddChild('Password').Text := Password;
162 AddChild('Note').Text := Note;
163 AddChild('KeepPassword').Text := BoolToStr(KeepPassword);
164 AddChild('SecureMode').Text := BoolToStr(SecureMode);
165 end;
166end;
167
168end.
Note: See TracBrowser for help on using the repository browser.