unit PluginServ;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  Windows, CM2GenInterface, CM2AutoUpdateInterface, ActiveX, Classes, DateUtils, ComObj, SysUtils;

type
  TCM2GenTime = class(TComObject, ICM2GenPlug, ICM2AutoUpdate)
  private
   LastErrorMsg: PChar;
  protected
   Function Name(OUT Name: Pchar): HResult; stdcall;
   Function Version(OUT Version: word): HResult; stdcall; // Using MMRB Notation
   Function RequiredCoreVersion(OUT RCV: Word): HResult; stdcall; // Using 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 Sensor system
   Function GetSensorCount(OUT SensorCount: Integer): HResult; stdcall;
   Function GetSensorNameOnly(Index: Integer; OUT SensorName: PChar; OUT SensorStyle: Byte): HResult; stdcall; // Designed for speed polling at startup
   Function GetSensor(Index: Integer; Parameters: PChar; OUT SensorName: PChar; OUT SensorStyle: byte; OUT SensorString: PChar; OUT SensorValue: Double): HResult; stdcall;
  // Declare "Event" functions
   Function Init: HResult; stdcall;
   Function Terminating: HResult; stdcall;
   Function Setup: HResult; stdcall;
  // Declare Support types
   // Default (Required)
   Function GetDefaultCount(SensorIndex: Integer; OUT DefaultCount: Integer): HResult; stdcall;
   Function GetDefault(SensorIndex: Integer; Index: Integer; OUT Name: Pchar; OUT Value: PChar; OUT Description: PChar): HResult; stdcall;
   // Optional
   Function GetOptionalCount(SensorIndex: Integer; OUT OptionalCount: Integer): HResult; stdcall;
   Function GetOptional(SensorIndex: Integer; Index: Integer; OUT Name: Pchar; OUT Value: PChar; OUT Description: PChar): HResult; stdcall;
   // auto update
   Function UpdateFileURL(OUT URL: Pchar): HResult; stdcall;
  end;

const
  Class_CM2GenTime: TGUID = '{3E3EBB94-F1D7-4954-914D-2CB8EEC303C1}';
  AutoUpdateFile = 'http://www.coolmon.org/update/time.upd';


implementation

uses ComServ;

{ TCM2GenTime }

function TCM2GenTime.GetDefault(SensorIndex, Index: Integer; out Name,
  Value, Description: PChar): HResult;
begin
 REsult := S_OK;
end;

function TCM2GenTime.GetDefaultCount(SensorIndex: Integer;
  out DefaultCount: Integer): HResult;
begin
 DefaultCOunt := 0;
 Result := S_OK;
end;

function TCM2GenTime.GetInfo(Index: Integer; out InfoName,
  InfoVar: PChar): HResult;
begin
 Result := S_OK;
 Case index of
  0: Begin
      Infoname := 'Original plugin by';
      Infovar := 'C. Aaangel';
     End;
  1: Begin
      Infoname := 'Modified by';
      Infovar := 'Mads Israelsen';
     End;
  2: Begin
      InfoName := 'Created';
      InfoVar := '14 Sep 2003';
     End;
  3: Begin
      InfoName := 'Updated';
      InfoVar := 'May 30th 2004';
     End;
  4: Begin
      InfoName := 'Copyright'; InfoVar := '(c) 2003 - 2004, by C. Aaangel';
     End;
  else begin
   Result := E_FAIL;
   LastErrorMsg := 'Index out of bounds';
  End;
 End;
end;

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

function TCM2GenTime.GetLastErrorMsg(OUT Msg: PChar): HResult;
begin
 If LastErrorMsg = '' then Msg := 'No error'
  else Msg := LastErrorMsg;
 Result := S_OK;
end;

function TCM2GenTime.GetOptional(SensorIndex, Index: Integer; out Name,
  Value, Description: PChar): HResult;
begin
 Result := S_OK;
end;

function TCM2GenTime.GetOptionalCount(SensorIndex: Integer;
  out OptionalCount: Integer): HResult;
begin
 Result := S_OK;
 OptionalCount := 0;
end;

function day(i : integer; short : boolean = false) : string;
begin
  if short then
    Case i of
      1 : result := 'Mon';
      2 : result := 'Tue';
      3 : result := 'Wed';
      4 : result := 'Thu';
      5 : result := 'Fri';
      6 : result := 'Sat';
      7 : result := 'Sun';
    end
  else
    Case i of
      1 : result := 'Sunday';
      2 : result := 'Monday';
      3 : result := 'Tuesday';
      4 : result := 'Wednesdays';
      5 : result := 'Thursday';
      6 : result := 'Friday';
      7 : result := 'Saturday';
    end;
end;

function monthToString(month : integer) : string;
begin
  Case month of
  1 : result := 'January';
  2 : result := 'February';
  3 : result := 'March';
  4 : result := 'April';
  5 : result := 'May';
  6 : result := 'June';
  7 : result := 'July';
  8 : result := 'August';
  9 : result := 'September';
  10 : result := 'October';
  11 : result := 'November';
  12 : result := 'December';
  end;
end;

Function AddZero(i:integer):string;
begin
 if i < 10 then result := '0' + intToStr(i) else
  result := intToStr(i);
