unit TMS.ServerWizard.WizardAbstract;

{$I bcl.inc}

interface

{$IFDEF DELPHIXE2_LVL}

uses
  Windows, ToolsAPI, Vcl.Graphics, TMS.ServerWizard.Creation, TMS.ServerWizard.Types;

type
  ITMSXDataWizard = interface(IInterface)
    ['{FDE9085F-E28B-482E-95FA-647D3093DBBC}']
    function GetValue(const AName: string): string;
    procedure SetValue(const AName, AValue: string);
  end;

  TTMSServerWizardAbstract = class(TNotifierObject, IOTAWizard,
    IOTARepositoryWizard, IOTARepositoryWizard60, IOTARepositoryWizard80,
    IOTARepositoryWizard160, {$IF COMPILERVERSION > 31}IOTARepositoryWizard190, {$IFEND}
    IOTAProjectWizard, IOTAProjectWizard100, ITMSXDataWizard)
  private
    FIcon : TIcon;
    FURL: string;
    function FormCreateClicked(const AServerKind : TServerKind;
      const Settings: TProjectCreationSettings; const Kinds: TProjectKinds): Boolean;
    function FormGetServerName: string;
  protected
    function GetWizardName : string;
    function GetServerKind : TServerKind; virtual; abstract;
    function BeforeExecute: Boolean; virtual;
  public
    constructor Create; virtual;
    destructor Destroy; override;

    property WizardName :string read GetWizardName;
    property ServerKind : TServerKind read GetServerKind;
  public
    // IOTAWizard
    procedure Execute;
    function GetIDString: string;
    function GetName: string; virtual;
    function GetState: TWizardState;
    procedure AfterSave;
    procedure BeforeSave;
    procedure Destroyed;
    procedure Modified;
    // IOTARepositoryWizard
    function GetAuthor: string;
    function GetComment: string;
    function GetGlyph: Cardinal;
    function GetPage: string;
    // IOTARepositoryWizard60
    function GetDesigner: string;
    // IOTARepositoryWizard80
    function GetGalleryCategory: IOTAGalleryCategory;
    function GetPersonality: string;
    // IOTARepositoryWizard160
    function GetFrameworkTypes: TArray<string>;
    function GetPlatforms: TArray<string>;
    // IOTARepositoryWizard190
    function GetSupportedPlatforms: TArray<string>;
    // IOTAProjectWizard
    // IOTAProjectWizard100
    function IsVisible(Project: IOTAProject): Boolean;
    //ITMSXDataWizard
    function GetValue(const AName: string): string;
    procedure SetValue(const AName, AValue: string);
  end;

{$ENDIF}

implementation

{$IFDEF DELPHIXE2_LVL}

{$R TMSServerWizardResources.res}

uses
  TMS.ServerWizard.GeneralWizardForm, System.SysUtils, TMS.ServerWizard.ToolsAPI.Utils;

{ TSparkleServerWizard }

constructor TTMSServerWizardAbstract.Create;
var
  CategoryManager : IOTAGalleryCategoryManager;
begin
  CategoryManager := (BorlandIDEServices as IOTAGalleryCategoryManager);

  CategoryManager.AddCategory(CategoryManager.FindCategory(sCategoryDelphiNew),
    strMyCustomCategory, strMyCustomCategory);

  FIcon := nil;
end;

destructor TTMSServerWizardAbstract.Destroy;
begin
  FIcon.Free;
  inherited;
end;

{$REGION 'IOTAWizard'}
  procedure TTMSServerWizardAbstract.AfterSave;
  begin
  end;

  procedure TTMSServerWizardAbstract.BeforeSave;
  begin
  end;

procedure TTMSServerWizardAbstract.Destroyed;
  begin
  end;

  procedure TTMSServerWizardAbstract.Modified;
  begin
  end;
{$ENDREGION}

function TTMSServerWizardAbstract.BeforeExecute : Boolean;
var
  PG : IOTAProjectGroup;
