unit Common.Ini;

interface

uses
  System.SysUtils, System.IniFiles, Vcl.Forms;

type
  TIniEntries = class
  private
    // [Settings]
    FMemoLogLevel: Integer;
    FMemoLogLevelFromIni: Boolean;
    FFileLogLevel: Integer;
    FFileLogLevelFromIni: Boolean;
    FLogFileNum: Integer;
    FLogFileNumFromIni: Boolean;
    FWebClientVersion: string;
    FWebClientVersionFromIni: Boolean;

    // [Database]
    FDatabaseServer: string;
    FDatabaseServerFromIni: Boolean;
    FDatabasePort: Integer;
    FDatabasePortFromIni: Boolean;
    FDatabaseName: string;
    FDatabaseNameFromIni: Boolean;
    FDatabaseUsername: string;
    FDatabaseUsernameFromIni: Boolean;
    FDatabasePassword: string;
    FDatabasePasswordFromIni: Boolean;

  public
    constructor Create;

    // [Settings]
    property MemoLogLevel: Integer read FMemoLogLevel;
    property MemoLogLevelFromIni: Boolean read FMemoLogLevelFromIni;
    property FileLogLevel: Integer read FFileLogLevel;
    property FileLogLevelFromIni: Boolean read FFileLogLevelFromIni;
    property LogFileNum: Integer read FLogFileNum;
    property LogFileNumFromIni: Boolean read FLogFileNumFromIni;

    property WebClientVersion: string read FWebClientVersion;
    property WebClientVersionFromIni: Boolean read FWebClientVersionFromIni;

    // [Database]
    property DatabaseServer: string read FDatabaseServer;
    property DatabaseServerFromIni: Boolean read FDatabaseServerFromIni;
    property DatabasePort: Integer read FDatabasePort;
    property DatabasePortFromIni: Boolean read FDatabasePortFromIni;
    property DatabaseName: string read FDatabaseName;
    property DatabaseNameFromIni: Boolean read FDatabaseNameFromIni;
    property DatabaseUsername: string read FDatabaseUsername;
    property DatabaseUsernameFromIni: Boolean read FDatabaseUsernameFromIni;
    property DatabasePassword: string read FDatabasePassword;
    property DatabasePasswordFromIni: Boolean read FDatabasePasswordFromIni;

  end;

procedure LoadIniEntries;

var
  IniEntries: TIniEntries;

implementation

procedure LoadIniEntries;
begin
  if Assigned(IniEntries) then
    IniEntries.Free;
  IniEntries := TIniEntries.Create;
end;

{ TIniEntries }

constructor TIniEntries.Create;
var
  iniFile: TIniFile;
begin
  iniFile := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));
  try
    // [Settings]
    FMemoLogLevel := iniFile.ReadInteger('Settings', 'MemoLogLevel', 3);
    FMemoLogLevelFromIni := iniFile.ValueExists('Settings', 'MemoLogLevel');

    FFileLogLevel := iniFile.ReadInteger('Settings', 'FileLogLevel', 3);
    FFileLogLevelFromIni := iniFile.ValueExists('Settings', 'FileLogLevel');

    FLogFileNum := iniFile.ReadInteger('Settings', 'LogFileNum', 0);
    FLogFileNumFromIni := iniFile.ValueExists('Settings', 'LogFileNum');

    FWebClientVersion := iniFile.ReadString('Settings', 'webClientVersion', '');
    FWebClientVersionFromIni := iniFile.ValueExists('Settings', 'webClientVersion');

    // [Database]
    FDatabaseServer := iniFile.ReadString('Database', 'Server', '192.168.74.10');
    FDatabaseServerFromIni := iniFile.ValueExists('Database', 'Server');

    FDatabasePort := iniFile.ReadInteger('Database', 'Port', 5433);
    FDatabasePortFromIni := iniFile.ValueExists('Database', 'Port');

    FDatabaseName := iniFile.ReadString('Database', 'Database', 'lems_wcso');
    FDatabaseNameFromIni := iniFile.ValueExists('Database', 'Database');

    FDatabaseUsername := iniFile.ReadString('Database', 'Username', 'postgres');
    FDatabaseUsernameFromIni := iniFile.ValueExists('Database', 'Username');

    FDatabasePassword := iniFile.ReadString('Database', 'Password', '');
    FDatabasePasswordFromIni := iniFile.ValueExists('Database', 'Password');

  finally
    iniFile.Free;
  end;
end;

end.
