unit U_BaseSettings;

interface

Uses SysUtils, Classes, Windows, Forms, Graphics, IniFiles, U_BaseCommon, U_ProtectionCommon;


const
{$if Defined(ES_GRAPH_SC)}
  fnSCSIniFile = 'sc_graph.ini';
{$ELSE}
  fnSCSIniFile = 'Scs.ini';
{$IFEND}
  fnUpdateIni = 'Update.ini';

const DefNBPath = 'NormBase\NB.dat';
const DefPMPath = 'Data\PM.dat';
      bnNB = 'NB';
      bnPM = 'PM';

const ComponPictFileName = 'ComponPicture.jpg';

//*** Sections
const scNormBase = 'NormBase';
      scProjMan = 'ProjectManager';
      scColorObjects = 'ColorObjects';
      scControls = 'Controls';
      scUpdateParams = 'UPDATEPARAMS';

      scEnvironmentDir = 'EnvironmentDir';

      // Для файла конфигурации групп компонентов
      scGeneral = 'General';
      scCompTypeGroups = 'CompTypeGroups';

      scElectrical = 'Electrical'; // Tolik 03/03/2021 --
      scReservFtp = 'ReservFtp'; // Tolik 02/01/2025 -- для резервного копирования на ФТП
      idtSelectedCompType = 'SelectedCompType';
      //idtGroupCondition = 'GroupCondition';


//*** Idents
const idtPath = 'Path';
      idtPnAdditionRestored = 'PnAdditionRestored';
      idtColorZeroPriceComponent = 'ColorZeroPriceComponent';
      idtControlJoinByNetType = 'ControlJoinByNetType';
      idtComplectControlByProducer = 'ComplectControlByProducer';
      idtControlJoinByProperties = 'ControlJoinByProperties';
      idtControlComplectByProperties = 'ControlComplectByProperties';
      idtDefaultNoLineCompon = 'DefaultNoLineCompon';
      idtDefaultLineCompon = 'DefaultLineCompon';
      idtDefaultCorkCompon = 'DefaultCorkCompon';
      idtDefaultTjoinCompon = 'DefaultTjoinCompon';
      idtNDS = 'NDS';
      idtDisableEdit = 'Disable Edit';
      idtFilterType = 'Filter Type';
      //idtIsObjectIndexing = 'IsObjectIndexing';
      idtIsAdministration = 'IsAdministration';
      idtIsUseFilter = 'IsUseFilter';
      //idtReserveLength = 'ReserveLength';
      //idtAccountReserveLength = 'AccountReserveLength';
      //idtAutoInsertingCompons = 'AutoInsertingCompons';
      idtDefNewItemType = 'DefNewItemType';
      idtDefCheckState = 'DefCheckState';
      idtRemindToBackUpBase = 'RemindToBackUpBase';
      idtRemindToBackUpBaseTime = 'RemindToBackUpBaseTime';
      idtRepDesignLanguageFile = 'RepDesignLanguageFile';
      idtRepPricePrecision = 'RepPricePrecision';
      idtRepKolvoPrecision = 'RepKolvoPrecision';
      idtSavedList = 'SavedList';
      idtSavedProject = 'SavedProject';
      idtLastNBDir = 'LastNBDir';
      idtNBLineComponGUIDForConfigurator = 'NBLineComponGUIDForConfigurator';
      idtUpdatePath = 'UpdatePath';
      idtCurrencyConvertionNB2PM = 'CurrencyConvertionNB2PM';

      idtFSCSMain_IsPanelExpertMode = 'F_SCSMain_IsPanelExpertMode';
      

      idtColorResourceActive = 'ColorResourceActive';
      idtColorResourceBack = 'ColorResourceBack';
      idtColorResourceFont = 'ColorResourceFont';
      idtColorResourceComponActive = 'ColorResourceComponActive';
      idtColorResourceComponBack = 'ColorResourceComponBack';
      idtColorResourceComponFont = 'ColorResourceComponFont';
      idtColorNormActive = 'ColorNormActive';
      idtColorNormBack = 'ColorNormBack';
      idtColorNormFont = 'ColorNormFont';
      idtColorTotalKolvoActive = 'ColorTotalKolvoActive';
      idtColorTotalKolvoBack = 'ColorTotalKolvoBack';
      idtColorTotalKolvoFont = 'ColorTotalKolvoFont';

      idtLastpcObjectsTabIndex = 'LastpcObjectsTabIndex';
      idtStayOnTopPCObjects = 'StayOnTopPCObjects';
      idtLiteVersion = 'LiteVersion';
      idtUseLiteFunctional = 'UseLiteFunctional';
      idtAllowConvertInterfToUniversal = 'AllowConvertInterfToUniversal';
      idtUseVerticalTraces = 'UseVerticalTraces';
      idtDropObjByOneClick = 'DropObjByOneClick'; // Tolik 01/08/2019 --
      idtStoreLastPaths = 'StoreLastPaths'; // Tolik 01/08/2019 --
      idtAutoScaleRasterImages = 'AutoScaleRasterImages'; // Tolik 09/08/219 --
      idtComponDrawMode = 'ComponDrawMode';
      idtComponDrawBasement = 'ComponDrawBasement';
      idtSendInfo = 'SendInfo';
      idtTraceElectrical = 'TraceElectrical'; // Tolik 03/03/2021 --
      idtShowAutoCreatedGuides = 'ShowAutoCreatedGuides';
      idtAllowDropCableToRoute = 'AllowDropCableToRoute'; // Tolik 28/05/2021 --
      idtAutoRouteCableAfterTraceCreation = 'AutoRouteCableAfterTraceCreation'; // Tolik 09/08/2021 --
      idtAutoAddNetworkEquipment = 'AutoAddNetworkEquipment'; // Tolik 26/03/2024
      idtConnectString = 'ConnectString'; // Tolik 02/01/2025 --

      //idtDirProjs = 'DirProjs';
      //idtDirCompons = 'DirCompons';
      //idtDirOtherSettings = 'DirOtherSettings';
      //idtDirFilters = 'DirFilters';
      //idtDirBackgroundLayers = 'DirBackglayers';
      //idtDirArchPlans = 'DirArchPlans';
      //idtDirDXF = 'DirDXF';

 {
type TListSettingRecord = record
          HeightRoom: Double;
          HeightCeiling: Double; //*** для потолка
          HeightSocket: Double;  //*** для розеток
          HeightCorob: Double;   //*** для коробов

          LengthKoef: Double;
          PortReserv: Double;
          MultiportReserv: Double;

          ShowObjectType: TShowType;  //*** отображать полное или краткое название
          ShowObjectMarking: Boolean;
          GroupListObjectsByType: Boolean;
     end; }


