source: trunk/Packages/ChronisPlatform/UCore.pas

Last change on this file was 5, checked in by chronos, 12 years ago
  • Added: Required packages.
File size: 6.1 KB
Line 
1unit UCore;
2
3{$mode delphi}
4
5interface
6
7uses
8 Registry, Classes, SysUtils, FileUtil, USqlDatabase, URegistry, Forms,
9 UApplicationInfo, UCoolTranslator, UPersistentForm, UModularSystem,
10 UFormModuleList;
11
12type
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
64procedure Register;
65
66var
67 Core: TCore;
68
69implementation
70
71{$R *.lfm}
72
73uses
74 UFormMain, UFormConnection, UFormSetting;
75
76procedure Register;
77begin
78 RegisterComponents('Chronis', [TChronisPlatform]);
79end;
80
81{ TChronisPlatform }
82
83procedure TChronisPlatform.SetRegistryContext(AValue: TRegistryContext);
84begin
85 if FRegistryContext = AValue then Exit;
86 FRegistryContext := AValue;
87 if Assigned(Core) then Core.RegistryContext := AValue;
88end;
89
90constructor TChronisPlatform.Create(AOwner: TComponent);
91begin
92 inherited Create(AOwner);
93end;
94
95destructor TChronisPlatform.Destroy;
96begin
97 inherited Destroy;
98end;
99
100procedure TChronisPlatform.Init;
101begin
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);
111end;
112
113procedure TChronisPlatform.Done;
114begin
115(* FreeAndNil(FormSetting);
116 FreeAndNil(FormConnection);
117 FreeAndNil(FormMain);
118 FreeAndNil(Core); *)
119end;
120
121
122{ TCore }
123
124procedure TCore.DataModuleCreate(Sender: TObject);
125begin
126 PersistentForm := TPersistentForm.Create;
127end;
128
129procedure TCore.DataModuleDestroy(Sender: TObject);
130begin
131 FreeAndNil(PersistentForm);
132end;
133
134procedure TCore.ModuleManagerUpdate(Sender: TObject);
135begin
136 if Assigned(FormModuleList) then
137 FormModuleList.Reload;
138 FormMain.ReloadPages;
139end;
140
141procedure TCore.SetRegistryContext(AValue: TRegistryContext);
142begin
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');
148end;
149
150procedure TCore.Init;
151begin
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;
178end;
179
180procedure TCore.Done;
181begin
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);
189end;
190
191procedure TCore.InitData;
192var
193 DbRows: TDbRows;
194begin
195 try
196 DbRows := TDbRows.Create;
197 Database.Query(DbRows, '');
198 finally
199 DbRows.Free;
200 end;
201end;
202
203procedure TCore.Connect;
204begin
205 Database.Connect;
206end;
207
208procedure TCore.LoadFromRegistry(AContext: TRegistryContext);
209begin
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;
221end;
222
223procedure TCore.SaveToRegistry(AContext: TRegistryContext);
224begin
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;
236end;
237
238end.
239
Note: See TracBrowser for help on using the repository browser.