{
********************************************************************************

SPackGui
Copyright (C) 2013 Geoffray Levasseur <geoffray.levasseurbrandin@numericable.fr>.
Copyright (C) <date> <add your name and mail address here>

http://www.geoffray-levasseur.org/
http://0.tuxfamilly.org/

This software is governed by the CeCILL license under French law and
abiding by the rules of distribution of free software.  You can  use,
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info".

As a counterpart to the access to the source code and  rights to copy,
modify and redistribute granted by the license, users are provided only
with a limited warranty  and the software's author,  the holder of the
economic rights,  and the successive licensors  have only  limited
liability.

In this respect, the user's attention is drawn to the risks associated
with loading,  using,  modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean  that it is complicated to manipulate,  and  that  also
therefore means  that it is reserved for developers  and  experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or
data to be ensured and,  more generally, to use and operate it in the
same conditions as regards security.

The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.

********************************************************************************

Description:
  configuration dialog for command line and network parameters

********************************************************************************
}
unit frmEnvironementSettings;

{$include ../common/defines.inc}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Buttons,
  ComCtrls, StdCtrls, maskedit;

type

  { TfEnvironmentSettings }

  TfEnvironmentSettings = class(TForm)
    btnBrowseInstPkgDir: TButton;
    btnBrowsePkgDwlDir: TButton;
    btnBrowseRepoDwlDir: TButton;
    btnGetSystemProxy: TBitBtn;
    btnCopyToFtp: TBitBtn;
    btnCopyToHttp: TBitBtn;
    btnCancel: TBitBtn;
    btnOk: TBitBtn;
    dlgSelectDirectory: TSelectDirectoryDialog;
    edInstall: TEdit;
    edInstPackageDir: TEdit;
    edPackageDwlDir: TEdit;
    edRepoDwlDir: TEdit;
    edReinstall: TEdit;
    edRemove: TEdit;
    edUpdate: TEdit;
    edHttpProxyAddress: TEdit;
    edHttpProxyPort: TEdit;
    edHttpProxyUser: TEdit;
    edFtpProxyAddress: TEdit;
    edFtpProxyPort: TEdit;
    edFtpProxyUser: TEdit;
    gbHttpProxy: TGroupBox;
    gbFtpProxy: TGroupBox;
    lbInstPackageDir: TLabel;
    lbPackageDwlDir: TLabel;
    lbRepoDwlDir: TLabel;
    lbHttpProxyAddress: TLabel;
    lbHttpProxyPort: TLabel;
    lbHttpProxyName: TLabel;
    lbHttpProxyPass: TLabel;
    lbFtpProxyAddress: TLabel;
    lbFtpProxyPort: TLabel;
    lbFtpProxyUser: TLabel;
    lbFtpProxyPass: TLabel;
    lbInstall: TLabel;
    lbRemove: TLabel;
    lbUpdate: TLabel;
    lbReinstall: TLabel;
    medHttpProxyPass: TMaskEdit;
    medFtpProxyPass: TMaskEdit;
    pcMain: TPageControl;
    tsPaths: TTabSheet;
    tsNetwork: TTabSheet;
    tsCommands: TTabSheet;
    procedure btnBrowseInstPkgDirClick(Sender: TObject);
    procedure btnBrowseRepoDwlDirClick(Sender: TObject);
    procedure btnBrowsePkgDwlDirClick(Sender: TObject);
    procedure btnCopyToFtpClick(Sender: TObject);
    procedure btnCopyToHttpClick(Sender: TObject);
    procedure btnGetSystemProxyClick(Sender: TObject);
    procedure btnOkClick(Sender: TObject);
    procedure edFtpProxyAddressChange(Sender: TObject);
    procedure edHttpProxyAddressChange(Sender: TObject);
    procedure edInstallChange(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
    PathChanged: Boolean;
  end;

var
  fEnvironmentSettings: TfEnvironmentSettings;

implementation

uses
  uIconManager, uCommon, uStrings, uUtils, uDebug;

{$R *.lfm}

{ TfEnvironmentSettings }

procedure TfEnvironmentSettings.FormCreate(Sender: TObject);
begin
  PathChanged := False;
  //init icons
  btnOk.Tag := icOk;
  btnCancel.Tag := icCancel;
  btnCopyToFtp.Tag := icDown;
  btnCopyToHttp.Tag := icUp;
  InitBtnGlyphs(Self);
  //init interface
  pcMain.ActivePageIndex := 0;
  edInstall.Text := sInstallPackage;
  edReinstall.Text := sReinstallPackage;
  edRemove.Text := sRemovePackage;
  edUpdate.Text :=  sUpdatePackage;
  edInstPackageDir.Text := sSpackPkgDir;
  edPackageDwlDir.Text := sDwlDir;
  edRepoDwlDir.Text := sRepoIndexDir;
  edHttpProxyAddress.Text := sHttpProxyAddress;
  if iHttpProxyPort <> 0 then
    edHttpProxyPort.Text := IntToStr(iHttpProxyPort)
  else
   edHttpProxyPort.Text := '';
  edHttpProxyUser.Text := sHttpProxyUser;
  medHttpProxyPass.Text := sHttpProxyPass;
  edFtpProxyAddress.Text := sFtpProxyAddress;
  if iFtpProxyPort <> 0 then
    edFtpProxyPort.Text := IntToStr(iFtpProxyPort)
  else
    edFtpProxyPort.Text := '';
  edFtpProxyUser.Text := sFtpProxyUser;
  medFtpProxyPass.Text := sFtpProxyPass;
  edInstallChange(nil);
  edHttpProxyAddressChange(nil);
  edFtpProxyAddressChange(nil);
end;

procedure TfEnvironmentSettings.edHttpProxyAddressChange(Sender: TObject);
begin
  edHttpProxyPort.Enabled := edHttpProxyAddress.Text <> '';
  edHttpProxyUser.Enabled := (edHttpProxyAddress.Text <> '') and
    (edHttpProxyPort.Text <> '');
  medHttpProxyPass.Enabled := edHttpProxyUser.Enabled and
    (edHttpProxyUser.Text <> '');
  if (Sender = edHttpProxyAddress) and (edHttpProxyAddress.Text <> '') and
    (edHttpProxyPort.Text = '') then
    edHttpProxyPort.Text := IntToStr(iDefaultProxyPort);
end;

procedure TfEnvironmentSettings.edInstallChange(Sender: TObject);
begin
  btnOk.Enabled := (Trim(edInstall.Text) <> '') and
    (Trim(edReinstall.Text) <> '') and (Trim(edRemove.Text) <> '') and
    (Trim(edUpdate.Text) <> '') and (Trim(edInstPackageDir.Text) <> '') and
    (Trim(edPackageDwlDir.Text) <> '') and (Trim(edRepoDwlDir.Text) <> '');
  PathChanged := True;
end;

procedure TfEnvironmentSettings.FormClose(Sender: TObject;
  var CloseAction: TCloseAction);
begin
  try
    SaveWindowGeometry(Self);
  except
    PrintLnDbg(rsErrorCannotSaveConf, vlLow);
  end;
end;

procedure TfEnvironmentSettings.edFtpProxyAddressChange(Sender: TObject);
begin
  edFtpProxyPort.Enabled := edFtpProxyAddress.Text <> '';
  edFtpProxyUser.Enabled := (edFtpProxyAddress.Text <> '') and
    (edFtpProxyPort.Text <> '');
  medFtpProxyPass.Enabled := edFtpProxyUser.Enabled and
    (edFtpProxyUser.Text <> '');
  if (Sender = edFtpProxyAddress) and (edFtpProxyAddress.Text <> '') and
    (edFtpProxyPort.Text = '') then
    edFtpProxyPort.Text := IntToStr(iDefaultProxyPort);
end;

procedure TfEnvironmentSettings.btnCopyToFtpClick(Sender: TObject);
begin
  edFtpProxyAddress.Text := edHttpProxyAddress.Text;
  edFtpProxyPort.Text := edHttpProxyPort.Text;
  edFtpProxyUser.Text := edHttpProxyUser.Text;
  medFtpProxyPass.Text := medHttpProxyPass.Text;
  edFtpProxyAddressChange(Sender);
end;

procedure TfEnvironmentSettings.btnBrowsePkgDwlDirClick(Sender: TObject);
begin
  dlgSelectDirectory.FileName := edRepoDwlDir.Text;
  if dlgSelectDirectory.Execute then
    edRepoDwlDir.Text := dlgSelectDirectory.FileName;
end;

procedure TfEnvironmentSettings.btnBrowseRepoDwlDirClick(Sender: TObject);
begin
  dlgSelectDirectory.FileName := edPackageDwlDir.Text;
  if dlgSelectDirectory.Execute then
    edPackageDwlDir.Text := dlgSelectDirectory.FileName;
end;

procedure TfEnvironmentSettings.btnBrowseInstPkgDirClick(Sender: TObject);
begin
  dlgSelectDirectory.FileName := edInstPackageDir.Text;
  if dlgSelectDirectory.Execute then
    edInstPackageDir.Text := dlgSelectDirectory.FileName;
end;

procedure TfEnvironmentSettings.btnCopyToHttpClick(Sender: TObject);
begin
  edHttpProxyAddress.Text := edFtpProxyAddress.Text;
  edHttpProxyPort.Text := edFtpProxyPort.Text;
  edHttpProxyUser.Text := edFtpProxyUser.Text;
  medHttpProxyPass.Text := medFtpProxyPass.Text;
  edHttpProxyAddressChange(Sender);
end;

procedure TfEnvironmentSettings.btnGetSystemProxyClick(Sender: TObject);
var
  sHttp, sFtp: string;
  Usr, Pass, Addr, Port: string;
begin
  sHttp := GetEnvironmentVariable(rsEnvHttpProxy);
  sFtp := GetEnvironmentVariable(rsEnvFtpProxy);
  if sHttp <> '' then
  begin
    PrintLnDbg(Format(rsInfoGetHttpProxySettings, [sHttp]), vlLow);
    GetProxyInfo(sHttp, Usr, Pass, Addr, Port);
    edHttpProxyUser.Text := Usr;
    medHttpProxyPass.Text := Pass;
    edHttpProxyAddress.Text := Addr;
    edHttpProxyPort.Text := Port;
  end;
  if sFtp <> '' then
  begin
    PrintLnDbg(Format(rsInfoGetFtpProxySettings, [sFtp]), vlLow);
    GetProxyInfo(sFtp, Usr, Pass, Addr, Port);
    edFtpProxyUser.Text := Usr;
    medFtpProxyPass.Text := Pass;
    edFtpProxyAddress.Text := Addr;
    edFtpProxyPort.Text := Port;
  end;
end;

procedure TfEnvironmentSettings.btnOkClick(Sender: TObject);
var
  Tmp: string;
begin
  iniMain.WriteString(rsConfSectionCommand, rsConfNameInstallCommand,
    edInstall.Text);
  iniMain.WriteString(rsConfSectionCommand, rsConfNameReinstallCommand,
    edReinstall.Text);
  iniMain.WriteString(rsConfSectionCommand, rsConfNameRemoveCommand,
    edRemove.Text);
  iniMain.WriteString(rsConfSectionCommand, rsConfNameUpdateCommand,
    edUpdate.Text);
  iniMain.WriteString(rsConfSectionPaths, rsConfNameInstPkgPath,
    edInstPackageDir.Text);
  iniMain.WriteString(rsConfSectionPaths, rsConfNamePkgDownloadPath,
    edPackageDwlDir.Text);
  iniMain.WriteString(rsConfSectionPaths, rsConfNameRepoDownloadPath,
    edRepoDwlDir.Text);
  Tmp := '';
  if Trim(edHttpProxyUser.Text) <> '' then
  begin
    Tmp := Trim(edHttpProxyUser.Text);
    if Trim(medHttpProxyPass.Text) <> '' then
      Tmp := Tmp + ':' + medHttpProxyPass.Text;
  end;
  if Tmp <> '' then
    Tmp := Tmp + '@';
  Tmp := Tmp + Trim(edHttpProxyAddress.Text);
  if Trim(edHttpProxyPort.Text) <> '' then
    Tmp := Tmp + ':' + edHttpProxyPort.Text;
  iniMain.WriteString(rsConfSectionProxy, rsConfNameHttpProxy, Tmp);
  Tmp := '';
  if Trim(edFtpProxyUser.Text) <> '' then
  begin
    Tmp := Trim(edFtpProxyUser.Text);
    if Trim(medFtpProxyPass.Text) <> '' then
      Tmp := Tmp + ':' + medFtpProxyPass.Text;
  end;
  if Tmp <> '' then
    Tmp := Tmp + '@';
  Tmp := Tmp + Trim(edFtpProxyAddress.Text);
  if Trim(edFtpProxyPort.Text) <> '' then
    Tmp := Tmp + ':' + edFtpProxyPort.Text;
  iniMain.WriteString(rsConfSectionProxy, rsConfNameFtpProxy, Tmp);
end;

end.

