aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/gui/widgets/e-addressbook-view.c
blob: 236b53f16f5ea3565f3f78eb84479c49d9422d6c (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
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* 
 * e-addressbook-view.c
 * Copyright (C) 2000  Ximian, Inc.
 * Author: Chris Lahey <clahey@ximian.com>
 *         Chris Toshok <toshok@ximian.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * 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/gtk.h>

#include <libgnome/gnome-i18n.h>
#include <libgnome/gnome-util.h>
#include <gtk/gtkscrolledwindow.h>
#include <gal/e-table/e-table-scrolled.h>
#include <gal/e-table/e-table-model.h>
#include <gal/widgets/e-popup-menu.h>
#include <gal/widgets/e-gui-utils.h>
#include <gal/menus/gal-view-factory-etable.h>
#include <gal/menus/gal-view-etable.h>
#include <gal/util/e-xml-utils.h>
#include <libgnomeui/gnome-dialog-util.h>

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

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

#include "e-util/e-categories-master-list-wombat.h"
#include "e-util/e-sexp.h"

#ifdef WITH_ADDRESSBOOK_VIEW_TREEVIEW
#include <gal/widgets/e-treeview-selection-model.h>
#include "gal-view-factory-treeview.h"
#include "gal-view-treeview.h"
#endif
#include "gal-view-minicard.h"
#include "gal-view-factory-minicard.h"

#include "eab-marshal.h"
#include "e-addressbook-view.h"
#include "e-addressbook-model.h"
#include "eab-gui-util.h"
#include "util/eab-book-util.h"
#include "e-addressbook-table-adapter.h"
#ifdef WITH_ADDRESSBOOK_VIEW_TREEVIEW
#include "e-addressbook-treeview-adapter.h"
#endif
#include "eab-contact-merging.h"

#include "widgets/misc/e-error.h"

#include "e-contact-editor.h"
#include <gdk/gdkkeysyms.h>
#include <ctype.h>
#include <string.h>

#include <libxml/tree.h>
#include <libxml/parser.h>

#define SHOW_ALL_SEARCH "(contains \"x-evolution-any-field\" \"\")"

#define d(x)

static void eab_view_init       (EABView         *card);
static void eab_view_class_init (EABViewClass    *klass);

static void eab_view_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
static void eab_view_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);

static void eab_view_dispose (GObject *object);
static void change_view_type (EABView *view, EABViewType view_type);

static void status_message     (GtkObject *object, const gchar *status, EABView *eav);
static void search_result      (GtkObject *object, EBookViewStatus status, EABView *eav);
static void folder_bar_message (GtkObject *object, const gchar *status, EABView *eav);
static void stop_state_changed (GtkObject *object, EABView *eav);
static void writable_status    (GtkObject *object, gboolean writable, EABView *eav);
static void backend_died       (GtkObject *object, EABView *eav);
static void contact_changed    (EABModel *model, gint index, EABView *eav);
static void contact_removed    (EABModel *model, gint index, EABView *eav);
static GList *get_selected_contacts (EABView *view);

static void command_state_change (EABView *eav);

static void selection_clear_event (GtkWidget *invisible, GdkEventSelection *event,
                   EABView *view);
static void selection_received (GtkWidget *invisible, GtkSelectionData *selection_data,
                guint time, EABView *view);
static void selection_get (GtkWidget *invisible, GtkSelectionData *selection_data,
               guint info, guint time_stamp, EABView *view);
static void invisible_destroyed (gpointer data, GObject *where_object_was);

static void make_suboptions             (EABView *view);
static void query_changed               (ESearchBar *esb, EABView *view);
static void search_activated            (ESearchBar *esb, EABView *view);
static void search_menu_activated       (ESearchBar *esb, int id, EABView *view);
static void connect_master_list_changed (EABView *view);
static ECategoriesMasterList *get_master_list (void);

#define PARENT_TYPE GTK_TYPE_VBOX
static GtkVBoxClass *parent_class = NULL;

/* The arguments we take */
enum {
    PROP_0,
    PROP_BOOK,
    PROP_SOURCE,
    PROP_QUERY,
    PROP_TYPE,
};

enum {
    STATUS_MESSAGE,
    SEARCH_RESULT,
    FOLDER_BAR_MESSAGE,
    COMMAND_STATE_CHANGE,
    LAST_SIGNAL
};

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

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

static GdkAtom clipboard_atom = GDK_NONE;

static GalViewCollection *collection = NULL;

enum {
    ESB_FULL_NAME,
    ESB_EMAIL,
    ESB_CATEGORY,
    ESB_ANY,
    ESB_ADVANCED
};

static ESearchBarItem addressbook_search_option_items[] = {
    { N_("Name begins with"), ESB_FULL_NAME, NULL },
    { N_("Email begins with"), ESB_EMAIL, NULL },
    { N_("Category is"), ESB_CATEGORY, NULL }, /* We attach subitems below */
    { N_("Any field contains"), ESB_ANY, NULL },
    { N_("Advanced..."), ESB_ADVANCED, NULL },
    { NULL, -1, NULL }
};

static ESearchBarItem addressbook_search_items[] = {
    { N_("Advanced..."), ESB_ADVANCED, NULL },
    { NULL, -1, NULL },
};

GType
eab_view_get_type (void)
{
    static GType type = 0;

    if (!type) {
        static const GTypeInfo info =  {
            sizeof (EABViewClass),
            NULL,           /* base_init */
            NULL,           /* base_finalize */
            (GClassInitFunc) eab_view_class_init,
            NULL,           /* class_finalize */
            NULL,           /* class_data */
            sizeof (EABView),
            0,             /* n_preallocs */
            (GInstanceInitFunc) eab_view_init,
        };

        type = g_type_register_static (PARENT_TYPE, "EABView", &info, 0);
    }

    return type;
}

static void
eab_view_class_init (EABViewClass *klass)
{
    GObjectClass *object_class;
    GtkWidgetClass *widget_class;

    object_class = G_OBJECT_CLASS(klass);
    widget_class = GTK_WIDGET_CLASS(klass);

    parent_class = gtk_type_class (PARENT_TYPE);

    object_class->set_property = eab_view_set_property;
    object_class->get_property = eab_view_get_property;
    object_class->dispose = eab_view_dispose;

    g_object_class_install_property (object_class, PROP_BOOK, 
                     g_param_spec_object ("book",
                                  _("Book"),
                                  /*_( */"XXX blurb" /*)*/,
                                  E_TYPE_BOOK,
                                  G_PARAM_READWRITE));

    g_object_class_install_property (object_class, PROP_SOURCE, 
                     g_param_spec_object ("source",
                                  _("Source"),
                                  /*_( */"XXX blurb" /*)*/,
                                  E_TYPE_SOURCE,
                                  G_PARAM_READWRITE));

    g_object_class_install_property (object_class, PROP_QUERY, 
                     g_param_spec_string ("query",
                                  _("Query"),
                                  /*_( */"XXX blurb" /*)*/,
                                  NULL,
                                  G_PARAM_READWRITE));

    g_object_class_install_property (object_class, PROP_TYPE, 
                     g_param_spec_int ("type",
                               _("Type"),
                               /*_( */"XXX blurb" /*)*/,
                               EAB_VIEW_NONE, 
                               EAB_VIEW_TABLE,
                               EAB_VIEW_NONE,
                               G_PARAM_READWRITE));

    eab_view_signals [STATUS_MESSAGE] =
        g_signal_new ("status_message",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (EABViewClass, status_message),
                  NULL, NULL,
                  eab_marshal_NONE__POINTER,
                  G_TYPE_NONE, 1, G_TYPE_POINTER);

    eab_view_signals [SEARCH_RESULT] =
        g_signal_new ("search_result",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (EABViewClass, search_result),
                  NULL, NULL,
                  eab_marshal_NONE__INT,
                  G_TYPE_NONE, 1, G_TYPE_INT);

    eab_view_signals [FOLDER_BAR_MESSAGE] =
        g_signal_new ("folder_bar_message",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (EABViewClass, folder_bar_message),
                  NULL, NULL,
                  eab_marshal_NONE__POINTER,
                  G_TYPE_NONE, 1, G_TYPE_POINTER);

    eab_view_signals [COMMAND_STATE_CHANGE] =
        g_signal_new ("command_state_change",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (EABViewClass, command_state_change),
                  NULL, NULL,
                  eab_marshal_NONE__NONE,
                  G_TYPE_NONE, 0);

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

    /* init the accessibility support for e_addressbook_view */
    eab_view_a11y_init();
}

