~lemonboy/pantheon-files/update-free-space

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
/***
    Copyright (C) 2015 elementary Developers

    This program is free software: you can redistribute it and/or modify it
    under the terms of the GNU Lesser General Public License version 3, as published
    by the Free Software Foundation.

    This program is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranties of
    MERCHANTABILITY, SATISFACTORY QUALITY, 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, see <http://www.gnu.org/licenses/>.

    Authors : Mr Jamie McCracken (jamiemcc at blueyonder dot co dot uk)
              Roth Robert <evfool@gmail.com>
              ammonkey <am.monkeyd@gmail.com>
              Jeremy Wootten <jeremy@elementaryos.org>
***/

namespace Marlin.Places {
    public class Sidebar : Marlin.AbstractSidebar {

        enum PlaceType {
            BUILT_IN,
            MOUNTED_VOLUME,
            BOOKMARK,
            BOOKMARKS_CATEGORY,
            PERSONAL_CATEGORY,
            STORAGE_CATEGORY
        }

        private const int MAX_BOOKMARKS_DROPPED = 100;
        private const int ROOT_INDENTATION_XPAD = 2;
        private const int EJECT_BUTTON_XPAD = 6;
        private const int ICON_XPAD = 6 + ROOT_INDENTATION_XPAD;
        private const int PROP_0 = 0;

        private static FM.DndHandler dnd_handler = new FM.DndHandler ();

        Gtk.TreeView tree_view;
        Gtk.CellRenderer indent_renderer;
        Gtk.CellRendererText name_renderer;
        Gtk.CellRendererPixbuf icon_cell_renderer;
        Marlin.IconSpinnerRenderer eject_spinner_cell_renderer;
        Gtk.CellRenderer expander_renderer;
        Gtk.TreePath select_path;
        Marlin.View.Window window;
        Marlin.BookmarkList bookmarks;
        VolumeMonitor volume_monitor;
        Marlin.TrashMonitor monitor;
        Gtk.IconTheme theme;
        GLib.Icon eject_icon;

        int eject_button_size = 20;
        uint n_builtins_before;
        string last_selected_uri;
        string slot_location;

        /* DnD */
        List<GLib.File> drag_list;
        Gtk.TreeRowReference? drag_row_ref;
        bool dnd_disabled = false;
        uint drag_data_info;
        uint drag_scroll_timer_id;
        Gdk.DragContext drag_context;
        bool received_drag_data;
        bool drop_occurred;
        bool internal_drag_started;
        bool dragged_out_of_window;
        bool renaming = false;

        /* Identifiers for target types */
        public enum TargetType {
            GTK_TREE_MODEL_ROW,
            TEXT_URI_LIST
            }

        /* Gtk.Target types for dragging from shortcut list */
         const Gtk.TargetEntry source_targets [] = {
            {"GTK_TREE_MODEL_ROW", Gtk.TargetFlags.SAME_WIDGET, TargetType.GTK_TREE_MODEL_ROW}
        };

         const Gtk.TargetEntry drop_targets [] = {
            {"GTK_TREE_MODEL_ROW", Gtk.TargetFlags.SAME_WIDGET, TargetType.GTK_TREE_MODEL_ROW},
            {"text/uri-list", Gtk.TargetFlags.SAME_APP, TargetType.TEXT_URI_LIST}
        };

        Gtk.Menu popupmenu;
        Gtk.MenuItem popupmenu_open_in_new_tab_item;
        Gtk.MenuItem popupmenu_open_in_new_window_item;
        Gtk.MenuItem popupmenu_remove_item;
        Gtk.MenuItem popupmenu_rename_item;
        Gtk.MenuItem popupmenu_separator_item1;
        Gtk.MenuItem popupmenu_separator_item2;
        Gtk.MenuItem popupmenu_mount_item;
        Gtk.MenuItem popupmenu_unmount_item;
        Gtk.MenuItem popupmenu_eject_item;
        Gtk.MenuItem popupmenu_rescan_item;
        Gtk.MenuItem popupmenu_format_item;
        Gtk.MenuItem popupmenu_empty_trash_item;
        Gtk.MenuItem popupmenu_start_item;
        Gtk.MenuItem popupmenu_stop_item;

        /* volume mounting - delayed open process */
        bool mounting = false;

        /* prevent multiple unmount processes */
        bool ejecting_or_unmounting = false;

        /* TODO Make it an option in Settings whether or not to show
         * bookmarks pointing to non-existent (or unmounted) files. */
        bool display_all_bookmarks = true;

        /* Remember vertical adjustment value when lose focus */
        double adjustment_val = 0.0;
        /* Remember path at button press */
        Gtk.TreePath? click_path = null;

        public signal bool request_focus ();
        public signal void sync_needed ();

        public Sidebar (Marlin.View.Window window) {
            init ();  /* creates the Gtk.TreeModel store. */
            this.last_selected_uri = null;
            this.set_policy (Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC);
            this.window = window;
            window.loading_uri.connect (loading_uri_callback);

            construct_tree_view ();
            configure_tree_view ();
            connect_tree_view_signals ();
            this.scroll_event.connect (update_adjustment_val);
            this.content_box.pack_start (this.tree_view, true);

            this.bookmarks = Marlin.BookmarkList.get_instance ();
            bookmarks.contents_changed.connect (update_places);

            set_up_trash_monitor ();
            set_up_volume_monitor ();

            set_up_theme ();
            this.show_all ();

            update_places ();
        }

        private void construct_tree_view () {
            tree_view = new Gtk.TreeView ();
            tree_view.set_size_request (Preferences.settings.get_int ("minimum-sidebar-width"), -1);
            tree_view.set_headers_visible (false);

            var cab = new Gtk.CellAreaBox ();
            var col = new Gtk.TreeViewColumn.with_area (cab);
            col.max_width = -1;
            col.expand = true;
            col.spacing = 3;

            var crt = new Gtk.CellRendererText ();
            this.indent_renderer = crt;
            cab.pack_start(crt, false, false, false);
            col.set_cell_data_func (crt, indent_cell_data_func);

            var crpb = new Gtk.CellRendererPixbuf ();
            this.icon_cell_renderer = crpb;
            crpb.follow_state = true;
            crpb.stock_size = Gtk.IconSize.MENU;
            cab.pack_start(crpb, false, false, false);
            col.set_attributes (crpb, "gicon", Column.ICON);
            col.set_cell_data_func (crpb, icon_cell_data_func);

            var crd = new Marlin.CellRendererDisk ();
            crd.ellipsize = Pango.EllipsizeMode.END;
            crd.ellipsize_set = true;
            crd.rpad = 12;
            cab.pack_start (crd, true, false, false);
            col.set_attributes (crd,
                                "text", Column.NAME,
                                "visible", Column.EJECT,
                                "free_space", Column.FREE_SPACE,
                                "disk_size", Column.DISK_SIZE);

            var crs = new Marlin.IconSpinnerRenderer ();
            eject_spinner_cell_renderer = crs;
            crs.mode = Gtk.CellRendererMode.ACTIVATABLE;
            crs.icon_size = Gtk.IconSize.MENU;
            crs.xpad = 0;
            crs.xalign = (float)1.0;
            cab.pack_start (crs, false, false, false);
            col.set_attributes (crs,
                                "gicon", Column.EJECT_ICON,
                                "visible", Column.EJECT,
                                "active", Column.SHOW_SPINNER,
                                "pulse", Column.SPINNER_PULSE);

            name_renderer = new Gtk.CellRendererText ();
            name_renderer.editable = false;
            name_renderer.editable_set = true;
            name_renderer.ellipsize = Pango.EllipsizeMode.END;
            name_renderer.ellipsize_set = true;
            name_renderer.edited.connect (edited);
            name_renderer.editing_canceled.connect (editing_canceled);
            cab.pack_start (name_renderer,true, false, false);
            col.set_attributes (name_renderer,
                                "text", Column.NAME,
                                "visible", Column.NO_EJECT,
                                "editable-set", Column.BOOKMARK);
            col.set_cell_data_func (name_renderer, category_renderer_func);

            tree_view.show_expanders = false;
            var cre = new Granite.Widgets.CellRendererExpander ();
            expander_renderer = cre;
            cre.is_category_expander = true;
            /* this is required to align the eject buttons to the right */
            int exp_size = cre.get_arrow_size (tree_view);
            Gtk.icon_size_lookup (Gtk.IconSize.MENU, out eject_button_size, null);
            cre.xpad = int.max ((eject_button_size - exp_size)/2, 0);
            cab.pack_start (cre, false, false, false);
            col.set_cell_data_func (cre, expander_cell_data_func);

            crt = new Gtk.CellRendererText ();
            crt.xpad = EJECT_BUTTON_XPAD;
            crt.xalign = (float)1.0;
            cab.pack_start(crt, false, false, false);

            tree_view.append_column (col);
            tree_view.tooltip_column = Column.TOOLTIP;
            tree_view.model = this.store;
        }

