installer/UnitfrmMain.pas
author Pavol Marko <pavolmarko@gmail.com>
Sun Oct 21 21:55:17 2007 +0000 (2007-10-21)
branchhookman_autogen
changeset 433 a2d986ddd522
parent 417 e4a791e7e3e6
permissions -rw-r--r--
Changed license in sourcehook_hookmangen_x86.h (agreed to by BAILOPAN and faluco)
Updated MSVC7 proj file
     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     Source: Boolean;
   163 begin
   164   { FTP }
   165   if jplWizard.ActivePage = jspFTP then begin
   166     if not IdFTP.Connected then
   167       IdFTP.Connect;
   168     eStr := TStringList.Create;
   169     ePath := '/';
   170     CurNode := trvDirectories.Selected;
   171     if (Assigned(CurNode)) then begin
   172       repeat
   173         ePath := '/' + CurNode.Text + ePath;
   174         CurNode := CurNode.Parent;
   175       until (not Assigned(CurNode));
   176     end;
   177     IdFTP.ChangeDir(ePath);
   178     IdFTP.List(eStr, '', False);
   179     eStr.CaseSensitive := False;
   180     // check if gameinfo.txt is in the directory -> valid installation
   181     if (eStr.IndexOf('gameinfo.txt') = -1) then begin
   182       MessageBox(Handle, 'Invalid directory. Please select your mod directory and try again.', PChar(Application.Title), MB_ICONWARNING);
   183       eStr.Free;
   184       exit;
   185     end
   186     else
   187       eStr.Free;
   188     // check for orangebox directory
   189     Source := True;
   190     if (AnsiSameText(ExtractFileName(trvDirectories.Selected.Text), 'tf')) then begin
   191       case MessageBox(Handle, 'It looks like your server is using the OrangeBox engine. Would you like to install the appropriate binaries for it?', PChar(Application.Title), MB_ICONQUESTION + MB_YESNOCANCEL) of
   192         mrYes: Source := False;
   193         mrNo: Source := True;
   194         mrCancel: begin
   195           eStr.Free;
   196           exit;
   197         end;
   198       end;
   199     end;
   200     // design stuff
   201     trvDirectories.Enabled := False;
   202     cmdConnect.Enabled := False;
   203     optWindows.Enabled := False;
   204     optLinux.Enabled := False;
   205     Screen.Cursor := crHourGlass;
   206 
   207     if optWindows.Checked then
   208       eOS := osWindows
   209     else
   210       eOS := osLinux;
   211 
   212     jspInstallProgress.Show;
   213     // installation
   214     Screen.Cursor := crAppStart;
   215     InstallFTP(eOS, Source);
   216   end
   217   else if jplWizard.ActivePage = jspInstallProgress then
   218     Close
   219   else if jplWizard.ActivePage = jspSelectMod then begin
   220     { Dedicated Server }
   221     if frbDedicatedServer.Checked then begin
   222       Source := True;
   223       ePath := trvMods.Selected.Text;
   224       if ePath = 'Counter-Strike:Source' then
   225         ePath := trvMods.Selected.Parent.Text + '\source dedicated server\cstrike'
   226       else if ePath = 'Day of Defeat:Source' then
   227         ePath := trvMods.Selected.Parent.Text + '\source dedicated server\dod'
   228       else if ePath = 'Half-Life 2 Deathmatch' then
   229         ePath := trvMods.Selected.Parent.Text + '\source dedicated server\hl2mp'
   230       else begin
   231         { get games }
   232         if ePath = 'Team Fortress 2' then
   233           ePath := trvMods.Selected.Parent.Text + '\source 2007 dedicated server\tf';
   234         { ask user, just in case }
   235         case MessageBox(Handle, 'It looks like your server is using the OrangeBox engine. Would you like to install the appropriate binaries for it?', PChar(Application.Title), MB_ICONQUESTION + MB_YESNOCANCEL) of
   236           mrYes: Source := False;
   237           mrNo: Source := True;
   238           mrCancel: exit;
   239         end;
   240       end;
   241       SteamPath := IncludeTrailingPathDelimiter(SteamPath) + 'steamapps\';
   242       // install it
   243       if DirectoryExists(SteamPath + ePath) then begin
   244         jspInstallProgress.Show;
   245         InstallDedicated(IncludeTrailingPathDelimiter(SteamPath + ePath), True, Source);
   246       end
   247       else begin
   248         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);
   249         jspSelectMod.Show;
   250         exit;
   251       end;
   252     end;
   253     { Standalone Server }
   254     if frbStandaloneServer.Checked then begin
   255       Source := True;
   256       ePath := trvMods.Selected.Text;
   257       if ePath = 'Counter-Strike:Source' then
   258         ePath := 'cstrike'
   259       else if ePath = 'Day of Defeat:Source' then
   260         ePath := 'dod'
   261       else if ePath = 'Half-Life 2 Deathmatch' then
   262         ePath := 'hl2mp'
   263       else begin
   264         { get games }
   265         if ePath = 'Team Fortress 2' then
   266           ePath := 'orangebox\tf';
   267         { ask user, just in case }
   268         case MessageBox(Handle, 'It looks like your server is using the OrangeBox engine. Would you like to install the appropriate binaries for it?', PChar(Application.Title), MB_ICONQUESTION + MB_YESNOCANCEL) of
   269           mrYes: Source := False;
   270           mrNo: Source := True;
   271           mrCancel: exit;
   272         end;
   273       end;
   274       // install it
   275       if DirectoryExists(StandaloneServer + ePath) then begin
   276         jspInstallProgress.Show;
   277         InstallDedicated(IncludeTrailingPathDelimiter(StandaloneServer + ePath), False, Source)
   278       end
   279       else begin
   280         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);
   281         jspSelectMod.Show;
   282         exit;
   283       end;
   284     end;
   285     { Listen Server }
   286     if frbListenServer.Checked then begin
   287       Source := True;
   288       ePath := trvMods.Selected.Text;
   289       if ePath = 'Counter-Strike:Source' then
   290         ePath := SteamPath + 'SteamApps\' + trvMods.Selected.Parent.Text + '\counter-strike source\cstrike'
   291       else if ePath = 'Half-Life 2 Deathmatch' then
   292         ePath := SteamPath + 'SteamApps\' + trvMods.Selected.Parent.Text + '\half-life 2 deathmatch\hl2mp'
   293       else if ePath = 'Day of Defeat:Source' then
   294         ePath := SteamPath + 'SteamApps\' + trvMods.Selected.Parent.Text + '\day of defeat source\dod'
   295       else begin
   296         { get games }
   297         if ePath = 'Team Fortress 2' then
   298           ePath := SteamPath + 'SteamApps\' + trvMods.Selected.Parent.Text + '\team fortress 2\tf';
   299         { ask user, just in case }
   300         case MessageBox(Handle, 'It looks like your server is using the OrangeBox engine. Would you like to install the appropriate binaries for it?', PChar(Application.Title), MB_ICONQUESTION + MB_YESNOCANCEL) of
   301           mrYes: Source := False;
   302           mrNo: Source := True;
   303           mrCancel: exit;
   304         end;
   305       end;
   306 
   307       if Pos(SteamPath, ePath) = 0 then
   308         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)
   309       else begin
   310          if not FileExists(ePath + '\gameinfo.txt') then begin
   311           MessageBox(Handle, 'You have to play this game once before installing Metamod:Source. Do that and try again.', PChar(Application.Title), MB_ICONWARNING);
   312           exit;
   313         end;
   314 
   315         jspInstallProgress.Show;
   316         InstallListen(IncludeTrailingPathDelimiter(ePath), Source);
   317       end;
   318     end;
   319     { Custom mod below }
   320   end
   321   else if jplWizard.ActivePage <> jspInstallMethod then
   322     jplWizard.NextPage
   323   else begin
   324     if frbDedicatedServer.Checked then begin    // Dedicated Server
   325       eRegistry := TRegistry.Create(KEY_READ);
   326       try
   327         eRegistry.RootKey := HKEY_CURRENT_USER;
   328         if eRegistry.OpenKey('Software\Valve\Steam', False) then begin
   329           ePath := eRegistry.ReadString('SteamPath');
   330           ePath := IncludeTrailingPathDelimiter(StringReplace(ePath, '/', '\', [rfReplaceAll]));
   331           SteamPath := ePath;
   332 
   333           ePath := ePath + 'SteamApps\';
   334           if DirectoryExists(ePath) then begin
   335             trvMods.Items.Clear;
   336             // Check Mods
   337             eStr := GetAllFiles(ePath + '*.*', faDirectory, False, True, False);
   338             for i := 0 to eStr.Count -1 do begin
   339               CurNode := trvMods.Items.Add(nil, eStr[i]);
   340 
   341               if DirectoryExists(ePath + eStr[i] + '\source dedicated server\cstrike') then
   342                 trvMods.Items.AddChild(CurNode, 'Counter-Strike:Source');
   343               if DirectoryExists(ePath + eStr[i] + '\source dedicated server\dod') then
   344                 trvMods.Items.AddChild(CurNode, 'Day of Defeat:Source');
   345               if DirectoryExists(ePath + eStr[i] + '\source dedicated server\hl2mp') then
   346                 trvMods.Items.AddChild(CurNode, 'Half-Life 2 Deatmatch');
   347               if DirectoryExists(ePath + eStr[i] + '\source 2007 dedicated server\tf') then
   348                 trvMods.Items.AddChild(CurNode, 'Team Fortress 2');
   349 
   350               if CurNode.Count = 0 then
   351                 CurNode.Free
   352               else
   353                 CurNode.Expand(False);
   354             end;
   355             // Misc
   356             jspSelectMod.Show;
   357             trvMods.Selected := nil;
   358             cmdNext.Enabled := False;
   359           end
   360           else
   361             MessageBox(Handle, 'You have to run Dedicated Server once before installing Metamod:Source!', 'Error', MB_ICONWARNING);
   362         end
   363         else
   364           MessageBox(Handle, 'You haven''t installed Steam yet! Download it at www.steampowered.com, install Dedicated Server and try again.', 'Error', MB_ICONWARNING);
   365       finally
   366         eRegistry.Free;
   367       end;
   368     end
   369     else if frbListenServer.Checked then begin  // Listen Server
   370       eRegistry := TRegistry.Create(KEY_READ);
   371       try
   372         eRegistry.RootKey := HKEY_CURRENT_USER;
   373         if eRegistry.OpenKey('Software\Valve\Steam', False) then begin
   374           ePath := eRegistry.ReadString('SteamPath');
   375           ePath := IncludeTrailingPathDelimiter(StringReplace(ePath, '/', '\', [rfReplaceAll]));
   376           SteamPath := ePath;
   377 
   378           ePath := ePath + 'SteamApps\';
   379           if DirectoryExists(ePath) then begin
   380             trvMods.Items.Clear;
   381             // Check Mods
   382             eStr := GetAllFiles(ePath + '*.*', faDirectory, False, True, False);
   383             for i := 0 to eStr.Count -1 do begin
   384               CurNode := trvMods.Items.Add(nil, eStr[i]);
   385 
   386               if DirectoryExists(ePath + eStr[i] + '\counter-strike source') then
   387                 trvMods.Items.AddChild(CurNode, 'Counter-Strike:Source');
   388               if DirectoryExists(ePath + eStr[i] + '\day of defeat source') then
   389                 trvMods.Items.AddChild(CurNode, 'Day of Defeat:Source');
   390               if DirectoryExists(ePath + eStr[i] + '\half-life 2 deathmatch') then
   391                 trvMods.Items.AddChild(CurNode, 'Half-Life 2 Deatmatch');
   392               if DirectoryExists(ePath + eStr[i] + '\team fortress 2') then
   393                 trvMods.Items.AddChild(CurNode, 'Team Fortress 2');
   394 
   395               if CurNode.Count = 0 then
   396                 CurNode.Free
   397               else
   398                 CurNode.Expand(False);
   399             end;
   400             // Misc
   401             jspSelectMod.Show;
   402             trvMods.Selected := nil;
   403             cmdNext.Enabled := False;
   404           end
   405           else
   406             MessageBox(Handle, 'You haven''t installed Steam yet! Download it at www.steampowered.com, install Dedicated Server and try again.', 'Error', MB_ICONWARNING);
   407         end
   408         else
   409           MessageBox(Handle, 'You haven''t installed Steam yet! Download it at www.steampowered.com, install Dedicated Server and try again.', 'Error', MB_ICONWARNING);
   410       finally
   411         eRegistry.Free;
   412       end;
   413     end
   414     else if frbStandaloneServer.Checked then begin // Standalone Server
   415       eRegistry := TRegistry.Create;
   416       try
   417         eRegistry.RootKey := HKEY_CURRENT_USER;
   418         if eRegistry.OpenKey('Software\Valve\HLServer', False) then begin
   419           StandaloneServer := IncludeTrailingPathDelimiter(eRegistry.ReadString('InstallPath'));
   420           if DirectoryExists(StandaloneServer + 'cstrike') then
   421             trvMods.Items.Add(nil, 'Counter-Strike:Source');
   422           if DirectoryExists(StandaloneServer + 'dod') then
   423             trvMods.Items.Add(nil, 'Day of Defeat:Source');
   424           if DirectoryExists(StandaloneServer + 'hl2mp') then
   425             trvMods.Items.Add(nil, 'Half-Life 2 Deatmatch');
   426           if DirectoryExists(StandaloneServer + 'orangebox\tf') then
   427             trvMods.Items.Add(nil, 'Team Fortress 2');
   428           jspSelectMod.Show;
   429           cmdNext.Enabled := False;
   430         end
   431         else
   432           MessageBox(Handle, 'You haven''t installed Half-Life Dedicated Server yet!',  'Error', MB_ICONWARNING);
   433       finally
   434         eRegistry.Free;
   435       end;
   436     end
   437     else if frbSelectMod.Checked then begin 
   438       { Custom mod }
   439       if frmSelectModPath.ShowModal = mrOk then begin
   440         ePath := frmSelectModPath.trvDirectory.SelectedFolder.PathName;
   441         { check if this is an orangebox game }
   442         Source := True;
   443         if (AnsiSameText(ExtractFileName(ePath), 'tf')) then begin
   444           case MessageBox(Handle, 'It looks like your server is using the OrangeBox engine. Would you like to install the appropriate binaries for it?', PChar(Application.Title), MB_ICONQUESTION + MB_YESNOCANCEL) of
   445             mrYes: Source := False;
   446             mrNo: Source := True;
   447             mrCancel: exit;
   448           end;
   449         end;
   450         { install now }
   451         jspInstallProgress.Show;
   452         InstallCustom(IncludeTrailingPathDelimiter(ePath), osWindows, Source);
   453       end;
   454     end
   455     else if frbFTP.Checked then // FTP
   456       jspFTP.Show;
   457   end;
   458 end;
   459 
   460 procedure TfrmMain.CheckNext(Sender: TObject);
   461 begin
   462   cmdNext.Enabled := frbAgree.Checked;
   463 end;
   464 
   465 procedure TfrmMain.cmdBackClick(Sender: TObject);
   466 begin
   467   if jplWizard.ActivePage = jspFTP then
   468     jspInstallMethod.Show
   469   else begin
   470     jplWizard.PrevPage;
   471     cmdBack.Visible := jplWizard.ActivePageIndex <> 0;
   472   end;
   473 end;
   474 
   475 procedure TfrmMain.cmdConnectClick(Sender: TObject);
   476 var i: integer;
   477     eStr: TStringList;
   478     CurNode: TTreeNode;
   479     Path: String;
   480 begin
   481   if (Trim(txtHost.Text) = '') or (Trim(txtUsername.Text) = '') then
   482     MessageBox(Handle, 'Please fill in each field!', PChar(Application.Title), MB_ICONWARNING)
   483   else if cmdConnect.Caption = 'Connect' then begin
   484     // ... design stuff ...
   485     Screen.Cursor := crHourGlass;
   486     cmdConnect.Enabled := False;
   487     cmdProxySettings.Enabled := False;
   488     txtHost.Enabled := False;
   489     txtPort.Enabled := False;
   490     txtUsername.Enabled := False;
   491     txtPassword.Enabled := False;
   492     chkPassive.Enabled := False;
   493     cmdConnect.Caption := 'Connecting...';
   494     // ... set values ...
   495     IdFTP.Host := txtHost.Text;
   496     IdFTP.Port := StrToInt(txtPort.Text);
   497     IdFTP.Username := txtUsername.Text;
   498     IdFTP.Passive := chkPassive.Checked;
   499     IdFTP.Password := txtPassword.Text;
   500     // ... connect and check values etc ...
   501     try
   502       IdFTP.Connect(True, 15000);
   503       // ... get initial directory ...
   504       Path := IdFTP.RetrieveCurrentDir;
   505       // ... "fix" path ...
   506       eStr := TStringList.Create;
   507       eStr.Text := StringReplace(Path, '/', #13, [rfReplaceAll]);
   508       for i := eStr.Count -1 downto 0 do begin
   509         if eStr[i] = '' then
   510           eStr.Delete(i);
   511       end;
   512       if (Copy(Path, Length(Path) -1, 1) <> '/') then
   513         Path := Path + '/';
   514       // ... connect successful, change captions ...
   515       trvDirectories.Enabled := True;
   516       cmdConnect.Enabled := True;
   517       cmdConnect.Caption := 'Disconnect';
   518       // ... change to / and create all the directories ...
   519       CurNode := nil;
   520       if (Path <> '/') then begin
   521         try
   522           IdFTP.ChangeDir('/');
   523           with GetAllDirs do begin
   524             for i := 0 to Count -1 do begin
   525               if (Assigned(CurNode)) then
   526                 trvDirectories.Items.AddChild(trvDirectories.Items.Add(nil, Strings[i]), 'Scanning...')
   527               else begin
   528                 CurNode := trvDirectories.Items.Add(nil, Strings[i]);
   529                 trvDirectories.Items.AddChild(CurNode, 'Scanning...');
   530                 if (Pos('/' + CurNode.Text + '/', Path) = 0) then
   531                   CurNode := nil;
   532               end
   533             end;
   534             Free;
   535           end;
   536           IdFTP.ChangeDir(Path);
   537         except
   538           if (IdFTP.Connected) then
   539             IdFTP.ChangeDir(Path)
   540           else
   541             IdFTP.Connect;
   542         end;
   543       end;
   544       // ... find directories in start path ...
   545       if eStr.Count <> 0 then begin
   546         for i := 0 to eStr.Count -1 do begin
   547           if (not ((i = 0) and (Assigned(CurNode)))) then
   548             CurNode := trvDirectories.Items.AddChild(CurNode, eStr[i]);
   549         end;
   550       end;
   551       trvDirectories.Selected := CurNode;
   552       eStr.Free;
   553       // ... scan for directories ...
   554       with GetAllDirs do begin
   555         for i := 0 to Count -1 do
   556           trvDirectories.Items.AddChild(trvDirectories.Items.AddChild(CurNode, Strings[i]), 'Scanning...');
   557         Free;
   558       end;
   559 
   560       if Assigned(CurNode) then
   561         CurNode.Expand(False);
   562     except
   563       on E: Exception do begin
   564         // reset button properties
   565         cmdConnect.Enabled := True;
   566         txtHost.Enabled := True;
   567         txtPort.Enabled := True;
   568         txtUsername.Enabled := True;
   569         txtPassword.Enabled := True;
   570         chkPassive.Enabled := True;
   571         cmdProxySettings.Enabled := True;
   572         cmdNext.Enabled := False;
   573         cmdConnect.Caption := 'Connect';
   574         // analyze messages
   575         if Pos('Login incorrect.', E.Message) <> 0 then begin // login failed
   576           MessageBox(Handle, 'Login incorrect. Check your FTP settings and try again.', PChar(Application.Title), MB_ICONWARNING);
   577           txtUsername.SetFocus;
   578           txtUsername.SelectAll;
   579         end
   580         else if Pos('Host not found.', E.Message) <> 0 then begin // host not found
   581           MessageBox(Handle, 'The entered host couldn''t be found. Check your settings and try again.', PChar(Application.Title), MB_ICONWARNING);
   582           txtHost.SetFocus;
   583           txtHost.SelectAll;
   584         end
   585         else if Pos('Connection refused.', E.Message) <> 0 then begin // wrong port (?)
   586           MessageBox(Handle, 'The host refused the connection. Check your port and try again.', PChar(Application.Title), MB_ICONWARNING);
   587           txtPort.SetFocus;
   588           txtPort.SelectAll;
   589         end
   590         else if E is EIdProtocolReplyError then begin // wrong port
   591           MessageBox(Handle, 'The port you entered is definitely wrong. Check it and try again.', PChar(Application.Title), MB_ICONWARNING);
   592           txtPort.SetFocus;
   593           txtPort.SelectAll;
   594         end
   595         else
   596           MessageBox(Handle, PChar(E.Message), PChar(Application.Title), MB_ICONWARNING); // unknown error
   597         // ... connect failed, leave procedure ...
   598         Screen.Cursor := crDefault;
   599         exit;
   600       end;
   601     end;
   602 
   603     Screen.Cursor := crDefault;
   604   end
   605   else begin
   606     Screen.Cursor := crHourGlass;
   607     IdFTP.Quit;
   608     trvDirectories.Items.Clear;
   609     trvDirectories.Enabled := False;
   610     cmdConnect.Enabled := True;
   611     cmdProxySettings.Enabled := True;
   612     txtHost.Enabled := True;
   613     txtPort.Enabled := True;
   614     txtUsername.Enabled := True;
   615     txtPassword.Enabled := True;
   616     chkPassive.Enabled := True;
   617     cmdConnect.Caption := 'Connect';
   618     cmdNext.Enabled := False;
   619     Screen.Cursor := crDefault;
   620   end;
   621 end;
   622 
   623 procedure TfrmMain.jplWizardChange(Sender: TObject);
   624 begin
   625   if (jplWizard.ActivePage = jspInstallProgress) then begin
   626     cmdNext.Caption := '&Finish';
   627     cmdNext.Enabled := False;
   628     cmdBack.Visible := False;
   629   end
   630   else begin
   631     cmdNext.Caption := '&Next >';
   632     cmdNext.Enabled := True;
   633     cmdBack.Visible := jplWizard.ActivePageIndex <> 0;
   634   end;
   635 
   636   if (jplWizard.ActivePage = jspLicense) then
   637     cmdNext.Enabled := frbAgree.Checked;
   638 
   639   if (jplWizard.ActivePage = jspFTP) then
   640     cmdNext.Enabled := False;
   641 end;
   642 
   643 procedure TfrmMain.FormCreate(Sender: TObject);
   644 begin
   645   if LowerCase(ParamStr(1)) = '-logftp' then begin
   646     MessageBox(Handle, 'FTP installation will be logged to FTP.log!', PChar(Application.Title), MB_ICONINFORMATION);
   647     IdLogFile.Filename := ExtractFilePath(ParamStr(0)) + 'FTP.log';
   648     IdLogFile.Active := True;
   649   end;
   650 
   651   rtfDetails.Clear;
   652 end;
   653 
   654 procedure TfrmMain.cmdProxySettingsClick(Sender: TObject);
   655 begin
   656   frmProxy.ShowModal;
   657   // Apply Proxy Settings
   658   case frmProxy.cboProxy.ItemIndex of
   659     0: IdFTP.ProxySettings.ProxyType := fpcmNone; // none
   660     1: IdFTP.ProxySettings.ProxyType := fpcmHttpProxyWithFtp; // HTTP Proxy with FTP
   661     2: IdFTP.ProxySettings.ProxyType := fpcmOpen; // Open
   662     3: IdFTP.ProxySettings.ProxyType := fpcmSite; // Site
   663     4: IdFTP.ProxySettings.ProxyType := fpcmTransparent; // Transparent
   664     5: IdFTP.ProxySettings.ProxyType := fpcmUserPass; // User (Password)
   665     6: IdFTP.ProxySettings.ProxyType := fpcmUserSite; // User (Site)
   666   end;
   667 
   668   IdFTP.ProxySettings.Host := frmProxy.txtHost.Text;
   669   IdFTP.ProxySettings.UserName := frmProxy.txtPort.Text;
   670   IdFTP.ProxySettings.Password := frmProxy.txtPassword.Text;
   671   IdFTP.ProxySettings.Port := StrToInt(frmProxy.txtPort.Text);
   672 end;
   673 
   674 procedure TfrmMain.txtPortChange(Sender: TObject);
   675 var i: integer;
   676 begin
   677   if txtPort.Text = '' then
   678     txtPort.Text := '21'
   679   else begin
   680     // check if value is numeric...
   681     for i := Length(txtPort.Text) downto 1 do begin
   682       if Pos(txtPort.Text[i], '0123456789') = 0 then begin
   683         txtPort.Text := '21';
   684         txtPort.SelStart := 4;
   685         exit;
   686       end;
   687     end;
   688   end;
   689 end;
   690 
   691 procedure TfrmMain.trvDirectoriesExpanded(Sender: TObject;
   692   Node: TTreeNode);
   693 var ePath: String;
   694     CurNode: TTreeNode;
   695     i: integer;
   696 begin
   697   if Node.Item[0].Text = 'Scanning...' then begin // no directories added yet
   698     Screen.Cursor := crHourGlass;
   699     // get complete path
   700     ePath := '/';
   701     CurNode := Node;
   702     repeat
   703       ePath := '/' + CurNode.Text + ePath;
   704       CurNode := CurNode.Parent;
   705     until (not Assigned(CurNode));
   706     // change dir and add directories in it
   707     try
   708       Repaint;
   709       IdFTP.ChangeDir(ePath);
   710       with GetAllDirs do begin
   711         Node.Item[0].Free;
   712         for i := 0 to Count -1 do begin
   713           trvDirectories.Items.AddChild(trvDirectories.Items.AddChild(Node, Strings[i]), 'Scanning...');
   714         end;
   715         Free;
   716       end;
   717     finally
   718       Application.ProcessMessages;
   719     end;
   720     Screen.Cursor := crDefault;
   721   end;
   722 end;
   723 
   724 procedure TfrmMain.trvDirectoriesChange(Sender: TObject; Node: TTreeNode);
   725 begin
   726   cmdNext.Enabled := Assigned(trvDirectories.Selected);
   727 end;
   728 
   729 procedure TfrmMain.IdFTPWork(Sender: TObject; AWorkMode: TWorkMode;
   730   const AWorkCount: Integer);
   731 begin
   732   if AWorkCount > 15 then begin
   733     ggeItem.Progress := AWorkCount;
   734     CurrProgress := AWorkCount;
   735   end;
   736 
   737   if Cancel then
   738     IdFTP.Abort;
   739     
   740   Application.ProcessMessages;
   741 end;
   742 
   743 procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
   744 begin
   745   if (jplWizard.ActivePage = jspFTP) and (IdFTP.Connected) then
   746     IdFTP.Quit;
   747   if (jplWizard.ActivePage = jspInstallProgress) and (ggeAll.Progress <> ggeAll.MaxValue) and (not Cancel) then begin
   748     if MessageBox(Handle, 'Do you really want to cancel the installation?', PChar(Application.Title), MB_ICONQUESTION + MB_YESNO) = mrYes then begin
   749       Screen.Cursor := crDefault;
   750       Application.OnException := ExceptionHandler;
   751       Cancel := True;
   752       if IdFTP.Connected then
   753         IdFTP.Quit;
   754     end
   755     else
   756       Action := caNone;
   757   end;
   758 end;
   759 
   760 procedure TfrmMain.ExceptionHandler(Sender: TObject; E: Exception);
   761 begin
   762   // IF any exceptions were raised after close, nobody would want them so leave this empty
   763 end;
   764 
   765 procedure TfrmMain.tmrSpeedTimer(Sender: TObject);
   766 begin
   767   Caption := CalcSpeed(OldProgress, CurrProgress);
   768   OldProgress := CurrProgress;
   769 end;
   770 
   771 procedure TfrmMain.trvDirectoriesExpanding(Sender: TObject;
   772   Node: TTreeNode; var AllowExpansion: Boolean);
   773 begin
   774   Node.ImageIndex := 1;
   775   Node.SelectedIndex := 1;
   776 end;
   777 
   778 procedure TfrmMain.trvDirectoriesCollapsing(Sender: TObject;
   779   Node: TTreeNode; var AllowCollapse: Boolean);
   780 begin
   781   Node.ImageIndex := 0;
   782   Node.SelectedIndex := 0;
   783 end;
   784 
   785 procedure TfrmMain.trvModsClick(Sender: TObject);
   786 begin
   787   if Assigned(trvMods.Selected) then
   788     cmdNext.Enabled := Assigned(trvMods.Selected.Parent)
   789   else
   790     cmdNext.Enabled := False;
   791 end;
   792 
   793 procedure TfrmMain.trvDirectoriesMouseDown(Sender: TObject;
   794   Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
   795 var Node: TTreeNode;
   796 begin
   797   Node := trvDirectories.GetNodeAt(X, Y);
   798   if (Assigned(Node)) then begin
   799     if (Node.DisplayRect(True).Right < X) then
   800       trvDirectories.Selected := nil;
   801   end;
   802 end;
   803 
   804 end.