aboutsummaryrefslogtreecommitdiffstats
path: root/mail/em-folder-tree.c
blob: b2d95cd71b6783f62b67c05f51939447679f4564 (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
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>  
 *
 *
 * Authors:
 *      Jeffrey Stedfast <fejj@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

#include <libxml/tree.h>

#include <glib/gi18n.h>
#include <gdk/gdkkeysyms.h>
#include <glib/gi18n.h>

#include <camel/camel-session.h>
#include <camel/camel-store.h>
#include <camel/camel-folder.h>
#include <camel/camel-vee-store.h>
#include <camel/camel-vtrash-folder.h>
#include <camel/camel-stream-mem.h>
#include <camel/camel-file-utils.h>
#include <camel/camel-stream-fs.h>

#include "e-util/e-mktemp.h"
#include "e-util/e-request.h"
#include "e-util/e-icon-factory.h"
#include "e-util/e-error.h"
#include "e-util/e-util.h"

#include "em-vfolder-rule.h"

#include "mail-mt.h"
#include "mail-ops.h"
#include "mail-tools.h"
#include "mail-config.h"
#include "mail-send-recv.h"
#include "mail-vfolder.h"

#include "em-utils.h"
#include "em-popup.h"
#include "em-folder-tree.h"
#include "em-folder-utils.h"
#include "em-folder-selector.h"
#include "em-folder-selection.h"
#include "em-folder-properties.h"
#include "em-event.h"

#include "e-mail-shell-module.h"

#define d(x)

#define EM_FOLDER_TREE_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), EM_TYPE_FOLDER_TREE, EMFolderTreePrivate))

struct _selected_uri {
    char *key;      /* store:path or account/path */
    char *uri;
    CamelStore *store;
    char *path;
};

struct _EMFolderTreePrivate {
    EMFolderTreeModel *model;

    GSList *select_uris;    /* selected_uri structures of each path pending selection. */
    GHashTable *select_uris_table; /*Removed as they're encountered, so use this to find uri's not presnet but selected */

    guint32 excluded;
    gboolean (*excluded_func)(EMFolderTree *emft, GtkTreeModel *model, GtkTreeIter *iter, void *data);
    void *excluded_data;

    guint do_multiselect:1; /* multiple select mode */
    guint cursor_set:1; /* set to TRUE means we or something
                 * else has set the cursor, otherwise
                 * we need to set it when we set the
                 * selection */

    guint save_state_id;

    guint autoscroll_id;
    guint autoexpand_id;
    GtkTreeRowReference *autoexpand_row;

    guint loading_row_id;
    guint loaded_row_id;

    GtkTreeRowReference *drag_row;
};

enum {
    FOLDER_ACTIVATED,  /* aka double-clicked or user hit enter */
    FOLDER_SELECTED,
    POPUP_EVENT,
    LAST_SIGNAL
};

/* Drag & Drop types */
enum DndDragType {
    DND_DRAG_TYPE_FOLDER,          /* drag an evo folder */
    DND_DRAG_TYPE_TEXT_URI_LIST,   /* drag to an mbox file */
    NUM_DRAG_TYPES
};

enum DndDropType {
    DND_DROP_TYPE_UID_LIST,        /* drop a list of message uids */
    DND_DROP_TYPE_FOLDER,          /* drop an evo folder */
    DND_DROP_TYPE_MESSAGE_RFC822,  /* drop a message/rfc822 stream */
    DND_DROP_TYPE_TEXT_URI_LIST,   /* drop an mbox file */
    NUM_DROP_TYPES
};

static GtkTargetEntry drag_types[] = {
    { "x-folder",         0, DND_DRAG_TYPE_FOLDER         },
    { "text/uri-list",    0, DND_DRAG_TYPE_TEXT_URI_LIST  },
};

static GtkTargetEntry drop_types[] = {
    { "x-uid-list" ,      0, DND_DROP_TYPE_UID_LIST       },
    { "x-folder",         0, DND_DROP_TYPE_FOLDER         },
    { "message/rfc822",   0, DND_DROP_TYPE_MESSAGE_RFC822 },
    { "text/uri-list",    0, DND_DROP_TYPE_TEXT_URI_LIST  },
};

static GdkAtom drag_atoms[NUM_DRAG_TYPES];
static GdkAtom drop_atoms[NUM_DROP_TYPES];

static guint signals[LAST_SIGNAL] = { 0 };

extern CamelSession *session;

static gboolean emft_save_state (EMFolderTree *emft);
static void emft_queue_save_state (EMFolderTree *emft);

static void emft_update_model_expanded_state (struct _EMFolderTreePrivate *priv, GtkTreeIter *iter, gboolean expanded);

static void emft_tree_row_activated (GtkTreeView *treeview, GtkTreePath *path, GtkTreeViewColumn *column, EMFolderTree *emft);
static gboolean emft_tree_test_collapse_row (GtkTreeView *treeview, GtkTreeIter *root, GtkTreePath *path, EMFolderTree *emft);
static void emft_tree_row_expanded (GtkTreeView *treeview, GtkTreeIter *root, GtkTreePath *path, EMFolderTree *emft);
static gboolean emft_tree_button_press (GtkTreeView *treeview, GdkEventButton *event, EMFolderTree *emft);
static void emft_tree_selection_changed (GtkTreeSelection *selection, EMFolderTree *emft);
static gboolean emft_tree_user_event (GtkTreeView *treeview, GdkEvent *e, EMFolderTree *emft);
static gboolean emft_popup_menu (GtkWidget *widget);

struct _emft_selection_data {
    GtkTreeModel *model;
    GtkTreeIter *iter;
    gboolean set;
};

static gpointer parent_class = NULL;

static void
folder_tree_emit_popup_event (EMFolderTree *emft,
                              GdkEvent *event)
{
    g_signal_emit (emft, signals[POPUP_EVENT], 0, event);
}

static void
emft_free_select_uri (struct _selected_uri *u)
{
    g_free (u->uri);
    if (u->store)
        camel_object_unref (u->store);
    g_free (u->key);
    g_free (u->path);
    g_free (u);
}

static void
folder_tree_finalize (GObject *object)
{
    EMFolderTreePrivate *priv;

    priv = EM_FOLDER_TREE_GET_PRIVATE (object);

    if (priv->select_uris != NULL) {
        g_slist_foreach (
            priv->select_uris,
            (GFunc) emft_free_select_uri, NULL);
        g_slist_free (priv->select_uris);
        g_hash_table_destroy (priv->select_uris_table);
        priv->select_uris = NULL;
    }

    /* Chain up to parent's finalize() method. */
    G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
em_folder_tree_destroy (GtkObject *object)
{
    EMFolderTreePrivate *priv;

    priv = EM_FOLDER_TREE_GET_PRIVATE (object);

    if (priv->loaded_row_id != 0) {
        g_signal_handler_disconnect (priv->model, priv->loaded_row_id);
        priv->loaded_row_id = 0;
    }

    if (priv->save_state_id != 0) {
        g_source_remove (priv->save_state_id);
        emft_save_state (EM_FOLDER_TREE (object));
    }

    if (priv->autoscroll_id != 0) {
        g_source_remove (priv->autoscroll_id);
        priv->autoscroll_id = 0;
    }

    if (priv->autoexpand_id != 0) {
        gtk_tree_row_reference_free (priv->autoexpand_row);
        priv->autoexpand_row = NULL;

        g_source_remove (priv->autoexpand_id);
        priv->autoexpand_id = 0;
    }

    priv->model = NULL;

    /* Chain up to parent's destroy() method. */
    GTK_OBJECT_CLASS (parent_class)->destroy (object);
}

static void
folder_tree_class_init (EMFolderTreeClass *class)
{
    GObjectClass *object_class;
    GtkObjectClass *gtk_object_class;
    GtkWidgetClass *widget_class;

    parent_class = g_type_class_peek_parent (class);
    g_type_class_add_private (class, sizeof (EMFolderTreePrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->finalize = folder_tree_finalize;

    gtk_object_class = GTK_OBJECT_CLASS (class);
    gtk_object_class->destroy = em_folder_tree_destroy;

    widget_class = GTK_WIDGET_CLASS (class);
    widget_class->popup_menu = emft_popup_menu;

    signals[FOLDER_SELECTED] = g_signal_new (
        "folder-selected",
        G_OBJECT_CLASS_TYPE (object_class),
        G_SIGNAL_RUN_FIRST,
        G_STRUCT_OFFSET (EMFolderTreeClass, folder_selected),
        NULL, NULL,
        e_marshal_VOID__STRING_STRING_UINT,
        G_TYPE_NONE, 3,
        G_TYPE_STRING,
        G_TYPE_STRING,
        G_TYPE_UINT);

    signals[FOLDER_ACTIVATED] = g_signal_new (
        "folder-activated",
        G_OBJECT_CLASS_TYPE (object_class),
        G_SIGNAL_RUN_FIRST,
        G_STRUCT_OFFSET (EMFolderTreeClass, folder_activated),
        NULL, NULL,
        e_marshal_VOID__STRING_STRING,
        G_TYPE_NONE, 2,
        G_TYPE_STRING,
        G_TYPE_STRING);

    signals[POPUP_EVENT] = g_signal_new (
        "popup-event",
        G_OBJECT_CLASS_TYPE (object_class),
        G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
        G_STRUCT_OFFSET (EMFolderTreeClass, popup_event),
        NULL, NULL,
        g_cclosure_marshal_VOID__BOXED,
        G_TYPE_NONE, 1,
        GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
}

static void
folder_tree_init (EMFolderTree *emft)
{
    GHashTable *select_uris_table;

    select_uris_table = g_hash_table_new (g_str_hash, g_str_equal);

    emft->priv = EM_FOLDER_TREE_GET_PRIVATE (emft);
    emft->priv->select_uris_table = select_uris_table;
}

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

    if (G_UNLIKELY (type == 0)) {
        static const GTypeInfo type_info = {
            sizeof (EMFolderTreeClass),
            (GBaseInitFunc) NULL,
            (GBaseFinalizeFunc) NULL,
            (GClassInitFunc) folder_tree_class_init,
            (GClassFinalizeFunc) NULL,
            NULL,  /* class_data */
            sizeof (EMFolderTree),
            0,     /* n_preallocs */
            (GInstanceInitFunc) folder_tree_init,
            NULL   /* value_table */
        };

        type = g_type_register_static (
            GTK_TYPE_TREE_VIEW, "EMFolderTree", &type_info, 0);
    }

    return type;
}

static gboolean
subdirs_contain_unread (GtkTreeModel *model, GtkTreeIter *root)
{
    unsigned int unread;
    GtkTreeIter iter;

    if (!gtk_tree_model_iter_children (model, &iter, root))
        return FALSE;

    do {
        gtk_tree_model_get (model, &iter, COL_UINT_UNREAD, &unread, -1);
        if (unread)
            return TRUE;

        if (gtk_tree_model_iter_has_child (model, &iter))
            if (subdirs_contain_unread (model, &iter))
                return TRUE;
    } while (gtk_tree_model_iter_next (model, &iter));

    return FALSE;
}


enum {
    FOLDER_ICON_NORMAL,
    FOLDER_ICON_INBOX,
    FOLDER_ICON_OUTBOX,
    FOLDER_ICON_TRASH,
    FOLDER_ICON_JUNK,
    FOLDER_ICON_SHARED_TO_ME,
    FOLDER_ICON_SHARED_BY_ME,
    FOLDER_ICON_SENT,
    FOLDER_ICON_VIRTUAL,
    FOLDER_ICON_LAST
};

static GdkPixbuf *folder_icons[FOLDER_ICON_LAST];

static void
render_pixbuf (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
           GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data)
{
    static gboolean initialised = FALSE;
    GdkPixbuf *pixbuf = NULL;
    gboolean is_store;
    guint32 flags;
    EMEventTargetCustomIcon *target;
    const char *folder_name;

    if (!initialised) {
        folder_icons[FOLDER_ICON_NORMAL] = e_icon_factory_get_icon ("folder", E_ICON_SIZE_MENU);
        folder_icons[FOLDER_ICON_INBOX] = e_icon_factory_get_icon ("mail-inbox", E_ICON_SIZE_MENU);
        folder_icons[FOLDER_ICON_OUTBOX] = e_icon_factory_get_icon ("mail-outbox", E_ICON_SIZE_MENU);
        folder_icons[FOLDER_ICON_TRASH] = e_icon_factory_get_icon ("user-trash", E_ICON_SIZE_MENU);
        folder_icons[FOLDER_ICON_JUNK] = e_icon_factory_get_icon ("mail-mark-junk", E_ICON_SIZE_MENU);
        folder_icons[FOLDER_ICON_SHARED_TO_ME] = e_icon_factory_get_icon ("stock_shared-to-me", E_ICON_SIZE_MENU);
        folder_icons[FOLDER_ICON_SHARED_BY_ME] = e_icon_factory_get_icon ("stock_shared-by-me", E_ICON_SIZE_MENU);
        folder_icons[FOLDER_ICON_SENT] = e_icon_factory_get_icon ("mail-sent", E_ICON_SIZE_MENU);
        folder_icons[FOLDER_ICON_VIRTUAL] = e_icon_factory_get_icon ("folder-saved-search", E_ICON_SIZE_MENU);

        initialised = TRUE;
    }

    gtk_tree_model_get (model, iter, COL_BOOL_IS_STORE, &is_store, COL_UINT_FLAGS, &flags, -1);

    if (!is_store) {
        switch((flags & CAMEL_FOLDER_TYPE_MASK)) {
        case CAMEL_FOLDER_TYPE_INBOX:
            pixbuf = folder_icons[FOLDER_ICON_INBOX];
            break;
        case CAMEL_FOLDER_TYPE_OUTBOX:
            pixbuf = folder_icons[FOLDER_ICON_OUTBOX];
            break;
        case CAMEL_FOLDER_TYPE_TRASH:
            pixbuf = folder_icons[FOLDER_ICON_TRASH];
            break;
        case CAMEL_FOLDER_TYPE_JUNK:
            pixbuf = folder_icons[FOLDER_ICON_JUNK];
            break;
        case CAMEL_FOLDER_TYPE_SENT:
            pixbuf = folder_icons[FOLDER_ICON_SENT];
            break;
        default:
            if (flags & CAMEL_FOLDER_SHARED_TO_ME)
                pixbuf = folder_icons[FOLDER_ICON_SHARED_TO_ME];
            else if (flags & CAMEL_FOLDER_SHARED_BY_ME)
                pixbuf = folder_icons[FOLDER_ICON_SHARED_BY_ME];
            else if (flags & CAMEL_FOLDER_VIRTUAL)
                pixbuf = folder_icons[FOLDER_ICON_VIRTUAL];
            else {
                pixbuf = folder_icons[FOLDER_ICON_NORMAL];
                g_object_set (renderer, "pixbuf", pixbuf, "visible", !is_store, NULL);
                gtk_tree_model_get (model, iter, COL_STRING_FULL_NAME, &folder_name, -1);
                target = em_event_target_new_custom_icon (em_event_peek(), renderer, folder_name, EM_EVENT_CUSTOM_ICON);
                    e_event_emit ((EEvent *)em_event_peek (), "folder.customicon", (EEventTarget *) target);
                return;
            }
        }
    }

    g_object_set (renderer, "pixbuf", pixbuf, "visible", !is_store, NULL);
}

static void
render_display_name (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
             GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data)
{
    gboolean is_store, bold;
    unsigned int unread;
    char *display;
    char *name;

    gtk_tree_model_get (model, iter, COL_STRING_DISPLAY_NAME, &name,
                COL_BOOL_IS_STORE, &is_store,
                COL_UINT_UNREAD, &unread, -1);

    if (!(bold = is_store || unread)) {
        if (gtk_tree_model_iter_has_child (model, iter))
            bold = subdirs_contain_unread (model, iter);
    }

    if (!is_store && unread) {
        /* Translators: This is the string used for displaying the
         * folder names in folder trees. "%s" will be replaced by
         * the folder's name and "%u" will be replaced with the
         * number of unread messages in the folder.
         *
         * Most languages should translate this as "%s (%u)". The
         * languages that use localized digits (like Persian) may
         * need to replace "%u" with "%Iu". Right-to-left languages
         * (like Arabic and Hebrew) may need to add bidirectional
         * formatting codes to take care of the cases the folder
         * name appears in either direction.
         *
         * Do not translate the "folder-display|" part. Remove it
         * from your translation.
         */
        display = g_strdup_printf (C_("folder-display", "%s (%u)"), name, unread);
        g_free (name);
    } else
        display = name;

    g_object_set (renderer, "text", display,
              "weight", bold ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL,
              NULL);

    g_free (display);
}

static gboolean
emft_select_func(GtkTreeSelection *selection, GtkTreeModel *model, GtkTreePath *path, gboolean selected, gpointer data)
{
    EMFolderTree *emft = data;
    gboolean is_store;
    guint32 flags;
    GtkTreeIter iter;

    /* NB: This will be called with selection==NULL from tree_row_activated */
    if (emft->priv->excluded == 0 && emft->priv->excluded_func == NULL)
        return TRUE;

    if (!gtk_tree_model_get_iter(model, &iter, path))
        return TRUE;

    if (emft->priv->excluded_func != NULL)
        return emft->priv->excluded_func(emft, model, &iter, emft->priv->excluded_data);

    gtk_tree_model_get(model, &iter, COL_UINT_FLAGS, &flags, COL_BOOL_IS_STORE, &is_store, -1);
    if (is_store)
        flags |= CAMEL_FOLDER_NOSELECT;

    return (flags & emft->priv->excluded) == 0;
}

static GtkTreeView *
folder_tree_new (EMFolderTree *emft, EMFolderTreeModel *model)
{
    GtkTreeSelection *selection;
    GtkTreeViewColumn *column;
    GtkCellRenderer *renderer;
    GtkWidget *tree;
    gboolean side_bar_search;
    GConfClient *gconf;

    gconf = mail_config_get_gconf_client ();
    side_bar_search = gconf_client_get_bool (gconf, "/apps/evolution/mail/display/side_bar_search", NULL);

    /* FIXME[KILL-BONOBO] Gross hack */
    tree = GTK_WIDGET (emft);
    gtk_tree_view_set_model (
        GTK_TREE_VIEW (tree), GTK_TREE_MODEL (model));
    GTK_WIDGET_SET_FLAGS(tree, GTK_CAN_FOCUS);

    column = gtk_tree_view_column_new ();
    gtk_tree_view_append_column ((GtkTreeView *) tree, column);

    renderer = gtk_cell_renderer_pixbuf_new ();
    gtk_tree_view_column_pack_start (column, renderer, FALSE);
    gtk_tree_view_column_set_cell_data_func (column, renderer, render_pixbuf, NULL, NULL);

    renderer = gtk_cell_renderer_text_new ();
    if (!gconf_client_get_bool (gconf, "/apps/evolution/mail/display/no_folder_dots", NULL))
        g_object_set (G_OBJECT (renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
    gtk_tree_view_column_pack_start (column, renderer, TRUE);
    gtk_tree_view_column_set_cell_data_func (column, renderer, render_display_name, NULL, NULL);

    selection = gtk_tree_view_get_selection ((GtkTreeView *) tree);
    gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
    gtk_tree_selection_set_select_function(selection, emft_select_func, emft, NULL);
    gtk_tree_view_set_headers_visible ((GtkTreeView *) tree, FALSE);

    gtk_tree_view_set_search_column((GtkTreeView *)tree, COL_STRING_DISPLAY_NAME);
    gtk_tree_view_set_enable_search((GtkTreeView *)tree, side_bar_search);

    return (GtkTreeView *) tree;
}

static void
em_folder_tree_construct (EMFolderTree *emft, EMFolderTreeModel *model)
{
    struct _EMFolderTreePrivate *priv = emft->priv;
    GtkTreeSelection *selection;

    priv->model = model;
    folder_tree_new (emft, model);
    gtk_widget_show (GTK_WIDGET (emft));

    g_signal_connect (emft, "row-expanded", G_CALLBACK (emft_tree_row_expanded), emft);
    g_signal_connect (emft, "test-collapse-row", G_CALLBACK (emft_tree_test_collapse_row), emft);
    g_signal_connect (emft, "row-activated", G_CALLBACK (emft_tree_row_activated), emft);
    g_signal_connect (emft, "button-press-event", G_CALLBACK (emft_tree_button_press), emft);
    g_signal_connect (emft, "key-press-event", G_CALLBACK (emft_tree_user_event), emft);

    selection = gtk_tree_view_get_selection ((GtkTreeView *) emft);
    g_signal_connect (selection, "changed", G_CALLBACK (emft_tree_selection_changed), emft);
}

GtkWidget *
em_folder_tree_new (EShellModule *shell_module)
{
    EMFolderTreeModel *model;
    EMFolderTree *emft;

    g_return_val_if_fail (E_IS_SHELL_MODULE (shell_module), NULL);

    model = e_mail_shell_module_get_folder_tree_model (shell_module);
    emft = (EMFolderTree *) em_folder_tree_new_with_model (model);
    g_object_unref (model);

    return (GtkWidget *) emft;
}

/* NOTE: Removes and frees the selected uri structure */
static void
emft_select_uri(EMFolderTree *emft, GtkTreePath *path, struct _selected_uri *u)
{
    struct _EMFolderTreePrivate *priv = emft->priv;
    GtkTreeView *tree_view;
    GtkTreeSelection *selection;

    tree_view = GTK_TREE_VIEW (emft);
    selection = gtk_tree_view_get_selection (tree_view);
    gtk_tree_selection_select_path(selection, path);
    if (!priv->cursor_set) {
        gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
        priv->cursor_set = TRUE;
    }
    gtk_tree_view_scroll_to_cell (tree_view, path, NULL, TRUE, 0.8f, 0.0f);
    g_hash_table_remove(priv->select_uris_table, u->key);
    priv->select_uris = g_slist_remove(priv->select_uris, u);
    emft_free_select_uri(u);
}

static void
emft_expand_node (EMFolderTreeModel *model, const char *key, EMFolderTree *emft)
{
    struct _EMFolderTreePrivate *priv = emft->priv;
    struct _EMFolderTreeModelStoreInfo *si;
    extern CamelStore *vfolder_store;
    EShellModule *shell_module;
    GtkTreeRowReference *row;
    GtkTreeView *tree_view;
    GtkTreePath *path;
    EAccount *account;
    CamelStore *store;
    const char *p;
    char *uid;
    size_t n;
    struct _selected_uri *u;

    if (!(p = strchr (key, '/')))
        n = strlen (key);
    else
        n = (p - key);

    uid = g_alloca (n + 1);
    memcpy (uid, key, n);
    uid[n] = '\0';

    tree_view = GTK_TREE_VIEW (emft);
    shell_module = em_folder_tree_model_get_shell_module (model);

    if ((account = mail_config_get_account_by_uid (uid)) && account->enabled) {
        CamelException ex;

        camel_exception_init (&ex);
        store = (CamelStore *) camel_session_get_service (session, account->source->url, CAMEL_PROVIDER_STORE, &ex);
        camel_exception_clear (&ex);

        if (store == NULL)
            return;
    } else if (!strcmp (uid, "vfolder")) {
        if (!(store = vfolder_store))
            return;

        camel_object_ref (store);
    } else if (!strcmp (uid, "local")) {
        if (!(store = e_mail_shell_module_get_local_store (shell_module)))
            return;

        camel_object_ref (store);
    } else {
        return;
    }

    if (!(si = g_hash_table_lookup (priv->model->store_hash, store))) {
        camel_object_unref (store);
        return;
    }

    camel_object_unref (store);

    if (p != NULL) {
        if (!(row = g_hash_table_lookup (si->full_hash, p + 1)))
            return;
    } else
        row = si->row;

    path = gtk_tree_row_reference_get_path (row);
    gtk_tree_view_expand_to_path (tree_view, path);

    u = g_hash_table_lookup(emft->priv->select_uris_table, key);
    if (u)
        emft_select_uri(emft, path, u);

    gtk_tree_path_free (path);
}

static void
emft_maybe_expand_row (EMFolderTreeModel *model, GtkTreePath *tree_path, GtkTreeIter *iter, EMFolderTree *emft)
{
    struct _EMFolderTreePrivate *priv = emft->priv;
    struct _EMFolderTreeModelStoreInfo *si;
    GtkTreeView *tree_view;
    gboolean is_store;
    CamelStore *store;
    EAccount *account;
    char *full_name;
    char *key;
    struct _selected_uri *u;
    gboolean is_expanded;

    tree_view = GTK_TREE_VIEW (emft);

    gtk_tree_model_get ((GtkTreeModel *) model, iter,
                COL_STRING_FULL_NAME, &full_name,
                COL_POINTER_CAMEL_STORE, &store,
                COL_BOOL_IS_STORE, &is_store,
                -1);

    si = g_hash_table_lookup (model->store_hash, store);
    if ((account = mail_config_get_account_by_name (si->display_name))) {
            key = g_strdup_printf ("%s/%s", account->uid, full_name ? full_name : "");
    } else if (CAMEL_IS_VEE_STORE (store)) {
        /* vfolder store */
        key = g_strdup_printf ("vfolder/%s", full_name ? full_name : "");
    } else {
        /* local store */
        key = g_strdup_printf ("local/%s", full_name ? full_name : "");
    }

    is_expanded = em_folder_tree_model_get_expanded (model, key);
    u = g_hash_table_lookup(priv->select_uris_table, key);
    if (is_expanded || u) {
        if (is_expanded) {
            gtk_tree_view_expand_to_path (tree_view, tree_path);
            gtk_tree_view_expand_row (tree_view, tree_path, FALSE);
        } else {
            char *c = strrchr (key, '/');

            *c = '\0';
            emft_expand_node (model, key, emft);
        }

        if (u)
            emft_select_uri(emft, tree_path, u);
    }

    g_free (full_name);
    g_free (key);
}

GtkWidget *
em_folder_tree_new_with_model (EMFolderTreeModel *model)
{
    EMFolderTree *emft;
    AtkObject *a11y;

    emft = g_object_new (EM_TYPE_FOLDER_TREE, NULL);
    em_folder_tree_construct (emft, model);
    g_object_ref (model);

    em_folder_tree_model_expand_foreach (model, (EMFTModelExpandFunc)emft_expand_node, emft);

    emft->priv->loading_row_id = g_signal_connect (model, "loading-row", G_CALLBACK (emft_maybe_expand_row), emft);
    emft->priv->loaded_row_id = g_signal_connect (model, "loaded-row", G_CALLBACK (emft_maybe_expand_row), emft);

    a11y = gtk_widget_get_accessible (GTK_WIDGET (emft));
    atk_object_set_name (a11y, _("Mail Folder Tree"));

    return (GtkWidget *) emft;
}

static void
tree_drag_begin (GtkWidget *widget, GdkDragContext *context, EMFolderTree *emft)
{
    struct _EMFolderTreePrivate *priv = emft->priv;
    GtkTreeSelection *selection;
    GtkTreeModel *model;
    GtkTreePath *path;
    GtkTreeIter iter;

    selection = gtk_tree_view_get_selection ((GtkTreeView *) widget);
    if (!gtk_tree_selection_get_selected (selection, &model, &iter))
        return;

    path = gtk_tree_model_get_path (model, &iter);
    priv->drag_row = gtk_tree_row_reference_new (model, path);
    gtk_tree_path_free (path);

    /* FIXME: set a drag icon? */
}

static void
tree_drag_data_delete(GtkWidget *widget, GdkDragContext *context, EMFolderTree *emft)
{
    struct _EMFolderTreePrivate *priv = emft->priv;
    char *full_name = NULL;
    GtkTreePath *src_path;
    gboolean is_store;
    CamelStore *store;
    CamelException ex;
    GtkTreeIter iter;

    if (!priv->drag_row || (src_path = gtk_tree_row_reference_get_path (priv->drag_row)))
        return;

    if (!gtk_tree_model_get_iter((GtkTreeModel *)priv->model, &iter, src_path))
        goto fail;

    gtk_tree_model_get((GtkTreeModel *)priv->model, &iter,
               COL_POINTER_CAMEL_STORE, &store,
               COL_STRING_FULL_NAME, &full_name,
               COL_BOOL_IS_STORE, &is_store, -1);

    if (is_store)
        goto fail;

    camel_exception_init(&ex);
    camel_store_delete_folder(store, full_name, &ex);
    if (camel_exception_is_set(&ex))
        camel_exception_clear(&ex);
fail:
    gtk_tree_path_free(src_path);
    g_free (full_name);
}

static void
tree_drag_data_get(GtkWidget *widget, GdkDragContext *context, GtkSelectionData *selection, guint info, guint time, EMFolderTree *emft)
{
    struct _EMFolderTreePrivate *priv = emft->priv;
    char *full_name = NULL, *uri = NULL;
    GtkTreePath *src_path;
    CamelFolder *folder;
    CamelStore *store;
    CamelException ex;
    GtkTreeIter iter;

    if (!priv->drag_row || !(src_path = gtk_tree_row_reference_get_path(priv->drag_row)))
        return;

    if (!gtk_tree_model_get_iter((GtkTreeModel *)priv->model, &iter, src_path))
        goto fail;

    gtk_tree_model_get((GtkTreeModel *)priv->model, &iter,
               COL_POINTER_CAMEL_STORE, &store,
               COL_STRING_FULL_NAME, &full_name,
               COL_STRING_URI, &uri, -1);

    /* make sure user isn't trying to drag on a placeholder row */
    if (full_name == NULL)
        goto fail;

    camel_exception_init(&ex);

    switch (info) {
    case DND_DRAG_TYPE_FOLDER:
        /* dragging to a new location in the folder tree */
        gtk_selection_data_set(selection, drag_atoms[info], 8, (unsigned char *)uri, strlen (uri) + 1);
        break;
    case DND_DRAG_TYPE_TEXT_URI_LIST:
        /* dragging to nautilus or something, probably */
        if ((folder = camel_store_get_folder(store, full_name, 0, &ex))) {
            GPtrArray *uids = camel_folder_get_uids(folder);

            em_utils_selection_set_urilist(selection, folder, uids);
            camel_folder_free_uids(folder, uids);
            camel_object_unref(folder);
        }
        break;
    default:
        abort();
    }

    if (camel_exception_is_set(&ex))
        camel_exception_clear(&ex);
fail:
    gtk_tree_path_free(src_path);
    g_free (full_name);
    g_free (uri);
}

/* TODO: Merge the drop handling code/menu's into one spot using a popup target for details */
/* Drop handling */
struct _DragDataReceivedAsync {
    MailMsg base;

    /* input data */
    GdkDragContext *context;

    /* Only selection->data and selection->length are valid */
    GtkSelectionData *selection;

    CamelStore *store;
    char *full_name;
    guint32 action;
    guint info;

    unsigned int move:1;
    unsigned int moved:1;
    unsigned int aborted:1;
};

static void
emft_drop_folder(struct _DragDataReceivedAsync *m)
{
    CamelFolder *src;

    d(printf(" * Drop folder '%s' onto '%s'\n", m->selection->data, m->full_name));

    if (!(src = mail_tool_uri_to_folder((char *)m->selection->data, 0, &m->base.ex)))
        return;

    em_folder_utils_copy_folders(src->parent_store, src->full_name, m->store, m->full_name?m->full_name:"", m->move);
    camel_object_unref(src);
}

static gchar *
emft_drop_async__desc (struct _DragDataReceivedAsync *m)
{
    CamelURL *url;
    char *buf;

    if (m->info == DND_DROP_TYPE_FOLDER) {
        url = camel_url_new ((char *)m->selection->data, NULL);

        if (m->move)
            buf = g_strdup_printf (_("Moving folder %s"), url->fragment ? url->fragment : url->path + 1);
        else
            buf = g_strdup_printf (_("Copying folder %s"), url->fragment ? url->fragment : url->path + 1);

        camel_url_free (url);

        return buf;
    } else {
        if (m->move)
            return g_strdup_printf (_("Moving messages into folder %s"), m->full_name);
        else
            return g_strdup_printf (_("Copying messages into folder %s"), m->full_name);
    }
}

static void
emft_drop_async__exec (struct _DragDataReceivedAsync *m)
{
    CamelFolder *folder;

    /* for types other than folder, we can't drop to the root path */
    if (m->info == DND_DROP_TYPE_FOLDER) {
        /* copy or move (aka rename) a folder */
        emft_drop_folder(m);
    } else if (m->full_name == NULL) {
        camel_exception_set (&m->base.ex, CAMEL_EXCEPTION_SYSTEM,
                     _("Cannot drop message(s) into toplevel store"));
    } else if ((folder = camel_store_get_folder (m->store, m->full_name, 0, &m->base.ex))) {
        switch (m->info) {
        case DND_DROP_TYPE_UID_LIST:
            /* import a list of uids from another evo folder */
            em_utils_selection_get_uidlist(m->selection, folder, m->move, &m->base.ex);
            m->moved = m->move && !camel_exception_is_set(&m->base.ex);
            break;
        case DND_DROP_TYPE_MESSAGE_RFC822:
            /* import a message/rfc822 stream */
            em_utils_selection_get_message(m->selection, folder);
            break;
        case DND_DROP_TYPE_TEXT_URI_LIST:
            /* import an mbox, maildir, or mh folder? */
            em_utils_selection_get_urilist(m->selection, folder);
            break;
        default:
            abort();
        }
        camel_object_unref(folder);
    }
}

static void
emft_drop_async__free (struct _DragDataReceivedAsync *m)
{
    g_object_unref(m->context);
    camel_object_unref(m->store);
    g_free(m->full_name);

    g_free(m->selection->data);
    g_free(m->selection);
}

static MailMsgInfo emft_drop_async_info = {
    sizeof (struct _DragDataReceivedAsync),
    (MailMsgDescFunc) emft_drop_async__desc,
    (MailMsgExecFunc) emft_drop_async__exec,
    (MailMsgDoneFunc) NULL,
    (MailMsgFreeFunc) emft_drop_async__free
};

static void
tree_drag_data_action(struct _DragDataReceivedAsync *m)
{
    m->move = m->action == GDK_ACTION_MOVE;
    mail_msg_unordered_push (m);
}

static void
emft_drop_popup_copy(EPopup *ep, EPopupItem *item, void *data)
{
    struct _DragDataReceivedAsync *m = data;

    m->action = GDK_ACTION_COPY;
    tree_drag_data_action(m);
}

static void
emft_drop_popup_move(EPopup *ep, EPopupItem *item, void *data)
{
    struct _DragDataReceivedAsync *m = data;

    m->action = GDK_ACTION_MOVE;
    tree_drag_data_action(m);
}

static void
emft_drop_popup_cancel(EPopup *ep, EPopupItem *item, void *data)
{
    struct _DragDataReceivedAsync *m = data;

    m->aborted = TRUE;
    mail_msg_unref(m);
}

static EPopupItem emft_drop_popup_menu[] = {
    { E_POPUP_ITEM, "00.emc.00", N_("_Copy to Folder"), emft_drop_popup_copy, NULL, NULL, 1 },
    { E_POPUP_ITEM, "00.emc.01", N_("_Move to Folder"), emft_drop_popup_move, NULL, NULL, 1 },
    { E_POPUP_ITEM, "00.emc.02", N_("_Copy"), emft_drop_popup_copy, NULL, "folder-copy", 2 },
    { E_POPUP_ITEM, "00.emc.03", N_("_Move"), emft_drop_popup_move, NULL, "folder-move", 2 },
    { E_POPUP_BAR, "10.emc" },
    { E_POPUP_ITEM, "99.emc.00", N_("Cancel _Drag"), emft_drop_popup_cancel, NULL, "dialog-cancel", 0 },
};

static void
emft_drop_popup_free(EPopup *ep, GSList *items, void *data)
{
    g_slist_free(items);
}

static void
tree_drag_data_received(GtkWidget *widget, GdkDragContext *context, int x, int y, GtkSelectionData *selection, guint info, guint time, EMFolderTree *emft)
{
    struct _EMFolderTreePrivate *priv = emft->priv;
    GtkTreeViewDropPosition pos;
    GtkTreeView *tree_view;
    GtkTreePath *dest_path;
    struct _DragDataReceivedAsync *m;
    gboolean is_store;
    CamelStore *store;
    GtkTreeIter iter;
    char *full_name;
    int i;

    tree_view = GTK_TREE_VIEW (emft);

    if (!gtk_tree_view_get_dest_row_at_pos (tree_view, x, y, &dest_path, &pos))
        return;

    /* this means we are receiving no data */
    if (!selection->data || selection->length == -1) {
        gtk_drag_finish(context, FALSE, FALSE, GDK_CURRENT_TIME);
        return;
    }

    if (!gtk_tree_model_get_iter((GtkTreeModel *)priv->model, &iter, dest_path)) {
        gtk_drag_finish(context, FALSE, FALSE, GDK_CURRENT_TIME);
        return;
    }

    gtk_tree_model_get((GtkTreeModel *)priv->model, &iter,
               COL_POINTER_CAMEL_STORE, &store,
               COL_BOOL_IS_STORE, &is_store,
               COL_STRING_FULL_NAME, &full_name, -1);

    /* make sure user isn't try to drop on a placeholder row */
    if (full_name == NULL && !is_store) {
        gtk_drag_finish (context, FALSE, FALSE, GDK_CURRENT_TIME);
        return;
    }

    m = mail_msg_new (&emft_drop_async_info);
    m->context = context;
    g_object_ref(context);
    m->store = store;
    camel_object_ref(store);
    m->full_name = full_name;
    m->action = context->action;
    m->info = info;

    /* need to copy, goes away once we exit */
    m->selection = g_malloc0(sizeof(*m->selection));
    m->selection->data = g_malloc(selection->length);
    memcpy(m->selection->data, selection->data, selection->length);
    m->selection->length = selection->length;

    if (context->action == GDK_ACTION_ASK) {
        EMPopup *emp;
        int mask;
        GSList *menus = NULL;
        GtkMenu *menu;

        emp = em_popup_new("org.gnome.mail.storageset.popup.drop");
        if (info != DND_DROP_TYPE_FOLDER)
            mask = ~1;
        else
            mask = ~2;

        for (i=0;i<sizeof(emft_drop_popup_menu)/sizeof(emft_drop_popup_menu[0]);i++) {
            EPopupItem *item = &emft_drop_popup_menu[i];

            if ((item->visible & mask) == 0)
                menus = g_slist_append(menus, item);
        }
        e_popup_add_items((EPopup *)emp, menus, NULL, emft_drop_popup_free, m);
        menu = e_popup_create_menu_once((EPopup *)emp, NULL, mask);
        gtk_menu_popup(menu, NULL, NULL, NULL, NULL, 0, gtk_get_current_event_time());
    } else {
        tree_drag_data_action(m);
    }
}

static gboolean
is_special_local_folder (const char *name)
{
    return (!strcmp (name, "Drafts") || !strcmp (name, "Inbox") || !strcmp (name, "Outbox") || !strcmp (name, "Sent") || !strcmp (name, "Templates"));
}

static GdkAtom
emft_drop_target(EMFolderTree *emft, GdkDragContext *context, GtkTreePath *path)
{
    struct _EMFolderTreePrivate *p = emft->priv;
    char *full_name = NULL, *uri = NULL, *src_uri = NULL;
    CamelStore *local, *sstore, *dstore;
    EShellModule *shell_module;
    GdkAtom atom = GDK_NONE;
    gboolean is_store;
    GtkTreeIter iter;
    GList *targets;
    guint32 flags = 0;

    /* This is a bit of a mess, but should handle all the cases properly */

    if (!gtk_tree_model_get_iter((GtkTreeModel *)p->model, &iter, path))
        return GDK_NONE;

    gtk_tree_model_get((GtkTreeModel *)p->model, &iter, COL_BOOL_IS_STORE, &is_store,
               COL_STRING_FULL_NAME, &full_name,
               COL_UINT_FLAGS, &flags,
               COL_POINTER_CAMEL_STORE, &dstore,
               COL_STRING_URI, &uri, -1);

    shell_module = em_folder_tree_model_get_shell_module (p->model);
    local = e_mail_shell_module_get_local_store (shell_module);

    targets = context->targets;

    /* Check for special destinations */
    if (uri && full_name) {
#if 0
        /* only allow copying/moving folders (not messages) into the local Outbox */
        if (dstore == local && !strcmp (full_name, "Outbox")) {
            GdkAtom xfolder;

            xfolder = drop_atoms[DND_DROP_TYPE_FOLDER];
            while (targets != NULL) {
                if (targets->data == (gpointer) xfolder) {
                    atom = xfolder;
                    goto done;
                }

                targets = targets->next;
            }

            goto done;
        }
#endif

        /* don't allow copying/moving into the UNMATCHED vfolder */
        if (!strncmp (uri, "vfolder:", 8) && !strcmp (full_name, CAMEL_UNMATCHED_NAME))
            goto done;

        /* don't allow copying/moving into a vTrash/vJunk folder */
        if (!strcmp (full_name, CAMEL_VTRASH_NAME)
            || !strcmp (full_name, CAMEL_VJUNK_NAME))
            goto done;

        if (flags & CAMEL_FOLDER_NOSELECT)
            goto done;
    }

    if (p->drag_row) {
        GtkTreePath *src_path = gtk_tree_row_reference_get_path(p->drag_row);

        if (src_path) {
            if (gtk_tree_model_get_iter((GtkTreeModel *)p->model, &iter, src_path))
                gtk_tree_model_get((GtkTreeModel *)p->model, &iter,
                           COL_POINTER_CAMEL_STORE, &sstore,
                           COL_STRING_URI, &src_uri, -1);

            /* can't dnd onto itself or below itself - bad things happen,
               no point dragging to where we were either */
            if (gtk_tree_path_compare(path, src_path) == 0
                || gtk_tree_path_is_descendant(path, src_path)
                || (gtk_tree_path_is_ancestor(path, src_path)
                && gtk_tree_path_get_depth(path) == gtk_tree_path_get_depth(src_path)-1)) {
                gtk_tree_path_free(src_path);
                goto done;
            }

            gtk_tree_path_free(src_path);
        }
    }

    /* Check for special sources, and vfolder stuff */
    if (src_uri) {
        CamelURL *url;
        char *url_path;

        /* FIXME: this is a total hack, but i think all we can do at present */
        /* Check for dragging from special folders which can't be moved/copied */
        url = camel_url_new(src_uri, NULL);
        url_path = url->fragment?url->fragment:url->path;
        if (url_path && url_path[0]) {
            /* don't allow moving any of the the local special folders */
            if (sstore == local && is_special_local_folder (url_path)) {
                GdkAtom xfolder;

                camel_url_free (url);

                /* TODO: not sure if this is legal, but it works, force copy for special local folders */
                context->suggested_action = GDK_ACTION_COPY;
                xfolder = drop_atoms[DND_DROP_TYPE_FOLDER];
                while (targets != NULL) {
                    if (targets->data == (gpointer) xfolder) {
                        atom = xfolder;
                        goto done;
                    }

                    targets = targets->next;
                }

                goto done;
            }

            /* don't allow copying/moving of the UNMATCHED vfolder */
            if (!strcmp (url->protocol, "vfolder") && !strcmp (url_path, CAMEL_UNMATCHED_NAME)) {
                camel_url_free (url);
                goto done;
            }

            /* don't allow copying/moving of any vTrash/vJunk folder nor maildir 'inbox' */
            if (strcmp(url_path, CAMEL_VTRASH_NAME) == 0
                || strcmp(url_path, CAMEL_VJUNK_NAME) == 0
                /* Dont allow drag from maildir 'inbox' */
                || strcmp(url_path, ".") == 0) {
                camel_url_free(url);
                goto done;
            }
        }
        camel_url_free(url);

        /* Search Folders can only be dropped into other Search Folders */
        if (strncmp(src_uri, "vfolder:", 8) == 0) {
            /* TODO: not sure if this is legal, but it works, force move only for vfolders */
            context->suggested_action = GDK_ACTION_MOVE;

            if (uri && strncmp(uri, "vfolder:", 8) == 0) {
                GdkAtom xfolder;

                xfolder = drop_atoms[DND_DROP_TYPE_FOLDER];
                while (targets != NULL) {
                    if (targets->data == (gpointer) xfolder) {
                        atom = xfolder;
                        goto done;
                    }

                    targets = targets->next;
                }
            }

            goto done;
        }
    }

    /* can't drag anything but a vfolder into a vfolder */
    if (uri && strncmp(uri, "vfolder:", 8) == 0)
        goto done;

    /* Now we either have a store or a normal folder */

    if (is_store) {
        GdkAtom xfolder;

        xfolder = drop_atoms[DND_DROP_TYPE_FOLDER];
        while (targets != NULL) {
            if (targets->data == (gpointer) xfolder) {
                atom = xfolder;
                goto done;
            }

            targets = targets->next;
        }
    } else {
        int i;

        while (targets != NULL) {
            for (i = 0; i < NUM_DROP_TYPES; i++) {
                if (targets->data == (gpointer) drop_atoms[i]) {
                    atom = drop_atoms[i];
                    goto done;
                }
            }

            targets = targets->next;
        }
    }

 done:

    g_free (full_name);
    g_free (uri);

    return atom;
}

static gboolean
tree_drag_drop (GtkWidget *widget, GdkDragContext *context, int x, int y, guint time, EMFolderTree *emft)
{
    struct _EMFolderTreePrivate *priv = emft->priv;
    GtkTreeViewColumn *column;
    GtkTreeView *tree_view;
    int cell_x, cell_y;
    GtkTreePath *path;
    GdkAtom target;

    tree_view = GTK_TREE_VIEW (emft);

    if (priv->autoscroll_id != 0) {
        g_source_remove (priv->autoscroll_id);
        priv->autoscroll_id = 0;
    }

    if (priv->autoexpand_id != 0) {
        gtk_tree_row_reference_free (priv->autoexpand_row);
        priv->autoexpand_row = NULL;

        g_source_remove (priv->autoexpand_id);
        priv->autoexpand_id = 0;
    }

    if (!gtk_tree_view_get_path_at_pos (tree_view, x, y, &path, &column, &cell_x, &cell_y))
        return FALSE;

    target = emft_drop_target(emft, context, path);
    gtk_tree_path_free (path);
    if (target == GDK_NONE)
        return FALSE;

    return TRUE;
}

static void
tree_drag_end (GtkWidget *widget, GdkDragContext *context, EMFolderTree *emft)
{
    struct _EMFolderTreePrivate *priv = emft->priv;

    if (priv->drag_row) {
        gtk_tree_row_reference_free (priv->drag_row);
        priv->drag_row = NULL;
    }

    /* FIXME: undo anything done in drag-begin */
}

static void
tree_drag_leave (GtkWidget *widget, GdkDragContext *context, guint time, EMFolderTree *emft)
{
    struct _EMFolderTreePrivate *priv = emft->priv;
    GtkTreeView *tree_view;

    tree_view = GTK_TREE_VIEW (emft);

    if (priv->autoscroll_id != 0) {
        g_source_remove (priv->autoscroll_id);
        priv->autoscroll_id = 0;
    }

    if (priv->autoexpand_id != 0) {
        gtk_tree_row_reference_free (priv->autoexpand_row);
        priv->autoexpand_row = NULL;

        g_source_remove (priv->autoexpand_id);
        priv->autoexpand_id = 0;
    }

    gtk_tree_view_set_drag_dest_row(tree_view, NULL, GTK_TREE_VIEW_DROP_BEFORE);
}


#define SCROLL_EDGE_SIZE 15

static gboolean
tree_autoscroll (EMFolderTree *emft)
{
    GtkAdjustment *vadjustment;
    GtkTreeView *tree_view;
    GdkRectangle rect;
    GdkWindow *window;
    int offset, y;
    float value;

    /* get the y pointer position relative to the treeview */
    tree_view = GTK_TREE_VIEW (emft);
    window = gtk_tree_view_get_bin_window (tree_view);
    gdk_window_get_pointer (window, NULL, &y, NULL);

    /* rect is in coorinates relative to the scrolled window relative to the treeview */
    gtk_tree_view_get_visible_rect (tree_view, &rect);

    /* move y into the same coordinate system as rect */
    y += rect.y;

    /* see if we are near the top edge */
    if ((offset = y - (rect.y + 2 * SCROLL_EDGE_SIZE)) > 0) {
        /* see if we are near the bottom edge */
        if ((offset = y - (rect.y + rect.height - 2 * SCROLL_EDGE_SIZE)) < 0)
            return TRUE;
    }

    vadjustment = gtk_tree_view_get_vadjustment (tree_view);

    value = CLAMP (vadjustment->value + offset, 0.0, vadjustment->upper - vadjustment->page_size);
    gtk_adjustment_set_value (vadjustment, value);

    return TRUE;
}

static gboolean
tree_autoexpand (EMFolderTree *emft)
{
    struct _EMFolderTreePrivate *priv = emft->priv;
    GtkTreeView *tree_view;
    GtkTreePath *path;

    tree_view = GTK_TREE_VIEW (emft);
    path = gtk_tree_row_reference_get_path (priv->autoexpand_row);
    gtk_tree_view_expand_row (tree_view, path, FALSE);
    gtk_tree_path_free (path);

    return TRUE;
}

static gboolean
tree_drag_motion (GtkWidget *widget, GdkDragContext *context, int x, int y, guint time, EMFolderTree *emft)
{
    struct _EMFolderTreePrivate *priv = emft->priv;
    GtkTreeModel *model = (GtkTreeModel *) priv->model;
    GtkTreeViewDropPosition pos;
    GtkTreeView *tree_view;
    GdkDragAction action = 0;
    GtkTreePath *path;
    GtkTreeIter iter;
    GdkAtom target;
    int i;

    tree_view = GTK_TREE_VIEW (emft);

    if (!gtk_tree_view_get_dest_row_at_pos(tree_view, x, y, &path, &pos))
        return FALSE;

    if (priv->autoscroll_id == 0)
        priv->autoscroll_id = g_timeout_add (150, (GSourceFunc) tree_autoscroll, emft);

    gtk_tree_model_get_iter (model, &iter, path);

    if (gtk_tree_model_iter_has_child (model, &iter) && !gtk_tree_view_row_expanded (tree_view, path)) {
        if (priv->autoexpand_id != 0) {
            GtkTreePath *autoexpand_path;

            autoexpand_path = gtk_tree_row_reference_get_path (priv->autoexpand_row);
            if (gtk_tree_path_compare (autoexpand_path, path) != 0) {
                /* row changed, restart timer */
                gtk_tree_row_reference_free (priv->autoexpand_row);
                priv->autoexpand_row = gtk_tree_row_reference_new (model, path);
                g_source_remove (priv->autoexpand_id);
                priv->autoexpand_id = g_timeout_add (600, (GSourceFunc) tree_autoexpand, emft);
            }

            gtk_tree_path_free (autoexpand_path);
        } else {
            priv->autoexpand_id = g_timeout_add (600, (GSourceFunc) tree_autoexpand, emft);
            priv->autoexpand_row = gtk_tree_row_reference_new (model, path);
        }
    } else if (priv->autoexpand_id != 0) {
        gtk_tree_row_reference_free (priv->autoexpand_row);
        priv->autoexpand_row = NULL;

        g_source_remove (priv->autoexpand_id);
        priv->autoexpand_id = 0;
    }

    target = emft_drop_target(emft, context, path);
    if (target != GDK_NONE) {
        for (i=0; i<NUM_DROP_TYPES; i++) {
            if (drop_atoms[i] == target) {
                switch (i) {
                case DND_DROP_TYPE_UID_LIST:
                case DND_DROP_TYPE_FOLDER:
                    action = context->suggested_action;
                    if (action == GDK_ACTION_COPY && (context->actions & GDK_ACTION_MOVE))
                        action = GDK_ACTION_MOVE;
                    gtk_tree_view_set_drag_dest_row(tree_view, path, GTK_TREE_VIEW_DROP_INTO_OR_AFTER);
                    break;
                default:
                    gtk_tree_view_set_drag_dest_row(tree_view, path, GTK_TREE_VIEW_DROP_INTO_OR_AFTER);
                    action = context->suggested_action;
                    break;
                }
                break;
            }
        }
    }

    gtk_tree_path_free(path);

    gdk_drag_status(context, action, time);

    return action != 0;
}

void
em_folder_tree_enable_drag_and_drop (EMFolderTree *emft)
{
    struct _EMFolderTreePrivate *priv;
    GtkTreeView *tree_view;
    static int setup = 0;
    int i;

    g_return_if_fail (EM_IS_FOLDER_TREE (emft));

    tree_view = GTK_TREE_VIEW (emft);

    priv = emft->priv;
    if (!setup) {
        for (i=0; i<NUM_DRAG_TYPES; i++)
            drag_atoms[i] = gdk_atom_intern(drag_types[i].target, FALSE);

        for (i=0; i<NUM_DROP_TYPES; i++)
            drop_atoms[i] = gdk_atom_intern(drop_types[i].target, FALSE);

        setup = 1;
    }

    gtk_drag_source_set((GtkWidget *)tree_view, GDK_BUTTON1_MASK, drag_types, NUM_DRAG_TYPES, GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_ASK);
    gtk_drag_dest_set((GtkWidget *)tree_view, GTK_DEST_DEFAULT_ALL, drop_types, NUM_DROP_TYPES, GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_ASK);

    g_signal_connect (tree_view, "drag-begin", G_CALLBACK (tree_drag_begin), emft);
    g_signal_connect (tree_view, "drag-data-delete", G_CALLBACK (tree_drag_data_delete), emft);
    g_signal_connect (tree_view, "drag-data-get", G_CALLBACK (tree_drag_data_get), emft);
    g_signal_connect (tree_view, "drag-data-received", G_CALLBACK (tree_drag_data_received), emft);
    g_signal_connect (tree_view, "drag-drop", G_CALLBACK (tree_drag_drop), emft);
    g_signal_connect (tree_view, "drag-end", G_CALLBACK (tree_drag_end), emft);
    g_signal_connect (tree_view, "drag-leave", G_CALLBACK (tree_drag_leave), emft);
    g_signal_connect (tree_view, "drag-motion", G_CALLBACK (tree_drag_motion), emft);
}

void
em_folder_tree_set_multiselect (EMFolderTree *tree, gboolean mode)
{
    GtkTreeSelection *sel;
    GtkTreeView *tree_view;

    tree_view = GTK_TREE_VIEW (tree);
    sel = gtk_tree_view_get_selection (tree_view);

    tree->priv->do_multiselect = mode;
    gtk_tree_selection_set_mode (sel, mode ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_SINGLE);
}

void em_folder_tree_set_excluded(EMFolderTree *emft, guint32 flags)
{
    emft->priv->excluded = flags;
}

void em_folder_tree_set_excluded_func(EMFolderTree *emft, EMFTExcludeFunc exclude, void *data)
{
    emft->priv->excluded_func = exclude;
    emft->priv->excluded_data = data;
}

GList *
em_folder_tree_get_selected_uris (EMFolderTree *emft)
{
    GtkTreeSelection *selection;
    GtkTreeView *tree_view;
    GtkTreeModel *model;
    GList *list = NULL, *rows, *l;
    GSList *sl;

    tree_view = GTK_TREE_VIEW (emft);
    selection = gtk_tree_view_get_selection (tree_view);

    /* at first, add lost uris */
    for (sl = emft->priv->select_uris; sl; sl = g_slist_next(sl))
        list = g_list_append (list, g_strdup (((struct _selected_uri *)sl->data)->uri));

    rows = gtk_tree_selection_get_selected_rows(selection, &model);
    for (l=rows; l; l=g_list_next(l)) {
        GtkTreeIter iter;
        GtkTreePath *path = l->data;

        if (gtk_tree_model_get_iter(model, &iter, path)) {
            char *uri;

            gtk_tree_model_get(model, &iter, COL_STRING_URI, &uri, -1);
            list = g_list_prepend (list, uri);
        }
        gtk_tree_path_free(path);
    }
    g_list_free(rows);

    return g_list_reverse (list);
}

static void
get_selected_uris_path_iterate (GtkTreeModel *model, GtkTreePath *treepath, GtkTreeIter *iter, gpointer data)
{
    GList **list = (GList **) data;
    char *full_name;

    gtk_tree_model_get (model, iter, COL_STRING_FULL_NAME, &full_name, -1);
    *list = g_list_append (*list, full_name);
}

GList *
em_folder_tree_get_selected_paths (EMFolderTree *emft)
{
    GtkTreeSelection *selection;
    GtkTreeView *tree_view;
    GList *list = NULL;

    tree_view = GTK_TREE_VIEW (emft);
    selection = gtk_tree_view_get_selection (tree_view);

    gtk_tree_selection_selected_foreach (selection, get_selected_uris_path_iterate, &list);

    return list;
}

static void
emft_clear_selected_list(EMFolderTree *emft)
{
    struct _EMFolderTreePrivate *priv = emft->priv;

    g_slist_foreach(priv->select_uris, (GFunc) emft_free_select_uri, NULL);
    g_slist_free(priv->select_uris);
    g_hash_table_destroy(priv->select_uris_table);
    priv->select_uris = NULL;
    priv->select_uris_table = g_hash_table_new(g_str_hash, g_str_equal);
    priv->cursor_set = FALSE;
}

void
em_folder_tree_set_selected_list (EMFolderTree *emft, GList *list, gboolean expand_only)
{
    struct _EMFolderTreePrivate *priv = emft->priv;
    int id = 0;

    /* FIXME: need to remove any currently selected stuff? */
    if (!expand_only)
        emft_clear_selected_list(emft);

    for (;list;list = list->next) {
        struct _selected_uri *u = g_malloc0(sizeof(*u));
        CamelURL *url;
        CamelException ex = { 0 };

        u->uri = g_strdup(list->data);
        u->store = (CamelStore *)camel_session_get_service (session, u->uri, CAMEL_PROVIDER_STORE, &ex);
        camel_exception_clear(&ex);

        url = camel_url_new(u->uri, NULL);
        if (u->store == NULL || url == NULL) {
            if (!expand_only) {
                u->key = g_strdup_printf("dummy-%d:%s", id++, u->uri);
                g_hash_table_insert(priv->select_uris_table, u->key, u);
                priv->select_uris = g_slist_append(priv->select_uris, u);
            }
        } else {
            const char *path;
            char *expand_key, *end;
            EAccount *account;

            if (((CamelService *)u->store)->provider->url_flags & CAMEL_URL_FRAGMENT_IS_PATH)
                path = url->fragment;
            else
                path = url->path && url->path[0]=='/' ? url->path+1:url->path;
            if (path == NULL)
                path = "";

            /* This makes sure all our parents up to the root are expanded */
            /* FIXME: Why does the expanded state store this made up path rather than the euri? */
            if ( (account = mail_config_get_account_by_source_url(u->uri)) )
                expand_key = g_strdup_printf ("%s/%s", account->uid, path);
            else if (CAMEL_IS_VEE_STORE (u->store))
                expand_key = g_strdup_printf ("vfolder/%s", path);
            else
                expand_key = g_strdup_printf ("local/%s", path);

            if (!expand_only) {
                u->key = g_strdup(expand_key);

                g_hash_table_insert(priv->select_uris_table, u->key, u);
                priv->select_uris = g_slist_append(priv->select_uris, u);
            }

            end = strrchr(expand_key, '/');
            do {
                emft_expand_node(priv->model, expand_key, emft);
                em_folder_tree_model_set_expanded(priv->model, expand_key, TRUE);
                *end = 0;
                end = strrchr(expand_key, '/');
            } while (end);
            g_free(expand_key);
        }

        if (url)
            camel_url_free(url);
    }
}


#if 0
static void
dump_fi (CamelFolderInfo *fi, int depth)
{
    int i;

    while (fi != NULL) {
        for (i = 0; i < depth; i++)
            fputs ("  ", stdout);

        printf ("path='%s'; full_name='%s'\n", fi->path, fi->full_name);

        if (fi->child)
            dump_fi (fi->child, depth + 1);

        fi = fi->sibling;
    }
}
#endif

struct _EMFolderTreeGetFolderInfo {
    MailMsg base;

    /* input data */
    GtkTreeRowReference *root;
    EMFolderTree *emft;
    CamelStore *store;
    guint32 flags;
    char *top;

    /* output data */
    CamelFolderInfo *fi;
};

static gchar *
emft_get_folder_info__desc (struct _EMFolderTreeGetFolderInfo *m)
{
    char *ret, *name;

    name = camel_service_get_name((CamelService *)m->store, TRUE);
    ret = g_strdup_printf(_("Scanning folders in \"%s\""), name);
    g_free(name);
    return ret;
}

static void
emft_get_folder_info__exec (struct _EMFolderTreeGetFolderInfo *m)
{
    guint32 flags = m->flags | CAMEL_STORE_FOLDER_INFO_SUBSCRIBED;

    m->fi = camel_store_get_folder_info (m->store, m->top, flags, &m->base.ex);
}

static void
emft_get_folder_info__done (struct _EMFolderTreeGetFolderInfo *m)
{
    struct _EMFolderTreePrivate *priv = m->emft->priv;
    struct _EMFolderTreeModelStoreInfo *si;
    GtkTreeIter root, iter, titer;
    CamelFolderInfo *fi;
    GtkTreeView *tree_view;
    GtkTreeStore *model;
    GtkTreePath *path;
    gboolean is_store;

    /* check that we haven't been destroyed */
    g_return_if_fail (GTK_IS_TREE_VIEW (m->emft));

    /* check that our parent folder hasn't been deleted/unsubscribed */
    if (!gtk_tree_row_reference_valid (m->root))
        return;

    if (!(si = g_hash_table_lookup (priv->model->store_hash, m->store))) {
        /* store has been removed in the interim - do nothing */
        return;
    }

    tree_view = GTK_TREE_VIEW (m->emft);
    model = (GtkTreeStore *) gtk_tree_view_get_model (tree_view);

    path = gtk_tree_row_reference_get_path (m->root);
    gtk_tree_model_get_iter ((GtkTreeModel *) model, &root, path);

    /* if we had an error, then we need to re-set the load subdirs state and collapse the node */
    if (!m->fi && camel_exception_is_set(&m->base.ex)) {
        gtk_tree_store_set(model, &root, COL_BOOL_LOAD_SUBDIRS, TRUE, -1);
        gtk_tree_view_collapse_row (tree_view, path);
        gtk_tree_path_free (path);
        return;
    }

    gtk_tree_path_free (path);

    /* make sure we still need to load the tree subfolders... */
    gtk_tree_model_get ((GtkTreeModel *) model, &root,
                COL_BOOL_IS_STORE, &is_store,
                -1);

    /* get the first child (which will be a dummy node) */
    gtk_tree_model_iter_children ((GtkTreeModel *) model, &iter, &root);

    /* Traverse to the last valid iter */
    titer = iter;
    while (gtk_tree_model_iter_next((GtkTreeModel *) model, &iter)) 
        titer = iter; /* Preserve the last valid iter */

    iter = titer;

    /* FIXME: camel's IMAP code is totally on crack here, @top's
     * folder info should be @fi and fi->child should be what we
     * want to fill our tree with... *sigh* */
    if (m->top && m->fi && !strcmp (m->fi->full_name, m->top)) {
        if (!(fi = m->fi->child))
            fi = m->fi->next;
    } else
        fi = m->fi;

    if (fi == NULL) {
        /* no children afterall... remove the "Loading..." placeholder node */
        emft_update_model_expanded_state (priv, &root, FALSE);

        gtk_tree_store_remove (model, &iter);

        if (is_store) {
            path = gtk_tree_model_get_path ((GtkTreeModel *) model, &root);
            gtk_tree_view_collapse_row (tree_view, path);
            emft_queue_save_state (m->emft);
            gtk_tree_path_free (path);
            return;
        }
    } else {
        int fully_loaded = (m->flags & CAMEL_STORE_FOLDER_INFO_RECURSIVE) ? TRUE : FALSE;

        do {
            em_folder_tree_model_set_folder_info (priv->model, &iter, si, fi, fully_loaded);

            if ((fi = fi->next) != NULL)
                gtk_tree_store_append (model, &iter, &root);
        } while (fi != NULL);
    }

    gtk_tree_store_set (model, &root, COL_BOOL_LOAD_SUBDIRS, FALSE, -1);
    emft_queue_save_state (m->emft);
}

static void
emft_get_folder_info__free (struct _EMFolderTreeGetFolderInfo *m)
{
    camel_store_free_folder_info (m->store, m->fi);

    gtk_tree_row_reference_free (m->root);
    g_object_unref(m->emft);
    camel_object_unref (m->store);
    g_free (m->top);
}

static MailMsgInfo get_folder_info_info = {
    sizeof (struct _EMFolderTreeGetFolderInfo),
    (MailMsgDescFunc) emft_get_folder_info__desc,
    (MailMsgExecFunc) emft_get_folder_info__exec,
    (MailMsgDoneFunc) emft_get_folder_info__done,
    (MailMsgFreeFunc) emft_get_folder_info__free
};

static void
emft_update_model_expanded_state (struct _EMFolderTreePrivate *priv, GtkTreeIter *iter, gboolean expanded)
{
    struct _EMFolderTreeModelStoreInfo *si;
    gboolean is_store;
    CamelStore *store;
    EAccount *account;
    char *full_name;
    char *key;

    gtk_tree_model_get ((GtkTreeModel *) priv->model, iter,
                COL_STRING_FULL_NAME, &full_name,
                COL_POINTER_CAMEL_STORE, &store,
                COL_BOOL_IS_STORE, &is_store,
                -1);

    si = g_hash_table_lookup (priv->model->store_hash, store);
    if ((account = mail_config_get_account_by_name (si->display_name))) {
            key = g_strdup_printf ("%s/%s", account->uid, full_name ? full_name : "");
    } else if (CAMEL_IS_VEE_STORE (store)) {
        /* vfolder store */
        key = g_strdup_printf ("vfolder/%s", full_name ? full_name : "");
    } else {
        /* local store */
        key = g_strdup_printf ("local/%s", full_name ? full_name : "");
    }

    em_folder_tree_model_set_expanded (priv->model, key, expanded);
    g_free (full_name);
    g_free (key);
}

static void
emft_tree_row_expanded (GtkTreeView *treeview, GtkTreeIter *root, GtkTreePath *tree_path, EMFolderTree *emft)
{
    struct _EMFolderTreePrivate *priv = emft->priv;
    struct _EMFolderTreeGetFolderInfo *m;
    GtkTreeModel *model;
    CamelStore *store;
    char *full_name;
    gboolean load;

    model = gtk_tree_view_get_model (treeview);

    gtk_tree_model_get (model, root,
                COL_STRING_FULL_NAME, &full_name,
                COL_POINTER_CAMEL_STORE, &store,
                COL_BOOL_LOAD_SUBDIRS, &load,
                -1);

    emft_update_model_expanded_state (priv, root, TRUE);

    if (!load) {
        emft_queue_save_state (emft);
        g_free (full_name);
        return;
    }

    gtk_tree_store_set((GtkTreeStore *)model, root, COL_BOOL_LOAD_SUBDIRS, FALSE, -1);

    m = mail_msg_new (&get_folder_info_info);
    m->root = gtk_tree_row_reference_new (model, tree_path);
    camel_object_ref (store);
    m->store = store;
    m->emft = emft;
    g_object_ref(emft);
    m->top = full_name;
    m->flags = CAMEL_STORE_FOLDER_INFO_RECURSIVE|CAMEL_STORE_FOLDER_INFO_FAST;

    mail_msg_unordered_push (m);
}

static gboolean
emft_tree_test_collapse_row (GtkTreeView *treeview, GtkTreeIter *root, GtkTreePath *tree_path, EMFolderTree *emft)
{
    GtkTreeSelection *selection;
    GtkTreeModel *model;
    GtkTreeIter cursor;

    selection = gtk_tree_view_get_selection (treeview);
    if (gtk_tree_selection_get_selected (selection, &model, &cursor)) {
        /* select the collapsed node IFF it is a parent of the currently selected folder */
        if (gtk_tree_store_is_ancestor ((GtkTreeStore *) model, root, &cursor))
            gtk_tree_view_set_cursor (treeview, tree_path, NULL, FALSE);
    }

    emft_update_model_expanded_state (emft->priv, root, FALSE);
    emft_queue_save_state (emft);

    return FALSE;
}

static void
emft_tree_row_activated (GtkTreeView *treeview, GtkTreePath *tree_path, GtkTreeViewColumn *column, EMFolderTree *emft)
{
    struct _EMFolderTreePrivate *priv = emft->priv;
    GtkTreeModel *model = (GtkTreeModel *) priv->model;
    char *full_name, *uri;
    GtkTreeIter iter;
    guint32 flags;

    if (!emft_select_func(NULL, model, tree_path, FALSE, emft))
        return;

    if (!gtk_tree_model_get_iter (model, &iter, tree_path))
        return;

    gtk_tree_model_get (model, &iter, COL_STRING_FULL_NAME, &full_name,
                COL_STRING_URI, &uri, COL_UINT_FLAGS, &flags, -1);

    emft_clear_selected_list(emft);

    g_signal_emit (emft, signals[FOLDER_SELECTED], 0, full_name, uri, flags);
    g_signal_emit (emft, signals[FOLDER_ACTIVATED], 0, full_name, uri);

    g_free(full_name);
    g_free(uri);
}

#if 0
static void
emft_popup_view (GtkWidget *item, EMFolderTree *emft)
{

}

static void
emft_popup_open_new (GtkWidget *item, EMFolderTree *emft)
{
}
#endif

static void
emft_popup_copy(EPopup *ep, EPopupItem *item, void *data)
{
    EMFolderTree *emft = data;
    CamelFolderInfo *fi = NULL;

    /* FIXME: use async apis */
    if ((fi = em_folder_tree_get_selected_folder_info (emft)) != NULL)
        em_folder_utils_copy_folder(fi, FALSE);
}

static void
emft_popup_move(EPopup *ep, EPopupItem *item, void *data)
{
    EMFolderTree *emft = data;
    CamelFolderInfo *fi = NULL;

    /* FIXME: use async apis */
    if ((fi = em_folder_tree_get_selected_folder_info (emft)) != NULL)
        em_folder_utils_copy_folder(fi, TRUE);
}

static void
emft_popup_new_folder (EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderTree *emft = data;
    CamelFolderInfo *fi;

    if ((fi = em_folder_tree_get_selected_folder_info (emft)) != NULL) {
        em_folder_utils_create_folder(fi, emft);
        camel_folder_info_free(fi);
    }
}

static void
selfunc (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
{
    struct _emft_selection_data *dat = (struct _emft_selection_data *) data;

    dat->model = model;
    if (!dat->set)
        *(dat->iter) = *iter;
    dat->set = TRUE;
}

static gboolean
emft_selection_get_selected (GtkTreeSelection *selection, GtkTreeModel **model, GtkTreeIter *iter)
{
    struct _emft_selection_data dat = { NULL, iter, FALSE };

    if (gtk_tree_selection_get_mode (selection) == GTK_SELECTION_MULTIPLE) {
        gtk_tree_selection_selected_foreach (selection, selfunc, &dat);
        if (model)
            *model = dat.model;
        return dat.set;
    } else {
        return gtk_tree_selection_get_selected (selection, model, iter);
    }
}

static void
emft_popup_delete_folder (EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderTree *emft = data;
    CamelFolder *folder;

    if ((folder = em_folder_tree_get_selected_folder (emft)) != NULL) {
        em_folder_utils_delete_folder(folder);
    }
}

static void
emft_popup_rename_folder (EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderTree *emft = data;
    CamelFolder *folder;

    if ((folder = em_folder_tree_get_selected_folder (emft)) != NULL) {
        em_folder_utils_rename_folder(folder);
    }
}

static void
emft_popup_refresh_folder (EPopup *ep, EPopupItem *pitem, void *data)
{
        EMFolderTree *emft = data;
        CamelFolder *folder;

        if ((folder = em_folder_tree_get_selected_folder (emft)) != NULL)
                mail_refresh_folder(folder, NULL, NULL);
}

static void
emft_popup_flush_outbox (EPopup *ep, EPopupItem *pitem, void *data)
{
    mail_send ();
}

static void
emft_popup_empty_trash (EPopup *ep, EPopupItem *pitem, void *data)
{
    em_utils_empty_trash (data);
}

static void
emft_popup_properties (EPopup *ep, EPopupItem *pitem, void *data)
{
    EMFolderTree *emft = data;
    GtkTreeSelection *selection;
    GtkTreeView *tree_view;
    GtkTreeModel *model;
    GtkTreeIter iter;
    char *uri;

    tree_view = GTK_TREE_VIEW (emft);
    selection = gtk_tree_view_get_selection (tree_view);
    if (!emft_selection_get_selected (selection, &model, &iter))
        return;

    gtk_tree_model_get (model, &iter, COL_STRING_URI, &uri, -1);
    em_folder_properties_show (NULL, NULL, uri);
    g_free (uri);
}

static EPopupItem emft_popup_items[] = {
#if 0
    { E_POPUP_ITEM, "00.emc.00", N_("_View"), emft_popup_view, NULL, NULL, EM_POPUP_FOLDER_SELECT },
    { E_POPUP_ITEM, "00.emc.01", N_("Open in _New Window"), emft_popup_open_new, NULL, NULL, EM_POPUP_FOLDER_SELECT },

    { E_POPUP_BAR, "10.emc" },
#endif
    /* FIXME: need to disable for nochildren folders */
    { E_POPUP_ITEM, "10.emc.00", N_("_New Folder..."), emft_popup_new_folder, NULL, "folder-new", 0, EM_POPUP_FOLDER_INFERIORS },

    { E_POPUP_ITEM, "10.emc.05", N_("_Copy..."), emft_popup_copy, NULL, "folder-copy", 0, EM_POPUP_FOLDER_FOLDER|EM_POPUP_FOLDER_SELECT },
    { E_POPUP_ITEM, "10.emc.06", N_("_Move..."), emft_popup_move, NULL, "folder-move", 0, EM_POPUP_FOLDER_FOLDER|EM_POPUP_FOLDER_DELETE },

    /* FIXME: need to disable for undeletable folders */
    { E_POPUP_BAR,  "20.emc" },
    { E_POPUP_ITEM, "20.emc.01", N_("_Delete"), emft_popup_delete_folder, NULL, "edit-delete", 0, EM_POPUP_FOLDER_FOLDER|EM_POPUP_FOLDER_DELETE },

    { E_POPUP_BAR, "30.emc" },
    { E_POPUP_ITEM, "30.emc.02", N_("_Rename..."), emft_popup_rename_folder, NULL, NULL, 0, EM_POPUP_FOLDER_FOLDER|EM_POPUP_FOLDER_DELETE },
    { E_POPUP_ITEM, "30.emc.03", N_("Re_fresh"), emft_popup_refresh_folder, NULL, "view-refresh", EM_POPUP_FOLDER_NONSTATIC, EM_POPUP_FOLDER_FOLDER|EM_POPUP_FOLDER_SELECT},
    { E_POPUP_ITEM, "30.emc.04", N_("Fl_ush Outbox"), emft_popup_flush_outbox, NULL, "mail-send", EM_POPUP_FOLDER_OUTBOX, 0 },


    { E_POPUP_BAR, "99.emc" },
    { E_POPUP_ITEM, "99.emc.00", N_("_Properties"), emft_popup_properties, NULL, "document-properties", 0, EM_POPUP_FOLDER_FOLDER|EM_POPUP_FOLDER_SELECT }
};
static EPopupItem trash_popup_item = {E_POPUP_ITEM, "30.emc.05", N_("_Empty Trash"), emft_popup_empty_trash,NULL,NULL, 1, EM_POPUP_FOLDER_FOLDER|EM_POPUP_FOLDER_SELECT};

static void
emft_popup_free(EPopup *ep, GSList *items, void *data)
{
    g_slist_free(items);
}

static gboolean
emft_popup (EMFolderTree *emft, GdkEvent *event)
{
    GtkTreeView *tree_view;
    GtkTreeSelection *selection;
    CamelStore *local, *store;
    EMPopupTargetFolder *target;
    EShellModule *shell_module;
    GtkTreeModel *model;
    GtkTreeIter iter;
    GSList *menus = NULL;
    guint32 info_flags = 0;
    guint32 flags = 0;
    guint32 folder_type_flags = 0;
    gboolean isstore;
    char *uri, *full_name;
    GtkMenu *menu;
    EMPopup *emp;
    CamelFolder *selfolder = NULL;
    int i;

    tree_view = GTK_TREE_VIEW (emft);

    /* this centralises working out when the user's done something */
    emft_tree_user_event(tree_view, (GdkEvent *)event, emft);

    /* FIXME: we really need the folderinfo to build a proper menu */
    selection = gtk_tree_view_get_selection (tree_view);
    if (!emft_selection_get_selected (selection, &model, &iter))
        return FALSE;

    shell_module = em_folder_tree_model_get_shell_module (emft->priv->model);

    gtk_tree_model_get (model, &iter, COL_POINTER_CAMEL_STORE, &store,
                COL_STRING_URI, &uri, COL_STRING_FULL_NAME, &full_name,
                COL_BOOL_IS_STORE, &isstore, COL_UINT_FLAGS, &folder_type_flags, -1);

    /* Stores have full_name == NULL, otherwise its just a placeholder */
    /* NB: This is kind of messy */
    if (!isstore && full_name == NULL) {
        g_free (uri);
        return FALSE;
    }

    /* TODO: em_popup_target_folder_new? */
    if (isstore) {
        flags |= EM_POPUP_FOLDER_STORE;
    } else {
        flags |= EM_POPUP_FOLDER_FOLDER;

        local = e_mail_shell_module_get_local_store (shell_module);

        /* don't allow deletion of special local folders */
        if (!(store == local && is_special_local_folder (full_name)))
            flags |= EM_POPUP_FOLDER_DELETE;

        /* hack for vTrash/vJunk */
        if (!strcmp (full_name, CAMEL_VTRASH_NAME) || !strcmp (full_name, CAMEL_VJUNK_NAME))
            info_flags |= CAMEL_FOLDER_VIRTUAL | CAMEL_FOLDER_NOINFERIORS;

        selfolder = em_folder_tree_get_selected_folder (emft);
        if (em_utils_folder_is_outbox (selfolder, NULL))
            info_flags |= CAMEL_FOLDER_TYPE_OUTBOX;
    }

    folder_tree_emit_popup_event (emft, event);

#if 0  /* KILL-BONOBO */
    /** @HookPoint-EMPopup: Folder Tree Context Menu
     * @Id: org.gnome.evolution.mail.foldertree.popup
     * @Class: org.gnome.evolution.mail.popup:1.0
     * @Target: EMPopupTargetFolder
     *
     * This is the context menu shown on the folder tree.
     */
    emp = em_popup_new ("org.gnome.evolution.mail.foldertree.popup");

    /* FIXME: pass valid fi->flags here */
    target = em_popup_target_new_folder (emp, uri, info_flags, flags);

    for (i = 0; i < sizeof (emft_popup_items) / sizeof (emft_popup_items[0]); i++)
        menus = g_slist_prepend (menus, &emft_popup_items[i]);

    if ((folder_type_flags & CAMEL_FOLDER_TYPE_MASK) == CAMEL_FOLDER_TYPE_TRASH)
        menus = g_slist_prepend (menus, &trash_popup_item);

    e_popup_add_items ((EPopup *)emp, menus, NULL, emft_popup_free, emft);

    menu = e_popup_create_menu_once ((EPopup *)emp, (EPopupTarget *)target, 0);

    if (event == NULL || event->type == GDK_KEY_PRESS) {
        /* FIXME: menu pos function */
        gtk_menu_popup (menu, NULL, NULL, NULL, NULL, 0, gtk_get_current_event_time());
    } else {
        gtk_menu_popup (menu, NULL, NULL, NULL, NULL, event->button.button, event->button.time);
    }
#endif

    g_free (full_name);
    g_free (uri);

    return TRUE;
}

static gboolean
emft_popup_menu (GtkWidget *widget)
{
    return emft_popup (EM_FOLDER_TREE (widget), NULL);
}

static gboolean
emft_tree_button_press (GtkTreeView *treeview, GdkEventButton *event, EMFolderTree *emft)
{
    GtkTreeSelection *selection;
    GtkTreePath *tree_path;

    /* this centralises working out when the user's done something */
    emft_tree_user_event(treeview, (GdkEvent *)event, emft);

    if (event->button != 3 && !(event->button == 1 && event->type == GDK_2BUTTON_PRESS))
        return FALSE;

    if (!gtk_tree_view_get_path_at_pos (treeview, (int) event->x, (int) event->y, &tree_path, NULL, NULL, NULL))
        return FALSE;

    /* select/focus the row that was right-clicked or double-clicked */
    selection = gtk_tree_view_get_selection (treeview);
    gtk_tree_selection_select_path(selection, tree_path);
    gtk_tree_view_set_cursor (treeview, tree_path, NULL, FALSE);

    if (event->button == 1 && event->type == GDK_2BUTTON_PRESS) {
        emft_tree_row_activated (treeview, tree_path, NULL, emft);
        gtk_tree_path_free (tree_path);
        return TRUE;
    }

    gtk_tree_path_free (tree_path);

    return emft_popup (emft, (GdkEvent *)event);
}

/* This is called for keyboard and mouse events, it seems the only way
 * we know the user has done something to the selection as opposed to
 * code or initialisation processes */
static gboolean
emft_tree_user_event (GtkTreeView *treeview, GdkEvent *e, EMFolderTree *emft)
{
    if (e && e->type == GDK_KEY_PRESS && e->key.keyval == GDK_space) {
        return TRUE;
    }
    if (!emft->priv->do_multiselect)
        emft_clear_selected_list(emft);

    emft->priv->cursor_set = TRUE;

    return FALSE;
}

static void
emft_tree_selection_changed (GtkTreeSelection *selection, EMFolderTree *emft)
{
    char *full_name, *uri;
    GtkTreeModel *model;
    GtkTreeIter iter;
    guint32 flags;

    if (!emft_selection_get_selected (selection, &model, &iter)) {
        em_folder_tree_model_set_selected (emft->priv->model, NULL);
        g_signal_emit (emft, signals[FOLDER_SELECTED], 0, NULL, NULL, 0);
        emft_queue_save_state (emft);
        return;
    }

    gtk_tree_model_get (model, &iter, COL_STRING_FULL_NAME, &full_name,
                COL_STRING_URI, &uri, COL_UINT_FLAGS, &flags, -1);

    g_signal_emit (emft, signals[FOLDER_SELECTED], 0, full_name, uri, flags);
    g_free(uri);
    g_free(full_name);
}

void
em_folder_tree_set_selected (EMFolderTree *emft, const char *uri, gboolean expand_only)
{
    GList *l = NULL;

    if (uri && uri[0])
        l = g_list_append(l, (void *)uri);

    em_folder_tree_set_selected_list(emft, l, expand_only);
    g_list_free(l);
}


void
em_folder_tree_select_next_path (EMFolderTree *emft, gboolean skip_read_folders)
{
    GtkTreeView *tree_view;
    GtkTreeSelection *selection;
    GtkTreeModel *model;
    GtkTreeIter iter, parent, child;
    GtkTreePath *current_path, *path = NULL;
    unsigned int unread = 0;
    struct _EMFolderTreePrivate *priv = emft->priv;

    g_return_if_fail (EM_IS_FOLDER_TREE (emft));

    tree_view = GTK_TREE_VIEW (emft);
    selection = gtk_tree_view_get_selection(tree_view);
    if (gtk_tree_selection_get_selected(selection, &model, &iter)) {

        current_path = gtk_tree_model_get_path (model, &iter);

        do {
        if (gtk_tree_model_iter_has_child (model, &iter)) {
            gtk_tree_model_iter_children (model, &child, &iter);
            path = gtk_tree_model_get_path (model, &child);
            iter = child;
        } else {
            while (1) {
                gboolean has_parent = gtk_tree_model_iter_parent (model, &parent, &iter);
                if (gtk_tree_model_iter_next (model, &iter)) {
                    path = gtk_tree_model_get_path (model, &iter);
                    break;
                } else {
                    if (has_parent) {
                        iter =  parent;
                    } else {
                        /* Reached end. Wrapup*/
                        gtk_tree_model_get_iter_first (model, &iter);
                        path = gtk_tree_model_get_path (model, &iter);
                        break;
                    }
                }
            }
        }
        gtk_tree_model_get (model, &iter, COL_UINT_UNREAD, &unread, -1);

        /* TODO : Flags here for better options */
        } while (skip_read_folders && unread <=0 && gtk_tree_path_compare (current_path, path));
    }

    if (path) {
        if (!gtk_tree_view_row_expanded (tree_view, path))
            gtk_tree_view_expand_to_path (tree_view, path);

        gtk_tree_selection_select_path(selection, path);

        if (!priv->cursor_set) {
            gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
            priv->cursor_set = TRUE;
        }
        gtk_tree_view_scroll_to_cell (tree_view, path, NULL, TRUE, 0.5f, 0.0f);
    }
    return;
}

static GtkTreeIter
get_last_child (GtkTreeModel *model, GtkTreeIter *iter)
{
    GtkTreeIter *child = g_new0 (GtkTreeIter, 1);
    gboolean has_child = gtk_tree_model_iter_has_child (model, iter);

    if (gtk_tree_model_iter_next (model, iter)) {
        return get_last_child (model, iter);
    } else if (has_child) {
        /* Pick the last one */
        int nchildren = gtk_tree_model_iter_n_children (model, iter);
        gtk_tree_model_iter_nth_child ( model, child, iter, nchildren-1);
        return get_last_child (model, child);
    }

    return *iter;
}

void
em_folder_tree_select_prev_path (EMFolderTree *emft, gboolean skip_read_folders)
{
    GtkTreeView *tree_view;
    GtkTreeSelection *selection;
    GtkTreeModel *model;
    GtkTreeIter iter, child;
    GtkTreePath *path = NULL, *current_path = NULL;
    unsigned int unread = 0;
    struct _EMFolderTreePrivate *priv = emft->priv;

    g_return_if_fail (EM_IS_FOLDER_TREE (emft));

    tree_view = GTK_TREE_VIEW (emft);
    selection = gtk_tree_view_get_selection (tree_view);

    if (gtk_tree_selection_get_selected(selection, &model, &iter)){

        current_path = gtk_tree_model_get_path (model, &iter);
        do {
        path = gtk_tree_model_get_path (model, &iter);
        if (!gtk_tree_path_prev (path)) {
            gtk_tree_path_up (path);

            if (!gtk_tree_path_compare (gtk_tree_path_new_first (), path))
            {
                gtk_tree_model_get_iter_first (model, &iter);
                iter = get_last_child (model,&iter);
                path = gtk_tree_model_get_path (model, &iter);
            }
        } else {
            gtk_tree_model_get_iter (model, &iter, path);
            if (gtk_tree_model_iter_has_child (model, &iter)) {
                int nchildren = gtk_tree_model_iter_n_children (model, &iter);
                gtk_tree_model_iter_nth_child ( model, &child, &iter, nchildren-1);
                path = gtk_tree_model_get_path (model, &child);
            }
        }

        /* TODO : Flags here for better options */
        gtk_tree_model_get_iter_from_string (model, &iter, gtk_tree_path_to_string (path) );

        gtk_tree_model_get (model, &iter, COL_UINT_UNREAD, &unread, -1);

        } while (skip_read_folders && unread <=0 && gtk_tree_path_compare (current_path, path));
    }

    if (path) {
        if (!gtk_tree_view_row_expanded (tree_view, path)) {
            gtk_tree_view_expand_to_path (tree_view, path);
        }

        gtk_tree_selection_select_path(selection, path);

        if (!priv->cursor_set) {
            gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
            priv->cursor_set = TRUE;
        }
        gtk_tree_view_scroll_to_cell (tree_view, path, NULL, TRUE, 0.5f, 0.0f);
    }
    return;
}


char *
em_folder_tree_get_selected_uri (EMFolderTree *emft)
{
    GtkTreeView *tree_view;
    GtkTreeSelection *selection;
    GtkTreeModel *model;
    GtkTreeIter iter;
    char *uri = NULL;

    g_return_val_if_fail (EM_IS_FOLDER_TREE (emft), NULL);

    tree_view = GTK_TREE_VIEW (emft);
    selection = gtk_tree_view_get_selection (tree_view);

    if (gtk_tree_selection_get_selected(selection, &model, &iter))
        gtk_tree_model_get(model, &iter, COL_STRING_URI, &uri, -1);

    return uri;
}

char *
em_folder_tree_get_selected_path (EMFolderTree *emft)
{
    GtkTreeView *tree_view;
    GtkTreeSelection *selection;
    GtkTreeModel *model;
    GtkTreeIter iter;
    char *name = NULL;

    g_return_val_if_fail (EM_IS_FOLDER_TREE (emft), NULL);

    tree_view = GTK_TREE_VIEW (emft);
    selection = gtk_tree_view_get_selection (tree_view);

    if (gtk_tree_selection_get_selected(selection, &model, &iter))
        gtk_tree_model_get(model, &iter, COL_STRING_FULL_NAME, &name, -1);

    return name;
}

CamelFolder *
em_folder_tree_get_selected_folder (EMFolderTree *emft)
{
    GtkTreeView *tree_view;
    GtkTreeSelection *selection;
    GtkTreeModel *model;
    GtkTreeIter iter;
    char *full_name = NULL;
    CamelException ex;
    CamelStore *store = NULL;
    CamelFolder *folder = NULL;

    g_return_val_if_fail (EM_IS_FOLDER_TREE (emft), NULL);

    camel_exception_init (&ex);

    tree_view = GTK_TREE_VIEW (emft);
    selection = gtk_tree_view_get_selection (tree_view);

    if (gtk_tree_selection_get_selected(selection, &model, &iter))
        gtk_tree_model_get (model, &iter, COL_POINTER_CAMEL_STORE, &store,
                    COL_STRING_FULL_NAME, &full_name, -1);

    folder = camel_store_get_folder (store, full_name, CAMEL_STORE_FOLDER_INFO_FAST, &ex);

    camel_exception_clear (&ex);

    return folder;
}

CamelFolderInfo *
em_folder_tree_get_selected_folder_info (EMFolderTree *emft)
{
    GtkTreeView *tree_view;
    GtkTreeSelection *selection;
    GtkTreeModel *model;
    GtkTreeIter iter;
    char *full_name = NULL, *name = NULL, *uri = NULL;
    CamelException ex;
    CamelStore *store = NULL;
    CamelFolderInfo *fi = NULL;

    g_return_val_if_fail (EM_IS_FOLDER_TREE (emft), NULL);

    camel_exception_init (&ex);

    tree_view = GTK_TREE_VIEW (emft);
    selection = gtk_tree_view_get_selection (tree_view);

    if (gtk_tree_selection_get_selected(selection, &model, &iter))
        gtk_tree_model_get (model, &iter, COL_POINTER_CAMEL_STORE, &store,
                    COL_STRING_FULL_NAME, &full_name,
                    COL_STRING_DISPLAY_NAME, &name,
                    COL_STRING_URI, &uri, -1);

    fi = camel_folder_info_new ();
    fi->full_name = g_strdup (full_name);
    fi->uri = g_strdup (uri);
    fi->name = g_strdup (name);

    d(g_print ("em_folder_tree_get_selected_folder_info: fi->full_name=[%s], fi->uri=[%s], fi->name=[%s]\n",
           fi->full_name, fi->uri, fi->name));
    d(g_print ("em_folder_tree_get_selected_folder_info: full_name=[%s], uri=[%s], name=[%s]\n",
           full_name, uri, name));

    if (!fi->full_name)
        goto done;

    g_free (fi->name);
        if (!g_ascii_strcasecmp (fi->full_name, "INBOX"))
                fi->name = g_strdup (_("Inbox"));
        else
                fi->name = g_strdup (name);
 done:
    return fi;
}

EMFolderTreeModel *
em_folder_tree_get_model (EMFolderTree *emft)
{
    g_return_val_if_fail (EM_IS_FOLDER_TREE (emft), NULL);

    return emft->priv->model;
}

EMFolderTreeModelStoreInfo *
em_folder_tree_get_model_storeinfo (EMFolderTree *emft, CamelStore *store)
{
    struct _EMFolderTreePrivate *priv = emft->priv;
    struct _EMFolderTreeModelStoreInfo *si;

    if (!(si = g_hash_table_lookup (priv->model->store_hash, store))) {
        g_return_val_if_reached (NULL);
    }
    return si;
}

static gboolean
emft_save_state (EMFolderTree *emft)
{
    struct _EMFolderTreePrivate *priv = emft->priv;

    em_folder_tree_model_save_state (priv->model);
    priv->save_state_id = 0;

    return FALSE;
}


static void
emft_queue_save_state (EMFolderTree *emft)
{
    struct _EMFolderTreePrivate *priv = emft->priv;

    if (priv->save_state_id != 0)
        return;

    priv->save_state_id = g_timeout_add (1000, (GSourceFunc) emft_save_state, emft);
}