1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
/*
 * lapiz-file-browser-store.c - Lapiz plugin providing easy file access
 * from the sidepanel
 *
 * Copyright (C) 2006 - Jesse van den Kieboom <jesse@icecrew.nl>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 */

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

#include <string.h>
#include <glib/gi18n-lib.h>
#include <gio/gio.h>

#include "lapiz-file-browser-store.h"
#include "lapiz-file-browser-marshal.h"
#include "lapiz-file-browser-enum-types.h"
#include "lapiz-file-browser-error.h"
#include "lapiz-file-browser-utils.h"

#define NODE_IS_DIR(node)		(FILE_IS_DIR((node)->flags))
#define NODE_IS_HIDDEN(node)		(FILE_IS_HIDDEN((node)->flags))
#define NODE_IS_TEXT(node)		(FILE_IS_TEXT((node)->flags))
#define NODE_LOADED(node)		(FILE_LOADED((node)->flags))
#define NODE_IS_FILTERED(node)		(FILE_IS_FILTERED((node)->flags))
#define NODE_IS_DUMMY(node)		(FILE_IS_DUMMY((node)->flags))

#define FILE_BROWSER_NODE_DIR(node)	((FileBrowserNodeDir *)(node))

#define DIRECTORY_LOAD_ITEMS_PER_CALLBACK 100
#define STANDARD_ATTRIBUTE_TYPES G_FILE_ATTRIBUTE_STANDARD_TYPE "," \
				 G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN "," \
			 	 G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP "," \
				 G_FILE_ATTRIBUTE_STANDARD_NAME "," \
				 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE "," \
				 G_FILE_ATTRIBUTE_STANDARD_ICON

typedef struct _FileBrowserNode    FileBrowserNode;
typedef struct _FileBrowserNodeDir FileBrowserNodeDir;
typedef struct _AsyncData	   AsyncData;
typedef struct _AsyncNode	   AsyncNode;

typedef gint (*SortFunc) (FileBrowserNode * node1,
			  FileBrowserNode * node2);

struct _AsyncData
{
	LapizFileBrowserStore * model;
	GCancellable * cancellable;
	gboolean trash;
	GList * files;
	GList * iter;
	gboolean removed;
};

struct _AsyncNode
{
	FileBrowserNodeDir *dir;
	GCancellable *cancellable;
	GSList *original_children;
};

typedef struct {
	LapizFileBrowserStore * model;
	gchar * virtual_root;
	GMountOperation * operation;
	GCancellable * cancellable;
} MountInfo;

struct _FileBrowserNode
{
	GFile *file;
	guint flags;
	gchar *name;

	GdkPixbuf *icon;
	GdkPixbuf *emblem;

	FileBrowserNode *parent;
	gint pos;
	gboolean inserted;
};

struct _FileBrowserNodeDir
{
	FileBrowserNode node;
	GSList *children;

	GCancellable *cancellable;
	GFileMonitor *monitor;
	LapizFileBrowserStore *model;
};

struct _LapizFileBrowserStorePrivate
{
	FileBrowserNode *root;
	FileBrowserNode *virtual_root;
	GType column_types[LAPIZ_FILE_BROWSER_STORE_COLUMN_NUM];

	LapizFileBrowserStoreFilterMode filter_mode;
	LapizFileBrowserStoreFilterFunc filter_func;
	gpointer filter_user_data;

	SortFunc sort_func;

	GSList *async_handles;
	MountInfo *mount_info;
};

static FileBrowserNode *model_find_node 		    (LapizFileBrowserStore *model,
							     FileBrowserNode *node,
							     GFile *uri);
static void model_remove_node                               (LapizFileBrowserStore * model,
							     FileBrowserNode * node,
							     CtkTreePath * path,
							     gboolean free_nodes);

static void set_virtual_root_from_node                      (LapizFileBrowserStore * model,
				                             FileBrowserNode * node);

static void lapiz_file_browser_store_iface_init             (CtkTreeModelIface * iface);
static CtkTreeModelFlags lapiz_file_browser_store_get_flags (CtkTreeModel * tree_model);
static gint lapiz_file_browser_store_get_n_columns          (CtkTreeModel * tree_model);
static GType lapiz_file_browser_store_get_column_type       (CtkTreeModel * tree_model,
							     gint index);
static gboolean lapiz_file_browser_store_get_iter           (CtkTreeModel * tree_model,
							     CtkTreeIter * iter,
							     CtkTreePath * path);
static CtkTreePath *lapiz_file_browser_store_get_path       (CtkTreeModel * tree_model,
							     CtkTreeIter * iter);
static void lapiz_file_browser_store_get_value              (CtkTreeModel * tree_model,
							     CtkTreeIter * iter,
							     gint column,
							     GValue * value);
static gboolean lapiz_file_browser_store_iter_next          (CtkTreeModel * tree_model,
							     CtkTreeIter * iter);
static gboolean lapiz_file_browser_store_iter_children      (CtkTreeModel * tree_model,
							     CtkTreeIter * iter,
							     CtkTreeIter * parent);
static gboolean lapiz_file_browser_store_iter_has_child     (CtkTreeModel * tree_model,
							     CtkTreeIter * iter);
static gint lapiz_file_browser_store_iter_n_children        (CtkTreeModel * tree_model,
							     CtkTreeIter * iter);
static gboolean lapiz_file_browser_store_iter_nth_child     (CtkTreeModel * tree_model,
							     CtkTreeIter * iter,
							     CtkTreeIter * parent,
							     gint n);
static gboolean lapiz_file_browser_store_iter_parent        (CtkTreeModel * tree_model,
							     CtkTreeIter * iter,
							     CtkTreeIter * child);
static void lapiz_file_browser_store_row_inserted	    (CtkTreeModel * tree_model,
							     CtkTreePath * path,
							     CtkTreeIter * iter);

static void lapiz_file_browser_store_drag_source_init       (CtkTreeDragSourceIface * iface);
static gboolean lapiz_file_browser_store_row_draggable      (CtkTreeDragSource * drag_source,
							     CtkTreePath       * path);
static gboolean lapiz_file_browser_store_drag_data_delete   (CtkTreeDragSource * drag_source,
							     CtkTreePath       * path);
static gboolean lapiz_file_browser_store_drag_data_get      (CtkTreeDragSource * drag_source,
							     CtkTreePath       * path,
							     CtkSelectionData  * selection_data);

static void file_browser_node_free                          (LapizFileBrowserStore * model,
							     FileBrowserNode * node);
static void model_add_node                                  (LapizFileBrowserStore * model,
							     FileBrowserNode * child,
							     FileBrowserNode * parent);
static void model_clear                                     (LapizFileBrowserStore * model,
							     gboolean free_nodes);
static gint model_sort_default                              (FileBrowserNode * node1,
							     FileBrowserNode * node2);
static void model_check_dummy                               (LapizFileBrowserStore * model,
							     FileBrowserNode * node);
static void next_files_async 				    (GFileEnumerator * enumerator,
							     AsyncNode * async);

static void delete_files                                    (AsyncData              *data);

G_DEFINE_DYNAMIC_TYPE_EXTENDED (LapizFileBrowserStore, lapiz_file_browser_store,
                                G_TYPE_OBJECT,
                                0,
                                G_ADD_PRIVATE_DYNAMIC (LapizFileBrowserStore)
                                G_IMPLEMENT_INTERFACE_DYNAMIC (CTK_TYPE_TREE_MODEL,
                                                               lapiz_file_browser_store_iface_init)
                                G_IMPLEMENT_INTERFACE_DYNAMIC (CTK_TYPE_TREE_DRAG_SOURCE,
                                                               lapiz_file_browser_store_drag_source_init))

/* Properties */
enum {
	PROP_0,

	PROP_ROOT,
	PROP_VIRTUAL_ROOT,
	PROP_FILTER_MODE
};

/* Signals */
enum
{
	BEGIN_LOADING,
	END_LOADING,
	ERROR,
	NO_TRASH,
	RENAME,
	BEGIN_REFRESH,
	END_REFRESH,
	UNLOAD,
	NUM_SIGNALS
};

static guint model_signals[NUM_SIGNALS] = { 0 };

static void
cancel_mount_operation (LapizFileBrowserStore *obj)
{
	if (obj->priv->mount_info != NULL)
	{
		obj->priv->mount_info->model = NULL;
		g_cancellable_cancel (obj->priv->mount_info->cancellable);
		obj->priv->mount_info = NULL;
	}
}

static void
lapiz_file_browser_store_finalize (GObject * object)
{
	LapizFileBrowserStore *obj = LAPIZ_FILE_BROWSER_STORE (object);
	GSList *item;

	/* Free all the nodes */
	file_browser_node_free (obj, obj->priv->root);

	/* Cancel any asynchronous operations */
	for (item = obj->priv->async_handles; item; item = item->next)
	{
		AsyncData *data = (AsyncData *) (item->data);
		g_cancellable_cancel (data->cancellable);

		data->removed = TRUE;
	}

	cancel_mount_operation (obj);

	g_slist_free (obj->priv->async_handles);
	G_OBJECT_CLASS (lapiz_file_browser_store_parent_class)->finalize (object);
}

static void
set_gvalue_from_node (GValue          *value,
                      FileBrowserNode *node)
{
	gchar * uri;

	if (node == NULL || !node->file) {
		g_value_set_string (value, NULL);
	} else {
		uri = g_file_get_uri (node->file);
		g_value_take_string (value, uri);
	}
}

static void
lapiz_file_browser_store_get_property (GObject    *object,
			               guint       prop_id,
			               GValue     *value,
			               GParamSpec *pspec)
{
	LapizFileBrowserStore *obj = LAPIZ_FILE_BROWSER_STORE (object);

	switch (prop_id)
	{
		case PROP_ROOT:
			set_gvalue_from_node (value, obj->priv->root);
			break;
		case PROP_VIRTUAL_ROOT:
			set_gvalue_from_node (value, obj->priv->virtual_root);
			break;
		case PROP_FILTER_MODE:
			g_value_set_flags (value, obj->priv->filter_mode);
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
			break;
	}
}

static void
lapiz_file_browser_store_set_property (GObject      *object,
			               guint         prop_id,
			               const GValue *value,
			               GParamSpec   *pspec)
{
	LapizFileBrowserStore *obj = LAPIZ_FILE_BROWSER_STORE (object);

	switch (prop_id)
	{
		case PROP_FILTER_MODE:
			lapiz_file_browser_store_set_filter_mode (obj,
			                                          g_value_get_flags (value));
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
			break;
	}
}

static void
lapiz_file_browser_store_class_init (LapizFileBrowserStoreClass * klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->finalize = lapiz_file_browser_store_finalize;

	object_class->get_property = lapiz_file_browser_store_get_property;
	object_class->set_property = lapiz_file_browser_store_set_property;

	g_object_class_install_property (object_class, PROP_ROOT,
					 g_param_spec_string ("root",
					 		      "Root",
					 		      "The root uri",
					 		      NULL,
					 		      G_PARAM_READABLE));

	g_object_class_install_property (object_class, PROP_VIRTUAL_ROOT,
					 g_param_spec_string ("virtual-root",
					 		      "Virtual Root",
					 		      "The virtual root uri",
					 		      NULL,
					 		      G_PARAM_READABLE));

	g_object_class_install_property (object_class, PROP_FILTER_MODE,
					 g_param_spec_flags ("filter-mode",
					 		      "Filter Mode",
					 		      "The filter mode",
					 		      LAPIZ_TYPE_FILE_BROWSER_STORE_FILTER_MODE,
					 		      lapiz_file_browser_store_filter_mode_get_default (),
					 		      G_PARAM_READWRITE));

	model_signals[BEGIN_LOADING] =
	    g_signal_new ("begin-loading",
			  G_OBJECT_CLASS_TYPE (object_class),
			  G_SIGNAL_RUN_LAST,
			  G_STRUCT_OFFSET (LapizFileBrowserStoreClass,
					   begin_loading), NULL, NULL,
			  g_cclosure_marshal_VOID__BOXED, G_TYPE_NONE, 1,
			  CTK_TYPE_TREE_ITER);
	model_signals[END_LOADING] =
	    g_signal_new ("end-loading",
			  G_OBJECT_CLASS_TYPE (object_class),
			  G_SIGNAL_RUN_LAST,
			  G_STRUCT_OFFSET (LapizFileBrowserStoreClass,
					   end_loading), NULL, NULL,
			  g_cclosure_marshal_VOID__BOXED, G_TYPE_NONE, 1,
			  CTK_TYPE_TREE_ITER);
	model_signals[ERROR] =
	    g_signal_new ("error", G_OBJECT_CLASS_TYPE (object_class),
			  G_SIGNAL_RUN_LAST,
			  G_STRUCT_OFFSET (LapizFileBrowserStoreClass,
					   error), NULL, NULL,
			  lapiz_file_browser_marshal_VOID__UINT_STRING,
			  G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_STRING);
	model_signals[NO_TRASH] =
	    g_signal_new ("no-trash", G_OBJECT_CLASS_TYPE (object_class),
			  G_SIGNAL_RUN_LAST,
			  G_STRUCT_OFFSET (LapizFileBrowserStoreClass,
					   no_trash), g_signal_accumulator_true_handled, NULL,
			  lapiz_file_browser_marshal_BOOLEAN__POINTER,
			  G_TYPE_BOOLEAN, 1, G_TYPE_POINTER);
	model_signals[RENAME] =
	    g_signal_new ("rename",
			  G_OBJECT_CLASS_TYPE (object_class),
			  G_SIGNAL_RUN_LAST,
			  G_STRUCT_OFFSET (LapizFileBrowserStoreClass,
					   rename), NULL, NULL,
			  lapiz_file_browser_marshal_VOID__STRING_STRING,
			  G_TYPE_NONE, 2,
			  G_TYPE_STRING,
			  G_TYPE_STRING);
	model_signals[BEGIN_REFRESH] =
	    g_signal_new ("begin-refresh",
	    		  G_OBJECT_CLASS_TYPE (object_class),
	    		  G_SIGNAL_RUN_LAST,
	    		  G_STRUCT_OFFSET (LapizFileBrowserStoreClass,
	    		  		   begin_refresh), NULL, NULL,
	    		  g_cclosure_marshal_VOID__VOID,
	    		  G_TYPE_NONE, 0);
	model_signals[END_REFRESH] =
	    g_signal_new ("end-refresh",
	    		  G_OBJECT_CLASS_TYPE (object_class),
	    		  G_SIGNAL_RUN_LAST,
	    		  G_STRUCT_OFFSET (LapizFileBrowserStoreClass,
	    		  		   end_refresh), NULL, NULL,
	    		  g_cclosure_marshal_VOID__VOID,
	    		  G_TYPE_NONE, 0);
	model_signals[UNLOAD] =
	    g_signal_new ("unload",
	    		  G_OBJECT_CLASS_TYPE (object_class),
	    		  G_SIGNAL_RUN_LAST,
	    		  G_STRUCT_OFFSET (LapizFileBrowserStoreClass,
	    		  		   unload), NULL, NULL,
	    		  g_cclosure_marshal_VOID__STRING,
	    		  G_TYPE_NONE, 1,
	    		  G_TYPE_STRING);
}

