-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsm_main.pas
407 lines (362 loc) · 11.3 KB
/
sm_main.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
unit SM_Main;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus,
ComCtrls, ExtCtrls, StdCtrls,sm_srv_base,sm_client_base, sm_types, sm_web,sm_settings, sm_control;
type
{ TSManager }
TSManager = class(TForm)
GroupBox1: TGroupBox;
GroupBox2: TGroupBox;
btns: TImageList;
ListBox1: TListBox;
ListView1: TListView;
MainMenu1: TMainMenu;
Memo1: TMemo;
MenuItem1: TMenuItem;
MenuItem2: TMenuItem;
MenuItem3: TMenuItem;
MenuItem4: TMenuItem;
MenuItem6: TMenuItem;
PageControl1: TPageControl;
ManagerPopup: TPopupMenu;
SMPanel: TPanel;
StatusBar1: TStatusBar;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
ToolBar1: TToolBar;
ToolButton1: TToolButton;
ToolButton2: TToolButton;
ToolButton3: TToolButton;
ToolButton4: TToolButton;
ToolButton5: TToolButton;
TreeView1: TTreeView;
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure ListView1Click(Sender: TObject);
procedure ListView1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure MenuItem2Click(Sender: TObject);
procedure ToolButton1Click(Sender: TObject);
procedure ToolButton3Click(Sender: TObject);
procedure ToolButton4Click(Sender: TObject);
procedure TreeView1Click(Sender: TObject);
procedure InstallClick(Sender: TObject);
procedure UpdateClick(Sender: TObject);
//simba integration
procedure SetOptions(SimbaPath,ServerUrl,LocalStoragePath,DescFileName: string;FirstRun: boolean);
private
procedure LoadPackageToListView(aPackageItem: TPackageItem;idx: integer);
procedure LoadToTreeView;
procedure UpdateFileData(aFileItem: TFileItem);
procedure UpdateStats();
procedure GetFromServer();
procedure ReloadFromServer();
{ private declarations }
public
{ public declarations }
end;
var
SManager: TSManager;
Repository: TServerStorage;
Local: TClientStorage;
Loader: TDownloader;
ManagerPopup: TPopupMenu;
Index: integer;//current selected category index
option: TOption;//just test options system
implementation
{$R *.lfm}
{ TSManager }
procedure TSManager.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
end;
procedure TSManager.ListView1Click(Sender: TObject);
begin
if not assigned(ListView1.Selected) then exit;
UpdateFileData (TFileItem(ListView1.Selected.Data));
end;
procedure TSManager.ListView1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
oFileItem: TFileItemEx;
begin
if not assigned(ListView1.Selected) then exit;
if (button=mbRight) then
begin
oFileItem:=Local.Items[index].Files.ItemsEx[ListView1.Selected.Index];
case oFileItem.Installed of
0:ManagerPopup.Items[0].Caption:='Install';
1:ManagerPopup.Items[0].Caption:='Uninstall';
end;
case oFileItem.Update of
0:ManagerPopup.Items[1].Caption:='Force update';
1:ManagerPopup.Items[1].Caption:='Update';
end;
ManagerPopup.PopUp;
end;
end;
procedure TSManager.MenuItem2Click(Sender: TObject);
begin
ToolButton1Click(Sender);
end;
procedure TSManager.ToolButton1Click(Sender: TObject);
begin
Application.Terminate;
end;
procedure TSManager.ToolButton3Click(Sender: TObject);
begin
if (index < 0) or (index > TreeView1.Items.Count) then exit;
ReloadFromServer;
Local.CheckStorage(Repository);
Local.CheckUpdates(Repository);
LoadPackageToListView(Repository.Items[index],Index);
UpdateStats;
end;
procedure TSManager.ToolButton4Click(Sender: TObject);
var
i,j: integer;
ExItem: TFileItemEx;
begin
Local.CheckStorage(Repository);
for i:=0 to local.Count-1 do
begin
for j:=0 to local.Items[i].Files.Count-1 do
begin
ExItem:=local.Items[i].Files.ItemsEx[j];
if (ExItem.Update > 0) and (ExItem.Installed > 0) then
if UpdateScript(Repository.Items[i].Files[j],option) then
begin
ExItem.Update:=0;
ExItem.Version:=Repository.Items[i].Files[j].Version;
ExItem.DateModify:=Repository.Items[i].Files[j].DateModify;
ExItem.Author:=Repository.Items[i].Files[j].Author;
ExItem.Email:=Repository.Items[i].Files[j].Email;
Local.UpdateLocalXMLRegistry(option.XMLStorage);
UpdateStats;
LoadPackageToListView(Repository.Items[i],i);
end;
end;
end;
Local.UpdateLocalXMLRegistry(option.XMLStorage);
end;
procedure TSManager.TreeView1Click(Sender: TObject);
begin
if not assigned(TreeView1.Selected) then exit;
index:=TreeView1.Selected.Index;
LoadPackageToListView(TPackageItem(TreeView1.Selected.Data),Index);
UpdateFileData (TFileItem(ListView1.Items[0].Data));
end;
procedure TSManager.InstallClick(Sender: TObject);
var
loc: TFileItemEx;
rep: TFileItem;
begin
rep:=Repository.Items[index].Files.Items[ListView1.Selected.Index];
loc:= Local.Items[index].Files.ItemsEx[ListView1.Selected.Index];
// getScript(rep,option);
//ShowMessage(GetPackageUrl(oFileItem.filename)); //just function testing
case loc.Installed of
0:begin loc.Installed:=1; getScript(rep,option); end;
1:begin loc.Installed:=0; RemoveScript(rep,option); end;
end;
loc.Version:=rep.Version;
loc.DateModify:=rep.DateModify;
loc.Author:=rep.Author;
loc.EMail:=rep.EMail;
loc.FileName:=rep.FileName;
Local.UpdateLocalXMLRegistry(option.XMLStorage);
LoadPackageToListView(Repository.Items[index],index);
UpdateStats;
// oFileItem
end;
procedure TSManager.UpdateClick(Sender: TObject);
var
loc: TFileItemEx;
rep: TFileItem;
begin
rep:=Repository.Items[index].Files.Items[ListView1.Selected.Index];
loc:= Local.Items[index].Files.ItemsEx[ListView1.Selected.Index];
if (loc.Installed < 1) then exit;
case loc.update of
0:begin getScript(rep,option); end;
1:begin loc.update:=0; UpdateScript(rep,option); end;
end;
loc.Version:=rep.Version;
loc.DateModify:=rep.DateModify;
loc.Author:=rep.Author;
loc.EMail:=rep.EMail;
loc.FileName:=rep.FileName;
Local.UpdateLocalXMLRegistry(option.XMLStorage);
LoadPackageToListView(Repository.Items[index],index);
UpdateStats;
end;
Procedure TSManager.SetOptionS(SimbaPath, ServerUrl,
LocalStoragePath,DescFileName: string; FirstRun: boolean);
begin
if not FirstRun then
begin
index:=0;
SetOptionsPaths(ServerUrl,LocalStoragePath,SimbaPath,DescFileName,option);
GetFromServer();
Local := TClientStorage.Create();
Local.LoadLocalXMLRegistry(option.XMLStorage);
Local.CheckStorage(Repository);
Local.UpdateLocalXMLRegistry(option.XMLStorage);
UpdateStats;
LoadToTreeView;
end
else
begin
index:=0;
SetOptionsPaths(ServerUrl,LocalStoragePath,SimbaPath,DescFileName,option);
GetFromServer();
Repository.SaveLocalXMLRegistry(option.XMLStorage);
Local := TClientStorage.Create();
Local.LoadLocalXMLRegistry(option.XMLStorage);
Local.CheckStorage(Repository);
Local.UpdateLocalXMLRegistry(option.XMLStorage);
UpdateStats;
LoadToTreeView;
end;
end;
procedure TSManager.LoadPackageToListView(aPackageItem: TPackageItem;idx: integer);
var
I: Integer;
oListItem: TListItem;
oFileItem: TFileItemEx;
begin
ListView1.Items.Clear;
for I := 0 to aPackageItem.Files.Count - 1 do
begin
oListItem:= ListView1.Items.Add;
// oListItem:= ListView1.AddItem(aPackageItem.Files[i].FileName, aPackageItem.Files[i]);
oListItem.Data:=aPackageItem.Files[i];
oListItem.Caption:=aPackageItem.Files[i].FileName;
oListItem.SubItems.Add(aPackageItem.Files[i].Author);
oListItem.SubItems.Add(aPackageItem.Files[i].EMail);
oListItem.SubItems.Add(DateToStr(aPackageItem.Files[i].DateModify));
oFileItem:=local.items[idx].Files.ItemsEx[i];
case oFileItem.Installed of
0:begin oListItem.SubItems.Add(FloatToStr(aPackageItem.Files[i].Version)); oListItem.SubItems.Add('Not installed'); end;
1:begin
if oFileItem.update > 0 then
oListItem.SubItems.Add(FloatToStr(oFileItem.version)+'<'+FloatToStr(aPackageItem.Files[i].Version))
else
oListItem.SubItems.Add(FloatToStr(oFileItem.version));
oListItem.SubItems.Add('Installed');
end;
end;
UpdateFileData(aPackageItem.Files[i]);
//Items.AddObject(nil, FConfig.Items[i].Name, FConfig.Items[i]);
end;
if aPackageItem.Files.Count = 0 then
UpdateFileData(nil);
// Exit;
// if aPackageItem.Files.Count > 0 then
// UpdateFileData(TFileItem(ListView1.Items[0].Data));
end;
procedure TSManager.LoadToTreeView;
var
I: Integer;
TempNode: TTreeNode;
begin
TreeView1.Items.Clear;
for I := 0 to Repository.Count - 1 do
//TreeView1.Items.AddObject(nil, FConfig.Items[i].Name, FConfig.Items[i]);
begin
TempNode:=TreeView1.Items.Add(nil,Repository.Items[i].Name);
TempNode.Data:=Repository.Items[i];
end;
Index:=0;
LoadPackageToListView(TPackageItem(Repository.Items[0]),0);
end;
procedure TSManager.UpdateFileData(aFileItem: TFileItem);
var
I: Integer;
sItem: TSubItem;
begin
ListBox1.Items.Clear;
Memo1.Lines.Text:='';
if not Assigned(aFileItem) then
Exit;
Memo1.Lines.Text:=aFileItem.Description;
for I := 0 to aFileItem.SubFiles.Count - 1 do
begin
sItem:=TSubItem(aFileItem.SubFiles[i]);
ListBox1.Items.Add (sItem.FileName);
end;
end;
procedure TSManager.UpdateStats();
procedure UpdateStatusBar(category ,scripts, installed, updates: integer);
begin
with StatusBar1 do
begin
Panels[0].Text:='Categories:'+#13+IntToStr(category);
Panels[1].Text:='Scripts:'+#13+IntToStr(scripts);
Panels[2].Text:='Installed:'+#13+IntToStr(installed);
Panels[3].Text:='Available updates:'+#13+IntToStr(updates);
// Panels[4].Text:='Memory: '+#13+IntToStr(getlost);
end;
end;
var
i,j: integer;
installed,updates: integer;
catcount, scriptcount: integer;
CatItem: TPackageItem;
ExItem: TFileItemEx;
begin
Local.CheckStorage(Repository);
catcount:=Repository.Count;
scriptcount:=0;
installed:=0;
updates:=0;
for i:=0 to Repository.Count -1 do
begin
CatItem:=Repository.Items[i];
scriptcount:=scriptcount+CatItem.Files.Count;
end;
for i:=0 to local.Count-1 do
begin
for j:=0 to local.Items[i].Files.Count-1 do
begin
ExItem:=local.Items[i].Files.ItemsEx[j];
if ExItem.Installed > 0 then inc(installed);
if ExItem.Update > 0 then inc(updates);
end;
end;
UpdateStatusBar(catcount,scriptcount,installed,updates);
end;
procedure TSManager.GetFromServer();
var
XML: TMemoryStream;
begin
XML:=TMemoryStream.Create;
try
Loader:=Tdownloader.Create(option.XMLSrvDesc+option.DescFilename);
loader.Download(XML);
XML.Position:=0;
Repository := TServerStorage.Create();
Repository.LoadFromXmlStream(XML);
// Repository.SaveLocalXMLRegistry(option.XMLStorage);
finally
XML.Free;
end;
end;
{FIXME~!}
procedure TSManager.ReloadFromServer();
var
XML: TMemoryStream;
begin
XML:=TMemoryStream.Create;
try
//Repository.Clear; always error. Fix me!
loader.Download(XML);
XML.Position:=0;
Repository := TServerStorage.Create();
Repository.LoadFromXmlStream(XML);
Local.CheckStorage(Repository);
finally
XML.Free;
end;
end;
end.