summaryrefslogtreecommitdiff
path: root/extras/tiopf/demos/Demo_21_AdrsBook_MGM/contactmanager.pas
blob: 7e764460249e272541c61e9c4dfa0e0671558189 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
unit contactmanager;

{$IFDEF FPC}
  {$mode objfpc}{$H+}
{$ENDIF}

interface

uses
  model;
  
type

  { TContactManager }

  TContactManager = class(TMarkObject)
  private
    FAddressTypeList: TAddressTypeList;
    FCityList: TCityList;
    FContactList: TContactList;
    FCountryList: TCountryList;
    procedure PopulateCountries;
    procedure PopulateCities;
    function GenPhone: string;
  public
    constructor Create; override;
    destructor Destroy; override;
    procedure PopulateContacts;
  published
    property AddressTypeList: TAddressTypeList read FAddressTypeList;
    property CountryList: TCountryList read FCountryList;
    property CityList: TCityList read FCityList;
    property ContactList: TContactList read FContactList;
  end;


// Global singleton
function gContactManager: TContactManager;


implementation

uses
  SysUtils, tiObject;

var
  uContactManager: TContactManager;
  
const
  LastNames: array[1..10] of string = ('Geldenhuys', 'Botha', 'Johnson', 'Fourie',
      'Louw', 'Bougas', 'van der Mescht', 'Waskanski', 'Welgens', 'Viljoen');

  FirstNames: array[1..10] of string = ('Graeme', 'Johan', 'Debbie', 'Freda',
      'Jack', 'Ryno', 'Dirkus', 'Angela', 'Denise', 'Daniel');
      
  StreetNames: array[1..11] of string = ('Stellenberg Rd', 'Stellendal Rd',
      'Abelia', 'Main Rd', 'Links Drive', 'Short Street',
      'Long Street', 'Loop Street', 'Hillside Rd', 'Mountain Rd', 'Beach Drive');

function gContactManager: TContactManager;
begin
  if not Assigned(uContactManager) then
    uContactManager:= TContactManager.Create;
  result:= uContactManager;
end;

{ TContactManager }

procedure TContactManager.PopulateCountries;
var
  i: integer;
begin
  FCountryList.Add(TCountry.CreateNew('za', 'South Africa'));
  FCountryList.Add(TCountry.CreateNew('gb', 'Great Britain'));
  FCountryList.Add(TCountry.CreateNew('uk', 'Ukrain'));
  FCountryList.Add(TCountry.CreateNew('fr', 'France'));
  FCountryList.Add(TCountry.CreateNew('us', 'United States'));
  FCountryList.Add(TCountry.CreateNew('gr', 'Germany'));

  { reset ObjectState property }
  for i := 0 to FCountryList.Count - 1 do
    FCountryList[i].ObjectState := posClean;
end;

procedure TContactManager.PopulateCities;
var
  c: TCity;
  i: integer;