        private void configure_tree_view () {
            var style_context = tree_view.get_style_context ();
            style_context.add_class ("sidebar");
            style_context.add_class ("source-list");

            tree_view.set_search_column (Column.NAME);
            var selection = tree_view.get_selection ();
            selection.set_mode (Gtk.SelectionMode.BROWSE);
            selection.set_select_function (tree_selection_func);

            this.drag_scroll_timer_id = 0;
            tree_view.enable_model_drag_source (Gdk.ModifierType.BUTTON1_MASK,
                                                source_targets,
                                                Gdk.DragAction.MOVE);
            Gtk.drag_dest_set (tree_view,
                               Gtk.DestDefaults.MOTION,
                               drop_targets,
                               Gdk.DragAction.MOVE | Gdk.DragAction.COPY | Gdk.DragAction.LINK);
        }

        private void connect_tree_view_signals () {
            tree_view.row_activated.connect (row_activated_callback);

            tree_view.drag_motion.connect (drag_motion_callback);
            tree_view.drag_leave.connect (drag_leave_callback);
            tree_view.drag_data_received.connect (drag_data_received_callback);
            tree_view.drag_drop.connect (drag_drop_callback);
            tree_view.drag_failed.connect (drag_failed_callback);
            tree_view.drag_end.connect (drag_end_callback);

            (tree_view.get_selection ()).changed.connect (selection_changed_cb);
            tree_view.popup_menu.connect (popup_menu_cb);
            tree_view.button_press_event.connect (button_press_event_cb);
            tree_view.button_release_event.connect (button_release_event_cb);
            tree_view.key_press_event.connect (key_press_event_cb);

            tree_view.row_expanded.connect (category_row_expanded_event_cb);
            tree_view.row_collapsed.connect (category_row_collapsed_event_cb);

            tree_view.add_events (Gdk.EventMask.FOCUS_CHANGE_MASK | Gdk.EventMask.ENTER_NOTIFY_MASK);
            tree_view.focus_in_event.connect (focus_in_event_cb);
            //tree_view.focus_out_event.connect (focus_out_event_cb);
            tree_view.enter_notify_event.connect (on_enter_notify_event);
            tree_view.leave_notify_event.connect (on_leave_notify_event);
        }

        private bool on_enter_notify_event () {
            /* Ensure tree has focus when scrolling but do not grab focus if either a bookmark
             *  is being renamed or request_focus is denied.
             */ 
            if (!tree_view.has_focus && !renaming && request_focus ())
                tree_view.grab_focus ();

            return false;
        }

        private bool on_leave_notify_event () {
            /* Signal Marlin.View.Window to synchronise sidebar with current tab */
            sync_needed ();
            return false;
        }

        private bool focus_in_event_cb (Gdk.EventFocus event) {
            /* Restore saved adjustment value to prevent unexpected scrolling */
            ((this as Gtk.ScrolledWindow).get_vadjustment ()).set_value (adjustment_val);
            return false;
        }

        private bool update_adjustment_val () {
            adjustment_val = ((this as Gtk.ScrolledWindow).get_vadjustment ()).value;
            return false;
        }

        private void set_up_trash_monitor () {
            monitor = Marlin.TrashMonitor.get ();
            monitor.trash_state_changed.connect (trash_state_changed_cb);
        }

        private void set_up_volume_monitor () {
            this.volume_monitor = GLib.VolumeMonitor.@get ();
            volume_monitor.volume_added.connect (volume_added_callback);
            volume_monitor.volume_removed.connect (volume_removed_callback);
            volume_monitor.volume_changed.connect (volume_changed_callback);

            volume_monitor.mount_added.connect (mount_added_callback);
            volume_monitor.mount_removed.connect (mount_removed_callback);
            volume_monitor.mount_changed.connect (mount_changed_callback);

            volume_monitor.drive_disconnected.connect (drive_disconnected_callback);
            volume_monitor.drive_changed.connect (drive_connected_callback);
            volume_monitor.drive_changed.connect (drive_changed_callback);
        }

        private void set_up_theme () {
            theme = Gtk.IconTheme.get_default ();
            theme.changed.connect (icon_theme_changed_callback);
            get_eject_icon ();
        }

        private void get_eject_icon () {
            if (eject_icon == null)
                eject_icon = new ThemedIcon.with_default_fallbacks ("media-eject-symbolic");
        }

        protected override Gtk.TreeIter add_place (Marlin.PlaceType place_type,
                                                   Gtk.TreeIter? parent,
                                                   string name,
                                                   Icon? icon,
                                                   string? uri,
                                                   Drive? drive,
                                                   Volume? volume,
                                                   Mount? mount,
                                                   uint index,
                                                   string tooltip) {
            Gdk.Pixbuf? pixbuf = null;
            if (icon != null) {
                Marlin.IconInfo? icon_info = Marlin.IconInfo.lookup (icon, Marlin.IconSize.SMALLEST);
                if (icon_info != null)
                    pixbuf = icon_info.get_pixbuf_nodefault ();
            }

            bool show_eject, show_unmount;
            check_unmount_and_eject (mount, volume, drive, out show_unmount, out show_eject);
            if (show_unmount || show_eject)
                    assert (place_type != Marlin.PlaceType.BOOKMARK);

            bool show_eject_button = false;
            if (mount != null)
                show_eject_button = (show_unmount || show_eject);

            GLib.Icon eject;
            if (show_eject_button)
                eject = this.eject_icon;
            else
                eject = null;

            GLib.Error error = null;
            string converted_name = name.locale_to_utf8 (name.length, null, null, out error);
            if (error != null) {
                warning ("Could not convert bookmark name. %s", error.message);
                converted_name = name;
            }

            Gtk.TreeIter iter;
            this.store.append (out iter, parent);
            this.store.@set (iter,
                            Column.ROW_TYPE, place_type,
                            Column.URI, uri,
                            Column.DRIVE, drive,
                            Column.VOLUME, volume,
                            Column.MOUNT, mount,
                            Column.NAME, converted_name,
                            Column.ICON, (GLib.Icon)pixbuf,
                            Column.INDEX, index,
                            Column.EJECT, show_eject_button,
                            Column.NO_EJECT, !show_eject_button,
                            Column.BOOKMARK, place_type == Marlin.PlaceType.BOOKMARK,
                            Column.TOOLTIP, tooltip,
                            Column.EJECT_ICON, eject,
                            Column.SHOW_SPINNER, false,
                            Column.SPINNER_PULSE, 0,
                            Column.FREE_SPACE, (uint64)0,
                            Column.DISK_SIZE, (uint64)0);
            return iter;
        }

        private bool recent_is_supported () {
            string [] supported;

            supported = GLib.Vfs.get_default ().get_supported_uri_schemes ();
            for (int i = 0; supported[i] != null; i++) {
                if (supported[i] == "recent") {
                    return true;
                }
            }

            return false;
        }

