source: tags/1.4.0/Forms/UFormImportSources.pas

Last change on this file was 145, checked in by chronos, 7 years ago
  • Added: Allow to disable selected acronym categories so acronyms without any enabled category won't be shown in main acronym search filter.
File size: 11.8 KB
Line 
1unit UFormImportSources;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
9 ActnList, Menus, UAcronym, UJobProgressView, UListViewSort, SpecializedList,
10 LazUTF8;
11
12type
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
72var
73 FormImportSources: TFormImportSources;
74
75implementation
76
77{$R *.lfm}
78
79uses
80 UCore, UFormMain, UFormImportSource;
81
82resourcestring
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
91procedure TFormImportSources.ListView1Data(Sender: TObject; Item: TListItem);
92begin
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;
105end;
106
107procedure TFormImportSources.ListView1DblClick(Sender: TObject);
108begin
109 AModify.Execute;
110end;
111
112procedure TFormImportSources.ListView1KeyPress(Sender: TObject; var Key: char);
113begin
114 if Key = #27 then Close;
115end;
116
117procedure TFormImportSources.ListView1Resize(Sender: TObject);
118begin
119 ListViewFilter1.UpdateFromListView(ListView1);
120end;
121
122procedure TFormImportSources.ListView1SelectItem(Sender: TObject;
123 Item: TListItem; Selected: Boolean);
124begin
125 UpdateInterface;
126end;
127
128procedure TFormImportSources.ListViewFilter1Change(Sender: TObject);
129begin
130 UpdateList;
131end;
132
133procedure TFormImportSources.ListViewSort1ColumnWidthChanged(Sender: TObject);
134begin
135 ListViewFilter1.UpdateFromListView(ListView1);
136end;
137
138function TFormImportSources.ListViewSort1CompareItem(Item1, Item2: TObject
139 ): Integer;
140begin
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;
153end;
154
155procedure TFormImportSources.ListViewSort1Filter(ListViewSort: TListViewSort);
156begin
157 ImportSources.AssignToList(ListViewSort1.List);
158 FilterList(ListViewSort1.List);
159end;
160
161procedure TFormImportSources.UpdateList;
162begin
163 ListViewSort1.Refresh;
164 UpdateInterface;
165end;
166
167procedure TFormImportSources.UpdateInterface;
168begin
169 ARemove.Enabled := Assigned(ListView1.Selected);
170 AModify.Enabled := Assigned(ListView1.Selected);
171end;
172
173procedure TFormImportSources.FormShow(Sender: TObject);
174begin
175 Core.PersistentForm1.Load(Self);
176 UpdateList;
177 Core.ScaleDPI1.ScaleControl(ToolBar1, Core.ScaleDPI1.DesignDPI);
178end;
179
180procedure TFormImportSources.ListView1Change(Sender: TObject; Item: TListItem;
181 Change: TItemChange);
182begin
183 if Assigned(Item) and (Change = ctState) then begin
184 TImportSource(Item.Data).Enabled := Item.Checked;
185 Core.AcronymDb.Modified := True;
186 end;
187end;
188
189procedure TFormImportSources.ListView1Click(Sender: TObject);
190begin
191
192end;
193
194procedure TFormImportSources.AAddExecute(Sender: TObject);
195var
196 NewImportSource: TImportSource;
197 I: Integer;
198begin
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;
223end;
224
225procedure TFormImportSources.ADisableExecute(Sender: TObject);
226var
227 I: Integer;
228begin
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;
235end;
236
237procedure TFormImportSources.AEnableExecute(Sender: TObject);
238var
239 I: Integer;
240begin
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;
247end;
248
249procedure TFormImportSources.AModifyExecute(Sender: TObject);
250var
251 NewImportSource: TImportSource;
252begin
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;
281end;
282
283procedure TFormImportSources.AProcessExecute(Sender: TObject);
284begin
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;
292end;
293
294procedure TFormImportSources.ProcessImportJob(Job: TJob);
295begin
296 TImportSource(ListView1.Selected.Data).Process;
297end;
298
299procedure TFormImportSources.FilterList(List: TListObject);
300var
301 I: Integer;
302 FoundCount: Integer;
303 EnteredCount: Integer;
304begin
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;
330end;
331
332procedure TFormImportSources.ARemoveExecute(Sender: TObject);
333var
334 I: Integer;
335begin
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;
345end;
346
347procedure TFormImportSources.FormClose(Sender: TObject;
348 var CloseAction: TCloseAction);
349begin
350 Core.PersistentForm1.Save(Self);
351end;
352
353procedure TFormImportSources.FormCreate(Sender: TObject);
354var
355 I: Integer;
356begin
357 Core.CoolTranslator1.TranslateComponentRecursive(Self);
358 for I := 0 to ToolBar1.ButtonCount - 1 do
359 ToolBar1.Buttons[I].Hint := ToolBar1.Buttons[I].Caption;
360end;
361
362end.
363
Note: See TracBrowser for help on using the repository browser.