From 5706fa833b06352e05fb0a39bd7cdad182b06bd8 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Sat, 16 Jun 2018 23:07:26 +0000 Subject: [PATCH] tvplanit: Import event category from ical file. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@6501 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- components/tvplanit/source/vpbase.pas | 52 ++++++++++++++++++++----- components/tvplanit/source/vpbaseds.pas | 2 +- components/tvplanit/source/vpdata.pas | 31 ++++++++++++++- components/tvplanit/source/vpical.pas | 34 +++++++++++++++- 4 files changed, 105 insertions(+), 14 deletions(-) diff --git a/components/tvplanit/source/vpbase.pas b/components/tvplanit/source/vpbase.pas index 4cb16a4e2..d338cd011 100644 --- a/components/tvplanit/source/vpbase.pas +++ b/components/tvplanit/source/vpbase.pas @@ -241,9 +241,12 @@ type public constructor Create(AOwner: TComponent); destructor Destroy; override; - function GetColor(Index: Integer): TColor; - function GetName(Index: Integer):string; + function GetColor(AIndex: Integer): TColor; + function GetName(AIndex: Integer):string; function GetCategory(AIndex: Integer): TVpCategoryInfo; + function IndexOfCategory(AName: String): Integer; + function IndexOfFirstUnusedCategory: Integer; + procedure SetCategoryName(AIndex: Integer; AName: String); published property Category0: TVpCategoryInfo index 0 read GetCategory write SetCat; property Category1: TVpCategoryInfo index 1 read GetCategory write SetCat; @@ -713,7 +716,7 @@ var begin inherited Create; FOwner := AOwner; - for i:=0 to High(FCat) do + for i:=Low(FCat) to High(FCat) do begin FCat[i] := TVpCategoryInfo.Create(FOwner); FCat[i].FIndex := i; @@ -726,7 +729,7 @@ destructor TVpCategoryColorMap.Destroy; var i: Integer; begin - for i:=0 to High(FCat) do FCat[i].Free; + for i:=Low(FCat) to High(FCat) do FCat[i].Free; inherited; end; @@ -735,18 +738,42 @@ begin Result := FCat[AIndex]; end; -function TVpCategoryColorMap.GetColor(Index: Integer): TColor; +function TVpCategoryColorMap.IndexOfCategory(AName: String): Integer; +var + i: Integer; begin - if Index <= High(FCat) then - Result := FCat[Index].Color + for i:=Low(FCat) to High(FCat) do + if SameText(FCat[i].Description, AName) then begin + Result := i; + exit; + end; + Result := -1; +end; + +function TVpCategoryColorMap.IndexOfFirstUnusedCategory: Integer; +var + i: Integer; +begin + for i := Low(FCat) to High(FCat) do + if FCat[i].Description = Format(RSCategoryDesc, [i]) then begin + Result := i; + exit; + end; + Result := -1; +end; + +function TVpCategoryColorMap.GetColor(AIndex: Integer): TColor; +begin + if (AIndex >= Low(FCat)) and (AIndex <= High(FCat)) then + Result := FCat[AIndex].Color else Result := clBlack; end; -function TVpCategoryColorMap.GetName(Index: Integer): string; +function TVpCategoryColorMap.GetName(AIndex: Integer): string; begin - if Index <= High(FCat) then - Result := FCat[Index].Description + if (AIndex >= Low(FCat)) and (AIndex <= High(FCat)) then + Result := FCat[AIndex].Description else Result := ''; end; @@ -756,6 +783,11 @@ begin FCat[AIndex] := AValue; end; +procedure TVpCategoryColorMap.SetCategoryName(AIndex: Integer; AName: String); +begin + if (AIndex >= Low(FCat)) and (AIndex <= High(FCat)) then + FCat[AIndex].Description := AName; +end; (*****************************************************************************) { TVpCategoryInfo } diff --git a/components/tvplanit/source/vpbaseds.pas b/components/tvplanit/source/vpbaseds.pas index 56483b1ef..aebc0bf2c 100644 --- a/components/tvplanit/source/vpbaseds.pas +++ b/components/tvplanit/source/vpbaseds.pas @@ -233,7 +233,7 @@ type FResource : TVpResource; dsAlertTimer : TTimer; { fires the alerts } FNotifiers : TList; - FLinkedOwner: TComponent; + FLinkedOwner : TComponent; {events} FOnConnect : TNotifyEvent; diff --git a/components/tvplanit/source/vpdata.pas b/components/tvplanit/source/vpdata.pas index 8e8e123ec..eed678be1 100644 --- a/components/tvplanit/source/vpdata.pas +++ b/components/tvplanit/source/vpdata.pas @@ -77,7 +77,7 @@ type TVpResources = class private - FOwner: TObject; + FOwner: TObject; // This is the Datastore. FResourceGroups: TList; function GetCount: Integer; function GetItem(Index: Integer): TVpResource; @@ -695,7 +695,7 @@ implementation uses Math, DateUtils, - VpException, VpConst, VpMisc; + VpException, VpConst, VpMisc, VpBaseDS; const TIME_EPS = 1.0 / SecondsInDay; // Epsilon for comparing times @@ -1269,6 +1269,9 @@ end; procedure TVpEvent.LoadFromICalendar(AEntry: TVpICalEvent); var dt: Double; + cat: String; + i, j, k: Integer; + datastore: TVpCustomDatastore; begin if AEntry = nil then exit; @@ -1279,6 +1282,30 @@ begin FLocation := AEntry.Location; // Start and end time already have been set --> Skip . + { Category } + { tvplanit has only 1 category, ical may have several. We pick the first one + defined in the datastore. If none is defined we create the first one. } + if AEntry.CategoryCount > 0 then begin + datastore := TVpCustomDatastore(Owner.Owner.Owner.Owner); + k := -1; + for i := 0 to AEntry.CategoryCount-1 do begin + cat := AEntry.category[i]; + j := datastore.CategoryColorMap.IndexOfCategory(cat); + if j <> -1 then begin + k := j; + break; + end; + end; + if k = -1 then begin // category not found in data store + k := datastore.CategoryColorMap.IndexOfFirstUnusedCategory; + if k <> -1 then + datastore.CategoryColorMap.SetCategoryName(k, AEntry.Category[0]); + end; + if k <> -1 then + FCategory := k; + end; + + { All-day event } FAllDayEvent := (frac(FStartTime) = 0) and (frac(FEndTime) = 0); diff --git a/components/tvplanit/source/vpical.pas b/components/tvplanit/source/vpical.pas index 867e7b725..262da4652 100644 --- a/components/tvplanit/source/vpical.pas +++ b/components/tvplanit/source/vpical.pas @@ -19,7 +19,7 @@ type private FCalendar: TVpICalendar; public - constructor Create(ACalendar: TVpICalendar); + constructor Create(ACalendar: TVpICalendar); virtual; function FindItem(AKey: String): TVpICalItem; end; @@ -46,6 +46,8 @@ type property AudioSrc: String read FAudioSrc; end; + { TVpICalEvent } + TVpICalEvent = class(TVpICalEntry) private FSummary: String; // --> Description @@ -62,9 +64,13 @@ type FRecurrenceCount: Integer; FRecurrenceByXXX: String; FAlarm: TVpICalAlarm; + FCategories: TStrings; + function GetCategory(AIndex: Integer): String; + function GetCategoryCount: Integer; function GetEndTime(UTC: Boolean): TDateTime; function GetStartTime(UTC: Boolean): TDateTime; public + constructor Create(ACalendar: TVpICalendar); override; destructor Destroy; override; procedure Analyze; override; procedure UseAlarm; @@ -73,6 +79,8 @@ type property Location: String read FLocation; property StartTime[UTC: Boolean]: TDateTime read GetStartTime; property EndTime[UTC: Boolean]: TDateTime read GetEndTime; + property Category[AIndex: Integer]: String read GetCategory; + property CategoryCount: Integer read GetCategoryCount; property Alarm: TVpICalAlarm read FAlarm; property RecurrenceFrequency: String read FRecurrenceFreq; property RecurrenceInterval: Integer read FRecurrenceInterval; @@ -287,8 +295,17 @@ end; { TVpICalEvent } {==============================================================================} +constructor TVpICalEvent.Create(ACalendar: TVpICalendar); +begin + inherited; + FCategories := TStringList.Create; + FCategories.Delimiter := VALUE_DELIMITER; + FCategories.StrictDelimiter := True; +end; + destructor TVpICalEvent.Destroy; begin + FCategories.Free; FAlarm.Free; inherited; end; @@ -328,6 +345,8 @@ begin FLocation := item.Value; 'DURATION': FDuration := ICalDuration(item.Value); + 'CATEGORIES': + FCategories.DelimitedText := item.Value; 'RRULE': begin L := TStringList.Create; @@ -354,6 +373,19 @@ begin end; end; +function TVpICalEvent.GetCategory(AIndex: Integer): String; +begin + if (AIndex >= 0) and (AIndex < FCategories.Count) then + Result := FCategories[AIndex] + else + Result := ''; +end; + +function TVpICalEvent.GetCategoryCount: Integer; +begin + Result := FCategories.Count; +end; + function TVpICalEvent.GetEndTime(UTC: Boolean): TDateTime; begin if FEndTime <> 0 then