unit System.PowerManagement;
interface
uses
System.Generics.Collections;
type
TPowerModeOption = ( Display, System, Away );
TPowerModeOptions =
set of TPowerModeOption;
IPowerManagementToken =
interface
[ '
{F6326420-9BE9-4CAE-9EC8-0E6C4EE1B265}' ]
function GetOptions( ): TPowerModeOptions;
property Options: TPowerModeOptions
read GetOptions;
end;
IPowerManagementService =
interface
procedure SetOptions( PowerModeOptions: TPowerModeOptions );
end;
PowerManagement =
class sealed
private type
TInternalPowerManagementToken =
class
private
FOptions: TPowerModeOptions;
public
constructor Create( Options: TPowerModeOptions );
destructor Destroy;
override;
property Options: TPowerModeOptions
read FOptions;
end;
TPowerManagementToken =
class( TInterfacedObject, IPowerManagementToken )
private
function GetOptions( ): TPowerModeOptions;
private
FToken: TInternalPowerManagementToken;
public
constructor Create( Options: TPowerModeOptions );
destructor Destroy;
override;
end;
TNullService =
class( TInterfacedObject, IPowerManagementService )
private { IPowerManagementService }
procedure SetOptions( PowerModeOptions: TPowerModeOptions );
end;
private
class var _Service : IPowerManagementService;
class var _Tokens : TList<TInternalPowerManagementToken>;
class var _CurrentOptions: TPowerModeOptions;
class constructor Create( );
class destructor Destroy( );
class procedure TokensNotify( Sender: TObject;
const Item: TInternalPowerManagementToken; Action: TCollectionNotification );
public
class function GetToken(
const PowerModeOptions: TPowerModeOptions ): IPowerManagementToken;
end;
implementation
uses
{$IFDEF MSWINDOWS}
System.PowerManagement.Win,
{$ENDIF}
System.SysUtils;
{ PowerManagement.TPowerManagementToken }
constructor PowerManagement.TPowerManagementToken.Create( Options: TPowerModeOptions );
begin
inherited Create( );
if Options <> [ ]
then
FToken := TInternalPowerManagementToken.Create( Options );
end;
destructor PowerManagement.TPowerManagementToken.Destroy;
begin
FToken.DisposeOf( );
inherited;
end;
function PowerManagement.TPowerManagementToken.GetOptions: TPowerModeOptions;
begin
if Assigned( FToken )
then
Result := FToken.Options
else
Result := [ ];
end;
{ PowerManagement }
class constructor PowerManagement.Create;
begin
{$IFDEF MSWINDOWS}
_Service := TWindowsPowerManagementService.Create;
{$ELSE}
_Service := TNullService.Create;
{$ENDIF}
_Tokens := TList<TInternalPowerManagementToken>.Create( );
_Tokens.OnNotify := TokensNotify;
_CurrentOptions := [ ];
end;
class destructor PowerManagement.Destroy;
begin
_Service.SetOptions( [ ] );
_Tokens.Free;
end;
class function PowerManagement.GetToken(
const PowerModeOptions: TPowerModeOptions ): IPowerManagementToken;
var
lToken: TPowerManagementToken;
begin
lToken := TPowerManagementToken.Create( PowerModeOptions );
Result := lToken;
end;
class procedure PowerManagement.TokensNotify(
Sender : TObject;
const Item: TInternalPowerManagementToken;
Action : TCollectionNotification );
var
lToken : TInternalPowerManagementToken;
lOptions: TPowerModeOptions;
begin
lOptions := [ ];
for lToken
in _Tokens
do
begin
lOptions := lOptions + lToken.FOptions;
end;
if _CurrentOptions <> lOptions
then
begin
_Service.SetOptions( lOptions );
_CurrentOptions := lOptions;
end;
end;
{ PowerManagement.TNullService }
procedure PowerManagement.TNullService.SetOptions( PowerModeOptions: TPowerModeOptions );
begin
// do nothing
end;
{ PowerManagement.TInternalPowerManagementToken }
constructor PowerManagement.TInternalPowerManagementToken.Create( Options: TPowerModeOptions );
begin
inherited Create;
FOptions := Options;
_Tokens.Add( Self );
end;
destructor PowerManagement.TInternalPowerManagementToken.Destroy;
begin
_Tokens.Remove( Self );
inherited;
end;
end.