static void
lapiz_file_browser_store_class_finalize (LapizFileBrowserStoreClass *klass G_GNUC_UNUSED)
{
	/* dummy function - used by G_DEFINE_DYNAMIC_TYPE_EXTENDED */
}

static void
lapiz_file_browser_store_iface_init (CtkTreeModelIface * iface)
{
	iface->get_flags = lapiz_file_browser_store_get_flags;
	iface->get_n_columns = lapiz_file_browser_store_get_n_columns;
	iface->get_column_type = lapiz_file_browser_store_get_column_type;
	iface->get_iter = lapiz_file_browser_store_get_iter;
	iface->get_path = lapiz_file_browser_store_get_path;
	iface->get_value = lapiz_file_browser_store_get_value;
	iface->iter_next = lapiz_file_browser_store_iter_next;
	iface->iter_children = lapiz_file_browser_store_iter_children;
	iface->iter_has_child = lapiz_file_browser_store_iter_has_child;
	iface->iter_n_children = lapiz_file_browser_store_iter_n_children;
	iface->iter_nth_child = lapiz_file_browser_store_iter_nth_child;
	iface->iter_parent = lapiz_file_browser_store_iter_parent;
	iface->row_inserted = lapiz_file_browser_store_row_inserted;
}

static void
lapiz_file_browser_store_drag_source_init (CtkTreeDragSourceIface * iface)
{
	iface->row_draggable = lapiz_file_browser_store_row_draggable;
	iface->drag_data_delete = lapiz_file_browser_store_drag_data_delete;
	iface->drag_data_get = lapiz_file_browser_store_drag_data_get;
}

static void
lapiz_file_browser_store_init (LapizFileBrowserStore * obj)
{
	obj->priv = lapiz_file_browser_store_get_instance_private (obj);

	obj->priv->column_types[LAPIZ_FILE_BROWSER_STORE_COLUMN_URI] =
	    G_TYPE_STRING;
	obj->priv->column_types[LAPIZ_FILE_BROWSER_STORE_COLUMN_NAME] =
	    G_TYPE_STRING;
	obj->priv->column_types[LAPIZ_FILE_BROWSER_STORE_COLUMN_FLAGS] =
	    G_TYPE_UINT;
	obj->priv->column_types[LAPIZ_FILE_BROWSER_STORE_COLUMN_ICON] =
	    GDK_TYPE_PIXBUF;
	obj->priv->column_types[LAPIZ_FILE_BROWSER_STORE_COLUMN_EMBLEM] =
	    GDK_TYPE_PIXBUF;

	// Default filter mode is hiding the hidden files
	obj->priv->filter_mode = lapiz_file_browser_store_filter_mode_get_default ();
	obj->priv->sort_func = model_sort_default;
}

static gboolean
node_has_parent (FileBrowserNode * node, FileBrowserNode * parent)
{
	if (node->parent == NULL)
		return FALSE;

	if (node->parent == parent)
		return TRUE;

	return node_has_parent (node->parent, parent);
}

static gboolean
node_in_tree (LapizFileBrowserStore * model, FileBrowserNode * node)
{
	return node_has_parent (node, model->priv->virtual_root);
}

static gboolean
model_node_visibility (LapizFileBrowserStore * model,
		       FileBrowserNode * node)
{
	if (node == NULL)
		return FALSE;

	if (NODE_IS_DUMMY (node))
		return !NODE_IS_HIDDEN (node);

	if (node == model->priv->virtual_root)
		return TRUE;

	if (!node_has_parent (node, model->priv->virtual_root))
		return FALSE;

	return !NODE_IS_FILTERED (node);
}

static gboolean
model_node_inserted (LapizFileBrowserStore * model,
		     FileBrowserNode * node)
{
	return node == model->priv->virtual_root || (model_node_visibility (model, node) && node->inserted);
}

/* Interface implementation */

static CtkTreeModelFlags
lapiz_file_browser_store_get_flags (CtkTreeModel * tree_model)
{
	g_return_val_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (tree_model),
			      (CtkTreeModelFlags) 0);

	return CTK_TREE_MODEL_ITERS_PERSIST;
}

static gint
lapiz_file_browser_store_get_n_columns (CtkTreeModel * tree_model)
{
	g_return_val_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (tree_model), 0);

	return LAPIZ_FILE_BROWSER_STORE_COLUMN_NUM;
}

static GType
lapiz_file_browser_store_get_column_type (CtkTreeModel * tree_model, gint idx)
{
	g_return_val_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (tree_model),
			      G_TYPE_INVALID);
	g_return_val_if_fail (idx < LAPIZ_FILE_BROWSER_STORE_COLUMN_NUM &&
			      idx >= 0, G_TYPE_INVALID);

	return LAPIZ_FILE_BROWSER_STORE (tree_model)->priv->column_types[idx];
}

static gboolean
lapiz_file_browser_store_get_iter (CtkTreeModel * tree_model,
				   CtkTreeIter * iter, CtkTreePath * path)
{
	gint * indices, depth, i;
	FileBrowserNode * node;
	LapizFileBrowserStore * model;
	gint num;

	g_assert (LAPIZ_IS_FILE_BROWSER_STORE (tree_model));
	g_assert (path != NULL);

	model = LAPIZ_FILE_BROWSER_STORE (tree_model);
	indices = ctk_tree_path_get_indices (path);
	depth = ctk_tree_path_get_depth (path);
	node = model->priv->virtual_root;

	for (i = 0; i < depth; ++i) {
		GSList * item;

		if (node == NULL)
			return FALSE;

		num = 0;

		if (!NODE_IS_DIR (node))
			return FALSE;

		for (item = FILE_BROWSER_NODE_DIR (node)->children; item; item = item->next) {
			FileBrowserNode * child;

			child = (FileBrowserNode *) (item->data);

			if (model_node_inserted (model, child)) {
				if (num == indices[i]) {
					node = child;
					break;
				}

				num++;
			}
		}

		if (item == NULL)
			return FALSE;

		node = (FileBrowserNode *) (item->data);
	}

	iter->user_data = node;
	iter->user_data2 = NULL;
	iter->user_data3 = NULL;

	return node != NULL;
}

static CtkTreePath *
lapiz_file_browser_store_get_path_real (LapizFileBrowserStore * model,
					FileBrowserNode * node)
{
	CtkTreePath *path;
	gint num = 0;

	path = ctk_tree_path_new ();

	while (node != model->priv->virtual_root) {
		GSList *item;

		if (node->parent == NULL) {
			ctk_tree_path_free (path);
			return NULL;
		}

		num = 0;

		for (item = FILE_BROWSER_NODE_DIR (node->parent)->children; item; item = item->next) {
			FileBrowserNode *check;

			check = (FileBrowserNode *) (item->data);

			if (model_node_visibility (model, check) && (check == node || check->inserted)) {
				if (check == node) {
					ctk_tree_path_prepend_index (path,
								     num);
					break;
				}

				++num;
			} else if (check == node) {
				if (NODE_IS_DUMMY (node))
					g_warning ("Dummy not visible???");

				ctk_tree_path_free (path);
				return NULL;
			}
		}

		node = node->parent;
	}

	return path;
}

static CtkTreePath *
lapiz_file_browser_store_get_path (CtkTreeModel * tree_model,
				   CtkTreeIter * iter)
{
	g_return_val_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (tree_model), NULL);
	g_return_val_if_fail (iter != NULL, NULL);
	g_return_val_if_fail (iter->user_data != NULL, NULL);

	return lapiz_file_browser_store_get_path_real (LAPIZ_FILE_BROWSER_STORE (tree_model),
						       (FileBrowserNode *) (iter->user_data));
}

static void
lapiz_file_browser_store_get_value (CtkTreeModel * tree_model,
				    CtkTreeIter * iter,
				    gint column,
				    GValue * value)
{
	FileBrowserNode *node;

	g_return_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (tree_model));
	g_return_if_fail (iter != NULL);
	g_return_if_fail (iter->user_data != NULL);

	node = (FileBrowserNode *) (iter->user_data);

	g_value_init (value, LAPIZ_FILE_BROWSER_STORE (tree_model)->priv->column_types[column]);

	switch (column) {
	case LAPIZ_FILE_BROWSER_STORE_COLUMN_URI:
		set_gvalue_from_node (value, node);
		break;
	case LAPIZ_FILE_BROWSER_STORE_COLUMN_NAME:
		g_value_set_string (value, node->name);
		break;
	case LAPIZ_FILE_BROWSER_STORE_COLUMN_FLAGS:
		g_value_set_uint (value, node->flags);
		break;
	case LAPIZ_FILE_BROWSER_STORE_COLUMN_ICON:
		g_value_set_object (value, node->icon);
		break;
	case LAPIZ_FILE_BROWSER_STORE_COLUMN_EMBLEM:
		g_value_set_object (value, node->emblem);
		break;
	default:
		g_return_if_reached ();
	}
}

static gboolean
lapiz_file_browser_store_iter_next (CtkTreeModel * tree_model,
				    CtkTreeIter * iter)
{
	LapizFileBrowserStore * model;
	FileBrowserNode * node;
	GSList * item;
	GSList * first;

	g_return_val_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (tree_model),
			      FALSE);
	g_return_val_if_fail (iter != NULL, FALSE);
	g_return_val_if_fail (iter->user_data != NULL, FALSE);

	model = LAPIZ_FILE_BROWSER_STORE (tree_model);
	node = (FileBrowserNode *) (iter->user_data);

	if (node->parent == NULL)
		return FALSE;

	first = g_slist_next (g_slist_find (FILE_BROWSER_NODE_DIR (node->parent)->children, node));

	for (item = first; item; item = item->next) {
		if (model_node_inserted (model, (FileBrowserNode *) (item->data))) {
			iter->user_data = item->data;
			return TRUE;
		}
	}

	return FALSE;
}

static gboolean
lapiz_file_browser_store_iter_children (CtkTreeModel * tree_model,
					CtkTreeIter * iter,
					CtkTreeIter * parent)
{
	FileBrowserNode * node;
	LapizFileBrowserStore * model;
	GSList * item;

	g_return_val_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (tree_model),
			      FALSE);
	g_return_val_if_fail (parent == NULL
			      || parent->user_data != NULL, FALSE);

	model = LAPIZ_FILE_BROWSER_STORE (tree_model);

	if (parent == NULL)
		node = model->priv->virtual_root;
	else
		node = (FileBrowserNode *) (parent->user_data);

	if (node == NULL)
		return FALSE;

	if (!NODE_IS_DIR (node))
		return FALSE;

	for (item = FILE_BROWSER_NODE_DIR (node)->children; item; item = item->next) {
		if (model_node_inserted (model, (FileBrowserNode *) (item->data))) {
			iter->user_data = item->data;
			return TRUE;
		}
	}

	return FALSE;
}

