tvplanit: Export contacts as vcard files. Fix some bugs in vcard import.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@8395 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2022-08-18 17:44:47 +00:00
parent 0afcfc7a66
commit 83950d045a
17 changed files with 512 additions and 176 deletions

View File

@ -47,6 +47,9 @@ type
FCarPhone: String;
FISDN: String;
FPager: String;
FBirthDay: TDateTime;
FAnniversary: TDateTime;
FSkip: Boolean;
public
@ -60,36 +63,41 @@ type
function GetPhone: String;
function GetWorkAddress: String;
property FirstName: String read FFirstName;
property LastName: String read FLastName;
property Title: String read FTitle;
procedure SaveToStrings(AList: TStrings);
property Company: String read FCompany;
property WorkAddress: String read FWorkAddress;
property WorkCity: String read FWorkCity;
property WorkZip: String read FWorkZip;
property WorkState: String read FWorkState;
property WorkCountry: String read FWorkCountry;
property WorkEMail: String read FWorkEMail;
property WorkPhone: String read FWorkPhone;
property WorkFax: String read FWorkFax;
property FirstName: String read FFirstName write FFirstName;
property LastName: String read FLastName write FLastName;
property Title: String read FTitle write FTitle;
property HomeAddress: String read FHomeAddress;
property HomeCity: String read FHomeCity;
property HomeZip: String read FHomeZip;
property HomeState: String read FHomeState;
property HomeCountry: String read FHomeCountry;
property HomeEMail: String read FHomeEMail;
property HomePhone: String read FHomePhone;
property HomeFax: String read FHomeFax;
property Company: String read FCompany write FCompany;
property WorkAddress: String read FWorkAddress write FWorkAddress;
property WorkCity: String read FWorkCity write FWorkCity;
property WorkZip: String read FWorkZip write FWorkZip;
property WorkState: String read FWorkState write FWorkState;
property WorkCountry: String read FWorkCountry write FWorkCountry;
property WorkEMail: String read FWorkEMail write FWorkEMail;
property WorkPhone: String read FWorkPhone write FWorkPhone;
property WorkFax: String read FWorkFax write FWorkFax;
property CarPhone: String read FCarPhone;
property Mobile: String read FMobile;
property ISDN: String read FISDN;
property Pager: String read FPager;
property HomeAddress: String read FHomeAddress write FHomeAddress;
property HomeCity: String read FHomeCity write FHomeCity;
property HomeZip: String read FHomeZip write FHomeZip;
property HomeState: String read FHomeState write FHomeState;
property HomeCountry: String read FHomeCountry write FHomeCountry;
property HomeEMail: String read FHomeEMail write FHomeEMail;
property HomePhone: String read FHomePhone write FHomePhone;
property HomeFax: String read FHomeFax write FHomeFax;
property CarPhone: String read FCarPhone write FCarPhone;
property Mobile: String read FMobile write FMobile;
property ISDN: String read FISDN write FISDN;
property Pager: String read FPager write FPager;
property BirthDay: TDateTime read FBirthday write FBirthDay;
property Anniversary: TDateTime read FAnniversary write FAnniversary;
property Version: String read FVersion;
property Skip: Boolean read FSkip write FSkip;
property Skip: Boolean read FSkip write FSkip; // Flag to skip import
end;
TVpVCards = class
@ -99,12 +107,17 @@ type
function GetCount: Integer;
protected
procedure LoadFromStrings(const AStrings: TStrings);
procedure SaveToStrings(AList: TStrings);
public
constructor Create;
destructor Destroy; override;
procedure Add(ACard: TVpVCard);
procedure ClearCards;
procedure LoadFromFile(const AFileName: String);
procedure LoadFromStream(const AStream: TStream);
procedure SaveToFile(const AFileName: String);
procedure SaveToStream(const AStream: TStream);
property Count: Integer read GetCount;
property Card[AIndex: Integer]: TVpVCard read GetCard; default;
end;
@ -203,6 +216,8 @@ var
item: TVpVCardItem;
fn, ln, t: String;
fullName: String;
fs: TFormatSettings;
dt: TDateTime;
begin
inherited;
@ -221,7 +236,11 @@ begin
if FTitle = '' then FTitle := t;
end;
'ORG':
FCompany := item.Value;
begin
FCompany := item.Value;
if (FCompany <> '') and (FCompany[Length(FCompany)] = ';') then
Delete(FCompany, Length(FCompany), 1);
end;
'ADR':
if item.Attributes.IndexOf('WORK') <> -1 then
VCardAddress(item.Value, FWorkAddress, FWorkCity, FWorkZip, FWorkState, FWorkCountry)
@ -233,10 +252,13 @@ begin
else
VCardAddress(item.Value, FWorkAddress, FWorkCity, FWorkZip, FWorkState, FWorkCountry);
'EMAIL':
if (FCompany = '') or (item.Attributes.IndexOf('HOME') <> -1) then
FHomeEMail := IfThen(FHomeEMail = '', item.Value, FHomeEMail + ITEM_SEPARATOR + item.Value)
if (item.Attributes.IndexOf('WORK') <> -1) then
FWorkEMail := IfThen(FWorkEMail = '', item.Value, FWorkEMail + ITEM_SEPARATOR + ' ' + item.Value)
else
FWorkEMail := IfThen(FWorkEMail = '', item.Value, FWorkEMail + ITEM_SEPARATOR + item.Value);
if (FCompany = '') or (item.Attributes.IndexOf('HOME') <> -1) then
FHomeEMail := IfThen(FHomeEMail = '', item.Value, FHomeEMail + ITEM_SEPARATOR + ' ' + item.Value)
else
FWorkEMail := IfThen(FWorkEMail = '', item.Value, FWorkEMail + ITEM_SEPARATOR + ' ' + item.Value);
'TEL':
if item.Attributes.IndexOf('CELL') <> -1 then
FMobile := item.Value
@ -256,10 +278,26 @@ begin
if item.Attributes.IndexOf('ISDN') <> -1 then
FISDN := item.Value
else
if (FCompany = '') or (item.Attributes.IndexOf('HOME') <> -1) then
FHomePhone := IfThen(FHomePhone = '', item.Value, FHomePhone + ITEM_SEPARATOR + item.Value)
if item.Attributes.IndexOf('WORK') <> -1 then
FWorkPhone := IfThen(FWorkPhone = '', item.Value, FWorkPhone + ITEM_SEPARATOR + ' ' + item.Value)
else
FWorkPhone := IfThen(FWorkPhone = '', item.Value, FWorkPhone + ITEM_SEPARATOR + item.Value);
if (item.Attributes.IndexOf('HOME') <> -1) then
FHomePhone := IfThen(FHomePhone = '', item.Value, FHomePhone + ITEM_SEPARATOR + ' ' +item.Value)
else
FWorkPhone := IfThen(FWorkPhone = '', item.Value, FWorkPhone + ITEM_SEPARATOR + ' ' + item.Value);
'BDAY', 'ANNIVERSARY':
begin
fs := FormatSettings;
fs.DateSeparator := '-';
fs.ShortDateFormat := 'yyyy/mm/dd';
if TryStrToDate(item.Value, dt) then
begin
if (item.Key = 'BDAY') then
FBirthday := dt
else if (item.Key = 'ANNIVERSARY') then
FAnniversary := dt;
end;
end;
end;
end;
@ -277,19 +315,12 @@ begin
end;
function TVpVCard.GetEMail: String;
var
sw, sh: String;
begin
if WorkEMail <> '' then sw := Format('%s (work)', [WorkEMail]) else sw := '';
if HomeEMail <> '' then sh := Format('%s (home)', [HomeEMail]) else sh := '';
if (sw <> '') and (sh <> '') then
Result := sw + '; ' + sh
else if (sw <> '') then
Result := sw
else if (sh <> '') then
Result := sh
else
Result := '';
Result := '';
if WorkEMail <> '' then Result := Format('%s; %s (work)', [Result, WorkEMail]);
if HomeEMail <> '' then Result := Format('%s; %s (home)', [Result, HomeEMail]);
if Result <> '' then
Delete(Result, 1, 2);
end;
function TVpVCard.GetFullName: String;
@ -312,19 +343,12 @@ begin
end;
function TVpVCard.GetPhone: String;
var
sw, sh: String;
begin
if WorkPhone <> '' then sw := Format('%s (work)', [WorkPhone]) else sw := '';
if HomePhone <> '' then sh := Format('%s (home)', [HomePhone]) else sh := '';
if (sw <> '') and (sh <> '') then
Result := sw + '; ' + sh
else if (sw <> '') then
Result := sw
else if (sh <> '') then
Result := sh
else
Result := '';
Result := '';
if WorkPhone <> '' then Result := Format('%s; %s (work)', [Result, WorkPhone]);
if HomePhone <> '' then Result := Format('%s; %s (home)', [Result, HomePhone]);
if Mobile <> '' then Result := Format('%s; %s (mobile)', [Result, Mobile]);
if Result <> '' then Delete(Result, 1, 2);
end;
function TVpVCard.GetWorkAddress: String;
@ -339,33 +363,63 @@ begin
if WorkCountry <> '' then
Result := Result + ', ' + WorkCountry;
end;
(*
function GetHomeAddress: String;
function GetWorkAddress: String;
property FirstName: String read FFirstName;
property LastName: String read FLastName;
property Title: String read FTitle;
procedure TVpVCard.SaveToStrings(AList: TStrings);
var
s: String;
begin
AList.Add('BEGIN:VCARD');
AList.Add('VERSION:3.0');
property Company: String read FCompany;
property WorkAddress: String read FWorkAddress;
property WorkCity: String read FWorkCity;
property WorkZip: String read FWorkZip;
property WorkState: String read FWorkState;
property WorkCountry: String read FWorkCountry;
property WorkEMail: String read FWorkEMail;
property WorkPhone: String read FWorkPhone;
property WorkFax: String read FWorkFax;
// Name
AList.Add('FN:' + GetFullName);
AList.Add('N:' + LastName + ';' + FirstName + ';;;');
// Addresses
if (WorkAddress <> '') or (WorkCity <> '') or (WorkZip <> '') or
(WorkState <> '') or (WorkCountry <> '')
then
AList.Add('ADR;TYPE=WORK:' + WorkAddress + ';' + WorkCity + ';' +
WorkState +';' + WorkZip + ';' + WorkCountry);
if (HomeAddress <> '') or (HomeCity <> '') or (HomeZip <> '') or
(HomeState <> '') or (HomeCountry <> '')
then
AList.Add('ADR;TYPE=HOME:' + HomeAddress + ';' + HomeCity + ';' +
HomeState +';' + HomeZip + ';' + HomeCountry);
// Company
if Company <> '' then
AList.Add('ORG:' + Company);
// Phone numbers
if WorkPhone <> '' then
AList.Add('TEL;TYPE=WORK:' + WorkPhone);
if HomePhone <> '' then
AList.Add('TEL;TYPE=HOME:' + HomePhone);
if Mobile <> '' then
AList.Add('TEL;TYPE=CELL:' + Mobile);
if CarPhone <> '' then
AList.Add('TEL;TYPE=CAR:' + CarPhone);
if ISDN <> '' then
AList.Add('TEL;TYPE=ISDN:' + ISDN);
if Pager <> '' then
AList.Add('TEL;TYPE=PAGER:' + Pager);
// Fax
if WorkFax <> '' then
AList.Add('TEL;TYPE="fax,work":' + WorkFax);
if HomeFax <> '' then
AList.Add('TEL;TYPE="fax,home":' + HomeFax);
// E-Mail
if WorkEMail <> '' then
AList.Add('EMAIL;TYPE=WORK:' + WorkEMail);
if HomeEMail <> '' then
AList.Add('EMAIL;TYPE=HOME:' + HomeEMail);
AList.Add('END:VCARD');
end;
property HomeAddress: String read FHomeAddress;
property HomeCity: String read FHomeCity;
property HomeZip: String read FHomeZip;
property HomeState: String read FHomeState;
property HomeCountry: String read FHomeCountry;
property HomeEMail: String read FHomeEMail;
property HomePhone: String read FHomePhone;
property HomeFax: String read FHomeFax;
*)
{==============================================================================}
{ TVpCards }
@ -383,6 +437,15 @@ begin
inherited;
end;
procedure TVpVCards.Add(ACard: TVpVCard);
var
n: Integer;
begin
n := Length(FCards);
SetLength(FCards, n+1);
FCards[n] := ACard;
end;
procedure TVpVCards.ClearCards;
var
j: Integer;
@ -466,5 +529,40 @@ begin
SetLength(FCards, n);
end;
procedure TVpVCards.SaveToFile(const AFileName: String);
var
L: TStrings;
begin
L := TStringList.Create;
try
SaveToStrings(L);
L.SaveToFile(AFileName);
finally
L.Free;
end;
end;
procedure TVpVCards.SaveToStream(const AStream: TStream);
var
L: TStrings;
begin
L := TStringList.Create;
try
SaveToStrings(L);
L.SaveToStream(AStream);
finally
L.Free;
end;
end;
procedure TVpVCards.SaveToStrings(AList: TStrings);
var
i: Integer;
begin
AList.clear;
for i := 0 to Count-1 do
FCards[i].SaveToStrings(AList);
end;
end.