Ignore:
Timestamp:
Jul 1, 2023, 8:17:50 PM (11 months ago)
Author:
chronos
Message:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/VCard/VCard.pas

    r152 r168  
    44
    55uses
    6   Classes, SysUtils, Dialogs, LazUTF8, Base64, Graphics, Common,
     6  Classes, SysUtils, Dialogs, LazUTF8, Base64, Graphics, Common, Table,
    77  Generics.Collections, Generics.Defaults, ListViewSort;
    88
     
    109109    function GetBySysNameGroups(SysName: string; Groups: TStringArray): TContactField;
    110110    function GetByIndex(Index: TContactFieldIndex): TContactField;
     111    function GetByTitle(Title: string): TContactField;
    111112    procedure LoadToStrings(AItems: TStrings);
    112113  end;
     
    235236    procedure SaveToFile(FileName: string);
    236237    procedure LoadFromFile(FileName: string);
     238    procedure ExportToStrings(Lines: TStrings; Format: TTableFormat; HumanReadableHeader: Boolean);
     239    procedure ImportFromStrings(Lines: TStrings; Format: TTableFormat; HumanReadableHeader: Boolean);
     240    procedure ExportToFile(FileName: string; Format: TTableFormat; HumanReadableHeader: Boolean);
     241    procedure ImportFromFile(FileName: string; Format: TTableFormat; HumanReadableHeader: Boolean);
     242    procedure ExportToTable(Table: TTable; HumanReadableHeader: Boolean);
     243    procedure ImportFromTable(Table: TTable; HumanReadableHeader: Boolean);
    237244    constructor Create(AOwner: TComponent); override;
    238245    destructor Destroy; override;
     
    642649end;
    643650
     651procedure TVCard.ExportToStrings(Lines: TStrings; Format: TTableFormat;
     652  HumanReadableHeader: Boolean);
     653var
     654  Table: TTable;
     655begin
     656  Table := TTable.Create;
     657  try
     658    ExportToTable(Table, HumanReadableHeader);
     659    Lines.Text := Table.GetOutput(Format);
     660  finally
     661    FreeAndNil(Table);
     662  end;
     663end;
     664
     665procedure TVCard.ImportFromStrings(Lines: TStrings; Format: TTableFormat;
     666  HumanReadableHeader: Boolean);
     667var
     668  Table: TTable;
     669begin
     670  Table := TTable.Create;
     671  try
     672    Table.SetInput(Format, Lines.Text);
     673    ImportFromTable(Table, HumanReadableHeader);
     674  finally
     675    FreeAndNil(Table);
     676  end;
     677end;
     678
     679procedure TVCard.ExportToFile(FileName: string; Format: TTableFormat;
     680  HumanReadableHeader: Boolean);
     681var
     682  Lines: TStringList;
     683begin
     684  Lines := TStringList.Create;
     685  try
     686    ExportToStrings(Lines, Format, HumanReadableHeader);
     687    Lines.SaveToFile(FileName);
     688  finally
     689    Lines.Free;
     690  end
     691end;
     692
     693procedure TVCard.ImportFromFile(FileName: string; Format: TTableFormat;
     694  HumanReadableHeader: Boolean);
     695var
     696  Lines: TStringList;
     697begin
     698  Lines := TStringList.Create;
     699  Lines.LoadFromFile(FileName);
     700  {$IF FPC_FULLVERSION>=30200}
     701  if (Length(Lines.Text) > 0) and (Pos(VCardBegin, Lines.Text) = 0) then begin
     702    Lines.LoadFromFile(FileName, TEncoding.Unicode);
     703    if (Length(Lines.Text) > 0) and (Pos(VCardBegin, Lines.Text) = 0) then begin
     704      Lines.LoadFromFile(FileName, TEncoding.BigEndianUnicode);
     705    end;
     706  end;
     707  {$ENDIF}
     708  try
     709    ImportFromStrings(Lines, Format, HumanReadableHeader);
     710  finally
     711    Lines.Free;
     712  end;
     713end;
     714
     715procedure TVCard.ExportToTable(Table: TTable; HumanReadableHeader: Boolean);
     716var
     717  Row: TRow;
     718  I: Integer;
     719  J: Integer;
     720  Values: TStringList;
     721  Index: Integer;
     722  Fields: TContactFields;
     723  Field: TContactField;
     724  Columns: TStringList;
     725begin
     726  Table.Clear;
     727
     728  Values := TStringList.Create;
     729  Columns := TStringList.Create;
     730  try
     731      // Get all properties types
     732      for I := 0 to Contacts.Count - 1 do begin
     733        for J := 0 to Contacts[I].Properties.Count - 1 do
     734        if not Contacts[I].Properties[J].Name.StartsWith('PHOTO') and
     735          (Table.Columns.IndexOf(Contacts[I].Properties[J].Name) = -1) then begin
     736            Table.Columns.Add(Contacts[I].Properties[J].Name);
     737            Columns.Add(Contacts[I].Properties[J].Name);
     738          end;
     739      end;
     740
     741      if HumanReadableHeader then begin
     742        Fields := TContact.GetFields;
     743        for I := 0 to Table.Columns.Count - 1 do begin
     744          Field := Fields.GetBySysName(Table.Columns[I]);
     745          if Assigned(Field) then Table.Columns[I] := Field.Title;
     746        end;
     747      end;
     748
     749      for I := 0 to Contacts.Count - 1 do begin
     750        Values.Clear;
     751        for J := 0 to Columns.Count - 1 do
     752          Values.Add('');
     753        for J := 0 to Contacts[I].Properties.Count - 1 do begin
     754          Index := Columns.IndexOf(Contacts[I].Properties[J].Name);
     755          if Index <> -1 then
     756            Values[Index] := Contacts[I].Properties[J].Value;
     757        end;
     758
     759        Row := Table.AddRow;
     760        for J := 0 to Values.Count - 1 do
     761          Row.Cells.Add(Values[J]);
     762      end;
     763  finally
     764    Values.Free;
     765    Columns.Free;
     766  end;
     767end;
     768
     769procedure TVCard.ImportFromTable(Table: TTable; HumanReadableHeader: Boolean);
     770var
     771  Contact: TContact;
     772  I: Integer;
     773  J: Integer;
     774  Fields: TContactFields;
     775  Field: TContactField;
     776begin
     777  if HumanReadableHeader then begin
     778    Fields := TContact.GetFields;
     779    for I := 0 to Table.Columns.Count - 1 do begin
     780      Field := Fields.GetByTitle(Table.Columns[I]);
     781      if Assigned(Field) then Table.Columns[I] := Field.SysName;
     782    end;
     783  end;
     784
     785  Contacts.Clear;
     786  for I := 0 to Table.Rows.Count - 1 do begin
     787    Contact := Contacts.AddNew;
     788    for J := 0 to Table.Rows[I].Cells.Count - 1 do
     789      Contact.Properties.AddNew(Table.Columns[J], Table.Rows[I].Cells[J]);
     790  end;
     791end;
     792
    644793constructor TVCard.Create(AOwner: TComponent);
    645794begin
     
    14451594      else Result := nil;
    14461595  end;
     1596end;
     1597
     1598function TContactFields.GetByTitle(Title: string): TContactField;
     1599var
     1600  I: Integer;
     1601begin
     1602  I := 0;
     1603  while (I < Count) and (Items[I].Title <> Title) do Inc(I);
     1604  if I < Count then Result := Items[I]
     1605    else Result := nil;
    14471606end;
    14481607
Note: See TracChangeset for help on using the changeset viewer.