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

196 lines
4.6 KiB
ObjectPascal
Raw Permalink Normal View History

unit VpImportPreview_VCard;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs,
VpBaseDS, VpImportPreview, VpVCard, Grids;
type
{ TVpImportPreviewVCardForm }
TVpImportPreviewVCardForm = class(TVpImportPreviewForm)
procedure GridGetEditText(Sender: TObject; {%H-}ACol, {%H-}ARow: Integer;
var Value: string);
procedure GridSetEditText(Sender: TObject; {%H-}ACol, {%H-}ARow: Integer;
const Value: string);
private
FVCards: TVpVCards;
FDefaultCategory: String;
function GetCardText(ACard: TVpVCard): String;
procedure SetVCards(const AValue: TVpVCards);
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 VCards: TVpVCards read FVCards write SetVCards;
property DefaultCategory: String read FDefaultCategory write FDefaultCategory;
end;
var
VpImportPreviewVCardForm: TVpImportPreviewVCardForm;
implementation
{$R *.lfm}
uses
VpSR;
constructor TVPImportPreviewVCardForm.Create(AOwner: TComponent);
begin
inherited;
Caption := RSImportVCard;
Grid.OnGetEditText := @GridGetEditText;
Grid.OnSetEditText := @GridSetEditText;
end;
procedure TVpImportPreviewVCardForm.CheckItem(ARow: Integer; AChecked: Boolean);
var
card: TVpVCard;
begin
if ARow < Grid.FixedRows then
exit;
card := TVpVCard(FItems[ARow - Grid.FixedRows]);
if card <> nil then
begin
card.Checked := AChecked;
inherited;
end;
end;
function TVpImportPreviewVCardForm.GetCellText(ACol, ARow: Integer): String;
var
card: TVpVCard;
begin
Result := '';
if (ARow >= Grid.FixedRows) then
begin
card := TVpVCard(FItems[ARow - Grid.FixedRows]);
case ACol of
1: Result := GetCardText(card);
2: Result := Grid.Columns[2].PickList[card.PickedCategory]
end;
end;
end;
function TVpImportPreviewVCardForm.GetCardText(ACard: TVpVCard): String;
var
s: String;
begin
Result := ACard.GetFullName;
s := ACard.GetWorkAddress;
if s <> '' then
Result := Result + LineEnding + RSWorkAddress + ': ' + s;
s := ACard.GetHomeAddress;
if s <> '' then
Result := Result + LineEnding + RSHomeAddress + ': ' + s;
s := ACard.GetPhone;
if s <> '' then
Result := Result + LineEnding + RSPhone + ': ' + s;
s := ACard.GetEMail;
if s <> '' then
Result := Result + LineEnding + RSEMail + ': ' + s;
s := ACard.Categories.CommaText;
if s = '' then s := RSNoneStr;
Result := Result + LineEnding + RSCategoryLabel + ' ' + s;
end;
procedure TVpImportPreviewVCardForm.GridGetEditText(Sender: TObject; ACol,
ARow: Integer; var Value: string);
var
card: TVpVCard;
begin
card := TVpVCard(FItems[Grid.Row - Grid.FixedRows]);
if card <> nil then
Value := Grid.Columns[2].PickList[card.PickedCategory];
end;
procedure TVpImportPreviewVCardForm.GridSetEditText(Sender: TObject; ACol,
ARow: Integer; const Value: string);
var
card: TVpVCard;
begin
card := TVpVCard(FItems[Grid.Row - Grid.FixedRows]);
if card <> nil then
card.PickedCategory := Grid.Columns[2].PickList.IndexOf(Value);;
end;
function TVpImportPreviewVCardForm.IsChecked(ARow: Integer): Boolean;
var
card: TVpVCard;
begin
Result := false;
if ARow < Grid.FixedRows then
exit;
card := TVpVCard(FItems[ARow - Grid.FixedRows]);
if (card <> nil) then
Result := card.Checked;
end;
procedure TVpImportPreviewVCardForm.PrepareItems;
var
i: Integer;
card: TVpVCard;
cat: string;
ct: TVpCategoryType;
L: TStrings;
begin
Grid.Columns[1].Title.Caption := RSContactItems;
Grid.Columns[2].Title.Caption := RSAssignedCategory;
// Populate picklist in column 2
L := TStringList.Create;
try
for ct in TVpCategoryType do
L.Add(CategoryLabel(ct));
Grid.Columns[2].PickList.Assign(L);
finally
L.Free;
end;
FItems.Clear;
if (FVCards <> nil) and (Datastore <> nil) then
for i := 0 to FVCards.Count-1 do
begin
// Add vcard
card := FVCards.Card[i];
FItems.Add(card);
// Select best category in picklist columns
cat := Datastore.FindBestCategory(card.Categories);
if cat = '' then cat := FDefaultCategory;
if cat <> '' then
card.PickedCategory := Grid.Columns[2].PickList.IndexOf(cat)
else
card.PickedCategory := ord(ctOther);
end;
inherited;
end;
procedure TVpImportPreviewVCardForm.SetVCards(const AValue: TVpVCards);
begin
if AValue <> FVCards then
begin
FVCards := AValue;
PrepareItems;
end;
end;
end.