aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/gui/component/select-names/e-select-names-completion.c
blob: 9650551f57b6d28a3878e3b5751c288e90bc5d92 (plain) (blame)
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 * e-select-names-completion.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 "e-select-names-completion.h"

#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include <gtk/gtksignal.h>
#include <libgnome/gnome-defs.h>
#include <libgnome/gnome-util.h>

#include <gal/unicode/gunicode.h>

#include <addressbook/backend/ebook/e-book-util.h>
#include <addressbook/backend/ebook/e-destination.h>
#include <addressbook/backend/ebook/e-card-simple.h>
#include <addressbook/backend/ebook/e-card-compare.h>


struct _ESelectNamesCompletionPrivate {

    ESelectNamesModel *model;

    EBook *book;
    gboolean book_ready;

    guint book_view_tag;
    EBookView *book_view;
    guint card_added_tag;
    guint seq_complete_tag;

    gchar *waiting_query;
    gint waiting_pos, waiting_limit;
    gchar *query_text;

    gchar *cached_query_text;
    GList *cached_cards;

    gboolean match_contact_lists;
    gboolean primary_only;

    gboolean can_fail_due_to_too_many_hits; /* like LDAP, for example... */
};

static void e_select_names_completion_class_init (ESelectNamesCompletionClass *);
static void e_select_names_completion_init (ESelectNamesCompletion *);
static void e_select_names_completion_destroy (GtkObject *object);

static void e_select_names_completion_got_book_view_cb (EBook *book, EBookStatus status, EBookView *view, gpointer user_data);
static void e_select_names_completion_card_added_cb    (EBookView *, const GList *cards, gpointer user_data);
static void e_select_names_completion_seq_complete_cb  (EBookView *, gpointer user_data);

static void e_select_names_completion_do_query (ESelectNamesCompletion *, const gchar *query_text, gint pos, gint limit);

static void e_select_names_completion_handle_request  (ECompletion *, const gchar *txt, gint pos, gint limit);
static void e_select_names_completion_end    (ECompletion *);

static GtkObjectClass *parent_class;

static FILE *out;

/*
 *
 * Query builders
 *
 */

typedef gchar *(*BookQuerySExp) (ESelectNamesCompletion *);
typedef ECompletionMatch *(*BookQueryMatchTester) (ESelectNamesCompletion *, EDestination *);

static void
our_match_destroy (ECompletionMatch *match)
{
    gtk_object_unref (GTK_OBJECT (match->user_data));
}

static ECompletionMatch *
make_match (EDestination *dest, const gchar *menu_form, double score)
{
    ECompletionMatch *match;
    ECard *card = e_destination_get_card (dest);

    match = e_completion_match_new (e_destination_get_name (dest), menu_form, score);

    e_completion_match_set_text (match, e_destination_get_name (dest), menu_form);
    match->sort_major = card ? floor (e_card_get_use_score (card)) : 0;
    match->sort_minor = e_destination_get_email_num (dest);

    match->user_data = dest;
    gtk_object_ref (GTK_OBJECT (dest));

    match->destroy = our_match_destroy;

    return match;
}

/*
 * Nickname query
 */

static gchar *
sexp_nickname (ESelectNamesCompletion *comp)
{
    gchar *query = g_strdup_printf ("(beginswith \"nickname\" \"%s\")", comp->priv->query_text);

    return query;
}

static ECompletionMatch *
match_nickname (ESelectNamesCompletion *comp, EDestination *dest)
{
    ECompletionMatch *match = NULL;
    gint len;
    ECard *card = e_destination_get_card (dest);
    double score;

    if (card->nickname == NULL)
        return NULL;

    len = g_utf8_strlen (comp->priv->query_text, -1);
    if (card->nickname && !g_utf8_strncasecmp (comp->priv->query_text, card->nickname, len)) {
        const gchar *name;
        gchar *str;

        score = len * 2; /* nickname gives 2 points per matching character */

        if (len == g_utf8_strlen (card->nickname, -1)) /* boost score on an exact match */
            score *= 10;

        name = e_destination_get_name (dest);
        if (name && *name)
            str = g_strdup_printf ("'%s' %s <%s>", card->nickname, name, e_destination_get_email (dest));
        else
            str = g_strdup_printf ("'%s' <%s>", card->nickname, e_destination_get_email (dest));

        match = make_match (dest, str, score);
        g_free (str);
    }

    return match;
}

/*
 * E-Mail Query
 */