        private void update_places () {
            Gtk.TreeIter iter;
            string mount_uri;

            this.last_selected_uri = null;
            this.select_path = null;
            this.n_builtins_before = 0;

            if ((tree_view.get_selection ()).get_selected (null, out iter))
                store.@get (iter, Column.URI, &last_selected_uri);
            else
                last_selected_uri = null;

            store.clear ();

            /* Add Bookmarks CATEGORY*/
            store.append (out iter, null);
            store.@set (iter,
                        Column.ICON, null,
                        Column.NAME, _("Personal"),
                        Column.ROW_TYPE, Marlin.PlaceType.BOOKMARKS_CATEGORY,
                        Column.EJECT, false,
                        Column.NO_EJECT, true,
                        Column.BOOKMARK, false,
                        Column.TOOLTIP, _("Your common places and bookmarks"));

            /* Add Home BUILTIN */
            try {
                mount_uri = GLib.Filename.to_uri (GLib.Environment.get_home_dir (), null);
            }
            catch (ConvertError e) {
                mount_uri = "";
            }

            add_place (Marlin.PlaceType.BUILT_IN,
                       iter,
                       _("Home"),
                       new ThemedIcon (Marlin.ICON_HOME),
                       mount_uri,
                       null,
                       null,
                       null,
                       0,
                       _("Open your personal folder"));

            n_builtins_before++;

            /*  Add Recents BUILTIN */
            if (recent_is_supported ()) {
                add_place (Marlin.PlaceType.BUILT_IN,
                    iter,
                    Marlin.PROTOCOL_NAME_RECENT,
                    new ThemedIcon (Marlin.ICON_RECENT),
                    Marlin.RECENT_URI,
                    null,
                    null,
                    null,
                    0,
                    _("View the list of recently used files"));

                n_builtins_before++;
            }

            /* Add bookmarks */
            uint bookmark_count = bookmarks.length ();
            unowned Bookmark bm;
            uint index;
            for (index = 0; index < bookmark_count; index++) {
                bm = bookmarks.item_at (index);
                if (bm == null
                 || (bm.uri_known_not_to_exist () && !display_all_bookmarks))
                    continue;

                add_bookmark (iter, bm, index);
            }

            /* Add trash */
            add_place (Marlin.PlaceType.BUILT_IN,
                       iter,
                       _("Trash"),
                       Marlin.TrashMonitor.get_icon (),
                       Marlin.TRASH_URI,
                       null,
                       null,
                       null,
                       index + n_builtins_before,
                       _("Open the Trash"));

            /* ADD STORAGE CATEGORY*/
            store.append (out iter, null);
            store.@set (iter,
                        Column.ICON, null,
                        Column.NAME, _("Devices"),
                        Column.ROW_TYPE, Marlin.PlaceType.STORAGE_CATEGORY,
                        Column.EJECT, false,
                        Column.NO_EJECT, true,
                        Column.BOOKMARK, false,
                        Column.TOOLTIP, _("Your local partitions and devices"));

            /* Add Filesystem BUILTIN */
            add_place (Marlin.PlaceType.BUILT_IN,
                       iter,
                       _("File System"),
                       new ThemedIcon.with_default_fallbacks (Marlin.ICON_FILESYSTEM),
                       "file:///",
                       null,
                       null,
                       null,
                       0,
                       _("Open the contents of the FileSystem"));

            /* Add all connected drives */
            GLib.List<GLib.Drive> drives = volume_monitor.get_connected_drives ();
            GLib.List<GLib.Volume> volumes;
            foreach (GLib.Drive drive in drives) {
                volumes = drive.get_volumes ();
                if (volumes != null)
                    add_volumes (iter, drive, volumes);

                else if (drive.is_media_removable ()
                     && !drive.is_media_check_automatic ()) {
                    /* If the drive has no mountable volumes and we cannot detect media change.. we
                     * display the drive in the sidebar so the user can manually poll the drive by
                     * right clicking and selecting "Rescan..."
                     *
                     * This is mainly for drives like floppies where media detection doesn't
                     * work.. but it's also for human beings who like to turn off media detection
                     * in the OS to save battery juice.
                     */
                    var name = drive.get_name ();
                    add_place (Marlin.PlaceType.BUILT_IN,
                               iter,
                               name,
                               drive.get_icon (),
                               null,
                               drive,
                               null,
                               null,
                               0,
                               (_("Mount and open %s")).printf (name));
                }
            }
            /* add all volumes that are not associated with a drive */
            volumes = volume_monitor.get_volumes ();
            foreach (Volume volume in volumes) {
                if (volume.get_drive () != null)
                    continue;

                var mount = volume.get_mount ();
                if (mount != null) {
                    var root = mount.get_default_location ();
                    var it = add_place (Marlin.PlaceType.MOUNTED_VOLUME,
                                        iter,
                                        mount.get_name (),
                                        mount.get_icon (),
                                        root.get_uri (),
                                        null,
                                        volume,
                                        mount,
                                        0,
                                        root.get_parse_name ());

                    uint64 fs_capacity, fs_free;
                    get_filesystem_space (root, out fs_capacity, out fs_free);
                    store.@set (it,
                                Column.FREE_SPACE, fs_free,
                                Column.DISK_SIZE, fs_capacity);
                } else {
                /* see comment above in why we add an icon for an unmounted mountable volume */
                    var name = volume.get_name ();
                    add_place (Marlin.PlaceType.MOUNTED_VOLUME,
                               iter,
                               name,
                               volume.get_icon (),
                               null,
                               null,
                               volume,
                               null,
                               0,
                               name);
                }
            }
            /* Add mounts that have no volume (/etc/mtab mounts, ftp, sftp,...) */
            GLib.List<Mount> network_mounts = null;
            var mounts = volume_monitor.get_mounts ();
            foreach (Mount mount in mounts) {
                if (mount.is_shadowed ())
                    continue;

                var volume = mount.get_volume ();
                if (volume != null)
                    continue;


                var root = mount.get_default_location ();
                if (root.is_native ()) {
                    string scheme = root.get_uri_scheme ();
                    if (scheme == "archive" ) {
                        network_mounts.prepend (mount);
                        continue;
                    }
                } else {
                    network_mounts.prepend (mount);
                    continue;
                }

                add_place (Marlin.PlaceType.MOUNTED_VOLUME,
                           iter,
                           mount.get_name (),
                           mount.get_icon (),
                           root.get_uri (),
                           null,
                           null,
                           mount,
                           0,
                           root.get_parse_name ());
            }

            /* ADD NETWORK CATEGORY */
            store.append (out iter, null);
            store.@set (iter,
                        Column.ICON, null,
                        Column.NAME, _("Network"),
                        Column.ROW_TYPE, Marlin.PlaceType.NETWORK_CATEGORY,
                        Column.EJECT, false,
                        Column.NO_EJECT, true,
                        Column.BOOKMARK, false,
                        Column.TOOLTIP, _("Your network places"));

            network_category_reference = new Gtk.TreeRowReference (store, store.get_path (iter));

            /* Add network mounts */
            network_mounts.reverse ();
            foreach (Mount mount in network_mounts) {
                var root = mount.get_default_location ();
                /* get_smb_share_from_uri will return the uri unaltered if does not have
                 * the smb scheme so we need not test.  This is required because the mount
                 * does not return the true root location of the share but the location used
                 * when creating the mount.
                 */
                string uri = Marlin.get_smb_share_from_uri (root.get_uri ());

                add_place (Marlin.PlaceType.BUILT_IN,
                           iter,
                           mount.get_name (),
                           mount.get_icon (),
                           uri,
                           null,
                           null,
                           mount,
                           0,
                           uri);
            }

            /* Add Entire Network BUILTIN */
            add_place (Marlin.PlaceType.BUILT_IN,
                       iter,
                       _("Entire Network"),
                       new GLib.ThemedIcon (Marlin.ICON_NETWORK),
                       "network:///",
                       null,
                       null,
                       null,
                       0,
                       _("Browse the contents of the network"));

            plugins.update_sidebar ((Gtk.Widget)this);

            expander_init_pref_state (tree_view);

            /* select any previously selected place or any place matching slot location */
            if (last_selected_uri != null)
                set_matching_selection (this.last_selected_uri);
            else
                set_matching_selection  (slot_location);
        }

        private void add_bookmark (Gtk.TreeIter iter, Marlin.Bookmark bm, uint index) {
            add_place ( Marlin.PlaceType.BOOKMARK,
                        iter,
                        bm.label.dup (),
                        bm.get_icon (),
                        bm.get_uri (),
                        null,
                        null,
                        null,
                        index + n_builtins_before,
                        bm.get_parse_name());
        }

        private void add_volumes (Gtk.TreeIter iter,
                                  GLib.Drive drive,
                                  GLib.List<GLib.Volume> volumes) {
            Gtk.TreeIter last_iter;
            foreach (Volume volume in volumes) {
                var mount = volume.get_mount ();
                if (mount != null) {
                    /* show mounted volume in sidebar */
                    var root = mount.get_default_location ();
                    last_iter = add_place (Marlin.PlaceType.MOUNTED_VOLUME,
                                           iter,
                                           mount.get_name (),
                                           mount.get_icon (),
                                           root.get_uri (),
                                           drive,
                                           volume,
                                           mount,
                                           0,
                                           root.get_parse_name ());

                    uint64 fs_capacity, fs_free;
                    get_filesystem_space (root, out fs_capacity, out fs_free);
                    store.@set (last_iter,
                                Column.FREE_SPACE, fs_free,
                                Column.DISK_SIZE, fs_capacity);
                } else {
                    /* Do show the unmounted volumes in the sidebar;
                    * this is so the user can mount it (in case automounting
                    * is off).
                    *
                    * Also, even if automounting is enabled, this gives a visual
                    * cue that the user should remember to yank out the media if
                    * he just unmounted it.
                    */
                    var name = volume.get_name ();
                    add_place (Marlin.PlaceType.MOUNTED_VOLUME,
                               iter,
                               name,
                               volume.get_icon (),
                               null,
                               drive,
                               volume,
                               null,
                               0,
                               (_("Mount and open %s")).printf (name));
                }
            }
        }

        private void get_filesystem_space (GLib.File root, out uint64 fs_capacity, out uint64 fs_free) {
            GLib.FileInfo info;
            try {
                info = root.query_filesystem_info ("filesystem::*", null);
            }
            catch (GLib.Error error) {
                warning ("Error querying root filesystem info: %s", error.message);
                info = null;
            }
            fs_capacity = 0;
            fs_free = 0;
            if (info != null) {
                fs_capacity = info.get_attribute_uint64 (FileAttribute.FILESYSTEM_SIZE);
                fs_free = info.get_attribute_uint64 (FileAttribute.FILESYSTEM_FREE);
            }
        }

/* DRAG N DROP FUNCTIONS START */

        private bool drag_failed_callback (Gdk.DragContext context, Gtk.DragResult result) {
            int x, y;
            Gdk.Device device;
            Plank.Widgets.PoofWindow poof_window;

            if (internal_drag_started && dragged_out_of_window) {
                device = context.get_device ();
                device.get_position (null, out x, out y);
                poof_window = Plank.Widgets.PoofWindow.get_default ();
                poof_window.show_at (x, y);
                if (drag_row_ref != null) {
                    Gtk.TreeIter iter;
                    store.get_iter (out iter, drag_row_ref.get_path ());
                    remove_bookmark_iter (iter);
                }
                return true;
            } else
                return false;
        }

