source: trunk/Packages/VCard/VCardProcessor.pas

Last change on this file was 152, checked in by chronos, 12 months ago
  • Added: VCardProcessor class for processing contacts available from VCard package.
  • Added: Normalize menu action for normalization of contact property values.
File size: 2.7 KB
Line 
1unit VCardProcessor;
2
3interface
4
5uses
6 Classes, SysUtils, VCard;
7
8type
9
10 { TVCardProcessor }
11
12 TVCardProcessor = class(TComponent)
13 public
14 RemovePhoneSpaces: Boolean;
15 RemovePhotos: Boolean;
16 AddDefaultPhoneCountryPrefix: Boolean;
17 DefaultPhoneCountryCode: string;
18 ConvertInternationalCallPrefixToCountryCode: Boolean;
19 DefaultInternationalCallPrefix: string;
20 NonPrefixedPhoneLength: Integer;
21 RemoveExactDuplicates: Boolean;
22 Order: Boolean;
23 procedure Process(VCard: TVCard);
24 constructor Create(AOwner: TComponent); override;
25 end;
26
27
28implementation
29
30function RemoveSpaces(Text: string): string;
31begin
32 Result := StringReplace(Text, ' ', '', [rfReplaceAll]);
33end;
34
35{ TVCardProcessor }
36
37procedure TVCardProcessor.Process(VCard: TVCard);
38var
39 I: Integer;
40 J: Integer;
41 ContactProperties: TContactProperties;
42 Value: string;
43begin
44 for I := 0 to VCard.Contacts.Count - 1 do
45 with TContact(VCard.Contacts[I]) do begin
46 if RemovePhoneSpaces or AddDefaultPhoneCountryPrefix or
47 ConvertInternationalCallPrefixToCountryCode then begin
48 ContactProperties := Properties.GetMultipleByName('TEL');
49 try
50 for J := 0 to ContactProperties.Count - 1 do begin
51 if RemovePhoneSpaces then
52 ContactProperties[J].Value := RemoveSpaces(ContactProperties[J].Value);
53 if AddDefaultPhoneCountryPrefix and (NonPrefixedPhoneLength > 0) then begin
54 Value := ContactProperties[J].Value;
55 if (Length(Trim(Value)) > 0) and (Trim(Value)[1] in ['0'..'9']) and
56 (Length(RemoveSpaces(Value)) = NonPrefixedPhoneLength) then
57 ContactProperties[J].Value := DefaultPhoneCountryCode + Value;
58 end;
59 if ConvertInternationalCallPrefixToCountryCode then begin
60 if ContactProperties[J].Value.StartsWith(DefaultInternationalCallPrefix) then
61 ContactProperties[J].Value := DefaultPhoneCountryCode + Copy(ContactProperties[J].Value, Length(DefaultInternationalCallPrefix) + 1, MaxInt);
62 end;
63 end;
64 finally
65 ContactProperties.Free;
66 end;
67 end;
68
69 if RemovePhotos then begin
70 ContactProperties := Properties.GetMultipleByName('PHOTO');
71 try
72 for J := ContactProperties.Count - 1 downto 0 do
73 Properties.Remove(ContactProperties[J]);
74 finally
75 ContactProperties.Free;
76 end;
77 end;
78 end;
79
80 if RemoveExactDuplicates then begin
81 VCard.Contacts.RemoveExactDuplicates;
82 for I := 0 to VCard.Contacts.Count - 1 do
83 VCard.Contacts[I].Properties.RemoveExactDuplicates;
84 end;
85
86 if Order then begin
87 VCard.Contacts.Sort;
88 end;
89
90 VCard.Modified := True;
91end;
92
93constructor TVCardProcessor.Create(AOwner: TComponent);
94begin
95 inherited;
96 NonPrefixedPhoneLength := 9;
97end;
98
99end.
100
Note: See TracBrowser for help on using the repository browser.