static gboolean
filter_tree_model_iter_has_child_real (LapizFileBrowserStore * model,
				       FileBrowserNode * node)
{
	GSList *item;

	if (!NODE_IS_DIR (node))
		return FALSE;

	for (item = FILE_BROWSER_NODE_DIR (node)->children; item; item = item->next) {
		if (model_node_inserted (model, (FileBrowserNode *) (item->data)))
			return TRUE;
	}

	return FALSE;
}

static gboolean
lapiz_file_browser_store_iter_has_child (CtkTreeModel * tree_model,
					 CtkTreeIter * iter)
{
	FileBrowserNode *node;
	LapizFileBrowserStore *model;

	g_return_val_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (tree_model),
			      FALSE);
	g_return_val_if_fail (iter == NULL
			      || iter->user_data != NULL, FALSE);

	model = LAPIZ_FILE_BROWSER_STORE (tree_model);

	if (iter == NULL)
		node = model->priv->virtual_root;
	else
		node = (FileBrowserNode *) (iter->user_data);

	return filter_tree_model_iter_has_child_real (model, node);
}

static gint
lapiz_file_browser_store_iter_n_children (CtkTreeModel * tree_model,
					  CtkTreeIter * iter)
{
	FileBrowserNode *node;
	LapizFileBrowserStore *model;
	GSList *item;
	gint num = 0;

	g_return_val_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (tree_model),
			      FALSE);
	g_return_val_if_fail (iter == NULL
			      || iter->user_data != NULL, FALSE);

	model = LAPIZ_FILE_BROWSER_STORE (tree_model);

	if (iter == NULL)
		node = model->priv->virtual_root;
	else
		node = (FileBrowserNode *) (iter->user_data);

	if (!NODE_IS_DIR (node))
		return 0;

	for (item = FILE_BROWSER_NODE_DIR (node)->children; item; item = item->next)
		if (model_node_inserted (model, (FileBrowserNode *) (item->data)))
			++num;

	return num;
}

static gboolean
lapiz_file_browser_store_iter_nth_child (CtkTreeModel * tree_model,
					 CtkTreeIter * iter,
					 CtkTreeIter * parent, gint n)
{
	FileBrowserNode *node;
	LapizFileBrowserStore *model;
	GSList *item;
	gint num = 0;

	g_return_val_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (tree_model),
			      FALSE);
	g_return_val_if_fail (parent == NULL
			      || parent->user_data != NULL, FALSE);

	model = LAPIZ_FILE_BROWSER_STORE (tree_model);

	if (parent == NULL)
		node = model->priv->virtual_root;
	else
		node = (FileBrowserNode *) (parent->user_data);

	if (!NODE_IS_DIR (node))
		return FALSE;

	for (item = FILE_BROWSER_NODE_DIR (node)->children; item;
	     item = item->next) {
		if (model_node_inserted (model, (FileBrowserNode *) (item->data))) {
			if (num == n) {
				iter->user_data = item->data;
				return TRUE;
			}

			++num;
		}
	}

	return FALSE;
}

static gboolean
lapiz_file_browser_store_iter_parent (CtkTreeModel * tree_model,
				      CtkTreeIter * iter,
				      CtkTreeIter * child)
{
	FileBrowserNode *node;
	LapizFileBrowserStore *model;

	g_return_val_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (tree_model), FALSE);
	g_return_val_if_fail (child != NULL, FALSE);
	g_return_val_if_fail (child->user_data != NULL, FALSE);

	node = (FileBrowserNode *) (child->user_data);
	model = LAPIZ_FILE_BROWSER_STORE (tree_model);

	if (!node_in_tree (model, node))
		return FALSE;

	if (node->parent == NULL)
		return FALSE;

	iter->user_data = node->parent;
	return TRUE;
}

static void
lapiz_file_browser_store_row_inserted (CtkTreeModel *tree_model G_GNUC_UNUSED,
				       CtkTreePath  *path G_GNUC_UNUSED,
				       CtkTreeIter  *iter)
{
	FileBrowserNode * node = (FileBrowserNode *)(iter->user_data);

	node->inserted = TRUE;
}

static gboolean
lapiz_file_browser_store_row_draggable (CtkTreeDragSource * drag_source,
					CtkTreePath       * path)
{
	CtkTreeIter iter;
	LapizFileBrowserStoreFlag flags;

	if (!ctk_tree_model_get_iter (CTK_TREE_MODEL (drag_source),
				      &iter, path))
	{
		return FALSE;
	}

	ctk_tree_model_get (CTK_TREE_MODEL (drag_source), &iter,
			    LAPIZ_FILE_BROWSER_STORE_COLUMN_FLAGS, &flags,
			    -1);

	return !FILE_IS_DUMMY(flags);
}

static gboolean
lapiz_file_browser_store_drag_data_delete (CtkTreeDragSource *drag_source G_GNUC_UNUSED,
					   CtkTreePath       *path G_GNUC_UNUSED)
{
	return FALSE;
}

static gboolean
lapiz_file_browser_store_drag_data_get (CtkTreeDragSource * drag_source,
					CtkTreePath       * path,
					CtkSelectionData  * selection_data)
{
	CtkTreeIter iter;
	gchar *uri;
	gchar *uris[2] = {0, };
	gboolean ret;

	if (!ctk_tree_model_get_iter (CTK_TREE_MODEL (drag_source),
				      &iter, path))
	{
		return FALSE;
	}

	ctk_tree_model_get (CTK_TREE_MODEL (drag_source), &iter,
			    LAPIZ_FILE_BROWSER_STORE_COLUMN_URI, &uri,
			    -1);

	g_assert (uri);

	uris[0] = uri;
	ret = ctk_selection_data_set_uris (selection_data, uris);

	g_free (uri);

	return ret;
}

#define FILTER_HIDDEN(mode) (mode & LAPIZ_FILE_BROWSER_STORE_FILTER_MODE_HIDE_HIDDEN)
#define FILTER_BINARY(mode) (mode & LAPIZ_FILE_BROWSER_STORE_FILTER_MODE_HIDE_BINARY)

/* Private */
static void
model_begin_loading (LapizFileBrowserStore * model, FileBrowserNode * node)
{
	CtkTreeIter iter;

	iter.user_data = node;
	g_signal_emit (model, model_signals[BEGIN_LOADING], 0, &iter);
}

static void
model_end_loading (LapizFileBrowserStore * model, FileBrowserNode * node)
{
	CtkTreeIter iter;

	iter.user_data = node;
	g_signal_emit (model, model_signals[END_LOADING], 0, &iter);
}

static void
model_node_update_visibility (LapizFileBrowserStore * model,
			      FileBrowserNode * node)
{
	CtkTreeIter iter;

	node->flags &= ~LAPIZ_FILE_BROWSER_STORE_FLAG_IS_FILTERED;

	if (FILTER_HIDDEN (model->priv->filter_mode) &&
	    NODE_IS_HIDDEN (node))
		node->flags |= LAPIZ_FILE_BROWSER_STORE_FLAG_IS_FILTERED;
	else if (FILTER_BINARY (model->priv->filter_mode) &&
		 (!NODE_IS_TEXT (node) && !NODE_IS_DIR (node)))
		node->flags |= LAPIZ_FILE_BROWSER_STORE_FLAG_IS_FILTERED;
	else if (model->priv->filter_func) {
		iter.user_data = node;

		if (!model->priv->
		    filter_func (model, &iter,
				 model->priv->filter_user_data))
			node->flags |=
			    LAPIZ_FILE_BROWSER_STORE_FLAG_IS_FILTERED;
	}
}

static gint
collate_nodes (FileBrowserNode * node1, FileBrowserNode * node2)
{
	if (node1->name == NULL)
		return -1;
	else if (node2->name == NULL)
		return 1;
	else {
		gchar *k1, *k2;
		gint result;

		k1 = g_utf8_collate_key_for_filename (node1->name, -1);
		k2 = g_utf8_collate_key_for_filename (node2->name, -1);

		result = strcmp (k1, k2);

		g_free (k1);
		g_free (k2);

		return result;
	}
}

static gint
model_sort_default (FileBrowserNode * node1, FileBrowserNode * node2)
{
	gint f1;
	gint f2;

	f1 = NODE_IS_DUMMY (node1);
	f2 = NODE_IS_DUMMY (node2);

	if (f1 && f2)
	{
		return 0;
	}
	else if (f1 || f2)
	{
		return f1 ? -1 : 1;
	}

	f1 = NODE_IS_DIR (node1);
	f2 = NODE_IS_DIR (node2);

	if (f1 != f2)
	{
		return f1 ? -1 : 1;
	}

	f1 = NODE_IS_HIDDEN (node1);
	f2 = NODE_IS_HIDDEN (node2);

	if (f1 != f2)
	{
		return f2 ? -1 : 1;
	}

	return collate_nodes (node1, node2);
}

static void
model_resort_node (LapizFileBrowserStore * model, FileBrowserNode * node)
{
	FileBrowserNodeDir *dir;
	GSList *item;
	FileBrowserNode *child;
	gint pos = 0;
	CtkTreeIter iter;
	CtkTreePath *path;
	gint *neworder;

	dir = FILE_BROWSER_NODE_DIR (node->parent);

	if (!model_node_visibility (model, node->parent)) {
		/* Just sort the children of the parent */
		dir->children = g_slist_sort (dir->children,
					      (GCompareFunc) (model->priv->
							      sort_func));
	} else {
		/* Store current positions */
		for (item = dir->children; item; item = item->next) {
			child = (FileBrowserNode *) (item->data);

			if (model_node_visibility (model, child))
				child->pos = pos++;
		}

		dir->children = g_slist_sort (dir->children,
					      (GCompareFunc) (model->priv->
							      sort_func));
		neworder = g_new (gint, pos);
		pos = 0;

		/* Store the new positions */
		for (item = dir->children; item; item = item->next) {
			child = (FileBrowserNode *) (item->data);

			if (model_node_visibility (model, child))
				neworder[pos++] = child->pos;
		}

		iter.user_data = node->parent;
		path =
		    lapiz_file_browser_store_get_path_real (model,
							    node->parent);

		ctk_tree_model_rows_reordered (CTK_TREE_MODEL (model),
					       path, &iter, neworder);

		g_free (neworder);
		ctk_tree_path_free (path);
	}
}

static void
row_changed (LapizFileBrowserStore * model,
	     CtkTreePath ** path,
	     CtkTreeIter * iter)
{
	CtkTreeRowReference *ref = ctk_tree_row_reference_new (CTK_TREE_MODEL (model), *path);

	/* Insert a copy of the actual path here because the row-inserted
	   signal may alter the path */
	ctk_tree_model_row_changed (CTK_TREE_MODEL(model), *path, iter);
	ctk_tree_path_free (*path);

	*path = ctk_tree_row_reference_get_path (ref);
	ctk_tree_row_reference_free (ref);
}

static void
row_inserted (LapizFileBrowserStore * model,
	      CtkTreePath ** path,
	      CtkTreeIter * iter)
{
	/* This function creates a row reference for the path because it's
	   uncertain what might change the actual model/view when we insert
	   a node, maybe another directory load is triggered for example.
	   Because functions that use this function rely on the notion that
	   the path remains pointed towards the inserted node, we use the
	   reference to keep track. */
	CtkTreeRowReference *ref = ctk_tree_row_reference_new (CTK_TREE_MODEL (model), *path);
	CtkTreePath * copy = ctk_tree_path_copy (*path);

	ctk_tree_model_row_inserted (CTK_TREE_MODEL(model), copy, iter);
	ctk_tree_path_free (copy);

	if (ref)
	{
		ctk_tree_path_free (*path);

		/* To restore the path, we get the path from the reference. But, since
		   we inserted a row, the path will be one index further than the
		   actual path of our node. We therefore call ctk_tree_path_prev */
		*path = ctk_tree_row_reference_get_path (ref);
		ctk_tree_path_prev (*path);
	}

	ctk_tree_row_reference_free (ref);
}

static void
row_deleted (LapizFileBrowserStore * model,
	     const CtkTreePath * path)
{
	CtkTreePath *copy = ctk_tree_path_copy (path);

	/* Delete a copy of the actual path here because the row-deleted
	   signal may alter the path */
	ctk_tree_model_row_deleted (CTK_TREE_MODEL(model), copy);
	ctk_tree_path_free (copy);
}

