unit ConfigDoc;

interface

uses
  Classes, IniFiles, UtilMethods;

type
  TConfigInfo = class
  private
    FIniFile: TIniFile;
    FUnits: TStrings;

    FStandardXmLib: string;
    FStandardGclLib: string;

    FOutputPath: string;
    FAuditPath: string;

    FCompanyName: string;
    FAutoSave: Boolean;
    FAutoSaveInterval: Integer;
    FOverRangeType: Integer;
    FExcelWithMis: Boolean;
    FIsLog: Boolean;
    FBatchInsertFrmHeight: Integer;
    FBatchInsertFrmWidth: Integer;

    procedure LoadSectionOfCustomize;
    procedure LoadSectionOfUnitList;
    procedure LoadSectionOfStdFile;
    procedure LoadSectionOfPath;
    procedure LoadSectionOfOptions;
    procedure LoadIniFile;

    procedure SaveIniFile;
    procedure SaveSectionOfStdFile;
    procedure SaveSectionOfOptions;
    procedure SaveSectionOfCustomize;

    function GetStandardGclLib: string;
    function GetStandardXmLib: string;
    procedure SetStandardGclLib(const Value: string);
    procedure SetStandardXmLib(const Value: string);
    function GetOutputPath: string;
    function GetAuditPath: string;
  public
    constructor Create(const AIniFile: string);
    destructor Destroy; override;

    property Units: TStrings read FUnits;

    property StandardXmLib: string read GetStandardXmLib write SetStandardXmLib;
    property StandardGclLib: string read GetStandardGclLib write SetStandardGclLib;

    property IsLog: Boolean read FIsLog;

    // Path
    property AuditPath: string read GetAuditPath;
    property OutputPath: string read GetOutputPath;

    // Options
    property CompanyName: string read FCompanyName write FCompanyName;
    property AutoSave: Boolean read FAutoSave write FAutoSave;
    property AutoSaveInterval: Integer read FAutoSaveInterval write FAutoSaveInterval;
    property OverRangeType: Integer read FOverRangeType write FOverRangeType;
    property ExcelWithMis: Boolean read FExcelWithMis write FExcelWithMis;

    // Customize
    property BatchInsertFrmHeight: Integer read FBatchInsertFrmHeight write FBatchInsertFrmHeight;
    property BatchInsertFrmWidth: Integer read FBatchInsertFrmWidth write FBatchInsertFrmWidth;
  end;

implementation

{ TConfigInfo }

constructor TConfigInfo.Create(const AIniFile: string);
begin
  FIniFile := TIniFile.Create(AIniFile);
  FUnits := TStringList.Create;
  LoadIniFile;
end;

destructor TConfigInfo.Destroy;
begin
  SaveIniFile;
  FUnits.Free;
  FIniFile.Free;
  inherited;
end;

function TConfigInfo.GetAuditPath: string;
begin
  Result := GetAppFilePath + FAuditPath;
end;

function TConfigInfo.GetOutputPath: string;
begin
  Result := GetAppFilePath + FOutputPath;
end;

function TConfigInfo.GetStandardGclLib: string;
begin
  Result := GetAppFilePath + '标准清单\工程量清单\' + FStandardGclLib;
end;

function TConfigInfo.GetStandardXmLib: string;
begin
  Result := GetAppFilePath + '标准清单\项目节清单\' + FStandardXmLib;
end;

procedure TConfigInfo.LoadIniFile;
begin
  LoadSectionOfUnitList;
  LoadSectionOfStdFile;
  LoadSectionOfPath;
  LoadSectionOfOptions;
  LoadSectionOfCustomize;

  FIsLog := FIniFile.ReadBool('Other', 'IsLog', True);
end;

procedure TConfigInfo.LoadSectionOfCustomize;
begin
  FBatchInsertFrmHeight := FIniFile.ReadInteger('Customize', 'BatchInsertFrmHeight', 382);
  FBatchInsertFrmWidth := FIniFile.ReadInteger('Customize', 'BatchInsertFrmWidth', 779);
end;

procedure TConfigInfo.LoadSectionOfOptions;
begin
  FCompanyName := FIniFile.ReadString('Options', 'CompanyName', '');
  FAutoSave := FIniFile.ReadBool('Options', 'AutoSave', True);
  FAutoSaveInterval := FIniFile.ReadInteger('Options', 'AutoSaveInterval', 15);
  FOverRangeType := FIniFile.ReadInteger('Options', 'OverRangeType', 0);
  FExcelWithMis := FIniFile.ReadBool('Options', 'ExcelWithMis', False);
end;

procedure TConfigInfo.LoadSectionOfPath;
begin
  FOutputPath := FIniFile.ReadString('Paths', 'Output', '导出文件\');
  FAuditPath := FIniFile.ReadString('Paths', 'AuditPath', '上报批复文件\');
end;

procedure TConfigInfo.LoadSectionOfStdFile;
begin
  FStandardXmLib := FIniFile.ReadString('StdFiles', 'XmLib', '');
  FStandardGclLib := FIniFile.ReadString('StdFiles', 'GclLib', '');
end;

procedure TConfigInfo.LoadSectionOfUnitList;
var
  iIndex: Integer;
begin
  if FIniFile.SectionExists('UnitList') then
  begin
    FIniFile.ReadSection('UnitList', FUnits);
    for iIndex := 0 to FUnits.Count - 1 do
      FUnits.Strings[iIndex] := FIniFile.ReadString('UnitList', FUnits.Strings[iIndex], '');
  end;
end;

procedure TConfigInfo.SaveIniFile;
begin
  SaveSectionOfStdFile;
  SaveSectionOfOptions;
  SaveSectionOfCustomize;
end;

procedure TConfigInfo.SaveSectionOfCustomize;
begin
  FIniFile.WriteInteger('Customize', 'BatchInsertFrmHeight', FBatchInsertFrmHeight);
  FIniFile.WriteInteger('Customize', 'BatchInsertFrmWidth', FBatchInsertFrmWidth);
end;

procedure TConfigInfo.SaveSectionOfOptions;
begin
  FIniFile.WriteString('Options', 'CompanyName', FCompanyName);
  FIniFile.WriteBool('Options', 'AutoSave', FAutoSave);
  FIniFile.WriteInteger('Options', 'AutoSaveInterval', FAutoSaveInterval);
  FIniFile.WriteInteger('Options', 'OverRangeType', FOverRangeType);
  FIniFile.WriteBool('Options', 'ExcelWithMis', FExcelWithMis);
end;

procedure TConfigInfo.SaveSectionOfStdFile;
begin
  FIniFile.WriteString('StdFiles', 'XmLib', FStandardXmLib);
  FIniFile.WriteString('StdFiles', 'GclLib', FStandardGclLib);
end;

procedure TConfigInfo.SetStandardGclLib(const Value: string);
begin
  FStandardGclLib := Value;
  SaveIniFile;
end;

procedure TConfigInfo.SetStandardXmLib(const Value: string);
begin
  FStandardXmLib := Value;
  SaveIniFile;
end;

end.
