unit ICSClass;

interface

Uses
  SysUtils;

Type
  TICSEntry = record
    Summary,
    RecurDefaultUnits,
    RRule,
    DTStart,
    DTEnd: String;
  end;
  TICS = Class(TObject)
  private
    flist: Array of TICSEntry;
    FVersion: String;
    FProdID: string;
    procedure SetVersion(const Value: String);
    procedure SetProdID(const Value: string);
    function GET_COUNT: Integer;
    function GET_ICSENTRY(Index: Integer): TICSEntry;
    procedure SET_ICSENTRY(Index: Integer; const Value: TICSEntry);
  public
    Constructor Create;
    Destructor Destroy; override;
    Function LoadFromFile(Filename: String): boolean;
    Procedure SaveToFile(Filename: String);
    Property ICSEntry[Index: Integer]: TICSEntry read GET_ICSENTRY write SET_ICSENTRY; default;
  Published
    property Version: String read FVersion write SetVersion;
    Property ProdID: string read FProdID write SetProdID;
    Property Count: Integer read GET_COUNT;
  end;

implementation

Uses
  Filter;

{ TICS }

constructor TICS.Create;
begin
  // Initialize the array with zero length
  Setlength(fList, 0);
end;

destructor TICS.Destroy;
begin
  // Since the ICSEntry is not a class but merely a record we don't need to free up the memory
  inherited;
end;

function TICS.GET_COUNT: Integer;
begin
  Result := length(fList);
end;

function TICS.GET_ICSENTRY(Index: Integer): TICSEntry;
begin
  Result := fList[Index];
end;

function TICS.LoadFromFile(Filename: String): boolean;
var
  txt: TextFile;
  S, N, S2: String;
  Index: Integer;
begin
  Result := False;
  IF Not FileExists(Filename) then
    Exit;

  Assignfile(Txt, Filename);
  Reset(txt);
  ReadLn(Txt, S);
  if Pos('BEGIN:VCALENDAR', UpperCase(S)) = 0 then
  begin
    Closefile(txt);
    exit;
  end;

  Index := -1;
  While not EOF(txt) do
  begin
    Readln(txt, S);
    if UpperCase(S) = 'BEGIN:VTIMEZONE' then
    begin
      While Pos('END:VTIMEZONE', UpperCase(S)) = 0 do
        ReadLn(txt, S);
      ReadLn(txt, S);
    end;    
    if UpperCase(S) = 'BEGIN:VTODO' then
    begin
      While Pos('END:VTODO', UpperCase(S)) = 0 do
        ReadLn(txt, S);
      ReadLn(txt, S);
    end;
    if Pos(';', S) <> 0 then
      if  Pos(';', S) < Pos(':', S) then
        N := GetFilt(S, 1, ';')
      else
        N := GetFilt(S, 1, ':')
    else
      N := GetFilt(S, 1, ':');
    N := UpperCase(N);
    if (N = '') or (N = ' ') or (N = 'END') or (N = 'VERSION') or (N = 'PRODID') or (N = 'CREATED') or (N = 'LAST-MODIFIED') or (N = 'DTSTAMP') or (N = 'UID') or (N = 'STATUS') or (N = 'CATEGORIES') or (N = 'CLASS') then
      Continue;
    If UpperCase(S) = 'BEGIN:VEVENT' then
    begin
      Setlength(fList, length(flist) + 1);
      Index := High(fList);
      Continue;
    end;
    If Index = -1 then
      Continue;
    If N = 'SUMMARY' then
    begin
      if Pos(':', S) = 0 then
        ReadLn(Txt, S);
      S2 := GetFilt(S, 2, ':');
      fList[Index].Summary := S2;
      Continue;
    end;
    If N = 'X-MOZILLA-RECUR-DEFAULT-UNITS' then
    begin
      if Pos(':', S) = 0 then
        ReadLn(Txt, S);
      S2 := GetFilt(S, 2, ':');
      fList[Index].RecurDefaultUnits := S2;
      Continue;
    end;
    If N = 'RRULE' then
    begin
      if Pos(':', S) = 0 then
        ReadLn(Txt, S);
      S2 := GetFilt(S, 2, ':');
      fList[Index].RRule := S2;
      Continue;
    end;
    If N = 'DTSTART' then
    begin
      While Pos(':', S) = 0 do
        ReadLn(Txt, S);
      S2 := GetFilt(S, 2, ':');
      if S2 <> '' then   
        fList[Index].DTStart := S2
      else
      try
        ReadLn(Txt, S);
        fList[Index].DTStart := Trim(S);
      except
      end;
      Continue;
    end;
    If N = 'DTEND' then
    begin
      While Pos(':', S) = 0 do
        ReadLn(Txt, S);
      S2 := GetFilt(S, 2, ':');
      fList[Index].DTEnd := S2;
      Continue;
    end;
  end;
    CloseFile(TXT);
  Result := True;
end;

procedure TICS.SaveToFile(Filename: String);
var
  Txt: TextFile;
  i: Integer;
begin
  Assignfile(Txt, Filename);
  Rewrite(txt);
  Writeln(txt, 'BEGIN:VCALENDAR');
  Writeln(txt, 'VERSION:2.0');
  Writeln(Txt, 'PRODID:' + ProdID);
  For i := 0 to Count - 1 do
  begin
    Writeln(Txt, 'BEGIN:VEVENT');
    Writeln(Txt, 'SUMMARY:' + fList[i].Summary);
    Writeln(TxT, 'X-MOZILLA-RECUR-DEFAULT-UNITS:' + fList[i].RecurDefaultUnits);
    Writeln(Txt, 'RRULE:' + fList[i].RRule);
    Writeln(Txt, 'DTSTART:' + fList[i].DTStart);
    Writeln(Txt, 'DTEND:' + fList[i].DTEnd);
    Writeln(Txt, 'END:VEVENT');
  end;
  Writeln(Txt, 'END:VCALENDAR');
  CloseFile(Txt);
end;

procedure TICS.SetProdID(const Value: string);
begin
  FProdID := Value;
end;

procedure TICS.SetVersion(const Value: String);
begin
  FVersion := Value;
end;

procedure TICS.SET_ICSENTRY(Index: Integer; const Value: TICSEntry);
begin
  If Index >= Length(fList) then
  begin
    SetLength(fList, Length(fList) +1);
    fList[High(fList)] := Value;
  end
  else
    fList[Index] := Value;
end;

end.