function GetIniPath: String;
function ReadAllFromIni: TSCSIni;
function ReadNBIni: TNBIni;
function ReadPMIni: TPMIni;
function ReadColors: TColorObjectsIni;
function ReadControls: TControlsIni;
function ReadEnvironments: TEnvironmentsIni;
function ReadEnvironmentDir(ADirType: Integer; const ADefDir: string): string;
procedure WriteNBIni(ANBIni: TNBIni);
procedure WritePMIni(APMIni: TPMIni);
procedure WriteControls(AControls: TControlsIni);
procedure WriteColors(var AIni: TSCSIni);
procedure WriteEnvironmentDir(ADirType: Integer; const ADir: string);

procedure ReadIniSectionKeys(const AFileName, ASection: string; ADestKeys: TStrings);
function ReadSetting(AFileName: String; ADataType: Integer; ASection, AIdent: String; ADefault: Variant): Variant;
function ReadSettingMemIni(const AFileName: String; ADataType: Integer;
  const ASection, AIdent: String; ADefault: Variant): Variant;
procedure WriteSetting(AFileName: String; ADataType: Integer;
            ASection, AIdent: String; AValue: Variant; ADirPath: String = '');
procedure WriteSettingMemIni(const AFileName: String; ADataType: Integer;
            const ASection, AIdent: String; AValue: Variant);



function ReadDBPath(AGDBMode: TDBKind): String;
function ReadPnAdditionRestored(AGDBMode: TDBKind): Boolean;
function ReadLastNBDir: Integer;
function ReadDefaultIDCompon(AIsLine: Integer): Integer;
function ReadDefaultIDCorkCompon: Integer;
function ReadDefaultIDTjoinCompon: Integer;
function ReadNDS: Double;
function ReadNBDisabeEdit(AGDBMode: TDBKind): Boolean;
//function ReadIsObjectIndexing: Boolean;
function ReadIsAdministration: Boolean;
//function ReadReserveLength: Double;
//function ReadAccountReserveLength: Boolean;
//function ReadAutoInsertingCompons: Boolean;


function ReadDefNewItemType: Integer;
function ReadDefCheckState: Boolean;
function ReadSavedIDList: Integer;
function ReadSavedIDProject: Integer;

function ReadUpdatePath: String;


