tvplanit: Refactor ical import preview to facilitate future import of vcards.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@8379 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2022-08-11 16:46:32 +00:00
parent 4596ff2bef
commit c22ce64166
23 changed files with 815 additions and 351 deletions

View File

@ -0,0 +1,207 @@
unit VpImportPreview_ICalEvent;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs,
VpData, VpBaseDS, VpImportPreview, VpICal;
type
{ TVpImportPreviewICalEventForm }
TVpImportPreviewICalEventForm = class(TVpImportPreviewForm)
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;
// property Kind: TVpICalItemKind read FKind write SetKind;
end;
var
VpImportPreviewICalEventForm: TVpImportPreviewICalEventForm;
implementation
{$R *.lfm}
uses
VpSR;
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
item: TVpICalEntry;
begin
Result := '';
if (ACol = 1) and (ARow >= Grid.FixedRows) then
begin
item := TVpICalEntry(FItems[ARow - Grid.FixedRows]);
if item <> nil then
Result := GetEventText(TVpICalEvent(item));
end;
end;
function TVpImportPreviewICalEventForm.GetEventText(AEvent: TVpICalEvent): String;
var
startTime, endTime: TDateTime;
sStartTime, sEndTime: String;
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);
if TVpEvent.IsAllDayEvent(startTime, endTime) then
begin
if trunc(startTime) = trunc(endTime) 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 := FDatastore.FindBestEventCategory(AEvent.Categories);
if cat = '' then cat := FDefaultCategory;
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;
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;
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;
end;
procedure TVpImportPreviewICalEventForm.SetCalendar(const AValue: TVpICalendar);
begin
if AValue <> FCalendar then
begin
FCalendar := AValue;
PrepareItems;
end;
end;
end.