static void
model_refilter_node (LapizFileBrowserStore * model,
		     FileBrowserNode * node,
		     CtkTreePath ** path)
{
	gboolean old_visible;
	gboolean new_visible;
	FileBrowserNodeDir *dir;
	GSList *item;
	CtkTreeIter iter;
	CtkTreePath *tmppath = NULL;
	gboolean in_tree;

	if (node == NULL)
		return;

	old_visible = model_node_visibility (model, node);
	model_node_update_visibility (model, node);

	in_tree = node_in_tree (model, node);

	if (path == NULL)
	{
		if (in_tree)
			tmppath = lapiz_file_browser_store_get_path_real (model,
								       node);
		else
			tmppath = ctk_tree_path_new_first ();

		path = &tmppath;
	}

	if (NODE_IS_DIR (node)) {
		if (in_tree)
			ctk_tree_path_down (*path);

		dir = FILE_BROWSER_NODE_DIR (node);

		for (item = dir->children; item; item = item->next) {
			model_refilter_node (model,
					     (FileBrowserNode *) (item->data),
					     path);
		}

		if (in_tree)
			ctk_tree_path_up (*path);
	}

	if (in_tree) {
		new_visible = model_node_visibility (model, node);

		if (old_visible != new_visible) {
			if (old_visible) {
				node->inserted = FALSE;
				row_deleted (model, *path);
			} else {
				iter.user_data = node;
				row_inserted (model, path, &iter);
				ctk_tree_path_next (*path);
			}
		} else if (old_visible) {
			ctk_tree_path_next (*path);
		}
	}

	model_check_dummy (model, node);

	if (tmppath)
		ctk_tree_path_free (tmppath);
}

static void
model_refilter (LapizFileBrowserStore * model)
{
	model_refilter_node (model, model->priv->root, NULL);
}

static void
file_browser_node_set_name (FileBrowserNode * node)
{
	g_free (node->name);

	if (node->file) {
		node->name = lapiz_file_browser_utils_file_basename (node->file);
	} else {
		node->name = NULL;
	}
}

static void
file_browser_node_init (FileBrowserNode * node, GFile * file,
			FileBrowserNode * parent)
{
	if (file != NULL) {
		node->file = g_object_ref (file);
		file_browser_node_set_name (node);
	}

	node->parent = parent;
}

static FileBrowserNode *
file_browser_node_new (GFile * file, FileBrowserNode * parent)
{
	FileBrowserNode *node = g_slice_new0 (FileBrowserNode);

	file_browser_node_init (node, file, parent);
	return node;
}

static FileBrowserNode *
file_browser_node_dir_new (LapizFileBrowserStore * model,
			   GFile * file, FileBrowserNode * parent)
{
	FileBrowserNode *node =
	    (FileBrowserNode *) g_slice_new0 (FileBrowserNodeDir);

	file_browser_node_init (node, file, parent);

	node->flags |= LAPIZ_FILE_BROWSER_STORE_FLAG_IS_DIRECTORY;

	FILE_BROWSER_NODE_DIR (node)->model = model;

	return node;
}

static void
file_browser_node_free_children (LapizFileBrowserStore * model,
				 FileBrowserNode * node)
{
	GSList *item;

	if (node == NULL)
		return;

	if (NODE_IS_DIR (node)) {
		for (item = FILE_BROWSER_NODE_DIR (node)->children; item;
		     item = item->next)
			file_browser_node_free (model,
						(FileBrowserNode *) (item->
								     data));

		g_slist_free (FILE_BROWSER_NODE_DIR (node)->children);
		FILE_BROWSER_NODE_DIR (node)->children = NULL;

		/* This node is no longer loaded */
		node->flags &= ~LAPIZ_FILE_BROWSER_STORE_FLAG_LOADED;
	}
}

static void
file_browser_node_free (LapizFileBrowserStore * model,
			FileBrowserNode * node)
{
	gchar *uri;

	if (node == NULL)
		return;

	if (NODE_IS_DIR (node))
	{
		FileBrowserNodeDir *dir;

		dir = FILE_BROWSER_NODE_DIR (node);

		if (dir->cancellable) {
			g_cancellable_cancel (dir->cancellable);
			g_object_unref (dir->cancellable);

			model_end_loading (model, node);
		}

		file_browser_node_free_children (model, node);

		if (dir->monitor) {
			g_file_monitor_cancel (dir->monitor);
			g_object_unref (dir->monitor);
		}
	}

	if (node->file)
	{
		uri = g_file_get_uri (node->file);
		g_signal_emit (model, model_signals[UNLOAD], 0, uri);

		g_free (uri);
		g_object_unref (node->file);
	}

	if (node->icon)
		g_object_unref (node->icon);

	if (node->emblem)
		g_object_unref (node->emblem);

	g_free (node->name);

	if (NODE_IS_DIR (node))
		g_slice_free (FileBrowserNodeDir, (FileBrowserNodeDir *)node);
	else
		g_slice_free (FileBrowserNode, (FileBrowserNode *)node);
}

/**
 * model_remove_node_children:
 * @model: the #LapizFileBrowserStore
 * @node: the FileBrowserNode to remove
 * @path: the path of the node, or NULL to let the path be calculated
 * @free_nodes: whether to also remove the nodes from memory
 *
 * Removes all the children of node from the model. This function is used
 * to remove the child nodes from the _model_. Don't use it to just free
 * a node.
 **/
static void
model_remove_node_children (LapizFileBrowserStore * model,
			    FileBrowserNode * node,
			    CtkTreePath * path,
			    gboolean free_nodes)
{
	FileBrowserNodeDir *dir;
	CtkTreePath *path_child;
	GSList *list;
	GSList *item;

	if (node == NULL || !NODE_IS_DIR (node))
		return;

	dir = FILE_BROWSER_NODE_DIR (node);

	if (dir->children == NULL)
		return;

	if (!model_node_visibility (model, node)) {
		// Node is invisible and therefore the children can just
		// be freed
		if (free_nodes)
			file_browser_node_free_children (model, node);

		return;
	}

	if (path == NULL)
		path_child =
		    lapiz_file_browser_store_get_path_real (model, node);
	else
		path_child = ctk_tree_path_copy (path);

	ctk_tree_path_down (path_child);

	list = g_slist_copy (dir->children);

	for (item = list; item; item = item->next) {
		model_remove_node (model, (FileBrowserNode *) (item->data),
				   path_child, free_nodes);
	}

	g_slist_free (list);
	ctk_tree_path_free (path_child);
}

/**
 * model_remove_node:
 * @model: the #LapizFileBrowserStore
 * @node: the FileBrowserNode to remove
 * @path: the path to use to remove this node, or NULL to use the path
 * calculated from the node itself
 * @free_nodes: whether to also remove the nodes from memory
 *
 * Removes this node and all its children from the model. This function is used
 * to remove the node from the _model_. Don't use it to just free
 * a node.
 **/
static void
model_remove_node (LapizFileBrowserStore * model,
		   FileBrowserNode * node,
		   CtkTreePath * path,
		   gboolean free_nodes)
{
	gboolean free_path = FALSE;
	FileBrowserNode *parent;

	if (path == NULL) {
		path =
		    lapiz_file_browser_store_get_path_real (model, node);
		free_path = TRUE;
	}

	model_remove_node_children (model, node, path, free_nodes);

	/* Only delete if the node is visible in the tree (but only when it's
	   not the virtual root) */
	if (model_node_visibility (model, node) && node != model->priv->virtual_root)
	{
		node->inserted = FALSE;
		row_deleted (model, path);
	}

	if (free_path)
		ctk_tree_path_free (path);

	parent = node->parent;

	if (free_nodes) {
		/* Remove the node from the parents children list */
		if (parent)
			FILE_BROWSER_NODE_DIR (node->parent)->children =
			    g_slist_remove (FILE_BROWSER_NODE_DIR
					    (node->parent)->children,
					    node);
	}

	/* If this is the virtual root, than set the parent as the virtual root */
	if (node == model->priv->virtual_root)
		set_virtual_root_from_node (model, parent);
	else if (parent && model_node_visibility (model, parent) && !(free_nodes && NODE_IS_DUMMY(node)))
		model_check_dummy (model, parent);

	/* Now free the node if necessary */
	if (free_nodes)
		file_browser_node_free (model, node);
}

/**
 * model_clear:
 * @model: the #LapizFileBrowserStore
 * @free_nodes: whether to also remove the nodes from memory
 *
 * Removes all nodes from the model. This function is used
 * to remove all the nodes from the _model_. Don't use it to just free the
 * nodes in the model.
 **/
static void
model_clear (LapizFileBrowserStore * model, gboolean free_nodes)
{
	CtkTreePath *path;
	FileBrowserNodeDir *dir;
	FileBrowserNode *dummy;

	path = ctk_tree_path_new ();
	model_remove_node_children (model, model->priv->virtual_root, path,
				    free_nodes);
	ctk_tree_path_free (path);

	/* Remove the dummy if there is one */
	if (model->priv->virtual_root) {
		dir = FILE_BROWSER_NODE_DIR (model->priv->virtual_root);

		if (dir->children != NULL) {
			dummy = (FileBrowserNode *) (dir->children->data);

			if (NODE_IS_DUMMY (dummy)
			    && model_node_visibility (model, dummy)) {
				path = ctk_tree_path_new_first ();

				dummy->inserted = FALSE;
				row_deleted (model, path);
				ctk_tree_path_free (path);
			}
		}
	}
}

static void
file_browser_node_unload (LapizFileBrowserStore * model,
			  FileBrowserNode * node, gboolean remove_children)
{
	FileBrowserNodeDir *dir;

	if (node == NULL)
		return;

	if (!NODE_IS_DIR (node) || !NODE_LOADED (node))
		return;

	dir = FILE_BROWSER_NODE_DIR (node);

	if (remove_children)
		model_remove_node_children (model, node, NULL, TRUE);

	if (dir->cancellable) {
		g_cancellable_cancel (dir->cancellable);
		g_object_unref (dir->cancellable);

		model_end_loading (model, node);
		dir->cancellable = NULL;
	}

	if (dir->monitor) {
		g_file_monitor_cancel (dir->monitor);
		g_object_unref (dir->monitor);

		dir->monitor = NULL;
	}

	node->flags &= ~LAPIZ_FILE_BROWSER_STORE_FLAG_LOADED;
}

static void
model_recomposite_icon_real (LapizFileBrowserStore * tree_model,
			     FileBrowserNode * node,
			     GFileInfo * info)
{
	GdkPixbuf *icon;

	g_return_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (tree_model));
	g_return_if_fail (node != NULL);

	if (node->file == NULL)
		return;

	if (info) {
		GIcon *gicon = g_file_info_get_icon (info);
		if (gicon != NULL)
			icon = lapiz_file_browser_utils_pixbuf_from_icon (gicon, CTK_ICON_SIZE_MENU);
		else
			icon = NULL;
	} else {
		icon = lapiz_file_browser_utils_pixbuf_from_file (node->file, CTK_ICON_SIZE_MENU);
	}

	if (node->icon)
		g_object_unref (node->icon);

	if (node->emblem) {
		gint icon_size;

		ctk_icon_size_lookup (CTK_ICON_SIZE_MENU, NULL, &icon_size);

		if (icon == NULL) {
			node->icon =
			    gdk_pixbuf_new (gdk_pixbuf_get_colorspace (node->emblem),
					    gdk_pixbuf_get_has_alpha (node->emblem),
					    gdk_pixbuf_get_bits_per_sample (node->emblem),
					    icon_size,
					    icon_size);
		} else {
			node->icon = gdk_pixbuf_copy (icon);
			g_object_unref (icon);
		}

		gdk_pixbuf_composite (node->emblem, node->icon,
				      icon_size - 10, icon_size - 10, 10,
				      10, icon_size - 10, icon_size - 10,
				      1, 1, GDK_INTERP_NEAREST, 255);
	} else {
		node->icon = icon;
	}
}

static void
model_recomposite_icon (LapizFileBrowserStore * tree_model,
			CtkTreeIter * iter)
{
	g_return_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (tree_model));
	g_return_if_fail (iter != NULL);
	g_return_if_fail (iter->user_data != NULL);

	model_recomposite_icon_real (tree_model,
				     (FileBrowserNode *) (iter->user_data),
				     NULL);
}

static FileBrowserNode *
model_create_dummy_node (LapizFileBrowserStore *model G_GNUC_UNUSED,
			 FileBrowserNode       *parent)
{
	FileBrowserNode *dummy;

	dummy = file_browser_node_new (NULL, parent);
	dummy->name = g_strdup (_("(Empty)"));

	dummy->flags |= LAPIZ_FILE_BROWSER_STORE_FLAG_IS_DUMMY;
	dummy->flags |= LAPIZ_FILE_BROWSER_STORE_FLAG_IS_HIDDEN;

	return dummy;
}

static FileBrowserNode *
model_add_dummy_node (LapizFileBrowserStore * model,
		      FileBrowserNode * parent)
{
	FileBrowserNode *dummy;

	dummy = model_create_dummy_node (model, parent);

	if (model_node_visibility (model, parent))
		dummy->flags &= ~LAPIZ_FILE_BROWSER_STORE_FLAG_IS_HIDDEN;

	model_add_node (model, dummy, parent);

	return dummy;
}

