source: oup/current/DataAccess/ConnectionManager.pas @ 247

Last change on this file since 247 was 247, checked in by alloc, 15 years ago
File size: 6.8 KB
Line 
1unit ConnectionManager;
2interface
3
4uses TypeDefs, DataAccess, Access_OniArchive, Access_OUP_ADB, Access_OniSplitArchive;
5
6type
7  TConnections = array of TDataAccess;
8
9  TConnectionListChangedEvent = procedure of object;
10
11 
12  TConnectionManager = class
13  private
14    FConnections: TConnections;
15    FLastID:      Integer;
16    FConnectionListChanged: TConnectionListChangedEvent;
17    function GetConnectionCount: Integer;
18    function GetConnection(ConnectionID: Integer): TDataAccess;
19    function GetConnectionByIndex(Index: Integer): TDataAccess;
20    function GetConnectionIndex(ConnectionID: Integer): Integer;
21    procedure RemoveConnection(ArrayIndex: Integer);
22  protected
23  public
24    property Count: Integer read GetConnectionCount;
25    property Connection[ConnectionID: Integer]: TDataAccess read GetConnection;
26    property ConnectionByIndex[Index: Integer]: TDataAccess read GetConnectionByIndex;
27    property ConnectionIndexByID[ConnectionID: Integer]: Integer read GetConnectionIndex;
28    property OnCoonnectionListChanged: TConnectionListChangedEvent read FConnectionListChanged write FConnectionListChanged;
29
30    constructor Create;
31    function Close: Boolean;
32
33    function OpenConnection(FileName: String; var Msg: TStatusMessages): Integer;
34    function CloseConnectionByIndex(Index: Integer; var Msg: TStatusMessages): Boolean; overload;
35    function CloseConnection(ID: Integer; var Msg: TStatusMessages): Boolean; overload;
36    function CloseConnection(FileName: String; var Msg: TStatusMessages): Boolean; overload;
37    function FileOpened(FileName: String): Integer;
38  published
39  end;
40
41
42var
43  ConManager: TConnectionManager;
44
45
46implementation
47uses
48  SysUtils, Dialogs;
49
50(*
51 Implementation of TConnectionManager
52*)
53
54
55function TConnectionManager.GetConnectionCount: Integer;
56begin
57  Result := Length(FConnections);
58end;
59
60function TConnectionManager.GetConnectionIndex(ConnectionID: Integer): Integer;
61var
62  i: Integer;
63begin
64  Result := -1;
65  if Count > 0 then
66    for i := 0 to Count - 1 do
67      if ConnectionByIndex[i].ConnectionID = ConnectionID then
68      begin
69        Result := i;
70        Break;
71      end;
72end;
73
74function TConnectionManager.GetConnection(ConnectionID: Integer): TDataAccess;
75var
76  i: Integer;
77begin
78  Result := nil;
79  if Length(FConnections) > 0 then
80  begin
81    for i := 0 to High(FConnections) do
82    begin
83      if FConnections[i].ConnectionID = ConnectionID then
84      begin
85        Result := FConnections[i];
86        Break;
87      end;
88    end;
89    if i = Length(FConnections) then
90      ShowMessage('Couldn''t find specified ConnectionID (' +
91          IntToStr(ConnectionID) + '). Please contact developer!!!');
92  end;
93end;
94
95
96function TConnectionManager.GetConnectionByIndex(Index: Integer): TDataAccess;
97begin
98  Result := nil;
99  if index < Length(FConnections) then
100  begin
101    Result := FConnections[index];
102  end;
103end;
104
105constructor TConnectionManager.Create;
106begin
107  inherited;
108  FLastID := 0;
109end;
110
111function TConnectionManager.Close: Boolean;
112begin
113  Result := False;
114  if Length(FConnections) > 0 then
115    Exit;
116
117  inherited;
118end;
119
120
121
122function TConnectionManager.OpenConnection(FileName: String; var Msg: TStatusMessages): Integer;
123var
124  i: Integer;
125  ext: String;
126  backend: TDataBackend;
127  CreateMsg: TStatusMessages;
128begin
129  Msg := SM_UnknownError;
130  Result := -1;
131
132  if Length(FConnections) > 0 then
133  begin
134    for i := 0 to High(FConnections) do
135    begin
136      if FConnections[i].FileName = FileName then
137      begin
138        Result := FConnections[i].ConnectionID;
139        Msg := SM_AlreadyOpened;
140        Exit;
141      end;
142    end;
143  end;
144
145  if not FileExists(FileName) then
146  begin
147    Msg := SM_FileNotFound;
148    Exit;
149  end;
150
151  ext := UpperCase(ExtractFileExt(FileName));
152
153  if ext = '.OLDB' then
154    backend := DB_ADB
155  else if ext = '.DAT' then
156    backend := DB_ONI
157  else if ext = '.ONI' then
158    backend := DB_ONISPLIT
159  else
160  begin
161    Msg := SM_UnknownExtension;
162    Exit;
163  end;
164
165  SetLength(FConnections, Length(FConnections) + 1);
166  i := High(FConnections);
167  case backend of
168    DB_ONI:
169      FConnections[i] := TAccess_OniArchive.Create(FileName, FLastID + 1, CreateMsg);
170    DB_ADB:
171      FConnections[i] := TAccess_OUP_ADB.Create(FileName, FLastID + 1, CreateMsg);
172    DB_ONISPLIT:
173      FConnections[i] := TAccess_OniSplitArchive.Create(FileName, FLastID + 1, CreateMsg);
174  end;
175
176  if CreateMsg = SM_OK then
177  begin
178    FLastID := FConnections[i].ConnectionID;
179    Result := FLastID;
180    Msg := SM_OK;
181  end
182  else
183  begin
184    FConnections[i].Close;
185    FConnections[i] := nil;
186    SetLength(FConnections, Length(FConnections) - 1);
187    Msg := CreateMsg;
188  end;
189end;
190
191
192procedure TConnectionManager.RemoveConnection(ArrayIndex: Integer);
193var
194  i: Integer;
195begin
196  if Length(FConnections) > 1 then
197  begin
198    for i := ArrayIndex to High(FConnections) - 1 do
199    begin
200      FConnections[i] := FConnections[i + 1];
201    end;
202  end;
203  SetLength(FConnections, Length(FConnections) - 1);
204end;
205
206function TConnectionManager.CloseConnectionByIndex(Index: Integer; var Msg: TStatusMessages): Boolean;
207begin
208  Msg := SM_UnknownError;
209  Result := False;
210
211  if Index < Length(FConnections) then
212  begin
213    FConnections[Index].Close;
214    RemoveConnection(Index);
215    Msg := SM_OK;
216    Result := True;
217  end;
218end;
219
220function TConnectionManager.CloseConnection(ID: Integer; var Msg: TStatusMessages): Boolean;
221var
222  i: Integer;
223begin
224  Msg := SM_UnknownError;
225  Result := False;
226
227  if Length(FConnections) > 0 then
228  begin
229    for i := 0 to High(FConnections) do
230    begin
231      if FConnections[i].ConnectionID = ID then
232      begin
233        FConnections[i].Close;
234        RemoveConnection(i);
235        Msg := SM_OK;
236        Result := True;
237        Exit;
238      end;
239    end;
240  end;
241end;
242
243function TConnectionManager.CloseConnection(FileName: String; var Msg: TStatusMessages): Boolean;
244var
245  i: Integer;
246begin
247  Msg := SM_UnknownError;
248  Result := False;
249
250  if Length(FConnections) > 0 then
251  begin
252    for i := 0 to High(FConnections) do
253    begin
254      if FConnections[i].FileName = FileName then
255      begin
256        FConnections[i].Close;
257        RemoveConnection(i);
258        Msg := SM_OK;
259        Result := True;
260        Exit;
261      end;
262    end;
263  end;
264end;
265
266
267function TConnectionManager.FileOpened(FileName: String): Integer;
268var
269  i: Integer;
270begin
271  Result := -1;
272  if Length(FConnections) > 0 then
273    for i := 0 to High(FConnections) do
274      if FConnections[i].FileName = FileName then
275      begin
276        Result := FConnections[i].ConnectionID;
277        Exit;
278      end;
279end;
280
281
282initialization
283  ConManager := TConnectionManager.Create;
284finalization
285  ConManager.Free;
286end.
Note: See TracBrowser for help on using the repository browser.