unit U_ProtectionBaseParams;

interface
                                                  
uses                          
  Windows, U_LNG, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, RzTabs, ExtCtrls, RzPanel, RzButton, RzRadChk,

  U_ProtectionBase, U_ProtectionCommon, U_BaseSettings, StdCtrls, Mask, RzEdit, Buttons, siComp, siLngLnk,
  RzLabel;

type
  TF_ProtectionBaseParams = class(TForm)
    RzPanel1: TRzPanel;
    pcMain: TRzPageControl;
    tsMain: TRzTabSheet;
    gbLicenseType: TRzGroupBox;
    cbLocal: TRzRadioButton;
    cbMultiUser: TRzRadioButton;
    RzPanel2: TRzPanel;
    cbNoAsk: TRzCheckBox;
    btApply: TRzBitBtn;
    btCancel: TRzBitBtn;
    pnMultiuserParams: TRzPanel;
    Label1: TLabel;
    Label2: TLabel;
    edServerName: TRzEdit;
    edLocalPathName: TRzEdit;
    pnLocal: TRzPanel;
    Label3: TLabel;
    edPathToNB: TRzEdit;
    btSelectPathToNB: TSpeedButton;
    OpenDialog: TOpenDialog;
    lng_Forms: TsiLangLinked;
    btBrowseServer: TSpeedButton;
    btSelRemotePath: TSpeedButton;
    lbConnectParams: TRzLabel;
    procedure cbLocalClick(Sender: TObject);
    procedure cbMultiUserClick(Sender: TObject);
    procedure btSelectPathToNBClick(Sender: TObject);
    procedure btApplyClick(Sender: TObject);
    procedure btBrowseServerClick(Sender: TObject);
    procedure btSelRemotePathClick(Sender: TObject);
    procedure lbConnectParamsClick(Sender: TObject);
  private
    procedure SetControls;
  public
    FDisableEdit: Boolean;
    FTestConnect: Boolean;
    function GetParams: TProtectionBaseParams;
    procedure SetParams(AParams: TProtectionBaseParams);
  end;

//var
  //F_ProtectionBaseParams: TF_ProtectionBaseParams;

implementation


Uses U_BaseCommon, U_BaseConstants, U_Main, U_BaseConnectParams;

{$R *.dfm}

{ TForm1 }



{ TF_ProtectionBaseParams }

function TF_ProtectionBaseParams.GetParams: TProtectionBaseParams;
begin
  ZeroMemory(@Result, SizeOf(TProtectionBaseParams));

  {$IF Defined(FLASH_SCS)}
      if GSCSIni.NB.SaveConnParams then
      begin
        if cbLocal.Checked then
          Result.LicenceType := ltLocal
        else
        if cbMultiUser.Checked then
          Result.LicenceType := ltMultiUser;

        Result.NoAsk := cbNoAsk.Checked;
        Result.ServerName := edServerName.Text;
        Result.LocalPathToNB := edLocalPathName.Text;
        Result.PathToNB := edPathToNB.Text;
      end
      else
      begin
        Result.NoAsk := not GSCSIni.NB.AutoShowParams;
        Result.ServerName := GSCSIni.NB.ServerName;
        Result.LocalPathToNB := GSCSIni.NB.LocalPathToNB;
        Result.PathToNB := '';
        Result.LicenceType := ltLocal;
      end;  
  {$ELSE}
    if cbLocal.Checked then
      Result.LicenceType := ltLocal
    else
    if cbMultiUser.Checked then
      Result.LicenceType := ltMultiUser;

    Result.NoAsk := cbNoAsk.Checked;
    Result.ServerName := edServerName.Text;
    Result.LocalPathToNB := edLocalPathName.Text;
    Result.PathToNB := edPathToNB.Text;
  {$IFEND}  

end;

procedure TF_ProtectionBaseParams.SetParams(AParams: TProtectionBaseParams);
begin
  pcMain.ActivePage := tsMain;
  if AParams.LicenceType = ltLocal then
    cbLocal.Checked := true
  else
  if AParams.LicenceType = ltMultiUser then
    cbMultiUser.Checked := true;
  cbNoAsk.Checked := AParams.NoAsk;
  edServerName.Text := AParams.ServerName;
  edLocalPathName.Text := AParams.LocalPathToNB;
  edPathToNB.Text := AParams.PathToNB;
end;

procedure TF_ProtectionBaseParams.SetControls;
begin
  pnLocal.Enabled := cbLocal.Checked;
  pnMultiuserParams.Enabled := cbMultiUser.Checked;
end;

procedure TF_ProtectionBaseParams.cbLocalClick(Sender: TObject);
begin
  SetControls;
end;

procedure TF_ProtectionBaseParams.cbMultiUserClick(Sender: TObject);
begin
  SetControls;
end;

procedure TF_ProtectionBaseParams.btSelectPathToNBClick(Sender: TObject);
begin
  OpenDialog.FileName := '';
  if FileExists(edPathToNB.Text) then
    OpenDialog.FileName := edPathToNB.Text;
  OpenDialog.InitialDir := ExtractFileDir(edPathToNB.Text);
  if OpenDialog.Execute then
  begin
    edPathToNB.Text := OpenDialog.FileName;
  end;