static gchar *
sexp_email (ESelectNamesCompletion *comp)
{
    return g_strdup_printf ("(beginswith \"email\" \"%s\")", comp->priv->query_text);
}

static ECompletionMatch *
match_email (ESelectNamesCompletion *comp, EDestination *dest)
{
    ECompletionMatch *match;
    gint len = strlen (comp->priv->query_text);
    const gchar *name = e_destination_get_name (dest);
    const gchar *email = e_destination_get_email (dest);
    double score;
    
    if (email
        && !g_utf8_strncasecmp (comp->priv->query_text, email, len)
        && !e_destination_is_evolution_list (dest)) {
        
        gchar *str;
        
        score = len * 2; /* 2 points for each matching character */

        if (name && *name)
            str = g_strdup_printf ("<%s> %s", email, name);
        else
            str = g_strdup (email);

        match = make_match (dest, str, score);

        g_free (str);

        return match;
    }

    return NULL;
}

/*
 * Name Query
 */

static gchar *
name_style_query (ESelectNamesCompletion *comp, const gchar *field)
{
    if (comp && comp->priv->query_text && *comp->priv->query_text) {
        gchar *cpy = g_strdup (comp->priv->query_text), *c;
        gchar **strv;
        gchar *query;
        gint i, count=0;

        for (c = cpy; *c; ++c) {
            if (*c == ',')
                *c = ' ';
        }

        strv = g_strsplit (cpy, " ", 0);
        for (i=0; strv[i]; ++i) {
            gchar *old;
            ++count;
            g_strstrip (strv[i]);
            old = strv[i];
            strv[i] = g_strdup_printf ("(contains \"%s\" \"%s\")", field, old);
            g_free (old);
        }

        if (count == 1) {
            query = strv[0];
            strv[0] = NULL;
        } else {
            gchar *joined = g_strjoinv (" ", strv);
            query = g_strdup_printf ("(and %s)", joined);
            g_free (joined);
        }

        g_free (cpy);
        g_strfreev (strv);

        return query;
    }

    return NULL;
}

static gchar *
sexp_name (ESelectNamesCompletion *comp)
{
    return name_style_query (comp, "full_name");
}

static ECompletionMatch *
match_name (ESelectNamesCompletion *comp, EDestination *dest)
{
    ECompletionMatch *final_match = NULL;
    gchar *menu_text = NULL;
    ECard *card;
    const gchar *email;
    gint match_len = 0;
    ECardMatchType match;
    ECardMatchPart first_match;
    double score = 0;
    gboolean have_given, have_additional, have_family;

    card = e_destination_get_card (dest);
    
    if (card->name == NULL)
        return NULL;

    email = e_destination_get_email (dest);

    match = e_card_compare_name_to_string_full (card, comp->priv->query_text, TRUE /* yes, allow partial matches */,
                            NULL, &first_match, &match_len);

    if (match <= E_CARD_MATCH_NONE)
        return NULL;
    
    score = match_len * 3; /* three points per match character */

#if 0
    if (card->nickname) {
        /* We massively boost the score if the nickname exists and is the same as one of the "real" names.  This keeps the
           nickname from matching ahead of the real name for this card. */
        len = strlen (card->nickname);
        if ((card->name->given && !g_utf8_strncasecmp (card->name->given, card->nickname, MIN (strlen (card->name->given), len)))
            || (card->name->family && !g_utf8_strncasecmp (card->name->family, card->nickname, MIN (strlen (card->name->family), len)))
            || (card->name->additional && !g_utf8_strncasecmp (card->name->additional, card->nickname, MIN (strlen (card->name->additional), len))))
            score *= 100;
    }
#endif

    have_given       = card->name->given && *card->name->given;
    have_additional  = card->name->additional && *card->name->additional;
    have_family      = card->name->family && *card->name->family;

    if (e_card_evolution_list (card)) {

        menu_text = e_card_name_to_string (card->name);

    } else if (first_match == E_CARD_MATCH_PART_GIVEN_NAME) {

        if (have_family)
            menu_text = g_strdup_printf ("%s %s <%s>", card->name->given, card->name->family, email);
        else
            menu_text = g_strdup_printf ("%s <%s>", card->name->given, email);

    } else if (first_match == E_CARD_MATCH_PART_ADDITIONAL_NAME) {

        if (have_given) {

            menu_text = g_strdup_printf ("%s%s%s, %s <%s>",
                             card->name->additional,
                             have_family ? " " : "",
                             have_family ? card->name->family : "",
                             card->name->given,
                             email);
        } else {

            menu_text = g_strdup_printf ("%s%s%s <%s>",
                             card->name->additional,
                             have_family ? " " : "",
                             have_family ? card->name->family : "",
                             email);
        }

    } else if (first_match == E_CARD_MATCH_PART_FAMILY_NAME) { 

        if (have_given)
            menu_text = g_strdup_printf ("%s, %s%s%s <%s>",
                             card->name->family,
                             card->name->given,
                             have_additional ? " " : "",
                             have_additional ? card->name->additional : "",
                             email);
        else
            menu_text = g_strdup_printf ("%s <%s>", card->name->family, email);

    } else { /* something funny happened */

        menu_text = g_strdup_printf ("<%s> ???", email);

    }

    if (menu_text) {
        g_strstrip (menu_text);
        final_match = make_match (dest, menu_text, score);
        g_free (menu_text);
    }
    
    return final_match;
}

