{*******************************************************} { } { ManagerConfig } { } { Copyright (C) 2011 Sunk } { } {*******************************************************} unit ManagerConfig; interface uses Define, Winapi.Windows, System.SysUtils, System.Classes, Vcl.ComCtrls, System.Generics.Collections, superobject; const INI_CONFIG = FAMILY_NAME+'.cfg'; type TCloseBtnEvent = (cbeQuestion, cbeHide, cbeExit); TFormSize = record bDo : Boolean; nWidth, nHeight : Integer; end; TFormPosition = record bDo : Boolean; nTop, nLeft : Integer; end; PSunkHotkey = ^TTgHotkey; TTgHotkey = record ShortCut: TShortCut; Modifiers: THKModifiers; Description: String; end; TNormalConfig = record SaveLog, FormInitHide : Boolean; FormSize : TFormSize; FormPosition : TFormPosition; CloseBtnEvent : TCloseBtnEvent; StartupExecute: Boolean; AlphaBlend : Boolean; AlphaBlendVal : WORD; end; TPathConfig = record LogPath, DataPath: String; end; TManagerConfig = class(TObject) private lstHK_: TList; PathConfig_: TPathConfig; procedure SetCloseBtnEvent(Value: TCloseBtnEvent); procedure SetForInitHide(Value: Boolean); procedure SetFormSizeSave(Value: Boolean); procedure SetFormSizeWidth(Value: Integer); procedure SetFormSizeHeight(Value: Integer); procedure SetFormPosSave(Value: Boolean); procedure SetFormPosLeft(Value: Integer); procedure SetFormPosTop(Value: Integer); procedure SetStartupExecute(Value: Boolean); procedure SetLogPath(const sLogPath: String); function GetLogPath: String; procedure SetDataPath(const sDataPath: String); function GetDatapath: String; public NormalConfig: TNormalConfig; // ±×³É ¹ÛÀ¸·Î »«´Ù 2015-12-17 Constructor Create; Destructor Destroy; override; procedure SetDefault; procedure SaveConfig; procedure LoadConfig; property CloseBtnEvent: TCloseBtnEvent read NormalConfig.CloseBtnEvent write SetCloseBtnEvent; property FormInitHide: Boolean read NormalConfig.FormInitHide write SetForInitHide; property FormSizeSave: Boolean read NormalConfig.FormSize.bDo write SetFormSizeSave; property FormWidth: Integer read NormalConfig.FormSize.nWidth write SetFormSizeWidth; property FormHeight: Integer read NormalConfig.FormSize.nHeight write SetFormSizeHeight; property FormPosSave: Boolean read NormalConfig.FormPosition.bDo write SetFormPosSave; property FormLeft: Integer read NormalConfig.FormPosition.nLeft write SetFormPosLeft; property FormTop: Integer read NormalConfig.FormPosition.nTop write SetFormPosTop; property StartupExecute: Boolean read NormalConfig.StartupExecute write SetStartupExecute; property LogPath: String read GetLogPath write SetLogPath; property DataPath: String read GetDatapath write SetDataPath; end; var gCfg: TManagerConfig = nil; implementation uses Tocsg.Safe, Tocsg.Path, Tocsg.JSON; { TManagerConfig } Constructor TManagerConfig.Create; begin Inherited Create; ASSERT(gCfg = nil); gCfg := self; SetDefault; end; Destructor TManagerConfig.Destroy; begin gCfg := nil; Inherited; end; procedure TManagerConfig.SetCloseBtnEvent(Value: TCloseBtnEvent); begin if NormalConfig.CloseBtnEvent <> Value then NormalConfig.CloseBtnEvent := Value; end; procedure TManagerConfig.SetForInitHide(Value: Boolean); begin if NormalConfig.FormInitHide <> Value then NormalConfig.FormInitHide := Value; end; procedure TManagerConfig.SetFormSizeSave(Value: Boolean); begin if NormalConfig.FormSize.bDo <> Value then NormalConfig.FormSize.bDo := Value; end; procedure TManagerConfig.SetFormSizeWidth(Value: Integer); begin if NormalConfig.FormSize.nWidth <> Value then NormalConfig.FormSize.nWidth := Value; end; procedure TManagerConfig.SetFormSizeHeight(Value: Integer); begin if NormalConfig.FormSize.nHeight <> Value then NormalConfig.FormSize.nHeight := Value; end; procedure TManagerConfig.SetFormPosSave(Value: Boolean); begin if NormalConfig.FormPosition.bDo <> Value then NormalConfig.FormPosition.bDo := Value; end; procedure TManagerConfig.SetFormPosLeft(Value: Integer); begin if NormalConfig.FormPosition.nLeft <> Value then NormalConfig.FormPosition.nLeft := Value; end; procedure TManagerConfig.SetFormPosTop(Value: Integer); begin if NormalConfig.FormPosition.nTop <> Value then NormalConfig.FormPosition.nTop := Value; end; procedure TManagerConfig.SetStartupExecute(Value: Boolean); begin if NormalConfig.StartupExecute <> Value then NormalConfig.StartupExecute := Value; end; procedure TManagerConfig.SetLogPath(const sLogPath: String); begin if PathConfig_.LogPath <> sLogPath then PathConfig_.LogPath := sLogPath; end; function TManagerConfig.GetLogPath: String; begin Result := PathConfig_.LogPath; if Result = '' then Result := GetRunExePathDir + DIR_DEFAULT_LOG_PATH; end; procedure TManagerConfig.SetDataPath(const sDataPath: String); begin if PathConfig_.DataPath <> sDataPath then PathConfig_.DataPath := sDataPath; end; function TManagerConfig.GetDatapath: String; begin Result := PathConfig_.DataPath; if Result = '' then Result := GetRunExePathDir + DIR_DEFAULT_DATA_PATH; end; procedure TManagerConfig.SetDefault; begin with NormalConfig do begin FormSize.bDo := false; FormSize.nWidth := -1; FormSize.nHeight := -1; FormPosition.bDo := false; FormPosition.nTop := -1; FormPosition.nLeft := -1; FormInitHide := false; SaveLog := true; CloseBtnEvent := cbeQuestion; StartupExecute := true; end; with PathConfig_ do begin LogPath := GetRunExePathDir + DIR_DEFAULT_LOG_PATH; DataPath := GetRunExePathDir + DIR_DEFAULT_DATA_PATH; end; end; procedure TManagerConfig.SaveConfig; var O: ISuperObject; sPath: String; fs: TFileStream; begin O := SO; O['NormalConfig'] := TTgJson.ValueToJsonObject(NormalConfig); O['PathConfig'] := TTgJson.ValueToJsonObject(PathConfig_); sPath := ExtractFilePath(GetRunExePath)+INI_CONFIG; Guard(fs, TFileStream.Create(sPath, fmCreate)); O.SaveTo(fs, true); end; procedure TManagerConfig.LoadConfig; var sPath: String; lstJsonData: TStringList; O: ISuperObject; begin sPath := ExtractFilePath(GetRunExePath) + INI_CONFIG; if not FileExists(sPath) then begin SetDefault; exit; end; Guard(lstJsonData, TStringList.Create); lstJsonData.LoadFromFile(sPath); O := SO(lstJsonData.Text); if O['NormalConfig'] <> nil then begin // NormalConfig := TTgJson.GetDataAsType(O['NormalConfig']); { SaveLog, FormInitHide : Boolean; FormSize : TFormSize; FormPosition : TFormPosition; CloseBtnEvent : TCloseBtnEvent; StartupExecute: Boolean; AlphaBlend : Boolean; AlphaBlendVal : WORD; } with NormalConfig do begin SaveLog := O['NormalConfig'].B['SaveLog']; FormInitHide := O['NormalConfig'].B['FormInitHide']; if O['NormalConfig'].O['FormSize'] = nil then begin FormSize.bDo := false; FormSize.nWidth := -1; FormSize.nHeight := -1; end else FormSize := TTgJson.GetDataAsType(O['NormalConfig'].O['FormSize']); if O['NormalConfig'].O['FormPosition'] = nil then begin FormPosition.bDo := false; FormPosition.nTop := -1; FormPosition.nLeft := -1; end else FormPosition := TTgJson.GetDataAsType(O['NormalConfig'].O['FormPosition']); CloseBtnEvent := TCloseBtnEvent(O['NormalConfig'].I['CloseBtnEvent']); StartupExecute := O['NormalConfig'].B['StartupExecute']; AlphaBlend := O['NormalConfig'].B['AlphaBlend']; AlphaBlendVal := O['NormalConfig'].I['AlphaBlendVal']; if AlphaBlendVal = 0 then AlphaBlendVal := 255; end; end; if O['PathConfig'] <> nil then PathConfig_ := TTgJson.GetDataAsType(O['PathConfig']); end; end.