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
|
/* GTK - The GIMP Toolkit
* gtkprintbackendcups.h: Default implementation of GtkPrintBackend
* for the Common Unix Print System (CUPS)
* Copyright (C) 2006, 2007 Red Hat, Inc.
*
* 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 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, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef __linux__
#define _GNU_SOURCE
#endif
#include "config.h"
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <time.h>
/* Cups 1.6 deprecates ppdFindAttr(), ppdFindCustomOption(),
* ppdFirstCustomParam(), and ppdNextCustomParam() among others. This
* turns off the warning so that it will compile.
*/
#ifdef HAVE_CUPS_API_1_6
# define _PPD_DEPRECATED
#endif
#include <cups/cups.h>
#include <cups/language.h>
#include <cups/http.h>
#include <cups/ipp.h>
#include <errno.h>
#include <cairo.h>
#include <cairo-pdf.h>
#include <cairo-ps.h>
#include <glib/gstdio.h>
#include <glib/gi18n-lib.h>
#include <gmodule.h>
#include <gtk/gtk.h>
#include <gtk/gtkprintbackend.h>
#include <gtk/gtkunixprint.h>
#include <gtk/gtkprinter-private.h>
#include "gtkprintbackendcups.h"
#include "gtkprintercups.h"
#include "gtkcupsutils.h"
typedef struct _GtkPrintBackendCupsClass GtkPrintBackendCupsClass;
#define GTK_PRINT_BACKEND_CUPS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_PRINT_BACKEND_CUPS, GtkPrintBackendCupsClass))
#define GTK_IS_PRINT_BACKEND_CUPS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_PRINT_BACKEND_CUPS))
#define GTK_PRINT_BACKEND_CUPS_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_PRINT_BACKEND_CUPS, GtkPrintBackendCupsClass))
#define _CUPS_MAX_ATTEMPTS 10
#define _CUPS_MAX_CHUNK_SIZE 8192
#ifdef HAVE_CUPS_API_1_6
#define AVAHI_IF_UNSPEC -1
#define AVAHI_PROTO_INET 0
#define AVAHI_PROTO_INET6 1
#define AVAHI_PROTO_UNSPEC -1
#define AVAHI_BUS "org.freedesktop.Avahi"
#define AVAHI_SERVER_IFACE "org.freedesktop.Avahi.Server"
#define AVAHI_SERVICE_BROWSER_IFACE "org.freedesktop.Avahi.ServiceBrowser"
#endif
/* define this to see warnings about ignored ppd options */
#undef PRINT_IGNORED_OPTIONS
#define _CUPS_MAP_ATTR_INT(attr, v, a) {if (!g_ascii_strcasecmp (attr->name, (a))) v = attr->values[0].integer;}
#define _CUPS_MAP_ATTR_STR(attr, v, a) {if (!g_ascii_strcasecmp (attr->name, (a))) v = attr->values[0].string.text;}
static GType print_backend_cups_type = 0;
typedef void (* GtkPrintCupsResponseCallbackFunc) (GtkPrintBackend *print_backend,
GtkCupsResult *result,
gpointer user_data);
typedef enum
{
DISPATCH_SETUP,
DISPATCH_REQUEST,
DISPATCH_SEND,
DISPATCH_CHECK,
DISPATCH_READ,
DISPATCH_ERROR
} GtkPrintCupsDispatchState;
typedef struct
{
GSource source;
http_t *http;
GtkCupsRequest *request;
GtkCupsPollState poll_state;
GPollFD *data_poll;
GtkPrintBackendCups *backend;
GtkPrintCupsResponseCallbackFunc callback;
gpointer callback_data;
} GtkPrintCupsDispatchWatch;
struct _GtkPrintBackendCupsClass
{
GtkPrintBackendClass parent_class;
};
struct _GtkPrintBackendCups
{
GtkPrintBackend parent_instance;
char *default_printer;
guint list_printers_poll;
guint list_printers_pending : 1;
gint list_printers_attempts;
guint got_default_printer : 1;
guint default_printer_poll;
GtkCupsConnectionTest *cups_connection_test;
gint reading_ppds;
char **covers;
int number_of_covers;
GList *requests;
GHashTable *auth;
gchar *username;
gboolean authentication_lock;
#ifdef HAVE_CUPS_API_1_6
GDBusConnection *dbus_connection;
gchar *avahi_default_printer;
guint avahi_service_browser_subscription_id;
guint avahi_service_browser_subscription_ids[2];
gchar *avahi_service_browser_paths[2];
GCancellable *avahi_cancellable;
#endif
};
static GObjectClass *backend_parent_class;
static void gtk_print_backend_cups_class_init (GtkPrintBackendCupsClass *class);
static void gtk_print_backend_cups_init (GtkPrintBackendCups *impl);
static void gtk_print_backend_cups_finalize (GObject *object);
static void gtk_print_backend_cups_dispose (GObject *object);
static void cups_get_printer_list (GtkPrintBackend *print_backend);
static void cups_get_default_printer (GtkPrintBackendCups *print_backend);
static void cups_get_local_default_printer (GtkPrintBackendCups *print_backend);
static void cups_request_execute (GtkPrintBackendCups *print_backend,
GtkCupsRequest *request,
GtkPrintCupsResponseCallbackFunc callback,
gpointer user_data,
GDestroyNotify notify);
static void cups_printer_get_settings_from_options (GtkPrinter *printer,
GtkPrinterOptionSet *options,
GtkPrintSettings *settings);
static gboolean cups_printer_mark_conflicts (GtkPrinter *printer,
GtkPrinterOptionSet *options);
static GtkPrinterOptionSet *cups_printer_get_options (GtkPrinter *printer,
GtkPrintSettings *settings,
GtkPageSetup *page_setup,
GtkPrintCapabilities capabilities);
static void cups_printer_prepare_for_print (GtkPrinter *printer,
GtkPrintJob *print_job,
GtkPrintSettings *settings,
GtkPageSetup *page_setup);
static GList * cups_printer_list_papers (GtkPrinter *printer);
static GtkPageSetup * cups_printer_get_default_page_size (GtkPrinter *printer);
static void cups_printer_request_details (GtkPrinter *printer);
static gboolean cups_request_default_printer (GtkPrintBackendCups *print_backend);
static gboolean cups_request_ppd (GtkPrinter *printer);
static gboolean cups_printer_get_hard_margins (GtkPrinter *printer,
gdouble *top,
gdouble *bottom,
gdouble *left,
gdouble *right);
static GtkPrintCapabilities cups_printer_get_capabilities (GtkPrinter *printer);
static void set_option_from_settings (GtkPrinterOption *option,
GtkPrintSettings *setting);
static void cups_begin_polling_info (GtkPrintBackendCups *print_backend,
GtkPrintJob *job,
int job_id);
static gboolean cups_job_info_poll_timeout (gpointer user_data);
static void gtk_print_backend_cups_print_stream (GtkPrintBackend *backend,
GtkPrintJob *job,
GIOChannel *data_io,
GtkPrintJobCompleteFunc callback,
gpointer user_data,
GDestroyNotify dnotify);
static cairo_surface_t * cups_printer_create_cairo_surface (GtkPrinter *printer,
GtkPrintSettings *settings,
gdouble width,
gdouble height,
GIOChannel *cache_io);
static void gtk_print_backend_cups_set_password (GtkPrintBackend *backend,
gchar **auth_info_required,
gchar **auth_info);
void overwrite_and_free (gpointer data);
static gboolean is_address_local (const gchar *address);
static gboolean request_auth_info (gpointer data);
#ifdef HAVE_CUPS_API_1_6
static void avahi_request_printer_list (GtkPrintBackendCups *cups_backend);
#endif
static void
gtk_print_backend_cups_register_type (GTypeModule *module)
{
const GTypeInfo print_backend_cups_info =
{
sizeof (GtkPrintBackendCupsClass),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) gtk_print_backend_cups_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (GtkPrintBackendCups),
0, /* n_preallocs */
(GInstanceInitFunc) gtk_print_backend_cups_init
};
print_backend_cups_type = g_type_module_register_type (module,
GTK_TYPE_PRINT_BACKEND,
"GtkPrintBackendCups",
&print_backend_cups_info, 0);
}
G_MODULE_EXPORT void
pb_module_init (GTypeModule *module)
{
GTK_NOTE (PRINTING,
g_print ("CUPS Backend: Initializing the CUPS print backend module\n"));
gtk_print_backend_cups_register_type (module);
gtk_printer_cups_register_type (module);
}
G_MODULE_EXPORT void
pb_module_exit (void)
{
}
G_MODULE_EXPORT GtkPrintBackend *
pb_module_create (void)
{
return gtk_print_backend_cups_new ();
}
/* CUPS 1.6 Getter/Setter Functions CUPS 1.6 makes private most of the
* IPP structures and enforces access via new getter functions, which
* are unfortunately not available in earlier versions. We define
* below those getter functions as macros for use when building
* against earlier CUPS versions.
*/
#ifndef HAVE_CUPS_API_1_6
#define ippGetOperation(ipp_request) ipp_request->request.op.operation_id
#define ippGetInteger(attr, index) attr->values[index].integer
#define ippGetBoolean(attr, index) attr->values[index].boolean
#define ippGetString(attr, index, foo) attr->values[index].string.text
#define ippGetValueTag(attr) attr->value_tag
#define ippGetName(attr) attr->name
#define ippGetCount(attr) attr->num_values
#define ippGetGroupTag(attr) attr->group_tag
static int
ippGetRange (ipp_attribute_t *attr,
int element,
int *upper)
{
*upper = attr->values[element].range.upper;
return (attr->values[element].range.lower);
}
#endif
/*
* GtkPrintBackendCups
*/
GType
gtk_print_backend_cups_get_type (void)
{
return print_backend_cups_type;
}
/**
* gtk_print_backend_cups_new:
*
* Creates a new #GtkPrintBackendCups object. #GtkPrintBackendCups
* implements the #GtkPrintBackend interface with direct access to
* the filesystem using Unix/Linux API calls
*
* Return value: the new #GtkPrintBackendCups object
*/
GtkPrintBackend *
gtk_print_backend_cups_new (void)
{
GTK_NOTE (PRINTING,
g_print ("CUPS Backend: Creating a new CUPS print backend object\n"));
return g_object_new (GTK_TYPE_PRINT_BACKEND_CUPS, NULL);
}
static void
gtk_print_backend_cups_class_init (GtkPrintBackendCupsClass *class)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (class);
GtkPrintBackendClass *backend_class = GTK_PRINT_BACKEND_CLASS (class);
backend_parent_class = g_type_class_peek_parent (class);
gobject_class->finalize = gtk_print_backend_cups_finalize;
gobject_class->dispose = gtk_print_backend_cups_dispose;
backend_class->request_printer_list = cups_get_printer_list;
backend_class->print_stream = gtk_print_backend_cups_print_stream;
backend_class->printer_request_details = cups_printer_request_details;
backend_class->printer_create_cairo_surface = cups_printer_create_cairo_surface;
backend_class->printer_get_options = cups_printer_get_options;
backend_class->printer_mark_conflicts = cups_printer_mark_conflicts;
backend_class->printer_get_settings_from_options = cups_printer_get_settings_from_options;
backend_class->printer_prepare_for_print = cups_printer_prepare_for_print;
backend_class->printer_list_papers = cups_printer_list_papers;
backend_class->printer_get_default_page_size = cups_printer_get_default_page_size;
backend_class->printer_get_hard_margins = cups_printer_get_hard_margins;
backend_class->printer_get_capabilities = cups_printer_get_capabilities;
backend_class->set_password = gtk_print_backend_cups_set_password;
}
static cairo_status_t
_cairo_write_to_cups (void *closure,
const unsigned char *data,
unsigned int length)
{
GIOChannel *io = (GIOChannel *)closure;
gsize written;
GError *error;
error = NULL;
GTK_NOTE (PRINTING,
g_print ("CUPS Backend: Writing %i byte chunk to temp file\n", length));
while (length > 0)
{
g_io_channel_write_chars (io, (gchar *)data, length, &written, &error);
if (error != NULL)
{
GTK_NOTE (PRINTING,
g_print ("CUPS Backend: Error writing to temp file, %s\n",
error->message));
g_error_free (error);
return CAIRO_STATUS_WRITE_ERROR;
}
GTK_NOTE (PRINTING,
g_print ("CUPS Backend: Wrote %" G_GSIZE_FORMAT " bytes to temp file\n", written));
data += written;
length -= written;
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_surface_t *
cups_printer_create_cairo_surface (GtkPrinter *printer,
GtkPrintSettings *settings,
gdouble width,
gdouble height,
GIOChannel *cache_io)
{
cairo_surface_t *surface;
ppd_file_t *ppd_file = NULL;
ppd_attr_t *ppd_attr = NULL;
ppd_attr_t *ppd_attr_res = NULL;
ppd_attr_t *ppd_attr_screen_freq = NULL;
ppd_attr_t *ppd_attr_res_screen_freq = NULL;
gchar *res_string = NULL;
gint level = 2;
if (gtk_printer_accepts_pdf (printer))
surface = cairo_pdf_surface_create_for_stream (_cairo_write_to_cups, cache_io, width, height);
else
surface = cairo_ps_surface_create_for_stream (_cairo_write_to_cups, cache_io, width, height);
ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
if (ppd_file != NULL)
{
ppd_attr = ppdFindAttr (ppd_file, "LanguageLevel", NULL);
if (ppd_attr != NULL)
level = atoi (ppd_attr->value);
if (gtk_print_settings_get_resolution (settings) == 0)
{
ppd_attr_res = ppdFindAttr (ppd_file, "DefaultResolution", NULL);
if (ppd_attr_res != NULL)
{
int res, res_x, res_y;
if (sscanf (ppd_attr_res->value, "%dx%ddpi", &res_x, &res_y) == 2)
{
if (res_x > 0 && res_y > 0)
gtk_print_settings_set_resolution_xy (settings, res_x, res_y);
}
else if (sscanf (ppd_attr_res->value, "%ddpi", &res) == 1)
{
if (res > 0)
gtk_print_settings_set_resolution (settings, res);
}
}
}
res_string = g_strdup_printf ("%ddpi",
gtk_print_settings_get_resolution (settings));
ppd_attr_res_screen_freq = ppdFindAttr (ppd_file, "ResScreenFreq", res_string);
g_free (res_string);
if (ppd_attr_res_screen_freq == NULL)
{
res_string = g_strdup_printf ("%dx%ddpi",
gtk_print_settings_get_resolution_x (settings),
gtk_print_settings_get_resolution_y (settings));
ppd_attr_res_screen_freq = ppdFindAttr (ppd_file, "ResScreenFreq", res_string);
g_free (res_string);
}
ppd_attr_screen_freq = ppdFindAttr (ppd_file, "ScreenFreq", NULL);
if (ppd_attr_res_screen_freq != NULL && atof (ppd_attr_res_screen_freq->value) > 0.0)
gtk_print_settings_set_printer_lpi (settings, atof (ppd_attr_res_screen_freq->value));
else if (ppd_attr_screen_freq != NULL && atof (ppd_attr_screen_freq->value) > 0.0)
gtk_print_settings_set_printer_lpi (settings, atof (ppd_attr_screen_freq->value));
}
if (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_PS)
{
if (level == 2)
cairo_ps_surface_restrict_to_level (surface, CAIRO_PS_LEVEL_2);
if (level == 3)
cairo_ps_surface_restrict_to_level (surface, CAIRO_PS_LEVEL_3);
}
cairo_surface_set_fallback_resolution (surface,
2.0 * gtk_print_settings_get_printer_lpi (settings),
2.0 * gtk_print_settings_get_printer_lpi (settings));
return surface;
}
typedef struct {
GtkPrintJobCompleteFunc callback;
GtkPrintJob *job;
gpointer user_data;
GDestroyNotify dnotify;
} CupsPrintStreamData;
static void
cups_free_print_stream_data (CupsPrintStreamData *data)
{
GTK_NOTE (PRINTING,
g_print ("CUPS Backend: %s\n", G_STRFUNC));
if (data->dnotify)
data->dnotify (data->user_data);
g_object_unref (data->job);
g_free (data);
}
static void
cups_print_cb (GtkPrintBackendCups *print_backend,
GtkCupsResult *result,
gpointer user_data)
{
GError *error = NULL;
CupsPrintStreamData *ps = user_data;
GDK_THREADS_ENTER ();
GTK_NOTE (PRINTING,
g_print ("CUPS Backend: %s\n", G_STRFUNC));
if (gtk_cups_result_is_error (result))
error = g_error_new_literal (gtk_print_error_quark (),
GTK_PRINT_ERROR_INTERNAL_ERROR,
gtk_cups_result_get_error_string (result));
if (ps->callback)
ps->callback (ps->job, ps->user_data, error);
if (error == NULL)
{
int job_id = 0;
ipp_attribute_t *attr; /* IPP job-id attribute */
ipp_t *response = gtk_cups_result_get_response (result);
if ((attr = ippFindAttribute (response, "job-id", IPP_TAG_INTEGER)) != NULL)
job_id = ippGetInteger (attr, 0);
if (!gtk_print_job_get_track_print_status (ps->job) || job_id == 0)
gtk_print_job_set_status (ps->job, GTK_PRINT_STATUS_FINISHED);
else
{
gtk_print_job_set_status (ps->job, GTK_PRINT_STATUS_PENDING);
cups_begin_polling_info (print_backend, ps->job, job_id);
}
}
else
gtk_print_job_set_status (ps->job, GTK_PRINT_STATUS_FINISHED_ABORTED);
if (error)
g_error_free (error);
GDK_THREADS_LEAVE ();
}
typedef struct {
GtkCupsRequest *request;
GtkPrinterCups *printer;
} CupsOptionsData;
static void
add_cups_options (const gchar *key,
const gchar *value,
gpointer user_data)
{
CupsOptionsData *data = (CupsOptionsData *) user_data;
GtkCupsRequest *request = data->request;
GtkPrinterCups *printer = data->printer;
gboolean custom_value = FALSE;
gchar *new_value = NULL;
gint i;
if (!key || !value)
return;
if (!g_str_has_prefix (key, "cups-"))
return;
if (strcmp (value, "gtk-ignore-value") == 0)
return;
key = key + strlen ("cups-");
if (printer && printer->ppd_file)
{
ppd_coption_t *coption;
gboolean found = FALSE;
gboolean custom_values_enabled = FALSE;
coption = ppdFindCustomOption (printer->ppd_file, key);
if (coption && coption->option)
{
for (i = 0; i < coption->option->num_choices; i++)
{
/* Are custom values enabled ? */
if (g_str_equal (coption->option->choices[i].choice, "Custom"))
custom_values_enabled = TRUE;
/* Is the value among available choices ? */
if (g_str_equal (coption->option->choices[i].choice, value))
found = TRUE;
}
if (custom_values_enabled && !found)
custom_value = TRUE;
}
}
/* Add "Custom." prefix to custom values if not already added. */
if (custom_value && !g_str_has_prefix (value, "Custom."))
{
new_value = g_strdup_printf ("Custom.%s", value);
gtk_cups_request_encode_option (request, key, new_value);
g_free (new_value);
}
else
gtk_cups_request_encode_option (request, key, value);
}
static void
gtk_print_backend_cups_print_stream (GtkPrintBackend *print_backend,
GtkPrintJob *job,
GIOChannel *data_io,
GtkPrintJobCompleteFunc callback,
gpointer user_data,
GDestroyNotify dnotify)
{
GtkPrinterCups *cups_printer;
CupsPrintStreamData *ps;
CupsOptionsData *options_data;
GtkCupsRequest *request = NULL;
GtkPrintSettings *settings;
const gchar *title;
char printer_absolute_uri[HTTP_MAX_URI];
GTK_NOTE (PRINTING,
g_print ("CUPS Backend: %s\n", G_STRFUNC));
cups_printer = GTK_PRINTER_CUPS (gtk_print_job_get_printer (job));
settings = gtk_print_job_get_settings (job);
request = gtk_cups_request_new_with_username (NULL,
GTK_CUPS_POST,
IPP_PRINT_JOB,
data_io,
NULL,
cups_printer->device_uri,
GTK_PRINT_BACKEND_CUPS (print_backend)->username);
#ifdef HAVE_CUPS_API_1_6
if (cups_printer->avahi_browsed)
{
http_t *http;
http = httpConnect (cups_printer->hostname, cups_printer->port);
if (http)
{
request = gtk_cups_request_new_with_username (http,
GTK_CUPS_POST,
IPP_PRINT_JOB,
data_io,
cups_printer->hostname,
cups_printer->device_uri,
GTK_PRINT_BACKEND_CUPS (print_backend)->username);
g_snprintf (printer_absolute_uri, HTTP_MAX_URI, "%s", cups_printer->printer_uri);
}
else
{
GError *error = NULL;
GTK_NOTE (PRINTING,
g_warning ("CUPS Backend: Error connecting to %s:%d",
cups_printer->hostname,
cups_printer->port));
error = g_error_new (gtk_print_error_quark (),
GTK_CUPS_ERROR_GENERAL,
"Error connecting to %s",
cups_printer->hostname);
gtk_print_job_set_status (job, GTK_PRINT_STATUS_FINISHED_ABORTED);
if (callback)
{
callback (job, user_data, error);
}
g_clear_error (&error);
return;
}
}
else
#endif
{
request = gtk_cups_request_new_with_username (NULL,
GTK_CUPS_POST,
IPP_PRINT_JOB,
data_io,
NULL,
cups_printer->device_uri,
GTK_PRINT_BACKEND_CUPS (print_backend)->username);
#if (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR >= 2) || CUPS_VERSION_MAJOR > 1
httpAssembleURIf (HTTP_URI_CODING_ALL,
printer_absolute_uri,
sizeof (printer_absolute_uri),
"ipp",
NULL,
"localhost",
ippPort (),
"/printers/%s",
gtk_printer_get_name (gtk_print_job_get_printer (job)));
#else
g_snprintf (printer_absolute_uri,
sizeof (printer_absolute_uri),
"ipp://localhost:%d/printers/%s",
ippPort (),
gtk_printer_get_name (gtk_print_job_get_printer (job)));
#endif
}
gtk_cups_request_set_ipp_version (request,
cups_printer->ipp_version_major,
cups_printer->ipp_version_minor);
gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION,
IPP_TAG_URI, "printer-uri",
NULL, printer_absolute_uri);
title = gtk_print_job_get_title (job);
if (title)
gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION,
IPP_TAG_NAME, "job-name",
NULL, title);
options_data = g_new0 (CupsOptionsData, 1);
options_data->request = request;
options_data->printer = cups_printer;
gtk_print_settings_foreach (settings, add_cups_options, options_data);
g_free (options_data);
ps = g_new0 (CupsPrintStreamData, 1);
ps->callback = callback;
ps->user_data = user_data;
ps->dnotify = dnotify;
ps->job = g_object_ref (job);
request->need_auth_info = cups_printer->auth_info_required != NULL;
request->auth_info_required = g_strdupv (cups_printer->auth_info_required);
cups_request_execute (GTK_PRINT_BACKEND_CUPS (print_backend),
request,
(GtkPrintCupsResponseCallbackFunc) cups_print_cb,
ps,
(GDestroyNotify)cups_free_print_stream_data);
}
void overwrite_and_free (gpointer data)
{
gchar *password = (gchar *) data;
if (password != NULL)
{
memset (password, 0, strlen (password));
g_free (password);
}
}
static void
gtk_print_backend_cups_init (GtkPrintBackendCups *backend_cups)
{
#ifdef HAVE_CUPS_API_1_6
gint i;
#endif
backend_cups->list_printers_poll = FALSE;
backend_cups->got_default_printer = FALSE;
backend_cups->list_printers_pending = FALSE;
backend_cups->list_printers_attempts = 0;
backend_cups->reading_ppds = 0;
backend_cups->requests = NULL;
backend_cups->auth = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, overwrite_and_free);
backend_cups->authentication_lock = FALSE;
backend_cups->covers = NULL;
backend_cups->number_of_covers = 0;
backend_cups->default_printer_poll = 0;
backend_cups->cups_connection_test = NULL;
backend_cups->username = NULL;
#ifdef HAVE_CUPS_API_1_6
backend_cups->dbus_connection = NULL;
backend_cups->avahi_default_printer = NULL;
backend_cups->avahi_service_browser_subscription_id = 0;
for (i = 0; i < 2; i++)
{
backend_cups->avahi_service_browser_paths[i] = NULL;
backend_cups->avahi_service_browser_subscription_ids[i] = 0;
}
#endif
cups_get_local_default_printer (backend_cups);
}
static void
gtk_print_backend_cups_finalize (GObject *object)
{
GtkPrintBackendCups *backend_cups;
GTK_NOTE (PRINTING,
g_print ("CUPS Backend: finalizing CUPS backend module\n"));
backend_cups = GTK_PRINT_BACKEND_CUPS (object);
g_free (backend_cups->default_printer);
backend_cups->default_printer = NULL;
g_strfreev (backend_cups->covers);
backend_cups->number_of_covers = 0;
gtk_cups_connection_test_free (backend_cups->cups_connection_test);
backend_cups->cups_connection_test = NULL;
g_hash_table_destroy (backend_cups->auth);
g_free (backend_cups->username);
#ifdef HAVE_CUPS_API_1_6
g_clear_object (&backend_cups->avahi_cancellable);
g_free (backend_cups->avahi_default_printer);
backend_cups->avahi_default_printer = NULL;
g_clear_object (&backend_cups->dbus_connection);
#endif
backend_parent_class->finalize (object);
}
static void
gtk_print_backend_cups_dispose (GObject *object)
{
GtkPrintBackendCups *backend_cups;
#ifdef HAVE_CUPS_API_1_6
gint i;
#endif
GTK_NOTE (PRINTING,
g_print ("CUPS Backend: %s\n", G_STRFUNC));
backend_cups = GTK_PRINT_BACKEND_CUPS (object);
if (backend_cups->list_printers_poll > 0)
g_source_remove (backend_cups->list_printers_poll);
backend_cups->list_printers_poll = 0;
backend_cups->list_printers_attempts = 0;
if (backend_cups->default_printer_poll > 0)
g_source_remove (backend_cups->default_printer_poll);
backend_cups->default_printer_poll = 0;
#ifdef HAVE_CUPS_API_1_6
g_cancellable_cancel (backend_cups->avahi_cancellable);
for (i = 0; i < 2; i++)
{
if (backend_cups->avahi_service_browser_subscription_ids[i] > 0)
{
g_dbus_connection_signal_unsubscribe (backend_cups->dbus_connection,
backend_cups->avahi_service_browser_subscription_ids[i]);
backend_cups->avahi_service_browser_subscription_ids[i] = 0;
}
if (backend_cups->avahi_service_browser_paths[i])
{
g_dbus_connection_call (backend_cups->dbus_connection,
AVAHI_BUS,
backend_cups->avahi_service_browser_paths[i],
AVAHI_SERVICE_BROWSER_IFACE,
"Free",
NULL,
NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
NULL,
NULL);
g_free (backend_cups->avahi_service_browser_paths[i]);
backend_cups->avahi_service_browser_paths[i] = NULL;
}
}
if (backend_cups->avahi_service_browser_subscription_id > 0)
{
g_dbus_connection_signal_unsubscribe (backend_cups->dbus_connection,
backend_cups->avahi_service_browser_subscription_id);
backend_cups->avahi_service_browser_subscription_id = 0;
}
#endif
backend_parent_class->dispose (object);
}
static gboolean
is_address_local (const gchar *address)
{
if (address[0] == '/' ||
strcmp (address, "127.0.0.1") == 0 ||
strcmp (address, "[::1]") == 0)
return TRUE;
else
return FALSE;
}
#ifndef HAVE_CUPS_API_1_2
/* Included from CUPS library because of backward compatibility */
const char *
httpGetHostname(http_t *http,
char *s,
int slen)
{
struct hostent *host;
if (!s || slen <= 1)
return (NULL);
if (http)
{
if (http->hostname[0] == '/')
g_strlcpy (s, "localhost", slen);
else
g_strlcpy (s, http->hostname, slen);
}
else
{
if (gethostname (s, slen) < 0)
g_strlcpy (s, "localhost", slen);
if (!strchr (s, '.'))
{
if ((host = gethostbyname (s)) != NULL && host->h_name)
g_strlcpy (s, host->h_name, slen);
}
}
return (s);
}
#endif
static void
gtk_print_backend_cups_set_password (GtkPrintBackend *backend,
gchar **auth_info_required,
gchar **auth_info)
{
GtkPrintBackendCups *cups_backend = GTK_PRINT_BACKEND_CUPS (backend);
GList *l;
char dispatch_hostname[HTTP_MAX_URI];
gchar *key;
gchar *username = NULL;
gchar *hostname = NULL;
gchar *password = NULL;
gint length;
gint i;
length = g_strv_length (auth_info_required);
if (auth_info != NULL)
for (i = 0; i < length; i++)
{
if (g_strcmp0 (auth_info_required[i], "username") == 0)
username = g_strdup (auth_info[i]);
else if (g_strcmp0 (auth_info_required[i], "hostname") == 0)
hostname = g_strdup (auth_info[i]);
else if (g_strcmp0 (auth_info_required[i], "password") == 0)
password = g_strdup (auth_info[i]);
}
if (hostname != NULL && username != NULL && password != NULL)
{
key = g_strconcat (username, "@", hostname, NULL);
g_hash_table_insert (cups_backend->auth, key, g_strdup (password));
}
g_free (cups_backend->username);
cups_backend->username = g_strdup (username);
GTK_NOTE (PRINTING,
g_print ("CUPS backend: storing password for %s\n", key));
for (l = cups_backend->requests; l; l = l->next)
{
GtkPrintCupsDispatchWatch *dispatch = l->data;
httpGetHostname (dispatch->request->http, dispatch_hostname, sizeof (dispatch_hostname));
if (is_address_local (dispatch_hostname))
strcpy (dispatch_hostname, "localhost");
if (dispatch->request->need_auth_info)
{
if (auth_info != NULL)
{
dispatch->request->auth_info = g_new0 (gchar *, length + 1);
for (i = 0; i < length; i++)
dispatch->request->auth_info[i] = g_strdup (auth_info[i]);
}
dispatch->backend->authentication_lock = FALSE;
dispatch->request->need_auth_info = FALSE;
}
else if (dispatch->request->password_state == GTK_CUPS_PASSWORD_REQUESTED || auth_info == NULL)
{
overwrite_and_free (dispatch->request->password);
dispatch->request->password = g_strdup (password);
g_free (dispatch->request->username);
dispatch->request->username = g_strdup (username);
dispatch->request->password_state = GTK_CUPS_PASSWORD_HAS;
dispatch->backend->authentication_lock = FALSE;
}
}
}
static gboolean
request_password (gpointer data)
{
GtkPrintCupsDispatchWatch *dispatch = data;
const gchar *username;
gchar *password;
gchar *prompt = NULL;
gchar *key = NULL;
char hostname[HTTP_MAX_URI];
gchar **auth_info_required;
gchar **auth_info_default;
gchar **auth_info_display;
gboolean *auth_info_visible;
gint length = 3;
gint i;
if (dispatch->backend->authentication_lock)
return FALSE;
httpGetHostname (dispatch->request->http, hostname, sizeof (hostname));
if (is_address_local (hostname))
strcpy (hostname, "localhost");
if (dispatch->backend->username != NULL)
username = dispatch->backend->username;
else
username = cupsUser ();
auth_info_required = g_new0 (gchar*, length + 1);
auth_info_required[0] = g_strdup ("hostname");
auth_info_required[1] = g_strdup ("username");
auth_info_required[2] = g_strdup ("password");
auth_info_default = g_new0 (gchar*, length + 1);
auth_info_default[0] = g_strdup (hostname);
auth_info_default[1] = g_strdup (username);
auth_info_display = g_new0 (gchar*, length + 1);
auth_info_display[1] = g_strdup (_("Username:"));
auth_info_display[2] = g_strdup (_("Password:"));
auth_info_visible = g_new0 (gboolean, length + 1);
auth_info_visible[1] = TRUE;
key = g_strconcat (username, "@", hostname, NULL);
password = g_hash_table_lookup (dispatch->backend->auth, key);
if (password && dispatch->request->password_state != GTK_CUPS_PASSWORD_NOT_VALID)
{
GTK_NOTE (PRINTING,
g_print ("CUPS backend: using stored password for %s\n", key));
overwrite_and_free (dispatch->request->password);
dispatch->request->password = g_strdup (password);
g_free (dispatch->request->username);
dispatch->request->username = g_strdup (username);
dispatch->request->password_state = GTK_CUPS_PASSWORD_HAS;
}
else
{
const char *job_title = gtk_cups_request_ipp_get_string (dispatch->request, IPP_TAG_NAME, "job-name");
const char *printer_uri = gtk_cups_request_ipp_get_string (dispatch->request, IPP_TAG_URI, "printer-uri");
char *printer_name = NULL;
if (printer_uri != NULL && strrchr (printer_uri, '/') != NULL)
printer_name = g_strdup (strrchr (printer_uri, '/') + 1);
if (dispatch->request->password_state == GTK_CUPS_PASSWORD_NOT_VALID)
g_hash_table_remove (dispatch->backend->auth, key);
dispatch->request->password_state = GTK_CUPS_PASSWORD_REQUESTED;
dispatch->backend->authentication_lock = TRUE;
switch (ippGetOperation (dispatch->request->ipp_request))
{
case IPP_PRINT_JOB:
if (job_title != NULL && printer_name != NULL)
prompt = g_strdup_printf ( _("Authentication is required to print document '%s' on printer %s"), job_title, printer_name);
else
prompt = g_strdup_printf ( _("Authentication is required to print a document on %s"), hostname);
break;
case IPP_GET_JOB_ATTRIBUTES:
if (job_title != NULL)
prompt = g_strdup_printf ( _("Authentication is required to get attributes of job '%s'"), job_title);
else
prompt = g_strdup ( _("Authentication is required to get attributes of a job"));
break;
case IPP_GET_PRINTER_ATTRIBUTES:
if (printer_name != NULL)
prompt = g_strdup_printf ( _("Authentication is required to get attributes of printer %s"), printer_name);
else
prompt = g_strdup ( _("Authentication is required to get attributes of a printer"));
break;
case CUPS_GET_DEFAULT:
prompt = g_strdup_printf ( _("Authentication is required to get default printer of %s"), hostname);
break;
case CUPS_GET_PRINTERS:
prompt = g_strdup_printf ( _("Authentication is required to get printers from %s"), hostname);
break;
default:
/* work around gcc warning about 0 not being a value for this enum */
if (ippGetOperation (dispatch->request->ipp_request) == 0)
prompt = g_strdup_printf ( _("Authentication is required to get a file from %s"), hostname);
else
prompt = g_strdup_printf ( _("Authentication is required on %s"), hostname);
break;
}
g_free (printer_name);
g_signal_emit_by_name (dispatch->backend, "request-password",
auth_info_required, auth_info_default, auth_info_display, auth_info_visible, prompt);
g_free (prompt);
}
for (i = 0; i < length; i++)
{
g_free (auth_info_required[i]);
g_free (auth_info_default[i]);
g_free (auth_info_display[i]);
}
g_free (auth_info_required);
g_free (auth_info_default);
g_free (auth_info_display);
g_free (auth_info_visible);
g_free (key);
return FALSE;
}
static void
cups_dispatch_add_poll (GSource *source)
{
GtkPrintCupsDispatchWatch *dispatch;
GtkCupsPollState poll_state;
dispatch = (GtkPrintCupsDispatchWatch *) source;
poll_state = gtk_cups_request_get_poll_state (dispatch->request);
/* Remove the old source if the poll state changed. */
if (poll_state != dispatch->poll_state && dispatch->data_poll != NULL)
{
g_source_remove_poll (source, dispatch->data_poll);
g_free (dispatch->data_poll);
dispatch->data_poll = NULL;
}
if (dispatch->request->http != NULL)
{
if (dispatch->data_poll == NULL)
{
dispatch->data_poll = g_new0 (GPollFD, 1);
dispatch->poll_state = poll_state;
if (poll_state == GTK_CUPS_HTTP_READ)
dispatch->data_poll->events = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI;
else if (poll_state == GTK_CUPS_HTTP_WRITE)
dispatch->data_poll->events = G_IO_OUT | G_IO_ERR;
else
dispatch->data_poll->events = 0;
#ifdef HAVE_CUPS_API_1_2
dispatch->data_poll->fd = httpGetFd (dispatch->request->http);
#else
dispatch->data_poll->fd = dispatch->request->http->fd;
#endif
g_source_add_poll (source, dispatch->data_poll);
}
}
}
static gboolean
check_auth_info (gpointer user_data)
{
GtkPrintCupsDispatchWatch *dispatch;
dispatch = (GtkPrintCupsDispatchWatch *) user_data;
if (!dispatch->request->need_auth_info)
{
if (dispatch->request->auth_info == NULL)
{
dispatch->callback (GTK_PRINT_BACKEND (dispatch->backend),
gtk_cups_request_get_result (dispatch->request),
dispatch->callback_data);
g_source_destroy ((GSource *) dispatch);
}
else
{
gint length;
gint i;
length = g_strv_length (dispatch->request->auth_info_required);
gtk_cups_request_ipp_add_strings (dispatch->request,
IPP_TAG_JOB,
IPP_TAG_TEXT,
"auth-info",
length,
NULL,
(const char * const *) dispatch->request->auth_info);
g_source_attach ((GSource *) dispatch, NULL);
g_source_unref ((GSource *) dispatch);
for (i = 0; i < length; i++)
overwrite_and_free (dispatch->request->auth_info[i]);
g_free (dispatch->request->auth_info);
dispatch->request->auth_info = NULL;
}
return FALSE;
}
return TRUE;
}
static gboolean
request_auth_info (gpointer user_data)
{
GtkPrintCupsDispatchWatch *dispatch;
const char *job_title;
const char *printer_uri;
gchar *prompt = NULL;
char *printer_name = NULL;
gint length;
gint i;
gboolean *auth_info_visible = NULL;
gchar **auth_info_default = NULL;
gchar **auth_info_display = NULL;
dispatch = (GtkPrintCupsDispatchWatch *) user_data;
if (dispatch->backend->authentication_lock)
return FALSE;
job_title = gtk_cups_request_ipp_get_string (dispatch->request, IPP_TAG_NAME, "job-name");
printer_uri = gtk_cups_request_ipp_get_string (dispatch->request, IPP_TAG_URI, "printer-uri");
length = g_strv_length (dispatch->request->auth_info_required);
auth_info_visible = g_new0 (gboolean, length);
auth_info_default = g_new0 (gchar *, length + 1);
auth_info_display = g_new0 (gchar *, length + 1);
for (i = 0; i < length; i++)
{
if (g_strcmp0 (dispatch->request->auth_info_required[i], "domain") == 0)
{
auth_info_display[i] = g_strdup (_("Domain:"));
auth_info_default[i] = g_strdup ("WORKGROUP");
auth_info_visible[i] = TRUE;
}
else if (g_strcmp0 (dispatch->request->auth_info_required[i], "username") == 0)
{
auth_info_display[i] = g_strdup (_("Username:"));
if (dispatch->backend->username != NULL)
auth_info_default[i] = g_strdup (dispatch->backend->username);
else
auth_info_default[i] = g_strdup (cupsUser ());
auth_info_visible[i] = TRUE;
}
else if (g_strcmp0 (dispatch->request->auth_info_required[i], "password") == 0)
{
auth_info_display[i] = g_strdup (_("Password:"));
auth_info_visible[i] = FALSE;
}
}
if (printer_uri != NULL && strrchr (printer_uri, '/') != NULL)
printer_name = g_strdup (strrchr (printer_uri, '/') + 1);
dispatch->backend->authentication_lock = TRUE;
if (job_title != NULL)
{
if (printer_name != NULL)
prompt = g_strdup_printf ( _("Authentication is required to print document '%s' on printer %s"), job_title, printer_name);
else
prompt = g_strdup_printf ( _("Authentication is required to print document '%s'"), job_title);
}
else
{
if (printer_name != NULL)
prompt = g_strdup_printf ( _("Authentication is required to print this document on printer %s"), printer_name);
else
prompt = g_strdup ( _("Authentication is required to print this document"));
}
g_signal_emit_by_name (dispatch->backend, "request-password",
dispatch->request->auth_info_required,
auth_info_default,
auth_info_display,
auth_info_visible,
prompt);
for (i = 0; i < length; i++)
{
g_free (auth_info_default[i]);
g_free (auth_info_display[i]);
}
g_free (auth_info_default);
g_free (auth_info_display);
g_free (printer_name);
g_free (prompt);
g_idle_add (check_auth_info, user_data);
return FALSE;
}
static gboolean
cups_dispatch_watch_check (GSource *source)
{
GtkPrintCupsDispatchWatch *dispatch;
GtkCupsPollState poll_state;
gboolean result;
GTK_NOTE (PRINTING,
g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
dispatch = (GtkPrintCupsDispatchWatch *) source;
poll_state = gtk_cups_request_get_poll_state (dispatch->request);
if (poll_state != GTK_CUPS_HTTP_IDLE && !dispatch->request->need_password)
if (!(dispatch->data_poll->revents & dispatch->data_poll->events))
return FALSE;
result = gtk_cups_request_read_write (dispatch->request, FALSE);
if (result && dispatch->data_poll != NULL)
{
g_source_remove_poll (source, dispatch->data_poll);
g_free (dispatch->data_poll);
dispatch->data_poll = NULL;
}
if (dispatch->request->need_password && dispatch->request->password_state != GTK_CUPS_PASSWORD_REQUESTED)
{
dispatch->request->need_password = FALSE;
g_idle_add (request_password, dispatch);
result = FALSE;
}
return result;
}
static gboolean
cups_dispatch_watch_prepare (GSource *source,
gint *timeout_)
{
GtkPrintCupsDispatchWatch *dispatch;
gboolean result;
dispatch = (GtkPrintCupsDispatchWatch *) source;
GTK_NOTE (PRINTING,
g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
*timeout_ = -1;
result = gtk_cups_request_read_write (dispatch->request, TRUE);
cups_dispatch_add_poll (source);
return result;
}
static gboolean
cups_dispatch_watch_dispatch (GSource *source,
GSourceFunc callback,
gpointer user_data)
{
GtkPrintCupsDispatchWatch *dispatch;
GtkPrintCupsResponseCallbackFunc ep_callback;
GtkCupsResult *result;
g_assert (callback != NULL);
ep_callback = (GtkPrintCupsResponseCallbackFunc) callback;
dispatch = (GtkPrintCupsDispatchWatch *) source;
result = gtk_cups_request_get_result (dispatch->request);
GTK_NOTE (PRINTING,
g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
if (gtk_cups_result_is_error (result))
{
GTK_NOTE (PRINTING,
g_print("Error result: %s (type %i, status %i, code %i)\n",
gtk_cups_result_get_error_string (result),
gtk_cups_result_get_error_type (result),
gtk_cups_result_get_error_status (result),
gtk_cups_result_get_error_code (result)));
}
ep_callback (GTK_PRINT_BACKEND (dispatch->backend), result, user_data);
return FALSE;
}
static void
cups_dispatch_watch_finalize (GSource *source)
{
GtkPrintCupsDispatchWatch *dispatch;
GtkCupsResult *result;
GTK_NOTE (PRINTING,
g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
dispatch = (GtkPrintCupsDispatchWatch *) source;
result = gtk_cups_request_get_result (dispatch->request);
if (gtk_cups_result_get_error_type (result) == GTK_CUPS_ERROR_AUTH)
{
const gchar *username;
gchar hostname[HTTP_MAX_URI];
gchar *key;
httpGetHostname (dispatch->request->http, hostname, sizeof (hostname));
if (is_address_local (hostname))
strcpy (hostname, "localhost");
if (dispatch->backend->username != NULL)
username = dispatch->backend->username;
else
username = cupsUser ();
key = g_strconcat (username, "@", hostname, NULL);
GTK_NOTE (PRINTING,
g_print ("CUPS backend: removing stored password for %s\n", key));
g_hash_table_remove (dispatch->backend->auth, key);
g_free (key);
if (dispatch->backend)
dispatch->backend->authentication_lock = FALSE;
}
gtk_cups_request_free (dispatch->request);
if (dispatch->backend)
{
/* We need to unref this at idle time, because it might be the
* last reference to this module causing the code to be
* unloaded (including this particular function!)
* Update: Doing this at idle caused a deadlock taking the
* mainloop context lock while being in a GSource callout for
* multithreaded apps. So, for now we just disable unloading
* of print backends. See _gtk_print_backend_create for the
* disabling.
*/
dispatch->backend->requests = g_list_remove (dispatch->backend->requests, dispatch);
g_object_unref (dispatch->backend);
dispatch->backend = NULL;
}
if (dispatch->data_poll)
{
g_source_remove_poll (source, dispatch->data_poll);
g_free (dispatch->data_poll);
dispatch->data_poll = NULL;
}
}
static GSourceFuncs _cups_dispatch_watch_funcs = {
cups_dispatch_watch_prepare,
cups_dispatch_watch_check,
cups_dispatch_watch_dispatch,
cups_dispatch_watch_finalize
};
static void
cups_request_execute (GtkPrintBackendCups *print_backend,
GtkCupsRequest *request,
GtkPrintCupsResponseCallbackFunc callback,
gpointer user_data,
GDestroyNotify notify)
{
GtkPrintCupsDispatchWatch *dispatch;
dispatch = (GtkPrintCupsDispatchWatch *) g_source_new (&_cups_dispatch_watch_funcs,
sizeof (GtkPrintCupsDispatchWatch));
g_source_set_name (&dispatch->source, "GTK+ CUPS backend");
GTK_NOTE (PRINTING,
g_print ("CUPS Backend: %s <source %p> - Executing cups request on server '%s' and resource '%s'\n", G_STRFUNC, dispatch, request->server, request->resource));
dispatch->request = request;
dispatch->backend = g_object_ref (print_backend);
dispatch->poll_state = GTK_CUPS_HTTP_IDLE;
dispatch->data_poll = NULL;
dispatch->callback = NULL;
dispatch->callback_data = NULL;
print_backend->requests = g_list_prepend (print_backend->requests, dispatch);
g_source_set_callback ((GSource *) dispatch, (GSourceFunc) callback, user_data, notify);
if (request->need_auth_info)
{
dispatch->callback = callback;
dispatch->callback_data = user_data;
request_auth_info (dispatch);
}
else
{
g_source_attach ((GSource *) dispatch, NULL);
g_source_unref ((GSource *) dispatch);
}
}
#if 0
static void
cups_request_printer_info_cb (GtkPrintBackendCups *backend,
GtkCupsResult *result,
gpointer user_data)
{
ipp_attribute_t *attr;
ipp_t *response;
gchar *printer_name;
GtkPrinterCups *cups_printer;
GtkPrinter *printer;
gchar *loc;
gchar *desc;
gchar *state_msg;
int job_count;
gboolean status_changed;
g_assert (GTK_IS_PRINT_BACKEND_CUPS (backend));
printer_name = (gchar *)user_data;
printer = gtk_print_backend_find_printer (GTK_PRINT_BACKEND (backend),
printer_name);
GTK_NOTE (PRINTING,
g_print ("CUPS Backend: %s - Got printer info for printer '%s'\n", G_STRFUNC, printer_name));
if (!printer)
{
GTK_NOTE (PRINTING,
g_print ("CUPS Backend: Could not find printer called '%s'\n", printer_name));
return;
}
cups_printer = GTK_PRINTER_CUPS (printer);
if (gtk_cups_result_is_error (result))
{
if (gtk_printer_is_new (printer))
{
gtk_print_backend_remove_printer (GTK_PRINT_BACKEND (backend),
printer);
return;
}
else
return; /* TODO: mark as inactive printer */
}
response = gtk_cups_result_get_response (result);
/* TODO: determine printer type and use correct icon */
gtk_printer_set_icon_name (printer, "gtk-print");
state_msg = "";
loc = "";
desc = "";
job_count = 0;
for (attr = response->attrs; attr != NULL; attr = attr->next)
{
if (!attr->name)
continue;
_CUPS_MAP_ATTR_STR (attr, loc, "printer-location");
_CUPS_MAP_ATTR_STR (attr, desc, "printer-info");
_CUPS_MAP_ATTR_STR (attr, state_msg, "printer-state-message");
_CUPS_MAP_ATTR_INT (attr, cups_printer->state, "printer-state");
_CUPS_MAP_ATTR_INT (attr, job_count, "queued-job-count");
}
status_changed = gtk_printer_set_job_count (printer, job_count);
status_changed |= gtk_printer_set_location (printer, loc);
status_changed |= gtk_printer_set_description (printer, desc);
status_changed |= gtk_printer_set_state_message (printer, state_msg);
if (status_changed)
g_signal_emit_by_name (GTK_PRINT_BACKEND (backend),
"printer-status-changed", printer);
}
static void
cups_request_printer_info (GtkPrintBackendCups *print_backend,
const gchar *printer_name)
{
GtkCupsRequest *request;
gchar *printer_uri;
static const char * const pattrs[] = /* Attributes we're interested in */
{
"printer-location",
"printer-info",
"printer-state-message",
"printer-state",
"queued-job-count",
"job-sheets-supported",
"job-sheets-default"
};
request = gtk_cups_request_new_with_username (NULL,
GTK_CUPS_POST,
IPP_GET_PRINTER_ATTRIBUTES,
NULL,
NULL,
NULL,
print_backend->username);
printer_uri = g_strdup_printf ("ipp://localhost/printers/%s",
printer_name);
gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION, IPP_TAG_URI,
"printer-uri", NULL, printer_uri);
GTK_NOTE (PRINTING,
g_print ("CUPS Backend: %s - Requesting printer info for URI '%s'\n", G_STRFUNC, printer_uri));
g_free (printer_uri);
gtk_cups_request_ipp_add_strings (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"requested-attributes", G_N_ELEMENTS (pattrs),
NULL, pattrs);
cups_request_execute (print_backend,
request,
(GtkPrintCupsResponseCallbackFunc) cups_request_printer_info_cb,
g_strdup (printer_name),
(GDestroyNotify) g_free);
}
#endif
typedef struct {
GtkPrintBackendCups *print_backend;
GtkPrintJob *job;
int job_id;
int counter;
} CupsJobPollData;
static void
job_object_died (gpointer user_data,
GObject *where_the_object_was)
{
CupsJobPollData *data = user_data;
data->job = NULL;
}
static void
cups_job_poll_data_free (CupsJobPollData *data)
{
if (data->job)
g_object_weak_unref (G_OBJECT (data->job), job_object_died, data);
g_free (data);
}
static void
cups_request_job_info_cb (GtkPrintBackendCups *print_backend,
GtkCupsResult *result,
gpointer user_data)
{
CupsJobPollData *data = user_data;
ipp_attribute_t *attr;
ipp_t *response;
int state;
gboolean done;
GDK_THREADS_ENTER ();
if (data->job == NULL)
{
cups_job_poll_data_free (data);
goto done;
}
data->counter++;
response = gtk_cups_result_get_response (result);
state = 0;
#ifdef HAVE_CUPS_API_1_6
attr = ippFindAttribute (response, "job-state", IPP_TAG_ENUM);
state = ippGetInteger (attr, 0);
#else
for (attr = response->attrs; attr != NULL; attr = attr->next)
{
if (!attr->name)
continue;
_CUPS_MAP_ATTR_INT (attr, state, "job-state");
}
#endif
done = FALSE;
switch (state)
{
case IPP_JOB_PENDING:
case IPP_JOB_HELD:
case IPP_JOB_STOPPED:
gtk_print_job_set_status (data->job,
GTK_PRINT_STATUS_PENDING);
break;
case IPP_JOB_PROCESSING:
gtk_print_job_set_status (data->job,
GTK_PRINT_STATUS_PRINTING);
break;
default:
case IPP_JOB_CANCELLED:
case IPP_JOB_ABORTED:
gtk_print_job_set_status (data->job,
GTK_PRINT_STATUS_FINISHED_ABORTED);
done = TRUE;
break;
case 0:
case IPP_JOB_COMPLETED:
gtk_print_job_set_status (data->job,
GTK_PRINT_STATUS_FINISHED);
done = TRUE;
break;
}
if (!done && data->job != NULL)
{
guint32 timeout;
if (data->counter < 5)
timeout = 100;
else if (data->counter < 10)
timeout = 500;
else
timeout = 1000;
g_timeout_add (timeout, cups_job_info_poll_timeout, data);
}
else
cups_job_poll_data_free (data);
done:
GDK_THREADS_LEAVE ();
}
static void
cups_request_job_info (CupsJobPollData *data)
{
GtkCupsRequest *request;
gchar *job_uri;
request = gtk_cups_request_new_with_username (NULL,
GTK_CUPS_POST,
IPP_GET_JOB_ATTRIBUTES,
NULL,
NULL,
NULL,
data->print_backend->username);
job_uri = g_strdup_printf ("ipp://localhost/jobs/%d", data->job_id);
gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION, IPP_TAG_URI,
"job-uri", NULL, job_uri);
g_free (job_uri);
cups_request_execute (data->print_backend,
request,
(GtkPrintCupsResponseCallbackFunc) cups_request_job_info_cb,
data,
NULL);
}
static gboolean
cups_job_info_poll_timeout (gpointer user_data)
{
CupsJobPollData *data = user_data;
if (data->job == NULL)
cups_job_poll_data_free (data);
else
cups_request_job_info (data);
return FALSE;
}
static void
cups_begin_polling_info (GtkPrintBackendCups *print_backend,
GtkPrintJob *job,
gint job_id)
{
CupsJobPollData *data;
data = g_new0 (CupsJobPollData, 1);
data->print_backend = print_backend;
data->job = job;
data->job_id = job_id;
data->counter = 0;
g_object_weak_ref (G_OBJECT (job), job_object_died, data);
cups_request_job_info (data);
}
static void
mark_printer_inactive (GtkPrinter *printer,
GtkPrintBackend *backend)
{
gtk_printer_set_is_active (printer, FALSE);
g_signal_emit_by_name (backend, "printer-removed", printer);
}
static gint
find_printer (GtkPrinter *printer,
const gchar *find_name)
{
const gchar *printer_name;
printer_name = gtk_printer_get_name (printer);
return g_ascii_strcasecmp (printer_name, find_name);
}
/* Printer messages we're interested in */
static const char * const printer_messages[] =
{
"toner-low",
"toner-empty",
"developer-low",
"developer-empty",
"marker-supply-low",
"marker-supply-empty",
"cover-open",
"door-open",
"media-low",
"media-empty",
"offline",
"other"
};
/* Our translatable versions of the printer messages */
static const char * printer_strings[] =
{
N_("Printer '%s' is low on toner."),
N_("Printer '%s' has no toner left."),
/* Translators: "Developer" like on photo development context */
N_("Printer '%s' is low on developer."),
/* Translators: "Developer" like on photo development context */
N_("Printer '%s' is out of developer."),
/* Translators: "marker" is one color bin of the printer */
N_("Printer '%s' is low on at least one marker supply."),
/* Translators: "marker" is one color bin of the printer */
N_("Printer '%s' is out of at least one marker supply."),
N_("The cover is open on printer '%s'."),
N_("The door is open on printer '%s'."),
N_("Printer '%s' is low on paper."),
N_("Printer '%s' is out of paper."),
N_("Printer '%s' is currently offline."),
N_("There is a problem on printer '%s'.")
};
/* Attributes we're interested in for printers */
static const char * const printer_attrs[] =
{
"printer-name",
"printer-uri-supported",
"member-uris",
"printer-location",
"printer-info",
"printer-state-message",
"printer-state-reasons",
"printer-state",
"queued-job-count",
"printer-is-accepting-jobs",
"job-sheets-supported",
"job-sheets-default",
"printer-type",
"auth-info-required",
"number-up-default",
"ipp-versions-supported",
"multiple-document-handling-supported",
"copies-supported",
"number-up-supported"
};
typedef enum
{
GTK_PRINTER_STATE_LEVEL_NONE = 0,
GTK_PRINTER_STATE_LEVEL_INFO = 1,
GTK_PRINTER_STATE_LEVEL_WARNING = 2,
GTK_PRINTER_STATE_LEVEL_ERROR = 3
} PrinterStateLevel;
typedef struct
{
const gchar *printer_name;
const gchar *printer_uri;
const gchar *member_uris;
const gchar *location;
const gchar *description;
gchar *state_msg;
const gchar *reason_msg;
PrinterStateLevel reason_level;
gint state;
gint job_count;
gboolean is_paused;
gboolean is_accepting_jobs;
const gchar *default_cover_before;
const gchar *default_cover_after;
gboolean default_printer;
gboolean got_printer_type;
gboolean remote_printer;
#ifdef HAVE_CUPS_API_1_6
gboolean avahi_printer;
#endif
gchar **auth_info_required;
guchar ipp_version_major;
guchar ipp_version_minor;
gboolean supports_copies;
gboolean supports_collate;
gboolean supports_number_up;
} PrinterSetupInfo;
static void
get_ipp_version (const char *ipp_version_string,
guchar *ipp_version_major,
guchar *ipp_version_minor)
{
gchar **ipp_version_strv;
gchar *endptr;
*ipp_version_major = 1;
*ipp_version_minor = 1;
if (ipp_version_string)
{
ipp_version_strv = g_strsplit (ipp_version_string, ".", 0);
if (ipp_version_strv)
{
if (g_strv_length (ipp_version_strv) == 2)
{
*ipp_version_major = (guchar) g_ascii_strtoull (ipp_version_strv[0], &endptr, 10);
if (endptr == ipp_version_strv[0])
*ipp_version_major = 1;
*ipp_version_minor = (guchar) g_ascii_strtoull (ipp_version_strv[1], &endptr, 10);
if (endptr == ipp_version_strv[1])
*ipp_version_minor = 1;
}
g_strfreev (ipp_version_strv);
}
}
}
static void
get_server_ipp_version (guchar *ipp_version_major,
guchar *ipp_version_minor)
{
*ipp_version_major = 1;
*ipp_version_minor = 1;
if (IPP_VERSION && strlen (IPP_VERSION) == 2)
{
*ipp_version_major = (unsigned char) IPP_VERSION[0];
*ipp_version_minor = (unsigned char) IPP_VERSION[1];
}
}
static gint
ipp_version_cmp (guchar ipp_version_major1,
guchar ipp_version_minor1,
guchar ipp_version_major2,
guchar ipp_version_minor2)
{
if (ipp_version_major1 == ipp_version_major2 &&
ipp_version_minor1 == ipp_version_minor2)
{
return 0;
}
else if (ipp_version_major1 < ipp_version_major2 ||
(ipp_version_major1 == ipp_version_major2 &&
ipp_version_minor1 < ipp_version_minor2))
{
return -1;
}
else
{
return 1;
}
}
static void
cups_printer_handle_attribute (GtkPrintBackendCups *cups_backend,
ipp_attribute_t *attr,
PrinterSetupInfo *info)
{
gint i,j;
if (strcmp (ippGetName (attr), "printer-name") == 0 &&
ippGetValueTag (attr) == IPP_TAG_NAME)
info->printer_name = ippGetString (attr, 0, NULL);
else if (strcmp (ippGetName (attr), "printer-uri-supported") == 0 &&
ippGetValueTag (attr) == IPP_TAG_URI)
info->printer_uri = ippGetString (attr, 0, NULL);
else if (strcmp (ippGetName (attr), "member-uris") == 0 &&
ippGetValueTag (attr) == IPP_TAG_URI)
info->member_uris = ippGetString (attr, 0, NULL);
else if (strcmp (ippGetName (attr), "printer-location") == 0)
info->location = ippGetString (attr, 0, NULL);
else if (strcmp (ippGetName (attr), "printer-info") == 0)
info->description = ippGetString (attr, 0, NULL);
else if (strcmp (ippGetName (attr), "printer-state-message") == 0)
info->state_msg = g_strdup (ippGetString (attr, 0, NULL));
else if (strcmp (ippGetName (attr), "printer-state-reasons") == 0)
/* Store most important reason to reason_msg and set
its importance at printer_state_reason_level */
{
for (i = 0; i < ippGetCount (attr); i++)
{
gboolean interested_in = FALSE;
if (strcmp (ippGetString (attr, i, NULL), "none") == 0)
continue;
/* Sets is_paused flag for paused printer. */
if (strcmp (ippGetString (attr, i, NULL), "paused") == 0)
{
info->is_paused = TRUE;
}
for (j = 0; j < G_N_ELEMENTS (printer_messages); j++)
if (strncmp (ippGetString (attr, i, NULL), printer_messages[j], strlen (printer_messages[j])) == 0)
{
interested_in = TRUE;
break;
}
if (!interested_in)
continue;
if (g_str_has_suffix (ippGetString (attr, i, NULL), "-report"))
{
if (info->reason_level <= GTK_PRINTER_STATE_LEVEL_INFO)
{
info->reason_msg = ippGetString (attr, i, NULL);
info->reason_level = GTK_PRINTER_STATE_LEVEL_INFO;
}
}
else if (g_str_has_suffix (ippGetString (attr, i, NULL), "-warning"))
{
if (info->reason_level <= GTK_PRINTER_STATE_LEVEL_WARNING)
{
info->reason_msg = ippGetString (attr, i, NULL);
info->reason_level = GTK_PRINTER_STATE_LEVEL_WARNING;
}
}
else /* It is error in the case of no suffix. */
{
info->reason_msg = ippGetString (attr, i, NULL);
info->reason_level = GTK_PRINTER_STATE_LEVEL_ERROR;
}
}
}
else if (strcmp (ippGetName (attr), "printer-state") == 0)
info->state = ippGetInteger (attr, 0);
else if (strcmp (ippGetName (attr), "queued-job-count") == 0)
info->job_count = ippGetInteger (attr, 0);
else if (strcmp (ippGetName (attr), "printer-is-accepting-jobs") == 0)
{
if (ippGetBoolean (attr, 0) == 1)
info->is_accepting_jobs = TRUE;
else
info->is_accepting_jobs = FALSE;
}
else if (strcmp (ippGetName (attr), "job-sheets-supported") == 0)
{
if (cups_backend->covers == NULL)
{
cups_backend->number_of_covers = ippGetCount (attr);
cups_backend->covers = g_new (char *, cups_backend->number_of_covers + 1);
for (i = 0; i < cups_backend->number_of_covers; i++)
cups_backend->covers[i] = g_strdup (ippGetString (attr, i, NULL));
cups_backend->covers[cups_backend->number_of_covers] = NULL;
}
}
else if (strcmp (ippGetName (attr), "job-sheets-default") == 0)
{
if (ippGetCount (attr) == 2)
{
info->default_cover_before = ippGetString (attr, 0, NULL);
info->default_cover_after = ippGetString (attr, 1, NULL);
}
}
else if (strcmp (ippGetName (attr), "printer-type") == 0)
{
info->got_printer_type = TRUE;
if (ippGetInteger (attr, 0) & 0x00020000)
info->default_printer = TRUE;
else
info->default_printer = FALSE;
if (ippGetInteger (attr, 0) & 0x00000002)
info->remote_printer = TRUE;
else
info->remote_printer = FALSE;
}
else if (strcmp (ippGetName (attr), "auth-info-required") == 0)
{
if (strcmp (ippGetString (attr, 0, NULL), "none") != 0)
{
info->auth_info_required = g_new0 (gchar *, ippGetCount (attr) + 1);
for (i = 0; i < ippGetCount (attr); i++)
info->auth_info_required[i] = g_strdup (ippGetString (attr, i, NULL));
}
}
else if (g_strcmp0 (ippGetName (attr), "ipp-versions-supported") == 0)
{
guchar server_ipp_version_major;
guchar server_ipp_version_minor;
guchar ipp_version_major;
guchar ipp_version_minor;
get_server_ipp_version (&server_ipp_version_major,
&server_ipp_version_minor);
for (i = 0; i < ippGetCount (attr); i++)
{
get_ipp_version (ippGetString (attr, i, NULL),
&ipp_version_major,
&ipp_version_minor);
if (ipp_version_cmp (ipp_version_major,
ipp_version_minor,
info->ipp_version_major,
info->ipp_version_minor) > 0 &&
ipp_version_cmp (ipp_version_major,
ipp_version_minor,
server_ipp_version_major,
server_ipp_version_minor) <= 0)
{
info->ipp_version_major = ipp_version_major;
info->ipp_version_minor = ipp_version_minor;
}
}
}
else if (g_strcmp0 (ippGetName (attr), "number-up-supported") == 0)
{
if (ippGetCount (attr) == 6)
{
info->supports_number_up = TRUE;
}
}
else if (g_strcmp0 (ippGetName (attr), "copies-supported") == 0)
{
int upper = 1;
ippGetRange (attr, 0, &upper);
if (upper > 1)
{
info->supports_copies = TRUE;
}
}
else if (g_strcmp0 (ippGetName (attr), "multiple-document-handling-supported") == 0)
{
for (i = 0; i < ippGetCount (attr); i++)
{
if (g_strcmp0 (ippGetString (attr, i, NULL), "separate-documents-collated-copies") == 0)
{
info->supports_collate = TRUE;
}
}
}
else
{
GTK_NOTE (PRINTING,
g_print ("CUPS Backend: Attribute %s ignored\n", ippGetName (attr)));
}
}
static GtkPrinter*
cups_create_printer (GtkPrintBackendCups *cups_backend,
PrinterSetupInfo *info)
{
GtkPrinterCups *cups_printer;
GtkPrinter *printer;
char uri[HTTP_MAX_URI]; /* Printer URI */
char method[HTTP_MAX_URI]; /* Method/scheme name */
char username[HTTP_MAX_URI]; /* Username:password */
char hostname[HTTP_MAX_URI]; /* Hostname */
char resource[HTTP_MAX_URI]; /* Resource name */
int port; /* Port number */
char *cups_server; /* CUPS server */
GtkPrintBackend *backend = GTK_PRINT_BACKEND (cups_backend);
cups_printer = gtk_printer_cups_new (info->printer_name, backend);
cups_printer->device_uri = g_strdup_printf ("/printers/%s",
info->printer_name);
/* Check to see if we are looking at a class */
if (info->member_uris)
{
cups_printer->printer_uri = g_strdup (info->member_uris);
/* TODO if member_uris is a class we need to recursivly find a printer */
GTK_NOTE (PRINTING,
g_print ("CUPS Backend: Found class with printer %s\n",
info->member_uris));
}
else
{
cups_printer->printer_uri = g_strdup (info->printer_uri);
GTK_NOTE (PRINTING,
g_print ("CUPS Backend: Found printer %s\n", info->printer_uri));
}
#ifdef HAVE_CUPS_API_1_2
httpSeparateURI (HTTP_URI_CODING_ALL, cups_printer->printer_uri,
method, sizeof (method),
username, sizeof (username),
hostname, sizeof (hostname),
&port,
resource, sizeof (resource));
#else
httpSeparate (cups_printer->printer_uri,
method,
username,
hostname,
&port,
resource);
#endif
if (strncmp (resource, "/printers/", 10) == 0)
{
cups_printer->ppd_name = g_strdup (resource + 10);
GTK_NOTE (PRINTING,
g_print ("CUPS Backend: Setting ppd name '%s' for printer/class '%s'\n", cups_printer->ppd_name, info->printer_name));
}
gethostname (uri, sizeof (uri));
cups_server = g_strdup (cupsServer());
if (strcasecmp (uri, hostname) == 0)
strcpy (hostname, "localhost");
/* if the cups server is local and listening at a unix domain socket
* then use the socket connection
*/
if ((strstr (hostname, "localhost") != NULL) &&
(cups_server[0] == '/'))
strcpy (hostname, cups_server);
g_free (cups_server);
cups_printer->default_cover_before = g_strdup (info->default_cover_before);
cups_printer->default_cover_after = g_strdup (info->default_cover_after);
cups_printer->hostname = g_strdup (hostname);
cups_printer->port = port;
cups_printer->auth_info_required = g_strdupv (info->auth_info_required);
g_strfreev (info->auth_info_required);
printer = GTK_PRINTER (cups_printer);
if (cups_backend->default_printer != NULL &&
strcmp (cups_backend->default_printer, gtk_printer_get_name (printer)) == 0)
gtk_printer_set_is_default (printer, TRUE);
#ifdef HAVE_CUPS_API_1_6
cups_printer->avahi_browsed = info->avahi_printer;
#endif
gtk_print_backend_add_printer (backend, printer);
return printer;
}
static void
set_printer_icon_name_from_info (GtkPrinter *printer,
PrinterSetupInfo *info)
{
/* Set printer icon according to importance
(none, report, warning, error - report is omitted). */
if (info->reason_level == GTK_PRINTER_STATE_LEVEL_ERROR)
gtk_printer_set_icon_name (printer, "printer-error");
else if (info->reason_level == GTK_PRINTER_STATE_LEVEL_WARNING)
gtk_printer_set_icon_name (printer, "printer-warning");
else if (gtk_printer_is_paused (printer))
gtk_printer_set_icon_name (printer, "printer-paused");
else
gtk_printer_set_icon_name (printer, "printer");
}
static void
set_info_state_message (PrinterSetupInfo *info)
{
gint i;
if (info->state_msg && strlen (info->state_msg) == 0)
{
gchar *tmp_msg2 = NULL;
if (info->is_paused && !info->is_accepting_jobs)
/* Translators: this is a printer status. */
tmp_msg2 = g_strdup ( _("Paused; Rejecting Jobs"));
if (info->is_paused && info->is_accepting_jobs)
/* Translators: this is a printer status. */
tmp_msg2 = g_strdup ( _("Paused"));
if (!info->is_paused && !info->is_accepting_jobs)
/* Translators: this is a printer status. */
tmp_msg2 = g_strdup ( _("Rejecting Jobs"));
if (tmp_msg2 != NULL)
{
g_free (info->state_msg);
info->state_msg = tmp_msg2;
}
}
/* Set description of the reason and combine it with printer-state-message. */
if (info->reason_msg)
{
gchar *reason_msg_desc = NULL;
gboolean found = FALSE;
for (i = 0; i < G_N_ELEMENTS (printer_messages); i++)
{
if (strncmp (info->reason_msg, printer_messages[i],
strlen (printer_messages[i])) == 0)
{
reason_msg_desc = g_strdup_printf (printer_strings[i],
info->printer_name);
found = TRUE;
break;
}
}
if (!found)
info->reason_level = GTK_PRINTER_STATE_LEVEL_NONE;
if (info->reason_level >= GTK_PRINTER_STATE_LEVEL_WARNING)
{
if (info->state_msg == NULL || info->state_msg[0] == '\0')
{
g_free (info->state_msg);
info->state_msg = reason_msg_desc;
reason_msg_desc = NULL;
}
else
{
gchar *tmp_msg = NULL;
/* Translators: this string connects multiple printer states together. */
tmp_msg = g_strjoin ( _("; "), info->state_msg,
reason_msg_desc, NULL);
g_free (info->state_msg);
info->state_msg = tmp_msg;
}
}
g_free (reason_msg_desc);
}
}
static void
set_default_printer (GtkPrintBackendCups *cups_backend,
const gchar *default_printer_name)
{
cups_backend->default_printer = g_strdup (default_printer_name);
cups_backend->got_default_printer = TRUE;
if (cups_backend->default_printer != NULL)
{
GtkPrinter *default_printer = NULL;
default_printer = gtk_print_backend_find_printer (GTK_PRINT_BACKEND (cups_backend),
cups_backend->default_printer);
if (default_printer != NULL)
{
gtk_printer_set_is_default (default_printer, TRUE);
g_signal_emit_by_name (GTK_PRINT_BACKEND (cups_backend),
"printer-status-changed", default_printer);
}
}
}
#ifdef HAVE_CUPS_API_1_6
typedef struct
{
gchar *name;
gchar *type;
gchar *domain;
gchar *host;
gint port;
} AvahiService;
void
avahi_service_free (AvahiService *service)
{
if (service)
{
g_free (service->name);
g_free (service->type);
g_free (service->domain);
g_free (service->host);
g_free (service);
}
}
static void
cups_request_avahi_printer_info_cb (GtkPrintBackendCups *cups_backend,
GtkCupsResult *result,
gpointer user_data)
{
PrinterSetupInfo *info = g_slice_new0 (PrinterSetupInfo);
GtkPrintBackend *backend = GTK_PRINT_BACKEND (cups_backend);
ipp_attribute_t *attr;
AvahiService *service = (AvahiService *) user_data;
GtkPrinter *printer;
gboolean list_has_changed = FALSE;
gboolean status_changed = FALSE;
ipp_t *response;
gdk_threads_enter ();
GTK_NOTE (PRINTING,
g_print ("CUPS Backend: %s\n", G_STRFUNC));
if (gtk_cups_result_is_error (result))
{
GTK_NOTE (PRINTING,
g_warning ("CUPS Backend: Error getting printer info: %s %d %d",
gtk_cups_result_get_error_string (result),
gtk_cups_result_get_error_type (result),
gtk_cups_result_get_error_code (result)));
goto done;
}
response = gtk_cups_result_get_response (result);
attr = ippFirstAttribute (response);
while (attr && ippGetGroupTag (attr) != IPP_TAG_PRINTER)
attr = ippNextAttribute (response);
if (attr)
{
while (attr && ippGetGroupTag (attr) == IPP_TAG_PRINTER)
{
cups_printer_handle_attribute (cups_backend, attr, info);
attr = ippNextAttribute (response);
}
if (info->printer_name && info->printer_uri)
{
info->avahi_printer = TRUE;
if (info->got_printer_type &&
info->default_printer &&
cups_backend->avahi_default_printer == NULL)
cups_backend->avahi_default_printer = g_strdup (info->printer_name);
set_info_state_message (info);
printer = gtk_print_backend_find_printer (backend, info->printer_name);
if (!printer)
{
printer = cups_create_printer (cups_backend, info);
list_has_changed = TRUE;
}
else
{
g_object_ref (printer);
}
gtk_printer_set_is_paused (printer, info->is_paused);
gtk_printer_set_is_accepting_jobs (printer, info->is_accepting_jobs);
if (!gtk_printer_is_active (printer))
{
gtk_printer_set_is_active (printer, TRUE);
gtk_printer_set_is_new (printer, TRUE);
list_has_changed = TRUE;
}
GTK_PRINTER_CUPS (printer)->remote = info->remote_printer;
GTK_PRINTER_CUPS (printer)->avahi_name = g_strdup (service->name);
GTK_PRINTER_CUPS (printer)->avahi_type = g_strdup (service->type);
GTK_PRINTER_CUPS (printer)->avahi_domain = g_strdup (service->domain);
GTK_PRINTER_CUPS (printer)->hostname = g_strdup (service->host);
GTK_PRINTER_CUPS (printer)->port = service->port;
GTK_PRINTER_CUPS (printer)->state = info->state;
GTK_PRINTER_CUPS (printer)->ipp_version_major = info->ipp_version_major;
GTK_PRINTER_CUPS (printer)->ipp_version_minor = info->ipp_version_minor;
GTK_PRINTER_CUPS (printer)->supports_copies = info->supports_copies;
GTK_PRINTER_CUPS (printer)->supports_collate = info->supports_collate;
GTK_PRINTER_CUPS (printer)->supports_number_up = info->supports_number_up;
status_changed = gtk_printer_set_job_count (printer, info->job_count);
status_changed |= gtk_printer_set_location (printer, info->location);
status_changed |= gtk_printer_set_description (printer, info->description);
status_changed |= gtk_printer_set_state_message (printer, info->state_msg);
status_changed |= gtk_printer_set_is_accepting_jobs (printer, info->is_accepting_jobs);
set_printer_icon_name_from_info (printer, info);
if (gtk_printer_is_new (printer))
{
g_signal_emit_by_name (backend, "printer-added", printer);
gtk_printer_set_is_new (printer, FALSE);
}
if (status_changed)
g_signal_emit_by_name (GTK_PRINT_BACKEND (backend),
"printer-status-changed", printer);
/* The ref is held by GtkPrintBackend, in add_printer() */
g_object_unref (printer);
}
}
done:
if (list_has_changed)
g_signal_emit_by_name (backend, "printer-list-changed");
if (!cups_backend->got_default_printer &&
gtk_print_backend_printer_list_is_done (backend) &&
cups_backend->avahi_default_printer != NULL)
{
set_default_printer (cups_backend, cups_backend->avahi_default_printer);
}
g_slice_free (PrinterSetupInfo, info);
gdk_threads_leave ();
}
static void
cups_request_avahi_printer_info (const gchar *printer_uri,
const gchar *host,
gint port,
const gchar *name,
const gchar *type,
const gchar *domain,
GtkPrintBackendCups *backend)
{
GtkCupsRequest *request;
AvahiService *service;
http_t *http;
http = httpConnect (host, port);
if (http)
{
service = (AvahiService *) g_new0 (AvahiService, 1);
service->name = g_strdup (name);
service->type = g_strdup (type);
service->domain = g_strdup (domain);
service->host = g_strdup (host);
service->port = port;
request = gtk_cups_request_new_with_username (http,
GTK_CUPS_POST,
IPP_GET_PRINTER_ATTRIBUTES,
NULL,
NULL,
NULL,
backend->username);
gtk_cups_request_set_ipp_version (request, 1, 1);
gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION, IPP_TAG_URI,
"printer-uri", NULL, printer_uri);
gtk_cups_request_ipp_add_strings (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"requested-attributes", G_N_ELEMENTS (printer_attrs),
NULL, printer_attrs);
cups_request_execute (backend,
request,
(GtkPrintCupsResponseCallbackFunc) cups_request_avahi_printer_info_cb,
service,
(GDestroyNotify) avahi_service_free);
}
}
typedef struct
{
gchar *printer_uri;
gchar *host;
gint port;
gchar *name;
gchar *type;
gchar *domain;
GtkPrintBackendCups *backend;
} AvahiConnectionTestData;
static void
avahi_connection_test_cb (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
AvahiConnectionTestData *data = (AvahiConnectionTestData *) user_data;
GSocketConnection *connection;
connection = g_socket_client_connect_to_host_finish (G_SOCKET_CLIENT (source_object),
res,
NULL);
g_object_unref (source_object);
if (connection != NULL)
{
g_io_stream_close (G_IO_STREAM (connection), NULL, NULL);
g_object_unref (connection);
cups_request_avahi_printer_info (data->printer_uri,
data->host,
data->port,
data->name,
data->type,
data->domain,
data->backend);
}
g_free (data->printer_uri);
g_free (data->host);
g_free (data->name);
g_free (data->type);
g_free (data->domain);
g_free (data);
}
static void
avahi_service_resolver_cb (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
AvahiConnectionTestData *data;
GtkPrintBackendCups *backend;
const gchar *name;
const gchar *host;
const gchar *type;
const gchar *domain;
const gchar *address;
const gchar *protocol_string;
GVariant *output;
GVariant *txt;
GVariant *child;
guint32 flags;
guint16 port;
GError *error = NULL;
gchar *suffix = NULL;
gchar *tmp;
gint interface;
gint protocol;
gint aprotocol;
gint i, j;
output = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object),
res,
&error);
if (output)
{
backend = GTK_PRINT_BACKEND_CUPS (user_data);
g_variant_get (output, "(ii&s&s&s&si&sq@aayu)",
&interface,
&protocol,
&name,
&type,
&domain,
&host,
&aprotocol,
&address,
&port,
&txt,
&flags);
for (i = 0; i < g_variant_n_children (txt); i++)
{
child = g_variant_get_child_value (txt, i);
tmp = g_new0 (gchar, g_variant_n_children (child) + 1);
for (j = 0; j < g_variant_n_children (child); j++)
{
tmp[j] = g_variant_get_byte (g_variant_get_child_value (child, j));
}
if (g_str_has_prefix (tmp, "rp="))
{
suffix = g_strdup (tmp + 3);
g_free (tmp);
break;
}
g_free (tmp);
}
if (suffix)
{
if (g_strcmp0 (type, "_ipp._tcp") == 0)
protocol_string = "ipp";
else
protocol_string = "ipps";
data = g_new0 (AvahiConnectionTestData, 1);
if (aprotocol == AVAHI_PROTO_INET6)
data->printer_uri = g_strdup_printf ("%s://[%s]:%u/%s", protocol_string, address, port, suffix);
else
data->printer_uri = g_strdup_printf ("%s://%s:%u/%s", protocol_string, address, port, suffix);
data->host = g_strdup (address);
data->port = port;
data->name = g_strdup (name);
data->type = g_strdup (type);
data->domain = g_strdup (domain);
data->backend = backend;
/* It can happen that the address is not reachable */
g_socket_client_connect_to_host_async (g_socket_client_new (),
address,
port,
backend->avahi_cancellable,
avahi_connection_test_cb,
data);
g_free (suffix);
}
g_variant_unref (output);
}
else
{
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
g_warning ("%s", error->message);
g_error_free (error);
}
}
static void
avahi_service_browser_signal_handler (GDBusConnection *connection,
const gchar *sender_name,
const gchar *object_path,
const gchar *interface_name,
const gchar *signal_name,
GVariant *parameters,
gpointer user_data)
{
GtkPrintBackendCups *backend = GTK_PRINT_BACKEND_CUPS (user_data);
gchar *name;
gchar *type;
gchar *domain;
guint flags;
gint interface;
gint protocol;
if (g_strcmp0 (signal_name, "ItemNew") == 0)
{
g_variant_get (parameters, "(ii&s&s&su)",
&interface,
&protocol,
&name,
&type,
&domain,
&flags);
if (g_strcmp0 (type, "_ipp._tcp") == 0 ||
g_strcmp0 (type, "_ipps._tcp") == 0)
{
g_dbus_connection_call (backend->dbus_connection,
AVAHI_BUS,
"/",
AVAHI_SERVER_IFACE,
"ResolveService",
g_variant_new ("(iisssiu)",
interface,
protocol,
name,
type,
domain,
AVAHI_PROTO_UNSPEC,
0),
G_VARIANT_TYPE ("(iissssisqaayu)"),
G_DBUS_CALL_FLAGS_NONE,
-1,
backend->avahi_cancellable,
avahi_service_resolver_cb,
user_data);
}
}
else if (g_strcmp0 (signal_name, "ItemRemove") == 0)
{
GtkPrinterCups *printer;
GList *list;
GList *iter;
g_variant_get (parameters, "(ii&s&s&su)",
&interface,
&protocol,
&name,
&type,
&domain,
&flags);
if (g_strcmp0 (type, "_ipp._tcp") == 0 ||
g_strcmp0 (type, "_ipps._tcp") == 0)
{
list = gtk_print_backend_get_printer_list (GTK_PRINT_BACKEND (backend));
for (iter = list; iter; iter = iter->next)
{
printer = GTK_PRINTER_CUPS (iter->data);
if (g_strcmp0 (printer->avahi_name, name) == 0 &&
g_strcmp0 (printer->avahi_type, type) == 0 &&
g_strcmp0 (printer->avahi_domain, domain) == 0)
{
if (g_strcmp0 (gtk_printer_get_name (GTK_PRINTER (printer)),
backend->avahi_default_printer) == 0)
{
g_free (backend->avahi_default_printer);
backend->avahi_default_printer = NULL;
}
g_signal_emit_by_name (backend, "printer-removed", printer);
gtk_print_backend_remove_printer (GTK_PRINT_BACKEND (backend),
GTK_PRINTER (printer));
g_signal_emit_by_name (backend, "printer-list-changed");
break;
}
}
}
g_list_free (list);
}
}
static void
avahi_service_browser_new_cb (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
GtkPrintBackendCups *cups_backend;
GVariant *output;
GError *error = NULL;
gint i;
output = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object),
res,
&error);
if (output)
{
cups_backend = GTK_PRINT_BACKEND_CUPS (user_data);
i = cups_backend->avahi_service_browser_paths[0] ? 1 : 0;
g_variant_get (output, "(o)", &cups_backend->avahi_service_browser_paths[i]);
cups_backend->avahi_service_browser_subscription_ids[i] =
g_dbus_connection_signal_subscribe (cups_backend->dbus_connection,
NULL,
AVAHI_SERVICE_BROWSER_IFACE,
NULL,
cups_backend->avahi_service_browser_paths[i],
NULL,
G_DBUS_SIGNAL_FLAGS_NONE,
avahi_service_browser_signal_handler,
user_data,
NULL);
/*
* The general subscription for all service browsers is not needed
* now because we are already subscribed to service browsers
* specific to _ipp._tcp and _ipps._tcp services.
*/
if (cups_backend->avahi_service_browser_paths[0] &&
cups_backend->avahi_service_browser_paths[1] &&
cups_backend->avahi_service_browser_subscription_id > 0)
{
g_dbus_connection_signal_unsubscribe (cups_backend->dbus_connection,
cups_backend->avahi_service_browser_subscription_id);
cups_backend->avahi_service_browser_subscription_id = 0;
}
g_variant_unref (output);
}
else
{
/*
* The creation of ServiceBrowser fails with G_IO_ERROR_DBUS_ERROR
* if Avahi is disabled.
*/
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR) &&
!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
g_warning ("%s", error->message);
g_error_free (error);
}
}
static void
avahi_create_browsers (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
GDBusConnection *dbus_connection;
GtkPrintBackendCups *cups_backend;
GError *error = NULL;
dbus_connection = g_bus_get_finish (res, &error);
if (!dbus_connection)
{
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
g_warning ("Couldn't connect to D-Bus system bus, %s", error->message);
g_error_free (error);
return;
}
cups_backend = GTK_PRINT_BACKEND_CUPS (user_data);
cups_backend->dbus_connection = dbus_connection;
/*
* We need to subscribe to signals of service browser before
* we actually create it because it starts to emit them right
* after its creation.
*/
cups_backend->avahi_service_browser_subscription_id =
g_dbus_connection_signal_subscribe (cups_backend->dbus_connection,
NULL,
AVAHI_SERVICE_BROWSER_IFACE,
NULL,
NULL,
NULL,
G_DBUS_SIGNAL_FLAGS_NONE,
avahi_service_browser_signal_handler,
cups_backend,
NULL);
/*
* Create service browsers for _ipp._tcp and _ipps._tcp services.
*/
g_dbus_connection_call (cups_backend->dbus_connection,
AVAHI_BUS,
"/",
AVAHI_SERVER_IFACE,
"ServiceBrowserNew",
g_variant_new ("(iissu)",
AVAHI_IF_UNSPEC,
AVAHI_PROTO_UNSPEC,
"_ipp._tcp",
"",
0),
G_VARIANT_TYPE ("(o)"),
G_DBUS_CALL_FLAGS_NONE,
-1,
cups_backend->avahi_cancellable,
avahi_service_browser_new_cb,
cups_backend);
g_dbus_connection_call (cups_backend->dbus_connection,
AVAHI_BUS,
"/",
AVAHI_SERVER_IFACE,
"ServiceBrowserNew",
g_variant_new ("(iissu)",
AVAHI_IF_UNSPEC,
AVAHI_PROTO_UNSPEC,
"_ipps._tcp",
"",
0),
G_VARIANT_TYPE ("(o)"),
G_DBUS_CALL_FLAGS_NONE,
-1,
cups_backend->avahi_cancellable,
avahi_service_browser_new_cb,
cups_backend);
}
static void
avahi_request_printer_list (GtkPrintBackendCups *cups_backend)
{
cups_backend->avahi_cancellable = g_cancellable_new ();
g_bus_get (G_BUS_TYPE_SYSTEM, cups_backend->avahi_cancellable, avahi_create_browsers, cups_backend);
}
#endif
static void
cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
GtkCupsResult *result,
gpointer user_data)
{
GtkPrintBackend *backend = GTK_PRINT_BACKEND (cups_backend);
ipp_attribute_t *attr;
ipp_t *response;
gboolean list_has_changed;
GList *removed_printer_checklist;
gchar *remote_default_printer = NULL;
GList *iter;
GDK_THREADS_ENTER ();
list_has_changed = FALSE;
GTK_NOTE (PRINTING,
g_print ("CUPS Backend: %s\n", G_STRFUNC));
cups_backend->list_printers_pending = FALSE;
if (gtk_cups_result_is_error (result))
{
GTK_NOTE (PRINTING,
g_warning ("CUPS Backend: Error getting printer list: %s %d %d",
gtk_cups_result_get_error_string (result),
gtk_cups_result_get_error_type (result),
gtk_cups_result_get_error_code (result)));
if (gtk_cups_result_get_error_type (result) == GTK_CUPS_ERROR_AUTH &&
gtk_cups_result_get_error_code (result) == 1)
{
/* Canceled by user, stop popping up more password dialogs */
if (cups_backend->list_printers_poll > 0)
g_source_remove (cups_backend->list_printers_poll);
cups_backend->list_printers_poll = 0;
cups_backend->list_printers_attempts = 0;
}
goto done;
}
/* Gather the names of the printers in the current queue
* so we may check to see if they were removed
*/
removed_printer_checklist = gtk_print_backend_get_printer_list (backend);
response = gtk_cups_result_get_response (result);
#ifdef HAVE_CUPS_API_1_6
for (attr = ippFirstAttribute (response); attr != NULL;
attr = ippNextAttribute (response))
{
GtkPrinter *printer;
gboolean status_changed = FALSE;
GList *node;
PrinterSetupInfo *info = g_slice_new0 (PrinterSetupInfo);
/* Skip leading attributes until we hit a printer...
*/
while (attr != NULL && ippGetGroupTag (attr) != IPP_TAG_PRINTER)
attr = ippNextAttribute (response);
if (attr == NULL)
break;
while (attr != NULL && ippGetGroupTag (attr) == IPP_TAG_PRINTER)
{
cups_printer_handle_attribute (cups_backend, attr, info);
attr = ippNextAttribute (response);
}
#else
for (attr = response->attrs; attr != NULL; attr = attr->next)
{
GtkPrinter *printer;
gboolean status_changed = FALSE;
GList *node;
PrinterSetupInfo *info = g_slice_new0 (PrinterSetupInfo);
/* Skip leading attributes until we hit a printer...
*/
while (attr != NULL && ippGetGroupTag (attr) != IPP_TAG_PRINTER)
attr = attr->next;
if (attr == NULL)
break;
while (attr != NULL && ippGetGroupTag (attr) == IPP_TAG_PRINTER)
{
cups_printer_handle_attribute (cups_backend, attr, info);
attr = attr->next;
}
#endif
if (info->printer_name == NULL ||
(info->printer_uri == NULL && info->member_uris == NULL))
{
if (attr == NULL)
break;
else
continue;
}
if (info->got_printer_type)
{
if (info->default_printer && !cups_backend->got_default_printer)
{
if (!info->remote_printer)
{
cups_backend->got_default_printer = TRUE;
cups_backend->default_printer = g_strdup (info->printer_name);
}
else
{
if (remote_default_printer == NULL)
remote_default_printer = g_strdup (info->printer_name);
}
}
}
else
{
if (!cups_backend->got_default_printer)
cups_get_default_printer (cups_backend);
}
/* remove name from checklist if it was found */
node = g_list_find_custom (removed_printer_checklist,
info->printer_name,
(GCompareFunc) find_printer);
removed_printer_checklist = g_list_delete_link (removed_printer_checklist,
node);
printer = gtk_print_backend_find_printer (backend, info->printer_name);
if (!printer)
{
printer = cups_create_printer (cups_backend, info);
list_has_changed = TRUE;
}
else
g_object_ref (printer);
GTK_PRINTER_CUPS (printer)->remote = info->remote_printer;
gtk_printer_set_is_paused (printer, info->is_paused);
gtk_printer_set_is_accepting_jobs (printer, info->is_accepting_jobs);
if (!gtk_printer_is_active (printer))
{
gtk_printer_set_is_active (printer, TRUE);
gtk_printer_set_is_new (printer, TRUE);
list_has_changed = TRUE;
}
if (gtk_printer_is_new (printer))
{
g_signal_emit_by_name (backend, "printer-added", printer);
gtk_printer_set_is_new (printer, FALSE);
}
#if 0
/* Getting printer info with separate requests overwhelms cups
* when the printer list has more than a handful of printers.
*/
cups_request_printer_info (cups_backend, gtk_printer_get_name (printer));
#endif
GTK_PRINTER_CUPS (printer)->state = info->state;
GTK_PRINTER_CUPS (printer)->ipp_version_major = info->ipp_version_major;
GTK_PRINTER_CUPS (printer)->ipp_version_minor = info->ipp_version_minor;
GTK_PRINTER_CUPS (printer)->supports_copies = info->supports_copies;
GTK_PRINTER_CUPS (printer)->supports_collate = info->supports_collate;
GTK_PRINTER_CUPS (printer)->supports_number_up = info->supports_number_up;
status_changed = gtk_printer_set_job_count (printer, info->job_count);
status_changed |= gtk_printer_set_location (printer, info->location);
status_changed |= gtk_printer_set_description (printer,
info->description);
set_info_state_message (info);
status_changed |= gtk_printer_set_state_message (printer, info->state_msg);
status_changed |= gtk_printer_set_is_accepting_jobs (printer, info->is_accepting_jobs);
set_printer_icon_name_from_info (printer, info);
if (status_changed)
g_signal_emit_by_name (GTK_PRINT_BACKEND (backend),
"printer-status-changed", printer);
/* The ref is held by GtkPrintBackend, in add_printer() */
g_object_unref (printer);
g_free (info->state_msg);
g_slice_free (PrinterSetupInfo, info);
if (attr == NULL)
break;
}
/* look at the removed printers checklist and mark any printer
as inactive if it is in the list, emitting a printer_removed signal */
if (removed_printer_checklist != NULL)
{
for (iter = removed_printer_checklist; iter; iter = iter->next)
{
#ifdef HAVE_CUPS_API_1_6
if (!GTK_PRINTER_CUPS (iter->data)->avahi_browsed)
#endif
{
mark_printer_inactive (GTK_PRINTER (iter->data), backend);
list_has_changed = TRUE;
}
}
g_list_free (removed_printer_checklist);
}
done:
if (list_has_changed)
g_signal_emit_by_name (backend, "printer-list-changed");
gtk_print_backend_set_list_done (backend);
if (!cups_backend->got_default_printer && remote_default_printer != NULL)
{
set_default_printer (cups_backend, remote_default_printer);
g_free (remote_default_printer);
}
#ifdef HAVE_CUPS_API_1_6
if (!cups_backend->got_default_printer && cups_backend->avahi_default_printer != NULL)
{
set_default_printer (cups_backend, cups_backend->avahi_default_printer);
}
#endif
GDK_THREADS_LEAVE ();
}
static void
update_backend_status (GtkPrintBackendCups *cups_backend,
GtkCupsConnectionState state)
{
switch (state)
{
case GTK_CUPS_CONNECTION_NOT_AVAILABLE:
g_object_set (cups_backend, "status", GTK_PRINT_BACKEND_STATUS_UNAVAILABLE, NULL);
break;
case GTK_CUPS_CONNECTION_AVAILABLE:
g_object_set (cups_backend, "status", GTK_PRINT_BACKEND_STATUS_OK, NULL);
break;
default: ;
}
}
static gboolean
cups_request_printer_list (GtkPrintBackendCups *cups_backend)
{
GtkCupsConnectionState state;
GtkCupsRequest *request;
if (cups_backend->reading_ppds > 0 || cups_backend->list_printers_pending)
return TRUE;
state = gtk_cups_connection_test_get_state (cups_backend->cups_connection_test);
update_backend_status (cups_backend, state);
if (cups_backend->list_printers_attempts == 60)
{
cups_backend->list_printers_attempts = -1;
if (cups_backend->list_printers_poll > 0)
g_source_remove (cups_backend->list_printers_poll);
cups_backend->list_printers_poll = gdk_threads_add_timeout (200,
(GSourceFunc) cups_request_printer_list,
cups_backend);
}
else if (cups_backend->list_printers_attempts != -1)
cups_backend->list_printers_attempts++;
if (state == GTK_CUPS_CONNECTION_IN_PROGRESS || state == GTK_CUPS_CONNECTION_NOT_AVAILABLE)
return TRUE;
else
if (cups_backend->list_printers_attempts > 0)
cups_backend->list_printers_attempts = 60;
cups_backend->list_printers_pending = TRUE;
request = gtk_cups_request_new_with_username (NULL,
GTK_CUPS_POST,
CUPS_GET_PRINTERS,
NULL,
NULL,
NULL,
cups_backend->username);
gtk_cups_request_ipp_add_strings (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"requested-attributes", G_N_ELEMENTS (printer_attrs),
NULL, printer_attrs);
cups_request_execute (cups_backend,
request,
(GtkPrintCupsResponseCallbackFunc) cups_request_printer_list_cb,
request,
NULL);
return TRUE;
}
static void
cups_get_printer_list (GtkPrintBackend *backend)
{
GtkPrintBackendCups *cups_backend;
cups_backend = GTK_PRINT_BACKEND_CUPS (backend);
if (cups_backend->cups_connection_test == NULL)
cups_backend->cups_connection_test = gtk_cups_connection_test_new (NULL, -1);
if (cups_backend->list_printers_poll == 0)
{
if (cups_request_printer_list (cups_backend))
cups_backend->list_printers_poll = gdk_threads_add_timeout (50,
(GSourceFunc) cups_request_printer_list,
backend);
#ifdef HAVE_CUPS_API_1_6
avahi_request_printer_list (cups_backend);
#endif
}
}
typedef struct {
GtkPrinterCups *printer;
GIOChannel *ppd_io;
http_t *http;
} GetPPDData;
static void
get_ppd_data_free (GetPPDData *data)
{
GTK_NOTE (PRINTING,
g_print ("CUPS Backend: %s\n", G_STRFUNC));
httpClose (data->http);
g_io_channel_unref (data->ppd_io);
g_object_unref (data->printer);
g_free (data);
}
static void
cups_request_ppd_cb (GtkPrintBackendCups *print_backend,
GtkCupsResult *result,
GetPPDData *data)
{
ipp_t *response;
GtkPrinter *printer;
GDK_THREADS_ENTER ();
GTK_NOTE (PRINTING,
g_print ("CUPS Backend: %s\n", G_STRFUNC));
printer = GTK_PRINTER (data->printer);
GTK_PRINTER_CUPS (printer)->reading_ppd = FALSE;
print_backend->reading_ppds--;
if (gtk_cups_result_is_error (result))
{
gboolean success = FALSE;
/* If we get a 404 then it is just a raw printer without a ppd
and not an error. Standalone Avahi printers also don't have
PPD files. */
if (((gtk_cups_result_get_error_type (result) == GTK_CUPS_ERROR_HTTP) &&
(gtk_cups_result_get_error_status (result) == HTTP_NOT_FOUND))
#ifdef HAVE_CUPS_API_1_6
|| GTK_PRINTER_CUPS (printer)->avahi_browsed
#endif
)
{
gtk_printer_set_has_details (printer, TRUE);
success = TRUE;
}
g_signal_emit_by_name (printer, "details-acquired", success);
goto done;
}
response = gtk_cups_result_get_response (result);
/* let ppdOpenFd take over the ownership of the open file */
g_io_channel_seek_position (data->ppd_io, 0, G_SEEK_SET, NULL);
data->printer->ppd_file = ppdOpenFd (dup (g_io_channel_unix_get_fd (data->ppd_io)));
ppdMarkDefaults (data->printer->ppd_file);
gtk_printer_set_has_details (printer, TRUE);
g_signal_emit_by_name (printer, "details-acquired", TRUE);
done:
GDK_THREADS_LEAVE ();
}
static gboolean
cups_request_ppd (GtkPrinter *printer)
{
GError *error;
GtkPrintBackend *print_backend;
GtkPrinterCups *cups_printer;
GtkCupsRequest *request;
char *ppd_filename = NULL;
gchar *resource;
http_t *http;
GetPPDData *data;
int fd;
cups_printer = GTK_PRINTER_CUPS (printer);
error = NULL;
GTK_NOTE (PRINTING,
g_print ("CUPS Backend: %s\n", G_STRFUNC));
if (cups_printer->remote)
{
GtkCupsConnectionState state;
state = gtk_cups_connection_test_get_state (cups_printer->remote_cups_connection_test);
if (state == GTK_CUPS_CONNECTION_IN_PROGRESS)
{
if (cups_printer->get_remote_ppd_attempts == 60)
{
cups_printer->get_remote_ppd_attempts = -1;
if (cups_printer->get_remote_ppd_poll > 0)
g_source_remove (cups_printer->get_remote_ppd_poll);
cups_printer->get_remote_ppd_poll = gdk_threads_add_timeout (200,
(GSourceFunc) cups_request_ppd,
printer);
}
else if (cups_printer->get_remote_ppd_attempts != -1)
cups_printer->get_remote_ppd_attempts++;
return TRUE;
}
gtk_cups_connection_test_free (cups_printer->remote_cups_connection_test);
cups_printer->remote_cups_connection_test = NULL;
cups_printer->get_remote_ppd_poll = 0;
cups_printer->get_remote_ppd_attempts = 0;
if (state == GTK_CUPS_CONNECTION_NOT_AVAILABLE)
{
g_signal_emit_by_name (printer, "details-acquired", FALSE);
return FALSE;
}
}
http = httpConnectEncrypt (cups_printer->hostname,
cups_printer->port,
cupsEncryption ());
data = g_new0 (GetPPDData, 1);
fd = g_file_open_tmp ("gtkprint_ppd_XXXXXX",
&ppd_filename,
&error);
#ifdef G_ENABLE_DEBUG
/* If we are debugging printing don't delete the tmp files */
if (!(gtk_debug_flags & GTK_DEBUG_PRINTING))
unlink (ppd_filename);
#else
unlink (ppd_filename);
#endif /* G_ENABLE_DEBUG */
if (error != NULL)
{
GTK_NOTE (PRINTING,
g_warning ("CUPS Backend: Failed to create temp file, %s\n",
error->message));
g_error_free (error);
httpClose (http);
g_free (ppd_filename);
g_free (data);
g_signal_emit_by_name (printer, "details-acquired", FALSE);
return FALSE;
}
data->http = http;
fchmod (fd, S_IRUSR | S_IWUSR);
data->ppd_io = g_io_channel_unix_new (fd);
g_io_channel_set_encoding (data->ppd_io, NULL, NULL);
g_io_channel_set_close_on_unref (data->ppd_io, TRUE);
data->printer = g_object_ref (printer);
resource = g_strdup_printf ("/printers/%s.ppd",
gtk_printer_cups_get_ppd_name (GTK_PRINTER_CUPS (printer)));
print_backend = gtk_printer_get_backend (printer);
request = gtk_cups_request_new_with_username (data->http,
GTK_CUPS_GET,
0,
data->ppd_io,
cups_printer->hostname,
resource,
GTK_PRINT_BACKEND_CUPS (print_backend)->username);
gtk_cups_request_set_ipp_version (request,
cups_printer->ipp_version_major,
cups_printer->ipp_version_minor);
GTK_NOTE (PRINTING,
g_print ("CUPS Backend: Requesting resource %s to be written to temp file %s\n", resource, ppd_filename));
cups_printer->reading_ppd = TRUE;
GTK_PRINT_BACKEND_CUPS (print_backend)->reading_ppds++;
cups_request_execute (GTK_PRINT_BACKEND_CUPS (print_backend),
request,
(GtkPrintCupsResponseCallbackFunc) cups_request_ppd_cb,
data,
(GDestroyNotify)get_ppd_data_free);
g_free (resource);
g_free (ppd_filename);
return FALSE;
}
/* Ordering matters for default preference */
static const char *lpoptions_locations[] = {
"/etc/cups/lpoptions",
".lpoptions",
".cups/lpoptions"
};
static void
cups_parse_user_default_printer (const char *filename,
char **printer_name)
{
FILE *fp;
char line[1024], *lineptr, *defname = NULL;
if ((fp = g_fopen (filename, "r")) == NULL)
return;
while (fgets (line, sizeof (line), fp) != NULL)
{
if (strncasecmp (line, "default", 7) != 0 || !isspace (line[7]))
continue;
lineptr = line + 8;
while (isspace (*lineptr))
lineptr++;
if (!*lineptr)
continue;
defname = lineptr;
while (!isspace (*lineptr) && *lineptr && *lineptr != '/')
lineptr++;
*lineptr = '\0';
if (*printer_name != NULL)
g_free (*printer_name);
*printer_name = g_strdup (defname);
}
fclose (fp);
}
static void
cups_get_user_default_printer (char **printer_name)
{
int i;
for (i = 0; i < G_N_ELEMENTS (lpoptions_locations); i++)
{
if (g_path_is_absolute (lpoptions_locations[i]))
{
cups_parse_user_default_printer (lpoptions_locations[i],
printer_name);
}
else
{
char *filename;
filename = g_build_filename (g_get_home_dir (),
lpoptions_locations[i], NULL);
cups_parse_user_default_printer (filename, printer_name);
g_free (filename);
}
}
}
static int
cups_parse_user_options (const char *filename,
const char *printer_name,
int num_options,
cups_option_t **options)
{
FILE *fp;
gchar line[1024], *lineptr, *name;
if ((fp = g_fopen (filename, "r")) == NULL)
return num_options;
while (fgets (line, sizeof (line), fp) != NULL)
{
if (strncasecmp (line, "dest", 4) == 0 && isspace (line[4]))
lineptr = line + 4;
else if (strncasecmp (line, "default", 7) == 0 && isspace (line[7]))
lineptr = line + 7;
else
continue;
/* Skip leading whitespace */
while (isspace (*lineptr))
lineptr++;
if (!*lineptr)
continue;
/* NUL-terminate the name, stripping the instance name */
name = lineptr;
while (!isspace (*lineptr) && *lineptr)
{
if (*lineptr == '/')
*lineptr = '\0';
lineptr++;
}
if (!*lineptr)
continue;
*lineptr++ = '\0';
if (strncasecmp (name, printer_name, strlen (printer_name)) != 0)
continue;
/* We found our printer, parse the options */
num_options = cupsParseOptions (lineptr, num_options, options);
}
fclose (fp);
return num_options;
}
static int
cups_get_user_options (const char *printer_name,
int num_options,
cups_option_t **options)
{
int i;
for (i = 0; i < G_N_ELEMENTS (lpoptions_locations); i++)
{
if (g_path_is_absolute (lpoptions_locations[i]))
{
num_options = cups_parse_user_options (lpoptions_locations[i],
printer_name,
num_options,
options);
}
else
{
char *filename;
filename = g_build_filename (g_get_home_dir (),
lpoptions_locations[i], NULL);
num_options = cups_parse_user_options (filename, printer_name,
num_options, options);
g_free (filename);
}
}
return num_options;
}
/* This function requests default printer from a CUPS server in regular intervals.
* In the case of unreachable CUPS server the request is repeated later.
* The default printer is not requested in the case of previous success.
*/
static void
cups_get_default_printer (GtkPrintBackendCups *backend)
{
GtkPrintBackendCups *cups_backend;
cups_backend = backend;
if (cups_backend->cups_connection_test == NULL)
cups_backend->cups_connection_test = gtk_cups_connection_test_new (NULL, -1);
if (cups_backend->default_printer_poll == 0)
{
if (cups_request_default_printer (cups_backend))
cups_backend->default_printer_poll = gdk_threads_add_timeout (200,
(GSourceFunc) cups_request_default_printer,
backend);
}
}
/* This function gets default printer from local settings.*/
static void
cups_get_local_default_printer (GtkPrintBackendCups *backend)
{
const char *str;
char *name = NULL;
if ((str = g_getenv ("LPDEST")) != NULL)
{
backend->default_printer = g_strdup (str);
backend->got_default_printer = TRUE;
return;
}
else if ((str = g_getenv ("PRINTER")) != NULL &&
strcmp (str, "lp") != 0)
{
backend->default_printer = g_strdup (str);
backend->got_default_printer = TRUE;
return;
}
/* Figure out user setting for default printer */
cups_get_user_default_printer (&name);
if (name != NULL)
{
backend->default_printer = name;
backend->got_default_printer = TRUE;
return;
}
}
static void
cups_request_default_printer_cb (GtkPrintBackendCups *print_backend,
GtkCupsResult *result,
gpointer user_data)
{
ipp_t *response;
ipp_attribute_t *attr;
GtkPrinter *printer;
GDK_THREADS_ENTER ();
if (gtk_cups_result_is_error (result))
{
if (gtk_cups_result_get_error_type (result) == GTK_CUPS_ERROR_AUTH &&
gtk_cups_result_get_error_code (result) == 1)
{
/* Canceled by user, stop popping up more password dialogs */
if (print_backend->list_printers_poll > 0)
g_source_remove (print_backend->list_printers_poll);
print_backend->list_printers_poll = 0;
}
return;
}
response = gtk_cups_result_get_response (result);
if ((attr = ippFindAttribute (response, "printer-name", IPP_TAG_NAME)) != NULL)
print_backend->default_printer = g_strdup (ippGetString (attr, 0, NULL));
print_backend->got_default_printer = TRUE;
if (print_backend->default_printer != NULL)
{
printer = gtk_print_backend_find_printer (GTK_PRINT_BACKEND (print_backend), print_backend->default_printer);
if (printer != NULL)
{
gtk_printer_set_is_default (printer, TRUE);
g_signal_emit_by_name (GTK_PRINT_BACKEND (print_backend), "printer-status-changed", printer);
}
}
/* Make sure to kick off get_printers if we are polling it,
* as we could have blocked this reading the default printer
*/
if (print_backend->list_printers_poll != 0)
cups_request_printer_list (print_backend);
GDK_THREADS_LEAVE ();
}
static gboolean
cups_request_default_printer (GtkPrintBackendCups *print_backend)
{
GtkCupsConnectionState state;
GtkCupsRequest *request;
state = gtk_cups_connection_test_get_state (print_backend->cups_connection_test);
update_backend_status (print_backend, state);
if (state == GTK_CUPS_CONNECTION_IN_PROGRESS || state == GTK_CUPS_CONNECTION_NOT_AVAILABLE)
return TRUE;
request = gtk_cups_request_new_with_username (NULL,
GTK_CUPS_POST,
CUPS_GET_DEFAULT,
NULL,
NULL,
NULL,
print_backend->username);
cups_request_execute (print_backend,
request,
(GtkPrintCupsResponseCallbackFunc) cups_request_default_printer_cb,
g_object_ref (print_backend),
g_object_unref);
return FALSE;
}
static void
cups_printer_request_details (GtkPrinter *printer)
{
GtkPrinterCups *cups_printer;
cups_printer = GTK_PRINTER_CUPS (printer);
if (!cups_printer->reading_ppd &&
gtk_printer_cups_get_ppd (cups_printer) == NULL)
{
if (cups_printer->remote)
{
if (cups_printer->get_remote_ppd_poll == 0)
{
cups_printer->remote_cups_connection_test =
gtk_cups_connection_test_new (cups_printer->hostname,
cups_printer->port);
if (cups_request_ppd (printer))
cups_printer->get_remote_ppd_poll = gdk_threads_add_timeout (50,
(GSourceFunc) cups_request_ppd,
printer);
}
}
else
cups_request_ppd (printer);
}
}
static char *
ppd_text_to_utf8 (ppd_file_t *ppd_file,
const char *text)
{
const char *encoding = NULL;
char *res;
if (g_ascii_strcasecmp (ppd_file->lang_encoding, "UTF-8") == 0)
{
return g_strdup (text);
}
else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin1") == 0)
{
encoding = "ISO-8859-1";
}
else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin2") == 0)
{
encoding = "ISO-8859-2";
}
else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "ISOLatin5") == 0)
{
encoding = "ISO-8859-5";
}
else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "JIS83-RKSJ") == 0)
{
encoding = "SHIFT-JIS";
}
else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "MacStandard") == 0)
{
encoding = "MACINTOSH";
}
else if (g_ascii_strcasecmp (ppd_file->lang_encoding, "WindowsANSI") == 0)
{
encoding = "WINDOWS-1252";
}
else
{
/* Fallback, try iso-8859-1... */
encoding = "ISO-8859-1";
}
res = g_convert (text, -1, "UTF-8", encoding, NULL, NULL, NULL);
if (res == NULL)
{
GTK_NOTE (PRINTING,
g_warning ("CUPS Backend: Unable to convert PPD text\n"));
res = g_strdup ("???");
}
return res;
}
/* TODO: Add more translations for common settings here */
static const struct {
const char *keyword;
const char *translation;
} cups_option_translations[] = {
{ "Duplex", N_("Two Sided") },
{ "MediaType", N_("Paper Type") },
{ "InputSlot", N_("Paper Source") },
{ "OutputBin", N_("Output Tray") },
{ "Resolution", N_("Resolution") },
{ "PreFilter", N_("GhostScript pre-filtering") },
};
static const struct {
const char *keyword;
const char *choice;
const char *translation;
} cups_choice_translations[] = {
{ "Duplex", "None", N_("One Sided") },
/* Translators: this is an option of "Two Sided" */
{ "Duplex", "DuplexNoTumble", N_("Long Edge (Standard)") },
/* Translators: this is an option of "Two Sided" */
{ "Duplex", "DuplexTumble", N_("Short Edge (Flip)") },
/* Translators: this is an option of "Paper Source" */
{ "InputSlot", "Auto", N_("Auto Select") },
/* Translators: this is an option of "Paper Source" */
{ "InputSlot", "AutoSelect", N_("Auto Select") },
/* Translators: this is an option of "Paper Source" */
{ "InputSlot", "Default", N_("Printer Default") },
/* Translators: this is an option of "Paper Source" */
{ "InputSlot", "None", N_("Printer Default") },
/* Translators: this is an option of "Paper Source" */
{ "InputSlot", "PrinterDefault", N_("Printer Default") },
/* Translators: this is an option of "Paper Source" */
{ "InputSlot", "Unspecified", N_("Auto Select") },
/* Translators: this is an option of "Resolution" */
{ "Resolution", "default", N_("Printer Default") },
/* Translators: this is an option of "GhostScript" */
{ "PreFilter", "EmbedFonts", N_("Embed GhostScript fonts only") },
/* Translators: this is an option of "GhostScript" */
{ "PreFilter", "Level1", N_("Convert to PS level 1") },
/* Translators: this is an option of "GhostScript" */
{ "PreFilter", "Level2", N_("Convert to PS level 2") },
/* Translators: this is an option of "GhostScript" */
{ "PreFilter", "No", N_("No pre-filtering") },
};
static const struct {
const char *name;
const char *translation;
} cups_group_translations[] = {
/* Translators: "Miscellaneous" is the label for a button, that opens
up an extra panel of settings in a print dialog. */
{ "Miscellaneous", N_("Miscellaneous") },
};
static const struct {
const char *ppd_keyword;
const char *name;
} ppd_option_names[] = {
{"Duplex", "gtk-duplex" },
{"MediaType", "gtk-paper-type"},
{"InputSlot", "gtk-paper-source"},
{"OutputBin", "gtk-output-tray"},
};
static const struct {
const char *lpoption;
const char *name;
} lpoption_names[] = {
{"number-up", "gtk-n-up" },
{"number-up-layout", "gtk-n-up-layout"},
{"job-billing", "gtk-billing-info"},
{"job-priority", "gtk-job-prio"},
};
/* keep sorted when changing */
static const char *color_option_whitelist[] = {
"BRColorEnhancement",
"BRColorMatching",
"BRColorMatching",
"BRColorMode",
"BRGammaValue",
"BRImprovedGray",
"BlackSubstitution",
"ColorModel",
"HPCMYKInks",
"HPCSGraphics",
"HPCSImages",
"HPCSText",
"HPColorSmart",
"RPSBlackMode",
"RPSBlackOverPrint",
"Rcmyksimulation",
};
/* keep sorted when changing */
static const char *color_group_whitelist[] = {
"ColorPage",
"FPColorWise1",
"FPColorWise2",
"FPColorWise3",
"FPColorWise4",
"FPColorWise5",
"HPColorOptionsPanel",
};
/* keep sorted when changing */
static const char *image_quality_option_whitelist[] = {
"BRDocument",
"BRHalfTonePattern",
"BRNormalPrt",
"BRPrintQuality",
"BitsPerPixel",
"Darkness",
"Dithering",
"EconoMode",
"Economode",
"HPEconoMode",
"HPEdgeControl",
"HPGraphicsHalftone",
"HPHalftone",
"HPLJDensity",
"HPPhotoHalftone",
"OutputMode",
"REt",
"RPSBitsPerPixel",
"RPSDitherType",
"Resolution",
"ScreenLock",
"Smoothing",
"TonerSaveMode",
"UCRGCRForImage",
};
/* keep sorted when changing */
static const char *image_quality_group_whitelist[] = {
"FPImageQuality1",
"FPImageQuality2",
"FPImageQuality3",
"ImageQualityPage",
};
/* keep sorted when changing */
static const char * finishing_option_whitelist[] = {
"BindColor",
"BindEdge",
"BindType",
"BindWhen",
"Booklet",
"FoldType",
"FoldWhen",
"HPStaplerOptions",
"Jog",
"Slipsheet",
"Sorter",
"StapleLocation",
"StapleOrientation",
"StapleWhen",
"StapleX",
"StapleY",
};
/* keep sorted when changing */
static const char *finishing_group_whitelist[] = {
"FPFinishing1",
"FPFinishing2",
"FPFinishing3",
"FPFinishing4",
"FinishingPage",
"HPFinishingPanel",
};
/* keep sorted when changing */
static const char *cups_option_blacklist[] = {
"Collate",
"Copies",
"OutputOrder",
"PageRegion",
"PageSize",
};
static char *
get_option_text (ppd_file_t *ppd_file,
ppd_option_t *option)
{
int i;
char *utf8;
for (i = 0; i < G_N_ELEMENTS (cups_option_translations); i++)
{
if (strcmp (cups_option_translations[i].keyword, option->keyword) == 0)
return g_strdup (_(cups_option_translations[i].translation));
}
utf8 = ppd_text_to_utf8 (ppd_file, option->text);
/* Some ppd files have spaces in the text before the colon */
g_strchomp (utf8);
return utf8;
}
static char *
get_choice_text (ppd_file_t *ppd_file,
ppd_choice_t *choice)
{
int i;
ppd_option_t *option = choice->option;
const char *keyword = option->keyword;
for (i = 0; i < G_N_ELEMENTS (cups_choice_translations); i++)
{
if (strcmp (cups_choice_translations[i].keyword, keyword) == 0 &&
strcmp (cups_choice_translations[i].choice, choice->choice) == 0)
return g_strdup (_(cups_choice_translations[i].translation));
}
return ppd_text_to_utf8 (ppd_file, choice->text);
}
static gboolean
group_has_option (ppd_group_t *group,
ppd_option_t *option)
{
int i;
if (group == NULL)
return FALSE;
if (group->num_options > 0 &&
option >= group->options && option < group->options + group->num_options)
return TRUE;
for (i = 0; i < group->num_subgroups; i++)
{
if (group_has_option (&group->subgroups[i],option))
return TRUE;
}
return FALSE;
}
static void
set_option_off (GtkPrinterOption *option)
{
/* Any of these will do, _set only applies the value
* if its allowed of the option */
gtk_printer_option_set (option, "False");
gtk_printer_option_set (option, "Off");
gtk_printer_option_set (option, "None");
}
static gboolean
value_is_off (const char *value)
{
return (strcasecmp (value, "None") == 0 ||
strcasecmp (value, "Off") == 0 ||
strcasecmp (value, "False") == 0);
}
static char *
ppd_group_name (ppd_group_t *group)
{
#if CUPS_VERSION_MAJOR > 1 || (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR > 1) || (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR == 1 && CUPS_VERSION_PATCH >= 18)
return group->name;
#else
return group->text;
#endif
}
static int
available_choices (ppd_file_t *ppd,
ppd_option_t *option,
ppd_choice_t ***available,
gboolean keep_if_only_one_option)
{
ppd_option_t *other_option;
int i, j;
gchar *conflicts;
ppd_const_t *constraint;
const char *choice, *other_choice;
ppd_option_t *option1, *option2;
ppd_group_t *installed_options;
int num_conflicts;
gboolean all_default;
int add_auto;
if (available)
*available = NULL;
conflicts = g_new0 (char, option->num_choices);
installed_options = NULL;
for (i = 0; i < ppd->num_groups; i++)
{
char *name;
name = ppd_group_name (&ppd->groups[i]);
if (strcmp (name, "InstallableOptions") == 0)
{
installed_options = &ppd->groups[i];
break;
}
}
for (i = ppd->num_consts, constraint = ppd->consts; i > 0; i--, constraint++)
{
option1 = ppdFindOption (ppd, constraint->option1);
if (option1 == NULL)
continue;
option2 = ppdFindOption (ppd, constraint->option2);
if (option2 == NULL)
continue;
if (option == option1)
{
choice = constraint->choice1;
other_option = option2;
other_choice = constraint->choice2;
}
else if (option == option2)
{
choice = constraint->choice2;
other_option = option1;
other_choice = constraint->choice1;
}
else
continue;
/* We only care of conflicts with installed_options and
PageSize */
if (!group_has_option (installed_options, other_option) &&
(strcmp (other_option->keyword, "PageSize") != 0))
continue;
if (*other_choice == 0)
{
/* Conflict only if the installed option is not off */
if (value_is_off (other_option->defchoice))
continue;
}
/* Conflict if the installed option has the specified default */
else if (strcasecmp (other_choice, other_option->defchoice) != 0)
continue;
if (*choice == 0)
{
/* Conflict with all non-off choices */
for (j = 0; j < option->num_choices; j++)
{
if (!value_is_off (option->choices[j].choice))
conflicts[j] = 1;
}
}
else
{
for (j = 0; j < option->num_choices; j++)
{
if (strcasecmp (option->choices[j].choice, choice) == 0)
conflicts[j] = 1;
}
}
}
num_conflicts = 0;
all_default = TRUE;
for (j = 0; j < option->num_choices; j++)
{
if (conflicts[j])
num_conflicts++;
else if (strcmp (option->choices[j].choice, option->defchoice) != 0)
all_default = FALSE;
}
if ((all_default && !keep_if_only_one_option) ||
(num_conflicts == option->num_choices))
{
g_free (conflicts);
return 0;
}
/* Some ppds don't have a "use printer default" option for
* InputSlot. This means you always have to select a particular slot,
* and you can't auto-pick source based on the paper size. To support
* this we always add an auto option if there isn't one already. If
* the user chooses the generated option we don't send any InputSlot
* value when printing. The way we detect existing auto-cases is based
* on feedback from Michael Sweet of cups fame.
*/
add_auto = 0;
if (strcmp (option->keyword, "InputSlot") == 0)
{
gboolean found_auto = FALSE;
for (j = 0; j < option->num_choices; j++)
{
if (!conflicts[j])
{
if (strcmp (option->choices[j].choice, "Auto") == 0 ||
strcmp (option->choices[j].choice, "AutoSelect") == 0 ||
strcmp (option->choices[j].choice, "Default") == 0 ||
strcmp (option->choices[j].choice, "None") == 0 ||
strcmp (option->choices[j].choice, "PrinterDefault") == 0 ||
strcmp (option->choices[j].choice, "Unspecified") == 0 ||
option->choices[j].code == NULL ||
option->choices[j].code[0] == 0)
{
found_auto = TRUE;
break;
}
}
}
if (!found_auto)
add_auto = 1;
}
if (available)
{
*available = g_new (ppd_choice_t *, option->num_choices - num_conflicts + add_auto);
i = 0;
for (j = 0; j < option->num_choices; j++)
{
if (!conflicts[j])
(*available)[i++] = &option->choices[j];
}
if (add_auto)
(*available)[i++] = NULL;
}
g_free (conflicts);
return option->num_choices - num_conflicts + add_auto;
}
static GtkPrinterOption *
create_pickone_option (ppd_file_t *ppd_file,
ppd_option_t *ppd_option,
const gchar *gtk_name)
{
GtkPrinterOption *option;
ppd_choice_t **available;
char *label;
int n_choices;
int i;
#ifdef HAVE_CUPS_API_1_2
ppd_coption_t *coption;
#endif
g_assert (ppd_option->ui == PPD_UI_PICKONE);
option = NULL;
n_choices = available_choices (ppd_file, ppd_option, &available, g_str_has_prefix (gtk_name, "gtk-"));
if (n_choices > 0)
{
/* right now only support one parameter per custom option
* if more than one print warning and only offer the default choices
*/
label = get_option_text (ppd_file, ppd_option);
#ifdef HAVE_CUPS_API_1_2
coption = ppdFindCustomOption (ppd_file, ppd_option->keyword);
if (coption)
{
ppd_cparam_t *cparam;
cparam = ppdFirstCustomParam (coption);
if (ppdNextCustomParam (coption) == NULL)
{
switch (cparam->type)
{
case PPD_CUSTOM_INT:
option = gtk_printer_option_new (gtk_name, label,
GTK_PRINTER_OPTION_TYPE_PICKONE_INT);
break;
case PPD_CUSTOM_PASSCODE:
option = gtk_printer_option_new (gtk_name, label,
GTK_PRINTER_OPTION_TYPE_PICKONE_PASSCODE);
break;
case PPD_CUSTOM_PASSWORD:
option = gtk_printer_option_new (gtk_name, label,
GTK_PRINTER_OPTION_TYPE_PICKONE_PASSWORD);
break;
case PPD_CUSTOM_REAL:
option = gtk_printer_option_new (gtk_name, label,
GTK_PRINTER_OPTION_TYPE_PICKONE_REAL);
break;
case PPD_CUSTOM_STRING:
option = gtk_printer_option_new (gtk_name, label,
GTK_PRINTER_OPTION_TYPE_PICKONE_STRING);
break;
#ifdef PRINT_IGNORED_OPTIONS
case PPD_CUSTOM_POINTS:
g_warning ("CUPS Backend: PPD Custom Points Option not supported");
break;
case PPD_CUSTOM_CURVE:
g_warning ("CUPS Backend: PPD Custom Curve Option not supported");
break;
case PPD_CUSTOM_INVCURVE:
g_warning ("CUPS Backend: PPD Custom Inverse Curve Option not supported");
break;
#endif
default:
break;
}
}
#ifdef PRINT_IGNORED_OPTIONS
else
g_warning ("CUPS Backend: Multi-parameter PPD Custom Option not supported");
#endif
}
#endif /* HAVE_CUPS_API_1_2 */
if (!option)
option = gtk_printer_option_new (gtk_name, label,
GTK_PRINTER_OPTION_TYPE_PICKONE);
g_free (label);
gtk_printer_option_allocate_choices (option, n_choices);
for (i = 0; i < n_choices; i++)
{
if (available[i] == NULL)
{
/* This was auto-added */
option->choices[i] = g_strdup ("gtk-ignore-value");
option->choices_display[i] = g_strdup (_("Printer Default"));
}
else
{
option->choices[i] = g_strdup (available[i]->choice);
option->choices_display[i] = get_choice_text (ppd_file, available[i]);
}
}
if (option->type != GTK_PRINTER_OPTION_TYPE_PICKONE)
{
if (g_str_has_prefix (ppd_option->defchoice, "Custom."))
gtk_printer_option_set (option, ppd_option->defchoice + 7);
else
gtk_printer_option_set (option, ppd_option->defchoice);
}
else
{
gtk_printer_option_set (option, ppd_option->defchoice);
}
}
#ifdef PRINT_IGNORED_OPTIONS
else
g_warning ("CUPS Backend: Ignoring pickone %s\n", ppd_option->text);
#endif
g_free (available);
return option;
}
static GtkPrinterOption *
create_boolean_option (ppd_file_t *ppd_file,
ppd_option_t *ppd_option,
const gchar *gtk_name)
{
GtkPrinterOption *option;
ppd_choice_t **available;
char *label;
int n_choices;
g_assert (ppd_option->ui == PPD_UI_BOOLEAN);
option = NULL;
n_choices = available_choices (ppd_file, ppd_option, &available, g_str_has_prefix (gtk_name, "gtk-"));
if (n_choices == 2)
{
label = get_option_text (ppd_file, ppd_option);
option = gtk_printer_option_new (gtk_name, label,
GTK_PRINTER_OPTION_TYPE_BOOLEAN);
g_free (label);
gtk_printer_option_allocate_choices (option, 2);
option->choices[0] = g_strdup ("True");
option->choices_display[0] = g_strdup ("True");
option->choices[1] = g_strdup ("False");
option->choices_display[1] = g_strdup ("False");
gtk_printer_option_set (option, ppd_option->defchoice);
}
#ifdef PRINT_IGNORED_OPTIONS
else
g_warning ("CUPS Backend: Ignoring boolean %s\n", ppd_option->text);
#endif
g_free (available);
return option;
}
static gchar *
get_ppd_option_name (const gchar *keyword)
{
int i;
for (i = 0; i < G_N_ELEMENTS (ppd_option_names); i++)
if (strcmp (ppd_option_names[i].ppd_keyword, keyword) == 0)
return g_strdup (ppd_option_names[i].name);
return g_strdup_printf ("cups-%s", keyword);
}
static gchar *
get_lpoption_name (const gchar *lpoption)
{
int i;
for (i = 0; i < G_N_ELEMENTS (ppd_option_names); i++)
if (strcmp (ppd_option_names[i].ppd_keyword, lpoption) == 0)
return g_strdup (ppd_option_names[i].name);
for (i = 0; i < G_N_ELEMENTS (lpoption_names); i++)
if (strcmp (lpoption_names[i].lpoption, lpoption) == 0)
return g_strdup (lpoption_names[i].name);
return g_strdup_printf ("cups-%s", lpoption);
}
static int
strptr_cmp (const void *a,
const void *b)
{
char **aa = (char **)a;
char **bb = (char **)b;
return strcmp (*aa, *bb);
}
static gboolean
string_in_table (gchar *str,
const gchar *table[],
gint table_len)
{
return bsearch (&str, table, table_len, sizeof (char *), (void *)strptr_cmp) != NULL;
}
#define STRING_IN_TABLE(_str, _table) (string_in_table (_str, _table, G_N_ELEMENTS (_table)))
static void
handle_option (GtkPrinterOptionSet *set,
ppd_file_t *ppd_file,
ppd_option_t *ppd_option,
ppd_group_t *toplevel_group,
GtkPrintSettings *settings)
{
GtkPrinterOption *option;
char *name;
int i;
if (STRING_IN_TABLE (ppd_option->keyword, cups_option_blacklist))
return;
name = get_ppd_option_name (ppd_option->keyword);
option = NULL;
if (ppd_option->ui == PPD_UI_PICKONE)
{
option = create_pickone_option (ppd_file, ppd_option, name);
}
else if (ppd_option->ui == PPD_UI_BOOLEAN)
{
option = create_boolean_option (ppd_file, ppd_option, name);
}
#ifdef PRINT_IGNORED_OPTIONS
else
g_warning ("CUPS Backend: Ignoring pickmany setting %s\n", ppd_option->text);
#endif
if (option)
{
char *name;
name = ppd_group_name (toplevel_group);
if (STRING_IN_TABLE (name,
color_group_whitelist) ||
STRING_IN_TABLE (ppd_option->keyword,
color_option_whitelist))
{
option->group = g_strdup ("ColorPage");
}
else if (STRING_IN_TABLE (name,
image_quality_group_whitelist) ||
STRING_IN_TABLE (ppd_option->keyword,
image_quality_option_whitelist))
{
option->group = g_strdup ("ImageQualityPage");
}
else if (STRING_IN_TABLE (name,
finishing_group_whitelist) ||
STRING_IN_TABLE (ppd_option->keyword,
finishing_option_whitelist))
{
option->group = g_strdup ("FinishingPage");
}
else
{
for (i = 0; i < G_N_ELEMENTS (cups_group_translations); i++)
{
if (strcmp (cups_group_translations[i].name, toplevel_group->name) == 0)
{
option->group = g_strdup (_(cups_group_translations[i].translation));
break;
}
}
if (i == G_N_ELEMENTS (cups_group_translations))
option->group = g_strdup (toplevel_group->text);
}
set_option_from_settings (option, settings);
gtk_printer_option_set_add (set, option);
}
g_free (name);
}
static void
handle_group (GtkPrinterOptionSet *set,
ppd_file_t *ppd_file,
ppd_group_t *group,
ppd_group_t *toplevel_group,
GtkPrintSettings *settings)
{
gint i;
gchar *name;
/* Ignore installable options */
name = ppd_group_name (toplevel_group);
if (strcmp (name, "InstallableOptions") == 0)
return;
for (i = 0; i < group->num_options; i++)
handle_option (set, ppd_file, &group->options[i], toplevel_group, settings);
for (i = 0; i < group->num_subgroups; i++)
handle_group (set, ppd_file, &group->subgroups[i], toplevel_group, settings);
}
static GtkPrinterOptionSet *
cups_printer_get_options (GtkPrinter *printer,
GtkPrintSettings *settings,
GtkPageSetup *page_setup,
GtkPrintCapabilities capabilities)
{
GtkPrinterOptionSet *set;
GtkPrinterOption *option;
ppd_file_t *ppd_file;
int i;
char *print_at[] = { "now", "at", "on-hold" };
char *n_up[] = {"1", "2", "4", "6", "9", "16" };
char *prio[] = {"100", "80", "50", "30" };
/* Translators: These strings name the possible values of the
* job priority option in the print dialog
*/
char *prio_display[] = {N_("Urgent"), N_("High"), N_("Medium"), N_("Low") };
char *n_up_layout[] = { "lrtb", "lrbt", "rltb", "rlbt", "tblr", "tbrl", "btlr", "btrl" };
/* Translators: These strings name the possible arrangements of
* multiple pages on a sheet when printing
*/
char *n_up_layout_display[] = { N_("Left to right, top to bottom"), N_("Left to right, bottom to top"),
N_("Right to left, top to bottom"), N_("Right to left, bottom to top"),
N_("Top to bottom, left to right"), N_("Top to bottom, right to left"),
N_("Bottom to top, left to right"), N_("Bottom to top, right to left") };
char *name;
int num_opts;
cups_option_t *opts = NULL;
GtkPrintBackendCups *backend;
GtkTextDirection text_direction;
GtkPrinterCups *cups_printer = NULL;
set = gtk_printer_option_set_new ();
/* Cups specific, non-ppd related settings */
/* Translators, this string is used to label the pages-per-sheet option
* in the print dialog
*/
option = gtk_printer_option_new ("gtk-n-up", _("Pages per Sheet"), GTK_PRINTER_OPTION_TYPE_PICKONE);
gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up),
n_up, n_up);
gtk_printer_option_set (option, "1");
set_option_from_settings (option, settings);
gtk_printer_option_set_add (set, option);
g_object_unref (option);
if (cups_printer_get_capabilities (printer) & GTK_PRINT_CAPABILITY_NUMBER_UP_LAYOUT)
{
for (i = 0; i < G_N_ELEMENTS (n_up_layout_display); i++)
n_up_layout_display[i] = _(n_up_layout_display[i]);
/* Translators, this string is used to label the option in the print
* dialog that controls in what order multiple pages are arranged
*/
option = gtk_printer_option_new ("gtk-n-up-layout", _("Page Ordering"), GTK_PRINTER_OPTION_TYPE_PICKONE);
gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up_layout),
n_up_layout, n_up_layout_display);
text_direction = gtk_widget_get_default_direction ();
if (text_direction == GTK_TEXT_DIR_LTR)
gtk_printer_option_set (option, "lrtb");
else
gtk_printer_option_set (option, "rltb");
set_option_from_settings (option, settings);
gtk_printer_option_set_add (set, option);
g_object_unref (option);
}
for (i = 0; i < G_N_ELEMENTS(prio_display); i++)
prio_display[i] = _(prio_display[i]);
/* Translators, this string is used to label the job priority option
* in the print dialog
*/
option = gtk_printer_option_new ("gtk-job-prio", _("Job Priority"), GTK_PRINTER_OPTION_TYPE_PICKONE);
gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (prio),
prio, prio_display);
gtk_printer_option_set (option, "50");
set_option_from_settings (option, settings);
gtk_printer_option_set_add (set, option);
g_object_unref (option);
/* Translators, this string is used to label the billing info entry
* in the print dialog
*/
option = gtk_printer_option_new ("gtk-billing-info", _("Billing Info"), GTK_PRINTER_OPTION_TYPE_STRING);
gtk_printer_option_set (option, "");
set_option_from_settings (option, settings);
gtk_printer_option_set_add (set, option);
g_object_unref (option);
backend = GTK_PRINT_BACKEND_CUPS (gtk_printer_get_backend (printer));
cups_printer = GTK_PRINTER_CUPS (printer);
if (backend != NULL && printer != NULL)
{
char *cover_default[] = {"none", "classified", "confidential", "secret", "standard", "topsecret", "unclassified" };
/* Translators, these strings are names for various 'standard' cover
* pages that the printing system may support.
*/
char *cover_display_default[] = {N_("None"), N_("Classified"), N_("Confidential"), N_("Secret"), N_("Standard"), N_("Top Secret"), N_("Unclassified"),};
char **cover = NULL;
char **cover_display = NULL;
char **cover_display_translated = NULL;
gint num_of_covers = 0;
gpointer value;
gint j;
num_of_covers = backend->number_of_covers;
cover = g_new (char *, num_of_covers + 1);
cover[num_of_covers] = NULL;
cover_display = g_new (char *, num_of_covers + 1);
cover_display[num_of_covers] = NULL;
cover_display_translated = g_new (char *, num_of_covers + 1);
cover_display_translated[num_of_covers] = NULL;
for (i = 0; i < num_of_covers; i++)
{
cover[i] = g_strdup (backend->covers[i]);
value = NULL;
for (j = 0; j < G_N_ELEMENTS (cover_default); j++)
if (strcmp (cover_default[j], cover[i]) == 0)
{
value = cover_display_default[j];
break;
}
cover_display[i] = (value != NULL) ? g_strdup (value) : g_strdup (backend->covers[i]);
}
for (i = 0; i < num_of_covers; i++)
cover_display_translated[i] = _(cover_display[i]);
/* Translators, this is the label used for the option in the print
* dialog that controls the front cover page.
*/
option = gtk_printer_option_new ("gtk-cover-before", _("Before"), GTK_PRINTER_OPTION_TYPE_PICKONE);
gtk_printer_option_choices_from_array (option, num_of_covers,
cover, cover_display_translated);
if (cups_printer->default_cover_before != NULL)
gtk_printer_option_set (option, cups_printer->default_cover_before);
else
gtk_printer_option_set (option, "none");
set_option_from_settings (option, settings);
gtk_printer_option_set_add (set, option);
g_object_unref (option);
/* Translators, this is the label used for the option in the print
* dialog that controls the back cover page.
*/
option = gtk_printer_option_new ("gtk-cover-after", _("After"), GTK_PRINTER_OPTION_TYPE_PICKONE);
gtk_printer_option_choices_from_array (option, num_of_covers,
cover, cover_display_translated);
if (cups_printer->default_cover_after != NULL)
gtk_printer_option_set (option, cups_printer->default_cover_after);
else
gtk_printer_option_set (option, "none");
set_option_from_settings (option, settings);
gtk_printer_option_set_add (set, option);
g_object_unref (option);
g_strfreev (cover);
g_strfreev (cover_display);
g_free (cover_display_translated);
}
/* Translators: this is the name of the option that controls when
* a print job is printed. Possible values are 'now', a specified time,
* or 'on hold'
*/
option = gtk_printer_option_new ("gtk-print-time", _("Print at"), GTK_PRINTER_OPTION_TYPE_PICKONE);
gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (print_at),
print_at, print_at);
gtk_printer_option_set (option, "now");
set_option_from_settings (option, settings);
gtk_printer_option_set_add (set, option);
g_object_unref (option);
/* Translators: this is the name of the option that allows the user
* to specify a time when a print job will be printed.
*/
option = gtk_printer_option_new ("gtk-print-time-text", _("Print at time"), GTK_PRINTER_OPTION_TYPE_STRING);
gtk_printer_option_set (option, "");
set_option_from_settings (option, settings);
gtk_printer_option_set_add (set, option);
g_object_unref (option);
/* Printer (ppd) specific settings */
ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
if (ppd_file)
{
GtkPaperSize *paper_size;
ppd_option_t *option;
const gchar *ppd_name;
ppdMarkDefaults (ppd_file);
paper_size = gtk_page_setup_get_paper_size (page_setup);
option = ppdFindOption (ppd_file, "PageSize");
if (option)
{
ppd_name = gtk_paper_size_get_ppd_name (paper_size);
if (ppd_name)
strncpy (option->defchoice, ppd_name, PPD_MAX_NAME);
else
{
gchar *custom_name;
char width[G_ASCII_DTOSTR_BUF_SIZE];
char height[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_formatd (width, sizeof (width), "%.2f",
gtk_paper_size_get_width (paper_size,
GTK_UNIT_POINTS));
g_ascii_formatd (height, sizeof (height), "%.2f",
gtk_paper_size_get_height (paper_size,
GTK_UNIT_POINTS));
/* Translators: this format is used to display a custom
* paper size. The two placeholders are replaced with
* the width and height in points. E.g: "Custom
* 230.4x142.9"
*/
custom_name = g_strdup_printf (_("Custom %sx%s"), width, height);
strncpy (option->defchoice, custom_name, PPD_MAX_NAME);
g_free (custom_name);
}
}
for (i = 0; i < ppd_file->num_groups; i++)
handle_group (set, ppd_file, &ppd_file->groups[i], &ppd_file->groups[i], settings);
}
/* Now honor the user set defaults for this printer */
num_opts = cups_get_user_options (gtk_printer_get_name (printer), 0, &opts);
for (i = 0; i < num_opts; i++)
{
if (STRING_IN_TABLE (opts[i].name, cups_option_blacklist))
continue;
name = get_lpoption_name (opts[i].name);
if (strcmp (name, "cups-job-sheets") == 0)
{
gchar **values;
gint num_values;
values = g_strsplit (opts[i].value, ",", 2);
num_values = g_strv_length (values);
option = gtk_printer_option_set_lookup (set, "gtk-cover-before");
if (option && num_values > 0)
gtk_printer_option_set (option, g_strstrip (values[0]));
option = gtk_printer_option_set_lookup (set, "gtk-cover-after");
if (option && num_values > 1)
gtk_printer_option_set (option, g_strstrip (values[1]));
g_strfreev (values);
}
else if (strcmp (name, "cups-job-hold-until") == 0)
{
GtkPrinterOption *option2 = NULL;
option = gtk_printer_option_set_lookup (set, "gtk-print-time-text");
if (option && opts[i].value)
{
option2 = gtk_printer_option_set_lookup (set, "gtk-print-time");
if (option2)
{
if (strcmp (opts[i].value, "indefinite") == 0)
gtk_printer_option_set (option2, "on-hold");
else
{
gtk_printer_option_set (option2, "at");
gtk_printer_option_set (option, opts[i].value);
}
}
}
}
else if (strcmp (name, "cups-sides") == 0)
{
option = gtk_printer_option_set_lookup (set, "gtk-duplex");
if (option && opts[i].value)
{
if (strcmp (opts[i].value, "two-sided-short-edge") == 0)
gtk_printer_option_set (option, "DuplexTumble");
else if (strcmp (opts[i].value, "two-sided-long-edge") == 0)
gtk_printer_option_set (option, "DuplexNoTumble");
}
}
else
{
option = gtk_printer_option_set_lookup (set, name);
if (option)
gtk_printer_option_set (option, opts[i].value);
}
g_free (name);
}
cupsFreeOptions (num_opts, opts);
return set;
}
static void
mark_option_from_set (GtkPrinterOptionSet *set,
ppd_file_t *ppd_file,
ppd_option_t *ppd_option)
{
GtkPrinterOption *option;
char *name = get_ppd_option_name (ppd_option->keyword);
option = gtk_printer_option_set_lookup (set, name);
if (option)
ppdMarkOption (ppd_file, ppd_option->keyword, option->value);
g_free (name);
}
static void
mark_group_from_set (GtkPrinterOptionSet *set,
ppd_file_t *ppd_file,
ppd_group_t *group)
{
int i;
for (i = 0; i < group->num_options; i++)
mark_option_from_set (set, ppd_file, &group->options[i]);
for (i = 0; i < group->num_subgroups; i++)
mark_group_from_set (set, ppd_file, &group->subgroups[i]);
}
static void
set_conflicts_from_option (GtkPrinterOptionSet *set,
ppd_file_t *ppd_file,
ppd_option_t *ppd_option)
{
GtkPrinterOption *option;
char *name;
if (ppd_option->conflicted)
{
name = get_ppd_option_name (ppd_option->keyword);
option = gtk_printer_option_set_lookup (set, name);
if (option)
gtk_printer_option_set_has_conflict (option, TRUE);
#ifdef PRINT_IGNORED_OPTIONS
else
g_warning ("CUPS Backend: Ignoring conflict for option %s", ppd_option->keyword);
#endif
g_free (name);
}
}
static void
set_conflicts_from_group (GtkPrinterOptionSet *set,
ppd_file_t *ppd_file,
ppd_group_t *group)
{
int i;
for (i = 0; i < group->num_options; i++)
set_conflicts_from_option (set, ppd_file, &group->options[i]);
for (i = 0; i < group->num_subgroups; i++)
set_conflicts_from_group (set, ppd_file, &group->subgroups[i]);
}
static gboolean
cups_printer_mark_conflicts (GtkPrinter *printer,
GtkPrinterOptionSet *options)
{
ppd_file_t *ppd_file;
int num_conflicts;
int i;
ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
if (ppd_file == NULL)
return FALSE;
ppdMarkDefaults (ppd_file);
for (i = 0; i < ppd_file->num_groups; i++)
mark_group_from_set (options, ppd_file, &ppd_file->groups[i]);
num_conflicts = ppdConflicts (ppd_file);
if (num_conflicts > 0)
{
for (i = 0; i < ppd_file->num_groups; i++)
set_conflicts_from_group (options, ppd_file, &ppd_file->groups[i]);
}
return num_conflicts > 0;
}
struct OptionData {
GtkPrinter *printer;
GtkPrinterOptionSet *options;
GtkPrintSettings *settings;
ppd_file_t *ppd_file;
};
typedef struct {
const char *cups;
const char *standard;
} NameMapping;
static void
map_settings_to_option (GtkPrinterOption *option,
const NameMapping table[],
gint n_elements,
GtkPrintSettings *settings,
const gchar *standard_name,
const gchar *cups_name)
{
int i;
char *name;
const char *cups_value;
const char *standard_value;
/* If the cups-specific setting is set, always use that */
name = g_strdup_printf ("cups-%s", cups_name);
cups_value = gtk_print_settings_get (settings, name);
g_free (name);
if (cups_value != NULL)
{
gtk_printer_option_set (option, cups_value);
return;
}
/* Otherwise we try to convert from the general setting */
standard_value = gtk_print_settings_get (settings, standard_name);
if (standard_value == NULL)
return;
for (i = 0; i < n_elements; i++)
{
if (table[i].cups == NULL && table[i].standard == NULL)
{
gtk_printer_option_set (option, standard_value);
break;
}
else if (table[i].cups == NULL &&
strcmp (table[i].standard, standard_value) == 0)
{
set_option_off (option);
break;
}
else if (strcmp (table[i].standard, standard_value) == 0)
{
gtk_printer_option_set (option, table[i].cups);
break;
}
}
}
static void
map_option_to_settings (const gchar *value,
const NameMapping table[],
gint n_elements,
GtkPrintSettings *settings,
const gchar *standard_name,
const gchar *cups_name)
{
int i;
char *name;
for (i = 0; i < n_elements; i++)
{
if (table[i].cups == NULL && table[i].standard == NULL)
{
gtk_print_settings_set (settings,
standard_name,
value);
break;
}
else if (table[i].cups == NULL && table[i].standard != NULL)
{
if (value_is_off (value))
{
gtk_print_settings_set (settings,
standard_name,
table[i].standard);
break;
}
}
else if (strcmp (table[i].cups, value) == 0)
{
gtk_print_settings_set (settings,
standard_name,
table[i].standard);
break;
}
}
/* Always set the corresponding cups-specific setting */
name = g_strdup_printf ("cups-%s", cups_name);
gtk_print_settings_set (settings, name, value);
g_free (name);
}
static const NameMapping paper_source_map[] = {
{ "Lower", "lower"},
{ "Middle", "middle"},
{ "Upper", "upper"},
{ "Rear", "rear"},
{ "Envelope", "envelope"},
{ "Cassette", "cassette"},
{ "LargeCapacity", "large-capacity"},
{ "AnySmallFormat", "small-format"},
{ "AnyLargeFormat", "large-format"},
{ NULL, NULL}
};
static const NameMapping output_tray_map[] = {
{ "Upper", "upper"},
{ "Lower", "lower"},
{ "Rear", "rear"},
{ NULL, NULL}
};
static const NameMapping duplex_map[] = {
{ "DuplexTumble", "vertical" },
{ "DuplexNoTumble", "horizontal" },
{ NULL, "simplex" }
};
static const NameMapping output_mode_map[] = {
{ "Standard", "normal" },
{ "Normal", "normal" },
{ "Draft", "draft" },
{ "Fast", "draft" },
};
static const NameMapping media_type_map[] = {
{ "Transparency", "transparency"},
{ "Standard", "stationery"},
{ NULL, NULL}
};
static const NameMapping all_map[] = {
{ NULL, NULL}
};
static void
set_option_from_settings (GtkPrinterOption *option,
GtkPrintSettings *settings)
{
const char *cups_value;
char *value;
if (settings == NULL)
return;
if (strcmp (option->name, "gtk-paper-source") == 0)
map_settings_to_option (option, paper_source_map, G_N_ELEMENTS (paper_source_map),
settings, GTK_PRINT_SETTINGS_DEFAULT_SOURCE, "InputSlot");
else if (strcmp (option->name, "gtk-output-tray") == 0)
map_settings_to_option (option, output_tray_map, G_N_ELEMENTS (output_tray_map),
settings, GTK_PRINT_SETTINGS_OUTPUT_BIN, "OutputBin");
else if (strcmp (option->name, "gtk-duplex") == 0)
map_settings_to_option (option, duplex_map, G_N_ELEMENTS (duplex_map),
settings, GTK_PRINT_SETTINGS_DUPLEX, "Duplex");
else if (strcmp (option->name, "cups-OutputMode") == 0)
map_settings_to_option (option, output_mode_map, G_N_ELEMENTS (output_mode_map),
settings, GTK_PRINT_SETTINGS_QUALITY, "OutputMode");
else if (strcmp (option->name, "cups-Resolution") == 0)
{
cups_value = gtk_print_settings_get (settings, option->name);
if (cups_value)
gtk_printer_option_set (option, cups_value);
else
{
int res = gtk_print_settings_get_resolution (settings);
int res_x = gtk_print_settings_get_resolution_x (settings);
int res_y = gtk_print_settings_get_resolution_y (settings);
if (res_x != res_y)
{
value = g_strdup_printf ("%dx%ddpi", res_x, res_y);
gtk_printer_option_set (option, value);
g_free (value);
}
else if (res != 0)
{
value = g_strdup_printf ("%ddpi", res);
gtk_printer_option_set (option, value);
g_free (value);
}
}
}
else if (strcmp (option->name, "gtk-paper-type") == 0)
map_settings_to_option (option, media_type_map, G_N_ELEMENTS (media_type_map),
settings, GTK_PRINT_SETTINGS_MEDIA_TYPE, "MediaType");
else if (strcmp (option->name, "gtk-n-up") == 0)
{
map_settings_to_option (option, all_map, G_N_ELEMENTS (all_map),
settings, GTK_PRINT_SETTINGS_NUMBER_UP, "number-up");
}
else if (strcmp (option->name, "gtk-n-up-layout") == 0)
{
map_settings_to_option (option, all_map, G_N_ELEMENTS (all_map),
settings, GTK_PRINT_SETTINGS_NUMBER_UP_LAYOUT, "number-up-layout");
}
else if (strcmp (option->name, "gtk-billing-info") == 0)
{
cups_value = gtk_print_settings_get (settings, "cups-job-billing");
if (cups_value)
gtk_printer_option_set (option, cups_value);
}
else if (strcmp (option->name, "gtk-job-prio") == 0)
{
cups_value = gtk_print_settings_get (settings, "cups-job-priority");
if (cups_value)
gtk_printer_option_set (option, cups_value);
}
else if (strcmp (option->name, "gtk-cover-before") == 0)
{
cups_value = gtk_print_settings_get (settings, "cover-before");
if (cups_value)
gtk_printer_option_set (option, cups_value);
}
else if (strcmp (option->name, "gtk-cover-after") == 0)
{
cups_value = gtk_print_settings_get (settings, "cover-after");
if (cups_value)
gtk_printer_option_set (option, cups_value);
}
else if (strcmp (option->name, "gtk-print-time") == 0)
{
cups_value = gtk_print_settings_get (settings, "print-at");
if (cups_value)
gtk_printer_option_set (option, cups_value);
}
else if (strcmp (option->name, "gtk-print-time-text") == 0)
{
cups_value = gtk_print_settings_get (settings, "print-at-time");
if (cups_value)
gtk_printer_option_set (option, cups_value);
}
else if (g_str_has_prefix (option->name, "cups-"))
{
cups_value = gtk_print_settings_get (settings, option->name);
if (cups_value)
gtk_printer_option_set (option, cups_value);
}
}
static void
foreach_option_get_settings (GtkPrinterOption *option,
gpointer user_data)
{
struct OptionData *data = user_data;
GtkPrintSettings *settings = data->settings;
const char *value;
value = option->value;
if (strcmp (option->name, "gtk-paper-source") == 0)
map_option_to_settings (value, paper_source_map, G_N_ELEMENTS (paper_source_map),
settings, GTK_PRINT_SETTINGS_DEFAULT_SOURCE, "InputSlot");
else if (strcmp (option->name, "gtk-output-tray") == 0)
map_option_to_settings (value, output_tray_map, G_N_ELEMENTS (output_tray_map),
settings, GTK_PRINT_SETTINGS_OUTPUT_BIN, "OutputBin");
else if (strcmp (option->name, "gtk-duplex") == 0)
map_option_to_settings (value, duplex_map, G_N_ELEMENTS (duplex_map),
settings, GTK_PRINT_SETTINGS_DUPLEX, "Duplex");
else if (strcmp (option->name, "cups-OutputMode") == 0)
map_option_to_settings (value, output_mode_map, G_N_ELEMENTS (output_mode_map),
settings, GTK_PRINT_SETTINGS_QUALITY, "OutputMode");
else if (strcmp (option->name, "cups-Resolution") == 0)
{
int res, res_x, res_y;
if (sscanf (value, "%dx%ddpi", &res_x, &res_y) == 2)
{
if (res_x > 0 && res_y > 0)
gtk_print_settings_set_resolution_xy (settings, res_x, res_y);
}
else if (sscanf (value, "%ddpi", &res) == 1)
{
if (res > 0)
gtk_print_settings_set_resolution (settings, res);
}
gtk_print_settings_set (settings, option->name, value);
}
else if (strcmp (option->name, "gtk-paper-type") == 0)
map_option_to_settings (value, media_type_map, G_N_ELEMENTS (media_type_map),
settings, GTK_PRINT_SETTINGS_MEDIA_TYPE, "MediaType");
else if (strcmp (option->name, "gtk-n-up") == 0)
map_option_to_settings (value, all_map, G_N_ELEMENTS (all_map),
settings, GTK_PRINT_SETTINGS_NUMBER_UP, "number-up");
else if (strcmp (option->name, "gtk-n-up-layout") == 0)
map_option_to_settings (value, all_map, G_N_ELEMENTS (all_map),
settings, GTK_PRINT_SETTINGS_NUMBER_UP_LAYOUT, "number-up-layout");
else if (strcmp (option->name, "gtk-billing-info") == 0 && strlen (value) > 0)
gtk_print_settings_set (settings, "cups-job-billing", value);
else if (strcmp (option->name, "gtk-job-prio") == 0)
gtk_print_settings_set (settings, "cups-job-priority", value);
else if (strcmp (option->name, "gtk-cover-before") == 0)
gtk_print_settings_set (settings, "cover-before", value);
else if (strcmp (option->name, "gtk-cover-after") == 0)
gtk_print_settings_set (settings, "cover-after", value);
else if (strcmp (option->name, "gtk-print-time") == 0)
gtk_print_settings_set (settings, "print-at", value);
else if (strcmp (option->name, "gtk-print-time-text") == 0)
gtk_print_settings_set (settings, "print-at-time", value);
else if (g_str_has_prefix (option->name, "cups-"))
gtk_print_settings_set (settings, option->name, value);
}
static gboolean
supports_am_pm (void)
{
struct tm tmp_tm = { 0 };
char time[8];
int length;
length = strftime (time, sizeof (time), "%p", &tmp_tm);
return length != 0;
}
/* Converts local time to UTC time. Local time has to be in one of these
* formats: HH:MM:SS, HH:MM, HH:MM:SS {am, pm}, HH:MM {am, pm}, HH {am, pm},
* {am, pm} HH:MM:SS, {am, pm} HH:MM, {am, pm} HH.
* Returns a newly allocated string holding UTC time in HH:MM:SS format
* or NULL.
*/
gchar *
localtime_to_utctime (const char *local_time)
{
const char *formats_0[] = {" %I : %M : %S %p ", " %p %I : %M : %S ",
" %H : %M : %S ",
" %I : %M %p ", " %p %I : %M ",
" %H : %M ",
" %I %p ", " %p %I "};
const char *formats_1[] = {" %H : %M : %S ", " %H : %M "};
const char *end = NULL;
struct tm *actual_local_time;
struct tm *actual_utc_time;
struct tm local_print_time;
struct tm utc_print_time;
struct tm diff_time;
gchar *utc_time = NULL;
int i, n;
if (local_time == NULL || local_time[0] == '\0')
return NULL;
n = supports_am_pm () ? G_N_ELEMENTS (formats_0) : G_N_ELEMENTS (formats_1);
for (i = 0; i < n; i++)
{
local_print_time.tm_hour = 0;
local_print_time.tm_min = 0;
local_print_time.tm_sec = 0;
if (supports_am_pm ())
end = strptime (local_time, formats_0[i], &local_print_time);
else
end = strptime (local_time, formats_1[i], &local_print_time);
if (end != NULL && end[0] == '\0')
break;
}
if (end != NULL && end[0] == '\0')
{
time_t rawtime;
time (&rawtime);
actual_utc_time = g_memdup (gmtime (&rawtime), sizeof (struct tm));
actual_local_time = g_memdup (localtime (&rawtime), sizeof (struct tm));
diff_time.tm_hour = actual_utc_time->tm_hour - actual_local_time->tm_hour;
diff_time.tm_min = actual_utc_time->tm_min - actual_local_time->tm_min;
diff_time.tm_sec = actual_utc_time->tm_sec - actual_local_time->tm_sec;
utc_print_time.tm_hour = ((local_print_time.tm_hour + diff_time.tm_hour) + 24) % 24;
utc_print_time.tm_min = ((local_print_time.tm_min + diff_time.tm_min) + 60) % 60;
utc_print_time.tm_sec = ((local_print_time.tm_sec + diff_time.tm_sec) + 60) % 60;
utc_time = g_strdup_printf ("%02d:%02d:%02d",
utc_print_time.tm_hour,
utc_print_time.tm_min,
utc_print_time.tm_sec);
}
return utc_time;
}
static void
cups_printer_get_settings_from_options (GtkPrinter *printer,
GtkPrinterOptionSet *options,
GtkPrintSettings *settings)
{
struct OptionData data;
const char *print_at, *print_at_time;
data.printer = printer;
data.options = options;
data.settings = settings;
data.ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
gtk_printer_option_set_foreach (options, foreach_option_get_settings, &data);
if (data.ppd_file != NULL)
{
GtkPrinterOption *cover_before, *cover_after;
cover_before = gtk_printer_option_set_lookup (options, "gtk-cover-before");
cover_after = gtk_printer_option_set_lookup (options, "gtk-cover-after");
if (cover_before && cover_after)
{
char *value = g_strdup_printf ("%s,%s", cover_before->value, cover_after->value);
gtk_print_settings_set (settings, "cups-job-sheets", value);
g_free (value);
}
print_at = gtk_print_settings_get (settings, "print-at");
print_at_time = gtk_print_settings_get (settings, "print-at-time");
if (strcmp (print_at, "at") == 0)
{
gchar *utc_time = NULL;
utc_time = localtime_to_utctime (print_at_time);
if (utc_time != NULL)
{
gtk_print_settings_set (settings, "cups-job-hold-until", utc_time);
g_free (utc_time);
}
else
gtk_print_settings_set (settings, "cups-job-hold-until", print_at_time);
}
else if (strcmp (print_at, "on-hold") == 0)
gtk_print_settings_set (settings, "cups-job-hold-until", "indefinite");
}
}
static void
cups_printer_prepare_for_print (GtkPrinter *printer,
GtkPrintJob *print_job,
GtkPrintSettings *settings,
GtkPageSetup *page_setup)
{
GtkPageSet page_set;
GtkPaperSize *paper_size;
const char *ppd_paper_name;
double scale;
GtkPrintCapabilities capabilities;
capabilities = cups_printer_get_capabilities (printer);
print_job->print_pages = gtk_print_settings_get_print_pages (settings);
print_job->page_ranges = NULL;
print_job->num_page_ranges = 0;
if (print_job->print_pages == GTK_PRINT_PAGES_RANGES)
print_job->page_ranges =
gtk_print_settings_get_page_ranges (settings,
&print_job->num_page_ranges);
if (capabilities & GTK_PRINT_CAPABILITY_COLLATE)
{
if (gtk_print_settings_get_collate (settings))
gtk_print_settings_set (settings, "cups-Collate", "True");
print_job->collate = FALSE;
}
else
{
print_job->collate = gtk_print_settings_get_collate (settings);
}
if (capabilities & GTK_PRINT_CAPABILITY_REVERSE)
{
if (gtk_print_settings_get_reverse (settings))
gtk_print_settings_set (settings, "cups-OutputOrder", "Reverse");
print_job->reverse = FALSE;
}
else
{
print_job->reverse = gtk_print_settings_get_reverse (settings);
}
if (capabilities & GTK_PRINT_CAPABILITY_COPIES)
{
if (gtk_print_settings_get_n_copies (settings) > 1)
gtk_print_settings_set_int (settings, "cups-copies",
gtk_print_settings_get_n_copies (settings));
print_job->num_copies = 1;
}
else
{
print_job->num_copies = gtk_print_settings_get_n_copies (settings);
}
scale = gtk_print_settings_get_scale (settings);
print_job->scale = 1.0;
if (scale != 100.0)
print_job->scale = scale/100.0;
page_set = gtk_print_settings_get_page_set (settings);
if (page_set == GTK_PAGE_SET_EVEN)
gtk_print_settings_set (settings, "cups-page-set", "even");
else if (page_set == GTK_PAGE_SET_ODD)
gtk_print_settings_set (settings, "cups-page-set", "odd");
print_job->page_set = GTK_PAGE_SET_ALL;
paper_size = gtk_page_setup_get_paper_size (page_setup);
ppd_paper_name = gtk_paper_size_get_ppd_name (paper_size);
if (ppd_paper_name != NULL)
gtk_print_settings_set (settings, "cups-PageSize", ppd_paper_name);
else
{
char width[G_ASCII_DTOSTR_BUF_SIZE];
char height[G_ASCII_DTOSTR_BUF_SIZE];
char *custom_name;
g_ascii_formatd (width, sizeof (width), "%.2f", gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS));
g_ascii_formatd (height, sizeof (height), "%.2f", gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS));
custom_name = g_strdup_printf (("Custom.%sx%s"), width, height);
gtk_print_settings_set (settings, "cups-PageSize", custom_name);
g_free (custom_name);
}
if (gtk_print_settings_get_number_up (settings) > 1)
{
GtkNumberUpLayout layout = gtk_print_settings_get_number_up_layout (settings);
GEnumClass *enum_class;
GEnumValue *enum_value;
switch (gtk_page_setup_get_orientation (page_setup))
{
case GTK_PAGE_ORIENTATION_PORTRAIT:
break;
case GTK_PAGE_ORIENTATION_LANDSCAPE:
if (layout < 4)
layout = layout + 2 + 4 * (1 - layout / 2);
else
layout = layout - 3 - 2 * (layout % 2);
break;
case GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT:
layout = (layout + 3 - 2 * (layout % 2)) % 4 + 4 * (layout / 4);
break;
case GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE:
if (layout < 4)
layout = layout + 5 - 2 * (layout % 2);
else
layout = layout - 6 + 4 * (1 - (layout - 4) / 2);
break;
}
enum_class = g_type_class_ref (GTK_TYPE_NUMBER_UP_LAYOUT);
enum_value = g_enum_get_value (enum_class, layout);
gtk_print_settings_set (settings, "cups-number-up-layout", enum_value->value_nick);
g_type_class_unref (enum_class);
if (!(capabilities & GTK_PRINT_CAPABILITY_NUMBER_UP))
{
print_job->number_up = gtk_print_settings_get_number_up (settings);
print_job->number_up_layout = gtk_print_settings_get_number_up_layout (settings);
}
}
print_job->rotate_to_orientation = TRUE;
}
static GtkPageSetup *
create_page_setup (ppd_file_t *ppd_file,
ppd_size_t *size)
{
char *display_name;
GtkPageSetup *page_setup;
GtkPaperSize *paper_size;
ppd_option_t *option;
ppd_choice_t *choice;
display_name = NULL;
option = ppdFindOption (ppd_file, "PageSize");
if (option)
{
choice = ppdFindChoice (option, size->name);
if (choice)
display_name = ppd_text_to_utf8 (ppd_file, choice->text);
}
if (display_name == NULL)
display_name = g_strdup (size->name);
page_setup = gtk_page_setup_new ();
paper_size = gtk_paper_size_new_from_ppd (size->name,
display_name,
size->width,
size->length);
gtk_page_setup_set_paper_size (page_setup, paper_size);
gtk_paper_size_free (paper_size);
gtk_page_setup_set_top_margin (page_setup, size->length - size->top, GTK_UNIT_POINTS);
gtk_page_setup_set_bottom_margin (page_setup, size->bottom, GTK_UNIT_POINTS);
gtk_page_setup_set_left_margin (page_setup, size->left, GTK_UNIT_POINTS);
gtk_page_setup_set_right_margin (page_setup, size->width - size->right, GTK_UNIT_POINTS);
g_free (display_name);
return page_setup;
}
static GList *
cups_printer_list_papers (GtkPrinter *printer)
{
ppd_file_t *ppd_file;
ppd_size_t *size;
GtkPageSetup *page_setup;
GList *l;
int i;
ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
if (ppd_file == NULL)
return NULL;
l = NULL;
for (i = 0; i < ppd_file->num_sizes; i++)
{
size = &ppd_file->sizes[i];
page_setup = create_page_setup (ppd_file, size);
l = g_list_prepend (l, page_setup);
}
return g_list_reverse (l);
}
static GtkPageSetup *
cups_printer_get_default_page_size (GtkPrinter *printer)
{
ppd_file_t *ppd_file;
ppd_size_t *size;
ppd_option_t *option;
ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
if (ppd_file == NULL)
return NULL;
option = ppdFindOption (ppd_file, "PageSize");
if (option == NULL)
return NULL;
size = ppdPageSize (ppd_file, option->defchoice);
if (size == NULL)
return NULL;
return create_page_setup (ppd_file, size);
}
static gboolean
cups_printer_get_hard_margins (GtkPrinter *printer,
gdouble *top,
gdouble *bottom,
gdouble *left,
gdouble *right)
{
ppd_file_t *ppd_file;
ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
if (ppd_file == NULL)
return FALSE;
*left = ppd_file->custom_margins[0];
*bottom = ppd_file->custom_margins[1];
*right = ppd_file->custom_margins[2];
*top = ppd_file->custom_margins[3];
return TRUE;
}
static GtkPrintCapabilities
cups_printer_get_capabilities (GtkPrinter *printer)
{
GtkPrintCapabilities capabilities = 0;
GtkPrinterCups *cups_printer = GTK_PRINTER_CUPS (printer);
if (gtk_printer_cups_get_ppd (cups_printer))
{
capabilities = GTK_PRINT_CAPABILITY_REVERSE;
}
if (cups_printer->supports_copies)
{
capabilities |= GTK_PRINT_CAPABILITY_COPIES;
}
if (cups_printer->supports_collate)
{
capabilities |= GTK_PRINT_CAPABILITY_COLLATE;
}
if (cups_printer->supports_number_up)
{
capabilities |= GTK_PRINT_CAPABILITY_NUMBER_UP;
#if (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR >= 1 && CUPS_VERSION_PATCH >= 15) || (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR > 1) || CUPS_VERSION_MAJOR > 1
capabilities |= GTK_PRINT_CAPABILITY_NUMBER_UP_LAYOUT;
#endif
}
return capabilities;
}
|