static void
eab_view_init (EABView *eav)
{
    eav->view_type = EAB_VIEW_NONE;

    eav->model = NULL;
    eav->object = NULL;
    eav->widget = NULL;
    eav->contact_display_window = NULL;
    eav->contact_display = NULL;
    eav->displayed_contact = -1;

    eav->view_instance = NULL;
    eav->view_menus = NULL;
    eav->current_view = NULL;
    eav->uic = NULL;

    eav->book = NULL;
    eav->source = NULL;
    eav->query = NULL;

    eav->invisible = NULL;
    eav->clipboard_contacts = NULL;
}

static void
eab_view_dispose (GObject *object)
{
    EABView *eav = EAB_VIEW(object);

    if (eav->model) {
        g_signal_handlers_disconnect_matched (eav->model,
                              G_SIGNAL_MATCH_DATA,
                              0, 0, NULL, NULL,
                              object);
        g_object_unref (eav->model);
        eav->model = NULL;
    }

    if (eav->book) {
        g_object_unref (eav->book);
        eav->book = NULL;
    }

    if (eav->source) {
        g_object_unref (eav->source);
        eav->source = NULL;
    }

    if (eav->query) {
        g_free(eav->query);
        eav->query = NULL;
    }

    eav->uic = NULL;

    if (eav->view_instance) {
        g_object_unref (eav->view_instance);
        eav->view_instance = NULL;
    }

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

    if (eav->clipboard_contacts) {
        g_list_foreach (eav->clipboard_contacts, (GFunc)g_object_unref, NULL);
        g_list_free (eav->clipboard_contacts);
        eav->clipboard_contacts = NULL;
    }

    if (eav->contact_display_window) {
        g_object_unref (eav->contact_display_window);
        eav->contact_display_window = NULL;
    }
        
    if (eav->invisible) {
        gtk_widget_destroy (eav->invisible);
        eav->invisible = NULL;
    }

    if (eav->ecml_changed_id != 0) {
        g_signal_handler_disconnect (get_master_list(),
                         eav->ecml_changed_id);
        eav->ecml_changed_id = 0;
    }

    if (G_OBJECT_CLASS(parent_class)->dispose)
        G_OBJECT_CLASS(parent_class)->dispose(object);
}

static void
set_paned_position (EABView *eav)
{
    GConfClient *gconf_client;
    gint         pos;

    /* XXX this should use the addressbook's global gconf client */
    gconf_client = gconf_client_get_default ();
    pos = gconf_client_get_int (gconf_client, "/apps/evolution/addressbook/display/vpane_position", NULL);
    if (pos < 1)
        pos = 144;

    gtk_paned_set_position (GTK_PANED (eav->paned), pos);

    g_object_unref (gconf_client);
}

static gboolean
get_paned_position (EABView *eav)
{
    GConfClient *gconf_client;
    gint pos;

    /* XXX this should use the addressbook's global gconf client */
    gconf_client = gconf_client_get_default ();

    pos = gtk_paned_get_position (GTK_PANED (eav->paned));
    gconf_client_set_int (gconf_client, "/apps/evolution/addressbook/display/vpane_position", pos, NULL);

    g_object_unref (gconf_client);

    return FALSE;
}

GtkWidget*
eab_view_new (void)
{
    GtkWidget *widget = GTK_WIDGET (g_object_new (E_TYPE_AB_VIEW, NULL));
    EABView *eav = EAB_VIEW (widget);
    FilterPart *part;

    /* create our model */
    eav->model = eab_model_new ();

    g_signal_connect (eav->model, "status_message",
              G_CALLBACK (status_message), eav);
    g_signal_connect (eav->model, "search_result",
              G_CALLBACK (search_result), eav);
    g_signal_connect (eav->model, "folder_bar_message",
              G_CALLBACK (folder_bar_message), eav);
    g_signal_connect (eav->model, "stop_state_changed",
              G_CALLBACK (stop_state_changed), eav);
    g_signal_connect (eav->model, "writable_status",
              G_CALLBACK (writable_status), eav);
    g_signal_connect (eav->model, "backend_died",
              G_CALLBACK (backend_died), eav);
    g_signal_connect (eav->model, "contact_changed",
              G_CALLBACK (contact_changed), eav);
    g_signal_connect (eav->model, "contact_removed",
              G_CALLBACK (contact_removed), eav);

    eav->editable = FALSE;
    eav->query = g_strdup (SHOW_ALL_SEARCH);

    /* create our search bar */
    eav->search = E_SEARCH_BAR (e_search_bar_new (NULL, addressbook_search_option_items));
    e_search_bar_set_menu (eav->search, addressbook_search_items);
    make_suboptions (eav);
    connect_master_list_changed (eav);
    g_signal_connect (eav->search, "query_changed",
              G_CALLBACK (query_changed), eav);
    g_signal_connect (eav->search, "search_activated",
              G_CALLBACK (search_activated), eav);
    g_signal_connect (eav->search, "menu_activated",
              G_CALLBACK (search_menu_activated), eav);
    gtk_box_pack_start (GTK_BOX (eav), GTK_WIDGET (eav->search), FALSE, FALSE, 0);
    gtk_widget_show (GTK_WIDGET (eav->search));
    gtk_widget_set_sensitive (GTK_WIDGET (eav->search), FALSE);

    /* create the search context */
    eav->search_context = rule_context_new ();
    rule_context_add_part_set (eav->search_context, "partset", filter_part_get_type (),
                   rule_context_add_part, rule_context_next_part);
    rule_context_load (eav->search_context, SEARCH_RULE_DIR "/addresstypes.xml", "");

    eav->search_rule = filter_rule_new ();
    part = rule_context_next_part (eav->search_context, NULL);

    if (part == NULL)
        g_warning ("Could not load addressbook search; no parts.");
    else
        filter_rule_add_part (eav->search_rule, filter_part_clone (part));

    /* create the paned window and contact display */
    eav->paned = gtk_vpaned_new ();
    gtk_box_pack_start (GTK_BOX (eav), eav->paned, TRUE, TRUE, 0);
    g_signal_connect_swapped (eav->paned, "button_release_event",
                  G_CALLBACK (get_paned_position), eav);

    eav->widget = gtk_label_new ("empty label here");
    gtk_container_add (GTK_CONTAINER (eav->paned), eav->widget);
    gtk_widget_show (eav->widget);

    eav->contact_display = eab_contact_display_new ();
    eav->contact_display_window = gtk_scrolled_window_new (NULL, NULL);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (eav->contact_display_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
    gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (eav->contact_display_window), GTK_SHADOW_IN);
    gtk_container_add (GTK_CONTAINER (eav->contact_display_window), eav->contact_display);
    gtk_container_add (GTK_CONTAINER (eav->paned), eav->contact_display_window);
    gtk_widget_show (eav->contact_display);
    gtk_widget_show (eav->contact_display_window);
    gtk_widget_show (eav->paned);

    /* gtk selection crap */
    eav->invisible = gtk_invisible_new ();

    gtk_selection_add_target (eav->invisible,
                  clipboard_atom,
                  GDK_SELECTION_TYPE_STRING,
                  0);
        
    g_signal_connect (eav->invisible, "selection_get",
              G_CALLBACK (selection_get), 
              eav);
    g_signal_connect (eav->invisible, "selection_clear_event",
              G_CALLBACK (selection_clear_event),
              eav);
    g_signal_connect (eav->invisible, "selection_received",
              G_CALLBACK (selection_received),
              eav);
    g_object_weak_ref (G_OBJECT (eav->invisible), invisible_destroyed, eav);

    return widget;
}

