-
Notifications
You must be signed in to change notification settings - Fork 1
/
configformu.pas
539 lines (490 loc) · 16.5 KB
/
configformu.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
unit ConfigFormU;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, LazFileUtils, ListViewFilterEdit, Forms,
Controls, Graphics, Dialogs, ExtCtrls, StdCtrls, Buttons, EditBtn,IniPropStorage,
ButtonPanel, ComCtrls
{$IFDEF UNIX}
,Process
{$ENDIF}
;
type
{ TConfigForm }
TConfigForm = class(TForm)
B_LoadPlugIn: TBitBtn;
B_LoadTheme: TBitBtn;
ButtonPanel1: TButtonPanel;
B_FontSelect: TSpeedButton;
CB_AutoOpenEdfiles: TCheckBox;
CB_AutoOpenPrjF: TCheckBox;
CB_CtrlAdvMode: TCheckBox;
CB_FilesMonitor: TCheckBox;
CB_BakTimeStamp: TCheckBox;
CB_LibAsReadOnly: TCheckBox;
Ed_PlEnabled: TCheckBox;
Ed_DefAuthor: TLabeledEdit;
Ed_EditorFontName: TComboBox;
Ed_EditorFontSize: TComboBox;
Ed_LibraryDir: TDirectoryEdit;
Ed_ProgramsDir: TDirectoryEdit;
Ed_ProjectsDir: TDirectoryEdit;
Ed_SnippetsDir: TDirectoryEdit;
Ed_LoadPlugIn: TFileNameEdit;
FontDialog1: TFontDialog;
Label7: TLabel;
Label8: TLabel;
L_PlugInVersion: TLabel;
L_PlugInName: TLabel;
LV_PlugIns: TListView;
Panel1: TPanel;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
L_ConfigDir: TLabel;
Notebook1: TNotebook;
ed_SelTheme: TRadioGroup;
Panel3: TPanel;
Panel4: TPanel;
PlugIns: TPage;
PlugInsFilter: TListViewFilterEdit;
Theme: TPage;
Paths: TPage;
Parameters: TPage;
Editor: TPage;
Panel2: TPanel;
Ed_SBAversion: TRadioGroup;
Splitter1: TSplitter;
TreeView1: TTreeView;
procedure B_FontSelectClick(Sender: TObject);
procedure B_LoadPlugInClick(Sender: TObject);
procedure B_LoadThemeClick(Sender: TObject);
procedure Ed_LoadPlugInAcceptFileName(Sender: TObject; var Value: String);
procedure Ed_PlEnabledClick(Sender: TObject);
procedure ed_SelThemeSelectionChanged(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure LV_PlugInsClick(Sender: TObject);
procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
private
procedure ListPlugIns;
{ private declarations }
public
{ public declarations }
end;
const //based in sub dirs in zip file from Github
DefSBAbaseDir='SBA-master';
DefLibraryDir='SBA-Library';
DefSnippetsDir='SBA-Snippets';
DefProgramsDir='SBA-Programs';
DefProjectsDir='sbaprojects';
cSBAbaseZipFile='sbamaster.zip';
cSBAlibraryZipFile='sbalibrary.zip';
cSBAprogramsZipFile='sbaprograms.zip';
cSBAsnippetsZipFile='sbasnippets.zip';
cSBARepoZipFile='/archive/master.zip';
cSBAthemeZipFile='theme.zip';
cSBADocZipFile='doc.zip';
cLocSBAprjparams='lprjparams.ini'; // Save the local parameters for each project file
cSBApluginsZipFile='plugins.zip';
cDefNewFileName='NewFile';
var
ConfigForm: TConfigForm;
ConfigFile,AppDir,ConfigDir,LibraryDir,SnippetsDir,ProgramsDir,
ProjectsDir,SBAbaseDir,ThemeDir,ThemeFile,TempFolder:string;
LocSBAPrjParams:string; //Local associated Prj parameters ini file
DefAuthor:string;
LibAsReadOnly:Boolean;
AutoOpenPrjF:Boolean;
AutoOpenEdFiles:Boolean;
CtrlAdvMode:Boolean;
EnableFilesMon:Boolean;
BakTimeStamp:Boolean;
IpCoreList,SnippetsList,ProgramsList:Tstringlist;
SBAversion:integer;
SelTheme:integer;
function GetConfigValues:boolean;
function SetConfigValues(IniProp:TIniPropStorage):boolean;
function SetUpConfig:boolean;
function SBAVersionToStr(v:integer):string;
function StrToSBAVersion(s:string):integer;
procedure UpdateLists;
implementation
uses MainFormU, SBAProgramU, UtilsU, DebugU, EditorU, PlugInU;
{$R *.lfm}
var
TempPlugIn:TPlugIn; //Keep this private
function SBAVersionToStr(v: integer): string;
begin
case v of
0: Result:='1.1';
1: Result:='1.2';
else Result:='1.1';
end;
end;
function StrToSBAVersion(s:string): integer;
begin
case s of
'1.1' : Exit(0);
'1.2' : Exit(1);
else Exit(0);
end;
end;
procedure UpdateLists;
begin
GetAllFileNamesOnly(LibraryDir,'*.ini',IpCoreList);
GetAllFileNamesOnly(SnippetsDir,'*.snp',SnippetsList);
GetAllFileNamesOnly(ProgramsDir,'*.prg',ProgramsList);
end;
function SetUpConfig: boolean;
{$IFDEF UNIX}
var s:string; //dummy string for RunCommandIndir
{$ENDIF}
begin
result:=false;
Info('SetUpConfig','ConfigDir= '+ConfigDir);
If Not DirectoryExistsUTF8(ConfigDir) then
If Not ForceDirectoriesUTF8(ConfigDir) Then
begin
ShowMessage('Failed to create config folder!: '+ConfigDir);
Exit;
end;
Info('SetUpConfig','ProjectsDir= '+ProjectsDir);
If Not DirectoryExistsUTF8(ProjectsDir) then
If Not ForceDirectoriesUTF8(ProjectsDir) Then
begin
ShowMessage('Failed to create SBA projects folder: '+ProjectsDir+#13#10' the system is going to try with the default path: '+GetUserDir+DefProjectsDir+PathDelim);
ProjectsDir:=GetUserDir+DefProjectsDir+PathDelim;
If Not ForceDirectoriesUTF8(ProjectsDir) Then Exit;
end;
Info('SetUpConfig','LibraryDir= '+LibraryDir);
If Not DirectoryExistsUTF8(LibraryDir) then
If not Unzip(AppDir+cSBAlibraryZipFile,ConfigDir) then
begin
ShowMessage('Failed to create SBA library folder: '+LibraryDir);
Exit;
end;
Info('SetUpConfig','SnippetsDir= '+SnippetsDir);
If Not DirectoryExistsUTF8(SnippetsDir) then
If not Unzip(AppDir+cSBAsnippetsZipFile,ConfigDir) then
begin
ShowMessage('Failed to create code snippets folder: '+SnippetsDir);
// Exit; // Non Critical
end;
Info('SetUpConfig','ProgramsDir= '+ProgramsDir);
If Not DirectoryExistsUTF8(ProgramsDir) then
If not Unzip(AppDir+cSBAprogramsZipFile,ConfigDir) then
begin
ShowMessage('Failed to create code programs folder: '+ProgramsDir);
// Exit; // Non Critical
end;
Info('SetUpConfig','SBAbaseDir= '+SBAbaseDir);
If FileExists(AppDir+cSBABaseZipFile) then
If Unzip(AppDir+cSBABaseZipFile,ConfigDir) then DeleteFile(AppDir+cSBABaseZipFile)
else begin
ShowMessage('Failed to create SBA base folder: '+SBAbaseDir);
Exit;
end;
Info('SetUpConfig','ThemeDir= '+ConfigDir+'theme');
If FileExists(AppDir+cSBAthemeZipFile) then
If Unzip(AppDir+cSBAthemeZipFile,ConfigDir+'theme') then DeleteFile(AppDir+cSBAthemeZipFile)
else begin
ShowMessage('Failed to create theme folder: '+ConfigDir+'theme');
// Exit; // Non Critical
end;
Info('SetUpConfig','PlugInsDir= '+ConfigDir+'plugins');
If FileExists(AppDir+cSBApluginsZipFile) then
If Unzip(AppDir+cSBApluginsZipFile,ConfigDir+'plugins') then DeleteFile(AppDir+cSBApluginsZipFile)
else begin
ShowMessage('Failed to create plugins folder: '+ConfigDir+'plugins');
// Exit; // Non Critical
end;
Info('SetUpConfig','DocDir= '+ConfigDir+'doc');
If FileExists(AppDir+cSBADocZipFile) then
If Unzip(AppDir+cSBADocZipFile,ConfigDir+'doc') then DeleteFile(AppDir+cSBADocZipFile)
else begin
ShowMessage('Failed to create doc help folder: '+ConfigDir+'doc');
// Exit; // Non Critical
end;
if FileExists(AppDir+cSBADefPrgTemplate) then if CopyFile(AppDir+cSBADefPrgTemplate,ConfigDir+cSBADefPrgTemplate) then DeleteFile(AppDir+cSBADefPrgTemplate);
if FileExists(AppDir+cSBAAdvPrgTemplate) then if CopyFile(AppDir+cSBAAdvPrgTemplate,ConfigDir+cSBAAdvPrgTemplate) then DeleteFile(AppDir+cSBAAdvPrgTemplate);
if FileExists(AppDir+'newbanner.gif') then if CopyFile(AppDir+'banner.gif',ConfigDir+'newbanner.gif') then DeleteFile(AppDir+'newbanner.gif');
if FileExists(AppDir+'templates.ini') then if CopyFile(AppDir+'templates.ini',ConfigDir+'templates.ini') then DeleteFile(AppDir+'templates.ini');
{$IFDEF UNIX}
RunCommandInDir(AppDir,'/bin/bash',['-c','chmod -R +x .'],s);
RunCommandInDir(ConfigDir+'plugins','/bin/bash',['-c','chmod -R +x .'],s);
{$ENDIF}
result:=true;
end;
function GetConfigValues: boolean;
begin
result:=false;
AppDir:=Application.location;
Info('GetConfigValues','Application folder: '+AppDir);
{$IFDEF Darwin}
AppDir := AppendPathDelim(copy(AppDir,1,Pos('/SBAcreator.app',AppDir)-1));
{$ENDIF}
If not FileExistsUTF8(GetAppConfigFile(false)) then
begin
MainForm.Top:=0;
MainForm.Left:=0;
end;
With MainForm.IniStor do
begin
ConfigFile:=GetAppConfigFile(false);
IniFileName:=ConfigFile;
Info('GetConfigValues','Config File: '+IniFileName);
ConfigDir:=ReadString('ConfigDir',GetAppConfigDirUTF8(false));
SBAbaseDir:=ReadString('SBAbaseDir',ConfigDir+DefSBAbaseDir+PathDelim);
LibraryDir:=ReadString('LibraryDir',ConfigDir+DefLibraryDir+PathDelim);
SnippetsDir:=ReadString('SnippetsDir',ConfigDir+DefSnippetsDir+PathDelim);
ProgramsDir:=ReadString('ProgramsDir',ConfigDir+DefProgramsDir+PathDelim);
ProjectsDir:=ReadString('ProjectsDir',GetUserDir+DefProjectsDir+PathDelim);
ThemeDir:=ReadString('ThemeDir',ConfigDir+'theme'+PathDelim);
LocSBAPrjParams:=ConfigDir+cLocSBAprjparams;
DefAuthor:=ReadString('DefAuthor','Author');
EditorFontName:=ReadString('EditorFontName','Courier New');
EditorFontSize:=ReadInteger('EditorFontSize',10);
if Screen.Fonts.IndexOf(EditorFontName)=-1 then EditorFontName:='Courier New';
LibAsReadOnly:=ReadBoolean('LibAsReadOnly',true);
AutoOpenPrjF:=ReadBoolean('AutoOpenPrjF',true);
AutoOpenEdFiles:=ReadBoolean('AutoOpenEdFiles',true);
CtrlAdvMode:=ReadBoolean('CtrlAdvMode',false);
EnableFilesMon:=ReadBoolean('EnableFilesMon',true);
BakTimeStamp:=ReadBoolean('BakTimeStamp',false);
SBAversion:=ReadInteger('SBAversion',1);
SelTheme:=ReadInteger('SelTheme',0);
end;
result:=true;
end;
function SetConfigValues(IniProp:TIniPropStorage):boolean;
begin
result:=false;
with ConfigForm, IniProp do
begin
WriteString('ConfigDir',ConfigDir);
{ TODO : Forzar la creación de directorios}
Ed_LibraryDir.text:=AppendPathDelim(TrimFilename(Ed_LibraryDir.text));
if LibraryDir<>Ed_LibraryDir.text then
begin
if DirectoryExistsUTF8(Ed_LibraryDir.text) then LibraryDir:=Ed_LibraryDir.text
else begin
showmessage('Library folder could not be changed');
exit;
end;
WriteString('LibraryDir',LibraryDir);
end;
Ed_SnippetsDir.text:=AppendPathDelim(TrimFilename(Ed_SnippetsDir.text));
if SnippetsDir<>Ed_SnippetsDir.text then
begin
if DirectoryExistsUTF8(Ed_SnippetsDir.text) then SnippetsDir:=Ed_SnippetsDir.text
else begin
showmessage('Snippet folder could not be changed');
exit;
end;
WriteString('SnippetsDir',SnippetsDir);
end;
//
Ed_ProgramsDir.text:=AppendPathDelim(TrimFilename(Ed_ProgramsDir.text));
if ProgramsDir<>Ed_ProgramsDir.text then
begin
if DirectoryExistsUTF8(Ed_ProgramsDir.text) then ProgramsDir:=Ed_ProgramsDir.text
else begin
showmessage('Programs folder could not be changed');
exit;
end;
WriteString('ProgramsDir',ProgramsDir);
end;
//
Ed_ProjectsDir.text:=AppendPathDelim(TrimFilename(Ed_ProjectsDir.text));
if ProjectsDir<>Ed_ProjectsDir.text then
begin
if ForceDirectoriesUTF8(Ed_ProjectsDir.text) then ProjectsDir:=Ed_ProjectsDir.text
else begin
showmessage('SBA Projects folder could not be changed');
exit;
end;
WriteString('ProjectsDir',ProjectsDir);
end;
//
DefAuthor:=Ed_DefAuthor.Text;
WriteString('DefAuthor',DefAuthor);
//
LibAsReadOnly:=CB_LibAsReadOnly.Checked;
WriteBoolean('LibAsReadOnly',LibAsReadOnly);
//
AutoOpenPrjF:=CB_AutoOpenPrjF.Checked;
WriteBoolean('AutoOpenPrjF',AutoOpenPrjF);
//
AutoOpenEdfiles:=CB_AutoOpenEdfiles.Checked;
WriteBoolean('AutoOpenEdfiles',AutoOpenEdfiles);
//
CtrlAdvMode:=CB_CtrlAdvMode.Checked;
WriteBoolean('CtrlAdvMode',CtrlAdvMode);
//
EnableFilesMon:=CB_FilesMonitor.Checked;
WriteBoolean('EnableFilesMon',EnableFilesMon);
//
BakTimeStamp:=CB_BakTimeStamp.Checked;
WriteBoolean('BakTimeStamp',BakTimeStamp);
//
EditorFontName:=Ed_EditorFontName.Text;
WriteString('EditorFontName',EditorFontName);
//
EditorFontSize:=StrToIntDef(Ed_EditorFontSize.Text,10);
WriteInteger('EditorFontSize',EditorFontSize);
//
WriteString('SBAbaseDir',SBAbaseDir);
//
SBAversion:=Ed_SBAversion.ItemIndex;
WriteInteger('SBAversion',SBAversion);
//
SelTheme:=Ed_SelTheme.ItemIndex;
WriteInteger('SelTheme',SelTheme);
end;
result:=true;
end;
{ TConfigForm }
procedure TConfigForm.FormShow(Sender: TObject);
begin
L_ConfigDir.caption:='Config Dir: '+ConfigDir;
Ed_LibraryDir.Text:=LibraryDir;
Ed_SnippetsDir.Text:=SnippetsDir;
Ed_ProgramsDir.Text:=ProgramsDir;
Ed_ProjectsDir.Text:=ProjectsDir;
Ed_DefAuthor.Text:=DefAuthor;
CB_LibAsReadOnly.checked:=LibAsReadOnly;
CB_AutoOpenPrjF.Checked:=AutoOpenPrjF;
CB_AutoOpenEdfiles.Checked:=AutoOpenEdfiles;
CB_CtrlAdvMode.Checked:=CtrlAdvMode;
CB_FilesMonitor.Checked:=EnableFilesMon;
CB_BakTimeStamp.Checked:=BakTimeStamp;
Ed_EditorFontName.Text:=EditorFontName;
Ed_EditorFontSize.Text:=IntToStr(EditorFontSize);
Ed_SBAversion.ItemIndex:=SBAversion;
Ed_SelTheme.ItemIndex:=SelTheme;
ListPlugIns;
end;
procedure TConfigForm.LV_PlugInsClick(Sender: TObject);
var
Item:TListItem;
PlugIn:TPlugIn;
begin
Item:=TListView(Sender).Selected;
if Item=nil then exit;
PlugIn:=TPlugIn(Item.Data);
Info('TConfigForm.LV_PlugInsClick',PlugIn.Name);
Ed_PlEnabled.Checked:=PlugIn.Enabled;
L_PlugInVersion.Caption:='Version: '+PlugIn.Version;
end;
procedure TConfigForm.ListPlugIns;
var
PlugIn:TPlugIn;
Data:TListViewDataItem;
begin
L_PlugInVersion.Caption:='';
L_PlugInName.Caption:='';
Ed_LoadPlugIn.Text:='';
B_LoadPlugIn.Enabled:=false;
PlugInsFilter.Items.Clear;
if assigned(PlugInsList) and (PlugInsList.Count>0) then
for PlugIn in PlugInsList do
begin
Data.Data := PlugIn;
SetLength(Data.StringArray,1);
Data.StringArray[0]:=PlugIn.Name;
PlugInsFilter.Items.Add(Data);
end;
PlugInsFilter.InvalidateFilter;
end;
procedure TConfigForm.TreeView1Change(Sender: TObject; Node: TTreeNode);
var index:Integer;
begin
index:=Notebook1.Pages.IndexOf(Node.Text);
if index>=0 then Notebook1.PageIndex:=Index;
end;
procedure TConfigForm.FormCreate(Sender: TObject);
begin
Ed_EditorFontName.items.assign(screen.fonts);
TreeView1.Selected:=TreeView1.Items[0];
end;
procedure TConfigForm.FormDestroy(Sender: TObject);
begin
Info('TConfigForm','FormDestroy');
end;
procedure TConfigForm.B_FontSelectClick(Sender: TObject);
begin
FontDialog1.Font.Name:=EditorFontName;
FontDialog1.Font.Size:=EditorFontSize;
if FontDialog1.Execute then
begin
Ed_EditorFontName.Text:=FontDialog1.Font.Name;
Ed_EditorFontSize.Text:=IntToStr(FontDialog1.Font.Size);
end;
end;
procedure TConfigForm.B_LoadPlugInClick(Sender: TObject);
var
d,s:string;
begin
if TempPlugIn=nil then exit;
d:=ConfigDir+'plugins'+PathDelim+GetDeepestDir(TempPlugIn.IniFile)+PathDelim;
if not DirectoryExists(d) then
begin
Info('TConfigForm.B_LoadPlugInClick','Rename from:'+TempPlugIn.Path+' to '+d);
if MoveDir(TempPlugIn.Path,d) then
begin
GetPlugInList;
ListPlugIns;
{$IFDEF UNIX}
RunCommandInDir(d,'/bin/bash',['-c','chmod -R +x .'],s);
{$ENDIF}
end else ShowMessage('Error: The Plugin folder cannot be created.');
end else ShowMessage('The Plugin folder already exists: '+d+' delete it manually if you want to install again.');
end;
procedure TConfigForm.B_LoadThemeClick(Sender: TObject);
begin
MainForm.LoadTheme(ThemeDir);
end;
procedure TConfigForm.Ed_LoadPlugInAcceptFileName(Sender: TObject;
var Value: String);
begin
Info('TConfigForm.Ed_LoadPlugInAcceptFileName',Value);
if assigned(TempPlugIn) then FreeAndNil(TempPlugIn);
TempPlugIn:=TestforPlugIn(Value,TempFolder+ExtractFileNameOnly(Value)+PathDelim);
if (TempPlugIn<>nil) then
begin
L_PlugInName.Caption:=TempPlugIn.Name;
B_LoadPlugIn.Enabled:=true;
end else
begin
L_PlugInName.Caption:='Invalid PlugIn file';
B_LoadPlugIn.Enabled:=false;
end;
Value:=ExtractFileName(Value);
end;
procedure TConfigForm.Ed_PlEnabledClick(Sender: TObject);
var
Item:TListItem;
PI:TPlugIn;
begin
Item:=LV_PlugIns.Selected;
if Item=nil then exit;
PI:=TPlugIn(Item.Data);
Info('TConfigForm.Ed_PlEnabledClick',PI.Enabled);
PI.Enabled:=Ed_PlEnabled.Checked;
end;
procedure TConfigForm.ed_SelThemeSelectionChanged(Sender: TObject);
begin
SelTheme:=Ed_SelTheme.ItemIndex;
end;
initialization
TempFolder:=GetTempDir(false)+'sbacreator'+PathDelim;
ForceDirectories(TempFolder);
finalization
DirDelete(TempFolder);
if assigned(TempPlugIn) then TempPlugIn.free;
end.