aboutsummaryrefslogtreecommitdiffstats
path: root/shell/e-storage-set-view.c
blob: 3129cdd9767d3470bc67cb58e2136e54a67f3871 (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-storage-set-view.c
 *
 * Copyright (C) 2000, 2001 Ximian, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Ettore Perazzoli
 * Etree-ification: Chris Toshok
 */

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

#include "e-storage-set-view.h"

#include <glib.h>
#include <gnome.h>
#include <libgnome/gnome-defs.h>
#include <libgnome/gnome-util.h>
#include <gal/util/e-util.h>
#include <gal/e-table/e-tree-memory-callbacks.h>
#include <gal/e-table/e-cell-tree.h>
#include <gal/e-table/e-cell-text.h>

#include <libgnome/gnome-util.h>

#include "e-util/e-gtk-utils.h"

#include "e-shell-constants.h"
#include "e-local-storage.h"

/*#define DEBUG_XML*/

#define DRAG_RESISTANCE 3   /* FIXME hardcoded in ETable to this value as
                 * well, and there is no way for us to use the
                 * same value as it's not exported.  */


/* This is used on the source side to define the two basic types that we always
   export.  */
enum _DndTargetTypeIdx {
    EVOLUTION_PATH_TARGET_TYPE_IDX = 0,
    E_SHORTCUT_TARGET_TYPE_IDX = 1
};
typedef enum _DndTargetTypeIdx DndTargetTypeIdx;

#define EVOLUTION_PATH_TARGET_TYPE "_EVOLUTION_PRIVATE_PATH"
#define E_SHORTCUT_TARGET_TYPE     "E-SHORTCUT"


#define PARENT_TYPE E_TREE_TYPE
static ETreeClass *parent_class = NULL;

struct _EStorageSetViewPrivate {
    EStorageSet *storage_set;

    BonoboUIContainer *container;

    ETreeModel *etree_model;
    ETreePath root_node;

    GHashTable *path_to_etree_node;

    GHashTable *type_name_to_pixbuf;

    /* Path of the row selected by the latest "cursor_activated" signal.  */
    const char *selected_row_path;

    gboolean show_folders;

    /* The `Evolution::ShellComponentDnd::SourceFolder' interface for the
       folder we are dragging from, or CORBA_OBJECT_NIL if no dragging is
       happening.  */
    GNOME_Evolution_ShellComponentDnd_SourceFolder drag_corba_source_interface;

    /* Source context information.  NULL if no dragging is in progress.  */
    GNOME_Evolution_ShellComponentDnd_SourceFolder_Context *drag_corba_source_context;

    /* The data.  */
    GNOME_Evolution_ShellComponentDnd_Data *drag_corba_data;

    /* When dragging, the X/Y coordinates of the point we drag from, as
       well as the corresponding row/column numbers in the table.  */
    int drag_x, drag_y;
    int drag_column, drag_row;
    ETreePath drag_path;
};


enum {
    FOLDER_SELECTED,
    STORAGE_SELECTED,
    DND_ACTION,
    LAST_SIGNAL
};

static unsigned int signals[LAST_SIGNAL] = { 0 };


/* DND stuff.  */

enum _DndTargetType {
    DND_TARGET_TYPE_URI_LIST,
    DND_TARGET_TYPE_E_SHORTCUT
};
typedef enum _DndTargetType DndTargetType;

#define URI_LIST_TYPE   "text/uri-list"
#define E_SHORTCUT_TYPE "E-SHORTCUT"


/* Sorting callbacks.  */

static int
storage_sort_callback (ETreeMemory *etmm,
               ETreePath node1,
               ETreePath node2,
               void *closure)
{
    char *folder_path1;
    char *folder_path2;
    gboolean path1_local;
    gboolean path2_local;

    folder_path1 = e_tree_memory_node_get_data(etmm, node1);
    folder_path2 = e_tree_memory_node_get_data(etmm, node2);
    
    path1_local = ! strcmp (folder_path1, G_DIR_SEPARATOR_S E_LOCAL_STORAGE_NAME);
    path2_local = ! strcmp (folder_path2, G_DIR_SEPARATOR_S E_LOCAL_STORAGE_NAME);

    if (path1_local && path2_local)
        return 0;
    if (path1_local)
        return -1;
    if (path2_local)
        return 1;
    
    return strcasecmp (e_tree_model_value_at (E_TREE_MODEL (etmm), node1, 0),
               e_tree_model_value_at (E_TREE_MODEL (etmm), node2, 0));
}

static int
folder_sort_callback (ETreeMemory *etmm, ETreePath path1, ETreePath path2, gpointer closure)
{
    return g_strcasecmp (e_tree_model_value_at (E_TREE_MODEL (etmm), path1, 0),
                 e_tree_model_value_at (E_TREE_MODEL (etmm), path2, 0));
}



/* Helper functions.  */

static gboolean
add_node_to_hash (EStorageSetView *storage_set_view,
          const char *path,
          ETreePath node)
{
    EStorageSetViewPrivate *priv;
    char *hash_path;

    g_return_val_if_fail (g_path_is_absolute (path), FALSE);

    priv = storage_set_view->priv;

    if (g_hash_table_lookup (priv->path_to_etree_node, path) != NULL) {
        g_warning ("EStorageSetView: Node already existing while adding -- %s", path);
        return FALSE;
    }

    hash_path = g_strdup (path);

    g_hash_table_insert (priv->path_to_etree_node, hash_path, node);

    return TRUE;
}

static ETreePath
lookup_node_in_hash (EStorageSetView *storage_set_view,
             const char *path)
{
    EStorageSetViewPrivate *priv;
    ETreePath node;

    priv = storage_set_view->priv;

    node = g_hash_table_lookup (priv->path_to_etree_node, path);
    if (node == NULL)
        g_warning ("EStorageSetView: Node not found while updating -- %s", path);

    return node;
}

static ETreePath
remove_node_from_hash (EStorageSetView *storage_set_view,
               const char *path)
{
    EStorageSetViewPrivate *priv;
    ETreePath node;

    priv = storage_set_view->priv;

    node = g_hash_table_lookup (priv->path_to_etree_node, path);
    if (node == NULL) {
        g_warning ("EStorageSetView: Node not found while removing -- %s", path);
        return NULL;
    }

    g_hash_table_remove (priv->path_to_etree_node, path);

    return node;
}

static GdkPixbuf*
get_pixbuf_for_folder (EStorageSetView *storage_set_view,
               EFolder *folder)
{
    GdkPixbuf *scaled_pixbuf;
    const char *type_name;
    EStorageSetViewPrivate *priv;

    priv = storage_set_view->priv;
    
    type_name = e_folder_get_type_string (folder);

    scaled_pixbuf = g_hash_table_lookup (priv->type_name_to_pixbuf, type_name);

    if (scaled_pixbuf == NULL) {
        EFolderTypeRegistry *folder_type_registry;
        EStorageSet *storage_set;
        GdkPixbuf *icon_pixbuf;
        int icon_pixbuf_width, icon_pixbuf_height;

        storage_set = priv->storage_set;
        folder_type_registry = e_storage_set_get_folder_type_registry (storage_set);

        icon_pixbuf = e_folder_type_registry_get_icon_for_type (folder_type_registry,
                                    type_name, TRUE);

        if (icon_pixbuf == NULL)
            return NULL;

        icon_pixbuf_width = gdk_pixbuf_get_width (icon_pixbuf);
        icon_pixbuf_height = gdk_pixbuf_get_height (icon_pixbuf);

        if (icon_pixbuf_width == E_SHELL_MINI_ICON_SIZE && icon_pixbuf_height == E_SHELL_MINI_ICON_SIZE) {
            scaled_pixbuf = gdk_pixbuf_ref (icon_pixbuf);
        } else {
            scaled_pixbuf = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (icon_pixbuf),
                            gdk_pixbuf_get_has_alpha (icon_pixbuf),
                            gdk_pixbuf_get_bits_per_sample (icon_pixbuf),
                            E_SHELL_MINI_ICON_SIZE, E_SHELL_MINI_ICON_SIZE);

            gdk_pixbuf_scale (icon_pixbuf, scaled_pixbuf,
                      0, 0, E_SHELL_MINI_ICON_SIZE, E_SHELL_MINI_ICON_SIZE,
                      0.0, 0.0,
                      (double) E_SHELL_MINI_ICON_SIZE / gdk_pixbuf_get_width (icon_pixbuf),
                      (double) E_SHELL_MINI_ICON_SIZE / gdk_pixbuf_get_height (icon_pixbuf),
                      GDK_INTERP_HYPER);
        }

        g_hash_table_insert (priv->type_name_to_pixbuf, g_strdup (type_name), scaled_pixbuf);
    }

    return scaled_pixbuf;
}