RuleContext *
eab_view_peek_search_context (EABView *view)
{
    return view->search_context;
}

FilterRule *
eab_view_peek_search_rule (EABView *view)
{
    return view->search_rule;
}

static void
writable_status (GtkObject *object, gboolean writable, EABView *eav)
{
    eav->editable = writable;
    command_state_change (eav);
}

static void
init_collection (void)
{
    GalViewFactory *factory;
    ETableSpecification *spec;
    char *galview;

    if (collection == NULL) {
        collection = gal_view_collection_new();

        gal_view_collection_set_title (collection, _("Address Book"));

        galview = gnome_util_prepend_user_home("/.evolution/addressbook/views");
        gal_view_collection_set_storage_directories
            (collection,
             EVOLUTION_GALVIEWSDIR "/addressbook/",
             galview);
        g_free(galview);

        spec = e_table_specification_new();
        e_table_specification_load_from_file (spec, EVOLUTION_ETSPECDIR "/e-addressbook-view.etspec");

        factory = gal_view_factory_etable_new (spec);
        g_object_unref (spec);
        gal_view_collection_add_factory (collection, factory);
        g_object_unref (factory);

        factory = gal_view_factory_minicard_new();
        gal_view_collection_add_factory (collection, factory);
        g_object_unref (factory);

#ifdef WITH_ADDRESSBOOK_VIEW_TREEVIEW
        factory = gal_view_factory_treeview_new ();
        gal_view_collection_add_factory (collection, factory);
        g_object_unref (factory);
#endif

        gal_view_collection_load(collection);
    }
}

static void
set_view_preview (EABView *view)
{
    /* XXX this should use the addressbook's global gconf client */
    GConfClient *gconf_client;
    gboolean state;

    gconf_client = gconf_client_get_default();
    state = gconf_client_get_bool(gconf_client, "/apps/evolution/addressbook/display/show_preview", NULL);
    bonobo_ui_component_set_prop (view->uic,
                      "/commands/ContactsViewPreview",
                      "state",
                      state ? "1" : "0", NULL);

    eab_view_show_contact_preview (view, state);
    
    g_object_unref (gconf_client);
}

static void
display_view(GalViewInstance *instance,
         GalView *view,
         gpointer data)
{
    EABView *address_view = data;
    if (GAL_IS_VIEW_ETABLE(view)) {
        change_view_type (address_view, EAB_VIEW_TABLE);
        gal_view_etable_attach_table (GAL_VIEW_ETABLE(view), e_table_scrolled_get_table(E_TABLE_SCROLLED(address_view->widget)));
    }
    else if (GAL_IS_VIEW_MINICARD(view)) {
        change_view_type (address_view, EAB_VIEW_MINICARD);
        gal_view_minicard_attach (GAL_VIEW_MINICARD (view), E_MINICARD_VIEW_WIDGET (address_view->object));
    }
#ifdef WITH_ADDRESSBOOK_VIEW_TREEVIEW
    else if (GAL_IS_VIEW_TREEVIEW (view)) {
        change_view_type (address_view, EAB_VIEW_TREEVIEW);
        gal_view_treeview_attach (GAL_VIEW_TREEVIEW(view), GTK_TREE_VIEW (address_view->object));
    }
#endif
    address_view->current_view = view;

    set_paned_position (address_view);
    set_view_preview (address_view);
}

static void
view_preview(BonoboUIComponent *uic, const char *path, Bonobo_UIComponent_EventType type, const char *state, void *data)
{
    /* XXX this should use the addressbook's global gconf client */
    GConfClient *gconf_client;
    EABView *view = EAB_VIEW (data);

    if (type != Bonobo_UIComponent_STATE_CHANGED)
        return;

    gconf_client = gconf_client_get_default();
    gconf_client_set_bool(gconf_client, "/apps/evolution/addressbook/display/show_preview", state[0] != '0', NULL);

    eab_view_show_contact_preview(view, state[0] != '0');

    g_object_unref (gconf_client);
}

static void
setup_menus (EABView *view)
{
    if (view->book && view->view_instance == NULL) {
        init_collection ();
        view->view_instance = gal_view_instance_new (collection, e_book_get_uri (view->book));
    }

    if (view->view_instance && view->uic) {
        view->view_menus = gal_view_menus_new(view->view_instance);
        gal_view_menus_apply(view->view_menus, view->uic, NULL);

        display_view (view->view_instance, gal_view_instance_get_current_view (view->view_instance), view);

        g_signal_connect(view->view_instance, "display_view",
                 G_CALLBACK (display_view), view);
    }

    bonobo_ui_component_add_listener(view->uic, "ContactsViewPreview", view_preview, view);

    set_view_preview (view);
}