/*
 * File As Query
 */

static gchar *
sexp_file_as (ESelectNamesCompletion *comp)
{
    return name_style_query (comp, "file_as");
}

static ECompletionMatch *
match_file_as (ESelectNamesCompletion *comp, EDestination *dest)
{
    const gchar *name;
    const gchar *email;
    gchar *cpy, **strv, *menu_text;
    gint i, len;
    double score = 0.00001;
    ECompletionMatch *match;

    name = e_destination_get_name (dest);
    email = e_destination_get_email (dest);

    if (!(name && *name))
        return NULL;

    cpy = g_strdup (comp->priv->query_text);
    strv = g_strsplit (cpy, " ", 0);

    for (i=0; strv[i] && score > 0; ++i) {
        len = g_utf8_strlen (strv[i], -1);
        if (!g_utf8_strncasecmp (name, strv[i], len))
            score += len; /* one point per character of the match */
        else
            score = 0;
    }
    
    g_free (cpy);
    g_strfreev (strv);

    if (score <= 0)
        return NULL;
    
    menu_text = g_strdup_printf ("%s <%s>", name, email);
    g_strstrip (menu_text);
    match = make_match (dest, menu_text, score);
    g_free (menu_text);

    return match;
}

/*
 * Initials Query
 */

static gchar *
sexp_initials (ESelectNamesCompletion *comp)
{
    return NULL;
}

static ECompletionMatch *
match_initials (ESelectNamesCompletion *comp, EDestination *dest)
{
    return NULL;
}


typedef struct _BookQuery BookQuery;
struct _BookQuery {
    gboolean primary;
    BookQuerySExp builder;
    BookQueryMatchTester tester;
};

static BookQuery book_queries[] = {
    { TRUE,  sexp_nickname, match_nickname},
    { TRUE,  sexp_email,    match_email },
    { TRUE,  sexp_name,     match_name },
    { TRUE,  sexp_file_as,  match_file_as },
    { FALSE, sexp_initials, match_initials }
};
static gint book_query_count = sizeof (book_queries) / sizeof (BookQuery);

/*
 * Build up a big compound sexp corresponding to all of our queries.
 */
static gchar *
book_query_sexp (ESelectNamesCompletion *comp)
{
    gint i, j, count = 0;
    gchar **queryv, *query;

    g_return_val_if_fail (comp && E_IS_SELECT_NAMES_COMPLETION (comp), NULL);

    if (! (comp->priv->query_text && *comp->priv->query_text))
        return NULL;

    if (comp->priv->primary_only) {
        for (i=0; i<book_query_count; ++i)
            if (book_queries[i].primary)
                ++count;
    } else {
        count = book_query_count;
    }

    queryv = g_new0 (gchar *, count+1);
    for (i=0, j=0; i<count; ++i) {
        queryv[j] = book_queries[i].builder (comp);
        if (queryv[j])
            ++j;
    }

    if (j == 0) {
        query = NULL;
    } else if (j == 1) {
        query = queryv[0];
        queryv[0] = NULL;
    } else {
        gchar *tmp = g_strjoinv (" ", queryv);
        query = g_strdup_printf ("(or %s)", tmp);
        g_free (tmp);
    }

    for (i=0; i<count; ++i)
        g_free (queryv[i]);
    g_free (queryv);

    return query;
}

/*
 * Sweep across all of our query rules and find the best score/match
 * string that applies to a given destination.
 */