static EFolder *
get_folder_at_node (EStorageSetView *storage_set_view,
            ETreePath path)
{
    EStorageSetViewPrivate *priv;
    const char *folder_path;

    priv = storage_set_view->priv;

    if (path == NULL)
        return NULL;

    folder_path = e_tree_memory_node_get_data (E_TREE_MEMORY(priv->etree_model), path);
    g_assert (folder_path != NULL);

    return e_storage_set_get_folder (priv->storage_set, folder_path);
}

static EvolutionShellComponentClient *
get_component_at_node (EStorageSetView *storage_set_view,
               ETreePath path)
{
    EStorageSetViewPrivate *priv;
    EvolutionShellComponentClient *component_client;
    EFolderTypeRegistry *folder_type_registry;
    EFolder *folder;

    priv = storage_set_view->priv;

    folder = get_folder_at_node (storage_set_view, path);
    if (folder == NULL)
        return NULL;

    folder_type_registry = e_storage_set_get_folder_type_registry (priv->storage_set);
    g_assert (folder_type_registry != NULL);

    component_client = e_folder_type_registry_get_handler_for_type (folder_type_registry,
                                    e_folder_get_type_string (folder));

    return component_client;
}

static GNOME_Evolution_ShellComponentDnd_ActionSet
convert_gdk_drag_action_set_to_corba (GdkDragAction action)
{
    GNOME_Evolution_ShellComponentDnd_Action retval;

    retval = GNOME_Evolution_ShellComponentDnd_ACTION_DEFAULT;

    if (action & GDK_ACTION_COPY)
        retval |= GNOME_Evolution_ShellComponentDnd_ACTION_COPY;
    if (action & GDK_ACTION_MOVE)
        retval |= GNOME_Evolution_ShellComponentDnd_ACTION_MOVE;
    if (action & GDK_ACTION_LINK)
        retval |= GNOME_Evolution_ShellComponentDnd_ACTION_LINK;
    if (action & GDK_ACTION_ASK)
        retval |= GNOME_Evolution_ShellComponentDnd_ACTION_ASK;

    return retval;
}

#if 0
static GdkDragAction
convert_corba_drag_action_set_to_gdk (GNOME_Evolution_ShellComponentDnd_ActionSet action)
{
    GdkDragAction retval;

    retval = GDK_ACTION_DEFAULT;

    if (action & GNOME_Evolution_ShellComponentDnd_ACTION_COPY)
        retval |= GDK_ACTION_COPY;
    if (action & GNOME_Evolution_ShellComponentDnd_ACTION_MOVE)
        retval |= GDK_ACTION_MOVE;
    if (action & GNOME_Evolution_ShellComponentDnd_ACTION_LINK)
        retval |= GDK_ACTION_LINK;
    if (action & GNOME_Evolution_ShellComponentDnd_ACTION_ASK)
        retval |= GDK_ACTION_ASK;

    return retval;
}
#endif


static GNOME_Evolution_ShellComponentDnd_ActionSet
convert_gdk_drag_action_to_corba (GdkDragAction action)
{
    if (action == GDK_ACTION_COPY)
        return GNOME_Evolution_ShellComponentDnd_ACTION_COPY;
    else if (action == GDK_ACTION_MOVE)
        return GNOME_Evolution_ShellComponentDnd_ACTION_MOVE;
    else if (action == GDK_ACTION_LINK)
        return GNOME_Evolution_ShellComponentDnd_ACTION_LINK;
    else if (action == GDK_ACTION_ASK)
        return GNOME_Evolution_ShellComponentDnd_ACTION_ASK;
    else {
        g_warning ("unknown GdkDragAction %d\n", action);
        return GNOME_Evolution_ShellComponentDnd_ACTION_DEFAULT;
    }
}

static GdkDragAction
convert_corba_drag_action_to_gdk (GNOME_Evolution_ShellComponentDnd_ActionSet action)
{
    if (action == GNOME_Evolution_ShellComponentDnd_ACTION_COPY)
        return GDK_ACTION_COPY;
    else if (action == GNOME_Evolution_ShellComponentDnd_ACTION_MOVE)
        return GDK_ACTION_MOVE;
    else if (action == GNOME_Evolution_ShellComponentDnd_ACTION_LINK)
        return GDK_ACTION_LINK;
    else if (action == GNOME_Evolution_ShellComponentDnd_ACTION_ASK)
        return GDK_ACTION_ASK;
    else {
        g_warning ("unknown GNOME_Evolution_ShellComponentDnd_ActionSet %d\n", action);
        return GDK_ACTION_DEFAULT;
    }
}

/* This will look for the targets in @drag_context, choose one that matches
   with the allowed types at @path, and return its name.  The EVOLUTION_PATH
   type always matches.  */
static const char *
find_matching_target_for_drag_context (EStorageSetView *storage_set_view,
                       ETreePath path,
                       GdkDragContext *drag_context)
{
    EStorageSetViewPrivate *priv;
    EFolderTypeRegistry *folder_type_registry;
    EFolder *folder;
    GList *accepted_types;
    GList *p, *q;

    priv = storage_set_view->priv;
    folder_type_registry = e_storage_set_get_folder_type_registry (priv->storage_set);

    folder = get_folder_at_node (storage_set_view, path);
    if (folder == NULL)
        return NULL;

    accepted_types = e_folder_type_registry_get_accepted_dnd_types_for_type (folder_type_registry,
                                         e_folder_get_type_string (folder));

    /* FIXME?  We might make this more efficient.  Currently it takes `n *
       m' string compares, where `n' is the number of targets in the
       @drag_context, and `m' is the number of supported types in
       @folder.  */

    for (p = drag_context->targets; p != NULL; p = p->next) {
        char *possible_type;

        possible_type = gdk_atom_name ((GdkAtom) p->data);
        if (strcmp (possible_type, EVOLUTION_PATH_TARGET_TYPE) == 0) {
            g_free (possible_type);
            return EVOLUTION_PATH_TARGET_TYPE;
        }

        for (q = accepted_types; q != NULL; q = q->next) {
            const char *accepted_type;

            accepted_type = (const char *) q->data;
            if (strcmp (possible_type, accepted_type) == 0) {
                g_free (possible_type);
                return accepted_type;
            }
        }

        g_free (possible_type);
    }

    return NULL;
}


/* Custom marshalling function.  */

typedef void (* GtkSignal_NONE__GDKDRAGCONTEXT_STRING_STRING_STRING) (GtkObject *object,
                                      GdkDragContext *action,
                                      const char *,
                                      const char *,
                                      const char *);

static void
marshal_NONE__GDKDRAGCONTEXT_STRING_STRING_STRING (GtkObject *object,
                           GtkSignalFunc func,
                           void *func_data,
                           GtkArg *args)
{
    GtkSignal_NONE__GDKDRAGCONTEXT_STRING_STRING_STRING rfunc;

    rfunc = (GtkSignal_NONE__GDKDRAGCONTEXT_STRING_STRING_STRING) func;
    (* rfunc) (object,
           GTK_VALUE_POINTER (args[0]),
           GTK_VALUE_STRING (args[1]),
           GTK_VALUE_STRING (args[2]),
           GTK_VALUE_STRING (args[3]));
}


/* DnD selection setup stuff.  */

/* This will create an array of GtkTargetEntries from the specified list of DND
   types.  The type name will *not* be allocated in the list, as this is
   supposed to be used only temporarily to set up the cell as a drag source.  */