static void
eab_view_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
    EABView *eav = EAB_VIEW(object);

    switch (prop_id){
    case PROP_BOOK:
        if (eav->book) {
            g_object_unref (eav->book);
        }
        if (g_value_get_object (value)) {
            eav->book = E_BOOK(g_value_get_object (value));
            g_object_ref (eav->book);
            gtk_widget_set_sensitive (GTK_WIDGET (eav->search), TRUE);
        }
        else {
            eav->book = NULL;
            gtk_widget_set_sensitive (GTK_WIDGET (eav->search), FALSE);
        }

        if (eav->view_instance) {
            g_object_unref (eav->view_instance);
            eav->view_instance = NULL;
        }

        g_object_set(eav->model,
                 "book", eav->book,
                 NULL);

        setup_menus (eav);

        break;
    case PROP_SOURCE:
        if (eav->source) {
            g_warning ("EABView at present does not support multiple writes on the \"source\" property.");
            break;
        }
        else {
            if (g_value_get_object (value)) {
                eav->source = E_SOURCE(g_value_get_object (value));
                g_object_ref (eav->source);
            }
            else {
                eav->source = NULL;
            }
        }
        break;
    case PROP_QUERY:
#if 0 /* This code will mess up ldap a bit.  We need to think about the ramifications of this more. */
        if ((g_value_get_string (value) == NULL && !strcmp (eav->query, SHOW_ALL_SEARCH)) ||
            (g_value_get_string (value) != NULL && !strcmp (eav->query, g_value_get_string (value))))
            break;
#endif
        g_free(eav->query);
        eav->query = g_strdup(g_value_get_string (value));
        if (!eav->query)
            eav->query = g_strdup (SHOW_ALL_SEARCH);
        g_object_set(eav->model,
                 "query", eav->query,
                 NULL);
        break;
    case PROP_TYPE:
        change_view_type(eav, g_value_get_int (value));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
eab_view_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
    EABView *eav = EAB_VIEW(object);

    switch (prop_id) {
    case PROP_BOOK:
        if (eav->book)
            g_value_set_object (value, eav->book);
        else
            g_value_set_object (value, NULL);
        break;
    case PROP_SOURCE:
        if (eav->source)
            g_value_set_object (value, eav->source);
        else
            g_value_set_object (value, NULL);
        break;

    case PROP_QUERY:
        g_value_set_string (value, eav->query);
        break;
    case PROP_TYPE:
        g_value_set_int (value, eav->view_type);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static ESelectionModel*
get_selection_model (EABView *view)
{
    if (view->view_type == EAB_VIEW_TABLE)
        return e_table_get_selection_model (e_table_scrolled_get_table (E_TABLE_SCROLLED(view->widget)));
    else if (view->view_type == EAB_VIEW_MINICARD)
        return e_minicard_view_widget_get_selection_model (E_MINICARD_VIEW_WIDGET(view->object));
#ifdef WITH_ADDRESSBOOK_VIEW_TREEVIEW
    else if (view->view_type == EAB_VIEW_TREEVIEW)
        return e_treeview_get_selection_model (GTK_TREE_VIEW (view->object));
#endif
    g_return_val_if_reached (NULL);
}

/* Popup menu stuff */
typedef struct {
    EABView *view;
    EPopupMenu *submenu;
    gpointer closure;
} ContactAndBook;

static ESelectionModel*
contact_and_book_get_selection_model (ContactAndBook *contact_and_book)
{
    return get_selection_model (contact_and_book->view);
}

static void
contact_and_book_free (ContactAndBook *contact_and_book)
{
    EABView *view = contact_and_book->view;
    ESelectionModel *selection;

    if (contact_and_book->submenu)
        gal_view_instance_free_popup_menu (view->view_instance,
                           contact_and_book->submenu);

    selection = contact_and_book_get_selection_model (contact_and_book);
    if (selection)
        e_selection_model_right_click_up(selection);

    g_object_unref (view);
}

static void
get_contact_list_1(gint model_row,
           gpointer closure)
{
    ContactAndBook *contact_and_book;
    GList **list;
    EABView *view;
    EContact *contact;

    contact_and_book = closure;
    list = contact_and_book->closure;
    view = contact_and_book->view;

    contact = eab_model_get_contact(view->model, model_row);
    *list = g_list_prepend(*list, contact);
}

static GList *
get_contact_list (ContactAndBook *contact_and_book)
{
    GList *list = NULL;
    ESelectionModel *selection;

    selection = contact_and_book_get_selection_model (contact_and_book);

    if (selection) {
        contact_and_book->closure = &list;
        e_selection_model_foreach (selection, get_contact_list_1, contact_and_book);
    }

    return list;
}

static void
has_email_address_1(gint model_row,
              gpointer closure)
{
    ContactAndBook *contact_and_book;
    gboolean *has_email;
    EABView *view;
    const EContact *contact;
    GList *email;

    contact_and_book = closure;
    has_email = contact_and_book->closure;
    view = contact_and_book->view;

    if (*has_email)
        return;

    contact = eab_model_contact_at(view->model, model_row);

    email = e_contact_get (E_CONTACT (contact), E_CONTACT_EMAIL);

    if (g_list_length (email) > 0)
        *has_email = TRUE;

    g_list_foreach (email, (GFunc)g_free, NULL);
    g_list_free (email);
}

static gboolean
get_has_email_address (ContactAndBook *contact_and_book)
{
    ESelectionModel *selection;
    gboolean has_email = FALSE;

    selection = contact_and_book_get_selection_model (contact_and_book);

    if (selection) {
        contact_and_book->closure = &has_email;
        e_selection_model_foreach (selection, has_email_address_1, contact_and_book);
    }

    return has_email;
}

static void
save_as (GtkWidget *widget, ContactAndBook *contact_and_book)
{
    GList *contacts = get_contact_list (contact_and_book);
    if (contacts) {
        eab_contact_list_save(_("Save as VCard..."), contacts, NULL);
        e_free_object_list(contacts);
    }
}

static void
send_as (GtkWidget *widget, ContactAndBook *contact_and_book)
{
    GList *contacts = get_contact_list (contact_and_book);
    if (contacts) {
        eab_send_contact_list(contacts, EAB_DISPOSITION_AS_ATTACHMENT);
        e_free_object_list(contacts);
    }
}

static void
send_to (GtkWidget *widget, ContactAndBook *contact_and_book)

{
    GList *contacts = get_contact_list (contact_and_book);

    if (contacts) {
        eab_send_contact_list(contacts, EAB_DISPOSITION_AS_TO);
        e_free_object_list(contacts);
    }
}

static void
print (GtkWidget *widget, ContactAndBook *contact_and_book)
{
    GList *contacts = get_contact_list (contact_and_book);
    if (contacts) {
        if (contacts->next)
            gtk_widget_show(e_contact_print_contact_list_dialog_new(contacts));
        else
            gtk_widget_show(e_contact_print_contact_dialog_new(contacts->data));
        e_free_object_list(contacts);
    }
}

#if 0 /* Envelope printing is disabled for Evolution 1.0. */
static void
print_envelope (GtkWidget *widget, ContactAndBook *contact_and_book)
{
    GList *cards = get_card_list (contact_and_book);
    if (cards) {
        gtk_widget_show(e_contact_list_print_envelope_dialog_new(contact_and_book->card));
        e_free_object_list(cards);
    }
}
#endif

static void
copy (GtkWidget *widget, ContactAndBook *contact_and_book)
{
    eab_view_copy (contact_and_book->view);
}

static void
paste (GtkWidget *widget, ContactAndBook *contact_and_book)
{
    eab_view_paste (contact_and_book->view);
}

static void
cut (GtkWidget *widget, ContactAndBook *contact_and_book)
{
    eab_view_cut (contact_and_book->view);
}

static void
delete (GtkWidget *widget, ContactAndBook *contact_and_book)
{
    if (eab_editor_confirm_delete(GTK_WINDOW(gtk_widget_get_toplevel(contact_and_book->view->widget)))) {
        EBook *book;
        GList *list = get_contact_list(contact_and_book);
        GList *iterator;
        gboolean bulk_remove = FALSE;

        bulk_remove = e_book_check_static_capability (contact_and_book->view->model->book,
                                  "bulk-remove");

        g_object_get(contact_and_book->view->model,
                 "book", &book,
                 NULL);

        if (bulk_remove) {
            GList *ids = NULL;

            for (iterator = list; iterator; iterator = iterator->next) {
                EContact *contact = iterator->data;
                ids = g_list_prepend (ids, (char*)e_contact_get_const (contact, E_CONTACT_UID));
            }

            /* Remove the cards all at once. */
            /* XXX no callback specified... ugh */
            e_book_async_remove_contacts (book,
                              ids,
                              NULL,
                              NULL);
            
            g_list_free (ids);
        }
        else {
            for (iterator = list; iterator; iterator = iterator->next) {
                EContact *contact = iterator->data;
                /* Remove the card. */
                /* XXX no callback specified... ugh */
                e_book_async_remove_contact (book,
                                 contact,
                                 NULL,
                                 NULL);
            }
        }
        e_free_object_list(list);
        g_object_unref(book);
    }
}

static void
copy_to_folder (GtkWidget *widget, ContactAndBook *contact_and_book)
{
    eab_view_copy_to_folder (contact_and_book->view);
}

static void
move_to_folder (GtkWidget *widget, ContactAndBook *contact_and_book)
{
    eab_view_move_to_folder (contact_and_book->view);
}

static void
free_popup_info (GtkWidget *w, ContactAndBook *contact_and_book)
{
    contact_and_book_free (contact_and_book);
}

static void
new_card (GtkWidget *widget, ContactAndBook *contact_and_book)
{
    EBook *book;
    EContact *contact = e_contact_new();

    g_object_get(contact_and_book->view->model,
             "book", &book,
             NULL);

    eab_show_contact_editor (book, contact, TRUE, TRUE);
    g_object_unref (book);
    g_object_unref (contact);
}

static void
new_list (GtkWidget *widget, ContactAndBook *contact_and_book)
{
    EBook *book;
    EContact *contact = e_contact_new ();

    g_object_get(contact_and_book->view->model,
             "book", &book,
             NULL);
    eab_show_contact_list_editor (book, contact, TRUE, TRUE);
    g_object_unref(book);
    g_object_unref(contact);
}

#if 0
static void
sources (GtkWidget *widget, ContactAndBook *contact_and_book)
{
    BonoboControl *control;
    GNOME_Evolution_ShellView shell_view;
    CORBA_Environment ev;

    control = g_object_get_data (G_OBJECT (gcal), "control");
    if (control == NULL)
        return;

    shell_view = get_shell_view_interface (control);
    if (shell_view == CORBA_OBJECT_NIL)
        return;

    CORBA_exception_init (&ev);
    
    GNOME_Evolution_ShellView_showSettings (shell_view, &ev);
    
    if (BONOBO_EX (&ev))
        g_message ("control_util_show_settings(): Could not show settings");

    CORBA_exception_free (&ev);
}
#endif

#define POPUP_READONLY_MASK 0x1
#define POPUP_NOSELECTION_MASK 0x2
#define POPUP_NOEMAIL_MASK 0x4

static void
do_popup_menu(EABView *view, GdkEvent *event)
{
    ContactAndBook *contact_and_book;
    GtkMenu *popup;
    EPopupMenu *submenu = NULL;
    ESelectionModel *selection_model;
    gboolean selection = FALSE;

    EPopupMenu menu[] = {
        E_POPUP_ITEM (N_("New Contact..."), G_CALLBACK(new_card), POPUP_READONLY_MASK),
        E_POPUP_ITEM (N_("New Contact List..."), G_CALLBACK(new_list), POPUP_READONLY_MASK),
        E_POPUP_SEPARATOR,
#if 0
        E_POPUP_ITEM (N_("Go to Folder..."), G_CALLBACK (goto_folder), 0),
        E_POPUP_ITEM (N_("Import..."), G_CALLBACK (import), POPUP_READONLY_MASK),
        E_POPUP_SEPARATOR,
        E_POPUP_ITEM (N_("Search for Contacts..."), G_CALLBACK (search), 0),
        E_POPUP_ITEM (N_("Address Book Sources..."), G_CALLBACK (sources), 0),
        E_POPUP_SEPARATOR,
        E_POPUP_ITEM (N_("Pilot Settings..."), G_CALLBACK (pilot_settings), 0),
#endif
        E_POPUP_SEPARATOR,
        E_POPUP_ITEM (N_("Save as VCard..."), G_CALLBACK(save_as), POPUP_NOSELECTION_MASK),
        E_POPUP_ITEM (N_("Forward Contact"), G_CALLBACK(send_as), POPUP_NOSELECTION_MASK),
        E_POPUP_ITEM (N_("Send Message to Contact"), G_CALLBACK(send_to), POPUP_NOSELECTION_MASK | POPUP_NOEMAIL_MASK),
        E_POPUP_ITEM (N_("Print"), G_CALLBACK(print), POPUP_NOSELECTION_MASK),
#if 0 /* Envelope printing is disabled for Evolution 1.0. */
        E_POPUP_ITEM (N_("Print Envelope"), G_CALLBACK(print_envelope), POPUP_NOSELECTION_MASK),
#endif
        E_POPUP_SEPARATOR,

        E_POPUP_ITEM (N_("Copy to Address Book..."), G_CALLBACK(copy_to_folder), POPUP_NOSELECTION_MASK), 
        E_POPUP_ITEM (N_("Move to Address Book..."), G_CALLBACK(move_to_folder), POPUP_READONLY_MASK | POPUP_NOSELECTION_MASK),
        E_POPUP_SEPARATOR,

        E_POPUP_ITEM (N_("Cut"), G_CALLBACK (cut), POPUP_READONLY_MASK | POPUP_NOSELECTION_MASK),
        E_POPUP_ITEM (N_("Copy"), G_CALLBACK (copy), POPUP_NOSELECTION_MASK),
        E_POPUP_ITEM (N_("Paste"), G_CALLBACK (paste), POPUP_READONLY_MASK),
        E_POPUP_ITEM (N_("Delete"), G_CALLBACK(delete), POPUP_READONLY_MASK | POPUP_NOSELECTION_MASK),
        E_POPUP_SEPARATOR,

#if 0
        E_POPUP_SUBMENU (N_("Current View"), submenu = gal_view_instance_get_popup_menu (view->view_instance), 0),
#endif
        E_POPUP_TERMINATOR
    };

    contact_and_book = g_new(ContactAndBook, 1);
    contact_and_book->view = view;
    contact_and_book->submenu = submenu;

    g_object_ref (contact_and_book->view);

    selection_model = contact_and_book_get_selection_model (contact_and_book);
    if (selection_model)
        selection = e_selection_model_selected_count (selection_model) > 0;

    popup = e_popup_menu_create (menu,
                     0,
                     (eab_model_editable (view->model) ? 0 : POPUP_READONLY_MASK) +
                     (selection ? 0 : POPUP_NOSELECTION_MASK) +
                     (get_has_email_address (contact_and_book) ? 0 : POPUP_NOEMAIL_MASK),
                     contact_and_book);

    g_signal_connect (popup, "selection-done",
              G_CALLBACK (free_popup_info), contact_and_book);
    e_popup_menu (popup, event);

}

static void
render_contact (int row, EABView *view)
{
    EContact *contact = eab_model_get_contact (view->model, row);

    view->displayed_contact = row;

    eab_contact_display_render (EAB_CONTACT_DISPLAY (view->contact_display), contact,
                    EAB_CONTACT_DISPLAY_RENDER_NORMAL);
}

static void
selection_changed (GObject *o, EABView *view)
{
    ESelectionModel *selection_model;

    command_state_change (view);

    selection_model = get_selection_model (view);

    if (e_selection_model_selected_count (selection_model) == 1)
        e_selection_model_foreach (selection_model,
                       (EForeachFunc)render_contact, view);
    else {
        view->displayed_contact = -1;
        eab_contact_display_render (EAB_CONTACT_DISPLAY (view->contact_display), NULL,
                        EAB_CONTACT_DISPLAY_RENDER_NORMAL);
    }
                        
}

static void
table_double_click(ETableScrolled *table, gint row, gint col, GdkEvent *event, EABView *view)
{
    if (E_IS_ADDRESSBOOK_TABLE_ADAPTER(view->object)) {
        EABModel *model = view->model;
        EContact *contact = eab_model_get_contact (model, row);
        EBook *book;

        g_object_get(model,
                 "book", &book,
                 NULL);
        
        g_assert (E_IS_BOOK (book));

        if (e_contact_get (contact, E_CONTACT_IS_LIST))
            eab_show_contact_list_editor (book, contact, FALSE, view->editable);
        else
            eab_show_contact_editor (book, contact, FALSE, view->editable);

        g_object_unref (book);
        g_object_unref (contact);
    }
}

static gint
table_right_click(ETableScrolled *table, gint row, gint col, GdkEvent *event, EABView *view)
{
    do_popup_menu(view, event);
    return TRUE;
}

static gint
table_white_space_event(ETableScrolled *table, GdkEvent *event, EABView *view)
{
    if (event->type == GDK_BUTTON_PRESS && ((GdkEventButton *)event)->button == 3) {
        do_popup_menu(view, event);
        return TRUE;
    } else {
        return FALSE;
    }
}

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

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

    contact_list = get_selected_contacts (view);

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

        value = eab_contact_list_to_string (contact_list);

        gtk_selection_data_set (selection_data,
                    selection_data->target,
                    8,
                    value, strlen (value));
        break;
    }
    case DND_TARGET_TYPE_SOURCE_VCARD: {
        char *value;

        value = eab_book_and_contact_list_to_string (view->book, contact_list);

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

    g_list_foreach (contact_list, (GFunc) g_object_unref, NULL);
    g_list_free (contact_list);
}

static void
emit_status_message (EABView *eav, const gchar *status)
{
    g_signal_emit (eav,
               eab_view_signals [STATUS_MESSAGE], 0,
               status);
}

static void
emit_search_result (EABView *eav, EBookViewStatus status)
{
    g_signal_emit (eav,
               eab_view_signals [SEARCH_RESULT], 0,
               status);
}

static void
emit_folder_bar_message (EABView *eav, const gchar *message)
{
    g_signal_emit (eav,
               eab_view_signals [FOLDER_BAR_MESSAGE], 0,
               message);
}

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

static void
search_result (GtkObject *object, EBookViewStatus status, EABView *eav)
{
    emit_search_result (eav, status);
}

static void
folder_bar_message (GtkObject *object, const gchar *status, EABView *eav)
{
    emit_folder_bar_message (eav, status);
}

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

static void
command_state_change (EABView *eav)
{
    /* Reffing during emission is unnecessary.  Gtk automatically refs during an emission. */
    g_signal_emit (eav, eab_view_signals [COMMAND_STATE_CHANGE], 0);
}

static void
backend_died (GtkObject *object, EABView *eav)
{
    e_error_run (GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (eav))),
             "addressbook:backend-died", e_book_get_uri (eav->book), NULL);
}

