1 | unit FormImportSources;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
|
---|
7 | ActnList, Menus, Acronym, JobProgressView, ListViewSort, LazUTF8, FormEx,
|
---|
8 | Generics.Collections;
|
---|
9 |
|
---|
10 | type
|
---|
11 |
|
---|
12 | { TFormImportSources }
|
---|
13 |
|
---|
14 | TFormImportSources = class(TFormEx)
|
---|
15 | AAdd: TAction;
|
---|
16 | APreview: TAction;
|
---|
17 | AEnable: TAction;
|
---|
18 | ADisable: TAction;
|
---|
19 | AProcess: TAction;
|
---|
20 | ActionList1: TActionList;
|
---|
21 | AModify: TAction;
|
---|
22 | ARemove: TAction;
|
---|
23 | JobProgressView1: TJobProgressView;
|
---|
24 | ListView1: TListView;
|
---|
25 | ListViewFilter1: TListViewFilter;
|
---|
26 | ListViewSort1: TListViewSort;
|
---|
27 | MenuItem1: TMenuItem;
|
---|
28 | MenuItem2: TMenuItem;
|
---|
29 | MenuItem3: TMenuItem;
|
---|
30 | MenuItem4: TMenuItem;
|
---|
31 | MenuItem5: TMenuItem;
|
---|
32 | MenuItem6: TMenuItem;
|
---|
33 | MenuItem7: TMenuItem;
|
---|
34 | MenuItem8: TMenuItem;
|
---|
35 | PopupMenuImportSource: TPopupMenu;
|
---|
36 | ToolBar1: TToolBar;
|
---|
37 | ToolButton1: TToolButton;
|
---|
38 | ToolButton2: TToolButton;
|
---|
39 | ToolButton3: TToolButton;
|
---|
40 | ToolButton4: TToolButton;
|
---|
41 | ToolButton5: TToolButton;
|
---|
42 | ToolButton6: TToolButton;
|
---|
43 | ToolButton7: TToolButton;
|
---|
44 | procedure AAddExecute(Sender: TObject);
|
---|
45 | procedure ADisableExecute(Sender: TObject);
|
---|
46 | procedure AEnableExecute(Sender: TObject);
|
---|
47 | procedure AModifyExecute(Sender: TObject);
|
---|
48 | procedure APreviewExecute(Sender: TObject);
|
---|
49 | procedure AProcessExecute(Sender: TObject);
|
---|
50 | procedure ARemoveExecute(Sender: TObject);
|
---|
51 | procedure FormActivate(Sender: TObject);
|
---|
52 | procedure FormCreate(Sender: TObject);
|
---|
53 | procedure FormShow(Sender: TObject);
|
---|
54 | procedure ListView1Change(Sender: TObject; Item: TListItem;
|
---|
55 | Change: TItemChange);
|
---|
56 | procedure ListView1Data(Sender: TObject; Item: TListItem);
|
---|
57 | procedure ListView1DblClick(Sender: TObject);
|
---|
58 | procedure ListView1KeyPress(Sender: TObject; var Key: char);
|
---|
59 | procedure ListView1Resize(Sender: TObject);
|
---|
60 | procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
|
---|
61 | Selected: Boolean);
|
---|
62 | procedure ListViewFilter1Change(Sender: TObject);
|
---|
63 | procedure ListViewSort1ColumnWidthChanged(Sender: TObject);
|
---|
64 | function ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
|
---|
65 | procedure ListViewSort1Filter(ListViewSort: TListViewSort);
|
---|
66 | private
|
---|
67 | PreviewAcronymDb: TAcronymDb;
|
---|
68 | procedure ProcessImportJob(Job: TJob);
|
---|
69 | procedure PreviewImportJob(Job: TJob);
|
---|
70 | procedure FilterList(List: TObjectList<TObject>);
|
---|
71 | public
|
---|
72 | ImportSources: TImportSources;
|
---|
73 | AcronymDb: TAcronymDb;
|
---|
74 | procedure UpdateList;
|
---|
75 | procedure UpdateInterface;
|
---|
76 | end;
|
---|
77 |
|
---|
78 |
|
---|
79 | implementation
|
---|
80 |
|
---|
81 | {$R *.lfm}
|
---|
82 |
|
---|
83 | uses
|
---|
84 | FormMain, FormImportSource, FormAcronyms;
|
---|
85 |
|
---|
86 | resourcestring
|
---|
87 | SRemoveImportSource = 'Remove import sources';
|
---|
88 | SRemoveImportSourceQuery = 'Do you really want to remove selected import sources?';
|
---|
89 | SImportSourceAlreadyExists = 'Import source %s already exists!';
|
---|
90 | SProcessSelectedSource = 'Process selected import source';
|
---|
91 | SPreviewSelectedSource = 'Preview selected import source';
|
---|
92 |
|
---|
93 |
|
---|
94 | { TFormImportSources }
|
---|
95 |
|
---|
96 | procedure TFormImportSources.ListView1Data(Sender: TObject; Item: TListItem);
|
---|
97 | begin
|
---|
98 | if Item.Index < ListViewSort1.List.Count then
|
---|
99 | with TImportSource(ListViewSort1.List[Item.Index]) do begin
|
---|
100 | Item.Caption := Name;
|
---|
101 | Item.Data := ListViewSort1.List[Item.Index];
|
---|
102 | Item.SubItems.Add(URL);
|
---|
103 | Item.SubItems.Add(Categories.GetString);
|
---|
104 | Item.SubItems.Add(IntToStr(ItemCount));
|
---|
105 | if LastImportTime <> 0 then
|
---|
106 | Item.SubItems.Add(DateToStr(LastImportTime))
|
---|
107 | else Item.SubItems.Add('');
|
---|
108 | Item.SubItems.Add(Format.Name);
|
---|
109 | Item.Checked := Enabled;
|
---|
110 | if Enabled then Item.StateIndex := 24 else
|
---|
111 | Item.StateIndex := 23;
|
---|
112 | end;
|
---|
113 | end;
|
---|
114 |
|
---|
115 | procedure TFormImportSources.ListView1DblClick(Sender: TObject);
|
---|
116 | begin
|
---|
117 | AModify.Execute;
|
---|
118 | end;
|
---|
119 |
|
---|
120 | procedure TFormImportSources.ListView1KeyPress(Sender: TObject; var Key: char);
|
---|
121 | begin
|
---|
122 | if Key = #27 then Close;
|
---|
123 | end;
|
---|
124 |
|
---|
125 | procedure TFormImportSources.ListView1Resize(Sender: TObject);
|
---|
126 | begin
|
---|
127 | ListViewFilter1.UpdateFromListView(ListView1);
|
---|
128 | end;
|
---|
129 |
|
---|
130 | procedure TFormImportSources.ListView1SelectItem(Sender: TObject;
|
---|
131 | Item: TListItem; Selected: Boolean);
|
---|
132 | begin
|
---|
133 | UpdateInterface;
|
---|
134 | end;
|
---|
135 |
|
---|
136 | procedure TFormImportSources.ListViewFilter1Change(Sender: TObject);
|
---|
137 | begin
|
---|
138 | UpdateList;
|
---|
139 | end;
|
---|
140 |
|
---|
141 | procedure TFormImportSources.ListViewSort1ColumnWidthChanged(Sender: TObject);
|
---|
142 | begin
|
---|
143 | ListViewFilter1.UpdateFromListView(ListView1);
|
---|
144 | end;
|
---|
145 |
|
---|
146 | function TFormImportSources.ListViewSort1CompareItem(Item1, Item2: TObject
|
---|
147 | ): Integer;
|
---|
148 | var
|
---|
149 | ImportSource1, ImportSource2: TImportSource;
|
---|
150 | begin
|
---|
151 | ImportSource1 := TImportSource(Item1);
|
---|
152 | ImportSource2 := TImportSource(Item2);
|
---|
153 | Result := 0;
|
---|
154 | if Assigned(Item1) and Assigned(Item2) and (ListViewSort1.Order <> soNone) then begin
|
---|
155 | with ListViewSort1 do
|
---|
156 | case Column of
|
---|
157 | 0: Result := CompareString(ImportSource1.Name, ImportSource2.Name);
|
---|
158 | 1: Result := CompareString(ImportSource1.URL, ImportSource2.URL);
|
---|
159 | 2: Result := CompareString(ImportSource1.Categories.GetString, ImportSource2.Categories.GetString);
|
---|
160 | 3: Result := CompareInteger(ImportSource1.ItemCount, ImportSource2.ItemCount);
|
---|
161 | 4: Result := CompareTime(ImportSource1.LastImportTime, ImportSource2.LastImportTime);
|
---|
162 | 5: Result := CompareString(ImportSource1.Format.Name, ImportSource2.Format.Name);
|
---|
163 | end;
|
---|
164 | if ListViewSort1.Order = soDown then Result := -Result;
|
---|
165 | end else Result := 0;
|
---|
166 | end;
|
---|
167 |
|
---|
168 | procedure TFormImportSources.ListViewSort1Filter(ListViewSort: TListViewSort);
|
---|
169 | begin
|
---|
170 | ImportSources.AssignToList(ListViewSort1.List);
|
---|
171 | FilterList(ListViewSort1.List);
|
---|
172 | end;
|
---|
173 |
|
---|
174 | procedure TFormImportSources.UpdateList;
|
---|
175 | begin
|
---|
176 | ListViewSort1.Refresh;
|
---|
177 | UpdateInterface;
|
---|
178 | end;
|
---|
179 |
|
---|
180 | procedure TFormImportSources.UpdateInterface;
|
---|
181 | begin
|
---|
182 | ARemove.Enabled := Assigned(ListView1.Selected);
|
---|
183 | AModify.Enabled := Assigned(ListView1.Selected);
|
---|
184 | AProcess.Enabled := Assigned(ListView1.Selected);
|
---|
185 | APreview.Enabled := Assigned(ListView1.Selected);
|
---|
186 | end;
|
---|
187 |
|
---|
188 | procedure TFormImportSources.FormShow(Sender: TObject);
|
---|
189 | begin
|
---|
190 | UpdateList;
|
---|
191 | ScaleDPI.ScaleControl(ToolBar1, ScaleDPI.DesignDPI);
|
---|
192 | end;
|
---|
193 |
|
---|
194 | procedure TFormImportSources.ListView1Change(Sender: TObject; Item: TListItem;
|
---|
195 | Change: TItemChange);
|
---|
196 | begin
|
---|
197 | if Assigned(Item) and (Change = ctState) then begin
|
---|
198 | TImportSource(Item.Data).Enabled := Item.Checked;
|
---|
199 | AcronymDb.Modified := True;
|
---|
200 | end;
|
---|
201 | end;
|
---|
202 |
|
---|
203 | procedure TFormImportSources.AAddExecute(Sender: TObject);
|
---|
204 | var
|
---|
205 | NewImportSource: TImportSource;
|
---|
206 | FormImportSource: TFormImportSource;
|
---|
207 | begin
|
---|
208 | NewImportSource := TImportSource.Create;
|
---|
209 | NewImportSource.Sources := ImportSources;
|
---|
210 | FormImportSource := TFormImportSource.Create(Self);
|
---|
211 | try
|
---|
212 | FormImportSource.AcronymDb := AcronymDb;
|
---|
213 | FormImportSource.Load(NewImportSource);
|
---|
214 | if FormImportSource.ShowModal = mrOk then begin
|
---|
215 | FormImportSource.Save(NewImportSource);
|
---|
216 | if not Assigned(ImportSources.SearchByName(NewImportSource.Name)) then begin;
|
---|
217 | ImportSources.Add(NewImportSource);
|
---|
218 |
|
---|
219 | NewImportSource := nil;
|
---|
220 | AcronymDb.Modified := True;
|
---|
221 | UpdateList;
|
---|
222 | end else ShowMessage(Format(SImportSourceAlreadyExists, [NewImportSource.Name]));
|
---|
223 | end;
|
---|
224 | if Assigned(NewImportSource) then NewImportSource.Free;
|
---|
225 | finally
|
---|
226 | FreeAndNil(FormImportSource);
|
---|
227 | end;
|
---|
228 | end;
|
---|
229 |
|
---|
230 | procedure TFormImportSources.ADisableExecute(Sender: TObject);
|
---|
231 | var
|
---|
232 | I: Integer;
|
---|
233 | begin
|
---|
234 | for I := ListView1.Items.Count - 1 downto 0 do
|
---|
235 | if ListView1.Items[I].Selected then begin
|
---|
236 | TImportSource(ListView1.Items[I].Data).Enabled := False;
|
---|
237 | AcronymDb.Modified := True;
|
---|
238 | end;
|
---|
239 | UpdateList;
|
---|
240 | end;
|
---|
241 |
|
---|
242 | procedure TFormImportSources.AEnableExecute(Sender: TObject);
|
---|
243 | var
|
---|
244 | I: Integer;
|
---|
245 | begin
|
---|
246 | for I := ListView1.Items.Count - 1 downto 0 do
|
---|
247 | if ListView1.Items[I].Selected then begin
|
---|
248 | TImportSource(ListView1.Items[I].Data).Enabled := True;
|
---|
249 | AcronymDb.Modified := True;
|
---|
250 | end;
|
---|
251 | UpdateList;
|
---|
252 | end;
|
---|
253 |
|
---|
254 | procedure TFormImportSources.AModifyExecute(Sender: TObject);
|
---|
255 | var
|
---|
256 | NewImportSource: TImportSource;
|
---|
257 | FormImportSource: TFormImportSource;
|
---|
258 | begin
|
---|
259 | if Assigned(ListView1.Selected) then begin
|
---|
260 | NewImportSource := TImportSource.Create;
|
---|
261 | NewImportSource.Assign(ListView1.Selected.Data);
|
---|
262 | FormImportSource := TFormImportSource.Create(Self);
|
---|
263 | try
|
---|
264 | FormImportSource.AcronymDb := AcronymDb;
|
---|
265 | FormImportSource.Load(NewImportSource);
|
---|
266 | if FormImportSource.ShowModal = mrOk then begin
|
---|
267 | FormImportSource.Save(NewImportSource);
|
---|
268 | if (NewImportSource.Name <> TImportSource(ListView1.Selected.Data).Name) then begin
|
---|
269 | if not Assigned(ImportSources.SearchByName(NewImportSource.Name)) then begin;
|
---|
270 | TImportSource(ListView1.Selected.Data).Assign(NewImportSource);
|
---|
271 | AcronymDb.Modified := True;
|
---|
272 | end else ShowMessage(Format(SImportSourceAlreadyExists, [NewImportSource.Name]));
|
---|
273 | end else begin
|
---|
274 | TImportSource(ListView1.Selected.Data).Assign(NewImportSource);
|
---|
275 | AcronymDb.Modified := True;
|
---|
276 | end;
|
---|
277 |
|
---|
278 | UpdateList;
|
---|
279 | end;
|
---|
280 | if Assigned(NewImportSource) then NewImportSource.Free;
|
---|
281 | finally
|
---|
282 | FreeAndNil(FormImportSource);
|
---|
283 | end;
|
---|
284 | end;
|
---|
285 | end;
|
---|
286 |
|
---|
287 | procedure TFormImportSources.APreviewExecute(Sender: TObject);
|
---|
288 | var
|
---|
289 | FormAcronyms: TFormAcronyms;
|
---|
290 | begin
|
---|
291 | if Assigned(ListView1.Selected) then begin
|
---|
292 | PreviewAcronymDb := TAcronymDb.Create;
|
---|
293 | try
|
---|
294 | JobProgressView1.AddJob(SPreviewSelectedSource, PreviewImportJob);
|
---|
295 | JobProgressView1.Start;
|
---|
296 | FormAcronyms := TFormAcronyms.Create(nil);
|
---|
297 | try
|
---|
298 | FormAcronyms.AcronymDb := PreviewAcronymDb;
|
---|
299 | FormAcronyms.Acronyms := PreviewAcronymDb.Acronyms;
|
---|
300 | FormAcronyms.ShowModal;
|
---|
301 | finally
|
---|
302 | FormAcronyms.Free;
|
---|
303 | end;
|
---|
304 | finally
|
---|
305 | FreeAndNil(PreviewAcronymDb);
|
---|
306 | end;
|
---|
307 | end;
|
---|
308 | end;
|
---|
309 |
|
---|
310 | procedure TFormImportSources.AProcessExecute(Sender: TObject);
|
---|
311 | begin
|
---|
312 | if Assigned(ListView1.Selected) then begin
|
---|
313 | AcronymDb.AddedCount := 0;
|
---|
314 | JobProgressView1.AddJob(SProcessSelectedSource, ProcessImportJob);
|
---|
315 | JobProgressView1.Start;
|
---|
316 | ShowMessage(Format(SAddedCount, [TImportSource(ListView1.Selected.Data).ItemCount,
|
---|
317 | AcronymDb.AddedCount]));
|
---|
318 | end;
|
---|
319 | end;
|
---|
320 |
|
---|
321 | procedure TFormImportSources.ProcessImportJob(Job: TJob);
|
---|
322 | begin
|
---|
323 | TImportSource(ListView1.Selected.Data).Process(AcronymDb);
|
---|
324 | end;
|
---|
325 |
|
---|
326 | procedure TFormImportSources.PreviewImportJob(Job: TJob);
|
---|
327 | begin
|
---|
328 | TImportSource(ListView1.Selected.Data).Process(PreviewAcronymDb);
|
---|
329 | end;
|
---|
330 |
|
---|
331 | procedure TFormImportSources.FilterList(List: TObjectList<TObject>);
|
---|
332 | var
|
---|
333 | I: Integer;
|
---|
334 | FoundCount: Integer;
|
---|
335 | EnteredCount: Integer;
|
---|
336 | begin
|
---|
337 | EnteredCount := ListViewFilter1.TextEnteredCount;
|
---|
338 | for I := List.Count - 1 downto 0 do begin
|
---|
339 | if List.Items[I] is TImportSource then begin
|
---|
340 | with TImportSource(List.Items[I]) do begin
|
---|
341 | with ListViewFilter1 do
|
---|
342 | if Visible and (EnteredCount > 0) then begin
|
---|
343 | FoundCount := 0;
|
---|
344 | if Pos(UTF8LowerCase(StringGrid.Cells[0, 0]),
|
---|
345 | UTF8LowerCase(TImportSource(List.Items[I]).Name)) > 0 then Inc(FoundCount);
|
---|
346 | if Pos(UTF8LowerCase(StringGrid.Cells[1, 0]),
|
---|
347 | UTF8LowerCase(TImportSource(List.Items[I]).URL)) > 0 then Inc(FoundCount);
|
---|
348 | if Pos(UTF8LowerCase(StringGrid.Cells[2, 0]),
|
---|
349 | UTF8LowerCase(TImportSource(List.Items[I]).Categories.GetString)) > 0 then Inc(FoundCount);
|
---|
350 | if Pos(UTF8LowerCase(StringGrid.Cells[3, 0]),
|
---|
351 | UTF8LowerCase(IntToStr(TImportSource(List.Items[I]).ItemCount))) > 0 then Inc(FoundCount);
|
---|
352 | if Pos(UTF8LowerCase(StringGrid.Cells[4, 0]),
|
---|
353 | UTF8LowerCase(DateTimeToStr(TImportSource(List.Items[I]).LastImportTime))) > 0 then Inc(FoundCount);
|
---|
354 | if Pos(UTF8LowerCase(StringGrid.Cells[5, 0]),
|
---|
355 | UTF8LowerCase(TImportSource(List.Items[I]).Format.Name)) > 0 then Inc(FoundCount);
|
---|
356 | if FoundCount <> EnteredCount then List.Delete(I);
|
---|
357 | end;
|
---|
358 | end;
|
---|
359 | end else
|
---|
360 | if TImportSource(List.Items[I]) is TImportSource then begin
|
---|
361 | List.Delete(I);
|
---|
362 | end;
|
---|
363 | end;
|
---|
364 | end;
|
---|
365 |
|
---|
366 | procedure TFormImportSources.ARemoveExecute(Sender: TObject);
|
---|
367 | var
|
---|
368 | I: Integer;
|
---|
369 | begin
|
---|
370 | if Assigned(ListView1.Selected) then begin
|
---|
371 | if MessageDlg(SRemoveImportSource, SRemoveImportSourceQuery,
|
---|
372 | TMsgDlgType.mtConfirmation, [mbCancel, mbOk], 0) = mrOk then begin
|
---|
373 | for I := ListView1.Items.Count - 1 downto 0 do
|
---|
374 | if ListView1.Items[I].Selected then
|
---|
375 | ImportSources.Remove(ListView1.Items[I].Data);
|
---|
376 | UpdateList;
|
---|
377 | end;
|
---|
378 | end;
|
---|
379 | end;
|
---|
380 |
|
---|
381 | procedure TFormImportSources.FormActivate(Sender: TObject);
|
---|
382 | begin
|
---|
383 | ListViewFilter1.UpdateFromListView(ListView1);
|
---|
384 | end;
|
---|
385 |
|
---|
386 | procedure TFormImportSources.FormCreate(Sender: TObject);
|
---|
387 | var
|
---|
388 | I: Integer;
|
---|
389 | begin
|
---|
390 | for I := 0 to ToolBar1.ButtonCount - 1 do
|
---|
391 | ToolBar1.Buttons[I].Hint := ToolBar1.Buttons[I].Caption;
|
---|
392 | {$IFDEF LCLQt5}
|
---|
393 | ListView1.Checkboxes := False;
|
---|
394 | ListView1.StateImages := ActionList1.Images;
|
---|
395 | {$ENDIF}
|
---|
396 | end;
|
---|
397 |
|
---|
398 | end.
|
---|
399 |
|
---|