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