source: trunk/Forms/UFormImportSources.pas

Last change on this file was 207, checked in by chronos, 3 years ago
  • Modified: Updated Common package.
  • Modified: CoolTranslator package merged into Common package.
  • Fixed: Build with Lazarus 2.0.12
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, LazUTF8, fgl;
10
11type
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
71var
72 FormImportSources: TFormImportSources;
73
74implementation
75
76{$R *.lfm}
77
78uses
79 UCore, UFormMain, UFormImportSource;
80
81resourcestring
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
90procedure TFormImportSources.ListView1Data(Sender: TObject; Item: TListItem);
91begin
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;
104end;
105
106procedure TFormImportSources.ListView1DblClick(Sender: TObject);
107begin
108 AModify.Execute;
109end;
110
111procedure TFormImportSources.ListView1KeyPress(Sender: TObject; var Key: char);
112begin
113 if Key = #27 then Close;
114end;
115
116procedure TFormImportSources.ListView1Resize(Sender: TObject);
117begin
118 ListViewFilter1.UpdateFromListView(ListView1);
119end;
120
121procedure TFormImportSources.ListView1SelectItem(Sender: TObject;
122 Item: TListItem; Selected: Boolean);
123begin
124 UpdateInterface;
125end;
126
127procedure TFormImportSources.ListViewFilter1Change(Sender: TObject);
128begin
129 UpdateList;
130end;
131
132procedure TFormImportSources.ListViewSort1ColumnWidthChanged(Sender: TObject);
133begin
134 ListViewFilter1.UpdateFromListView(ListView1);
135end;
136
137function TFormImportSources.ListViewSort1CompareItem(Item1, Item2: TObject
138 ): Integer;
139begin
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;
152end;
153
154procedure TFormImportSources.ListViewSort1Filter(ListViewSort: TListViewSort);
155begin
156 ImportSources.AssignToList(ListViewSort1.List);
157 FilterList(ListViewSort1.List);
158end;
159
160procedure TFormImportSources.UpdateList;
161begin
162 ListViewSort1.Refresh;
163 UpdateInterface;
164end;
165
166procedure TFormImportSources.UpdateInterface;
167begin
168 ARemove.Enabled := Assigned(ListView1.Selected);
169 AModify.Enabled := Assigned(ListView1.Selected);
170end;
171
172procedure TFormImportSources.FormShow(Sender: TObject);
173begin
174 Core.PersistentForm1.Load(Self);
175 UpdateList;
176 Core.ScaleDPI1.ScaleControl(ToolBar1, Core.ScaleDPI1.DesignDPI);
177end;
178
179procedure TFormImportSources.ListView1Change(Sender: TObject; Item: TListItem;
180 Change: TItemChange);
181begin
182 if Assigned(Item) and (Change = ctState) then begin
183 TImportSource(Item.Data).Enabled := Item.Checked;
184 Core.AcronymDb.Modified := True;
185 end;
186end;
187
188procedure TFormImportSources.ListView1Click(Sender: TObject);
189begin
190
191end;
192
193procedure TFormImportSources.AAddExecute(Sender: TObject);
194var
195 NewImportSource: TImportSource;
196 I: Integer;
197begin
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;
222end;
223
224procedure TFormImportSources.ADisableExecute(Sender: TObject);
225var
226 I: Integer;
227begin
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;
234end;
235
236procedure TFormImportSources.AEnableExecute(Sender: TObject);
237var
238 I: Integer;
239begin
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;
246end;
247
248procedure TFormImportSources.AModifyExecute(Sender: TObject);
249var
250 NewImportSource: TImportSource;
251begin
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;
280end;
281
282procedure TFormImportSources.AProcessExecute(Sender: TObject);
283begin
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;
291end;
292
293procedure TFormImportSources.ProcessImportJob(Job: TJob);
294begin
295 TImportSource(ListView1.Selected.Data).Process;
296end;
297
298procedure TFormImportSources.FilterList(List: TFPGObjectList<TObject>);
299var
300 I: Integer;
301 FoundCount: Integer;
302 EnteredCount: Integer;
303begin
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;
329end;
330
331procedure TFormImportSources.ARemoveExecute(Sender: TObject);
332var
333 I: Integer;
334begin
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;
344end;
345
346procedure TFormImportSources.FormClose(Sender: TObject;
347 var CloseAction: TCloseAction);
348begin
349 Core.PersistentForm1.Save(Self);
350end;
351
352procedure TFormImportSources.FormCreate(Sender: TObject);
353var
354 I: Integer;
355begin
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;
360end;
361
362end.
363
Note: See TracBrowser for help on using the repository browser.