static GtkTargetEntry *
create_target_entries_from_dnd_type_list (GList *dnd_types,
                      int *num_entries_return)
{
    GtkTargetEntry *entries;
    GList *p;
    int num_entries;
    int i;

    if (dnd_types == NULL)
        num_entries = 0;
    else
        num_entries = g_list_length (dnd_types);

    /* We always add two entries, one for an Evolution URI type, and one
       for e-shortcuts.  This will let us do drag & drop within Evolution
       at least.  */
    num_entries += 2;

    entries = g_new (GtkTargetEntry, num_entries);

    i = 0;

    /* The Evolution URI will always come first.  */
    entries[i].target = EVOLUTION_PATH_TARGET_TYPE;
    entries[i].flags = 0;
    entries[i].info = i;
    g_assert (i == EVOLUTION_PATH_TARGET_TYPE_IDX);
    i ++;

    /* ...Then the shortcut type.  */
    entries[i].target = E_SHORTCUT_TARGET_TYPE;
    entries[i].flags = 0;
    entries[i].info = i;
    g_assert (i == E_SHORTCUT_TARGET_TYPE_IDX);
    i ++;

    for (p = dnd_types; p != NULL; p = p->next, i++) {
        const char *dnd_type;

        g_assert (i < num_entries);

        dnd_type = (const char *) p->data;

        entries[i].target = (char *) dnd_type;
        entries[i].flags  = 0;
        entries[i].info   = i;
    }

    *num_entries_return = num_entries;
    return entries;
}

static void
free_target_entries (GtkTargetEntry *entries)
{
    g_assert (entries != NULL);

    /* The target names are not strdup()ed so a simple free will do.  */
    g_free (entries);
}

static GtkTargetList *
create_target_list_for_node (EStorageSetView *storage_set_view,
                 ETreePath node)
{
    EStorageSetViewPrivate *priv;
    GtkTargetList *target_list;
    EFolderTypeRegistry *folder_type_registry;
    GList *exported_dnd_types;
    GtkTargetEntry *target_entries;
    EFolder *folder;
    const char *folder_type;
    int num_target_entries;

    priv = storage_set_view->priv;

    folder_type_registry = e_storage_set_get_folder_type_registry (priv->storage_set);

    folder = get_folder_at_node (storage_set_view, node);
    folder_type = e_folder_get_type_string (folder);

    exported_dnd_types = e_folder_type_registry_get_exported_dnd_types_for_type (folder_type_registry,
                                             folder_type);

    target_entries = create_target_entries_from_dnd_type_list (exported_dnd_types,
                                   &num_target_entries);
    g_assert (target_entries != NULL);

    target_list = gtk_target_list_new (target_entries, num_target_entries);

    free_target_entries (target_entries);

    return target_list;
}

static void
set_e_shortcut_selection (EStorageSetView *storage_set_view,
              GtkSelectionData *selection_data)
{
    EStorageSetViewPrivate *priv;
    int shortcut_len;
    char *shortcut;
    const char *trailing_slash;
    const char *name;

    g_assert (storage_set_view != NULL);
    g_assert (selection_data != NULL);

    priv = storage_set_view->priv;

    trailing_slash = strrchr (priv->selected_row_path, '/');
    if (trailing_slash == NULL)
        name = NULL;
    else
        name = trailing_slash + 1;

    /* FIXME: Get `evolution:' from somewhere instead of hardcoding it here.  */

    if (name != NULL)
        shortcut_len = strlen (name);
    else
        shortcut_len = 0;
    
    shortcut_len ++;    /* Separating zero.  */

    shortcut_len += strlen ("evolution:");
    shortcut_len += strlen (priv->selected_row_path);
    shortcut_len ++;    /* Trailing zero.  */

    shortcut = g_malloc (shortcut_len);

    if (name == NULL)
        sprintf (shortcut, "%cevolution:%s", '\0', priv->selected_row_path);
    else
        sprintf (shortcut, "%s%cevolution:%s", name, '\0', priv->selected_row_path);

    gtk_selection_data_set (selection_data, selection_data->target,
                8, (guchar *) shortcut, shortcut_len);

    g_free (shortcut);
}

static void
set_evolution_path_selection (EStorageSetView *storage_set_view,
                  GtkSelectionData *selection_data)
{
    EStorageSetViewPrivate *priv;

    g_assert (storage_set_view != NULL);
    g_assert (selection_data != NULL);

    priv = storage_set_view->priv;

    gtk_selection_data_set (selection_data, selection_data->target,
                8, (guchar *) priv->selected_row_path, strlen (priv->selected_row_path) + 1);
}


/* Callbacks for folder operations.  */

static void
folder_xfer_callback (EStorageSet *storage_set,
              EStorageResult result,
              void *data)
{
    g_print ("Folder Xfer result -- %s\n", e_storage_result_to_string (result));
}


/* Folder context menu.  */
/* FIXME: This should be moved somewhere else, so that also the shortcut code
   can share it.  */

#if 0
static void
folder_context_menu_activate_cb (BonoboUIComponent *uih,
                 void *data,
                 const char *path)
{
    EStorageSetView *storage_set_view;
    EStorageSetViewPrivate *priv;

    storage_set_view = E_STORAGE_SET_VIEW (data);
    priv = storage_set_view->priv;

    gtk_signal_emit (GTK_OBJECT (storage_set_view), signals[FOLDER_SELECTED],
             priv->selected_row_path);
}

static void
remove_cb(EStorageSet *storage_set, EStorageResult result, void *data)
{
    g_print ("remove_cb: %d\n", result);
}

static void
folder_context_menu_remove_cb (BonoboUIComponent *uih,
                   void *data,
                   const char *path)
{
    EStorageSetView *storage_set_view;
    EStorageSetViewPrivate *priv;

    storage_set_view = E_STORAGE_SET_VIEW (data);
    priv = storage_set_view->priv;

    e_storage_set_async_remove_folder (priv->storage_set, priv->selected_row_path,
                       remove_cb, storage_set_view);
}
#endif

static void
popup_folder_menu (EStorageSetView *storage_set_view,
           GdkEventButton *event)
{
    EvolutionShellComponentClient *handler;
    EStorageSetViewPrivate *priv;
    EFolderTypeRegistry *folder_type_registry;
    EFolder *folder;
    GtkWidget *menu;

    priv = storage_set_view->priv;

    folder = e_storage_set_get_folder (priv->storage_set, priv->selected_row_path);
    if (folder == NULL) {
        /* Uh!?  */
        return;
    }

    folder_type_registry = e_storage_set_get_folder_type_registry (priv->storage_set);
    g_assert (folder_type_registry != NULL);

    handler = e_folder_type_registry_get_handler_for_type (folder_type_registry,
                                   e_folder_get_type_string (folder));
    g_assert (handler != NULL);

    menu = gtk_menu_new ();
    bonobo_window_add_popup (bonobo_ui_container_get_win (priv->container),
                 GTK_MENU (menu), "/popups/FolderPopup");

    evolution_shell_component_client_populate_folder_context_menu (handler,
                                       priv->container,
                                       e_folder_get_physical_uri (folder),
                                       e_folder_get_type_string (folder));

    gtk_widget_show (GTK_WIDGET (menu));
    gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL, 3, 0);
}


/* GtkObject methods.  */

static void
pixbuf_free_func (gpointer key, gpointer value, gpointer user_data)
{
    g_free (key);
    gdk_pixbuf_unref ((GdkPixbuf*)value);
}