begin
  // Close the current project group before creating projects
  // Otherwise, it adds projects, and there can be conflicts - eg, the crash where
  // two projects have ConnectionModule in is avoided when creating several projects
  // at once, but happens when you add a second project after creating a first
  // Also, follows Delphi behaviour of closing project group when using File > New.
  Result := true;
  PG := CurrentProjectGroup;
  if Assigned(PG) then begin
    if not PG.Close then
      Exit(false); // If the user doesn't want to save / continue, don't go ahead
    PG := nil; // Don't hold reference to a closed project group
  end;
end;

procedure TTMSServerWizardAbstract.Execute;
const
  cURL = 'http://%s:%s/%s';
var
  WizardForm : TTMSGeneralWizardForm;
begin
  if not BeforeExecute then
    Exit;

  WizardForm := TTMSGeneralWizardForm.Create(nil, ServerKind, FormCreateClicked, FormGetServerName);
  try
    WizardForm.ShowModal;
    FURL := Format(cURL, [WizardForm.edtHost.Text, WizardForm.edtPort.Text, WizardForm.edtPath.Text]);
  finally
    WizardForm.Free;
  end;
end;

function TTMSServerWizardAbstract.GetAuthor: string;
begin
  Result := strAuthor;
end;

function TTMSServerWizardAbstract.GetComment: string;
begin
  Result := Format(strComment, [WizardName]);
end;

function TTMSServerWizardAbstract.GetDesigner: string;
begin
  Result := dVCL;
end;

function TTMSServerWizardAbstract.GetFrameworkTypes: TArray<string>;
begin
  Result := nil;
end;

function TTMSServerWizardAbstract.GetGalleryCategory: IOTAGalleryCategory;
begin
  Result := (BorlandIDEServices as IOTAGalleryCategoryManager)
    .FindCategory(strMyCustomCategory);
end;

function TTMSServerWizardAbstract.GetGlyph: Cardinal;
var
  ResName : string;
begin
  if not Assigned(FIcon) then begin
    FIcon := TIcon.Create;
    ResName := Format('%sICON', [UpperCase(WizardName)]);
    FIcon.LoadFromResourceName(HInstance, ResName);
  end;

  Result := CopyIcon(FIcon.Handle);
end;

function TTMSServerWizardAbstract.GetIDString: string;
begin
  Result := Format(strIDString, [WizardName]);
end;

function TTMSServerWizardAbstract.GetName: string;
begin
  Result := Format(strName, [WizardName]);
end;

function TTMSServerWizardAbstract.GetPage: string;
begin
  Result := strAuthor;
end;

function TTMSServerWizardAbstract.GetPersonality: string;
begin
  Result := sDelphiPersonality;
end;

function TTMSServerWizardAbstract.GetPlatforms: TArray<string>;
begin
  Result := nil;
end;

function TTMSServerWizardAbstract.GetState: TWizardState;
begin
  Result := [wsEnabled];
end;

function TTMSServerWizardAbstract.GetSupportedPlatforms: TArray<string>;
begin
  Result := nil;
end;

function TTMSServerWizardAbstract.IsVisible(Project: IOTAProject): Boolean;
begin
  Result := True;
end;

function TTMSServerWizardAbstract.FormGetServerName : string;
begin
  Result := WizardName;
end;

function TTMSServerWizardAbstract.FormCreateClicked(const AServerKind : TServerKind;
  const Settings : TProjectCreationSettings; const Kinds : TProjectKinds) : Boolean;
var
  Creator : TServerCreator;
begin
  Creator := TServerCreator.Create(AServerKind, Settings, Kinds);
  try
    Result := Creator.CreateServers;
  finally
    Creator.Free;
  end;
end;

function TTMSServerWizardAbstract.GetWizardName: string;
begin
  Result := ServerKindToString(ServerKind)
end;

function TTMSServerWizardAbstract.GetValue(const AName: string): string;
const
  cURLName = 'URL';
begin
  if AName = cURLName then
    Result := FURL
  else
    Result := '';
end;

procedure TTMSServerWizardAbstract.SetValue(const AName, AValue: string);
begin
end;

{$ENDIF}

end.
