installer/UnitfrmMain.pas
author David Anderson <dvander@alliedmods.net>
Tue Sep 18 20:03:47 2007 +0000 (2007-09-18)
branchsourcemm-1.6.0
changeset 347 e82968c21606
permissions -rw-r--r--
branched 1.4.2 to 1.6.0
     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, pngimage;
    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     imgIcon4: 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     imgIcon5: TImage;
    86     lblSelectMod: TLabel;
    87     lblSelectModInfo: TLabel;
    88     bvlSelectMod: TBevel;
    89     lblInfo: TLabel;
    90     chkPassive: TFlatCheckBox;
    91     lblStep3: TLabel;
    92     pnlOS: TPanel;
    93     optWindows: TFlatRadioButton;
    94     optLinux: TFlatRadioButton;
    95     IdFTP: TIdFTP;
    96     cmdProxySettings: TFlatButton;
    97     IdAntiFreeze: TIdAntiFreeze;
    98     frbStandaloneServer: TFlatRadioButton;
    99     tmrSpeed: TTimer;
   100     IdLogFile: TIdLogFile;
   101     shpMods: TShape;
   102     trvMods: TTreeView;
   103     FlatRadioButton1: TFlatRadioButton;
   104     procedure jvwStepsCancelButtonClick(Sender: TObject);
   105     procedure cmdCancelClick(Sender: TObject);
   106     procedure cmdNextClick(Sender: TObject);
   107     procedure CheckNext(Sender: TObject);
   108     procedure cmdBackClick(Sender: TObject);
   109     procedure cmdConnectClick(Sender: TObject);
   110     procedure jplWizardChange(Sender: TObject);
   111     procedure FormCreate(Sender: TObject);
   112     procedure cmdProxySettingsClick(Sender: TObject);
   113     procedure txtPortChange(Sender: TObject);
   114     procedure trvDirectoriesExpanded(Sender: TObject; Node: TTreeNode);
   115     procedure trvDirectoriesChange(Sender: TObject; Node: TTreeNode);
   116     procedure IdFTPWork(Sender: TObject; AWorkMode: TWorkMode;
   117       const AWorkCount: Integer);
   118     procedure FormClose(Sender: TObject; var Action: TCloseAction);
   119     procedure tmrSpeedTimer(Sender: TObject);
   120     procedure trvDirectoriesExpanding(Sender: TObject; Node: TTreeNode;
   121       var AllowExpansion: Boolean);
   122     procedure trvDirectoriesCollapsing(Sender: TObject; Node: TTreeNode;
   123       var AllowCollapse: Boolean);
   124     procedure trvModsClick(Sender: TObject);
   125     procedure trvDirectoriesMouseDown(Sender: TObject;
   126       Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
   127   private
   128     OldProgress: Integer;
   129     CurrProgress: Integer;
   130   public
   131     procedure ExceptionHandler(Sender: TObject; E: Exception);
   132   end;
   133 
   134 var
   135   frmMain: TfrmMain;
   136 
   137 var VERSION: String = '<none>';
   138 
   139 implementation
   140 
   141 uses UnitFunctions, UnitfrmProxy, UnitInstall, UnitSelectModPath;
   142 
   143 {$R *.dfm}
   144 
   145 procedure TfrmMain.jvwStepsCancelButtonClick(Sender: TObject);
   146 begin
   147   Close;
   148 end;
   149 
   150 procedure TfrmMain.cmdCancelClick(Sender: TObject);
   151 begin
   152   Close;
   153 end;
   154 
   155 procedure TfrmMain.cmdNextClick(Sender: TObject);
   156 var ePath: String;
   157     eRegistry: TRegistry;
   158     eStr: TStringList;
   159     CurNode: TTreeNode;
   160     eOS: TOS;
   161     i: integer;
   162 begin
   163   { FTP }
   164   if jplWizard.ActivePage = jspFTP then begin
   165     if not IdFTP.Connected then
   166       IdFTP.Connect;
   167     eStr := TStringList.Create;
   168     ePath := '/';
   169     CurNode := trvDirectories.Selected;
   170     if (Assigned(CurNode)) then begin
   171       repeat
   172         ePath := '/' + CurNode.Text + ePath;
   173         CurNode := CurNode.Parent;
   174       until (not Assigned(CurNode));
   175     end;
   176     IdFTP.ChangeDir(ePath);
   177     IdFTP.List(eStr, '', False);
   178     if eStr.IndexOf('gameinfo.txt') = -1 then begin
   179       MessageBox(Handle, 'Invalid directory. Please select your mod directory and try again.', PChar(Application.Title), MB_ICONWARNING);
   180       eStr.Free;
   181       exit;
   182     end
   183     else
   184       eStr.Free;
   185 
   186     // design stuff
   187     trvDirectories.Enabled := False;
   188     cmdConnect.Enabled := False;
   189     optWindows.Enabled := False;
   190     optLinux.Enabled := False;
   191     Screen.Cursor := crHourGlass;
   192 
   193     if optWindows.Checked then
   194       eOS := osWindows
   195     else
   196       eOS := osLinux;
   197 
   198     jspInstallProgress.Show;
   199     // installation
   200     Screen.Cursor := crAppStart;
   201     InstallFTP(eOS);
   202   end
   203   else if jplWizard.ActivePage = jspInstallProgress then
   204     Close
   205   else if jplWizard.ActivePage = jspSelectMod then begin
   206     { Dedicated Server }
   207     if (frbDedicatedServer.Checked) or (frbStandaloneServer.Checked) then begin
   208       ePath := trvMods.Selected.Text;
   209       if ePath = 'Counter-Strike:Source' then
   210         ePath := 'cstrike'
   211       else if ePath = 'Day of Defeat:Source' then
   212         ePath := 'dod'
   213       else
   214         ePath := 'hl2mp';
   215       ePath := 'SteamApps\' + trvMods.Selected.Parent.Text + '\source dedicated server\' + ePath;
   216       // install it
   217       if frbDedicatedServer.Checked then begin
   218         if DirectoryExists(SteamPath + ePath) then begin
   219           jspInstallProgress.Show;
   220           InstallDedicated(IncludeTrailingPathDelimiter(SteamPath + ePath), True);
   221         end
   222         else begin
   223           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);
   224           jspSelectMod.Show;
   225           exit;
   226         end;
   227       end
   228       else begin
   229         if DirectoryExists(StandaloneServer + ePath) then begin
   230           jspInstallProgress.Show;
   231           InstallDedicated(IncludeTrailingPathDelimiter(StandaloneServer + ePath), False)
   232         end
   233         else begin
   234           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);
   235           jspSelectMod.Show;
   236           exit;
   237         end;
   238       end;
   239     end;
   240     { Listen Server }
   241     if frbListenServer.Checked then begin
   242       ePath := trvMods.Selected.Text;
   243       if ePath = 'Counter-Strike:Source' then
   244         ePath := SteamPath + 'SteamApps\' + trvMods.Selected.Parent.Text + '\counter-strike source\cstrike'
   245       else if ePath = 'Half-Life 2 Deathmatch' then
   246         ePath := SteamPath + 'SteamApps\' + trvMods.Selected.Parent.Text + '\half-life 2 deathmatch\hl2mp'
   247       else
   248         ePath := SteamPath + 'SteamApps\' + trvMods.Selected.Parent.Text + '\day of defeat source\dod';
   249 
   250       if Pos(SteamPath, ePath) = 0 then
   251         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)
   252       else begin
   253          if not FileExists(ePath + '\gameinfo.txt') then begin
   254           MessageBox(Handle, 'You have to play this game once before installing Metamod:Source. Do that and try again.', PChar(Application.Title), MB_ICONWARNING);
   255           exit;
   256         end;
   257 
   258         jspInstallProgress.Show;
   259         InstallListen(IncludeTrailingPathDelimiter(ePath));
   260       end;
   261     end;
   262     { Custom mod below }
   263   end
   264   else if jplWizard.ActivePage <> jspInstallMethod then
   265     jplWizard.NextPage
   266   else begin
   267     if frbDedicatedServer.Checked then begin    // Dedicated Server
   268       eRegistry := TRegistry.Create(KEY_READ);
   269       try
   270         eRegistry.RootKey := HKEY_CURRENT_USER;
   271         if eRegistry.OpenKey('Software\Valve\Steam', False) then begin
   272           ePath := eRegistry.ReadString('SteamPath');
   273           ePath := IncludeTrailingPathDelimiter(StringReplace(ePath, '/', '\', [rfReplaceAll]));
   274           SteamPath := ePath;
   275 
   276           ePath := ePath + 'SteamApps\';
   277           if DirectoryExists(ePath) then begin
   278             trvMods.Items.Clear;
   279             // Check Mods
   280             eStr := GetAllFiles(ePath + '*.*', faDirectory, False, True, False);
   281             for i := 0 to eStr.Count -1 do begin
   282               CurNode := trvMods.Items.Add(nil, eStr[i]);
   283 
   284               if DirectoryExists(ePath + eStr[i] + '\source dedicated server\cstrike') then
   285                 trvMods.Items.AddChild(CurNode, 'Counter-Strike:Source');
   286               if DirectoryExists(ePath + eStr[i] + '\source dedicated server\dod') then
   287                 trvMods.Items.AddChild(CurNode, 'Day of Defeat:Source');
   288               if DirectoryExists(ePath + eStr[i] + '\source dedicated server\hl2mp') then
   289                 trvMods.Items.AddChild(CurNode, 'Half-Life 2 Deatmatch');
   290 
   291               if CurNode.Count = 0 then
   292                 CurNode.Free
   293               else
   294                 CurNode.Expand(False);
   295             end;
   296             // Misc
   297             jspSelectMod.Show;
   298             trvMods.Selected := nil;
   299             cmdNext.Enabled := False;
   300           end
   301           else
   302             MessageBox(Handle, 'You have to run Dedicated Server once before installing Metamod:Source!', 'Error', MB_ICONWARNING);
   303         end
   304         else
   305           MessageBox(Handle, 'You haven''t installed Steam yet! Download it at www.steampowered.com, install Dedicated Server and try again.', 'Error', MB_ICONWARNING);
   306       finally
   307         eRegistry.Free;
   308       end;
   309     end
   310     else if frbListenServer.Checked then begin  // Listen Server
   311       eRegistry := TRegistry.Create(KEY_READ);
   312       try
   313         eRegistry.RootKey := HKEY_CURRENT_USER;
   314         if eRegistry.OpenKey('Software\Valve\Steam', False) then begin
   315           ePath := eRegistry.ReadString('SteamPath');
   316           ePath := IncludeTrailingPathDelimiter(StringReplace(ePath, '/', '\', [rfReplaceAll]));
   317           SteamPath := ePath;
   318 
   319           ePath := ePath + 'SteamApps\';
   320           if DirectoryExists(ePath) then begin
   321             trvMods.Items.Clear;
   322             // Check Mods
   323             eStr := GetAllFiles(ePath + '*.*', faDirectory, False, True, False);
   324             for i := 0 to eStr.Count -1 do begin
   325               CurNode := trvMods.Items.Add(nil, eStr[i]);
   326 
   327               if DirectoryExists(ePath + eStr[i] + '\counter-strike source') then
   328                 trvMods.Items.AddChild(CurNode, 'Counter-Strike:Source');
   329               if DirectoryExists(ePath + eStr[i] + '\day of defeat source') then
   330                 trvMods.Items.AddChild(CurNode, 'Day of Defeat:Source');
   331               if DirectoryExists(ePath + eStr[i] + '\half-life 2 deathmatch') then
   332                 trvMods.Items.AddChild(CurNode, 'Half-Life 2 Deatmatch');
   333 
   334               if CurNode.Count = 0 then
   335                 CurNode.Free
   336               else
   337                 CurNode.Expand(False);
   338             end;
   339             // Misc
   340             jspSelectMod.Show;
   341             trvMods.Selected := nil;
   342             cmdNext.Enabled := False;
   343           end
   344           else
   345             MessageBox(Handle, 'You haven''t installed Steam yet! Download it at www.steampowered.com, install Dedicated Server and try again.', 'Error', MB_ICONWARNING);
   346         end
   347         else
   348           MessageBox(Handle, 'You haven''t installed Steam yet! Download it at www.steampowered.com, install Dedicated Server and try again.', 'Error', MB_ICONWARNING);
   349       finally
   350         eRegistry.Free;
   351       end;
   352     end
   353     else if frbStandaloneServer.Checked then begin // Standalone Server
   354       eRegistry := TRegistry.Create;
   355       try
   356         eRegistry.RootKey := HKEY_CURRENT_USER;
   357         if eRegistry.OpenKey('Software\Valve\HLServer', False) then begin
   358           StandaloneServer := IncludeTrailingPathDelimiter(eRegistry.ReadString('InstallPath'));
   359           if DirectoryExists(StandaloneServer + 'cstrike') then
   360             trvMods.Items.Add(nil, 'Counter-Strike:Source');
   361           if DirectoryExists(StandaloneServer + 'dod') then
   362             trvMods.Items.Add(nil, 'Day of Defeat:Source');
   363           if DirectoryExists(StandaloneServer + 'hl2mp') then
   364             trvMods.Items.Add(nil, 'Half-Life 2 Deatmatch');
   365           jspSelectMod.Show;
   366           cmdNext.Enabled := False;
   367         end
   368         else
   369           MessageBox(Handle, 'You haven''t installed Half-Life Dedicated Server yet!',  'Error', MB_ICONWARNING);
   370       finally
   371         eRegistry.Free;
   372       end;
   373     end
   374     else if frbSelectMod.Checked then begin 
   375       { Custom mod }
   376       if frmSelectModPath.ShowModal = mrOk then begin
   377         jspInstallProgress.Show;
   378         InstallCustom(IncludeTrailingPathDelimiter(frmSelectModPath.trvDirectory.SelectedFolder.PathName), osWindows);
   379       end;
   380     end
   381     else if frbFTP.Checked then // FTP
   382       jspFTP.Show;
   383   end;
   384 end;
   385 
   386 procedure TfrmMain.CheckNext(Sender: TObject);
   387 begin
   388   cmdNext.Enabled := frbAgree.Checked;
   389 end;
   390 
   391 procedure TfrmMain.cmdBackClick(Sender: TObject);
   392 begin
   393   if jplWizard.ActivePage = jspFTP then
   394     jspInstallMethod.Show
   395   else begin
   396     jplWizard.PrevPage;
   397     cmdBack.Visible := jplWizard.ActivePageIndex <> 0;
   398   end;
   399 end;
   400 
   401 procedure TfrmMain.cmdConnectClick(Sender: TObject);
   402 var i: integer;
   403     eStr: TStringList;
   404     CurNode: TTreeNode;
   405     Path: String;
   406 begin
   407   if (Trim(txtHost.Text) = '') or (Trim(txtUsername.Text) = '') then
   408     MessageBox(Handle, 'Please fill in each field!', PChar(Application.Title), MB_ICONWARNING)
   409   else if cmdConnect.Caption = 'Connect' then begin
   410     // ... design stuff ...
   411     Screen.Cursor := crHourGlass;
   412     cmdConnect.Enabled := False;
   413     cmdProxySettings.Enabled := False;
   414     txtHost.Enabled := False;
   415     txtPort.Enabled := False;
   416     txtUsername.Enabled := False;
   417     txtPassword.Enabled := False;
   418     chkPassive.Enabled := False;
   419     cmdConnect.Caption := 'Connecting...';
   420     // ... set values ...
   421     IdFTP.Host := txtHost.Text;
   422     IdFTP.Port := StrToInt(txtPort.Text);
   423     IdFTP.Username := txtUsername.Text;
   424     IdFTP.Passive := chkPassive.Checked;
   425     IdFTP.Password := txtPassword.Text;
   426     // ... connect and check values etc ...
   427     try
   428       IdFTP.Connect(True, 15000);
   429       // ... get initial directory ...
   430       Path := IdFTP.RetrieveCurrentDir;
   431       // ... "fix" path ...
   432       eStr := TStringList.Create;
   433       eStr.Text := StringReplace(Path, '/', #13, [rfReplaceAll]);
   434       for i := eStr.Count -1 downto 0 do begin
   435         if eStr[i] = '' then
   436           eStr.Delete(i);
   437       end;
   438       if (Copy(Path, Length(Path) -1, 1) <> '/') then
   439         Path := Path + '/';
   440       // ... connect successful, change captions ...
   441       trvDirectories.Enabled := True;
   442       cmdConnect.Enabled := True;
   443       cmdConnect.Caption := 'Disconnect';
   444       // ... change to / and create all the directories ...
   445       CurNode := nil;
   446       if (Path <> '/') then begin
   447         try
   448           IdFTP.ChangeDir('/');
   449           with GetAllDirs do begin
   450             for i := 0 to Count -1 do begin
   451               if (Assigned(CurNode)) then
   452                 trvDirectories.Items.AddChild(trvDirectories.Items.Add(nil, Strings[i]), 'Scanning...')
   453               else begin
   454                 CurNode := trvDirectories.Items.Add(nil, Strings[i]);
   455                 trvDirectories.Items.AddChild(CurNode, 'Scanning...');
   456                 if (Pos('/' + CurNode.Text + '/', Path) = 0) then
   457                   CurNode := nil;
   458               end
   459             end;
   460             Free;
   461           end;
   462           IdFTP.ChangeDir(Path);
   463         except
   464           if (IdFTP.Connected) then
   465             IdFTP.ChangeDir(Path)
   466           else
   467             IdFTP.Connect;
   468         end;
   469       end;
   470       // ... find directories in start path ...
   471       if eStr.Count <> 0 then begin
   472         for i := 0 to eStr.Count -1 do begin
   473           if (not ((i = 0) and (Assigned(CurNode)))) then
   474             CurNode := trvDirectories.Items.AddChild(CurNode, eStr[i]);
   475         end;
   476       end;
   477       trvDirectories.Selected := CurNode;
   478       eStr.Free;
   479       // ... scan for directories ...
   480       with GetAllDirs do begin
   481         for i := 0 to Count -1 do
   482           trvDirectories.Items.AddChild(trvDirectories.Items.AddChild(CurNode, Strings[i]), 'Scanning...');
   483         Free;
   484       end;
   485 
   486       if Assigned(CurNode) then
   487         CurNode.Expand(False);
   488     except
   489       on E: Exception do begin
   490         // reset button properties
   491         cmdConnect.Enabled := True;
   492         txtHost.Enabled := True;
   493         txtPort.Enabled := True;
   494         txtUsername.Enabled := True;
   495         txtPassword.Enabled := True;
   496         chkPassive.Enabled := True;
   497         cmdProxySettings.Enabled := True;
   498         cmdNext.Enabled := False;
   499         cmdConnect.Caption := 'Connect';
   500         // analyze messages
   501         if Pos('Login incorrect.', E.Message) <> 0 then begin // login failed
   502           MessageBox(Handle, 'Login incorrect. Check your FTP settings and try again.', PChar(Application.Title), MB_ICONWARNING);
   503           txtUsername.SetFocus;
   504           txtUsername.SelectAll;
   505         end
   506         else if Pos('Host not found.', E.Message) <> 0 then begin // host not found
   507           MessageBox(Handle, 'The entered host couldn''t be found. Check your settings and try again.', PChar(Application.Title), MB_ICONWARNING);
   508           txtHost.SetFocus;
   509           txtHost.SelectAll;
   510         end
   511         else if Pos('Connection refused.', E.Message) <> 0 then begin // wrong port (?)
   512           MessageBox(Handle, 'The host refused the connection. Check your port and try again.', PChar(Application.Title), MB_ICONWARNING);
   513           txtPort.SetFocus;
   514           txtPort.SelectAll;
   515         end
   516         else if E is EIdProtocolReplyError then begin // wrong port
   517           MessageBox(Handle, 'The port you entered is definitely wrong. Check it and try again.', PChar(Application.Title), MB_ICONWARNING);
   518           txtPort.SetFocus;
   519           txtPort.SelectAll;
   520         end
   521         else
   522           MessageBox(Handle, PChar(E.Message), PChar(Application.Title), MB_ICONWARNING); // unknown error
   523         // ... connect failed, leave procedure ...
   524         Screen.Cursor := crDefault;
   525         exit;
   526       end;
   527     end;
   528 
   529     Screen.Cursor := crDefault;
   530   end
   531   else begin
   532     Screen.Cursor := crHourGlass;
   533     IdFTP.Quit;
   534     trvDirectories.Items.Clear;
   535     trvDirectories.Enabled := False;
   536     cmdConnect.Enabled := True;
   537     cmdProxySettings.Enabled := True;
   538     txtHost.Enabled := True;
   539     txtPort.Enabled := True;
   540     txtUsername.Enabled := True;
   541     txtPassword.Enabled := True;
   542     chkPassive.Enabled := True;
   543     cmdConnect.Caption := 'Connect';
   544     cmdNext.Enabled := False;
   545     Screen.Cursor := crDefault;
   546   end;
   547 end;
   548 
   549 procedure TfrmMain.jplWizardChange(Sender: TObject);
   550 begin
   551   if (jplWizard.ActivePage = jspInstallProgress) then begin
   552     cmdNext.Caption := '&Finish';
   553     cmdNext.Enabled := False;
   554     cmdBack.Visible := False;
   555   end
   556   else begin
   557     cmdNext.Caption := '&Next >';
   558     cmdNext.Enabled := True;
   559     cmdBack.Visible := jplWizard.ActivePageIndex <> 0;
   560   end;
   561 
   562   if (jplWizard.ActivePage = jspLicense) then
   563     cmdNext.Enabled := frbAgree.Checked;
   564 
   565   if (jplWizard.ActivePage = jspFTP) then
   566     cmdNext.Enabled := False;
   567 end;
   568 
   569 procedure TfrmMain.FormCreate(Sender: TObject);
   570 begin
   571   if LowerCase(ParamStr(1)) = '-logftp' then begin
   572     MessageBox(Handle, 'FTP installation will be logged to FTP.log!', PChar(Application.Title), MB_ICONINFORMATION);
   573     IdLogFile.Filename := ExtractFilePath(ParamStr(0)) + 'FTP.log';
   574     IdLogFile.Active := True;
   575   end;
   576 
   577   rtfDetails.Clear;
   578 end;
   579 
   580 procedure TfrmMain.cmdProxySettingsClick(Sender: TObject);
   581 begin
   582   frmProxy.ShowModal;
   583   // Apply Proxy Settings
   584   case frmProxy.cboProxy.ItemIndex of
   585     0: IdFTP.ProxySettings.ProxyType := fpcmNone; // none
   586     1: IdFTP.ProxySettings.ProxyType := fpcmHttpProxyWithFtp; // HTTP Proxy with FTP
   587     2: IdFTP.ProxySettings.ProxyType := fpcmOpen; // Open
   588     3: IdFTP.ProxySettings.ProxyType := fpcmSite; // Site
   589     4: IdFTP.ProxySettings.ProxyType := fpcmTransparent; // Transparent
   590     5: IdFTP.ProxySettings.ProxyType := fpcmUserPass; // User (Password)
   591     6: IdFTP.ProxySettings.ProxyType := fpcmUserSite; // User (Site)
   592   end;
   593 
   594   IdFTP.ProxySettings.Host := frmProxy.txtHost.Text;
   595   IdFTP.ProxySettings.UserName := frmProxy.txtPort.Text;
   596   IdFTP.ProxySettings.Password := frmProxy.txtPassword.Text;
   597   IdFTP.ProxySettings.Port := StrToInt(frmProxy.txtPort.Text);
   598 end;
   599 
   600 procedure TfrmMain.txtPortChange(Sender: TObject);
   601 var i: integer;
   602 begin
   603   if txtPort.Text = '' then
   604     txtPort.Text := '21'
   605   else begin
   606     // check if value is numeric...
   607     for i := Length(txtPort.Text) downto 1 do begin
   608       if Pos(txtPort.Text[i], '0123456789') = 0 then begin
   609         txtPort.Text := '21';
   610         txtPort.SelStart := 4;
   611         exit;
   612       end;
   613     end;
   614   end;
   615 end;
   616 
   617 procedure TfrmMain.trvDirectoriesExpanded(Sender: TObject;
   618   Node: TTreeNode);
   619 var ePath: String;
   620     CurNode: TTreeNode;
   621     i: integer;
   622 begin
   623   if Node.Item[0].Text = 'Scanning...' then begin // no directories added yet
   624     Screen.Cursor := crHourGlass;
   625     // get complete path
   626     ePath := '/';
   627     CurNode := Node;
   628     repeat
   629       ePath := '/' + CurNode.Text + ePath;
   630       CurNode := CurNode.Parent;
   631     until (not Assigned(CurNode));
   632     // change dir and add directories in it
   633     try
   634       Repaint;
   635       IdFTP.ChangeDir(ePath);
   636       with GetAllDirs do begin
   637         Node.Item[0].Free;
   638         for i := 0 to Count -1 do begin
   639           trvDirectories.Items.AddChild(trvDirectories.Items.AddChild(Node, Strings[i]), 'Scanning...');
   640         end;
   641         Free;
   642       end;
   643     finally
   644       Application.ProcessMessages;
   645     end;
   646     Screen.Cursor := crDefault;
   647   end;
   648 end;
   649 
   650 procedure TfrmMain.trvDirectoriesChange(Sender: TObject; Node: TTreeNode);
   651 begin
   652   cmdNext.Enabled := Assigned(trvDirectories.Selected);
   653 end;
   654 
   655 procedure TfrmMain.IdFTPWork(Sender: TObject; AWorkMode: TWorkMode;
   656   const AWorkCount: Integer);
   657 begin
   658   if AWorkCount > 15 then begin
   659     ggeItem.Progress := AWorkCount;
   660     CurrProgress := AWorkCount;
   661   end;
   662 
   663   if Cancel then
   664     IdFTP.Abort;
   665     
   666   Application.ProcessMessages;
   667 end;
   668 
   669 procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
   670 begin
   671   if (jplWizard.ActivePage = jspFTP) and (IdFTP.Connected) then
   672     IdFTP.Quit;
   673   if (jplWizard.ActivePage = jspInstallProgress) and (ggeAll.Progress <> ggeAll.MaxValue) and (not Cancel) then begin
   674     if MessageBox(Handle, 'Do you really want to cancel the installation?', PChar(Application.Title), MB_ICONQUESTION + MB_YESNO) = mrYes then begin
   675       Screen.Cursor := crDefault;
   676       Application.OnException := ExceptionHandler;
   677       Cancel := True;
   678       if IdFTP.Connected then
   679         IdFTP.Quit;
   680     end
   681     else
   682       Action := caNone;
   683   end;
   684 end;
   685 
   686 procedure TfrmMain.ExceptionHandler(Sender: TObject; E: Exception);
   687 begin
   688   // IF any exceptions were raised after close, nobody would want them so leave this empty
   689 end;
   690 
   691 procedure TfrmMain.tmrSpeedTimer(Sender: TObject);
   692 begin
   693   Caption := CalcSpeed(OldProgress, CurrProgress);
   694   OldProgress := CurrProgress;
   695 end;
   696 
   697 procedure TfrmMain.trvDirectoriesExpanding(Sender: TObject;
   698   Node: TTreeNode; var AllowExpansion: Boolean);
   699 begin
   700   Node.ImageIndex := 1;
   701   Node.SelectedIndex := 1;
   702 end;
   703 
   704 procedure TfrmMain.trvDirectoriesCollapsing(Sender: TObject;
   705   Node: TTreeNode; var AllowCollapse: Boolean);
   706 begin
   707   Node.ImageIndex := 0;
   708   Node.SelectedIndex := 0;
   709 end;
   710 
   711 procedure TfrmMain.trvModsClick(Sender: TObject);
   712 begin
   713   if Assigned(trvMods.Selected) then
   714     cmdNext.Enabled := Assigned(trvMods.Selected.Parent)
   715   else
   716     cmdNext.Enabled := False;
   717 end;
   718 
   719 procedure TfrmMain.trvDirectoriesMouseDown(Sender: TObject;
   720   Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
   721 var Node: TTreeNode;
   722 begin
   723   Node := trvDirectories.GetNodeAt(X, Y);
   724   if (Assigned(Node)) then begin
   725     if (Node.DisplayRect(True).Right < X) then
   726       trvDirectories.Selected := nil;
   727   end;
   728 end;
   729 
   730 end.