tvplanit: Split ical import off of TVpDayView popupmenu handler into separate method ImportIcalFile.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@8369 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2022-08-05 18:48:09 +00:00
parent 425c4683fd
commit d553c35eb5
2 changed files with 65 additions and 34 deletions

View File

@ -76,6 +76,8 @@ type
TVpContact = class; TVpContact = class;
TVpTask = class; TVpTask = class;
TVpEvents = array of TVpEvent;
TVpResources = class TVpResources = class
private private
FOwner: TObject; // This is the Datastore. FOwner: TObject; // This is the Datastore.

View File

@ -447,6 +447,7 @@ type
function BuildEventString(AEvent: TVpEvent; UseAsHint: Boolean): String; function BuildEventString(AEvent: TVpEvent; UseAsHint: Boolean): String;
procedure DeleteActiveEvent(Verify: Boolean); procedure DeleteActiveEvent(Verify: Boolean);
procedure DragDrop(Source: TObject; X, Y: Integer); override; procedure DragDrop(Source: TObject; X, Y: Integer); override;
function ImportICalFile(AFileName: String; ADefaultCategory: Integer = 0): TVpEvents;
procedure Invalidate; override; procedure Invalidate; override;
function IsHoliday(ADate: TDate; out AHolidayName: String): Boolean; function IsHoliday(ADate: TDate; out AHolidayName: String): Boolean;
procedure LoadLanguage; procedure LoadLanguage;
@ -1354,29 +1355,11 @@ begin
dlg.Options := dlg.Options + [ofAllowMultiSelect, ofFileMustExist]; dlg.Options := dlg.Options + [ofAllowMultiSelect, ofFileMustExist];
if dlg.Execute then begin if dlg.Execute then begin
Screen.Cursor := crHourGlass; Screen.Cursor := crHourGlass;
Application.ProcessMessages;
ical := TVpICalendar.Create;
try try
for fn in dlg.Files do begin Application.ProcessMessages;
ical.LoadFromFile(fn); for fn in dlg.Files do
for i := 0 to ical.Count-1 do begin ImportICalFile(fn);
if not (ical[i] is TVpICalEvent) then
Continue;
startTime := TVpICalEvent(ical[i]).StartTime[false]; // use local times
endTime := TVpICalEvent(ical[i]).EndTime[false];
if (startTime = 0) and (endTime = 0) then
continue;
id := DataStore.GetNextID(EventsTableName);
FActiveEvent := Datastore.Resource.Schedule.AddEvent(id, starttime, endtime);
FActiveEvent.Changed := true;
FActiveEvent.LoadFromICalendar(TVpICalEvent(ical[i]));
Datastore.PostEvents;
Datastore.NotifyDependents;
end;
end;
Invalidate;
finally finally
ical.Free;
Screen.Cursor := crDefault; Screen.Cursor := crDefault;
end; end;
end; end;
@ -1393,7 +1376,6 @@ begin
if FActiveEvent <> nil then if FActiveEvent <> nil then
DeleteActiveEvent (True); DeleteActiveEvent (True);
end; end;
{=====}
procedure TVpDayView.PopupEditEvent(Sender: TObject); procedure TVpDayView.PopupEditEvent(Sender: TObject);
begin begin
@ -1404,49 +1386,41 @@ begin
{ edit this Event } { edit this Event }
dvSpawnEventEditDialog(False); dvSpawnEventEditDialog(False);
end; end;
{=====}
procedure TVpDayView.PopupToday(Sender: TObject); procedure TVpDayView.PopupToday(Sender: TObject);
begin begin
Date := Now; Date := Now;
end; end;
{=====}
procedure TVpDayView.PopupTomorrow(Sender: TObject); procedure TVpDayView.PopupTomorrow(Sender: TObject);
begin begin
Date := Now + 1; Date := Now + 1;
end; end;
{=====}
procedure TVpDayView.PopupYesterday(Sender: TObject); procedure TVpDayView.PopupYesterday(Sender: TObject);
begin begin
Date := Now - 1; Date := Now - 1;
end; end;
{=====}
procedure TVpDayView.PopupNextDay(Sender: TObject); procedure TVpDayView.PopupNextDay(Sender: TObject);
begin begin
Date := Date + 1; Date := Date + 1;
end; end;
{=====}
procedure TVpDayView.PopupPrevDay(Sender: TObject); procedure TVpDayView.PopupPrevDay(Sender: TObject);
begin begin
Date := Date - 1; Date := Date - 1;
end; end;
{=====}
procedure TVpDayView.PopupNextWeek(Sender: TObject); procedure TVpDayView.PopupNextWeek(Sender: TObject);
begin begin
Date := Date + 7; Date := Date + 7;
end; end;
{=====}
procedure TVpDayView.PopupPrevWeek(Sender: TObject); procedure TVpDayView.PopupPrevWeek(Sender: TObject);
begin begin
Date := Date - 7; Date := Date - 7;
end; end;
{=====}
procedure TVpDayView.PopupNextMonth(Sender: TObject); procedure TVpDayView.PopupNextMonth(Sender: TObject);
var var
@ -1463,7 +1437,6 @@ begin
Date := EncodeDate(Y, M, D); Date := EncodeDate(Y, M, D);
end; end;
{=====}
procedure TVpDayView.PopupPrevMonth(Sender: TObject); procedure TVpDayView.PopupPrevMonth(Sender: TObject);
var var
@ -1480,7 +1453,6 @@ begin
Date := EncodeDate(Y, M, D); Date := EncodeDate(Y, M, D);
end; end;
{=====}
procedure TVpDayView.PopupNextYear(Sender: TObject); procedure TVpDayView.PopupNextYear(Sender: TObject);
var var
@ -1489,7 +1461,6 @@ begin
DecodeDate(Date, Y, M, D); DecodeDate(Date, Y, M, D);
Date := EncodeDate(Y + 1, M, 1); Date := EncodeDate(Y + 1, M, 1);
end; end;
{=====}
procedure TVpDayView.PopupPrevYear(Sender: TObject); procedure TVpDayView.PopupPrevYear(Sender: TObject);
var var
@ -2406,7 +2377,65 @@ begin
dvEndingEditing := False; dvEndingEditing := False;
end; end;
end; end;
{=====}
{ Reads the events listed in the specified ical file and adds them to the
day view control. All events imported are collected in the Result array.
ADefaultCategory is the category to which the event is assigned if no fitting
category has been found in the ical, i.e. when the event's category is 0.
If you are not happy with this category replacement you can iterate over the
Result array and change it. }
function TVpDayView.ImportICalFile(AFileName: String;
ADefaultCategory: Integer = 0): TVpEvents;
const
BLOCK_SIZE = 10;
var
ical: TVpICalendar;
startTime, endTime: TDateTime;
i: Integer;
id: Integer;
event: TVpEvent;
eventCounter: Integer;
begin
if ReadOnly or (not CheckCreateResource) or
(not Assigned(DataStore)) or (not Assigned(DataStore.Resource))
then
Exit;
SetLength(Result, BLOCK_SIZE);
eventCounter := 0;
ical := TVpICalendar.Create;
try
ical.LoadFromFile(AFileName);
for i := 0 to ical.Count-1 do begin
if not (ical[i] is TVpICalEvent) then
Continue;
startTime := TVpICalEvent(ical[i]).StartTime[false]; // use local times
endTime := TVpICalEvent(ical[i]).EndTime[false];
if (startTime = 0) and (endTime = 0) then
continue;
id := DataStore.GetNextID(EventsTableName);
event := Datastore.Resource.Schedule.AddEvent(id, starttime, endtime);
event.Changed := true;
event.LoadFromICalendar(TVpICalEvent(ical[i]));
if (event.Category = 0) and (ADefaultCategory <> 0) then
event.Category := ADefaultCategory;
Result[eventCounter] := event;
inc(eventCounter);
if eventCounter mod BLOCK_SIZE = 0 then
SetLength(Result, eventCounter + BLOCK_SIZE);
end;
SetLength(Result, eventCounter);
if eventCounter > 0 then
FActiveEvent := Result[High(Result)];
Datastore.PostEvents;
Datastore.NotifyDependents;
Invalidate;
finally
ical.Free;
Screen.Cursor := crDefault;
end;
end;
procedure TVpDayView.KeyDown(var Key: Word; Shift: TShiftState); procedure TVpDayView.KeyDown(var Key: Word; Shift: TShiftState);
var var