        private void drag_end_callback (Gdk.DragContext context) {
            internal_drag_started = false;
            dragged_out_of_window = false;
        }

        private bool drag_motion_callback (Gdk.DragContext context,
                                           int x,
                                           int y,
                                           uint time) {
            if (!received_drag_data
             && !get_drag_data (tree_view, context, time))
                    return false;

            Gtk.TreeViewDropPosition pos;
            Gtk.TreePath path;
            if (!compute_drop_position (tree_view, x, y, out path, out pos))
                return false;

            Gdk.DragAction action = Gdk.DragAction.DEFAULT;
            if (pos == Gtk.TreeViewDropPosition.BEFORE
             || pos == Gtk.TreeViewDropPosition.AFTER) {
                if (received_drag_data
                 && drag_data_info == TargetType.GTK_TREE_MODEL_ROW) {
                    action = Gdk.DragAction.MOVE;
                    internal_drag_started = true;
                }
                else if (drag_list != null
                      && can_accept_files_as_bookmarks (drag_list))
                    action = Gdk.DragAction.COPY;
            }
            else if (drag_list != null && path != null) {
                Gtk.TreeIter iter;
                store.get_iter (out iter, path);
                string uri;
                this.store.@get (iter, Column.URI, out uri);
                if (uri != null) {
                    GOF.File file = GOF.File.get_by_uri (uri);
                    if (file.ensure_query_info ())
                        file.accepts_drop (drag_list, context, out action);
                }
            }

            tree_view.set_drag_dest_row (path, pos);
            GLib.Signal.stop_emission_by_name (tree_view, "drag-motion");

            Gdk.drag_status (context, action, time);

            /* start the drag autoscroll timer if not already running */
            if (drag_scroll_timer_id < 1) {
                drag_context = context;
                drag_scroll_timer_id = GLib.Timeout.add_full (GLib.Priority.LOW,
                                                              50,
                                                              drag_scroll_timer);
            }
            return true;
        }

        private bool drag_drop_callback (Gdk.DragContext context,
                                         int x,
                                         int y,
                                         uint time) {
            drop_occurred = true;
            bool retval = get_drag_data (tree_view, context, time);
            GLib.Signal.stop_emission_by_name (tree_view, "drag_drop");
            return retval;
        }

        private  bool get_drag_data (Gtk.TreeView tree_view,
                                     Gdk.DragContext context,
                                     uint32 time) {
            var target_list = Gtk.drag_dest_get_target_list (tree_view);
            var target = Gtk.drag_dest_find_target (tree_view,
                                                    context,
                                                    target_list);

            if (target == Gdk.Atom.NONE)
                return false;

            Gtk.drag_get_data ((Gtk.Widget)tree_view, context, target, time);
            return true;
        }

        private void drag_data_received_callback (Gtk.Widget widget,
                                                  Gdk.DragContext context,
                                                  int x,
                                                  int y,
                                                  Gtk.SelectionData selection_data,
                                                  uint info,
                                                  uint time) {
            if (!received_drag_data) {
                this.drag_list = null;
                this.drag_row_ref = null;
                if (selection_data.get_target () != Gdk.Atom.NONE
                    && info == TargetType.TEXT_URI_LIST) {
                    string s = (string)(selection_data.get_data ());
                    drag_list = EelGFile.list_new_from_string (s);
                } else {
                    if (info == TargetType.GTK_TREE_MODEL_ROW) {
                        Gtk.TreePath path;
                        Gtk.tree_get_row_drag_data (selection_data, null, out path);
                        drag_row_ref = new Gtk.TreeRowReference (store, path);
                    }
                }
                received_drag_data = true;
                drag_data_info = info;
            }

            GLib.Signal.stop_emission_by_name (widget, "drag-data-received");

            if (!drop_occurred) /* called from drag_motion_callback */
                return;

            drop_occurred = false;
            bool success = process_drop (context, x, y, info);
            Gtk.drag_finish (context, success, false, time);
            free_drag_data ();
            update_places ();
        }

        private bool process_drop (Gdk.DragContext context, int x, int y, uint info) {
            Gtk.TreePath tree_path;
            Gtk.TreeViewDropPosition drop_pos;
            if (compute_drop_position (tree_view, x, y, out tree_path, out drop_pos)) {
                Gtk.TreeIter iter;
                if (!store.get_iter (out iter, tree_path))
                    return false;

                if (drop_pos == Gtk.TreeViewDropPosition.BEFORE
                 || drop_pos == Gtk.TreeViewDropPosition.AFTER)
                    return process_drop_between (iter, drop_pos, info);
                else
                    return process_drop_onto (iter, context, info);
            }
            return false;
        }

        private bool process_drop_between (Gtk.TreeIter iter,
                                           Gtk.TreeViewDropPosition drop_pos,
                                           uint info) {
            Marlin.PlaceType type;
            uint position;
            store.@get (iter,
                        Column.ROW_TYPE, out type,
                        Column.INDEX, out position);

            if (type == Marlin.PlaceType.BOOKMARK || type == Marlin.PlaceType.BUILT_IN) {
                if (type == Marlin.PlaceType.BOOKMARK && drop_pos == Gtk.TreeViewDropPosition.BEFORE)
                    position--;

                switch (info) {
                    case TargetType.TEXT_URI_LIST:
                        drop_drag_list (position);
                        return true;
                    case TargetType.GTK_TREE_MODEL_ROW:
                        reorder_bookmarks (position);
                        return true;
                    default:
                        assert_not_reached ();
                }
            }
            return false;
        }

        private bool process_drop_onto (Gtk.TreeIter iter, Gdk.DragContext context, uint info) {
            string drop_uri;
            store.@get (iter, Column.URI, out drop_uri);

            var real_action = context.get_selected_action ();
            if (real_action == Gdk.DragAction.ASK) {
                var actions = context.get_actions ();
                if (drop_uri.has_prefix ("trash:///"))
                    actions &= Gdk.DragAction.MOVE;

                real_action = dnd_handler.drag_drop_action_ask ((Gtk.Widget)tree_view, window, actions);
            }

            if (real_action == Gdk.DragAction.DEFAULT)
                return false;

            switch (info) {
                 case TargetType.TEXT_URI_LIST:
                    Marlin.FileOperations.copy_move (drag_list,
                                                     null,
                                                     File.new_for_uri (drop_uri),
                                                     real_action,
                                                     this, null, null);
                    return true;
                case TargetType.GTK_TREE_MODEL_ROW:
                    return false;
                default:
                    return false;;
            }
        }

        private void drag_leave_callback (Gdk.DragContext context, uint time) {
            dragged_out_of_window = true;
            free_drag_data ();
            tree_view.set_drag_dest_row (null, Gtk.TreeViewDropPosition.BEFORE);
            GLib.Signal.stop_emission_by_name (tree_view, "drag-leave");
        }

        private void free_drag_data () {
            received_drag_data = false;
            /* stop any running drag autoscroll timer */
            if (drag_scroll_timer_id > 0) {
                GLib.Source.remove (drag_scroll_timer_id);
                drag_scroll_timer_id = 0;
            }
        }

        private  bool can_accept_file_as_bookmark (GLib.File file) {
            GLib.FileType ftype = file.query_file_type (GLib.FileQueryInfoFlags.NONE, null);
            return ftype == GLib.FileType.DIRECTORY;
        }

        private bool can_accept_files_as_bookmarks (List<GLib.File> items) {
        /* Iterate through selection checking if item will get accepted as a bookmark.
         * Does not accept more than MAX_BOOKMARKS_DROPPED bookmarks
         */
            int count = 0;
            items.@foreach ((file) => {
                if (can_accept_file_as_bookmark (file))
                    count++;
            });
            return count > 0 && count <= MAX_BOOKMARKS_DROPPED;
        }

        private void drop_drag_list (uint position) {
            if (drag_list == null)
                return;

            GLib.List<string> uris = null;
            drag_list.@foreach ((file) => {
                if (can_accept_file_as_bookmark (file))
                    uris.prepend (file.get_uri ());
            });

            if (uris != null)
                bookmarks.insert_uris (uris, position);
        }

        public void add_uri (string uri, string? label = null) {
            bookmarks.insert_uri_at_end (uri, label);
        }

