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 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977
|
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright (C) 2006-2007 Red Hat, Inc.
* Copyright (C) 2014 Руслан Ижбулатов
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Authors: Alexander Larsson <alexl@redhat.com>
* Руслан Ижбулатов <lrn1986@gmail.com>
*/
#include "config.h"
#define COBJMACROS
#include <string.h>
#include "gcontenttype.h"
#include "gwin32appinfo.h"
#include "gappinfo.h"
#include "gioerror.h"
#include "gfile.h"
#include <glib/gstdio.h>
#include "glibintl.h"
#include <gio/gwin32registrykey.h>
#include <shlobj.h>
/* Contains the definitions from shlobj.h that are
* guarded as Windows8-or-newer and are unavailable
* to GLib, being only Windows7-or-newer.
*/
#include "gwin32api-application-activation-manager.h"
#include <windows.h>
/* For SHLoadIndirectString() */
#include <shlwapi.h>
#include <glib/gstdioprivate.h>
#include "giowin32-priv.h"
#include "glib-private.h"
/* We need to watch 8 places:
* 0) HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations
* (anything below that key)
* On change: re-enumerate subkeys, read their values.
* 1) HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts
* (anything below that key)
* On change: re-enumerate subkeys
* 2) HKEY_CURRENT_USER\\Software\\Clients (anything below that key)
* On change: re-read the whole hierarchy of handlers
* 3) HKEY_LOCAL_MACHINE\\Software\\Clients (anything below that key)
* On change: re-read the whole hierarchy of handlers
* 4) HKEY_LOCAL_MACHINE\\Software\\RegisteredApplications (values of that key)
* On change: re-read the value list of registered applications
* 5) HKEY_CURRENT_USER\\Software\\RegisteredApplications (values of that key)
* On change: re-read the value list of registered applications
* 6) HKEY_CLASSES_ROOT\\Applications (anything below that key)
* On change: re-read the whole hierarchy of apps
* 7) HKEY_CLASSES_ROOT (only its subkeys)
* On change: re-enumerate subkeys, try to filter out wrong names.
*
*
* About verbs. A registry key (the name of that key is known as ProgID)
* can contain a "shell" subkey, which can then contain a number of verb
* subkeys (the most common being the "open" verb), and each of these
* contains a "command" subkey, which has a default string value that
* is the command to be run.
* Most ProgIDs are in HKEY_CLASSES_ROOT, but some are nested deeper in
* the registry (such as HKEY_CURRENT_USER\\Software\\<softwarename>).
*
* Verb selection works like this (according to https://docs.microsoft.com/en-us/windows/win32/shell/context ):
* 1) If "open" verb is available, that verb is used.
* 2) If the Shell subkey has a default string value, and if a verb subkey
* with that name exists, that verb is used.
* 3) The first subkey found in the list of verb subkeys is used.
* 4) The "openwith" verb is used
*
* Testing suggests that Windows never reaches the point 4 in any realistic
* circumstances. If a "command" subkey is missing for a verb, or if it has
* an empty string as its default value, the app launch fails
* (the "openwith" verb is not used, even if it's present).
* If the command is present, but is not valid (runs nonexisting executable,
* for example), then other verbs are not checked.
* It seems that when the documentation said "openwith verb", it meant
* that Windows invokes the default "Open with..." dialog (it does not
* look at the "openwith" verb subkey, even if it's there).
* If a verb subkey that is supposed to be used is present, but it lacks
* a command subkey, an error message is shown and nothing else happens.
*/
#define _verb_idx(array,index) ((GWin32AppInfoShellVerb *) g_ptr_array_index (array, index))
#define _lookup_by_verb(array, verb, dst, itemtype) do { \
gsize _index; \
itemtype *_v; \
for (_index = 0; array && _index < array->len; _index++) \
{ \
_v = (itemtype *) g_ptr_array_index (array, _index); \
if (_wcsicmp (_v->verb_name, (verb)) == 0) \
{ \
*(dst) = _v; \
break; \
} \
} \
if (array == NULL || _index >= array->len) \
*(dst) = NULL; \
} while (0)
#define _verb_lookup(array, verb, dst) _lookup_by_verb (array, verb, dst, GWin32AppInfoShellVerb)
/* Because with subcommands a verb would have
* a name like "foo\\bar", but the key its command
* should be looked for is "shell\\foo\\shell\\bar\\command"
*/
typedef struct _reg_verb {
gunichar2 *name;
gunichar2 *shellpath;
} reg_verb;
typedef struct _GWin32AppInfoURLSchema GWin32AppInfoURLSchema;
typedef struct _GWin32AppInfoFileExtension GWin32AppInfoFileExtension;
typedef struct _GWin32AppInfoShellVerb GWin32AppInfoShellVerb;
typedef struct _GWin32AppInfoHandler GWin32AppInfoHandler;
typedef struct _GWin32AppInfoApplication GWin32AppInfoApplication;
typedef struct _GWin32AppInfoURLSchemaClass GWin32AppInfoURLSchemaClass;
typedef struct _GWin32AppInfoFileExtensionClass GWin32AppInfoFileExtensionClass;
typedef struct _GWin32AppInfoShellVerbClass GWin32AppInfoShellVerbClass;
typedef struct _GWin32AppInfoHandlerClass GWin32AppInfoHandlerClass;
typedef struct _GWin32AppInfoApplicationClass GWin32AppInfoApplicationClass;
struct _GWin32AppInfoURLSchemaClass
{
GObjectClass parent_class;
};
struct _GWin32AppInfoFileExtensionClass
{
GObjectClass parent_class;
};
struct _GWin32AppInfoHandlerClass
{
GObjectClass parent_class;
};
struct _GWin32AppInfoApplicationClass
{
GObjectClass parent_class;
};
struct _GWin32AppInfoShellVerbClass
{
GObjectClass parent_class;
};
struct _GWin32AppInfoURLSchema {
GObject parent_instance;
/* url schema (stuff before ':') */
gunichar2 *schema;
/* url schema (stuff before ':'), in UTF-8 */
gchar *schema_u8;
/* url schema (stuff before ':'), in UTF-8, folded */
gchar *schema_u8_folded;
/* Handler currently selected for this schema. Can be NULL. */
GWin32AppInfoHandler *chosen_handler;
/* Maps folded handler IDs -> to GWin32AppInfoHandlers for this schema.
* Includes the chosen handler, if any.
*/
GHashTable *handlers;
};
struct _GWin32AppInfoHandler {
GObject parent_instance;
/* Usually a class name in HKCR */
gunichar2 *handler_id;
/* Registry object obtained by opening @handler_id.
* Can be used to watch this handler.
* May be %NULL (for fake handlers that we made up).
*/
GWin32RegistryKey *key;
/* @handler_id, in UTF-8, folded */
gchar *handler_id_folded;
/* Icon of the application for this handler */
GIcon *icon;
/* Verbs that this handler supports */
GPtrArray *verbs; /* of GWin32AppInfoShellVerb */
/* AppUserModelID for a UWP application. When this is not NULL,
* this handler launches a UWP application.
* UWP applications are launched using a COM interface and have no commandlines,
* and the verbs will reflect that too.
*/
gunichar2 *uwp_aumid;
};
struct _GWin32AppInfoShellVerb {
GObject parent_instance;
/* The verb that is used to invoke this handler. */
gunichar2 *verb_name;
/* User-friendly (localized) verb name. */
gchar *verb_displayname;
/* %TRUE if this verb is for a UWP app.
* It means that @command, @executable and @dll_function are %NULL.
*/
gboolean is_uwp;
/* shell/verb/command */
gunichar2 *command;
/* Same as @command, but in UTF-8 */
gchar *command_utf8;
/* Executable of the program (UTF-8) */
gchar *executable;
/* Executable of the program (for matching, in folded form; UTF-8) */
gchar *executable_folded;
/* Pointer to a location within @executable */
gchar *executable_basename;
/* If not NULL, then @executable and its derived fields contain the name
* of a DLL file (without the name of the function that rundll32.exe should
* invoke), and this field contains the name of the function to be invoked.
* The application is then invoked as 'rundll32.exe "dll_path",dll_function other_arguments...'.
*/
gchar *dll_function;
/* The application that is linked to this verb. */
GWin32AppInfoApplication *app;
};
struct _GWin32AppInfoFileExtension {
GObject parent_instance;
/* File extension (with leading '.') */
gunichar2 *extension;
/* File extension (with leading '.'), in UTF-8 */
gchar *extension_u8;
/* handler currently selected for this extension. Can be NULL. */
GWin32AppInfoHandler *chosen_handler;
/* Maps folded handler IDs -> to GWin32AppInfoHandlers for this extension.
* Includes the chosen handler, if any.
*/
GHashTable *handlers;
};
struct _GWin32AppInfoApplication {
GObject parent_instance;
/* Canonical name (used for key names).
* For applications tracked by id this is the root registry
* key path for the application.
* For applications tracked by executable name this is the
* basename of the executable.
* For UWP apps this is the AppUserModelID.
* For fake applications this is the full filename of the
* executable (as far as it can be inferred from a command line,
* meaning that it can also be a basename, if that's
* all that a commandline happen to give us).
*/
gunichar2 *canonical_name;
/* @canonical_name, in UTF-8 */
gchar *canonical_name_u8;
/* @canonical_name, in UTF-8, folded */
gchar *canonical_name_folded;
/* Human-readable name in English. Can be NULL */
gunichar2 *pretty_name;
/* Human-readable name in English, UTF-8. Can be NULL */
gchar *pretty_name_u8;
/* Human-readable name in user's language. Can be NULL */
gunichar2 *localized_pretty_name;
/* Human-readable name in user's language, UTF-8. Can be NULL */
gchar *localized_pretty_name_u8;
/* Description, could be in user's language. Can be NULL */
gunichar2 *description;
/* Description, could be in user's language, UTF-8. Can be NULL */
gchar *description_u8;
/* Verbs that this application supports */
GPtrArray *verbs; /* of GWin32AppInfoShellVerb */
/* Explicitly supported URLs, hashmap from map-owned gchar ptr (schema,
* UTF-8, folded) -> to a GWin32AppInfoHandler
* Schema can be used as a key in the urls hashmap.
*/
GHashTable *supported_urls;
/* Explicitly supported extensions, hashmap from map-owned gchar ptr
* (.extension, UTF-8, folded) -> to a GWin32AppInfoHandler
* Extension can be used as a key in the extensions hashmap.
*/
GHashTable *supported_exts;
/* Icon of the application (remember, handler can have its own icon too) */
GIcon *icon;
/* Set to TRUE to prevent this app from appearing in lists of apps for
* opening files. This will not prevent it from appearing in lists of apps
* just for running, or lists of apps for opening exts/urls for which this
* app reports explicit support.
*/
gboolean no_open_with;
/* Set to TRUE for applications from HKEY_CURRENT_USER.
* Give them priority over applications from HKEY_LOCAL_MACHINE, when all
* other things are equal.
*/
gboolean user_specific;
/* Set to TRUE for applications that are machine-wide defaults (i.e. default
* browser) */
gboolean default_app;
/* Set to TRUE for UWP applications */
gboolean is_uwp;
};
#define G_TYPE_WIN32_APPINFO_URL_SCHEMA (g_win32_appinfo_url_schema_get_type ())
#define G_WIN32_APPINFO_URL_SCHEMA(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_WIN32_APPINFO_URL_SCHEMA, GWin32AppInfoURLSchema))
#define G_TYPE_WIN32_APPINFO_FILE_EXTENSION (g_win32_appinfo_file_extension_get_type ())
#define G_WIN32_APPINFO_FILE_EXTENSION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_WIN32_APPINFO_FILE_EXTENSION, GWin32AppInfoFileExtension))
#define G_TYPE_WIN32_APPINFO_HANDLER (g_win32_appinfo_handler_get_type ())
#define G_WIN32_APPINFO_HANDLER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_WIN32_APPINFO_HANDLER, GWin32AppInfoHandler))
#define G_TYPE_WIN32_APPINFO_APPLICATION (g_win32_appinfo_application_get_type ())
#define G_WIN32_APPINFO_APPLICATION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_WIN32_APPINFO_APPLICATION, GWin32AppInfoApplication))
#define G_TYPE_WIN32_APPINFO_SHELL_VERB (g_win32_appinfo_shell_verb_get_type ())
#define G_WIN32_APPINFO_SHELL_VERB(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_WIN32_APPINFO_SHELL_VERB, GWin32AppInfoShellVerb))
GType g_win32_appinfo_url_schema_get_type (void) G_GNUC_CONST;
GType g_win32_appinfo_file_extension_get_type (void) G_GNUC_CONST;
GType g_win32_appinfo_shell_verb_get_type (void) G_GNUC_CONST;
GType g_win32_appinfo_handler_get_type (void) G_GNUC_CONST;
GType g_win32_appinfo_application_get_type (void) G_GNUC_CONST;
G_DEFINE_TYPE (GWin32AppInfoURLSchema, g_win32_appinfo_url_schema, G_TYPE_OBJECT)
G_DEFINE_TYPE (GWin32AppInfoFileExtension, g_win32_appinfo_file_extension, G_TYPE_OBJECT)
G_DEFINE_TYPE (GWin32AppInfoShellVerb, g_win32_appinfo_shell_verb, G_TYPE_OBJECT)
G_DEFINE_TYPE (GWin32AppInfoHandler, g_win32_appinfo_handler, G_TYPE_OBJECT)
G_DEFINE_TYPE (GWin32AppInfoApplication, g_win32_appinfo_application, G_TYPE_OBJECT)
static void
g_win32_appinfo_url_schema_dispose (GObject *object)
{
GWin32AppInfoURLSchema *url = G_WIN32_APPINFO_URL_SCHEMA (object);
g_clear_pointer (&url->schema, g_free);
g_clear_pointer (&url->schema_u8, g_free);
g_clear_pointer (&url->schema_u8_folded, g_free);
g_clear_object (&url->chosen_handler);
g_clear_pointer (&url->handlers, g_hash_table_destroy);
G_OBJECT_CLASS (g_win32_appinfo_url_schema_parent_class)->dispose (object);
}
static void
g_win32_appinfo_handler_dispose (GObject *object)
{
GWin32AppInfoHandler *handler = G_WIN32_APPINFO_HANDLER (object);
g_clear_pointer (&handler->handler_id, g_free);
g_clear_pointer (&handler->handler_id_folded, g_free);
g_clear_object (&handler->key);
g_clear_object (&handler->icon);
g_clear_pointer (&handler->verbs, g_ptr_array_unref);
g_clear_pointer (&handler->uwp_aumid, g_free);
G_OBJECT_CLASS (g_win32_appinfo_handler_parent_class)->dispose (object);
}
static void
g_win32_appinfo_file_extension_dispose (GObject *object)
{
GWin32AppInfoFileExtension *ext = G_WIN32_APPINFO_FILE_EXTENSION (object);
g_clear_pointer (&ext->extension, g_free);
g_clear_pointer (&ext->extension_u8, g_free);
g_clear_object (&ext->chosen_handler);
g_clear_pointer (&ext->handlers, g_hash_table_destroy);
G_OBJECT_CLASS (g_win32_appinfo_file_extension_parent_class)->dispose (object);
}
static void
g_win32_appinfo_shell_verb_dispose (GObject *object)
{
GWin32AppInfoShellVerb *shverb = G_WIN32_APPINFO_SHELL_VERB (object);
g_clear_pointer (&shverb->verb_name, g_free);
g_clear_pointer (&shverb->verb_displayname, g_free);
g_clear_pointer (&shverb->command, g_free);
g_clear_pointer (&shverb->command_utf8, g_free);
g_clear_pointer (&shverb->executable_folded, g_free);
g_clear_pointer (&shverb->executable, g_free);
g_clear_pointer (&shverb->dll_function, g_free);
g_clear_object (&shverb->app);
G_OBJECT_CLASS (g_win32_appinfo_shell_verb_parent_class)->dispose (object);
}
static void
g_win32_appinfo_application_dispose (GObject *object)
{
GWin32AppInfoApplication *app = G_WIN32_APPINFO_APPLICATION (object);
g_clear_pointer (&app->canonical_name_u8, g_free);
g_clear_pointer (&app->canonical_name_folded, g_free);
g_clear_pointer (&app->canonical_name, g_free);
g_clear_pointer (&app->pretty_name, g_free);
g_clear_pointer (&app->localized_pretty_name, g_free);
g_clear_pointer (&app->description, g_free);
g_clear_pointer (&app->pretty_name_u8, g_free);
g_clear_pointer (&app->localized_pretty_name_u8, g_free);
g_clear_pointer (&app->description_u8, g_free);
g_clear_pointer (&app->supported_urls, g_hash_table_destroy);
g_clear_pointer (&app->supported_exts, g_hash_table_destroy);
g_clear_object (&app->icon);
g_clear_pointer (&app->verbs, g_ptr_array_unref);
G_OBJECT_CLASS (g_win32_appinfo_application_parent_class)->dispose (object);
}
static const gchar *
g_win32_appinfo_application_get_some_name (GWin32AppInfoApplication *app)
{
if (app->localized_pretty_name_u8)
return app->localized_pretty_name_u8;
if (app->pretty_name_u8)
return app->pretty_name_u8;
return app->canonical_name_u8;
}
static void
g_win32_appinfo_url_schema_class_init (GWin32AppInfoURLSchemaClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->dispose = g_win32_appinfo_url_schema_dispose;
}
static void
g_win32_appinfo_file_extension_class_init (GWin32AppInfoFileExtensionClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->dispose = g_win32_appinfo_file_extension_dispose;
}
static void
g_win32_appinfo_shell_verb_class_init (GWin32AppInfoShellVerbClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->dispose = g_win32_appinfo_shell_verb_dispose;
}
static void
g_win32_appinfo_handler_class_init (GWin32AppInfoHandlerClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->dispose = g_win32_appinfo_handler_dispose;
}
static void
g_win32_appinfo_application_class_init (GWin32AppInfoApplicationClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->dispose = g_win32_appinfo_application_dispose;
}
static void
g_win32_appinfo_url_schema_init (GWin32AppInfoURLSchema *self)
{
self->handlers = g_hash_table_new_full (g_str_hash,
g_str_equal,
g_free,
g_object_unref);
}
static void
g_win32_appinfo_shell_verb_init (GWin32AppInfoShellVerb *self)
{
}
static void
g_win32_appinfo_file_extension_init (GWin32AppInfoFileExtension *self)
{
self->handlers = g_hash_table_new_full (g_str_hash,
g_str_equal,
g_free,
g_object_unref);
}
static void
g_win32_appinfo_handler_init (GWin32AppInfoHandler *self)
{
self->verbs = g_ptr_array_new_with_free_func (g_object_unref);
}
static void
g_win32_appinfo_application_init (GWin32AppInfoApplication *self)
{
self->supported_urls = g_hash_table_new_full (g_str_hash,
g_str_equal,
g_free,
g_object_unref);
self->supported_exts = g_hash_table_new_full (g_str_hash,
g_str_equal,
g_free,
g_object_unref);
self->verbs = g_ptr_array_new_with_free_func (g_object_unref);
}
/* The AppInfo threadpool that does asynchronous AppInfo tree rebuilds */
static GThreadPool *gio_win32_appinfo_threadpool;
/* This mutex is held by a thread that reads or writes the AppInfo tree.
* (tree object references can be obtained and later read without
* holding this mutex, since objects are practically immutable).
*/
static GMutex gio_win32_appinfo_mutex;
/* Any thread wanting to access AppInfo can wait on this condition */
static GCond gio_win32_appinfo_cond;
/* Increased to indicate that AppInfo tree does needs to be rebuilt.
* AppInfo thread checks this to see if it needs to
* do a tree re-build. If the value changes during a rebuild,
* another rebuild is triggered after that.
* Other threads check this to see if they need
* to wait for a tree re-build to finish.
*/
static gint gio_win32_appinfo_update_counter = 0;
/* Map of owned ".ext" (with '.', UTF-8, folded)
* to GWin32AppInfoFileExtension ptr
*/
static GHashTable *extensions = NULL;
/* Map of owned "schema" (without ':', UTF-8, folded)
* to GWin32AppInfoURLSchema ptr
*/
static GHashTable *urls = NULL;
/* Map of owned "appID" (UTF-8, folded) to
* a GWin32AppInfoApplication
*/
static GHashTable *apps_by_id = NULL;
/* Map of owned "app.exe" (UTF-8, folded) to
* a GWin32AppInfoApplication.
* This map and its values are separate from apps_by_id. The fact that an app
* with known ID has the same executable [base]name as an app in this map does
* not mean that they are the same application.
*/
static GHashTable *apps_by_exe = NULL;
/* Map of owned "path:\to\app.exe" (UTF-8, folded) to
* a GWin32AppInfoApplication.
* The app objects in this map are fake - they are linked to
* handlers that do not have any apps associated with them.
*/
static GHashTable *fake_apps = NULL;
/* Map of owned "handler id" (UTF-8, folded)
* to a GWin32AppInfoHandler
*/
static GHashTable *handlers = NULL;
/* Temporary (only exists while the registry is being scanned) table
* that maps GWin32RegistryKey objects (keeps a ref) to owned AUMId wchar strings.
*/
static GHashTable *uwp_handler_table = NULL;
/* Watch this whole subtree */
static GWin32RegistryKey *url_associations_key;
/* Watch this whole subtree */
static GWin32RegistryKey *file_exts_key;
/* Watch this whole subtree */
static GWin32RegistryKey *user_clients_key;
/* Watch this whole subtree */
static GWin32RegistryKey *system_clients_key;
/* Watch this key */
static GWin32RegistryKey *user_registered_apps_key;
/* Watch this key */
static GWin32RegistryKey *system_registered_apps_key;
/* Watch this whole subtree */
static GWin32RegistryKey *applications_key;
/* Watch this key */
static GWin32RegistryKey *classes_root_key;
#define URL_ASSOCIATIONS L"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\"
#define USER_CHOICE L"\\UserChoice"
#define OPEN_WITH_PROGIDS L"\\OpenWithProgids"
#define FILE_EXTS L"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\"
#define HKCR L"HKEY_CLASSES_ROOT\\"
#define HKCU L"HKEY_CURRENT_USER\\"
#define HKLM L"HKEY_LOCAL_MACHINE\\"
#define REG_PATH_MAX 256
#define REG_PATH_MAX_SIZE (REG_PATH_MAX * sizeof (gunichar2))
/* for g_wcsdup(),
* _g_win32_extract_executable(),
* _g_win32_fixup_broken_microsoft_rundll_commandline()
*/
#include "giowin32-private.c"
/* for g_win32_package_parser_enum_packages() */
#include "gwin32packageparser.h"
static void
read_handler_icon (GWin32RegistryKey *key,
GIcon **icon_out)
{
GWin32RegistryKey *icon_key;
GWin32RegistryValueType default_type;
gchar *default_value;
g_assert (icon_out);
*icon_out = NULL;
icon_key = g_win32_registry_key_get_child_w (key, L"DefaultIcon", NULL);
if (icon_key == NULL)
return;
if (g_win32_registry_key_get_value (icon_key,
NULL,
TRUE,
"",
&default_type,
(gpointer *) &default_value,
NULL,
NULL))
{
/* TODO: For UWP handlers this string is usually in @{...} form,
* see grab_registry_string() below. Right now this
* string is read as-is and the icon would silently fail to load.
* Also, right now handler icon is not used anywhere
* (only app icon is used).
*/
if (default_type == G_WIN32_REGISTRY_VALUE_STR &&
default_value[0] != '\0')
*icon_out = g_themed_icon_new (default_value);
g_clear_pointer (&default_value, g_free);
}
g_object_unref (icon_key);
}
static void
reg_verb_free (gpointer p)
{
if (p == NULL)
return;
g_free (((reg_verb *) p)->name);
g_free (((reg_verb *) p)->shellpath);
g_free (p);
}
#define is_open(x) ( \
((x)[0] == L'o' || (x)[0] == L'O') && \
((x)[1] == L'p' || (x)[1] == L'P') && \
((x)[2] == L'e' || (x)[2] == L'E') && \
((x)[3] == L'n' || (x)[3] == L'N') && \
((x)[4] == L'\0') \
)
/* default verb (if any) comes first,
* then "open", then the rest of the verbs
* are sorted alphabetically
*/
static gint
compare_verbs (gconstpointer a,
gconstpointer b,
gpointer user_data)
{
const reg_verb *ca = (const reg_verb *) a;
const reg_verb *cb = (const reg_verb *) b;
const gunichar2 *def = (const gunichar2 *) user_data;
gboolean is_open_ca;
gboolean is_open_cb;
if (def != NULL)
{
if (_wcsicmp (ca->name, def) == 0)
return -1;
else if (_wcsicmp (cb->name, def) == 0)
return 1;
}
is_open_ca = is_open (ca->name);
is_open_cb = is_open (cb->name);
if (is_open_ca && !is_open_cb)
return -1;
else if (is_open_ca && !is_open_cb)
return 1;
return _wcsicmp (ca->name, cb->name);
}
static gboolean build_registry_path (gunichar2 *output, gsize output_size, ...) G_GNUC_NULL_TERMINATED;
static gboolean build_registry_pathv (gunichar2 *output, gsize output_size, va_list components);
static GWin32RegistryKey *_g_win32_registry_key_build_and_new_w (GError **error, ...) G_GNUC_NULL_TERMINATED;
/* Called by process_verbs_commands.
* @verb is a verb name
* @command_line is the commandline of that verb
* @command_line_utf8 is the UTF-8 version of @command_line
* @verb_displayname is the prettier display name of the verb (might be NULL)
* @verb_is_preferred is TRUE if the verb is the preferred one
* @invent_new_verb_name is TRUE when the verb should be added
* even if a verb with such
* name already exists (in which case
* a new name is invented), unless
* the existing verb runs exactly the same
* commandline.
*/
typedef void (*verb_command_func) (gpointer handler_data1,
gpointer handler_data2,
const gunichar2 *verb,
const gunichar2 *command_line,
const gchar *command_line_utf8,
const gchar *verb_displayname,
gboolean verb_is_preferred,
gboolean invent_new_verb_name);
static gunichar2 * decide_which_id_to_use (const gunichar2 *program_id,
GWin32RegistryKey **return_key,
gchar **return_handler_id_u8,
gchar **return_handler_id_u8_folded,
gunichar2 **return_uwp_aumid);
static GWin32AppInfoURLSchema * get_schema_object (const gunichar2 *schema,
const gchar *schema_u8,
const gchar *schema_u8_folded);
static GWin32AppInfoHandler * get_handler_object (const gchar *handler_id_u8_folded,
GWin32RegistryKey *handler_key,
const gunichar2 *handler_id,
const gunichar2 *uwp_aumid);
static GWin32AppInfoFileExtension *get_ext_object (const gunichar2 *ext,
const gchar *ext_u8,
const gchar *ext_u8_folded);
static void process_verbs_commands (GList *verbs,
const reg_verb *preferred_verb,
const gunichar2 *path_to_progid,
const gunichar2 *progid,
gboolean autoprefer_first_verb,
verb_command_func handler,
gpointer handler_data1,
gpointer handler_data2);
static void handler_add_verb (gpointer handler_data1,
gpointer handler_data2,
const gunichar2 *verb,
const gunichar2 *command_line,
const gchar *command_line_utf8,
const gchar *verb_displayname,
gboolean verb_is_preferred,
gboolean invent_new_verb_name);
static void process_uwp_verbs (GList *verbs,
const reg_verb *preferred_verb,
const gunichar2 *path_to_progid,
const gunichar2 *progid,
gboolean autoprefer_first_verb,
GWin32AppInfoHandler *handler_rec,
GWin32AppInfoApplication *app);
static void uwp_handler_add_verb (GWin32AppInfoHandler *handler_rec,
GWin32AppInfoApplication *app,
const gunichar2 *verb,
const gchar *verb_displayname,
gboolean verb_is_preferred);
/* output_size is in *bytes*, not gunichar2s! */
static gboolean
build_registry_path (gunichar2 *output, gsize output_size, ...)
{
va_list ap;
gboolean result;
va_start (ap, output_size);
result = build_registry_pathv (output, output_size, ap);
va_end (ap);
return result;
}
/* output_size is in *bytes*, not gunichar2s! */
static gboolean
build_registry_pathv (gunichar2 *output, gsize output_size, va_list components)
{
va_list lentest;
gunichar2 *p;
gunichar2 *component;
gsize length;
if (output == NULL)
return FALSE;
va_copy (lentest, components);
for (length = 0, component = va_arg (lentest, gunichar2 *);
component != NULL;
component = va_arg (lentest, gunichar2 *))
{
length += wcslen (component);
}
va_end (lentest);
if ((length >= REG_PATH_MAX_SIZE) ||
(length * sizeof (gunichar2) >= output_size))
return FALSE;
output[0] = L'\0';
for (p = output, component = va_arg (components, gunichar2 *);
component != NULL;
component = va_arg (components, gunichar2 *))
{
length = wcslen (component);
wcscat (p, component);
p += length;
}
return TRUE;
}
static GWin32RegistryKey *
_g_win32_registry_key_build_and_new_w (GError **error, ...)
{
va_list ap;
gunichar2 key_path[REG_PATH_MAX_SIZE + 1];
GWin32RegistryKey *key;
va_start (ap, error);
key = NULL;
if (build_registry_pathv (key_path, sizeof (key_path), ap))
key = g_win32_registry_key_new_w (key_path, error);
va_end (ap);
return key;
}
/* Gets the list of shell verbs (a GList of reg_verb, put into @verbs)
* from the @program_id_key.
* If one of the verbs should be preferred,
* a pointer to this verb (in the GList) will be
* put into @preferred_verb.
* Does not automatically assume that the first verb
* is preferred (when no other preferences exist).
* @verbname_prefix is prefixed to the name of the verb
* (this is used for subcommands) and is initially an
* empty string.
* @verbshell_prefix is the subkey of @program_id_key
* that contains the verbs. It is "Shell" initially,
* but grows with recursive invocations (for subcommands).
* @is_uwp points to a boolean, which
* indicates whether the function is being called for a UWP app.
* It might be switched from %TRUE to %FALSE on return,
* if the application turns out to not to be UWP on closer inspection.
* If the application is already known not to be UWP before the
* call, this pointer can be %NULL instead.
* Returns TRUE on success, FALSE on failure.
*/
static gboolean
get_verbs (GWin32RegistryKey *program_id_key,
const reg_verb **preferred_verb,
GList **verbs,
const gunichar2 *verbname_prefix,
const gunichar2 *verbshell_prefix,
gboolean *is_uwp)
{
GWin32RegistrySubkeyIter iter;
GWin32RegistryKey *key;
GWin32RegistryValueType val_type;
gunichar2 *default_verb;
gsize verbshell_prefix_len;
gsize verbname_prefix_len;
GList *i;
g_assert (program_id_key && verbs && preferred_verb);
*verbs = NULL;
*preferred_verb = NULL;
key = g_win32_registry_key_get_child_w (program_id_key,
verbshell_prefix,
NULL);
if (key == NULL)
return FALSE;
if (!g_win32_registry_subkey_iter_init (&iter, key, NULL))
{
g_object_unref (key);
return FALSE;
}
verbshell_prefix_len = g_utf16_len (verbshell_prefix);
verbname_prefix_len = g_utf16_len (verbname_prefix);
while (g_win32_registry_subkey_iter_next (&iter, TRUE, NULL))
{
const gunichar2 *name;
gsize name_len;
GWin32RegistryKey *subkey;
gboolean has_subcommands;
const reg_verb *tmp;
GWin32RegistryValueType subc_type;
reg_verb *rverb;
const gunichar2 *shell = L"Shell";
const gsize shell_len = g_utf16_len (shell);
if (!g_win32_registry_subkey_iter_get_name_w (&iter, &name, &name_len, NULL))
continue;
subkey = g_win32_registry_key_get_child_w (key,
name,
NULL);
/* We may not have the required access rights to open the child key */
if (subkey == NULL)
continue;
/* The key we're looking at is "<some_root>/Shell/<this_key>",
* where "Shell" is verbshell_prefix.
* If it has a value named 'Subcommands' (doesn't matter what its data is),
* it means that this key has its own Shell subkey, the subkeys
* of which are shell commands (i.e. <some_root>/Shell/<this_key>/Shell/<some_other_keys>).
* To handle that, create new, extended nameprefix and shellprefix,
* and call the function recursively.
* name prefix "" -> "<this_key_name>\\"
* shell prefix "Shell" -> "Shell\\<this_key_name>\\Shell"
* The root, program_id_key, remains the same in all invocations.
* Essentially, we're flattening the command tree into a list.
*/
has_subcommands = FALSE;
if ((is_uwp == NULL || !(*is_uwp)) && /* Assume UWP apps don't have subcommands */
g_win32_registry_key_get_value_w (subkey,
NULL,
TRUE,
L"Subcommands",
&subc_type,
NULL,
NULL,
NULL) &&
subc_type == G_WIN32_REGISTRY_VALUE_STR)
{
gboolean dummy = FALSE;
gunichar2 *new_nameprefix = g_new (gunichar2, verbname_prefix_len + name_len + 1 + 1);
gunichar2 *new_shellprefix = g_new (gunichar2, verbshell_prefix_len + 1 + name_len + 1 + shell_len + 1);
memcpy (&new_shellprefix[0], verbshell_prefix, verbshell_prefix_len * sizeof (gunichar2));
new_shellprefix[verbshell_prefix_len] = L'\\';
memcpy (&new_shellprefix[verbshell_prefix_len + 1], name, name_len * sizeof (gunichar2));
new_shellprefix[verbshell_prefix_len + 1 + name_len] = L'\\';
memcpy (&new_shellprefix[verbshell_prefix_len + 1 + name_len + 1], shell, shell_len * sizeof (gunichar2));
new_shellprefix[verbshell_prefix_len + 1 + name_len + 1 + shell_len] = 0;
memcpy (&new_nameprefix[0], verbname_prefix, verbname_prefix_len * sizeof (gunichar2));
memcpy (&new_nameprefix[verbname_prefix_len], name, (name_len) * sizeof (gunichar2));
new_nameprefix[verbname_prefix_len + name_len] = L'\\';
new_nameprefix[verbname_prefix_len + name_len + 1] = 0;
has_subcommands = get_verbs (program_id_key, &tmp, verbs, new_nameprefix, new_shellprefix, &dummy);
g_free (new_shellprefix);
g_free (new_nameprefix);
}
/* Presence of subcommands means that this key itself is not a command-key */
if (has_subcommands)
{
g_clear_object (&subkey);
continue;
}
if (is_uwp != NULL && *is_uwp &&
!g_win32_registry_key_get_value_w (subkey,
NULL,
TRUE,
L"ActivatableClassId",
&subc_type,
NULL,
NULL,
NULL))
{
/* We expected a UWP app, but it lacks ActivatableClassId
* on a verb, which means that it does not behave like
* a UWP app should (msedge being an example - it's UWP,
* but has its own launchable exe file and a simple ID),
* so we have to treat it like a normal app.
*/
*is_uwp = FALSE;
}
g_clear_object (&subkey);
/* We don't look at the command sub-key and its value (the actual command line) here.
* We save the registry path instead, and use it later in process_verbs_commands().
* The name of the verb is also saved.
* verbname_prefix is prefixed to the verb name (it's either an empty string
* or already ends with a '\\', so no extra separators needed).
* verbshell_prefix is prefixed to the verb key path (this one needs a separator,
* because it never has one - all verbshell prefixes end with "Shell", not "Shell\\")
*/
rverb = g_new0 (reg_verb, 1);
rverb->name = g_new (gunichar2, verbname_prefix_len + name_len + 1);
memcpy (&rverb->name[0], verbname_prefix, verbname_prefix_len * sizeof (gunichar2));
memcpy (&rverb->name[verbname_prefix_len], name, name_len * sizeof (gunichar2));
rverb->name[verbname_prefix_len + name_len] = 0;
rverb->shellpath = g_new (gunichar2, verbshell_prefix_len + 1 + name_len + 1);
memcpy (&rverb->shellpath[0], verbshell_prefix, verbshell_prefix_len * sizeof (gunichar2));
memcpy (&rverb->shellpath[verbshell_prefix_len], L"\\", sizeof (gunichar2));
memcpy (&rverb->shellpath[verbshell_prefix_len + 1], name, name_len * sizeof (gunichar2));
rverb->shellpath[verbshell_prefix_len + 1 + name_len] = 0;
*verbs = g_list_append (*verbs, rverb);
}
g_win32_registry_subkey_iter_clear (&iter);
if (*verbs == NULL)
{
g_object_unref (key);
return FALSE;
}
default_verb = NULL;
if (g_win32_registry_key_get_value_w (key,
NULL,
TRUE,
L"",
&val_type,
(void **) &default_verb,
NULL,
NULL) &&
(val_type != G_WIN32_REGISTRY_VALUE_STR ||
g_utf16_len (default_verb) <= 0))
g_clear_pointer (&default_verb, g_free);
g_object_unref (key);
/* Only sort at the top level */
if (verbname_prefix[0] == 0)
{
*verbs = g_list_sort_with_data (*verbs, compare_verbs, default_verb);
for (i = *verbs; default_verb && *preferred_verb == NULL && i; i = i->next)
if (_wcsicmp (default_verb, ((const reg_verb *) i->data)->name) == 0)
*preferred_verb = (const reg_verb *) i->data;
}
g_clear_pointer (&default_verb, g_free);
return TRUE;
}
/* Grabs a URL association (from HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\
* or from an application with Capabilities, or just a schema subkey in HKCR).
* @program_id is a ProgID of the handler for the URL.
* @schema is the schema for the URL.
* @schema_u8 and @schema_u8_folded are UTF-8 and folded UTF-8
* respectively.
* @app is the app to which the URL handler belongs (can be NULL).
* @is_user_choice is TRUE if this association is clearly preferred
*/
static void
get_url_association (const gunichar2 *program_id,
const gunichar2 *schema,
const gchar *schema_u8,
const gchar *schema_u8_folded,
GWin32AppInfoApplication *app,
gboolean is_user_choice)
{
GWin32AppInfoURLSchema *schema_rec;
GWin32AppInfoHandler *handler_rec;
gunichar2 *handler_id;
GList *verbs;
const reg_verb *preferred_verb;
gchar *handler_id_u8;
gchar *handler_id_u8_folded;
gunichar2 *uwp_aumid;
gboolean is_uwp;
GWin32RegistryKey *handler_key;
if ((handler_id = decide_which_id_to_use (program_id,
&handler_key,
&handler_id_u8,
&handler_id_u8_folded,
&uwp_aumid)) == NULL)
return;
is_uwp = uwp_aumid != NULL;
if (!get_verbs (handler_key, &preferred_verb, &verbs, L"", L"Shell", &is_uwp))
{
g_clear_pointer (&handler_id, g_free);
g_clear_pointer (&handler_id_u8, g_free);
g_clear_pointer (&handler_id_u8_folded, g_free);
g_clear_object (&handler_key);
g_clear_pointer (&uwp_aumid, g_free);
return;
}
if (!is_uwp && uwp_aumid != NULL)
g_clear_pointer (&uwp_aumid, g_free);
schema_rec = get_schema_object (schema,
schema_u8,
schema_u8_folded);
handler_rec = get_handler_object (handler_id_u8_folded,
handler_key,
handler_id,
uwp_aumid);
if (is_user_choice || schema_rec->chosen_handler == NULL)
g_set_object (&schema_rec->chosen_handler, handler_rec);
g_hash_table_insert (schema_rec->handlers,
g_strdup (handler_id_u8_folded),
g_object_ref (handler_rec));
g_clear_object (&handler_key);
if (app)
g_hash_table_insert (app->supported_urls,
g_strdup (schema_rec->schema_u8_folded),
g_object_ref (handler_rec));
if (uwp_aumid == NULL)
process_verbs_commands (g_steal_pointer (&verbs),
preferred_verb,
HKCR,
handler_id,
TRUE,
handler_add_verb,
handler_rec,
app);
else
process_uwp_verbs (g_steal_pointer (&verbs),
preferred_verb,
HKCR,
handler_id,
TRUE,
handler_rec,
app);
g_clear_pointer (&handler_id_u8, g_free);
g_clear_pointer (&handler_id_u8_folded, g_free);
g_clear_pointer (&handler_id, g_free);
g_clear_pointer (&uwp_aumid, g_free);
}
/* Grabs a file extension association (from HKCR\.ext or similar).
* @program_id is a ProgID of the handler for the extension.
* @file_extension is the extension (with the leading '.')
* @app is the app to which the extension handler belongs (can be NULL).
* @is_user_choice is TRUE if this is clearly the preferred association
*/
static void
get_file_ext (const gunichar2 *program_id,
const gunichar2 *file_extension,
GWin32AppInfoApplication *app,
gboolean is_user_choice)
{
GWin32AppInfoHandler *handler_rec;
gunichar2 *handler_id;
const reg_verb *preferred_verb;
GList *verbs;
gchar *handler_id_u8;
gchar *handler_id_u8_folded;
gunichar2 *uwp_aumid;
gboolean is_uwp;
GWin32RegistryKey *handler_key;
GWin32AppInfoFileExtension *file_extn;
gchar *file_extension_u8;
gchar *file_extension_u8_folded;
if ((handler_id = decide_which_id_to_use (program_id,
&handler_key,
&handler_id_u8,
&handler_id_u8_folded,
&uwp_aumid)) == NULL)
return;
if (!g_utf16_to_utf8_and_fold (file_extension,
-1,
&file_extension_u8,
&file_extension_u8_folded))
{
g_clear_pointer (&handler_id, g_free);
g_clear_pointer (&handler_id_u8, g_free);
g_clear_pointer (&handler_id_u8_folded, g_free);
g_clear_pointer (&uwp_aumid, g_free);
g_clear_object (&handler_key);
return;
}
is_uwp = uwp_aumid != NULL;
if (!get_verbs (handler_key, &preferred_verb, &verbs, L"", L"Shell", &is_uwp))
{
g_clear_pointer (&handler_id, g_free);
g_clear_pointer (&handler_id_u8, g_free);
g_clear_pointer (&handler_id_u8_folded, g_free);
g_clear_object (&handler_key);
g_clear_pointer (&file_extension_u8, g_free);
g_clear_pointer (&file_extension_u8_folded, g_free);
g_clear_pointer (&uwp_aumid, g_free);
return;
}
if (!is_uwp && uwp_aumid != NULL)
g_clear_pointer (&uwp_aumid, g_free);
file_extn = get_ext_object (file_extension, file_extension_u8, file_extension_u8_folded);
handler_rec = get_handler_object (handler_id_u8_folded,
handler_key,
handler_id,
uwp_aumid);
if (is_user_choice || file_extn->chosen_handler == NULL)
g_set_object (&file_extn->chosen_handler, handler_rec);
g_hash_table_insert (file_extn->handlers,
g_strdup (handler_id_u8_folded),
g_object_ref (handler_rec));
if (app)
g_hash_table_insert (app->supported_exts,
g_strdup (file_extension_u8_folded),
g_object_ref (handler_rec));
g_clear_pointer (&file_extension_u8, g_free);
g_clear_pointer (&file_extension_u8_folded, g_free);
g_clear_object (&handler_key);
if (uwp_aumid == NULL)
process_verbs_commands (g_steal_pointer (&verbs),
preferred_verb,
HKCR,
handler_id,
TRUE,
handler_add_verb,
handler_rec,
app);
else
process_uwp_verbs (g_steal_pointer (&verbs),
preferred_verb,
HKCR,
handler_id,
TRUE,
handler_rec,
app);
g_clear_pointer (&handler_id, g_free);
g_clear_pointer (&handler_id_u8, g_free);
g_clear_pointer (&handler_id_u8_folded, g_free);
g_clear_pointer (&uwp_aumid, g_free);
}
/* Returns either a @program_id or the string from
* the default value of the program_id key (which is a name
* of a proxy class), or NULL.
* Does not check that proxy represents a valid
* record, just checks that it exists.
* Can return the class key (HKCR/program_id or HKCR/proxy_id).
* Can convert returned value to UTF-8 and fold it.
*/
static gunichar2 *
decide_which_id_to_use (const gunichar2 *program_id,
GWin32RegistryKey **return_key,
gchar **return_handler_id_u8,
gchar **return_handler_id_u8_folded,
gunichar2 **return_uwp_aumid)
{
GWin32RegistryKey *key;
GWin32RegistryKey *uwp_key;
GWin32RegistryValueType val_type;
gunichar2 *proxy_id;
gunichar2 *return_id;
gunichar2 *uwp_aumid;
gboolean got_value;
gchar *handler_id_u8;
gchar *handler_id_u8_folded;
g_assert (program_id);
if (return_key)
*return_key = NULL;
if (return_uwp_aumid)
*return_uwp_aumid = NULL;
key = g_win32_registry_key_get_child_w (classes_root_key, program_id, NULL);
if (key == NULL)
return NULL;
/* Check for UWP first */
uwp_aumid = NULL;
uwp_key = g_win32_registry_key_get_child_w (key, L"Application", NULL);
if (uwp_key != NULL)
{
got_value = g_win32_registry_key_get_value_w (uwp_key,
NULL,
TRUE,
L"AppUserModelID",
&val_type,
(void **) &uwp_aumid,
NULL,
NULL);
if (got_value && val_type != G_WIN32_REGISTRY_VALUE_STR)
g_clear_pointer (&uwp_aumid, g_free);
/* Other values in the Application key contain useful information
* (description, name, icon), but it's inconvenient to read
* it here (we don't have an app object *yet*). Store the key
* in a table instead, and look at it later.
*/
if (uwp_aumid == NULL)
g_debug ("ProgramID %S looks like a UWP application, but isn't",
program_id);
else
g_hash_table_insert (uwp_handler_table, g_object_ref (uwp_key), g_wcsdup (uwp_aumid, -1));
g_object_unref (uwp_key);
}
/* Then check for proxy */
proxy_id = NULL;
if (uwp_aumid == NULL)
{
got_value = g_win32_registry_key_get_value_w (key,
NULL,
TRUE,
L"",
&val_type,
(void **) &proxy_id,
NULL,
NULL);
if (got_value && val_type != G_WIN32_REGISTRY_VALUE_STR)
g_clear_pointer (&proxy_id, g_free);
}
return_id = NULL;
if (proxy_id)
{
GWin32RegistryKey *proxy_key;
proxy_key = g_win32_registry_key_get_child_w (classes_root_key, proxy_id, NULL);
if (proxy_key)
{
if (return_key)
*return_key = g_steal_pointer (&proxy_key);
g_clear_object (&proxy_key);
return_id = g_steal_pointer (&proxy_id);
}
g_clear_pointer (&proxy_id, g_free);
}
if ((return_handler_id_u8 ||
return_handler_id_u8_folded) &&
!g_utf16_to_utf8_and_fold (return_id == NULL ? program_id : return_id,
-1,
&handler_id_u8,
&handler_id_u8_folded))
{
g_clear_object (&key);
if (return_key)
g_clear_object (return_key);
g_clear_pointer (&return_id, g_free);
return NULL;
}
if (return_handler_id_u8)
*return_handler_id_u8 = g_steal_pointer (&handler_id_u8);
g_clear_pointer (&handler_id_u8, g_free);
if (return_handler_id_u8_folded)
*return_handler_id_u8_folded = g_steal_pointer (&handler_id_u8_folded);
g_clear_pointer (&handler_id_u8_folded, g_free);
if (return_uwp_aumid)
*return_uwp_aumid = g_steal_pointer (&uwp_aumid);
g_clear_pointer (&uwp_aumid, g_free);
if (return_id == NULL && return_key)
*return_key = g_steal_pointer (&key);
g_clear_object (&key);
if (return_id == NULL)
return g_wcsdup (program_id, -1);
return return_id;
}
/* Grabs the command for each verb from @verbs,
* and invokes @handler for it. Consumes @verbs.
* @path_to_progid and @progid are concatenated to
* produce a path to the key where Shell/verb/command
* subkeys are looked up.
* @preferred_verb, if not NULL, will be used to inform
* the @handler that a verb is preferred.
* @autoprefer_first_verb will automatically make the first
* verb to be preferred, if @preferred_verb is NULL.
* @handler_data1 and @handler_data2 are passed to @handler as-is.
*/
static void
process_verbs_commands (GList *verbs,
const reg_verb *preferred_verb,
const gunichar2 *path_to_progid,
const gunichar2 *progid,
gboolean autoprefer_first_verb,
verb_command_func handler,
gpointer handler_data1,
gpointer handler_data2)
{
GList *i;
gboolean got_value;
g_assert (handler != NULL);
g_assert (verbs != NULL);
g_assert (progid != NULL);
for (i = verbs; i; i = i->next)
{
const reg_verb *verb = (const reg_verb *) i->data;
GWin32RegistryKey *key;
GWin32RegistryKey *verb_key;
gunichar2 *command_value;
gchar *command_value_utf8;
GWin32RegistryValueType val_type;
gunichar2 *verb_displayname;
gchar *verb_displayname_u8;
key = _g_win32_registry_key_build_and_new_w (NULL, path_to_progid, progid,
L"\\", verb->shellpath, L"\\command", NULL);
if (key == NULL)
{
g_debug ("%S%S\\shell\\%S does not have a \"command\" subkey",
path_to_progid, progid, verb->shellpath);
continue;
}
command_value = NULL;
got_value = g_win32_registry_key_get_value_w (key,
NULL,
TRUE,
L"",
&val_type,
(void **) &command_value,
NULL,
NULL);
g_clear_object (&key);
if (!got_value ||
val_type != G_WIN32_REGISTRY_VALUE_STR ||
(command_value_utf8 = g_utf16_to_utf8 (command_value,
-1,
NULL,
NULL,
NULL)) == NULL)
{
g_clear_pointer (&command_value, g_free);
continue;
}
verb_displayname = NULL;
verb_displayname_u8 = NULL;
verb_key = _g_win32_registry_key_build_and_new_w (NULL, path_to_progid, progid,
L"\\", verb->shellpath, NULL);
if (verb_key)
{
gsize verb_displayname_len;
got_value = g_win32_registry_key_get_value_w (verb_key,
g_win32_registry_get_os_dirs_w (),
TRUE,
L"MUIVerb",
&val_type,
(void **) &verb_displayname,
&verb_displayname_len,
NULL);
if (got_value &&
val_type == G_WIN32_REGISTRY_VALUE_STR &&
verb_displayname_len > sizeof (gunichar2))
verb_displayname_u8 = g_utf16_to_utf8 (verb_displayname, -1, NULL, NULL, NULL);
g_clear_pointer (&verb_displayname, g_free);
if (verb_displayname_u8 == NULL)
{
got_value = g_win32_registry_key_get_value_w (verb_key,
NULL,
TRUE,
L"",
&val_type,
(void **) &verb_displayname,
&verb_displayname_len,
NULL);
if (got_value &&
val_type == G_WIN32_REGISTRY_VALUE_STR &&
verb_displayname_len > sizeof (gunichar2))
verb_displayname_u8 = g_utf16_to_utf8 (verb_displayname, -1, NULL, NULL, NULL);
}
g_clear_pointer (&verb_displayname, g_free);
g_clear_object (&verb_key);
}
handler (handler_data1, handler_data2, verb->name, command_value, command_value_utf8,
verb_displayname_u8,
(preferred_verb && _wcsicmp (verb->name, preferred_verb->name) == 0) ||
(!preferred_verb && autoprefer_first_verb && i == verbs),
FALSE);
g_clear_pointer (&command_value, g_free);
g_clear_pointer (&command_value_utf8, g_free);
g_clear_pointer (&verb_displayname_u8, g_free);
}
g_list_free_full (verbs, reg_verb_free);
}
static void
process_uwp_verbs (GList *verbs,
const reg_verb *preferred_verb,
const gunichar2 *path_to_progid,
const gunichar2 *progid,
gboolean autoprefer_first_verb,
GWin32AppInfoHandler *handler_rec,
GWin32AppInfoApplication *app)
{
GList *i;
g_assert (verbs != NULL);
for (i = verbs; i; i = i->next)
{
const reg_verb *verb = (const reg_verb *) i->data;
GWin32RegistryKey *key;
gboolean got_value;
GWin32RegistryValueType val_type;
gunichar2 *acid;
gsize acid_len;
key = _g_win32_registry_key_build_and_new_w (NULL, path_to_progid, progid,
L"\\", verb->shellpath, NULL);
if (key == NULL)
{
g_debug ("%S%S\\%S does not exist",
path_to_progid, progid, verb->shellpath);
continue;
}
acid = NULL;
got_value = g_win32_registry_key_get_value_w (key,
g_win32_registry_get_os_dirs_w (),
TRUE,
L"ActivatableClassId",
&val_type,
(void **) &acid,
&acid_len,
NULL);
if (got_value &&
val_type == G_WIN32_REGISTRY_VALUE_STR &&
acid_len > sizeof (gunichar2))
{
/* TODO: default value of a shell subkey, if not empty,
* migh contain something like @{Some.Identifier_1234.456.678.789_some_words?ms-resource://Arbitrary.Path/Pointing/Somewhere}
* and it might be possible to turn it into a nice displayname.
*/
uwp_handler_add_verb (handler_rec,
app,
verb->name,
NULL,
(preferred_verb && _wcsicmp (verb->name, preferred_verb->name) == 0) ||
(!preferred_verb && autoprefer_first_verb && i == verbs));
}
else
{
g_debug ("%S%S\\%S does not have an ActivatableClassId string value",
path_to_progid, progid, verb->shellpath);
}
g_clear_pointer (&acid, g_free);
g_clear_object (&key);
}
g_list_free_full (verbs, reg_verb_free);
}
/* Looks up a schema object identified by
* @schema_u8_folded in the urls hash table.
* If such object doesn't exist,
* creates it and puts it into the urls hash table.
* Returns the object.
*/
static GWin32AppInfoURLSchema *
get_schema_object (const gunichar2 *schema,
const gchar *schema_u8,
const gchar *schema_u8_folded)
{
GWin32AppInfoURLSchema *schema_rec;
schema_rec = g_hash_table_lookup (urls, schema_u8_folded);
if (schema_rec != NULL)
return schema_rec;
schema_rec = g_object_new (G_TYPE_WIN32_APPINFO_URL_SCHEMA, NULL);
schema_rec->schema = g_wcsdup (schema, -1);
schema_rec->schema_u8 = g_strdup (schema_u8);
schema_rec->schema_u8_folded = g_strdup (schema_u8_folded);
g_hash_table_insert (urls, g_strdup (schema_rec->schema_u8_folded), schema_rec);
return schema_rec;
}
/* Looks up a handler object identified by
* @handler_id_u8_folded in the handlers hash table.
* If such object doesn't exist,
* creates it and puts it into the handlers hash table.
* Returns the object.
*/
static GWin32AppInfoHandler *
get_handler_object (const gchar *handler_id_u8_folded,
GWin32RegistryKey *handler_key,
const gunichar2 *handler_id,
const gunichar2 *uwp_aumid)
{
GWin32AppInfoHandler *handler_rec;
handler_rec = g_hash_table_lookup (handlers, handler_id_u8_folded);
if (handler_rec != NULL)
return handler_rec;
handler_rec = g_object_new (G_TYPE_WIN32_APPINFO_HANDLER, NULL);
if (handler_key)
handler_rec->key = g_object_ref (handler_key);
handler_rec->handler_id = g_wcsdup (handler_id, -1);
handler_rec->handler_id_folded = g_strdup (handler_id_u8_folded);
if (uwp_aumid)
handler_rec->uwp_aumid = g_wcsdup (uwp_aumid, -1);
if (handler_key)
read_handler_icon (handler_key, &handler_rec->icon);
g_hash_table_insert (handlers, g_strdup (handler_id_u8_folded), handler_rec);
return handler_rec;
}
static void
handler_add_verb (gpointer handler_data1,
gpointer handler_data2,
const gunichar2 *verb,
const gunichar2 *command_line,
const gchar *command_line_utf8,
const gchar *verb_displayname,
gboolean verb_is_preferred,
gboolean invent_new_verb_name)
{
GWin32AppInfoHandler *handler_rec = (GWin32AppInfoHandler *) handler_data1;
GWin32AppInfoApplication *app_rec = (GWin32AppInfoApplication *) handler_data2;
GWin32AppInfoShellVerb *shverb = NULL;
_verb_lookup (handler_rec->verbs, verb, &shverb);
if (shverb != NULL)
return;
shverb = g_object_new (G_TYPE_WIN32_APPINFO_SHELL_VERB, NULL);
shverb->verb_name = g_wcsdup (verb, -1);
shverb->verb_displayname = g_strdup (verb_displayname);
shverb->command = g_wcsdup (command_line, -1);
shverb->command_utf8 = g_strdup (command_line_utf8);
shverb->is_uwp = FALSE; /* This function is for non-UWP verbs only */
if (app_rec)
shverb->app = g_object_ref (app_rec);
_g_win32_extract_executable (shverb->command,
&shverb->executable,
&shverb->executable_basename,
&shverb->executable_folded,
NULL,
&shverb->dll_function);
if (shverb->dll_function != NULL)
_g_win32_fixup_broken_microsoft_rundll_commandline (shverb->command);
if (!verb_is_preferred)
g_ptr_array_add (handler_rec->verbs, shverb);
else
g_ptr_array_insert (handler_rec->verbs, 0, shverb);
}
/* Tries to generate a new name for a verb that looks
* like "verb (%x)", where %x is an integer in range of [0;255).
* On success puts new verb (and new verb displayname) into
* @new_verb and @new_displayname and return TRUE.
* On failure puts NULL into both and returns FALSE.
*/
static gboolean
generate_new_verb_name (GPtrArray *verbs,
const gunichar2 *verb,
const gchar *verb_displayname,
gunichar2 **new_verb,
gchar **new_displayname)
{
gsize counter;
GWin32AppInfoShellVerb *shverb = NULL;
gsize orig_len = g_utf16_len (verb);
gsize new_verb_name_len = orig_len + strlen (" ()") + 2 + 1;
gunichar2 *new_verb_name = g_new (gunichar2, new_verb_name_len);
*new_verb = NULL;
*new_displayname = NULL;
memcpy (new_verb_name, verb, orig_len * sizeof (gunichar2));
for (counter = 0; counter < 255; counter++)
{
_snwprintf (&new_verb_name[orig_len], new_verb_name_len, L" (%zx)", counter);
_verb_lookup (verbs, new_verb_name, &shverb);
if (shverb == NULL)
{
*new_verb = new_verb_name;
if (verb_displayname != NULL)
*new_displayname = g_strdup_printf ("%s (%zx)", verb_displayname, counter);
return TRUE;
}
}
return FALSE;
}
static void
app_add_verb (gpointer handler_data1,
gpointer handler_data2,
const gunichar2 *verb,
const gunichar2 *command_line,
const gchar *command_line_utf8,
const gchar *verb_displayname,
gboolean verb_is_preferred,
gboolean invent_new_verb_name)
{
gunichar2 *new_verb = NULL;
gchar *new_displayname = NULL;
GWin32AppInfoApplication *app_rec = (GWin32AppInfoApplication *) handler_data2;
GWin32AppInfoShellVerb *shverb = NULL;
_verb_lookup (app_rec->verbs, verb, &shverb);
/* Special logic for fake apps - do our best to
* collate all possible verbs in the app,
* including the verbs that have the same name but
* different commandlines, in which case a new
* verb name has to be invented.
*/
if (shverb != NULL)
{
gsize vi;
if (!invent_new_verb_name)
return;
for (vi = 0; vi < app_rec->verbs->len; vi++)
{
GWin32AppInfoShellVerb *app_verb;
app_verb = _verb_idx (app_rec->verbs, vi);
if (_wcsicmp (command_line, app_verb->command) == 0)
break;
}
if (vi < app_rec->verbs->len ||
!generate_new_verb_name (app_rec->verbs,
verb,
verb_displayname,
&new_verb,
&new_displayname))
return;
}
shverb = g_object_new (G_TYPE_WIN32_APPINFO_SHELL_VERB, NULL);
if (new_verb == NULL)
shverb->verb_name = g_wcsdup (verb, -1);
else
shverb->verb_name = g_steal_pointer (&new_verb);
if (new_displayname == NULL)
shverb->verb_displayname = g_strdup (verb_displayname);
else
shverb->verb_displayname = g_steal_pointer (&new_displayname);
shverb->command = g_wcsdup (command_line, -1);
shverb->command_utf8 = g_strdup (command_line_utf8);
shverb->app = g_object_ref (app_rec);
_g_win32_extract_executable (shverb->command,
&shverb->executable,
&shverb->executable_basename,
&shverb->executable_folded,
NULL,
&shverb->dll_function);
if (shverb->dll_function != NULL)
_g_win32_fixup_broken_microsoft_rundll_commandline (shverb->command);
if (!verb_is_preferred)
g_ptr_array_add (app_rec->verbs, shverb);
else
g_ptr_array_insert (app_rec->verbs, 0, shverb);
}
static void
uwp_app_add_verb (GWin32AppInfoApplication *app_rec,
const gunichar2 *verb,
const gchar *verb_displayname)
{
GWin32AppInfoShellVerb *shverb = NULL;
_verb_lookup (app_rec->verbs, verb, &shverb);
if (shverb != NULL)
return;
shverb = g_object_new (G_TYPE_WIN32_APPINFO_SHELL_VERB, NULL);
shverb->verb_name = g_wcsdup (verb, -1);
shverb->app = g_object_ref (app_rec);
shverb->verb_displayname = g_strdup (verb_displayname);
shverb->is_uwp = TRUE;
/* Strictly speaking, this is unnecessary, but
* let's make it clear that UWP verbs have no
* commands and executables.
*/
shverb->command = NULL;
shverb->command_utf8 = NULL;
shverb->executable = NULL;
shverb->executable_basename = NULL;
shverb->executable_folded = NULL;
shverb->dll_function = NULL;
g_ptr_array_add (app_rec->verbs, shverb);
}
static void
uwp_handler_add_verb (GWin32AppInfoHandler *handler_rec,
GWin32AppInfoApplication *app,
const gunichar2 *verb,
const gchar *verb_displayname,
gboolean verb_is_preferred)
{
GWin32AppInfoShellVerb *shverb = NULL;
_verb_lookup (handler_rec->verbs, verb, &shverb);
if (shverb != NULL)
return;
shverb = g_object_new (G_TYPE_WIN32_APPINFO_SHELL_VERB, NULL);
shverb->verb_name = g_wcsdup (verb, -1);
shverb->verb_displayname = g_strdup (verb_displayname);
shverb->is_uwp = TRUE;
if (app)
shverb->app = g_object_ref (app);
shverb->command = NULL;
shverb->command_utf8 = NULL;
shverb->executable = NULL;
shverb->executable_basename = NULL;
shverb->executable_folded = NULL;
shverb->dll_function = NULL;
if (!verb_is_preferred)
g_ptr_array_add (handler_rec->verbs, shverb);
else
g_ptr_array_insert (handler_rec->verbs, 0, shverb);
}
/* Looks up a file extension object identified by
* @ext_u8_folded in the extensions hash table.
* If such object doesn't exist,
* creates it and puts it into the extensions hash table.
* Returns the object.
*/
static GWin32AppInfoFileExtension *
get_ext_object (const gunichar2 *ext,
const gchar *ext_u8,
const gchar *ext_u8_folded)
{
GWin32AppInfoFileExtension *file_extn;
if (g_hash_table_lookup_extended (extensions,
ext_u8_folded,
NULL,
(void **) &file_extn))
return file_extn;
file_extn = g_object_new (G_TYPE_WIN32_APPINFO_FILE_EXTENSION, NULL);
file_extn->extension = g_wcsdup (ext, -1);
file_extn->extension_u8 = g_strdup (ext_u8);
g_hash_table_insert (extensions, g_strdup (ext_u8_folded), file_extn);
return file_extn;
}
/* Iterates over HKCU\\Software\\Clients or HKLM\\Software\\Clients,
* (depending on @user_registry being TRUE or FALSE),
* collecting applications listed there.
* Puts the path to the client key for each client into @priority_capable_apps
* (only for clients with file or URL associations).
*/
static void
collect_capable_apps_from_clients (GPtrArray *capable_apps,
GPtrArray *priority_capable_apps,
gboolean user_registry)
{
GWin32RegistryKey *clients;
GWin32RegistrySubkeyIter clients_iter;
const gunichar2 *client_type_name;
gsize client_type_name_len;
if (user_registry)
clients =
g_win32_registry_key_new_w (L"HKEY_CURRENT_USER\\Software\\Clients",
NULL);
else
clients =
g_win32_registry_key_new_w (L"HKEY_LOCAL_MACHINE\\Software\\Clients",
NULL);
if (clients == NULL)
return;
if (!g_win32_registry_subkey_iter_init (&clients_iter, clients, NULL))
{
g_object_unref (clients);
return;
}
while (g_win32_registry_subkey_iter_next (&clients_iter, TRUE, NULL))
{
GWin32RegistrySubkeyIter subkey_iter;
GWin32RegistryKey *system_client_type;
GWin32RegistryValueType default_type;
gunichar2 *default_value = NULL;
const gunichar2 *client_name;
gsize client_name_len;
if (!g_win32_registry_subkey_iter_get_name_w (&clients_iter,
&client_type_name,
&client_type_name_len,
NULL))
continue;
system_client_type = g_win32_registry_key_get_child_w (clients,
client_type_name,
NULL);
if (system_client_type == NULL)
continue;
if (g_win32_registry_key_get_value_w (system_client_type,
NULL,
TRUE,
L"",
&default_type,
(gpointer *) &default_value,
NULL,
NULL))
{
if (default_type != G_WIN32_REGISTRY_VALUE_STR ||
default_value[0] == L'\0')
g_clear_pointer (&default_value, g_free);
}
if (!g_win32_registry_subkey_iter_init (&subkey_iter,
system_client_type,
NULL))
{
g_clear_pointer (&default_value, g_free);
g_object_unref (system_client_type);
continue;
}
while (g_win32_registry_subkey_iter_next (&subkey_iter, TRUE, NULL))
{
GWin32RegistryKey *system_client;
GWin32RegistryKey *system_client_assoc;
gboolean add;
gunichar2 *keyname;
if (!g_win32_registry_subkey_iter_get_name_w (&subkey_iter,
&client_name,
&client_name_len,
NULL))
continue;
system_client = g_win32_registry_key_get_child_w (system_client_type,
client_name,
NULL);
if (system_client == NULL)
continue;
add = FALSE;
system_client_assoc = g_win32_registry_key_get_child_w (system_client,
L"Capabilities\\FileAssociations",
NULL);
if (system_client_assoc != NULL)
{
add = TRUE;
g_object_unref (system_client_assoc);
}
else
{
system_client_assoc = g_win32_registry_key_get_child_w (system_client,
L"Capabilities\\UrlAssociations",
NULL);
if (system_client_assoc != NULL)
{
add = TRUE;
g_object_unref (system_client_assoc);
}
}
if (add)
{
keyname = g_wcsdup (g_win32_registry_key_get_path_w (system_client), -1);
if (default_value && wcscmp (default_value, client_name) == 0)
g_ptr_array_add (priority_capable_apps, keyname);
else
g_ptr_array_add (capable_apps, keyname);
}
g_object_unref (system_client);
}
g_win32_registry_subkey_iter_clear (&subkey_iter);
g_clear_pointer (&default_value, g_free);
g_object_unref (system_client_type);
}
g_win32_registry_subkey_iter_clear (&clients_iter);
g_object_unref (clients);
}
/* Iterates over HKCU\\Software\\RegisteredApplications or HKLM\\Software\\RegisteredApplications,
* (depending on @user_registry being TRUE or FALSE),
* collecting applications listed there.
* Puts the path to the app key for each app into @capable_apps.
*/
static void
collect_capable_apps_from_registered_apps (GPtrArray *capable_apps,
gboolean user_registry)
{
GWin32RegistryValueIter iter;
const gunichar2 *reg_path;
gunichar2 *value_data;
gsize value_data_size;
GWin32RegistryValueType value_type;
GWin32RegistryKey *registered_apps;
if (user_registry)
reg_path = L"HKEY_CURRENT_USER\\Software\\RegisteredApplications";
else
reg_path = L"HKEY_LOCAL_MACHINE\\Software\\RegisteredApplications";
registered_apps =
g_win32_registry_key_new_w (reg_path, NULL);
if (!registered_apps)
return;
if (!g_win32_registry_value_iter_init (&iter, registered_apps, NULL))
{
g_object_unref (registered_apps);
return;
}
while (g_win32_registry_value_iter_next (&iter, TRUE, NULL))
{
gunichar2 possible_location[REG_PATH_MAX_SIZE + 1];
GWin32RegistryKey *location;
gunichar2 *p;
if ((!g_win32_registry_value_iter_get_value_type (&iter,
&value_type,
NULL)) ||
(value_type != G_WIN32_REGISTRY_VALUE_STR) ||
(!g_win32_registry_value_iter_get_data_w (&iter, TRUE,
(void **) &value_data,
&value_data_size,
NULL)) ||
(value_data_size < sizeof (gunichar2)) ||
(value_data[0] == L'\0'))
continue;
if (!build_registry_path (possible_location, sizeof (possible_location),
user_registry ? HKCU : HKLM, value_data, NULL))
continue;
location = g_win32_registry_key_new_w (possible_location, NULL);
if (location == NULL)
continue;
p = wcsrchr (possible_location, L'\\');
if (p)
{
*p = L'\0';
g_ptr_array_add (capable_apps, g_wcsdup (possible_location, -1));
}
g_object_unref (location);
}
g_win32_registry_value_iter_clear (&iter);
g_object_unref (registered_apps);
}
/* Looks up an app object identified by
* @canonical_name_folded in the @app_hashmap.
* If such object doesn't exist,
* creates it and puts it into the @app_hashmap.
* Returns the object.
*/
static GWin32AppInfoApplication *
get_app_object (GHashTable *app_hashmap,
const gunichar2 *canonical_name,
const gchar *canonical_name_u8,
const gchar *canonical_name_folded,
gboolean user_specific,
gboolean default_app,
gboolean is_uwp)
{
GWin32AppInfoApplication *app;
app = g_hash_table_lookup (app_hashmap, canonical_name_folded);
if (app != NULL)
return app;
app = g_object_new (G_TYPE_WIN32_APPINFO_APPLICATION, NULL);
app->canonical_name = g_wcsdup (canonical_name, -1);
app->canonical_name_u8 = g_strdup (canonical_name_u8);
app->canonical_name_folded = g_strdup (canonical_name_folded);
app->no_open_with = FALSE;
app->user_specific = user_specific;
app->default_app = default_app;
app->is_uwp = is_uwp;
g_hash_table_insert (app_hashmap,
g_strdup (canonical_name_folded),
app);
return app;
}
/* Grabs an application that has Capabilities.
* @app_key_path is the path to the application key
* (which must have a "Capabilities" subkey).
* @default_app is TRUE if the app has priority
*/
static void
read_capable_app (const gunichar2 *app_key_path,
gboolean user_specific,
gboolean default_app)
{
GWin32AppInfoApplication *app;
gchar *canonical_name_u8 = NULL;
gchar *canonical_name_folded = NULL;
gchar *app_key_path_u8 = NULL;
gchar *app_key_path_u8_folded = NULL;
GWin32RegistryKey *appkey = NULL;
gunichar2 *fallback_friendly_name;
GWin32RegistryValueType vtype;
gboolean success;
gunichar2 *friendly_name;
gunichar2 *description;
gunichar2 *narrow_application_name;
gunichar2 *icon_source;
GWin32RegistryKey *capabilities;
GWin32RegistryKey *default_icon_key;
GWin32RegistryKey *associations;
const reg_verb *preferred_verb;
GList *verbs = NULL;
gboolean verbs_in_root_key = TRUE;
appkey = NULL;
capabilities = NULL;
if (!g_utf16_to_utf8_and_fold (app_key_path,
-1,
&canonical_name_u8,
&canonical_name_folded) ||
!g_utf16_to_utf8_and_fold (app_key_path,
-1,
&app_key_path_u8,
&app_key_path_u8_folded) ||
(appkey = g_win32_registry_key_new_w (app_key_path, NULL)) == NULL ||
(capabilities = g_win32_registry_key_get_child_w (appkey, L"Capabilities", NULL)) == NULL ||
!(get_verbs (appkey, &preferred_verb, &verbs, L"", L"Shell", NULL) ||
(verbs_in_root_key = FALSE) ||
get_verbs (capabilities, &preferred_verb, &verbs, L"", L"Shell", NULL)))
{
g_clear_pointer (&canonical_name_u8, g_free);
g_clear_pointer (&canonical_name_folded, g_free);
g_clear_object (&appkey);
g_clear_object (&capabilities);
g_clear_pointer (&app_key_path_u8, g_free);
g_clear_pointer (&app_key_path_u8_folded, g_free);
return;
}
app = get_app_object (apps_by_id,
app_key_path,
canonical_name_u8,
canonical_name_folded,
user_specific,
default_app,
FALSE);
process_verbs_commands (g_steal_pointer (&verbs),
preferred_verb,
L"", /* [ab]use the fact that two strings are simply concatenated */
verbs_in_root_key ? app_key_path : g_win32_registry_key_get_path_w (capabilities),
FALSE,
app_add_verb,
app,
app);
fallback_friendly_name = NULL;
success = g_win32_registry_key_get_value_w (appkey,
NULL,
TRUE,
L"",
&vtype,
(void **) &fallback_friendly_name,
NULL,
NULL);
if (success && vtype != G_WIN32_REGISTRY_VALUE_STR)
g_clear_pointer (&fallback_friendly_name, g_free);
if (fallback_friendly_name &&
app->pretty_name == NULL)
{
app->pretty_name = g_wcsdup (fallback_friendly_name, -1);
g_clear_pointer (&app->pretty_name_u8, g_free);
app->pretty_name_u8 = g_utf16_to_utf8 (fallback_friendly_name,
-1,
NULL,
NULL,
NULL);
}
friendly_name = NULL;
success = g_win32_registry_key_get_value_w (capabilities,
g_win32_registry_get_os_dirs_w (),
TRUE,
L"LocalizedString",
&vtype,
(void **) &friendly_name,
NULL,
NULL);
if (success &&
vtype != G_WIN32_REGISTRY_VALUE_STR)
g_clear_pointer (&friendly_name, g_free);
if (friendly_name &&
app->localized_pretty_name == NULL)
{
app->localized_pretty_name = g_wcsdup (friendly_name, -1);
g_clear_pointer (&app->localized_pretty_name_u8, g_free);
app->localized_pretty_name_u8 = g_utf16_to_utf8 (friendly_name,
-1,
NULL,
NULL,
NULL);
}
description = NULL;
success = g_win32_registry_key_get_value_w (capabilities,
g_win32_registry_get_os_dirs_w (),
TRUE,
L"ApplicationDescription",
&vtype,
(void **) &description,
NULL,
NULL);
if (success && vtype != G_WIN32_REGISTRY_VALUE_STR)
g_clear_pointer (&description, g_free);
if (description && app->description == NULL)
{
app->description = g_wcsdup (description, -1);
g_clear_pointer (&app->description_u8, g_free);
app->description_u8 = g_utf16_to_utf8 (description, -1, NULL, NULL, NULL);
}
default_icon_key = g_win32_registry_key_get_child_w (appkey,
L"DefaultIcon",
NULL);
icon_source = NULL;
if (default_icon_key != NULL)
{
success = g_win32_registry_key_get_value_w (default_icon_key,
NULL,
TRUE,
L"",
&vtype,
(void **) &icon_source,
NULL,
NULL);
if (success &&
vtype != G_WIN32_REGISTRY_VALUE_STR)
g_clear_pointer (&icon_source, g_free);
g_object_unref (default_icon_key);
}
if (icon_source == NULL)
{
success = g_win32_registry_key_get_value_w (capabilities,
NULL,
TRUE,
L"ApplicationIcon",
&vtype,
(void **) &icon_source,
NULL,
NULL);
if (success &&
vtype != G_WIN32_REGISTRY_VALUE_STR)
g_clear_pointer (&icon_source, g_free);
}
if (icon_source &&
app->icon == NULL)
{
gchar *name = g_utf16_to_utf8 (icon_source, -1, NULL, NULL, NULL);
app->icon = g_themed_icon_new (name);
g_free (name);
}
narrow_application_name = NULL;
success = g_win32_registry_key_get_value_w (capabilities,
g_win32_registry_get_os_dirs_w (),
TRUE,
L"ApplicationName",
&vtype,
(void **) &narrow_application_name,
NULL,
NULL);
if (success && vtype != G_WIN32_REGISTRY_VALUE_STR)
g_clear_pointer (&narrow_application_name, g_free);
if (narrow_application_name &&
app->localized_pretty_name == NULL)
{
app->localized_pretty_name = g_wcsdup (narrow_application_name, -1);
g_clear_pointer (&app->localized_pretty_name_u8, g_free);
app->localized_pretty_name_u8 = g_utf16_to_utf8 (narrow_application_name,
-1,
NULL,
NULL,
NULL);
}
associations = g_win32_registry_key_get_child_w (capabilities,
L"FileAssociations",
NULL);
if (associations != NULL)
{
GWin32RegistryValueIter iter;
if (g_win32_registry_value_iter_init (&iter, associations, NULL))
{
gunichar2 *file_extension;
gunichar2 *extension_handler;
gsize file_extension_len;
gsize extension_handler_size;
GWin32RegistryValueType value_type;
while (g_win32_registry_value_iter_next (&iter, TRUE, NULL))
{
if ((!g_win32_registry_value_iter_get_value_type (&iter,
&value_type,
NULL)) ||
(value_type != G_WIN32_REGISTRY_VALUE_STR) ||
(!g_win32_registry_value_iter_get_name_w (&iter,
&file_extension,
&file_extension_len,
NULL)) ||
(file_extension_len <= 0) ||
(file_extension[0] != L'.') ||
(!g_win32_registry_value_iter_get_data_w (&iter, TRUE,
(void **) &extension_handler,
&extension_handler_size,
NULL)) ||
(extension_handler_size < sizeof (gunichar2)) ||
(extension_handler[0] == L'\0'))
continue;
get_file_ext (extension_handler, file_extension, app, FALSE);
}
g_win32_registry_value_iter_clear (&iter);
}
g_object_unref (associations);
}
associations = g_win32_registry_key_get_child_w (capabilities, L"URLAssociations", NULL);
if (associations != NULL)
{
GWin32RegistryValueIter iter;
if (g_win32_registry_value_iter_init (&iter, associations, NULL))
{
gunichar2 *url_schema;
gunichar2 *schema_handler;
gsize url_schema_len;
gsize schema_handler_size;
GWin32RegistryValueType value_type;
while (g_win32_registry_value_iter_next (&iter, TRUE, NULL))
{
gchar *schema_u8 = NULL;
gchar *schema_u8_folded = NULL;
if ((!g_win32_registry_value_iter_get_value_type (&iter,
&value_type,
NULL)) ||
((value_type != G_WIN32_REGISTRY_VALUE_STR) &&
(value_type != G_WIN32_REGISTRY_VALUE_EXPAND_STR)) ||
(!g_win32_registry_value_iter_get_name_w (&iter,
&url_schema,
&url_schema_len,
NULL)) ||
(url_schema_len <= 0) ||
(url_schema[0] == L'\0') ||
(!g_win32_registry_value_iter_get_data_w (&iter, TRUE,
(void **) &schema_handler,
&schema_handler_size,
NULL)) ||
(schema_handler_size < sizeof (gunichar2)) ||
(schema_handler[0] == L'\0'))
continue;
if (g_utf16_to_utf8_and_fold (url_schema,
url_schema_len,
&schema_u8,
&schema_u8_folded))
get_url_association (schema_handler, url_schema, schema_u8, schema_u8_folded, app, FALSE);
g_clear_pointer (&schema_u8, g_free);
g_clear_pointer (&schema_u8_folded, g_free);
}
g_win32_registry_value_iter_clear (&iter);
}
g_object_unref (associations);
}
g_clear_pointer (&fallback_friendly_name, g_free);
g_clear_pointer (&description, g_free);
g_clear_pointer (&icon_source, g_free);
g_clear_pointer (&narrow_application_name, g_free);
g_object_unref (appkey);
g_object_unref (capabilities);
g_clear_pointer (&app_key_path_u8, g_free);
g_clear_pointer (&app_key_path_u8_folded, g_free);
g_clear_pointer (&canonical_name_u8, g_free);
g_clear_pointer (&canonical_name_folded, g_free);
}
/* Iterates over subkeys in HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\
* and calls get_url_association() for each one that has a user-chosen handler.
*/
static void
read_urls (GWin32RegistryKey *url_associations)
{
GWin32RegistrySubkeyIter url_iter;
if (url_associations == NULL)
return;
if (!g_win32_registry_subkey_iter_init (&url_iter, url_associations, NULL))
return;
while (g_win32_registry_subkey_iter_next (&url_iter, TRUE, NULL))
{
gchar *schema_u8 = NULL;
gchar *schema_u8_folded = NULL;
const gunichar2 *url_schema = NULL;
gunichar2 *program_id = NULL;
GWin32RegistryKey *user_choice = NULL;
gsize url_schema_len;
GWin32RegistryValueType val_type;
if (g_win32_registry_subkey_iter_get_name_w (&url_iter,
&url_schema,
&url_schema_len,
NULL) &&
g_utf16_to_utf8_and_fold (url_schema,
url_schema_len,
&schema_u8,
&schema_u8_folded) &&
(user_choice = _g_win32_registry_key_build_and_new_w (NULL, URL_ASSOCIATIONS,
url_schema, USER_CHOICE,
NULL)) != NULL &&
g_win32_registry_key_get_value_w (user_choice,
NULL,
TRUE,
L"Progid",
&val_type,
(void **) &program_id,
NULL,
NULL) &&
val_type == G_WIN32_REGISTRY_VALUE_STR)
get_url_association (program_id, url_schema, schema_u8, schema_u8_folded, NULL, TRUE);
g_clear_pointer (&program_id, g_free);
g_clear_pointer (&user_choice, g_object_unref);
g_clear_pointer (&schema_u8, g_free);
g_clear_pointer (&schema_u8_folded, g_free);
}
g_win32_registry_subkey_iter_clear (&url_iter);
}
/* Reads an application that is only registered by the basename of its
* executable (and doesn't have Capabilities subkey).
* @incapable_app is the registry key for the app.
* @app_exe_basename is the basename of its executable.
*/
static void
read_incapable_app (GWin32RegistryKey *incapable_app,
const gunichar2 *app_exe_basename,
const gchar *app_exe_basename_u8,
const gchar *app_exe_basename_u8_folded)
{
GWin32RegistryValueIter sup_iter;
GWin32AppInfoApplication *app;
GList *verbs;
const reg_verb *preferred_verb;
gunichar2 *friendly_app_name;
gboolean success;
GWin32RegistryValueType vtype;
gboolean no_open_with;
GWin32RegistryKey *default_icon_key;
gunichar2 *icon_source;
GIcon *icon = NULL;
GWin32RegistryKey *supported_key;
if (!get_verbs (incapable_app, &preferred_verb, &verbs, L"", L"Shell", NULL))
return;
app = get_app_object (apps_by_exe,
app_exe_basename,
app_exe_basename_u8,
app_exe_basename_u8_folded,
FALSE,
FALSE,
FALSE);
process_verbs_commands (g_steal_pointer (&verbs),
preferred_verb,
L"HKEY_CLASSES_ROOT\\Applications\\",
app_exe_basename,
TRUE,
app_add_verb,
app,
app);
friendly_app_name = NULL;
success = g_win32_registry_key_get_value_w (incapable_app,
g_win32_registry_get_os_dirs_w (),
TRUE,
L"FriendlyAppName",
&vtype,
(void **) &friendly_app_name,
NULL,
NULL);
if (success && vtype != G_WIN32_REGISTRY_VALUE_STR)
g_clear_pointer (&friendly_app_name, g_free);
no_open_with = g_win32_registry_key_get_value_w (incapable_app,
NULL,
TRUE,
L"NoOpenWith",
&vtype,
NULL,
NULL,
NULL);
default_icon_key =
g_win32_registry_key_get_child_w (incapable_app,
L"DefaultIcon",
NULL);
icon_source = NULL;
if (default_icon_key != NULL)
{
success =
g_win32_registry_key_get_value_w (default_icon_key,
NULL,
TRUE,
L"",
&vtype,
(void **) &icon_source,
NULL,
NULL);
if (success && vtype != G_WIN32_REGISTRY_VALUE_STR)
g_clear_pointer (&icon_source, g_free);
g_object_unref (default_icon_key);
}
if (icon_source)
{
gchar *name = g_utf16_to_utf8 (icon_source, -1, NULL, NULL, NULL);
if (name != NULL)
icon = g_themed_icon_new (name);
g_free (name);
}
app->no_open_with = no_open_with;
if (friendly_app_name &&
app->localized_pretty_name == NULL)
{
app->localized_pretty_name = g_wcsdup (friendly_app_name, -1);
g_clear_pointer (&app->localized_pretty_name_u8, g_free);
app->localized_pretty_name_u8 =
g_utf16_to_utf8 (friendly_app_name, -1, NULL, NULL, NULL);
}
if (icon && app->icon == NULL)
app->icon = g_object_ref (icon);
supported_key =
g_win32_registry_key_get_child_w (incapable_app,
L"SupportedTypes",
NULL);
if (supported_key &&
g_win32_registry_value_iter_init (&sup_iter, supported_key, NULL))
{
gunichar2 *ext_name;
gsize ext_name_len;
while (g_win32_registry_value_iter_next (&sup_iter, TRUE, NULL))
{
if ((!g_win32_registry_value_iter_get_name_w (&sup_iter,
&ext_name,
&ext_name_len,
NULL)) ||
(ext_name_len <= 0) ||
(ext_name[0] != L'.'))
continue;
get_file_ext (ext_name, ext_name, app, FALSE);
}
g_win32_registry_value_iter_clear (&sup_iter);
}
g_clear_object (&supported_key);
g_free (friendly_app_name);
g_free (icon_source);
g_clear_object (&icon);
}
/* Iterates over subkeys of HKEY_CLASSES_ROOT\\Applications
* and calls read_incapable_app() for each one.
*/
static void
read_exeapps (void)
{
GWin32RegistryKey *local_applications_key;
GWin32RegistrySubkeyIter app_iter;
local_applications_key =
g_win32_registry_key_new_w (L"HKEY_CLASSES_ROOT\\Applications", NULL);
if (local_applications_key == NULL)
return;
if (!g_win32_registry_subkey_iter_init (&app_iter, local_applications_key, NULL))
{
g_object_unref (local_applications_key);
return;
}
while (g_win32_registry_subkey_iter_next (&app_iter, TRUE, NULL))
{
const gunichar2 *app_exe_basename;
gsize app_exe_basename_len;
GWin32RegistryKey *incapable_app;
gchar *app_exe_basename_u8;
gchar *app_exe_basename_u8_folded;
if (!g_win32_registry_subkey_iter_get_name_w (&app_iter,
&app_exe_basename,
&app_exe_basename_len,
NULL) ||
!g_utf16_to_utf8_and_fold (app_exe_basename,
app_exe_basename_len,
&app_exe_basename_u8,
&app_exe_basename_u8_folded))
continue;
incapable_app =
g_win32_registry_key_get_child_w (local_applications_key,
app_exe_basename,
NULL);
if (incapable_app != NULL)
read_incapable_app (incapable_app,
app_exe_basename,
app_exe_basename_u8,
app_exe_basename_u8_folded);
g_clear_object (&incapable_app);
g_clear_pointer (&app_exe_basename_u8, g_free);
g_clear_pointer (&app_exe_basename_u8_folded, g_free);
}
g_win32_registry_subkey_iter_clear (&app_iter);
g_object_unref (local_applications_key);
}
/* Iterates over subkeys of HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\
* and calls get_file_ext() for each associated handler
* (starting with user-chosen handler, if any)
*/
static void
read_exts (GWin32RegistryKey *file_exts)
{
GWin32RegistrySubkeyIter ext_iter;
const gunichar2 *file_extension;
gsize file_extension_len;
if (file_exts == NULL)
return;
if (!g_win32_registry_subkey_iter_init (&ext_iter, file_exts, NULL))
return;
while (g_win32_registry_subkey_iter_next (&ext_iter, TRUE, NULL))
{
GWin32RegistryKey *open_with_progids;
gunichar2 *program_id;
GWin32RegistryValueIter iter;
gunichar2 *value_name;
gsize value_name_len;
GWin32RegistryValueType value_type;
GWin32RegistryKey *user_choice;
if (!g_win32_registry_subkey_iter_get_name_w (&ext_iter,
&file_extension,
&file_extension_len,
NULL))
continue;
program_id = NULL;
user_choice = _g_win32_registry_key_build_and_new_w (NULL, FILE_EXTS, file_extension,
USER_CHOICE, NULL);
if (user_choice &&
g_win32_registry_key_get_value_w (user_choice,
NULL,
TRUE,
L"Progid",
&value_type,
(void **) &program_id,
NULL,
NULL) &&
value_type == G_WIN32_REGISTRY_VALUE_STR)
{
/* Note: program_id could be "ProgramID" or "Applications\\program.exe".
* The code still works, but handler_id might have a backslash
* in it - that might trip us up later on.
* Even though in that case this is logically an "application"
* registry entry, we don't treat it in any special way.
* We do scan that registry branch anyway, just not here.
*/
get_file_ext (program_id, file_extension, NULL, TRUE);
}
g_clear_object (&user_choice);
g_clear_pointer (&program_id, g_free);
open_with_progids = _g_win32_registry_key_build_and_new_w (NULL, FILE_EXTS,
file_extension,
OPEN_WITH_PROGIDS,
NULL);
if (open_with_progids == NULL)
continue;
if (!g_win32_registry_value_iter_init (&iter, open_with_progids, NULL))
{
g_clear_object (&open_with_progids);
continue;
}
while (g_win32_registry_value_iter_next (&iter, TRUE, NULL))
{
if (!g_win32_registry_value_iter_get_name_w (&iter, &value_name,
&value_name_len,
NULL) ||
(value_name_len == 0))
continue;
get_file_ext (value_name, file_extension, NULL, FALSE);
}
g_win32_registry_value_iter_clear (&iter);
g_clear_object (&open_with_progids);
}
g_win32_registry_subkey_iter_clear (&ext_iter);
}
/* Iterates over subkeys in HKCR, calls
* get_file_ext() for any subkey that starts with ".",
* or get_url_association() for any subkey that could
* be a URL schema and has a "URL Protocol" value.
*/
static void
read_classes (GWin32RegistryKey *classes_root)
{
GWin32RegistrySubkeyIter class_iter;
const gunichar2 *class_name;
gsize class_name_len;
if (classes_root == NULL)
return;
if (!g_win32_registry_subkey_iter_init (&class_iter, classes_root, NULL))
return;
while (g_win32_registry_subkey_iter_next (&class_iter, TRUE, NULL))
{
if ((!g_win32_registry_subkey_iter_get_name_w (&class_iter,
&class_name,
&class_name_len,
NULL)) ||
(class_name_len <= 1))
continue;
if (class_name[0] == L'.')
{
GWin32RegistryKey *class_key;
GWin32RegistryValueIter iter;
GWin32RegistryKey *open_with_progids;
gunichar2 *value_name;
gsize value_name_len;
/* Read the data from the HKCR\\.ext (usually proxied
* to another HKCR subkey)
*/
get_file_ext (class_name, class_name, NULL, FALSE);
class_key = g_win32_registry_key_get_child_w (classes_root, class_name, NULL);
if (class_key == NULL)
continue;
open_with_progids = g_win32_registry_key_get_child_w (class_key, L"OpenWithProgids", NULL);
g_clear_object (&class_key);
if (open_with_progids == NULL)
continue;
if (!g_win32_registry_value_iter_init (&iter, open_with_progids, NULL))
{
g_clear_object (&open_with_progids);
continue;
}
/* Read the data for other handlers for this extension */
while (g_win32_registry_value_iter_next (&iter, TRUE, NULL))
{
if (!g_win32_registry_value_iter_get_name_w (&iter, &value_name,
&value_name_len,
NULL) ||
(value_name_len == 0))
continue;
get_file_ext (value_name, class_name, NULL, FALSE);
}
g_win32_registry_value_iter_clear (&iter);
g_clear_object (&open_with_progids);
}
else
{
gsize i;
GWin32RegistryKey *class_key;
gboolean success;
GWin32RegistryValueType vtype;
gchar *schema_u8;
gchar *schema_u8_folded;
for (i = 0; i < class_name_len; i++)
if (!iswalpha (class_name[i]))
break;
if (i != class_name_len)
continue;
class_key = g_win32_registry_key_get_child_w (classes_root, class_name, NULL);
if (class_key == NULL)
continue;
success = g_win32_registry_key_get_value_w (class_key,
NULL,
TRUE,
L"URL Protocol",
&vtype,
NULL,
NULL,
NULL);
g_clear_object (&class_key);
if (!success ||
vtype != G_WIN32_REGISTRY_VALUE_STR)
continue;
if (!g_utf16_to_utf8_and_fold (class_name, -1, &schema_u8, &schema_u8_folded))
continue;
get_url_association (class_name, class_name, schema_u8, schema_u8_folded, NULL, FALSE);
g_clear_pointer (&schema_u8, g_free);
g_clear_pointer (&schema_u8_folded, g_free);
}
}
g_win32_registry_subkey_iter_clear (&class_iter);
}
/* Iterates over all handlers and over all apps,
* and links handler verbs to apps if a handler
* runs the same executable as one of the app verbs.
*/
static void
link_handlers_to_unregistered_apps (void)
{
GHashTableIter iter;
GHashTableIter app_iter;
GWin32AppInfoHandler *handler;
gchar *handler_id_fld;
GWin32AppInfoApplication *app;
gchar *canonical_name_fld;
gchar *appexe_fld_basename;
g_hash_table_iter_init (&iter, handlers);
while (g_hash_table_iter_next (&iter,
(gpointer *) &handler_id_fld,
(gpointer *) &handler))
{
gsize vi;
if (handler->uwp_aumid != NULL)
continue;
for (vi = 0; vi < handler->verbs->len; vi++)
{
GWin32AppInfoShellVerb *handler_verb;
const gchar *handler_exe_basename;
enum
{
SH_UNKNOWN,
GOT_SH_INFO,
ERROR_GETTING_SH_INFO,
} have_stat_handler = SH_UNKNOWN;
GWin32PrivateStat handler_verb_exec_info = { 0 };
handler_verb = _verb_idx (handler->verbs, vi);
if (handler_verb->app != NULL)
continue;
if (handler_verb->executable_folded == NULL)
continue;
handler_exe_basename = g_utf8_find_basename (handler_verb->executable_folded, -1);
g_hash_table_iter_init (&app_iter, apps_by_id);
while (g_hash_table_iter_next (&app_iter,
(gpointer *) &canonical_name_fld,
(gpointer *) &app))
{
GWin32AppInfoShellVerb *app_verb;
gsize ai;
if (app->is_uwp)
continue;
for (ai = 0; ai < app->verbs->len; ai++)
{
GWin32PrivateStat app_verb_exec_info;
const gchar *app_exe_basename;
app_verb = _verb_idx (app->verbs, ai);
if (app_verb->executable_folded == NULL)
continue;
app_exe_basename = g_utf8_find_basename (app_verb->executable_folded, -1);
/* First check that the executable paths are identical */
if (g_strcmp0 (app_verb->executable_folded, handler_verb->executable_folded) != 0)
{
/* If not, check the basenames. If they are different, don't bother
* with further checks.
*/
if (g_strcmp0 (app_exe_basename, handler_exe_basename) != 0)
continue;
/* Get filesystem IDs for both files.
* For the handler that is attempted only once.
*/
if (have_stat_handler == SH_UNKNOWN)
{
if (GLIB_PRIVATE_CALL (g_win32_stat_utf8) (handler_verb->executable_folded,
&handler_verb_exec_info) == 0)
have_stat_handler = GOT_SH_INFO;
else
have_stat_handler = ERROR_GETTING_SH_INFO;
}
if (have_stat_handler != GOT_SH_INFO ||
(GLIB_PRIVATE_CALL (g_win32_stat_utf8) (app_verb->executable_folded,
&app_verb_exec_info) != 0) ||
app_verb_exec_info.file_index != handler_verb_exec_info.file_index)
continue;
}
handler_verb->app = g_object_ref (app);
break;
}
}
if (handler_verb->app != NULL)
continue;
g_hash_table_iter_init (&app_iter, apps_by_exe);
while (g_hash_table_iter_next (&app_iter,
(gpointer *) &appexe_fld_basename,
(gpointer *) &app))
{
if (app->is_uwp)
continue;
/* Use basename because apps_by_exe only has basenames */
if (g_strcmp0 (handler_exe_basename, appexe_fld_basename) != 0)
continue;
handler_verb->app = g_object_ref (app);
break;
}
}
}
}
/* Finds all .ext and schema: handler verbs that have no app linked to them,
* creates a "fake app" object and links these verbs to these
* objects. Objects are identified by the full path to
* the executable being run, thus multiple different invocations
* get grouped in a more-or-less natural way.
* The iteration goes separately over .ext and schema: handlers
* (instead of the global handlers hashmap) to allow us to
* put the handlers into supported_urls or supported_exts as
* needed (handler objects themselves have no knowledge of extensions
* and/or URLs they are associated with).
*/
static void
link_handlers_to_fake_apps (void)
{
GHashTableIter iter;
GHashTableIter handler_iter;
gchar *extension_utf8_folded;
GWin32AppInfoFileExtension *file_extn;
gchar *handler_id_fld;
GWin32AppInfoHandler *handler;
gchar *url_utf8_folded;
GWin32AppInfoURLSchema *schema;
g_hash_table_iter_init (&iter, extensions);
while (g_hash_table_iter_next (&iter,
(gpointer *) &extension_utf8_folded,
(gpointer *) &file_extn))
{
g_hash_table_iter_init (&handler_iter, file_extn->handlers);
while (g_hash_table_iter_next (&handler_iter,
(gpointer *) &handler_id_fld,
(gpointer *) &handler))
{
gsize vi;
if (handler->uwp_aumid != NULL)
continue;
for (vi = 0; vi < handler->verbs->len; vi++)
{
GWin32AppInfoShellVerb *handler_verb;
GWin32AppInfoApplication *app;
gunichar2 *exename_utf16;
handler_verb = _verb_idx (handler->verbs, vi);
if (handler_verb->app != NULL)
continue;
exename_utf16 = g_utf8_to_utf16 (handler_verb->executable, -1, NULL, NULL, NULL);
if (exename_utf16 == NULL)
continue;
app = get_app_object (fake_apps,
exename_utf16,
handler_verb->executable,
handler_verb->executable_folded,
FALSE,
FALSE,
FALSE);
g_clear_pointer (&exename_utf16, g_free);
handler_verb->app = g_object_ref (app);
app_add_verb (app,
app,
handler_verb->verb_name,
handler_verb->command,
handler_verb->command_utf8,
handler_verb->verb_displayname,
TRUE,
TRUE);
g_hash_table_insert (app->supported_exts,
g_strdup (extension_utf8_folded),
g_object_ref (handler));
}
}
}
g_hash_table_iter_init (&iter, urls);
while (g_hash_table_iter_next (&iter,
(gpointer *) &url_utf8_folded,
(gpointer *) &schema))
{
g_hash_table_iter_init (&handler_iter, schema->handlers);
while (g_hash_table_iter_next (&handler_iter,
(gpointer *) &handler_id_fld,
(gpointer *) &handler))
{
gsize vi;
if (handler->uwp_aumid != NULL)
continue;
for (vi = 0; vi < handler->verbs->len; vi++)
{
GWin32AppInfoShellVerb *handler_verb;
GWin32AppInfoApplication *app;
gchar *command_utf8_folded;
handler_verb = _verb_idx (handler->verbs, vi);
if (handler_verb->app != NULL)
continue;
command_utf8_folded = g_utf8_casefold (handler_verb->command_utf8, -1);
app = get_app_object (fake_apps,
handler_verb->command,
handler_verb->command_utf8,
command_utf8_folded,
FALSE,
FALSE,
FALSE);
g_clear_pointer (&command_utf8_folded, g_free);
handler_verb->app = g_object_ref (app);
app_add_verb (app,
app,
handler_verb->verb_name,
handler_verb->command,
handler_verb->command_utf8,
handler_verb->verb_displayname,
TRUE,
TRUE);
g_hash_table_insert (app->supported_urls,
g_strdup (url_utf8_folded),
g_object_ref (handler));
}
}
}
}
static GWin32AppInfoHandler *
find_uwp_handler_for_ext (GWin32AppInfoFileExtension *file_extn,
const gunichar2 *app_user_model_id)
{
GHashTableIter handler_iter;
gchar *handler_id_fld;
GWin32AppInfoHandler *handler;
g_hash_table_iter_init (&handler_iter, file_extn->handlers);
while (g_hash_table_iter_next (&handler_iter,
(gpointer *) &handler_id_fld,
(gpointer *) &handler))
{
if (handler->uwp_aumid == NULL)
continue;
if (_wcsicmp (handler->uwp_aumid, app_user_model_id) == 0)
return handler;
}
return NULL;
}
static GWin32AppInfoHandler *
find_uwp_handler_for_schema (GWin32AppInfoURLSchema *schema,
const gunichar2 *app_user_model_id)
{
GHashTableIter handler_iter;
gchar *handler_id_fld;
GWin32AppInfoHandler *handler;
g_hash_table_iter_init (&handler_iter, schema->handlers);
while (g_hash_table_iter_next (&handler_iter,
(gpointer *) &handler_id_fld,
(gpointer *) &handler))
{
if (handler->uwp_aumid == NULL)
continue;
if (_wcsicmp (handler->uwp_aumid, app_user_model_id) == 0)
return handler;
}
return NULL;
}
static gboolean
uwp_package_cb (gpointer user_data,
const gunichar2 *full_package_name,
const gunichar2 *package_name,
const gunichar2 *display_name,
const gunichar2 *app_user_model_id,
gboolean show_in_applist,
GPtrArray *supported_extgroups,
GPtrArray *supported_protocols)
{
guint i, i_verb, i_ext;
gint extensions_considered;
GWin32AppInfoApplication *app;
gchar *app_user_model_id_u8;
gchar *app_user_model_id_u8_folded;
GHashTableIter iter;
GWin32AppInfoHandler *ext_handler;
GWin32AppInfoHandler *url;
if (!g_utf16_to_utf8_and_fold (app_user_model_id,
-1,
&app_user_model_id_u8,
&app_user_model_id_u8_folded))
return TRUE;
app = get_app_object (apps_by_id,
app_user_model_id,
app_user_model_id_u8,
app_user_model_id_u8_folded,
TRUE,
FALSE,
TRUE);
if (!app->pretty_name && !app->pretty_name_u8 && display_name)
{
char *display_name_u8 = g_utf16_to_utf8 (display_name, -1, NULL, NULL, NULL);
app->pretty_name = g_wcsdup (display_name, -1);
app->pretty_name_u8 = g_steal_pointer (&display_name_u8);
}
extensions_considered = 0;
for (i = 0; i < supported_extgroups->len; i++)
{
GWin32PackageExtGroup *grp = (GWin32PackageExtGroup *) g_ptr_array_index (supported_extgroups, i);
extensions_considered += grp->extensions->len;
for (i_ext = 0; i_ext < grp->extensions->len; i_ext++)
{
wchar_t *ext = (wchar_t *) g_ptr_array_index (grp->extensions, i_ext);
gchar *ext_u8;
gchar *ext_u8_folded;
GWin32AppInfoFileExtension *file_extn;
GWin32AppInfoHandler *handler_rec;
if (!g_utf16_to_utf8_and_fold (ext,
-1,
&ext_u8,
&ext_u8_folded))
continue;
file_extn = get_ext_object (ext, ext_u8, ext_u8_folded);
g_free (ext_u8);
handler_rec = find_uwp_handler_for_ext (file_extn, app_user_model_id);
if (handler_rec == NULL)
{
/* Use AppUserModelId as the ID of the new fake handler */
handler_rec = get_handler_object (app_user_model_id_u8_folded,
NULL,
app_user_model_id,
app_user_model_id);
g_hash_table_insert (file_extn->handlers,
g_strdup (app_user_model_id_u8_folded),
g_object_ref (handler_rec));
}
if (file_extn->chosen_handler == NULL)
g_set_object (&file_extn->chosen_handler, handler_rec);
/* This is somewhat wasteful, but for 100% correct handling
* we need to remember which extensions (handlers) support
* which verbs, and each handler gets its own copy of the
* verb object, since our design is handler-centric,
* not verb-centric. The app also gets a list of verbs,
* but without handlers it would have no idea which
* verbs can be used with which extensions.
*/
for (i_verb = 0; i_verb < grp->verbs->len; i_verb++)
{
wchar_t *verb = NULL;
verb = (wchar_t *) g_ptr_array_index (grp->verbs, i_verb);
/* *_add_verb() functions are no-ops when a verb already exists,
* so we're free to call them as many times as we want.
*/
uwp_handler_add_verb (handler_rec,
app,
verb,
NULL,
FALSE);
}
g_hash_table_insert (app->supported_exts,
g_steal_pointer (&ext_u8_folded),
g_object_ref (handler_rec));
}
}
g_hash_table_iter_init (&iter, app->supported_exts);
/* Pile up all handler verbs into the app too,
* for cases when we don't have a ref to a handler.
*/
while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &ext_handler))
{
guint i_hverb;
if (!ext_handler)
continue;
for (i_hverb = 0; i_hverb < ext_handler->verbs->len; i_hverb++)
{
GWin32AppInfoShellVerb *handler_verb;
handler_verb = _verb_idx (ext_handler->verbs, i_hverb);
uwp_app_add_verb (app, handler_verb->verb_name, handler_verb->verb_displayname);
if (handler_verb->app == NULL && handler_verb->is_uwp)
handler_verb->app = g_object_ref (app);
}
}
if (app->verbs->len == 0 && extensions_considered > 0)
g_debug ("Unexpectedly, UWP app `%S' (AUMId `%s') supports %d extensions but has no verbs",
full_package_name, app_user_model_id_u8, extensions_considered);
for (i = 0; i < supported_protocols->len; i++)
{
wchar_t *proto = (wchar_t *) g_ptr_array_index (supported_protocols, i);
gchar *proto_u8;
gchar *proto_u8_folded;
GWin32AppInfoURLSchema *schema_rec;
GWin32AppInfoHandler *handler_rec;
if (!g_utf16_to_utf8_and_fold (proto,
-1,
&proto_u8,
&proto_u8_folded))
continue;
schema_rec = get_schema_object (proto,
proto_u8,
proto_u8_folded);
g_free (proto_u8);
handler_rec = find_uwp_handler_for_schema (schema_rec, app_user_model_id);
if (handler_rec == NULL)
{
/* Use AppUserModelId as the ID of the new fake handler */
handler_rec = get_handler_object (app_user_model_id_u8_folded,
NULL,
app_user_model_id,
app_user_model_id);
g_hash_table_insert (schema_rec->handlers,
g_strdup (app_user_model_id_u8_folded),
g_object_ref (handler_rec));
}
if (schema_rec->chosen_handler == NULL)
g_set_object (&schema_rec->chosen_handler, handler_rec);
/* Technically, UWP apps don't use verbs for URIs,
* but we only store an app field in verbs,
* so each UWP URI handler has to have one.
* Let's call it "open".
*/
uwp_handler_add_verb (handler_rec,
app,
L"open",
NULL,
TRUE);
g_hash_table_insert (app->supported_urls,
g_steal_pointer (&proto_u8_folded),
g_object_ref (handler_rec));
}
g_hash_table_iter_init (&iter, app->supported_urls);
while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &url))
{
guint i_hverb;
if (!url)
continue;
for (i_hverb = 0; i_hverb < url->verbs->len; i_hverb++)
{
GWin32AppInfoShellVerb *handler_verb;
handler_verb = _verb_idx (url->verbs, i_hverb);
uwp_app_add_verb (app, handler_verb->verb_name, handler_verb->verb_displayname);
if (handler_verb->app == NULL && handler_verb->is_uwp)
handler_verb->app = g_object_ref (app);
}
}
g_free (app_user_model_id_u8);
g_free (app_user_model_id_u8_folded);
return TRUE;
}
/* Calls SHLoadIndirectString() in a loop to resolve
* a string in @{...} format (also supports other indirect
* strings, but we aren't using it for those).
* Consumes the input, but may return it unmodified
* (not an indirect string). May return %NULL (the string
* is indirect, but the OS failed to load it).
*/
static gunichar2 *
resolve_string (gunichar2 *at_string)
{
HRESULT hr;
gunichar2 *result = NULL;
gsize result_size;
/* This value is arbitrary */
const gsize reasonable_size_limit = 8192;
if (at_string == NULL || at_string[0] != L'@')
return at_string;
/* In case of a no-op @at_string will be copied into the output,
* buffer so allocate at least that much.
*/
result_size = wcslen (at_string) + 1;
while (TRUE)
{
result = g_renew (gunichar2, result, result_size);
/* Since there's no built-in way to detect too small buffer size,
* we do so by putting a sentinel at the end of the buffer.
* If it's 0 (result is always 0-terminated, even if the buffer
* is too small), then try larger buffer.
*/
result[result_size - 1] = 0xff;
/* This string accepts size in characters, not bytes. */
hr = SHLoadIndirectString (at_string, result, result_size, NULL);
if (!SUCCEEDED (hr))
{
g_free (result);
g_free (at_string);
return NULL;
}
else if (result[result_size - 1] != 0 ||
result_size >= reasonable_size_limit)
{
/* Now that the length is known, allocate the exact amount */
gunichar2 *copy = g_wcsdup (result, -1);
g_free (result);
g_free (at_string);
return copy;
}
result_size *= 2;
}
g_assert_not_reached ();
return at_string;
}
static void
grab_registry_string (GWin32RegistryKey *handler_appkey,
const gunichar2 *value_name,
gunichar2 **destination,
gchar **destination_u8)
{
gunichar2 *value;
gsize value_size;
GWin32RegistryValueType vtype;
const gunichar2 *ms_resource_prefix = L"ms-resource:";
gsize ms_resource_prefix_len = wcslen (ms_resource_prefix);
/* Right now this function is not used without destination,
* enforce this. destination_u8 is optional.
*/
g_assert (destination != NULL);
if (*destination != NULL)
return;
value = NULL;
if (g_win32_registry_key_get_value_w (handler_appkey,
NULL,
TRUE,
value_name,
&vtype,
(void **) &value,
&value_size,
NULL) &&
vtype != G_WIN32_REGISTRY_VALUE_STR)
g_clear_pointer (&value, g_free);
/* There's no way for us to resolve "ms-resource:..." strings */
if (value != NULL &&
value_size >= ms_resource_prefix_len &&
memcmp (value,
ms_resource_prefix,
ms_resource_prefix_len * sizeof (gunichar2)) == 0)
g_clear_pointer (&value, g_free);
if (value == NULL)
return;
*destination = resolve_string (g_steal_pointer (&value));
if (*destination == NULL)
return;
if (destination_u8)
*destination_u8 = g_utf16_to_utf8 (*destination, -1, NULL, NULL, NULL);
}
static void
read_uwp_handler_info (void)
{
GHashTableIter iter;
GWin32RegistryKey *handler_appkey;
gunichar2 *aumid;
g_hash_table_iter_init (&iter, uwp_handler_table);
while (g_hash_table_iter_next (&iter, (gpointer *) &handler_appkey, (gpointer *) &aumid))
{
gchar *aumid_u8_folded;
GWin32AppInfoApplication *app;
if (!g_utf16_to_utf8_and_fold (aumid,
-1,
NULL,
&aumid_u8_folded))
continue;
app = g_hash_table_lookup (apps_by_id, aumid_u8_folded);
g_clear_pointer (&aumid_u8_folded, g_free);
if (app == NULL)
continue;
grab_registry_string (handler_appkey, L"ApplicationDescription", &app->description, &app->description_u8);
grab_registry_string (handler_appkey, L"ApplicationName", &app->localized_pretty_name, &app->localized_pretty_name_u8);
/* TODO: ApplicationIcon value (usually also @{...}) resolves into
* an image (PNG only?) with implicit multiple variants (scale, size, etc).
*/
}
}
static void
update_registry_data (void)
{
guint i;
GPtrArray *capable_apps_keys;
GPtrArray *user_capable_apps_keys;
GPtrArray *priority_capable_apps_keys;
GWin32RegistryKey *url_associations;
GWin32RegistryKey *file_exts;
GWin32RegistryKey *classes_root;
DWORD collect_start, collect_end, alloc_end, capable_end, url_end, ext_end, exeapp_end, classes_end, uwp_end, postproc_end;
GError *error = NULL;
url_associations =
g_win32_registry_key_new_w (L"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations",
NULL);
file_exts =
g_win32_registry_key_new_w (L"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts",
NULL);
classes_root = g_win32_registry_key_new_w (L"HKEY_CLASSES_ROOT", NULL);
capable_apps_keys = g_ptr_array_new_with_free_func (g_free);
user_capable_apps_keys = g_ptr_array_new_with_free_func (g_free);
priority_capable_apps_keys = g_ptr_array_new_with_free_func (g_free);
g_clear_pointer (&apps_by_id, g_hash_table_destroy);
g_clear_pointer (&apps_by_exe, g_hash_table_destroy);
g_clear_pointer (&fake_apps, g_hash_table_destroy);
g_clear_pointer (&urls, g_hash_table_destroy);
g_clear_pointer (&extensions, g_hash_table_destroy);
g_clear_pointer (&handlers, g_hash_table_destroy);
collect_start = GetTickCount ();
collect_capable_apps_from_clients (capable_apps_keys,
priority_capable_apps_keys,
FALSE);
collect_capable_apps_from_clients (user_capable_apps_keys,
priority_capable_apps_keys,
TRUE);
collect_capable_apps_from_registered_apps (user_capable_apps_keys,
TRUE);
collect_capable_apps_from_registered_apps (capable_apps_keys,
FALSE);
collect_end = GetTickCount ();
apps_by_id =
g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
apps_by_exe =
g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
fake_apps =
g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
urls =
g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
extensions =
g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
handlers =
g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
uwp_handler_table =
g_hash_table_new_full (g_direct_hash, g_direct_equal, g_object_unref, g_free);
alloc_end = GetTickCount ();
for (i = 0; i < priority_capable_apps_keys->len; i++)
read_capable_app (g_ptr_array_index (priority_capable_apps_keys, i),
TRUE,
TRUE);
for (i = 0; i < user_capable_apps_keys->len; i++)
read_capable_app (g_ptr_array_index (user_capable_apps_keys, i),
TRUE,
FALSE);
for (i = 0; i < capable_apps_keys->len; i++)
read_capable_app (g_ptr_array_index (capable_apps_keys, i),
FALSE,
FALSE);
capable_end = GetTickCount ();
read_urls (url_associations);
url_end = GetTickCount ();
read_exts (file_exts);
ext_end = GetTickCount ();
read_exeapps ();
exeapp_end = GetTickCount ();
read_classes (classes_root);
classes_end = GetTickCount ();
if (!g_win32_package_parser_enum_packages (uwp_package_cb, NULL, &error))
{
g_debug ("Unable to get UWP apps: %s", error->message);
g_clear_error (&error);
}
read_uwp_handler_info ();
uwp_end = GetTickCount ();
link_handlers_to_unregistered_apps ();
link_handlers_to_fake_apps ();
postproc_end = GetTickCount ();
g_debug ("Collecting capable appnames: %lums\n"
"Allocating hashtables:...... %lums\n"
"Reading capable apps: %lums\n"
"Reading URL associations:... %lums\n"
"Reading extension assocs: %lums\n"
"Reading exe-only apps:...... %lums\n"
"Reading classes: %lums\n"
"Reading UWP apps: %lums\n"
"Postprocessing:..............%lums\n"
"TOTAL: %lums",
collect_end - collect_start,
alloc_end - collect_end,
capable_end - alloc_end,
url_end - capable_end,
ext_end - url_end,
exeapp_end - ext_end,
classes_end - exeapp_end,
uwp_end - classes_end,
postproc_end - uwp_end,
postproc_end - collect_start);
g_clear_object (&classes_root);
g_clear_object (&url_associations);
g_clear_object (&file_exts);
g_ptr_array_free (capable_apps_keys, TRUE);
g_ptr_array_free (user_capable_apps_keys, TRUE);
g_ptr_array_free (priority_capable_apps_keys, TRUE);
g_hash_table_unref (uwp_handler_table);
return;
}
static void
watch_keys (void);
/* This function is called when any of our registry watchers detect
* changes in the registry.
*/
static void
keys_updated (GWin32RegistryKey *key,
gpointer user_data)
{
watch_keys ();
/* Indicate the tree as not up-to-date, push a new job for the AppInfo thread */
g_atomic_int_inc (&gio_win32_appinfo_update_counter);
/* We don't use the data pointer, but it must be non-NULL */
g_thread_pool_push (gio_win32_appinfo_threadpool, (gpointer) keys_updated, NULL);
}
static void
watch_keys (void)
{
if (url_associations_key)
g_win32_registry_key_watch (url_associations_key,
TRUE,
G_WIN32_REGISTRY_WATCH_NAME |
G_WIN32_REGISTRY_WATCH_ATTRIBUTES |
G_WIN32_REGISTRY_WATCH_VALUES,
keys_updated,
NULL,
NULL);
if (file_exts_key)
g_win32_registry_key_watch (file_exts_key,
TRUE,
G_WIN32_REGISTRY_WATCH_NAME |
G_WIN32_REGISTRY_WATCH_ATTRIBUTES |
G_WIN32_REGISTRY_WATCH_VALUES,
keys_updated,
NULL,
NULL);
if (user_clients_key)
g_win32_registry_key_watch (user_clients_key,
TRUE,
G_WIN32_REGISTRY_WATCH_NAME |
G_WIN32_REGISTRY_WATCH_ATTRIBUTES |
G_WIN32_REGISTRY_WATCH_VALUES,
keys_updated,
NULL,
NULL);
if (system_clients_key)
g_win32_registry_key_watch (system_clients_key,
TRUE,
G_WIN32_REGISTRY_WATCH_NAME |
G_WIN32_REGISTRY_WATCH_ATTRIBUTES |
G_WIN32_REGISTRY_WATCH_VALUES,
keys_updated,
NULL,
NULL);
if (applications_key)
g_win32_registry_key_watch (applications_key,
TRUE,
G_WIN32_REGISTRY_WATCH_NAME |
G_WIN32_REGISTRY_WATCH_ATTRIBUTES |
G_WIN32_REGISTRY_WATCH_VALUES,
keys_updated,
NULL,
NULL);
if (user_registered_apps_key)
g_win32_registry_key_watch (user_registered_apps_key,
TRUE,
G_WIN32_REGISTRY_WATCH_NAME |
G_WIN32_REGISTRY_WATCH_ATTRIBUTES |
G_WIN32_REGISTRY_WATCH_VALUES,
keys_updated,
NULL,
NULL);
if (system_registered_apps_key)
g_win32_registry_key_watch (system_registered_apps_key,
TRUE,
G_WIN32_REGISTRY_WATCH_NAME |
G_WIN32_REGISTRY_WATCH_ATTRIBUTES |
G_WIN32_REGISTRY_WATCH_VALUES,
keys_updated,
NULL,
NULL);
if (classes_root_key)
g_win32_registry_key_watch (classes_root_key,
FALSE,
G_WIN32_REGISTRY_WATCH_NAME |
G_WIN32_REGISTRY_WATCH_ATTRIBUTES |
G_WIN32_REGISTRY_WATCH_VALUES,
keys_updated,
NULL,
NULL);
}
/* This is the main function of the AppInfo thread */
static void
gio_win32_appinfo_thread_func (gpointer data,
gpointer user_data)
{
gint saved_counter;
g_mutex_lock (&gio_win32_appinfo_mutex);
saved_counter = g_atomic_int_get (&gio_win32_appinfo_update_counter);
if (saved_counter > 0)
update_registry_data ();
/* If the counter didn't change while we were working, then set it to zero.
* Otherwise we need to rebuild the tree again, so keep it greater than zero.
* Numeric value doesn't matter - even if we're asked to rebuild N times,
* we just need to rebuild once, and as long as there were no new rebuild
* requests while we were working, we're done.
*/
if (g_atomic_int_compare_and_exchange (&gio_win32_appinfo_update_counter,
saved_counter,
0))
g_cond_broadcast (&gio_win32_appinfo_cond);
g_mutex_unlock (&gio_win32_appinfo_mutex);
}
/* Initializes Windows AppInfo. Creates the registry watchers,
* the AppInfo thread, and initiates an update of the AppInfo tree.
* Called with do_wait = `FALSE` at startup to prevent it from
* blocking until the tree is updated. All subsequent calls
* from everywhere else are made with do_wait = `TRUE`, blocking
* until the tree is re-built (if needed).
*/
void
gio_win32_appinfo_init (gboolean do_wait)
{
static gsize initialized;
if (g_once_init_enter (&initialized))
{
HMODULE gio_dll_extra;
url_associations_key =
g_win32_registry_key_new_w (L"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations",
NULL);
file_exts_key =
g_win32_registry_key_new_w (L"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts",
NULL);
user_clients_key =
g_win32_registry_key_new_w (L"HKEY_CURRENT_USER\\Software\\Clients",
NULL);
system_clients_key =
g_win32_registry_key_new_w (L"HKEY_LOCAL_MACHINE\\Software\\Clients",
NULL);
applications_key =
g_win32_registry_key_new_w (L"HKEY_CLASSES_ROOT\\Applications",
NULL);
user_registered_apps_key =
g_win32_registry_key_new_w (L"HKEY_CURRENT_USER\\Software\\RegisteredApplications",
NULL);
system_registered_apps_key =
g_win32_registry_key_new_w (L"HKEY_LOCAL_MACHINE\\Software\\RegisteredApplications",
NULL);
classes_root_key =
g_win32_registry_key_new_w (L"HKEY_CLASSES_ROOT",
NULL);
watch_keys ();
/* We don't really require an exclusive pool, but the implementation
* details might cause the g_thread_pool_push() call below to block
* if the pool is not exclusive (specifically - for POSIX threads backend
* lacking thread scheduler settings).
*/
gio_win32_appinfo_threadpool = g_thread_pool_new (gio_win32_appinfo_thread_func,
NULL,
1,
TRUE,
NULL);
g_mutex_init (&gio_win32_appinfo_mutex);
g_cond_init (&gio_win32_appinfo_cond);
g_atomic_int_set (&gio_win32_appinfo_update_counter, 1);
/* Trigger initial tree build. Fake data pointer. */
g_thread_pool_push (gio_win32_appinfo_threadpool, (gpointer) keys_updated, NULL);
/* Increment the DLL refcount */
GetModuleHandleExA (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN,
(const char *) gio_win32_appinfo_init,
&gio_dll_extra);
/* gio DLL cannot be unloaded now */
g_once_init_leave (&initialized, TRUE);
}
if (!do_wait)
return;
/* Previously, we checked each of the watched keys here.
* Now we just look at the update counter, because each key
* has a change callback keys_updated, which increments this counter.
*/
if (g_atomic_int_get (&gio_win32_appinfo_update_counter) > 0)
{
g_mutex_lock (&gio_win32_appinfo_mutex);
while (g_atomic_int_get (&gio_win32_appinfo_update_counter) > 0)
g_cond_wait (&gio_win32_appinfo_cond, &gio_win32_appinfo_mutex);
g_mutex_unlock (&gio_win32_appinfo_mutex);
}
}
static void g_win32_app_info_iface_init (GAppInfoIface *iface);
struct _GWin32AppInfo
{
GObject parent_instance;
/*<private>*/
gchar **supported_types;
GWin32AppInfoApplication *app;
GWin32AppInfoHandler *handler;
guint startup_notify : 1;
};
G_DEFINE_TYPE_WITH_CODE (GWin32AppInfo, g_win32_app_info, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
g_win32_app_info_iface_init))
static void
g_win32_app_info_finalize (GObject *object)
{
GWin32AppInfo *info;
info = G_WIN32_APP_INFO (object);
g_clear_pointer (&info->supported_types, g_strfreev);
g_clear_object (&info->app);
g_clear_object (&info->handler);
G_OBJECT_CLASS (g_win32_app_info_parent_class)->finalize (object);
}
static void
g_win32_app_info_class_init (GWin32AppInfoClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->finalize = g_win32_app_info_finalize;
}
static void
g_win32_app_info_init (GWin32AppInfo *local)
{
}
static GAppInfo *
g_win32_app_info_new_from_app (GWin32AppInfoApplication *app,
GWin32AppInfoHandler *handler)
{
GWin32AppInfo *new_info;
GHashTableIter iter;
gpointer ext;
int i;
new_info = g_object_new (G_TYPE_WIN32_APP_INFO, NULL);
new_info->app = g_object_ref (app);
gio_win32_appinfo_init (TRUE);
g_mutex_lock (&gio_win32_appinfo_mutex);
i = 0;
g_hash_table_iter_init (&iter, new_info->app->supported_exts);
while (g_hash_table_iter_next (&iter, &ext, NULL))
{
if (ext)
i += 1;
}
new_info->supported_types = g_new (gchar *, i + 1);
i = 0;
g_hash_table_iter_init (&iter, new_info->app->supported_exts);
while (g_hash_table_iter_next (&iter, &ext, NULL))
{
if (!ext)
continue;
new_info->supported_types[i] = g_strdup ((gchar *) ext);
i += 1;
}
g_mutex_unlock (&gio_win32_appinfo_mutex);
new_info->supported_types[i] = NULL;
new_info->handler = handler ? g_object_ref (handler) : NULL;
return G_APP_INFO (new_info);
}
static GAppInfo *
g_win32_app_info_dup (GAppInfo *appinfo)
{
GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
GWin32AppInfo *new_info;
new_info = g_object_new (G_TYPE_WIN32_APP_INFO, NULL);
if (info->app)
new_info->app = g_object_ref (info->app);
if (info->handler)
new_info->handler = g_object_ref (info->handler);
new_info->startup_notify = info->startup_notify;
if (info->supported_types)
{
int i;
for (i = 0; info->supported_types[i]; i++)
break;
new_info->supported_types = g_new (gchar *, i + 1);
for (i = 0; info->supported_types[i]; i++)
new_info->supported_types[i] = g_strdup (info->supported_types[i]);
new_info->supported_types[i] = NULL;
}
return G_APP_INFO (new_info);
}
static gboolean
g_win32_app_info_equal (GAppInfo *appinfo1,
GAppInfo *appinfo2)
{
GWin32AppInfoShellVerb *shverb1 = NULL;
GWin32AppInfoShellVerb *shverb2 = NULL;
GWin32AppInfo *info1 = G_WIN32_APP_INFO (appinfo1);
GWin32AppInfo *info2 = G_WIN32_APP_INFO (appinfo2);
GWin32AppInfoApplication *app1 = info1->app;
GWin32AppInfoApplication *app2 = info2->app;
if (app1 == NULL ||
app2 == NULL)
return info1 == info2;
if (app1->canonical_name_folded != NULL &&
app2->canonical_name_folded != NULL)
return (g_strcmp0 (app1->canonical_name_folded,
app2->canonical_name_folded)) == 0;
if (app1->verbs->len > 0 &&
app2->verbs->len > 0)
{
shverb1 = _verb_idx (app1->verbs, 0);
shverb2 = _verb_idx (app2->verbs, 0);
if (shverb1->executable_folded != NULL &&
shverb2->executable_folded != NULL)
return (g_strcmp0 (shverb1->executable_folded,
shverb2->executable_folded)) == 0;
}
return app1 == app2;
}
static const char *
g_win32_app_info_get_id (GAppInfo *appinfo)
{
GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
GWin32AppInfoShellVerb *shverb;
if (info->app == NULL)
return NULL;
if (info->app->canonical_name_u8)
return info->app->canonical_name_u8;
if (info->app->verbs->len > 0 &&
(shverb = _verb_idx (info->app->verbs, 0))->executable_basename != NULL)
return shverb->executable_basename;
return NULL;
}
static const char *
g_win32_app_info_get_name (GAppInfo *appinfo)
{
GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
if (info->app && info->app->pretty_name_u8)
return info->app->pretty_name_u8;
else if (info->app && info->app->canonical_name_u8)
return info->app->canonical_name_u8;
else
return P_("Unnamed");
}
static const char *
g_win32_app_info_get_display_name (GAppInfo *appinfo)
{
GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
if (info->app)
{
if (info->app->localized_pretty_name_u8)
return info->app->localized_pretty_name_u8;
else if (info->app->pretty_name_u8)
return info->app->pretty_name_u8;
}
return g_win32_app_info_get_name (appinfo);
}
static const char *
g_win32_app_info_get_description (GAppInfo *appinfo)
{
GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
if (info->app == NULL)
return NULL;
return info->app->description_u8;
}
static const char *
g_win32_app_info_get_executable (GAppInfo *appinfo)
{
GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
if (info->app == NULL)
return NULL;
if (info->app->verbs->len > 0 && !info->app->is_uwp)
return _verb_idx (info->app->verbs, 0)->executable;
return NULL;
}
static const char *
g_win32_app_info_get_commandline (GAppInfo *appinfo)
{
GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
if (info->app == NULL)
return NULL;
if (info->app->verbs->len > 0 && !info->app->is_uwp)
return _verb_idx (info->app->verbs, 0)->command_utf8;
return NULL;
}
static GIcon *
g_win32_app_info_get_icon (GAppInfo *appinfo)
{
GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
if (info->app == NULL)
return NULL;
return info->app->icon;
}
typedef struct _file_or_uri {
gchar *uri;
gchar *file;
} file_or_uri;
static char *
expand_macro_single (char macro, file_or_uri *obj)
{
char *result = NULL;
switch (macro)
{
case '*':
case '0':
case '1':
case 'l':
case 'd':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
/* TODO: handle 'l' and 'd' differently (longname and desktop name) */
if (obj->file)
{
result = g_strdup (obj->file);
}
else if (obj->uri)
{
const char *prefix = "file:///";
const size_t prefix_len = strlen (prefix);
if (g_str_has_prefix (obj->uri, prefix) && obj->uri[prefix_len] != 0)
{
GFile *file = g_file_new_for_uri (obj->uri);
result = g_file_get_path (file);
g_object_unref (file);
}
if (!result)
result = g_strdup (obj->uri);
}
break;
case 'u':
case 'U':
if (obj->uri)
result = g_shell_quote (obj->uri);
break;
case 'f':
case 'F':
if (obj->file)
result = g_shell_quote (obj->file);
break;
}
return result;
}
static gboolean
expand_macro (char macro,
GString *exec,
GWin32AppInfo *info,
GList **stat_obj_list,
GList **obj_list)
{
GList *objs = *obj_list;
char *expanded;
gboolean result = FALSE;
g_return_val_if_fail (exec != NULL, FALSE);
/*
Legend: (from http://msdn.microsoft.com/en-us/library/windows/desktop/cc144101%28v=vs.85%29.aspx)
%* - replace with all parameters
%~ - replace with all parameters starting with and following the second parameter
%0 or %1 the first file parameter. For example "C:\\Users\\Eric\\Destop\\New Text Document.txt". Generally this should be in quotes and the applications command line parsing should accept quotes to disambiguate files with spaces in the name and different command line parameters (this is a security best practice and I believe mentioned in MSDN).
%<n> (where N is 2 - 9), replace with the nth parameter
%s - show command
%h - hotkey value
%i - IDList stored in a shared memory handle is passed here.
%l - long file name form of the first parameter. Note win32 applications will be passed the long file name, win16 applications get the short file name. Specifying %L is preferred as it avoids the need to probe for the application type.
%d - desktop absolute parsing name of the first parameter (for items that don't have file system paths)
%v - for verbs that are none implies all, if there is no parameter passed this is the working directory
%w - the working directory
*/
switch (macro)
{
case '*':
case '~':
if (*stat_obj_list)
{
gint i;
GList *o;
for (o = *stat_obj_list, i = 0;
macro == '~' && o && i < 2;
o = o->next, i++);
for (; o; o = o->next)
{
expanded = expand_macro_single (macro, o->data);
if (expanded)
{
if (o != *stat_obj_list)
g_string_append (exec, " ");
g_string_append (exec, expanded);
g_free (expanded);
}
}
objs = NULL;
result = TRUE;
}
break;
case '0':
case '1':
case 'l':
case 'd':
if (*stat_obj_list)
{
GList *o;
o = *stat_obj_list;
if (o)
{
expanded = expand_macro_single (macro, o->data);
if (expanded)
{
if (o != *stat_obj_list)
g_string_append (exec, " ");
g_string_append (exec, expanded);
g_free (expanded);
}
}
if (objs)
objs = objs->next;
result = TRUE;
}
break;
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (*stat_obj_list)
{
gint i;
GList *o;
gint n = 0;
switch (macro)
{
case '2':
n = 2;
break;
case '3':
n = 3;
break;
case '4':
n = 4;
break;
case '5':
n = 5;
break;
case '6':
n = 6;
break;
case '7':
n = 7;
break;
case '8':
n = 8;
break;
case '9':
n = 9;
break;
}
for (o = *stat_obj_list, i = 0; o && i < n; o = o->next, i++);
if (o)
{
expanded = expand_macro_single (macro, o->data);
if (expanded)
{
if (o != *stat_obj_list)
g_string_append (exec, " ");
g_string_append (exec, expanded);
g_free (expanded);
}
}
result = TRUE;
if (objs)
objs = NULL;
}
break;
case 's':
break;
case 'h':
break;
case 'i':
break;
case 'v':
break;
case 'w':
expanded = g_get_current_dir ();
g_string_append (exec, expanded);
g_free (expanded);
break;
case 'u':
case 'f':
if (objs)
{
expanded = expand_macro_single (macro, objs->data);
if (expanded)
{
g_string_append (exec, expanded);
g_free (expanded);
}
objs = objs->next;
result = TRUE;
}
break;
case 'U':
case 'F':
while (objs)
{
expanded = expand_macro_single (macro, objs->data);
if (expanded)
{
g_string_append (exec, expanded);
g_free (expanded);
}
objs = objs->next;
result = TRUE;
if (objs != NULL && expanded)
g_string_append_c (exec, ' ');
}
break;
case 'c':
if (info->app && info->app->localized_pretty_name_u8)
{
expanded = g_shell_quote (info->app->localized_pretty_name_u8);
g_string_append (exec, expanded);
g_free (expanded);
}
break;
case 'm': /* deprecated */
case 'n': /* deprecated */
case 'N': /* deprecated */
/*case 'd': *//* deprecated */
case 'D': /* deprecated */
break;
case '%':
g_string_append_c (exec, '%');
break;
}
*obj_list = objs;
return result;
}
static gboolean
expand_application_parameters (GWin32AppInfo *info,
const gchar *exec_line,
GList **objs,
int *argc,
char ***argv,
GError **error)
{
GList *obj_list = *objs;
GList **stat_obj_list = objs;
const char *p = exec_line;
GString *expanded_exec;
gboolean res;
gchar *a_char;
expanded_exec = g_string_new (NULL);
res = FALSE;
while (*p)
{
if (p[0] == '%' && p[1] != '\0')
{
if (expand_macro (p[1],
expanded_exec,
info, stat_obj_list,
objs))
res = TRUE;
p++;
}
else
g_string_append_c (expanded_exec, *p);
p++;
}
/* No file substitutions */
if (obj_list == *objs && obj_list != NULL && !res)
{
/* If there is no macro default to %f. This is also what KDE does */
g_string_append_c (expanded_exec, ' ');
expand_macro ('f', expanded_exec, info, stat_obj_list, objs);
}
/* Replace '\\' with '/', because g_shell_parse_argv considers them
* to be escape sequences.
*/
for (a_char = expanded_exec->str;
a_char <= &expanded_exec->str[expanded_exec->len];
a_char++)
{
if (*a_char == '\\')
*a_char = '/';
}
res = g_shell_parse_argv (expanded_exec->str, argc, argv, error);
g_string_free (expanded_exec, TRUE);
return res;
}
static gchar *
get_appath_for_exe (const gchar *exe_basename)
{
GWin32RegistryKey *apppath_key = NULL;
GWin32RegistryValueType val_type;
gchar *appath = NULL;
gboolean got_value;
gchar *key_path = g_strdup_printf ("HKEY_LOCAL_MACHINE\\"
"SOFTWARE\\"
"Microsoft\\"
"Windows\\"
"CurrentVersion\\"
"App Paths\\"
"%s", exe_basename);
apppath_key = g_win32_registry_key_new (key_path, NULL);
g_clear_pointer (&key_path, g_free);
if (apppath_key == NULL)
return NULL;
got_value = g_win32_registry_key_get_value (apppath_key,
NULL,
TRUE,
"Path",
&val_type,
(void **) &appath,
NULL,
NULL);
g_object_unref (apppath_key);
if (got_value &&
val_type == G_WIN32_REGISTRY_VALUE_STR)
return appath;
g_clear_pointer (&appath, g_free);
return appath;
}
/* GDesktopAppInfo::launch_uris_async emits all GAppLaunchContext's signals
* on the main thread.
*
* We do the same: when g_win32_app_info_launch_uris_impl has a non-NULL
* from_task argument we schedule the signal emissions on the main loop,
* taking care not to emit signals after the task itself is completed
* (see g_task_get_completed).
*/
typedef struct {
GAppLaunchContext *context; /* (owned) */
GWin32AppInfo *info; /* (owned) */
} EmitLaunchStartedData;
static void
emit_launch_started_data_free (EmitLaunchStartedData *data)
{
g_clear_object (&data->context);
g_clear_object (&data->info);
g_free (data);
}
static gboolean
emit_launch_started_cb (EmitLaunchStartedData *data)
{
g_signal_emit_by_name (data->context, "launch-started", data->info, NULL);
return G_SOURCE_REMOVE;
}
static void
emit_launch_started (GAppLaunchContext *context,
GWin32AppInfo *info,
GTask *from_task)
{
if (!context || !info)
return;
if (!from_task)
g_signal_emit_by_name (context, "launch-started", info, NULL);
else
{
EmitLaunchStartedData *data;
data = g_new (EmitLaunchStartedData, 1);
data->context = g_object_ref (context);
data->info = g_object_ref (info);
g_main_context_invoke_full (g_task_get_context (from_task),
g_task_get_priority (from_task),
G_SOURCE_FUNC (emit_launch_started_cb),
g_steal_pointer (&data),
(GDestroyNotify) emit_launch_started_data_free);
}
}
typedef struct {
GAppLaunchContext *context; /* (owned) */
GWin32AppInfo *info; /* (owned) */
GPid pid; /* (owned) */
} EmitLaunchedData;
static void
emit_launched_data_free (EmitLaunchedData *data)
{
g_clear_object (&data->context);
g_clear_object (&data->info);
g_spawn_close_pid (data->pid);
g_free (data);
}
static GVariant*
make_platform_data (GPid pid)
{
GVariantBuilder builder;
g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
/* pid handles are never bigger than 2^24 as per
* https://docs.microsoft.com/en-us/windows/win32/sysinfo/kernel-objects,
* so truncating to `int32` is valid.
* The gsize cast is to silence a compiler warning
* about conversion from pointer to integer of
* different size. */
g_variant_builder_add (&builder, "{sv}", "pid", g_variant_new_int32 ((gsize) pid));
return g_variant_ref_sink (g_variant_builder_end (&builder));
}
static gboolean
emit_launched_cb (EmitLaunchedData *data)
{
GVariant *platform_data = make_platform_data (data->pid);
g_signal_emit_by_name (data->context, "launched", data->info, platform_data);
g_variant_unref (platform_data);
return G_SOURCE_REMOVE;
}
static void
emit_launched (GAppLaunchContext *context,
GWin32AppInfo *info,
GPid *pid,
GTask *from_task)
{
if (!context || !info)
return;
if (!from_task)
{
GVariant *platform_data = make_platform_data (*pid);
g_signal_emit_by_name (context, "launched", info, platform_data);
g_variant_unref (platform_data);
g_spawn_close_pid (*pid);
}
else
{
EmitLaunchedData *data;
data = g_new (EmitLaunchedData, 1);
data->context = g_object_ref (context);
data->info = g_object_ref (info);
data->pid = *pid;
g_main_context_invoke_full (g_task_get_context (from_task),
g_task_get_priority (from_task),
G_SOURCE_FUNC (emit_launched_cb),
g_steal_pointer (&data),
(GDestroyNotify) emit_launched_data_free);
}
*pid = NULL;
}
typedef struct {
GAppLaunchContext *context; /* (owned) */
GWin32AppInfo *info; /* (owned) */
} EmitLaunchFailedData;
static void
emit_launch_failed_data_free (EmitLaunchFailedData *data)
{
g_clear_object (&data->context);
g_clear_object (&data->info);
g_free (data);
}
static gboolean
emit_launch_failed_cb (EmitLaunchFailedData *data)
{
g_signal_emit_by_name (data->context, "launch-failed", data->info, NULL);
return G_SOURCE_REMOVE;
}
static void
emit_launch_failed (GAppLaunchContext *context,
GWin32AppInfo *info,
GTask *from_task)
{
if (!context || !info)
return;
if (!from_task)
g_signal_emit_by_name (context, "launch-failed", info, NULL);
else
{
EmitLaunchFailedData *data;
data = g_new (EmitLaunchFailedData, 1);
data->context = g_object_ref (context);
data->info = g_object_ref (info);
g_main_context_invoke_full (g_task_get_context (from_task),
g_task_get_priority (from_task),
G_SOURCE_FUNC (emit_launch_failed_cb),
g_steal_pointer (&data),
(GDestroyNotify) emit_launch_failed_data_free);
}
}
typedef enum
{
/* PLAIN: just open the application, without arguments of any kind
* corresponds to: LaunchActivatedEventArgs */
UWP_ACTIVATION_TYPE_PLAIN,
/* FILE: open the applications passing a set of files
* corresponds to: FileActivatedEventArgs */
UWP_ACTIVATION_TYPE_FILE,
/* PROTOCOL: open the application passing a URI which describe an
app activity
* corresponds to: ProtocolActivatedEventArgs */
UWP_ACTIVATION_TYPE_PROTOCOL,
} UwpActivationType;
static gboolean
g_win32_app_info_launch_uwp_single (IApplicationActivationManager *app_activation_manager,
UwpActivationType activation_type,
IShellItemArray *items,
const wchar_t *verb,
GWin32AppInfo *info,
GAppLaunchContext *launch_context,
GTask *from_task,
GError **error)
{
const wchar_t *canonical_name = (const wchar_t *) info->app->canonical_name;
DWORD process_id = 0;
HRESULT hr = S_OK;
emit_launch_started (launch_context, info, from_task);
/* The Activate methods return a process identifier (PID), so we should consider
* those methods as potentially blocking */
switch (activation_type)
{
case UWP_ACTIVATION_TYPE_PLAIN:
g_assert (items == NULL);
hr = IApplicationActivationManager_ActivateApplication (app_activation_manager,
canonical_name,
NULL, AO_NONE,
&process_id);
break;
case UWP_ACTIVATION_TYPE_PROTOCOL:
g_assert (items != NULL);
hr = IApplicationActivationManager_ActivateForProtocol (app_activation_manager,
canonical_name,
items,
&process_id);
break;
case UWP_ACTIVATION_TYPE_FILE:
g_assert (items != NULL);
hr = IApplicationActivationManager_ActivateForFile (app_activation_manager,
canonical_name,
items, verb,
&process_id);
break;
}
if (FAILED (hr))
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"The app %s failed to launch: 0x%lx",
g_win32_appinfo_application_get_some_name (info->app), hr);
emit_launch_failed (launch_context, info, from_task);
return FALSE;
}
if (launch_context)
{
DWORD access_rights = 0;
HANDLE process_handle = NULL;
/* Unfortunately, there's a race condition here.
* ApplicationActivationManager methods return a process ID, but it
* keeps no open HANDLE to the spawned process internally (tested
* on Windows 10 21H2). So we cannot guarantee that by the time
* OpenProcess is called, process ID still referes to the spawned
* process. Anyway hitting such case is extremely unlikely.
*
* https://docs.microsoft.com/en-us/answers/questions/942879/
* iapplicationactivationmanager-race-condition.html
*
* Maybe we could make use of the WinRT APIs to activate UWP apps,
* instead? */
/* As documented on MSDN, the handle returned by CreateProcess has
* PROCESS_ALL_ACCESS rights. First try passing PROCESS_ALL_ACCESS
* to have the same access rights as the non-UWP code-path; should
* that fail with ERROR_ACCESS_DENIED error code, retry using safe
* access rights */
access_rights = PROCESS_ALL_ACCESS;
process_handle = OpenProcess (access_rights, FALSE, process_id);
if (!process_handle && GetLastError () == ERROR_ACCESS_DENIED)
{
DWORD access_rights = PROCESS_QUERY_LIMITED_INFORMATION |
SYNCHRONIZE;
process_handle = OpenProcess (access_rights, FALSE, process_id);
}
if (!process_handle)
{
g_warning ("OpenProcess failed with error code %" G_GUINT32_FORMAT,
(guint32) GetLastError ());
}
/* Emit the launched signal regardless if we have the process
* HANDLE or NULL */
emit_launched (launch_context, info, (GPid*) &process_handle, from_task);
g_spawn_close_pid ((GPid) process_handle);
}
return TRUE;
}
static gboolean
g_win32_app_info_supports_files (GAppInfo *appinfo);
static IShellItemArray *
make_item_array (gboolean for_files,
GList *objs,
GError **error);
static inline GList
make_single_entry_list (gpointer data)
{
GList l = { NULL, NULL, NULL };
l.data = data;
return l;
}
static gboolean
g_win32_app_info_launch_uwp_internal (GWin32AppInfo *info,
gboolean for_files,
GList *objs, /* (element-type file_or_uri) */
GWin32AppInfoShellVerb *shverb,
GAppLaunchContext *launch_context,
GTask *from_task,
GError **error)
{
IApplicationActivationManager *paam = NULL;
gboolean com_initialized = FALSE;
gboolean result = FALSE;
HRESULT hr;
/* ApplicationActivationManager threading model is both,
* prefer the multithreaded apartment type, as we don't
* need anything of the STA here. */
hr = CoInitializeEx (NULL, COINIT_MULTITHREADED);
if (SUCCEEDED (hr))
com_initialized = TRUE;
else if (hr != RPC_E_CHANGED_MODE)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Failed to initialize the COM support library for the thread: 0x%lx", hr);
goto cleanup;
}
/* It's best to instantiate ApplicationActivationManager out-of-proc,
* as documented on MSDN:
*
* An IApplicationActivationManager object creates a thread in its
* host process to serve any activated event arguments objects
* (LaunchActivatedEventArgs, FileActivatedEventArgs, and Protocol-
* ActivatedEventArgs) that are passed to the app. If the calling
* process is long-lived, you can create this object in-proc,
* based on the assumption that the event arguments will exist long
* enough for the target app to use them.
* However, if the calling process is spawned only to launch the
* target app, it should create the IApplicationActivationManager
* object out-of-process, by using CLSCTX_LOCAL_SERVER. This causes
* the object to be created in a Dllhost instance that automatically
* manages the object's lifetime based on outstanding references to
* the activated event argument objects.
*/
hr = CoCreateInstance (&CLSID_ApplicationActivationManager, NULL,
CLSCTX_LOCAL_SERVER,
&IID_IApplicationActivationManager, (void **) &paam);
if (FAILED (hr))
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Failed to create ApplicationActivationManager: 0x%lx", hr);
goto cleanup;
}
if (!objs)
{
result = g_win32_app_info_launch_uwp_single (paam, UWP_ACTIVATION_TYPE_PLAIN, NULL, NULL,
info, launch_context, from_task, error);
}
else if (for_files)
{
IShellItemArray *items = make_item_array (TRUE, objs, error);
if (!items)
goto cleanup;
result = g_win32_app_info_launch_uwp_single (paam, UWP_ACTIVATION_TYPE_FILE, items,
shverb->verb_name,
info, launch_context, from_task, error);
IShellItemArray_Release (items);
}
else
{
gboolean supports_files;
gboolean supports_file_uris;
gboolean outcome = TRUE;
GList *l;
supports_files = g_win32_app_info_supports_files (G_APP_INFO (info));
supports_file_uris = info->app &&
info->app->supported_urls &&
g_hash_table_lookup (info->app->supported_urls, "file");
for (l = objs; l != NULL; l = l->next)
{
file_or_uri *obj = (file_or_uri*) l->data;
GList single = make_single_entry_list (obj);
IShellItemArray *item = NULL;
UwpActivationType type;
/* Most UWP applications support opening files but do not support
* the file:// protocol in URI's. That's because the UWP platform
* has a specific activation for files (see FileActivatedEventArgs)
* which is different from protocol activation. Here we check for
* that. */
if (!supports_file_uris && supports_files && obj->file)
{
type = UWP_ACTIVATION_TYPE_FILE;
item = make_item_array (TRUE, &single, error);
}
else
{
type = UWP_ACTIVATION_TYPE_PROTOCOL;
item = make_item_array (FALSE, &single, error);
}
if (!item)
{
outcome = FALSE;
continue;
}
if (!g_win32_app_info_launch_uwp_single (paam, type,
item, shverb->verb_name, info,
launch_context, from_task, error))
outcome = FALSE;
IShellItemArray_Release (item);
}
result = outcome;
}
cleanup:
if (paam)
{
IApplicationActivationManager_Release (paam);
paam = NULL;
}
if (com_initialized)
{
CoUninitialize ();
com_initialized = FALSE;
}
return result;
}
static gboolean
g_win32_app_info_launch_internal (GWin32AppInfo *info,
GList *objs, /* (element-type file_or_uri) */
gboolean for_files, /* UWP only */
GAppLaunchContext *launch_context,
GSpawnFlags spawn_flags,
GTask *from_task,
GError **error)
{
gboolean completed = FALSE;
char **argv, **envp;
int argc;
const gchar *command;
gchar *apppath;
GWin32AppInfoShellVerb *shverb;
GPid pid = NULL;
g_return_val_if_fail (info != NULL, FALSE);
g_return_val_if_fail (info->app != NULL, FALSE);
argv = NULL;
shverb = NULL;
if (!info->app->is_uwp &&
info->handler != NULL &&
info->handler->verbs->len > 0)
shverb = _verb_idx (info->handler->verbs, 0);
else if (info->app->verbs->len > 0)
shverb = _verb_idx (info->app->verbs, 0);
if (shverb == NULL)
{
if (info->app->is_uwp || info->handler == NULL)
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
P_("The app ‘%s’ in the application object has no verbs"),
g_win32_appinfo_application_get_some_name (info->app));
else if (info->handler->verbs->len == 0)
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
P_("The app ‘%s’ and the handler ‘%s’ in the application object have no verbs"),
g_win32_appinfo_application_get_some_name (info->app),
info->handler->handler_id_folded);
return FALSE;
}
if (info->app->is_uwp)
return g_win32_app_info_launch_uwp_internal (info,
for_files,
objs,
shverb,
launch_context,
from_task,
error);
if (launch_context)
envp = g_app_launch_context_get_environment (launch_context);
else
envp = g_get_environ ();
g_assert (shverb->command_utf8 != NULL);
command = shverb->command_utf8;
apppath = get_appath_for_exe (shverb->executable_basename);
if (apppath)
{
gchar **p;
gsize p_index;
for (p = envp, p_index = 0; p[0]; p++, p_index++)
if ((p[0][0] == 'p' || p[0][0] == 'P') &&
(p[0][1] == 'a' || p[0][1] == 'A') &&
(p[0][2] == 't' || p[0][2] == 'T') &&
(p[0][3] == 'h' || p[0][3] == 'H') &&
(p[0][4] == '='))
break;
if (p[0] == NULL)
{
gchar **new_envp;
new_envp = g_new (char *, g_strv_length (envp) + 2);
new_envp[0] = g_strdup_printf ("PATH=%s", apppath);
for (p_index = 0; p_index <= g_strv_length (envp); p_index++)
new_envp[1 + p_index] = envp[p_index];
g_free (envp);
envp = new_envp;
}
else
{
gchar *p_path;
p_path = &p[0][5];
if (p_path[0] != '\0')
envp[p_index] = g_strdup_printf ("PATH=%s%c%s",
apppath,
G_SEARCHPATH_SEPARATOR,
p_path);
else
envp[p_index] = g_strdup_printf ("PATH=%s", apppath);
g_free (&p_path[-5]);
}
}
do
{
if (from_task && g_task_return_error_if_cancelled (from_task))
goto out;
if (!expand_application_parameters (info,
command,
&objs,
&argc,
&argv,
error))
goto out;
emit_launch_started (launch_context, info, from_task);
if (!g_spawn_async (NULL,
argv,
envp,
spawn_flags |
G_SPAWN_DO_NOT_REAP_CHILD,
NULL,
NULL,
&pid,
error))
{
emit_launch_failed (launch_context, info, from_task);
goto out;
}
else if (launch_context)
emit_launched (launch_context, info, &pid, from_task);
g_spawn_close_pid (pid);
pid = NULL;
g_strfreev (argv);
argv = NULL;
}
while (objs != NULL);
completed = TRUE;
out:
g_spawn_close_pid (pid);
g_strfreev (argv);
g_strfreev (envp);
return completed;
}
static void
free_file_or_uri (gpointer ptr)
{
file_or_uri *obj = ptr;
g_free (obj->file);
g_free (obj->uri);
g_free (obj);
}
static gboolean
g_win32_app_supports_uris (GWin32AppInfoApplication *app)
{
gssize num_of_uris_supported;
if (app == NULL)
return FALSE;
num_of_uris_supported = (gssize) g_hash_table_size (app->supported_urls);
if (g_hash_table_lookup (app->supported_urls, "file"))
num_of_uris_supported -= 1;
return num_of_uris_supported > 0;
}
static gboolean
g_win32_app_info_supports_uris (GAppInfo *appinfo)
{
GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
if (info->app == NULL)
return FALSE;
return g_win32_app_supports_uris (info->app);
}
static gboolean
g_win32_app_info_supports_files (GAppInfo *appinfo)
{
GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
if (info->app == NULL)
return FALSE;
return g_hash_table_size (info->app->supported_exts) > 0;
}
static IShellItemArray *
make_item_array (gboolean for_files,
GList *objs, /* (element-type file_or_uri) */
GError **error)
{
ITEMIDLIST **item_ids;
IShellItemArray *items;
GList *p;
gsize count;
gsize i;
HRESULT hr;
count = g_list_length (objs);
items = NULL;
item_ids = g_new (ITEMIDLIST*, count);
for (i = 0, p = objs; p != NULL; p = p->next, i++)
{
file_or_uri *obj = (file_or_uri*) p->data;
wchar_t *file_or_uri_utf16;
if (!for_files)
file_or_uri_utf16 = g_utf8_to_utf16 (obj->uri, -1, NULL, NULL, error);
else
file_or_uri_utf16 = g_utf8_to_utf16 (obj->file, -1, NULL, NULL, error);
if (file_or_uri_utf16 == NULL)
break;
if (for_files)
{
wchar_t *c;
gsize len;
gsize len_tail;
len = wcslen (file_or_uri_utf16);
/* Filenames *MUST* use single backslashes, else the call
* will fail. First convert all slashes to backslashes,
* then remove duplicates.
*/
for (c = file_or_uri_utf16; for_files && *c != 0; c++)
{
if (*c == L'/')
*c = L'\\';
}
for (len_tail = 0, c = &file_or_uri_utf16[len - 1];
for_files && c > file_or_uri_utf16;
c--, len_tail++)
{
if (c[0] != L'\\' || c[-1] != L'\\')
continue;
memmove (&c[-1], &c[0], len_tail * sizeof (wchar_t));
}
}
hr = SHParseDisplayName (file_or_uri_utf16, NULL, &item_ids[i], 0, NULL);
if (FAILED (hr))
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"File or URI `%S' cannot be parsed by SHParseDisplayName: 0x%lx",
file_or_uri_utf16, hr);
g_free (file_or_uri_utf16);
break;
}
g_free (file_or_uri_utf16);
}
if (i == count)
{
hr = SHCreateShellItemArrayFromIDLists (count, (const ITEMIDLIST **) item_ids, &items);
if (FAILED (hr))
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"SHCreateShellItemArrayFromIDLists() failed: 0x%lx", hr);
items = NULL;
}
}
count = i;
for (i = 0; i < count; i++)
CoTaskMemFree (item_ids[i]);
g_free (item_ids);
return items;
}
static gboolean
g_win32_app_info_launch_uris_impl (GAppInfo *appinfo,
GList *uris, /* (element-type utf8) */
GAppLaunchContext *launch_context,
GTask *from_task,
GError **error)
{
gboolean res = FALSE;
gboolean do_files;
GList *objs = NULL;
GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
gboolean is_uwp;
do_files = g_win32_app_info_supports_files (appinfo);
while (uris)
{
file_or_uri *obj;
obj = g_new0 (file_or_uri, 1);
if (do_files)
{
GFile *file;
gchar *path;
file = g_file_new_for_uri (uris->data);
path = g_file_get_path (file);
obj->file = path;
g_object_unref (file);
}
obj->uri = g_strdup (uris->data);
objs = g_list_prepend (objs, obj);
uris = uris->next;
}
objs = g_list_reverse (objs);
is_uwp = (info->app != NULL && info->app->is_uwp);
res = g_win32_app_info_launch_internal (info,
objs,
FALSE,
launch_context,
is_uwp ?
0 :
G_SPAWN_SEARCH_PATH,
from_task,
error);
g_list_free_full (objs, free_file_or_uri);
return res;
}
static gboolean
g_win32_app_info_launch_uris (GAppInfo *appinfo,
GList *uris,
GAppLaunchContext *launch_context,
GError **error)
{
return g_win32_app_info_launch_uris_impl (appinfo, uris, launch_context, NULL, error);
}
typedef struct
{
GList *uris; /* (element-type utf8) (owned) (nullable) */
GAppLaunchContext *context; /* (owned) (nullable) */
} LaunchUrisData;
static void
launch_uris_data_free (LaunchUrisData *data)
{
g_clear_object (&data->context);
g_list_free_full (data->uris, g_free);
g_free (data);
}
static void
launch_uris_async_thread (GTask *task,
gpointer source_object,
gpointer task_data,
GCancellable *cancellable)
{
GAppInfo *appinfo = G_APP_INFO (source_object);
LaunchUrisData *data = task_data;
GError *local_error = NULL;
gboolean succeeded;
succeeded = g_win32_app_info_launch_uris_impl (appinfo, data->uris, data->context, task, &local_error);
if (succeeded)
g_task_return_boolean (task, TRUE);
else if (!g_task_had_error (task))
g_task_return_error (task, g_steal_pointer (&local_error));
}
static void
g_win32_app_info_launch_uris_async (GAppInfo *appinfo,
GList *uris,
GAppLaunchContext *context,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GTask *task;
LaunchUrisData *data;
task = g_task_new (appinfo, cancellable, callback, user_data);
g_task_set_source_tag (task, g_win32_app_info_launch_uris_async);
data = g_new0 (LaunchUrisData, 1);
data->uris = g_list_copy_deep (uris, (GCopyFunc) g_strdup, NULL);
g_set_object (&data->context, context);
g_task_set_task_data (task, g_steal_pointer (&data), (GDestroyNotify) launch_uris_data_free);
g_task_run_in_thread (task, launch_uris_async_thread);
g_object_unref (task);
}
static gboolean
g_win32_app_info_launch_uris_finish (GAppInfo *appinfo,
GAsyncResult *result,
GError **error)
{
g_return_val_if_fail (g_task_is_valid (result, appinfo), FALSE);
return g_task_propagate_boolean (G_TASK (result), error);
}
static gboolean
g_win32_app_info_should_show (GAppInfo *appinfo)
{
/* FIXME: This is a placeholder implementation to avoid crashes
* for now. It can be made more specific to @appinfo in future. */
return TRUE;
}
static gboolean
g_win32_app_info_launch (GAppInfo *appinfo,
GList *files, /* (element-type GFile) */
GAppLaunchContext *launch_context,
GError **error)
{
gboolean res = FALSE;
gboolean do_uris;
GList *objs = NULL;
GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
gboolean is_uwp;
do_uris = g_win32_app_info_supports_uris (appinfo);
while (files)
{
file_or_uri *obj;
obj = g_new0 (file_or_uri, 1);
obj->file = g_file_get_path (G_FILE (files->data));
if (do_uris)
obj->uri = g_file_get_uri (G_FILE (files->data));
objs = g_list_prepend (objs, obj);
files = files->next;
}
objs = g_list_reverse (objs);
is_uwp = (info->app != NULL && info->app->is_uwp);
res = g_win32_app_info_launch_internal (info,
objs,
TRUE,
launch_context,
is_uwp ?
0 :
G_SPAWN_SEARCH_PATH,
NULL,
error);
g_list_free_full (objs, free_file_or_uri);
return res;
}
static const char **
g_win32_app_info_get_supported_types (GAppInfo *appinfo)
{
GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
return (const char**) info->supported_types;
}
GAppInfo *
g_app_info_create_from_commandline (const char *commandline,
const char *application_name,
GAppInfoCreateFlags flags,
GError **error)
{
GWin32AppInfo *info;
GWin32AppInfoApplication *app;
gunichar2 *app_command;
g_return_val_if_fail (commandline, NULL);
app_command = g_utf8_to_utf16 (commandline, -1, NULL, NULL, NULL);
if (app_command == NULL)
return NULL;
info = g_object_new (G_TYPE_WIN32_APP_INFO, NULL);
app = g_object_new (G_TYPE_WIN32_APPINFO_APPLICATION, NULL);
app->no_open_with = FALSE;
app->user_specific = FALSE;
app->default_app = FALSE;
if (application_name)
{
app->canonical_name = g_utf8_to_utf16 (application_name,
-1,
NULL,
NULL,
NULL);
app->canonical_name_u8 = g_strdup (application_name);
app->canonical_name_folded = g_utf8_casefold (application_name, -1);
}
app_add_verb (app,
app,
L"open",
app_command,
commandline,
"open",
TRUE,
FALSE);
g_clear_pointer (&app_command, g_free);
info->app = app;
info->handler = NULL;
return G_APP_INFO (info);
}
/* GAppInfo interface init */
static void
g_win32_app_info_iface_init (GAppInfoIface *iface)
{
iface->dup = g_win32_app_info_dup;
iface->equal = g_win32_app_info_equal;
iface->get_id = g_win32_app_info_get_id;
iface->get_name = g_win32_app_info_get_name;
iface->get_description = g_win32_app_info_get_description;
iface->get_executable = g_win32_app_info_get_executable;
iface->get_icon = g_win32_app_info_get_icon;
iface->launch = g_win32_app_info_launch;
iface->supports_uris = g_win32_app_info_supports_uris;
iface->supports_files = g_win32_app_info_supports_files;
iface->launch_uris = g_win32_app_info_launch_uris;
iface->launch_uris_async = g_win32_app_info_launch_uris_async;
iface->launch_uris_finish = g_win32_app_info_launch_uris_finish;
iface->should_show = g_win32_app_info_should_show;
/* iface->set_as_default_for_type = g_win32_app_info_set_as_default_for_type;*/
/* iface->set_as_default_for_extension = g_win32_app_info_set_as_default_for_extension;*/
/* iface->add_supports_type = g_win32_app_info_add_supports_type;*/
/* iface->can_remove_supports_type = g_win32_app_info_can_remove_supports_type;*/
/* iface->remove_supports_type = g_win32_app_info_remove_supports_type;*/
/* iface->can_delete = g_win32_app_info_can_delete;*/
/* iface->do_delete = g_win32_app_info_delete;*/
iface->get_commandline = g_win32_app_info_get_commandline;
iface->get_display_name = g_win32_app_info_get_display_name;
/* iface->set_as_last_used_for_type = g_win32_app_info_set_as_last_used_for_type;*/
iface->get_supported_types = g_win32_app_info_get_supported_types;
}
GAppInfo *
g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
{
GWin32AppInfoURLSchema *scheme = NULL;
char *scheme_down;
GAppInfo *result;
GWin32AppInfoShellVerb *shverb;
scheme_down = g_utf8_casefold (uri_scheme, -1);
if (!scheme_down)
return NULL;
if (strcmp (scheme_down, "file") == 0)
{
g_free (scheme_down);
return NULL;
}
gio_win32_appinfo_init (TRUE);
g_mutex_lock (&gio_win32_appinfo_mutex);
g_set_object (&scheme, g_hash_table_lookup (urls, scheme_down));
g_free (scheme_down);
g_mutex_unlock (&gio_win32_appinfo_mutex);
result = NULL;
if (scheme != NULL &&
scheme->chosen_handler != NULL &&
scheme->chosen_handler->verbs->len > 0 &&
(shverb = _verb_idx (scheme->chosen_handler->verbs, 0))->app != NULL)
result = g_win32_app_info_new_from_app (shverb->app,
scheme->chosen_handler);
g_clear_object (&scheme);
return result;
}
GAppInfo *
g_app_info_get_default_for_type (const char *content_type,
gboolean must_support_uris)
{
GWin32AppInfoFileExtension *ext = NULL;
char *ext_down;
GAppInfo *result;
GWin32AppInfoShellVerb *shverb;
ext_down = g_utf8_casefold (content_type, -1);
if (!ext_down)
return NULL;
gio_win32_appinfo_init (TRUE);
g_mutex_lock (&gio_win32_appinfo_mutex);
/* Assuming that "content_type" is a file extension, not a MIME type */
g_set_object (&ext, g_hash_table_lookup (extensions, ext_down));
g_free (ext_down);
g_mutex_unlock (&gio_win32_appinfo_mutex);
if (ext == NULL)
return NULL;
result = NULL;
if (ext->chosen_handler != NULL &&
ext->chosen_handler->verbs->len > 0 &&
(shverb = _verb_idx (ext->chosen_handler->verbs, 0))->app != NULL &&
(!must_support_uris ||
g_win32_app_supports_uris (shverb->app)))
result = g_win32_app_info_new_from_app (shverb->app,
ext->chosen_handler);
else
{
GHashTableIter iter;
GWin32AppInfoHandler *handler;
g_hash_table_iter_init (&iter, ext->handlers);
while (result == NULL &&
g_hash_table_iter_next (&iter, NULL, (gpointer *) &handler))
{
if (handler->verbs->len == 0)
continue;
shverb = _verb_idx (handler->verbs, 0);
if (shverb->app &&
(!must_support_uris ||
g_win32_app_supports_uris (shverb->app)))
result = g_win32_app_info_new_from_app (shverb->app, handler);
}
}
g_clear_object (&ext);
return result;
}
GList *
g_app_info_get_all (void)
{
GHashTableIter iter;
gpointer value;
GList *infos;
GList *apps;
GList *apps_i;
gio_win32_appinfo_init (TRUE);
g_mutex_lock (&gio_win32_appinfo_mutex);
apps = NULL;
g_hash_table_iter_init (&iter, apps_by_id);
while (g_hash_table_iter_next (&iter, NULL, &value))
apps = g_list_prepend (apps, g_object_ref (G_OBJECT (value)));
g_mutex_unlock (&gio_win32_appinfo_mutex);
infos = NULL;
for (apps_i = apps; apps_i; apps_i = apps_i->next)
infos = g_list_prepend (infos,
g_win32_app_info_new_from_app (apps_i->data, NULL));
g_list_free_full (apps, g_object_unref);
return infos;
}
GList *
g_app_info_get_all_for_type (const char *content_type)
{
GWin32AppInfoFileExtension *ext = NULL;
char *ext_down;
GWin32AppInfoHandler *handler;
GHashTableIter iter;
GHashTable *apps = NULL;
GList *result;
GWin32AppInfoShellVerb *shverb;
ext_down = g_utf8_casefold (content_type, -1);
if (!ext_down)
return NULL;
gio_win32_appinfo_init (TRUE);
g_mutex_lock (&gio_win32_appinfo_mutex);
/* Assuming that "content_type" is a file extension, not a MIME type */
g_set_object (&ext, g_hash_table_lookup (extensions, ext_down));
g_free (ext_down);
g_mutex_unlock (&gio_win32_appinfo_mutex);
if (ext == NULL)
return NULL;
result = NULL;
/* Used as a set to ensure uniqueness */
apps = g_hash_table_new (g_direct_hash, g_direct_equal);
if (ext->chosen_handler != NULL &&
ext->chosen_handler->verbs->len > 0 &&
(shverb = _verb_idx (ext->chosen_handler->verbs, 0))->app != NULL)
{
g_hash_table_add (apps, shverb->app);
result = g_list_prepend (result,
g_win32_app_info_new_from_app (shverb->app,
ext->chosen_handler));
}
g_hash_table_iter_init (&iter, ext->handlers);
while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &handler))
{
gsize vi;
for (vi = 0; vi < handler->verbs->len; vi++)
{
shverb = _verb_idx (handler->verbs, vi);
if (shverb->app == NULL ||
g_hash_table_contains (apps, shverb->app))
continue;
g_hash_table_add (apps, shverb->app);
result = g_list_prepend (result,
g_win32_app_info_new_from_app (shverb->app,
handler));
}
}
g_clear_object (&ext);
result = g_list_reverse (result);
g_hash_table_unref (apps);
return result;
}
GList *
g_app_info_get_fallback_for_type (const gchar *content_type)
{
/* TODO: fix this once gcontenttype support is improved */
return g_app_info_get_all_for_type (content_type);
}
GList *
g_app_info_get_recommended_for_type (const gchar *content_type)
{
/* TODO: fix this once gcontenttype support is improved */
return g_app_info_get_all_for_type (content_type);
}
void
g_app_info_reset_type_associations (const char *content_type)
{
/* nothing to do */
}
|