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 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200
|
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2006 Alexander Olk
//
// Authors:
//
// Alexander Olk alex.olk@googlemail.com
// Gert Driesen (drieseng@users.sourceforge.net)
// Eric Petit (surfzoid2002@yahoo.fr)
//
// TODO:
// Keyboard shortcuts (DEL, F5, F2)
// ??
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Resources;
using System.Text;
using System.Threading;
using System.Xml;
namespace System.Windows.Forms
{
#region FileDialog
[DefaultProperty ("FileName")]
[DefaultEvent ("FileOk")]
public abstract class FileDialog : CommonDialog
{
protected static readonly object EventFileOk = new object ();
private static int MaxFileNameItems = 10;
internal enum FileDialogType
{
OpenFileDialog,
SaveFileDialog
}
private bool addExtension = true;
private bool checkFileExists;
private bool checkPathExists = true;
private string defaultExt;
private bool dereferenceLinks = true;
private string[] fileNames;
private string filter = "";
private int filterIndex = 1;
private string initialDirectory;
private bool restoreDirectory;
private bool showHelp;
private string title;
private bool validateNames = true;
private bool auto_upgrade_enable = true;
private FileDialogCustomPlacesCollection custom_places;
private bool supportMultiDottedExtensions;
private bool checkForIllegalChars = true;
private Button cancelButton;
private ToolBarButton upToolBarButton;
private PopupButtonPanel popupButtonPanel;
private Button openSaveButton;
private Button helpButton;
private Label fileTypeLabel;
private ToolBarButton menueToolBarButton;
private ContextMenu menueToolBarButtonContextMenu;
private ToolBar smallButtonToolBar;
private DirComboBox dirComboBox;
private ComboBox fileNameComboBox;
private Label fileNameLabel;
private MWFFileView mwfFileView;
private MwfFileViewItemComparer file_view_comparer;
private Label searchSaveLabel;
private ToolBarButton newdirToolBarButton;
private ToolBarButton backToolBarButton;
private ComboBox fileTypeComboBox;
private ImageList imageListTopToolbar;
private CheckBox readonlyCheckBox;
private bool multiSelect;
private string restoreDirectoryString = String.Empty;
internal FileDialogType fileDialogType;
private bool do_not_call_OnSelectedIndexChangedFileTypeComboBox;
private bool showReadOnly;
private bool readOnlyChecked;
internal bool createPrompt;
internal bool overwritePrompt = true;
private FileFilter fileFilter;
private string[] configFileNames = null;
private string lastFolder = String.Empty;
private MWFVFS vfs;
private const string filedialog_string = "FileDialog";
private const string lastfolder_string = "LastFolder";
private const string width_string = "Width";
private const string height_string = "Height";
private const string filenames_string = "FileNames";
private const string x_string = "X";
private const string y_string = "Y";
private readonly char [] wildcard_chars = new char [] { '*', '?' };
private bool disable_form_closed_event;
internal FileDialog ()
{
form = new DialogForm (this);
vfs = new MWFVFS ();
Size formConfigSize = Size.Empty;
Point formConfigLocation = Point.Empty;
object formWidth = MWFConfig.GetValue (filedialog_string, width_string);
object formHeight = MWFConfig.GetValue (filedialog_string, height_string);
if (formHeight != null && formWidth != null)
formConfigSize = new Size ((int)formWidth, (int)formHeight);
object formLocationX = MWFConfig.GetValue (filedialog_string, x_string);
object formLocationY = MWFConfig.GetValue (filedialog_string, y_string);
if (formLocationX != null && formLocationY != null)
formConfigLocation = new Point ((int)formLocationX, (int)formLocationY);
configFileNames = (string[])MWFConfig.GetValue (filedialog_string, filenames_string);
fileTypeComboBox = new ComboBox ();
backToolBarButton = new ToolBarButton ();
newdirToolBarButton = new ToolBarButton ();
searchSaveLabel = new Label ();
mwfFileView = new MWFFileView (vfs);
fileNameLabel = new Label ();
fileNameComboBox = new ComboBox ();
dirComboBox = new DirComboBox (vfs);
smallButtonToolBar = new ToolBar ();
menueToolBarButton = new ToolBarButton ();
fileTypeLabel = new Label ();
openSaveButton = new Button ();
helpButton = new Button ();
popupButtonPanel = new PopupButtonPanel ();
upToolBarButton = new ToolBarButton ();
cancelButton = new Button ();
form.CancelButton = cancelButton;
imageListTopToolbar = new ImageList ();
menueToolBarButtonContextMenu = new ContextMenu ();
readonlyCheckBox = new CheckBox ();
form.SuspendLayout ();
//imageListTopToolbar
imageListTopToolbar.ColorDepth = ColorDepth.Depth32Bit;
imageListTopToolbar.ImageSize = new Size (16, 16); // 16, 16
imageListTopToolbar.Images.Add (ResourceImageLoader.Get ("go-previous.png"));
imageListTopToolbar.Images.Add (ResourceImageLoader.Get ("go-top.png"));
imageListTopToolbar.Images.Add (ResourceImageLoader.Get ("folder-new.png"));
imageListTopToolbar.Images.Add (ResourceImageLoader.Get ("preferences-system-windows.png"));
imageListTopToolbar.TransparentColor = Color.Transparent;
// searchLabel
searchSaveLabel.FlatStyle = FlatStyle.System;
searchSaveLabel.Location = new Point (6, 6);
searchSaveLabel.Size = new Size (86, 22);
searchSaveLabel.TextAlign = ContentAlignment.MiddleRight;
// dirComboBox
dirComboBox.Anchor = ((AnchorStyles)(((AnchorStyles.Top | AnchorStyles.Left) | AnchorStyles.Right)));
dirComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
dirComboBox.Location = new Point (99, 6);
dirComboBox.Size = new Size (261, 22);
dirComboBox.TabIndex = 7;
// smallButtonToolBar
smallButtonToolBar.Anchor = ((AnchorStyles)((AnchorStyles.Top | AnchorStyles.Right)));
smallButtonToolBar.Appearance = ToolBarAppearance.Flat;
smallButtonToolBar.AutoSize = false;
smallButtonToolBar.Buttons.AddRange (new ToolBarButton [] {
backToolBarButton,
upToolBarButton,
newdirToolBarButton,
menueToolBarButton});
smallButtonToolBar.ButtonSize = new Size (24, 24); // 21, 16
smallButtonToolBar.Divider = false;
smallButtonToolBar.Dock = DockStyle.None;
smallButtonToolBar.DropDownArrows = true;
smallButtonToolBar.ImageList = imageListTopToolbar;
smallButtonToolBar.Location = new Point (372, 6);
smallButtonToolBar.ShowToolTips = true;
smallButtonToolBar.Size = new Size (140, 28);
smallButtonToolBar.TabIndex = 8;
smallButtonToolBar.TextAlign = ToolBarTextAlign.Right;
// buttonPanel
popupButtonPanel.Dock = DockStyle.None;
popupButtonPanel.Anchor = ((AnchorStyles)((((AnchorStyles.Top | AnchorStyles.Bottom) | AnchorStyles.Left))));
popupButtonPanel.Location = new Point (6, 35);
popupButtonPanel.Size = new Size (87, 338);
popupButtonPanel.TabIndex = 9;
// mwfFileView
mwfFileView.Anchor = ((AnchorStyles)((((AnchorStyles.Top | AnchorStyles.Bottom) | AnchorStyles.Left) | AnchorStyles.Right)));
mwfFileView.Location = new Point (99, 35);
mwfFileView.Size = new Size (450, 283);
mwfFileView.MultiSelect = false;
mwfFileView.TabIndex = 10;
mwfFileView.RegisterSender (dirComboBox);
mwfFileView.RegisterSender (popupButtonPanel);
// fileNameLabel
fileNameLabel.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Left)));
fileNameLabel.FlatStyle = FlatStyle.System;
fileNameLabel.Location = new Point (101, 326);
fileNameLabel.Size = new Size (70, 21);
fileNameLabel.Text = "File name:";
fileNameLabel.TextAlign = ContentAlignment.MiddleLeft;
// fileNameComboBox
fileNameComboBox.Anchor = ((AnchorStyles)(((AnchorStyles.Bottom | AnchorStyles.Left) | AnchorStyles.Right)));
fileNameComboBox.Location = new Point (195, 326);
fileNameComboBox.Size = new Size (246, 22);
fileNameComboBox.TabIndex = 1;
fileNameComboBox.MaxDropDownItems = MaxFileNameItems;
fileNameComboBox.RestoreContextMenu ();
UpdateRecentFiles ();
// fileTypeLabel
fileTypeLabel.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Left)));
fileTypeLabel.FlatStyle = FlatStyle.System;
fileTypeLabel.Location = new Point (101, 355);
fileTypeLabel.Size = new Size (90, 21);
fileTypeLabel.Text = "Files of type:";
fileTypeLabel.TextAlign = ContentAlignment.MiddleLeft;
// fileTypeComboBox
fileTypeComboBox.Anchor = ((AnchorStyles)(((AnchorStyles.Bottom | AnchorStyles.Left) | AnchorStyles.Right)));
fileTypeComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
fileTypeComboBox.Location = new Point (195, 355);
fileTypeComboBox.Size = new Size (246, 22);
fileTypeComboBox.TabIndex = 2;
// backToolBarButton
backToolBarButton.ImageIndex = 0;
backToolBarButton.Enabled = false;
backToolBarButton.Style = ToolBarButtonStyle.PushButton;
mwfFileView.AddControlToEnableDisableByDirStack (backToolBarButton);
// upToolBarButton
upToolBarButton.ImageIndex = 1;
upToolBarButton.Style = ToolBarButtonStyle.PushButton;
mwfFileView.SetFolderUpToolBarButton (upToolBarButton);
// newdirToolBarButton
newdirToolBarButton.ImageIndex = 2;
newdirToolBarButton.Style = ToolBarButtonStyle.PushButton;
// menueToolBarButton
menueToolBarButton.ImageIndex = 3;
menueToolBarButton.DropDownMenu = menueToolBarButtonContextMenu;
menueToolBarButton.Style = ToolBarButtonStyle.DropDownButton;
// menueToolBarButtonContextMenu
menueToolBarButtonContextMenu.MenuItems.AddRange (mwfFileView.ViewMenuItems);
// openSaveButton
openSaveButton.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
openSaveButton.FlatStyle = FlatStyle.System;
openSaveButton.Location = new Point (474, 326);
openSaveButton.Size = new Size (75, 23);
openSaveButton.TabIndex = 4;
openSaveButton.FlatStyle = FlatStyle.System;
// cancelButton
cancelButton.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
cancelButton.FlatStyle = FlatStyle.System;
cancelButton.Location = new Point (474, 353);
cancelButton.Size = new Size (75, 23);
cancelButton.TabIndex = 5;
cancelButton.Text = "Cancel";
cancelButton.FlatStyle = FlatStyle.System;
// helpButton
helpButton.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
helpButton.FlatStyle = FlatStyle.System;
helpButton.Location = new Point (474, 353);
helpButton.Size = new Size (75, 23);
helpButton.TabIndex = 6;
helpButton.Text = "Help";
helpButton.FlatStyle = FlatStyle.System;
helpButton.Visible = false;
// checkBox
readonlyCheckBox.Anchor = ((AnchorStyles)(((AnchorStyles.Bottom | AnchorStyles.Left) | AnchorStyles.Right)));
readonlyCheckBox.Text = "Open Readonly";
readonlyCheckBox.Location = new Point (195, 350);
readonlyCheckBox.Size = new Size (245, 21);
readonlyCheckBox.TabIndex = 3;
readonlyCheckBox.FlatStyle = FlatStyle.System;
readonlyCheckBox.Visible = false;
form.SizeGripStyle = SizeGripStyle.Show;
form.AcceptButton = openSaveButton;
form.MaximizeBox = true;
form.MinimizeBox = true;
form.FormBorderStyle = FormBorderStyle.Sizable;
form.ClientSize = new Size (555, 385);
form.MinimumSize = form.Size;
form.Controls.Add (smallButtonToolBar);
form.Controls.Add (cancelButton);
form.Controls.Add (openSaveButton);
form.Controls.Add (mwfFileView);
form.Controls.Add (fileTypeLabel);
form.Controls.Add (fileNameLabel);
form.Controls.Add (fileTypeComboBox);
form.Controls.Add (fileNameComboBox);
form.Controls.Add (dirComboBox);
form.Controls.Add (searchSaveLabel);
form.Controls.Add (popupButtonPanel);
form.Controls.Add (helpButton);
form.Controls.Add (readonlyCheckBox);
form.ResumeLayout (true);
if (formConfigSize != Size.Empty) {
form.ClientSize = formConfigSize;
}
if (formConfigLocation != Point.Empty) {
form.Location = formConfigLocation;
}
openSaveButton.Click += new EventHandler (OnClickOpenSaveButton);
cancelButton.Click += new EventHandler (OnClickCancelButton);
helpButton.Click += new EventHandler (OnClickHelpButton);
smallButtonToolBar.ButtonClick += new ToolBarButtonClickEventHandler (OnClickSmallButtonToolBar);
fileTypeComboBox.SelectedIndexChanged += new EventHandler (OnSelectedIndexChangedFileTypeComboBox);
mwfFileView.SelectedFileChanged += new EventHandler (OnSelectedFileChangedFileView);
mwfFileView.ForceDialogEnd += new EventHandler (OnForceDialogEndFileView);
mwfFileView.SelectedFilesChanged += new EventHandler (OnSelectedFilesChangedFileView);
mwfFileView.ColumnClick += new ColumnClickEventHandler(OnColumnClickFileView);
dirComboBox.DirectoryChanged += new EventHandler (OnDirectoryChangedDirComboBox);
popupButtonPanel.DirectoryChanged += new EventHandler (OnDirectoryChangedPopupButtonPanel);
readonlyCheckBox.CheckedChanged += new EventHandler (OnCheckCheckChanged);
form.FormClosed += new FormClosedEventHandler (OnFileDialogFormClosed);
custom_places = new FileDialogCustomPlacesCollection ();
}
[DefaultValue(true)]
public bool AddExtension {
get {
return addExtension;
}
set {
addExtension = value;
}
}
[MonoTODO ("Stub, value not respected")]
[DefaultValue (true)]
public bool AutoUpgradeEnabled {
get { return auto_upgrade_enable; }
set { auto_upgrade_enable = value; }
}
[DefaultValue(false)]
public virtual bool CheckFileExists {
get {
return checkFileExists;
}
set {
checkFileExists = value;
}
}
[DefaultValue(true)]
public bool CheckPathExists {
get {
return checkPathExists;
}
set {
checkPathExists = value;
}
}
[MonoTODO ("Stub, collection not used")]
[Browsable (false)]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public FileDialogCustomPlacesCollection CustomPlaces {
get { return custom_places; }
}
[DefaultValue("")]
public string DefaultExt {
get {
if (defaultExt == null)
return string.Empty;
return defaultExt;
}
set {
if (value != null && value.Length > 0) {
// remove leading dot
if (value [0] == '.')
value = value.Substring (1);
}
defaultExt = value;
}
}
// in MS.NET it doesn't make a difference if
// DerefenceLinks is true or false
// if the selected file is a link FileDialog
// always returns the link
[DefaultValue(true)]
public bool DereferenceLinks {
get {
return dereferenceLinks;
}
set {
dereferenceLinks = value;
}
}
[DefaultValue("")]
public string FileName {
get {
if (fileNames == null || fileNames.Length == 0)
return string.Empty;
if (fileNames [0].Length == 0)
return string.Empty;
// skip check for illegal characters if the filename was set
// through FileDialog API
if (!checkForIllegalChars)
return fileNames [0];
// ensure filename contains only valid characters
Path.GetFullPath (fileNames [0]);
// but return filename as is
return fileNames [0];
}
set {
if (value != null) {
fileNames = new string [1] { value };
} else {
fileNames = new string [0];
}
// skip check for illegal characters if the filename was set
// through FileDialog API
checkForIllegalChars = false;
}
}
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string[] FileNames {
get {
if (fileNames == null || fileNames.Length == 0) {
return new string [0];
}
string[] new_filenames = new string [fileNames.Length];
fileNames.CopyTo (new_filenames, 0);
// skip check for illegal characters if the filename was set
// through FileDialog API
if (!checkForIllegalChars)
return new_filenames;
foreach (string fileName in new_filenames) {
// ensure filename contains only valid characters
Path.GetFullPath (fileName);
}
return new_filenames;
}
}
[DefaultValue("")]
[Localizable(true)]
public string Filter {
get {
return filter;
}
set {
if (value == null) {
filter = "";
if (fileFilter != null)
fileFilter.FilterArrayList.Clear ();
} else {
if (FileFilter.CheckFilter (value)) {
filter = value;
fileFilter = new FileFilter (filter);
} else
throw new ArgumentException ("The provided filter string"
+ " is invalid. The filter string should contain a"
+ " description of the filter, followed by the "
+ " vertical bar (|) and the filter pattern. The"
+ " strings for different filtering options should"
+ " also be separated by the vertical bar. Example:"
+ " Text files (*.txt)|*.txt|All files (*.*)|*.*");
}
UpdateFilters ();
}
}
[DefaultValue(1)]
public int FilterIndex {
get {
return filterIndex;
}
set {
filterIndex = value;
}
}
[DefaultValue("")]
public string InitialDirectory {
get {
if (initialDirectory == null)
return string.Empty;
return initialDirectory;
}
set {
initialDirectory = value;
}
}
[DefaultValue(false)]
public bool RestoreDirectory {
get {
return restoreDirectory;
}
set {
restoreDirectory = value;
}
}
[DefaultValue(false)]
public bool ShowHelp {
get {
return showHelp;
}
set {
showHelp = value;
ResizeAndRelocateForHelpOrReadOnly ();
}
}
[DefaultValue(false)]
public bool SupportMultiDottedExtensions {
get {
return supportMultiDottedExtensions;
}
set {
supportMultiDottedExtensions = value;
}
}
[DefaultValue("")]
[Localizable(true)]
public string Title {
get {
if (title == null)
return string.Empty;
return title;
}
set {
title = value;
}
}
// this one is a hard one ;)
// Win32 filename:
// - up to MAX_PATH characters (windef.h) = 260
// - no trailing dots or spaces
// - case preserving
// - etc...
// NTFS/Posix filename:
// - up to 32,768 Unicode characters
// - trailing periods or spaces
// - case sensitive
// - etc...
[DefaultValue(true)]
public bool ValidateNames {
get {
return validateNames;
}
set {
validateNames = value;
}
}
public override void Reset ()
{
addExtension = true;
checkFileExists = false;
checkPathExists = true;
DefaultExt = null;
dereferenceLinks = true;
FileName = null;
Filter = String.Empty;
FilterIndex = 1;
InitialDirectory = null;
restoreDirectory = false;
SupportMultiDottedExtensions = false;
ShowHelp = false;
Title = null;
validateNames = true;
UpdateFilters ();
}
public override string ToString ()
{
return String.Format("{0}: Title: {1}, FileName: {2}", base.ToString (), Title, FileName);
}
public event CancelEventHandler FileOk {
add { Events.AddHandler (EventFileOk, value); }
remove { Events.RemoveHandler (EventFileOk, value); }
}
protected virtual IntPtr Instance {
get {
if (form == null)
return IntPtr.Zero;
return form.Handle;
}
}
// This is just for internal use with MSs version, so it doesn't need to be implemented
// as it can't really be accessed anyways
protected int Options {
get { return -1; }
}
internal virtual string DialogTitle {
get {
return Title;
}
}
[MonoTODO ("Not implemented, will throw NotImplementedException")]
protected override IntPtr HookProc (IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam)
{
throw new NotImplementedException ();
}
protected void OnFileOk (CancelEventArgs e)
{
CancelEventHandler fo = (CancelEventHandler) Events [EventFileOk];
if (fo != null)
fo (this, e);
}
private void CleanupOnClose ()
{
WriteConfigValues ();
Mime.CleanFileCache ();
disable_form_closed_event = true;
}
protected override bool RunDialog (IntPtr hWndOwner)
{
ReadConfigValues ();
form.Text = DialogTitle;
// avoid using the FileNames property to skip the invalid characters
// check
string fileName = null;
if (fileNames != null && fileNames.Length != 0)
fileName = fileNames [0];
else
fileName = string.Empty;
SelectFilter ();
form.Refresh ();
SetFileAndDirectory (fileName);
fileNameComboBox.Select ();
return true;
}
internal virtual bool ShowReadOnly {
set {
showReadOnly = value;
ResizeAndRelocateForHelpOrReadOnly ();
}
get {
return showReadOnly;
}
}
internal virtual bool ReadOnlyChecked {
set {
readOnlyChecked = value;
readonlyCheckBox.Checked = value;
}
get {
return readOnlyChecked;
}
}
internal bool BMultiSelect {
set {
multiSelect = value;
mwfFileView.MultiSelect = value;
}
get {
return multiSelect;
}
}
internal string OpenSaveButtonText {
set {
openSaveButton.Text = value;
}
}
internal string SearchSaveLabel {
set {
searchSaveLabel.Text = value;
}
}
internal string FileTypeLabel {
set {
fileTypeLabel.Text = value;
}
}
internal string CustomFilter {
get {
string fname = fileNameComboBox.Text;
if (fname.IndexOfAny (wildcard_chars) == -1)
return null;
return fname;
}
}
private void SelectFilter ()
{
int filter_to_select = (filterIndex - 1);
if (mwfFileView.FilterArrayList == null || mwfFileView.FilterArrayList.Count == 0) {
filter_to_select = -1;
} else {
if (filter_to_select < 0 || filter_to_select >= mwfFileView.FilterArrayList.Count)
filter_to_select = 0;
}
do_not_call_OnSelectedIndexChangedFileTypeComboBox = true;
fileTypeComboBox.BeginUpdate ();
fileTypeComboBox.SelectedIndex = filter_to_select;
fileTypeComboBox.EndUpdate ();
do_not_call_OnSelectedIndexChangedFileTypeComboBox = false;
mwfFileView.FilterIndex = filter_to_select + 1;
}
private void SetFileAndDirectory (string fname)
{
if (fname.Length != 0) {
bool rooted = Path.IsPathRooted (fname);
if (!rooted) {
mwfFileView.ChangeDirectory (null, lastFolder);
fileNameComboBox.Text = fname;
} else {
string dirname = Path.GetDirectoryName (fname);
if (dirname != null && dirname.Length > 0 && Directory.Exists (dirname)) {
fileNameComboBox.Text = Path.GetFileName (fname);
mwfFileView.ChangeDirectory (null, dirname);
} else {
fileNameComboBox.Text = fname;
mwfFileView.ChangeDirectory (null, lastFolder);
}
}
} else {
mwfFileView.ChangeDirectory (null, lastFolder);
fileNameComboBox.Text = null;
}
}
void OnClickOpenSaveButton (object sender, EventArgs e)
{
// for filenames typed or selected by user, enable check for
// illegal characters in filename(s)
checkForIllegalChars = true;
if (fileDialogType == FileDialogType.OpenFileDialog) {
ListView.SelectedListViewItemCollection sl = mwfFileView.SelectedItems;
if (sl.Count > 0 && sl [0] != null) {
if (sl.Count == 1) {
FileViewListViewItem item = sl [0] as FileViewListViewItem;
FSEntry fsEntry = item.FSEntry;
if ((fsEntry.Attributes & FileAttributes.Directory) == FileAttributes.Directory) {
mwfFileView.ChangeDirectory (null, fsEntry.FullName, CustomFilter);
return;
}
} else {
foreach (FileViewListViewItem item in sl) {
FSEntry fsEntry = item.FSEntry;
if ((fsEntry.Attributes & FileAttributes.Directory) == FileAttributes.Directory) {
mwfFileView.ChangeDirectory (null, fsEntry.FullName, CustomFilter);
return;
}
}
}
}
}
// Custom filter, typed by the user, ignoring the stored filters
if (fileNameComboBox.Text.IndexOfAny (wildcard_chars) != -1) {
mwfFileView.UpdateFileView (fileNameComboBox.Text);
return;
}
ArrayList files = new ArrayList ();
FileNamesTokenizer tokenizer = new FileNamesTokenizer (
fileNameComboBox.Text, multiSelect);
tokenizer.GetNextFile ();
while (tokenizer.CurrentToken != TokenType.EOF) {
string fileName = tokenizer.TokenText;
string internalfullfilename;
if (!Path.IsPathRooted (fileName)) {
// on unix currentRealFolder for "Recently used files" is null,
// because recently used files don't get saved as links in a directory
// recently used files get saved in a xml file
if (mwfFileView.CurrentRealFolder != null)
fileName = Path.Combine (mwfFileView.CurrentRealFolder, fileName);
else
if (mwfFileView.CurrentFSEntry != null) {
fileName = mwfFileView.CurrentFSEntry.FullName;
}
}
FileInfo fileInfo = new FileInfo (fileName);
if (fileInfo.Exists || fileDialogType == FileDialogType.SaveFileDialog) {
internalfullfilename = fileName;
} else {
DirectoryInfo dirInfo = new DirectoryInfo (fileName);
if (dirInfo.Exists) {
mwfFileView.ChangeDirectory (null, dirInfo.FullName, CustomFilter);
fileNameComboBox.Text = null;
return;
} else {
internalfullfilename = fileName;
}
}
if (addExtension) {
string current_extension = Path.GetExtension (fileName);
if (current_extension.Length == 0) {
string filter_extension = string.Empty;
if (AddFilterExtension (internalfullfilename))
filter_extension = GetExtension (internalfullfilename);
if (filter_extension.Length == 0 && DefaultExt.Length > 0) {
filter_extension = "." + DefaultExt;
if (checkFileExists) {
// ignore DefaultExt if file not exist
if (!File.Exists (internalfullfilename + filter_extension))
filter_extension = string.Empty;
}
}
internalfullfilename += filter_extension;
}
}
if (checkFileExists) {
if (!File.Exists (internalfullfilename)) {
string message = "\"" + internalfullfilename + "\" does not exist. Please verify that you have entered the correct file name.";
MessageBox.Show (message, openSaveButton.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
}
if (fileDialogType == FileDialogType.SaveFileDialog) {
if (overwritePrompt) {
if (File.Exists (internalfullfilename)) {
string message = "\"" + internalfullfilename + "\" already exists. Do you want to overwrite it?";
DialogResult dr = MessageBox.Show (message, openSaveButton.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
if (dr == DialogResult.Cancel)
return;
}
}
if (createPrompt) {
if (!File.Exists (internalfullfilename)) {
string message = "\"" + internalfullfilename + "\" does not exist. Do you want to create it?";
DialogResult dr = MessageBox.Show (message, openSaveButton.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
if (dr == DialogResult.Cancel)
return;
}
}
}
files.Add (internalfullfilename);
tokenizer.GetNextFile ();
}
if (files.Count > 0) {
fileNames = new string [files.Count];
for (int i = 0; i < files.Count; i++) {
string fileName = (string) files [i];
fileNames [i] = fileName;
mwfFileView.WriteRecentlyUsed (fileName);
if (!File.Exists (fileName))
// ignore files that do not exist
continue;
if (fileNameComboBox.Items.IndexOf (fileName) == -1)
fileNameComboBox.Items.Insert (0, fileName);
}
// remove items above the maximum items that we want to display
while (fileNameComboBox.Items.Count > MaxFileNameItems)
fileNameComboBox.Items.RemoveAt (MaxFileNameItems);
} else {
// If a directory is selected, navigate into it
foreach (FileViewListViewItem item in mwfFileView.SelectedItems) {
FSEntry fsEntry = item.FSEntry;
if ((fsEntry.Attributes & FileAttributes.Directory) == FileAttributes.Directory) {
mwfFileView.ChangeDirectory (null, fsEntry.FullName, CustomFilter);
return;
}
}
return;
}
if (checkPathExists && mwfFileView.CurrentRealFolder != null) {
if (!Directory.Exists (mwfFileView.CurrentRealFolder)) {
string message = "\"" + mwfFileView.CurrentRealFolder + "\" does not exist. Please verify that you have entered the correct directory name.";
MessageBox.Show (message, openSaveButton.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
if (InitialDirectory.Length == 0 || !Directory.Exists (InitialDirectory))
mwfFileView.ChangeDirectory (null, lastFolder, CustomFilter);
else
mwfFileView.ChangeDirectory (null, InitialDirectory, CustomFilter);
return;
}
}
if (restoreDirectory) {
lastFolder = restoreDirectoryString;
} else {
lastFolder = mwfFileView.CurrentFolder;
}
// update value of FilterIndex with user-selected filter
filterIndex = fileTypeComboBox.SelectedIndex + 1;
CancelEventArgs cancelEventArgs = new CancelEventArgs ();
OnFileOk (cancelEventArgs);
if (cancelEventArgs.Cancel)
return;
CleanupOnClose ();
form.DialogResult = DialogResult.OK;
}
bool AddFilterExtension (string fileName)
{
if (fileDialogType == FileDialogType.OpenFileDialog) {
if (DefaultExt.Length == 0)
return true;
if (checkFileExists) {
// if CheckFileExists is true, only add filter extension if
// file with DefaultExt does not exist
string fullFileName = fileName + "." + DefaultExt;
return !File.Exists (fullFileName);
} else {
// if CheckFileExists is false, only add filter extension
// if specified file does not exist
return !File.Exists (fileName);
}
}
return true;
}
string GetExtension (string fileName)
{
string filter_extension = String.Empty;
if (fileFilter == null || fileTypeComboBox.SelectedIndex == -1)
return filter_extension;
FilterStruct filterstruct = (FilterStruct) fileFilter.FilterArrayList
[fileTypeComboBox.SelectedIndex];
for (int i = 0; i < filterstruct.filters.Count; i++) {
string extension = filterstruct.filters [i];
if (extension.StartsWith ("*"))
extension = extension.Remove (0, 1);
if (extension.IndexOf ('*') != -1)
continue;
if (!supportMultiDottedExtensions) {
int lastdot = extension.LastIndexOf('.');
if (lastdot > 0) {
if (extension.LastIndexOf('.', lastdot - 1) != -1) {
extension = extension.Remove(0, lastdot);
}
}
}
if (!checkFileExists) {
filter_extension = extension;
break;
}
if (fileDialogType == FileDialogType.SaveFileDialog) {
// when DefaultExt is set, only consider first filter
// extension (and do not check if file exists)
if (DefaultExt.Length > 0) {
filter_extension = extension;
break;
}
}
// MSDN: If the CheckFileExists property is true,
// the dialog box adds the first extension from the
// current file filter that matches an existing file
string fullfilename = fileName + extension;
if (File.Exists (fullfilename)) {
filter_extension = extension;
break;
} else {
if (fileDialogType == FileDialogType.SaveFileDialog) {
// when DefaultExt is set, only consider first filter
// extension
if (DefaultExt.Length > 0) {
filter_extension = extension;
break;
}
}
}
}
return filter_extension;
}
void OnClickCancelButton (object sender, EventArgs e)
{
if (restoreDirectory)
mwfFileView.CurrentFolder = restoreDirectoryString;
CleanupOnClose ();
form.DialogResult = DialogResult.Cancel;
}
void OnClickHelpButton (object sender, EventArgs e)
{
OnHelpRequest (e);
}
void OnClickSmallButtonToolBar (object sender, ToolBarButtonClickEventArgs e)
{
if (e.Button == upToolBarButton) {
mwfFileView.OneDirUp (CustomFilter);
} else
if (e.Button == backToolBarButton) {
mwfFileView.PopDir (CustomFilter);
} else
if (e.Button == newdirToolBarButton) {
mwfFileView.CreateNewFolder ();
}
}
void OnSelectedIndexChangedFileTypeComboBox (object sender, EventArgs e)
{
if (do_not_call_OnSelectedIndexChangedFileTypeComboBox) {
do_not_call_OnSelectedIndexChangedFileTypeComboBox = false;
return;
}
UpdateRecentFiles ();
mwfFileView.FilterIndex = fileTypeComboBox.SelectedIndex + 1;
}
void OnSelectedFileChangedFileView (object sender, EventArgs e)
{
fileNameComboBox.Text = mwfFileView.CurrentFSEntry.Name;
}
void OnSelectedFilesChangedFileView (object sender, EventArgs e)
{
string selectedFiles = mwfFileView.SelectedFilesString;
if (selectedFiles != null && selectedFiles.Length != 0)
fileNameComboBox.Text = selectedFiles;
}
void OnForceDialogEndFileView (object sender, EventArgs e)
{
OnClickOpenSaveButton (this, EventArgs.Empty);
}
void OnDirectoryChangedDirComboBox (object sender, EventArgs e)
{
mwfFileView.ChangeDirectory (sender, dirComboBox.CurrentFolder, CustomFilter);
}
void OnDirectoryChangedPopupButtonPanel (object sender, EventArgs e)
{
mwfFileView.ChangeDirectory (sender, popupButtonPanel.CurrentFolder, CustomFilter);
}
void OnCheckCheckChanged (object sender, EventArgs e)
{
ReadOnlyChecked = readonlyCheckBox.Checked;
}
void OnFileDialogFormClosed (object sender, FormClosedEventArgs e)
{
HandleFormClosedEvent (sender);
}
private void OnColumnClickFileView (object sender, ColumnClickEventArgs e)
{
if (file_view_comparer == null)
file_view_comparer = new MwfFileViewItemComparer (true);
file_view_comparer.ColumnIndex = e.Column;
file_view_comparer.Ascendent = !file_view_comparer.Ascendent;
if (mwfFileView.ListViewItemSorter == null)
mwfFileView.ListViewItemSorter = file_view_comparer;
else
mwfFileView.Sort ();
}
void HandleFormClosedEvent (object sender)
{
if (!disable_form_closed_event)
OnClickCancelButton (sender, EventArgs.Empty);
disable_form_closed_event = false;
}
private void UpdateFilters ()
{
if (fileFilter == null)
fileFilter = new FileFilter ();
ArrayList filters = fileFilter.FilterArrayList;
fileTypeComboBox.BeginUpdate ();
fileTypeComboBox.Items.Clear ();
foreach (FilterStruct fs in filters) {
fileTypeComboBox.Items.Add (fs.filterName);
}
fileTypeComboBox.EndUpdate ();
mwfFileView.FilterArrayList = filters;
}
private void UpdateRecentFiles ()
{
fileNameComboBox.Items.Clear ();
if (configFileNames != null) {
foreach (string configFileName in configFileNames) {
if (configFileName == null || configFileName.Trim ().Length == 0)
continue;
// add no more than 10 items
if (fileNameComboBox.Items.Count >= MaxFileNameItems)
break;
fileNameComboBox.Items.Add (configFileName);
}
}
}
private void ResizeAndRelocateForHelpOrReadOnly ()
{
form.SuspendLayout ();
int fx = form.Size.Width - form.MinimumSize.Width;
int fy = form.Size.Height - form.MinimumSize.Height;
if (!ShowHelp && !ShowReadOnly)
fy += 29;
mwfFileView.Size = new Size (450 + fx, 254 + fy);
fileNameLabel.Location = new Point (101, 298 + fy);
fileNameComboBox.Location = new Point (195, 298 + fy);
fileTypeLabel.Location = new Point (101, 326 + fy);
fileTypeComboBox.Location = new Point (195, 326 + fy);
openSaveButton.Location = new Point (474 + fx, 298 + fy);
cancelButton.Location = new Point (474 + fx, 324 + fy);
helpButton.Location = new Point (474 + fx, 353 + fy);
readonlyCheckBox.Location = new Point (195, 350 + fy);
helpButton.Visible = ShowHelp;
readonlyCheckBox.Visible = ShowReadOnly;
form.ResumeLayout ();
}
private void WriteConfigValues ()
{
MWFConfig.SetValue (filedialog_string, width_string, form.ClientSize.Width);
MWFConfig.SetValue (filedialog_string, height_string, form.ClientSize.Height);
MWFConfig.SetValue (filedialog_string, x_string, form.Location.X);
MWFConfig.SetValue (filedialog_string, y_string, form.Location.Y);
MWFConfig.SetValue (filedialog_string, lastfolder_string, lastFolder);
string[] fileNameCBItems = new string [fileNameComboBox.Items.Count];
fileNameComboBox.Items.CopyTo (fileNameCBItems, 0);
MWFConfig.SetValue (filedialog_string, filenames_string, fileNameCBItems);
}
private void ReadConfigValues ()
{
lastFolder = (string)MWFConfig.GetValue (filedialog_string, lastfolder_string);
if (lastFolder != null && lastFolder.IndexOf ("://") == -1) {
if (!Directory.Exists (lastFolder)) {
lastFolder = MWFVFS.DesktopPrefix;
}
}
if (InitialDirectory.Length > 0 && Directory.Exists (InitialDirectory))
lastFolder = InitialDirectory;
else
if (lastFolder == null || lastFolder.Length == 0)
lastFolder = Environment.CurrentDirectory;
if (RestoreDirectory)
restoreDirectoryString = lastFolder;
}
class FileNamesTokenizer
{
public FileNamesTokenizer (string text, bool allowMultiple)
{
_text = text;
_position = 0;
_tokenType = TokenType.BOF;
_allowMultiple = allowMultiple;
}
public TokenType CurrentToken {
get { return _tokenType; }
}
public string TokenText {
get { return _tokenText; }
}
public bool AllowMultiple {
get { return _allowMultiple; }
}
private int ReadChar ()
{
if (_position < _text.Length) {
return _text [_position++];
} else {
return -1;
}
}
private int PeekChar ()
{
if (_position < _text.Length) {
return _text [_position];
} else {
return -1;
}
}
private void SkipWhitespaceAndQuotes ()
{
int ch;
while ((ch = PeekChar ()) != -1) {
if ((char) ch != '"' && !char.IsWhiteSpace ((char) ch))
break;
ReadChar ();
}
}
public void GetNextFile ()
{
if (_tokenType == TokenType.EOF)
throw new Exception ("");
int ch;
SkipWhitespaceAndQuotes ();
if (PeekChar () == -1) {
_tokenType = TokenType.EOF;
return;
}
_tokenType = TokenType.FileName;
StringBuilder sb = new StringBuilder ();
while ((ch = PeekChar ()) != -1) {
if ((char) ch == '"') {
ReadChar ();
if (AllowMultiple)
break;
int pos = _position;
SkipWhitespaceAndQuotes ();
if (PeekChar () == -1) {
break;
}
_position = ++pos;
sb.Append ((char) ch);
} else {
sb.Append ((char) ReadChar ());
}
}
_tokenText = sb.ToString ();
}
private readonly bool _allowMultiple;
private int _position;
private readonly string _text;
private TokenType _tokenType;
private string _tokenText;
}
internal enum TokenType
{
BOF,
EOF,
FileName,
}
}
#endregion
#region PopupButtonPanel
internal class PopupButtonPanel : Control, IUpdateFolder
{
#region PopupButton
internal class PopupButton : Control
{
internal enum PopupButtonState
{ Normal, Down, Up}
private Image image = null;
private PopupButtonState popupButtonState = PopupButtonState.Normal;
private StringFormat text_format = new StringFormat();
private Rectangle text_rect = Rectangle.Empty;
public PopupButton ()
{
text_format.Alignment = StringAlignment.Center;
text_format.LineAlignment = StringAlignment.Near;
SetStyle (ControlStyles.DoubleBuffer, true);
SetStyle (ControlStyles.AllPaintingInWmPaint, true);
SetStyle (ControlStyles.UserPaint, true);
SetStyle (ControlStyles.Selectable, false);
}
public Image Image {
set {
image = value;
Invalidate ();
}
get {
return image;
}
}
public PopupButtonState ButtonState {
set {
popupButtonState = value;
Invalidate ();
}
get {
return popupButtonState;
}
}
#region UIA Framework Members
internal void PerformClick ()
{
OnClick (EventArgs.Empty);
}
#endregion
protected override void OnPaint (PaintEventArgs pe)
{
Draw (pe);
base.OnPaint (pe);
}
private void Draw (PaintEventArgs pe)
{
Graphics gr = pe.Graphics;
gr.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (BackColor), ClientRectangle);
// draw image
if (image != null) {
int i_x = (ClientSize.Width - image.Width) / 2;
int i_y = 4;
gr.DrawImage (image, i_x, i_y);
}
if (Text != String.Empty) {
if (text_rect == Rectangle.Empty)
text_rect = new Rectangle (0, Height - 30, Width, Height - 30);
gr.DrawString (Text, Font, Brushes.White, text_rect, text_format);
}
switch (popupButtonState) {
case PopupButtonState.Up:
gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.White), 0, 0, ClientSize.Width - 1, 0);
gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.White), 0, 0, 0, ClientSize.Height - 1);
gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.Black), ClientSize.Width - 1, 0, ClientSize.Width - 1, ClientSize.Height - 1);
gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.Black), 0, ClientSize.Height - 1, ClientSize.Width - 1, ClientSize.Height - 1);
break;
case PopupButtonState.Down:
gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.Black), 0, 0, ClientSize.Width - 1, 0);
gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.Black), 0, 0, 0, ClientSize.Height - 1);
gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.White), ClientSize.Width - 1, 0, ClientSize.Width - 1, ClientSize.Height - 1);
gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.White), 0, ClientSize.Height - 1, ClientSize.Width - 1, ClientSize.Height - 1);
break;
}
}
protected override void OnMouseEnter (EventArgs e)
{
if (popupButtonState != PopupButtonState.Down)
popupButtonState = PopupButtonState.Up;
PopupButtonPanel panel = Parent as PopupButtonPanel;
if (panel.focusButton != null && panel.focusButton.ButtonState == PopupButtonState.Up) {
panel.focusButton.ButtonState = PopupButtonState.Normal;
panel.SetFocusButton (null);
}
Invalidate ();
base.OnMouseEnter (e);
}
protected override void OnMouseLeave (EventArgs e)
{
if (popupButtonState == PopupButtonState.Up)
popupButtonState = PopupButtonState.Normal;
Invalidate ();
base.OnMouseLeave (e);
}
protected override void OnClick (EventArgs e)
{
popupButtonState = PopupButtonState.Down;
Invalidate ();
base.OnClick (e);
}
}
#endregion
private PopupButton recentlyusedButton;
private PopupButton desktopButton;
private PopupButton personalButton;
private PopupButton mycomputerButton;
private PopupButton networkButton;
private PopupButton lastPopupButton = null;
private PopupButton focusButton = null;
private string currentPath;
private int currentFocusIndex;
public PopupButtonPanel ()
{
SuspendLayout ();
BackColor = Color.FromArgb (128, 128, 128);
Size = new Size (85, 336);
InternalBorderStyle = BorderStyle.Fixed3D;
recentlyusedButton = new PopupButton ();
desktopButton = new PopupButton ();
personalButton = new PopupButton ();
mycomputerButton = new PopupButton ();
networkButton = new PopupButton ();
recentlyusedButton.Size = new Size (81, 64);
recentlyusedButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesRecentDocuments, 32);
recentlyusedButton.BackColor = BackColor;
recentlyusedButton.ForeColor = Color.Black;
recentlyusedButton.Location = new Point (2, 2);
recentlyusedButton.Text = "Recently\nused";
recentlyusedButton.Click += new EventHandler (OnClickButton);
desktopButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesDesktop, 32);
desktopButton.BackColor = BackColor;
desktopButton.ForeColor = Color.Black;
desktopButton.Size = new Size (81, 64);
desktopButton.Location = new Point (2, 66);
desktopButton.Text = "Desktop";
desktopButton.Click += new EventHandler (OnClickButton);
personalButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesPersonal, 32);
personalButton.BackColor = BackColor;
personalButton.ForeColor = Color.Black;
personalButton.Size = new Size (81, 64);
personalButton.Location = new Point (2, 130);
personalButton.Text = "Personal";
personalButton.Click += new EventHandler (OnClickButton);
mycomputerButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesMyComputer, 32);
mycomputerButton.BackColor = BackColor;
mycomputerButton.ForeColor = Color.Black;
mycomputerButton.Size = new Size (81, 64);
mycomputerButton.Location = new Point (2, 194);
mycomputerButton.Text = "My Computer";
mycomputerButton.Click += new EventHandler (OnClickButton);
networkButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesMyNetwork, 32);
networkButton.BackColor = BackColor;
networkButton.ForeColor = Color.Black;
networkButton.Size = new Size (81, 64);
networkButton.Location = new Point (2, 258);
networkButton.Text = "My Network";
networkButton.Click += new EventHandler (OnClickButton);
Controls.Add (recentlyusedButton);
Controls.Add (desktopButton);
Controls.Add (personalButton);
Controls.Add (mycomputerButton);
Controls.Add (networkButton);
ResumeLayout (false);
KeyDown += new KeyEventHandler (Key_Down);
SetStyle (ControlStyles.StandardClick, false);
}
void OnClickButton (object sender, EventArgs e)
{
if (lastPopupButton != null && lastPopupButton != sender as PopupButton)
lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
lastPopupButton = sender as PopupButton;
if (sender == recentlyusedButton) {
currentPath = MWFVFS.RecentlyUsedPrefix;
} else
if (sender == desktopButton) {
currentPath = MWFVFS.DesktopPrefix;
} else
if (sender == personalButton) {
currentPath = MWFVFS.PersonalPrefix;
} else
if (sender == mycomputerButton) {
currentPath = MWFVFS.MyComputerPrefix;
} else
if (sender == networkButton) {
currentPath = MWFVFS.MyNetworkPrefix;
}
EventHandler eh = (EventHandler)(Events [PDirectoryChangedEvent]);
if (eh != null)
eh (this, EventArgs.Empty);
}
static object UIAFocusedItemChangedEvent = new object ();
internal event EventHandler UIAFocusedItemChanged {
add { Events.AddHandler (UIAFocusedItemChangedEvent, value); }
remove { Events.RemoveHandler (UIAFocusedItemChangedEvent, value); }
}
internal void OnUIAFocusedItemChanged ()
{
EventHandler eh = (EventHandler) Events [UIAFocusedItemChangedEvent];
if (eh != null)
eh (this, EventArgs.Empty);
}
internal PopupButton UIAFocusButton {
get {
return focusButton;
}
}
public string CurrentFolder {
set {
string currentPath = value;
if (currentPath == MWFVFS.RecentlyUsedPrefix) {
if (lastPopupButton != recentlyusedButton) {
if (lastPopupButton != null)
lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
recentlyusedButton.ButtonState = PopupButton.PopupButtonState.Down;
lastPopupButton = recentlyusedButton;
}
} else
if (currentPath == MWFVFS.DesktopPrefix) {
if (lastPopupButton != desktopButton) {
if (lastPopupButton != null)
lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
desktopButton.ButtonState = PopupButton.PopupButtonState.Down;
lastPopupButton = desktopButton;
}
} else
if (currentPath == MWFVFS.PersonalPrefix) {
if (lastPopupButton != personalButton) {
if (lastPopupButton != null)
lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
personalButton.ButtonState = PopupButton.PopupButtonState.Down;
lastPopupButton = personalButton;
}
} else
if (currentPath == MWFVFS.MyComputerPrefix) {
if (lastPopupButton != mycomputerButton) {
if (lastPopupButton != null)
lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
mycomputerButton.ButtonState = PopupButton.PopupButtonState.Down;
lastPopupButton = mycomputerButton;
}
} else
if (currentPath == MWFVFS.MyNetworkPrefix) {
if (lastPopupButton != networkButton) {
if (lastPopupButton != null)
lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
networkButton.ButtonState = PopupButton.PopupButtonState.Down;
lastPopupButton = networkButton;
}
} else {
if (lastPopupButton != null) {
lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
lastPopupButton = null;
}
}
}
get {
return currentPath;
}
}
protected override void OnGotFocus (EventArgs e)
{
if (lastPopupButton != recentlyusedButton) {
recentlyusedButton.ButtonState = PopupButton.PopupButtonState.Up;
SetFocusButton (recentlyusedButton);
}
currentFocusIndex = 0;
base.OnGotFocus (e);
}
protected override void OnLostFocus (EventArgs e)
{
if (focusButton != null && focusButton.ButtonState != PopupButton.PopupButtonState.Down)
focusButton.ButtonState = PopupButton.PopupButtonState.Normal;
base.OnLostFocus (e);
}
protected override bool IsInputKey (Keys key)
{
switch (key) {
case Keys.Up:
case Keys.Down:
case Keys.Right:
case Keys.Left:
case Keys.Enter:
return true;
}
return base.IsInputKey (key);
}
private void Key_Down (object sender, KeyEventArgs e)
{
bool update_focus = false;
if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Up) {
currentFocusIndex --;
if (currentFocusIndex < 0)
currentFocusIndex = Controls.Count - 1;
update_focus = true;
} else
if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Right) {
currentFocusIndex++;
if (currentFocusIndex == Controls.Count)
currentFocusIndex = 0;
update_focus = true;
} else
if (e.KeyCode == Keys.Enter) {
focusButton.ButtonState = PopupButton.PopupButtonState.Down;
OnClickButton (focusButton, EventArgs.Empty);
}
if (update_focus) {
PopupButton newfocusButton = Controls [currentFocusIndex] as PopupButton;
if (focusButton != null && focusButton.ButtonState != PopupButton.PopupButtonState.Down)
focusButton.ButtonState = PopupButton.PopupButtonState.Normal;
if (newfocusButton.ButtonState != PopupButton.PopupButtonState.Down)
newfocusButton.ButtonState = PopupButton.PopupButtonState.Up;
SetFocusButton (newfocusButton);
}
e.Handled = true;
}
static object PDirectoryChangedEvent = new object ();
public event EventHandler DirectoryChanged {
add { Events.AddHandler (PDirectoryChangedEvent, value); }
remove { Events.RemoveHandler (PDirectoryChangedEvent, value); }
}
internal void SetFocusButton (PopupButton button)
{
if (button == focusButton)
return;
focusButton = button;
OnUIAFocusedItemChanged ();
}
}
#endregion
#region DirComboBox
internal class DirComboBox : ComboBox, IUpdateFolder
{
#region DirComboBoxItem
internal class DirComboBoxItem
{
private int imageIndex;
private string name;
private string path;
private int xPos;
private ImageList imageList;
public DirComboBoxItem (ImageList imageList, int imageIndex, string name, string path, int xPos)
{
this.imageList = imageList;
this.imageIndex = imageIndex;
this.name = name;
this.path = path;
this.xPos = xPos;
}
public int ImageIndex {
get {
return imageIndex;
}
}
public string Name {
get {
return name;
}
}
public string Path {
get {
return path;
}
}
public int XPos {
get {
return xPos;
}
}
public ImageList ImageList {
set {
imageList = value;
}
get {
return imageList;
}
}
#region UIA Framework Members
public override string ToString ()
{
return name;
}
#endregion
}
#endregion
private ImageList imageList = new ImageList();
private string currentPath;
private Stack folderStack = new Stack();
private static readonly int indent = 6;
private DirComboBoxItem recentlyUsedDirComboboxItem;
private DirComboBoxItem desktopDirComboboxItem;
private DirComboBoxItem personalDirComboboxItem;
private DirComboBoxItem myComputerDirComboboxItem;
private DirComboBoxItem networkDirComboboxItem;
private ArrayList myComputerItems = new ArrayList ();
private DirComboBoxItem mainParentDirComboBoxItem = null;
private DirComboBoxItem real_parent = null;
private MWFVFS vfs;
public DirComboBox (MWFVFS vfs)
{
this.vfs = vfs;
SuspendLayout ();
DrawMode = DrawMode.OwnerDrawFixed;
imageList.ColorDepth = ColorDepth.Depth32Bit;
imageList.ImageSize = new Size (16, 16);
imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesRecentDocuments, 16));
imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesDesktop, 16));
imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesPersonal, 16));
imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesMyComputer, 16));
imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesMyNetwork, 16));
imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.NormalFolder, 16));
imageList.TransparentColor = Color.Transparent;
recentlyUsedDirComboboxItem = new DirComboBoxItem (imageList, 0, "Recently used", MWFVFS.RecentlyUsedPrefix, 0);
desktopDirComboboxItem = new DirComboBoxItem (imageList, 1, "Desktop", MWFVFS.DesktopPrefix, 0);
personalDirComboboxItem = new DirComboBoxItem (imageList, 2, "Personal folder", MWFVFS.PersonalPrefix, indent);
myComputerDirComboboxItem = new DirComboBoxItem (imageList, 3, "My Computer", MWFVFS.MyComputerPrefix, indent);
networkDirComboboxItem = new DirComboBoxItem (imageList, 4, "My Network", MWFVFS.MyNetworkPrefix, indent);
ArrayList al = this.vfs.GetMyComputerContent ();
foreach (FSEntry fsEntry in al) {
myComputerItems.Add (new DirComboBoxItem (MimeIconEngine.LargeIcons, fsEntry.IconIndex, fsEntry.Name, fsEntry.FullName, indent * 2));
}
al.Clear ();
al = null;
mainParentDirComboBoxItem = myComputerDirComboboxItem;
ResumeLayout (false);
}
public string CurrentFolder {
set {
currentPath = value;
CreateComboList ();
}
get {
return currentPath;
}
}
private void CreateComboList ()
{
real_parent = null;
DirComboBoxItem selection = null;
if (currentPath == MWFVFS.RecentlyUsedPrefix) {
mainParentDirComboBoxItem = recentlyUsedDirComboboxItem;
selection = recentlyUsedDirComboboxItem;
} else if (currentPath == MWFVFS.DesktopPrefix) {
selection = desktopDirComboboxItem;
mainParentDirComboBoxItem = desktopDirComboboxItem;
} else if (currentPath == MWFVFS.PersonalPrefix) {
selection = personalDirComboboxItem;
mainParentDirComboBoxItem = personalDirComboboxItem;
} else if (currentPath == MWFVFS.MyComputerPrefix) {
selection = myComputerDirComboboxItem;
mainParentDirComboBoxItem = myComputerDirComboboxItem;
} else if (currentPath == MWFVFS.MyNetworkPrefix) {
selection = networkDirComboboxItem;
mainParentDirComboBoxItem = networkDirComboboxItem;
} else {
foreach (DirComboBoxItem dci in myComputerItems) {
if (dci.Path == currentPath) {
mainParentDirComboBoxItem = selection = dci;
break;
}
}
}
BeginUpdate ();
Items.Clear ();
Items.Add (recentlyUsedDirComboboxItem);
Items.Add (desktopDirComboboxItem);
Items.Add (personalDirComboboxItem);
Items.Add (myComputerDirComboboxItem);
Items.AddRange (myComputerItems);
Items.Add (networkDirComboboxItem);
if (selection == null)
real_parent = CreateFolderStack ();
if (real_parent != null) {
int local_indent = 0;
if (real_parent == desktopDirComboboxItem)
local_indent = 1;
else
if (real_parent == personalDirComboboxItem || real_parent == networkDirComboboxItem)
local_indent = 2;
else
local_indent = 3;
selection = AppendToParent (local_indent, real_parent);
}
EndUpdate ();
if (selection != null)
SelectedItem = selection;
}
private DirComboBoxItem CreateFolderStack ()
{
folderStack.Clear ();
DirectoryInfo di = new DirectoryInfo (currentPath);
folderStack.Push (di);
bool ignoreCase = !XplatUI.RunningOnUnix;
while (di.Parent != null) {
di = di.Parent;
if (mainParentDirComboBoxItem != personalDirComboboxItem && string.Compare (di.FullName, ThemeEngine.Current.Places (UIIcon.PlacesDesktop), ignoreCase) == 0)
return desktopDirComboboxItem;
else
if (mainParentDirComboBoxItem == personalDirComboboxItem) {
if (string.Compare (di.FullName, ThemeEngine.Current.Places (UIIcon.PlacesPersonal), ignoreCase) == 0)
return personalDirComboboxItem;
} else
foreach (DirComboBoxItem dci in myComputerItems) {
if (string.Compare (dci.Path, di.FullName, ignoreCase) == 0) {
return dci;
}
}
folderStack.Push (di);
}
return null;
}
private DirComboBoxItem AppendToParent (int nr_indents, DirComboBoxItem parentDirComboBoxItem)
{
DirComboBoxItem selection = null;
int index = Items.IndexOf (parentDirComboBoxItem) + 1;
int xPos = indent * nr_indents;
while (folderStack.Count != 0) {
DirectoryInfo dii = folderStack.Pop () as DirectoryInfo;
DirComboBoxItem dci = new DirComboBoxItem (imageList, 5, dii.Name, dii.FullName, xPos);
Items.Insert (index, dci);
index++;
selection = dci;
xPos += indent;
}
return selection;
}
protected override void OnDrawItem (DrawItemEventArgs e)
{
if (e.Index == -1)
return;
DirComboBoxItem dcbi = Items [e.Index] as DirComboBoxItem;
Bitmap bmp = new Bitmap (e.Bounds.Width, e.Bounds.Height, e.Graphics);
Graphics gr = Graphics.FromImage (bmp);
Color backColor = e.BackColor;
Color foreColor = e.ForeColor;
int xPos = dcbi.XPos;
if ((e.State & DrawItemState.ComboBoxEdit) != 0)
xPos = 0;
gr.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (backColor),
new Rectangle (0, 0, bmp.Width, bmp.Height));
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected &&
(!DroppedDown || (e.State & DrawItemState.ComboBoxEdit) != DrawItemState.ComboBoxEdit)) {
foreColor = ThemeEngine.Current.ColorHighlightText;
int w = (int) gr.MeasureString (dcbi.Name, e.Font).Width;
gr.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (ThemeEngine.Current.ColorHighlight),
new Rectangle (xPos + 23, 1, w + 3, e.Bounds.Height - 2));
if ((e.State & DrawItemState.Focus) == DrawItemState.Focus) {
ControlPaint.DrawFocusRectangle (gr, new Rectangle (xPos + 22, 0, w + 5,
e.Bounds.Height), foreColor, ThemeEngine.Current.ColorHighlight);
}
}
gr.DrawString (dcbi.Name, e.Font , ThemeEngine.Current.ResPool.GetSolidBrush (foreColor), new Point (24 + xPos, (bmp.Height - e.Font.Height) / 2));
gr.DrawImage (dcbi.ImageList.Images [dcbi.ImageIndex], new Rectangle (new Point (xPos + 2, 0), new Size (16, 16)));
e.Graphics.DrawImage (bmp, e.Bounds.X, e.Bounds.Y);
gr.Dispose ();
bmp.Dispose ();
}
protected override void OnSelectedIndexChanged (EventArgs e)
{
if (Items.Count > 0) {
DirComboBoxItem dcbi = Items [SelectedIndex] as DirComboBoxItem;
currentPath = dcbi.Path;
}
}
protected override void OnSelectionChangeCommitted (EventArgs e)
{
EventHandler eh = (EventHandler)(Events [CDirectoryChangedEvent]);
if (eh != null)
eh (this, EventArgs.Empty);
}
static object CDirectoryChangedEvent = new object ();
public event EventHandler DirectoryChanged {
add { Events.AddHandler (CDirectoryChangedEvent, value); }
remove { Events.RemoveHandler (CDirectoryChangedEvent, value); }
}
}
#endregion
#region FilterStruct
internal struct FilterStruct
{
public string filterName;
public StringCollection filters;
public FilterStruct (string filterName, string filter)
{
this.filterName = filterName;
filters = new StringCollection ();
SplitFilters (filter);
}
private void SplitFilters (string filter)
{
string[] split = filter.Split (new char [] {';'});
foreach (string s in split) {
filters.Add (s.Trim ());
}
}
}
#endregion
#region FileFilter
internal class FileFilter
{
private ArrayList filterArrayList = new ArrayList();
private string filter;
public FileFilter ()
{}
public FileFilter (string filter) : base ()
{
this.filter = filter;
SplitFilter ();
}
public static bool CheckFilter (string val)
{
if (val.Length == 0)
return true;
string[] filters = val.Split (new char [] {'|'});
if ((filters.Length % 2) != 0)
return false;
return true;
}
public ArrayList FilterArrayList {
set {
filterArrayList = value;
}
get {
return filterArrayList;
}
}
public string Filter {
set {
filter = value;
SplitFilter ();
}
get {
return filter;
}
}
private void SplitFilter ()
{
filterArrayList.Clear ();
if (filter.Length == 0)
return;
string[] filters = filter.Split (new char [] {'|'});
for (int i = 0; i < filters.Length; i += 2) {
FilterStruct filterStruct = new FilterStruct (filters [i], filters [i + 1]);
filterArrayList.Add (filterStruct);
}
}
}
#endregion
#region MWFFileView
internal class MWFFileView : ListView
{
private ArrayList filterArrayList;
private bool showHiddenFiles = false;
private string selectedFilesString;
private int filterIndex = 1;
private ToolTip toolTip;
private int oldItemIndexForToolTip = -1;
private ContextMenu contextMenu;
private MenuItem menuItemView;
private MenuItem menuItemNew;
private MenuItem smallIconMenutItem;
private MenuItem tilesMenutItem;
private MenuItem largeIconMenutItem;
private MenuItem listMenutItem;
private MenuItem detailsMenutItem;
private MenuItem newFolderMenuItem;
private MenuItem showHiddenFilesMenuItem;
private int previousCheckedMenuItemIndex;
private ArrayList viewMenuItemClones = new ArrayList ();
private FSEntry currentFSEntry = null;
private string currentFolder;
private string currentRealFolder;
private FSEntry currentFolderFSEntry;
// store DirectoryInfo for a back button for example
private Stack directoryStack = new Stack();
// list of controls(components to enable or disable depending on current directoryStack.Count
private ArrayList dirStackControlsOrComponents = new ArrayList ();
private ToolBarButton folderUpToolBarButton;
private ArrayList registered_senders = new ArrayList ();
private bool should_push = true;
private MWFVFS vfs;
private View old_view;
private int old_menuitem_index;
private bool do_update_view = false;
private ColumnHeader [] columns;
public MWFFileView (MWFVFS vfs)
{
this.vfs = vfs;
this.vfs.RegisterUpdateDelegate (new MWFVFS.UpdateDelegate (RealFileViewUpdate), this);
SuspendLayout ();
contextMenu = new ContextMenu ();
toolTip = new ToolTip ();
toolTip.InitialDelay = 300;
toolTip.ReshowDelay = 0;
// contextMenu
// View menu item
menuItemView = new MenuItem ("View");
smallIconMenutItem = new MenuItem ("Small Icon", new EventHandler (OnClickViewMenuSubItem));
smallIconMenutItem.RadioCheck = true;
menuItemView.MenuItems.Add (smallIconMenutItem);
tilesMenutItem = new MenuItem ("Tiles", new EventHandler (OnClickViewMenuSubItem));
tilesMenutItem.RadioCheck = true;
menuItemView.MenuItems.Add (tilesMenutItem);
largeIconMenutItem = new MenuItem ("Large Icon", new EventHandler (OnClickViewMenuSubItem));
largeIconMenutItem.RadioCheck = true;
menuItemView.MenuItems.Add (largeIconMenutItem);
listMenutItem = new MenuItem ("List", new EventHandler (OnClickViewMenuSubItem));
listMenutItem.RadioCheck = true;
listMenutItem.Checked = true;
menuItemView.MenuItems.Add (listMenutItem);
previousCheckedMenuItemIndex = listMenutItem.Index;
detailsMenutItem = new MenuItem ("Details", new EventHandler (OnClickViewMenuSubItem));
detailsMenutItem.RadioCheck = true;
menuItemView.MenuItems.Add (detailsMenutItem);
contextMenu.MenuItems.Add (menuItemView);
contextMenu.MenuItems.Add (new MenuItem ("-"));
// New menu item
menuItemNew = new MenuItem ("New");
newFolderMenuItem = new MenuItem ("New Folder", new EventHandler (OnClickNewFolderMenuItem));
menuItemNew.MenuItems.Add (newFolderMenuItem);
contextMenu.MenuItems.Add (menuItemNew);
contextMenu.MenuItems.Add (new MenuItem ("-"));
// Show hidden files menu item
showHiddenFilesMenuItem = new MenuItem ("Show hidden files", new EventHandler (OnClickContextMenu));
showHiddenFilesMenuItem.Checked = showHiddenFiles;
contextMenu.MenuItems.Add (showHiddenFilesMenuItem);
LabelWrap = true;
SmallImageList = MimeIconEngine.SmallIcons;
LargeImageList = MimeIconEngine.LargeIcons;
View = old_view = View.List;
LabelEdit = true;
ContextMenu = contextMenu;
// Create columns, but only add them when view changes to Details
columns = new ColumnHeader [4];
columns [0] = CreateColumnHeader (" Name", 170, HorizontalAlignment.Left);
columns [1] = CreateColumnHeader ("Size ", 80, HorizontalAlignment.Right);
columns [2] = CreateColumnHeader (" Type", 100, HorizontalAlignment.Left);
columns [3] = CreateColumnHeader (" Last Access", 150, HorizontalAlignment.Left);
AllowColumnReorder = true;
ResumeLayout (false);
KeyDown += new KeyEventHandler (MWF_KeyDown);
}
ColumnHeader CreateColumnHeader (string text, int width, HorizontalAlignment alignment)
{
ColumnHeader col = new ColumnHeader ();
col.Text = text;
col.Width = width;
col.TextAlign = alignment;
return col;
}
public string CurrentFolder {
get {
return currentFolder;
}
set {
currentFolder = value;
}
}
public string CurrentRealFolder {
get {
return currentRealFolder;
}
}
public FSEntry CurrentFSEntry {
get {
return currentFSEntry;
}
}
public MenuItem[] ViewMenuItems {
get {
MenuItem[] menuItemClones = new MenuItem [] {
smallIconMenutItem.CloneMenu (),
tilesMenutItem.CloneMenu (),
largeIconMenutItem.CloneMenu (),
listMenutItem.CloneMenu (),
detailsMenutItem.CloneMenu ()
};
viewMenuItemClones.Add (menuItemClones);
return menuItemClones;
}
}
public ArrayList FilterArrayList {
set {
filterArrayList = value;
}
get {
return filterArrayList;
}
}
public bool ShowHiddenFiles {
set {
showHiddenFiles = value;
}
get {
return showHiddenFiles;
}
}
public int FilterIndex {
set {
filterIndex = value;
if (Visible)
UpdateFileView ();
}
get {
return filterIndex;
}
}
public string SelectedFilesString {
set {
selectedFilesString = value;
}
get {
return selectedFilesString;
}
}
public void PushDir ()
{
if (currentFolder != null)
directoryStack.Push (currentFolder);
EnableOrDisableDirstackObjects ();
}
public void PopDir ()
{
PopDir (null);
}
public void PopDir (string filter)
{
if (directoryStack.Count == 0)
return;
string new_folder = directoryStack.Pop () as string;
EnableOrDisableDirstackObjects ();
should_push = false;
ChangeDirectory (null, new_folder, filter);
}
public void RegisterSender (IUpdateFolder iud)
{
registered_senders.Add (iud);
}
public void CreateNewFolder ()
{
if (currentFolder == MWFVFS.MyComputerPrefix ||
currentFolder == MWFVFS.RecentlyUsedPrefix)
return;
FSEntry fsEntry = new FSEntry ();
fsEntry.Attributes = FileAttributes.Directory;
fsEntry.FileType = FSEntry.FSEntryType.Directory;
fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("inode/directory");
fsEntry.LastAccessTime = DateTime.Now;
// FIXME: when ListView.LabelEdit is available use it
// listViewItem.BeginEdit();
TextEntryDialog ted = new TextEntryDialog ();
ted.IconPictureBoxImage = MimeIconEngine.LargeIcons.Images.GetImage (fsEntry.IconIndex);
string folder = String.Empty;
if (currentFolderFSEntry.RealName != null)
folder = currentFolderFSEntry.RealName;
else
folder = currentFolder;
string tmp_filename = "New Folder";
if (Directory.Exists (Path.Combine (folder, tmp_filename))) {
int i = 1;
if (XplatUI.RunningOnUnix) {
tmp_filename = tmp_filename + "-" + i;
} else {
tmp_filename = tmp_filename + " (" + i + ")";
}
while (Directory.Exists (Path.Combine (folder, tmp_filename))) {
i++;
if (XplatUI.RunningOnUnix) {
tmp_filename = "New Folder" + "-" + i;
} else {
tmp_filename = "New Folder" + " (" + i + ")";
}
}
}
ted.FileName = tmp_filename;
if (ted.ShowDialog () == DialogResult.OK) {
string new_folder = Path.Combine (folder, ted.FileName);
if (vfs.CreateFolder (new_folder)) {
fsEntry.FullName = new_folder;
fsEntry.Name = ted.FileName;
FileViewListViewItem listViewItem = new FileViewListViewItem (fsEntry);
BeginUpdate ();
Items.Add (listViewItem);
EndUpdate ();
listViewItem.EnsureVisible ();
}
}
}
public void SetSelectedIndexTo (string fname)
{
foreach (FileViewListViewItem item in Items) {
if (item.Text == fname) {
BeginUpdate ();
SelectedItems.Clear ();
item.Selected = true;
EndUpdate ();
break;
}
}
}
public void OneDirUp ()
{
OneDirUp (null);
}
public void OneDirUp (string filter)
{
string parent_folder = vfs.GetParent ();
if (parent_folder != null)
ChangeDirectory (null, parent_folder, filter);
}
public void ChangeDirectory (object sender, string folder)
{
ChangeDirectory (sender, folder, null);
}
public void ChangeDirectory (object sender, string folder, string filter)
{
if (folder == MWFVFS.DesktopPrefix || folder == MWFVFS.RecentlyUsedPrefix)
folderUpToolBarButton.Enabled = false;
else
folderUpToolBarButton.Enabled = true;
foreach (IUpdateFolder iuf in registered_senders) {
iuf.CurrentFolder = folder;
}
if (should_push)
PushDir ();
else
should_push = true;
currentFolderFSEntry = vfs.ChangeDirectory (folder);
currentFolder = folder;
if (currentFolder.IndexOf ("://") != -1)
currentRealFolder = currentFolderFSEntry.RealName;
else
currentRealFolder = currentFolder;
BeginUpdate ();
Items.Clear ();
SelectedItems.Clear ();
if (folder == MWFVFS.RecentlyUsedPrefix) {
old_view = View;
View = View.Details;
old_menuitem_index = previousCheckedMenuItemIndex;
UpdateMenuItems (detailsMenutItem);
do_update_view = true;
} else
if (View != old_view && do_update_view) {
UpdateMenuItems (menuItemView.MenuItems [old_menuitem_index]);
View = old_view;
do_update_view = false;
}
EndUpdate ();
try {
UpdateFileView (filter);
} catch (Exception e) {
if (should_push)
PopDir ();
MessageBox.Show (e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public void UpdateFileView ()
{
UpdateFileView (null);
}
public void UpdateFileView (string custom_filter)
{
if (custom_filter != null) {
StringCollection custom_filters = new StringCollection ();
custom_filters.Add (custom_filter);
vfs.GetFolderContent (custom_filters);
} else if (filterArrayList != null && filterArrayList.Count != 0) {
FilterStruct fs = (FilterStruct)filterArrayList [filterIndex - 1];
vfs.GetFolderContent (fs.filters);
} else
vfs.GetFolderContent ();
}
public void RealFileViewUpdate (ArrayList directoriesArrayList, ArrayList fileArrayList)
{
BeginUpdate ();
Items.Clear ();
SelectedItems.Clear ();
foreach (FSEntry directoryFSEntry in directoriesArrayList) {
if (!ShowHiddenFiles)
if (directoryFSEntry.Name.StartsWith (".") || (directoryFSEntry.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
continue;
FileViewListViewItem listViewItem = new FileViewListViewItem (directoryFSEntry);
Items.Add (listViewItem);
}
StringCollection collection = new StringCollection ();
foreach (FSEntry fsEntry in fileArrayList) {
// remove duplicates. that can happen when you read recently used files for example
if (collection.Contains (fsEntry.Name)) {
string fileName = fsEntry.Name;
if (collection.Contains (fileName)) {
int i = 1;
while (collection.Contains (fileName + "(" + i + ")")) {
i++;
}
fileName = fileName + "(" + i + ")";
}
fsEntry.Name = fileName;
}
collection.Add (fsEntry.Name);
DoOneFSEntry (fsEntry);
}
EndUpdate ();
collection.Clear ();
collection = null;
directoriesArrayList.Clear ();
fileArrayList.Clear ();
}
public void AddControlToEnableDisableByDirStack (object control)
{
dirStackControlsOrComponents.Add (control);
}
public void SetFolderUpToolBarButton (ToolBarButton tb)
{
folderUpToolBarButton = tb;
}
public void WriteRecentlyUsed (string fullfilename)
{
vfs.WriteRecentlyUsedFiles (fullfilename);
}
private void EnableOrDisableDirstackObjects ()
{
foreach (object o in dirStackControlsOrComponents) {
if (o is Control) {
Control c = o as Control;
c.Enabled = (directoryStack.Count > 1);
} else
if (o is ToolBarButton) {
ToolBarButton t = o as ToolBarButton;
t.Enabled = (directoryStack.Count > 0);
}
}
}
private void DoOneFSEntry (FSEntry fsEntry)
{
if (!ShowHiddenFiles)
if (fsEntry.Name.StartsWith (".") || (fsEntry.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
return;
FileViewListViewItem listViewItem = new FileViewListViewItem (fsEntry);
Items.Add (listViewItem);
}
private void MWF_KeyDown (object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Back) {
OneDirUp ();
} else if (e.Control && e.KeyCode == Keys.A && MultiSelect) {
foreach (ListViewItem lvi in Items)
lvi.Selected = true;
}
}
#region UIA Framework Members
internal void PerformClick ()
{
OnClick (EventArgs.Empty);
}
internal void PerformDoubleClick ()
{
OnDoubleClick (EventArgs.Empty);
}
#endregion
protected override void OnClick (EventArgs e)
{
if (!MultiSelect) {
if (SelectedItems.Count > 0) {
FileViewListViewItem listViewItem = SelectedItems [0] as FileViewListViewItem;
FSEntry fsEntry = listViewItem.FSEntry;
if (fsEntry.FileType == FSEntry.FSEntryType.File) {
currentFSEntry = fsEntry;
EventHandler eh = (EventHandler)(Events [MSelectedFileChangedEvent]);
if (eh != null)
eh (this, EventArgs.Empty);
}
}
}
base.OnClick (e);
}
protected override void OnDoubleClick (EventArgs e)
{
if (SelectedItems.Count > 0) {
FileViewListViewItem listViewItem = SelectedItems [0] as FileViewListViewItem;
FSEntry fsEntry = listViewItem.FSEntry;
if ((fsEntry.Attributes & FileAttributes.Directory) == FileAttributes.Directory) {
ChangeDirectory (null, fsEntry.FullName);
EventHandler eh = (EventHandler)(Events [MDirectoryChangedEvent]);
if (eh != null)
eh (this, EventArgs.Empty);
} else {
currentFSEntry = fsEntry;
EventHandler eh = (EventHandler)(Events [MSelectedFileChangedEvent]);
if (eh != null)
eh (this, EventArgs.Empty);
eh = (EventHandler)(Events [MForceDialogEndEvent]);
if (eh != null)
eh (this, EventArgs.Empty);
return;
}
}
base.OnDoubleClick (e);
}
protected override void OnSelectedIndexChanged (EventArgs e)
{
if (SelectedItems.Count > 0) {
selectedFilesString = String.Empty;
if (SelectedItems.Count == 1) {
FileViewListViewItem listViewItem = SelectedItems [0] as FileViewListViewItem;
FSEntry fsEntry = listViewItem.FSEntry;
if ((fsEntry.Attributes & FileAttributes.Directory) != FileAttributes.Directory)
selectedFilesString = SelectedItems [0].Text;
} else {
foreach (FileViewListViewItem lvi in SelectedItems) {
FSEntry fsEntry = lvi.FSEntry;
if ((fsEntry.Attributes & FileAttributes.Directory) != FileAttributes.Directory)
selectedFilesString = selectedFilesString + "\"" + lvi.Text + "\" ";
}
}
EventHandler eh = (EventHandler)(Events [MSelectedFilesChangedEvent]);
if (eh != null)
eh (this, EventArgs.Empty);
}
base.OnSelectedIndexChanged (e);
}
protected override void OnMouseMove (MouseEventArgs e)
{
FileViewListViewItem item = GetItemAt (e.X, e.Y) as FileViewListViewItem;
if (item != null) {
int currentItemIndex = item.Index;
if (currentItemIndex != oldItemIndexForToolTip) {
oldItemIndexForToolTip = currentItemIndex;
if (toolTip != null && toolTip.Active)
toolTip.Active = false;
FSEntry fsEntry = item.FSEntry;
string output = String.Empty;
if (fsEntry.FileType == FSEntry.FSEntryType.Directory)
output = "Directory: " + fsEntry.FullName;
else if (fsEntry.FileType == FSEntry.FSEntryType.Device)
output = "Device: "+ fsEntry.FullName;
else if (fsEntry.FileType == FSEntry.FSEntryType.Network)
output = "Network: " + fsEntry.FullName;
else
output = "File: " + fsEntry.FullName;
toolTip.SetToolTip (this, output);
toolTip.Active = true;
}
} else
toolTip.Active = false;
base.OnMouseMove (e);
}
void OnClickContextMenu (object sender, EventArgs e)
{
MenuItem senderMenuItem = sender as MenuItem;
if (senderMenuItem == showHiddenFilesMenuItem) {
senderMenuItem.Checked = !senderMenuItem.Checked;
showHiddenFiles = senderMenuItem.Checked;
UpdateFileView ();
}
}
void OnClickViewMenuSubItem (object sender, EventArgs e)
{
MenuItem senderMenuItem = (MenuItem)sender;
UpdateMenuItems (senderMenuItem);
// update me - call BeginUpdate/EndUpdate to avoid flicker when columns change
BeginUpdate ();
switch (senderMenuItem.Index) {
case 0:
View = View.SmallIcon;
break;
case 1:
View = View.Tile;
break;
case 2:
View = View.LargeIcon;
break;
case 3:
View = View.List;
break;
case 4:
View = View.Details;
break;
default:
break;
}
if (View == View.Details)
Columns.AddRange (columns);
else {
ListViewItemSorter = null;
Columns.Clear ();
}
EndUpdate ();
}
protected override void OnBeforeLabelEdit (LabelEditEventArgs e)
{
FileViewListViewItem listViewItem = SelectedItems [0] as FileViewListViewItem;
FSEntry fsEntry = listViewItem.FSEntry;
// only allow editing of files or directories
if (fsEntry.FileType != FSEntry.FSEntryType.Directory &&
fsEntry.FileType != FSEntry.FSEntryType.File)
e.CancelEdit = true;
base.OnBeforeLabelEdit (e);
}
protected override void OnAfterLabelEdit (LabelEditEventArgs e)
{
base.OnAfterLabelEdit (e);
// no changes were made
if (e.Label == null || Items [e.Item].Text == e.Label)
return;
FileViewListViewItem listViewItem = SelectedItems [0] as FileViewListViewItem;
FSEntry fsEntry = listViewItem.FSEntry;
string folder = (currentFolderFSEntry.RealName != null) ?
currentFolderFSEntry.RealName : currentFolder;
switch (fsEntry.FileType) {
case FSEntry.FSEntryType.Directory:
string sourceDir = (fsEntry.RealName != null) ? fsEntry.RealName : fsEntry.FullName;
string destDir = Path.Combine (folder, e.Label);
if (!vfs.MoveFolder (sourceDir, destDir)) {
e.CancelEdit = true;
} else {
if (fsEntry.RealName != null)
fsEntry.RealName = destDir;
else
fsEntry.FullName = destDir;
}
break;
case FSEntry.FSEntryType.File:
string sourceFile = (fsEntry.RealName != null) ? fsEntry.RealName : fsEntry.FullName;
string destFile = Path.Combine (folder, e.Label);
if (!vfs.MoveFile (sourceFile, destFile)) {
e.CancelEdit = true;
} else {
if (fsEntry.RealName != null)
fsEntry.RealName = destFile;
else
fsEntry.FullName = destFile;
}
break;
}
}
private void UpdateMenuItems (MenuItem senderMenuItem)
{
menuItemView.MenuItems [previousCheckedMenuItemIndex].Checked = false;
menuItemView.MenuItems [senderMenuItem.Index].Checked = true;
foreach (MenuItem[] items in viewMenuItemClones) {
items [previousCheckedMenuItemIndex].Checked = false;
items [senderMenuItem.Index].Checked = true;
}
previousCheckedMenuItemIndex = senderMenuItem.Index;
}
void OnClickNewFolderMenuItem (object sender, EventArgs e)
{
CreateNewFolder ();
}
static object MSelectedFileChangedEvent = new object ();
static object MSelectedFilesChangedEvent = new object ();
static object MDirectoryChangedEvent = new object ();
static object MForceDialogEndEvent = new object ();
public event EventHandler SelectedFileChanged {
add { Events.AddHandler (MSelectedFileChangedEvent, value); }
remove { Events.RemoveHandler (MSelectedFileChangedEvent, value); }
}
public event EventHandler SelectedFilesChanged {
add { Events.AddHandler (MSelectedFilesChangedEvent, value); }
remove { Events.RemoveHandler (MSelectedFilesChangedEvent, value); }
}
public event EventHandler DirectoryChanged {
add { Events.AddHandler (MDirectoryChangedEvent, value); }
remove { Events.RemoveHandler (MDirectoryChangedEvent, value); }
}
public event EventHandler ForceDialogEnd {
add { Events.AddHandler (MForceDialogEndEvent, value); }
remove { Events.RemoveHandler (MForceDialogEndEvent, value); }
}
}
#endregion
#region FileListViewItem
internal class FileViewListViewItem : ListViewItem
{
private FSEntry fsEntry;
public FileViewListViewItem (FSEntry fsEntry)
{
this.fsEntry = fsEntry;
ImageIndex = fsEntry.IconIndex;
Text = fsEntry.Name;
switch (fsEntry.FileType) {
case FSEntry.FSEntryType.Directory:
SubItems.Add (String.Empty);
SubItems.Add ("Directory");
SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ());
break;
case FSEntry.FSEntryType.File:
long fileLen = 1;
try {
if (fsEntry.FileSize > 1024)
fileLen = fsEntry.FileSize / 1024;
} catch (Exception) {
fileLen = 1;
}
SubItems.Add (fileLen.ToString () + " KB");
SubItems.Add ("File");
SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ());
break;
case FSEntry.FSEntryType.Device:
SubItems.Add (String.Empty);
SubItems.Add ("Device");
SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ());
break;
case FSEntry.FSEntryType.RemovableDevice:
SubItems.Add (String.Empty);
SubItems.Add ("RemovableDevice");
SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ());
break;
default:
break;
}
}
public FSEntry FSEntry {
set {
fsEntry = value;
}
get {
return fsEntry;
}
}
}
#endregion
#region MwfFileViewItemComparer
class MwfFileViewItemComparer : IComparer
{
int column_index;
bool asc;
public MwfFileViewItemComparer (bool asc)
{
this.asc = asc;
}
public int ColumnIndex {
get {
return column_index;
}
set {
column_index = value;
}
}
public bool Ascendent {
get {
return asc;
}
set {
asc = value;
}
}
public int Compare (object a, object b)
{
ListViewItem item_a = (ListViewItem)a;
ListViewItem item_b = (ListViewItem)b;
int retval;
if (asc)
retval = String.Compare (item_a.SubItems [column_index].Text,
item_b.SubItems [column_index].Text);
else
retval = String.Compare (item_b.SubItems [column_index].Text,
item_a.SubItems [column_index].Text);
return retval;
}
}
#endregion
#region IUpdateFolder
internal interface IUpdateFolder
{
string CurrentFolder {get; set;}
}
#endregion
#region TextEntryDialog
// FIXME: When ListView.LabelEdit is implemented remove me
internal class TextEntryDialog : Form
{
private Label label1;
private Button okButton;
private TextBox newNameTextBox;
private PictureBox iconPictureBox;
private Button cancelButton;
private GroupBox groupBox1;
public TextEntryDialog ()
{
groupBox1 = new GroupBox ();
cancelButton = new Button ();
iconPictureBox = new PictureBox ();
newNameTextBox = new TextBox ();
okButton = new Button ();
label1 = new Label ();
groupBox1.SuspendLayout ();
SuspendLayout ();
// groupBox1
groupBox1.Controls.Add (newNameTextBox);
groupBox1.Controls.Add (label1);
groupBox1.Controls.Add (iconPictureBox);
groupBox1.Location = new Point (8, 8);
groupBox1.Size = new Size (232, 160);
groupBox1.TabIndex = 5;
groupBox1.TabStop = false;
groupBox1.Text = "New Name";
// cancelButton
cancelButton.DialogResult = DialogResult.Cancel;
cancelButton.Location = new Point (168, 176);
cancelButton.TabIndex = 4;
cancelButton.Text = "Cancel";
// iconPictureBox
iconPictureBox.BorderStyle = BorderStyle.Fixed3D;
iconPictureBox.Location = new Point (86, 24);
iconPictureBox.Size = new Size (60, 60);
iconPictureBox.TabIndex = 3;
iconPictureBox.TabStop = false;
iconPictureBox.SizeMode = PictureBoxSizeMode.CenterImage;
// newNameTextBox
newNameTextBox.Location = new Point (16, 128);
newNameTextBox.Size = new Size (200, 20);
newNameTextBox.TabIndex = 5;
newNameTextBox.Text = String.Empty;
// okButton
okButton.DialogResult = DialogResult.OK;
okButton.Location = new Point (80, 176);
okButton.TabIndex = 3;
okButton.Text = "OK";
// label1
label1.Location = new Point (16, 96);
label1.Size = new Size (200, 23);
label1.TabIndex = 4;
label1.Text = "Enter Name:";
label1.TextAlign = ContentAlignment.MiddleCenter;
// MainForm
AcceptButton = okButton;
AutoScaleBaseSize = new Size (5, 13);
CancelButton = cancelButton;
ClientSize = new Size (248, 205);
Controls.Add (groupBox1);
Controls.Add (cancelButton);
Controls.Add (okButton);
FormBorderStyle = FormBorderStyle.FixedDialog;
Text = "New Folder or File";
groupBox1.ResumeLayout (false);
ResumeLayout (false);
newNameTextBox.Select ();
}
public Image IconPictureBoxImage {
set {
iconPictureBox.Image = value;
}
}
public string FileName {
get {
return newNameTextBox.Text;
}
set {
newNameTextBox.Text = value;
}
}
}
#endregion
#region MWFVFS
internal class MWFVFS
{
private FileSystem fileSystem;
public static readonly string DesktopPrefix = "Desktop://";
public static readonly string PersonalPrefix = "Personal://";
public static readonly string MyComputerPrefix = "MyComputer://";
public static readonly string RecentlyUsedPrefix = "RecentlyUsed://";
public static readonly string MyNetworkPrefix = "MyNetwork://";
public static readonly string MyComputerPersonalPrefix = "MyComputerPersonal://";
public static Hashtable MyComputerDevicesPrefix = new Hashtable ();
public delegate void UpdateDelegate (ArrayList folders, ArrayList files);
private UpdateDelegate updateDelegate;
private Control calling_control;
private ThreadStart get_folder_content_thread_start;
private Thread worker;
private WorkerThread workerThread = null;
private StringCollection the_filters;
public MWFVFS ()
{
if (XplatUI.RunningOnUnix) {
fileSystem = new UnixFileSystem ();
} else {
fileSystem = new WinFileSystem ();
}
}
public FSEntry ChangeDirectory (string folder)
{
return fileSystem.ChangeDirectory (folder);
}
public void GetFolderContent ()
{
GetFolderContent (null);
}
public void GetFolderContent (StringCollection filters)
{
the_filters = filters;
if (workerThread != null) {
workerThread.Stop ();
workerThread = null;
}
// Added next line to ensure the control is created before BeginInvoke is called on it
calling_control.CreateControl();
workerThread = new WorkerThread (fileSystem, the_filters, updateDelegate, calling_control);
get_folder_content_thread_start = new ThreadStart (workerThread.GetFolderContentThread);
worker = new Thread (get_folder_content_thread_start);
worker.IsBackground = true;
worker.Start();
}
internal class WorkerThread
{
private FileSystem fileSystem;
private StringCollection the_filters;
private UpdateDelegate updateDelegate;
private Control calling_control;
private readonly object lockobject = new object ();
private bool stopped = false;
public WorkerThread (FileSystem fileSystem, StringCollection the_filters, UpdateDelegate updateDelegate, Control calling_control)
{
this.fileSystem = fileSystem;
this.the_filters = the_filters;
this.updateDelegate = updateDelegate;
this.calling_control = calling_control;
}
public void GetFolderContentThread()
{
ArrayList folders;
ArrayList files;
fileSystem.GetFolderContent (the_filters, out folders, out files);
if (stopped)
return;
if (updateDelegate != null) {
lock (this) {
object[] objectArray = new object[2];
objectArray[0] = folders;
objectArray[1] = files;
calling_control.BeginInvoke (updateDelegate, objectArray);
}
}
}
public void Stop ()
{
lock (lockobject) {
stopped = true;
}
}
}
public ArrayList GetFoldersOnly ()
{
return fileSystem.GetFoldersOnly ();
}
public void WriteRecentlyUsedFiles (string filename)
{
fileSystem.WriteRecentlyUsedFiles (filename);
}
public ArrayList GetRecentlyUsedFiles ()
{
return fileSystem.GetRecentlyUsedFiles ();
}
public ArrayList GetMyComputerContent ()
{
return fileSystem.GetMyComputerContent ();
}
public ArrayList GetMyNetworkContent ()
{
return fileSystem.GetMyNetworkContent ();
}
public bool CreateFolder (string new_folder)
{
try {
if (Directory.Exists (new_folder)) {
string message = "Folder \"" + new_folder + "\" already exists.";
MessageBox.Show (message, new_folder, MessageBoxButtons.OK,
MessageBoxIcon.Error);
return false;
} else
Directory.CreateDirectory (new_folder);
} catch (Exception e) {
MessageBox.Show (e.Message, new_folder, MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
return true;
}
public bool MoveFolder (string sourceDirName, string destDirName)
{
try {
if (Directory.Exists (destDirName)) {
string message = "Cannot rename " + Path.GetFileName (sourceDirName)
+ ": A folder with the name you specified already exists."
+ " Specify a different folder name.";
MessageBox.Show (message, "Error Renaming Folder", MessageBoxButtons.OK,
MessageBoxIcon.Error);
return false;
} else
Directory.Move (sourceDirName, destDirName);
} catch (Exception e) {
MessageBox.Show (e.Message, "Error Renaming Folder",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
return true;
}
public bool MoveFile (string sourceFileName, string destFileName)
{
try {
if (File.Exists (destFileName)) {
string message = "Cannot rename " + Path.GetFileName (sourceFileName)
+ ": A file with the name you specified already exists."
+ " Specify a different file name.";
MessageBox.Show (message, "Error Renaming File",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
} else
File.Move (sourceFileName, destFileName);
} catch (Exception e) {
MessageBox.Show (e.Message, "Error Renaming File",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
return true;
}
public string GetParent ()
{
return fileSystem.GetParent ();
}
public void RegisterUpdateDelegate(UpdateDelegate updateDelegate, Control control)
{
this.updateDelegate = updateDelegate;
calling_control = control;
}
}
#endregion
#region FileSystem
internal abstract class FileSystem
{
protected string currentTopFolder = String.Empty;
protected FSEntry currentFolderFSEntry = null;
protected FSEntry currentTopFolderFSEntry = null;
private FileInfoComparer fileInfoComparer = new FileInfoComparer ();
private FSEntryComparer fsEntryComparer = new FSEntryComparer ();
public FSEntry ChangeDirectory (string folder)
{
if (folder == MWFVFS.DesktopPrefix) {
currentTopFolder = MWFVFS.DesktopPrefix;
currentTopFolderFSEntry = currentFolderFSEntry = GetDesktopFSEntry ();
} else
if (folder == MWFVFS.PersonalPrefix) {
currentTopFolder = MWFVFS.PersonalPrefix;
currentTopFolderFSEntry = currentFolderFSEntry = GetPersonalFSEntry ();
} else
if (folder == MWFVFS.MyComputerPersonalPrefix) {
currentTopFolder = MWFVFS.MyComputerPersonalPrefix;
currentTopFolderFSEntry = currentFolderFSEntry = GetMyComputerPersonalFSEntry ();
} else
if (folder == MWFVFS.RecentlyUsedPrefix) {
currentTopFolder = MWFVFS.RecentlyUsedPrefix;
currentTopFolderFSEntry = currentFolderFSEntry = GetRecentlyUsedFSEntry ();
} else
if (folder == MWFVFS.MyComputerPrefix) {
currentTopFolder = MWFVFS.MyComputerPrefix;
currentTopFolderFSEntry = currentFolderFSEntry = GetMyComputerFSEntry ();
} else
if (folder == MWFVFS.MyNetworkPrefix) {
currentTopFolder = MWFVFS.MyNetworkPrefix;
currentTopFolderFSEntry = currentFolderFSEntry = GetMyNetworkFSEntry ();
} else {
bool found = false;
foreach (DictionaryEntry entry in MWFVFS.MyComputerDevicesPrefix) {
FSEntry fsEntry = entry.Value as FSEntry;
if (folder == fsEntry.FullName) {
currentTopFolder = entry.Key as string;
currentTopFolderFSEntry = currentFolderFSEntry = fsEntry;
found = true;
break;
}
}
if (!found) {
currentFolderFSEntry = GetDirectoryFSEntry (new DirectoryInfo (folder), currentTopFolderFSEntry);
}
}
return currentFolderFSEntry;
}
public string GetParent ()
{
return currentFolderFSEntry.Parent;
}
// directories_out and files_out contain FSEntry objects
public void GetFolderContent (StringCollection filters, out ArrayList directories_out, out ArrayList files_out)
{
directories_out = new ArrayList ();
files_out = new ArrayList ();
if (currentFolderFSEntry.FullName == MWFVFS.DesktopPrefix) {
FSEntry personalFSEntry = GetPersonalFSEntry ();
directories_out.Add (personalFSEntry);
FSEntry myComputerFSEntry = GetMyComputerFSEntry ();
directories_out.Add (myComputerFSEntry);
FSEntry myNetworkFSEntry = GetMyNetworkFSEntry ();
directories_out.Add (myNetworkFSEntry);
ArrayList d_out = null;
ArrayList f_out = null;
GetNormalFolderContent (ThemeEngine.Current.Places (UIIcon.PlacesDesktop), filters, out d_out, out f_out);
directories_out.AddRange (d_out);
files_out.AddRange (f_out);
} else
if (currentFolderFSEntry.FullName == MWFVFS.RecentlyUsedPrefix) {
files_out = GetRecentlyUsedFiles ();
} else
if (currentFolderFSEntry.FullName == MWFVFS.MyComputerPrefix) {
directories_out.AddRange (GetMyComputerContent ());
} else
if (currentFolderFSEntry.FullName == MWFVFS.PersonalPrefix || currentFolderFSEntry.FullName == MWFVFS.MyComputerPersonalPrefix) {
ArrayList d_out = null;
ArrayList f_out = null;
GetNormalFolderContent (ThemeEngine.Current.Places (UIIcon.PlacesPersonal), filters, out d_out, out f_out);
directories_out.AddRange (d_out);
files_out.AddRange (f_out);
} else
if (currentFolderFSEntry.FullName == MWFVFS.MyNetworkPrefix) {
directories_out.AddRange (GetMyNetworkContent ());
} else {
GetNormalFolderContent (currentFolderFSEntry.FullName, filters, out directories_out, out files_out);
}
}
public ArrayList GetFoldersOnly ()
{
ArrayList directories_out = new ArrayList ();
if (currentFolderFSEntry.FullName == MWFVFS.DesktopPrefix) {
FSEntry personalFSEntry = GetPersonalFSEntry ();
directories_out.Add (personalFSEntry);
FSEntry myComputerFSEntry = GetMyComputerFSEntry ();
directories_out.Add (myComputerFSEntry);
FSEntry myNetworkFSEntry = GetMyNetworkFSEntry ();
directories_out.Add (myNetworkFSEntry);
ArrayList d_out = GetNormalFolders (ThemeEngine.Current.Places (UIIcon.PlacesDesktop));
directories_out.AddRange (d_out);
} else
if (currentFolderFSEntry.FullName == MWFVFS.RecentlyUsedPrefix) {
//files_out = GetRecentlyUsedFiles ();
} else
if (currentFolderFSEntry.FullName == MWFVFS.MyComputerPrefix) {
directories_out.AddRange (GetMyComputerContent ());
} else
if (currentFolderFSEntry.FullName == MWFVFS.PersonalPrefix || currentFolderFSEntry.FullName == MWFVFS.MyComputerPersonalPrefix) {
ArrayList d_out = GetNormalFolders (ThemeEngine.Current.Places (UIIcon.PlacesPersonal));
directories_out.AddRange (d_out);
} else
if (currentFolderFSEntry.FullName == MWFVFS.MyNetworkPrefix) {
directories_out.AddRange (GetMyNetworkContent ());
} else {
directories_out = GetNormalFolders (currentFolderFSEntry.FullName);
}
return directories_out;
}
protected void GetNormalFolderContent (string from_folder, StringCollection filters, out ArrayList directories_out, out ArrayList files_out)
{
DirectoryInfo dirinfo = new DirectoryInfo (from_folder);
directories_out = new ArrayList ();
DirectoryInfo[] dirs = null;
try {
dirs = dirinfo.GetDirectories ();
} catch (Exception) {}
if (dirs != null)
for (int i = 0; i < dirs.Length; i++) {
directories_out.Add (GetDirectoryFSEntry (dirs [i], currentTopFolderFSEntry));
}
directories_out.Sort (fsEntryComparer);
files_out = new ArrayList ();
ArrayList files = new ArrayList ();
try {
if (filters == null) {
files.AddRange (dirinfo.GetFiles ());
} else {
foreach (string s in filters)
files.AddRange (dirinfo.GetFiles (s));
files.Sort (fileInfoComparer);
}
} catch (Exception) {}
for (int i = 0; i < files.Count; i++) {
FSEntry fs = GetFileFSEntry (files [i] as FileInfo);
if (fs != null)
files_out.Add (fs);
}
}
protected ArrayList GetNormalFolders (string from_folder)
{
DirectoryInfo dirinfo = new DirectoryInfo (from_folder);
ArrayList directories_out = new ArrayList ();
DirectoryInfo[] dirs = null;
try {
dirs = dirinfo.GetDirectories ();
} catch (Exception) {}
if (dirs != null)
for (int i = 0; i < dirs.Length; i++) {
directories_out.Add (GetDirectoryFSEntry (dirs [i], currentTopFolderFSEntry));
}
return directories_out;
}
protected virtual FSEntry GetDirectoryFSEntry (DirectoryInfo dirinfo, FSEntry topFolderFSEntry)
{
FSEntry fs = new FSEntry ();
fs.Attributes = dirinfo.Attributes;
fs.FullName = dirinfo.FullName;
fs.Name = dirinfo.Name;
fs.MainTopNode = topFolderFSEntry;
fs.FileType = FSEntry.FSEntryType.Directory;
fs.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("inode/directory");
fs.LastAccessTime = dirinfo.LastAccessTime;
return fs;
}
protected virtual FSEntry GetFileFSEntry (FileInfo fileinfo)
{
// *sigh* FileInfo gives us no usable information for links to directories
// so, return null
if ((fileinfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
return null;
FSEntry fs = new FSEntry ();
fs.Attributes = fileinfo.Attributes;
fs.FullName = fileinfo.FullName;
fs.Name = fileinfo.Name;
fs.FileType = FSEntry.FSEntryType.File;
fs.IconIndex = MimeIconEngine.GetIconIndexForFile (fileinfo.FullName);
fs.FileSize = fileinfo.Length;
fs.LastAccessTime = fileinfo.LastAccessTime;
return fs;
}
internal class FileInfoComparer : IComparer
{
public int Compare (object fileInfo1, object fileInfo2)
{
return String.Compare (((FileInfo)fileInfo1).Name, ((FileInfo)fileInfo2).Name);
}
}
internal class FSEntryComparer : IComparer
{
public int Compare (object fileInfo1, object fileInfo2)
{
return String.Compare (((FSEntry)fileInfo1).Name, ((FSEntry)fileInfo2).Name);
}
}
protected abstract FSEntry GetDesktopFSEntry ();
protected abstract FSEntry GetRecentlyUsedFSEntry ();
protected abstract FSEntry GetPersonalFSEntry ();
protected abstract FSEntry GetMyComputerPersonalFSEntry ();
protected abstract FSEntry GetMyComputerFSEntry ();
protected abstract FSEntry GetMyNetworkFSEntry ();
public abstract void WriteRecentlyUsedFiles (string fileToAdd);
public abstract ArrayList GetRecentlyUsedFiles ();
public abstract ArrayList GetMyComputerContent ();
public abstract ArrayList GetMyNetworkContent ();
}
#endregion
#region UnixFileSystem
internal class UnixFileSystem : FileSystem
{
private MasterMount masterMount = new MasterMount ();
private FSEntry desktopFSEntry = null;
private FSEntry recentlyusedFSEntry = null;
private FSEntry personalFSEntry = null;
private FSEntry mycomputerpersonalFSEntry = null;
private FSEntry mycomputerFSEntry = null;
private FSEntry mynetworkFSEntry = null;
private string personal_folder;
private string recently_used_path;
private string full_kde_recent_document_dir;
public UnixFileSystem ()
{
personal_folder = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
recently_used_path = Path.Combine (personal_folder, ".recently-used");
full_kde_recent_document_dir = personal_folder + "/.kde/share/apps/RecentDocuments";
desktopFSEntry = new FSEntry ();
desktopFSEntry.Attributes = FileAttributes.Directory;
desktopFSEntry.FullName = MWFVFS.DesktopPrefix;
desktopFSEntry.Name = "Desktop";
desktopFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesDesktop);
desktopFSEntry.FileType = FSEntry.FSEntryType.Directory;
desktopFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("desktop/desktop");
desktopFSEntry.LastAccessTime = DateTime.Now;
recentlyusedFSEntry = new FSEntry ();
recentlyusedFSEntry.Attributes = FileAttributes.Directory;
recentlyusedFSEntry.FullName = MWFVFS.RecentlyUsedPrefix;
recentlyusedFSEntry.Name = "Recently Used";
recentlyusedFSEntry.FileType = FSEntry.FSEntryType.Directory;
recentlyusedFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("recently/recently");
recentlyusedFSEntry.LastAccessTime = DateTime.Now;
personalFSEntry = new FSEntry ();
personalFSEntry.Attributes = FileAttributes.Directory;
personalFSEntry.FullName = MWFVFS.PersonalPrefix;
personalFSEntry.Name = "Personal";
personalFSEntry.MainTopNode = GetDesktopFSEntry ();
personalFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
personalFSEntry.FileType = FSEntry.FSEntryType.Directory;
personalFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("directory/home");
personalFSEntry.LastAccessTime = DateTime.Now;
mycomputerpersonalFSEntry = new FSEntry ();
mycomputerpersonalFSEntry.Attributes = FileAttributes.Directory;
mycomputerpersonalFSEntry.FullName = MWFVFS.MyComputerPersonalPrefix;
mycomputerpersonalFSEntry.Name = "Personal";
mycomputerpersonalFSEntry.MainTopNode = GetMyComputerFSEntry ();
mycomputerpersonalFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
mycomputerpersonalFSEntry.FileType = FSEntry.FSEntryType.Directory;
mycomputerpersonalFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("directory/home");
mycomputerpersonalFSEntry.LastAccessTime = DateTime.Now;
mycomputerFSEntry = new FSEntry ();
mycomputerFSEntry.Attributes = FileAttributes.Directory;
mycomputerFSEntry.FullName = MWFVFS.MyComputerPrefix;
mycomputerFSEntry.Name = "My Computer";
mycomputerFSEntry.MainTopNode = GetDesktopFSEntry ();
mycomputerFSEntry.FileType = FSEntry.FSEntryType.Directory;
mycomputerFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("workplace/workplace");
mycomputerFSEntry.LastAccessTime = DateTime.Now;
mynetworkFSEntry = new FSEntry ();
mynetworkFSEntry.Attributes = FileAttributes.Directory;
mynetworkFSEntry.FullName = MWFVFS.MyNetworkPrefix;
mynetworkFSEntry.Name = "My Network";
mynetworkFSEntry.MainTopNode = GetDesktopFSEntry ();
mynetworkFSEntry.FileType = FSEntry.FSEntryType.Directory;
mynetworkFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("network/network");
mynetworkFSEntry.LastAccessTime = DateTime.Now;
}
public override void WriteRecentlyUsedFiles (string fileToAdd)
{
if (File.Exists (recently_used_path) && new FileInfo (recently_used_path).Length > 0) {
XmlDocument xml_doc = new XmlDocument ();
xml_doc.Load (recently_used_path);
XmlNode grand_parent_node = xml_doc.SelectSingleNode ("RecentFiles");
if (grand_parent_node != null) {
// create a new element
XmlElement new_recent_item_node = xml_doc.CreateElement ("RecentItem");
XmlElement new_child = xml_doc.CreateElement ("URI");
UriBuilder ub = new UriBuilder ();
ub.Path = fileToAdd;
ub.Host = null;
ub.Scheme = "file";
XmlText new_text_child = xml_doc.CreateTextNode (ub.ToString ());
new_child.AppendChild (new_text_child);
new_recent_item_node.AppendChild (new_child);
new_child = xml_doc.CreateElement ("Mime-Type");
new_text_child = xml_doc.CreateTextNode (Mime.GetMimeTypeForFile (fileToAdd));
new_child.AppendChild (new_text_child);
new_recent_item_node.AppendChild (new_child);
new_child = xml_doc.CreateElement ("Timestamp");
long seconds = (long)(DateTime.UtcNow - new DateTime (1970, 1, 1)).TotalSeconds;
new_text_child = xml_doc.CreateTextNode (seconds.ToString ());
new_child.AppendChild (new_text_child);
new_recent_item_node.AppendChild (new_child);
new_child = xml_doc.CreateElement ("Groups");
new_recent_item_node.AppendChild (new_child);
// now search the nodes in grand_parent_node for another instance of the new uri and if found remove it
// so that the new node is the first one
foreach (XmlNode n in grand_parent_node.ChildNodes) {
XmlNode uri_node = n.SelectSingleNode ("URI");
if (uri_node != null) {
XmlNode uri_node_child = uri_node.FirstChild;
if (uri_node_child is XmlText)
if (ub.ToString () == ((XmlText)uri_node_child).Data) {
grand_parent_node.RemoveChild (n);
break;
}
}
}
// prepend the new recent item to the grand parent node list
grand_parent_node.PrependChild (new_recent_item_node);
// limit the # of RecentItems to 10
if (grand_parent_node.ChildNodes.Count > 10) {
while (grand_parent_node.ChildNodes.Count > 10)
grand_parent_node.RemoveChild (grand_parent_node.LastChild);
}
try {
xml_doc.Save (recently_used_path);
} catch (Exception) {
}
}
} else {
XmlDocument xml_doc = new XmlDocument ();
xml_doc.AppendChild (xml_doc.CreateXmlDeclaration ("1.0", String.Empty, String.Empty));
XmlElement recentFiles_element = xml_doc.CreateElement ("RecentFiles");
XmlElement new_recent_item_node = xml_doc.CreateElement ("RecentItem");
XmlElement new_child = xml_doc.CreateElement ("URI");
UriBuilder ub = new UriBuilder ();
ub.Path = fileToAdd;
ub.Host = null;
ub.Scheme = "file";
XmlText new_text_child = xml_doc.CreateTextNode (ub.ToString ());
new_child.AppendChild (new_text_child);
new_recent_item_node.AppendChild (new_child);
new_child = xml_doc.CreateElement ("Mime-Type");
new_text_child = xml_doc.CreateTextNode (Mime.GetMimeTypeForFile (fileToAdd));
new_child.AppendChild (new_text_child);
new_recent_item_node.AppendChild (new_child);
new_child = xml_doc.CreateElement ("Timestamp");
long seconds = (long)(DateTime.UtcNow - new DateTime (1970, 1, 1)).TotalSeconds;
new_text_child = xml_doc.CreateTextNode (seconds.ToString ());
new_child.AppendChild (new_text_child);
new_recent_item_node.AppendChild (new_child);
new_child = xml_doc.CreateElement ("Groups");
new_recent_item_node.AppendChild (new_child);
recentFiles_element.AppendChild (new_recent_item_node);
xml_doc.AppendChild (recentFiles_element);
try {
xml_doc.Save (recently_used_path);
} catch (Exception) {
}
}
}
// return an ArrayList with FSEntry objects
public override ArrayList GetRecentlyUsedFiles ()
{
// check for GNOME and KDE
ArrayList files_al = new ArrayList ();
// GNOME
if (File.Exists (recently_used_path)) {
try {
XmlTextReader xtr = new XmlTextReader (recently_used_path);
while (xtr.Read ()) {
if (xtr.NodeType == XmlNodeType.Element && xtr.Name.ToUpper () == "URI") {
xtr.Read ();
Uri uri = new Uri (xtr.Value);
if (!files_al.Contains (uri.LocalPath))
if (File.Exists (uri.LocalPath)) {
FSEntry fs = GetFileFSEntry (new FileInfo (uri.LocalPath));
if (fs != null)
files_al.Add (fs);
}
}
}
xtr.Close ();
} catch (Exception) {
}
}
// KDE
if (Directory.Exists (full_kde_recent_document_dir)) {
string[] files = Directory.GetFiles (full_kde_recent_document_dir, "*.desktop");
foreach (string file_name in files) {
StreamReader sr = new StreamReader (file_name);
string line = sr.ReadLine ();
while (line != null) {
line = line.Trim ();
if (line.StartsWith ("URL=")) {
line = line.Replace ("URL=", String.Empty);
line = line.Replace ("$HOME", personal_folder);
Uri uri = new Uri (line);
if (!files_al.Contains (uri.LocalPath))
if (File.Exists (uri.LocalPath)) {
FSEntry fs = GetFileFSEntry (new FileInfo (uri.LocalPath));
if (fs != null)
files_al.Add (fs);
}
break;
}
line = sr.ReadLine ();
}
sr.Close ();
}
}
return files_al;
}
// return an ArrayList with FSEntry objects
public override ArrayList GetMyComputerContent ()
{
ArrayList my_computer_content_arraylist = new ArrayList ();
if (masterMount.ProcMountAvailable) {
masterMount.GetMounts ();
foreach (MasterMount.Mount mount in masterMount.Block_devices) {
FSEntry fsEntry = new FSEntry ();
fsEntry.FileType = FSEntry.FSEntryType.Device;
fsEntry.FullName = mount.mount_point;
fsEntry.Name = "HDD (" + mount.fsType + ", " + mount.device_short + ")";
fsEntry.FsType = mount.fsType;
fsEntry.DeviceShort = mount.device_short;
fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("harddisk/harddisk");
fsEntry.Attributes = FileAttributes.Directory;
fsEntry.MainTopNode = GetMyComputerFSEntry ();
my_computer_content_arraylist.Add (fsEntry);
if (!MWFVFS.MyComputerDevicesPrefix.Contains (fsEntry.FullName + "://"))
MWFVFS.MyComputerDevicesPrefix.Add (fsEntry.FullName + "://", fsEntry);
}
foreach (MasterMount.Mount mount in masterMount.Removable_devices) {
FSEntry fsEntry = new FSEntry ();
fsEntry.FileType = FSEntry.FSEntryType.RemovableDevice;
fsEntry.FullName = mount.mount_point;
bool is_dvd_cdrom = mount.fsType == MasterMount.FsTypes.usbfs ? false : true;
string type_name = is_dvd_cdrom ? "DVD/CD-Rom" : "USB";
string mime_type = is_dvd_cdrom ? "cdrom/cdrom" : "removable/removable";
fsEntry.Name = type_name +" (" + mount.device_short + ")";
fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType (mime_type);
fsEntry.FsType = mount.fsType;
fsEntry.DeviceShort = mount.device_short;
fsEntry.Attributes = FileAttributes.Directory;
fsEntry.MainTopNode = GetMyComputerFSEntry ();
my_computer_content_arraylist.Add (fsEntry);
string contain_string = fsEntry.FullName + "://";
if (!MWFVFS.MyComputerDevicesPrefix.Contains (contain_string))
MWFVFS.MyComputerDevicesPrefix.Add (contain_string, fsEntry);
}
}
my_computer_content_arraylist.Add (GetMyComputerPersonalFSEntry ());
return my_computer_content_arraylist;
}
public override ArrayList GetMyNetworkContent ()
{
ArrayList fsEntries = new ArrayList ();
foreach (MasterMount.Mount mount in masterMount.Network_devices) {
FSEntry fsEntry = new FSEntry ();
fsEntry.FileType = FSEntry.FSEntryType.Network;
fsEntry.FullName = mount.mount_point;
fsEntry.FsType = mount.fsType;
fsEntry.DeviceShort = mount.device_short;
fsEntry.Name = "Network (" + mount.fsType + ", " + mount.device_short + ")";
switch (mount.fsType) {
case MasterMount.FsTypes.nfs:
fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("nfs/nfs");
break;
case MasterMount.FsTypes.smbfs:
fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("smb/smb");
break;
case MasterMount.FsTypes.ncpfs:
fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("network/network");
break;
case MasterMount.FsTypes.cifs:
fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("network/network");
break;
default:
break;
}
fsEntry.Attributes = FileAttributes.Directory;
fsEntry.MainTopNode = GetMyNetworkFSEntry ();
fsEntries.Add (fsEntry);
}
return fsEntries;
}
protected override FSEntry GetDesktopFSEntry ()
{
return desktopFSEntry;
}
protected override FSEntry GetRecentlyUsedFSEntry ()
{
return recentlyusedFSEntry;
}
protected override FSEntry GetPersonalFSEntry ()
{
return personalFSEntry;
}
protected override FSEntry GetMyComputerPersonalFSEntry ()
{
return mycomputerpersonalFSEntry;
}
protected override FSEntry GetMyComputerFSEntry ()
{
return mycomputerFSEntry;
}
protected override FSEntry GetMyNetworkFSEntry ()
{
return mynetworkFSEntry;
}
}
#endregion
#region WinFileSystem
internal class WinFileSystem : FileSystem
{
private FSEntry desktopFSEntry = null;
private FSEntry recentlyusedFSEntry = null;
private FSEntry personalFSEntry = null;
private FSEntry mycomputerpersonalFSEntry = null;
private FSEntry mycomputerFSEntry = null;
private FSEntry mynetworkFSEntry = null;
public WinFileSystem ()
{
desktopFSEntry = new FSEntry ();
desktopFSEntry.Attributes = FileAttributes.Directory;
desktopFSEntry.FullName = MWFVFS.DesktopPrefix;
desktopFSEntry.Name = "Desktop";
desktopFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesDesktop);
desktopFSEntry.FileType = FSEntry.FSEntryType.Directory;
desktopFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("desktop/desktop");
desktopFSEntry.LastAccessTime = DateTime.Now;
recentlyusedFSEntry = new FSEntry ();
recentlyusedFSEntry.Attributes = FileAttributes.Directory;
recentlyusedFSEntry.FullName = MWFVFS.RecentlyUsedPrefix;
recentlyusedFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesRecentDocuments);
recentlyusedFSEntry.Name = "Recently Used";
recentlyusedFSEntry.FileType = FSEntry.FSEntryType.Directory;
recentlyusedFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("recently/recently");
recentlyusedFSEntry.LastAccessTime = DateTime.Now;
personalFSEntry = new FSEntry ();
personalFSEntry.Attributes = FileAttributes.Directory;
personalFSEntry.FullName = MWFVFS.PersonalPrefix;
personalFSEntry.Name = "Personal";
personalFSEntry.MainTopNode = GetDesktopFSEntry ();
personalFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
personalFSEntry.FileType = FSEntry.FSEntryType.Directory;
personalFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("directory/home");
personalFSEntry.LastAccessTime = DateTime.Now;
mycomputerpersonalFSEntry = new FSEntry ();
mycomputerpersonalFSEntry.Attributes = FileAttributes.Directory;
mycomputerpersonalFSEntry.FullName = MWFVFS.MyComputerPersonalPrefix;
mycomputerpersonalFSEntry.Name = "Personal";
mycomputerpersonalFSEntry.MainTopNode = GetMyComputerFSEntry ();
mycomputerpersonalFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
mycomputerpersonalFSEntry.FileType = FSEntry.FSEntryType.Directory;
mycomputerpersonalFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("directory/home");
mycomputerpersonalFSEntry.LastAccessTime = DateTime.Now;
mycomputerFSEntry = new FSEntry ();
mycomputerFSEntry.Attributes = FileAttributes.Directory;
mycomputerFSEntry.FullName = MWFVFS.MyComputerPrefix;
mycomputerFSEntry.Name = "My Computer";
mycomputerFSEntry.MainTopNode = GetDesktopFSEntry ();
mycomputerFSEntry.FileType = FSEntry.FSEntryType.Directory;
mycomputerFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("workplace/workplace");
mycomputerFSEntry.LastAccessTime = DateTime.Now;
mynetworkFSEntry = new FSEntry ();
mynetworkFSEntry.Attributes = FileAttributes.Directory;
mynetworkFSEntry.FullName = MWFVFS.MyNetworkPrefix;
mynetworkFSEntry.Name = "My Network";
mynetworkFSEntry.MainTopNode = GetDesktopFSEntry ();
mynetworkFSEntry.FileType = FSEntry.FSEntryType.Directory;
mynetworkFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("network/network");
mynetworkFSEntry.LastAccessTime = DateTime.Now;
}
public override void WriteRecentlyUsedFiles (string fileToAdd)
{
// TODO: Implement this method
// use SHAddToRecentDocs ?
}
public override ArrayList GetRecentlyUsedFiles ()
{
ArrayList al = new ArrayList ();
DirectoryInfo di = new DirectoryInfo (recentlyusedFSEntry.RealName);
FileInfo[] fileinfos = di.GetFiles ();
foreach (FileInfo fi in fileinfos) {
FSEntry fs = GetFileFSEntry (fi);
if (fs != null)
al.Add (fs);
}
return al;
}
public override ArrayList GetMyComputerContent ()
{
string[] logical_drives = Directory.GetLogicalDrives ();
ArrayList my_computer_content_arraylist = new ArrayList ();
foreach (string drive in logical_drives) {
FSEntry fsEntry = new FSEntry ();
fsEntry.FileType = FSEntry.FSEntryType.Device;
fsEntry.FullName = drive;
fsEntry.Name = drive;
fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("harddisk/harddisk");
fsEntry.Attributes = FileAttributes.Directory;
fsEntry.MainTopNode = GetMyComputerFSEntry ();
my_computer_content_arraylist.Add (fsEntry);
string contain_string = fsEntry.FullName + "://";
if (!MWFVFS.MyComputerDevicesPrefix.Contains (contain_string))
MWFVFS.MyComputerDevicesPrefix.Add (contain_string, fsEntry);
}
my_computer_content_arraylist.Add (GetMyComputerPersonalFSEntry ());
return my_computer_content_arraylist;
}
public override ArrayList GetMyNetworkContent ()
{
// TODO: Implement this method
return new ArrayList ();
}
protected override FSEntry GetDesktopFSEntry ()
{
return desktopFSEntry;
}
protected override FSEntry GetRecentlyUsedFSEntry ()
{
return recentlyusedFSEntry;
}
protected override FSEntry GetPersonalFSEntry ()
{
return personalFSEntry;
}
protected override FSEntry GetMyComputerPersonalFSEntry ()
{
return mycomputerpersonalFSEntry;
}
protected override FSEntry GetMyComputerFSEntry ()
{
return mycomputerFSEntry;
}
protected override FSEntry GetMyNetworkFSEntry ()
{
return mynetworkFSEntry;
}
}
#endregion
#region FSEntry
internal class FSEntry
{
public enum FSEntryType
{
Desktop,
RecentlyUsed,
MyComputer,
File,
Directory,
Device,
RemovableDevice,
Network
}
private MasterMount.FsTypes fsType;
private string device_short;
private string fullName;
private string name;
private string realName = null;
private FileAttributes attributes = FileAttributes.Normal;
private long fileSize;
private FSEntryType fileType;
private DateTime lastAccessTime;
private FSEntry mainTopNode = null;
private int iconIndex;
private string parent;
public MasterMount.FsTypes FsType {
set {
fsType = value;
}
get {
return fsType;
}
}
public string DeviceShort {
set {
device_short = value;
}
get {
return device_short;
}
}
public string FullName {
set {
fullName = value;
}
get {
return fullName;
}
}
public string Name {
set {
name = value;
}
get {
return name;
}
}
public string RealName {
set {
realName = value;
}
get {
return realName;
}
}
public FileAttributes Attributes {
set {
attributes = value;
}
get {
return attributes;
}
}
public long FileSize {
set {
fileSize = value;
}
get {
return fileSize;
}
}
public FSEntryType FileType {
set {
fileType = value;
}
get {
return fileType;
}
}
public DateTime LastAccessTime {
set {
lastAccessTime = value;
}
get {
return lastAccessTime;
}
}
public int IconIndex {
set {
iconIndex = value;
}
get {
return iconIndex;
}
}
public FSEntry MainTopNode {
set {
mainTopNode = value;
}
get {
return mainTopNode;
}
}
public string Parent {
set {
parent = value;
}
get {
parent = GetParent ();
return parent;
}
}
private string GetParent ()
{
if (fullName == MWFVFS.PersonalPrefix) {
return MWFVFS.DesktopPrefix;
} else
if (fullName == MWFVFS.MyComputerPersonalPrefix) {
return MWFVFS.MyComputerPrefix;
} else
if (fullName == MWFVFS.MyComputerPrefix) {
return MWFVFS.DesktopPrefix;
} else
if (fullName == MWFVFS.MyNetworkPrefix) {
return MWFVFS.DesktopPrefix;
} else
if (fullName == MWFVFS.DesktopPrefix) {
return null;
} else
if (fullName == MWFVFS.RecentlyUsedPrefix) {
return null;
} else {
foreach (DictionaryEntry entry in MWFVFS.MyComputerDevicesPrefix) {
FSEntry fsEntry = entry.Value as FSEntry;
if (fullName == fsEntry.FullName) {
return fsEntry.MainTopNode.FullName;
}
}
DirectoryInfo dirInfo = new DirectoryInfo (fullName);
DirectoryInfo dirInfoParent = dirInfo.Parent;
if (dirInfoParent != null) {
FSEntry fsEntry = MWFVFS.MyComputerDevicesPrefix [dirInfoParent.FullName + "://"] as FSEntry;
if (fsEntry != null) {
return fsEntry.FullName;
}
if (mainTopNode != null) {
if (dirInfoParent.FullName == ThemeEngine.Current.Places (UIIcon.PlacesDesktop) &&
mainTopNode.FullName == MWFVFS.DesktopPrefix) {
return mainTopNode.FullName;
} else
if (dirInfoParent.FullName == ThemeEngine.Current.Places (UIIcon.PlacesPersonal) &&
mainTopNode.FullName == MWFVFS.PersonalPrefix) {
return mainTopNode.FullName;
} else
if (dirInfoParent.FullName == ThemeEngine.Current.Places (UIIcon.PlacesPersonal) &&
mainTopNode.FullName == MWFVFS.MyComputerPersonalPrefix) {
return mainTopNode.FullName;
}
}
return dirInfoParent.FullName;
}
}
return null;
}
}
#endregion
#region MasterMount
// Alexsantas little *nix helper
internal class MasterMount
{
// add more...
internal enum FsTypes
{
none,
ext2,
ext3,
hpfs,
iso9660,
jfs,
minix,
msdos,
ntfs,
reiserfs,
ufs,
umsdos,
vfat,
sysv,
xfs,
ncpfs,
nfs,
smbfs,
usbfs,
cifs
}
internal struct Mount
{
public string device_or_filesystem;
public string device_short;
public string mount_point;
public FsTypes fsType;
}
bool proc_mount_available = false;
ArrayList block_devices = new ArrayList ();
ArrayList network_devices = new ArrayList ();
ArrayList removable_devices = new ArrayList ();
private MountComparer mountComparer = new MountComparer ();
public MasterMount ()
{
// maybe check if the current user can access /proc/mounts
if (XplatUI.RunningOnUnix)
if (File.Exists ("/proc/mounts"))
proc_mount_available = true;
}
public ArrayList Block_devices {
get {
return block_devices;
}
}
public ArrayList Network_devices {
get {
return network_devices;
}
}
public ArrayList Removable_devices {
get {
return removable_devices;
}
}
public bool ProcMountAvailable {
get {
return proc_mount_available;
}
}
public void GetMounts ()
{
if (!proc_mount_available)
return;
block_devices.Clear ();
network_devices.Clear ();
removable_devices.Clear ();
try {
StreamReader sr = new StreamReader ("/proc/mounts");
string line = sr.ReadLine ();
ArrayList lines = new ArrayList ();
while (line != null) {
if (lines.IndexOf (line) == -1) { // Avoid duplicates
ProcessProcMountLine (line);
lines.Add (line);
}
line = sr.ReadLine ();
}
sr.Close ();
block_devices.Sort (mountComparer);
network_devices.Sort (mountComparer);
removable_devices.Sort (mountComparer);
} catch {
// bla
}
}
private void ProcessProcMountLine (string line)
{
string[] split = line.Split (new char [] {' '});
if (split != null && split.Length > 0) {
Mount mount = new Mount ();
if (split [0].StartsWith ("/dev/"))
mount.device_short = split [0].Replace ("/dev/", String.Empty);
else
mount.device_short = split [0];
mount.device_or_filesystem = split [0];
mount.mount_point = split [1];
// TODO: other removable devices, floppy
// ssh
// network mount
if (split [2] == "nfs") {
mount.fsType = FsTypes.nfs;
network_devices.Add (mount);
} else if (split [2] == "smbfs") {
mount.fsType = FsTypes.smbfs;
network_devices.Add (mount);
} else if (split [2] == "cifs") {
mount.fsType = FsTypes.cifs;
network_devices.Add (mount);
} else if (split [2] == "ncpfs") {
mount.fsType = FsTypes.ncpfs;
network_devices.Add (mount);
} else if (split [2] == "iso9660") { //cdrom
mount.fsType = FsTypes.iso9660;
removable_devices.Add (mount);
} else if (split [2] == "usbfs") { //usb ? not tested
mount.fsType = FsTypes.usbfs;
removable_devices.Add (mount);
} else if (split [0].StartsWith ("/")) { //block devices
if (split [1].StartsWith ("/dev/")) // root static, do not add
return;
if (split [2] == "ext2")
mount.fsType = FsTypes.ext2;
else if (split [2] == "ext3")
mount.fsType = FsTypes.ext3;
else if (split [2] == "reiserfs")
mount.fsType = FsTypes.reiserfs;
else if (split [2] == "xfs")
mount.fsType = FsTypes.xfs;
else if (split [2] == "vfat")
mount.fsType = FsTypes.vfat;
else if (split [2] == "ntfs")
mount.fsType = FsTypes.ntfs;
else if (split [2] == "msdos")
mount.fsType = FsTypes.msdos;
else if (split [2] == "umsdos")
mount.fsType = FsTypes.umsdos;
else if (split [2] == "hpfs")
mount.fsType = FsTypes.hpfs;
else if (split [2] == "minix")
mount.fsType = FsTypes.minix;
else if (split [2] == "jfs")
mount.fsType = FsTypes.jfs;
block_devices.Add (mount);
}
}
}
public class MountComparer : IComparer
{
public int Compare (object mount1, object mount2)
{
return String.Compare (((Mount)mount1).device_short, ((Mount)mount2).device_short);
}
}
}
#endregion
#region MWFConfig
// easy to use class to store and read internal MWF config settings.
// the config values are stored in the users home dir as a hidden xml file called "mwf_config".
// currently supports int, string, byte, color and arrays (including byte arrays)
// don't forget, when you read a value you still have to cast this value.
//
// usage:
// MWFConfig.SetValue ("SomeClass", "What", value);
// object o = MWFConfig.GetValue ("SomeClass", "What");
//
// example:
//
// string[] configFileNames = (string[])MWFConfig.GetValue ("FileDialog", "FileNames");
// MWFConfig.SetValue ("FileDialog", "LastFolder", "/home/user");
internal class MWFConfig
{
private static MWFConfigInstance Instance = new MWFConfigInstance ();
private static object lock_object = new object();
public static object GetValue (string class_name, string value_name)
{
lock (lock_object) {
return Instance.GetValue (class_name, value_name);
}
}
public static void SetValue (string class_name, string value_name, object value)
{
lock (lock_object) {
Instance.SetValue (class_name, value_name, value);
}
}
public static void Flush ()
{
lock (lock_object) {
Instance.Flush ();
}
}
public static void RemoveClass (string class_name)
{
lock (lock_object) {
Instance.RemoveClass (class_name);
}
}
public static void RemoveClassValue (string class_name, string value_name)
{
lock (lock_object) {
Instance.RemoveClassValue (class_name, value_name);
}
}
public static void RemoveAllClassValues (string class_name)
{
lock (lock_object) {
Instance.RemoveAllClassValues (class_name);
}
}
internal class MWFConfigInstance
{
Hashtable classes_hashtable = new Hashtable ();
static string full_file_name;
static string default_file_name;
readonly string configName = "MWFConfig";
static MWFConfigInstance ()
{
string b = "mwf_config";
string dir = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
if (XplatUI.RunningOnUnix) {
dir = Path.Combine (dir, ".mono");
try {
Directory.CreateDirectory (dir);
} catch {}
}
default_file_name = Path.Combine (dir, b);
full_file_name = default_file_name;
}
public MWFConfigInstance ()
{
Open (default_file_name);
}
// only for testing
public MWFConfigInstance (string filename)
{
string path = Path.GetDirectoryName (filename);
if (path == null || path == String.Empty) {
path = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
full_file_name = Path.Combine (path, filename);
} else
full_file_name = filename;
Open (full_file_name);
}
~MWFConfigInstance ()
{
Flush ();
}
public object GetValue (string class_name, string value_name)
{
ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
if (class_entry != null)
return class_entry.GetValue (value_name);
return null;
}
public void SetValue (string class_name, string value_name, object value)
{
ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
if (class_entry == null) {
class_entry = new ClassEntry ();
class_entry.ClassName = class_name;
classes_hashtable [class_name] = class_entry;
}
class_entry.SetValue (value_name, value);
}
private void Open (string filename)
{
try {
XmlTextReader xtr = new XmlTextReader (filename);
ReadConfig (xtr);
xtr.Close ();
} catch (Exception) {
}
}
public void Flush ()
{
try {
XmlTextWriter xtw = new XmlTextWriter (full_file_name, null);
xtw.Formatting = Formatting.Indented;
WriteConfig (xtw);
xtw.Close ();
if (!XplatUI.RunningOnUnix)
File.SetAttributes (full_file_name, FileAttributes.Hidden);
} catch (Exception){
}
}
public void RemoveClass (string class_name)
{
ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
if (class_entry != null) {
class_entry.RemoveAllClassValues ();
classes_hashtable.Remove (class_name);
}
}
public void RemoveClassValue (string class_name, string value_name)
{
ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
if (class_entry != null) {
class_entry.RemoveClassValue (value_name);
}
}
public void RemoveAllClassValues (string class_name)
{
ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
if (class_entry != null) {
class_entry.RemoveAllClassValues ();
}
}
private void ReadConfig (XmlTextReader xtr)
{
if (!CheckForMWFConfig (xtr))
return;
while (xtr.Read ()) {
switch (xtr.NodeType) {
case XmlNodeType.Element:
ClassEntry class_entry = classes_hashtable [xtr.Name] as ClassEntry;
if (class_entry == null) {
class_entry = new ClassEntry ();
class_entry.ClassName = xtr.Name;
classes_hashtable [xtr.Name] = class_entry;
}
class_entry.ReadXml (xtr);
break;
}
}
}
private bool CheckForMWFConfig (XmlTextReader xtr)
{
if (xtr.Read ()) {
if (xtr.NodeType == XmlNodeType.Element) {
if (xtr.Name == configName)
return true;
}
}
return false;
}
private void WriteConfig (XmlTextWriter xtw)
{
if (classes_hashtable.Count == 0)
return;
xtw.WriteStartElement (configName);
foreach (DictionaryEntry entry in classes_hashtable) {
ClassEntry class_entry = entry.Value as ClassEntry;
class_entry.WriteXml (xtw);
}
xtw.WriteEndElement ();
}
internal class ClassEntry
{
private Hashtable classvalues_hashtable = new Hashtable ();
private string className;
public string ClassName {
set {
className = value;
}
get {
return className;
}
}
public void SetValue (string value_name, object value)
{
ClassValue class_value = classvalues_hashtable [value_name] as ClassValue;
if (class_value == null) {
class_value = new ClassValue ();
class_value.Name = value_name;
classvalues_hashtable [value_name] = class_value;
}
class_value.SetValue (value);
}
public object GetValue (string value_name)
{
ClassValue class_value = classvalues_hashtable [value_name] as ClassValue;
if (class_value == null) {
return null;
}
return class_value.GetValue ();
}
public void RemoveAllClassValues ()
{
classvalues_hashtable.Clear ();
}
public void RemoveClassValue (string value_name)
{
ClassValue class_value = classvalues_hashtable [value_name] as ClassValue;
if (class_value != null) {
classvalues_hashtable.Remove (value_name);
}
}
public void ReadXml (XmlTextReader xtr)
{
while (xtr.Read ()) {
switch (xtr.NodeType) {
case XmlNodeType.Element:
string name = xtr.GetAttribute ("name");
ClassValue class_value = classvalues_hashtable [name] as ClassValue;
if (class_value == null) {
class_value = new ClassValue ();
class_value.Name = name;
classvalues_hashtable [name] = class_value;
}
class_value.ReadXml (xtr);
break;
case XmlNodeType.EndElement:
return;
}
}
}
public void WriteXml (XmlTextWriter xtw)
{
if (classvalues_hashtable.Count == 0)
return;
xtw.WriteStartElement (className);
foreach (DictionaryEntry entry in classvalues_hashtable) {
ClassValue class_value = entry.Value as ClassValue;
class_value.WriteXml (xtw);
}
xtw.WriteEndElement ();
}
}
internal class ClassValue
{
private object value;
private string name;
public string Name {
set {
name = value;
}
get {
return name;
}
}
public void SetValue (object value)
{
this.value = value;
}
public object GetValue ()
{
return value;
}
public void ReadXml (XmlTextReader xtr)
{
string type;
string single_value;
type = xtr.GetAttribute ("type");
if (type == "byte_array" || type.IndexOf ("-array") == -1) {
single_value = xtr.ReadString ();
if (type == "string") {
value = single_value;
} else
if (type == "int") {
value = Int32.Parse (single_value);
} else
if (type == "byte") {
value = Byte.Parse (single_value);
} else
if (type == "color") {
int color = Int32.Parse (single_value);
value = Color.FromArgb (color);
} else
if (type == "byte-array") {
byte[] b_array = Convert.FromBase64String (single_value);
value = b_array;
}
} else {
ReadXmlArrayValues (xtr, type);
}
}
private void ReadXmlArrayValues (XmlTextReader xtr, string type)
{
ArrayList al = new ArrayList ();
while (xtr.Read ()) {
switch (xtr.NodeType) {
case XmlNodeType.Element:
string single_value = xtr.ReadString ();
if (type == "int-array") {
int int_val = Int32.Parse (single_value);
al.Add (int_val);
} else
if (type == "string-array") {
string str_val = single_value;
al.Add (str_val);
}
break;
case XmlNodeType.EndElement:
if (xtr.Name == "value") {
if (type == "int-array") {
value = al.ToArray (typeof(int));
} else
if (type == "string-array") {
value = al.ToArray (typeof(string));
}
return;
}
break;
}
}
}
public void WriteXml (XmlTextWriter xtw)
{
xtw.WriteStartElement ("value");
xtw.WriteAttributeString ("name", name);
if (value is Array) {
WriteArrayContent (xtw);
} else {
WriteSingleContent (xtw);
}
xtw.WriteEndElement ();
}
private void WriteSingleContent (XmlTextWriter xtw)
{
string type_string = String.Empty;
if (value is string)
type_string = "string";
else
if (value is int)
type_string = "int";
else
if (value is byte)
type_string = "byte";
else
if (value is Color)
type_string = "color";
xtw.WriteAttributeString ("type", type_string);
if (value is Color)
xtw.WriteString (((Color)value).ToArgb ().ToString ());
else
xtw.WriteString (value.ToString ());
}
private void WriteArrayContent (XmlTextWriter xtw)
{
string type_string = String.Empty;
string type_name = String.Empty;
if (value is string[]) {
type_string = "string-array";
type_name = "string";
} else
if (value is int[]) {
type_string = "int-array";
type_name = "int";
} else
if (value is byte[]) {
type_string = "byte-array";
type_name = "byte";
}
xtw.WriteAttributeString ("type", type_string);
if (type_string != "byte-array") {
Array array = value as Array;
foreach (object o in array) {
xtw.WriteStartElement (type_name);
xtw.WriteString (o.ToString ());
xtw.WriteEndElement ();
}
} else {
byte[] b_array = value as byte [];
xtw.WriteString (Convert.ToBase64String (b_array, 0, b_array.Length));
}
}
}
}
}
#endregion
}
|