begin
  c:= TCity.CreateNew;
  c.Name:= 'Somerset West';
  c.ZIP := '7130';
  c.Country:= TCountry(FCountryList.FindByProps(['ISO'], ['za'], True));
  FCityList.Add(c);

  c:= TCity.CreateNew;
  c.Name:= 'Cape Town';
  c.ZIP := '8000';
  c.Country:= TCountry(FCountryList.FindByProps(['ISO'], ['za'], True));
  FCityList.Add(c);

  c:= TCity.CreateNew;
  c.Name:= 'Pretoria';
  c.ZIP := '0001';
  c.Country:= TCountry(FCountryList.FindByProps(['ISO'], ['za'], True));
  FCityList.Add(c);

  c:= TCity.CreateNew;
  c.Name:= 'Durban';
  c.ZIP := '2000';
  c.Country:= TCountry(FCountryList.FindByProps(['ISO'], ['za'], True));
  FCityList.Add(c);

  c:= TCity.CreateNew;
  c.Name:= 'London';
  c.ZIP := 'EC9 5NW';
  c.Country:= TCountry(FCountryList.FindByProps(['ISO'], ['gb'], True));

  FCityList.Add(c);
  c:= TCity.CreateNew;
  c.Name:= 'Watford';
  c.ZIP := 'NW9 7BJ';
  c.Country:= TCountry(FCountryList.FindByProps(['ISO'], ['gb'], True));
  FCityList.Add(c);

  c:= TCity.CreateNew;
  c.Name:= 'Frankfurt';
  c.ZIP := 'FK2000';
  c.Country:= TCountry(FCountryList.FindByProps(['ISO'], ['gr'], True));
  FCityList.Add(c);

  c:= TCity.CreateNew;
  c.Name:= 'New York';
  c.ZIP := 'NY2008';
  c.Country:= TCountry(FCountryList.FindByProps(['ISO'], ['us'], True));
  FCityList.Add(c);

  c:= TCity.CreateNew;
  c.Name:= 'San Fransisco';
  c.ZIP := 'SF2500';
  c.Country:= TCountry(FCountryList.FindByProps(['ISO'], ['us'], True));
  FCityList.Add(c);

  c:= TCity.CreateNew;
  c.Name:= 'Paris';
  c.ZIP := 'PRS007';
  c.Country:= TCountry(FCountryList.FindByProps(['ISO'], ['fr'], True));
  FCityList.Add(c);

  c:= TCity.CreateNew;
  c.Name:= 'Big City';
  c.ZIP := 'BC5 7WN';
  c.Country:= TCountry(FCountryList.FindByProps(['ISO'], ['uk'], True));
  FCityList.Add(c);

  { reset ObjectState property }
  for i := 0 to FCityList.Count - 1 do
    FCityList[i].ObjectState := posClean;
end;

function TContactManager.GenPhone: string;
begin
  result:= '+27 ' + IntToStr(Random(9)) + IntToStr(Random(9)) + ' '
    + IntToStr(Random(9)) + IntToStr(Random(9)) + IntToStr(Random(9)) + '-'
    + IntToStr(Random(9)) + IntToStr(Random(9)) + IntToStr(Random(9)) + IntToStr(Random(9));
end;

constructor TContactManager.Create;
begin
  inherited Create;
  FAddressTypeList := TAddressTypeList.Create;
  FAddressTypeList.Owner := self;
  FCountryList := TCountryList.Create;
  FCountryList.Owner := self;
  FCityList := TCityList.Create;
  FCityList.Owner := self;
  FContactList := TContactList.Create;
  FContactList.Owner := self;
end;

destructor TContactManager.Destroy;
begin
  FContactList.Free;
  FCityList.Free;
  FCountryList.Free;
  FAddressTypeList.Free;
  inherited Destroy;
end;

procedure TContactManager.PopulateContacts;
var
  C: TContact;
  I,J: Integer;
  A: TAddress;
begin
  PopulateCountries;
  PopulateCities;
  for I:= 1 to 10 do
  begin
    C:= TContact.CreateNew;
    C.FirstName:= FirstNames[I];
    C.LastName := LastNames[I];
    C.Mobile   := GenPhone;
    C.Email    := LowerCase(FirstNames[i])+ '@freepascal.org';
    for J:= 1 to 1+Random(2) do
    begin
      A:= TAddress.CreateNew;
      A.Street := StreetNames[1+Random(10)];
      A.Nr     := Random(100)+1;
      A.City   := FCityList[Random(10)];
      A.Fax    := GenPhone;
      A.Telephone1:= GenPhone;
      If Random(2)>0 then
         A.Telephone2:= GenPhone;
      A.Dirty := False;
      C.AddressList.Add(A);
    end;
    C.Comments := 'My name is ' + C.FirstName + '.';
    C.ObjectState := posClean;
    FContactList.Add(C);
  end;
end;


initialization
  uContactManager:= nil;
  
finalization
  uContactManager.Free;

end.