end;


function TCM2GenTime.GetSensor(Index: Integer; Parameters: PChar;
  out SensorName: PChar; out sensorstyle: byte; out SensorString: PChar;
  out SensorValue: Double): HResult;
begin
  Result := S_OK;
  Case Index of
   0: Begin
       SensorName := 'Date';
       SensorStyle := 1;
       SensorString := Pchar(DateToStr(Now));
       SensorValue := 0;
      End;
   1: Begin
       SensorName := 'Day';
       SensorStyle := 1;
       SensorString := PChar(addZero(Dayof(now)));
       SensorValue := Dayof(now);
      End;
   2: Begin
       SensorName := 'Dayname';
       SensorStyle := 1;
       SensorString := PChar(Day(dayofweek(now)));
       SensorValue := 0;
      End;
   3: Begin
       SensorName := 'Week';
       SensorStyle := 1;
       SensorString := PChar(IntToStr(Weekof(now)));
       SensorValue := Weekof(now);
      End;
   4: Begin
       SensorName := 'Month';
       SensorStyle := 1;
       SensorString := PChar(addZero(monthof(now)));
       SensorValue := monthof(now);
      End;
   5: Begin
       SensorName := 'Monthname';
       SensorStyle := 1;
       SensorString := PChar(monthToString(monthof(now)));
       SensorValue := 0;
      End;
   6: Begin
       SensorName := 'Year1';
       SensorStyle := 1;
       SensorString := PChar(intToStr(yearof(now)));
       SensorValue := yearof(now);
      End;
   7: Begin
       SensorName := 'Year2';
       SensorStyle := 1;
       SensorString := PChar(copy(intToStr(yearof(now)),3,2));
       SensorValue := 0;
      End;

   8: Begin
       SensorName := 'time';
       SensorStyle := 1;
       SensorString := PChar(timeToStr(now));
       SensorValue := 0;
      End;
   9: Begin
       SensorName := 'hour12';
       SensorStyle := 1;
       if isPM(now) then
         SensorString := PChar(addZero(HourOf(now) - 12))
       else SensorString := PChar(addZero(HourOf(now)));
       if isPM(now) then
         SensorValue := HourOf(now) - 12
       else SensorValue := HourOf(now);
      End;
   10: Begin
       SensorName := 'hour24';
       SensorStyle := 1;
       SensorString := PChar(addZero(HourOf(now)));
       SensorValue := HourOf(now);
      End;
   11: Begin
       SensorName := 'PM-AM';
       SensorStyle := 1;
       if isPM(now) then
       SensorString := PChar('PM') else SensorString := PChar('AM');
       SensorValue := 0;
      End;
   12: Begin
       SensorName := 'minutes';
       SensorStyle := 1;
       SensorString := PChar(addZero(minuteof(now)));
       SensorValue := minuteof(now);
      End;
   13: Begin
       SensorName := 'seconds';
       SensorStyle := 1;
       SensorString := PChar(addZero(secondOf(now)));
       SensorValue := secondOf(now);
      End;
   else begin
    Result := E_FAIL;
    LastErrorMsg := 'Index out of bounds';
   End;
  End;
end;

function TCM2GenTime.GetSensorCount(out SensorCount: Integer): HResult;
begin
 Result := S_OK;
 SensorCount := 14;
end;

function TCM2GenTime.GetSensorNameOnly(Index: Integer;
  out SensorName: PChar; out sensorstyle: byte): HResult;
begin
  Result := S_OK;
  SensorStyle := 1;
  Case Index of
   0: Begin
       SensorName := 'Date';
      End;
   1: Begin
       SensorName := 'Day';
      End;
   2: Begin
       SensorName := 'DayName';
      End;
   3: Begin
       SensorName := 'Week';
      End;
   4: Begin
       SensorName := 'Month';
      End;
   5: Begin
       SensorName := 'MonthName';
      End;
   6: Begin
       SensorName := 'Year1';
      End;
   7: Begin
       SensorName := 'Year2';
      End;
   8: Begin
       SensorName := 'Time';
      End;
   9: Begin
       SensorName := 'Hour12';
      End;
   10: Begin
       SensorName := 'Hour24';
      End;
   11: Begin
       SensorName := 'AmPm';
      End;
   12: Begin
       SensorName := 'Minutes';
      End;
   13: Begin
       SensorName := 'Seconds';
      End;
   else begin
    Result := E_FAIL;
    LastErrorMsg := 'Index out of bounds';
   End;
  End;
end;

function TCM2GenTime.Init: HResult;
begin
 Result := S_OK;
end;

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

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

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

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

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

function TCM2GenTime.UpdateFileURL(out URL: Pchar): HResult;
begin
 URL := PChar(AutoUpdateFile);
 Result := S_OK;
end;

function TCM2GenTime.Version(out Version: word): HResult;
begin
 Version := 0500;
 Result := S_OK;
end;

initialization
  TComObjectFactory.Create(ComServer, TCM2GenTime, Class_CM2GenTime,
    'CM2GenTime', 'CoolMon 2 Gen PlugIn - Time', ciMultiInstance, tmApartment);
end.