static ECompletionMatch *
book_query_score (ESelectNamesCompletion *comp, EDestination *dest)
{
    ECompletionMatch *best_match = NULL;
    gint i;

    g_return_val_if_fail (E_IS_SELECT_NAMES_COMPLETION (comp), NULL);
    g_return_val_if_fail (E_IS_DESTINATION (dest), NULL);

    if (! (comp->priv->query_text && *comp->priv->query_text))
        return NULL;
    
    for (i=0; i<book_query_count; ++i) {

        ECompletionMatch *this_match = NULL;

        if (book_queries[i].primary || !comp->priv->primary_only) {
            if (book_queries[i].tester && e_destination_get_card (dest)) {
                this_match = book_queries[i].tester (comp, dest);
            }

            if (this_match) {
                if (best_match == NULL || this_match->score > best_match->score) {
                    e_completion_match_unref (best_match);
                    best_match = this_match;
                } else {
                    e_completion_match_unref (this_match);
                }
            }
        }
    }

    return best_match;
}

static void
book_query_process_card_list (ESelectNamesCompletion *comp, const GList *cards)
{
    while (cards) {
        ECard *card = E_CARD (cards->data);

        if (e_card_evolution_list (card)) {

            if (comp->priv->match_contact_lists) {

                EDestination *dest = e_destination_new ();
                ECompletionMatch *match;
                e_destination_set_card (dest, card, 0);
                match = book_query_score (comp, dest);
                if (match && match->score > 0) {
                    e_completion_found_match (E_COMPLETION (comp), match);
                } else {
                    e_completion_match_unref (match);
                }
                gtk_object_unref (GTK_OBJECT (dest));

            }

        } else if (card->email) {
            gint i;
            for (i=0; i<e_list_length (card->email); ++i) {
                EDestination *dest = e_destination_new ();
                const gchar *email;
                ECompletionMatch *match;
                
                e_destination_set_card (dest, card, i);
                email = e_destination_get_email (dest);

                if (email && *email) {
                
                    match = book_query_score (comp, dest);
                    if (match && match->score > 0) {
                        e_completion_found_match (E_COMPLETION (comp), match);
                    } else {
                        e_completion_match_unref (match);
                    }
                }

                gtk_object_unref (GTK_OBJECT (dest));
            }
        }
        
        cards = g_list_next (cards);
    }
}

#if 0
static gchar *
initials_query_match_cb (QueryInfo *qi, ECard *card, double *score)
{
    gint len;
    gchar f='\0', m='\0', l='\0'; /* initials */
    gchar cf, cm, cl;

    len = strlen (qi->comp->priv->query_text);
    
    if (len == 2) {
        
        f = qi->comp->priv->query_text[0];
        m = '\0';
        l = qi->comp->priv->query_text[1];

    } else if (len == 3) {

        f = qi->comp->priv->query_text[0];
        m = qi->comp->priv->query_text[1];
        l = qi->comp->priv->query_text[2];

    } else {
        return NULL;
    }

    cf = card->name->given ? *card->name->given : '\0';
    cm = card->name->additional ? *card->name->additional : '\0';
    cl = card->name->family ? *card->name->family : '\0';

    if (f && isupper ((gint) f))
        f = tolower ((gint) f);
    if (m && isupper ((gint) m))
        m = tolower ((gint) m);
    if (l && isupper ((gint) l))
        l = tolower ((gint) l);

    if (cf && isupper ((gint) cf))
        cf = tolower ((gint) cf);
    if (cm && isupper ((gint) cm))
        cm = tolower ((gint) cm);
    if (cl && isupper ((gint) cl))
        cl = tolower ((gint) cl);

    if ((f == '\0' || (f == cf)) && (m == '\0' || (m == cm)) && (l == '\0' || (l == cl))) {
        if (score)
            *score = 3;
        if (m)
            return g_strdup_printf ("%s %s %s", card->name->given, card->name->additional, card->name->family);
        else
            return g_strdup_printf ("%s %s", card->name->given, card->name->family);
    }

    return NULL;
}

static gboolean
start_initials_query (ESelectNamesCompletion *comp)
{
    gint len;
    gchar *query;

    if (comp && comp->priv->query_text && *(comp->priv->query_text)) {
        
        len = strlen (comp->priv->query_text);
        if (len < 2 || len > 3)
            return FALSE;

        query = g_strdup_printf ("(contains \"x-evolution-any-field\" \"%c\")", *(comp->priv->query_text));
        query_info_start (comp, comp->priv->query_text, query, initials_query_match_cb);
        g_free (query);
        return TRUE;
    }

    return FALSE;
}
#endif


/*
 *
 * ESelectNamesCompletion code
 *
 */


