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