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