static void
model_check_dummy (LapizFileBrowserStore * model, FileBrowserNode * node)
{
	// Hide the dummy child if needed
	if (NODE_IS_DIR (node)) {
		FileBrowserNode *dummy;
		CtkTreeIter iter;
		CtkTreePath *path;
		guint flags;
		FileBrowserNodeDir *dir;

		dir = FILE_BROWSER_NODE_DIR (node);

		if (dir->children == NULL) {
			model_add_dummy_node (model, node);
			return;
		}

		dummy = (FileBrowserNode *) (dir->children->data);

		if (!NODE_IS_DUMMY (dummy)) {
			dummy = model_create_dummy_node (model, node);
			dir->children = g_slist_prepend (dir->children, dummy);
		}

		if (!model_node_visibility (model, node)) {
			dummy->flags |=
			    LAPIZ_FILE_BROWSER_STORE_FLAG_IS_HIDDEN;
			return;
		}

		/* Temporarily set the node to invisible to check
		 * for real children */
		flags = dummy->flags;
		dummy->flags |= LAPIZ_FILE_BROWSER_STORE_FLAG_IS_HIDDEN;

		if (!filter_tree_model_iter_has_child_real (model, node)) {
			dummy->flags &=
			    ~LAPIZ_FILE_BROWSER_STORE_FLAG_IS_HIDDEN;

			if (FILE_IS_HIDDEN (flags)) {
				// Was hidden, needs to be inserted
				iter.user_data = dummy;
				path =
				    lapiz_file_browser_store_get_path_real
				    (model, dummy);

				row_inserted (model, &path, &iter);
				ctk_tree_path_free (path);
			}
		} else {
			if (!FILE_IS_HIDDEN (flags)) {
				// Was shown, needs to be removed

				// To get the path we need to set it to visible temporarily
				dummy->flags &=
				    ~LAPIZ_FILE_BROWSER_STORE_FLAG_IS_HIDDEN;
				path =
				    lapiz_file_browser_store_get_path_real
				    (model, dummy);
				dummy->flags |=
				    LAPIZ_FILE_BROWSER_STORE_FLAG_IS_HIDDEN;

				dummy->inserted = FALSE;
				row_deleted (model, path);
				ctk_tree_path_free (path);
			}
		}
	}
}

static void
insert_node_sorted (LapizFileBrowserStore * model,
		    FileBrowserNode * child,
		    FileBrowserNode * parent)
{
	FileBrowserNodeDir *dir;

	dir = FILE_BROWSER_NODE_DIR (parent);

	if (model->priv->sort_func == NULL) {
		dir->children = g_slist_append (dir->children, child);
	} else {
		dir->children =
		    g_slist_insert_sorted (dir->children, child,
					   (GCompareFunc) (model->priv->
							   sort_func));
	}
}

static void
model_add_node (LapizFileBrowserStore * model, FileBrowserNode * child,
		FileBrowserNode * parent)
{
	/* Add child to parents children */
	insert_node_sorted (model, child, parent);

	if (model_node_visibility (model, parent) &&
	    model_node_visibility (model, child)) {
		CtkTreeIter iter;
		CtkTreePath *path;

		iter.user_data = child;
		path = lapiz_file_browser_store_get_path_real (model, child);

		/* Emit row inserted */
		row_inserted (model, &path, &iter);
		ctk_tree_path_free (path);
	}

	model_check_dummy (model, parent);
	model_check_dummy (model, child);
}

static void
model_add_nodes_batch (LapizFileBrowserStore * model,
		       GSList * children,
		       FileBrowserNode * parent)
{
	GSList *sorted_children;
	GSList *child;
	GSList *prev;
	GSList *l;
	FileBrowserNodeDir *dir;

	dir = FILE_BROWSER_NODE_DIR (parent);

	sorted_children = g_slist_sort (children, (GCompareFunc) model->priv->sort_func);

	child = sorted_children;
	l = dir->children;
	prev = NULL;

	model_check_dummy (model, parent);

	while (child) {
		FileBrowserNode *node = child->data;
		CtkTreeIter iter;
		CtkTreePath *path;

		/* reached the end of the first list, just append the second */
		if (l == NULL) {

			dir->children = g_slist_concat (dir->children, child);

			for (l = child; l; l = l->next) {
				if (model_node_visibility (model, parent) &&
				    model_node_visibility (model, l->data)) {
					iter.user_data = l->data;
					path = lapiz_file_browser_store_get_path_real (model, l->data);

					// Emit row inserted
					row_inserted (model, &path, &iter);
					ctk_tree_path_free (path);
				}

				model_check_dummy (model, l->data);
			}

			break;
		}

		if (model->priv->sort_func (l->data, node) > 0) {
			GSList *next_child;

			if (prev == NULL) {
				/* prepend to the list */
				dir->children = g_slist_prepend (dir->children, child);
			} else {
				prev->next = child;
			}

			next_child = child->next;
			prev = child;
			child->next = l;
			child = next_child;

			if (model_node_visibility (model, parent) &&
			    model_node_visibility (model, node)) {
				iter.user_data = node;
				path = lapiz_file_browser_store_get_path_real (model, node);

				// Emit row inserted
				row_inserted (model, &path, &iter);
				ctk_tree_path_free (path);
			}

			model_check_dummy (model, node);

			/* try again at the same l position with the
			 * next child */
		} else {

			/* Move to the next item in the list */
			prev = l;
			l = l->next;
		}
	}
}

static gchar const *
backup_content_type (GFileInfo * info)
{
	gchar const * content;

	if (!g_file_info_get_is_backup (info))
		return NULL;

	content = g_file_info_get_content_type (info);

	if (!content || g_content_type_equals (content, "application/x-trash"))
		return "text/plain";

	return content;
}

static void
file_browser_node_set_from_info (LapizFileBrowserStore * model,
				 FileBrowserNode * node,
				 GFileInfo * info,
				 gboolean isadded)
{
	gchar const * content;
	gboolean free_info = FALSE;
	CtkTreePath * path;
	gchar * uri;
	GError * error = NULL;

	if (info == NULL) {
		info = g_file_query_info (node->file,
					  STANDARD_ATTRIBUTE_TYPES,<--- There is an unknown macro here somewhere. Configuration is required. If G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN is a macro then please configure it.
					  G_FILE_QUERY_INFO_NONE,
					  NULL,
					  &error);

		if (!info) {
			if (!(error->domain == G_IO_ERROR && error->code == G_IO_ERROR_NOT_FOUND)) {
				uri = g_file_get_uri (node->file);
				g_warning ("Could not get info for %s: %s", uri, error->message);
				g_free (uri);
			}
			g_error_free (error);

			return;
		}

		free_info = TRUE;
	}

	if (g_file_info_get_is_hidden (info) || g_file_info_get_is_backup (info))
		node->flags |= LAPIZ_FILE_BROWSER_STORE_FLAG_IS_HIDDEN;

	if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
		node->flags |= LAPIZ_FILE_BROWSER_STORE_FLAG_IS_DIRECTORY;
	else {
		if (!(content = backup_content_type (info)))
			content = g_file_info_get_content_type (info);

		if (!content ||
		    g_content_type_is_unknown (content) ||
		    g_content_type_is_a (content, "text/plain"))
			node->flags |= LAPIZ_FILE_BROWSER_STORE_FLAG_IS_TEXT;
	}

	model_recomposite_icon_real (model, node, info);

	if (free_info)
		g_object_unref (info);

	if (isadded) {
		path = lapiz_file_browser_store_get_path_real (model, node);
		model_refilter_node (model, node, &path);
		ctk_tree_path_free (path);

		model_check_dummy (model, node->parent);
	} else {
		model_node_update_visibility (model, node);
	}
}

static FileBrowserNode *
node_list_contains_file (GSList *children, GFile * file)
{
	GSList *item;

	for (item = children; item; item = item->next) {
		FileBrowserNode *node;

		node = (FileBrowserNode *) (item->data);

		if (node->file != NULL
		    && g_file_equal (node->file, file))
			return node;
	}

	return NULL;
}

static FileBrowserNode *
model_add_node_from_file (LapizFileBrowserStore * model,
			  FileBrowserNode * parent,
			  GFile * file,
			  GFileInfo * info)
{
	FileBrowserNode *node;
	gboolean free_info = FALSE;
	GError * error = NULL;

	if ((node = node_list_contains_file (FILE_BROWSER_NODE_DIR (parent)->children, file)) == NULL) {
		if (info == NULL) {
			info = g_file_query_info (file,
						  STANDARD_ATTRIBUTE_TYPES,
						  G_FILE_QUERY_INFO_NONE,
						  NULL,
						  &error);
			free_info = TRUE;
		}

		if (!info) {
			g_warning ("Error querying file info: %s", error->message);
			g_error_free (error);

			/* FIXME: What to do now then... */
			node = file_browser_node_new (file, parent);
		} else if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) {
			node = file_browser_node_dir_new (model, file, parent);
		} else {
			node = file_browser_node_new (file, parent);
		}

		file_browser_node_set_from_info (model, node, info, FALSE);
		model_add_node (model, node, parent);

		if (info && free_info)
			g_object_unref (info);
	}

	return node;
}

/* We pass in a copy of the list of parent->children so that we do
 * not have to check if a file already exists among the ones we just
 * added */
static void
model_add_nodes_from_files (LapizFileBrowserStore * model,
			    FileBrowserNode * parent,
			    GSList * original_children,
			    GList * files)
{
	GList *item;
	GSList *nodes = NULL;

	for (item = files; item; item = item->next) {
		GFileInfo *info = G_FILE_INFO (item->data);
		GFileType type;
		gchar const * name;
		GFile * file;
		FileBrowserNode *node;

		type = g_file_info_get_file_type (info);

		/* Skip all non regular, non directory files */
		if (type != G_FILE_TYPE_REGULAR &&
		    type != G_FILE_TYPE_DIRECTORY &&
		    type != G_FILE_TYPE_SYMBOLIC_LINK) {
			g_object_unref (info);
			continue;
		}

		name = g_file_info_get_name (info);

		/* Skip '.' and '..' directories */
		if (type == G_FILE_TYPE_DIRECTORY &&
		    (strcmp (name, ".") == 0 ||
		     strcmp (name, "..") == 0)) {
			continue;
		}

		file = g_file_get_child (parent->file, name);

		if ((node = node_list_contains_file (original_children, file)) == NULL) {

			if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) {
				node = file_browser_node_dir_new (model, file, parent);
			} else {
				node = file_browser_node_new (file, parent);
			}

			file_browser_node_set_from_info (model, node, info, FALSE);

			nodes = g_slist_prepend (nodes, node);
		}

		g_object_unref (file);
		g_object_unref (info);
	}

	if (nodes)
		model_add_nodes_batch (model, nodes, parent);
}

static FileBrowserNode *
model_add_node_from_dir (LapizFileBrowserStore * model,
			 FileBrowserNode * parent,
			 GFile * file)
{
	FileBrowserNode *node;

	/* Check if it already exists */
	if ((node = node_list_contains_file (FILE_BROWSER_NODE_DIR (parent)->children, file)) == NULL) {
		node = file_browser_node_dir_new (model, file, parent);
		file_browser_node_set_from_info (model, node, NULL, FALSE);

		if (node->name == NULL) {
			file_browser_node_set_name (node);
		}

		if (node->icon == NULL) {
			node->icon = lapiz_file_browser_utils_pixbuf_from_theme ("folder", CTK_ICON_SIZE_MENU);
		}

		model_add_node (model, node, parent);
	}

	return node;
}

static void
on_directory_monitor_event (GFileMonitor     *monitor G_GNUC_UNUSED,
			    GFile            *file,
			    GFile            *other_file G_GNUC_UNUSED,
			    GFileMonitorEvent event_type,
			    FileBrowserNode  *parent)
{
	FileBrowserNode *node;
	FileBrowserNodeDir *dir = FILE_BROWSER_NODE_DIR (parent);

	switch (event_type) {
	case G_FILE_MONITOR_EVENT_DELETED:
		node = node_list_contains_file (dir->children, file);

		if (node != NULL) {
			model_remove_node (dir->model, node, NULL, TRUE);
		}
		break;
	case G_FILE_MONITOR_EVENT_CREATED:
		if (g_file_query_exists (file, NULL)) {
			model_add_node_from_file (dir->model, parent, file, NULL);
		}

		break;
	default:
		break;
	}
}

static void
async_node_free (AsyncNode *async)
{
	g_object_unref (async->cancellable);
	g_slist_free (async->original_children);
	g_free (async);
}

static void
model_iterate_next_files_cb (GFileEnumerator * enumerator,
			     GAsyncResult * result,
			     AsyncNode * async)
{
	GList * files;
	GError * error = NULL;
	FileBrowserNodeDir * dir = async->dir;
	FileBrowserNode * parent = (FileBrowserNode *)dir;

	files = g_file_enumerator_next_files_finish (enumerator, result, &error);

	if (files == NULL) {
		g_file_enumerator_close (enumerator, NULL, NULL);
		async_node_free (async);

		if (!error)
		{
			/* We're done loading */
			g_object_unref (dir->cancellable);
			dir->cancellable = NULL;

/*
 * FIXME: This is temporarly, it is a bug in gio:
 * http://bugzilla.gnome.org/show_bug.cgi?id=565924
 */
			if (g_file_is_native (parent->file) && dir->monitor == NULL) {
				dir->monitor = g_file_monitor_directory (parent->file,
									 G_FILE_MONITOR_NONE,
									 NULL,
									 NULL);
				if (dir->monitor != NULL)
				{
					g_signal_connect (dir->monitor,
							  "changed",
							  G_CALLBACK (on_directory_monitor_event),
							  parent);
				}
			}

			model_check_dummy (dir->model, parent);
			model_end_loading (dir->model, parent);
		} else {
			/* Simply return if we were cancelled */
			if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
				return;

			/* Otherwise handle the error appropriately */
			g_signal_emit (dir->model,
				       model_signals[ERROR],
				       0,
				       LAPIZ_FILE_BROWSER_ERROR_LOAD_DIRECTORY,
				       error->message);

			file_browser_node_unload (dir->model, (FileBrowserNode *)parent, TRUE);
			g_error_free (error);
		}
	} else if (g_cancellable_is_cancelled (async->cancellable)) {
		/* Check cancel state manually */
		g_file_enumerator_close (enumerator, NULL, NULL);
		async_node_free (async);
	} else {
		model_add_nodes_from_files (dir->model, parent, async->original_children, files);

		g_list_free (files);
		next_files_async (enumerator, async);
	}
}

