source: trunk/Forms/UFormMain.pas@ 23

Last change on this file since 23 was 22, checked in by chronos, 9 years ago
  • Added: Now import formats can be specified using new edit windows.
  • Added: Import source can now be downloaded and processed including download from https URLs.
File size: 15.4 KB
Line 
1unit UFormMain;
2
3{$mode delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus,
9 ComCtrls, StdCtrls, ExtCtrls, ActnList, UAcronym, UPersistentForm,
10 URegistry, ULastOpenedList, UListViewSort, UCoolTranslator, Registry,
11 SpecializedList, LazUTF8;
12
13type
14
15 { TFormMain }
16
17 TFormMain = class(TForm)
18 AProcessImports: TAction;
19 AShowImportFormats: TAction;
20 AShowAbout: TAction;
21 AShowImportSources: TAction;
22 AShowAcronyms: TAction;
23 AShowCategories: TAction;
24 ASettings: TAction;
25 AFileSaveAs: TAction;
26 AFileClose: TAction;
27 AFileSave: TAction;
28 AFileNew: TAction;
29 AFileOpen: TAction;
30 AImport: TAction;
31 AShow: TAction;
32 AExit: TAction;
33 ActionList1: TActionList;
34 CheckBoxExactMath: TCheckBox;
35 CoolTranslator1: TCoolTranslator;
36 ImageList1: TImageList;
37 LastOpenedList1: TLastOpenedList;
38 ListViewAcronyms: TListView;
39 ListViewFilter1: TListViewFilter;
40 ListViewSort1: TListViewSort;
41 MainMenu1: TMainMenu;
42 MenuItem1: TMenuItem;
43 MenuItem10: TMenuItem;
44 MenuItem11: TMenuItem;
45 MenuItem12: TMenuItem;
46 MenuItem13: TMenuItem;
47 MenuItem14: TMenuItem;
48 MenuItem15: TMenuItem;
49 MenuItem16: TMenuItem;
50 MenuItem17: TMenuItem;
51 MenuItem18: TMenuItem;
52 MenuItem19: TMenuItem;
53 MenuItem20: TMenuItem;
54 MenuItem21: TMenuItem;
55 MenuItem22: TMenuItem;
56 MenuItem23: TMenuItem;
57 MenuItem4: TMenuItem;
58 MenuItem5: TMenuItem;
59 MenuItem6: TMenuItem;
60 MenuItem7: TMenuItem;
61 MenuItemOpenRecent: TMenuItem;
62 MenuItem2: TMenuItem;
63 MenuItem3: TMenuItem;
64 MenuItem8: TMenuItem;
65 MenuItem9: TMenuItem;
66 OpenDialog1: TOpenDialog;
67 PersistentForm1: TPersistentForm;
68 PopupMenuTryIcon: TPopupMenu;
69 SaveDialog1: TSaveDialog;
70 TrayIcon1: TTrayIcon;
71 procedure AExitExecute(Sender: TObject);
72 procedure AFileCloseExecute(Sender: TObject);
73 procedure AFileNewExecute(Sender: TObject);
74 procedure AFileOpenExecute(Sender: TObject);
75 procedure AFileSaveAsExecute(Sender: TObject);
76 procedure AFileSaveExecute(Sender: TObject);
77 procedure AImportExecute(Sender: TObject);
78 procedure AProcessImportsExecute(Sender: TObject);
79 procedure ASettingsExecute(Sender: TObject);
80 procedure AShowAboutExecute(Sender: TObject);
81 procedure AShowAcronymsExecute(Sender: TObject);
82 procedure AShowCategoriesExecute(Sender: TObject);
83 procedure AShowExecute(Sender: TObject);
84 procedure AShowImportFormatsExecute(Sender: TObject);
85 procedure AShowImportSourcesExecute(Sender: TObject);
86 procedure CheckBoxExactMathChange(Sender: TObject);
87 procedure EditSearchChange(Sender: TObject);
88 procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
89 procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
90 procedure FormCreate(Sender: TObject);
91 procedure FormDestroy(Sender: TObject);
92 procedure FormShow(Sender: TObject);
93 procedure LastOpenedList1Change(Sender: TObject);
94 procedure ListViewAcronymsData(Sender: TObject; Item: TListItem);
95 procedure ListViewAcronymsSelectItem(Sender: TObject; Item: TListItem;
96 Selected: Boolean);
97 procedure ListViewFilter1Change(Sender: TObject);
98 function ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
99 procedure ListViewSort1Filter(ListViewSort: TListViewSort);
100 procedure TrayIcon1Click(Sender: TObject);
101 private
102 FAlwaysOnTop: Boolean;
103 RegistryContext: TRegistryContext;
104 ProjectClosed: Boolean;
105 procedure SetAlwaysOnTop(AValue: Boolean);
106 procedure FilterList(List: TListObject);
107 procedure OpenRecentClick(Sender: TObject);
108 procedure UpdateAcronymsList;
109 procedure UpdateInterface;
110 procedure LoadConfig;
111 procedure SaveConfig;
112 public
113 AcronymDb: TAcronymDb;
114 function CompareStrings(Strings1, Strings2: TStrings): Boolean;
115 property AlwaysOnTop: Boolean read FAlwaysOnTop write SetAlwaysOnTop;
116 end;
117
118var
119 FormMain: TFormMain;
120
121implementation
122
123{$R *.lfm}
124
125uses
126 UFormImport, UFormSettings, UFormCategories, UFormAcronyms,
127 UFormImportSources, UFormAbout, UFormImportFormats;
128
129resourcestring
130 SModified = 'modified';
131 SAppExit = 'Application exit';
132 SAppExitQuery = 'Acronyms were modified. Do you want to save them to file before exit?';
133
134const
135 ProjectExt = '.adp';
136 DefaultFileName = 'Acronyms' + ProjectExt;
137 DefaultRegKey = '\Software\Acronym Decoder';
138
139{ TFormMain }
140
141procedure TFormMain.FormCreate(Sender: TObject);
142begin
143 AcronymDb := TAcronymDb.Create;
144end;
145
146procedure TFormMain.EditSearchChange(Sender: TObject);
147begin
148 UpdateAcronymsList;
149end;
150
151procedure TFormMain.FormClose(Sender: TObject; var CloseAction: TCloseAction);
152begin
153 PersistentForm1.Save(Self);
154 SaveConfig;
155end;
156
157procedure TFormMain.FormCloseQuery(Sender: TObject; var CanClose: boolean);
158begin
159 AFileClose.Execute;
160 CanClose := ProjectClosed;
161end;
162
163procedure TFormMain.AExitExecute(Sender: TObject);
164begin
165 Close;
166end;
167
168procedure TFormMain.AFileCloseExecute(Sender: TObject);
169var
170 ModalResult: TModalResult;
171 DoClose: Boolean;
172begin
173 DoClose := False;
174 if Assigned(AcronymDb) then begin
175 if AcronymDb.Modified then begin
176 ModalResult := MessageDlg(SAppExit, SAppExitQuery,
177 mtConfirmation, [mbYes, mbNo, mbCancel], 0);
178 if ModalResult = mrYes then begin
179 AFileSave.Execute;
180 DoClose := True;
181 end
182 else if ModalResult = mrNo then begin
183 DoClose := True;
184 end else ProjectClosed := False;
185 end else DoClose := True;
186 end;
187 if DoClose then begin
188 FreeAndNil(AcronymDb);
189 UpdateAcronymsList;
190 UpdateInterface;
191 ProjectClosed := True;
192 end;
193end;
194
195procedure TFormMain.AFileNewExecute(Sender: TObject);
196begin
197 AFileClose.Execute;
198 if ProjectClosed then begin
199 AcronymDb := TAcronymDb.Create;
200 AcronymDb.FileName := DefaultFileName;
201 AcronymDb.Acronyms.Clear;
202 UpdateAcronymsList;
203 UpdateInterface;
204 end;
205end;
206
207procedure TFormMain.AFileOpenExecute(Sender: TObject);
208begin
209 OpenDialog1.DefaultExt := ProjectExt;
210 if Assigned(AcronymDb) then
211 OpenDialog1.FileName := AcronymDb.FileName;
212 if OpenDialog1.Execute then begin
213 AFileClose.Execute;
214 if ProjectClosed then begin
215 AFileNew.Execute;
216 AcronymDb.LoadFromFile(OpenDialog1.FileName);
217 LastOpenedList1.AddItem(OpenDialog1.FileName);
218 UpdateAcronymsList;
219 UpdateInterface;
220 end;
221 end;
222end;
223
224procedure TFormMain.AFileSaveAsExecute(Sender: TObject);
225begin
226 SaveDialog1.DefaultExt := ProjectExt;
227 SaveDialog1.FileName := AcronymDb.FileName;
228 if SaveDialog1.Execute then begin
229 AcronymDb.SaveToFile(SaveDialog1.FileName);
230 LastOpenedList1.AddItem(SaveDialog1.FileName);
231 UpdateInterface;
232 end;
233end;
234
235procedure TFormMain.AFileSaveExecute(Sender: TObject);
236begin
237 if FileExists(AcronymDb.FileName) then begin
238 AcronymDb.SaveToFile(AcronymDb.FileName);
239 LastOpenedList1.AddItem(AcronymDb.FileName);
240 UpdateInterface;
241 end else AFileSaveAs.Execute;
242end;
243
244procedure TFormMain.AImportExecute(Sender: TObject);
245begin
246 FormImport.ShowModal;
247 UpdateAcronymsList;
248 UpdateInterface;
249end;
250
251procedure TFormMain.AProcessImportsExecute(Sender: TObject);
252var
253 I: Integer;
254begin
255 for I := 0 to AcronymDb.ImportSources.Count - 1 do
256 TImportSource(AcronymDb.ImportSources[I]).Process;
257 UpdateAcronymsList;
258 UpdateInterface;
259end;
260
261procedure TFormMain.ASettingsExecute(Sender: TObject);
262begin
263 FormSettings.Load;
264 if FormSettings.ShowModal = mrOk then
265 FormSettings.Save;
266end;
267
268procedure TFormMain.AShowAboutExecute(Sender: TObject);
269begin
270 FormAbout.ShowModal;
271end;
272
273procedure TFormMain.AShowAcronymsExecute(Sender: TObject);
274begin
275 FormAcronyms.ShowModal;
276end;
277
278procedure TFormMain.AShowCategoriesExecute(Sender: TObject);
279begin
280 FormCategories.Categories := AcronymDb.Categories;
281 FormCategories.ShowModal;
282 UpdateAcronymsList;
283 UpdateInterface;
284end;
285
286procedure TFormMain.AShowExecute(Sender: TObject);
287begin
288 Show;
289end;
290
291procedure TFormMain.AShowImportFormatsExecute(Sender: TObject);
292begin
293 FormImportFormats.ImportFormats := AcronymDb.ImportFormats;
294 FormImportFormats.ShowModal;
295 UpdateInterface;
296end;
297
298procedure TFormMain.AShowImportSourcesExecute(Sender: TObject);
299begin
300 FormImportSources.ImportSources := AcronymDb.ImportSources;
301 FormImportSources.ShowModal;
302 UpdateInterface;
303end;
304
305procedure TFormMain.CheckBoxExactMathChange(Sender: TObject);
306begin
307 UpdateAcronymsList;
308end;
309
310procedure TFormMain.FormDestroy(Sender: TObject);
311begin
312 FreeAndNil(AcronymDb);
313end;
314
315procedure TFormMain.FormShow(Sender: TObject);
316begin
317 LoadConfig;
318 PersistentForm1.Load(Self);
319
320 if (LastOpenedList1.Items.Count > 0) and FileExists(LastOpenedList1.Items[0]) then
321 AcronymDB.LoadFromFile(LastOpenedList1.Items[0]);
322 UpdateAcronymsList;
323 ListViewFilter1.UpdateFromListView(ListViewAcronyms);
324 UpdateInterface;
325end;
326
327procedure TFormMain.LastOpenedList1Change(Sender: TObject);
328begin
329 LastOpenedList1.LoadToMenuItem(MenuItemOpenRecent, OpenRecentClick);
330end;
331
332procedure TFormMain.ListViewAcronymsData(Sender: TObject; Item: TListItem);
333begin
334 if Item.Index < ListViewSort1.List.Count then
335 with TAcronymMeaning(ListViewSort1.List[Item.Index]) do begin
336 Item.Caption := Acronym.Name;
337 Item.SubItems.Add(Name);
338 Item.Data := TAcronymMeaning(ListViewSort1.List[Item.Index]);
339 Item.SubItems.Add(Categories.GetString);
340 end;
341end;
342
343procedure TFormMain.ListViewAcronymsSelectItem(Sender: TObject;
344 Item: TListItem; Selected: Boolean);
345begin
346 UpdateInterface;
347end;
348
349procedure TFormMain.ListViewFilter1Change(Sender: TObject);
350begin
351 UpdateAcronymsList;
352end;
353
354function TFormMain.ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
355begin
356 Result := 0;
357 if Assigned(Item1) and Assigned(Item2) and (ListViewSort1.Order <> soNone) then begin
358 with ListViewSort1 do
359 case Column of
360 0: Result := CompareString(TAcronymMeaning(Item1).Acronym.Name, TAcronymMeaning(Item2).Acronym.Name);
361 1: Result := CompareString(TAcronymMeaning(Item1).Description, TAcronymMeaning(Item2).Description);
362 2: Result := CompareString(TAcronymMeaning(Item1).Categories.GetString, TAcronymMeaning(Item2).Categories.GetString);
363 end;
364 if ListViewSort1.Order = soDown then Result := -Result;
365 end else Result := 0;
366end;
367
368procedure TFormMain.ListViewSort1Filter(ListViewSort: TListViewSort);
369begin
370 AcronymDb.Acronyms.Sort(AcronymComparer);
371 AcronymDb.AssignToList(ListViewSort1.List);
372 FilterList(ListViewSort1.List);
373end;
374
375procedure TFormMain.TrayIcon1Click(Sender: TObject);
376begin
377 Visible := not Visible;
378end;
379
380function TFormMain.CompareStrings(Strings1, Strings2: TStrings): Boolean;
381var
382 I: Integer;
383begin
384 Result := Strings1.Count = Strings2.Count;
385 if not Result then Exit;
386 for I := 0 to Strings1.Count - 1 do
387 if (Strings1[I] <> Strings2[I]) or (Strings1.Objects[I] <> Strings2.Objects[I]) then begin
388 Result := False;
389 Exit;
390 end;
391end;
392
393procedure TFormMain.SetAlwaysOnTop(AValue: Boolean);
394begin
395 if FAlwaysOnTop = AValue then Exit;
396 FAlwaysOnTop := AValue;
397 if FAlwaysOnTop then FormStyle := fsSystemStayOnTop
398 else FormStyle := fsNormal;
399end;
400
401procedure TFormMain.FilterList(List: TListObject);
402var
403 I: Integer;
404 FoundCount: Integer;
405 EnteredCount: Integer;
406begin
407 EnteredCount := ListViewFilter1.TextEnteredCount;
408 for I := List.Count - 1 downto 0 do begin
409 if List.Items[I] is TAcronymMeaning then begin
410 with TAcronymMeaning(List.Items[I]) do begin
411 with ListViewFilter1 do
412 if Visible and (EnteredCount > 0) then begin
413 FoundCount := 0;
414 if CheckBoxExactMath.Checked then begin
415 if TextEnteredColumn(0) and (StringGrid.Cells[0, 0] =
416 TAcronymMeaning(List.Items[I]).Acronym.Name) then Inc(FoundCount);
417 if TextEnteredColumn(1) and (StringGrid.Cells[1, 0] =
418 TAcronymMeaning(List.Items[I]).Name) then Inc(FoundCount);
419 if TextEnteredColumn(2) and (StringGrid.Cells[2, 0] =
420 TAcronymMeaning(List.Items[I]).Categories.GetString) then Inc(FoundCount);
421 end else begin
422 if Pos(UTF8LowerCase(StringGrid.Cells[0, 0]),
423 UTF8LowerCase(TAcronymMeaning(List.Items[I]).Acronym.Name)) > 0 then Inc(FoundCount);
424 if Pos(UTF8LowerCase(StringGrid.Cells[1, 0]),
425 UTF8LowerCase(TAcronymMeaning(List.Items[I]).Name)) > 0 then Inc(FoundCount);
426 if Pos(UTF8LowerCase(StringGrid.Cells[2, 0]),
427 UTF8LowerCase(TAcronymMeaning(List.Items[I]).Categories.GetString)) > 0 then Inc(FoundCount);
428 end;
429 if FoundCount <> EnteredCount then List.Delete(I);
430 end else List.Delete(I);
431 end;
432 end else
433 if TAcronymMeaning(List.Items[I]) is TAcronymMeaning then begin
434 List.Delete(I);
435 end;
436 end;
437end;
438
439procedure TFormMain.OpenRecentClick(Sender: TObject);
440begin
441 AFileClose.Execute;
442 if ProjectClosed then begin
443 AFileNew.Execute;
444 AcronymDb.LoadFromFile(TMenuItem(Sender).Caption);
445 LastOpenedList1.AddItem(TMenuItem(Sender).Caption);
446 UpdateAcronymsList;
447 UpdateInterface;
448 end;
449end;
450
451procedure TFormMain.UpdateAcronymsList;
452begin
453 if Assigned(AcronymDb) then begin
454 ListViewSort1.Refresh;
455 end else ListViewSort1.List.Clear;
456end;
457
458procedure TFormMain.UpdateInterface;
459var
460 Title: string;
461begin
462 ListViewAcronyms.Enabled := Assigned(AcronymDb);
463 AFileClose.Enabled := Assigned(AcronymDb);
464 AFileSave.Enabled := Assigned(AcronymDb) and AcronymDb.Modified;
465 AFileSaveAs.Enabled := Assigned(AcronymDb);
466 AImport.Enabled := Assigned(AcronymDb);
467 AShowCategories.Enabled := Assigned(AcronymDb);
468 AShowAcronyms.Enabled := Assigned(AcronymDb);
469 AShowImportSources.Enabled := Assigned(AcronymDb);
470
471 Title := Application.Title;
472 if Assigned(AcronymDb) then Title := Title + ' - ' + ExtractFileName(AcronymDb.FileName);
473 if Assigned(AcronymDb) and AcronymDb.Modified then Title := Title + ' (' + SModified + ')';
474 Caption := Title;
475end;
476
477procedure TFormMain.LoadConfig;
478begin
479 RegistryContext := RegContext(HKEY_CURRENT_USER, DefaultRegKey);
480 PersistentForm1.RegistryContext := RegistryContext;
481 LastOpenedList1.LoadFromRegistry(RegistryContext);
482
483 with TRegistryEx.Create do
484 try
485 RootKey := HKEY_CURRENT_USER;
486 OpenKey(DefaultRegKey, True);
487 if ValueExists('LanguageCode') then
488 CoolTranslator1.Language := CoolTranslator1.Languages.SearchByCode(ReadStringWithDefault('LanguageCode', ''))
489 else CoolTranslator1.Language := CoolTranslator1.Languages.SearchByCode('');
490 AlwaysOnTop := ReadBoolWithDefault('AlwaysOnTop', False);
491 CheckBoxExactMath.Checked := ReadBoolWithDefault('ExactMatch', False);
492 finally
493 Free;
494 end;
495end;
496
497procedure TFormMain.SaveConfig;
498begin
499 LastOpenedList1.SaveToRegistry(RegistryContext);
500
501 with TRegistryEx.Create do
502 try
503 RootKey := HKEY_CURRENT_USER;
504 OpenKey(DefaultRegKey, True);
505 if Assigned(CoolTranslator1.Language) and (CoolTranslator1.Language.Code <> '') then
506 WriteString('LanguageCode', CoolTranslator1.Language.Code)
507 else DeleteValue('LanguageCode');
508 WriteBool('AlwaysOnTop', AlwaysOnTop);
509 WriteBool('ExactMatch', CheckBoxExactMath.Checked);
510 finally
511 Free;
512 end;
513end;
514
515end.
516
Note: See TracBrowser for help on using the repository browser.