-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddressBook.cpp
303 lines (267 loc) · 7.87 KB
/
AddressBook.cpp
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "AddressBook.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
static inline void ValidCtrCheck(TAddressBook *)
{
new TAddressBook(NULL);
}
//---------------------------------------------------------------------------
__fastcall TAddressBook::TAddressBook(TComponent *Owner)
: TComponent(Owner)
{
FDuplicatesAllowed = true;
FListDelimiters = new AnsiString();
*FListDelimiters = ",;\t";
Addresses = new TStringList();
LastEntry = new TStringList();
LastAddress = new TStringList();
LastEntryCtr = -1;
LastAddressCtr = -1;
}
//---------------------------------------------------------------------------
__fastcall TAddressBook::~TAddressBook()
{
delete FListDelimiters;
delete Addresses;
delete LastEntry;
delete LastAddress;
}
//---------------------------------------------------------------------------
namespace Addressbook
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TAddressBook)};
RegisterComponents("Celestial", classes, 0);
}
}
//---------------------------------------------------------------------------
AnsiString __fastcall TAddressBook::GetListDelimiters()
{
return *FListDelimiters;
}
//---------------------------------------------------------------------------
void __fastcall TAddressBook::SetListDelimiters(AnsiString ld)
{
*FListDelimiters = ld;
}
//---------------------------------------------------------------------------
int __fastcall TAddressBook::GetCount()
{
return Addresses->Count;
}
//---------------------------------------------------------------------------
bool __fastcall TAddressBook::IsDelimiter(AnsiString s) {
bool rv = false;
for (int i = 1; i < FListDelimiters->Length(); i++) {
if (FListDelimiters->SubString(i, 1) == s.SubString(1, 1)) {
rv = true;
break;
}
}
return rv;
}
//---------------------------------------------------------------------------
void __fastcall TAddressBook::ParseList(AnsiString s) {
AnsiString *is = new AnsiString();
AnsiString *ss = new AnsiString();
*is = s;
*is = *is + FListDelimiters->SubString(1, 1) + " ";
for (int i = 1; i < is->Length(); i++) {
if (IsDelimiter(is->SubString(i, 1))) {
TMailIdentity *mi = new TMailIdentity(this, true);
if (!ss->IsEmpty()) {
mi->AddAddress(*ss);
mi->DispName = *ss;
if (mi->DispName != "")
Add(mi);
}
*ss = "";
}
else
*ss += is->SubString(i, 1);
}
delete ss;
delete is;
}
//---------------------------------------------------------------------------
void __fastcall TAddressBook::Clear()
{
TMailIdentity *ptr;
for (int i = 0; i < Addresses->Count; i++) {
ptr = (TMailIdentity *)Addresses->Objects[i];
delete ptr;
Addresses->Objects[i] = NULL;
}
Addresses->Clear();
}
//---------------------------------------------------------------------------
bool __fastcall TAddressBook::FindIdentity(TMailIdentity *mid)
{
bool rc;
if (!mid->DispName.IsEmpty()) {
for (int i = 0; i < Addresses->Count; i++) {
TMailIdentity *ptr = (TMailIdentity *)Addresses->Objects[i];
if (!ptr->DispName.IsEmpty()) {
if (!FCaseSensitive)
rc = (ptr->DispName.AnsiCompareIC(mid->DispName) == 0);
else
rc = (ptr->DispName.AnsiCompare(mid->DispName) == 0);
} else
rc = false;
}
} else
rc = false;
return rc;
}
//---------------------------------------------------------------------------
void __fastcall TAddressBook::Add(TMailIdentity *mid)
{
if (mid->DispName != "") {
if (!FDuplicatesAllowed) {
if (!FindIdentity(mid))
Addresses->AddObject(mid->DispName, mid);
} else
Addresses->AddObject(mid->DispName, mid);
}
GetAllEntries();
GetAllAddresses();
}
//---------------------------------------------------------------------------
void __fastcall TAddressBook::Add(AnsiString mid)
{
ParseList(mid);
GetAllEntries();
GetAllAddresses();
}
//---------------------------------------------------------------------------
TMailIdentity * __fastcall TAddressBook::GetEntry(int i)
{
TMailIdentity *ptr;
if ((i >= 0) && (i < GetCount()))
ptr = (TMailIdentity *)Addresses->Objects[i];
return ptr;
}
//---------------------------------------------------------------------------
TMailIdentity * __fastcall TAddressBook::FindEntry(AnsiString str)
{
TMailIdentity *rv = NULL;
TMailIdentity *ptr;
for (int i = 0; i < GetCount(); i++) {
ptr = GetEntry(i);
if (ptr->DispName != str) {
int k = ptr->ContainsAddress(str);
if (k > -1)
rv = ptr;
} else
rv = ptr;
if (rv != NULL)
break;
}
return rv;
}
//---------------------------------------------------------------------------
void __fastcall TAddressBook::Export(AnsiString fname)
{
TStringList *tl = new TStringList();
for (int i = 0; i < GetCount(); i++) {
tl->Add("[" + Addresses->Strings[i] + "]");
TMailIdentity *ptr = GetEntry(i);
for (int j = 0; j < ptr->GetAddressCount(); j++)
tl->Add(ptr->GetAddress(j));
tl->Add("");
}
tl->SaveToFile(fname);
delete tl;
}
//---------------------------------------------------------------------------
void __fastcall TAddressBook::Append(TAddressBook *ab)
{
for (int i = 0; i < ab->Count; i++)
Add(ab->GetEntry(i));
}
//---------------------------------------------------------------------------
void __fastcall TAddressBook::Append(TMailIdentity *mi)
{
Add(mi);
}
//---------------------------------------------------------------------------
void __fastcall TAddressBook::SetDuplicatesAllowed(bool dup)
{
FDuplicatesAllowed = dup;
}
//---------------------------------------------------------------------------
void __fastcall TAddressBook::SetCaseSensitive(bool cs)
{
FCaseSensitive = cs;
}
//---------------------------------------------------------------------------
AnsiString __fastcall TAddressBook::GetFirstEntry()
{
AnsiString rv;
if (LastEntry->Count > 0)
rv = LastEntry->Strings[0];
else
rv = NULL;
return rv;
}
//---------------------------------------------------------------------------
AnsiString __fastcall TAddressBook::GetNextEntry()
{
AnsiString rv;
if (LastEntryCtr + 1 < LastEntry->Count)
rv = LastEntry->Strings[++LastEntryCtr];
else
rv = NULL;
return rv;
}
//---------------------------------------------------------------------------
AnsiString __fastcall TAddressBook::GetFirstAddress()
{
AnsiString rv;
if (LastAddress->Count > 0)
rv = LastAddress->Strings[0];
else
rv = NULL;
return rv;
}
//---------------------------------------------------------------------------
AnsiString __fastcall TAddressBook::GetNextAddress()
{
AnsiString rv;
if (LastAddressCtr + 1 < LastAddress->Count)
rv = LastAddress->Strings[++LastAddressCtr];
else
rv = NULL;
return rv;
}
//---------------------------------------------------------------------------
void __fastcall TAddressBook::GetAllEntries()
{
LastEntry->Clear();
for (int i = 0; i < Addresses->Count; i++)
LastEntry->Add(Addresses->Strings[i]);
if (LastEntry->Count > 0)
LastEntryCtr = 0;
}
//---------------------------------------------------------------------------
void __fastcall TAddressBook::GetAllAddresses()
{
TMailIdentity *ptr;
LastAddress->Clear();
for (int i = 0; i < Addresses->Count; i++) {
ptr = (TMailIdentity *)Addresses->Objects[i];
if (ptr != NULL)
for (int j = 0; j < ptr->GetAddressCount(); j++)
LastAddress->Add(ptr->GetAddress(j));
}
if (LastAddress->Count > 0)
LastAddressCtr = 0;
}
//---------------------------------------------------------------------------