        private  bool drag_scroll_timer () {
            Gtk.Adjustment adjustment;
            double val;
            int offset;
            int y, x;
            int w, h;

            /* verify that we are realized */
            if (get_realized ()) {
                /* determine pointer location and window geometry */
                Gtk.Widget widget = (this as Gtk.Bin).get_child ();
                Gdk.Device pointer = drag_context.get_device ();
                Gdk.Window window = widget.get_window ();

                window.get_device_position (pointer, out x, out y, null);
                window.get_geometry (null, null, out w, out h);
                /* check if we are near the edge (vertical) */
                offset = y - (2 * 20);
                if (offset > 0)
                    offset = int.max (y - (h - 2 * 20), 0);

                /* change the vertical adjustment appropriately */
                if (offset != 0) {
                    /* determine the vertical adjustment */
                    adjustment = (this as Gtk.ScrolledWindow).get_vadjustment ();
                    /* determine the new value */
                    val = (adjustment.value + 2.0 * offset);
                    val = val.clamp (adjustment.lower,
                                     adjustment.upper - adjustment.page_size);

                    /* apply the new value */
                    adjustment.value = val;
                }
                /* check if we are near the edge (horizontal) */
                 offset = x - (2 * 20);
                 if (offset > 0)
                    offset = int.max (x - (w - 2 * 20), 0);

                /* change the horizontal adjustment appropriately */
                if (offset != 0) {
                    /* determine the horizontal adjustment */
                    adjustment = (this as Gtk.ScrolledWindow).get_hadjustment ();
                    /* determine the new value */
                    val = (adjustment.value + 2 * offset);
                    val = val.clamp (adjustment.lower,
                                     adjustment.upper - adjustment.page_size);

                    /* apply the new value */
                    adjustment.value = val;
                }
            }
            return true;
        }

        /* Computes the appropriate row and position for dropping */
        private bool compute_drop_position (Gtk.TreeView tree_view,
                                            int x,
                                            int y,
                                            out Gtk.TreePath path,
                                            out Gtk.TreeViewDropPosition drop_position
                                            ) {
            path = null;
            int num_rows = store.iter_n_children (null);
            if (!tree_view.get_dest_row_at_pos (x, y, out path, out drop_position)) {
                warning ("compute_drop position dest_row_at_pos UNKNOWN");
                return false;
            }

            int row = (path.get_indices ()) [0];
            if (row == 1 || row == 2) {
                /* Hardcoded shortcuts can only be dragged into */
                drop_position = Gtk.TreeViewDropPosition.INTO_OR_BEFORE;
            } else if (row >= num_rows) {
                row = num_rows - 1; /* row not used after this?? */
                drop_position = Gtk.TreeViewDropPosition.AFTER;
            } else if (drop_position != Gtk.TreeViewDropPosition.BEFORE
                    && received_drag_data
                    && drag_data_info == TargetType.GTK_TREE_MODEL_ROW)
                /* bookmark rows are never dragged into other bookmark rows */
                drop_position = Gtk.TreeViewDropPosition.AFTER;

            if (path.get_depth () == 1)
                return false;

            return true;
        }

/* BOOKMARK/SHORTCUT FUNCTIONS */

        private void open_selected_bookmark (Gtk.TreeModel model,
                                             Gtk.TreePath path,
                                             Marlin.OpenFlag flags) {
            if (path == null)
                return;

            Gtk.TreeIter iter;
            if (!store.get_iter (out iter, path))
                return;

            string? uri = null;
            Marlin.PluginCallbackFunc? f = null;
            store.@get (iter, Column.URI, out uri, Column.PLUGIN_CALLBACK, out f);

            if (uri != null) {
                var location = File.new_for_uri (uri);
                /* Navigate to the clicked location */
                if (flags == Marlin.OpenFlag.NEW_WINDOW) {
                    window.add_window (location, Marlin.ViewMode.CURRENT);
                } else if (flags == Marlin.OpenFlag.NEW_TAB) {
                    window.add_tab (location, Marlin.ViewMode.CURRENT);
                } else {
                    window.file_path_change_request (location);
                }
            } else if (f != null) {
                f (this);
            } else if (!ejecting_or_unmounting) {
                Drive drive;
                Volume volume;

                var mount_op = new Gtk.MountOperation (window);
                store.@get (iter,
                            Column.DRIVE, out drive,
                            Column.VOLUME, out volume);

                if (volume != null && !mounting)
                    mount_volume (volume, mount_op, flags);

                else if (drive != null && volume == null
                        && (drive.can_start () || drive.can_start_degraded ()))
                    start_drive (drive, mount_op);
            }
        }

        private void mount_volume (Volume volume, Gtk.MountOperation mount_op, Marlin.OpenFlag flags) {
            mounting = true;
            volume.mount.begin (GLib.MountMountFlags.NONE,
                                mount_op,
                                null,
                                (obj, res) => {
                try {
                    mounting = false;
                    volume.mount.end (res);
                    Mount mount = volume.get_mount ();
                    if (mount != null) {
                        var location = mount.get_default_location ();
                        if (flags == Marlin.OpenFlag.NEW_WINDOW) {
                            var app = Marlin.Application.get ();
                            app.create_window (location, window.get_screen ());
                        } else if (flags == Marlin.OpenFlag.NEW_TAB) {
                            window.add_tab (location, Marlin.ViewMode.CURRENT);
                        } else {
                            window.file_path_change_request (location);
                        }
                    }
                }
                catch (GLib.Error error) {
                    warning ("Error mounting volume %s: %s", volume.get_name (), error.message);
                }
            });
        }

        private void start_drive (Drive drive, Gtk.MountOperation mount_op) {
            drive.start.begin (DriveStartFlags.NONE,
                               mount_op,
                               null,
                               (obj, res) => {
                    try {
                        drive.start.end (res);
                    }
                    catch (GLib.Error error) {
                            var primary = _("Unable to start %s".printf (drive.get_name ()));
                            Eel.show_error_dialog (primary, error.message, null);
                    }
                }
            );
        }

        private void rename_selected_bookmark () {
            Gtk.TreeIter iter;
            if (!get_selected_iter ( out iter))
                return;

            if (!bookmark_at_iter (iter))
                 return;

            var path = store.get_path (iter);
            var column = tree_view.get_column (0);
            name_renderer.editable = true;
            renaming = true;
            tree_view.set_cursor_on_cell (path, column, name_renderer, true);
            /* Restore vertical scroll adjustment to stop tree_view scrolling to top on rename
             * For some reason, scroll to cell does not always work here
             */
            ((this as Gtk.ScrolledWindow).get_vadjustment ()).set_value (adjustment_val);
        }

        private void remove_selected_bookmarks () {
            Gtk.TreeIter iter;
            if (!get_selected_iter (out iter))
                return;

            remove_bookmark_iter (iter);
        }

        private void remove_bookmark_iter (Gtk.TreeIter? iter) {
            if (iter == null)
                return;

            if (!bookmark_at_iter (iter))
                 return;

            uint index;
            store.@get (iter, Column.INDEX, out index);
            index = index <= n_builtins_before ? 0 : index - n_builtins_before;
            bookmarks.delete_item_at (index);
        }

        /* Reorder the selected bookmark to the specified position */
        private void reorder_bookmarks (uint new_position) {
            if (drag_row_ref != null) {
                Gtk.TreeIter iter;
                store.get_iter (out iter, drag_row_ref.get_path ());
                drag_row_ref = null;

                if (!bookmark_at_iter (iter))
                    return;

                uint old_position;
                store.@get (iter, Column.INDEX, out old_position);

                if (old_position <= n_builtins_before)
                    old_position = 0;
                else
                    old_position-= n_builtins_before;

                if (old_position >= bookmarks.length ())
                    return;

                bookmarks.move_item (old_position, new_position);
            }
        }

/* POPUP MENU FUNCTIONS */

        private void build_popup_menu () {
            if (popupmenu != null)
                return;

            popupmenu = new Gtk.Menu ();
            popupmenu.attach_to_widget ((Gtk.Widget)this, (Gtk.MenuDetachFunc)popup_menu_detach_cb);

            var item = new Gtk.ImageMenuItem.with_mnemonic (_("Open"));
            var image = new Gtk.Image.from_icon_name ("document-open", Gtk.IconSize.MENU);

            item.set_image (image);
            item.activate.connect (open_shortcut_cb);
            item.show ();
            popupmenu.append (item);

            item = new Gtk.ImageMenuItem.with_mnemonic (_("Open in New _Tab"));
            popupmenu_open_in_new_tab_item = item;
            item.activate.connect (open_shortcut_in_new_tab_cb);
            item.show ();
            popupmenu.append (item);

            item = new Gtk.ImageMenuItem.with_mnemonic (_("Open in New _Window"));
            popupmenu_open_in_new_window_item = item;
            item.activate.connect (open_shortcut_in_new_window_cb);
            item.show ();
            popupmenu.append (item);

            popupmenu_separator_item1 = Eel.gtk_menu_append_separator (popupmenu);

            item = new Gtk.ImageMenuItem.with_label (_("Remove"));
            popupmenu_remove_item = item;
            image = new Gtk.Image.from_icon_name ("list-remove", Gtk.IconSize.MENU);
            item.set_image (image);
            item.activate.connect (remove_shortcut_cb);
            item.show ();
            popupmenu.append (item);

            item = new Gtk.ImageMenuItem.with_label (_("Rename"));
            popupmenu_rename_item = item;
            item.activate.connect (rename_shortcut_cb);
            item.show ();
            popupmenu.append (item);

            /* Mount/Unmount/Eject menu items */
            popupmenu_separator_item2 = Eel.gtk_menu_append_separator (popupmenu);

            item = new Gtk.ImageMenuItem.with_mnemonic (_("_Mount"));
            popupmenu_mount_item = item;
            item.activate.connect (mount_shortcut_cb);
            item.show ();
            popupmenu.append (item);

            item = new Gtk.ImageMenuItem.with_mnemonic (_("_Unmount"));
            popupmenu_unmount_item = item;
            item.activate.connect (eject_or_unmount_shortcut_cb);
            item.show ();
            popupmenu.append (item);

            item = new Gtk.ImageMenuItem.with_mnemonic (_("_Eject"));
            popupmenu_eject_item = item;
            item.activate.connect (eject_or_unmount_shortcut_cb);
            item.show ();
            popupmenu.append (item);

            /* Empty Trash menu item */
            item = new Gtk.ImageMenuItem.with_mnemonic (_("Empty _Trash"));
            popupmenu_empty_trash_item = item;
            item.activate.connect (empty_trash_cb);
            item.show ();
            popupmenu.append (item);
            check_popup_sensitivity ();
        }