GtkType
e_select_names_completion_get_type (void)
{
    static GtkType select_names_complete_type = 0;
  
    if (!select_names_complete_type) {
        GtkTypeInfo select_names_complete_info = {
            "ESelectNamesCompletion",
            sizeof (ESelectNamesCompletion),
            sizeof (ESelectNamesCompletionClass),
            (GtkClassInitFunc) e_select_names_completion_class_init,
            (GtkObjectInitFunc) e_select_names_completion_init,
            NULL, NULL, /* reserved */
            (GtkClassInitFunc) NULL
        };

        select_names_complete_type = gtk_type_unique (e_completion_get_type (), &select_names_complete_info);
    }

    return select_names_complete_type;
}

static void
e_select_names_completion_class_init (ESelectNamesCompletionClass *klass)
{
    GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
    ECompletionClass *completion_class = E_COMPLETION_CLASS (klass);

    parent_class = GTK_OBJECT_CLASS (gtk_type_class (e_completion_get_type ()));

    object_class->destroy = e_select_names_completion_destroy;

    completion_class->request_completion = e_select_names_completion_handle_request;
    completion_class->end_completion = e_select_names_completion_end;

    if (getenv ("EVO_DEBUG_SELECT_NAMES_COMPLETION")) {
        out = fopen ("/tmp/evo-debug-select-names-completion", "w");
        if (out)
            setvbuf (out, NULL, _IONBF, 0);
    }
}

static void
e_select_names_completion_init (ESelectNamesCompletion *comp)
{
    comp->priv = g_new0 (struct _ESelectNamesCompletionPrivate, 1);
    comp->priv->match_contact_lists = TRUE;
}

static void
e_select_names_completion_destroy (GtkObject *object)
{
    ESelectNamesCompletion *comp = E_SELECT_NAMES_COMPLETION (object);

    if (comp->priv->model)
        gtk_object_unref (GTK_OBJECT (comp->priv->model));

    if (comp->priv->book)
        gtk_object_unref (GTK_OBJECT (comp->priv->book));

    if (comp->priv->card_added_tag) {
        gtk_signal_disconnect (GTK_OBJECT (comp->priv->book_view), comp->priv->card_added_tag);
        comp->priv->card_added_tag = 0;
    }

    if (comp->priv->seq_complete_tag) {
        gtk_signal_disconnect (GTK_OBJECT (comp->priv->book_view), comp->priv->seq_complete_tag);
        comp->priv->seq_complete_tag = 0;
    }

    if (comp->priv->book_view)
        gtk_object_unref (GTK_OBJECT (comp->priv->book_view));
    
    g_free (comp->priv->waiting_query);
    g_free (comp->priv->query_text);

    g_free (comp->priv->cached_query_text);
    g_list_foreach (comp->priv->cached_cards, (GFunc)gtk_object_unref, NULL);
    g_list_free (comp->priv->cached_cards);

    g_free (comp->priv);

    if (parent_class->destroy)
        parent_class->destroy (object);
}


/*
 *
 *  EBook/EBookView Callbacks & Query Stuff
 *
 */

static gchar *
clean_query_text (const gchar *s)
{
    gchar *q = g_new (gchar, strlen(s)+1), *t;

    t = q;
    while (*s) {
        if (*s != ',' && *s != '"') {
            *t = *s;
            ++t;
        }
        ++s;
    }
    *t = '\0';

    g_strstrip (q);

    return q;
}

static void
e_select_names_completion_clear_cache (ESelectNamesCompletion *comp)
{
    g_free (comp->priv->cached_query_text);
    comp->priv->cached_query_text = NULL;

    g_list_foreach (comp->priv->cached_cards, (GFunc)gtk_object_unref, NULL);
    g_list_free (comp->priv->cached_cards);
    comp->priv->cached_cards = NULL;
}

