unit VpImportPreview_ICalTask; {$mode objfpc}{$H+} interface uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs, VpData, VpBaseDS, VpImportPreview, VpICal, Grids; type { TVpImportPreviewICalTaskForm } TVpImportPreviewICalTaskForm = 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; FDatastore: TVpCustomDatastore; FDefaultCategory: String; FTimeFormat: String; function GetTaskText(AToDo: TVpICalToDo): 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 VpImportPreviewICalTaskForm: TVpImportPreviewICalTaskForm; implementation {$R *.lfm} uses VpSR, VpConst; constructor TVPImportPreviewICalTaskForm.Create(AOwner: TComponent); begin inherited; Caption := RSImportICalTask; FTimeFormat := 'c'; // short date + long time format end; procedure TVpImportPreviewICalTaskForm.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 TVpImportPreviewICalTaskForm.GetCellText(ACol, ARow: Integer): String; var task: TVpICalToDo; begin Result := ''; if (ARow >= Grid.FixedRows) then begin task := TVpICalToDo(FItems[ARow - Grid.FixedRows]); case ACol of 1: Result := GetTaskText(task); 2: Result := Grid.Columns[2].PickList[task.PickedCategory] end; end; end; procedure TVpImportPreviewICalTaskForm.GridGetEditText(Sender: TObject; ACol, ARow: Integer; var Value: string); var task: TVpICalToDo; begin task := TVpICalToDo(FItems[Grid.Row - Grid.FixedRows]); if task <> nil then Value := Grid.Columns[2].PickList[task.PickedCategory]; end; procedure TVpImportPreviewICalTaskForm.GridSetEditText(Sender: TObject; ACol, ARow: Integer; const Value: string); var task: TVpICalToDo; begin task:= TVpICalToDo(FItems[Grid.Row - Grid.FixedRows]); if task <> nil then task.PickedCategory := Grid.Columns[2].PickList.IndexOf(Value);; end; function TVpImportPreviewICalTaskForm.GetTaskText(AToDo: TVpICalToDo): String; var s: String; dt: TDateTime; begin Result := RSDescriptionLbl + ' ' + AToDo.Summary; dt := AToDo.DueTime[false]; if dt <> NO_DATE then s := FormatDateTime(FTimeFormat, dt) else s := RSNoneStr; Result := Result + LineEnding + RSDueDateLabel + ' ' + s; dt := AToDo.StartTime[false]; if dt <> NO_DATE then s := FormatDateTime(FTimeFormat, dt) else s := RSNoneStr; Result := Result + LineEnding + RSCreatedOn + ' ' + s; case TVpTask.GetTaskPriority(AToDo.Priority) of tpLow: s := RSLow; tpNormal: s := RSNormal; tpHigh: s := RSHigh; end; Result := Result + LineEnding + RSPriorityLabel + ' ' + s; s := AToDo.Categories.CommaText; if s = '' then s := RSNoneStr; Result := Result + LineEnding + RSCategoryLabel + ' ' + s; end; function TVpImportPreviewICalTaskForm.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 TVpImportPreviewICalTaskForm.PrepareItems; var i: Integer; ct: TVpCategoryType; cat: String; L: TStrings; task: TVpICalToDo; begin FItems.Clear; if FCalendar <> nil then for i := 0 to FCalendar.Count-1 do if (FCalendar.Entry[i] is TVpICalToDo) then FItems.Add(FCalendar.Entry[i]); inherited; if (FCalendar <> nil) and (FDataStore <> nil) and (Grid.Columns.Count = 2) then begin Grid.Columns[1].Title.Caption := RSTaskItems; with Grid.Columns.Add do begin SizePriority := 0; Width := 160; Title.Caption := RSAssignedCategory; ButtonStyle := cbsPickList; L := TStringList.Create; try for ct in TVpCategoryType do L.Add(CategoryLabel(ct)); PickList.Assign(L); finally L.Free; end; end; for i := 0 to FItems.Count-1 do begin task := TVpICalToDo(FItems[i]); cat := FDatastore.FindBestTaskCategory(task.Categories); if cat = '' then cat := FDefaultCategory; if cat <> '' then task.PickedCategory := Grid.Columns[2].PickList.IndexOf(cat) else task.PickedCategory := ord(ctOther); end; end; end; procedure TVpImportPreviewICalTaskForm.SetCalendar(const AValue: TVpICalendar); begin if AValue <> FCalendar then begin FCalendar := AValue; PrepareItems; end; end; end.