-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexplorersprocessor.pas
201 lines (176 loc) · 4.94 KB
/
explorersprocessor.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
unit explorersprocessor;
{$ifdef fpc}{$mode delphi}{$endif}{$H+}
interface
uses
Classes, SysUtils, registry;
type
IExplorerStorage = interface
procedure ReadConfig(ExplorerNames: TStrings);
procedure SaveConfig(ExplorerNames: TStrings);
procedure ClearConfig;
end;
TRegistryExplorerStorage = class(TInterfacedObject, IExplorerStorage)
private
FRegistryKey: String;
public
procedure ReadConfig(ExplorerNames: TStrings);
procedure SaveConfig(ExplorerNames: TStrings);
procedure ClearConfig;
constructor Create(ARegistryKey: string);
end;
TExplorersProcessor = class
private
FExplorerStorage: IExplorerStorage;
procedure CorrectExplorers(const ExplorerNames: TStrings);
public
ListOfAllExplorers: TStrings;
ListOfKnownExplorers: TStringList;
ListOfUnknownExplorers: TStringList;
constructor Create(AListOfExplorers: TStringArray; AExplorerStorage: IExplorerStorage);
destructor Destroy; override;
procedure GetExplorers;
procedure SetExplorers(InputExplorers: String);
end;
function StringReplaceAll(const haystack: string; const needle: string; const replacement: string): string;
const
SettingExplorers = 'Explorers';
implementation
constructor TExplorersProcessor.Create(AListOfExplorers: TStringArray; AExplorerStorage: IExplorerStorage);
begin
FExplorerStorage := AExplorerStorage;
ListOfAllExplorers := TStringList.Create;
ListOfAllExplorers.AddStrings(AListOfExplorers);
ListOfKnownExplorers := TStringList.Create;
ListOfUnknownExplorers := TStringList.Create;
end;
destructor TExplorersProcessor.Destroy;
begin
FreeAndNil(ListOfUnknownExplorers);
FreeAndNil(ListOfKnownExplorers);
FreeAndNil(ListOfAllExplorers);
inherited Destroy;
end;
procedure TExplorersProcessor.GetExplorers;
var
ReadExplorers: TStringList;
begin
ReadExplorers := TStringList.Create;
try
FExplorerStorage.ReadConfig(ReadExplorers);
ReadExplorers.Sort;
CorrectExplorers(ReadExplorers);
finally
ReadExplorers.Free;
end;
end;
procedure TExplorersProcessor.SetExplorers(InputExplorers: String);
var
SplitExplorerNames: TStringList;
begin
if Trim(InputExplorers) = '' then
begin
FExplorerStorage.ClearConfig;
end
else
begin
SplitExplorerNames := TStringList.Create;
try
InputExplorers := StringReplaceAll(InputExplorers, ' ,', ',');
InputExplorers := StringReplaceAll(InputExplorers, ', ', ',');
SplitExplorerNames.StrictDelimiter := true;
SplitExplorerNames.DelimitedText := InputExplorers;
SplitExplorerNames.Sort;
CorrectExplorers(SplitExplorerNames);
finally
FreeAndNil(SplitExplorerNames);
end;
end;
end;
procedure TExplorersProcessor.CorrectExplorers(const ExplorerNames: TStrings);
var
I: integer;
ExplorerName: String;
previousKnown: string;
begin
previousKnown := ListOfKnownExplorers.DelimitedText;
ListOfUnknownExplorers.Clear;
ListOfKnownExplorers.Clear;
for ExplorerName in ExplorerNames do
begin
I := ListOfAllExplorers.IndexOf(ExplorerName);
if I = -1 then
begin
ListOfUnknownExplorers.Add(ExplorerName);
end
else
begin
ListOfKnownExplorers.Add(ListOfAllExplorers[I]);
end;
end;
ListOfUnknownExplorers.Sort;
ListOfKnownExplorers.Sort;
if (ListOfUnknownExplorers.Count > 0) or (previousKnown <> ListOfKnownExplorers.DelimitedText) then
begin
FExplorerStorage.SaveConfig(ListOfKnownExplorers);
end;
end;
function StringReplaceAll(const haystack: string; const needle: string; const replacement: string): string;
begin
Result := haystack;
repeat
Result := StringReplace(Result, needle, replacement, [rfReplaceAll]);
until Pos(needle, Result) = 0;
end;
constructor TRegistryExplorerStorage.Create(ARegistryKey: string);
begin
FRegistryKey := ARegistryKey;
end;
procedure TRegistryExplorerStorage.ReadConfig(ExplorerNames: TStrings);
var
Registry: TRegistry;
begin
Registry := TRegistry.Create;
try
Registry.RootKey := HKEY_CURRENT_USER;
if Registry.OpenKeyReadOnly(FRegistryKey) then
begin
Registry.ReadStringList(SettingExplorers, ExplorerNames);
end;
finally
Registry.Free;
end;
end;
procedure TRegistryExplorerStorage.SaveConfig(ExplorerNames: TStrings);
var
Registry: TRegistry;
begin
Registry := TRegistry.Create;
try
Registry.RootKey := HKEY_CURRENT_USER;
if Registry.OpenKey(FRegistryKey, true) then
begin
Registry.WriteStringList(SettingExplorers, ExplorerNames, True);
end;
finally
Registry.Free;
end;end;
procedure TRegistryExplorerStorage.ClearConfig;
var
Registry: TRegistry;
begin
Registry := TRegistry.Create;
try
Registry.RootKey := HKEY_CURRENT_USER;
if Registry.OpenKey(FRegistryKey, true) then
begin
Registry.DeleteValue(SettingExplorers);
if (not Registry.HasSubKeys) and (Length(Registry.GetValueNames) = 0) then
begin
Registry.DeleteKey(FRegistryKey);
end;
end;
finally
Registry.Free;
end;
end;
end.