Changeset 2 for trunk/UMainForm.pas


Ignore:
Timestamp:
Feb 2, 2011, 11:31:12 AM (13 years ago)
Author:
chronos
Message:
  • Added: Routines for Load and Save configuration.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UMainForm.pas

    r1 r2  
    11unit UMainForm;
    22
    3 {$mode objfpc}{$H+}
     3{$mode Delphi}{$H+}
    44
    55interface
     
    77uses
    88  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
    9   StdCtrls, ActnList;
     9  StdCtrls, ActnList, Contnrs, USource, UInstance, UOptions,
     10  DOM, XMLWrite, XMLRead;
     11
     12const
     13  ConfigFileName = 'Config.xml';
    1014
    1115type
     
    1519  TForm1 = class(TForm)
    1620    AAdd: TAction;
     21    ACompile: TAction;
    1722    AOptions: TAction;
    1823    ARemove: TAction;
     
    2530    Button4: TButton;
    2631    Button5: TButton;
     32    Button6: TButton;
    2733    Label1: TLabel;
    2834    ListView1: TListView;
     35    procedure AAddExecute(Sender: TObject);
     36    procedure ACompileExecute(Sender: TObject);
     37    procedure ARemoveExecute(Sender: TObject);
     38    procedure AStartExecute(Sender: TObject);
     39    procedure AUpdateExecute(Sender: TObject);
     40    procedure FormCreate(Sender: TObject);
     41    procedure FormDestroy(Sender: TObject);
     42    procedure FormShow(Sender: TObject);
     43    procedure ListView1Data(Sender: TObject; Item: TListItem);
     44    procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
     45      Selected: Boolean);
    2946  private
    30     { private declarations }
     47    procedure ReloadList;
     48    procedure LoadInstanceList;
     49    procedure SaveInstanceList;
    3150  public
    32     { public declarations }
     51    Instances: TObjectList;
     52    Sources: TSourceList;
    3353  end;
    3454
     
    4060{$R *.lfm}
    4161
     62{ TForm1 }
     63
     64procedure TForm1.FormCreate(Sender: TObject);
     65begin
     66  Instances := TObjectList.Create;
     67  Sources := TSourceList.Create;
     68end;
     69
     70procedure TForm1.AAddExecute(Sender: TObject);
     71begin
     72  OptionsForm.ShowModal;
     73end;
     74
     75procedure TForm1.ACompileExecute(Sender: TObject);
     76begin
     77  if Assigned(ListView1.Selected) then
     78    if ListView1.Selected.Index < Instances.Count then begin
     79      TInstance(Instances[ListView1.Selected.Index]).Build;
     80      ReloadList;
     81    end;
     82end;
     83
     84procedure TForm1.ARemoveExecute(Sender: TObject);
     85begin
     86  if Assigned(ListView1.Selected) then
     87    if ListView1.Selected.Index < Instances.Count then begin
     88      Instances.Remove(ListView1.Selected.Data);
     89      ReloadList;
     90    end;
     91end;
     92
     93procedure TForm1.AStartExecute(Sender: TObject);
     94begin
     95  if Assigned(ListView1.Selected) then
     96    if ListView1.Selected.Index < Instances.Count then begin
     97      TInstance(Instances[ListView1.Selected.Index]).Start;
     98      ReloadList;
     99    end;
     100end;
     101
     102procedure TForm1.AUpdateExecute(Sender: TObject);
     103begin
     104  if Assigned(ListView1.Selected) then
     105    if ListView1.Selected.Index < Instances.Count then begin
     106      TInstance(Instances[ListView1.Selected.Index]).Update;
     107      ReloadList;
     108    end;
     109end;
     110
     111procedure TForm1.FormDestroy(Sender: TObject);
     112begin
     113  Instances.Free;
     114  Sources.Free;
     115end;
     116
     117procedure TForm1.FormShow(Sender: TObject);
     118begin
     119  ListView1SelectItem(nil, nil, False);
     120end;
     121
     122procedure TForm1.ListView1Data(Sender: TObject; Item: TListItem);
     123begin
     124  if Item.Index < Instances.Count then
     125  with TInstance(Instances[Item.Index]) do begin
     126    Item.Caption := Name;
     127    Item.Data := Instances[Item.Index];
     128    Item.SubItems.Add(FPCRevision);
     129    Item.SubItems.Add(IDERevision);
     130  end;
     131end;
     132
     133procedure TForm1.ListView1SelectItem(Sender: TObject; Item: TListItem;
     134  Selected: Boolean);
     135begin
     136  ACompile.Enabled := Assigned(ListView1.Selected);
     137  AUpdate.Enabled := Assigned(ListView1.Selected);
     138  AStart.Enabled := Assigned(ListView1.Selected);
     139  ARemove.Enabled := Assigned(ListView1.Selected);
     140  AOptions.Enabled := Assigned(ListView1.Selected);
     141end;
     142
     143procedure TForm1.ReloadList;
     144begin
     145  ListView1.Items.Count := Instances.Count;
     146  ListView1.Refresh;
     147end;
     148
     149procedure TForm1.LoadInstanceList;
     150var
     151  Doc: TXMLDocument;
     152  NewNode: TDOMNode;
     153  NewSubNode: TDOMNode;
     154  I: Integer;
     155  NewSource: TSource;
     156  NewInstance: TInstance;
     157  Child: TDOMNode;
     158begin
     159  try
     160    ReadXMLFile(Doc, UTF8Decode(ConfigFileName));
     161
     162    Instances.Free;
     163    Sources.Free;
     164
     165    NewNode := Doc.DocumentElement.FindNode('Sources');
     166    Child := NewNode.FirstChild;
     167    while Assigned(Child) do begin
     168      NewSource := TSource.Create;
     169      with NewSource do begin
     170        NewNode := Doc.DocumentElement.FindNode('Id');
     171        if Assigned(NewNode) then
     172          Id := StrToInt(NewNode.TextContent);
     173        NewNode := Doc.DocumentElement.FindNode('Name');
     174        if Assigned(NewNode) then
     175          Name := UTF8Encode(string(NewNode.TextContent));
     176        NewNode := Doc.DocumentElement.FindNode('SubversionURL');
     177        if Assigned(NewNode) then
     178          SubversionURL := UTF8Encode(string(NewNode.TextContent));
     179        NewNode := Doc.DocumentElement.FindNode('Version');
     180        if Assigned(NewNode) then
     181          Version := UTF8Encode(string(NewNode.TextContent));
     182        NewNode := Doc.DocumentElement.FindNode('Variation');
     183        if Assigned(NewNode) then
     184          Variation := UTF8Encode(string(NewNode.TextContent));
     185        NewNode := Doc.DocumentElement.FindNode('SourceType');
     186        if Assigned(NewNode) then
     187          SourceType := TSourceType(StrToInt(NewNode.TextContent));
     188      end;
     189      Child := Child.NextSibling;
     190    end;
     191
     192    NewNode := Doc.DocumentElement.FindNode('Instances');
     193    Child := NewNode.FirstChild;
     194    while Assigned(Child) do begin
     195      NewInstance := TInstance.Create;
     196      with NewInstance do begin
     197        NewNode := Doc.DocumentElement.FindNode('Id');
     198        if Assigned(NewNode) then
     199          Id := StrToInt(NewNode.TextContent);
     200        NewNode := Doc.DocumentElement.FindNode('Name');
     201        if Assigned(NewNode) then
     202          Name := UTF8Encode(string(NewNode.TextContent));
     203        NewNode := Doc.DocumentElement.FindNode('IDEDate');
     204        if Assigned(NewNode) then
     205          IDEDate := StrToDateTime(string(NewNode.TextContent));
     206        NewNode := Doc.DocumentElement.FindNode('IDERevision');
     207        if Assigned(NewNode) then
     208          IDERevision := UTF8Encode(string(NewNode.TextContent));
     209        NewNode := Doc.DocumentElement.FindNode('IDESource');
     210        if Assigned(NewNode) then
     211          IDESource := Sources.FindById(StrToInt(NewNode.TextContent));
     212        NewNode := Doc.DocumentElement.FindNode('FPCDate');
     213        if Assigned(NewNode) then
     214          FPCDate := StrToDateTime(string(NewNode.TextContent));
     215        NewNode := Doc.DocumentElement.FindNode('FPCRevision');
     216        if Assigned(NewNode) then
     217          FPCRevision := UTF8Encode(string(NewNode.TextContent));
     218        NewNode := Doc.DocumentElement.FindNode('FPCSource');
     219        if Assigned(NewNode) then
     220          FPCSource := Sources.FindById(StrToInt(NewNode.TextContent));
     221      end;
     222      Child := Child.NextSibling;
     223    end;
     224  finally
     225    Doc.Free;
     226  end;
     227end;
     228
     229procedure TForm1.SaveInstanceList;
     230var
     231  I: Integer;
     232  Doc: TXMLDocument;
     233  RootNode: TDOMNode;
     234  NewNode: TDOMNode;
     235  NewNode2: TDOMNode;
     236begin
     237  Doc := TXMLDocument.Create;
     238  with Doc do
     239  try
     240    RootNode := CreateElement('InstanceList');
     241    AppendChild(RootNode);
     242    with RootNode do begin
     243      NewNode := OwnerDocument.CreateElement('Instances');
     244      with NewNode do
     245      for I := 0 to Instances.Count - 1 do
     246      with TInstance(Instances[I]) do begin
     247        NewNode2 := OwnerDocument.CreateElement('Id');
     248        NewNode2.TextContent := IntToStr(Id);
     249        AppendChild(NewNode);
     250        NewNode2 := OwnerDocument.CreateElement('Name');
     251        NewNode2.TextContent := UTF8Decode(Name);
     252        AppendChild(NewNode);
     253        NewNode2 := OwnerDocument.CreateElement('IDEDate');
     254        NewNode2.TextContent := DateTimeToStr(IDEDate);
     255        AppendChild(NewNode);
     256        NewNode2 := OwnerDocument.CreateElement('IDERevision');
     257        NewNode2.TextContent := UTF8Decode(IDERevision);
     258        AppendChild(NewNode);
     259        NewNode2 := OwnerDocument.CreateElement('IDESource');
     260        NewNode2.TextContent := IntToStr(IDESource.Id);
     261        AppendChild(NewNode);
     262        NewNode2 := OwnerDocument.CreateElement('FPCDate');
     263        NewNode2.TextContent := DateTimeToStr(FPCDate);
     264        AppendChild(NewNode);
     265        NewNode2 := OwnerDocument.CreateElement('FPCRevision');
     266        NewNode2.TextContent := UTF8Decode(FPCRevision);
     267        AppendChild(NewNode);
     268        NewNode2 := OwnerDocument.CreateElement('FPCSource');
     269        NewNode2.TextContent := IntToStr(FPCSource.Id);
     270        AppendChild(NewNode);
     271      end;
     272      AppendChild(RootNode);
     273
     274      NewNode := OwnerDocument.CreateElement('Sources');
     275      with NewNode do
     276      for I := 0 to Sources.Count - 1 do
     277      with TSource(Sources[I]) do begin
     278        NewNode2 := OwnerDocument.CreateElement('Id');
     279        NewNode2.TextContent := IntToStr(Id);
     280        AppendChild(NewNode);
     281        NewNode2 := OwnerDocument.CreateElement('Name');
     282        NewNode2.TextContent := UTF8Decode(Name);
     283        AppendChild(NewNode);
     284        NewNode2 := OwnerDocument.CreateElement('SubversionURL');
     285        NewNode2.TextContent := UTF8Decode(SubversionURL);
     286        AppendChild(NewNode);
     287        NewNode2 := OwnerDocument.CreateElement('Version');
     288        NewNode2.TextContent := UTF8Decode(Version);
     289        AppendChild(NewNode);
     290        NewNode2 := OwnerDocument.CreateElement('Variation');
     291        NewNode2.TextContent := UTF8Decode(Variation);
     292        AppendChild(NewNode);
     293        NewNode2 := OwnerDocument.CreateElement('SourceType');
     294        NewNode2.TextContent := IntToStr(Byte(SourceType));
     295        AppendChild(NewNode);
     296      end;
     297      AppendChild(RootNode);
     298    end;
     299    WriteXMLFile(Doc, UTF8Decode(ConfigFileName));
     300  finally
     301    Free;
     302  end;
     303end;
     304
    42305end.
    43306
Note: See TracChangeset for help on using the changeset viewer.