aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/gui/widgets/e-addressbook-view.c
blob: 709b0fdfded9fcd16e72d2d1f1b81d0b490e6769 (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
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* 
 * e-table-field-chooser.c
 * Copyright (C) 2000  Ximian, Inc.
 * Author: Chris Lahey <clahey@ximian.com>
 *
 * This library 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 library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>

#include <gtk/gtkinvisible.h>

#include <libgnome/gnome-paper.h>
#include <libgnome/gnome-i18n.h>
#include <libgnome/gnome-util.h>
#include <gal/e-table/e-table-scrolled.h>
#include <gal/e-table/e-table-model.h>
#include <gal/widgets/e-scroll-frame.h>
#include <gal/widgets/e-popup-menu.h>
#include <gal/menus/gal-view-factory-etable.h>
#include <gal/menus/gal-view-etable.h>
#include <libgnomeui/gnome-dialog-util.h>

#include <libgnomeprint/gnome-print.h>
#include <libgnomeprint/gnome-print-dialog.h>
#include <libgnomeprint/gnome-print-master.h>
#include <libgnomeprint/gnome-print-master-preview.h>

#include "addressbook/printing/e-contact-print.h"
#include "addressbook/printing/e-contact-print-envelope.h"

#include "gal-view-factory-minicard.h"
#include "gal-view-minicard.h"

#include "e-addressbook-view.h"
#include "e-addressbook-model.h"
#include "e-addressbook-util.h"
#include "e-addressbook-table-adapter.h"
#include "e-addressbook-reflow-adapter.h"
#include "e-minicard-view-widget.h"
#include "e-contact-save-as.h"
#include "e-card-merging.h"

#include "e-contact-editor.h"
#include <ctype.h>

static void e_addressbook_view_init     (EAddressbookView        *card);
static void e_addressbook_view_class_init   (EAddressbookViewClass   *klass);
static void e_addressbook_view_set_arg (GtkObject *o, GtkArg *arg, guint arg_id);
static void e_addressbook_view_get_arg (GtkObject *object, GtkArg *arg, guint arg_id);
static void e_addressbook_view_destroy (GtkObject *object);
static void change_view_type (EAddressbookView *view, EAddressbookViewType view_type);

static void status_message (GtkObject *object, const gchar *status, EAddressbookView *eav);
static void stop_state_changed (GtkObject *object, EAddressbookView *eav);
static void writable_status (GtkObject *object, gboolean writable, EAddressbookView *eav);
static void command_state_change (EAddressbookView *eav);

static void selection_clear_event (GtkWidget *invisible, GdkEventSelection *event,
                   EAddressbookView *view);
static void selection_received (GtkWidget *invisible, GtkSelectionData *selection_data,
                guint time, EAddressbookView *view);
static void selection_get (GtkWidget *invisible, GtkSelectionData *selection_data,
               guint info, guint time_stamp, EAddressbookView *view);
static void invisible_destroyed (GtkWidget *invisible, EAddressbookView *view);

static GtkTableClass *parent_class = NULL;

/* The arguments we take */
enum {
    ARG_0,
    ARG_BOOK,
    ARG_QUERY,
    ARG_TYPE,
};

enum {
    STATUS_MESSAGE,
    COMMAND_STATE_CHANGE,
    LAST_SIGNAL
};

enum DndTargetType {
    DND_TARGET_TYPE_VCARD,
};
#define VCARD_TYPE "text/x-vcard"
static GtkTargetEntry drag_types[] = {
    { VCARD_TYPE, 0, DND_TARGET_TYPE_VCARD },
};
static const int num_drag_types = sizeof (drag_types) / sizeof (drag_types[0]);

static guint e_addressbook_view_signals [LAST_SIGNAL] = {0, };

static GdkAtom clipboard_atom = GDK_NONE;

GtkType
e_addressbook_view_get_type (void)
{
    static GtkType type = 0;

    if (!type) {
        static const GtkTypeInfo info =
        {
            "EAddressbookView",
            sizeof (EAddressbookView),
            sizeof (EAddressbookViewClass),
            (GtkClassInitFunc) e_addressbook_view_class_init,
            (GtkObjectInitFunc) e_addressbook_view_init,
            /* reserved_1 */ NULL,
                /* reserved_2 */ NULL,
            (GtkClassInitFunc) NULL,
        };
        
        type = gtk_type_unique (gtk_table_get_type (), &info);
    }

    return type;
}

static void
e_addressbook_view_class_init (EAddressbookViewClass *klass)
{
    GtkObjectClass *object_class;

    object_class = GTK_OBJECT_CLASS(klass);

    parent_class = gtk_type_class (gtk_table_get_type ());

    object_class->set_arg = e_addressbook_view_set_arg;
    object_class->get_arg = e_addressbook_view_get_arg;
    object_class->destroy = e_addressbook_view_destroy;

    gtk_object_add_arg_type ("EAddressbookView::book", GTK_TYPE_OBJECT,
                 GTK_ARG_READWRITE, ARG_BOOK);
    gtk_object_add_arg_type ("EAddressbookView::query", GTK_TYPE_STRING,
                 GTK_ARG_READWRITE, ARG_QUERY);
    gtk_object_add_arg_type ("EAddressbookView::type", GTK_TYPE_ENUM,
                 GTK_ARG_READWRITE, ARG_TYPE);

    e_addressbook_view_signals [STATUS_MESSAGE] =
        gtk_signal_new ("status_message",
                GTK_RUN_LAST,
                object_class->type,
                GTK_SIGNAL_OFFSET (EAddressbookViewClass, status_message),
                gtk_marshal_NONE__POINTER,
                GTK_TYPE_NONE, 1, GTK_TYPE_POINTER);

    e_addressbook_view_signals [COMMAND_STATE_CHANGE] =
        gtk_signal_new ("command_state_change",
                GTK_RUN_LAST,
                object_class->type,
                GTK_SIGNAL_OFFSET (EAddressbookViewClass, command_state_change),
                gtk_marshal_NONE__NONE,
                GTK_TYPE_NONE, 0);

    gtk_object_class_add_signals (object_class, e_addressbook_view_signals, LAST_SIGNAL);

    if (!clipboard_atom)
        clipboard_atom = gdk_atom_intern ("CLIPBOARD", FALSE);
}

static void
e_addressbook_view_init (EAddressbookView *eav)
{
    eav->view_type = E_ADDRESSBOOK_VIEW_NONE;

    eav->model = e_addressbook_model_new ();

    gtk_signal_connect (GTK_OBJECT(eav->model),
                "status_message",
                GTK_SIGNAL_FUNC (status_message),
                eav);

    gtk_signal_connect (GTK_OBJECT(eav->model),
                "stop_state_changed",
                GTK_SIGNAL_FUNC (stop_state_changed),
                eav);

    gtk_signal_connect (GTK_OBJECT(eav->model),
                "writable_status",
                GTK_SIGNAL_FUNC (writable_status),
                eav);

    eav->editable = FALSE;
    eav->book = NULL;
    eav->query = g_strdup("(contains \"x-evolution-any-field\" \"\")");

    eav->object = NULL;
    eav->widget = NULL;

    eav->view_collection = NULL;
    eav->view_menus = NULL;

    eav->invisible = gtk_invisible_new ();

    gtk_selection_add_target (eav->invisible,
                  clipboard_atom,
                  GDK_SELECTION_TYPE_STRING,
                  0);
        
    gtk_signal_connect (GTK_OBJECT(eav->invisible), "selection_get",
                GTK_SIGNAL_FUNC (selection_get), 
                eav);
    gtk_signal_connect (GTK_OBJECT(eav->invisible), "selection_clear_event",
                GTK_SIGNAL_FUNC (selection_clear_event),
                eav);
    gtk_signal_connect (GTK_OBJECT(eav->invisible), "selection_received",
                GTK_SIGNAL_FUNC (selection_received),
                eav);
    gtk_signal_connect (GTK_OBJECT(eav->invisible), "destroy",
                GTK_SIGNAL_FUNC (invisible_destroyed),
                eav);
}

static void
e_addressbook_view_destroy (GtkObject *object)
{
    EAddressbookView *eav = E_ADDRESSBOOK_VIEW(object);

    if (eav->model)
        gtk_object_unref(GTK_OBJECT(eav->model));

    if (eav->book)
        gtk_object_unref(GTK_OBJECT(eav->book));
    g_free(eav->query);

    if (eav->view_collection) {
        gtk_object_unref (GTK_OBJECT (eav->view_collection));
        eav->view_collection = NULL;
    }

    if (eav->view_menus) {
        gtk_object_unref (GTK_OBJECT (eav->view_menus));
        eav->view_menus = NULL;
    }

    if (eav->clipboard_cards) {
        g_list_foreach (eav->clipboard_cards, (GFunc)gtk_object_unref, NULL);
        g_list_free (eav->clipboard_cards);
        eav->clipboard_cards = NULL;
    }
        
    if (eav->invisible)
        gtk_widget_destroy (eav->invisible);

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

GtkWidget*
e_addressbook_view_new (void)
{
    GtkWidget *widget = GTK_WIDGET (gtk_type_new (e_addressbook_view_get_type ()));
    return widget;
}

static void
book_writable_cb (EBook *book, gboolean writable, EAddressbookView *eav)
{
    eav->editable = writable;
    gtk_object_set (GTK_OBJECT (eav->model),
            "editable", eav->editable,
            NULL);
}

static void
e_addressbook_view_set_arg (GtkObject *object, GtkArg *arg, guint arg_id)
{
    EAddressbookView *eav = E_ADDRESSBOOK_VIEW(object);

    switch (arg_id){
    case ARG_BOOK:
        if (eav->book) {
            gtk_object_unref(GTK_OBJECT(eav->book));
        }
        if (GTK_VALUE_OBJECT(*arg)) {
            eav->book = E_BOOK(GTK_VALUE_OBJECT(*arg));
            gtk_object_ref(GTK_OBJECT(eav->book));
            gtk_signal_connect (GTK_OBJECT (eav->book),
                        "writable_status",
                        book_writable_cb, eav);
        }
        else
            eav->book = NULL;
        gtk_object_set(GTK_OBJECT(eav->model),
                   "book", eav->book,
                   "editable", eav->editable,
                   NULL);

        break;
    case ARG_QUERY:
        g_free(eav->query);
        eav->query = g_strdup(GTK_VALUE_STRING(*arg));
        if (!eav->query)
            eav->query = g_strdup("(contains \"x-evolution-any-field\" \"\")");
        gtk_object_set(GTK_OBJECT(eav->model),
                   "query", eav->query,
                   NULL);
        break;
    case ARG_TYPE:
        change_view_type(eav, GTK_VALUE_ENUM(*arg));
        break;
    default:
        break;
    }
}

static void
e_addressbook_view_get_arg (GtkObject *object, GtkArg *arg, guint arg_id)
{
    EAddressbookView *eav = E_ADDRESSBOOK_VIEW(object);

    switch (arg_id) {
    case ARG_BOOK:
        if (eav->book)
            GTK_VALUE_OBJECT (*arg) = GTK_OBJECT(eav->book);
        else
            GTK_VALUE_OBJECT (*arg) = NULL;
        break;
    case ARG_QUERY:
        GTK_VALUE_STRING (*arg) = eav->query;
        break;
    case ARG_TYPE:
        GTK_VALUE_ENUM (*arg) = eav->view_type;
        break;
    default:
        arg->type = GTK_TYPE_INVALID;
        break;
    }
}



typedef struct {
    EAddressbookView *view;
    char letter;
} LetterClosure;

static void
jump_to_letter(GtkWidget *button, LetterClosure *closure)
{
    char *query;
    switch (closure->letter) {
    case '1':
        query = g_strdup ("(not (or "
                  "(beginswith \"file_as\" \"a\") "
                  "(beginswith \"file_as\" \"b\") "
                  "(beginswith \"file_as\" \"c\") "
                  "(beginswith \"file_as\" \"d\") "
                  "(beginswith \"file_as\" \"e\") "
                  "(beginswith \"file_as\" \"f\") "
                  "(beginswith \"file_as\" \"g\") "
                  "(beginswith \"file_as\" \"h\") "
                  "(beginswith \"file_as\" \"i\") "
                  "(beginswith \"file_as\" \"j\") "
                  "(beginswith \"file_as\" \"k\") "
                  "(beginswith \"file_as\" \"l\") "
                  "(beginswith \"file_as\" \"m\") "
                  "(beginswith \"file_as\" \"n\") "
                  "(beginswith \"file_as\" \"o\") "
                  "(beginswith \"file_as\" \"p\") "
                  "(beginswith \"file_as\" \"q\") "
                  "(beginswith \"file_as\" \"r\") "
                  "(beginswith \"file_as\" \"s\") "
                  "(beginswith \"file_as\" \"t\") "
                  "(beginswith \"file_as\" \"u\") "
                  "(beginswith \"file_as\" \"v\") "
                  "(beginswith \"file_as\" \"w\") "
                  "(beginswith \"file_as\" \"x\") "
                  "(beginswith \"file_as\" \"y\") "
                  "(beginswith \"file_as\" \"z\") "

                  "(beginswith \"file_as\" \"A\") "
                  "(beginswith \"file_as\" \"B\") "
                  "(beginswith \"file_as\" \"C\") "
                  "(beginswith \"file_as\" \"D\") "
                  "(beginswith \"file_as\" \"E\") "
                  "(beginswith \"file_as\" \"F\") "
                  "(beginswith \"file_as\" \"G\") "
                  "(beginswith \"file_as\" \"H\") "
                  "(beginswith \"file_as\" \"I\") "
                  "(beginswith \"file_as\" \"J\") "
                  "(beginswith \"file_as\" \"K\") "
                  "(beginswith \"file_as\" \"L\") "
                  "(beginswith \"file_as\" \"M\") "
                  "(beginswith \"file_as\" \"N\") "
                  "(beginswith \"file_as\" \"O\") "
                  "(beginswith \"file_as\" \"P\") "
                  "(beginswith \"file_as\" \"Q\") "
                  "(beginswith \"file_as\" \"R\") "
                  "(beginswith \"file_as\" \"S\") "
                  "(beginswith \"file_as\" \"T\") "
                  "(beginswith \"file_as\" \"U\") "
                  "(beginswith \"file_as\" \"V\") "
                  "(beginswith \"file_as\" \"W\") "
                  "(beginswith \"file_as\" \"X\") "
                  "(beginswith \"file_as\" \"Y\") "
                  "(beginswith \"file_as\" \"Z\") "
                  "))");
        break;
    default:
        query = g_strdup_printf ("(or "
                     "(beginswith \"file_as\" \"%c\")"
                     "(beginswith \"file_as\" \"%c\")"
                     ")", closure->letter, toupper (closure->letter));
        break;
    }
    gtk_object_set (GTK_OBJECT (closure->view),
            "query", query,
            NULL);
    g_free (query);
}

static void
free_closure(GtkWidget *button, LetterClosure *closure)
{
    g_free(closure);
}

static void
connect_button (EAddressbookView *view, GladeXML *gui, char letter)
{
    char *name;
    GtkWidget *button;
    LetterClosure *closure;
    name = g_strdup_printf("button-%c", letter);
    button = glade_xml_get_widget(gui, name);
    g_free(name);
    if (!button)
        return;
    closure = g_new(LetterClosure, 1);
    closure->view = view;
    closure->letter = letter;
    gtk_signal_connect(GTK_OBJECT(button), "clicked",
               GTK_SIGNAL_FUNC(jump_to_letter), closure);
    gtk_signal_connect(GTK_OBJECT(button), "destroy",
               GTK_SIGNAL_FUNC(free_closure), closure);
}

static GtkWidget *
create_alphabet (EAddressbookView *view)
{
    GtkWidget *widget;
    char letter;
    GladeXML *gui = glade_xml_new (EVOLUTION_GLADEDIR "/alphabet.glade", NULL);

    widget = glade_xml_get_widget(gui, "scrolledwindow-top");
    if (!widget) {
        return NULL;
    }
    
    connect_button(view, gui, '1');
    for (letter = 'a'; letter <= 'z'; letter ++) {
        connect_button(view, gui, letter);
    }
    
    gtk_object_unref(GTK_OBJECT(gui));
    return widget;
}

static void
minicard_selection_change (EMinicardViewWidget *widget, EAddressbookView *view)
{
    command_state_change (view);
}

static void
create_minicard_view (EAddressbookView *view)
{
    GtkWidget *scrollframe;
    GtkWidget *alphabet;
    GtkWidget *minicard_view;
    GtkWidget *minicard_hbox;
    EAddressbookReflowAdapter *adapter;

    gtk_widget_push_visual (gdk_rgb_get_visual ());
    gtk_widget_push_colormap (gdk_rgb_get_cmap ());

    minicard_hbox = gtk_hbox_new(FALSE, 0);

    adapter = E_ADDRESSBOOK_REFLOW_ADAPTER(e_addressbook_reflow_adapter_new (view->model));
    minicard_view = e_minicard_view_widget_new(adapter);

    gtk_signal_connect(GTK_OBJECT(minicard_view), "selection_change",
               GTK_SIGNAL_FUNC(minicard_selection_change), view);


    view->object = GTK_OBJECT(minicard_view);
    view->widget = minicard_hbox;

    scrollframe = e_scroll_frame_new (NULL, NULL);
    e_scroll_frame_set_policy (E_SCROLL_FRAME (scrollframe),
                   GTK_POLICY_AUTOMATIC,
                   GTK_POLICY_AUTOMATIC);

    gtk_container_add (GTK_CONTAINER (scrollframe), minicard_view);


    gtk_box_pack_start(GTK_BOX(minicard_hbox), scrollframe, TRUE, TRUE, 0);

    alphabet = create_alphabet(view);
    if (alphabet) {
        gtk_object_ref(GTK_OBJECT(alphabet));
        gtk_widget_unparent(alphabet);
        gtk_box_pack_start(GTK_BOX(minicard_hbox), alphabet, FALSE, FALSE, 0);
        gtk_object_unref(GTK_OBJECT(alphabet));
    }

    gtk_table_attach(GTK_TABLE(view), minicard_hbox,
             0, 1,
             0, 1,
             GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND,
             0, 0);

    gtk_widget_show_all( GTK_WIDGET(minicard_hbox) );

    gtk_widget_pop_visual ();
    gtk_widget_pop_colormap ();

    e_reflow_model_changed (E_REFLOW_MODEL (adapter));

    gtk_object_unref (GTK_OBJECT (adapter));
}

static void
table_double_click(ETableScrolled *table, gint row, gint col, GdkEvent *event, EAddressbookView *view)
{
    if (E_IS_ADDRESSBOOK_TABLE_ADAPTER(view->object)) {
        EAddressbookModel *model = view->model;
        ECard *card = e_addressbook_model_get_card(model, row);
        EBook *book;

        gtk_object_get(GTK_OBJECT(model),
                   "book", &book,
                   NULL);
        
        g_assert (E_IS_BOOK (book));

        if (e_card_evolution_list (card))
            e_addressbook_show_contact_list_editor (book, card, FALSE, view->editable);
        else
            e_addressbook_show_contact_editor (book, card, FALSE, view->editable);
    }
}

typedef struct {
    EBook *book;
    ECard *card;
    EAddressbookView *view;
    GtkWidget *widget;
    gpointer closure;
} CardAndBook;

static void
card_and_book_free (CardAndBook *card_and_book)
{
    gtk_object_unref(GTK_OBJECT(card_and_book->card));
    gtk_object_unref(GTK_OBJECT(card_and_book->book));
    gtk_object_unref(GTK_OBJECT(card_and_book->view));
}

static void
get_card_list_1(gint model_row,
          gpointer closure)
{
    CardAndBook *card_and_book;
    GList **list;
    EAddressbookView *view;
    ECard *card;

    card_and_book = closure;
    list = card_and_book->closure;
    view = card_and_book->view;

    card = e_addressbook_model_get_card(view->model, model_row);
    *list = g_list_prepend(*list, card);
}

static GList *
get_card_list (CardAndBook *card_and_book)
{
    GList *list = NULL;
    ETable *table;

    table = E_TABLE(card_and_book->widget);
    card_and_book->closure = &list;
    e_table_selected_row_foreach(table,
                     get_card_list_1,
                     card_and_book);
    return list;
}

static void
save_as (GtkWidget *widget, CardAndBook *card_and_book)
{
    e_contact_save_as(_("Save as VCard"), card_and_book->card);
    card_and_book_free(card_and_book);
}

static void
send_as (GtkWidget *widget, CardAndBook *card_and_book)
{
    e_card_send(card_and_book->card, E_CARD_DISPOSITION_AS_ATTACHMENT);
    card_and_book_free(card_and_book);
}

static void
send_to (GtkWidget *widget, CardAndBook *card_and_book)

{
    e_card_send(card_and_book->card, E_CARD_DISPOSITION_AS_TO);
    card_and_book_free(card_and_book);
}

static void
print (GtkWidget *widget, CardAndBook *card_and_book)
{
    gtk_widget_show(e_contact_print_card_dialog_new(card_and_book->card));
    card_and_book_free(card_and_book);
}

#if 0 /* Envelope printing is disabled for Evolution 1.0. */
static void
print_envelope (GtkWidget *widget, CardAndBook *card_and_book)
{
    gtk_widget_show(e_contact_print_envelope_dialog_new(card_and_book->card));
    card_and_book_free(card_and_book);
}
#endif

static void
delete (GtkWidget *widget, CardAndBook *card_and_book)
{
    if (e_contact_editor_confirm_delete(GTK_WINDOW(gtk_widget_get_toplevel(card_and_book->widget)))) {
        GList *list = get_card_list(card_and_book);
        GList *iterator;
        for (iterator = list; iterator; iterator = iterator->next) {
            ECard *card = iterator->data;
            /* Add the card in the contact editor to our ebook */
            e_book_remove_card (card_and_book->book,
                        card,
                        NULL,
                        NULL);
        }
        e_free_object_list(list);
    }
    card_and_book_free(card_and_book);
}

static gint
table_right_click(ETableScrolled *table, gint row, gint col, GdkEvent *event, EAddressbookView *view)
{
    if (E_IS_ADDRESSBOOK_TABLE_ADAPTER(view->object)) {
        EAddressbookModel *model = view->model;
        CardAndBook *card_and_book;

        EPopupMenu menu[] = {
            {N_("Save as VCard"), NULL, GTK_SIGNAL_FUNC(save_as), NULL, 0}, 
            {N_("Send contact to other"), NULL, GTK_SIGNAL_FUNC(send_as), NULL, 0},
            {N_("Send message to contact"), NULL, GTK_SIGNAL_FUNC(send_to), NULL, 0},
            {N_("Print"), NULL, GTK_SIGNAL_FUNC(print), NULL, 0},
#if 0 /* Envelope printing is disabled for Evolution 1.0. */
            {N_("Print Envelope"), NULL, GTK_SIGNAL_FUNC(print_envelope), NULL, 0},
#endif
            {N_("Delete"), NULL, GTK_SIGNAL_FUNC(delete), NULL, 0},
            {NULL, NULL, NULL, NULL, 0}
        };

        card_and_book = g_new(CardAndBook, 1);
        card_and_book->card = e_addressbook_model_get_card(model, row);
        card_and_book->widget = GTK_WIDGET(table);
        card_and_book->view = view;
        gtk_object_get(GTK_OBJECT(model),
                   "book", &(card_and_book->book),
                   NULL);

        gtk_object_ref(GTK_OBJECT(card_and_book->book));
        gtk_object_ref(GTK_OBJECT(card_and_book->view));

        e_popup_menu_run (menu, event, 0, 0, card_and_book);
        return TRUE;
    } else
        return FALSE;
}

static void
table_selection_change(ETableScrolled *table, EAddressbookView *view)
{
    command_state_change (view);
}

static void
table_drag_data_get (ETable             *table,
             int                 row,
             int                 col,
             GdkDragContext     *context,
             GtkSelectionData   *selection_data,
             guint               info,
             guint               time,
             gpointer            user_data)
{
    EAddressbookView *view = user_data;

    printf ("table_drag_data_get (row %d, col %d)\n", row, col);

    if (!E_IS_ADDRESSBOOK_TABLE_ADAPTER(view->object))
        return;

    switch (info) {
    case DND_TARGET_TYPE_VCARD: {
        char *value;

        row = e_table_view_to_model_row (table, row);
        value = e_card_get_vcard(view->model->data[row]);

        gtk_selection_data_set (selection_data,
                    selection_data->target,
                    8,
                    value, strlen (value));
        break;
    }
    }
}

static void
emit_status_message (EAddressbookView *eav, const gchar *status)
{
    gtk_signal_emit (GTK_OBJECT (eav),
             e_addressbook_view_signals [STATUS_MESSAGE],
             status);
}

static void
status_message (GtkObject *object, const gchar *status, EAddressbookView *eav)
{
    emit_status_message (eav, status);
}

static void
stop_state_changed (GtkObject *object, EAddressbookView *eav)
{
    command_state_change (eav);
}

static void
writable_status (GtkObject *object, gboolean writable, EAddressbookView *eav)
{
    command_state_change (eav);
}

static void
command_state_change (EAddressbookView *eav)
{
    gtk_signal_emit (GTK_OBJECT (eav),
             e_addressbook_view_signals [COMMAND_STATE_CHANGE]);
}

#ifdef JUST_FOR_TRANSLATORS
static char *list [] = {
    N_("* Click here to add a contact *"),
    N_("File As"),
    N_("Full Name"),
    N_("Email"),
    N_("Primary Phone"),
    N_("Assistant Phone"),
    N_("Business Phone"),
    N_("Callback Phone"),
    N_("Company Phone"),
    N_("Home Phone"),
    N_("Organization"),
    N_("Business Address"),
    N_("Home Address"),
    N_("Mobile Phone"),
    N_("Car Phone"),
    N_("Business Fax"),
    N_("Home Fax"),
    N_("Business Phone 2"),
    N_("Home Phone 2"),
    N_("ISDN"),
    N_("Other Phone"),
    N_("Other Fax"),
    N_("Pager"),
    N_("Radio"),
    N_("Telex"),
    N_("TTY"),
    N_("Other Address"),
    N_("Email 2"),
    N_("Email 3"),
    N_("Web Site"),
    N_("Department"),
    N_("Office"),
    N_("Title"),
    N_("Profession"),
    N_("Manager"),
    N_("Assistant"),
    N_("Nickname"),
    N_("Spouse"),
    N_("Note"),
    N_("Free-busy URL"),
};
#endif

#define SPEC "<?xml version=\"1.0\"?>      \
<ETableSpecification click-to-add=\"true\" draw-grid=\"true\" _click-to-add-message=\"* Click here to add a contact *\">   \
  <ETableColumn model_col= \"0\" _title=\"File As\"          expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col= \"1\" _title=\"Full Name\"        expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col= \"3\" _title=\"Email\"            expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col= \"4\" _title=\"Primary Phone\"    expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col= \"5\" _title=\"Assistant Phone\"  expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col= \"6\" _title=\"Business Phone\"   expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col= \"7\" _title=\"Callback Phone\"   expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col= \"8\" _title=\"Company Phone\"    expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col= \"9\" _title=\"Home Phone\"       expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"10\" _title=\"Organization\"     expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"11\" _title=\"Business Address\" expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"12\" _title=\"Home Address\"     expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"13\" _title=\"Mobile Phone\"     expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"14\" _title=\"Car Phone\"        expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"15\" _title=\"Business Fax\"     expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"16\" _title=\"Home Fax\"         expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"17\" _title=\"Business Phone 2\" expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"18\" _title=\"Home Phone 2\"     expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"19\" _title=\"ISDN\"             expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"20\" _title=\"Other Phone\"      expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"21\" _title=\"Other Fax\"        expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"22\" _title=\"Pager\"            expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"23\" _title=\"Radio\"            expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"24\" _title=\"Telex\"            expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"25\" _title=\"TTY\"              expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"26\" _title=\"Other Address\"    expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"27\" _title=\"Email 2\"          expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"28\" _title=\"Email 3\"          expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"29\" _title=\"Web Site\"         expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"30\" _title=\"Department\"       expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"31\" _title=\"Office\"           expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"32\" _title=\"Title\"            expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"33\" _title=\"Profession\"       expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"34\" _title=\"Manager\"          expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"35\" _title=\"Assistant\"        expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"36\" _title=\"Nickname\"         expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"37\" _title=\"Spouse\"           expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"38\" _title=\"Note\"             expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableColumn model_col=\"39\" _title=\"Free-busy URL\"    expansion=\"1.0\" minimum_width=\"20\" resizable=\"true\" cell=\"string\" compare=\"string\"/> \
  <ETableState>                            \
    <column source=\"0\"/>                 \
    <column source=\"1\"/>                 \
    <column source=\"5\"/>                 \
    <column source=\"3\"/>                 \
    <column source=\"4\"/>                 \
    <grouping>                             \
      <leaf column=\"0\" ascending=\"true\"/> \
    </grouping>                            \
  </ETableState>                           \
</ETableSpecification>"

static void
create_table_view (EAddressbookView *view)
{
    ETableModel *adapter;
    ECardSimple *simple;
    GtkWidget *table;
    
    simple = e_card_simple_new(NULL);

    adapter = e_addressbook_table_adapter_new(view->model);

    /* Here we create the table.  We give it the three pieces of
       the table we've created, the header, the model, and the
       initial layout.  It does the rest.  */
    table = e_table_scrolled_new (adapter, NULL, SPEC, NULL);

    view->object = GTK_OBJECT(adapter);
    view->widget = table;

    gtk_signal_connect(GTK_OBJECT(e_table_scrolled_get_table(E_TABLE_SCROLLED(table))), "double_click",
               GTK_SIGNAL_FUNC(table_double_click), view);
    gtk_signal_connect(GTK_OBJECT(e_table_scrolled_get_table(E_TABLE_SCROLLED(table))), "right_click",
               GTK_SIGNAL_FUNC(table_right_click), view);
    gtk_signal_connect(GTK_OBJECT(e_table_scrolled_get_table(E_TABLE_SCROLLED(table))), "selection_change",
               GTK_SIGNAL_FUNC(table_selection_change), view);

    /* drag & drop signals */
    e_table_drag_source_set (E_TABLE(E_TABLE_SCROLLED(table)->table), GDK_BUTTON1_MASK,
                 drag_types, num_drag_types, GDK_ACTION_MOVE);
    
    gtk_signal_connect (GTK_OBJECT (E_TABLE_SCROLLED(table)->table),
                "table_drag_data_get",
                GTK_SIGNAL_FUNC (table_drag_data_get),
                view);

    gtk_table_attach(GTK_TABLE(view), table,
             0, 1,
             0, 1,
             GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND,
             0, 0);

    gtk_widget_show( GTK_WIDGET(table) );

    gtk_object_unref(GTK_OBJECT(simple));
}


static void
change_view_type (EAddressbookView *view, EAddressbookViewType view_type)
{
    if (view_type == view->view_type)
        return;

    if (view->widget) {
        gtk_widget_destroy (view->widget);
        view->widget = NULL;
    }
    view->object = NULL;

    switch (view_type) {
    case E_ADDRESSBOOK_VIEW_MINICARD:
        create_minicard_view (view);
        break;
    case E_ADDRESSBOOK_VIEW_TABLE:
        create_table_view (view);
        break;
    default:
        g_warning ("view_type must be either TABLE or MINICARD\n");
        return;
    }

    view->view_type = view_type;

    command_state_change (view);
}

static void
e_contact_print_destroy(GnomeDialog *dialog, gpointer data)
{
    ETableScrolled *table = gtk_object_get_data(GTK_OBJECT(dialog), "table");
    EPrintable *printable = gtk_object_get_data(GTK_OBJECT(dialog), "printable");
    gtk_object_unref(GTK_OBJECT(printable));
    gtk_object_unref(GTK_OBJECT(table));
}

static void
e_contact_print_button(GnomeDialog *dialog, gint button, gpointer data)
{
    GnomePrintMaster *master;
    GnomePrintContext *pc;
    EPrintable *printable = gtk_object_get_data(GTK_OBJECT(dialog), "printable");
    GtkWidget *preview;
    switch( button ) {
    case GNOME_PRINT_PRINT:
        master = gnome_print_master_new_from_dialog( GNOME_PRINT_DIALOG(dialog) );
        pc = gnome_print_master_get_context( master );
        e_printable_reset(printable);
        while (e_printable_data_left(printable)) {
            if (gnome_print_gsave(pc) == -1)
                /* FIXME */;
            if (gnome_print_translate(pc, 72, 72) == -1)
                /* FIXME */;
            e_printable_print_page(printable,
                           pc,
                           6.5 * 72,
                           5 * 72,
                           TRUE);
            if (gnome_print_grestore(pc) == -1)
                /* FIXME */;
            if (gnome_print_showpage(pc) == -1)
                /* FIXME */;
        }
        gnome_print_master_close(master);
        gnome_print_master_print(master);
        gtk_object_unref(GTK_OBJECT(master));
        gnome_dialog_close(dialog);
        break;
    case GNOME_PRINT_PREVIEW:
        master = gnome_print_master_new_from_dialog( GNOME_PRINT_DIALOG(dialog) );
        pc = gnome_print_master_get_context( master );
        e_printable_reset(printable);
        while (e_printable_data_left(printable)) {
            if (gnome_print_gsave(pc) == -1)
                /* FIXME */;
            if (gnome_print_translate(pc, 72, 72) == -1)
                /* FIXME */;
            e_printable_print_page(printable,
                           pc,
                           6.5 * 72,
                           9 * 72,
                           TRUE);
            if (gnome_print_grestore(pc) == -1)
                /* FIXME */;
            if (gnome_print_showpage(pc) == -1)
                /* FIXME */;
        }
        gnome_print_master_close(master);
        preview = GTK_WIDGET(gnome_print_master_preview_new(master, "Print Preview"));
        gtk_widget_show_all(preview);
        gtk_object_unref(GTK_OBJECT(master));
        break;
    case GNOME_PRINT_CANCEL:
        gnome_dialog_close(dialog);
        break;
    }
}

static void
display_view(GalViewCollection *collection,
         GalView *view,
         gpointer data)
{
    EAddressbookView *address_view = data;
    if (GAL_IS_VIEW_ETABLE(view)) {
        change_view_type (address_view, E_ADDRESSBOOK_VIEW_TABLE);
        e_table_set_state_object(e_table_scrolled_get_table(E_TABLE_SCROLLED(address_view->widget)), GAL_VIEW_ETABLE(view)->state);
    } else if (GAL_IS_VIEW_MINICARD(view)) {
        change_view_type (address_view, E_ADDRESSBOOK_VIEW_MINICARD);
    }
}

void
e_addressbook_view_setup_menus (EAddressbookView *view,
                BonoboUIComponent *uic)
{
    GalViewFactory *factory;
    ETableSpecification *spec;
    char *galview;

    g_return_if_fail (view != NULL);
    g_return_if_fail (E_IS_ADDRESSBOOK_VIEW (view));
    g_return_if_fail (uic != NULL);
    g_return_if_fail (BONOBO_IS_UI_COMPONENT (uic));
    g_return_if_fail (view->view_collection == NULL);

    g_assert (view->view_collection == NULL);
    g_assert (view->view_menus == NULL);

    view->view_collection = gal_view_collection_new();

    galview = gnome_util_prepend_user_home("/evolution/views/addressbook/");
    gal_view_collection_set_storage_directories(view->view_collection,
                            EVOLUTION_DATADIR "/evolution/views/addressbook/",
                            galview);
    g_free(galview);

    spec = e_table_specification_new();
    e_table_specification_load_from_string(spec, SPEC);

    factory = gal_view_factory_etable_new (spec);
    gtk_object_unref (GTK_OBJECT (spec));
    gal_view_collection_add_factory (view->view_collection, factory);
    gtk_object_unref (GTK_OBJECT (factory));

    factory = gal_view_factory_minicard_new ();
    gal_view_collection_add_factory (view->view_collection, factory);
    gtk_object_unref (GTK_OBJECT (factory));

    gal_view_collection_load(view->view_collection);

    view->view_menus = gal_view_menus_new(view->view_collection);
    gal_view_menus_apply(view->view_menus, uic, NULL);
    gtk_signal_connect(GTK_OBJECT(view->view_collection), "display_view",
               display_view, view);
}

/**
 * e_addressbook_view_discard_menus:
 * @view: An addressbook view.
 * 
 * Makes an addressbook view discard its GAL view menus and its views collection
 * objects.  This should be called when the corresponding Bonobo component is
 * deactivated.
 **/
void
e_addressbook_view_discard_menus (EAddressbookView *view)
{
    g_return_if_fail (view != NULL);
    g_return_if_fail (E_IS_ADDRESSBOOK_VIEW (view));
    g_return_if_fail (view->view_collection);

    g_assert (view->view_collection != NULL);
    g_assert (view->view_menus != NULL);

    gtk_object_unref (GTK_OBJECT (view->view_collection));
    view->view_collection = NULL;

    gtk_object_unref (GTK_OBJECT (view->view_menus));
    view->view_menus = NULL;
}

static ESelectionModel*
get_selection_model (EAddressbookView *view)
{
    if (view->view_type == E_ADDRESSBOOK_VIEW_MINICARD)
        return e_minicard_view_widget_get_selection_model (E_MINICARD_VIEW_WIDGET(view->object));
    else
        return E_SELECTION_MODEL(E_TABLE_SCROLLED(view->widget)->table->selection);
}


void
e_addressbook_view_print(EAddressbookView *view)
{
    if (view->view_type == E_ADDRESSBOOK_VIEW_MINICARD) {
        char *query;
        EBook *book;
        GtkWidget *print;

        gtk_object_get (GTK_OBJECT(view->model),
                "query", &query,
                "book", &book,
                NULL);
        print = e_contact_print_dialog_new(book, query);
        g_free(query);
        gtk_widget_show_all(print);
    } else if (view->view_type == E_ADDRESSBOOK_VIEW_TABLE) {
        GtkWidget *dialog;
        EPrintable *printable;
        ETable *etable;

        dialog = gnome_print_dialog_new("Print cards", GNOME_PRINT_DIALOG_RANGE | GNOME_PRINT_DIALOG_COPIES);
        gnome_print_dialog_construct_range_any(GNOME_PRINT_DIALOG(dialog), GNOME_PRINT_RANGE_ALL | GNOME_PRINT_RANGE_SELECTION,
                               NULL, NULL, NULL);

        gtk_object_get(GTK_OBJECT(view->widget), "table", &etable, NULL);
        printable = e_table_get_printable(etable);

        gtk_object_ref(GTK_OBJECT(view->widget));

        gtk_object_set_data(GTK_OBJECT(dialog), "table", view->widget);
        gtk_object_set_data(GTK_OBJECT(dialog), "printable", printable);
        
        gtk_signal_connect(GTK_OBJECT(dialog),
                   "clicked", GTK_SIGNAL_FUNC(e_contact_print_button), NULL);
        gtk_signal_connect(GTK_OBJECT(dialog),
                   "destroy", GTK_SIGNAL_FUNC(e_contact_print_destroy), NULL);
        gtk_widget_show(dialog);
    }
}

static void
card_deleted_cb (EBook* book, EBookStatus status, gpointer user_data)
{
    EAddressbookView *view = user_data;

    emit_status_message (view, _("Done."));

    if (status != E_BOOK_STATUS_SUCCESS) {
        e_addressbook_error_dialog (_("Error removing card"), status);
    }
}

static void
do_remove (int i, gpointer user_data)
{
    EBook *book;
    ECard *card;
    EAddressbookView *view = user_data;

    gtk_object_get (GTK_OBJECT(view->model),
            "book", &book,
            NULL);

    card = e_addressbook_model_get_card (view->model, i);

    e_book_remove_card(book, card, card_deleted_cb, view);

    gtk_object_unref (GTK_OBJECT (card));
}

void
e_addressbook_view_delete_selection(EAddressbookView *view)
{
    ESelectionModel *model = get_selection_model (view);

    g_return_if_fail (model);

    emit_status_message (view, _("Removing cards..."));

    e_selection_model_foreach (model,
                   do_remove,
                   view);
}

static void
invisible_destroyed (GtkWidget *invisible, EAddressbookView *view)
{
    view->invisible = NULL;
}

static void
selection_get (GtkWidget *invisible,
           GtkSelectionData *selection_data,
           guint info,
           guint time_stamp,
           EAddressbookView *view)
{
    char *value;

    value = e_card_list_get_vcard(view->clipboard_cards);

    gtk_selection_data_set (selection_data, GDK_SELECTION_TYPE_STRING,
                8, value, strlen (value));
                
}

static void
selection_clear_event (GtkWidget *invisible,
               GdkEventSelection *event,
               EAddressbookView *view)
{
    if (view->clipboard_cards) {
        g_list_foreach (view->clipboard_cards, (GFunc)gtk_object_unref, NULL);
        g_list_free (view->clipboard_cards);
        view->clipboard_cards = NULL;
    }
}

static void
selection_received (GtkWidget *invisible,
            GtkSelectionData *selection_data,
            guint time,
            EAddressbookView *view)
{
    if (selection_data->length < 0 || selection_data->type != GDK_SELECTION_TYPE_STRING) {
        return;
    }
    else {
        /* XXX make sure selection_data->data = \0 terminated */
        GList *card_list = e_card_load_cards_from_string (selection_data->data);
        GList *l;
        
        if (!card_list /* it wasn't a vcard list */)
            return;

        for (l = card_list; l; l = l->next) {
            ECard *card = l->data;

            e_card_merging_book_add_card (view->book, card, NULL /* XXX */, NULL);
        }

        g_list_foreach (card_list, (GFunc)gtk_object_unref, NULL);
        g_list_free (card_list);
    }
}

static void
add_to_list (int model_row, gpointer closure)
{
    GList **list = closure;
    *list = g_list_prepend (*list, GINT_TO_POINTER (model_row));
}

static GList *
get_selected_cards (EAddressbookView *view)
{
    GList *list;
    GList *iterator;
    ESelectionModel *selection = get_selection_model (view);

    list = NULL;
    e_selection_model_foreach (selection, add_to_list, &list);

    for (iterator = list; iterator; iterator = iterator->next) {
        iterator->data = e_addressbook_model_card_at (view->model, GPOINTER_TO_INT (iterator->data));
    }
    list = g_list_reverse (list);
    return list;
}

void
e_addressbook_view_save_as (EAddressbookView *view)
{
    GList *list = get_selected_cards (view);
    if (list)
        e_contact_list_save_as (_("Save as VCard"), list);
    g_list_free (list);
}

void
e_addressbook_view_send (EAddressbookView *view)
{
    GList *list = get_selected_cards (view);
    if (list)
        e_card_list_send (list, E_CARD_DISPOSITION_AS_ATTACHMENT);
    g_list_free (list);
}

void
e_addressbook_view_send_to (EAddressbookView *view)
{
    GList *list = get_selected_cards (view);
    if (list)
        e_card_list_send (list, E_CARD_DISPOSITION_AS_TO);
    g_list_free (list);
}

void
e_addressbook_view_cut (EAddressbookView *view)
{
    e_addressbook_view_copy (view);
    e_addressbook_view_delete_selection (view);
}

void
e_addressbook_view_copy (EAddressbookView *view)
{
    view->clipboard_cards = get_selected_cards (view);

    gtk_selection_owner_set (view->invisible, clipboard_atom, GDK_CURRENT_TIME);
}

void
e_addressbook_view_paste (EAddressbookView *view)
{
    gtk_selection_convert (view->invisible, clipboard_atom,
                   GDK_SELECTION_TYPE_STRING,
                   GDK_CURRENT_TIME);
}

void
e_addressbook_view_select_all (EAddressbookView *view)
{
    ESelectionModel *model = get_selection_model (view);

    g_return_if_fail (model);

    e_selection_model_select_all (model);
}

void
e_addressbook_view_show_all(EAddressbookView *view)
{
    gtk_object_set(GTK_OBJECT(view),
               "query", NULL,
               NULL);
}

void
e_addressbook_view_stop(EAddressbookView *view)
{
    e_addressbook_model_stop (view->model);
}

static gboolean
e_addressbook_view_selection_nonempty (EAddressbookView  *view)
{
    ESelectionModel *selection_model;

    selection_model = get_selection_model (view);
    if (selection_model == NULL)
        return FALSE;

    return e_selection_model_selected_count (selection_model) != 0;
}

gboolean
e_addressbook_view_can_create (EAddressbookView  *view)
{
    return e_addressbook_model_editable (view->model);
}

gboolean
e_addressbook_view_can_print (EAddressbookView  *view)
{
    return e_addressbook_view_selection_nonempty (view);
}

gboolean
e_addressbook_view_can_save_as (EAddressbookView  *view)
{
    return e_addressbook_view_selection_nonempty (view);
}

gboolean   e_addressbook_view_can_send (EAddressbookView  *view)
{
    return e_addressbook_view_selection_nonempty (view);
}

gboolean   e_addressbook_view_can_send_to (EAddressbookView  *view)
{
    return e_addressbook_view_selection_nonempty (view);
}

gboolean
e_addressbook_view_can_delete (EAddressbookView  *view)
{
    return e_addressbook_view_selection_nonempty (view) && e_addressbook_model_editable (view->model);
}

gboolean
e_addressbook_view_can_cut (EAddressbookView *view)
{
    return e_addressbook_view_selection_nonempty (view) && e_addressbook_model_editable (view->model);
}

gboolean
e_addressbook_view_can_copy (EAddressbookView *view)
{
    return e_addressbook_view_selection_nonempty (view);
}

gboolean
e_addressbook_view_can_paste (EAddressbookView *view)
{
    return e_addressbook_model_editable (view->model);
}

gboolean
e_addressbook_view_can_select_all (EAddressbookView *view)
{
    return e_addressbook_model_card_count (view->model) != 0;
}

gboolean
e_addressbook_view_can_stop (EAddressbookView  *view)
{
    return e_addressbook_model_can_stop (view->model);
}