static void
next_files_async (GFileEnumerator * enumerator,
		  AsyncNode * async)
{
	g_file_enumerator_next_files_async (enumerator,
					    DIRECTORY_LOAD_ITEMS_PER_CALLBACK,
					    G_PRIORITY_DEFAULT,
					    async->cancellable,
					    (GAsyncReadyCallback)model_iterate_next_files_cb,
					    async);
}

static void
model_iterate_children_cb (GFile * file,
			   GAsyncResult * result,
			   AsyncNode * async)
{
	GError * error = NULL;
	GFileEnumerator * enumerator;

	if (g_cancellable_is_cancelled (async->cancellable))
	{
		async_node_free (async);
		return;
	}

	enumerator = g_file_enumerate_children_finish (file, result, &error);

	if (enumerator == NULL) {
		/* Simply return if we were cancelled or if the dir is not there */
		FileBrowserNodeDir *dir = async->dir;

		/* Otherwise handle the error appropriately */
		g_signal_emit (dir->model,
			       model_signals[ERROR],
			       0,
			       LAPIZ_FILE_BROWSER_ERROR_LOAD_DIRECTORY,
			       error->message);

		file_browser_node_unload (dir->model, (FileBrowserNode *)dir, TRUE);
		g_error_free (error);
		async_node_free (async);
	} else {
		next_files_async (enumerator, async);
	}
}

static void
model_load_directory (LapizFileBrowserStore * model,
		      FileBrowserNode * node)
{
	FileBrowserNodeDir *dir;
	AsyncNode *async;

	g_return_if_fail (NODE_IS_DIR (node));

	dir = FILE_BROWSER_NODE_DIR (node);

	/* Cancel a previous load */
	if (dir->cancellable != NULL) {
		file_browser_node_unload (dir->model, node, TRUE);
	}

	node->flags |= LAPIZ_FILE_BROWSER_STORE_FLAG_LOADED;
	model_begin_loading (model, node);

	dir->cancellable = g_cancellable_new ();

	async = g_new (AsyncNode, 1);
	async->dir = dir;
	async->cancellable = g_object_ref (dir->cancellable);
	async->original_children = g_slist_copy (dir->children);

	/* Start loading async */
	g_file_enumerate_children_async (node->file,
					 STANDARD_ATTRIBUTE_TYPES,
					 G_FILE_QUERY_INFO_NONE,
					 G_PRIORITY_DEFAULT,
					 async->cancellable,
					 (GAsyncReadyCallback)model_iterate_children_cb,
					 async);
}

static GList *
get_parent_files (LapizFileBrowserStore * model, GFile * file)
{
	GList * result = NULL;

	result = g_list_prepend (result, g_object_ref (file));

	while ((file = g_file_get_parent (file))) {
		if (g_file_equal (file, model->priv->root->file)) {
			g_object_unref (file);
			break;
		}

		result = g_list_prepend (result, file);
	}

	return result;
}

static void
model_fill (LapizFileBrowserStore * model, FileBrowserNode * node,
	    CtkTreePath ** path)
{
	gboolean free_path = FALSE;
	CtkTreeIter iter = {0,};
	GSList *item;
	FileBrowserNode *child;

	if (node == NULL) {
		node = model->priv->virtual_root;
		*path = ctk_tree_path_new ();
		free_path = TRUE;
	}

	if (*path == NULL) {
		*path =
		    lapiz_file_browser_store_get_path_real (model, node);
		free_path = TRUE;
	}

	if (!model_node_visibility (model, node)) {
		if (free_path)
			ctk_tree_path_free (*path);

		return;
	}

	if (node != model->priv->virtual_root) {
		/* Insert node */
		iter.user_data = node;

		row_inserted(model, path, &iter);
	}

	if (NODE_IS_DIR (node)) {
		/* Go to the first child */
		ctk_tree_path_down (*path);

		for (item = FILE_BROWSER_NODE_DIR (node)->children; item;
		     item = item->next) {
			child = (FileBrowserNode *) (item->data);

			if (model_node_visibility (model, child)) {
				model_fill (model, child, path);

				/* Increase path for next child */
				ctk_tree_path_next (*path);
			}
		}

		/* Move back up to node path */
		ctk_tree_path_up (*path);
	}

	model_check_dummy (model, node);

	if (free_path)
		ctk_tree_path_free (*path);
}

static void
set_virtual_root_from_node (LapizFileBrowserStore * model,
			    FileBrowserNode * node)
{
	FileBrowserNode *next;
	FileBrowserNode *prev;
	FileBrowserNode *check;
	FileBrowserNodeDir *dir;
	GSList *item;
	GSList *copy;
	CtkTreePath *empty = NULL;

	g_assert (node != NULL);

	prev = node;
	next = prev->parent;

	/* Free all the nodes below that we don't need in cache */
	while (prev != model->priv->root) {
		dir = FILE_BROWSER_NODE_DIR (next);
		copy = g_slist_copy (dir->children);

		for (item = copy; item; item = item->next) {
			check = (FileBrowserNode *) (item->data);

			if (prev == node) {
				/* Only free the children, keeping this depth in cache */
				if (check != node) {
					file_browser_node_free_children
					    (model, check);
					file_browser_node_unload (model,
								  check,
								  FALSE);
				}
			} else if (check != prev) {
				/* Only free when the node is not in the chain */
				dir->children =
				    g_slist_remove (dir->children, check);
				file_browser_node_free (model, check);
			}
		}

		if (prev != node)
			file_browser_node_unload (model, next, FALSE);

		g_slist_free (copy);
		prev = next;
		next = prev->parent;
	}

	/* Free all the nodes up that we don't need in cache */
	for (item = FILE_BROWSER_NODE_DIR (node)->children; item;
	     item = item->next) {
		check = (FileBrowserNode *) (item->data);

		if (NODE_IS_DIR (check)) {
			for (copy =
			     FILE_BROWSER_NODE_DIR (check)->children; copy;
			     copy = copy->next) {
				file_browser_node_free_children (model,
								 (FileBrowserNode
								  *)
								 (copy->
								  data));
				file_browser_node_unload (model,
							  (FileBrowserNode
							   *) (copy->data),
							  FALSE);
			}
		} else if (NODE_IS_DUMMY (check)) {
			check->flags |=
			    LAPIZ_FILE_BROWSER_STORE_FLAG_IS_HIDDEN;
		}
	}

	/* Now finally, set the virtual root, and load it up! */
	model->priv->virtual_root = node;

	/* Notify that the virtual-root has changed before loading up new nodes so that the
	   "root_changed" signal can be emitted before any "inserted" signals */
	g_object_notify (G_OBJECT (model), "virtual-root");

	model_fill (model, NULL, &empty);

	if (!NODE_LOADED (node))
		model_load_directory (model, node);
}

static void
set_virtual_root_from_file (LapizFileBrowserStore * model,
			    GFile * file)
{
	GList * files;
	GList * item;
	FileBrowserNode * parent;
	GFile * check;

	/* Always clear the model before altering the nodes */
	model_clear (model, FALSE);

	/* Create the node path, get all the uri's */
	files = get_parent_files (model, file);
	parent = model->priv->root;

	for (item = files; item; item = item->next) {
		check = G_FILE (item->data);

		parent = model_add_node_from_dir (model, parent, check);
		g_object_unref (check);
	}

	g_list_free (files);
	set_virtual_root_from_node (model, parent);
}

static FileBrowserNode *
model_find_node_children (LapizFileBrowserStore * model,
			  FileBrowserNode * parent,
			  GFile * file)
{
	FileBrowserNodeDir *dir;
	FileBrowserNode *child;
	FileBrowserNode *result;
	GSList *children;

	if (!NODE_IS_DIR (parent))
		return NULL;

	dir = FILE_BROWSER_NODE_DIR (parent);

	for (children = dir->children; children; children = children->next) {
		child = (FileBrowserNode *)(children->data);

		result = model_find_node (model, child, file);

		if (result)
			return result;
	}

	return NULL;
}

static FileBrowserNode *
model_find_node (LapizFileBrowserStore * model,
		 FileBrowserNode * node,
		 GFile * file)
{
	if (node == NULL)
		node = model->priv->root;

	if (node->file && g_file_equal (node->file, file))
		return node;

	if (NODE_IS_DIR (node) && g_file_has_prefix (file, node->file))
		return model_find_node_children (model, node, file);

	return NULL;
}

static GQuark
lapiz_file_browser_store_error_quark (void)
{
	static GQuark quark = 0;

	if (G_UNLIKELY (quark == 0)) {
		quark = g_quark_from_string ("lapiz_file_browser_store_error");
	}

	return quark;
}

static GFile *
unique_new_name (GFile * directory, gchar const * name)
{
	GFile * newuri = NULL;
	guint num = 0;
	gchar * newname;

	while (newuri == NULL || g_file_query_exists (newuri, NULL)) {
		if (newuri != NULL)
			g_object_unref (newuri);

		if (num == 0)
			newname = g_strdup (name);
		else
			newname = g_strdup_printf ("%s(%d)", name, num);

		newuri = g_file_get_child (directory, newname);
		g_free (newname);

		++num;
	}

	return newuri;
}

static LapizFileBrowserStoreResult
model_root_mounted (LapizFileBrowserStore * model, gchar const * virtual_root)
{
	model_check_dummy (model, model->priv->root);
	g_object_notify (G_OBJECT (model), "root");

	if (virtual_root != NULL)
		return
		    lapiz_file_browser_store_set_virtual_root_from_string
		    (model, virtual_root);
	else
		set_virtual_root_from_node (model,
					    model->priv->root);

	return LAPIZ_FILE_BROWSER_STORE_RESULT_OK;
}

static void
handle_root_error (LapizFileBrowserStore * model, GError *error)
{
	FileBrowserNode * root;

	g_signal_emit (model,
		       model_signals[ERROR],
		       0,
		       LAPIZ_FILE_BROWSER_ERROR_SET_ROOT,
		       error->message);

	/* Set the virtual root to the root */
	root = model->priv->root;
	model->priv->virtual_root = root;

	/* Set the root to be loaded */
	root->flags |= LAPIZ_FILE_BROWSER_STORE_FLAG_LOADED;

	/* Check the dummy */
	model_check_dummy (model, root);

	g_object_notify (G_OBJECT (model), "root");
	g_object_notify (G_OBJECT (model), "virtual-root");
}

static void
mount_cb (GFile * file,
	  GAsyncResult * res,
	  MountInfo * mount_info)
{
	gboolean mounted;
	GError * error = NULL;
	LapizFileBrowserStore * model = mount_info->model;

	mounted = g_file_mount_enclosing_volume_finish (file, res, &error);

	if (mount_info->model)
	{
		model->priv->mount_info = NULL;
		model_end_loading (model, model->priv->root);
	}

	if (!mount_info->model || g_cancellable_is_cancelled (mount_info->cancellable))
	{
		// Reset because it might be reused?
		g_cancellable_reset (mount_info->cancellable);
	}
	else if (mounted)
	{
		model_root_mounted (model, mount_info->virtual_root);
	}
	else if (error->code != G_IO_ERROR_CANCELLED)
	{
		handle_root_error (model, error);
	}

	if (error)
		g_error_free (error);

	g_object_unref (mount_info->operation);
	g_object_unref (mount_info->cancellable);
	g_free (mount_info->virtual_root);

	g_free (mount_info);
}