        private void update_popup_menu () {
            build_popup_menu ();
        }

        private new void popup_menu (Gdk.EventButton? event) {
            update_popup_menu ();
            Eel.pop_up_context_menu (popupmenu,
                                     Eel.DEFAULT_POPUP_MENU_DISPLACEMENT,
                                     Eel.DEFAULT_POPUP_MENU_DISPLACEMENT,
                                     event);
        }

        /* Callback used when the file list's popup menu is detached */
        public void popup_menu_detach_cb (Gtk.Widget attach_widget, Gtk.Menu menu) {
            popupmenu = null;
            popupmenu_remove_item = null;
            popupmenu_rename_item = null;
            popupmenu_separator_item1 = null;
            popupmenu_separator_item2 = null;
            popupmenu_mount_item = null;
            popupmenu_unmount_item = null;
            popupmenu_eject_item = null;
            popupmenu_rescan_item = null;
            popupmenu_format_item = null;
            popupmenu_start_item = null;
            popupmenu_stop_item = null;
            popupmenu_empty_trash_item = null;
        }

        /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
        private bool popup_menu_cb (Gtk.Widget widget) {
            popup_menu (null);
            return true;
        }

/* TREEVIEW FUNCTIONS */

        private  bool get_selected_iter (out Gtk.TreeIter iter) {
            return (tree_view.get_selection ()).get_selected (null, out iter);
        }

        private void set_matching_selection (string? location) {
            /* set selection if any place matches location */
            /* first matching place is selected */
            /* Matching is done by comparing GLib.Files made from uris so that */
            /* different but equivalent uris are matched */

            var selection = tree_view.get_selection ();
            selection.unselect_all ();
            if (location == null)
                return;

            var file1 = GLib.File.new_for_path (location);

            Gtk.TreeIter iter;
            bool valid = store.get_iter_first (out iter);

            while (valid) {
                Gtk.TreeIter child_iter;
                bool child_valid = store.iter_children (out child_iter, iter);
                while (child_valid) {
                    string uri;
                    store.@get (child_iter, Column.URI, out uri);
                    if (uri == null)
                        break;

                    var file2 = GLib.File.new_for_path (uri);
                    if (file1.equal (file2)) {
                        selection.select_iter (child_iter);
                        this.last_selected_uri = location;
                        valid = false; /* escape from outer loop */
                        break;
                    }
                    child_valid = store.iter_next (ref child_iter);
                }
                valid = valid && store.iter_next (ref iter);
            }
        }

        private void edited (Gtk.CellRendererText cell, string path_string, string new_text) {
            editing_canceled (cell);

            var path = new Gtk.TreePath.from_string (path_string);

            Gtk.TreeIter iter;
            store.get_iter (out iter, path);

            uint index;
            store.@get (iter, Column.INDEX, out index);
            index-= this.n_builtins_before;

            Marlin.Bookmark? bookmark = this.bookmarks.item_at (index);
            if (bookmark != null) {
                bookmark.label = new_text;
                update_places ();
            }
        }

        private void editing_canceled (Gtk.CellRenderer cell) {
            ((Gtk.CellRendererText)cell).editable = false;
            renaming = false;
        }

        private void icon_cell_data_func (Gtk.CellLayout layout,
                                          Gtk.CellRenderer cell,
                                          Gtk.TreeModel model,
                                          Gtk.TreeIter iter) {
            cell.set_visible (!store.iter_has_child (iter));
        }

        private void indent_cell_data_func (Gtk.CellLayout layout,
                                                 Gtk.CellRenderer cell,
                                                 Gtk.TreeModel model,
                                                 Gtk.TreeIter iter) {
            var path = store.get_path (iter);
            var depth = path.get_depth ();
            cell.xpad = depth > 1 ? ICON_XPAD : ROOT_INDENTATION_XPAD;
        }

        private void expander_cell_data_func (Gtk.CellLayout layout,
                                                 Gtk.CellRenderer cell,
                                                 Gtk.TreeModel model,
                                                 Gtk.TreeIter iter) {
            Marlin.PlaceType type;
            store.@get (iter, Column.ROW_TYPE, out type, -1);

            if (type == Marlin.PlaceType.PERSONAL_CATEGORY ||
                type == Marlin.PlaceType.STORAGE_CATEGORY ||
                type == Marlin.PlaceType.NETWORK_CATEGORY ||
                type == Marlin.PlaceType.BOOKMARKS_CATEGORY)
                expander_renderer.visible = true;
            else
                expander_renderer.visible = false;
        }

        private void expander_update_pref_state (Marlin.PlaceType type, bool flag) {
            switch (type) {
                case Marlin.PlaceType.NETWORK_CATEGORY:
                    Preferences.settings.set_boolean ("sidebar-cat-network-expander", flag);
                    break;
                case Marlin.PlaceType.STORAGE_CATEGORY:
                    Preferences.settings.set_boolean ("sidebar-cat-devices-expander", flag);
                    break;
                case Marlin.PlaceType.BOOKMARKS_CATEGORY:
                    Preferences.settings.set_boolean ("sidebar-cat-personal-expander", flag);
                    break;
            }
        }

        private void expander_init_pref_state (Gtk.TreeView tree_view) {
            var path = new Gtk.TreePath.from_indices (0,-1);
            if (Preferences.settings.get_boolean ("sidebar-cat-personal-expander"))
                tree_view.expand_row (path, false);
            else
                tree_view.collapse_row (path);

            path = new Gtk.TreePath.from_indices (1,-1);
            if (Preferences.settings.get_boolean ("sidebar-cat-devices-expander"))
                tree_view.expand_row (path, false);
            else
                tree_view.collapse_row (path);

            path = new Gtk.TreePath.from_indices (2,-1);
            if (Preferences.settings.get_boolean ("sidebar-cat-network-expander"))
                tree_view.expand_row (path, false);
            else
                tree_view.collapse_row (path);
        }


        private void category_renderer_func (Gtk.CellLayout layout,
                                             Gtk.CellRenderer renderer,
                                             Gtk.TreeModel model,
                                             Gtk.TreeIter iter) {

            var crt = renderer as Gtk.CellRendererText;
            Marlin.PlaceType type;
            model.@get (iter, Column.ROW_TYPE, out type, -1);

            if (type == Marlin.PlaceType.PERSONAL_CATEGORY ||
                type == Marlin.PlaceType.STORAGE_CATEGORY ||
                type == Marlin.PlaceType.NETWORK_CATEGORY ||
                type == Marlin.PlaceType.BOOKMARKS_CATEGORY) {

                crt.weight = 900;
                crt.weight_set = true;
                crt.height = 20;
            } else {
                crt.weight_set = false;
                crt.height = -1;
            }
        }

        private bool tree_selection_func (Gtk.TreeSelection selection,
                                          Gtk.TreeModel model,
                                          Gtk.TreePath path,
                                          bool path_currently_selected) {
        /* Don't allow categories to be selected. */
            return !category_at_path (path);
        }

        private void category_row_expanded_event_cb (Gtk.TreeView tree,
                                                     Gtk.TreeIter iter,
                                                     Gtk.TreePath path) {
            Marlin.PlaceType type;
            store.@get (iter, Column.ROW_TYPE, out type);
            expander_update_pref_state (type, true);
        }

        private void category_row_collapsed_event_cb (Gtk.TreeView tree,
                                                      Gtk.TreeIter iter,
                                                      Gtk.TreePath path) {
            Marlin.PlaceType type;
            store.@get (iter, Column.ROW_TYPE, out type);
            expander_update_pref_state (type, false);
        }

        /* Callback used when the selection in the shortcuts tree changes */
        private void selection_changed_cb () {
            check_popup_sensitivity ();
        }

        private void row_activated_callback (Gtk.TreePath path,
                                             Gtk.TreeViewColumn column) {
            open_selected_bookmark ((Gtk.TreeModel)store, path, 0);
        }

/* KEY, BUTTON AND SCROLL EVENT HANDLERS */

       /* Callback used when a button is pressed on the shortcuts list.
         * We trap button 3 to bring up a popup menu, and button 2 to
         * open in a new tab.
         */
        private bool key_press_event_cb (Gtk.Widget widget, Gdk.EventKey event) {
            Gdk.ModifierType modifiers = Gtk.accelerator_get_default_mod_mask ();
            if (event.keyval == Gdk.Key.Down
             && (event.state & modifiers) == Gdk.ModifierType.MOD1_MASK)
                return eject_or_unmount_selection ();

            if (event.keyval == Gdk.Key.F2 && (event.state & modifiers) == 0) {
                rename_selected_bookmark ();
                return true;
            }

            return false;
        }

