unit VpImportPreview_ICalEvent; {$mode objfpc}{$H+} interface uses lazlogger, Classes, SysUtils, Forms, Controls, Graphics, Dialogs, VpData, VpBaseDS, VpImportPreview, VpICal, Grids; type { TVpImportPreviewICalEventForm } TVpImportPreviewICalEventForm = class(TVpImportPreviewForm) procedure GridGetEditText(Sender: TObject; ACol, ARow: Integer; var Value: string); procedure GridPickListSelect(Sender: TObject); procedure GridSetEditText(Sender: TObject; ACol, ARow: Integer; const Value: string); private FCalendar: TVpICalendar; FDatastore: TVpCustomDatastore; FDefaultCategory: String; FTimeFormat: String; function GetEventText(AEvent: TVpICalEvent): String; procedure SetCalendar(const AValue: TVpICalendar); protected function GetCellText(ACol, ARow: Integer): String; override; procedure PrepareItems; override; public constructor Create(AOwner: TComponent); override; procedure CheckItem(ARow: Integer; AChecked: Boolean); override; function IsChecked(ARow: Integer): Boolean; override; property Calendar: TVpICalendar read FCalendar write SetCalendar; property Datastore: TVpCustomDatastore read FDatastore write FDatastore; property DefaultCategory: String read FDefaultCategory write FDefaultCategory; end; var VpImportPreviewICalEventForm: TVpImportPreviewICalEventForm; implementation {$R *.lfm} uses VpSR, VpConst; constructor TVPImportPreviewICalEventForm.Create(AOwner: TComponent); begin inherited; Caption := RSImportICalEvent; FTimeFormat := 'c'; // short date + long time format end; procedure TVpImportPreviewICalEventForm.CheckItem(ARow: Integer; AChecked: Boolean); var item: TVpICalEntry; begin if ARow < Grid.FixedRows then exit; item := TVpICalEntry(FItems[ARow - Grid.FixedRows]); if item <> nil then item.Skip := not AChecked; end; function TVpImportPreviewICalEventForm.GetCellText(ACol, ARow: Integer): String; var event: TVpICalEvent; begin Result := ''; if (ARow >= Grid.FixedRows) then begin event := TVpICalEvent(FItems[ARow - Grid.FixedRows]); if event <> nil then case ACol of 1: Result := GetEventText(event); 2: Result := Grid.Columns[2].PickList[event.PickedCategory]; //if FDatastore <> nil then // Result := FDatastore.FindBestEventCategory(TVpICalEvent(item).Categories); end; end; end; function TVpImportPreviewICalEventForm.GetEventText(AEvent: TVpICalEvent): String; var startTime, endTime: TDateTime; sStartTime, sEndTime: String; nDays: Integer; advTime: Integer; advTimeUnits: TVpAlarmAdvType; dingPath: String; s: String; cat: String; begin startTime := AEvent.StartTime[false]; endTime := AEvent.EndTime[false]; sStartTime := FormatDateTime(FTimeFormat, startTime); sEndTime := FormatDateTime(FTimeFormat, endTime - OneSecond); if AEvent.IsAllDayEvent then begin if endTime = NO_DATE then nDays := 1 else nDays := round(endTime - startTime); if nDays in [0, 1] then Result := Format('%s (%s)', [sStartTime, RSAllDay]) else Result := Format('%s - %s (%s)', [sStartTime, sEndTime, RSAllDay]); end else Result := RSStartTimeLbl + ' ' + sStartTime + LineEnding + RSEndTimeLbl + ' ' + sEndTime; Result := Result + LineEnding + RSDescriptionLbl + ' ' + AEvent.Summary + LineEnding + RSCategoryLbl + ' '; // Categories if Assigned(FDatastore) then begin cat := AEvent.Categories.Text; if cat = '' then cat := '(none)'; Result := Result + cat; end; // Recurrence if AEvent.RecurrenceFrequency <> '' then begin s := ''; case Uppercase(AEvent.RecurrenceFrequency) of 'YEARLY': if AEvent.RecurrenceInterval in [0, 1] then s := Format(RSYearlyOn, [FormatDateTime('dd/mm',startTime)]) else s := Format(RSEveryYearsOn, [AEvent.RecurrenceInterval, FormatDateTime('dd/mm', startTime)]); 'MONTHLY': if AEvent.RecurrenceInterval in [0, 1] then s := Format(RSMonthlyOn, [FormatDateTime('d', startTime)]) else s := Format(RSEveryMonthsOn, [AEvent.RecurrenceInterval, FormatDateTime('d', startTime)]); 'WEEKLY': if AEvent.RecurrenceInterval in [0, 1] then s := Format(RSWeeklyOn, [FormatDateTime('dddd',startTime)]) else s := Format(RSEveryWeeksOn, [AEvent.RecurrenceInterval, FormatDateTime('ddd', startTime)]); 'DAILY': if AEvent.RecurrenceInterval in [0, 1] then s := RSDaily else s := Format(RSEveryDays, [AEvent.RecurrenceInterval]); end; if s <> '' then Result := Result + LineEnding + RSRepeat + ' ' + s; end; // Alarm if AEvent.Alarm <> nil then begin TVpEvent.GetAlarmParams(AEvent.Alarm.Trigger, advTime, advTimeUnits); dingPath := AEvent.Alarm.AudioSrc; if advTime <> 1 then begin case advTimeUnits of atMinutes: s := Format(RSXMinutes, [advTime]); atHours: s := Format(RSXHours, [advTime]); atDays: s := Format(RSXDays, [advTime]); end; s := Format(RSAlarmIn, [s]); end else case advTimeUnits of atMinutes: s := Format(RSAlarmIn, [RS1Minute]); atHours: s := Format(RSAlarmIn, [RS1Hour]); atDays: s := Format(RSAlarmIn, [RS1Day]); end; Result := Result + LineEnding + s; if FileExists(dingPath) then Result := Format('%s, %s: %s', [Result, RSSound, dingPath]); end else Result := Result + LineEnding + RSNoAlarm; end; procedure TVpImportPreviewICalEventForm.GridGetEditText(Sender: TObject; ACol, ARow: Integer; var Value: string); var event: TVpICalEvent; begin DebugLn('GetEditText: "' + Value + '"'); event := TVpICalEvent(FItems[Grid.Row - Grid.FixedRows]); if event <> nil then Value := Grid.Columns[2].PickList[event.PickedCategory]; end; procedure TVpImportPreviewICalEventForm.GridPickListSelect(Sender: TObject); var item: TVpICalEntry; cat: String; col: TGridcolumn; begin item := TVpICalEntry(FItems[Grid.Row - Grid.FixedRows]); if item <> nil then begin col := Grid.Columns[2]; //TVpICalEvent(item).PickedCategory := Grid.Columns[2].PickList.IndexOf(ItemIndex); end; end; procedure TVpImportPreviewICalEventForm.GridSetEditText(Sender: TObject; ACol, ARow: Integer; const Value: string); var event: TVpICalEvent; begin event := TVpICalEvent(FItems[Grid.Row - Grid.FixedRows]); if event <> nil then event.PickedCategory := Grid.Columns[2].PickList.IndexOf(Value);; // DebugLn('SetEditText: "' + Value + '"'); end; function TVpImportPreviewICalEventForm.IsChecked(ARow: Integer): Boolean; var item: TVpICalEntry; begin Result := false; if ARow < Grid.FixedRows then exit; item := TVpICalEntry(FItems[ARow - Grid.FixedRows]); if (item <> nil) then Result := not item.Skip; end; procedure TVpImportPreviewICalEventForm.PrepareItems; var i: Integer; L: TStrings; event: TVpICalEvent; cat: String; begin FItems.Clear; if FCalendar <> nil then for i := 0 to FCalendar.Count-1 do if (FCalendar.Entry[i] is TVpICalEvent) then FItems.Add(FCalendar.Entry[i]); inherited; if (FCalendar <> nil) and (FDataStore <> nil) and (Grid.Columns.Count = 2) then begin with Grid.Columns.Add do begin SizePriority := 0; Width := 160; Title.Caption := 'Category to be applied'; ButtonStyle := cbsPickList; L := TStringList.Create; try for i := 0 to 9 do L.Add(FDatastore.CategoryColorMap.GetName(i)); PickList.Assign(L); finally L.Free; end; end; for i := 0 to FItems.Count-1 do begin event := TVpICalEvent(FItems[i]); cat := FDatastore.FindBestEventCategory(event.Categories); if cat = '' then cat := FDefaultCategory; if cat <> '' then event.PickedCategory := Grid.Columns[2].PickList.IndexOf(cat) else event.PickedCategory := 0; end; end; end; procedure TVpImportPreviewICalEventForm.SetCalendar(const AValue: TVpICalendar); begin if AValue <> FCalendar then begin FCalendar := AValue; PrepareItems; end; end; end.