procedure WritePnAdditionRestored(AGDBMode: TDBKind; ARestored: Boolean);
procedure WriteLastNBDir(AGDBMode: TDBKind; ALastNBDir: Integer);
procedure WriteUpdatePath(AUpdatePath: String; ADirPath: String = '');


{function SetDBPath(AGDBMode: TDBKind; ADBPath: string);
function SetDefaultIDCompon(AIsLine: Integer; ADefIDCompon: Integer);
function SetNDS(ANDS: Double);
function SetNBDisabeEdit(AGDBMode: TDBKind; ANBDisabeEdit: Boolean);
function SetIsObjectIndexing(AIsObjectIndexing: Boolean);
function SetIsAdministration(AIsAdministration: Boolean);

function SetDefNewItemType(ADefNewItemType: Integer);
function SetDefCheckState(ADefCheckState: Boolean);
function SetSavedIDList(ASavedIDList: Integer);
}

implementation
uses U_Main, Unit_DM_SCS, FIBQuery, U_BaseOptions;

function GetIniPath: String;
begin
  {$if Defined(ES_GRAPH_SC)}
    Result := ExeDir + '\' + fnSCSIniFile;
  {$else}
    Result := ExtractFilePath(paramstr(0)) + fnSCSIniFile;
  {$ifend}
end;

function ReadAllFromIni: TSCSIni;
begin
  ZeroMemory(@Result, SizeOf(TSCSIni));
  Result.NB := ReadNBIni;
  Result.PM := ReadPMIni;
  Result.Controls := ReadControls; 
end;

function ReadNBIni: TNBIni;
var
  Ini: TIniFile;
begin
  Result.AutoShowParams := true;
  Result.SaveConnParams := true;
  Result.LicenseTypeInt := 1;
  Result.ServerName := '';
  Result.LocalPathToNB := '';
  Result.Common.DBPath := '';
  Result.Common.PnAdditionRestored := true;
  Result.ColorZeroPriceComponent := clRed;
  Result.ControlJoinByNetType := true;
  Result.ComplectControlByProducer := false;
  Result.ControlJoinByProperties := true;
  Result.ControlComplectByProperties := true;
  Result.DisableEdit := false;
  Result.IDLastNBDir := -1;
  Result.IsAdministration := false;
  Result.FilterType := fltNone;
  Result.IsUseFilter := false;
  Result.RemindToBackUpBase := true;
  {$IF Defined(ES_GRAPH_SC)}
    Result.RemindToBackUpBase := false;
    Result.RemindToBackUpBaseTime := 65;
  {$ELSE}
    Result.RemindToBackUpBaseTime := 15;
  {$IFEND}
  Result.CurrencyConvertionNB2PM := cckByTarget;
  try
    if FileExists(GetIniPath) then
    begin
      Ini := TIniFile.Create(GetIniPath);

      Result.AutoShowParams := ini.ReadBool(scNormBase, pnShowLicenceType, Result.AutoShowParams);
      Result.LicenseTypeInt := ini.ReadInteger(scNormBase, pnLicenseTypeNB, Result.LicenseTypeInt);

      Result.ServerName := Ini.ReadString(scNormBase, pnServerNameNB, Result.ServerName);;
      Result.LocalPathToNB := Ini.ReadString(scNormBase, pnLocalPathToNB, Result.LocalPathToNB);;

      Result.Common.DBPath := Ini.ReadString(scNormBase, idtPath, Result.Common.DBPath);
      Result.Common.PnAdditionRestored := Ini.ReadBool(scNormBase, idtPnAdditionRestored, Result.Common.PnAdditionRestored);
      Result.ColorZeroPriceComponent := Ini.ReadInteger(scNormBase, idtColorZeroPriceComponent, Result.ColorZeroPriceComponent);

      Result.ControlJoinByNetType := ini.ReadBool(scNormBase, idtControlJoinByNetType, Result.ControlJoinByNetType);
      Result.ComplectControlByProducer := ini.ReadBool(scNormBase, idtComplectControlByProducer, Result.ComplectControlByProducer);
      Result.ControlJoinByProperties := ini.ReadBool(scNormBase, idtControlJoinByProperties, Result.ControlJoinByProperties);
      Result.ControlComplectByProperties := ini.ReadBool(scNormBase, idtControlComplectByProperties, Result.ControlComplectByProperties);

      Result.DisableEdit := ini.ReadBool(scNormBase, idtDisableEdit, Result.DisableEdit);
      Result.IDLastNBDir := ini.ReadInteger(scNormBase, idtLastNBDir, Result.IDLastNBDir);
      Result.IsAdministration := ini.ReadBool(scNormBase, idtIsAdministration, Result.IsAdministration);

      Result.FilterType := TFilterType(ini.ReadInteger(scNormBase, idtFilterType, Ord(Result.FilterType)));
      Result.IsUseFilter := ini.ReadBool(scNormBase, idtIsUseFilter, Result.IsUseFilter);

      Result.RemindToBackUpBase := ini.ReadBool(scNormBase, idtRemindToBackUpBase, Result.RemindToBackUpBase);
      Result.RemindToBackUpBaseTime := ini.ReadInteger(scNormBase, idtRemindToBackUpBaseTime, Result.RemindToBackUpBaseTime);

      Result.CurrencyConvertionNB2PM := ini.ReadInteger(scNormBase, idtCurrencyConvertionNB2PM, Result.CurrencyConvertionNB2PM);

      Ini.Free;
    end;
  except
    on E: Exception do AddExceptionToLog(': '+E.Message);
  end;

  {$IF Defined(TRIAL_SCS)}
    Result.RemindToBackUpBase := False;
  {$IFEND}