        private bool button_press_event_cb (Gtk.Widget widget, Gdk.EventButton event) {
            click_path = null;
            var tree_view = widget as Gtk.TreeView;
            if (event.window != tree_view.get_bin_window ())
                return true;

            if (renaming)
                return true;

            Gtk.TreePath? path = get_path_at_click_position (event);
            if (path == null)
                return false;

            this.click_path = path.copy ();


            switch (event.button) {
                case Gdk.BUTTON_PRIMARY:
                /* If the user clicked over a category, toggle expansion. The entire row
                 * is a valid area.
                 */
                    if (path != null && category_at_path (path)) {
                        if (tree_view.is_row_expanded (path))
                            tree_view.collapse_row (path);
                        else
                            tree_view.expand_row (path, false);

                        return true;
                    } else if (!bookmark_at_path (path))
                        block_drag_and_drop ();

                    break;

                case Gdk.BUTTON_SECONDARY:
                    if (path != null && !category_at_path (path))
                        popup_menu (event);

                    break;

                case Gdk.BUTTON_MIDDLE:
                    if (path != null && !category_at_path (path))
                        open_selected_bookmark (store, path, Marlin.OpenFlag.NEW_TAB);

                    break;
            }

            return false;
        }

        private bool button_release_event_cb (Gtk.Widget widget, Gdk.EventButton event) {
            Gtk.TreePath? path = get_path_at_click_position (event);

            if (dnd_disabled) {
                unblock_drag_and_drop ();
                /* Do not take action if a blocked drag was attempted (mouse over different row
                 * from when button pressed or not over row) */
                if (path == null || (click_path != null && path.compare (click_path) != 0)) {
                    return true;
                }
            }

            if (renaming) 
                return true;

            if (over_eject_button (event)) {
                eject_or_unmount_bookmark (path);
                return false;
            }

            if (event.button ==1) {
                if (event.window != tree_view.get_bin_window ())
                    return false;

                if (path != null) {
                    if ((event.state & Gdk.ModifierType.CONTROL_MASK) != 0)
                        open_selected_bookmark (store, path, Marlin.OpenFlag.NEW_TAB);
                    else
                        open_selected_bookmark (store, path, Marlin.OpenFlag.DEFAULT);
                }
            }

            return false;
        }

        public new void style_set (Gtk.Style previous_style) {
            update_places ();
        }

/* MOUNT UNMOUNT AND EJECT FUNCTIONS */

         private void do_unmount (Mount? mount, Gtk.TreeRowReference? row_ref = null) {
            if (mount == null)
                return;

            /* Do not offer to empty trash every time - this can be done
             * from the context menu if needed */
            ejecting_or_unmounting = true;
            GLib.MountOperation mount_op = new Gtk.MountOperation (window as Gtk.Window);
            mount.unmount_with_operation.begin (GLib.MountUnmountFlags.NONE,
                                                mount_op,
                                                null,
                                                (obj, res) => {
                try {
                    mount.unmount_with_operation.end (res);
                }
                catch (GLib.Error error) {
                    debug ("Error while unmounting");
                }
                finish_eject_or_unmount (row_ref);
            });
         }

        private void empty_trash_on_mount (Mount? mount, Gtk.TreeRowReference? row_ref = null) {
            if (Marlin.FileOperations.has_trash_files (mount)) {
                unowned GLib.List<unowned GLib.File>? dirs = Marlin.FileOperations.get_trash_dirs_for_mount (mount);
                /* Marlin.FileOperations will show a confirm dialog according to settings */
                if (dirs != null)
                    Marlin.FileOperations.empty_trash_dirs (null, dirs.copy ());
            }
        }

        private bool over_eject_button (Gdk.EventButton event) {
            unowned Gtk.TreeViewColumn column;
            int width, x_offset, hseparator;
            bool show_eject;
            Gtk.TreeIter iter;
            Gtk.TreePath path;

            int x, y;
            tree_view.convert_bin_window_to_tree_coords ((int)event.x, (int)event.y, out x, out y);

            int cell_x, cell_y;
            if (tree_view.get_path_at_pos (x, y, out path, out column, out cell_x, out cell_y)) {
                if (path == null)
                    return false;

                store.get_iter (out iter, path);
                store.@get (iter, Column.EJECT, out show_eject);

                if (!show_eject || ejecting_or_unmounting)
                    return false;

                tree_view.style_get ("horizontal-separator", out hseparator, null);
                /* reload the cell attributes for this particular row */
                column.cell_set_cell_data (store, iter, false, false);
                column.cell_get_position (eject_spinner_cell_renderer, out x_offset, out width);

                x_offset += width - hseparator - eject_button_size;
                if (cell_x - x_offset >= 0 && cell_x - x_offset <= eject_button_size)
                    return true;
            }

            return false;
        }

        private void do_eject (GLib.Mount? mount, GLib.Volume? volume, GLib.Drive? drive, Gtk.TreeRowReference? row_ref = null) {
            GLib.MountOperation mount_op = new GLib.MountOperation ();

            if (drive != null) {
                ejecting_or_unmounting = true;
                drive.eject_with_operation.begin (GLib.MountUnmountFlags.NONE,
                                                  mount_op,
                                                  null,
                                                  (obj, res) => {
                    try {
                        drive.eject_with_operation.end (res);
                    }
                    catch (GLib.Error error) {
                        warning ("Error ejecting drive: %s", error.message);
                    }
                    finish_eject_or_unmount (row_ref);
                });
                return;
            }

            if (volume != null){
                ejecting_or_unmounting = true;
                volume.eject_with_operation.begin (GLib.MountUnmountFlags.NONE,
                                                   mount_op,
                                                   null,
                                                   (obj, res) => {
                    try {
                        volume.eject_with_operation.end (res);
                    }
                    catch (GLib.Error error) {
                        warning ("Error ejecting volume: %s", error.message);
                    }
                    finish_eject_or_unmount (row_ref);
                });
                return;
            }

            if (mount != null){
                ejecting_or_unmounting = true;
                mount.eject_with_operation.begin (GLib.MountUnmountFlags.NONE,
                                                  mount_op,
                                                  null,
                                                  (obj, res) => {
                    try {
                        mount.eject_with_operation.end (res);
                    }
                    catch (GLib.Error error) {
                        warning ("Error ejecting mount: %s", error.message);
                    }
                    finish_eject_or_unmount (row_ref);
                });
                return;
            }
        }

        private void finish_eject_or_unmount (Gtk.TreeRowReference? row_ref) {
            ejecting_or_unmounting = false;
            if (row_ref != null && row_ref.valid ()) {
                Gtk.TreeIter iter;
                if (store.get_iter (out iter, row_ref.get_path ()))
                    store.@set (iter, Column.SHOW_SPINNER, false) ;
            }
        }

        private bool eject_or_unmount_bookmark (Gtk.TreePath? path) {
            if (path == null || ejecting_or_unmounting)
                return false;

            Gtk.TreeIter iter;
            if (!store.get_iter (out iter, path))
                return false;

            Mount mount;
            Volume volume;
            Drive drive;
            bool spinner_active;
            store.@get (iter,
                        Column.MOUNT, out mount,
                        Column.VOLUME, out volume,
                        Column.DRIVE, out drive,
                        Column.SHOW_SPINNER, out spinner_active);

            /* Return if already ejecting */
            if (spinner_active)
                return true;

            bool can_unmount, can_eject;
            check_unmount_and_eject (mount, volume, drive, out can_unmount, out can_eject);

            if (!(can_eject || can_unmount))
                return false;

            var rowref = new Gtk.TreeRowReference (store, path);
            store.@set (iter, Column.SHOW_SPINNER, true);

            Timeout.add (100, ()=>{
                uint val;

                if (!rowref.valid ())
                    return false;

                store.@get (iter, Column.SHOW_SPINNER, out spinner_active);
                if (!spinner_active)
                    return false;

                store.@get (iter, Column.SPINNER_PULSE, out val);
                store.@set (iter, Column.SPINNER_PULSE, ++val);
                return true;
            });

            if (can_eject)
                do_eject (mount, volume, drive, rowref);
            else if (can_unmount)
                do_unmount (mount, rowref);

            return true;
        }

        private bool eject_or_unmount_selection () {
            Gtk.TreeIter iter;
            if (!get_selected_iter (out iter))
                return false;
            else
                return eject_or_unmount_bookmark (store.get_path (iter));
        }

/* POPUP MENU CALLBACK FUNCTIONS */

        private void open_shortcut_from_menu (Marlin.OpenFlag flags) {
            Gtk.TreePath path;
            tree_view.get_cursor (out path, null);
            open_selected_bookmark (store, path, flags);
        }

        private void open_shortcut_cb (Gtk.MenuItem item) {
            open_shortcut_from_menu (Marlin.OpenFlag.DEFAULT);
        }