static void
contact_changed (EABModel *model, gint index, EABView *eav)
{
    if (eav->displayed_contact == index) {
        /* if the contact that's presently displayed is changed, re-render it */
        render_contact (index, eav);
    }
}

static void
contact_removed (EABModel *model, gint index, EABView *eav)
{
    if (eav->displayed_contact == index) {
        /* if the contact that's presently displayed is changed, clear the display */
        eab_contact_display_render (EAB_CONTACT_DISPLAY (eav->contact_display), NULL,
                        EAB_CONTACT_DISPLAY_RENDER_NORMAL);
        eav->displayed_contact = -1;
    }
}

static void
minicard_right_click (EMinicardView *minicard_view_item, GdkEvent *event, EABView *view)
{
       do_popup_menu(view, event);
}

static void
create_minicard_view (EABView *view)
{
    GtkWidget *scrolled_window;
    GtkWidget *minicard_view;
    EAddressbookReflowAdapter *adapter;

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

    g_signal_connect(minicard_view, "selection_change",
             G_CALLBACK(selection_changed), view);

    g_signal_connect(minicard_view, "right_click",
             G_CALLBACK(minicard_right_click), view);

    scrolled_window = gtk_scrolled_window_new (NULL, NULL);
    gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_IN);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
                    GTK_POLICY_AUTOMATIC,
                    GTK_POLICY_AUTOMATIC);

    view->object = G_OBJECT(minicard_view);
    view->widget = scrolled_window;

    gtk_container_add (GTK_CONTAINER (scrolled_window), minicard_view);
    gtk_widget_show (minicard_view);

    gtk_widget_show_all( GTK_WIDGET(scrolled_window) );

    gtk_paned_add1 (GTK_PANED (view->paned), scrolled_window);

    e_reflow_model_changed (E_REFLOW_MODEL (adapter));
}

static void
create_table_view (EABView *view)
{
    ETableModel *adapter;
    GtkWidget *table;
    
    adapter = eab_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_from_spec_file (adapter, NULL, EVOLUTION_ETSPECDIR "/e-addressbook-view.etspec", NULL);

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

    g_signal_connect(e_table_scrolled_get_table(E_TABLE_SCROLLED(table)), "double_click",
             G_CALLBACK(table_double_click), view);
    g_signal_connect(e_table_scrolled_get_table(E_TABLE_SCROLLED(table)), "right_click",
             G_CALLBACK(table_right_click), view);
    g_signal_connect(e_table_scrolled_get_table(E_TABLE_SCROLLED(table)), "white_space_event",
             G_CALLBACK(table_white_space_event), view);
    g_signal_connect(e_table_scrolled_get_table(E_TABLE_SCROLLED(table)), "selection_change",
             G_CALLBACK(selection_changed), 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 | GDK_ACTION_COPY);
    
    g_signal_connect (E_TABLE_SCROLLED(table)->table,
              "table_drag_data_get",
              G_CALLBACK (table_drag_data_get),
              view);

    gtk_paned_add1 (GTK_PANED (view->paned), table);

    gtk_widget_show( GTK_WIDGET(table) );
}

#ifdef WITH_ADDRESSBOOK_VIEW_TREEVIEW
static void
treeview_row_activated(GtkTreeView *treeview,
               GtkTreePath *path, GtkTreeViewColumn *column,
               EABView *view)
{
    EABModel *model = view->model;
    int row = gtk_tree_path_get_indices (path)[0];
    ECard *card = eab_model_get_card(model, row);
    EBook *book;

    g_object_get(model,
             "book", &book,
             NULL);
        
    g_assert (E_IS_BOOK (book));

    if (e_card_evolution_list (card))
        eab_show_contact_list_editor (book, card, FALSE, view->editable);
    else
        eab_show_contact_editor (book, card, FALSE, view->editable);

    g_object_unref (book);
    g_object_unref (card);
}

static void
create_treeview_view (EABView *view)
{
    GtkTreeModel *adapter;
    ECardSimple *simple;
    GtkWidget *treeview;
    GtkWidget *scrolled;
    int i;

    simple = e_card_simple_new(NULL);

    adapter = eab_treeview_adapter_new(view->model);

    scrolled = gtk_scrolled_window_new (NULL, NULL);
    gtk_scrolled_window_set_shadow (GTK_SCROLLED_WINDOW (scrolled), GTK_SHADOW_IN);
    treeview = gtk_tree_view_new_with_model (adapter);

    gtk_widget_show (treeview);

    gtk_container_add (GTK_CONTAINER (scrolled), treeview);

    for (i = 0; i < 15; i ++) {
        GtkTreeViewColumn *column =
            gtk_tree_view_column_new_with_attributes (e_card_simple_get_name (simple, i),
                                  gtk_cell_renderer_text_new (),
                                  "text", i,
                                  NULL);

        gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
    }

    view->object = G_OBJECT(treeview);
    view->widget = scrolled;

    gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview)), GTK_SELECTION_MULTIPLE);
    gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (treeview), 
                        GDK_BUTTON1_MASK,
                        drag_types,
                        num_drag_types,
                        GDK_ACTION_MOVE);

    g_signal_connect(treeview, "row_activated",
             G_CALLBACK (treeview_row_activated), view);
#if 0
    g_signal_connect(e_table_scrolled_get_table(E_TABLE_SCROLLED(table)), "right_click",
             G_CALLBACK(table_right_click), 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);
    
    g_signal_connect (E_TABLE_SCROLLED(table)->table,
              "table_drag_data_get",
              G_CALLBACK (table_drag_data_get),
              view);
#endif


    g_signal_connect(e_treeview_get_selection_model (GTK_TREE_VIEW (treeview)), "selection_changed",
             G_CALLBACK(selection_changed), view);

    gtk_paned_add1 (GTK_PANED (view->paned), scrolled);

    gtk_widget_show( GTK_WIDGET(scrolled) );

    g_object_unref (simple);
}
#endif

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

    if (view->widget) {
        gtk_container_remove (GTK_CONTAINER (view->paned), view->widget);
        view->widget = NULL;
    }
    view->object = NULL;

    switch (view_type) {
    case EAB_VIEW_TABLE:
        create_table_view (view);
        break;
    case EAB_VIEW_MINICARD:
        create_minicard_view (view);
        break;
#ifdef WITH_ADDRESSBOOK_VIEW_TREEVIEW
    case EAB_VIEW_TREEVIEW:
        create_treeview_view (view);
        break;
#endif
    default:
        g_warning ("view_type not recognized.");
        return;
    }

    view->view_type = view_type;

    command_state_change (view);
}



static void
search_activated (ESearchBar *esb, EABView *v)
{
    ECategoriesMasterList *master_list;
    char *search_word, *search_query;
    const char *category_name;
    int search_type, subid;

    g_message ("in search_activated");

    g_object_get(esb,
             "text", &search_word,
             "item_id", &search_type,
             NULL);

    if (search_type == ESB_ADVANCED) {
        gtk_widget_show(eab_search_dialog_new(v));
    }
    else {
        if ((search_word && strlen (search_word)) || search_type == ESB_CATEGORY) {
            GString *s = g_string_new ("");
            e_sexp_encode_string (s, search_word);
            switch (search_type) {
            case ESB_ANY:
                search_query = g_strdup_printf ("(contains \"x-evolution-any-field\" %s)",
                                s->str);
                break;
            case ESB_FULL_NAME:
                search_query = g_strdup_printf ("(beginswith \"full_name\" %s)",
                                s->str);
                break;
            case ESB_EMAIL:
                search_query = g_strdup_printf ("(beginswith \"email\" %s)",
                                s->str);
                break;
            case ESB_CATEGORY:
                subid = e_search_bar_get_subitem_id (esb);

                if (subid < 0 || subid == G_MAXINT) {
                    /* match everything */
                    search_query = g_strdup ("(contains \"x-evolution-any-field\" \"\")");
                } else {
                    master_list = get_master_list ();
                    category_name = e_categories_master_list_nth (master_list, subid);
                    search_query = g_strdup_printf ("(is \"category_list\" \"%s\")", category_name);
                }
                break;
            default:
                search_query = g_strdup ("(contains \"x-evolution-any-field\" \"\")");
                break;
            }
            g_string_free (s, TRUE);
        } else
            search_query = g_strdup ("(contains \"x-evolution-any-field\" \"\")");

        if (search_query)
            g_object_set (v,
                      "query", search_query,
                      NULL);

        g_free (search_query);
    }

    g_free (search_word);
}

static void
search_menu_activated (ESearchBar *esb, int id, EABView *view)
{
    if (id == ESB_ADVANCED)
        gtk_widget_show(eab_search_dialog_new(view));
}

static void
query_changed (ESearchBar *esb, EABView *view)
{
    int search_type;

    search_type = e_search_bar_get_item_id(esb);
    if (search_type == ESB_ADVANCED)
        gtk_widget_show(eab_search_dialog_new(view));
}

static int
compare_subitems (const void *a, const void *b)
{
    const ESearchBarSubitem *subitem_a = a;
    const ESearchBarSubitem *subitem_b = b;
    char *collate_a, *collate_b;
    int ret;

    collate_a = g_utf8_collate_key (subitem_a->text, -1);
    collate_b = g_utf8_collate_key (subitem_b->text, -1);

    ret =  strcmp (collate_a, collate_b);

    g_free (collate_a);
    g_free (collate_b);
    
    return ret;
}

static void
make_suboptions (EABView *view)
{
    ESearchBarSubitem *subitems, *s;
    ECategoriesMasterList *master_list;
    gint i, N;

    master_list = get_master_list ();
    N = e_categories_master_list_count (master_list);
    subitems = g_new (ESearchBarSubitem, N+2);

    subitems[0].id = G_MAXINT;
    subitems[0].text = g_strdup (_("Any Category"));
    subitems[0].translate = FALSE;

    for (i=0; i<N; ++i) {
        const char *category = e_categories_master_list_nth (master_list, i);

        subitems[i+1].id = i;
        subitems[i+1].text = g_strdup (category);
        subitems[i+1].translate = FALSE;
    }
    subitems[N+1].id = -1;
    subitems[N+1].text = NULL;

    qsort (subitems + 1, N, sizeof (subitems[0]), compare_subitems);

    e_search_bar_set_suboption (view->search, ESB_CATEGORY, subitems);

    for (s = subitems; s->id != -1; s++) {
        if (s->text)
            g_free (s->text);
    }
    g_free (subitems);
}

static void
ecml_changed (ECategoriesMasterList *ecml, EABView *view)
{
    make_suboptions (view);
}

static ECategoriesMasterList *
get_master_list (void)
{
    static ECategoriesMasterList *category_list = NULL;

    if (category_list == NULL)
        category_list = e_categories_master_list_wombat_new ();
    return category_list;
}

static void
connect_master_list_changed (EABView *view)
{
    view->ecml_changed_id =
        g_signal_connect (get_master_list(), "changed",
                  G_CALLBACK (ecml_changed), view);
}



typedef struct {
    GtkWidget *table;
    GObject *printable;
} EContactPrintDialogWeakData;

static void
e_contact_print_destroy(gpointer data, GObject *where_object_was)
{
    EContactPrintDialogWeakData *weak_data = data;
    g_object_unref (weak_data->printable);
    g_object_unref (weak_data->table);
    g_free (weak_data);
}

static void
e_contact_print_button(GtkDialog *dialog, gint response, gpointer data)
{
    GnomePrintJob *master;
    GnomePrintContext *pc;
    EPrintable *printable = g_object_get_data(G_OBJECT(dialog), "printable");
    GtkWidget *preview;
    switch( response ) {
    case GNOME_PRINT_DIALOG_RESPONSE_PRINT:
        master = gnome_print_job_new(gnome_print_dialog_get_config ( GNOME_PRINT_DIALOG(dialog) ));
        pc = gnome_print_job_get_context( master );
        e_printable_reset(printable);
        while (e_printable_data_left(printable)) {
            gnome_print_beginpage (pc, "Contacts");
            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_job_close(master);
        gnome_print_job_print(master);
        g_object_unref (master);
        gtk_widget_destroy((GtkWidget *)dialog);
        break;
    case GNOME_PRINT_DIALOG_RESPONSE_PREVIEW:
        master = gnome_print_job_new (gnome_print_dialog_get_config ( GNOME_PRINT_DIALOG(dialog) ));
        pc = gnome_print_job_get_context( master );
        e_printable_reset(printable);
        while (e_printable_data_left(printable)) {
            gnome_print_beginpage (pc, "Contacts");
            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_job_close(master);
        preview = GTK_WIDGET(gnome_print_job_preview_new(master, "Print Preview"));
        gtk_widget_show_all(preview);
        g_object_unref (master);
        break;
    case GNOME_PRINT_DIALOG_RESPONSE_CANCEL:
    default:
        gtk_widget_destroy((GtkWidget *)dialog);
        break;
    }
}

void
eab_view_show_contact_preview (EABView *view, gboolean show)
{
    g_return_if_fail (view && E_IS_ADDRESSBOOK_VIEW (view));

    if (show)
        gtk_widget_show (view->contact_display_window);
    else
        gtk_widget_hide (view->contact_display_window);
}

void
eab_view_setup_menus (EABView *view,
              BonoboUIComponent *uic)
{

    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));

    init_collection ();

    view->uic = uic;

    setup_menus (view);

    /* XXX toshok - yeah this really doesn't belong here, but it
       needs to happen at the same time and takes the uic */
    e_search_bar_set_ui_component (view->search, uic);
}

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

    if (view->view_menus) {
        gal_view_menus_unmerge (view->view_menus, NULL);

        g_object_unref (view->view_menus);
        view->view_menus = NULL;
    }

    if (view->view_instance) {
        g_object_unref (view->view_instance);
        view->view_instance = NULL;
    }

    view->uic = NULL;
}

void
eab_view_print(EABView *view)
{
    if (view->view_type == EAB_VIEW_MINICARD) {
        char *query;
        EBook *book;
        GtkWidget *print;

        g_object_get (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 == EAB_VIEW_TABLE) {
        GtkWidget *dialog;
        EPrintable *printable;
        ETable *etable;
        EContactPrintDialogWeakData *weak_data;

        dialog = gnome_print_dialog_new(NULL, "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);

        g_object_get(view->widget, "table", &etable, NULL);
        printable = e_table_get_printable(etable);
        g_object_ref (printable);
        gtk_object_sink (GTK_OBJECT (printable));
        g_object_unref(etable);
        g_object_ref (view->widget);

        g_object_set_data (G_OBJECT (dialog), "table", view->widget);
        g_object_set_data (G_OBJECT (dialog), "printable", printable);
        
        g_signal_connect(dialog,
                 "response", G_CALLBACK(e_contact_print_button), NULL);

        weak_data = g_new (EContactPrintDialogWeakData, 1);

        weak_data->table = view->widget;
        weak_data->printable = G_OBJECT (printable);

        g_object_weak_ref (G_OBJECT (dialog), e_contact_print_destroy, weak_data);

        gtk_widget_show(dialog);
    }
#ifdef WITH_ADDRESSBOOK_VIEW_TREEVIEW
    else if (view->view_type == EAB_VIEW_TREEVIEW) {
        /* XXX */
    }
#endif
}

void
eab_view_print_preview(EABView *view)
{
    if (view->view_type == EAB_VIEW_MINICARD) {
        char *query;
        EBook *book;

        g_object_get (view->model,
                  "query", &query,
                  "book", &book,
                  NULL);
        e_contact_print_preview(book, query);
        g_free(query);
    } else if (view->view_type == EAB_VIEW_TABLE) {
        EPrintable *printable;
        ETable *etable;
        GnomePrintJob *master;
        GnomePrintContext *pc;
        GnomePrintConfig *config;
        GtkWidget *preview;

        g_object_get(view->widget, "table", &etable, NULL);
        printable = e_table_get_printable(etable);
        g_object_unref(etable);
        g_object_ref (printable);
        gtk_object_sink (GTK_OBJECT (printable));

        master = gnome_print_job_new(NULL);
        config = gnome_print_job_get_config (master);
        gnome_print_config_set_int (config, GNOME_PRINT_KEY_NUM_COPIES, 1);
        pc = gnome_print_job_get_context( master );
        e_printable_reset(printable);
        while (e_printable_data_left(printable)) {
            gnome_print_beginpage (pc, "Contacts");
            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_job_close(master);
        preview = GTK_WIDGET(gnome_print_job_preview_new(master, "Print Preview"));
        gtk_widget_show_all(preview);
        g_object_unref (master);
        g_object_unref (printable);
    }
#ifdef WITH_ADDRESSBOOK_VIEW_TREEVIEW
    else if (view->view_type == EAB_VIEW_TREEVIEW) {
        /* XXX */
    }
#endif
}

void
eab_view_delete_selection(EABView *view)
{
    ContactAndBook contact_and_book;

    memset (&contact_and_book, 0, sizeof (contact_and_book));
    contact_and_book.view = view;

    delete (GTK_WIDGET (view), &contact_and_book);
}

static void
invisible_destroyed (gpointer data, GObject *where_object_was)
{
    EABView *view = data;
    view->invisible = NULL;
}

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

    value = eab_contact_list_to_string (view->clipboard_contacts);

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

static void
selection_clear_event (GtkWidget *invisible,
               GdkEventSelection *event,
               EABView *view)
{
    if (view->clipboard_contacts) {
        g_list_foreach (view->clipboard_contacts, (GFunc)g_object_unref, NULL);
        g_list_free (view->clipboard_contacts);
        view->clipboard_contacts = NULL;
    }
}

static void
selection_received (GtkWidget *invisible,
            GtkSelectionData *selection_data,
            guint time,
            EABView *view)
{
    if (selection_data->length <= 0 || selection_data->type != GDK_SELECTION_TYPE_STRING) {
        return;
    } else {
        GList *contact_list;
        GList *l;
        char *str = NULL;

        if (selection_data->data [selection_data->length - 1] != 0) {
            str = g_malloc0 (selection_data->length + 1);
            memcpy (str, selection_data->data, selection_data->length);
            contact_list = eab_contact_list_from_string (str);
        } else
            contact_list = eab_contact_list_from_string (selection_data->data);
        
        for (l = contact_list; l; l = l->next) {
            EContact *contact = l->data;

            /* XXX NULL for a callback /sigh */
            eab_merging_book_add_contact (view->book, contact, NULL /* XXX */, NULL);
        }

        g_list_foreach (contact_list, (GFunc)g_object_unref, NULL);
        g_list_free (contact_list);
        g_free (str);
    }
}

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_contacts (EABView *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 = eab_model_get_contact (view->model, GPOINTER_TO_INT (iterator->data));
    }
    list = g_list_reverse (list);
    return list;
}

void
eab_view_save_as (EABView *view)
{
    GList *list = get_selected_contacts (view);
    if (list)
        eab_contact_list_save (_("Save as VCard..."), list, NULL);
    e_free_object_list(list);
}

void
eab_view_view (EABView *view)
{
    GList *list = get_selected_contacts (view);
    eab_show_multiple_contacts (view->book, list, view->editable);
    e_free_object_list(list);
}

void
eab_view_send (EABView *view)
{
    GList *list = get_selected_contacts (view);
    if (list)
        eab_send_contact_list (list, EAB_DISPOSITION_AS_ATTACHMENT);
    e_free_object_list(list);
}

void
eab_view_send_to (EABView *view)
{
    GList *list = get_selected_contacts (view);
    if (list)
        eab_send_contact_list (list, EAB_DISPOSITION_AS_TO);
    e_free_object_list(list);
}

void
eab_view_cut (EABView *view)
{
    eab_view_copy (view);
    eab_view_delete_selection (view);
}

void
eab_view_copy (EABView *view)
{
    view->clipboard_contacts = get_selected_contacts (view);

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

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

void
eab_view_select_all (EABView *view)
{
    ESelectionModel *model = get_selection_model (view);

    g_return_if_fail (model);

    e_selection_model_select_all (model);
}

void
eab_view_show_all(EABView *view)
{
    g_object_set(view,
             "query", NULL,
             NULL);
}

void
eab_view_stop(EABView *view)
{
    if (view)
        eab_model_stop (view->model);
}

static void
view_transfer_contacts (EABView *view, gboolean delete_from_source)
{
    EBook *book;
    GList *contacts;
    GtkWindow *parent_window;

    g_object_get(view->model, 
             "book", &book,
             NULL);
    contacts = get_selected_contacts (view);
    parent_window = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (view)));

    eab_transfer_contacts (book, contacts, delete_from_source, parent_window);
    g_object_unref(book);
}

void
eab_view_copy_to_folder (EABView *view)
{
    view_transfer_contacts (view, FALSE);
}

void
eab_view_move_to_folder (EABView *view)
{
    view_transfer_contacts (view, TRUE);
}


static gboolean
eab_view_selection_nonempty (EABView  *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
eab_view_can_create (EABView  *view)
{
    return view ? eab_model_editable (view->model) : FALSE;
}

gboolean
eab_view_can_print (EABView  *view)
{
    return view && view->model ? eab_model_contact_count (view->model) : FALSE;
}

gboolean
eab_view_can_save_as (EABView  *view)
{
    return view ? eab_view_selection_nonempty (view) : FALSE;
}

gboolean
eab_view_can_view (EABView  *view)
{
    return view ? eab_view_selection_nonempty (view) : FALSE;
}

gboolean 
eab_view_can_send (EABView  *view)
{
    return view ? eab_view_selection_nonempty (view) : FALSE;
}

gboolean   
eab_view_can_send_to (EABView  *view)
{
    return view ? eab_view_selection_nonempty (view) : FALSE;
}

gboolean
eab_view_can_delete (EABView  *view)
{
    return view ? eab_view_selection_nonempty (view) && eab_model_editable (view->model) : FALSE;
}

gboolean
eab_view_can_cut (EABView *view)
{
    return view ? eab_view_selection_nonempty (view) && eab_model_editable (view->model) : FALSE;
}

gboolean
eab_view_can_copy (EABView *view)
{
    return view ? eab_view_selection_nonempty (view) : FALSE;
}

gboolean
eab_view_can_paste (EABView *view)
{
    return view ? eab_model_editable (view->model) : FALSE;
}

gboolean
eab_view_can_select_all (EABView *view)
{
    return view ? eab_model_contact_count (view->model) != 0 : FALSE;
}

gboolean
eab_view_can_stop (EABView  *view)
{
    return view ? eab_model_can_stop (view->model) : FALSE;
}

gboolean
eab_view_can_copy_to_folder (EABView *view)
{
    return view ? eab_view_selection_nonempty (view) : FALSE;
}

gboolean
eab_view_can_move_to_folder (EABView *view)
{
    return view ? eab_view_selection_nonempty (view) && eab_model_editable (view->model) : FALSE;
}