static void
e_select_names_completion_got_book_view_cb (EBook *book, EBookStatus status, EBookView *view, gpointer user_data)
{
    ESelectNamesCompletion *comp;

    if (status != E_BOOK_STATUS_SUCCESS)
        return;

    comp = E_SELECT_NAMES_COMPLETION (user_data);

    comp->priv->book_view_tag = 0;

    if (comp->priv->card_added_tag) {
        gtk_signal_disconnect (GTK_OBJECT (comp->priv->book_view), comp->priv->card_added_tag);
        comp->priv->card_added_tag = 0;
    }
    if (comp->priv->seq_complete_tag) {
        gtk_signal_disconnect (GTK_OBJECT (comp->priv->book_view), comp->priv->seq_complete_tag);
        comp->priv->seq_complete_tag = 0;
    }

    gtk_object_ref (GTK_OBJECT (view));
    if (comp->priv->book_view)
        gtk_object_unref (GTK_OBJECT (comp->priv->book_view));
    comp->priv->book_view = view;

    comp->priv->card_added_tag = 
        gtk_signal_connect (GTK_OBJECT (view),
                    "card_added",
                    GTK_SIGNAL_FUNC (e_select_names_completion_card_added_cb),
                    comp);

    comp->priv->seq_complete_tag =
        gtk_signal_connect (GTK_OBJECT (view),
                    "sequence_complete",
                    GTK_SIGNAL_FUNC (e_select_names_completion_seq_complete_cb),
                    comp);
}

static void
e_select_names_completion_card_added_cb (EBookView *book_view, const GList *cards, gpointer user_data)
{
    ESelectNamesCompletion *comp = E_SELECT_NAMES_COMPLETION (user_data);


    if (e_completion_searching (E_COMPLETION (comp))) {
        book_query_process_card_list (comp, cards);

        /* Save the list of matching cards. */
        while (cards) {
            comp->priv->cached_cards = g_list_prepend (comp->priv->cached_cards, cards->data);
            gtk_object_ref (GTK_OBJECT (cards->data));
            cards = g_list_next (cards);
        }
    }
}

static void
e_select_names_completion_seq_complete_cb (EBookView *book_view, gpointer user_data)
{
    ESelectNamesCompletion *comp = E_SELECT_NAMES_COMPLETION (user_data);

    /*
     * We aren't searching, but the addressbook has changed -- clear our card cache so that
     * future completion requests will take the changes into account.
     */
    if (! e_completion_searching (E_COMPLETION (comp))) {
        e_select_names_completion_clear_cache (comp);
        return;
    }

    g_free (comp->priv->query_text);
    comp->priv->query_text = NULL;

    if (out)
        fprintf (out, "ending search\n");

    e_completion_end_search (E_COMPLETION (comp)); /* That's all folks! */

    /* Need to launch a new completion if another one is pending. */
    if (comp->priv->waiting_query) {
        gchar *s = comp->priv->waiting_query;
        comp->priv->waiting_query = NULL;
        e_completion_begin_search (E_COMPLETION (comp), s, comp->priv->waiting_pos, comp->priv->waiting_limit);
        g_free (s);
    }
}

static void
e_select_names_completion_stop_query (ESelectNamesCompletion *comp)
{
    g_return_if_fail (comp && E_IS_SELECT_NAMES_COMPLETION (comp));

    if (out)
        fprintf (out, "stopping query\n");

    if (comp->priv->waiting_query) {
        if (out)
            fprintf (out, "stopped waiting query\n");
        g_free (comp->priv->waiting_query);
        comp->priv->waiting_query = NULL;
    }

    g_free (comp->priv->query_text);
    comp->priv->query_text = NULL;

    if (comp->priv->book_view_tag) {
        if (out)
            fprintf (out, "cancelled book view creation\n");
        e_book_cancel (comp->priv->book, comp->priv->book_view_tag);
        comp->priv->book_view_tag = 0;
    }

    if (comp->priv->book_view) {

        if (out)
            fprintf (out, "disconnecting book view signals\n");

        if (comp->priv->card_added_tag) {
            gtk_signal_disconnect (GTK_OBJECT (comp->priv->book_view), comp->priv->card_added_tag);
            comp->priv->card_added_tag = 0;
        }
        if (comp->priv->seq_complete_tag) {
            gtk_signal_disconnect (GTK_OBJECT (comp->priv->book_view), comp->priv->seq_complete_tag);
            comp->priv->seq_complete_tag = 0;
        }
    
        if (out)
            fprintf (out, "unrefed book view\n");
        gtk_object_unref (GTK_OBJECT (comp->priv->book_view));
        comp->priv->book_view = NULL;
    }

    /* Clear the cache, which may contain partial results. */
    e_select_names_completion_clear_cache (comp);

}

