Table of Contents

Managing events

Events (calendar items) live inside a calendar. TTMSFNCCloudGoogleCalendar loads one calendar's events into its Items collection and creates, updates, and deletes them. Each event carries a time span, a color, a visibility, and the ID of the calendar it belongs to. This chapter covers loading a calendar's events, creating a new one, and editing or removing events.

Reading the items of a calendar

Events are fetched per calendar: call GetCalendar(ACalendarID) and, when OnGetCalendar fires, read the Items collection. Each TTMSFNCCloudGoogleCalendarItem exposes Summary, Description, Location, StartTime, EndTime, IsAllDay, Color, Visibility, and CalendarID.

Creating an event

Add an item to Items, set its summary, time span, and options, point its CalendarID at the target calendar, and call Add. The result arrives in OnAddCalendarItem. Set Color from the palette (for example icPurple) and Visibility (such as viPrivate) to control how the event appears. This example combines a calendar from Managing calendars with a new event placed in it.

procedure TForm1.LoadEvents(ACal: TTMSFNCCloudGoogleCal);
begin
  // Loads the items (events) of a single calendar; result arrives in OnGetCalendar.
  FCalendar.GetCalendar(ACal.ID);
end;

procedure TForm1.CalendarGetCalendar(Sender: TObject;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
var
  I: Integer;
  Item: TTMSFNCCloudGoogleCalendarItem;
begin
  if not ARequestResult.Success then
    Exit;

  ListBox2.Clear;
  for I := 0 to FCalendar.Items.Count - 1 do
  begin
    Item := FCalendar.Items[I];
    ListBox2.Items.AddObject(
      FormatDateTime('dd/mm hh:nn', Item.StartTime) + ' - ' + Item.Summary, Item);
  end;
end;

procedure TForm1.AddEvent(ACal: TTMSFNCCloudGoogleCal);
var
  Item: TTMSFNCCloudGoogleCalendarItem;
begin
  Item := FCalendar.Items.Add;
  Item.Summary := 'Project kickoff';
  Item.Description := 'Initial meeting';
  Item.Location := 'Meeting room A';
  Item.Color := icPurple;
  Item.StartTime := Now;
  Item.EndTime := Now + EncodeTime(1, 0, 0, 0);
  Item.Visibility := TTMSFNCCloudGoogleCalendarVisibility.viPrivate;
  Item.CalendarID := ACal.ID; // ties the event to a calendar
  FCalendar.Add(Item); // result arrives in OnAddCalendarItem
end;

Editing and removing items

Edit an event by changing the fields on an item from Items and calling Update; remove one with Delete. Both report through their events (OnUpdateCalendarItem, OnDeleteCalendarItem); reload the calendar's events with GetCalendar afterwards.

Pitfalls

  • Set CalendarID before adding. An event must name the calendar it belongs to, or the Add is rejected.
  • Use IsAllDay for date-only events, otherwise the StartTime/EndTime times are used.
  • Act in the completion event. The Items collection and a new event's ID are only guaranteed once the matching event fires.

See also