source: trunk/Forms/UMainForm.pas

Last change on this file was 13, checked in by chronos, 6 years ago
  • Fixed: Build under Lazarus 1.8.0.
File size: 18.1 KB
Line 
1unit UMainForm;
2
3{$mode Delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
9 StdCtrls, ActnList, Menus, ExtCtrls, Contnrs, USource, UInstance,
10 UInstanceOptions, DOM, XMLWrite, XMLRead, UMainOptions,
11 USourceSelection, HTTPSend;
12
13const
14 ConfigFileName = 'Config.xml';
15
16const
17 BooleanText: array[Boolean] of string = ('No', 'Yes');
18
19type
20
21 { TMainForm }
22
23 TMainForm = class(TForm)
24 AAdd: TAction;
25 ACompile: TAction;
26 ASourceList: TAction;
27 AOptions: TAction;
28 AUpdate: TAction;
29 AEdit: TAction;
30 ARemove: TAction;
31 AStart: TAction;
32 ADownload: TAction;
33 ActionList1: TActionList;
34 ImageListLarge: TImageList;
35 ImageListSmall: TImageList;
36 MainMenu1: TMainMenu;
37 MenuItem10: TMenuItem;
38 MenuItem7: TMenuItem;
39 Label1: TLabel;
40 ListView1: TListView;
41 MenuItem1: TMenuItem;
42 MenuItem2: TMenuItem;
43 MenuItem3: TMenuItem;
44 MenuItem4: TMenuItem;
45 MenuItem5: TMenuItem;
46 MenuItem6: TMenuItem;
47 MenuItem9: TMenuItem;
48 PopupMenu1: TPopupMenu;
49 ToolBar1: TToolBar;
50 ToolButton1: TToolButton;
51 ToolButton2: TToolButton;
52 ToolButton3: TToolButton;
53 ToolButton4: TToolButton;
54 ToolButton5: TToolButton;
55 ToolButton6: TToolButton;
56 ToolButton7: TToolButton;
57 procedure AAddExecute(Sender: TObject);
58 procedure ACompileExecute(Sender: TObject);
59 procedure AEditExecute(Sender: TObject);
60 procedure AOptionsExecute(Sender: TObject);
61 procedure ARemoveExecute(Sender: TObject);
62 procedure ASourceListExecute(Sender: TObject);
63 procedure AStartExecute(Sender: TObject);
64 procedure ADownloadExecute(Sender: TObject);
65 procedure AUpdateExecute(Sender: TObject);
66 procedure FormCreate(Sender: TObject);
67 procedure FormDestroy(Sender: TObject);
68 procedure FormShow(Sender: TObject);
69 procedure ListView1Data(Sender: TObject; Item: TListItem);
70 procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
71 Selected: Boolean);
72 private
73 procedure ReloadList;
74 procedure LoadInstanceList;
75 procedure SaveInstanceList;
76 public
77 Platform: string;
78 TemplateDir: string;
79 BinUtilsDir: string;
80 InstanceDir: string;
81 SubversionDir: string;
82 SourceDir: string;
83 SourceURL: string;
84 Instances: TInstanceList;
85 Sources: TSourceList;
86 function GetBinUtilsDir: string;
87 function GetCompilerPath: string;
88 function GetBaseDir: string;
89 procedure InstallDependencies;
90 end;
91
92var
93 MainForm: TMainForm;
94
95implementation
96
97{$R *.lfm}
98
99{ TMainForm }
100
101procedure TMainForm.FormCreate(Sender: TObject);
102begin
103 Instances := TInstanceList.Create;
104 Sources := TSourceList.Create;
105 BinUtilsDir := 'BinUtils';
106 InstanceDir := 'Instance';
107 TemplateDir := 'Template';
108 SourceDir := 'Source';
109 SourceURL := 'http://tv.zdechov.net/fpc/SourceList.xml';
110 {$IFDEF Windows}
111 Platform := 'i386-win32';
112 SubversionDir := 'C:\Program Files\Subversion\bin';
113 {$ENDIF}
114 {$IFDEF Linux}
115 Platform := 'i386-linux';
116 SubversionDir := '';
117 {$ENDIF}
118 LoadInstanceList;
119end;
120
121procedure TMainForm.AAddExecute(Sender: TObject);
122begin
123 InstanceOptionsForm.Instance := nil;
124 if InstanceOptionsForm.ShowModal = mrOK then begin
125 Instances.Add(InstanceOptionsForm.Instance);
126 ReloadList;
127 end;
128end;
129
130procedure TMainForm.ACompileExecute(Sender: TObject);
131begin
132 if Assigned(ListView1.Selected) then
133 if ListView1.Selected.Index < Instances.Count then
134 with TInstance(Instances[ListView1.Selected.Index]) do begin
135 Build;
136 ReloadList;
137 end;
138end;
139
140procedure TMainForm.AEditExecute(Sender: TObject);
141begin
142 if Assigned(ListView1.Selected) then begin
143 InstanceOptionsForm.Instance := ListView1.Selected.Data;
144 if InstanceOptionsForm.ShowModal = mrOk then
145 ReloadList;
146 end;
147end;
148
149procedure TMainForm.AOptionsExecute(Sender: TObject);
150begin
151 MainOptionsForm.ShowModal;
152end;
153
154procedure TMainForm.ARemoveExecute(Sender: TObject);
155begin
156 if Assigned(ListView1.Selected) then
157 if ListView1.Selected.Index < Instances.Count then begin
158 Instances.Remove(ListView1.Selected.Data);
159 ReloadList;
160 end;
161end;
162
163procedure TMainForm.ASourceListExecute(Sender: TObject);
164begin
165 SourceSelectionForm.ProjectType := '';
166 SourceSelectionForm.ShowModal;
167end;
168
169procedure TMainForm.AStartExecute(Sender: TObject);
170begin
171 if Assigned(ListView1.Selected) then
172 if ListView1.Selected.Index < Instances.Count then begin
173 TInstance(Instances[ListView1.Selected.Index]).Start;
174 ReloadList;
175 end;
176end;
177
178procedure TMainForm.ADownloadExecute(Sender: TObject);
179begin
180 if Assigned(ListView1.Selected) then
181 if ListView1.Selected.Index < Instances.Count then
182 with TInstance(Instances[ListView1.Selected.Index]) do begin
183 ForceDirectories(GetPath);
184 if Assigned(FPCSource) then begin
185 FPCSource.Download;
186 FPCSource.ExportTo(GetPath + DirectorySeparator + FPCSource.ProjectShortName);
187 end;
188 if Assigned(IDESource) then begin
189 IDESource.Download;
190 IDESource.ExportTo(GetPath + DirectorySeparator + IDESource.ProjectShortName);
191 end;
192 ReloadList;
193 end;
194end;
195
196procedure TMainForm.AUpdateExecute(Sender: TObject);
197begin
198 if Assigned(ListView1.Selected) then
199 if ListView1.Selected.Index < Instances.Count then
200 with TInstance(Instances[ListView1.Selected.Index]) do begin
201 if Assigned(FPCSource) then begin
202 FPCSource.Update;
203 FPCSource.ExportTo(GetPath + DirectorySeparator + FPCSource.ProjectShortName);
204 end;
205 if Assigned(IDESource) then begin
206 IDESource.Update;
207 IDESource.ExportTo(GetPath + DirectorySeparator + IDESource.ProjectShortName);
208 end;
209 ReloadList;
210 end;
211end;
212
213procedure TMainForm.FormDestroy(Sender: TObject);
214begin
215 SaveInstanceList;
216 Instances.Free;
217 Sources.Free;
218end;
219
220procedure TMainForm.FormShow(Sender: TObject);
221begin
222 ReloadList;
223 ListView1SelectItem(nil, nil, False);
224end;
225
226procedure TMainForm.ListView1Data(Sender: TObject; Item: TListItem);
227begin
228 if Item.Index < Instances.Count then
229 with TInstance(Instances[Item.Index]) do begin
230 Item.Caption := Name;
231 Item.Data := Instances[Item.Index];
232 if Assigned(FPCSource) then
233 Item.SubItems.Add(FPCSource.GetName)
234 else Item.SubItems.Add('');
235 if Assigned(IDESource) then
236 Item.SubItems.Add(IDESource.GetName)
237 else Item.SubItems.Add('');
238 Item.SubItems.Add(BooleanText[Downloaded]);
239 Item.SubItems.Add(BooleanText[Compiled]);
240 end;
241end;
242
243procedure TMainForm.ListView1SelectItem(Sender: TObject; Item: TListItem;
244 Selected: Boolean);
245begin
246 if Assigned(ListView1.Selected) then
247 with TInstance(ListView1.Selected.Data) do begin
248 AEdit.Enabled := True;
249 ACompile.Enabled := Downloaded;
250 AUpdate.Enabled := Downloaded;
251 ADownload.Enabled := not Downloaded;
252 AStart.Enabled := Downloaded and Compiled and Assigned(IDESource) and
253 FileExists(GetPath + DirectorySeparator +
254 IDESource.ProjectShortName + DirectorySeparator + IDESource.GetExecutableFile);
255 ARemove.Enabled := True;
256 end else begin
257 AEdit.Enabled := False;
258 ACompile.Enabled := False;
259 ADownload.Enabled := False;
260 AStart.Enabled := False;
261 ARemove.Enabled := False;
262 AUpdate.Enabled := False;
263 end;
264end;
265
266procedure TMainForm.ReloadList;
267begin
268 if ListView1.Items.Count <> Instances.Count then
269 ListView1.Items.Count := Instances.Count;
270 ListView1.Items[-1]; // workaround
271 ListView1.Refresh;
272end;
273
274procedure TMainForm.LoadInstanceList;
275var
276 Doc: TXMLDocument;
277 NewNode: TDOMNode;
278 NewSubNode: TDOMNode;
279 I: Integer;
280 NewSource: TSource;
281 NewInstance: TInstance;
282 Child: TDOMNode;
283begin
284 if FileExists(ConfigFileName) then
285 try
286 ReadXMLFile(Doc, UTF8Decode(ConfigFileName));
287
288 Instances.Clear;
289 Sources.Clear;
290 NewNode := Doc.DocumentElement.FindNode('Platform');
291 if Assigned(NewNode) then
292 Platform := UTF8Encode(string(NewNode.TextContent));
293 NewNode := Doc.DocumentElement.FindNode('TemplateDir');
294 if Assigned(NewNode) then
295 TemplateDir := UTF8Encode(string(NewNode.TextContent));
296 NewNode := Doc.DocumentElement.FindNode('SubversionDir');
297 if Assigned(NewNode) then
298 SubversionDir := UTF8Encode(string(NewNode.TextContent));
299 NewNode := Doc.DocumentElement.FindNode('SubversionDir');
300 if Assigned(NewNode) then
301 SubversionDir := UTF8Encode(string(NewNode.TextContent));
302 NewNode := Doc.DocumentElement.FindNode('InstanceDir');
303 if Assigned(NewNode) then
304 InstanceDir := UTF8Encode(string(NewNode.TextContent));
305 NewNode := Doc.DocumentElement.FindNode('SourceURL');
306 if Assigned(NewNode) then
307 SourceURL := UTF8Encode(string(NewNode.TextContent));
308
309 NewNode := Doc.DocumentElement.FindNode('Sources');
310 Child := NewNode.FirstChild;
311 while Assigned(Child) do begin
312 NewSource := TSource.Create;
313 with NewSource do begin
314 NewNode := Child.FindNode('Id');
315 if Assigned(NewNode) then
316 Id := StrToInt(NewNode.TextContent);
317 NewNode := Child.FindNode('ProjectName');
318 if Assigned(NewNode) then
319 ProjectName := UTF8Encode(string(NewNode.TextContent));
320 NewNode := Child.FindNode('ProjectShortName');
321 if Assigned(NewNode) then
322 ProjectShortName := UTF8Encode(string(NewNode.TextContent));
323 NewNode := Child.FindNode('ProjectType');
324 if Assigned(NewNode) then
325 ProjectType := UTF8Encode(string(NewNode.TextContent));
326 NewNode := Child.FindNode('SubversionURL');
327 if Assigned(NewNode) then
328 SubversionURL := UTF8Encode(string(NewNode.TextContent));
329 NewNode := Child.FindNode('VersionNumber');
330 if Assigned(NewNode) then
331 VersionNumber := UTF8Encode(string(NewNode.TextContent));
332 NewNode := Child.FindNode('VersionType');
333 if Assigned(NewNode) then
334 VersionType := UTF8Encode(string(NewNode.TextContent));
335 NewNode := Child.FindNode('ExecutableFile');
336 if Assigned(NewNode) then
337 ExecutableFile := UTF8Encode(string(NewNode.TextContent));
338 NewNode := Child.FindNode('SourceType');
339 if Assigned(NewNode) then
340 SourceType := TSourceType(StrToInt(NewNode.TextContent));
341 end;
342 Sources.Add(NewSource);
343 Child := Child.NextSibling;
344 end;
345
346 NewNode := Doc.DocumentElement.FindNode('Instances');
347 Child := NewNode.FirstChild;
348 while Assigned(Child) do begin
349 NewInstance := TInstance.Create;
350 with NewInstance do begin
351 NewNode := Child.FindNode('Id');
352 if Assigned(NewNode) then
353 Id := StrToInt(NewNode.TextContent);
354 NewNode := Child.FindNode('Name');
355 if Assigned(NewNode) then
356 Name := UTF8Encode(string(NewNode.TextContent));
357 NewNode := Child.FindNode('IDEDate');
358 if Assigned(NewNode) then
359 IDEDate := StrToDateTime(string(NewNode.TextContent));
360 NewNode := Child.FindNode('IDERevision');
361 if Assigned(NewNode) then
362 IDERevision := UTF8Encode(string(NewNode.TextContent));
363 NewNode := Child.FindNode('IDESource');
364 if Assigned(NewNode) then
365 IDESource := Sources.FindById(StrToInt(NewNode.TextContent));
366 NewNode := Child.FindNode('FPCDate');
367 if Assigned(NewNode) then
368 FPCDate := StrToDateTime(string(NewNode.TextContent));
369 NewNode := Child.FindNode('FPCRevision');
370 if Assigned(NewNode) then
371 FPCRevision := UTF8Encode(string(NewNode.TextContent));
372 NewNode := Child.FindNode('FPCSource');
373 if Assigned(NewNode) then
374 FPCSource := Sources.FindById(StrToInt(NewNode.TextContent));
375 end;
376 Instances.Add(NewInstance);
377 Child := Child.NextSibling;
378 end;
379 finally
380 Doc.Free;
381 end;
382end;
383
384procedure TMainForm.SaveInstanceList;
385var
386 I: Integer;
387 Doc: TXMLDocument;
388 RootNode: TDOMNode;
389 NewNode: TDOMNode;
390 NewNode2: TDOMNode;
391 NewNode3: TDOMNode;
392begin
393 Doc := TXMLDocument.Create;
394 with Doc do
395 try
396 RootNode := CreateElement('InstanceList');
397 AppendChild(RootNode);
398 with RootNode do begin
399 NewNode := OwnerDocument.CreateElement('Platform');
400 NewNode.TextContent := UTF8Decode(Platform);
401 AppendChild(NewNode);
402 NewNode := OwnerDocument.CreateElement('TemplateDir');
403 NewNode.TextContent := UTF8Decode(TemplateDir);
404 AppendChild(NewNode);
405 NewNode := OwnerDocument.CreateElement('SubversionDir');
406 NewNode.TextContent := UTF8Decode(SubversionDir);
407 AppendChild(NewNode);
408 NewNode := OwnerDocument.CreateElement('SourceDir');
409 NewNode.TextContent := UTF8Decode(SourceDir);
410 AppendChild(NewNode);
411 NewNode := OwnerDocument.CreateElement('InstanceDir');
412 NewNode.TextContent := UTF8Decode(InstanceDir);
413 AppendChild(NewNode);
414 NewNode := OwnerDocument.CreateElement('SourceURL');
415 NewNode.TextContent := UTF8Decode(SourceURL);
416 AppendChild(NewNode);
417
418 NewNode := OwnerDocument.CreateElement('Sources');
419 with NewNode do
420 for I := 0 to Sources.Count - 1 do
421 with TSource(Sources[I]) do begin
422 NewNode3 := OwnerDocument.CreateElement('Item');
423 with NewNode3 do begin
424 NewNode2 := OwnerDocument.CreateElement('Id');
425 NewNode2.TextContent := IntToStr(Id);
426 AppendChild(NewNode2);
427 NewNode2 := OwnerDocument.CreateElement('ProjectName');
428 NewNode2.TextContent := UTF8Decode(ProjectName);
429 AppendChild(NewNode2);
430 NewNode2 := OwnerDocument.CreateElement('ProjectShortName');
431 NewNode2.TextContent := UTF8Decode(ProjectShortName);
432 AppendChild(NewNode2);
433 NewNode2 := OwnerDocument.CreateElement('ProjectType');
434 NewNode2.TextContent := UTF8Decode(ProjectType);
435 AppendChild(NewNode2);
436 NewNode2 := OwnerDocument.CreateElement('SubversionURL');
437 NewNode2.TextContent := UTF8Decode(SubversionURL);
438 AppendChild(NewNode2);
439 NewNode2 := OwnerDocument.CreateElement('VersionType');
440 NewNode2.TextContent := UTF8Decode(VersionType);
441 AppendChild(NewNode2);
442 NewNode2 := OwnerDocument.CreateElement('VersionNumber');
443 NewNode2.TextContent := UTF8Decode(VersionNumber);
444 AppendChild(NewNode2);
445 NewNode2 := OwnerDocument.CreateElement('ExecutableFile');
446 NewNode2.TextContent := UTF8Decode(ExecutableFile);
447 AppendChild(NewNode2);
448 NewNode2 := OwnerDocument.CreateElement('SourceType');
449 NewNode2.TextContent := IntToStr(Byte(SourceType));
450 AppendChild(NewNode2);
451 end;
452 AppendChild(NewNode3);
453 end;
454 AppendChild(NewNode);
455
456 NewNode := OwnerDocument.CreateElement('Instances');
457 with NewNode do
458 for I := 0 to Instances.Count - 1 do
459 with TInstance(Instances[I]) do begin
460 NewNode3 := OwnerDocument.CreateElement('Id');
461 with NewNode3 do begin
462 NewNode2 := OwnerDocument.CreateElement('Id');
463 NewNode2.TextContent := IntToStr(Id);
464 AppendChild(NewNode2);
465 NewNode2 := OwnerDocument.CreateElement('Name');
466 NewNode2.TextContent := UTF8Decode(Name);
467 AppendChild(NewNode2);
468 NewNode2 := OwnerDocument.CreateElement('IDEDate');
469 NewNode2.TextContent := DateTimeToStr(IDEDate);
470 AppendChild(NewNode2);
471 NewNode2 := OwnerDocument.CreateElement('IDERevision');
472 NewNode2.TextContent := UTF8Decode(IDERevision);
473 AppendChild(NewNode2);
474 NewNode2 := OwnerDocument.CreateElement('IDESource');
475 if Assigned(IDESource) then NewNode2.TextContent := IntToStr(IDESource.Id)
476 else NewNode2.TextContent := '0';
477 AppendChild(NewNode2);
478 NewNode2 := OwnerDocument.CreateElement('FPCDate');
479 NewNode2.TextContent := DateTimeToStr(FPCDate);
480 AppendChild(NewNode2);
481 NewNode2 := OwnerDocument.CreateElement('FPCRevision');
482 NewNode2.TextContent := UTF8Decode(FPCRevision);
483 AppendChild(NewNode2);
484 NewNode2 := OwnerDocument.CreateElement('FPCSource');
485 if Assigned(IDESource) then NewNode2.TextContent := IntToStr(FPCSource.Id)
486 else NewNode2.TextContent := '0';
487 AppendChild(NewNode2);
488 end;
489 AppendChild(NewNode3);
490 end;
491 AppendChild(NewNode);
492 end;
493 WriteXMLFile(Doc, UTF8Decode(ConfigFileName));
494 finally
495 Free;
496 end;
497end;
498
499function TMainForm.GetBinUtilsDir: string;
500begin
501 {$IFDEF Windows}
502 Result := GetBaseDir + DirectorySeparator +
503 BinUtilsDir + DirectorySeparator + Platform;
504 {$ENDIF}
505 {$IFDEF Linux}
506 Result := '/usr/bin';
507 {$ENDIF}
508end;
509
510function TMainForm.GetCompilerPath: string;
511begin
512 {$IFDEF Windows}
513 Result := GetBinUtilsDir + DirectorySeparator + 'fpc';
514 {$ENDIF}
515 {$IFDEF Linux}
516 Result := '/usr/lib/fpc/2.6.2/ppcx64';
517 {$ENDIF}
518end;
519
520function TMainForm.GetBaseDir: string;
521begin
522 Result := ExtractFileDir(Application.ExeName);
523end;
524
525procedure TMainForm.InstallDependencies;
526begin
527 {$IFDEF Windows}
528 {$ENDIF}
529 {$IFDEF Linux}
530 if FileExists('yum') then begin
531 end else
532 if FileExists('apt-get') then begin
533 (* sudo apt-get install -y subversion
534 sudo apt-get install -y cvs
535 sudo apt-get install -y alien
536 sudo apt-get install -y libncurses5-dev
537 sudo apt-get install -y libgtk2.0-dev
538 sudo apt-get install -y libgdk-pixbuf-dev
539 sudo apt-get install -y libXp-dev
540 sudo apt-get install -y libgtk1.2-dev
541 sudo apt-get install -y libXxf86vm-dev
542 sudo apt-get install -y glutg3-dev
543 sudo apt-get install -y libgli-mesa-dev
544 sudo apt-get install -y mesa-utils
545 *)
546 end else ShowMessage('No package manager found.');
547 {$ENDIF}
548end;
549
550procedure DownloadFile(const Url, PathToSaveTo: string);
551var
552 fs: TFileStream;
553begin
554 fs := TFileStream.Create(PathToSaveTo, fmOpenWrite or fmCreate);
555 try
556 HttpGetBinary(Url, fs);
557 finally
558 fs.Free;
559 end;
560end;
561
562end.
563
Note: See TracBrowser for help on using the repository browser.