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