1 | unit UHostAddressList;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, URegistry, Windows, SysUtils, Xmldom, XMLIntf, msxmldom, XMLDoc;
|
---|
7 |
|
---|
8 | type
|
---|
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 |
|
---|
28 | implementation
|
---|
29 |
|
---|
30 | { THostAddressList }
|
---|
31 |
|
---|
32 | procedure THostAddressList.Clear;
|
---|
33 | var
|
---|
34 | I: Integer;
|
---|
35 | begin
|
---|
36 | for I := 0 to Count - 1 do
|
---|
37 | THostAddress(Items[I]).Free;
|
---|
38 | inherited;
|
---|
39 | end;
|
---|
40 |
|
---|
41 | procedure THostAddressList.LoadFromRegistry(Path: string);
|
---|
42 | var
|
---|
43 | I: Integer;
|
---|
44 | begin
|
---|
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;
|
---|
64 | end;
|
---|
65 |
|
---|
66 | procedure THostAddressList.LoadFromXML(FileName: string);
|
---|
67 | var
|
---|
68 | XmlDocument: IXMLDocument;
|
---|
69 | I: Integer;
|
---|
70 | NewHostAddress: THostAddress;
|
---|
71 | begin
|
---|
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;
|
---|
89 | end;
|
---|
90 |
|
---|
91 | procedure THostAddressList.SaveToRegistry(Path: string);
|
---|
92 | var
|
---|
93 | I: Integer;
|
---|
94 | begin
|
---|
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;
|
---|
114 | end;
|
---|
115 |
|
---|
116 | procedure THostAddressList.SaveToXML(FileName: string);
|
---|
117 | var
|
---|
118 | XmlDocument: TXMLDocument;
|
---|
119 | NewNode: IXMLNode;
|
---|
120 | HostAddressList: IXMLNode;
|
---|
121 | I: Integer;
|
---|
122 | begin
|
---|
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);
|
---|
139 | end;
|
---|
140 |
|
---|
141 | { THostAddress }
|
---|
142 |
|
---|
143 | procedure THostAddress.LoadFromXmlNode(Node: IXMLNode);
|
---|
144 | begin
|
---|
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;
|
---|
153 | end;
|
---|
154 |
|
---|
155 | procedure THostAddress.SaveToXmlNode(Node: IXMLNode);
|
---|
156 | begin
|
---|
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;
|
---|
166 | end;
|
---|
167 |
|
---|
168 | end.
|
---|