end;

function ReadPMIni: TPMIni;
var
  Ini: TIniFile;
begin
  Result.Common.DBPath := '';
  Result.SaveConnParams := true;
  Result.Common.PnAdditionRestored := true;
  Result.IDLastProject := 0;
  try
    if FileExists(GetIniPath) then
    begin
      Ini := TIniFile.Create(GetIniPath);

      Result.Common.DBPath := Ini.ReadString(scProjMan, idtPath, Result.Common.DBPath);
      Result.Common.PnAdditionRestored := Ini.ReadBool(scProjMan, idtPnAdditionRestored, Result.Common.PnAdditionRestored);
      Result.IDLastProject := Ini.ReadInteger(scProjMan, idtSavedProject, Result.IDLastProject);
      Result.RepDesignLanguageFile := Ini.ReadString(scProjMan, idtRepDesignLanguageFile, fnRepDesignLangRus);
      Result.RepPricePrecision := Ini.ReadInteger(scProjMan, idtRepPricePrecision, 2);
      Result.RepKolvoPrecision := Ini.ReadInteger(scProjMan, idtRepKolvoPrecision, 2);
      Result.NBLineComponGUIDForConfigurator := Ini.ReadString(scProjMan, idtNBLineComponGUIDForConfigurator, '');

      Ini.Free;
    end;
  except
    on E: Exception do AddExceptionToLog(': '+E.Message);
  end;
end;

function ReadColors: TColorObjectsIni;
var
  Ini: TIniFile;
begin
  Result.Resource.Active := true;
  Result.Resource.Back := clYellow;
  Result.Resource.Font := clBlack;
  Result.ResourceCompon.Active := true;
  Result.ResourceCompon.Back := $004080FF;
  Result.ResourceCompon.Font := clBlack;
  Result.Norm.Active := false;
  Result.Norm.Back := clLime;
  Result.Norm.Font := clBlack;
  Result.TotalKolvo.Active := true;
  Result.TotalKolvo.Back := clRed;
  Result.TotalKolvo.Font := clBlack;
  try
    if FileExists(GetIniPath) then
    begin
      Ini := TIniFile.Create(GetIniPath);

      Result.Resource.Active := Ini.ReadBool(scColorObjects, idtColorResourceActive, Result.Resource.Active);
      Result.Resource.Back := Ini.ReadInteger(scColorObjects, idtColorResourceBack, Result.Resource.Back);
      Result.Resource.Font := Ini.ReadInteger(scColorObjects, idtColorResourceFont, Result.Resource.Font);
      Result.ResourceCompon.Active := Ini.ReadBool(scColorObjects, idtColorResourceComponActive, Result.ResourceCompon.Active);
      Result.ResourceCompon.Back := Ini.ReadInteger(scColorObjects, idtColorResourceComponBack, Result.ResourceCompon.Back);
      Result.ResourceCompon.Font := Ini.ReadInteger(scColorObjects, idtColorResourceComponFont, Result.ResourceCompon.Font);
      Result.Norm.Active := Ini.ReadBool(scColorObjects, idtColorNormActive, Result.Norm.Active);
      Result.Norm.Back := Ini.ReadInteger(scColorObjects, idtColorNormBack, Result.Norm.Back);
      Result.Norm.Font := Ini.ReadInteger(scColorObjects, idtColorNormFont, Result.Norm.Font);

      Result.TotalKolvo.Active := Ini.ReadBool(scColorObjects, idtColorTotalKolvoActive, Result.TotalKolvo.Active);
      Result.TotalKolvo.Back := Ini.ReadInteger(scColorObjects, idtColorTotalKolvoBack, Result.TotalKolvo.Back);
      Result.TotalKolvo.Font := Ini.ReadInteger(scColorObjects, idtColorTotalKolvoFont, Result.Norm.Font);

      Ini.Free;
    end;
  except
    on E: Exception do AddExceptionToLogEx('ReadColors', E.Message);
  end;
