-
Notifications
You must be signed in to change notification settings - Fork 4
/
uMRUList.pas
210 lines (163 loc) · 4.36 KB
/
uMRUList.pas
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
{*******************************************************}
{ }
{ Delphi REST Client Framework }
{ }
{ Copyright(c) 2013-2015 Embarcadero Technologies, Inc. }
{ }
{*******************************************************}
unit uMRUList;
interface
uses
uRESTObjects;
const
MRUDBFILE = 'mru.dat'; // do not localize
MRUCAPACITY = 40;
type
TMRUList = class(TObject)
private
FItems : TRESTRequestParamsObjectList;
FAutoSave : boolean;
FFilename : string;
public
constructor Create( const AFilename : string = '' ); overload;
destructor Destroy; override;
procedure Clear;
procedure AddItem( AItem : TRESTRequestParams );
function ContainsItem( const AURL : string ) : boolean;
function RemoveItem( const AURL : string ) : boolean;
procedure SaveToFile( const AFilename : string = '' );
procedure LoadFromFile( const AFilename : string = '' );
property AutoSave : boolean read FAutoSave write FAutoSave;
property Items : TRESTRequestParamsObjectList read FItems;
end;
implementation
uses System.JSON, System.SysUtils, System.Classes;
{ TMRUList }
procedure TMRUList.AddItem( AItem : TRESTRequestParams );
var LItem : TRESTRequestParams;
begin
Assert( Assigned(AItem) );
/// we do not want duplicates in the mru-list
if ContainsItem( AItem.ToString ) then
RemoveItem( AItem.ToString );
/// ensure that we do not exceed the desired capacity
if (FItems.Count > MRUCAPACITY) then
FItems.Delete( FItems.Count-1 );
LItem:= TRESTRequestParams.Create;
LItem.Assign( AItem );
/// as this is a MRU-list, we want the most recent item on top
FItems.Insert( 0, LItem );
if FAutoSave then
SaveToFile;
end;
procedure TMRUList.Clear;
begin
FItems.Clear;
end;
function TMRUList.ContainsItem( const AURL : string ) : boolean;
var LItem : TRESTRequestParams;
begin
result:= FALSE;
for LItem IN FItems do
begin
if SameText(LItem.ToString, AURL) then
begin
result:= TRUE;
BREAK;
end;
end;
end;
constructor TMRUList.Create( const AFilename : string = '' );
begin
inherited Create;
FFilename:= AFilename;
FAutoSave:= (FFilename <> '');
FItems:= TRESTRequestParamsObjectList.Create;
FItems.OwnsObjects:= TRUE;
if (FFilename <> '') AND FileExists(FFilename) then
LoadFromFile( FFilename );
end;
destructor TMRUList.Destroy;
begin
Clear;
FreeAndNIL( FItems );
inherited;
end;
procedure TMRUList.LoadFromFile( const AFilename : string = '' );
var LStream : TStringStream;
LRoot : TJSONArray;
LItem : TRESTRequestParams;
LFilename : string;
i : integer;
begin
Clear;
if (AFilename <> '') then
LFilename:= AFilename
else
LFilename:= FFilename;
if (LFilename = '') OR (NOT FileExists(LFilename)) then
EXIT;
LStream:= TStringStream.Create;
TRY
LStream.LoadFromFile( LFilename );
LRoot:= TJSONObject.ParseJSONValue( LStream.DataString ) AS TJSONArray;
for i:= 0 to LRoot.Count - 1 do
begin
LItem:= TRESTRequestParams.Create;
LItem.FromJSONObject( LRoot.Items[i] AS TJSONObject );
FItems.Add( LItem );
end;
FINALLY
FreeAndNIL( LStream );
END;
LRoot:= TJSONArray.Create;
TRY
for LItem IN FItems do
LRoot.Add( LItem.AsJSONObject );
FINALLY
FreeAndNIL( LRoot );
END;
end;
function TMRUList.RemoveItem( const AURL : string ) : boolean;
var LItem : TRESTRequestParams;
begin
result:= FALSE;
for LItem IN FItems do
begin
if SameText(LItem.ToString, AURL) then
begin
FItems.Remove( LItem );
result:= TRUE;
BREAK;
end;
end;
if FAutoSave then
SaveToFile;
end;
procedure TMRUList.SaveToFile( const AFilename : string = '' );
var LStream : TStringStream;
LRoot : TJSONArray;
LItem : TRESTRequestParams;
LFilename : string;
begin
if (AFilename <> '') then
LFilename:= AFilename
else
LFilename:= FFilename;
if (LFilename = '') then
EXIT;
LRoot:= TJSONArray.Create;
TRY
for LItem IN FItems do
LRoot.Add( LItem.AsJSONObject );
LStream:= TStringStream.Create( LRoot.ToString );
TRY
LStream.SaveToFile( LFilename );
FINALLY
FreeAndNIL( LStream );
END;
FINALLY
FreeAndNIL( LRoot );
END;
end;
end.