end;

procedure TF_ProtectionBaseParams.btApplyClick(Sender: TObject);
var
  Params: TProtectionBaseParams;
  DefNBPath: string;
begin
  Params := GetParams;

  // Сохраняем общие настройки //12.05.2009
  // Сохранить в реестр
  SaveBoolToRegistry(pnShowLicenceType, Not Params.NoAsk);

  {$IF Defined(FLASH_SCS)}
    GSCSIni.NB.AutoShowParams := Not Params.NoAsk;
    WriteNBIni(GSCSIni.NB);
  {$IFEND}


  if Not FDisableEdit then
  begin
    DefNBPath := GetPathToDefNB;

    // Сохраняем настройки //06.04.2009
    // Сохранить в реестр
    //12.05.2009 SaveBoolToRegistry(pnShowLicenceType, Not Params.NoAsk);
    {$IF Defined(FLASH_SCS)}
      GSCSIni.NB.LicenseTypeInt := Ord(Params.LicenceType);
      GSCSIni.NB.ServerName := Params.ServerName;
      GSCSIni.NB.LocalPathToNB := Params.LocalPathToNB;
      WriteNBIni(GSCSIni.NB);
    {$ELSE}
      SaveIntToRegistry(pnLicenseTypeNB, Ord(Params.LicenceType));
      SaveStrToRegistry(pnServerNameNB, Params.ServerName);
      SaveStrToRegistry(pnLocalPathToNB, Params.LocalPathToNB);
    {$IFEND}

    if AnsiUpperCaseFileName(Params.PathToNB) = AnsiUpperCaseFileName(DefNBPath) then
      Params.PathToNB := '';
    GSCSIni.NB.Common.DBPath := Params.PathToNB;
    WriteNBIni(GSCSIni.NB);

    // Проверяем можно ли подключиться к базе, если надо то обновляем
    ModalResult := mrNone;
    if ConnectToNBByParams(Params, FTestConnect, true) then
      ModalResult := mrOk
    else
    begin
    {$IF Defined(FLASH_SCS)}
      if (Params.LicenceType = ltMultiUser) then
      begin
        Params.PathToNB := '';
        Params.LocalPathToNB := '';
        Params.LicenceType := ltLocal;
        Params.ServerName := '';
        GSCSIni.NB.LocalPathToNB := '';
        GSCSIni.NB.ServerName := '';
        GSCSIni.NB.LicenseTypeInt := 1;
        GSCSIni.NB.SaveConnParams := False;
        if ConnectToNBByParams(Params, FTestConnect, true) then
          ModalResult := mrOk
      end;
    {$IFEND}
    end;
  end;
end;

procedure TF_ProtectionBaseParams.btBrowseServerClick(Sender: TObject);
var
  NewServer: String;
  ServerName, LocalName: String;
begin
  try
    //30.03.2011 NewServer := ShowServerDialog(Handle);
    NewServer := BrowseComputer('', '\\'+edServerName.Text);
    if NewServer <> '' then
    begin
      //NewServer := Copy(NewServer, 3, length(NewServer)-2);
      ExtractServerName(NewServer, ServerName, LocalName);
      if ServerName = '' then
        ServerName := LocalName;
      if ServerName <> edServerName.Text then
      begin
        edServerName.Text := ServerName;
        edLocalPathName.SetFocus;
      end;
    end;
  except
    on E: Exception do AddExceptionToLogEx('TF_ProtectionBaseParams.btBrowseServerClick', E.Message);
  end;
end;

procedure TF_ProtectionBaseParams.btSelRemotePathClick(Sender: TObject);
var
  CurrPath: String;
  CurrFirstDir: String;

  RemotePath: String;
  ServerName: String;
  LocalPath: String;
begin
  try
    CurrPath := '';
    if edServerName.Text <> '' then
    begin
      //CurrPath := '\\'+edServerName.Text + '\' + ExtractFirstDirName(edLocalPathName.Text);
      if AnsiUpperCase(edServerName.Text) <> AnsiUpperCase(snLocalHost) then
      begin
        CurrPath := '\\'+edServerName.Text;
        if edLocalPathName.Text <> '' then
          CurrPath := CurrPath + '\' + ExtractFirstDirName(ExtractFileDir(edLocalPathName.Text));
      end
      else
        CurrPath := ExtractFileDir(edLocalPathName.Text);
    end;
    RemotePath := BrowseDialogRemote(Handle, CurrPath, cConnect_Msg3_1);
    if RemotePath <> '' then
    begin
      ExtractServerName(RemotePath, ServerName, LocalPath);
      if ServerName = '' then
        ServerName := snLocalHost;

      edServerName.Text := ServerName;
      edLocalPathName.Text := LocalPath + fnNB;
    end;
  except
    on E: Exception do AddExceptionToLogEx('TF_ProtectionBaseParams.btSelRemotePathClick', E.Message);
  end;
end;

procedure TF_ProtectionBaseParams.lbConnectParamsClick(Sender: TObject);
begin
  ShowBaseConnectParams(F_NormBase.DM.ConnectParams, F_NormBase.GDBMode, true); 
end;

end.