end;

function ReadControls: TControlsIni;
var
  Ini: TIniFile;
begin
  Result.F_SCSMain_IsPanelExpertMode := false;
  try
    if FileExists(GetIniPath) then
    begin
      Ini := TIniFile.Create(GetIniPath);

      Result.F_SCSMain_IsPanelExpertMode := Ini.ReadBool(scControls, idtFSCSMain_IsPanelExpertMode, Result.F_SCSMain_IsPanelExpertMode);

      Ini.Free;
    end;
  except
    on E: Exception do AddExceptionToLogEx('ReadControls', E.Message);
  end;
end;

function ReadEnvironments: TEnvironmentsIni;
begin
  Result.TempDir := ReadEnvironmentDir(dctTemp, '');
end;

function ReadEnvironmentDir(ADirType: Integer; const ADefDir: string): string;
var
  Ini: TIniFile;
begin
  Result := ADefDir;
  try
    if FileExists(GetIniPath) then
    begin
      Ini := TIniFile.Create(GetIniPath);
      Result := Ini.ReadString(scEnvironmentDir, IntToStr(ADirType), ADefDir);
      if Result = '' then
        Result := ADefDir;
      Ini.Free;
    end;
  except
    on E: Exception do AddExceptionToLogEx('ReadEnvironmentDir', E.Message);
  end;
end;

procedure WriteNBIni(ANBIni: TNBIni);
var
  Ini: TIniFile;
begin
  try
    Ini := TIniFile.Create(GetIniPath);

    Ini.WriteString(scNormBase, idtPath, ANBIni.Common.DBPath);

    if (ANBIni.SaveConnParams) then
    begin
      Ini.WriteBool(scNormBase, pnShowLicenceType, ANBIni.AutoShowParams);
      Ini.WriteInteger(scNormBase, pnLicenseTypeNB, ANBIni.LicenseTypeInt);
      Ini.WriteString(scNormBase, pnServerNameNB, ANBIni.ServerName);
      Ini.WriteString(scNormBase, pnLocalPathToNB, ANBIni.LocalPathToNB);
    end;

    Ini.WriteBool(scNormBase, idtPnAdditionRestored, ANBIni.Common.PnAdditionRestored);
    Ini.WriteInteger(scNormBase, idtColorZeroPriceComponent, ANBIni.ColorZeroPriceComponent);

    ini.WriteBool(scNormBase, idtControlJoinByNetType, ANBIni.ControlJoinByNetType);
    ini.WriteBool(scNormBase, idtComplectControlByProducer, ANBIni.ComplectControlByProducer);
    ini.WriteBool(scNormBase, idtControlJoinByProperties , ANBIni.ControlJoinByProperties);
    ini.WriteBool(scNormBase, idtControlComplectByProperties , ANBIni.ControlComplectByProperties);

    ini.WriteBool(scNormBase, idtDisableEdit, ANBIni.DisableEdit);
    ini.WriteBool(scNormBase, idtIsAdministration, ANBIni.IsAdministration);
    ini.WriteInteger(scNormBase, idtLastNBDir, ANBIni.IDLastNBDir);

    ini.WriteInteger(scNormBase, idtFilterType, Ord(ANBIni.FilterType));
    ini.WriteBool(scNormBase, idtIsUseFilter, ANBIni.IsUseFilter);

    ini.WriteBool(scNormBase, idtRemindToBackUpBase, ANBIni.RemindToBackUpBase);
    ini.WriteInteger(scNormBase, idtRemindToBackUpBaseTime, ANBIni.RemindToBackUpBaseTime);
    ini.WriteInteger(scNormBase, idtCurrencyConvertionNB2PM, ANBIni.CurrencyConvertionNB2PM);

    Ini.Free;
  except
    on E: Exception do AddExceptionToLog(': '+E.Message);
  end;
end;

procedure WritePMIni(APMIni: TPMIni);
var
  Ini: TIniFile;
