Ignore:
Timestamp:
Oct 9, 2012, 11:48:44 AM (12 years ago)
Author:
chronos
Message:
  • Added: Load and save module list to registry.
  • Added: Installation and uninstallation of modules are now performed in two steps as BeforeStart, BeforeStop and AfterStart, AfterStop.
  • Fixed: Demo works again.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • ModularSystem/UModularSystem.pas

    r421 r428  
    66
    77uses
    8   Classes, SysUtils, Contnrs;
     8  Classes, SysUtils, Contnrs, URegistry;
    99
    1010type
     
    1919  TModule = class(TComponent)
    2020  private
     21    FEnabled: Boolean;
    2122    FRunning: Boolean;
    2223    FInstalled: Boolean;
     
    2930    FDependencies: TStringList;
    3031    FDescription: TStringList;
     32    procedure SetEnabled(AValue: Boolean);
    3133    procedure SetInstalled(AValue: Boolean);
    3234    procedure SetRunning(AValue: Boolean);
     35  protected
     36    procedure BeforeStart; virtual;
     37    procedure AfterStart; virtual;
     38    procedure BeforeStop; virtual;
     39    procedure AfterStop; virtual;
    3340  public
    3441    API: TAPI;
    35     MarkForInstall: Boolean;
    3642    procedure Start; virtual;
    3743    procedure Stop; virtual;
     
    4854    property Running: Boolean read FRunning write SetRunning;
    4955    property Installed: Boolean read FInstalled write SetInstalled;
     56    property Enabled: Boolean read FEnabled write SetEnabled;
    5057  published
    5158    property Version: string read FVersion write FVersion;
     
    7986    procedure EnumModulesInstall(Dependencies, ModuleList: TStringList);
    8087    procedure EnumModulesUninstall(ModuleName: string; ModuleList: TStringList);
    81     procedure RegisterModule(Module: TModule; MarkForInstall: Boolean = False);
     88    procedure RegisterModule(Module: TModule; Enabled: Boolean = True);
    8289    procedure UnregisterModule(Module: TModule);
    8390    procedure StartInstalled;
    84     procedure InstallMarked;
     91    procedure InstallEnabled;
    8592    procedure StopAll;
    8693    procedure UninstallAll;
     94    procedure LoadFromRegistry(Context: TRegistryContext);
     95    procedure SaveToRegistry(Context: TRegistryContext);
    8796    constructor Create(AOwner: TComponent); override;
    8897    destructor Destroy; override;
     
    143152  for I := 0 to Dependencies.Count - 1 do begin
    144153    Module := FindModuleByName(Dependencies[I]);
    145     if Assigned(Module) then begin
     154    if Assigned(Module) and Module.Enabled then begin
    146155      if not Module.Running then Module.Start;
    147156    end else raise Exception.CreateFmt(SModuleNotFound, [ModuleName, Dependencies[I]]);
     
    199208  for I := 0 to Dependencies.Count - 1 do begin
    200209    Module := FindModuleByName(Dependencies[I]);
    201     if Assigned(Module) then begin
     210    if Assigned(Module) and Module.Enabled then begin
    202211      if not Module.Installed then Module.Install;
    203212    end else raise Exception.CreateFmt(SModuleNotFound, [ModuleName, Dependencies[I]]);
     
    248257
    249258procedure TModuleManager.RegisterModule(Module: TModule;
    250   MarkForInstall: Boolean = False);
     259  Enabled: Boolean = True);
    251260begin
    252261  Modules.Add(Module);
    253262  Module.Manager := Self;
    254263  Module.API := API;
    255   Module.MarkForInstall := MarkForInstall;
     264  Module.Enabled := Enabled;
    256265end;
    257266
     
    270279end;
    271280
    272 procedure TModuleManager.InstallMarked;
     281procedure TModuleManager.InstallEnabled;
    273282var
    274283  I: Integer;
     
    276285  for I := 0 to Modules.Count - 1 do
    277286  with TModule(Modules[I]) do
    278     if not Installed and MarkForInstall then Install;
     287    if not Installed and Enabled then Install;
    279288end;
    280289
     
    310319end;
    311320
     321procedure TModuleManager.LoadFromRegistry(Context: TRegistryContext);
     322var
     323  I: Integer;
     324begin
     325  with TRegistryEx.Create do
     326  try
     327    RootKey := Context.RootKey;
     328    for I := 0 to Modules.Count - 1 do
     329    with TModule(Modules[I]) do begin
     330      OpenKey(Context.Key + '\' + Identification, True);
     331      Running := ReadBoolWithDefault('Run',  Enabled);
     332    end;
     333  finally
     334    Free;
     335  end;
     336end;
     337
     338procedure TModuleManager.SaveToRegistry(Context: TRegistryContext);
     339var
     340  I: Integer;
     341begin
     342  with TRegistryEx.Create do
     343  try
     344    RootKey := Context.RootKey;
     345    for I := 0 to Modules.Count - 1 do
     346    with TModule(Modules[I]) do begin
     347      OpenKey(Context.Key + '\' + Identification, True);
     348      WriteBool('Run', Running);
     349    end;
     350  finally
     351    Free;
     352  end;
     353end;
     354
    312355{ TModule }
    313356
     
    318361end;
    319362
     363procedure TModule.BeforeStart;
     364begin
     365  if Running then Exit;
     366  if not Installed then Install;
     367  Manager.StartDependencies(Identification, Dependencies);
     368end;
     369
     370procedure TModule.AfterStart;
     371begin
     372  FRunning := True;
     373end;
     374
     375procedure TModule.BeforeStop;
     376begin
     377  if not Running then Exit;
     378  FRunning := False;
     379  Manager.StopDependencies(Identification);
     380end;
     381
     382procedure TModule.AfterStop;
     383begin
     384end;
     385
    320386procedure TModule.SetInstalled(AValue: Boolean);
    321387begin
     
    324390end;
    325391
     392procedure TModule.SetEnabled(AValue: Boolean);
     393begin
     394  if FEnabled = AValue then Exit;
     395  FEnabled := AValue;
     396  if not FEnabled and FInstalled then Uninstall;
     397end;
     398
    326399procedure TModule.Start;
    327400begin
    328   if Running then Exit;
    329   Manager.StartDependencies(Identification, Dependencies);
    330   FRunning := True;
     401  BeforeStart;
     402  AfterStart;
    331403end;
    332404
    333405procedure TModule.Stop;
    334406begin
    335   if not Running then Exit;
    336   Manager.StopDependencies(Identification);
    337   FRunning := False;
     407  BeforeStop;
     408  AfterStop;
    338409end;
    339410
Note: See TracChangeset for help on using the changeset viewer.