1 | unit UCore;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Registry, Classes, SysUtils, FileUtil, USqlDatabase, URegistry, Forms,
|
---|
9 | UApplicationInfo, UCoolTranslator, UPersistentForm, UModularSystem,
|
---|
10 | UFormModuleList;
|
---|
11 |
|
---|
12 | type
|
---|
13 | TChronisPlatform = class;
|
---|
14 |
|
---|
15 | { TCore }
|
---|
16 |
|
---|
17 | TCore = class(TDataModule)
|
---|
18 | CoolTranslator1: TCoolTranslator;
|
---|
19 | Database: TSqlDatabase;
|
---|
20 | ModuleManager: TModuleManager;
|
---|
21 | procedure DataModuleCreate(Sender: TObject);
|
---|
22 | procedure DataModuleDestroy(Sender: TObject);
|
---|
23 | procedure ModuleManagerUpdate(Sender: TObject);
|
---|
24 | private
|
---|
25 | FRegistryContext: TRegistryContext;
|
---|
26 | procedure SetRegistryContext(AValue: TRegistryContext);
|
---|
27 | public
|
---|
28 | Platform: TChronisPlatform;
|
---|
29 | FormModuleList: TFormModuleList;
|
---|
30 | FormList: TForm;
|
---|
31 | PersistentForm: TPersistentForm;
|
---|
32 | procedure Init;
|
---|
33 | procedure Done;
|
---|
34 | procedure InitData;
|
---|
35 | procedure Connect;
|
---|
36 | procedure LoadFromRegistry(AContext: TRegistryContext);
|
---|
37 | procedure SaveToRegistry(AContext: TRegistryContext);
|
---|
38 | property RegistryContext: TRegistryContext read FRegistryContext
|
---|
39 | write SetRegistryContext;
|
---|
40 | end;
|
---|
41 |
|
---|
42 | { TChronisPlatform }
|
---|
43 |
|
---|
44 | TChronisPlatform = class(TComponent)
|
---|
45 | private
|
---|
46 | FOnRegisterModules: TNotifyEvent;
|
---|
47 | FOnUnregisterModules: TNotifyEvent;
|
---|
48 | FRegistryContext: TRegistryContext;
|
---|
49 | procedure SetRegistryContext(AValue: TRegistryContext);
|
---|
50 | public
|
---|
51 | constructor Create(AOwner: TComponent); override;
|
---|
52 | destructor Destroy; override;
|
---|
53 | procedure Init;
|
---|
54 | procedure Done;
|
---|
55 | property RegistryContext: TRegistryContext read FRegistryContext
|
---|
56 | write SetRegistryContext;
|
---|
57 | published
|
---|
58 | property OnRegisterModules: TNotifyEvent read FOnRegisterModules
|
---|
59 | write FOnRegisterModules;
|
---|
60 | property OnUnregisterModules: TNotifyEvent read FOnUnregisterModules
|
---|
61 | write FOnUnregisterModules;
|
---|
62 | end;
|
---|
63 |
|
---|
64 | procedure Register;
|
---|
65 |
|
---|
66 | var
|
---|
67 | Core: TCore;
|
---|
68 |
|
---|
69 | implementation
|
---|
70 |
|
---|
71 | {$R *.lfm}
|
---|
72 |
|
---|
73 | uses
|
---|
74 | UFormMain, UFormConnection, UFormSetting;
|
---|
75 |
|
---|
76 | procedure Register;
|
---|
77 | begin
|
---|
78 | RegisterComponents('Chronis', [TChronisPlatform]);
|
---|
79 | end;
|
---|
80 |
|
---|
81 | { TChronisPlatform }
|
---|
82 |
|
---|
83 | procedure TChronisPlatform.SetRegistryContext(AValue: TRegistryContext);
|
---|
84 | begin
|
---|
85 | if FRegistryContext = AValue then Exit;
|
---|
86 | FRegistryContext := AValue;
|
---|
87 | if Assigned(Core) then Core.RegistryContext := AValue;
|
---|
88 | end;
|
---|
89 |
|
---|
90 | constructor TChronisPlatform.Create(AOwner: TComponent);
|
---|
91 | begin
|
---|
92 | inherited Create(AOwner);
|
---|
93 | end;
|
---|
94 |
|
---|
95 | destructor TChronisPlatform.Destroy;
|
---|
96 | begin
|
---|
97 | inherited Destroy;
|
---|
98 | end;
|
---|
99 |
|
---|
100 | procedure TChronisPlatform.Init;
|
---|
101 | begin
|
---|
102 | Application.CreateForm(TCore, Core);
|
---|
103 | Core.Platform := Self;
|
---|
104 | Core.RegistryContext := FRegistryContext;
|
---|
105 | Application.CreateForm(TFormMain, FormMain); // first form
|
---|
106 | Application.CreateForm(TFormConnection, FormConnection);
|
---|
107 | Application.CreateForm(TFormSetting, FormSetting);
|
---|
108 | //Core := TCore.Create(Application);
|
---|
109 | //FormMain := TFormMain.Create(Application);
|
---|
110 | //FormConnection := TFormConnection.Create(Application);
|
---|
111 | end;
|
---|
112 |
|
---|
113 | procedure TChronisPlatform.Done;
|
---|
114 | begin
|
---|
115 | (* FreeAndNil(FormSetting);
|
---|
116 | FreeAndNil(FormConnection);
|
---|
117 | FreeAndNil(FormMain);
|
---|
118 | FreeAndNil(Core); *)
|
---|
119 | end;
|
---|
120 |
|
---|
121 |
|
---|
122 | { TCore }
|
---|
123 |
|
---|
124 | procedure TCore.DataModuleCreate(Sender: TObject);
|
---|
125 | begin
|
---|
126 | PersistentForm := TPersistentForm.Create;
|
---|
127 | end;
|
---|
128 |
|
---|
129 | procedure TCore.DataModuleDestroy(Sender: TObject);
|
---|
130 | begin
|
---|
131 | FreeAndNil(PersistentForm);
|
---|
132 | end;
|
---|
133 |
|
---|
134 | procedure TCore.ModuleManagerUpdate(Sender: TObject);
|
---|
135 | begin
|
---|
136 | if Assigned(FormModuleList) then
|
---|
137 | FormModuleList.Reload;
|
---|
138 | FormMain.ReloadPages;
|
---|
139 | end;
|
---|
140 |
|
---|
141 | procedure TCore.SetRegistryContext(AValue: TRegistryContext);
|
---|
142 | begin
|
---|
143 | if (FRegistryContext.RootKey = AValue.RootKey) and
|
---|
144 | (FRegistryContext.Key = AValue.Key) then Exit;
|
---|
145 | FRegistryContext := AValue;
|
---|
146 | PersistentForm.RegistryContext := RegContext(RegistryContext.RootKey,
|
---|
147 | RegistryContext.Key + '\Forms');
|
---|
148 | end;
|
---|
149 |
|
---|
150 | procedure TCore.Init;
|
---|
151 | begin
|
---|
152 | LoadFromRegistry(RegistryContext);
|
---|
153 |
|
---|
154 | try
|
---|
155 | ModuleManager.Modules.BeginUpdate;
|
---|
156 |
|
---|
157 | // Init modules
|
---|
158 | with TRegistryEx.Create do
|
---|
159 | try
|
---|
160 | Context := RegistryContext;
|
---|
161 | if Assigned(Platform.OnRegisterModules) then
|
---|
162 | Platform.OnRegisterModules(Self);
|
---|
163 | if ReadBoolWithDefault('ModuleManagerInstalled', False) then
|
---|
164 | ModuleManager.LoadFromRegistry(RegContext(RegistryContext.RootKey, RegistryContext.Key + '\Modules'))
|
---|
165 | else begin
|
---|
166 | ModuleManager.Modules.Perform([maEnable]);
|
---|
167 | ModuleManager.SaveToRegistry(RegContext(RegistryContext.RootKey, RegistryContext.Key + '\Modules'));
|
---|
168 | WriteBool('ModuleManagerInstalled', True);
|
---|
169 | end;
|
---|
170 | finally
|
---|
171 | Free;
|
---|
172 | end;
|
---|
173 | ModuleManager.Modules.Perform([maStart], [mcEnabled]);
|
---|
174 | finally
|
---|
175 | ModuleManager.Modules.EndUpdate;
|
---|
176 | end;
|
---|
177 | Connect;
|
---|
178 | end;
|
---|
179 |
|
---|
180 | procedure TCore.Done;
|
---|
181 | begin
|
---|
182 | SaveToRegistry(RegistryContext);
|
---|
183 | FormMain.Hide; // Speed up undocking with hidden main form
|
---|
184 | ModuleManager.SaveToRegistry(RegContext(RegistryContext.RootKey, RegistryContext.Key + '\Modules'));
|
---|
185 | if Assigned(FormModuleList) then FreeAndNil(FormModuleList);
|
---|
186 | ModuleManager.OnUpdate := nil;
|
---|
187 | if Assigned(Platform.OnUnregisterModules) then
|
---|
188 | Platform.OnUnregisterModules(Self);
|
---|
189 | end;
|
---|
190 |
|
---|
191 | procedure TCore.InitData;
|
---|
192 | var
|
---|
193 | DbRows: TDbRows;
|
---|
194 | begin
|
---|
195 | try
|
---|
196 | DbRows := TDbRows.Create;
|
---|
197 | Database.Query(DbRows, '');
|
---|
198 | finally
|
---|
199 | DbRows.Free;
|
---|
200 | end;
|
---|
201 | end;
|
---|
202 |
|
---|
203 | procedure TCore.Connect;
|
---|
204 | begin
|
---|
205 | Database.Connect;
|
---|
206 | end;
|
---|
207 |
|
---|
208 | procedure TCore.LoadFromRegistry(AContext: TRegistryContext);
|
---|
209 | begin
|
---|
210 | with TRegistryEx.Create do
|
---|
211 | try
|
---|
212 | Context := AContext;
|
---|
213 | Database.HostName := ReadStringWithDefault('HostName', 'localhost');
|
---|
214 | Database.Database := ReadStringWithDefault('Schema', 'dochazka');
|
---|
215 | Database.UserName := ReadStringWithDefault('UserName', 'dochazka');
|
---|
216 | Database.Password := ReadStringWithDefault('Password', '');
|
---|
217 | CoolTranslator1.Language := CoolTranslator1.Languages.SearchByCode(ReadStringWithDefault('Language', ''));
|
---|
218 | finally
|
---|
219 | Free;
|
---|
220 | end;
|
---|
221 | end;
|
---|
222 |
|
---|
223 | procedure TCore.SaveToRegistry(AContext: TRegistryContext);
|
---|
224 | begin
|
---|
225 | with TRegistryEx.Create do
|
---|
226 | try
|
---|
227 | Context := AContext;
|
---|
228 | WriteString('HostName', Database.HostName);
|
---|
229 | WriteString('Schema', Database.Database);
|
---|
230 | WriteString('UserName', Database.UserName);
|
---|
231 | WriteString('Password', Database.Password);
|
---|
232 | WriteString('Language', CoolTranslator1.Language.Code);
|
---|
233 | finally
|
---|
234 | Free;
|
---|
235 | end;
|
---|
236 | end;
|
---|
237 |
|
---|
238 | end.
|
---|
239 |
|
---|