begin
  try
    Ini := TIniFile.Create(GetIniPath);
    if APMIni.SaveConnParams = true then
      Ini.WriteString(scProjMan, idtPath, APMIni.Common.DBPath);
    Ini.WriteBool(scProjMan, idtPnAdditionRestored, APMIni.Common.PnAdditionRestored);
    Ini.WriteInteger(scProjMan, idtSavedProject, APMIni.IDLastProject);
    Ini.WriteString(scProjMan, idtRepDesignLanguageFile, APMIni.RepDesignLanguageFile);
    Ini.WriteInteger(scProjMan, idtRepPricePrecision, APMIni.RepPricePrecision);
    Ini.WriteInteger(scProjMan, idtRepKolvoPrecision, APMIni.RepKolvoPrecision);
    Ini.WriteString(scProjMan, idtNBLineComponGUIDForConfigurator, APMIni.NBLineComponGUIDForConfigurator);
    
    Ini.Free;
  except
    on E: Exception do AddExceptionToLog('WritePMIni: '+E.Message);
  end;
end;

procedure WriteControls(AControls: TControlsIni);
var
  Ini: TIniFile;
begin
  try
    Ini := TIniFile.Create(GetIniPath);

    Ini.WriteBool(scControls, idtFSCSMain_IsPanelExpertMode, AControls.F_SCSMain_IsPanelExpertMode);

    Ini.Free;
  except
    on E: Exception do AddExceptionToLog('WriteControls: '+E.Message);
  end;
end;

procedure WriteColors(var AIni: TSCSIni);
var
  Ini: TIniFile;
begin
  try
    Ini := TIniFile.Create(GetIniPath);

    Ini.WriteBool(scColorObjects, idtColorResourceActive, AIni.Colors.Resource.Active);
    Ini.WriteInteger(scColorObjects, idtColorResourceBack, AIni.Colors.Resource.Back);
    Ini.WriteInteger(scColorObjects, idtColorResourceFont, AIni.Colors.Resource.Font);
    Ini.WriteBool(scColorObjects, idtColorResourceComponActive, AIni.Colors.ResourceCompon.Active);
    Ini.WriteInteger(scColorObjects, idtColorResourceComponBack, AIni.Colors.ResourceCompon.Back);
    Ini.WriteInteger(scColorObjects, idtColorResourceComponFont, AIni.Colors.ResourceCompon.Font);
    Ini.WriteBool(scColorObjects, idtColorNormActive, AIni.Colors.Norm.Active);
    Ini.WriteInteger(scColorObjects, idtColorNormBack, AIni.Colors.Norm.Back);
    Ini.WriteInteger(scColorObjects, idtColorNormFont, AIni.Colors.Norm.Font);

    Ini.WriteBool(scColorObjects, idtColorTotalKolvoActive, AIni.Colors.TotalKolvo.Active);
    Ini.WriteInteger(scColorObjects, idtColorTotalKolvoBack, AIni.Colors.TotalKolvo.Back);
    Ini.WriteInteger(scColorObjects, idtColorTotalKolvoFont, AIni.Colors.TotalKolvo.Font);

    Ini.Free;
  except
    on E: Exception do AddExceptionToLog('WriteControls: '+E.Message);
  end;
end;

procedure WriteEnvironmentDir(ADirType: Integer; const ADir: string);
var
  Ini: TIniFile;
begin
  try
    if FileExists(GetIniPath) then
    begin
      Ini := TIniFile.Create(GetIniPath);
      Ini.WriteString(scEnvironmentDir, IntToStr(ADirType), ADir);
      Ini.Free;
    end;
  except
    on E: Exception do AddExceptionToLogEx('WriteEnvironmentDir', E.Message);
  end;
end;

procedure ReadIniSectionKeys(const AFileName, ASection: string; ADestKeys: TStrings);
var
  Ini: TMemIniFile;
begin
  try
    Ini := TMemIniFile.Create(AFileName);

    Ini.ReadSection(ASection, ADestKeys);

    FreeAndNil(Ini);
  except
    on E: Exception do AddExceptionToLog('ReadSetting: '+E.Message);
  end;
end;

function ReadSetting(AFileName: String; ADataType: Integer; ASection, AIdent: String; ADefault: Variant): Variant;
var
  ini: TIniFile;
