Changeset 168 for trunk/Packages/VCard/VCard.pas
- Timestamp:
- Jul 1, 2023, 8:17:50 PM (17 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/VCard/VCard.pas
r152 r168 4 4 5 5 uses 6 Classes, SysUtils, Dialogs, LazUTF8, Base64, Graphics, Common, 6 Classes, SysUtils, Dialogs, LazUTF8, Base64, Graphics, Common, Table, 7 7 Generics.Collections, Generics.Defaults, ListViewSort; 8 8 … … 109 109 function GetBySysNameGroups(SysName: string; Groups: TStringArray): TContactField; 110 110 function GetByIndex(Index: TContactFieldIndex): TContactField; 111 function GetByTitle(Title: string): TContactField; 111 112 procedure LoadToStrings(AItems: TStrings); 112 113 end; … … 235 236 procedure SaveToFile(FileName: string); 236 237 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); 237 244 constructor Create(AOwner: TComponent); override; 238 245 destructor Destroy; override; … … 642 649 end; 643 650 651 procedure TVCard.ExportToStrings(Lines: TStrings; Format: TTableFormat; 652 HumanReadableHeader: Boolean); 653 var 654 Table: TTable; 655 begin 656 Table := TTable.Create; 657 try 658 ExportToTable(Table, HumanReadableHeader); 659 Lines.Text := Table.GetOutput(Format); 660 finally 661 FreeAndNil(Table); 662 end; 663 end; 664 665 procedure TVCard.ImportFromStrings(Lines: TStrings; Format: TTableFormat; 666 HumanReadableHeader: Boolean); 667 var 668 Table: TTable; 669 begin 670 Table := TTable.Create; 671 try 672 Table.SetInput(Format, Lines.Text); 673 ImportFromTable(Table, HumanReadableHeader); 674 finally 675 FreeAndNil(Table); 676 end; 677 end; 678 679 procedure TVCard.ExportToFile(FileName: string; Format: TTableFormat; 680 HumanReadableHeader: Boolean); 681 var 682 Lines: TStringList; 683 begin 684 Lines := TStringList.Create; 685 try 686 ExportToStrings(Lines, Format, HumanReadableHeader); 687 Lines.SaveToFile(FileName); 688 finally 689 Lines.Free; 690 end 691 end; 692 693 procedure TVCard.ImportFromFile(FileName: string; Format: TTableFormat; 694 HumanReadableHeader: Boolean); 695 var 696 Lines: TStringList; 697 begin 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; 713 end; 714 715 procedure TVCard.ExportToTable(Table: TTable; HumanReadableHeader: Boolean); 716 var 717 Row: TRow; 718 I: Integer; 719 J: Integer; 720 Values: TStringList; 721 Index: Integer; 722 Fields: TContactFields; 723 Field: TContactField; 724 Columns: TStringList; 725 begin 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; 767 end; 768 769 procedure TVCard.ImportFromTable(Table: TTable; HumanReadableHeader: Boolean); 770 var 771 Contact: TContact; 772 I: Integer; 773 J: Integer; 774 Fields: TContactFields; 775 Field: TContactField; 776 begin 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; 791 end; 792 644 793 constructor TVCard.Create(AOwner: TComponent); 645 794 begin … … 1445 1594 else Result := nil; 1446 1595 end; 1596 end; 1597 1598 function TContactFields.GetByTitle(Title: string): TContactField; 1599 var 1600 I: Integer; 1601 begin 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; 1447 1606 end; 1448 1607
Note:
See TracChangeset
for help on using the changeset viewer.