        private void open_shortcut_in_new_window_cb (Gtk.MenuItem item) {
            open_shortcut_from_menu (Marlin.OpenFlag.NEW_WINDOW);
        }

        private void open_shortcut_in_new_tab_cb (Gtk.MenuItem item) {
            open_shortcut_from_menu (Marlin.OpenFlag.NEW_TAB);
        }

        private void mount_shortcut_cb (Gtk.MenuItem item) {
            Gtk.TreeIter iter;
            if (!get_selected_iter (out iter))
                return;

            Volume volume;
            store.@get (iter, Column.VOLUME, out volume);
            if (volume != null)
                Marlin.FileOperations.mount_volume (null, volume, false);
         }

        private void remove_shortcut_cb (Gtk.MenuItem item) {
            remove_selected_bookmarks ();
        }

        private void rename_shortcut_cb (Gtk.MenuItem item) {
            rename_selected_bookmark ();
        }

        private void eject_or_unmount_shortcut_cb (Gtk.MenuItem item) {
            Gtk.TreeIter iter;
            if (!get_selected_iter (out iter))
                return;
            else
                eject_or_unmount_bookmark (store.get_path (iter));
        }

        private void empty_trash_cb (Gtk.MenuItem item) {
            Gtk.TreeIter iter;
            if (!get_selected_iter (out iter))
                return;

            Mount mount;
            string uri;
            store.@get (iter,
                        Column.URI, out uri,
                        Column.MOUNT, out mount);

            if (mount != null)
                /* A particular mount was clicked - empty only the trash on the mount */
                empty_trash_on_mount (mount);
            else
                /* Trash icon was clicked - empty all trash directories, including any mounted. */
                Marlin.FileOperations.empty_trash (window);
        }

/* VOLUME MONITOR CALLBACK FUNCTIONS */

        private void mount_added_callback (Mount mount) {
            update_places ();
        }
        private void mount_removed_callback (Mount mount) {
            update_places ();
        }
        private void mount_changed_callback (Mount mount) {
            update_places ();
        }

        private void volume_added_callback (Volume volume) {
            update_places ();
        }

        private void volume_removed_callback (Volume volume) {
            update_places ();
        }

        private void volume_changed_callback (Volume volume) {
            update_places ();
        }

        private void drive_connected_callback (VolumeMonitor volume_monitor, Drive drive) {
            update_places ();
        }

        private void drive_disconnected_callback (VolumeMonitor volume_monitor, Drive drive) {
            update_places ();
        }

        private void drive_changed_callback (VolumeMonitor volume_monitor, Drive drive) {
            if (ejecting_or_unmounting)
                return;

            if (!drive.is_media_check_automatic ()) {
                drive.poll_for_media.begin (null, (obj, res) => {
                    try {
                        if (drive.poll_for_media.end (res)) 
                            eject_drive_if_no_media (drive);
                    }
                    catch (GLib.Error e) {
                        warning ("Could not poll for media");
                    }
                });
            } else 
                 eject_drive_if_no_media (drive);
        }

        private void eject_drive_if_no_media (Drive drive) {
            if (!drive.has_media () && drive.can_eject ())
                do_eject (null, null, drive, null);
        }

/* MISCELLANEOUS CALLBACK FUNCTIONS */

        private void icon_theme_changed_callback (Gtk.IconTheme icon_theme) {
            get_eject_icon ();
            update_places ();
        }

        private void loading_uri_callback (string location) {
                set_matching_selection (location);
                slot_location = location;
        }

        private void trash_state_changed_cb (Marlin.TrashMonitor trash_monitor, bool state) {
            update_places ();
            check_popup_sensitivity ();
        }

/* CHECK FUNCTIONS */
        private void check_unmount_and_eject (Mount? mount,
                                              Volume? volume,
                                              Drive? drive,
                                              out bool show_unmount,
                                              out bool show_eject) {
            show_unmount = false;
            show_eject = false;

            if (drive != null)
                show_eject = drive.can_eject ();

            if (volume != null)
                show_eject = volume.can_eject ();

            if (mount != null) {
                show_eject = mount.can_eject ();
                show_unmount = mount.can_unmount () && !show_eject;
            }
        }

        private void check_visibility (Mount? mount,
                                       Volume? volume,
                                       Drive? drive,
                                       out bool show_mount,
                                       out bool show_unmount,
                                       out bool show_eject,
                                       out bool show_rescan,
                                       out bool show_format,
                                       out bool show_start,
                                       out bool show_stop) {
            show_mount = false;
            show_format = false;
            show_rescan = false;
            show_start = false;
            show_stop = false;

            check_unmount_and_eject (mount, volume, drive, out show_unmount, out show_eject);

            if (drive != null) {
                if (drive.is_media_removable () &&
                    !drive.is_media_check_automatic () &&
                    drive.can_poll_for_media ())
                        show_rescan = true;

                show_start = drive.can_start ()|| drive.can_start_degraded ();
                show_stop = drive.can_stop ();

                if (show_stop)
                    show_unmount = false;
            }

            if (volume != null && mount == null)
                show_mount = volume.can_mount ();
        }

        private void check_popup_sensitivity () {
            if (popupmenu == null)
                return;

            Gtk.TreeIter iter;
            if (!get_selected_iter (out iter))
                return;

            Marlin.PlaceType type;
            Drive drive;
            Volume volume;
            Mount mount;
            string uri;
            bool is_bookmark;
            store.@get (iter,
                        Column.ROW_TYPE, out type,
                        Column.DRIVE, out drive,
                        Column.VOLUME, out volume,
                        Column.MOUNT, out mount,
                        Column.URI, out uri,
                        Column.BOOKMARK, out is_bookmark);

            popupmenu_open_in_new_tab_item.show ();
            Eel.gtk_widget_set_shown (popupmenu_remove_item, is_bookmark);
            Eel.gtk_widget_set_shown (popupmenu_rename_item, is_bookmark);
            Eel.gtk_widget_set_shown (popupmenu_separator_item1, is_bookmark);

            bool show_mount, show_unmount, show_eject, show_rescan, show_format, show_start, show_stop;
            check_visibility (mount,
                              volume,
                              drive,
                              out show_mount,
                              out show_unmount,
                              out show_eject,
                              out show_rescan,
                              out show_format,
                              out show_start,
                              out show_stop);

            /* Context menu shows Empty Trash for the Trash icon and for any mount with a native
             * file system whose trash contains files */
            bool show_empty_trash = (uri != null) &&
                                    ((uri == Marlin.TRASH_URI) ||
                                    Marlin.FileOperations.has_trash_files (mount));

            Eel.gtk_widget_set_shown (popupmenu_separator_item2,
                                      show_eject || show_unmount ||
                                      show_mount || show_empty_trash);

            Eel.gtk_widget_set_shown (popupmenu_mount_item, show_mount);
            Eel.gtk_widget_set_shown (popupmenu_unmount_item, show_unmount);
            Eel.gtk_widget_set_shown (popupmenu_eject_item, show_eject);
            Eel.gtk_widget_set_shown (popupmenu_empty_trash_item, show_empty_trash);
            popupmenu_empty_trash_item.set_sensitive (!(Marlin.TrashMonitor.is_empty ()));

            bool is_plugin = (type == Marlin.PlaceType.PLUGIN_ITEM);
            Eel.gtk_widget_set_shown (popupmenu_open_in_new_tab_item, !is_plugin);
            Eel.gtk_widget_set_shown (popupmenu_open_in_new_window_item, !is_plugin);
        }

        /**
         * Checks whether a tree path points to a main category.
         */
        private bool category_at_path (Gtk.TreePath path) {
        /* We determine whether an item is a category based on its level indentation.
         * According to the current implementation, a level of 1 (i.e. root) necessarily
         * means that the item is a category.
         */
            return path.get_depth () == 1;
        }

        private bool bookmark_at_path (Gtk.TreePath? path) {
            if (path != null) {
                Gtk.TreeIter iter;
                store.get_iter (out iter, path);
                return bookmark_at_iter (iter);
            } else
                return false;
        }

        private bool bookmark_at_iter (Gtk.TreeIter? iter) {
            if (iter == null)
                return false;

            bool is_bookmark;
            store.@get (iter, Column.BOOKMARK, out is_bookmark, -1);
            return is_bookmark;
        }

        private Gtk.TreePath? get_path_at_click_position (Gdk.EventButton event) {
            int tx, ty;
            tree_view.convert_bin_window_to_tree_coords ((int)event.x, (int)event.y, out tx, out ty);
            Gtk.TreePath? path = null;
            tree_view.get_path_at_pos (tx, ty, out path, null, null, null);
            return path;
        }

        protected void block_drag_and_drop () {
            tree_view.unset_rows_drag_source ();
            dnd_disabled = true;
        }

        protected void unblock_drag_and_drop () {
            tree_view.enable_model_drag_source (Gdk.ModifierType.BUTTON1_MASK,
                                                source_targets,
                                                Gdk.DragAction.MOVE);
            dnd_disabled = false;
        }

    }
}