begin
  try
    try
      Result := ADefault;
      ini := nil;
      if FileExists(fnSCSIniFile) then
      begin
        {$if Defined(ES_GRAPH_SC)}
          ini := TIniFile.Create(ExeDir + '\' + AFileName);
        {$else}
          ini := TIniFile.Create(extractfilepath(paramstr(0)) + AFileName);
        {$ifend}
        case ADataType of
          dtBoolean:
              Result := ini.ReadBool(ASection, AIdent, ADefault);
          dtFloat:
              Result := ini.ReadFloat(ASection, AIdent, ADefault);
          dtInteger:
              Result := ini.ReadInteger(ASection, AIdent, ADefault);
          dtString:
              Result := ini.ReadString(ASection, AIdent, ADefault);
        end;
      end;
    except
      on E: Exception do AddExceptionToLog('ReadSetting: '+E.Message);
    end;
  finally
    if ini <> nil then
      ini.Free;
  end;
end;

function ReadSettingMemIni(const AFileName: String; ADataType: Integer;
  const ASection, AIdent: String; ADefault: Variant): Variant;
var
  ini: TMemIniFile;
begin
  Result := ADefault;
  try
    try
      ini := TMemIniFile.Create(AFileName);
      case ADataType of
        dtBoolean:
          Result := ini.ReadBool(ASection, AIdent, ADefault);
        dtFloat:
          Result := ini.ReadFloat(ASection, AIdent, ADefault);
        dtInteger:
          Result := ini.ReadInteger(ASection, AIdent, ADefault);
        dtString:
          Result := ini.ReadString(ASection, AIdent, ADefault);
      end;          
    except
      on E: Exception do AddExceptionToLog('ReadSettingMemIni: '+E.Message);
    end;
  finally
    if ini <> nil then
      ini.Free;
  end;
end;

procedure WriteSetting(AFileName: String; ADataType: Integer;
  ASection, AIdent: String; AValue: Variant; ADirPath: String = '');
var
  ini: TIniFile;
  DirPath: String;
begin
  try
    DirPath := ADirPath;
    if DirPath = '' then
      {$if Defined(ES_GRAPH_SC)}
        DirPath := ExeDir + '\';
      {$else}
        DirPath := extractfilepath(paramstr(0));
      {$ifend}

    ini := TIniFile.Create(DirPath + AFileName);
    case ADataType of
      dtBoolean:
        ini.WriteBool(ASection, AIdent, AValue);
      dtFloat:
        ini.WriteFloat(ASection, AIdent, AValue);
      dtInteger:
        ini.WriteInteger(ASection, AIdent, AValue);
      dtString:
        ini.WriteString(ASection, AIdent, AValue);
    end;
    FreeAndNil(ini);
  except
    on E: Exception do AddExceptionToLog('WriteSetting: '+E.Message);
  end;
end;


procedure WriteSettingMemIni(const AFileName: String; ADataType: Integer;
  const ASection, AIdent: String; AValue: Variant);
var
  ini: TMemIniFile;
begin
  try
    ini := TMemIniFile.Create(AFileName);
    case ADataType of
      dtBoolean:
        ini.WriteBool(ASection, AIdent, AValue);
      dtFloat:
        ini.WriteFloat(ASection, AIdent, AValue);
      dtInteger:
        ini.WriteInteger(ASection, AIdent, AValue);
      dtString:
        ini.WriteString(ASection, AIdent, AValue);
    end;
    ini.UpdateFile;
    FreeAndNil(ini);
  except
    on E: Exception do AddExceptionToLog('WriteSettingMemIni: '+E.Message);
  end;
end;

// #############################################################################

function ReadDBPath(AGDBMode: TDBKind): String;
begin
    Result := '';
    case AGDBMode of
      bkNormBase:
          Result := ReadSetting(fnSCSIniFile, dtString, scNormBase, idtPath, '');
      bkProjectManager:
          Result := ReadSetting(fnSCSIniFile, dtString, scProjMan, idtPath, '');
    end;
end;

function ReadPnAdditionRestored(AGDBMode: TDBKind): Boolean;
begin
    Result := true;
    case AGDBMode of
      bkNormBase:
          Result := ReadSetting(fnSCSIniFile, dtBoolean, scNormBase, idtPnAdditionRestored, true);
      bkProjectManager:
          Result := ReadSetting(fnSCSIniFile, dtBoolean, scProjMan, idtPnAdditionRestored, true);
    end;
end;

function ReadLastNBDir: Integer;
begin
    Result := 0;
    Result := ReadSetting(fnSCSIniFile, dtInteger, scNormBase, idtLastNBDir, 0);
end;


function ReadDefaultIDCompon(AIsLine: Integer): Integer;
begin
    Result := 0;
    case AIsLine of
      biFalse:
          Result := ReadSetting(fnSCSIniFile, dtInteger, scNormBase, idtDefaultNoLineCompon, 0);
      biTrue:
          Result := ReadSetting(fnSCSIniFile, dtInteger, scNormBase, idtDefaultLineCompon, 0);
    end;
end;

function ReadDefaultIDCorkCompon: Integer;
begin
    Result := -1;
    Result := ReadSetting(fnSCSIniFile, dtInteger, scNormBase, idtDefaultCorkCompon, 0);
end;

function ReadDefaultIDTjoinCompon: Integer;
begin
    Result := -1;
    Result := ReadSetting(fnSCSIniFile, dtInteger, scNormBase, idtDefaultTjoinCompon, 0);
end;

function ReadNDS: Double;
begin
    Result := 20;
    Result := ReadSetting(fnSCSIniFile, dtFloat, scNormBase, idtNDS, 20);
end;


function ReadNBDisabeEdit(AGDBMode: TDBKind): Boolean;
begin
    Result := false;
    case AGDBMode of
      bkNormBase:
          Result := ReadSetting(fnSCSIniFile, dtBoolean, scNormBase, idtDisableEdit, false);
      bkProjectManager:
          Result := ReadSetting(fnSCSIniFile, dtBoolean, scProjMan, idtDisableEdit, false);
    end;
end;

{
function ReadIsObjectIndexing: Boolean;
begin
    Result := true;
    Result :=  ReadSetting(dtBoolean, scNormBase, idtIsObjectIndexing, true);
end;
}

function ReadIsAdministration: Boolean;
begin
    Result := false;
    Result := ReadSetting(fnSCSIniFile, dtBoolean, scNormBase, idtIsAdministration, false);
end;
{
function ReadReserveLength: Double;
begin
    Result := 0.5;
    Result := ReadSetting(dtFloat, scNormBase, idtReserveLength, 0.5);
end;
}
{
function ReadAccountReserveLength: Boolean;
begin
    Result := false;
    Result := ReadSetting(dtBoolean, scNormBase, idtAccountReserveLength, false);
end;
}


{
function ReadAutoInsertingCompons: Boolean;
begin
    Result := true;
    Result := ReadSetting(dtBoolean, scProjMan, idtAutoInsertingCompons, true);
end;
}

function ReadDefNewItemType: Integer;
begin
    Result := 0;
    Result := ReadSetting(fnSCSIniFile, dtInteger, scProjMan, idtDefNewItemType, 0);
end;



function ReadDefCheckState: Boolean;
begin
    Result := true;
    Result := ReadSetting(fnSCSIniFile, dtBoolean, scProjMan, idtDefCheckState, true);
end;



function ReadSavedIDList: Integer;
begin
    Result := 0;
    Result := ReadSetting(fnSCSIniFile, dtInteger, scProjMan, idtSavedList, 0);
end;

function ReadSavedIDProject: Integer;
begin
    Result := 0;
    Result := ReadSetting(fnSCSIniFile, dtInteger, scProjMan, idtSavedProject, 0);
end;


function ReadUpdatePath: String;
begin
  Result := ReadSetting(fnUpdateIni, dtString, scUpdateParams, idtUpdatePath, '');
  if Result <> '' then
    {$if Defined(ES_GRAPH_SC)}
      Result := ExeDir + '\' + Result;
    {$else}
      Result := extractfilepath(paramstr(0)) + Result;
    {$ifend}
end;

procedure WritePnAdditionRestored(AGDBMode: TDBKind; ARestored: Boolean);
begin
    case AGDBMode of
      bkNormBase:
          WriteSetting(fnSCSIniFile, dtBoolean, scNormBase, idtPnAdditionRestored, ARestored);
      bkProjectManager:
          WriteSetting(fnSCSIniFile, dtBoolean, scProjMan, idtPnAdditionRestored, ARestored);
    end;
end;

procedure WriteLastNBDir(AGDBMode: TDBKind; ALastNBDir: Integer);
begin
  if AGDBMode = bkNormBase then
    WriteSetting(fnSCSIniFile, dtBoolean, scNormBase, idtLastNBDir, ALastNBDir);
end;

procedure WriteUpdatePath(AUpdatePath: String; ADirPath: String = '');
begin
  WriteSetting(fnUpdateIni, dtString, scUpdateParams, idtUpdatePath, AUpdatePath, ADirPath);
end;

end.