static LapizFileBrowserStoreResult
model_mount_root (LapizFileBrowserStore * model, gchar const * virtual_root)
{
	GFileInfo * info;
	GError * error = NULL;
	MountInfo * mount_info;

	info = g_file_query_info (model->priv->root->file,
				  G_FILE_ATTRIBUTE_STANDARD_TYPE,
				  G_FILE_QUERY_INFO_NONE,
				  NULL,
				  &error);

	if (!info) {
		if (error->code == G_IO_ERROR_NOT_MOUNTED) {
			/* Try to mount it */
			FILE_BROWSER_NODE_DIR (model->priv->root)->cancellable = g_cancellable_new ();

			mount_info = g_new(MountInfo, 1);
			mount_info->model = model;
			mount_info->virtual_root = g_strdup (virtual_root);

			/* FIXME: we should be setting the correct window */
			mount_info->operation = ctk_mount_operation_new (NULL);
			mount_info->cancellable = g_object_ref (FILE_BROWSER_NODE_DIR (model->priv->root)->cancellable);

			model_begin_loading (model, model->priv->root);
			g_file_mount_enclosing_volume (model->priv->root->file,
						       G_MOUNT_MOUNT_NONE,
						       mount_info->operation,
						       mount_info->cancellable,
						       (GAsyncReadyCallback)mount_cb,
						       mount_info);

			model->priv->mount_info = mount_info;
			return LAPIZ_FILE_BROWSER_STORE_RESULT_MOUNTING;
		}
		else
		{
			handle_root_error (model, error);
		}

		g_error_free (error);
	} else {
		g_object_unref (info);

		return model_root_mounted (model, virtual_root);
	}

	return LAPIZ_FILE_BROWSER_STORE_RESULT_OK;
}

/* Public */
LapizFileBrowserStore *
lapiz_file_browser_store_new (gchar const *root)
{
	LapizFileBrowserStore *obj =
	    LAPIZ_FILE_BROWSER_STORE (g_object_new
				      (LAPIZ_TYPE_FILE_BROWSER_STORE,
				       NULL));

	lapiz_file_browser_store_set_root (obj, root);
	return obj;
}

void
lapiz_file_browser_store_set_value (LapizFileBrowserStore * tree_model,
				    CtkTreeIter * iter, gint column,
				    GValue * value)
{
	gpointer data;
	FileBrowserNode *node;
	CtkTreePath *path;

	g_return_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (tree_model));
	g_return_if_fail (column ==
			  LAPIZ_FILE_BROWSER_STORE_COLUMN_EMBLEM);
	g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
	g_return_if_fail (iter != NULL);
	g_return_if_fail (iter->user_data != NULL);

	data = g_value_get_object (value);

	if (data)
		g_return_if_fail (GDK_IS_PIXBUF (data));

	node = (FileBrowserNode *) (iter->user_data);

	if (node->emblem)
		g_object_unref (node->emblem);

	if (data)
		node->emblem = g_object_ref (GDK_PIXBUF (data));
	else
		node->emblem = NULL;

	model_recomposite_icon (tree_model, iter);

	if (model_node_visibility (tree_model, node)) {
		path = lapiz_file_browser_store_get_path (CTK_TREE_MODEL (tree_model),
							  iter);
		row_changed (tree_model, &path, iter);
		ctk_tree_path_free (path);
	}
}

LapizFileBrowserStoreResult
lapiz_file_browser_store_set_virtual_root (LapizFileBrowserStore * model,
					   CtkTreeIter * iter)
{
	g_return_val_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (model),
			      LAPIZ_FILE_BROWSER_STORE_RESULT_NO_CHANGE);
	g_return_val_if_fail (iter != NULL,
			      LAPIZ_FILE_BROWSER_STORE_RESULT_NO_CHANGE);
	g_return_val_if_fail (iter->user_data != NULL,
			      LAPIZ_FILE_BROWSER_STORE_RESULT_NO_CHANGE);

	model_clear (model, FALSE);
	set_virtual_root_from_node (model,
				    (FileBrowserNode *) (iter->user_data));

	return TRUE;
}

LapizFileBrowserStoreResult
lapiz_file_browser_store_set_virtual_root_from_string
    (LapizFileBrowserStore * model, gchar const *root) {
	GFile *file;

	g_return_val_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (model),
			      LAPIZ_FILE_BROWSER_STORE_RESULT_NO_CHANGE);

	file = g_file_new_for_uri (root);
	if (file == NULL) {
		g_warning ("Invalid uri (%s)", root);
		return LAPIZ_FILE_BROWSER_STORE_RESULT_NO_CHANGE;
	}

	/* Check if uri is already the virtual root */
	if (model->priv->virtual_root &&
	    g_file_equal (model->priv->virtual_root->file, file)) {
		g_object_unref (file);
		return LAPIZ_FILE_BROWSER_STORE_RESULT_NO_CHANGE;
	}

	/* Check if uri is the root itself */
	if (g_file_equal (model->priv->root->file, file)) {
		g_object_unref (file);

		/* Always clear the model before altering the nodes */
		model_clear (model, FALSE);
		set_virtual_root_from_node (model, model->priv->root);
		return LAPIZ_FILE_BROWSER_STORE_RESULT_OK;
	}

	if (!g_file_has_prefix (file, model->priv->root->file)) {
		gchar *str, *str1;

		str = g_file_get_parse_name (model->priv->root->file);
		str1 = g_file_get_parse_name (file);

		g_warning
		    ("Virtual root (%s) is not below actual root (%s)",
		     str1, str);

		g_free (str);
		g_free (str1);

		g_object_unref (file);
		return LAPIZ_FILE_BROWSER_STORE_RESULT_ERROR;
	}

	set_virtual_root_from_file (model, file);
	g_object_unref (file);

	return LAPIZ_FILE_BROWSER_STORE_RESULT_OK;
}

LapizFileBrowserStoreResult
lapiz_file_browser_store_set_virtual_root_top (LapizFileBrowserStore *
					       model)
{
	g_return_val_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (model),
			      LAPIZ_FILE_BROWSER_STORE_RESULT_NO_CHANGE);

	if (model->priv->virtual_root == model->priv->root)
		return LAPIZ_FILE_BROWSER_STORE_RESULT_NO_CHANGE;

	model_clear (model, FALSE);
	set_virtual_root_from_node (model, model->priv->root);

	return LAPIZ_FILE_BROWSER_STORE_RESULT_OK;
}

LapizFileBrowserStoreResult
lapiz_file_browser_store_set_virtual_root_up (LapizFileBrowserStore *
					      model)
{
	g_return_val_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (model),
			      LAPIZ_FILE_BROWSER_STORE_RESULT_NO_CHANGE);

	if (model->priv->virtual_root == model->priv->root)
		return LAPIZ_FILE_BROWSER_STORE_RESULT_NO_CHANGE;

	model_clear (model, FALSE);
	set_virtual_root_from_node (model,
				    model->priv->virtual_root->parent);

	return LAPIZ_FILE_BROWSER_STORE_RESULT_OK;
}

gboolean
lapiz_file_browser_store_get_iter_virtual_root (LapizFileBrowserStore *
						model, CtkTreeIter * iter)
{
	g_return_val_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (model), FALSE);
	g_return_val_if_fail (iter != NULL, FALSE);

	if (model->priv->virtual_root == NULL)
		return FALSE;

	iter->user_data = model->priv->virtual_root;
	return TRUE;
}

gboolean
lapiz_file_browser_store_get_iter_root (LapizFileBrowserStore * model,
					CtkTreeIter * iter)
{
	g_return_val_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (model), FALSE);
	g_return_val_if_fail (iter != NULL, FALSE);

	if (model->priv->root == NULL)
		return FALSE;

	iter->user_data = model->priv->root;
	return TRUE;
}

gboolean
lapiz_file_browser_store_iter_equal (LapizFileBrowserStore * model,
				     CtkTreeIter * iter1,
				     CtkTreeIter * iter2)
{
	g_return_val_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (model), FALSE);
	g_return_val_if_fail (iter1 != NULL, FALSE);
	g_return_val_if_fail (iter2 != NULL, FALSE);
	g_return_val_if_fail (iter1->user_data != NULL, FALSE);
	g_return_val_if_fail (iter2->user_data != NULL, FALSE);

	return (iter1->user_data == iter2->user_data);
}

void
lapiz_file_browser_store_cancel_mount_operation (LapizFileBrowserStore *store)
{
	g_return_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (store));

	cancel_mount_operation (store);
}

LapizFileBrowserStoreResult
lapiz_file_browser_store_set_root_and_virtual_root (LapizFileBrowserStore *
						    model,
						    gchar const *root,
						    gchar const *virtual_root)
{
	GFile * file = NULL;
	GFile * vfile = NULL;
	FileBrowserNode * node;
	gboolean equal = FALSE;

	g_return_val_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (model),
			      LAPIZ_FILE_BROWSER_STORE_RESULT_NO_CHANGE);

	if (root == NULL && model->priv->root == NULL)
		return LAPIZ_FILE_BROWSER_STORE_RESULT_NO_CHANGE;

	if (root != NULL) {
		file = g_file_new_for_uri (root);
	}

	if (root != NULL && model->priv->root != NULL) {
		equal = g_file_equal (file, model->priv->root->file);

		if (equal && virtual_root == NULL) {
			g_object_unref (file);
			return LAPIZ_FILE_BROWSER_STORE_RESULT_NO_CHANGE;
		}
	}

	if (virtual_root) {
		vfile = g_file_new_for_uri (virtual_root);

		if (equal && g_file_equal (vfile, model->priv->virtual_root->file)) {
			if (file)
				g_object_unref (file);

			g_object_unref (vfile);
			return LAPIZ_FILE_BROWSER_STORE_RESULT_NO_CHANGE;
		}

		g_object_unref (vfile);
	}

	/* make sure to cancel any previous mount operations */
	cancel_mount_operation (model);

	/* Always clear the model before altering the nodes */
	model_clear (model, TRUE);
	file_browser_node_free (model, model->priv->root);

	model->priv->root = NULL;
	model->priv->virtual_root = NULL;

	if (file != NULL) {
		/* Create the root node */
		node = file_browser_node_dir_new (model, file, NULL);

		g_object_unref (file);

		model->priv->root = node;
		return model_mount_root (model, virtual_root);
	} else {
		g_object_notify (G_OBJECT (model), "root");
		g_object_notify (G_OBJECT (model), "virtual-root");
	}

	return LAPIZ_FILE_BROWSER_STORE_RESULT_OK;
}

LapizFileBrowserStoreResult
lapiz_file_browser_store_set_root (LapizFileBrowserStore * model,
				   gchar const *root)
{
	g_return_val_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (model),
			      LAPIZ_FILE_BROWSER_STORE_RESULT_NO_CHANGE);
	return lapiz_file_browser_store_set_root_and_virtual_root (model,
								   root,
								   NULL);
}

gchar *
lapiz_file_browser_store_get_root (LapizFileBrowserStore * model)
{
	g_return_val_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (model), NULL);

	if (model->priv->root == NULL || model->priv->root->file == NULL)
		return NULL;
	else
		return g_file_get_uri (model->priv->root->file);
}

gchar *
lapiz_file_browser_store_get_virtual_root (LapizFileBrowserStore * model)
{
	g_return_val_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (model), NULL);

	if (model->priv->virtual_root == NULL || model->priv->virtual_root->file == NULL)
		return NULL;
	else
		return g_file_get_uri (model->priv->virtual_root->file);
}

void
_lapiz_file_browser_store_iter_expanded (LapizFileBrowserStore * model,
					 CtkTreeIter * iter)
{
	FileBrowserNode *node;

	g_return_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (model));
	g_return_if_fail (iter != NULL);
	g_return_if_fail (iter->user_data != NULL);

	node = (FileBrowserNode *) (iter->user_data);

	if (NODE_IS_DIR (node) && !NODE_LOADED (node)) {
		/* Load it now */
		model_load_directory (model, node);
	}
}

void
_lapiz_file_browser_store_iter_collapsed (LapizFileBrowserStore * model,
					  CtkTreeIter * iter)
{
	FileBrowserNode *node;
	GSList *item;

	g_return_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (model));
	g_return_if_fail (iter != NULL);
	g_return_if_fail (iter->user_data != NULL);

	node = (FileBrowserNode *) (iter->user_data);

	if (NODE_IS_DIR (node) && NODE_LOADED (node)) {
		/* Unload children of the children, keeping 1 depth in cache */

		for (item = FILE_BROWSER_NODE_DIR (node)->children; item;
		     item = item->next) {
			node = (FileBrowserNode *) (item->data);

			if (NODE_IS_DIR (node) && NODE_LOADED (node)) {
				file_browser_node_unload (model, node,
							  TRUE);
				model_check_dummy (model, node);
			}
		}
	}
}

LapizFileBrowserStoreFilterMode
lapiz_file_browser_store_get_filter_mode (LapizFileBrowserStore * model)
{
	return model->priv->filter_mode;
}

void
lapiz_file_browser_store_set_filter_mode (LapizFileBrowserStore * model,
					  LapizFileBrowserStoreFilterMode
					  mode)
{
	g_return_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (model));

	if (model->priv->filter_mode == mode)
		return;

	model->priv->filter_mode = mode;
	model_refilter (model);

	g_object_notify (G_OBJECT (model), "filter-mode");
}

void
lapiz_file_browser_store_set_filter_func (LapizFileBrowserStore * model,
					  LapizFileBrowserStoreFilterFunc
					  func, gpointer user_data)
{
	g_return_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (model));

	model->priv->filter_func = func;
	model->priv->filter_user_data = user_data;
	model_refilter (model);
}

void
lapiz_file_browser_store_refilter (LapizFileBrowserStore * model)
{
	model_refilter (model);
}

