source: trunk/Core.pas

Last change on this file was 33, checked in by chronos, 6 months ago
  • Fixed: Load main form settings before form translation.
File size: 5.5 KB
Line 
1unit Core;
2
3interface
4
5uses
6 Classes, SysUtils, FileUtil, Controls, ActnList, DbEngine, Translator,
7 ApplicationInfo, PersistentForm, Forms, RegistryEx, ScaleDPI, Theme, FormMain;
8
9type
10 { TCore }
11
12 TCore = class(TDataModule)
13 AAbout: TAction;
14 ADatabaseConnect: TAction;
15 ADatabaseDisconnect: TAction;
16 ApplicationInfo1: TApplicationInfo;
17 APreferences: TAction;
18 AExit: TAction;
19 ActionList1: TActionList;
20 ThemeManager1: TThemeManager;
21 Translator1: TTranslator;
22 ImageList1: TImageList;
23 PersistentForm1: TPersistentForm;
24 ScaleDPI1: TScaleDPI;
25 procedure AAboutExecute(Sender: TObject);
26 procedure ADatabaseConnectExecute(Sender: TObject);
27 procedure ADatabaseDisconnectExecute(Sender: TObject);
28 procedure AExitExecute(Sender: TObject);
29 procedure APreferencesExecute(Sender: TObject);
30 procedure Translator1Translate(Sender: TObject);
31 procedure DataModuleCreate(Sender: TObject);
32 procedure DataModuleDestroy(Sender: TObject);
33 private
34 FDbClient: TDbClient;
35 Initialized: Boolean;
36 procedure SetDbClient(AValue: TDbClient);
37 public
38 Preferences: TPreferences;
39 DbManager: TDbManager;
40 FormMain: TFormMain;
41 procedure LoadConfig;
42 procedure SaveConfig;
43 procedure Init;
44 procedure Done;
45 procedure UpdateInterface;
46 property DbClient: TDbClient read FDbClient write SetDbClient;
47 end;
48
49var
50 Core: TCore;
51
52const
53 DefaultRegKey = '\Software\Chronosoft\MyData';
54
55
56implementation
57
58uses
59 FormTables, FormAbout, FormDatabases, FormPreferences, FormEx;
60
61{$R *.lfm}
62
63{ TCore }
64
65procedure TCore.DataModuleCreate(Sender: TObject);
66begin
67 DbManager := TDbManager.Create;
68 Preferences := TPreferences.Create;
69
70 LoadConfig;
71
72 TFormEx.ScaleDPI := ScaleDPI1;
73 TFormEx.Translator := Translator1;
74 TFormEx.ThemeManager := ThemeManager1;
75 TFormEx.PersistentForm := PersistentForm1;
76
77 FormMain := TFormMain.Create(nil);
78 FormMain.Show;
79end;
80
81procedure TCore.DataModuleDestroy(Sender: TObject);
82begin
83 FreeAndNil(FormMain);
84 DbClient := nil;
85 FreeAndNil(Preferences);
86 FreeAndNil(DbManager);
87end;
88
89procedure TCore.AExitExecute(Sender: TObject);
90begin
91 FormMain.Close;
92end;
93
94procedure TCore.ADatabaseDisconnectExecute(Sender: TObject);
95begin
96 DbClient := nil;
97end;
98
99procedure TCore.ADatabaseConnectExecute(Sender: TObject);
100begin
101 with TFormDatabases.Create(nil) do
102 try
103 DbManager := Self.DbManager;
104 if ShowModal = mrOk then begin
105 DbClient := nil;
106 DbClient := SelectedProfile.GetClient;
107 end;
108 finally
109 Free;
110 end;
111end;
112
113procedure TCore.AAboutExecute(Sender: TObject);
114begin
115 with TFormAbout.Create(nil) do
116 try
117 ApplicationInfo := ApplicationInfo1;
118 ShowModal;
119 finally
120 Free;
121 end;
122end;
123
124procedure TCore.APreferencesExecute(Sender: TObject);
125begin
126 with TFormPreferences.Create(nil) do
127 try
128 Load(Preferences);
129 if ShowModal = mrOk then Save(Preferences);
130 finally
131 Free;
132 end;
133end;
134
135procedure TCore.Translator1Translate(Sender: TObject);
136var
137 I: Integer;
138begin
139 for I := 0 to DbManager.DataTypes.Count - 1 do begin
140 DbManager.DataTypes[I].Title := Translator1.TranslateText(DbManager.DataTypes[I].Title, DbManager.DataTypes[I].Title);
141 end;
142end;
143
144procedure TCore.SetDbClient(AValue: TDbClient);
145begin
146 if FDbClient = AValue then Exit;
147 if Assigned(FDbClient) then begin
148 FormMain.FormTables.DbClient := nil;
149 DbClient.Save;
150 FreeAndNil(FDbClient);
151 end;
152 FDbClient := AValue;
153 if Assigned(FDbClient) then begin
154 DbClient.Load;
155 Preferences.LastDatabaseName := FDbClient.ConnectProfile.Name;
156 FormMain.FormTables.DbClient := DbClient;
157 end;
158 UpdateInterface;
159end;
160
161procedure TCore.LoadConfig;
162begin
163 with TRegistryEx.Create do
164 try
165 CurrentContext := TRegistryContext.Create(rrKeyCurrentUser, DefaultRegKey);
166 Preferences.RememberDatabase := ReadBoolWithDefault('RememberDatabase', True);
167 Preferences.LastDatabaseName := ReadStringWithDefault('LastDatabaseName', '');
168 Translator1.Language := Translator1.Languages.SearchByCode(
169 ReadStringWithDefault('Language', ''));
170 finally
171 Free;
172 end;
173 DbManager.ConnectProfiles.LoadFromRegistry(TRegistryContext.Create(rrKeyCurrentUser, DefaultRegKey + '\Database'));
174end;
175
176procedure TCore.SaveConfig;
177begin
178 with TRegistryEx.Create do
179 try
180 CurrentContext := TRegistryContext.Create(rrKeyCurrentUser, DefaultRegKey);
181 WriteBool('RememberDatabase', Preferences.RememberDatabase);
182 WriteString('LastDatabaseName', Preferences.LastDatabaseName);
183 WriteString('Language', Translator1.Language.Code);
184 finally
185 Free;
186 end;
187 DbManager.ConnectProfiles.SaveToRegistry(TRegistryContext.Create(rrKeyCurrentUser, DefaultRegKey + '\Database'));
188end;
189
190procedure TCore.Init;
191var
192 ConnectProfile: TDbConnectProfile;
193begin
194 if not Initialized then begin
195 {$IFDEF UNIX}
196 // If installed in Linux system then use installation directory for po files
197 if Application.ExeName = '/usr/bin/MyData' then
198 Translator1.POFilesFolder := '/usr/share/MyData/languages';
199 {$ENDIF}
200 Initialized := True;
201 UpdateInterface;
202 if Preferences.RememberDatabase then begin
203 ConnectProfile := DbManager.ConnectProfiles.SearchByName(Preferences.LastDatabaseName);
204 if Assigned(ConnectProfile) then begin
205 DbClient := ConnectProfile.GetClient;
206 end;
207 end else ADatabaseConnect.Execute;
208 end;
209end;
210
211procedure TCore.Done;
212begin
213 SaveConfig;
214 DbClient := nil;
215 Application.Terminate;
216end;
217
218procedure TCore.UpdateInterface;
219begin
220 ADatabaseDisconnect.Enabled := Assigned(DbClient);
221 FormMain.UpdateInterface;
222end;
223
224end.
225
Note: See TracBrowser for help on using the repository browser.