1 | unit Core;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, FileUtil, Controls, ActnList, DbEngine, Translator,
|
---|
7 | ApplicationInfo, PersistentForm, Forms, RegistryEx, ScaleDPI, Theme, FormMain;
|
---|
8 |
|
---|
9 | type
|
---|
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 |
|
---|
49 | var
|
---|
50 | Core: TCore;
|
---|
51 |
|
---|
52 | const
|
---|
53 | DefaultRegKey = '\Software\Chronosoft\MyData';
|
---|
54 |
|
---|
55 |
|
---|
56 | implementation
|
---|
57 |
|
---|
58 | uses
|
---|
59 | FormTables, FormAbout, FormDatabases, FormPreferences, FormEx;
|
---|
60 |
|
---|
61 | {$R *.lfm}
|
---|
62 |
|
---|
63 | { TCore }
|
---|
64 |
|
---|
65 | procedure TCore.DataModuleCreate(Sender: TObject);
|
---|
66 | begin
|
---|
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;
|
---|
79 | end;
|
---|
80 |
|
---|
81 | procedure TCore.DataModuleDestroy(Sender: TObject);
|
---|
82 | begin
|
---|
83 | FreeAndNil(FormMain);
|
---|
84 | DbClient := nil;
|
---|
85 | FreeAndNil(Preferences);
|
---|
86 | FreeAndNil(DbManager);
|
---|
87 | end;
|
---|
88 |
|
---|
89 | procedure TCore.AExitExecute(Sender: TObject);
|
---|
90 | begin
|
---|
91 | FormMain.Close;
|
---|
92 | end;
|
---|
93 |
|
---|
94 | procedure TCore.ADatabaseDisconnectExecute(Sender: TObject);
|
---|
95 | begin
|
---|
96 | DbClient := nil;
|
---|
97 | end;
|
---|
98 |
|
---|
99 | procedure TCore.ADatabaseConnectExecute(Sender: TObject);
|
---|
100 | begin
|
---|
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;
|
---|
111 | end;
|
---|
112 |
|
---|
113 | procedure TCore.AAboutExecute(Sender: TObject);
|
---|
114 | begin
|
---|
115 | with TFormAbout.Create(nil) do
|
---|
116 | try
|
---|
117 | ApplicationInfo := ApplicationInfo1;
|
---|
118 | ShowModal;
|
---|
119 | finally
|
---|
120 | Free;
|
---|
121 | end;
|
---|
122 | end;
|
---|
123 |
|
---|
124 | procedure TCore.APreferencesExecute(Sender: TObject);
|
---|
125 | begin
|
---|
126 | with TFormPreferences.Create(nil) do
|
---|
127 | try
|
---|
128 | Load(Preferences);
|
---|
129 | if ShowModal = mrOk then Save(Preferences);
|
---|
130 | finally
|
---|
131 | Free;
|
---|
132 | end;
|
---|
133 | end;
|
---|
134 |
|
---|
135 | procedure TCore.Translator1Translate(Sender: TObject);
|
---|
136 | var
|
---|
137 | I: Integer;
|
---|
138 | begin
|
---|
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;
|
---|
142 | end;
|
---|
143 |
|
---|
144 | procedure TCore.SetDbClient(AValue: TDbClient);
|
---|
145 | begin
|
---|
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;
|
---|
159 | end;
|
---|
160 |
|
---|
161 | procedure TCore.LoadConfig;
|
---|
162 | begin
|
---|
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'));
|
---|
174 | end;
|
---|
175 |
|
---|
176 | procedure TCore.SaveConfig;
|
---|
177 | begin
|
---|
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'));
|
---|
188 | end;
|
---|
189 |
|
---|
190 | procedure TCore.Init;
|
---|
191 | var
|
---|
192 | ConnectProfile: TDbConnectProfile;
|
---|
193 | begin
|
---|
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;
|
---|
209 | end;
|
---|
210 |
|
---|
211 | procedure TCore.Done;
|
---|
212 | begin
|
---|
213 | SaveConfig;
|
---|
214 | DbClient := nil;
|
---|
215 | Application.Terminate;
|
---|
216 | end;
|
---|
217 |
|
---|
218 | procedure TCore.UpdateInterface;
|
---|
219 | begin
|
---|
220 | ADatabaseDisconnect.Enabled := Assigned(DbClient);
|
---|
221 | FormMain.UpdateInterface;
|
---|
222 | end;
|
---|
223 |
|
---|
224 | end.
|
---|
225 |
|
---|