source: trunk/Modules/System/ModuleSystem.pas

Last change on this file was 151, checked in by chronos, 9 months ago
File size: 4.3 KB
Line 
1unit ModuleSystem;
2
3interface
4
5uses
6 Classes, SysUtils, ModularSystem, Generics;
7
8type
9
10 { TModuleSystem }
11
12 TModuleSystem = class(TModule)
13 private
14 procedure ModuleChange(Sender: TObject; Module: TModule);
15 public
16 constructor Create(Owner: TComponent); override;
17 destructor Destroy; override;
18 procedure Start; override;
19 procedure Stop; override;
20 procedure Install; override;
21 procedure Uninstall; override;
22 procedure Upgrade; override;
23 procedure UpdateModuleList;
24 end;
25
26
27implementation
28
29uses
30 Core, SqlDatabase;
31
32{ TModuleSystem }
33
34procedure TModuleSystem.ModuleChange(Sender: TObject; Module: TModule);
35var
36 DbRows: TDbRows;
37 Data: TDictionaryStringString;
38begin
39 try
40 DbRows := TDbRows.Create;
41 Data := TDictionaryStringString.Create;
42 if Module.Installed then Data.Add('Installed', '1')
43 else Data.Add('Installed', '0');
44 Core.Core.CommonDatabase.Update('SystemModule', Data, 'Name="' + Module.Identification + '"');
45 finally
46 Data.Free;
47 DbRows.Free;
48 end;
49end;
50
51constructor TModuleSystem.Create(Owner: TComponent);
52begin
53 inherited Create(Owner);
54 Identification := 'System';
55 Title := 'Base modular system';
56 Version := '1.0';
57 License := 'GNU/LGPL v3';
58 Author := 'Chronosoft';
59end;
60
61destructor TModuleSystem.Destroy;
62begin
63 inherited;
64end;
65
66procedure TModuleSystem.Start;
67var
68 DbRows: TDbRows;
69 I: Integer;
70 Module: TModule;
71begin
72 try
73 DbRows := TDbRows.Create;
74 Core.Core.CommonDatabase.Select(DbRows, 'SystemModule', '`Name`, `Installed`');
75 for I := 0 to DbRows.Count - 1 do
76 with DbRows[I] do begin
77 Module := Manager.FindModuleByName(Items['Name']);
78 if Assigned(Module) then
79 if Items['Installed'] = '1' then Module.SetInstalledState(True)
80 else Module.SetInstalledState(False);
81 end;
82 finally
83 DbRows.Free;
84 end;
85 Manager.OnModuleChange := ModuleChange;
86 inherited;
87end;
88
89procedure TModuleSystem.Stop;
90begin
91 BeforeStop;
92 Manager.OnModuleChange := nil;
93 AfterStop;
94end;
95
96procedure TModuleSystem.Install;
97var
98 DbRows: TDbRows;
99begin
100 try
101 DbRows := TDbRows.Create;
102 Core.Core.CommonDatabase.Query(DbRows,
103 'CREATE TABLE IF NOT EXISTS `SystemModule` (' +
104 ' `Id` int(11) NOT NULL AUTO_INCREMENT,' +
105 ' `Name` varchar(255) COLLATE utf8_czech_ci NOT NULL,' +
106 ' `Title` varchar(255) COLLATE utf8_czech_ci NOT NULL,' +
107 ' `Creator` varchar(255) COLLATE utf8_czech_ci NOT NULL,' +
108 ' `Version` varchar(255) COLLATE utf8_czech_ci NOT NULL,' +
109 ' `License` varchar(255) COLLATE utf8_czech_ci NOT NULL,' +
110 ' `Installed` int(11) NOT NULL,' +
111 ' `Description` text COLLATE utf8_czech_ci NOT NULL,' +
112 ' `Dependencies` varchar(255) COLLATE utf8_czech_ci NOT NULL,' +
113 ' PRIMARY KEY (`Id`)' +
114 ') ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1 ;');
115 finally
116 DbRows.Free;
117 end;
118 UpdateModuleList;
119 inherited;
120end;
121
122procedure TModuleSystem.Uninstall;
123var
124 DbRows: TDbRows;
125begin
126 inherited;
127 try
128 DbRows := TDbRows.Create;
129 Core.Core.CommonDatabase.Query(DbRows, 'DROP TABLE IF EXISTS `SystemModule`');
130 finally
131 DbRows.Free;
132 end;
133end;
134
135procedure TModuleSystem.Upgrade;
136begin
137 inherited;
138end;
139
140procedure TModuleSystem.UpdateModuleList;
141var
142 DbRows: TDbRows;
143 I: Integer;
144 Index: Integer;
145 Data: TDictionaryStringString;
146begin
147 try
148 DbRows := TDbRows.Create;
149 Data := TDictionaryStringString.Create;
150
151 Core.Core.CommonDatabase.Select(DbRows, 'SystemModule', 'Name');
152
153 for I := 0 to Manager.Modules.Count - 1 do
154 with TModule(Manager.Modules[I]) do begin
155 Data.Clear;
156 Data.Add('Name', Identification);
157 Data.Add('Version', Version);
158 Data.Add('License', License);
159 Data.Add('Creator', Author);
160 Data.Add('Description', Description.Text);
161 Data.Add('Title', Title);
162 Data.Add('Dependencies', Dependencies.Text);
163 if Installed then Data.Add('Installed', '1')
164 else Data.Add('Installed', '0');
165
166 Index := 0;
167 while (Index < DbRows.Count) and (DbRows[Index].Items['Name'] <> Identification) do Inc(Index);
168 if Index >= DbRows.Count then Core.Core.CommonDatabase.Insert('SystemModule', Data)
169 else Core.Core.CommonDatabase.Update('SystemModule', Data, 'Name="' + Identification + '"');
170 end;
171 finally
172 Data.Free;
173 DbRows.Free;
174 end;
175end;
176
177end.
178
Note: See TracBrowser for help on using the repository browser.