unit PrefTypesNProcs;

interface

Type
  TGeneral = Record
   PreventShowDesktop: Boolean;
  End;
  TSuspend = Record
   AutoSuspend: Boolean;
   PollOnResume: Boolean;
  End;
  TSystrayIcon = Record
   Retray: Boolean;
   RetrayInterval: SmallInt;
   SwitchIconWhenSuspended: Boolean;
  end;
  TScreenRelation = Record
   InRelationToDesktop: Boolean;
  End;
  TXML = Record
   DumpToXML: Boolean;
   Filename: String;
  end;
  TOptions = Record
   General: TGeneral;
   Suspend: TSuspend;
   SystrayIcon: TSystrayIcon;
   ScreenRelation: TScreenRelation;
   XML: TXML;
  End;
  Procedure LoadOptions; // Load Toptions from registry
  Procedure SaveOptions; // Save TOptions to Reg
  Procedure InitOptions; // Set default value in TOptions
  Procedure SetOptions; // Init options to enviroment

Const
 RegPath = '\Software\The CoolMon Project\CoolMon2\Options\';

implementation

uses Registry, Unit1, Windows, SysUtils, CM2Tree;

procedure InitOptions;
begin
 CM2MainForm.Options.Suspend.AutoSuspend := True;
 CM2MainForm.Options.Suspend.PollOnResume := true;
 CM2MainForm.Options.SystrayIcon.Retray := True;
 CM2MainForm.Options.SystrayIcon.RetrayInterval := 10;
 CM2MainForm.Options.SystrayIcon.SwitchIconWhenSuspended := True;
 CM2MainForm.Options.General.PreventShowDesktop := True;
 CM2MainForm.Options.ScreenRelation.InRelationToDesktop := True;
 CM2MainForm.Options.XML.DumpToXML := false;
 CM2MainForm.Options.XML.Filename := 'sensors.xml'
end;

procedure LoadOptions;
Var
 Reg: TRegistry;
begin
 Reg := TRegistry.Create;
 Reg.RootKey := HKEY_CURRENT_USER;
 If Reg.OpenKey(RegPath, false) then begin
  If reg.ValueExists('Suspend.AutoSuspend') then CM2MainForm.Options.Suspend.AutoSuspend := Reg.ReadBool('Suspend.AutoSuspend');
  If Reg.ValueExists('Suspend.PollOnResume') then CM2MainForm.Options.Suspend.PollOnResume := Reg.ReadBool('Suspend.PollOnResume');
  If Reg.ValueExists('SystrayIcon.Retray') then CM2MainForm.Options.SystrayIcon.Retray := REg.ReadBool('SystrayIcon.Retray');
  If Reg.ValueExists('SystrayIcon.RetrayInterval') then CM2MainForm.Options.SystrayIcon.RetrayInterval := Reg.ReadInteger('SystrayIcon.RetrayInterval');
  If Reg.ValueExists('SystrayIcon.SwitchIconWhenSuspended') then CM2MainForm.Options.SystrayIcon.SwitchIconWhenSuspended := Reg.ReadBool('SystrayIcon.SwitchIconWhenSuspended');
  If Reg.ValueExists('General.PreventShowDesktop') Then CM2MAinForm.Options.General.PreventShowDesktop := Reg.ReadBool('General.PreventShowDesktop');
  If Reg.ValueExists('ScreenRelation.InRelationToDesktop') then CM2MainForm.Options.ScreenRelation.InRelationToDesktop := Reg.ReadBool('ScreenRelation.InRelationToDesktop');
  If Reg.ValueExists('XML.DumpToXML') then CM2MainForm.Options.XML.DumpToXML := Reg.ReadBool('XML.DumpToXML');
  If Reg.ValueExists('XML.Filename') then CM2MainForm.Options.XML.Filename := Reg.ReadString('XML.Filename');
 end;
 Reg.CloseKey;
 FreeAndNil(Reg);
end;

procedure SaveOptions;
Var
 Reg: TRegistry;
begin
 Reg := TRegistry.Create;
 Reg.RootKey := HKEY_CURRENT_USER;
 Reg.OpenKey(RegPAth, true);
 // General
 Reg.WriteBool('General.PreventShowDesktop', CM2MainForm.Options.General.PreventShowDesktop);
 // Suspend
 Reg.WriteBool('Suspend.AutoSuspend', CM2MainForm.Options.Suspend.AutoSuspend);
 Reg.WriteBool('Suspend.PollOnResume', CM2MainForm.Options.Suspend.PollOnResume);
 // SystrayIcon
 Reg.WriteBool('SystrayIcon.Retray', CM2MainForm.Options.SystrayIcon.Retray);
 Reg.WriteInteger('SystrayIcon.RetrayInterval', CM2MainForm.Options.SystrayIcon.RetrayInterval);
 Reg.WriteBool('SystrayIcon.SwitchIconWhenSuspended', CM2MainForm.Options.SystrayIcon.SwitchIconWhenSuspended);
 Reg.WriteBool('ScreenRelation.InRelationToDesktop', CM2MainForm.Options.ScreenRelation.InRelationToDesktop);
 Reg.WriteBool('XML.DumpToXML', CM2MainForm.Options.XML.DumpToXML);
 Reg.WriteString('XML.Filename', CM2MainForm.Options.XML.Filename);
 Reg.CloseKey;
 FreeAndNil(Reg);
end;

procedure SetOptions;
begin
 CM2MainForm.CheckForSuspend.Enabled := CM2MainForm.Options.Suspend.AutoSuspend;
// CM2MainForm.tray.ReTray := CM2MainForm.Options.SystrayIcon.Retray;
// CM2MAinForm.tray.ReTrayInterval := CM2MainForm.Options.SystrayIcon.RetrayInterval * 1000; // Convert from sec to Msec
 CM2MainForm.FormMgr.PreventShowDesktop := CM2MainForm.Options.General.PreventShowDesktop;
 CM2Tree.RelationToDesktop := CM2MainForm.Options.ScreenRelation.InRelationToDesktop;
 CM2MainForm.VarMgr.XMLFilename := CM2MainForm.Options.XML.Filename;
 CM2MainForm.VarMgr.SaveToXml := CM2MainForm.Options.XML.DumpToXML;
end;

end.
