source: trunk/Forms/FormImportSources.pas

Last change on this file was 227, checked in by chronos, 44 hours ago
  • Modified: Do not reference global Core object if possible.
File size: 11.1 KB
Line 
1unit FormImportSources;
2
3interface
4
5uses
6 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
7 ActnList, Menus, Acronym, JobProgressView, ListViewSort, LazUTF8, FormEx,
8 Generics.Collections;
9
10type
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
70implementation
71
72{$R *.lfm}
73
74uses
75 FormMain, FormImportSource;
76
77resourcestring
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
86procedure TFormImportSources.ListView1Data(Sender: TObject; Item: TListItem);
87begin
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;
100end;
101
102procedure TFormImportSources.ListView1DblClick(Sender: TObject);
103begin
104 AModify.Execute;
105end;
106
107procedure TFormImportSources.ListView1KeyPress(Sender: TObject; var Key: char);
108begin
109 if Key = #27 then Close;
110end;
111
112procedure TFormImportSources.ListView1Resize(Sender: TObject);
113begin
114 ListViewFilter1.UpdateFromListView(ListView1);
115end;
116
117procedure TFormImportSources.ListView1SelectItem(Sender: TObject;
118 Item: TListItem; Selected: Boolean);
119begin
120 UpdateInterface;
121end;
122
123procedure TFormImportSources.ListViewFilter1Change(Sender: TObject);
124begin
125 UpdateList;
126end;
127
128procedure TFormImportSources.ListViewSort1ColumnWidthChanged(Sender: TObject);
129begin
130 ListViewFilter1.UpdateFromListView(ListView1);
131end;
132
133function TFormImportSources.ListViewSort1CompareItem(Item1, Item2: TObject
134 ): Integer;
135begin
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;
148end;
149
150procedure TFormImportSources.ListViewSort1Filter(ListViewSort: TListViewSort);
151begin
152 ImportSources.AssignToList(ListViewSort1.List);
153 FilterList(ListViewSort1.List);
154end;
155
156procedure TFormImportSources.UpdateList;
157begin
158 ListViewSort1.Refresh;
159 UpdateInterface;
160end;
161
162procedure TFormImportSources.UpdateInterface;
163begin
164 ARemove.Enabled := Assigned(ListView1.Selected);
165 AModify.Enabled := Assigned(ListView1.Selected);
166end;
167
168procedure TFormImportSources.FormShow(Sender: TObject);
169begin
170 UpdateList;
171 ScaleDPI.ScaleControl(ToolBar1, ScaleDPI.DesignDPI);
172end;
173
174procedure TFormImportSources.ListView1Change(Sender: TObject; Item: TListItem;
175 Change: TItemChange);
176begin
177 if Assigned(Item) and (Change = ctState) then begin
178 TImportSource(Item.Data).Enabled := Item.Checked;
179 AcronymDb.Modified := True;
180 end;
181end;
182
183procedure TFormImportSources.AAddExecute(Sender: TObject);
184var
185 NewImportSource: TImportSource;
186 FormImportSource: TFormImportSource;
187 I: Integer;
188begin
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;
214end;
215
216procedure TFormImportSources.ADisableExecute(Sender: TObject);
217var
218 I: Integer;
219begin
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;
226end;
227
228procedure TFormImportSources.AEnableExecute(Sender: TObject);
229var
230 I: Integer;
231begin
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;
238end;
239
240procedure TFormImportSources.AModifyExecute(Sender: TObject);
241var
242 NewImportSource: TImportSource;
243 FormImportSource: TFormImportSource;
244begin
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;
274end;
275
276procedure TFormImportSources.AProcessExecute(Sender: TObject);
277begin
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;
285end;
286
287procedure TFormImportSources.ProcessImportJob(Job: TJob);
288begin
289 TImportSource(ListView1.Selected.Data).Process;
290end;
291
292procedure TFormImportSources.FilterList(List: TObjectList<TObject>);
293var
294 I: Integer;
295 FoundCount: Integer;
296 EnteredCount: Integer;
297begin
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;
323end;
324
325procedure TFormImportSources.ARemoveExecute(Sender: TObject);
326var
327 I: Integer;
328begin
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;
338end;
339
340procedure TFormImportSources.FormCreate(Sender: TObject);
341var
342 I: Integer;
343begin
344 for I := 0 to ToolBar1.ButtonCount - 1 do
345 ToolBar1.Buttons[I].Hint := ToolBar1.Buttons[I].Caption;
346end;
347
348end.
349
Note: See TracBrowser for help on using the repository browser.