Changeset 73 for trunk/UContact.pas


Ignore:
Timestamp:
Dec 13, 2021, 11:33:11 PM (2 years ago)
Author:
chronos
Message:
  • Added: Copy, cut and paste context menu action in contacts list.
  • Modified: Merge multiple files action replaced by Combine action. During Combine action files are simply added into final contacts list even with duplicate contacts.
  • Modified: Added Merge button into Find duplicate window to merge contacts by selected contact field.
  • Modified: Show only used contact fields in Find duplicates window.
  • Fixed: Wrong items were removed if contacts and properties lists were in filtered state.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UContact.pas

    r72 r73  
    9393
    9494  TContactProperties = class(TFPGObjectList<TContactProperty>)
     95    procedure Assign(Source: TContactProperties);
    9596    procedure AssignToList(List: TFPGObjectList<TObject>);
    9697    function GetByName(Name: string): TContactProperty;
     
    110111    Properties: TContactProperties;
    111112    Parent: TContactsFile;
     113    function HasField(FieldIndex: TContactFieldIndex): Boolean;
    112114    function FullNameToFileName: string;
    113115    function GetProperty(Field: TContactField): TContactProperty; overload;
     
    128130  TContacts = class(TFPGObjectList<TContact>)
    129131    ContactsFile: TContactsFile;
     132    procedure Assign(Source: TContacts);
     133    procedure AddContacts(Contacts: TContacts);
     134    procedure InsertContacts(Index: Integer; Contacts: TContacts);
    130135    procedure AssignToList(List: TFPGObjectList<TObject>);
    131136    function AddNew: TContact;
    132     function Search(FullName: string): TContact;
     137    function Search(Text: string; FieldIndex: TContactFieldIndex): TContact;
     138    function CountByField(FieldIndex: TContactFieldIndex): Integer;
     139    procedure Merge(Contact: TContact; FieldIndex: TContactFieldIndex);
    133140    function ToString: ansistring; override;
    134141  end;
     
    148155    function GetFileExt: string; override;
    149156    function GetFileFilter: string; override;
     157    procedure SaveToStrings(Output: TStrings);
     158    procedure LoadFromStrings(Lines: TStrings);
    150159    procedure SaveToFile(FileName: string); override;
    151160    procedure LoadFromFile(FileName: string); override;
     
    388397{ TContactProperties }
    389398
     399procedure TContactProperties.Assign(Source: TContactProperties);
     400var
     401  I: Integer;
     402begin
     403  while Count < Source.Count do
     404    Add(TContactProperty.Create);
     405  while Count > Source.Count do
     406    Delete(Count - 1);
     407  for I := 0 to Count - 1 do
     408    Items[I].Assign(Source.Items[I]);
     409end;
     410
    390411procedure TContactProperties.AssignToList(List: TFPGObjectList<TObject>);
    391412var
     
    579600{ TContacts }
    580601
     602procedure TContacts.Assign(Source: TContacts);
     603var
     604  I: Integer;
     605begin
     606  while Count < Source.Count do
     607    Add(TContact.Create);
     608  while Count > Source.Count do
     609    Delete(Count - 1);
     610  for I := 0 to Count - 1 do begin
     611    Items[I].Assign(Source.Items[I]);
     612    Items[I].Parent := ContactsFile;
     613  end;
     614end;
     615
     616procedure TContacts.AddContacts(Contacts: TContacts);
     617var
     618  I: Integer;
     619  NewContact: TContact;
     620begin
     621  for I := 0 to Contacts.Count - 1 do begin
     622    NewContact := TContact.Create;
     623    NewContact.Assign(Contacts[I]);
     624    NewContact.Parent := ContactsFile;
     625    Add(NewContact);
     626  end;
     627end;
     628
     629procedure TContacts.InsertContacts(Index: Integer; Contacts: TContacts);
     630var
     631  I: Integer;
     632  NewContact: TContact;
     633begin
     634  for I := 0 to Contacts.Count - 1 do begin
     635    NewContact := TContact.Create;
     636    NewContact.Assign(Contacts[I]);
     637    NewContact.Parent := ContactsFile;
     638    Insert(Index, NewContact);
     639    Inc(Index);
     640  end;
     641end;
     642
    581643procedure TContacts.AssignToList(List: TFPGObjectList<TObject>);
    582644var
     
    596658end;
    597659
    598 function TContacts.Search(FullName: string): TContact;
    599 var
    600   Contact: TContact;
     660function TContacts.Search(Text: string; FieldIndex: TContactFieldIndex): TContact;
     661var
     662  I: Integer;
    601663begin
    602664  Result := nil;
    603   for Contact in Self do
    604     if Contact.Fields[cfFullName] = FullName then begin
    605       Result := Contact;
     665  for I := 0 to Count - 1 do
     666    if Items[I].Fields[FieldIndex] = Text then begin
     667      Result := Items[I];
    606668      Break;
    607669    end;
     670end;
     671
     672function TContacts.CountByField(FieldIndex: TContactFieldIndex): Integer;
     673var
     674  I: Integer;
     675begin
     676  Result := 0;
     677  for I := 0 to Count - 1 do
     678    if Items[I].HasField(FieldIndex) then
     679      Inc(Result);
     680end;
     681
     682procedure TContacts.Merge(Contact: TContact; FieldIndex: TContactFieldIndex);
     683var
     684  NewContact: TContact;
     685begin
     686  NewContact := Search(Contact.Fields[FieldIndex], FieldIndex);
     687  if Assigned(NewContact) then begin
     688    NewContact.UpdateFrom(Contact);
     689  end else begin
     690    NewContact := TContact.Create;
     691    NewContact.Assign(Contact);
     692    NewContact.Parent := ContactsFile;
     693    Add(NewContact);
     694  end;
    608695end;
    609696
     
    741828end;
    742829
     830function TContact.HasField(FieldIndex: TContactFieldIndex): Boolean;
     831var
     832  Field: TContactField;
     833begin
     834  if not Assigned(Parent) then raise Exception.Create(SContactHasNoParent);
     835  Field := Parent.Fields.GetByIndex(FieldIndex);
     836  if Assigned(Field) then begin
     837    Result := Assigned(GetProperty(Field));
     838  end else raise Exception.Create(SFieldIndexNotDefined);
     839end;
     840
    743841function TContact.FullNameToFileName: string;
    744842var
     
    778876
    779877procedure TContact.Assign(Source: TContact);
    780 var
    781   I: Integer;
    782 begin
    783   while Properties.Count < Source.Properties.Count do
    784     Properties.Add(TContactProperty.Create);
    785   while Properties.Count > Source.Properties.Count do
    786     Properties.Delete(Properties.Count - 1);
    787   for I := 0 to Properties.Count - 1 do
    788     Properties[I].Assign(Source.Properties[I]);
     878begin
     879  Properties.Assign(Source.Properties);
    789880end;
    790881
     
    11141205end;
    11151206
     1207procedure TContactsFile.SaveToStrings(Output: TStrings);
     1208var
     1209  I: Integer;
     1210begin
     1211  for I := 0 to Contacts.Count - 1 do
     1212    Contacts[I].SaveToStrings(Output);
     1213end;
     1214
     1215procedure TContactsFile.LoadFromStrings(Lines: TStrings);
     1216var
     1217  Contact: TContact;
     1218  I: Integer;
     1219  NewI: Integer;
     1220begin
     1221  Contacts.Clear;
     1222
     1223  I := 0;
     1224  while I < Lines.Count do begin
     1225    Contact := TContact.Create;
     1226    Contact.Parent := Self;
     1227    NewI := Contact.LoadFromStrings(Lines, I);
     1228    if NewI <= Lines.Count then begin
     1229      if NewI <> -1 then begin
     1230        Contacts.Add(Contact);
     1231        I := NewI;
     1232      end else begin
     1233        FreeAndNil(Contact);
     1234        Inc(I);
     1235      end;
     1236    end else begin
     1237      FreeAndNil(Contact);
     1238      Break;
     1239    end;
     1240  end;
     1241end;
     1242
    11161243function TContactsFile.NewItem(Key, Value: string): string;
    11171244var
     
    11251252procedure TContactsFile.SaveToFile(FileName: string);
    11261253var
    1127   Output: TStringList;
    1128   I: Integer;
     1254  Lines: TStringList;
    11291255begin
    11301256  inherited;
    1131   Output := TStringList.Create;
     1257  Lines := TStringList.Create;
    11321258  try
    1133     for I := 0 to Contacts.Count - 1 do
    1134       Contacts[I].SaveToStrings(Output);
    1135     Output.SaveToFile(FileName);
     1259    SaveToStrings(Lines);
     1260    Lines.SaveToFile(FileName);
    11361261  finally
    1137     Output.Free;
     1262    Lines.Free;
    11381263  end
    11391264end;
     
    11421267var
    11431268  Lines: TStringList;
    1144   Contact: TContact;
    1145   I: Integer;
    1146   NewI: Integer;
    11471269begin
    11481270  inherited;
    1149   Contacts.Clear;
    11501271  Lines := TStringList.Create;
    11511272  Lines.LoadFromFile(FileName);
     
    11591280  {$ENDIF}
    11601281  try
    1161     I := 0;
    1162     while I < Lines.Count do begin
    1163       Contact := TContact.Create;
    1164       Contact.Parent := Self;
    1165       NewI := Contact.LoadFromStrings(Lines, I);
    1166       if NewI <= Lines.Count then begin
    1167         if NewI <> -1 then begin
    1168           Contacts.Add(Contact);
    1169           I := NewI;
    1170         end else begin
    1171           FreeAndNil(Contact);
    1172           Inc(I);
    1173         end;
    1174       end else begin
    1175         FreeAndNil(Contact);
    1176         Break;
    1177       end;
    1178     end;
     1282    LoadFromStrings(Lines);
    11791283  finally
    11801284    Lines.Free;
Note: See TracChangeset for help on using the changeset viewer.