Changeset 73 for trunk/UContact.pas
- Timestamp:
- Dec 13, 2021, 11:33:11 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UContact.pas
r72 r73 93 93 94 94 TContactProperties = class(TFPGObjectList<TContactProperty>) 95 procedure Assign(Source: TContactProperties); 95 96 procedure AssignToList(List: TFPGObjectList<TObject>); 96 97 function GetByName(Name: string): TContactProperty; … … 110 111 Properties: TContactProperties; 111 112 Parent: TContactsFile; 113 function HasField(FieldIndex: TContactFieldIndex): Boolean; 112 114 function FullNameToFileName: string; 113 115 function GetProperty(Field: TContactField): TContactProperty; overload; … … 128 130 TContacts = class(TFPGObjectList<TContact>) 129 131 ContactsFile: TContactsFile; 132 procedure Assign(Source: TContacts); 133 procedure AddContacts(Contacts: TContacts); 134 procedure InsertContacts(Index: Integer; Contacts: TContacts); 130 135 procedure AssignToList(List: TFPGObjectList<TObject>); 131 136 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); 133 140 function ToString: ansistring; override; 134 141 end; … … 148 155 function GetFileExt: string; override; 149 156 function GetFileFilter: string; override; 157 procedure SaveToStrings(Output: TStrings); 158 procedure LoadFromStrings(Lines: TStrings); 150 159 procedure SaveToFile(FileName: string); override; 151 160 procedure LoadFromFile(FileName: string); override; … … 388 397 { TContactProperties } 389 398 399 procedure TContactProperties.Assign(Source: TContactProperties); 400 var 401 I: Integer; 402 begin 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]); 409 end; 410 390 411 procedure TContactProperties.AssignToList(List: TFPGObjectList<TObject>); 391 412 var … … 579 600 { TContacts } 580 601 602 procedure TContacts.Assign(Source: TContacts); 603 var 604 I: Integer; 605 begin 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; 614 end; 615 616 procedure TContacts.AddContacts(Contacts: TContacts); 617 var 618 I: Integer; 619 NewContact: TContact; 620 begin 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; 627 end; 628 629 procedure TContacts.InsertContacts(Index: Integer; Contacts: TContacts); 630 var 631 I: Integer; 632 NewContact: TContact; 633 begin 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; 641 end; 642 581 643 procedure TContacts.AssignToList(List: TFPGObjectList<TObject>); 582 644 var … … 596 658 end; 597 659 598 function TContacts.Search( FullName: string): TContact;599 var 600 Contact: TContact;660 function TContacts.Search(Text: string; FieldIndex: TContactFieldIndex): TContact; 661 var 662 I: Integer; 601 663 begin 602 664 Result := nil; 603 for Contact in Selfdo604 if Contact.Fields[cfFullName] = FullNamethen begin605 Result := Contact;665 for I := 0 to Count - 1 do 666 if Items[I].Fields[FieldIndex] = Text then begin 667 Result := Items[I]; 606 668 Break; 607 669 end; 670 end; 671 672 function TContacts.CountByField(FieldIndex: TContactFieldIndex): Integer; 673 var 674 I: Integer; 675 begin 676 Result := 0; 677 for I := 0 to Count - 1 do 678 if Items[I].HasField(FieldIndex) then 679 Inc(Result); 680 end; 681 682 procedure TContacts.Merge(Contact: TContact; FieldIndex: TContactFieldIndex); 683 var 684 NewContact: TContact; 685 begin 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; 608 695 end; 609 696 … … 741 828 end; 742 829 830 function TContact.HasField(FieldIndex: TContactFieldIndex): Boolean; 831 var 832 Field: TContactField; 833 begin 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); 839 end; 840 743 841 function TContact.FullNameToFileName: string; 744 842 var … … 778 876 779 877 procedure 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]); 878 begin 879 Properties.Assign(Source.Properties); 789 880 end; 790 881 … … 1114 1205 end; 1115 1206 1207 procedure TContactsFile.SaveToStrings(Output: TStrings); 1208 var 1209 I: Integer; 1210 begin 1211 for I := 0 to Contacts.Count - 1 do 1212 Contacts[I].SaveToStrings(Output); 1213 end; 1214 1215 procedure TContactsFile.LoadFromStrings(Lines: TStrings); 1216 var 1217 Contact: TContact; 1218 I: Integer; 1219 NewI: Integer; 1220 begin 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; 1241 end; 1242 1116 1243 function TContactsFile.NewItem(Key, Value: string): string; 1117 1244 var … … 1125 1252 procedure TContactsFile.SaveToFile(FileName: string); 1126 1253 var 1127 Output: TStringList; 1128 I: Integer; 1254 Lines: TStringList; 1129 1255 begin 1130 1256 inherited; 1131 Output:= TStringList.Create;1257 Lines := TStringList.Create; 1132 1258 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); 1136 1261 finally 1137 Output.Free;1262 Lines.Free; 1138 1263 end 1139 1264 end; … … 1142 1267 var 1143 1268 Lines: TStringList; 1144 Contact: TContact;1145 I: Integer;1146 NewI: Integer;1147 1269 begin 1148 1270 inherited; 1149 Contacts.Clear;1150 1271 Lines := TStringList.Create; 1151 1272 Lines.LoadFromFile(FileName); … … 1159 1280 {$ENDIF} 1160 1281 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); 1179 1283 finally 1180 1284 Lines.Free;
Note:
See TracChangeset
for help on using the changeset viewer.