LapizFileBrowserStoreFilterMode
lapiz_file_browser_store_filter_mode_get_default (void)
{
	return LAPIZ_FILE_BROWSER_STORE_FILTER_MODE_HIDE_HIDDEN;
}

void
lapiz_file_browser_store_refresh (LapizFileBrowserStore * model)
{
	g_return_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (model));

	if (model->priv->root == NULL || model->priv->virtual_root == NULL)
		return;

	/* Clear the model */
	g_signal_emit (model, model_signals[BEGIN_REFRESH], 0);
	file_browser_node_unload (model, model->priv->virtual_root, TRUE);
	model_load_directory (model, model->priv->virtual_root);
	g_signal_emit (model, model_signals[END_REFRESH], 0);
}

static void
reparent_node (FileBrowserNode * node, gboolean reparent)
{
	FileBrowserNodeDir * dir;
	GSList * child;
	GFile * parent;
	gchar * base;

	if (!node->file) {
		return;
	}

	if (reparent) {
		parent = node->parent->file;
		base = g_file_get_basename (node->file);
		g_object_unref (node->file);

		node->file = g_file_get_child (parent, base);
		g_free (base);
	}

	if (NODE_IS_DIR (node)) {
		dir = FILE_BROWSER_NODE_DIR (node);

		for (child = dir->children; child; child = child->next) {
			reparent_node ((FileBrowserNode *)child->data, TRUE);
		}
	}
}

gboolean
lapiz_file_browser_store_rename (LapizFileBrowserStore * model,
				 CtkTreeIter * iter,
				 const gchar * new_name,
				 GError ** error)
{
	FileBrowserNode *node;
	GFile * file;
	GFile * parent;
	GFile * previous;
	GError * err = NULL;
	gchar * olduri;
	gchar * newuri;
	CtkTreePath *path;

	g_return_val_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (model), FALSE);
	g_return_val_if_fail (iter != NULL, FALSE);
	g_return_val_if_fail (iter->user_data != NULL, FALSE);

	node = (FileBrowserNode *) (iter->user_data);

	parent = g_file_get_parent (node->file);
	g_return_val_if_fail (parent != NULL, FALSE);

	file = g_file_get_child (parent, new_name);
	g_object_unref (parent);

	if (g_file_equal (node->file, file)) {
		g_object_unref (file);
		return TRUE;
	}

	if (g_file_move (node->file, file, G_FILE_COPY_NONE, NULL, NULL, NULL, &err)) {
		previous = node->file;
		node->file = file;

		/* This makes sure the actual info for the node is requeried */
		file_browser_node_set_name (node);
		file_browser_node_set_from_info (model, node, NULL, TRUE);

		reparent_node (node, FALSE);

		if (model_node_visibility (model, node)) {
			path = lapiz_file_browser_store_get_path_real (model, node);
			row_changed (model, &path, iter);
			ctk_tree_path_free (path);

			/* Reorder this item */
			model_resort_node (model, node);
		} else {
			g_object_unref (previous);

			if (error != NULL)
				*error = g_error_new_literal (lapiz_file_browser_store_error_quark (),
							      LAPIZ_FILE_BROWSER_ERROR_RENAME,
				       			      _("The renamed file is currently filtered out. You need to adjust your filter settings to make the file visible"));
			return FALSE;
		}

		olduri = g_file_get_uri (previous);
		newuri = g_file_get_uri (node->file);

		g_signal_emit (model, model_signals[RENAME], 0, olduri, newuri);

		g_object_unref (previous);
		g_free (olduri);
		g_free (newuri);

		return TRUE;
	} else {
		g_object_unref (file);

		if (err) {
			if (error != NULL) {
				*error =
				    g_error_new_literal
				    (lapiz_file_browser_store_error_quark (),
				     LAPIZ_FILE_BROWSER_ERROR_RENAME,
				     err->message);
			}

			g_error_free (err);
		}

		return FALSE;
	}
}

static void
async_data_free (AsyncData * data)
{
	g_object_unref (data->cancellable);

	g_list_foreach (data->files, (GFunc)g_object_unref, NULL);
	g_list_free (data->files);

	if (!data->removed)
		data->model->priv->async_handles = g_slist_remove (data->model->priv->async_handles, data);

	g_free (data);
}

static gboolean
emit_no_trash (AsyncData * data)
{
	/* Emit the no trash error */
	gboolean ret;

	g_signal_emit (data->model, model_signals[NO_TRASH], 0, data->files, &ret);
	return ret;
}

static void
delete_file_finished (GFile        *file,
		      GAsyncResult *res,
		      AsyncData    *data)
{
	GError * error = NULL;
	gboolean ok;

	if (data->trash)
	{
		ok = g_file_trash_finish (file, res, &error);
	}
	else
	{
		ok = g_file_delete_finish (file, res, &error);
	}

	if (ok)
	{
		/* Remove the file from the model */
		FileBrowserNode *node = model_find_node (data->model, NULL, file);

		if (node != NULL)
		{
			model_remove_node (data->model, node, NULL, TRUE);
		}

		/* Process the next file */
		data->iter = data->iter->next;
	}
	else if (!ok && error != NULL)
	{
		gint code = error->code;
		g_error_free (error);

		if (data->trash && code == G_IO_ERROR_NOT_SUPPORTED) {
			/* Trash is not supported on this system. Ask the user
			 * if he wants to delete completely the files instead.
			 */
			if (emit_no_trash (data))
			{
				/* Changes this into a delete job */
				data->trash = FALSE;
				data->iter = data->files;
			}
			else
			{
				/* End the job */
				async_data_free (data);
				return;
			}
		}
		else if (code == G_IO_ERROR_CANCELLED)
		{
			/* Job has been cancelled, end the job */
			async_data_free (data);
			return;
		}
	}

	/* Continue the job */
	delete_files (data);
}

static void
delete_files (AsyncData *data)
{
	GFile *file;

	/* Check if our job is done */
	if (data->iter == NULL)
	{
		async_data_free (data);
		return;
	}

	file = G_FILE (data->iter->data);

	if (data->trash)
	{
		g_file_trash_async (file,
				    G_PRIORITY_DEFAULT,
				    data->cancellable,
				    (GAsyncReadyCallback)delete_file_finished,
				    data);
	}
	else
	{
		g_file_delete_async (file,
				     G_PRIORITY_DEFAULT,
				     data->cancellable,
				     (GAsyncReadyCallback)delete_file_finished,
				     data);
	}
}

LapizFileBrowserStoreResult
lapiz_file_browser_store_delete_all (LapizFileBrowserStore *model,
				     GList *rows, gboolean trash)
{
	FileBrowserNode * node;
	AsyncData * data;
	GList * files = NULL;
	GList * row;
	CtkTreeIter iter;
	CtkTreePath * prev = NULL;
	CtkTreePath * path;

	g_return_val_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (model), LAPIZ_FILE_BROWSER_STORE_RESULT_NO_CHANGE);

	if (rows == NULL)
		return LAPIZ_FILE_BROWSER_STORE_RESULT_NO_CHANGE;

	/* First we sort the paths so that we can later on remove any
	   files/directories that are actually subfiles/directories of
	   a directory that's also deleted */
	rows = g_list_sort (g_list_copy (rows), (GCompareFunc)ctk_tree_path_compare);

	for (row = rows; row; row = row->next) {
		path = (CtkTreePath *)(row->data);

		if (!ctk_tree_model_get_iter (CTK_TREE_MODEL (model), &iter, path))
			continue;

		/* Skip if the current path is actually a descendant of the
		   previous path */
		if (prev != NULL && ctk_tree_path_is_descendant (path, prev))
			continue;

		prev = path;
		node = (FileBrowserNode *)(iter.user_data);
		files = g_list_prepend (files, g_object_ref (node->file));
	}

	data = g_new (AsyncData, 1);

	data->model = model;
	data->cancellable = g_cancellable_new ();
	data->files = files;
	data->trash = trash;
	data->iter = files;
	data->removed = FALSE;

	model->priv->async_handles =
	    g_slist_prepend (model->priv->async_handles, data);

	delete_files (data);
	g_list_free (rows);

	return LAPIZ_FILE_BROWSER_STORE_RESULT_OK;
}

LapizFileBrowserStoreResult
lapiz_file_browser_store_delete (LapizFileBrowserStore * model,
				 CtkTreeIter * iter, gboolean trash)
{
	FileBrowserNode *node;
	GList *rows = NULL;
	LapizFileBrowserStoreResult result;

	g_return_val_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (model), LAPIZ_FILE_BROWSER_STORE_RESULT_NO_CHANGE);
	g_return_val_if_fail (iter != NULL, LAPIZ_FILE_BROWSER_STORE_RESULT_NO_CHANGE);
	g_return_val_if_fail (iter->user_data != NULL, LAPIZ_FILE_BROWSER_STORE_RESULT_NO_CHANGE);

	node = (FileBrowserNode *) (iter->user_data);

	if (NODE_IS_DUMMY (node))
		return LAPIZ_FILE_BROWSER_STORE_RESULT_NO_CHANGE;

	rows = g_list_append(NULL, lapiz_file_browser_store_get_path_real (model, node));
	result = lapiz_file_browser_store_delete_all (model, rows, trash);

	g_list_foreach (rows, (GFunc)ctk_tree_path_free, NULL);
	g_list_free (rows);

	return result;
}

gboolean
lapiz_file_browser_store_new_file (LapizFileBrowserStore * model,
				   CtkTreeIter * parent,
				   CtkTreeIter * iter)
{
	GFile * file;
	GFileOutputStream * stream;
	FileBrowserNodeDir *parent_node;
	gboolean result = FALSE;
	FileBrowserNode *node;
	GError * error = NULL;

	g_return_val_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (model), FALSE);
	g_return_val_if_fail (parent != NULL, FALSE);
	g_return_val_if_fail (parent->user_data != NULL, FALSE);
	g_return_val_if_fail (NODE_IS_DIR
			      ((FileBrowserNode *) (parent->user_data)),
			      FALSE);
	g_return_val_if_fail (iter != NULL, FALSE);

	parent_node = FILE_BROWSER_NODE_DIR (parent->user_data);
	/* Translators: This is the default name of new files created by the file browser pane. */
	file = unique_new_name (((FileBrowserNode *) parent_node)->file, _("file"));

	stream = g_file_create (file, G_FILE_CREATE_NONE, NULL, &error);

	if (!stream)
	{
		g_signal_emit (model, model_signals[ERROR], 0,
			       LAPIZ_FILE_BROWSER_ERROR_NEW_FILE,
			       error->message);
		g_error_free (error);
	} else {
		g_object_unref (stream);
		node = model_add_node_from_file (model,
						 (FileBrowserNode *)parent_node,
						 file,
						 NULL);

		if (model_node_visibility (model, node)) {
			iter->user_data = node;
			result = TRUE;
		} else {
			g_signal_emit (model, model_signals[ERROR], 0,
				       LAPIZ_FILE_BROWSER_ERROR_NEW_FILE,
				       _
				       ("The new file is currently filtered out. You need to adjust your filter settings to make the file visible"));
		}
	}

	g_object_unref (file);
	return result;
}

gboolean
lapiz_file_browser_store_new_directory (LapizFileBrowserStore * model,
					CtkTreeIter * parent,
					CtkTreeIter * iter)
{
	GFile * file;
	FileBrowserNodeDir *parent_node;
	GError * error = NULL;
	FileBrowserNode *node;
	gboolean result = FALSE;

	g_return_val_if_fail (LAPIZ_IS_FILE_BROWSER_STORE (model), FALSE);
	g_return_val_if_fail (parent != NULL, FALSE);
	g_return_val_if_fail (parent->user_data != NULL, FALSE);
	g_return_val_if_fail (NODE_IS_DIR
			      ((FileBrowserNode *) (parent->user_data)),
			      FALSE);
	g_return_val_if_fail (iter != NULL, FALSE);

	parent_node = FILE_BROWSER_NODE_DIR (parent->user_data);
	/* Translators: This is the default name of new directories created by the file browser pane. */
	file = unique_new_name (((FileBrowserNode *) parent_node)->file, _("directory"));

	if (!g_file_make_directory (file, NULL, &error)) {
		g_signal_emit (model, model_signals[ERROR], 0,
			       LAPIZ_FILE_BROWSER_ERROR_NEW_DIRECTORY,
			       error->message);
		g_error_free (error);
	} else {
		node = model_add_node_from_file (model,
						 (FileBrowserNode *)parent_node,
						 file,
						 NULL);

		if (model_node_visibility (model, node)) {
			iter->user_data = node;
			result = TRUE;
		} else {
			g_signal_emit (model, model_signals[ERROR], 0,
				       LAPIZ_FILE_BROWSER_ERROR_NEW_FILE,
				       _
				       ("The new directory is currently filtered out. You need to adjust your filter settings to make the directory visible"));
		}
	}

	g_object_unref (file);
	return result;
}

void
_lapiz_file_browser_store_register_type (GTypeModule *type_module)
{
	lapiz_file_browser_store_register_type (type_module);
}

// ex:ts=8:noet: