aboutsummaryrefslogtreecommitdiffstats
path: root/mail/message-list.c
blob: 3e478c388ab4ed6ceb5839ddcb2f2ccfabf87e40 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * message-list.c: Displays the messages.
 *                 Implements CORBA's Evolution::MessageList
 *
 * Author:
 *     Miguel de Icaza (miguel@helixcode.com)
 *     Bertrand Guiheneuf (bg@aful.org)
 *     And just about everyone else in evolution ...
 *
 * (C) 2000 Helix Code, Inc.
 */

#include <config.h>
#include <gnome.h>
#include <bonobo/bonobo-main.h>
#include <camel/camel-exception.h>
#include <camel/camel-folder.h>
#include <e-util/ename/e-name-western.h>

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

#include "mail-config.h"
#include "message-list.h"
#include "message-thread.h"
#include "mail-threads.h"
#include "mail-tools.h"
#include "Mail.h"

#include <gal/util/e-util.h>
#include <gal/widgets/e-gui-utils.h>
#include <gal/e-table/e-table-header-item.h>
#include <gal/e-table/e-table-item.h>

#include "art/mail-new.xpm"
#include "art/mail-read.xpm"
#include "art/mail-replied.xpm"
#include "art/attachment.xpm"
#include "art/priority-high.xpm"
#include "art/empty.xpm"
#include "art/score-lowest.xpm"
#include "art/score-lower.xpm"
#include "art/score-low.xpm"
#include "art/score-normal.xpm"
#include "art/score-high.xpm"
#include "art/score-higher.xpm"
#include "art/score-highest.xpm"

#define TIMEIT

#ifdef TIMEIT
#include <sys/time.h>
#include <unistd.h>
#endif

#define d(x)
#define t(x)

/*
 * Default sizes for the ETable display
 *
 */
#define N_CHARS(x) (CHAR_WIDTH * (x))

#define COL_ICON_WIDTH         (16)
#define COL_ATTACH_WIDTH       (16)
#define COL_CHECK_BOX_WIDTH    (16)
#define COL_FROM_EXPANSION     (24.0)
#define COL_FROM_WIDTH_MIN     (32)
#define COL_SUBJECT_EXPANSION  (30.0)
#define COL_SUBJECT_WIDTH_MIN  (32)
#define COL_SENT_EXPANSION     (24.0)
#define COL_SENT_WIDTH_MIN     (32)
#define COL_RECEIVED_EXPANSION (20.0)
#define COL_RECEIVED_WIDTH_MIN (32)
#define COL_TO_EXPANSION       (24.0)
#define COL_TO_WIDTH_MIN       (32)
#define COL_SIZE_EXPANSION     (6.0)
#define COL_SIZE_WIDTH_MIN     (32)

#define PARENT_TYPE (bonobo_object_get_type ())

struct _EMailAddress {
    ENameWestern *wname;
    gchar *address;
};

typedef struct _EMailAddress EMailAddress;

static BonoboObjectClass *message_list_parent_class;
static POA_GNOME_Evolution_MessageList__vepv evolution_message_list_vepv;

static void on_cursor_change_cmd (ETableScrolled *table, int row, gpointer user_data);
static gint on_click (ETableScrolled *table, gint row, gint col, GdkEvent *event, MessageList *list);
static char *filter_date (const void *data);
static void free_tree_ids (ETreeModel *etm);

static void save_tree_state(MessageList *ml);

static void folder_changed (CamelObject *o, gpointer event_data, gpointer user_data);
static void message_changed (CamelObject *o, gpointer event_data, gpointer user_data);

/* note: @changes is owned/freed by the caller */
static void mail_do_regenerate_messagelist (MessageList *list, const gchar *search, CamelFolderChangeInfo *changes);

/* macros for working with id's (stored in the tree nodes) */
#define id_is_uid(id) (id[0] == 'u')/* is this a uid id? */
#define id_is_subject(id) (id[0] == 's') /* is this a subject id? */
#define id_uid(id) (&id[1]) /* get the uid part of the id */
#define id_subject(id) (&id[1]) /* get the subject part of the id */

enum {
    MESSAGE_SELECTED,
    LAST_SIGNAL
};

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

static struct {
    char **image_base;
    GdkPixbuf  *pixbuf;
} states_pixmaps [] = {
    { mail_new_xpm,     NULL },
    { mail_read_xpm,    NULL },
    { mail_replied_xpm, NULL },
    { empty_xpm,        NULL },
    { attachment_xpm,   NULL },
    { priority_high_xpm,    NULL },
    { score_lowest_xpm,     NULL },
    { score_lower_xpm,      NULL },
    { score_low_xpm,        NULL },
    { score_normal_xpm,     NULL },
    { score_high_xpm,       NULL },
    { score_higher_xpm,     NULL },
    { score_highest_xpm,    NULL },
    { NULL,         NULL }
};

enum DndTargetTyhpe {
    DND_TARGET_LIST_TYPE_URI,
};
#define URI_LIST_TYPE "text/uri-list"
static GtkTargetEntry drag_types[] = {
    { URI_LIST_TYPE, 0, DND_TARGET_LIST_TYPE_URI },
};
static const int num_drag_types = sizeof (drag_types) / sizeof (drag_types[0]);

static EMailAddress *
e_mail_address_new (const char *address)
{
    CamelInternetAddress *cia;
    EMailAddress *new;
    const char *name, *addr;
    
    cia = camel_internet_address_new ();
    camel_address_decode (CAMEL_ADDRESS (cia), address);
    camel_internet_address_get (cia, 0, &name, &addr);
    
    new = g_new (EMailAddress, 1);
    new->address = g_strdup (addr);
    if (name && *name) {
        new->wname = e_name_western_parse (name);
    } else {
        new->wname = NULL;
    }
    
    camel_object_unref (CAMEL_OBJECT (cia));
    
    return new;
}

static void
e_mail_address_free (EMailAddress *addr)
{
    g_return_if_fail (addr != NULL);
    
    g_free (addr->address);
    if (addr->wname)
        e_name_western_free (addr->wname);
    g_free (addr);
}

static gint
e_mail_address_compare (gconstpointer address1, gconstpointer address2)
{
    const EMailAddress *addr1 = address1;
    const EMailAddress *addr2 = address2;
    gint retval;
    
    g_return_val_if_fail (addr1 != NULL, 1);
    g_return_val_if_fail (addr2 != NULL, -1);
    
    if (!addr1->wname || !addr2->wname) {
        /* have to compare addresses, one or both don't have names */
        g_return_val_if_fail (addr1->address != NULL, 1);
        g_return_val_if_fail (addr2->address != NULL, -1);
        
        retval = g_strcasecmp (addr1->address, addr2->address);
    } else {
        if (!addr1->wname->last && !addr2->wname->last) {
            /* neither has a last name - default to address? */
            /* FIXME: what do we compare next? */
            g_return_val_if_fail (addr1->address != NULL, 1);
            g_return_val_if_fail (addr2->address != NULL, -1);
            
            retval = g_strcasecmp (addr1->address, addr2->address);
        } else {
            /* compare last names */
            if (!addr1->wname->last)
                retval = -1;
            else if (!addr2->wname->last)
                retval = 1;
            else {
                retval = g_strcasecmp (addr1->wname->last, addr2->wname->last);
                if (!retval) {
                    /* last names are identical - compare first names */
                    if (!addr1->wname->first)
                        retval = -1;
                    else if (!addr2->wname->first)
                        retval = 1;
                    else {
                        retval = g_strcasecmp (addr1->wname->first, addr2->wname->first);
                        if (!retval) {
                            /* first names are identical - compare addresses */
                            g_return_val_if_fail (addr1->address != NULL, 1);
                            g_return_val_if_fail (addr2->address != NULL, -1);
                            
                            retval = g_strcasecmp (addr1->address, addr2->address);
                        }
                    }
                }
            }
        }
    }
    
    return retval;
}

static gint
address_compare (gconstpointer address1, gconstpointer address2)
{
    EMailAddress *addr1, *addr2;
    gint retval;
    
    addr1 = e_mail_address_new (address1);
    addr2 = e_mail_address_new (address2);
    retval = e_mail_address_compare (addr1, addr2);
    e_mail_address_free (addr1);
    e_mail_address_free (addr2);
    
    return retval;
}

static gint
subject_compare (gconstpointer subject1, gconstpointer subject2)
{
    char *sub1;
    char *sub2;
    
    /* trim off any "Re:"'s at the beginning of subject1 */
    sub1 = (char *) subject1;
    while (!g_strncasecmp (sub1, "Re:", 3)) {
        sub1 += 3;
        /* jump over any spaces */
        for ( ; *sub1 && isspace (*sub1); sub1++);
    }
    
    /* trim off any "Re:"'s at the beginning of subject2 */
    sub2 = (char *) subject2;
    while (!g_strncasecmp (sub2, "Re:", 3)) {
        sub2 += 3;
        /* jump over any spaces */
        for ( ; *sub2 && isspace (*sub2); sub2++);
    }
    
    /* jump over any spaces */
    for ( ; *sub1 && isspace (*sub1); sub1++);
    for ( ; *sub2 && isspace (*sub2); sub2++);
    
    return g_strcasecmp (sub1, sub2);
}

/* Gets the uid of the message displayed at a given view row */
static const char *
get_message_uid (MessageList *message_list, int row)
{
    ETreeModel *model = (ETreeModel *)message_list->table_model;
    ETreePath *node;
    const char *uid;

    if (row >= e_table_model_row_count (message_list->table_model))
        return NULL;

    node = e_tree_model_node_at_row (model, row);
    g_return_val_if_fail (node != NULL, NULL);
    uid = e_tree_model_node_get_data (model, node);

    if (!id_is_uid(uid))
        return NULL;

    return id_uid(uid);
}

/* Gets the CamelMessageInfo for the message displayed at the given
 * view row.
 */
static const CamelMessageInfo *
get_message_info (MessageList *message_list, int row)
{
    const char *uid;
    
    uid = get_message_uid(message_list, row);
    if (uid)
        return camel_folder_get_message_info(message_list->folder, uid);

    return NULL;
}

/**
 * message_list_select:
 * @message_list: a MessageList
 * @base_row: the (model) row to start from
 * @direction: the direction to search in
 * @flags: a set of flag values
 * @mask: a mask for comparing against @flags
 *
 * This moves the message list selection to a suitable row. @base_row
 * lists the first (model) row to try, but as a special case, model
 * row -1 is mapped to the last row. @flags and @mask combine to specify
 * what constitutes a suitable row. @direction is
 * %MESSAGE_LIST_SELECT_NEXT if it should find the next matching
 * message, or %MESSAGE_LIST_SELECT_PREVIOUS if it should find the
 * previous. If no suitable row is found, the selection will be
 * unchanged but the message display will be cleared.
 **/
void
message_list_select (MessageList *message_list, int base_row,
             MessageListSelectDirection direction,
             guint32 flags, guint32 mask)
{
    const CamelMessageInfo *info;
    int vrow, mrow, last;
    ETableScrolled *ets = E_TABLE_SCROLLED (message_list->etable);

    switch (direction) {
    case MESSAGE_LIST_SELECT_PREVIOUS:
        last = -1;
        break;
    case MESSAGE_LIST_SELECT_NEXT:
        last = e_table_model_row_count (message_list->table_model);
        break;
    default:
        g_warning("Invalid argument to message_list_select");
        return;
    }

    if (base_row == -1)
        base_row = e_table_model_row_count(message_list->table_model) - 1;

    /* model_to_view_row etc simply dont work for sorted views.  Sigh. */
    vrow = e_table_model_to_view_row (ets->table, base_row);

    /* We don't know whether to use < or > due to "direction" */
    while (vrow != last) {
        mrow = e_table_view_to_model_row (ets->table, vrow);
        info = get_message_info (message_list, mrow);
        if (info && (info->flags & mask) == flags) {
            e_table_scrolled_set_cursor_row (ets, vrow);
            gtk_signal_emit(GTK_OBJECT (message_list), message_list_signals [MESSAGE_SELECTED], info->uid);
            return;
        }
        vrow += direction;
    }

    gtk_signal_emit(GTK_OBJECT (message_list), message_list_signals [MESSAGE_SELECTED], NULL);
}

#if 0
static void
message_list_drag_data_get (ETable             *table,
                int                 row,
                int                 col,
                GdkDragContext     *context,
                GtkSelectionData   *selection_data,
                guint               info,
                guint               time,
                gpointer            user_data)
{
    MessageList *mlist = (MessageList *) user_data;
    CamelMessageInfo *info = get_message_info (mlist, row);
    CamelException *ex;
    CamelFolder *folder;
    char *dirname = "/tmp/ev-XXXXXXXXXX";
    char *filename;
    char *url;
    
    switch (info) {
    case DND_TARGET_LIST_TYPE_URI:
        /* drag & drop into nautilus */
        mktemp (dirname);
        filename = g_strdup_printf ("%s.eml", info->subject);
        url = g_strdup_printf ("file:%s", dirname);
        
        ex = camel_exception_new ();
        folder = mail_tool_get_folder_from_urlname (url, filename, CAMEL_STORE_FOLDER_CREATE, ex);
        if (camel_exception_is_set (ex)) {
            camel_exception_free (ex);
            g_free (url);
            return;
        }
        
        gtk_selection_data_set (selection_data, selection_data->target, 8,
                    (guchar *) url, strlen (url));
        
        camel_object_unref (CAMEL_OBJECT (folder));
        g_free (filename);
        g_free (url);
        break;
    default:
        break;
    }
}
#endif

/*
 * SimpleTableModel::col_count
 */
static int
ml_col_count (ETableModel *etm, void *data)
{
    return COL_LAST;
}

static void *
ml_duplicate_value (ETableModel *etm, int col, const void *value, void *data)
{
    switch (col){
    case COL_MESSAGE_STATUS:
    case COL_FLAGGED:
    case COL_SCORE:
    case COL_ATTACHMENT:
    case COL_DELETED:
    case COL_UNREAD:
    case COL_SENT:
    case COL_RECEIVED:
        return (void *) value;

    case COL_FROM:
    case COL_SUBJECT:
    case COL_TO:
    case COL_SIZE:
        return g_strdup (value);
    default:
        g_assert_not_reached ();
    }
    return NULL;
}

static void
ml_free_value (ETableModel *etm, int col, void *value, void *data)
{
    switch (col){
    case COL_MESSAGE_STATUS:
    case COL_FLAGGED:
    case COL_SCORE:
    case COL_ATTACHMENT:
    case COL_DELETED:
    case COL_UNREAD:
    case COL_SENT:
    case COL_RECEIVED:
        break;

    case COL_FROM:
    case COL_SUBJECT:
    case COL_TO:
    case COL_SIZE:
        g_free (value);
        break;
    default:
        g_assert_not_reached ();
    }
}

static void *
ml_initialize_value (ETableModel *etm, int col, void *data)
{
    switch (col){
    case COL_MESSAGE_STATUS:
    case COL_FLAGGED:
    case COL_SCORE:
    case COL_ATTACHMENT:
    case COL_DELETED:
    case COL_UNREAD:
    case COL_SENT:
    case COL_RECEIVED:
        return NULL;

    case COL_FROM:
    case COL_SUBJECT:
    case COL_TO:
    case COL_SIZE:
        return g_strdup("");
    default:
        g_assert_not_reached ();
    }

    return NULL;
}

static gboolean
ml_value_is_empty (ETableModel *etm, int col, const void *value, void *data)
{
    switch (col){
    case COL_MESSAGE_STATUS:
    case COL_FLAGGED:
    case COL_SCORE:
    case COL_ATTACHMENT:
    case COL_DELETED:
    case COL_UNREAD:
    case COL_SENT:
    case COL_RECEIVED:
        return value == NULL;

    case COL_FROM:
    case COL_SUBJECT:
    case COL_TO:
    case COL_SIZE:
        return !(value && *(char *)value);
    default:
        g_assert_not_reached ();
        return FALSE;
    }
}

static char *
ml_value_to_string (ETableModel *etm, int col, const void *value, void *data)
{
    switch (col){
    case COL_MESSAGE_STATUS:
        switch ((int) value) {
        case 0:
            return g_strdup (_("Unseen"));
            break;
        case 1:
            return g_strdup (_("Seen"));
            break;
        case 2:
            return g_strdup (_("Answered"));
            break;
        default:
            return g_strdup ("");
            break;
        }
        break;
        
    case COL_SCORE:
        switch ((int) value) {
        case -3:
            return g_strdup ("Lowest");
            break;
        case -2:
            return g_strdup ("Lower");
            break;
        case -1:
            return g_strdup ("Low");
            break;
        case 1:
            return g_strdup ("High");
            break;
        case 2:
            return g_strdup ("Higher");
            break;
        case 3:
            return g_strdup ("Highest");
            break;
        default:
            return g_strdup ("Normal");
            break;
        }
        break;
        
    case COL_ATTACHMENT:
    case COL_FLAGGED:
    case COL_DELETED:
    case COL_UNREAD:
        return g_strdup_printf("%d", (int) value);
        
    case COL_SENT:
    case COL_RECEIVED:
        return filter_date (value);
        
    case COL_FROM:
    case COL_SUBJECT:
    case COL_TO:
    case COL_SIZE:
        return g_strdup (value);
    default:
        g_assert_not_reached ();
        return NULL;
    }
}

static GdkPixbuf *
ml_tree_icon_at (ETreeModel *etm, ETreePath *path, void *model_data)
{
    /* we dont really need an icon ... */
    return NULL;
}

/* return true if there are any unread messages in the subtree */
static int
subtree_unread(MessageList *ml, ETreePath *node)
{
    const CamelMessageInfo *info;
    char *uid;

    while (node) {
        ETreePath *child;
        uid = e_tree_model_node_get_data((ETreeModel *)ml->table_model, node);
        if (id_is_uid(uid)) {
            info = camel_folder_get_message_info(ml->folder, id_uid(uid));
            if (!(info->flags & CAMEL_MESSAGE_SEEN))
                return TRUE;
        }
        if ((child = e_tree_model_node_get_first_child (E_TREE_MODEL (ml->table_model), node)))
            if (subtree_unread(ml, child))
                return TRUE;
        node = e_tree_model_node_get_next (E_TREE_MODEL (ml->table_model), node);
    }
    return FALSE;
}

static gboolean
content_is_attachment(CamelMessageContentInfo *ci)
{
    CamelMessageContentInfo *child;

    /* no info about content */
    if (ci == NULL)
        return FALSE;

    /* we assume multipart/mixed is an attachment always
       other multipart / * is only an attachment if it contains multipart/mixed's, or
       non-text parts */
    if (header_content_type_is(ci->type, "multipart", "*")) {
        if (header_content_type_is(ci->type, "multipart", "mixed")) {
            return TRUE;
        }
        child = ci->childs;
        while (child) {
            if (content_is_attachment(child)) {
                return TRUE;
            }
            child = child->next;
        }
        return FALSE;
    } else {
        return !header_content_type_is(ci->type, "text", "*");
    }
}

static void *
ml_tree_value_at (ETreeModel *etm, ETreePath *path, int col, void *model_data)
{
    MessageList *message_list = model_data;
    const CamelMessageInfo *msg_info;
    static char buffer [10];
    char *uid;

    /* retrieve the message information array */
    uid = e_tree_model_node_get_data (etm, path);
    if (!id_is_uid(uid))
        goto fake;
    uid = id_uid(uid);

    msg_info = camel_folder_get_message_info (message_list->folder, uid);
    if (msg_info == NULL) {
        g_warning("UID for message-list not found in folder: %s", uid);
        return NULL;
    }
    
    switch (col){
    case COL_MESSAGE_STATUS:
        if (msg_info->flags & CAMEL_MESSAGE_ANSWERED)
            return GINT_TO_POINTER (2);
        else if (msg_info->flags & CAMEL_MESSAGE_SEEN)
            return GINT_TO_POINTER (1);
        else
            return GINT_TO_POINTER (0);
        
    case COL_FLAGGED:
        if (msg_info->flags & CAMEL_MESSAGE_FLAGGED)
            return GINT_TO_POINTER (1);
        else
            return GINT_TO_POINTER (0);

    case COL_SCORE:
    {
        const char *tag;
        int score = 0;
        
        tag = camel_tag_get ((CamelTag **) &msg_info->user_tags, "score");
        if (tag)
            score = atoi (tag);
        
        return GINT_TO_POINTER (score);
    }
        
    case COL_ATTACHMENT:
        if (content_is_attachment(msg_info->content))
            return (void *)1;
        else
            return (void *)0;
        
    case COL_FROM:
        if (msg_info->from)
            return msg_info->from;
        else
            return "";
        
    case COL_SUBJECT:
        if (msg_info->subject)
            return msg_info->subject;
        else
            return "";
        
    case COL_SENT:
        return GINT_TO_POINTER (msg_info->date_sent);
        
    case COL_RECEIVED:
        return GINT_TO_POINTER (msg_info->date_received);
        
    case COL_TO:
        if (msg_info->to)
            return msg_info->to;
        else
            return "";
        
    case COL_SIZE:
        sprintf (buffer, "%d", msg_info->size);
        return buffer;
        
    case COL_DELETED:
        if (msg_info->flags & CAMEL_MESSAGE_DELETED)
            return GINT_TO_POINTER (1);
        else
            return GINT_TO_POINTER (0);
        
    case COL_UNREAD:
        return GINT_TO_POINTER (!(msg_info->flags & CAMEL_MESSAGE_SEEN));
        
    case COL_COLOUR:
    {
        const char *colour;

        colour = camel_tag_get ((CamelTag **) &msg_info->user_tags,
                    "colour");
        if (colour)
            return (void *)colour;
        else if (msg_info->flags & CAMEL_MESSAGE_FLAGGED)
            /* FIXME: extract from the xpm somehow. */
            return "#A7453E";
        else
            return NULL;
    }
    }

    g_assert_not_reached ();
    
 fake:
    /* This is a fake tree parent */
    switch (col){
    case COL_UNREAD:
        /* this value should probably be cached, as it could take a bit
           of processing to evaluate all the time */
        return (void *)subtree_unread(message_list,
                          e_tree_model_node_get_first_child(etm, path));
    case COL_MESSAGE_STATUS:
    case COL_FLAGGED:
    case COL_SCORE:
    case COL_ATTACHMENT:
    case COL_DELETED:
    case COL_COLOUR:
    case COL_SENT:
    case COL_RECEIVED:
        return (void *) 0;
        
    case COL_SUBJECT:
        return id_subject(uid);
        
    case COL_FROM:
    case COL_TO:
    case COL_SIZE:
        return "?";
    }
    g_assert_not_reached ();
    
    return NULL;
}

static void
ml_tree_set_value_at (ETreeModel *etm, ETreePath *path, int col,
              const void *val, void *model_data)
{
    g_assert_not_reached ();
}

static gboolean
ml_tree_is_cell_editable (ETreeModel *etm, ETreePath *path, int col, void *model_data)
{
    return FALSE;
}

static void
message_list_init_images (void)
{
    int i;
    
    /*
     * Only load once, and share
     */
    if (states_pixmaps [0].pixbuf)
        return;
    
    for (i = 0; states_pixmaps [i].image_base; i++){
        states_pixmaps [i].pixbuf = gdk_pixbuf_new_from_xpm_data (
            (const char **) states_pixmaps [i].image_base);
    }
}

static char *
filter_date (const void *data)
{
    time_t date = GPOINTER_TO_INT (data);
    char buf[26], *p;

    if (date == 0)
        return g_strdup ("?");

#ifdef CTIME_R_THREE_ARGS
    ctime_r (&date, buf, 26);
#else
    ctime_r (&date, buf);
#endif

    p = strchr (buf, '\n');
    if (p)
        *p = '\0';

    return g_strdup (buf);
}

static ETableExtras *
message_list_create_extras (void)
{
    int i;
    GdkPixbuf *images [7];
    ETableExtras *extras;
    ECell *cell;

    extras = e_table_extras_new();
    e_table_extras_add_pixbuf(extras, "status", states_pixmaps [0].pixbuf);
    e_table_extras_add_pixbuf(extras, "score", states_pixmaps [11].pixbuf);
    e_table_extras_add_pixbuf(extras, "attachment", states_pixmaps [4].pixbuf);
    e_table_extras_add_pixbuf(extras, "flagged", states_pixmaps [5].pixbuf);
    
    e_table_extras_add_compare(extras, "address_compare", address_compare);
    e_table_extras_add_compare(extras, "subject_compare", subject_compare);
    
    for (i = 0; i < 3; i++)
        images [i] = states_pixmaps [i].pixbuf;
    
    e_table_extras_add_cell(extras, "render_message_status", e_cell_toggle_new (0, 3, images));

    for (i = 0; i < 2; i++)
        images [i] = states_pixmaps [i + 3].pixbuf;
    
    e_table_extras_add_cell(extras, "render_attachment", e_cell_toggle_new (0, 2, images));
    
    images [1] = states_pixmaps [5].pixbuf;
    e_table_extras_add_cell(extras, "render_flagged", e_cell_toggle_new (0, 2, images));

    for (i = 0; i < 7; i++)
        images[i] = states_pixmaps [i + 5].pixbuf;
    
    e_table_extras_add_cell(extras, "render_score", e_cell_toggle_new (0, 7, images));
    
    /* date cell */
    cell = e_cell_text_new (NULL, GTK_JUSTIFY_LEFT);
    gtk_object_set (GTK_OBJECT (cell),
            "text_filter", filter_date,
            NULL);
    gtk_object_set (GTK_OBJECT (cell),
            "strikeout_column", COL_DELETED,
            NULL);
    gtk_object_set (GTK_OBJECT (cell),
            "bold_column", COL_UNREAD,
            NULL);
    gtk_object_set (GTK_OBJECT (cell),
            "color_column", COL_COLOUR,
            NULL);
    e_table_extras_add_cell(extras, "render_date", cell);
    
    /* text cell */
    cell = e_cell_text_new (NULL, GTK_JUSTIFY_LEFT);
    gtk_object_set (GTK_OBJECT (cell),
            "strikeout_column", COL_DELETED,
            NULL);
    gtk_object_set (GTK_OBJECT (cell),
            "bold_column", COL_UNREAD,
            NULL);
    gtk_object_set (GTK_OBJECT (cell),
            "color_column", COL_COLOUR,
            NULL);
    e_table_extras_add_cell(extras, "render_text", cell);

    e_table_extras_add_cell(extras, "render_tree", 
        e_cell_tree_new (NULL, NULL, /* let the tree renderer default the pixmaps */
                 TRUE, cell));

    return extras;
}

static void
save_header_state(MessageList *ml)
{
    char *filename;

    if (ml->folder == NULL
        || ml->etable == NULL)
        return;

    filename = mail_config_folder_to_cachename(ml->folder, "et-header-");
    e_table_scrolled_save_state(E_TABLE_SCROLLED(ml->etable), filename);
    g_free(filename);
}

static char *
message_list_get_layout (MessageList *message_list)
{
    /* Default: Status, Attachments, Priority, From, Subject, Date */
    return g_strdup ("<ETableSpecification cursor-mode=\"line\" draw-grid=\"true\">"
             "<ETableColumn model_col= \"0\" pixbuf=\"status\" expansion=\"0.0\" minimum_width=\"18\" resizable=\"false\" cell=\"render_message_status\" compare=\"integer\" sortable=\"false\"/>"
             "<ETableColumn model_col= \"1\" pixbuf=\"flagged\" expansion=\"0.0\" minimum_width=\"20\" resizable=\"false\" cell=\"render_flagged\" compare=\"integer\"/>"
             "<ETableColumn model_col= \"2\" pixbuf=\"score\" expansion=\"0.0\" minimum_width=\"20\" resizable=\"false\" cell=\"render_score\" compare=\"integer\"/>"
             "<ETableColumn model_col= \"3\" pixbuf=\"attachment\" expansion=\"0.0\" minimum_width=\"18\" resizable=\"false\" cell=\"render_attachment\" compare=\"integer\" sortable=\"false\"/>"
             "<ETableColumn model_col= \"4\" _title=\"From\" expansion=\"24.0\" minimum_width=\"32\" resizable=\"true\" cell=\"render_text\" compare=\"address_compare\"/>"
             "<ETableColumn model_col= \"5\" _title=\"Subject\" expansion=\"30.0\" minimum_width=\"32\" resizable=\"true\" cell=\"render_tree\" compare=\"subject_compare\"/>"
             "<ETableColumn model_col= \"6\" _title=\"Date\" expansion=\"24.0\" minimum_width=\"32\" resizable=\"true\" cell=\"render_date\" compare=\"integer\"/>"
             "<ETableColumn model_col= \"7\" _title=\"Received\" expansion=\"20.0\" minimum_width=\"32\" resizable=\"true\" cell=\"render_date\" compare=\"integer\"/>"
             "<ETableColumn model_col= \"8\" _title=\"To\" expansion=\"24.0\" minimum_width=\"32\" resizable=\"true\" cell=\"render_text\" compare=\"address_compare\"/>"
             "<ETableColumn model_col= \"9\" _title=\"Size\" expansion=\"6.0\" minimum_width=\"32\" resizable=\"true\" cell=\"render_text\" compare=\"string\"/>"
             "<ETableState> <column source=\"0\"/> <column source=\"3\"/> <column source=\"1\"/>"
             "<column source=\"4\"/> <column source=\"5\"/> <column source=\"6\"/>"
             "<grouping> </grouping> </ETableState>"
             "</ETableSpecification>");
}

static void
message_list_setup_etable(MessageList *message_list)
{
    /* build the spec based on the folder, and possibly from a saved file */
    /* otherwise, leave default */
    if (message_list->folder) {
        char *path;
        char *name;
        struct stat st;
        
        name = camel_service_get_name (CAMEL_SERVICE (message_list->folder->parent_store), TRUE);
        printf ("folder name is '%s'\n", name);
        path = mail_config_folder_to_cachename (message_list->folder, "et-header-");
        
        if (path && stat (path, &st) == 0 && st.st_size > 0 && S_ISREG (st.st_mode)) {
            /* build based on saved file */
            e_table_scrolled_load_state (E_TABLE_SCROLLED (message_list->etable), path);
        } else if (strstr (name, "/Drafts") || strstr (name, "/Outbox") || strstr (name, "/Sent")) {
            /* these folders have special defaults */
            char *state = "<ETableState>"
                "<column source=\"0\"/> <column source=\"1\"/> "
                "<column source=\"8\"/> <column source=\"5\"/> "
                "<column source=\"6\"/> <grouping> </grouping> </ETableState>";
            
            e_table_scrolled_set_state (E_TABLE_SCROLLED (message_list->etable), state);
        }
        
        g_free (path);
        g_free (name);
    }
}

/*
 * GtkObject::init
 */
static void
message_list_init (GtkObject *object)
{
    ETableExtras *extras;
    MessageList *message_list = MESSAGE_LIST (object);
    char *spec;

    message_list->table_model = (ETableModel *)
        e_tree_simple_new (ml_col_count,
                   ml_duplicate_value,
                   ml_free_value,
                   ml_initialize_value,
                   ml_value_is_empty,
                   ml_value_to_string,
                   ml_tree_icon_at, ml_tree_value_at,
                   ml_tree_set_value_at,
                   ml_tree_is_cell_editable,
                   message_list);
    e_tree_model_root_node_set_visible ((ETreeModel *)message_list->table_model, FALSE);
    gtk_signal_connect (GTK_OBJECT (message_list->table_model), "destroy", 
                (GtkSignalFunc) free_tree_ids, NULL);

    /*
     * The etable
     */

    spec = message_list_get_layout (message_list);
    extras = message_list_create_extras();
    message_list->etable = e_table_scrolled_new (message_list->table_model, extras, spec, NULL);
    g_free (spec);
    gtk_object_sink(GTK_OBJECT(extras));

    e_scroll_frame_set_policy (E_SCROLL_FRAME (message_list->etable), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);

    gtk_object_set(GTK_OBJECT(message_list->etable),
               "drawfocus", FALSE,
               NULL);

    /*
     *gtk_signal_connect (GTK_OBJECT (message_list->etable), "realize",
     *          GTK_SIGNAL_FUNC (select_row), message_list);
     */

    gtk_signal_connect (GTK_OBJECT (message_list->etable), "cursor_change",
               GTK_SIGNAL_FUNC (on_cursor_change_cmd), message_list);

    gtk_signal_connect (GTK_OBJECT (message_list->etable), "click",
                GTK_SIGNAL_FUNC (on_click), message_list);

#if 0
    /* drag & drop */
    e_table_drag_source_set (message_list->etable, GDK_BUTTON1_MASK,
                 drag_types, num_drag_types, GDK_ACTION_MOVE);
    
    gtk_signal_connect (GTK_OBJECT (message_list->etable), "drag_data_get",
                GTK_SIGNAL_FUNC (message_list_drag_data_get), message_list);
#endif
    
    gtk_widget_show (message_list->etable);

    gtk_object_ref (GTK_OBJECT (message_list->table_model));
    gtk_object_sink (GTK_OBJECT (message_list->table_model));
    
    /*
     * We do own the Etable, not some widget container
     */
    gtk_object_ref (GTK_OBJECT (message_list->etable));
    gtk_object_sink (GTK_OBJECT (message_list->etable));

    message_list->uid_rowmap = g_hash_table_new (g_str_hash, g_str_equal);
}

static void
message_list_destroy (GtkObject *object)
{
    MessageList *message_list = MESSAGE_LIST (object);

    if (message_list->folder) {
        save_tree_state(message_list);
        save_header_state(message_list);
    }
    
    gtk_object_unref (GTK_OBJECT (message_list->table_model));
    gtk_object_unref (GTK_OBJECT (message_list->etable));
    
    g_hash_table_destroy (message_list->uid_rowmap);

    if (message_list->idle_id != 0)
        g_source_remove(message_list->idle_id);
    
    if (message_list->seen_id)
        gtk_timeout_remove (message_list->seen_id);
    
    if (message_list->folder) {
        camel_object_unhook_event((CamelObject *)message_list->folder, "folder_changed",
                      folder_changed, message_list);
        camel_object_unhook_event((CamelObject *)message_list->folder, "message_changed",
                      message_changed, message_list);
        camel_object_unref (CAMEL_OBJECT (message_list->folder));
    }
    
    GTK_OBJECT_CLASS (message_list_parent_class)->destroy (object);
}

/*
 * CORBA method: Evolution::MessageList::select_message
 */
static void
MessageList_select_message (PortableServer_Servant _servant,
                const CORBA_long message_number,
                CORBA_Environment *ev)
{
    printf ("FIXME: select message method\n");
}

/*
 * CORBA method: Evolution::MessageList::open_message
 */
static void
MessageList_open_message (PortableServer_Servant _servant,
              const CORBA_long message_number,
              CORBA_Environment *ev)
{
    printf ("FIXME: open message method\n");
}

static POA_GNOME_Evolution_MessageList__epv *
evolution_message_list_get_epv (void)
{
    POA_GNOME_Evolution_MessageList__epv *epv;

    epv = g_new0 (POA_GNOME_Evolution_MessageList__epv, 1);

    epv->selectMessage = MessageList_select_message;
    epv->openMessage   = MessageList_open_message;

    return epv;
}

static void
message_list_corba_class_init (void)
{
    evolution_message_list_vepv.Bonobo_Unknown_epv = bonobo_object_get_epv ();
    evolution_message_list_vepv.GNOME_Evolution_MessageList_epv = evolution_message_list_get_epv ();
}

/*
 * GtkObjectClass::init
 */
static void
message_list_class_init (GtkObjectClass *object_class)
{
    message_list_parent_class = gtk_type_class (PARENT_TYPE);

    object_class->destroy = message_list_destroy;

    message_list_corba_class_init ();

    message_list_signals[MESSAGE_SELECTED] =
        gtk_signal_new ("message_selected",
                GTK_RUN_LAST,
                object_class->type,
                GTK_SIGNAL_OFFSET (MessageListClass, message_selected),
                gtk_marshal_NONE__STRING,
                GTK_TYPE_NONE, 1, GTK_TYPE_STRING);

    gtk_object_class_add_signals(object_class, message_list_signals, LAST_SIGNAL);

    message_list_init_images ();
}

static void
message_list_construct (MessageList *message_list, GNOME_Evolution_MessageList corba_message_list)
{
    bonobo_object_construct (BONOBO_OBJECT (message_list), corba_message_list);
}

static GNOME_Evolution_MessageList
create_corba_message_list (BonoboObject *object)
{
    POA_GNOME_Evolution_MessageList *servant;
    CORBA_Environment ev;

    servant = (POA_GNOME_Evolution_MessageList *) g_new0 (BonoboObjectServant, 1);
    servant->vepv = &evolution_message_list_vepv;

    CORBA_exception_init (&ev);
    POA_GNOME_Evolution_MessageList__init ((PortableServer_Servant) servant, &ev);
    if (ev._major != CORBA_NO_EXCEPTION){
        g_free (servant);
        CORBA_exception_free (&ev);
        return CORBA_OBJECT_NIL;
    }

    CORBA_exception_free (&ev);
    return (GNOME_Evolution_MessageList) bonobo_object_activate_servant (object, servant);
}

BonoboObject *
message_list_new (void)
{
    GNOME_Evolution_MessageList corba_object;
    MessageList *message_list;

    message_list = gtk_type_new (message_list_get_type ());

    corba_object = create_corba_message_list (BONOBO_OBJECT (message_list));
    if (corba_object == CORBA_OBJECT_NIL){
        gtk_object_destroy (GTK_OBJECT (message_list));
        return NULL;
    }

    message_list->idle_id = 0;

    message_list_construct (message_list, corba_object);

    return BONOBO_OBJECT (message_list);
}

static void
clear_tree (MessageList *ml)
{
    ETreeModel *etm = E_TREE_MODEL (ml->table_model);

#ifdef TIMEIT
    struct timeval start, end;
    unsigned long diff;

    printf("Clearing tree\n");
    gettimeofday(&start, NULL);
#endif

    /* we also reset the uid_rowmap since it is no longer useful/valid anyway */
    g_hash_table_destroy (ml->uid_rowmap);
    ml->uid_rowmap = g_hash_table_new(g_str_hash, g_str_equal);
    free_tree_ids(etm);
    
    if (ml->tree_root) {
        /* FIXME: Freeze here, buddy. */
        e_tree_model_node_remove (etm, ml->tree_root);
    }

    ml->tree_root = e_tree_model_node_insert (etm, NULL, 0, NULL);
    e_tree_model_node_set_expanded (etm, ml->tree_root, TRUE);

#ifdef TIMEIT
    gettimeofday(&end, NULL);
    diff = end.tv_sec * 1000 + end.tv_usec/1000;
    diff -= start.tv_sec * 1000 + start.tv_usec/1000;
    printf("Clearing tree took %ld.%03ld seconds\n", diff / 1000, diff % 1000);
#endif

}

/* we save the node id to the file if the node should be closed when
   we start up.  We only save nodeid's for messages with children */
static void
save_node_state(MessageList *ml, FILE *out, ETreePath *node)
{
    char *data;
    const CamelMessageInfo *info;

    while (node) {
        ETreePath *child = e_tree_model_node_get_first_child (E_TREE_MODEL (ml->table_model), node);
        if (child
            && !e_tree_model_node_is_expanded((ETreeModel *)ml->table_model, node)) {
            data = e_tree_model_node_get_data((ETreeModel *)ml->table_model, node);
            if (data) {
                if (id_is_uid(data)) {
                    info = camel_folder_get_message_info(ml->folder, id_uid(data));
                    if (info) {
                        fprintf(out, "%08x%08x\n", info->message_id.id.part.hi, info->message_id.id.part.lo);
                    }
                } else {
                    fprintf(out, "%s\n", data);
                }
            }
        }
        if (child) {
            save_node_state(ml, out, child);
        }
        node = e_tree_model_node_get_next (E_TREE_MODEL (ml->table_model), node);
    }
}

static GHashTable *
load_tree_state(MessageList *ml)
{
    char *filename, linebuf[10240];
    GHashTable *result;
    FILE *in;
    int len;

    result = g_hash_table_new(g_str_hash, g_str_equal);
    filename = mail_config_folder_to_cachename(ml->folder, "treestate-");
    in = fopen(filename, "r");
    if (in) {
        while (fgets(linebuf, sizeof(linebuf), in) != NULL) {
            len = strlen(linebuf);
            if (len) {
                linebuf[len-1] = 0;
                g_hash_table_insert(result, g_strdup(linebuf), (void *)1);
            }
        }
        fclose(in);
    }
    g_free(filename);
    return result;
}

/* save tree info */
static void
save_tree_state(MessageList *ml)
{
    char *filename;
    ETreePath *node;
    ETreePath *child;
    FILE *out;

    filename = mail_config_folder_to_cachename(ml->folder, "treestate-");
    out = fopen(filename, "w");
    if (out) {
        node = e_tree_model_get_root((ETreeModel *)ml->table_model);
        child = e_tree_model_node_get_first_child ((ETreeModel *)ml->table_model, node);
        if (node && child) {
            save_node_state(ml, out, child);
        }
        fclose(out);
    }
    g_free(filename);
}

static void
free_node_state(void *key, void *value, void *data)
{
    g_free(key);
}

static void
free_tree_state(GHashTable *expanded_nodes)
{
    g_hash_table_foreach(expanded_nodes, free_node_state, 0);
    g_hash_table_destroy(expanded_nodes);
}

/* only call if we have a tree model */
/* builds the tree structure */
static void build_subtree (MessageList *ml, ETreePath *parent, struct _container *c, int *row, GHashTable *);

static void build_subtree_diff (MessageList *ml, ETreePath *parent, ETreePath *path, struct _container *c, int *row, GHashTable *expanded_nodes);

static void
build_tree (MessageList *ml, struct _thread_messages *thread)
{
    int row = 0;
    GHashTable *expanded_nodes;
    ETreeModel *etm = (ETreeModel *)ml->table_model;
    ETreePath *top;

#ifdef TIMEIT
    struct timeval start, end;
    unsigned long diff;

    printf("Building tree\n");
    gettimeofday(&start, NULL);
#endif
    expanded_nodes = load_tree_state(ml);

#ifdef TIMEIT
    gettimeofday(&end, NULL);
    diff = end.tv_sec * 1000 + end.tv_usec/1000;
    diff -= start.tv_sec * 1000 + start.tv_usec/1000;
    printf("Loading tree state took %ld.%03ld seconds\n", diff / 1000, diff % 1000);
#endif

    if (ml->tree_root == NULL) {
        ml->tree_root = e_tree_model_node_insert(etm, NULL, 0, NULL);
        e_tree_model_node_set_expanded(etm, ml->tree_root, TRUE);
    }

    top = e_tree_model_node_get_first_child(etm, ml->tree_root);
    if (top == NULL) {
        build_subtree(ml, ml->tree_root, thread->tree, &row, expanded_nodes);
    } else {
        build_subtree_diff(ml, ml->tree_root, top,  thread->tree, &row, expanded_nodes);
    }

    free_tree_state(expanded_nodes);
    
#ifdef TIMEIT
    gettimeofday(&end, NULL);
    diff = end.tv_sec * 1000 + end.tv_usec/1000;
    diff -= start.tv_sec * 1000 + start.tv_usec/1000;
    printf("Building tree took %ld.%03ld seconds\n", diff / 1000, diff % 1000);
#endif

}

static char *
new_id_from_uid(const char *uid)
{
    char *res;
    int len;

    len = strlen(uid)+2;
    res = g_malloc(len);
    res[0] = 'u';
    strcpy(res+1, uid);
    return res;
}

static char *
new_id_from_subject(const char *subject)
{
    char *res;
    int len;

    len = strlen(subject)+2;
    res = g_malloc(len);
    res[0] = 's';
    strcpy(res+1, subject);
    return res;
}

/* this is about 20% faster than build_subtree_diff,
   entirely because e_tree_model_node_insert(xx, -1 xx)
   is faster than inserting to the right row :( */
/* Otherwise, this code would probably go as it does the same thing essentially */
static void
build_subtree (MessageList *ml, ETreePath *parent, struct _container *c, int *row, GHashTable *expanded_nodes)
{
    ETreeModel *tree = E_TREE_MODEL (ml->table_model);
    ETreePath *node;
    char *id;
    int expanded = FALSE;

    e_tree_model_freeze (tree);

    while (c) {
        if (c->message) {
            id = new_id_from_uid(c->message->uid);
            g_hash_table_insert(ml->uid_rowmap, id_uid(id), GINT_TO_POINTER ((*row)++));
            if (c->child) {
                if (c->message) {
                    char key[17];
                    sprintf(key, "%08x%08x", c->message->message_id.id.part.hi, c->message->message_id.id.part.lo);
                    expanded = !g_hash_table_lookup(expanded_nodes, key) != 0;
                } else
                    expanded = TRUE;
            }
        } else {
            id = new_id_from_subject(c->root_subject);
            if (c->child) {
                expanded = !g_hash_table_lookup(expanded_nodes, id) != 0;
            }
        }
        node = e_tree_model_node_insert(tree, parent, -1, id);
        if (c->child) {
            /* by default, open all trees */
            if (expanded)
                e_tree_model_node_set_expanded(tree, node, expanded);
            build_subtree(ml, node, c->child, row, expanded_nodes);
        }
        c = c->next;
    }

    e_tree_model_thaw (tree);
}

/* compares a thread tree node with the etable tree node to see if they point to
   the same object */
static int
node_equal(ETreeModel *etm, ETreePath *ap, struct _container *bp)
{
    char *uid;

    uid = e_tree_model_node_get_data(etm, ap);

    if (id_is_uid(uid)) {
        if (bp->message && strcmp(id_uid(uid), bp->message->uid)==0)
            return 1;
    } else if (id_is_subject(uid)) {
        if (bp->message == NULL && strcmp(id_subject(uid), bp->root_subject) == 0)
            return 1;
    }
    return 0;
}

/* adds a single node, retains save state, and handles adding children if required */
static void
add_node_diff(MessageList *ml, ETreePath *parent, ETreePath *path, struct _container *c, int *row, int myrow, GHashTable *expanded_nodes)
{
    ETreeModel *etm = E_TREE_MODEL (ml->table_model);
    ETreePath *node;
    char *id;
    int expanded = FALSE;

    if (c->message) {
        id = new_id_from_uid(c->message->uid);
        /* need to remove the id first, as GHashTable' wont replace the key pointer for us */
        g_hash_table_remove(ml->uid_rowmap, id_uid(id));
        g_hash_table_insert(ml->uid_rowmap, id_uid(id), GINT_TO_POINTER (*row));
        if (c->child) {
            if (c->message) {
                char key[17];
                sprintf(key, "%08x%08x", c->message->message_id.id.part.hi, c->message->message_id.id.part.lo);
                expanded = !g_hash_table_lookup(expanded_nodes, key) != 0;
            } else
                expanded = TRUE;
        }
    } else {
        id = new_id_from_subject(c->root_subject);
        if (c->child) {
            expanded = !g_hash_table_lookup(expanded_nodes, id) != 0;
        }
    }

    t(printf("Adding node: %s row %d\n", id, myrow));

    node = e_tree_model_node_insert(etm, parent, myrow, id);
    (*row)++;
    if (c->child) {
        e_tree_model_node_set_expanded(etm, node, expanded);
        t(printf("Building subtree ...\n"));
        build_subtree_diff(ml, node, NULL, c->child, row, expanded_nodes);
    }
}

/* removes node, children recursively and all associated data */
static void
remove_node_diff(MessageList *ml, ETreePath *node, int depth)
{
    ETreeModel *etm = E_TREE_MODEL (ml->table_model);
    ETreePath *cp, *cn;
    char *uid, *olduid;
    int oldrow;

    t(printf("Removing node: %s\n", (char *)e_tree_model_node_get_data(etm, node)));

    /* we depth-first remove all node data's ... */
    cp = e_tree_model_node_get_first_child(etm, node);
    while (cp) {
        cn = e_tree_model_node_get_next(etm, cp);
        remove_node_diff(ml, cp, depth+1);
        cp = cn;
    }

    /* and the rowid entry - if and only if it is referencing this node */
    uid = e_tree_model_node_get_data(etm, node);
    if (id_is_uid(uid)
        && g_hash_table_lookup_extended(ml->uid_rowmap, id_uid(uid), (void *)&olduid, (void *)&oldrow)
        && olduid == id_uid(uid)) {
        t(printf("removing rowid map entry: %s\n", id_uid(uid)));
        g_hash_table_remove(ml->uid_rowmap, id_uid(uid));
    }
    g_free(uid);
    e_tree_model_node_set_data(etm, node, NULL);

    /* and only at the toplevel, remove the node (etree should optimise this remove somewhat) */
    if (depth == 0)
        e_tree_model_node_remove(etm, node);
}

/* applies a new tree structure to an existing tree, but only by changing things
   that have changed */
static void
build_subtree_diff(MessageList *ml, ETreePath *parent, ETreePath *path, struct _container *c, int *row, GHashTable *expanded_nodes)
{
    ETreeModel *etm = E_TREE_MODEL (ml->table_model);
    ETreePath *ap, *ai, *at, *tmp;
    struct _container *bp, *bi, *bt;
    int i, j, myrow = 0;

    ap = path;
    bp = c;

    while (ap || bp) {
        t(printf("Processing row: %d (subtree row %d)\n", *row, myrow));
        if (ap == NULL) {
            t(printf("out of old nodes\n"));
            /* ran out of old nodes - remaining nodes are added */
            add_node_diff(ml, parent, ap, bp, row, myrow, expanded_nodes);
            myrow++;
            bp = bp->next;
        } else if (bp == NULL) {
            t(printf("out of new nodes\n"));
            /* ran out of new nodes - remaining nodes are removed */
            tmp = e_tree_model_node_get_next(etm, ap);
            remove_node_diff(ml, ap, 0);
            ap = tmp;
        } else if (node_equal(etm, ap, bp)) {
            /*t(printf("nodes match, verify\n"));*/
            /* matching nodes, verify details/children */
            if (bp->message) {
                char *olduid;
                int oldrow;

                /* if this is a message row, check/update the row id map */
                if (g_hash_table_lookup_extended(ml->uid_rowmap, bp->message->uid, (void *)&olduid, (void *)&oldrow)) {
                    if (oldrow != (*row)) {
                        g_hash_table_insert(ml->uid_rowmap, olduid, (void *)(*row));
                    }
                } else {
                    g_warning("Cannot find uid %s in table?", bp->message->uid);
                    /*g_assert_not_reached();*/
                }
            }
            *row = (*row)+1;
            myrow++;
            tmp = e_tree_model_node_get_first_child(etm, ap);
            /* make child lists match (if either has one) */
            if (bp->child || tmp) {
                build_subtree_diff(ml, ap, tmp, bp->child, row, expanded_nodes);
            }
            ap = e_tree_model_node_get_next(etm, ap);
            bp = bp->next;
        } else {
            t(printf("searching for matches\n"));
            /* we have to scan each side for a match */
            bi = bp->next;
            ai = e_tree_model_node_get_next(etm, ap);
            for (i=1;bi!=NULL;i++,bi=bi->next) {
                if (node_equal(etm, ap, bi))
                    break;
            }
            for (j=1;ai!=NULL;j++,ai=e_tree_model_node_get_next(etm, ai)) {
                if (node_equal(etm, ai, bp))
                    break;
            }
            if (i<j) {
                /* smaller run of new nodes - must be nodes to add */
                if (bi) {
                    bt = bp;
                    while (bt != bi) {
                        t(printf("adding new node 0\n"));
                        add_node_diff(ml, parent, NULL, bt, row, myrow, expanded_nodes);
                        myrow++;
                        bt = bt->next;
                    }
                    bp = bi;
                } else {
                    t(printf("adding new node 1\n"));
                    /* no match in new nodes, add one, try next */
                    add_node_diff(ml, parent, NULL, bp, row, myrow, expanded_nodes);
                    myrow++;
                    bp = bp->next;
                }
            } else {
                /* bigger run of old nodes - must be nodes to remove */
                if (ai) {
                    at = ap;
                    while (at != ai) {
                        t(printf("removing old node 0\n"));
                        tmp = e_tree_model_node_get_next(etm, at);
                        remove_node_diff(ml, at, 0);
                        at = tmp;
                    }
                    ap = ai;
                } else {
                    t(printf("adding new node 2\n"));
                    /* didn't find match in old nodes, must be new node? */
                    add_node_diff(ml, parent, NULL, bp, row, myrow, expanded_nodes);
                    myrow++;
                    bp = bp->next;
#if 0
                    tmp = e_tree_model_node_get_next(etm, ap);
                    remove_node_diff(etm, ap, 0);
                    ap = tmp;
#endif
                }
            }
        }
    }
}

static gboolean
free_ids_cb (ETreeModel *model, ETreePath *node, gpointer data)
{
    g_free (e_tree_model_node_get_data (model, node));
    return FALSE;
}

static void
free_tree_ids (ETreeModel *etm)
{
    ETreePath *root = e_tree_model_get_root (etm);

    if (root)
        e_tree_model_node_traverse(etm, root, free_ids_cb, NULL);
}

static void build_flat_diff(MessageList *ml, CamelFolderChangeInfo *changes);

static void
build_flat (MessageList *ml, GPtrArray *uids, CamelFolderChangeInfo *changes)
{
    ETreeModel *tree = E_TREE_MODEL (ml->table_model);
    ETreePath *node;
    char *uid;
    int i;

#ifdef TIMEIT
    struct timeval start, end;
    unsigned long diff;

    printf("Building flat\n");
    gettimeofday(&start, NULL);
#endif

    if (changes) {
        build_flat_diff(ml, changes);
    } else {
        e_table_model_pre_change(ml->table_model);
        clear_tree (ml);
        for (i = 0; i < uids->len; i++) {
            uid = new_id_from_uid(uids->pdata[i]);
            node = e_tree_model_node_insert (tree, ml->tree_root, -1, uid);
            g_hash_table_insert (ml->uid_rowmap, id_uid(uid), GINT_TO_POINTER (i));
        }
        e_table_model_changed(ml->table_model);
    }

#ifdef TIMEIT
    gettimeofday(&end, NULL);
    diff = end.tv_sec * 1000 + end.tv_usec/1000;
    diff -= start.tv_sec * 1000 + start.tv_usec/1000;
    printf("Building flat took %ld.%03ld seconds\n", diff / 1000, diff % 1000);
#endif

}

/* used to sort the rows to match list order */
struct _uidsort {
    int row;
    char *uid;
};

static int
sort_uid_cmp(const void *ap, const void *bp)
{
    const struct _uidsort *a = (struct _uidsort *)ap;
    const struct _uidsort *b = (struct _uidsort *)bp;

    if (a->row < b->row)
        return -1;
    else if (a->row > b->row)
        return 1;
    return 0;
}

static void
sort_uid_to_rows(MessageList *ml, GPtrArray *uids)
{
    struct _uidsort *uidlist;
    int i;

    uidlist = g_malloc(sizeof(struct _uidsort) * uids->len);
    for (i=0;i<uids->len;i++) {
        uidlist[i].row = (int)g_hash_table_lookup(ml->uid_rowmap, uids->pdata[i]);
        uidlist[i].uid = uids->pdata[i];
    }
    qsort(uidlist, uids->len, sizeof(struct _uidsort), sort_uid_cmp);
    for (i=0;i<uids->len;i++) {
        uids->pdata[i] = uidlist[i].uid;
    }
    g_free(uidlist);
}

static void
build_flat_diff(MessageList *ml, CamelFolderChangeInfo *changes)
{
    int row, i;
    ETreePath *node;
    char *uid;
    int oldrow;
    char *olduid;

#ifdef TIMEIT
    struct timeval start, end;
    unsigned long diff;
        
    gettimeofday(&start, NULL);
#endif

    printf("updating changes to display\n");

    /* remove individual nodes? */
    if (changes->uid_removed->len > 0) {
        /* first, we need to sort the row id's to match the summary order */
        sort_uid_to_rows(ml, changes->uid_removed);

        /* we remove from the end, so that the rowmap remains valid as we go */
        d(printf("Removing messages from view:\n"));
        for (i=changes->uid_removed->len-1;i>=0;i--) {
            d(printf(" %s\n", (char *)changes->uid_removed->pdata[i]));
            if (g_hash_table_lookup_extended(ml->uid_rowmap, changes->uid_removed->pdata[i], (void *)&olduid, (void *)&row)) {
                node = e_tree_model_node_at_row((ETreeModel *)ml->table_model, row);
                uid = e_tree_model_node_get_data((ETreeModel *)ml->table_model, node);
                if (uid && id_is_uid(uid) && !strcmp(id_uid(uid), changes->uid_removed->pdata[i])) {
                    g_hash_table_remove(ml->uid_rowmap, olduid);
                    e_tree_model_node_remove((ETreeModel *)ml->table_model, node);
                    g_free(uid);
                    d(printf("  - removed\n"));
                } else {
                    d(printf("  - is this the right uid, it doesn't match my map?\n"));
                }
            }
        }
    }

    /* add new nodes? - just append to the end */
    if (changes->uid_added->len > 0) {
        node = e_tree_model_node_get_last_child((ETreeModel *)ml->table_model, ml->tree_root);
        row = e_tree_model_row_of_node((ETreeModel *)ml->table_model, node) + 1;
        d(printf("Adding messages to view:\n"));
        for (i=0;i<changes->uid_added->len;i++) {
            d(printf(" %s\n", (char *)changes->uid_added->pdata[i]));
            uid = new_id_from_uid(changes->uid_added->pdata[i]);
            node = e_tree_model_node_insert((ETreeModel *)ml->table_model, ml->tree_root, row, uid);
            g_hash_table_insert(ml->uid_rowmap, id_uid(uid), GINT_TO_POINTER (row));
            row++;
        }
    }

    /* now, check the rowmap, some rows might've changed (with removes) */
    if (changes->uid_removed->len) {
        d(printf("checking uid mappings\n"));
        row = 0;
        node = e_tree_model_node_get_first_child ((ETreeModel *)ml->table_model, ml->tree_root);
        while (node) {
            uid = e_tree_model_node_get_data((ETreeModel *)ml->table_model, node);
            if (id_is_uid(uid)) {
                if (g_hash_table_lookup_extended(ml->uid_rowmap, id_uid(uid), (void *)&olduid, (void *)&oldrow)) {
                    if (oldrow != row) {
                        d(printf("row %d moved to new row %d\n", oldrow, row));
                        g_hash_table_insert(ml->uid_rowmap, olduid, (void *)row);
                    }
                } else { /* missing?  shouldn't happen */
                    g_warning("Uid vanished from rowmap?: %s\n", uid);
                }
            }
            row++;
            node = e_tree_model_node_get_next((ETreeModel *)ml->table_model, node);
        }
    }

#ifdef TIMEIT
    gettimeofday(&end, NULL);
    diff = end.tv_sec * 1000 + end.tv_usec/1000;
    diff -= start.tv_sec * 1000 + start.tv_usec/1000;
    printf("Inserting changes took %ld.%03ld seconds\n", diff / 1000, diff % 1000);
#endif

}

static void
main_folder_changed (CamelObject *o, gpointer event_data, gpointer user_data)
{
    MessageList *ml = MESSAGE_LIST (user_data);
    CamelFolderChangeInfo *changes = (CamelFolderChangeInfo *)event_data;

    printf("folder changed event, changes = %p\n", changes);

    mail_do_regenerate_messagelist(ml, ml->search, changes);
}

static void
folder_changed (CamelObject *o, gpointer event_data, gpointer user_data)
{
    /* similarly to message_changed, copy the change list and propagate it to
       the main thread and free it */
    CamelFolderChangeInfo *changes;

    if (event_data) {
        changes = camel_folder_change_info_new();
        camel_folder_change_info_cat(changes, (CamelFolderChangeInfo *)event_data);
    } else {
        changes = NULL;
    }
    mail_op_forward_event (main_folder_changed, o, changes, user_data);
}

static void
main_message_changed (CamelObject *o, gpointer uid, gpointer user_data)
{
    MessageList *message_list = MESSAGE_LIST (user_data);
    int row;

    row = GPOINTER_TO_INT (g_hash_table_lookup (message_list->uid_rowmap,
                            uid));
    if (row != -1)
        e_table_model_row_changed (message_list->table_model, row);

    g_free (uid);
}

static void
message_changed (CamelObject *o, gpointer event_data, gpointer user_data)
{
    /* Here we copy the data because our thread may free the copy that we would reference.
     * The other thread would be passed a uid parameter that pointed to freed data.
     * We copy it and free it in the handler. 
     */
    mail_op_forward_event (main_message_changed, o, g_strdup ((gchar *)event_data), user_data);
}

void
message_list_set_folder (MessageList *message_list, CamelFolder *camel_folder)
{
    CamelException ex;

    g_return_if_fail (message_list != NULL);
    g_return_if_fail (camel_folder != NULL);
    g_return_if_fail (IS_MESSAGE_LIST (message_list));
    g_return_if_fail (CAMEL_IS_FOLDER (camel_folder));
    g_return_if_fail (camel_folder_has_summary_capability (camel_folder));

    if (message_list->folder == camel_folder)
        return;

    camel_exception_init (&ex);

    if (message_list->folder)
        camel_object_unref (CAMEL_OBJECT (message_list->folder));

    message_list->folder = camel_folder;

    /* build the etable suitable for this folder */
    message_list_setup_etable(message_list);

    camel_object_hook_event(CAMEL_OBJECT (camel_folder), "folder_changed",
               folder_changed, message_list);
    camel_object_hook_event(CAMEL_OBJECT (camel_folder), "message_changed",
               message_changed, message_list);

    camel_object_ref (CAMEL_OBJECT (camel_folder));

    clear_tree(message_list);
    mail_do_regenerate_messagelist (message_list, message_list->search, NULL);
}

GtkWidget *
message_list_get_widget (MessageList *message_list)
{
    return message_list->etable;
}

E_MAKE_TYPE (message_list, "MessageList", MessageList, message_list_class_init, message_list_init, PARENT_TYPE);

static gboolean
on_cursor_change_idle (gpointer data)
{
    MessageList *message_list = data;

    printf("emitting cursor changed signal, for uid %s\n", message_list->cursor_uid);
    gtk_signal_emit(GTK_OBJECT (message_list), message_list_signals [MESSAGE_SELECTED], message_list->cursor_uid);

    message_list->idle_id = 0;
    return FALSE;
}

static void
on_cursor_change_cmd (ETableScrolled *table, int row, gpointer user_data)
{
    MessageList *message_list;
    
    message_list = MESSAGE_LIST (user_data);
    
    message_list->cursor_row = row;
    message_list->cursor_uid = get_message_uid (message_list, row);

    if (!message_list->idle_id) {
        message_list->idle_id =
            g_idle_add_full (G_PRIORITY_LOW, on_cursor_change_idle,
                     message_list, NULL);
    }
}

static gint
on_click (ETableScrolled *table, gint row, gint col, GdkEvent *event, MessageList *list)
{
    int flag;
    const CamelMessageInfo *info;

    if (col == COL_MESSAGE_STATUS)
        flag = CAMEL_MESSAGE_SEEN;
    else if (col == COL_FLAGGED)
        flag = CAMEL_MESSAGE_FLAGGED;
    else
        return FALSE;

    mail_tool_camel_lock_up();

    info = get_message_info(list, row);
    if (info == NULL) {
        mail_tool_camel_lock_down();
        return FALSE;
    }
    
    camel_folder_set_message_flags(list->folder, info->uid, flag, ~info->flags);

    mail_tool_camel_lock_down();

    if (flag == CAMEL_MESSAGE_SEEN && list->seen_id) {
        gtk_timeout_remove (list->seen_id);
        list->seen_id = 0;
    }

    return TRUE;
}

struct message_list_foreach_data {
    MessageList *message_list;
    MessageListForeachFunc callback;
    gpointer user_data;
};

static void
mlfe_callback (int row, gpointer user_data)
{
    struct message_list_foreach_data *mlfe_data = user_data;
    const char *uid;

    uid = get_message_uid (mlfe_data->message_list, row);
    if (uid) {
        mlfe_data->callback (mlfe_data->message_list,
                     uid,
                     mlfe_data->user_data);
    }
}

void
message_list_foreach (MessageList *message_list,
              MessageListForeachFunc callback,
              gpointer user_data)
{
    struct message_list_foreach_data mlfe_data;

    mlfe_data.message_list = message_list;
    mlfe_data.callback = callback;
    mlfe_data.user_data = user_data;
    e_table_scrolled_selected_row_foreach (E_TABLE_SCROLLED (message_list->etable),
                           mlfe_callback, &mlfe_data);
}

/* set whether we are in threaded view or flat view */
void
message_list_set_threaded(MessageList *ml, gboolean threaded)
{
    if (ml->threaded ^ threaded) {
        ml->threaded = threaded;

        clear_tree(ml);
        mail_do_regenerate_messagelist(ml, ml->search, NULL);
    }
}

void
message_list_set_search(MessageList *ml, const char *search)
{
    if (search == NULL || search[0] == '\0')
        if (ml->search == NULL || ml->search[0]=='\0')
            return;

    if (search != NULL && ml->search !=NULL && strcmp(search, ml->search)==0)
        return;

    clear_tree(ml);
    mail_do_regenerate_messagelist(ml, search, NULL);
}

/* ** REGENERATE MESSAGELIST ********************************************** */

typedef struct regenerate_messagelist_input_s {
    MessageList *ml;
    CamelFolder *folder;
    char *search;
    CamelFolderChangeInfo *changes;
    gboolean dotree;    /* we are building a tree */
} regenerate_messagelist_input_t;

typedef struct regenerate_messagelist_data_s {
    GPtrArray *uids;
    struct _thread_messages *tree;
    CamelFolderChangeInfo *changes;
} regenerate_messagelist_data_t;

static gchar *describe_regenerate_messagelist (gpointer in_data, gboolean gerund);
static void setup_regenerate_messagelist   (gpointer in_data, gpointer op_data, CamelException *ex);
static void do_regenerate_messagelist      (gpointer in_data, gpointer op_data, CamelException *ex);
static void cleanup_regenerate_messagelist (gpointer in_data, gpointer op_data, CamelException *ex);

static gchar *describe_regenerate_messagelist (gpointer in_data, gboolean gerund)
{
    if (gerund)
        return g_strdup (_("Rebuilding message view"));
    else
        return g_strdup (_("Rebuild message view"));
}

static void setup_regenerate_messagelist (gpointer in_data, gpointer op_data, CamelException *ex)
{
    regenerate_messagelist_input_t *input = (regenerate_messagelist_input_t *) in_data;

    if (!IS_MESSAGE_LIST (input->ml)) {
        camel_exception_set (ex, CAMEL_EXCEPTION_INVALID_PARAM,
                     "No messagelist specified to regenerate");
        return;
    }

    gtk_object_ref (GTK_OBJECT (input->ml));
}

static void do_regenerate_messagelist (gpointer in_data, gpointer op_data, CamelException *ex)
{
    regenerate_messagelist_input_t *input = (regenerate_messagelist_input_t *) in_data;
    regenerate_messagelist_data_t *data = (regenerate_messagelist_data_t *) op_data;

    mail_tool_camel_lock_up();

    if (input->search) {
        data->uids = camel_folder_search_by_expression(input->ml->folder, input->search, ex);
    } else {
        data->uids = camel_folder_get_uids (input->ml->folder);
    }

    if (camel_exception_is_set (ex)) {
        mail_tool_camel_lock_down();
        return;
    }

    if (input->dotree && data->uids)
        data->tree = thread_messages(input->ml->folder, data->uids);
    else
        data->tree = NULL;

    mail_tool_camel_lock_down();
}

static void cleanup_regenerate_messagelist (gpointer in_data, gpointer op_data, CamelException *ex)
{
    regenerate_messagelist_input_t *input = (regenerate_messagelist_input_t *) in_data;
    regenerate_messagelist_data_t *data = (regenerate_messagelist_data_t *) op_data;

    ETreeModel *etm;

    etm = E_TREE_MODEL (input->ml->table_model);

    if (data->uids == NULL) { /*exception*/
        gtk_object_unref (GTK_OBJECT (input->ml));
        return;
    }

    if (input->dotree)
        build_tree(input->ml, data->tree);
    else
        build_flat(input->ml, data->uids, input->changes);

    if (input->search)
        camel_folder_search_free (input->ml->folder, data->uids);
    else
        camel_folder_free_uids (input->ml->folder, data->uids);

    /* update what we have as our search string */
        if (input->ml->search && input->ml->search != input->search)
                g_free(input->ml->search);
    input->ml->search = input->search;

    if (data->tree)
        thread_messages_free(data->tree);

    if (input->changes)
        camel_folder_change_info_free(input->changes);

    gtk_object_unref (GTK_OBJECT (input->ml));
}

static const mail_operation_spec op_regenerate_messagelist =
{
    describe_regenerate_messagelist,
    sizeof (regenerate_messagelist_data_t),
    setup_regenerate_messagelist,
    do_regenerate_messagelist,
    cleanup_regenerate_messagelist
};

/* if changes == NULL, then update the whole list, otherwise just update the changes */
static void
mail_do_regenerate_messagelist (MessageList *list, const gchar *search, CamelFolderChangeInfo *changes)
{
    regenerate_messagelist_input_t *input;

    /* This gets called on empty folder-browsers by the bonobo ui
     * callback for threaded view.
     */
    if (!list->folder)
        return;

    /* see if we need to goto the child thread at all anyway */
    /* currently the only case is the flat view with updates and no search */
    if (search == NULL && changes != NULL && !list->threaded) {
        build_flat_diff(list, changes);
        camel_folder_change_info_free(changes);
        return;
    }

    input = g_new (regenerate_messagelist_input_t, 1);
    input->ml = list;
    input->search = g_strdup (search);
    input->changes = changes;
    input->dotree = list->threaded;

    mail_operation_queue (&op_regenerate_messagelist, input, TRUE);
}