static void
e_select_names_completion_start_query (ESelectNamesCompletion *comp, const gchar *query_text)
{
    g_return_if_fail (comp && E_IS_SELECT_NAMES_COMPLETION (comp));
    g_return_if_fail (query_text);

    e_select_names_completion_stop_query (comp);  /* Stop any prior queries. */

    if (comp->priv->book_ready) {
        gchar *sexp;
    
        g_free (comp->priv->query_text);
        comp->priv->query_text = g_strdup (query_text);

        g_free (comp->priv->cached_query_text);
        comp->priv->cached_query_text = g_strdup (query_text);

        sexp = book_query_sexp (comp);
        if (sexp && *sexp) {

            if (out)
                fprintf (out, "\n\n**** starting query: \"%s\"\n", comp->priv->query_text);

            comp->priv->book_view_tag = e_book_get_book_view (comp->priv->book, sexp, 
                                      e_select_names_completion_got_book_view_cb, comp);

            if (! comp->priv->book_view_tag)
                g_warning ("Exception calling e_book_get_book_view");

        } else {
            g_free (comp->priv->query_text);
            comp->priv->query_text = NULL;
        }
        g_free (sexp);

    } else {

        comp->priv->waiting_query = g_strdup (query_text);

    }
}

static void
e_select_names_completion_do_query (ESelectNamesCompletion *comp, const gchar *query_text, gint pos, gint limit)
{
    gchar *clean;
    gboolean query_is_still_running, can_reuse_cached_cards;

    g_return_if_fail (comp != NULL);
    g_return_if_fail (E_IS_SELECT_NAMES_COMPLETION (comp));

    clean = clean_query_text (query_text);
    if (! (clean && *clean)) {
        g_free (clean);
        e_completion_end_search (E_COMPLETION (comp));
        return;
    }

    query_is_still_running = comp->priv->book_view_tag;

    if (out) {
        fprintf (out, "do_query: %s => %s\n", query_text, clean);
        if (query_is_still_running)
            fprintf (out, "a query is still running!\n");
    }
    if (comp->priv->cached_query_text && out)
        fprintf (out, "cached: %s\n", comp->priv->cached_query_text);

    can_reuse_cached_cards = (comp->priv->cached_query_text
                  && (!comp->priv->can_fail_due_to_too_many_hits || comp->priv->cached_cards != NULL)
                  && (strlen (comp->priv->cached_query_text) <= strlen (clean))
                  && !g_utf8_strncasecmp (comp->priv->cached_query_text, clean, strlen (comp->priv->cached_query_text)));


    if (can_reuse_cached_cards) {

        if (out)
            fprintf (out, "can reuse cached card!\n");

        if (query_is_still_running) {
            g_free (comp->priv->waiting_query);
            comp->priv->waiting_query = clean;
            comp->priv->waiting_pos = pos;
            comp->priv->waiting_limit = limit;
            if (out)
                fprintf (out, "waiting for running query to complete: %s\n", comp->priv->waiting_query);
            return;
        }

        g_free (comp->priv->query_text);
        comp->priv->query_text = clean;
        if (out)
            fprintf (out, "using existing query info: %s (vs %s)\n", comp->priv->query_text, comp->priv->cached_query_text);
        book_query_process_card_list (comp, comp->priv->cached_cards);
        e_completion_end_search (E_COMPLETION (comp));
        return;
    }
    
    e_select_names_completion_start_query (comp, clean);
    g_free (clean);
}


/*
 *
 *  Completion Search Override - a Framework for Christian-Resurrection-Holiday Edible-Chicken-Ova
 *
 */

typedef struct _SearchOverride SearchOverride;
struct _SearchOverride {
    const gchar *trigger;
    const gchar *text[4];
};
static SearchOverride override[] = { 
    { "why?", { "\"I must create a system, or be enslaved by another man's.\"",
            "            -- Wiliam Blake, \"Jerusalem\"",
            NULL } },
    { "easter-egg?", { "What were you expecting, a flight simulator?", NULL } },
    { NULL, { NULL } } };

static gboolean
search_override_check (SearchOverride *over, const gchar *text)
{
    /* The g_utf8_validate is needed because as of 2001-06-11,
     * EText doesn't translate from locale->UTF8 when you paste
     * into it.
     */
    if (over == NULL || text == NULL || !g_utf8_validate (text, -1, NULL))
        return FALSE;

    return !g_utf8_strcasecmp (over->trigger, text);
}


/*
 *
 *  Completion Callbacks
 *
 */

