unit CMLx_PlugServ;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  Windows, ActiveX, Classes, ComObj, CM2AutoUpdateInterface, CM2CMLxInterface;

type
  TLogix = class(TComObject, ICM2CMLxPlug, ICM2AutoUpdate)
  private
   LastErrMsg: String;
  protected
   Function Name(OUT Name: Pchar): HResult; stdcall;
   Function Version(OUT Version: word): HResult; stdcall; // Uses MMRB Notation
   Function RequiredCoreVersion(OUT RCV: Word): HResult; stdcall; // Uses MMRB Notation - Please refer to CM2Plugtypes for details
   Function SetParentHandle(Handle: THandle): HResult; stdcall;
   // Declare Error commands
   Function GetLastErrorMsg(OUT Msg: PChar): HResult; stdcall;
   // Declare Info system
   Function GetInfoCount(OUT InfoCount: Integer): HResult; stdcall;
   Function GetInfo(Index: Integer; OUT InfoName: PChar; OUT InfoVar: PChar): HResult; stdcall;
  // Declare "Event" types
   Function Init: HResult; stdcall;
   Function Terminating: HResult; stdcall;
   Function Setup: HResult; stdcall;
   // Plugin Specific
   Function TagName(OUT Tag: PChar): HResult; stdcall;
   Function Perform(Attribute: PChar; Parameters: PChar; OUT ResultString: PChar; OUT ResultType: Byte): HResult; stdcall;
   // Update
   Function UpdateFileURL(OUT URL: Pchar): HResult; stdcall;
  end;

const
  Class_Logix: TGUID = '{7DC8E971-2882-40E1-ACB4-E6D0E721EE7A}';
  RequiredCV = 0610;
  PlugVersion = 0100;
  UpdateFile = 'http://www.coolmon.org/update/logix.upd';

implementation

uses ComServ, DumForm, Forms, xmldom, XMLIntf, msxmldom, XMLDoc, sysutils;

{ TLogix }

function TLogix.GetInfo(Index: Integer; out InfoName,
  InfoVar: PChar): HResult;
begin
 Result := S_OK;
 Case Index of
  0: Begin InfoName := 'Programmed By'; InfoVar := 'C. Aaangel'; end;
  1: Begin InfoName := 'Logic Engine By'; InfoVar := 'M. Israelsen'; end;
  2: Begin InfoName := 'Created'; InfoVar := '13th December 2004'; end;
  3: Begin InfoName := 'Updated'; InfoVar := '13th December 2004'; end;
  4: Begin InfoName := 'Copyright'; InfoVar := '(c) 2004 - The CoolMon Project'; end;
  else begin
   Result := E_FAIL;
   LastErrMsg := 'Index out of bounds';
  end;
 end;
end;

function TLogix.GetInfoCount(out InfoCount: Integer): HResult;
begin
 Result := S_OK;
 InfoCount := 5;
end;

function TLogix.GetLastErrorMsg(out Msg: PChar): HResult;
begin
 If LastErrMsg = '' then MSg := 'Unknown error'
  else Msg := Pchar(LastErrMsg);
 Result := S_OK;
end;

function TLogix.Init: HResult;
begin
 result := S_OK;
end;

function TLogix.Name(out Name: Pchar): HResult;
begin
 Name := 'Logix';
 Result := S_OK;
end;

function TLogix.Perform(Attribute, Parameters: PChar;
  out ResultString: PChar; out ResultType: Byte): HResult;
Var
 DummyForm: TDummyForm;
 Statement: String;
 OnTrue: String;
 OnFalse: String;
 I: Integer;
 Node: IXMLNode;
begin
 DummyForm := TDummyForm.Create(nil);
 DummyForm.XML.XML.Text := Parameters;
 DummyForm.XML.Active := true;
 Node := dummyform.XML.ChildNodes.Nodes['cml'];
 Statement := '';
 For I := 0 to Node.ChildNodes.Count - 1 do begin
  If uppercase(Node.childnodes[I].Nodename) = 'STATEMENT' then begin
   Statement := Node.ChildNodes[I].NodeValue; 
  end;
 end;
 DummyForm.XML.Active := False;
 FreeAndNil(DummyForm);
 ResultType := rtCML;
 Result := S_OK;
end;

function TLogix.RequiredCoreVersion(out RCV: Word): HResult;
begin
 RCV := RequiredCV;
 Result := S_OK;
end;

function TLogix.SetParentHandle(Handle: THandle): HResult;
begin
 result := S_OK;
end;

function TLogix.Setup: HResult;
begin
 Result := E_NOTIMPL;
end;

function TLogix.TagName(out Tag: PChar): HResult;
begin
 Result := S_OK;
 Tag := 'Logix';
end;

function TLogix.Terminating: HResult;
begin
 Result := S_OK;
end;

function TLogix.UpdateFileURL(out URL: Pchar): HResult;
begin
 URL := Pchar(UpdateFile);
 Result := S_OK;
end;

function TLogix.Version(out Version: word): HResult;
begin
 Version := PlugVersion;
 Result := S_OK;
end;

initialization
  TComObjectFactory.Create(ComServer, TLogix, Class_Logix,
    'Logix', '', ciMultiInstance, tmApartment);
end.
 