File: ddfiles.pp

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (301 lines) | stat: -rw-r--r-- 8,107 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
{
 Copyright (c) 2007 by Michael Van Canneyt.

 *****************************************************************************
 *  See the file COPYING.modifiedLGPL.txt, included in this distribution,
 *  for details about the license.
 *****************************************************************************
}
unit ddfiles;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, inifiles, inicol;
  
Type
  { TRecentItem }

  TRecentItem = Class(TNamedIniCollectionItem)
  private
    FLastUse: TDateTime;
    FUseCount: Integer;
  Public
    Procedure SaveToIni(Ini: TCustomInifile; ASection : String); override;
    Procedure LoadFromIni(Ini: TCustomInifile; ASection : String); override;
    Procedure Use;
  Published
    Property LastUse : TDateTime Read FLastUse;
    Property UseCount : Integer Read FUseCount;
  end;

  { TRecentDataDict }
  
  TRecentDataDict = Class(TRecentItem)
  private
    FFileName: String;
  Public
    Procedure SaveToIni(Ini: TCustomInifile; ASection : String); override;
    Procedure LoadFromIni(Ini: TCustomInifile; ASection : String); override;
  Published
    Property FileName : String Read FFileName Write FFileName;
  end;

  { TRecentDataDicts }

  TRecentDataDicts = Class(TNamedIniCollection)
  private
    function GetDD(Index: Integer): TRecentDataDict;
    procedure SetDD(Index: Integer; const AValue: TRecentDataDict);
  Public
    Constructor Create(AItemClass: TCollectionItemClass);
    Function IndexOfFileName(AFileName : String) : Integer;
    Function FindFromUserData(UserData : TObject) : TRecentDataDict;
    Function FindFromFileName(AFileName : String) : TRecentDataDict;
    Function FindFromName(AName : String) : TRecentDataDict;
    Function AddDataDict(AFileName : String) : TRecentDataDict;
    Property DataDicts [Index: Integer] : TRecentDataDict Read GetDD Write SetDD; default;
  end;

  { TRecentConnection }

  TRecentConnection = Class(TRecentItem)
  private
    FConnectionString: String;
    FEngineName: String;
  Public
    Procedure SaveToIni(Ini: TCustomInifile; ASection : String); override;
    Procedure LoadFromIni(Ini: TCustomInifile; ASection : String); override;
  Published
    Property EngineName : String Read FEngineName Write FEngineName;
    Property ConnectionString : String Read FConnectionString Write FConnectionString;
  end;
  
  { TRecentConnections }

  TRecentConnections = Class(TNamedIniCollection)
  private
    function GetConnection(Index: Integer): TRecentConnection;
    procedure SetConnection(Index: Integer; const AValue: TRecentConnection);
  Public
    Constructor Create(AItemClass : TCollectionItemClass);
    Function IndexOfConnectionString(Const AConnectionString : String) : Integer;
    Function FindFromUserData(UserData : TObject) : TRecentConnection;
    Function FindFromConnection(Const AConnectionString : String) : TRecentConnection;
    Function FindFromName(Const AName : String) : TRecentConnection;
    Function AddConnection(Const AName: String) : TRecentConnection;
    Property Connections [Index: Integer] : TRecentConnection Read GetConnection Write SetConnection; default;
  end;

implementation

{ ---------------------------------------------------------------------
  TRecentDataDict
  ---------------------------------------------------------------------}

Const
  KeyFileName   = 'FileName';
  KeyLastUsed   = 'LastUsed';
  KeyUseCount   = 'UseCount';
  KeyConnection = 'Connection';
  KeyEngineName = 'EngineName';
  
procedure TRecentDataDict.SaveToIni(Ini: TCustomInifile; ASection: String);
begin
  Inherited;
  With Ini do
    begin
    WriteString(ASection,KeyFileName,self.FFileName);
    end;
end;

procedure TRecentDataDict.LoadFromIni(Ini: TCustomInifile; ASection: String);
begin
  Inherited;
  With Ini do
    begin
    Self.FFileName:=ReadString(ASection,KeyFileName,Self.FFileName);
    end;
end;

{ ---------------------------------------------------------------------
  TRecentDataDicts
  ---------------------------------------------------------------------}

function TRecentDataDicts.GetDD(Index: Integer): TRecentDataDict;
begin
  Result:=TRecentDataDict(Items[Index]);
end;

procedure TRecentDataDicts.SetDD(Index: Integer; const AValue: TRecentDataDict);
begin
  Items[Index]:=AValue;
