Files
lazarus-ccr/components/tvplanit/source/vpimportpreview_icalevent.pas

267 lines
7.4 KiB
ObjectPascal
Raw Normal View History

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 GridSetEditText(Sender: TObject; ACol, ARow: Integer;
const Value: string);
private
FCalendar: TVpICalendar;
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 DefaultCategory: String read FDefaultCategory write FDefaultCategory;
end;
var
VpImportPreviewICalEventForm: TVpImportPreviewICalEventForm;
implementation
{$R *.lfm}
uses
VpSR, VpConst;
constructor TVPImportPreviewICalEventForm.Create(AOwner: TComponent);
begin
inherited;
Grid.OnGetEditText := @GridGetEditText;
Grid.OnSetEditText := @GridSetEditText;
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 begin
item.Checked := AChecked;
inherited;
end;
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];
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;
// Categories
if Assigned(Datastore) then
begin
cat := AEvent.Categories.CommaText;
if cat = '' then cat := RSNoneStr;
Result := Result + LineEnding + RSCategoryLbl + ' ' + 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
event := TVpICalEvent(FItems[Grid.Row - Grid.FixedRows]);
if event <> nil then
Value := Grid.Columns[2].PickList[event.PickedCategory];
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);;
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 := item.Checked;
end;
procedure TVpImportPreviewICalEventForm.PrepareItems;
var
i: Integer;
L: TStrings;
event: TVpICalEvent;
cat: String;
begin
Grid.Columns[1].Title.Caption := RSEventItems;
Grid.Columns[2].Title.Caption := RSAssignedCategory;
// Populate picklist in column 2
L := TStringList.Create;
try
for i := 0 to 9 do
L.Add(Datastore.CategoryColorMap.GetName(i));
Grid.Columns[2].PickList.Assign(L);
finally
L.Free;
end;
FItems.Clear;
if (FCalendar <> nil) and (Datastore <> nil) then
for i := 0 to FCalendar.Count-1 do
if (FCalendar.Entry[i] is TVpICalEvent) then
begin
// Add ical event
event := TVpIcalEvent(FCalendar.Entry[i]);
FItems.Add(event);
// Select best category in picklist column
cat := Datastore.FindBestEventCategory(event.Categories);
if cat = '' then cat := FDefaultCategory;
if cat <> '' then
event.PickedCategory := Grid.Columns[2].PickList.IndexOf(cat)
else
event.PickedCategory := 0;
end;
inherited;
end;
procedure TVpImportPreviewICalEventForm.SetCalendar(const AValue: TVpICalendar);
begin
if AValue <> FCalendar then
begin
FCalendar := AValue;
PrepareItems;
end;
end;
end.