Ignore:
Timestamp:
Aug 8, 2016, 1:41:25 PM (8 years ago)
Author:
chronos
Message:
  • Modified: Non-visual code moved from FormMain to Core data module.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Forms/UFormMain.pas

    r83 r86  
    120120    procedure TrayIcon1Click(Sender: TObject);
    121121  private
    122     InitializeStarted: Boolean;
    123     InitializeFinished: Boolean;
    124     FAlwaysOnTop: Boolean;
    125122    RegistryContext: TRegistryContext;
    126123    ProjectClosed: Boolean;
    127124    ImportTotalItemCount: Integer;
    128     function FindFirstNonOption: string;
    129     procedure ProjectOpen(FileName: string);
    130     procedure Initialize;
    131125    procedure ProcessImportsJob(Job: TJob);
    132     procedure SetAlwaysOnTop(AValue: Boolean);
    133126    procedure FilterList(List: TListObject);
    134127    procedure OpenRecentClick(Sender: TObject);
     128  public
     129    procedure LoadConfig;
     130    procedure SaveConfig;
    135131    procedure UpdateAcronymsList;
    136132    procedure UpdateInterface;
    137     procedure LoadConfig;
    138     procedure SaveConfig;
    139   public
    140     AcronymDb: TAcronymDb;
    141     StartOnLogon: Boolean;
    142     StartMinimizedToTray: Boolean;
     133    procedure ProjectOpen(FileName: string);
    143134    function CompareStrings(Strings1, Strings2: TStrings): Boolean;
    144     property AlwaysOnTop: Boolean read FAlwaysOnTop write SetAlwaysOnTop;
    145135  end;
    146136
     
    159149uses
    160150  UFormImport, UFormSettings, UFormCategories, UFormAcronyms,
    161   UFormImportSources, UFormAbout, UFormImportFormats;
     151  UFormImportSources, UFormAbout, UFormImportFormats, UCore;
    162152
    163153resourcestring
     
    171161  DefaultRegKey = '\Software\Chronosoft\Acronym Decoder';
    172162  RegistryRunKey = '\Software\Microsoft\Windows\CurrentVersion\Run';
    173   ExampleFile = 'Example acronyms.adp';
    174163
    175164{ TFormMain }
     
    179168  I: Integer;
    180169begin
    181   AcronymDb := TAcronymDb.Create;
    182   InitializeStarted := False;
    183   InitializeFinished := False;
    184170  for I := 0 to ToolBar1.ButtonCount - 1 do
    185171    ToolBar1.Buttons[I].Hint := ToolBar1.Buttons[I].Caption;
     
    193179procedure TFormMain.FormHide(Sender: TObject);
    194180begin
    195   if InitializeFinished then PersistentForm1.Save(Self);
     181  if Core.InitializeFinished then PersistentForm1.Save(Self);
    196182end;
    197183
     
    199185begin
    200186  PersistentForm1.Save(Self);
    201   SaveConfig;
     187  Core.SaveConfig;
    202188end;
    203189
     
    219205begin
    220206  DoClose := False;
    221   if Assigned(AcronymDb) then begin
    222     if AcronymDb.Modified then begin
     207  if Assigned(Core.AcronymDb) then begin
     208    if Core.AcronymDb.Modified then begin
    223209       ModalResult := MessageDlg(SAppExit, SAppExitQuery,
    224210       mtConfirmation, [mbYes, mbNo, mbCancel], 0);
     
    233219  end;
    234220  if DoClose then begin
    235     FreeAndNil(AcronymDb);
     221    FreeAndNil(Core.AcronymDb);
    236222    UpdateAcronymsList;
    237223    UpdateInterface;
     
    244230  AFileClose.Execute;
    245231  if ProjectClosed  then begin
    246     AcronymDb := TAcronymDb.Create;
    247     AcronymDb.FileName := DefaultFileName;
    248     AcronymDb.Acronyms.Clear;
     232    Core.AcronymDb := TAcronymDb.Create;
     233    Core.AcronymDb.FileName := DefaultFileName;
     234    Core.AcronymDb.Acronyms.Clear;
    249235    UpdateAcronymsList;
    250236    UpdateInterface;
     
    255241begin
    256242  OpenDialog1.DefaultExt := ProjectExt;
    257   if Assigned(AcronymDb) then
    258     OpenDialog1.FileName := AcronymDb.FileName;
     243  if Assigned(Core.AcronymDb) then
     244    OpenDialog1.FileName := Core.AcronymDb.FileName;
    259245  if OpenDialog1.Execute then begin
    260246    ProjectOpen(OpenDialog1.FileName);
     
    265251begin
    266252  SaveDialog1.DefaultExt := ProjectExt;
    267   SaveDialog1.FileName := AcronymDb.FileName;
     253  SaveDialog1.FileName := Core.AcronymDb.FileName;
    268254  if SaveDialog1.Execute then begin
    269     AcronymDb.SaveToFile(SaveDialog1.FileName);
     255    Core.AcronymDb.SaveToFile(SaveDialog1.FileName);
    270256    LastOpenedList1.AddItem(SaveDialog1.FileName);
    271257    UpdateInterface;
     
    275261procedure TFormMain.AFileSaveExecute(Sender: TObject);
    276262begin
    277   if FileExists(AcronymDb.FileName) then begin
    278     AcronymDb.SaveToFile(AcronymDb.FileName);
    279     LastOpenedList1.AddItem(AcronymDb.FileName);
     263  if FileExists(Core.AcronymDb.FileName) then begin
     264    Core.AcronymDb.SaveToFile(Core.AcronymDb.FileName);
     265    LastOpenedList1.AddItem(Core.AcronymDb.FileName);
    280266    UpdateInterface;
    281267  end else AFileSaveAs.Execute;
     
    292278begin
    293279  ImportTotalItemCount := 0;
    294   AcronymDb.AddedCount := 0;
     280  Core.AcronymDb.AddedCount := 0;
    295281  JobProgressView1.AddJob(SProcessImportSources, ProcessImportsJob);
    296282  JobProgressView1.Start;
    297   ShowMessage(Format(SAddedCount, [ImportTotalItemCount, AcronymDb.AddedCount]));
     283  ShowMessage(Format(SAddedCount, [ImportTotalItemCount, Core.AcronymDb.AddedCount]));
    298284  UpdateAcronymsList;
    299285  UpdateInterface;
     
    304290  I: Integer;
    305291begin
    306   for I := 0 to AcronymDb.ImportSources.Count - 1 do
    307   with TImportSource(AcronymDb.ImportSources[I]) do
     292  for I := 0 to Core.AcronymDb.ImportSources.Count - 1 do
     293  with TImportSource(Core.AcronymDb.ImportSources[I]) do
    308294  if Enabled then begin
    309295    Process;
    310296    ImportTotalItemCount := ImportTotalItemCount + ItemCount;
    311     Job.Progress.Max := AcronymDb.ImportSources.Count;
     297    Job.Progress.Max := Core.AcronymDb.ImportSources.Count;
    312298    Job.Progress.Value := I;
    313299    if Job.Terminate then Break;
     
    320306  if FormSettings.ShowModal = mrOk then begin
    321307    FormSettings.Save;
    322     SaveConfig;
     308    Core.SaveConfig;
    323309  end;
    324310end;
     
    338324procedure TFormMain.AShowCategoriesExecute(Sender: TObject);
    339325begin
    340   FormCategories.Categories := AcronymDb.Categories;
     326  FormCategories.Categories := Core.AcronymDb.Categories;
    341327  FormCategories.ShowModal;
    342328  UpdateAcronymsList;
     
    352338procedure TFormMain.AShowImportFormatsExecute(Sender: TObject);
    353339begin
    354   FormImportFormats.ImportFormats := AcronymDb.ImportFormats;
     340  FormImportFormats.ImportFormats := Core.AcronymDb.ImportFormats;
    355341  FormImportFormats.ShowModal;
    356342  UpdateInterface;
     
    359345procedure TFormMain.AShowImportSourcesExecute(Sender: TObject);
    360346begin
    361   FormImportSources.ImportSources := AcronymDb.ImportSources;
     347  FormImportSources.ImportSources := Core.AcronymDb.ImportSources;
    362348  FormImportSources.ShowModal;
    363349  UpdateAcronymsList;
     
    377363procedure TFormMain.FormDestroy(Sender: TObject);
    378364begin
    379   FreeAndNil(AcronymDb);
    380365end;
    381366
    382367procedure TFormMain.FormShow(Sender: TObject);
    383368begin
    384   Initialize;
     369  Core.Initialize;
    385370
    386371  if Visible then begin
     
    442427procedure TFormMain.ListViewSort1Filter(ListViewSort: TListViewSort);
    443428begin
    444   AcronymDb.AssignToList(ListViewSort1.List);
     429  Core.AcronymDb.AssignToList(ListViewSort1.List);
    445430  FilterList(ListViewSort1.List);
    446431end;
     
    456441  if not Visible then AShow.Execute
    457442    else Hide;
    458 end;
    459 
    460 function TFormMain.FindFirstNonOption: string;
    461 var
    462   S: string;
    463   I: Integer;
    464 begin
    465   Result := '';
    466   for I := 1 to Application.ParamCount do begin
    467     S := Application.Params[I];
    468     if S[1] = Application.OptionChar then Continue;
    469     Result := S;
    470     Break;
    471   end;
    472443end;
    473444
     
    478449    try
    479450      AFileNew.Execute;
    480       AcronymDb.LoadFromFile(FileName);
     451      Core.AcronymDb.LoadFromFile(FileName);
    481452      LastOpenedList1.AddItem(FileName);
    482453    finally
     
    484455      UpdateInterface;
    485456    end;
    486   end;
    487 end;
    488 
    489 procedure TFormMain.Initialize;
    490 var
    491   FileNameOption: string;
    492 begin
    493   if not InitializeStarted then begin
    494     InitializeStarted := True;
    495     LoadConfig;
    496 
    497     {$IFDEF 0}
    498     if Application.HasOption('h', 'help') then begin
    499       WriteLn('AcronymDecoder <project file>');
    500       WriteLn('  -t    --tray   Start minimized in system tray');
    501       WriteLn('  -h    --help  Show this help');
    502       Application.Terminate;
    503       Exit;
    504     end;
    505     {$ENDIF}
    506 
    507     if Application.HasOption('t', 'tray') then begin
    508       Visible := False;
    509     end;
    510 
    511     FileNameOption := FindFirstNonOption;
    512     if FileNameOption <> '' then begin
    513       // Open file specified as command line parameter
    514       AcronymDB.LoadFromFile(FileNameOption);
    515       LastOpenedList1.AddItem(OpenDialog1.FileName);
    516     end else
    517     if (LastOpenedList1.Items.Count > 0) and FileExists(LastOpenedList1.Items[0]) then begin
    518       // Open last opened file
    519       AcronymDB.LoadFromFile(LastOpenedList1.Items[0])
    520     end else begin
    521       // Open default database with examples if no item is in recent openned history
    522       FileNameOption := ExtractFileDir(Application.ExeName) + DirectorySeparator + ExampleFile;
    523       {$IFDEF Linux}
    524       // If installed in Linux system then use installation directory for po files
    525       if Application.ExeName = '/usr/bin/' + ExtractFileNameOnly(Application.ExeName) then
    526         FileNameOption := '/usr/share/' + ExtractFileNameOnly(Application.ExeName) + '/Examples/' + ExampleFile;
    527       {$ENDIF}
    528       ProjectOpen(FileNameOption);
    529     end;
    530     UpdateAcronymsList;
    531     ListViewFilter1.UpdateFromListView(ListViewAcronyms);
    532     InitializeFinished := True;
    533457  end;
    534458end;
     
    545469      Exit;
    546470    end;
    547 end;
    548 
    549 procedure TFormMain.SetAlwaysOnTop(AValue: Boolean);
    550 begin
    551   if FAlwaysOnTop = AValue then Exit;
    552   FAlwaysOnTop := AValue;
    553   if FAlwaysOnTop then FormStyle := fsSystemStayOnTop
    554     else FormStyle := fsNormal;
    555471end;
    556472
     
    598514  if ProjectClosed then begin
    599515    AFileNew.Execute;
    600     AcronymDb.LoadFromFile(TMenuItem(Sender).Caption);
     516    Core.AcronymDb.LoadFromFile(TMenuItem(Sender).Caption);
    601517    LastOpenedList1.AddItem(TMenuItem(Sender).Caption);
    602518    UpdateAcronymsList;
     
    607523procedure TFormMain.UpdateAcronymsList;
    608524begin
    609   if Assigned(AcronymDb) then begin
     525  if Assigned(Core.AcronymDb) then begin
    610526    ListViewSort1.Refresh;
    611527  end else begin
     
    620536  Title: string;
    621537begin
    622   ListViewAcronyms.Enabled := Assigned(AcronymDb);
    623   AFileClose.Enabled := Assigned(AcronymDb);
    624   AFileSave.Enabled := Assigned(AcronymDb) and AcronymDb.Modified;
    625   AFileSaveAs.Enabled := Assigned(AcronymDb);
    626   AManualImport.Enabled := Assigned(AcronymDb);
    627   AProcessImports.Enabled := Assigned(AcronymDb);
    628   AShowCategories.Enabled := Assigned(AcronymDb);
    629   AShowAcronyms.Enabled := Assigned(AcronymDb);
    630   AShowCategories.Enabled := Assigned(AcronymDb);
    631   AShowImportSources.Enabled := Assigned(AcronymDb);
    632   AShowImportFormats.Enabled := Assigned(AcronymDb);
     538  ListViewAcronyms.Enabled := Assigned(Core.AcronymDb);
     539  AFileClose.Enabled := Assigned(Core.AcronymDb);
     540  AFileSave.Enabled := Assigned(Core.AcronymDb) and Core.AcronymDb.Modified;
     541  AFileSaveAs.Enabled := Assigned(Core.AcronymDb);
     542  AManualImport.Enabled := Assigned(Core.AcronymDb);
     543  AProcessImports.Enabled := Assigned(Core.AcronymDb);
     544  AShowCategories.Enabled := Assigned(Core.AcronymDb);
     545  AShowAcronyms.Enabled := Assigned(Core.AcronymDb);
     546  AShowCategories.Enabled := Assigned(Core.AcronymDb);
     547  AShowImportSources.Enabled := Assigned(Core.AcronymDb);
     548  AShowImportFormats.Enabled := Assigned(Core.AcronymDb);
    633549  ToolBar1.Visible := MenuItemToolbar.Checked;
    634550
    635551  Title := Application.Title;
    636   if Assigned(AcronymDb) and (ExtractFileName(AcronymDb.FileName) <> '') then
    637     if Assigned(AcronymDb) then Title := Title + ' - ' + ExtractFileName(AcronymDb.FileName);
    638   if Assigned(AcronymDb) and AcronymDb.Modified then Title := Title + ' (' + SModified + ')';
     552  if Assigned(Core.AcronymDb) and (ExtractFileName(Core.AcronymDb.FileName) <> '') then
     553    if Assigned(Core.AcronymDb) then Title := Title + ' - ' + ExtractFileName(Core.AcronymDb.FileName);
     554  if Assigned(Core.AcronymDb) and Core.AcronymDb.Modified then Title := Title + ' (' + SModified + ')';
    639555  Caption := Title;
    640556end;
     
    660576      CoolTranslator1.Language := CoolTranslator1.Languages.SearchByCode(ReadStringWithDefault('LanguageCode', ''))
    661577      else CoolTranslator1.Language := CoolTranslator1.Languages.SearchByCode('');
    662     AlwaysOnTop := ReadBoolWithDefault('AlwaysOnTop', False);
    663     StartMinimizedToTray := ReadBoolWithDefault('StartMinimizedToTray', False);
     578    Core.AlwaysOnTop := ReadBoolWithDefault('AlwaysOnTop', False);
     579    Core.StartMinimizedToTray := ReadBoolWithDefault('StartMinimizedToTray', False);
    664580    CheckBoxExactMath.Checked := ReadBoolWithDefault('ExactMatch', False);
    665581    MenuItemToolbar.Checked := ReadBoolWithDefault('ToolBarVisible', True);
     
    672588    RootKey := HKEY_CURRENT_USER;
    673589    OpenKey(RegistryRunKey, True);
    674     StartOnLogon := ValueExists('Acronym Decoder');
     590    Core.StartOnLogon := ValueExists('Acronym Decoder');
    675591  finally
    676592    Free;
     
    690606      WriteString('LanguageCode', CoolTranslator1.Language.Code)
    691607      else DeleteValue('LanguageCode');
    692     WriteBool('AlwaysOnTop', AlwaysOnTop);
    693     WriteBool('StartMinimizedToTray', StartMinimizedToTray);
     608    WriteBool('AlwaysOnTop', Core.AlwaysOnTop);
     609    WriteBool('StartMinimizedToTray', Core.StartMinimizedToTray);
    694610    WriteBool('ExactMatch', CheckBoxExactMath.Checked);
    695611    WriteBool('ToolBarVisible', MenuItemToolbar.Checked);
     
    702618    RootKey := HKEY_CURRENT_USER;
    703619    OpenKey(RegistryRunKey, True);
    704     if StartOnLogon then begin
    705       if StartMinimizedToTray then WriteString('Acronym Decoder', Application.ExeName + ' ' + Application.OptionChar + 't')
     620    if Core.StartOnLogon then begin
     621      if Core.StartMinimizedToTray then WriteString('Acronym Decoder', Application.ExeName + ' ' + Application.OptionChar + 't')
    706622        else WriteString('Acronym Decoder', Application.ExeName)
    707623    end else DeleteValue('Acronym Decoder');
Note: See TracChangeset for help on using the changeset viewer.