zeoslib  UNKNOWN
 All Files
ZConnectionGroup.pas
Go to the documentation of this file.
1 {*********************************************************}
2 { }
3 { Zeos Database Objects }
4 { Database Connection Component }
5 { }
6 { Originally written by una.bicicleta }
7 {*********************************************************}
8 
9 {@********************************************************}
10 { Copyright (c) 1999-2012 Zeos Development Group }
11 { }
12 { License Agreement: }
13 { }
14 { This library is distributed in the hope that it will be }
15 { useful, but WITHOUT ANY WARRANTY; without even the }
16 { implied warranty of MERCHANTABILITY or FITNESS FOR }
17 { A PARTICULAR PURPOSE. See the GNU Lesser General }
18 { Public License for more details. }
19 { }
20 { The source code of the ZEOS Libraries and packages are }
21 { distributed under the Library GNU General Public }
22 { License (see the file COPYING / COPYING.ZEOS) }
23 { with the following modification: }
24 { As a special exception, the copyright holders of this }
25 { library give you permission to link this library with }
26 { independent modules to produce an executable, }
27 { regardless of the license terms of these independent }
28 { modules, and to copy and distribute the resulting }
29 { executable under terms of your choice, provided that }
30 { you also meet, for each linked independent module, }
31 { the terms and conditions of the license of that module. }
32 { An independent module is a module which is not derived }
33 { from or based on this library. If you modify this }
34 { library, you may extend this exception to your version }
35 { of the library, but you are not obligated to do so. }
36 { If you do not wish to do so, delete this exception }
37 { statement from your version. }
38 { }
39 { }
40 { The project web site is located on: }
41 { http://zeos.firmos.at (FORUM) }
42 { http://sourceforge.net/p/zeoslib/tickets/ (BUGTRACKER)}
43 { svn://svn.code.sf.net/p/zeoslib/code-0/trunk (SVN) }
44 { }
45 { http://www.sourceforge.net/projects/zeoslib. }
46 { }
47 { }
48 { Zeos Development Group. }
49 {********************************************************@}
50 
51 unit ZConnectionGroup;
52 
53 interface
54 {$I ZComponent.inc}
55 
56 uses
57  SysUtils, Classes, {$IFDEF MSEgui}mclasses,{$ENDIF} Forms, Dialogs,
58  ZDbcIntfs,ZCompatibility;
59 
60 const
61  mask = 'æææ#2ææ0#ææ39æ-V„–FFVæææ'; { define your own mask }
62 
63 type
64  TZConnectionGroup = class;
65  TZConnectionGroupLink = class;
66 
67  TZConnectionGroupLink = class(TObject)
68  private
69  FSender: TObject;
70  FOnChange: TNotifyEvent;
71  public
72  destructor Destroy; override;
73  procedure Change;
74  dynamic;
75  property OnChange: TNotifyEvent read FOnChange write FOnChange;
76  property Sender: TObject read FSender write FSender;
77  end;
78 
79 {$HINTS OFF}
80  TZConnectionGroup = class(TComponent)
81  private
82  FOnChange: TNotifyEvent;
83  procedure DoChange(Sender: TObject);
84  procedure Change;
85  protected
86  FClients: tList;
87  FProtocol: string;
88  FHostName: string;
89  FPort: Integer;
90  FDatabase: string;
91  FUser: string;
92  FPassword: string;
93  FLibLocation: String;
94  //FCatalog:string;
95 
96  procedure UnregisterAllDataSets;
97  procedure SetUser(const Value: string);
98  procedure SetPassword(const Value: string);
99  procedure DefineProperties(filer: tfiler);override;
100 
101  procedure SetProtocol(const Value: string);
102  procedure SetHostName(const Value: string);
103  procedure SetConnPort(const Value: integer);
104  procedure SetDatabase(const Value: string);
105  procedure SetLibLocation(const Value: String);
106  //procedure SetCatalog(const Value: string);
107 
108  function Encrypt(const str: string): string; //virtual;
109  function Decrypt(const str: string): string; //virtual;
110  public
111  constructor Create(AOwner: TComponent); override;
112  destructor Destroy; override;
113  procedure ReadPass(reader:treader);
114  procedure WritePass(writer:twriter);
115  procedure ReadUser(reader:treader);
116  procedure WriteUser(writer:twriter);
117  procedure RegisterChanges(Value: TZConnectionGroupLink);
118  procedure UnregisterChanges(Value: TZConnectionGroupLink);
119  published
120  property Protocol: string read FProtocol write SetProtocol;
121  property HostName: string read FHostName write SetHostName;
122  property Port: Integer read FPort write SetConnPort default 0;
123  property Database: string read FDatabase write SetDatabase;
124  property User: string read FUser write SetUser stored false;
125  property Password: string read FPassword write SetPassword stored false;
126  property LibraryLocation: String read FLibLocation write SetLibLocation;
127  //property Catalog: string read FCatalog write SetCatalog;
128  property OnChange: TNotifyEvent read FOnChange write FOnChange;
129  // -- todo ----
130  // add another property or event ?
131  end;
132 {$HINTS ON}
133 
134 implementation
135 
136 // === { TZConnectionGroupLink } ====================================================
137 destructor TZConnectionGroupLink.Destroy;
138 begin
139  if Sender is TZConnectionGroup then
140  TZConnectionGroup(Sender).UnregisterChanges(Self);
141  inherited Destroy;
142 end;
143 
144 procedure TZConnectionGroupLink.Change;
145 begin
146  if Assigned(FOnChange) then
147  FOnChange(Sender);
148 end;
149 
150 
151 // === { TZConnectionGroup } =============================================
152 constructor TZConnectionGroup.Create(AOwner: TComponent);
153 begin
154  inherited Create(AOwner);
155  FClients := tList.Create;
156 end;
157 
158 destructor TZConnectionGroup.Destroy;
159 begin
160  UnregisterAllDataSets;
161  FClients := nil;
162  FClients.Free;
163  inherited Destroy;
164 end;
165 
166 procedure TZConnectionGroup.DoChange(Sender: TObject);
167 begin
168  Change;
169 end;
170 
171 procedure TZConnectionGroup.Change;
172  var i:Integer;
173 begin
174  if Assigned(FOnChange) then
175  begin
176  FOnChange(Self);
177  end;
178  if FClients <> nil then
179  for I := 0 to FClients.Count - 1 do
180  TZConnectionGroupLink(FClients[I]).Change;
181 end;
182 
183 // === { TZConnectionGroup } =============================================
184 function TZConnectionGroup.Decrypt(const str: string): string;
185 var n: integer;
186 begin
187  result:='';
188  for n:=1 to length(str) do
189  result:=result+
190  chr(ord(str[n]) xor ord(mask[((n-1) mod length(mask)) +1]));
191 end;
192 
193 function TZConnectionGroup.Encrypt(const str: string): string;
194 begin
195  result:=Decrypt(str); { symmetrical encryption }
196 end;
197 
198 procedure TZConnectionGroup.ReadPass(reader:treader);
199 begin
200  reader.readlistbegin;
201  FPassword := Decrypt(reader.readstring);
202  reader.readlistend;
203 end;
204 
205 procedure TZConnectionGroup.WritePass(writer:twriter);
206 begin
207  writer.writelistbegin;
208  writer.writestring(Encrypt(FPassword));
209  writer.writelistend;
210 end;
211 
212 procedure TZConnectionGroup.ReadUser(reader:treader);
213 begin
214  reader.readlistbegin;
215  FUser := Decrypt(reader.readstring);
216  reader.readlistend;
217 end;
218 
219 procedure TZConnectionGroup.WriteUser(writer:twriter);
220 begin
221  writer.writelistbegin;
222  writer.writestring(Encrypt(FUser));
223  writer.writelistend;
224 end;
225 
226 procedure TZConnectionGroup.DefineProperties(filer: tfiler);
227 begin
228  inherited defineproperties(filer);
229  filer.DefineProperty('str1',ReadUser,WriteUser,true);
230  filer.DefineProperty('str2',ReadPass,WritePass,true);
231 end;
232 
233 
234 procedure TZConnectionGroup.SetUser(const Value: string);
235 begin
236  if FUser <> Value then
237  begin
238  FUser := Value;
239  Change;
240  end;
241 end;
242 
243 procedure TZConnectionGroup.SetPassword(const Value: string);
244 begin
245  if FPassword <> Value then
246  begin
247  FPassword := Value;
248  Change;
249  end;
250 end;
251 
252 procedure TZConnectionGroup.SetProtocol(const Value: string);
253 begin
254  if FProtocol <> Value then
255  begin
256  FProtocol := Value;
257  Change;
258  end;
259 end;
260 
261 procedure TZConnectionGroup.SetHostName(const Value: string);
262 begin
263  if FHostName <> Value then
264  begin
265  FHostName := Value;
266  Change;
267  end;
268 end;
269 
270 procedure TZConnectionGroup.SetDatabase(const Value: string);
271 begin
272  if FDatabase <> Value then
273  begin
274  FDatabase := Value;
275  Change;
276  end;
277 end;
278 
279 procedure TZConnectionGroup.SetLibLocation(const Value: String);
280 begin
281  if FLibLocation <> Value then
282  begin
283  FLibLocation := Value;
284  Change;
285  end;
286 end;
287 
288 {
289 procedure TZConnectionGroup.SetCatalog(const Value: string);
290 begin
291  if FCatalog <> Value then
292  begin
293  FCatalog := Value;
294  Change;
295  end;
296 end;
297 }
298 
299 procedure TZConnectionGroup.SetConnPort(const Value: integer);
300 begin
301  if FPort <> Value then
302  begin
303  FPort := Value;
304  Change;
305  end;
306 end;
307 
308 procedure TZConnectionGroup.UnregisterAllDataSets;
309 var
310  I: Integer;
311  Current: TZConnectionGroupLink;
312 begin
313  for I := FClients.Count - 1 downto 0 do
314  begin
315  Current := TZConnectionGroupLink(FClients[I]);
316  FClients.Remove(Current);
317 // try
318 // Current := nil;
319 // except
320 // //Ignore
321 // end;
322  end;
323 end;
324 
325 procedure TZConnectionGroup.RegisterChanges(Value: TZConnectionGroupLink);
326 begin
327  Value.Sender := Self;
328  if FClients <> nil then
329  FClients.Add(Value);
330 end;
331 
332 procedure TZConnectionGroup.UnregisterChanges(Value: TZConnectionGroupLink);
333 var
334  I: Integer;
335 begin
336  if FClients <> nil then
337  for I := 0 to FClients.Count - 1 do
338  if FClients[I] = Value then
339  begin
340  Value.Sender := nil;
341  FClients.Delete(I);
342  Break;
343  end;
344 end;
345 
346 end.