end;

constructor TRecentDataDicts.Create(AItemClass: TCollectionItemClass);
begin
  inherited Create(AItemClass);
  FPrefix:='DataDict';
  FSectionPrefix:='DD';
end;

function TRecentDataDicts.IndexOfFileName(AFileName: String): Integer;
begin
  Result:=Count-1;
  While (Result>=0) and (Not SameFileName(GetDD(Result).FileName,AFileName)) do
    Dec(Result);
end;

function TRecentDataDicts.AddDataDict(AFileName: String): TRecentDataDict;
begin
  Result:=Add as TRecentDataDict;
  Result.FileName:=AFileName;
end;

function TRecentDataDicts.FindFromUserData(UserData: TObject): TRecentDataDict;

begin
  Result:=TRecentDataDict(FindByUserData(UserData));
end;

function TRecentDataDicts.FindFromFileName(AFileName: String): TRecentDataDict;

Var
  I : integer;

begin
  I:=IndexOfFileName(AFileName);
  If (I=-1) then
    Result:=Nil
  else
    Result:=GetDD(I);
end;

function TRecentDataDicts.FindFromName(AName: String): TRecentDataDict;

begin
  Result:=TRecentDataDict(FindByName(AName));
end;

{ ---------------------------------------------------------------------
  TRecentConnection
  ---------------------------------------------------------------------}


procedure TRecentConnection.SaveToIni(Ini: TCustomInifile; ASection: String);
begin
  Inherited;
  With Ini do
   begin
   WriteString(ASection,KeyConnection,FConnectionString);
   WriteString(ASection,KeyEngineName,FEngineName);
   end;
end;

procedure TRecentConnection.LoadFromIni(Ini: TCustomInifile; ASection: String);
begin
  Inherited;
  With Ini do
    begin
    FConnectionString:=ReadString(ASection,KeyConnection,FConnectionString);
    FEngineName:=ReadString(ASection,KeyEngineName,FEngineName);
    end;
end;

{ ---------------------------------------------------------------------
  TRecentConnections
  ---------------------------------------------------------------------}

function TRecentConnections.GetConnection(Index: Integer): TRecentConnection;
begin
  Result:=TRecentConnection(Items[Index]);
end;

procedure TRecentConnections.SetConnection(Index: Integer;
  const AValue: TRecentConnection);
begin
  Items[Index]:=AValue;
end;

constructor TRecentConnections.Create(AItemClass: TCollectionItemClass);
begin
  inherited Create(AItemClass);
  FPrefix:='Connection';
  FSectionPrefix:='CONN';
end;

function TRecentConnections.IndexOfConnectionString(const AConnectionString: String
  ): Integer;
begin
  Result:=Count-1;
  While (Result>=0) and (CompareText(GetConnection(Result).ConnectionString,AConnectionString)<>0) do
    Dec(Result);
end;

function TRecentConnections.AddConnection(Const AName: String): TRecentConnection;
begin
  Result:=Add as TRecentConnection;
  Result.Name:=AName;
end;

function TRecentConnections.FindFromUserData(UserData: TObject): TRecentConnection;
Var
  I : Integer;

begin
  I:=IndexOfUserData(UserData);
  If (I=-1) then
    Result:=Nil
  Else
    Result:=GetConnection(I);
end;

function TRecentConnections.FindFromConnection(Const AConnectionString: String): TRecentConnection;
Var
  I : Integer;

begin
  I:=IndexOfConnectionString(AConnectionString);
  If (I=-1) then
    Result:=Nil
  Else
    Result:=GetConnection(I);
end;

function TRecentConnections.FindFromName(Const AName: String): TRecentConnection;

begin
  Result:=TRecentConnection(FindByName(AName));
end;


{ TRecentItem }

procedure TRecentItem.SaveToIni(Ini: TCustomInifile; ASection: String);
begin
  With Ini do
    begin
    WriteInteger(ASection,KeyUseCount,FUseCount);
    WriteDateTime(ASection,KeyLastUsed,FLastUse);
    end;
end;

procedure TRecentItem.LoadFromIni(Ini: TCustomInifile; ASection: String);
begin
  With Ini do
    begin
    FUseCount:=ReadInteger(ASection,KeyUseCount,0);
    FLastUse:=ReadDateTime(ASection,KeyLastUsed,0);
    end;
end;

procedure TRecentItem.Use;
begin
  FLastUse:=Now;
  inc(FUseCount);
end;

end.