static void
destroy (GtkObject *object)
{
    EStorageSetView *storage_set_view;
    EStorageSetViewPrivate *priv;

    storage_set_view = E_STORAGE_SET_VIEW (object);
    priv = storage_set_view->priv;

    /* need to destroy our tree */
    e_tree_memory_node_remove (E_TREE_MEMORY(priv->etree_model), priv->root_node);
    gtk_object_unref (GTK_OBJECT (priv->etree_model));

    /* the data in the hash table was all freed by freeing the tree */
    g_hash_table_destroy (priv->path_to_etree_node);

    /* now free up all the type_names and pixbufs stored in the
           hash table and destroy the hash table itself */
    g_hash_table_foreach (priv->type_name_to_pixbuf, pixbuf_free_func, NULL);
    g_hash_table_destroy (priv->type_name_to_pixbuf);

    gtk_object_unref (GTK_OBJECT (priv->storage_set));

    if (priv->drag_corba_source_interface != CORBA_OBJECT_NIL) {
        CORBA_Environment ev;

        CORBA_exception_init (&ev);

        g_assert (priv->drag_corba_source_context != NULL);

        GNOME_Evolution_ShellComponentDnd_SourceFolder_endDrag (priv->drag_corba_source_interface,
                                    priv->drag_corba_source_context,
                                    &ev);

        Bonobo_Unknown_unref (priv->drag_corba_source_interface, &ev);
        CORBA_Object_release (priv->drag_corba_source_interface, &ev);

        CORBA_exception_free (&ev);
    }

    if (priv->drag_corba_source_context != NULL)
        CORBA_free (priv->drag_corba_source_context);

    if (priv->drag_corba_data != NULL)
        CORBA_free (priv->drag_corba_data);

    g_free (priv);

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


/* GtkWidget methods.  */

static int
button_press_event (GtkWidget *widget,
            GdkEventButton *event)
{
    EStorageSetView *storage_set_view;
    EStorageSetViewPrivate *priv;
    ETree *tree;
    int row, column;

    storage_set_view = E_STORAGE_SET_VIEW (widget);
    priv = storage_set_view->priv;

    tree = E_TREE (widget);

    /* FIXME correct? */
    if (GTK_WIDGET_CLASS (parent_class)->button_press_event != NULL)
        (* GTK_WIDGET_CLASS (parent_class)->button_press_event) (widget, event);

    if (event->button != 1)
        return FALSE;

    e_tree_get_cell_at (tree, event->x, event->y, &row, &column);

    priv->drag_x = event->x;
    priv->drag_y = event->y;
    priv->drag_column = column;
    priv->drag_path = e_tree_node_at_row(E_TREE(storage_set_view), row);
    priv->drag_row = row;

    /* FIXME correct? */
    return TRUE;
}

static int
motion_notify_event (GtkWidget *widget,
             GdkEventMotion *event)
{
    EStorageSetView *storage_set_view;
    EStorageSetViewPrivate *priv;
    ETree *tree;
    GtkTargetList *target_list;
    GdkDragAction actions;
    GdkDragContext *context;

    storage_set_view = E_STORAGE_SET_VIEW (widget);
    priv = storage_set_view->priv;

    tree = E_TREE (widget);

    /* FIXME correct? */
    if (GTK_WIDGET_CLASS (parent_class)->motion_notify_event != NULL)
        (* GTK_WIDGET_CLASS (parent_class)->motion_notify_event) (widget, event);

    /* FIXME correct? */
    if (! (event->state & GDK_BUTTON1_MASK))
        return FALSE;

    if (ABS (priv->drag_x - event->x) < DRAG_RESISTANCE
        && ABS (priv->drag_y - event->y) < DRAG_RESISTANCE)
        return FALSE;

    target_list = create_target_list_for_node (storage_set_view, priv->drag_path);
    if (target_list == NULL)
        return FALSE;

    actions = GDK_ACTION_MOVE | GDK_ACTION_COPY;

    context = e_tree_drag_begin (tree,
                      priv->drag_row, priv->drag_column,
                      target_list, actions,
                      1, (GdkEvent *) event);
    gtk_drag_set_icon_default (context);

    gtk_target_list_unref (target_list);

    return FALSE;
}


/* ETree methods.  */

/* -- Source-side DnD.  */

static void
tree_drag_begin (ETree *etree,
         int row, ETreePath path, int col,
         GdkDragContext *context)
{
    EStorageSetView *storage_set_view;
    EStorageSetViewPrivate *priv;
    EFolder *folder;
    EvolutionShellComponentClient *component_client;
    GNOME_Evolution_ShellComponent corba_component;
    GNOME_Evolution_ShellComponentDnd_ActionSet possible_actions;
    GNOME_Evolution_ShellComponentDnd_Action suggested_action;
    CORBA_Environment ev;

    storage_set_view = E_STORAGE_SET_VIEW (etree);
    priv = storage_set_view->priv;

    priv->selected_row_path = e_tree_memory_node_get_data (E_TREE_MEMORY(priv->etree_model), path);

    g_assert (priv->drag_corba_source_interface == CORBA_OBJECT_NIL);

    folder = get_folder_at_node (storage_set_view, path);
    component_client = get_component_at_node (storage_set_view, path);

    g_assert (component_client != NULL);

    /* Query the `ShellComponentDnd::SourceFolder' interface on the
       component.  */
    /* FIXME we could use the new
       `evolution_shell_component_client_get_dnd_source_interface()'
       call. */

    CORBA_exception_init (&ev);

    corba_component = bonobo_object_corba_objref (BONOBO_OBJECT (component_client));
    priv->drag_corba_source_interface = Bonobo_Unknown_queryInterface (corba_component,
                                       "IDL:GNOME/Evolution/ShellComponentDnd/SourceFolder:1.0",
                                       &ev);
    if (ev._major != CORBA_NO_EXCEPTION
        || priv->drag_corba_source_interface == CORBA_OBJECT_NIL) {
        priv->drag_corba_source_interface = CORBA_OBJECT_NIL;

        CORBA_exception_free (&ev);
        return;
    }

    GNOME_Evolution_ShellComponentDnd_SourceFolder_beginDrag (priv->drag_corba_source_interface,
                                  e_folder_get_physical_uri (folder),
                                  e_folder_get_type_string (folder),
                                  &possible_actions,
                                  &suggested_action,
                                  &ev);

    if (ev._major != CORBA_NO_EXCEPTION) {
        Bonobo_Unknown_unref (priv->drag_corba_source_interface, &ev);
        CORBA_Object_release (priv->drag_corba_source_interface, &ev);

        priv->drag_corba_source_interface = CORBA_OBJECT_NIL;

        CORBA_exception_free (&ev);
        return;
    }

    CORBA_exception_free (&ev);

    if (priv->drag_corba_source_context != NULL)
        CORBA_free (priv->drag_corba_source_context);

    priv->drag_corba_source_context = GNOME_Evolution_ShellComponentDnd_SourceFolder_Context__alloc ();
    priv->drag_corba_source_context->physicalUri     = CORBA_string_dup (e_folder_get_physical_uri (folder));
    priv->drag_corba_source_context->folderType      = CORBA_string_dup (e_folder_get_type_string (folder));
    priv->drag_corba_source_context->possibleActions = possible_actions;
    priv->drag_corba_source_context->suggestedAction = suggested_action;
}

static void
tree_drag_end (ETree *tree,
           int row,
           ETreePath path,
           int col,
           GdkDragContext *context)
{
    EStorageSetView *storage_set_view;
    EStorageSetViewPrivate *priv;
    CORBA_Environment ev;

    storage_set_view = E_STORAGE_SET_VIEW (tree);
    priv = storage_set_view->priv;

    if (priv->drag_corba_source_interface == CORBA_OBJECT_NIL)
        return;

    CORBA_exception_init (&ev);

    GNOME_Evolution_ShellComponentDnd_SourceFolder_endDrag (priv->drag_corba_source_interface,
                                priv->drag_corba_source_context,
                                &ev);

    CORBA_free (priv->drag_corba_source_context);
    priv->drag_corba_source_context = NULL;

    Bonobo_Unknown_unref (priv->drag_corba_source_interface, &ev);
    CORBA_Object_release (priv->drag_corba_source_interface, &ev);

    CORBA_exception_free (&ev);
}

static void
tree_drag_data_get (ETree *etree,
            int drag_row,
            ETreePath drag_path,
            int drag_col,
            GdkDragContext *context,
            GtkSelectionData *selection_data,
            unsigned int info,
            guint32 time)
{
    EStorageSetView *storage_set_view;
    EStorageSetViewPrivate *priv;
    CORBA_Environment ev;
    char *target_type;

    storage_set_view = E_STORAGE_SET_VIEW (etree);
    priv = storage_set_view->priv;

    switch (info) {
    case E_SHORTCUT_TARGET_TYPE_IDX:
        set_e_shortcut_selection (storage_set_view, selection_data);
        return;
    case EVOLUTION_PATH_TARGET_TYPE_IDX:
        set_evolution_path_selection (storage_set_view, selection_data);
        return;
    }

    g_assert (info > 0);

    if (priv->drag_corba_source_interface == CORBA_OBJECT_NIL)
        return;

    target_type = gdk_atom_name ((GdkAtom) context->targets->data);

    CORBA_exception_init (&ev);

    GNOME_Evolution_ShellComponentDnd_SourceFolder_getData (priv->drag_corba_source_interface,
                                priv->drag_corba_source_context,
                                convert_gdk_drag_action_set_to_corba (context->action),
                                target_type,
                                & priv->drag_corba_data,
                                &ev);

    if (ev._major != CORBA_NO_EXCEPTION)
        gtk_selection_data_set (selection_data, selection_data->target, 8, "", -1);
    else
        gtk_selection_data_set (selection_data,
                    priv->drag_corba_data->target,
                    priv->drag_corba_data->format,
                    priv->drag_corba_data->bytes._buffer,
                    priv->drag_corba_data->bytes._length);

    g_free (target_type);

    CORBA_exception_free (&ev);
}

static void
tree_drag_data_delete (ETree *tree,
               int row,
               ETreePath path,
               int col,
               GdkDragContext *context)
{
    EStorageSetView *storage_set_view;
    EStorageSetViewPrivate *priv;
    CORBA_Environment ev;

    storage_set_view = E_STORAGE_SET_VIEW (tree);
    priv = storage_set_view->priv;

    if (priv->drag_corba_source_interface == CORBA_OBJECT_NIL)
        return;

    CORBA_exception_init (&ev);

    GNOME_Evolution_ShellComponentDnd_SourceFolder_deleteData (priv->drag_corba_source_interface,
                                   priv->drag_corba_source_context,
                                   &ev);

    CORBA_exception_free (&ev);
}

/* -- Destination-side DnD.  */

static gboolean
handle_evolution_path_drag_motion (EStorageSetView *storage_set_view,
                   ETreePath path,
                   GdkDragContext *context,
                   unsigned int time)
{
    GdkModifierType modifiers;
    GdkDragAction action;

    gdk_window_get_pointer (NULL, NULL, NULL, &modifiers);

    if ((modifiers & GDK_CONTROL_MASK) != 0)
        action = GDK_ACTION_COPY;
    else
        action = GDK_ACTION_MOVE;

    gdk_drag_status (context, action, time);

    return TRUE;
}

static gboolean
tree_drag_motion (ETree *tree,
          int row,
          ETreePath path,
          int col,
          GdkDragContext *context,
          int x,
          int y,
          unsigned int time)
{
    EStorageSetView *storage_set_view;
    EFolder *folder;
    EStorageSetViewPrivate *priv;
    EvolutionShellComponentClient *component_client;
    GNOME_Evolution_ShellComponentDnd_DestinationFolder destination_folder_interface;
    GNOME_Evolution_ShellComponentDnd_Action suggested_action;
    GNOME_Evolution_ShellComponentDnd_DestinationFolder_Context corba_context;
    CORBA_boolean can_handle;
    CORBA_Environment ev;
    const char *dnd_type;

    storage_set_view = E_STORAGE_SET_VIEW (tree);
    priv = storage_set_view->priv;

    component_client = get_component_at_node (storage_set_view, path);
    if (component_client == NULL)
        return FALSE;

    dnd_type = find_matching_target_for_drag_context (storage_set_view, path, context);
    if (dnd_type == NULL)
        return FALSE;

    g_print ("drag_motion %s\n", dnd_type);

    if (strcmp (dnd_type, EVOLUTION_PATH_TARGET_TYPE) == 0)
        return handle_evolution_path_drag_motion (storage_set_view, path, context, time);

    destination_folder_interface = evolution_shell_component_client_get_dnd_destination_interface (component_client);
    if (destination_folder_interface == NULL)
        return FALSE;

    CORBA_exception_init (&ev);

    corba_context.dndType = (char *) dnd_type; /* (Safe cast, as we don't actually free the corba_context.)  */
    corba_context.possibleActions = convert_gdk_drag_action_set_to_corba (context->actions);
    corba_context.suggestedAction = convert_gdk_drag_action_to_corba (context->suggested_action);

    folder = get_folder_at_node (storage_set_view, path);
    
    can_handle = GNOME_Evolution_ShellComponentDnd_DestinationFolder_handleMotion (destination_folder_interface,
                                               e_folder_get_physical_uri (folder),
                                               &corba_context,
                                               &suggested_action,
                                               &ev);
    if (ev._major != CORBA_NO_EXCEPTION || ! can_handle) {
        CORBA_exception_free (&ev);
        return FALSE;
    }

    CORBA_exception_free (&ev);

    gdk_drag_status (context, convert_corba_drag_action_to_gdk (suggested_action), time);

    return TRUE;
}

static gboolean
tree_drag_drop (ETree *etree,
        int row,
        ETreePath path,
        int col,
        GdkDragContext *context,
        int x,
        int y,
        unsigned int time)
{
    if (context->targets != NULL) {
        gtk_drag_get_data (GTK_WIDGET (etree), context,
                   GPOINTER_TO_INT (context->targets->data),
                   time);
        return TRUE;
    }

    return FALSE;
}

static void
tree_drag_data_received (ETree *etree,
             int row,
             ETreePath path,
             int col,
             GdkDragContext *context,
             int x,
             int y,
             GtkSelectionData *selection_data,
             unsigned int info,
             unsigned int time)
{
    EStorageSetView *storage_set_view;
    EStorageSetViewPrivate *priv;
    char *target_type;

    storage_set_view = E_STORAGE_SET_VIEW (etree);
    priv = storage_set_view->priv;

    target_type = gdk_atom_name (selection_data->target);

    if (strcmp (target_type, EVOLUTION_PATH_TARGET_TYPE) == 0) {
        const char *source_path;
        const char *destination_folder_path;
        char *destination_path;

        source_path = (const char *) selection_data->data;
        /* (Basic sanity checks.)  */
        if (source_path == NULL || source_path[0] != G_DIR_SEPARATOR || source_path[1] == '\0')
            return;

        destination_folder_path = e_tree_memory_node_get_data (E_TREE_MEMORY (priv->etree_model), path);
        if (destination_folder_path == NULL)
            return;

        destination_path = g_concat_dir_and_file (destination_folder_path,
                              g_basename (source_path));


        switch (context->action) {
        case GDK_ACTION_MOVE:
            g_print ("EStorageSetView: Moving from `%s' to `%s'\n", source_path, destination_path);
            e_storage_set_async_xfer_folder (priv->storage_set, source_path, destination_path, TRUE,
                             folder_xfer_callback, NULL);
            break;
        case GDK_ACTION_COPY:
            g_print ("EStorageSetView: Copying from `%s' to `%s'\n", source_path, destination_path);
            e_storage_set_async_xfer_folder (priv->storage_set, source_path, destination_path, FALSE,
                             folder_xfer_callback, NULL);
            break;
        default:
            g_warning ("EStorageSetView: Don't know action %d\n", context->action);
        }

        g_free (destination_path);
    }
    else {
        GNOME_Evolution_ShellComponentDnd_DestinationFolder destination_folder_interface;
        GNOME_Evolution_ShellComponentDnd_DestinationFolder_Context corba_context;
        GNOME_Evolution_ShellComponentDnd_Data corba_data;
        EvolutionShellComponentClient *component_client;
        const char *target_path;

        target_path = e_tree_memory_node_get_data (E_TREE_MEMORY(priv->etree_model), path);

        component_client = get_component_at_node (storage_set_view, path);
        if (component_client != NULL) {
            destination_folder_interface = evolution_shell_component_client_get_dnd_destination_interface (component_client);
            if (destination_folder_interface != NULL) {
                EFolder *folder;
                CORBA_Environment ev;
                CORBA_boolean handled;

                CORBA_exception_init (&ev);


                folder = get_folder_at_node (storage_set_view, path);
    
                corba_context.dndType = (char *) target_type;
                corba_context.possibleActions = convert_gdk_drag_action_set_to_corba (context->actions);
                corba_context.suggestedAction = convert_gdk_drag_action_to_corba (context->suggested_action);

                corba_data.format = selection_data->format;
                corba_data.target = selection_data->target;

                corba_data.bytes._release = FALSE;
                corba_data.bytes._length = selection_data->length;
                corba_data.bytes._buffer = selection_data->data;

                /* pass off the data to the component's DestinationFolderInterface */
                handled = GNOME_Evolution_ShellComponentDnd_DestinationFolder_handleDrop (destination_folder_interface,
                                                      e_folder_get_physical_uri (folder),
                                                      &corba_context,
                                                      convert_gdk_drag_action_to_corba (context->action),
                                                      &corba_data,
                                                      &ev);
            }
        }
    }

    g_free (target_type);
}

static gboolean
right_click (ETree *etree,
         int row,
         ETreePath path,
         int col,
         GdkEvent *event)
{
    EStorageSetView *storage_set_view;
    EStorageSetViewPrivate *priv;

    storage_set_view = E_STORAGE_SET_VIEW (etree);
    priv = storage_set_view->priv;

    if (priv->container)
        popup_folder_menu (storage_set_view, (GdkEventButton *) event);

    return TRUE;
}

static void
cursor_activated (ETree *tree,
          int row,
          ETreePath path)
{
    EStorageSetView *storage_set_view;
    EStorageSetViewPrivate *priv;

    storage_set_view = E_STORAGE_SET_VIEW (tree);

    priv = storage_set_view->priv;

    priv->selected_row_path = e_tree_memory_node_get_data (E_TREE_MEMORY (priv->etree_model), path);

    if (e_tree_model_node_depth (priv->etree_model, path) >= 2) {
        /* it was a folder */
        gtk_signal_emit (GTK_OBJECT (storage_set_view), signals[FOLDER_SELECTED],
                 priv->selected_row_path);
    } else {
        /* it was a storage */
        gtk_signal_emit (GTK_OBJECT (storage_set_view), signals[STORAGE_SELECTED],
                 priv->selected_row_path + 1);
    }
}


/* ETreeModel Methods */

static GdkPixbuf*
etree_icon_at (ETreeModel *etree,
           ETreePath tree_path,
           void *model_data)
{
    EStorageSetView *storage_set_view;
    EStorageSet *storage_set;
    EFolder *folder;
    char *path;

    /* folders are from depth 2 on.  depth 1 are storages and 0 is
           our (invisible) root node. */
    if (e_tree_model_node_depth (etree, tree_path) < 2)
        return NULL;

    storage_set_view = E_STORAGE_SET_VIEW (model_data);
    storage_set = storage_set_view->priv->storage_set;

    path = (char*)e_tree_memory_node_get_data (E_TREE_MEMORY(etree), tree_path);

    folder = e_storage_set_get_folder (storage_set, path);
    if (folder == NULL)
        return NULL;

    return get_pixbuf_for_folder (storage_set_view, folder);
}

/* This function returns the number of columns in our ETreeModel. */
static int
etree_column_count (ETreeModel *etc, void *data)
{
    return 2;
}

static gboolean
etree_has_save_id (ETreeModel *etm, void *data)
{
    return TRUE;
}

static gchar *
etree_get_save_id (ETreeModel *etm, ETreePath node, void *model_data)
{
    return g_strdup(e_tree_memory_node_get_data (E_TREE_MEMORY(etm), node));
}

static gboolean
etree_has_get_node_by_id (ETreeModel *etm, void *data)
{
    return TRUE;
}

static ETreePath
etree_get_node_by_id (ETreeModel *etm, gchar *save_id, void *model_data)
{
    EStorageSetView *storage_set_view;
    storage_set_view = E_STORAGE_SET_VIEW (model_data);

    return g_hash_table_lookup (storage_set_view->priv->path_to_etree_node, save_id);
}

static void *
etree_value_at (ETreeModel *etree, ETreePath tree_path, int col, void *model_data)
{
    EStorageSetView *storage_set_view;
    EStorageSet *storage_set;
    EStorage *storage;
    EFolder *folder;
    char *path;

    storage_set_view = E_STORAGE_SET_VIEW (model_data);
    storage_set = storage_set_view->priv->storage_set;

    path = (char *) e_tree_memory_node_get_data (E_TREE_MEMORY(etree), tree_path);

    folder = e_storage_set_get_folder (storage_set, path);
    if (folder != NULL) {
        if (col == 0)
            return (void *) e_folder_get_name (folder);
        else
            return (void *) e_folder_get_highlighted (folder);
    }

    storage = e_storage_set_get_storage (storage_set, path + 1);
    if (storage != NULL && col == 0)
        return (void *) e_storage_get_name (storage);

    return NULL;
}

static void
etree_set_value_at (ETreeModel *etree, ETreePath path, int col, const void *val, void *model_data)
{
    /* nada */
}

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


/* This function duplicates the value passed to it. */
static void *
etree_duplicate_value (ETreeModel *etc, int col, const void *value, void *data)
{
    if (col == 0)
        return g_strdup (value);
    else
        return (void *)value;
}

/* This function frees the value passed to it. */
static void
etree_free_value (ETreeModel *etc, int col, void *value, void *data)
{
    if (col == 0)
        g_free (value);
}

/* This function creates an empty value. */
static void *
etree_initialize_value (ETreeModel *etc, int col, void *data)
{
    if (col == 0)
        return g_strdup ("");
    else
        return NULL;
}

/* This function reports if a value is empty. */
static gboolean
etree_value_is_empty (ETreeModel *etc, int col, const void *value, void *data)
{
    if (col == 0)
        return !(value && *(char *)value);
    else
        return !value;
}

/* This function reports if a value is empty. */
static char *
etree_value_to_string (ETreeModel *etc, int col, const void *value, void *data)
{
    if (col == 0)
        return g_strdup(value);
    else
        return g_strdup(value ? "Yes" : "No");
}


/* StorageSet signal handling.  */

static void
new_storage_cb (EStorageSet *storage_set,
        EStorage *storage,
        void *data)
{
    EStorageSetView *storage_set_view;
    EStorageSetViewPrivate *priv;
    ETreePath node;
    char *path;

    storage_set_view = E_STORAGE_SET_VIEW (data);
    priv = storage_set_view->priv;

    path = g_strconcat (G_DIR_SEPARATOR_S, e_storage_get_name (storage), NULL);

    node = e_tree_memory_node_insert (E_TREE_MEMORY(priv->etree_model),
                      priv->root_node,
                      -1, path);
    e_tree_memory_sort_node (E_TREE_MEMORY(priv->etree_model), priv->root_node, storage_sort_callback, storage_set_view);

    if (! add_node_to_hash (storage_set_view, path, node)) {
        e_tree_memory_node_remove (E_TREE_MEMORY(priv->etree_model), node);
        return;
    }
}

static void
removed_storage_cb (EStorageSet *storage_set,
            EStorage *storage,
            void *data)
{
    EStorageSetView *storage_set_view;
    EStorageSetViewPrivate *priv;
    ETreeModel *etree;
    ETreePath node;
    char *path;

    storage_set_view = E_STORAGE_SET_VIEW (data);
    priv = storage_set_view->priv;
    etree = priv->etree_model;

    path = g_strconcat (G_DIR_SEPARATOR_S, e_storage_get_name (storage), NULL);
    node = remove_node_from_hash (storage_set_view, path);
    g_free (path);

    e_tree_memory_node_remove (E_TREE_MEMORY(etree), node);
}

static void
new_folder_cb (EStorageSet *storage_set,
           const char *path,
           void *data)
{
    EStorageSetView *storage_set_view;
    EStorageSetViewPrivate *priv;
    ETreeModel *etree;
    ETreePath parent_node;
    ETreePath new_node;
    const char *last_separator;
    char *parent_path;
    char *copy_of_path;

    g_return_if_fail (g_path_is_absolute (path));

    storage_set_view = E_STORAGE_SET_VIEW (data);
    priv = storage_set_view->priv;
    etree = priv->etree_model;

    last_separator = strrchr (path, G_DIR_SEPARATOR);

    parent_path = g_strndup (path, last_separator - path);
    parent_node = g_hash_table_lookup (priv->path_to_etree_node, parent_path);
    if (parent_node == NULL) {
        g_warning ("EStorageSetView: EStorageSet reported new subfolder for non-existing folder -- %s\n",
               parent_path);
        g_free (parent_path);
        return;
    }

    g_free (parent_path);

    copy_of_path = g_strdup (path);
    new_node = e_tree_memory_node_insert (E_TREE_MEMORY(etree), parent_node, -1, copy_of_path);
    e_tree_memory_sort_node(E_TREE_MEMORY(etree), parent_node, folder_sort_callback, storage_set_view);

    if (! add_node_to_hash (storage_set_view, path, new_node)) {
        e_tree_memory_node_remove (E_TREE_MEMORY(etree), new_node);
        return;
    }
}

static void
updated_folder_cb (EStorageSet *storage_set,
           const char *path,
           void *data)
{
    EStorageSetView *storage_set_view;
    EStorageSetViewPrivate *priv;
    ETreeModel *etree;
    ETreePath node;

    storage_set_view = E_STORAGE_SET_VIEW (data);
    priv = storage_set_view->priv;
    etree = priv->etree_model;

    node = lookup_node_in_hash (storage_set_view, path);
    e_tree_model_node_data_changed (etree, node);
}

static void
removed_folder_cb (EStorageSet *storage_set,
           const char *path,
           void *data)
{
    EStorageSetView *storage_set_view;
    EStorageSetViewPrivate *priv;
    ETreeModel *etree;
    ETreePath node;

    storage_set_view = E_STORAGE_SET_VIEW (data);
    priv = storage_set_view->priv;
    etree = priv->etree_model;

    node = remove_node_from_hash (storage_set_view, path);
    e_tree_memory_node_remove (E_TREE_MEMORY(etree), node);
}


static void
class_init (EStorageSetViewClass *klass)
{
    GtkObjectClass *object_class;
    ETreeClass *etree_class;
    GtkWidgetClass *widget_class;

    parent_class = gtk_type_class (e_tree_get_type ());

    object_class = GTK_OBJECT_CLASS (klass);
    object_class->destroy = destroy;

    widget_class = GTK_WIDGET_CLASS (klass);
    widget_class->button_press_event  = button_press_event;
    widget_class->motion_notify_event = motion_notify_event;

    etree_class = E_TREE_CLASS (klass);
    etree_class->right_click             = right_click;
    etree_class->cursor_activated        = cursor_activated;
    etree_class->tree_drag_begin         = tree_drag_begin;
    etree_class->tree_drag_end           = tree_drag_end;
    etree_class->tree_drag_data_get      = tree_drag_data_get;
    etree_class->tree_drag_data_delete   = tree_drag_data_delete;
    etree_class->tree_drag_motion        = tree_drag_motion;
    etree_class->tree_drag_drop          = tree_drag_drop;
    etree_class->tree_drag_data_received = tree_drag_data_received;

    signals[FOLDER_SELECTED]
        = gtk_signal_new ("folder_selected",
                  GTK_RUN_FIRST,
                  object_class->type,
                  GTK_SIGNAL_OFFSET (EStorageSetViewClass, folder_selected),
                  gtk_marshal_NONE__STRING,
                  GTK_TYPE_NONE, 1,
                  GTK_TYPE_STRING);

    signals[STORAGE_SELECTED]
        = gtk_signal_new ("storage_selected",
                  GTK_RUN_FIRST,
                  object_class->type,
                  GTK_SIGNAL_OFFSET (EStorageSetViewClass, storage_selected),
                  gtk_marshal_NONE__STRING,
                  GTK_TYPE_NONE, 1,
                  GTK_TYPE_STRING);

    signals[DND_ACTION]
        = gtk_signal_new ("dnd_action",
                  GTK_RUN_FIRST,
                  object_class->type,
                  GTK_SIGNAL_OFFSET (EStorageSetViewClass, dnd_action),
                  marshal_NONE__GDKDRAGCONTEXT_STRING_STRING_STRING,
                  GTK_TYPE_NONE, 4,
                  GTK_TYPE_GDK_DRAG_CONTEXT,
                  GTK_TYPE_STRING,
                  GTK_TYPE_STRING,
                  GTK_TYPE_STRING);

    gtk_object_class_add_signals (object_class, signals, LAST_SIGNAL);
}

static void
init (EStorageSetView *storage_set_view)
{
    EStorageSetViewPrivate *priv;

    priv = g_new (EStorageSetViewPrivate, 1);

    priv->storage_set                 = NULL;
    priv->path_to_etree_node          = g_hash_table_new (g_str_hash, g_str_equal);
    priv->type_name_to_pixbuf         = g_hash_table_new (g_str_hash, g_str_equal);
    priv->selected_row_path           = NULL;
    priv->show_folders                = TRUE;

    priv->drag_corba_source_interface = CORBA_OBJECT_NIL;

    priv->drag_corba_source_context   = NULL;
    priv->drag_corba_data             = NULL;

    priv->drag_x                      = 0;
    priv->drag_y                      = 0;
    priv->drag_column                 = 0;
    priv->drag_row                    = 0;

    storage_set_view->priv = priv;
}


/* Handling of the "changed" signal in EFolders displayed in the EStorageSetView.  */

struct _FolderChangedCallbackData {
    EStorageSetView *storage_set_view;
    char *path;
};
typedef struct _FolderChangedCallbackData FolderChangedCallbackData;

static void
folder_changed_callback_data_destroy_notify (void *data)
{
    FolderChangedCallbackData *callback_data;

    callback_data = (FolderChangedCallbackData *) data;

    g_free (callback_data->path);
    g_free (callback_data);
}

static void
folder_changed_cb (EFolder *folder,
           void *data)
{
    EStorageSetView *storage_set_view;
    EStorageSetViewPrivate *priv;
    FolderChangedCallbackData *callback_data;
    ETreePath node;

    callback_data = (FolderChangedCallbackData *) data;

    storage_set_view = callback_data->storage_set_view;
    priv = callback_data->storage_set_view->priv;

    node = g_hash_table_lookup (priv->path_to_etree_node, callback_data->path);
    if (node == NULL) {
        g_warning ("EStorageSetView -- EFolder::changed emitted for a folder whose path I don't know.");
        return;
    }

    e_tree_model_node_data_changed (priv->etree_model, node);
}


static void
insert_folders (EStorageSetView *storage_set_view,
        ETreePath parent,
        EStorage *storage,
        const char *path)
{
    EStorageSetViewPrivate *priv;
    ETreeModel *etree;
    ETreePath node;
    GList *folder_path_list;
    GList *p;
    const char *storage_name;

    priv = storage_set_view->priv;
    etree = priv->etree_model;

    storage_name = e_storage_get_name (storage);

    folder_path_list = e_storage_get_subfolder_paths (storage, path);
    if (folder_path_list == NULL)
        return;

    for (p = folder_path_list; p != NULL; p = p->next) {
        FolderChangedCallbackData *folder_changed_callback_data;
        EFolder *folder;
        const char *folder_name;
        const char *folder_path;
        char *full_path;

        folder_path = (const char *) p->data;
        folder = e_storage_get_folder (storage, folder_path);
        folder_name = e_folder_get_name (folder);

        full_path = g_strconcat ("/", storage_name, folder_path, NULL);
        node = e_tree_memory_node_insert (E_TREE_MEMORY(etree), parent, -1, (void *) full_path);
        e_tree_memory_sort_node(E_TREE_MEMORY(etree), parent, folder_sort_callback, storage_set_view);
        add_node_to_hash (storage_set_view, full_path, node);

        insert_folders (storage_set_view, node, storage, folder_path);

        folder_changed_callback_data = g_new (FolderChangedCallbackData, 1);
        folder_changed_callback_data->storage_set_view = storage_set_view;
        folder_changed_callback_data->path = g_strdup (full_path);

        e_gtk_signal_connect_full_while_alive (GTK_OBJECT (folder), "changed",
                               GTK_SIGNAL_FUNC (folder_changed_cb),
                               NULL,
                               folder_changed_callback_data,
                               folder_changed_callback_data_destroy_notify,
                               FALSE, FALSE,
                               GTK_OBJECT (storage_set_view));
    }

    e_free_string_list (folder_path_list);
}

static void
insert_storages (EStorageSetView *storage_set_view)
{
    EStorageSetViewPrivate *priv;
    EStorageSet *storage_set;
    GList *storage_list;
    GList *p;

    priv = storage_set_view->priv;

    storage_set = priv->storage_set;

    storage_list = e_storage_set_get_storage_list (storage_set);

    for (p = storage_list; p != NULL; p = p->next) {
        EStorage *storage = E_STORAGE (p->data);
        const char *name;
        char *path;
        ETreePath parent;

        name = e_storage_get_name (storage);
        path = g_strconcat ("/", name, NULL);

        parent = e_tree_memory_node_insert (E_TREE_MEMORY(priv->etree_model), priv->root_node,
                            -1, path);
        e_tree_memory_sort_node (E_TREE_MEMORY(priv->etree_model), priv->root_node, storage_sort_callback, storage_set_view);

        g_hash_table_insert (priv->path_to_etree_node, path, parent);

        if (priv->show_folders)
            insert_folders (storage_set_view, parent, storage, "/");
    }

    e_free_object_list (storage_list);
}

void
e_storage_set_view_construct (EStorageSetView   *storage_set_view,
                  EStorageSet       *storage_set,
                  BonoboUIContainer *container)
{
    EStorageSetViewPrivate *priv;
    ETableExtras *extras;
    ECell *cell;

    g_return_if_fail (storage_set_view != NULL);
    g_return_if_fail (E_IS_STORAGE_SET_VIEW (storage_set_view));
    g_return_if_fail (storage_set != NULL);
    g_return_if_fail (E_IS_STORAGE_SET (storage_set));

    priv = storage_set_view->priv;

    priv->container = container;

    priv->etree_model = e_tree_memory_callbacks_new (etree_icon_at,

                             etree_column_count,

                             etree_has_save_id,
                             etree_get_save_id,
                             etree_has_get_node_by_id,
                             etree_get_node_by_id,

                             etree_value_at,
                             etree_set_value_at,
                             etree_is_editable,

                             etree_duplicate_value,
                             etree_free_value,
                             etree_initialize_value,
                             etree_value_is_empty,
                             etree_value_to_string,

                             storage_set_view);

    e_tree_memory_set_node_destroy_func (E_TREE_MEMORY (priv->etree_model), (GFunc) g_free, NULL);
    e_tree_memory_set_expanded_default (E_TREE_MEMORY (priv->etree_model), TRUE);

    priv->root_node = e_tree_memory_node_insert (E_TREE_MEMORY(priv->etree_model), NULL, -1, g_strdup ("/Root Node"));

    extras = e_table_extras_new ();
    cell = e_cell_text_new (NULL, GTK_JUSTIFY_LEFT);
    gtk_object_set (GTK_OBJECT (cell), "bold_column", 1, NULL);
    e_table_extras_add_cell (extras, "render_tree",
                 e_cell_tree_new (NULL, NULL, TRUE, cell));

    e_tree_construct_from_spec_file (E_TREE (storage_set_view), priv->etree_model, extras,
                     EVOLUTION_ETSPECDIR "/e-storage-set-view.etspec", NULL);

    e_tree_root_node_set_visible (E_TREE(storage_set_view), FALSE);

    gtk_object_unref (GTK_OBJECT (extras));

    gtk_object_ref (GTK_OBJECT (storage_set));
    priv->storage_set = storage_set;

    e_tree_drag_dest_set (E_TREE (storage_set_view), 0, NULL, 0, GDK_ACTION_MOVE | GDK_ACTION_COPY);

    gtk_signal_connect_while_alive (GTK_OBJECT (storage_set), "new_storage",
                    GTK_SIGNAL_FUNC (new_storage_cb), storage_set_view,
                    GTK_OBJECT (storage_set_view));
    gtk_signal_connect_while_alive (GTK_OBJECT (storage_set), "removed_storage",
                    GTK_SIGNAL_FUNC (removed_storage_cb), storage_set_view,
                    GTK_OBJECT (storage_set_view));
    gtk_signal_connect_while_alive (GTK_OBJECT (storage_set), "new_folder",
                    GTK_SIGNAL_FUNC (new_folder_cb), storage_set_view,
                    GTK_OBJECT (storage_set_view));
    gtk_signal_connect_while_alive (GTK_OBJECT (storage_set), "updated_folder",
                    GTK_SIGNAL_FUNC (updated_folder_cb), storage_set_view,
                    GTK_OBJECT (storage_set_view));
    gtk_signal_connect_while_alive (GTK_OBJECT (storage_set), "removed_folder",
                    GTK_SIGNAL_FUNC (removed_folder_cb), storage_set_view,
                    GTK_OBJECT (storage_set_view));

    insert_storages (storage_set_view);
}

GtkWidget *
e_storage_set_view_new (EStorageSet *storage_set,
            BonoboUIContainer *container)
{
    GtkWidget *new;

    g_return_val_if_fail (storage_set != NULL, NULL);
    g_return_val_if_fail (E_IS_STORAGE_SET (storage_set), NULL);

    new = gtk_type_new (e_storage_set_view_get_type ());
    e_storage_set_view_construct (E_STORAGE_SET_VIEW (new), storage_set, container);

    return new;
}


void
e_storage_set_view_set_current_folder (EStorageSetView *storage_set_view,
                       const char *path)
{
    EStorageSetViewPrivate *priv;
    ETreePath node;

    g_return_if_fail (storage_set_view != NULL);
    g_return_if_fail (E_IS_STORAGE_SET_VIEW (storage_set_view));
    g_return_if_fail (path != NULL && g_path_is_absolute (path));

    priv = storage_set_view->priv;

    node = g_hash_table_lookup (priv->path_to_etree_node, path);
    if (node == NULL) {
        return;
    }

    e_tree_show_node (E_TREE(storage_set_view), node);
    e_tree_set_cursor (E_TREE (storage_set_view), node);

    gtk_signal_emit (GTK_OBJECT (storage_set_view), signals[FOLDER_SELECTED], path);
}

const char *
e_storage_set_view_get_current_folder (EStorageSetView *storage_set_view)
{
    EStorageSetViewPrivate *priv;
    ETreePath etree_node;
    const char *path;

    g_return_val_if_fail (storage_set_view != NULL, NULL);
    g_return_val_if_fail (E_IS_STORAGE_SET_VIEW (storage_set_view), NULL);

    priv = storage_set_view->priv;

    if (!priv->show_folders)
        return NULL; /* Mmh! */

    etree_node = e_tree_get_cursor (E_TREE (storage_set_view));

    if (etree_node == NULL)
        return NULL;    /* Mmh? */

    path = (char*)e_tree_memory_node_get_data(E_TREE_MEMORY(priv->etree_model), etree_node);

    return path;
}

void
e_storage_set_view_set_show_folders (EStorageSetView *storage_set_view,
                     gboolean show)
{
    EStorageSetViewPrivate *priv;

    g_return_if_fail (storage_set_view != NULL);
    g_return_if_fail (E_IS_STORAGE_SET_VIEW (storage_set_view));

    priv = storage_set_view->priv;

    if (show == priv->show_folders)
        return;

    /* tear down existing tree and hash table mappings */
    e_tree_memory_node_remove (E_TREE_MEMORY(priv->etree_model), priv->root_node);

    /* now re-add the root node */
    priv->root_node = e_tree_memory_node_insert (E_TREE_MEMORY(priv->etree_model), NULL, -1, g_strdup ("/Root Node"));

    /* then reinsert the storages after setting the "show_folders"
       flag.  insert_storages will call insert_folders if
       show_folders is TRUE */

    priv->show_folders = show;
    insert_storages (storage_set_view);
}

gboolean
e_storage_set_view_get_show_folders (EStorageSetView *storage_set_view)
{
    return storage_set_view->priv->show_folders;
}


E_MAKE_TYPE (e_storage_set_view, "EStorageSetView", EStorageSetView, class_init, init, PARENT_TYPE)