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