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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* e-card-compare.c
*
* Copyright (C) 2001 Ximian, Inc.
*
* Developed by Jon Trowbridge <trow@ximian.com>
*/
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
#include <config.h>
#include <ctype.h>
#include <gal/unicode/gunicode.h>
#include "e-book-util.h"
#include "e-card-compare.h"
/* This is an "optimistic" combiner: the best of the two outcomes is
selected. */
static ECardMatchType
combine_comparisons (ECardMatchType prev,
ECardMatchType new_info)
{
if (new_info == E_CARD_MATCH_NOT_APPLICABLE)
return prev;
return (ECardMatchType) MAX ((gint) prev, (gint) new_info);
}
/*** Name comparisons ***/
/* This *so* doesn't belong here... at least not implemented in a
sucky way like this. But by getting it in here now, I can fix it
up w/o adding a new feature when we are in feature freeze. :-) */
/* This is very Anglocentric. */
static gchar *name_synonyms[][2] = {
{ "jon", "john" }, /* Ah, the hacker's perogative */
{ "joseph", "joe" },
{ "robert", "bob" },
{ "richard", "dick" },
{ "william", "bill" },
{ "anthony", "tony" },
{ "michael", "mike" },
{ "eric", "erik" },
{ "elizabeth", "liz" },
{ "jeff", "geoff" },
{ "jeff", "geoffrey" },
{ "jim", "james" },
{ "abigal", "abby" },
{ "amanda", "amy" },
{ "amanda", "manda" },
{ "jennifer", "jenny" },
{ "rebecca", "becca" },
{ "rebecca", "becky" },
{ "anderson", "andersen" },
/* We could go on and on... */
{ NULL, NULL }
};
static gboolean
name_fragment_match (const gchar *a, const gchar *b)
{
gint i, len_a, len_b;
/* This will cause "Chris" and "Christopher" to match. */
len_a = g_utf8_strlen (a, -1);
len_b = g_utf8_strlen (b, -1);
if (!g_utf8_strncasecmp (a, b, MIN (len_a, len_b)))
return TRUE;
/* Check for nicknames. Yes, the linear search blows. */
for (i=0; name_synonyms[i][0]; ++i) {
if (!g_utf8_strcasecmp (name_synonyms[i][0], a)
&& !g_utf8_strcasecmp (name_synonyms[i][1], b))
return TRUE;
if (!g_utf8_strcasecmp (name_synonyms[i][0], b)
&& !g_utf8_strcasecmp (name_synonyms[i][1], a))
return TRUE;
}
return FALSE;
}
ECardMatchType
e_card_compare_name (ECard *card1, ECard *card2)
{
ECardName *a, *b;
gint matches=0, possible=0;
g_return_val_if_fail (card1 && E_IS_CARD (card1), E_CARD_MATCH_NOT_APPLICABLE);
g_return_val_if_fail (card2 && E_IS_CARD (card2), E_CARD_MATCH_NOT_APPLICABLE);
a = card1->name;
b = card2->name;
if (a == NULL || b == NULL)
return E_CARD_MATCH_NOT_APPLICABLE;
if (a->given && b->given) {
++possible;
if (name_fragment_match (a->given, b->given))
++matches;
}
if (a->additional && b->additional) {
++possible;
if (name_fragment_match (a->additional, b->additional))
++matches;
}
if (a->family && b->family) {
++possible;
if (name_fragment_match (a->family, b->family))
++matches;
}
/* Now look at the # of matches and try to intelligently map
an E_CARD_MATCH_* type to it. */
if (possible == 0)
return E_CARD_MATCH_NOT_APPLICABLE;
if (matches == 0)
return E_CARD_MATCH_NONE;
if (matches == possible) {
return possible > 1 ? E_CARD_MATCH_EXACT : E_CARD_MATCH_PARTIAL;
} else if (matches == possible-1)
return E_CARD_MATCH_PARTIAL;
else
return E_CARD_MATCH_VAGUE;
}
/*** Nickname Comparisons ***/
ECardMatchType
e_card_compare_nickname (ECard *card1, ECard *card2)
{
g_return_val_if_fail (card1 && E_IS_CARD (card1), E_CARD_MATCH_NOT_APPLICABLE);
g_return_val_if_fail (card2 && E_IS_CARD (card2), E_CARD_MATCH_NOT_APPLICABLE);
return E_CARD_MATCH_NOT_APPLICABLE;
}
/*** E-mail Comparisons ***/
static gboolean
match_email_username (const gchar *addr1, const gchar *addr2)
{
gint c1, c2;
if (addr1 == NULL || addr2 == NULL)
return FALSE;
while (*addr1 && *addr2 && *addr1 != '@' && *addr2 != '@') {
c1 = isupper (*addr1) ? tolower (*addr1) : *addr1;
c2 = isupper (*addr2) ? tolower (*addr2) : *addr2;
if (c1 != c2)
return FALSE;
++addr1;
++addr2;
}
return *addr1 == *addr2;
}
static gboolean
match_email_hostname (const gchar *addr1, const gchar *addr2)
{
gint c1, c2;
gboolean seen_at1, seen_at2;
if (addr1 == NULL || addr2 == NULL)
return FALSE;
/* Walk to the end of each string. */
seen_at1 = FALSE;
if (*addr1) {
while (*addr1) {
if (*addr1 == '@')
seen_at1 = TRUE;
++addr1;
}
--addr1;
}
seen_at2 = FALSE;
if (*addr2) {
while (*addr2) {
if (*addr2 == '@')
seen_at2 = TRUE;
++addr2;
}
--addr2;
}
if (!seen_at1 && !seen_at2)
return TRUE;
if (!seen_at1 || !seen_at2)
return FALSE;
while (*addr1 != '@' && *addr2 != '@') {
c1 = isupper (*addr1) ? tolower (*addr1) : *addr1;
c2 = isupper (*addr2) ? tolower (*addr2) : *addr2;
if (c1 != c2)
return FALSE;
--addr1;
--addr2;
}
/* This will match bob@foo.ximian.com and bob@ximian.com */
return *addr1 == '.' || *addr2 == '.';
}
static ECardMatchType
compare_email_addresses (const gchar *addr1, const gchar *addr2)
{
if (addr1 == NULL || addr2 == NULL)
return E_CARD_MATCH_NOT_APPLICABLE;
if (match_email_username (addr1, addr2))
return match_email_hostname (addr1, addr2) ? E_CARD_MATCH_EXACT : E_CARD_MATCH_PARTIAL;
return E_CARD_MATCH_NONE;
}
ECardMatchType
e_card_compare_email (ECard *card1, ECard *card2)
{
EIterator *i1, *i2;
ECardMatchType match = E_CARD_MATCH_NOT_APPLICABLE;
g_return_val_if_fail (card1 && E_IS_CARD (card1), E_CARD_MATCH_NOT_APPLICABLE);
g_return_val_if_fail (card2 && E_IS_CARD (card2), E_CARD_MATCH_NOT_APPLICABLE);
if (card1->email == NULL || card2->email == NULL)
return E_CARD_MATCH_NOT_APPLICABLE;
i1 = e_list_get_iterator (card1->email);
i2 = e_list_get_iterator (card2->email);
/* Do pairwise-comparisons on all of the e-mail addresses. If
we find an exact match, there is no reason to keep
checking. */
e_iterator_reset (i1);
while (e_iterator_is_valid (i1) && match != E_CARD_MATCH_EXACT) {
const gchar *addr1 = (const gchar *) e_iterator_get (i1);
e_iterator_reset (i2);
while (e_iterator_is_valid (i2) && match != E_CARD_MATCH_EXACT) {
const gchar *addr2 = (const gchar *) e_iterator_get (i2);
match = combine_comparisons (match, compare_email_addresses (addr1, addr2));
e_iterator_next (i2);
}
e_iterator_next (i1);
}
gtk_object_unref (GTK_OBJECT (i1));
gtk_object_unref (GTK_OBJECT (i2));
return match;
}
ECardMatchType
e_card_compare_address (ECard *card1, ECard *card2)
{
g_return_val_if_fail (card1 && E_IS_CARD (card1), E_CARD_MATCH_NOT_APPLICABLE);
g_return_val_if_fail (card2 && E_IS_CARD (card2), E_CARD_MATCH_NOT_APPLICABLE);
/* Unimplemented */
return E_CARD_MATCH_NOT_APPLICABLE;
}
ECardMatchType
e_card_compare_telephone (ECard *card1, ECard *card2)
{
g_return_val_if_fail (card1 && E_IS_CARD (card1), E_CARD_MATCH_NOT_APPLICABLE);
g_return_val_if_fail (card2 && E_IS_CARD (card2), E_CARD_MATCH_NOT_APPLICABLE);
/* Unimplemented */
return E_CARD_MATCH_NOT_APPLICABLE;
}
ECardMatchType
e_card_compare (ECard *card1, ECard *card2)
{
ECardMatchType result;
g_return_val_if_fail (card1 && E_IS_CARD (card1), E_CARD_MATCH_NOT_APPLICABLE);
g_return_val_if_fail (card2 && E_IS_CARD (card2), E_CARD_MATCH_NOT_APPLICABLE);
result = E_CARD_MATCH_NONE;
result = combine_comparisons (result, e_card_compare_name (card1, card2));
result = combine_comparisons (result, e_card_compare_nickname (card1, card2));
result = combine_comparisons (result, e_card_compare_email (card1, card2));
result = combine_comparisons (result, e_card_compare_address (card1, card2));
result = combine_comparisons (result, e_card_compare_telephone (card1, card2));
return result;
}
typedef struct _MatchSearchInfo MatchSearchInfo;
struct _MatchSearchInfo {
ECard *card;
ECardMatchQueryCallback cb;
gpointer closure;
};
static void
match_search_info_free (MatchSearchInfo *info)
{
if (info) {
gtk_object_unref (GTK_OBJECT (info->card));
g_free (info);
}
}
static void
simple_query_cb (EBook *book, EBookSimpleQueryStatus status, const GList *cards, gpointer closure)
{
MatchSearchInfo *info = (MatchSearchInfo *) closure;
ECardMatchType best_match = E_CARD_MATCH_NONE;
ECard *best_card = NULL;
const GList *i;
if (status != E_BOOK_SIMPLE_QUERY_STATUS_SUCCESS) {
info->cb (info->card, NULL, E_CARD_MATCH_NONE, info->closure);
match_search_info_free (info);
return;
}
for (i = cards; i != NULL; i = g_list_next (i)) {
ECard *this_card = E_CARD (i->data);
ECardMatchType this_match = e_card_compare (info->card, this_card);
if ((gint)this_match > (gint)best_match) {
best_match = this_match;
best_card = this_card;
}
}
info->cb (info->card, best_card, best_match, info->closure);
match_search_info_free (info);
}
#define MAX_QUERY_PARTS 10
static void
use_common_book_cb (EBook *book, gpointer closure)
{
MatchSearchInfo *info = (MatchSearchInfo *) closure;
ECard *card = info->card;
gchar *query_parts[MAX_QUERY_PARTS];
gint p=0;
gchar *query, *qj;
if (book == NULL) {
info->cb (info->card, NULL, E_CARD_MATCH_NONE, info->closure);
match_search_info_free (info);
return;
}
if (card->nickname)
query_parts[p++] = g_strdup_printf ("(beginswith \"nickname\" \"%s\")", card->nickname);
if (card->name->given && strlen (card->name->given) > 1)
query_parts[p++] = g_strdup_printf ("(contains \"full_name\" \"%s\")", card->name->given);
if (card->name->additional && strlen (card->name->additional) > 1)
query_parts[p++] = g_strdup_printf ("(contains \"full_name\" \"%s\")", card->name->additional);
if (card->name->family && strlen (card->name->family) > 1)
query_parts[p++] = g_strdup_printf ("(contains \"full_name\" \"%s\")", card->name->family);
if (card->email) {
EIterator *iter = e_list_get_iterator (card->email);
while (e_iterator_is_valid (iter) && p < MAX_QUERY_PARTS) {
gchar *addr = g_strdup (e_iterator_get (iter));
if (addr) {
gchar *s = addr;
while (*s) {
if (*s == '@') {
*s = '\0';
break;
}
++s;
}
query_parts[p++] = g_strdup_printf ("beginswith \"email\" \"%s\")", addr);
g_free (addr);
}
e_iterator_next (iter);
}
}
/* Build up our full query from the parts. */
query_parts[p] = NULL;
qj = g_strjoinv (" ", query_parts);
if (p > 0) {
query = g_strdup_printf ("(or %s)", qj);
} else {
query = qj;
qj = NULL;
}
e_book_simple_query (book, query, simple_query_cb, info);
for (p=0; query_parts[p]; ++p)
g_free (query_parts[p]);
g_free (qj);
g_free (query);
}
void
e_card_locate_match (ECard *card, ECardMatchQueryCallback cb, gpointer closure)
{
MatchSearchInfo *info;
g_return_if_fail (card && E_IS_CARD (card));
g_return_if_fail (cb != NULL);
info = g_new (MatchSearchInfo, 1);
info->card = card;
gtk_object_ref (GTK_OBJECT (card));
info->cb = cb;
info->closure = closure;
e_book_use_local_address_book (use_common_book_cb, info);
}
|