static void
e_select_names_completion_handle_request (ECompletion *comp, const gchar *text, gint pos, gint limit)
{
    ESelectNamesCompletion *selcomp = E_SELECT_NAMES_COMPLETION (comp);
    const gchar *str;
    gint index, j;
    
    g_return_if_fail (comp != NULL);
    g_return_if_fail (E_IS_SELECT_NAMES_COMPLETION (comp));
    g_return_if_fail (text != NULL);
    
    if (out) {
        fprintf (out, "\n\n**** requesting completion\n");
        fprintf (out, "text=\"%s\" pos=%d limit=%d\n", text, pos, limit);
    }

    e_select_names_model_text_pos (selcomp->priv->model, pos, &index, NULL, NULL);
    str = index >= 0 ? e_select_names_model_get_string (selcomp->priv->model, index) : NULL;

    if (out)
        fprintf (out, "index=%d str=\"%s\"\n", index, str);

    if (str == NULL || *str == '\0') {
        if (out)
            fprintf (out, "aborting empty query\n");
        e_completion_end_search (comp);
        return;
    }

    for (j=0; override[j].trigger; ++j) {
        if (search_override_check (&(override[j]), str)) {
            gint k;

            for (k=0; override[j].text[k]; ++k) {
                ECompletionMatch *match = g_new (ECompletionMatch, 1);
                e_completion_match_construct (match);
                e_completion_match_set_text (match, text, override[j].text[k]);
                match->score = 1 / (double) (k + 1);
                e_completion_found_match (comp, match);
            }

            e_completion_end_search (comp);
            return;
        }
    }

    e_select_names_completion_do_query (selcomp, str, pos, limit);
}

static void
e_select_names_completion_end (ECompletion *comp)
{
    g_return_if_fail (comp != NULL);
    g_return_if_fail (E_IS_COMPLETION (comp));

    if (out)
        fprintf (out, "completion ended\n");
}

static void
check_capabilities (ESelectNamesCompletion *comp, EBook *book)
{
    gchar *cap = e_book_get_static_capabilities (book);
    comp->priv->can_fail_due_to_too_many_hits = !strcmp (cap, "net");
    if (comp->priv->can_fail_due_to_too_many_hits) {
        g_message ("using LDAP source for completion!");
    }
    g_free (cap);
}

static void
e_select_names_completion_book_ready (EBook *book, EBookStatus status, ESelectNamesCompletion *comp)
{
    comp->priv->book_ready = TRUE;

    g_return_if_fail (E_IS_BOOK (book));
    g_return_if_fail (E_IS_SELECT_NAMES_COMPLETION (comp));

    check_capabilities (comp, book);

    /* If waiting_query is non-NULL, someone tried to start a query before the book was ready.
       Now that it is, get started. */
    if (comp->priv->waiting_query) {
        e_select_names_completion_start_query (comp, comp->priv->waiting_query);
        g_free (comp->priv->waiting_query);
        comp->priv->waiting_query = NULL;
    }

    gtk_object_unref (GTK_OBJECT (comp)); /* post-async unref */
}


/*
 *
 *  Our Pseudo-Constructor
 *
 */

ECompletion *
e_select_names_completion_new (EBook *book, ESelectNamesModel *model)
{
    ESelectNamesCompletion *comp;

    g_return_val_if_fail (book == NULL || E_IS_BOOK (book), NULL);
    g_return_val_if_fail (model, NULL);
    g_return_val_if_fail (E_IS_SELECT_NAMES_MODEL (model), NULL);

    comp = (ESelectNamesCompletion *) gtk_type_new (e_select_names_completion_get_type ());

    if (book == NULL) {

        comp->priv->book = e_book_new ();
        gtk_object_ref (GTK_OBJECT (comp->priv->book));
        gtk_object_sink (GTK_OBJECT (comp->priv->book));

        comp->priv->book_ready = FALSE;
        gtk_object_ref (GTK_OBJECT (comp)); /* ref ourself before our async call */
        e_book_load_local_address_book (comp->priv->book, (EBookCallback) e_select_names_completion_book_ready, comp);

    } else {

        comp->priv->book = book;
        gtk_object_ref (GTK_OBJECT (comp->priv->book));
        comp->priv->book_ready = TRUE;

        check_capabilities (comp, book);

    }
        
    comp->priv->model = model;
    gtk_object_ref (GTK_OBJECT (model));

    return E_COMPLETION (comp);
}

gboolean
e_select_names_completion_get_match_contact_lists (ESelectNamesCompletion *comp)
{
    g_return_val_if_fail (E_IS_SELECT_NAMES_COMPLETION (comp), FALSE);
    return comp->priv->match_contact_lists;
}


void
e_select_names_completion_set_match_contact_lists (ESelectNamesCompletion *comp, gboolean x)
{
    g_return_if_fail (E_IS_SELECT_NAMES_COMPLETION (comp));
    comp->priv->match_contact_lists = x;
}