-
Notifications
You must be signed in to change notification settings - Fork 1
/
MRUList.pas
205 lines (178 loc) · 4.14 KB
/
MRUList.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
unit MRUList;
interface
type
TMRUList = class(TObject)
private
FSize: integer;
FItems: array of string;
function GetItem(iIndex: integer): string;
function GetItemCount: integer;
function GetItemIndex(s: string): integer;
procedure SetSize(const Value: integer);
public
property Size: integer read FSize write SetSize;
property Item[iIndex: integer]: string read GetItem;
property ItemCount: integer read GetItemCount;
function AddFile(sName: string): integer;
function Store(): integer;
function Load(): integer;
constructor Create;
destructor Destroy; override;
end;
implementation
{ TMRUList }
uses SysUtils, Registry, Windows;
function RegGetInt(Reg: TRegistry; sKey: string; iDefault: integer): integer;
begin
try
Result := Reg.ReadInteger(sKey);
except
on ERegistryException do Result := iDefault;
end;
end;
function RegGetDateTime(Reg: TRegistry; sKey: string; dtDefault: TDateTime): TDateTime;
begin
try
Result := Reg.ReadDateTime(sKey);
except
on ERegistryException do Result := dtDefault;
end;
end;
function RegGetBool(Reg: TRegistry; sKey: string; bDefault: boolean): boolean;
begin
try
Result := Reg.ReadBool(sKey);
except
on ERegistryException do Result := bDefault;
end;
end;
function RegGetString(Reg: TRegistry; sKey: string; sDefault: string): string;
begin
try
Result := Reg.ReadString(sKey);
except
on ERegistryException do Result := sDefault;
end;
if (Result = '') then Result := sDefault;
end;
function TMRUList.GetItemIndex(s: string): integer;
var
i: Integer;
begin
s := LowerCase(s);
Result := -1;
for i := 0 to Length(FItems) - 1 do
begin
if LowerCase(FItems[i]) = s then
begin
Result := i;
break;
end;
end;
end;
procedure TMRUList.SetSize(const Value: integer);
begin
FSize := Value;
SetLength(FItems,FSize);
end;
function TMRUList.Store: integer;
var
Reg: TRegistry;
i: Integer;
begin
try
Reg := TRegistry.Create(KEY_READ or KEY_WRITE);
try
Reg.RootKey := HKEY_CURRENT_USER;
if Reg.OpenKey('\Software\Grok\Beepola\MRU',true) then
begin
Reg.WriteInteger('Count',FSize);
for i := 0 to FSize - 1 do
Reg.WriteString(IntToStr(i),FItems[i]);
end;
finally
Reg.CloseKey;
end;
except
on ERegistryException do;
end;
Result := FSize;
FreeAndNil(Reg);
end;
function TMRUList.Load: integer;
var
Reg: TRegistry;
i, iCount: Integer;
begin
try
Reg := TRegistry.Create(KEY_READ or KEY_WRITE);
try
Reg.RootKey := HKEY_CURRENT_USER;
if Reg.OpenKey('\Software\Grok\Beepola\MRU',true) then
begin
iCount := RegGetInt(Reg,'Count',0);
for i := 0 to FSize-1 do
begin
if i < iCount then
FItems[i] := RegGetString(Reg,IntToStr(i),'')
else
FItems[i] := '';
end;
end;
finally
Reg.CloseKey;
end;
except
on ERegistryException do;
end;
Result := FSize;
FreeAndNil(Reg);
end;
function TMRUList.AddFile(sName: string): integer;
var
i: Integer;
begin
i := GetItemIndex(sName);
Result := i;
if i = -1 then
begin
// Not in list. Add it to the top...
for i := Length(FItems) - 2 downto 0 do
FItems[i+1] := FItems[i];
FItems[0] := sName;
end
else
begin
// In list. Move it to the top of the list
for i := (i-1) downto 0 do
FItems[i+1] := FItems[i];
FItems[0] := sName;
end;
Store;
end;
constructor TMRUList.Create;
begin
Size := 20;
Load();
end;
destructor TMRUList.Destroy;
begin
Store();
inherited;
end;
function TMRUList.GetItem(iIndex: integer): string;
begin
if (iIndex >= 0) and (iIndex < Length(FItems)) then
Result := FItems[iIndex]
else
Result := '';
end;
function TMRUList.GetItemCount: integer;
var
i: integer;
begin
Result := 0;
for i := 0 to FSize - 1 do
if FItems[i] <> '' then inc(Result);
end;
end.