installer/UnitfrmMain.pas
author Christian Hammacher <bmasterc@gmail.com>
Wed Dec 28 23:56:45 2005 +0000 (2005-12-28)
changeset 173 c36e2cf2baad
parent 169 314eb459448e
child 182 f60d482a6e07
permissions -rw-r--r--
again design stuff
     1 unit UnitfrmMain;
     2 
     3 interface
     4 
     5 uses
     6   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
     7   Dialogs, TFlatRadioButtonUnit, StdCtrls, ComCtrls, mxFlatControls, JvPageList,
     8   ExtCtrls, JvExControls, JvComponent, TFlatButtonUnit, jpeg, TFlatEditUnit,
     9   TFlatGaugeUnit, ImgList, FileCtrl, Registry, CheckLst, TFlatComboBoxUnit,
    10   TFlatCheckBoxUnit, IdBaseComponent, IdComponent, IdTCPConnection,
    11   IdTCPClient, IdFTP, IdException, IdAntiFreezeBase, IdAntiFreeze,
    12   IdIntercept, IdLogBase, IdLogFile;
    13 
    14 type
    15   TfrmMain = class(TForm)
    16     jplWizard: TJvPageList;
    17     jspWelcome: TJvStandardPage;
    18     pnlButtons: TPanel;
    19     bvlSpace: TBevel;
    20     cmdNext: TFlatButton;
    21     cmdCancel: TFlatButton;
    22     imgInstall: TImage;
    23     lblWelcome: TLabel;
    24     lblInfo1: TLabel;
    25     lblInfo2: TLabel;
    26     lblInfo3: TLabel;
    27     jspLicense: TJvStandardPage;
    28     pnlLicense: TPanel;
    29     imgIcon1: TImage;
    30     lblTitle1: TLabel;
    31     lblSubTitle1: TLabel;
    32     freLicense: TmxFlatRichEdit;
    33     frbAgree: TFlatRadioButton;
    34     ftbDontAgree: TFlatRadioButton;
    35     jspInstallMethod: TJvStandardPage;
    36     pnlHeader2: TPanel;
    37     imgIcon2: TImage;
    38     lblTitle2: TLabel;
    39     lblSubTitle2: TLabel;
    40     lblInstallMethod: TLabel;
    41     pnlInstallMethod: TPanel;
    42     frbDedicatedServer: TFlatRadioButton;
    43     frbListenServer: TFlatRadioButton;
    44     frbSelectMod: TFlatRadioButton;
    45     frbFTP: TFlatRadioButton;
    46     cmdBack: TFlatButton;
    47     jspFTP: TJvStandardPage;
    48     pnlHeader3: TPanel;
    49     imgIcon3: TImage;
    50     lblTitle3: TLabel;
    51     lblSubTitle3: TLabel;
    52     lblStep1: TLabel;
    53     pnlFTPData: TPanel;
    54     lblHost: TLabel;
    55     txtHost: TFlatEdit;
    56     lblUserName: TLabel;
    57     txtUserName: TFlatEdit;
    58     txtPassword: TFlatEdit;
    59     lblPassword: TLabel;
    60     txtPort: TFlatEdit;
    61     lblPort: TLabel;
    62     lblStep2: TLabel;
    63     cmdConnect: TFlatButton;
    64     pnlDirectory: TPanel;
    65     trvDirectories: TTreeView;
    66     lblStep4: TLabel;
    67     jspInstallProgress: TJvStandardPage;
    68     pnlHeader5: TPanel;
    69     imgIcon5: TImage;
    70     lblTitle5: TLabel;
    71     lblSubTitle5: TLabel;
    72     ggeAll: TFlatGauge;
    73     lblProgress: TLabel;
    74     ggeItem: TFlatGauge;
    75     rtfDetails: TmxFlatRichEdit;
    76     lblDetails: TLabel;
    77     bvlSpace2: TBevel;
    78     ilImages: TImageList;
    79     bvlSpacer1: TBevel;
    80     bvlSpacer2: TBevel;
    81     bvlSpacer3: TBevel;
    82     bvlSpacer5: TBevel;
    83     jspSelectMod: TJvStandardPage;
    84     pnlSelectMod: TPanel;
    85     imgIcon6: TImage;
    86     lblSelectMod: TLabel;
    87     lblSelectModInfo: TLabel;
    88     bvlSelectMod: TBevel;
    89     lblInfo: TLabel;
    90     lstMods: TmxFlatListBox;
    91     chkPassive: TFlatCheckBox;
    92     lblStep3: TLabel;
    93     pnlOS: TPanel;
    94     optWindows: TFlatRadioButton;
    95     optLinux: TFlatRadioButton;
    96     IdFTP: TIdFTP;
    97     cmdProxySettings: TFlatButton;
    98     IdAntiFreeze: TIdAntiFreeze;
    99     frbStandaloneServer: TFlatRadioButton;
   100     tmrSpeed: TTimer;
   101     IdLogFile: TIdLogFile;
   102     procedure jvwStepsCancelButtonClick(Sender: TObject);
   103     procedure cmdCancelClick(Sender: TObject);
   104     procedure cmdNextClick(Sender: TObject);
   105     procedure CheckNext(Sender: TObject);
   106     procedure cmdBackClick(Sender: TObject);
   107     procedure cmdConnectClick(Sender: TObject);
   108     procedure jplWizardChange(Sender: TObject);
   109     procedure FormCreate(Sender: TObject);
   110     procedure lstModsClick(Sender: TObject);
   111     procedure cmdProxySettingsClick(Sender: TObject);
   112     procedure txtPortChange(Sender: TObject);
   113     procedure trvDirectoriesExpanded(Sender: TObject; Node: TTreeNode);
   114     procedure trvDirectoriesChange(Sender: TObject; Node: TTreeNode);
   115     procedure IdFTPWork(Sender: TObject; AWorkMode: TWorkMode;
   116       const AWorkCount: Integer);
   117     procedure FormClose(Sender: TObject; var Action: TCloseAction);
   118     procedure tmrSpeedTimer(Sender: TObject);
   119     procedure trvDirectoriesExpanding(Sender: TObject; Node: TTreeNode;
   120       var AllowExpansion: Boolean);
   121     procedure trvDirectoriesCollapsing(Sender: TObject; Node: TTreeNode;
   122       var AllowCollapse: Boolean);
   123   private
   124     OldProgress: Integer;
   125     CurrProgress: Integer;
   126   public
   127     procedure ExceptionHandler(Sender: TObject; E: Exception);
   128   end;
   129 
   130 var
   131   frmMain: TfrmMain;
   132 
   133 const VERSION = '1.2';
   134 
   135 implementation
   136 
   137 uses UnitFunctions, UnitfrmProxy, UnitInstall, UnitSelectModPath;
   138 
   139 {$R *.dfm}
   140 
   141 procedure TfrmMain.jvwStepsCancelButtonClick(Sender: TObject);
   142 begin
   143   Close;
   144 end;
   145 
   146 procedure TfrmMain.cmdCancelClick(Sender: TObject);
   147 begin
   148   Close;
   149 end;
   150 
   151 procedure TfrmMain.cmdNextClick(Sender: TObject);
   152 var ePath: String;
   153     eRegistry: TRegistry;
   154     eStr: TStringList;
   155     CurNode: TTreeNode;
   156     eOS: TOS;
   157 begin
   158   { FTP }
   159   if jplWizard.ActivePage = jspFTP then begin
   160     if not IdFTP.Connected then
   161       IdFTP.Connect;
   162     eStr := TStringList.Create;
   163     ePath := '/';
   164     CurNode := trvDirectories.Selected;
   165     repeat
   166       ePath := '/' + CurNode.Text + ePath;
   167       CurNode := CurNode.Parent;
   168     until (not Assigned(CurNode));
   169     IdFTP.ChangeDir(ePath); 
   170     IdFTP.List(eStr, '', False);
   171     if eStr.IndexOf('gameinfo.txt') = -1 then begin
   172       MessageBox(Handle, 'Invalid directory. Please select your mod directory and try again.', PChar(Application.Title), MB_ICONWARNING);
   173       eStr.Free;
   174       exit;
   175     end
   176     else
   177       eStr.Free;
   178 
   179     // design stuff
   180     trvDirectories.Enabled := False;
   181     cmdConnect.Enabled := False;
   182     optWindows.Enabled := False;
   183     optLinux.Enabled := False;
   184     Screen.Cursor := crHourGlass;
   185 
   186     if optWindows.Checked then
   187       eOS := osWindows
   188     else if optLinux.Checked then
   189       eOS := osLinux;
   190 
   191     jspInstallProgress.Show;
   192     // installation
   193     Screen.Cursor := crAppStart;
   194     InstallFTP(eOS);
   195   end
   196   else if jplWizard.ActivePage = jspInstallProgress then
   197     Close
   198   else if jplWizard.ActivePage = jspSelectMod then begin
   199     { Dedicated Server }
   200     if (frbDedicatedServer.Checked) or (frbStandaloneServer.Checked) then begin
   201       jspInstallProgress.Show;
   202       ePath := lstMods.Items[lstMods.ItemIndex];
   203       if ePath = 'Counter-Strike:Source' then
   204         ePath := 'cstrike'
   205       else if ePath = 'Day of Defeat:Source' then
   206         ePath := 'dod'
   207       else
   208         ePath := 'hl2mp';
   209       // install it
   210       if frbDedicatedServer.Checked then begin
   211         if DirectoryExists(SteamPath + ePath) then
   212           InstallDedicated(IncludeTrailingPathDelimiter(SteamPath + ePath), True)
   213         else begin
   214           MessageBox(Handle, 'Error: The directory of the mod you selected doesn''t exist any more. Run Dedicated Server with the chosen mod and try again.', PChar(Application.Title), MB_ICONERROR);
   215           Application.Terminate;
   216           exit;
   217         end;
   218       end
   219       else begin
   220         if DirectoryExists(StandaloneServer + ePath) then
   221           InstallDedicated(IncludeTrailingPathDelimiter(StandaloneServer + ePath), False)
   222         else begin
   223           MessageBox(Handle, 'Error: The directory of the mod you selected doesn''t exist (any more). Run Half-Life Dedicated Server with the chosen mod again and restart.', PChar(Application.Title), MB_ICONERROR);
   224           Application.Terminate;
   225           exit;
   226         end;
   227       end;
   228     end;
   229     { Listen Server }
   230     if frbListenServer.Checked then begin
   231       ePath := lstMods.Items[lstMods.ItemIndex];
   232       if ePath = 'Counter-Strike:Source' then
   233         ePath := SteamPath + 'counter-strike source\cstrike'
   234       else if ePath = 'Half-Life 2 Deathmatch' then
   235         ePath := SteamPath + 'half-life 2 deathmatch\hl2mp'
   236       else
   237         ePath := SteamPath + 'Day Of Defeat source\dod';
   238 
   239       if Pos(SteamPath, ePath) = 0 then
   240         MessageBox(Handle, 'An error occured. Please report this bug to the Metamod:Source team and post a new thread on the forums of www.amxmodx.org.', PChar(Application.Title), MB_ICONSTOP)
   241       else begin
   242         if not FileExists(ePath + '\gameinfo.txt') then begin
   243           MessageBox(Handle, 'You have to play this game once before installing Metamod:Source. Do this and try again.', PChar(Application.Title), MB_ICONWARNING);
   244           exit;
   245         end;
   246 
   247         jspInstallProgress.Show;
   248         ePath := IncludeTrailingPathDelimiter(ePath);
   249         InstallListen(ePath);
   250       end;
   251     end;
   252     { Custom mod below }
   253   end
   254   else if jplWizard.ActivePage <> jspInstallMethod then
   255     jplWizard.NextPage
   256   else begin
   257     if frbDedicatedServer.Checked then begin    // Dedicated Server
   258       eRegistry := TRegistry.Create(KEY_READ);
   259       try
   260         eRegistry.RootKey := HKEY_CURRENT_USER;
   261         if eRegistry.OpenKey('Software\Valve\Steam', False) then begin
   262           ePath := eRegistry.ReadString('ModInstallPath');
   263           ePath := Copy(ePath, 1, Length(ePath) -10) + '\source dedicated server\';
   264           if DirectoryExists(ePath) then begin
   265             SteamPath := ePath;
   266             lstMods.Clear;
   267             // Check Mods
   268             if DirectoryExists(SteamPath + 'cstrike') then
   269               lstMods.Items.Add('Counter-Strike:Source');
   270             if DirectoryExists(SteamPath + 'dod') then
   271               lstMods.Items.Add('Day of Defeat:Source');
   272             if DirectoryExists(SteamPath + 'hl2mp') then
   273               lstMods.Items.Add('Half-Life 2 Deatmatch');
   274             // Misc
   275             jspSelectMod.Show;
   276             lstMods.ItemIndex := -1;
   277             cmdNext.Enabled := False;
   278           end
   279           else
   280             MessageBox(Handle, 'You have to run Dedicated Server once before installing Metamod:Source!', 'Error', MB_ICONWARNING);
   281         end
   282         else
   283           MessageBox(Handle, 'You haven''t installed Steam yet! Download it at www.steampowered.com, install Dedicated Server and try again.', 'Error', MB_ICONWARNING);
   284       finally
   285         eRegistry.Free;
   286       end;
   287     end
   288     else if frbListenServer.Checked then begin  // Listen Server
   289       eRegistry := TRegistry.Create(KEY_READ);
   290       try
   291         eRegistry.RootKey := HKEY_CURRENT_USER;
   292         if eRegistry.OpenKey('Software\Valve\Steam', False) then begin
   293           ePath := eRegistry.ReadString('ModInstallPath') + '\';
   294           lstMods.Clear;
   295           ePath := Copy(ePath, 1, Length(ePath) -10);
   296           if DirectoryExists(ePath) then begin
   297             SteamPath := ePath;
   298             // Check Mods
   299             if DirectoryExists(SteamPath + 'counter-strike source') then
   300               lstMods.Items.Add('Counter-Strike:Source');
   301             if DirectoryExists(SteamPath + 'half-life 2 deathmatch') then
   302               lstMods.Items.Add('Half-Life 2 Deathmatch');
   303             if DirectoryExists(SteamPath + 'Day Of Defeat source') then
   304               lstMods.Items.Add('Day of Defeat: Source');
   305             // Misc
   306             jspSelectMod.Show;
   307             lstMods.ItemIndex := -1;
   308             cmdNext.Enabled := False;
   309           end
   310           else
   311             MessageBox(Handle, 'You haven''t installed Steam yet! Download it at www.steampowered.com, install Dedicated Server and try again.', 'Error', MB_ICONWARNING);
   312         end
   313         else
   314           MessageBox(Handle, 'You haven''t installed Steam yet! Download it at www.steampowered.com, install Dedicated Server and try again.', 'Error', MB_ICONWARNING);
   315       finally
   316         eRegistry.Free;
   317       end;
   318     end
   319     else if frbStandaloneServer.Checked then begin // Standalone Server
   320       eRegistry := TRegistry.Create;
   321       try
   322         eRegistry.RootKey := HKEY_CURRENT_USER;
   323         if eRegistry.OpenKey('Software\Valve\HLServer', False) then begin
   324           StandaloneServer := IncludeTrailingPathDelimiter(eRegistry.ReadString('InstallPath'));
   325           if DirectoryExists(StandaloneServer + 'cstrike') then
   326             lstMods.Items.Add('Counter-Strike:Source');
   327           if DirectoryExists(StandaloneServer + 'dod') then
   328             lstMods.Items.Add('Day of Defeat:Source');
   329           if DirectoryExists(StandaloneServer + 'hl2mp') then
   330             lstMods.Items.Add('Half-Life 2 Deatmatch');    
   331           jspSelectMod.Show;
   332         end
   333         else
   334           MessageBox(Handle, 'You haven''t installed Half-Life Dedicated Server yet!',  'Error', MB_ICONWARNING);
   335       finally
   336         eRegistry.Free;
   337       end;
   338     end
   339     else if frbSelectMod.Checked then begin 
   340       { Custom mod }
   341       if frmSelectModPath.ShowModal = mrOk then begin
   342         jspInstallProgress.Show;
   343         InstallCustom(IncludeTrailingPathDelimiter(frmSelectModPath.trvDirectory.SelectedFolder.PathName), osWindows);
   344       end;
   345     end
   346     else if frbFTP.Checked then // FTP
   347       jspFTP.Show;
   348   end;
   349 end;
   350 
   351 procedure TfrmMain.CheckNext(Sender: TObject);
   352 begin
   353   cmdNext.Enabled := frbAgree.Checked;
   354 end;
   355 
   356 procedure TfrmMain.cmdBackClick(Sender: TObject);
   357 begin
   358   if jplWizard.ActivePage = jspFTP then
   359     jspInstallMethod.Show
   360   else begin
   361     jplWizard.PrevPage;
   362     cmdBack.Visible := jplWizard.ActivePageIndex <> 0;
   363   end;
   364 end;
   365 
   366 procedure TfrmMain.cmdConnectClick(Sender: TObject);
   367 var i: integer;
   368     eStr: TStringList;
   369     CurNode: TTreeNode;
   370 begin
   371   if (Trim(txtHost.Text) = '') or (Trim(txtUsername.Text) = '') then
   372     MessageBox(Handle, 'Please fill in each field!', PChar(Application.Title), MB_ICONWARNING)
   373   else if cmdConnect.Caption = 'Connect' then begin
   374     // ... design stuff ...
   375     Screen.Cursor := crHourGlass;
   376     cmdConnect.Enabled := False;
   377     cmdProxySettings.Enabled := False;
   378     txtHost.Enabled := False;
   379     txtPort.Enabled := False;
   380     txtUsername.Enabled := False;
   381     txtPassword.Enabled := False;
   382     chkPassive.Enabled := False;
   383     cmdConnect.Caption := 'Connecting...';
   384     // ... set values ...
   385     IdFTP.Host := txtHost.Text;
   386     IdFTP.Port := StrToInt(txtPort.Text);
   387     IdFTP.Username := txtUsername.Text;
   388     IdFTP.Passive := chkPassive.Checked;
   389     IdFTP.Password := txtPassword.Text;
   390     // ... connect and check values etc ...
   391     try
   392       IdFTP.Connect(True, 15000);
   393       // ... scan for initial directory ...
   394       eStr := TStringList.Create;
   395       eStr.Text := StringReplace(IdFTP.RetrieveCurrentDir, '/', #13, [rfReplaceAll]);
   396       for i := eStr.Count -1 downto 0 do begin
   397         if eStr[i] = '' then
   398           eStr.Delete(i);
   399       end;
   400       // ... connect successful, change captions ...
   401       trvDirectories.Enabled := True;
   402       cmdConnect.Enabled := True;
   403       cmdConnect.Caption := 'Disconnect';
   404 
   405       CurNode := nil;
   406       if eStr.Count <> 0 then begin
   407         for i := 0 to eStr.Count -1 do
   408           CurNode := trvDirectories.Items.AddChild(CurNode, eStr[i]);
   409       end;
   410       if trvDirectories.Items.Count <> 0 then
   411         trvDirectories.Items.Item[0].Expand(True);
   412       eStr.Free;
   413 
   414       // ... scan for directories ...
   415       with GetAllDirs do begin
   416         for i := 0 to Count -1 do
   417           trvDirectories.Items.AddChild(trvDirectories.Items.AddChild(CurNode, Strings[i]), 'Scanning...');
   418         Free;
   419       end;
   420 
   421       if Assigned(CurNode) then
   422         CurNode.Expand(False);
   423     except
   424       on E: Exception do begin
   425         // reset button properties
   426         cmdConnect.Enabled := True;
   427         txtHost.Enabled := True;
   428         txtPort.Enabled := True;
   429         txtUsername.Enabled := True;
   430         txtPassword.Enabled := True;
   431         chkPassive.Enabled := True;
   432         cmdProxySettings.Enabled := True;
   433         cmdNext.Enabled := False;
   434         cmdConnect.Caption := 'Connect';
   435         // analyze messages
   436         if Pos('Login incorrect.', E.Message) <> 0 then begin // login failed
   437           MessageBox(Handle, 'Login incorrect. Check your FTP settings and try again.', PChar(Application.Title), MB_ICONWARNING);
   438           txtUsername.SetFocus;
   439           txtUsername.SelectAll;
   440         end
   441         else if Pos('Host not found.', E.Message) <> 0 then begin // host not found
   442           MessageBox(Handle, 'The entered host couldn''t be found. Check your settings and try again.', PChar(Application.Title), MB_ICONWARNING);
   443           txtHost.SetFocus;
   444           txtHost.SelectAll;
   445         end
   446         else if Pos('Connection refused.', E.Message) <> 0 then begin // wrong port (?)
   447           MessageBox(Handle, 'The host refused the connection. Check your port and try again.', PChar(Application.Title), MB_ICONWARNING);
   448           txtPort.SetFocus;
   449           txtPort.SelectAll;
   450         end
   451         else if E is EIdProtocolReplyError then begin // wrong port
   452           MessageBox(Handle, 'The port you entered is definitely wrong. Check it and try again.', PChar(Application.Title), MB_ICONWARNING);
   453           txtPort.SetFocus;
   454           txtPort.SelectAll;
   455         end
   456         else
   457           MessageBox(Handle, PChar(E.Message), PChar(Application.Title), MB_ICONWARNING); // unknown error
   458         // ... connect failed, leave procedure ...
   459         Screen.Cursor := crDefault;
   460         exit;
   461       end;
   462     end;
   463 
   464     Screen.Cursor := crDefault;
   465   end
   466   else begin
   467     Screen.Cursor := crHourGlass;
   468     IdFTP.Quit;
   469     trvDirectories.Items.Clear;
   470     trvDirectories.Enabled := False;
   471     cmdConnect.Enabled := True;
   472     cmdProxySettings.Enabled := True;
   473     txtHost.Enabled := True;
   474     txtPort.Enabled := True;
   475     txtUsername.Enabled := True;
   476     txtPassword.Enabled := True;
   477     chkPassive.Enabled := True;
   478     cmdConnect.Caption := 'Connect';
   479     cmdNext.Enabled := False;
   480     Screen.Cursor := crDefault;
   481   end;
   482 end;
   483 
   484 procedure TfrmMain.jplWizardChange(Sender: TObject);
   485 begin
   486   if (jplWizard.ActivePage = jspInstallProgress) then begin
   487     cmdNext.Caption := '&Finish';
   488     cmdNext.Enabled := False;
   489     cmdBack.Visible := False;
   490   end
   491   else begin
   492     cmdNext.Caption := '&Next >';
   493     cmdNext.Enabled := True;
   494     cmdBack.Visible := jplWizard.ActivePageIndex <> 0;
   495   end;
   496 
   497   if (jplWizard.ActivePage = jspLicense) then
   498     cmdNext.Enabled := frbAgree.Checked;
   499 
   500   if (jplWizard.ActivePage = jspFTP) then
   501     cmdNext.Enabled := False;
   502 end;
   503 
   504 procedure TfrmMain.FormCreate(Sender: TObject);
   505 begin
   506   if LowerCase(ParamStr(1)) = '-logftp' then begin
   507     MessageBox(Handle, 'FTP installation will be logged to FTP.log!', PChar(Application.Title), MB_ICONINFORMATION);
   508     IdLogFile.Filename := ExtractFilePath(ParamStr(0)) + 'FTP.log';
   509     IdLogFile.Active := True;
   510   end;
   511 
   512   rtfDetails.Clear;
   513 end;
   514 
   515 procedure TfrmMain.lstModsClick(Sender: TObject);
   516 begin
   517   cmdNext.Enabled := lstMods.ItemIndex <> -1;
   518 end;
   519 
   520 procedure TfrmMain.cmdProxySettingsClick(Sender: TObject);
   521 begin
   522   frmProxy.ShowModal;
   523   // Apply Proxy Settings
   524   case frmProxy.cboProxy.ItemIndex of
   525     0: IdFTP.ProxySettings.ProxyType := fpcmNone; // none
   526     1: IdFTP.ProxySettings.ProxyType := fpcmHttpProxyWithFtp; // HTTP Proxy with FTP
   527     2: IdFTP.ProxySettings.ProxyType := fpcmOpen; // Open
   528     3: IdFTP.ProxySettings.ProxyType := fpcmSite; // Site
   529     4: IdFTP.ProxySettings.ProxyType := fpcmTransparent; // Transparent
   530     5: IdFTP.ProxySettings.ProxyType := fpcmUserPass; // User (Password)
   531     6: IdFTP.ProxySettings.ProxyType := fpcmUserSite; // User (Site)
   532   end;
   533 
   534   IdFTP.ProxySettings.Host := frmProxy.txtHost.Text;
   535   IdFTP.ProxySettings.UserName := frmProxy.txtPort.Text;
   536   IdFTP.ProxySettings.Password := frmProxy.txtPassword.Text;
   537   IdFTP.ProxySettings.Port := StrToInt(frmProxy.txtPort.Text);
   538 end;
   539 
   540 procedure TfrmMain.txtPortChange(Sender: TObject);
   541 var i: integer;
   542 begin
   543   if txtPort.Text = '' then
   544     txtPort.Text := '21'
   545   else begin
   546     // check if value is numeric...
   547     for i := Length(txtPort.Text) downto 1 do begin
   548       if Pos(txtPort.Text[i], '0123456789') = 0 then begin
   549         txtPort.Text := '21';
   550         txtPort.SelStart := 4;
   551         exit;
   552       end;
   553     end;
   554   end;
   555 end;
   556 
   557 procedure TfrmMain.trvDirectoriesExpanded(Sender: TObject;
   558   Node: TTreeNode);
   559 var ePath: String;
   560     CurNode: TTreeNode;
   561     i: integer;
   562 begin
   563   if Node.Item[0].Text = 'Scanning...' then begin // no directories added yet
   564     Screen.Cursor := crHourGlass;
   565     // get complete path
   566     ePath := '/';
   567     CurNode := Node;
   568     repeat
   569       ePath := '/' + CurNode.Text + ePath;
   570       CurNode := CurNode.Parent;
   571     until (not Assigned(CurNode));
   572     // change dir and add directories in it
   573     try
   574       Repaint;
   575       IdFTP.ChangeDir(ePath);
   576       with GetAllDirs do begin
   577         Node.Item[0].Free;
   578         for i := 0 to Count -1 do begin
   579           trvDirectories.Items.AddChild(trvDirectories.Items.AddChild(Node, Strings[i]), 'Scanning...');
   580         end;
   581         Free;
   582       end;
   583     finally
   584       Application.ProcessMessages;
   585     end;
   586     Screen.Cursor := crDefault;
   587   end;
   588 end;
   589 
   590 procedure TfrmMain.trvDirectoriesChange(Sender: TObject; Node: TTreeNode);
   591 begin
   592   cmdNext.Enabled := Assigned(trvDirectories.Selected);
   593 end;
   594 
   595 procedure TfrmMain.IdFTPWork(Sender: TObject; AWorkMode: TWorkMode;
   596   const AWorkCount: Integer);
   597 begin
   598   if AWorkCount > 15 then begin
   599     ggeItem.Progress := AWorkCount;
   600     CurrProgress := AWorkCount;
   601   end;
   602 
   603   if Cancel then
   604     IdFTP.Abort;
   605     
   606   Application.ProcessMessages;
   607 end;
   608 
   609 procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
   610 begin
   611   if (jplWizard.ActivePage = jspFTP) and (IdFTP.Connected) then
   612     IdFTP.Quit;
   613   if (jplWizard.ActivePage = jspInstallProgress) and (ggeAll.Progress <> ggeAll.MaxValue) and (not Cancel) then begin
   614     if MessageBox(Handle, 'Do you really want to cancel the installation?', PChar(Application.Title), MB_ICONQUESTION + MB_YESNO) = mrYes then begin
   615       Screen.Cursor := crDefault;
   616       Application.OnException := ExceptionHandler;
   617       Cancel := True;
   618       if IdFTP.Connected then
   619         IdFTP.Quit;
   620     end
   621     else
   622       Action := caNone;
   623   end;
   624 end;
   625 
   626 procedure TfrmMain.ExceptionHandler(Sender: TObject; E: Exception);
   627 begin
   628   // IF any exceptions were raised after close, nobody would want them so leave this empty
   629 end;
   630 
   631 procedure TfrmMain.tmrSpeedTimer(Sender: TObject);
   632 begin
   633   Caption := CalcSpeed(OldProgress, CurrProgress);
   634   OldProgress := CurrProgress;
   635 end;
   636 
   637 procedure TfrmMain.trvDirectoriesExpanding(Sender: TObject;
   638   Node: TTreeNode; var AllowExpansion: Boolean);
   639 begin
   640   Node.ImageIndex := 1;
   641   Node.SelectedIndex := 1;
   642 end;
   643 
   644 procedure TfrmMain.trvDirectoriesCollapsing(Sender: TObject;
   645   Node: TTreeNode; var AllowCollapse: Boolean);
   646 begin
   647   Node.ImageIndex := 0;
   648   Node.SelectedIndex := 0;
   649 end;
   650 
   651 end.