1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245
|
/***************************************************************************
tqsl.cpp
-------------------
begin : Mon Nov 4 2002
copyright : (C) 2002-2025 by ARRL and the TrustedQSL Developers
author : Jon Bloom
email : jbloom@arrl.org
***************************************************************************/
#include <curl/curl.h> // has to be before something else in this list
#include <stdlib.h>
#include <errno.h>
#ifndef _WIN32
#include <unistd.h>
#include <fcntl.h>
#endif
#include <expat.h>
#include <sys/stat.h>
#ifndef _WIN32
#include <dirent.h>
#else
#include <direct.h>
#include "windirent.h"
#endif
#include <wx/wxprec.h>
#include <wx/object.h>
#include <wx/wxchar.h>
#include <wx/config.h>
#include <wx/regex.h>
#include <wx/tokenzr.h>
#include <wx/hashmap.h>
#include <wx/hyperlink.h>
#include <wx/cmdline.h>
#include <wx/notebook.h>
#include <wx/statline.h>
#include <wx/app.h>
#include <wx/stdpaths.h>
#include <wx/intl.h>
#include <wx/cshelp.h>
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#include <wx/wxhtml.h>
#include <wx/wfstream.h>
#ifdef _MSC_VER
// stdint exists on vs2012 and (I think) 2010, but not 2008 or its platform
#if _MSC_VER < 1600
#define uint8_t unsigned char
#endif
#else
#include <stdint.h> //for uint8_t; should be cstdint but this is C++11 and not universally supported
#endif
#ifdef _WIN32
#include <io.h>
HRESULT IsElevated(BOOL * pbElevated);
static HRESULT GetElevationType(__out TOKEN_ELEVATION_TYPE * ptet);
#endif
#include <zlib.h>
#include <openssl/opensslv.h> // only for version info!
#include <sqlite3.h> // only for version info!
#include <iostream>
#include <fstream>
#include <memory>
#include <string>
#include <vector>
#include <map>
#include <cstdio>
#ifdef HAVE_CONFIG_H
#include "sysconfig.h"
#endif
#include "tqslhelp.h"
#include "crqwiz.h"
#include "getpassword.h"
#include "loadcertwiz.h"
#include "tqsllib.h"
#include "util.h"
#include "wxutil.h"
#include "tqslwiz.h"
#include "qsodatadialog.h"
#include "tqslerrno.h"
#include "tqslexcept.h"
#include "tqslpaths.h"
#include "stationdial.h"
#include "tqslconvert.h"
#include "dxcc.h"
#include "tqsl_prefs.h"
#include "tqslbuild.h"
#include "tqslapp.h"
#include "tqsltrace.h"
#include "winstrdefs.h"
#include "jsonval.h"
#include "jsonreader.h"
using std::ifstream;
using std::ios;
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
using std::vector;
using std::string;
#ifdef _WIN32
#include "key.xpm"
#else
#include "key-new.xpm"
#endif
#include "save.xpm"
#include "upload.xpm"
#include "upload_dis.xpm"
#include "file_edit.xpm"
#include "file_edit_dis.xpm"
#include "loc_add.xpm"
#include "loc_add_dis.xpm"
#include "delete.xpm"
#include "delete_dis.xpm"
#include "edit.xpm"
#include "edit_dis.xpm"
#include "download.xpm"
#include "download_dis.xpm"
#include "properties.xpm"
#include "properties_dis.xpm"
#include "import.xpm"
#include "lotw.xpm"
/// GEOMETRY
#define LABEL_HEIGHT 20
#define CERTLIST_FLAGS TQSL_SELECT_CERT_WITHKEYS | TQSL_SELECT_CERT_SUPERCEDED | TQSL_SELECT_CERT_EXPIRED
static wxMenu *stn_menu;
static wxString flattenCallSign(const wxString& call);
static wxString ErrorTitle(_("TQSL Error"));
static bool verify_cert(tQSL_Location loc, bool editing);
static CURL* tqsl_curl_init(const char *logTitle, const char *url, FILE **curlLogFile, bool newFile);
static wxString origCommandLine = wxT("");
static MyFrame *frame = 0;
static char unipwd[64];
static bool quiet = false;
static bool verifyCA = true;
static FILE *curlLogFile;
static CURL *curlReq;
static FILE *origStderr;
static int lock_db(bool wait);
static void unlock_db(void);
int get_address_field(const char *callsign, const char *field, string& result);
static void cert_cleanup(void);
static void free_certlist(void);
static const char *OSTYPE =
#ifdef __WIN32
"Windows";
#elif defined(__APPLE__)
"Mac";
#else
"Linux";
#endif
static void exitNow(int status, bool quiet) {
const char *errors[] = { __("Success"), // TQSL_EXIT_SUCCESS
__("User Cancelled"), // TQSL_EXIT_CANCEL
__("Upload Rejected"), // TQSL_EXIT_REJECTED
__("Unexpected LoTW Response"), // TQSL_EXIT_UNEXP_RESP
__("TQSL Error"), // TQSL_EXIT_TQSL_ERROR
__("TQSLLib Error"), // TQSL_EXIT_LIB_ERROR
__("Error opening input file"), // TQSL_EXIT_ERR_OPEN_INPUT
__("Error opening output file"), // TQSL_EXIT_ERR_OPEN_OUTPUT
__("No QSOs to upload"), // TQSL_EXIT_NO_QSOS
__("Some QSOs not processed"), // TQSL_EXIT_SUPPRESSED
__("Command Syntax Error"), // TQSL_EXIT_COMMAND_ERROR
__("LoTW Connection Failed"), // TQSL_EXIT_CONNECTION_FAILED
__("Unknown"), // TQSL_EXIT_UNKNOWN
__("The TQSL upload tracking database is locked"),// TQSL_EXIT_BUSY
__("Previously signed QSOs were detected"), // TQSL_EXIT_UPLOADED_ALREADY
__("Incorrect passphrase when importing P12 file")// TQSL_EXIT_BAD_PASSPHRASE
};
int stat = status;
if (stat > TQSL_EXIT_BAD_PASSPHRASE || stat < 0) stat = TQSL_EXIT_UNKNOWN;
wxString msg = wxString::Format(wxT("Final Status: %hs (%d)"), errors[stat], status);
wxString err = wxGetTranslation(wxString::FromUTF8(errors[stat]));
wxString localmsg = wxString::Format(_("Final Status: %hs (%d)"), (const char *)err.ToUTF8(), status);
if (!quiet) {
if (msg != localmsg) {
wxLogMessage(localmsg);
}
wxLogMessage(msg);
} else {
fprintf(origStderr, "Final Status: %s(%d)\n", errors[stat], status);
tqslTrace(NULL, "Final Status: %s", errors[stat]);
}
exit(status);
}
/////////// Application //////////////
class QSLApp : public wxApp {
public:
QSLApp();
virtual ~QSLApp();
class MyFrame *GUIinit(bool checkUpdates, bool quiet = false);
virtual bool OnInit();
virtual int OnRun();
wxLanguage GetLang() {return lang; }
// virtual wxLog *CreateLogTarget();
protected:
wxLanguage lang; // Language specified by user
wxLocale* locale; // Locale we're using
};
QSLApp::~QSLApp() {
wxConfigBase *c = wxConfigBase::Set(0);
if (c)
delete c;
tqsl_closeDiagFile();
free_certlist();
}
IMPLEMENT_APP(QSLApp)
static int
getCertPassword(char *buf, int bufsiz, tQSL_Cert cert) {
tqslTrace("getCertPassword", "buf = %lx, bufsiz=%d, cert=%lx", buf, bufsiz, cert);
char call[TQSL_CALLSIGN_MAX+1] = "";
int dxcc = 0;
tqsl_getCertificateCallSign(cert, call, sizeof call);
tqsl_getCertificateDXCCEntity(cert, &dxcc);
DXCC dx;
dx.getByEntity(dxcc);
// TRANSLATORS: this is followed by the callsign and entity name
wxString fmt = _("Enter the passphrase to unlock the callsign certificate for %hs -- %hs\n(This is the passphrase you made up when you installed the callsign certificate.)");
wxString message = wxString::Format(fmt, call, dx.name());
wxWindow* top = wxGetApp().GetTopWindow();
if (frame->IsQuiet()) {
frame->Show(true);
}
top->SetFocus();
top->Raise();
wxString pwd;
int ret = getPasswordFromUser(pwd, message, _("Enter passphrase"), wxT(""), top);
if (ret != wxID_OK)
return 1;
strncpy(buf, pwd.ToUTF8(), bufsiz);
utf8_to_ucs2(buf, unipwd, sizeof unipwd);
return 0;
}
class ConvertingDialog : public wxDialog {
public:
explicit ConvertingDialog(wxWindow *parent, const char *filename = "");
void OnCancel(wxCommandEvent&);
bool running;
wxStaticText *msg;
private:
wxButton *canbut;
DECLARE_EVENT_TABLE()
};
BEGIN_EVENT_TABLE(ConvertingDialog, wxDialog)
EVT_BUTTON(TQSL_CD_CANBUT, ConvertingDialog::OnCancel)
END_EVENT_TABLE()
void
ConvertingDialog::OnCancel(wxCommandEvent&) {
running = false;
canbut->Enable(FALSE);
}
ConvertingDialog::ConvertingDialog(wxWindow *parent, const char *filename)
: wxDialog(parent, -1, wxString(_("Signing QSO Data"))),
running(true) {
wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
wxString label = wxString(_("Converting ")) + wxString::FromUTF8(filename) + wxT(" ") + _("to TQSL format");
sizer->Add(new wxStaticText(this, -1, label), 0, wxALL|wxALIGN_CENTER, 10);
msg = new wxStaticText(this, TQSL_CD_MSG, wxT(" "));
sizer->Add(msg, 0, wxALL|wxALIGN_LEFT, 10);
canbut = new wxButton(this, TQSL_CD_CANBUT, _("Cancel"));
sizer->Add(canbut, 0, wxALL|wxEXPAND, 10);
SetAutoLayout(TRUE);
SetSizer(sizer);
sizer->Fit(this);
sizer->SetSizeHints(this);
CenterOnParent();
}
#define TQSL_PROG_CANBUT TQSL_ID_LOW+30
DECLARE_EVENT_TYPE(wxEVT_LOGUPLOAD_DONE, -1)
DEFINE_EVENT_TYPE(wxEVT_LOGUPLOAD_DONE)
DECLARE_EVENT_TYPE(wxEVT_LOGUPLOAD_PROGRESS, -1)
DEFINE_EVENT_TYPE(wxEVT_LOGUPLOAD_PROGRESS)
class UploadDialog : public wxDialog {
public:
explicit UploadDialog(wxWindow *parent, wxString title = wxString(_("Uploading Signed Data")), wxString label = wxString(_("Uploading signed log data...")));
void OnCancel(wxCommandEvent&);
void OnDone(wxCommandEvent&);
void OnProgress(wxCommandEvent&) { }
int doUpdateProgress(double dltotal, double dlnow, double ultotal, double ulnow);
static int UpdateProgress(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow) {
return (reinterpret_cast<UploadDialog*>(clientp))->doUpdateProgress(dltotal, dlnow, ultotal, ulnow);
}
private:
wxButton *canbut;
wxGauge* progress;
bool cancelled;
DECLARE_EVENT_TABLE()
};
BEGIN_EVENT_TABLE(UploadDialog, wxDialog)
EVT_BUTTON(TQSL_PROG_CANBUT, UploadDialog::OnCancel)
EVT_COMMAND(wxID_ANY, wxEVT_LOGUPLOAD_DONE, UploadDialog::OnDone)
EVT_COMMAND(wxID_ANY, wxEVT_LOGUPLOAD_PROGRESS, UploadDialog::OnProgress)
END_EVENT_TABLE()
void
UploadDialog::OnCancel(wxCommandEvent&) {
cancelled = true;
canbut->Enable(false);
}
UploadDialog::UploadDialog(wxWindow *parent, wxString title, wxString label)
: wxDialog(parent, -1, title), cancelled(false) {
tqslTrace("UploadDialog::UploadDialog", "parent = %lx", parent);
wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
sizer->Add(new wxStaticText(this, -1, label), 0, wxALL|wxALIGN_CENTER, 10);
progress = new wxGauge(this, -1, 100);
progress->SetValue(0);
sizer->Add(progress, 0, wxALL|wxEXPAND);
canbut = new wxButton(this, TQSL_PROG_CANBUT, _("Cancel"));
sizer->Add(canbut, 0, wxALL|wxEXPAND, 10);
SetAutoLayout(TRUE);
SetSizer(sizer);
sizer->Fit(this);
sizer->SetSizeHints(this);
CenterOnParent();
}
void UploadDialog::OnDone(wxCommandEvent&) {
tqslTrace("UploadDialog::OnDone", "Upload complete");
EndModal(1);
}
int UploadDialog::doUpdateProgress(double dltotal, double dlnow, double ultotal, double ulnow) {
// This logging is just noise. No reason to bother.
#ifdef LOG_DL_PROGRESS
static double lastDlnow = 0.0;
if (dlnow != lastDlnow) {
tqslTrace("UploadDialog::doUpdateProgresss", "dltotal=%f, dlnow=%f, ultotal=%f, ulnow=%f", dltotal, dlnow, ultotal, ulnow);
lastDlnow = dlnow;
}
#endif
// Post an event to the GUI thread
wxCommandEvent evt(wxEVT_LOGUPLOAD_PROGRESS, wxID_ANY);
GetEventHandler()->AddPendingEvent(evt);
if (cancelled) return 1;
// Avoid ultotal at zero.
if (ultotal > 0.0000001) progress->SetValue(static_cast<int>((100*(ulnow/ultotal))));
return 0;
}
#define TQSL_DR_START TQSL_ID_LOW+10
#define TQSL_DR_END TQSL_ID_LOW+11
#define TQSL_DR_OK TQSL_ID_LOW+12
#define TQSL_DR_CAN TQSL_ID_LOW+13
#define TQSL_DR_MSG TQSL_ID_LOW+14
class DateRangeDialog : public wxDialog {
public:
explicit DateRangeDialog(wxWindow *parent = 0);
tQSL_Date start, end;
private:
void OnOk(wxCommandEvent&);
void OnCancel(wxCommandEvent&);
virtual bool TransferDataFromWindow();
wxTextCtrl *start_tc, *end_tc;
wxStaticText *msg;
DECLARE_EVENT_TABLE()
};
BEGIN_EVENT_TABLE(DateRangeDialog, wxDialog)
EVT_BUTTON(TQSL_DR_OK, DateRangeDialog::OnOk)
EVT_BUTTON(TQSL_DR_CAN, DateRangeDialog::OnCancel)
END_EVENT_TABLE()
DateRangeDialog::DateRangeDialog(wxWindow *parent) : wxDialog(parent, -1, wxString(_("QSO Date Range"))) {
tqslTrace("DateRangeDialog::DateRangeDialog", NULL);
wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
wxString msgstr = _("You may set the starting and/or ending QSO dates in order to select QSOs from the input file.");
msgstr += wxT("\n\n");
msgstr += _("QSOs prior to the starting date or after the ending date will not be signed or included in the output file.");
msgstr += wxT("\n\n");
msgstr += _("You may leave either date (or both dates) blank.");
wxSize sz = getTextSize(this);
int em_w = sz.GetWidth();
wxStaticText *st = new wxStaticText(this, -1, msgstr);
st->Wrap(em_w * 30);
sizer->Add(st, 0, wxALL|wxALIGN_CENTER, 10);
wxBoxSizer *hsizer = new wxBoxSizer(wxHORIZONTAL);
hsizer->Add(new wxStaticText(this, -1, _("Start Date (YYYY-MM-DD)")), 0, wxRIGHT|wxALIGN_CENTER_VERTICAL, 5);
start_tc = new wxTextCtrl(this, TQSL_DR_START);
start_tc->SetMaxLength(10);
start_tc->SetMinSize(wxSize(em_w * 10, -1));
hsizer->Add(start_tc, 0, 0, 0);
sizer->Add(hsizer, 0, wxALL|wxALIGN_CENTER, 10);
hsizer = new wxBoxSizer(wxHORIZONTAL);
hsizer->Add(new wxStaticText(this, -1, _("End Date (YYYY-MM-DD)")), 0, wxRIGHT|wxALIGN_CENTER_VERTICAL, 5);
end_tc = new wxTextCtrl(this, TQSL_DR_END);
end_tc->SetMaxLength(10);
end_tc->SetMinSize(wxSize(em_w * 10, -1));
hsizer->Add(end_tc, 0, 0, 0);
sizer->Add(hsizer, 0, wxALL|wxALIGN_CENTER, 10);
msg = new wxStaticText(this, TQSL_DR_MSG, wxT(""));
sizer->Add(msg, 0, wxALL, 0);
hsizer = new wxBoxSizer(wxHORIZONTAL);
hsizer->Add(new wxButton(this, TQSL_DR_OK, _("OK")), 0, wxRIGHT, 5);
hsizer->Add(new wxButton(this, TQSL_DR_CAN, _("Cancel")), 0, wxLEFT, 10);
sizer->Add(hsizer, 0, wxALIGN_CENTER|wxALL, 10);
SetAutoLayout(TRUE);
SetSizer(sizer);
sizer->Fit(this);
sizer->SetSizeHints(this);
CenterOnParent();
}
bool
DateRangeDialog::TransferDataFromWindow() {
tqslTrace("DateRangeDialog::TransferDataFromWindow", NULL);
wxString text = start_tc->GetValue();
tqslTrace("DateRangeDialog::TransferDataFromWindow", "start=%s", S(text));
if (text.Trim().IsEmpty()) {
start.year = start.month = start.day = 0;
} else if (tqsl_initDate(&start, text.ToUTF8()) || !tqsl_isDateValid(&start)) {
msg->SetLabel(_("Start date is invalid"));
return false;
}
text = end_tc->GetValue();
tqslTrace("DateRangeDialog::TransferDataFromWindow", "end=%s", S(text));
if (text.Trim().IsEmpty()) {
end.year = end.month = end.day = 0;
} else if (tqsl_initDate(&end, text.ToUTF8()) || !tqsl_isDateValid(&end)) {
msg->SetLabel(_("End date is invalid"));
return false;
}
return true;
}
void
DateRangeDialog::OnOk(wxCommandEvent&) {
tqslTrace("DateRangeDialog::OnOk", NULL);
if (TransferDataFromWindow())
EndModal(wxOK);
}
void
DateRangeDialog::OnCancel(wxCommandEvent&) {
tqslTrace("DateRangeDialog::OnCancel", NULL);
EndModal(wxCANCEL);
}
#define TQSL_DP_OK TQSL_ID_LOW+20
#define TQSL_DP_CAN TQSL_ID_LOW+21
#define TQSL_DP_ALLOW TQSL_ID_LOW+22
class DupesDialog : public wxDialog {
public:
explicit DupesDialog(wxWindow *parent = 0, int qso_count = 0, int dupes = 0, int action = TQSL_ACTION_ASK);
private:
void OnOk(wxCommandEvent&);
void OnCancel(wxCommandEvent&);
void OnAllow(wxCommandEvent&);
DECLARE_EVENT_TABLE()
};
BEGIN_EVENT_TABLE(DupesDialog, wxDialog)
EVT_BUTTON(TQSL_DP_OK, DupesDialog::OnOk)
EVT_BUTTON(TQSL_DP_CAN, DupesDialog::OnCancel)
EVT_BUTTON(TQSL_DP_ALLOW, DupesDialog::OnAllow)
END_EVENT_TABLE()
DupesDialog::DupesDialog(wxWindow *parent, int qso_count, int dupes, int action)
: wxDialog(parent, -1, wxString(_("Previously Signed QSOs Detected")), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE) {
tqslTrace("DupesDialog::DupesDialog", "qso_count = %d, dupes =%d, action= =%d", qso_count, dupes, action);
wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
wxString message;
if (qso_count == dupes) {
wxString fmt = _("This log contains %d QSO(s) which appear to have been previously signed for upload to LoTW, and no new QSOs.");
fmt += wxT("\n\n");
fmt += _("Click 'Cancel' to abandon processing this log file (Recommended).");
fmt += wxT("\n");
fmt += _("Click 'Re-Upload QSOs' to re-process this log while allowing previously signed QSOs.");
message = wxString::Format(fmt, qso_count);
} else {
int newq = qso_count - dupes;
wxString fmt = _("This log contains %d QSO(s) which appear to have been previously signed for upload to LoTW, and %d QSOs which are new.");
fmt += wxT("\n\n");
fmt += _("Click 'New QSOs Only' to sign normally, without the previously signed QSOs (Recommended).");
fmt += wxT("\n");
fmt += _("Click 'Cancel' to abandon processing this log file.");
fmt += wxT("\n");
fmt += _("Click 'Re-Upload QSOs' to re-process this log while allowing previously signed QSOs.");
wxString fmt1 = _("This log contains %d QSO(s) which appear to have been previously signed for upload to LoTW, and one QSO which is new.");
fmt1 += wxT("\n\n");
fmt1 += _("Click 'New QSOs Only' to sign normally, without the previously signed QSOs (Recommended).");
fmt1 += wxT("\n");
fmt1 += _("Click 'Cancel' to abandon processing this log file.");
fmt1 += wxT("\n");
fmt1 += _("Click 'Re-Upload QSOs' to re-process this log while allowing previously signed QSOs.");
if (newq == 1) {
message = wxString::Format(fmt1, dupes);
} else {
message = wxString::Format(fmt, dupes, newq);
}
}
if (action == TQSL_ACTION_UNSPEC) {
if (qso_count == dupes) {
message+= wxT("\n\n");
message += _("The log file you are uploading using your QSO Logging system consists entirely of previously uploaded QSOs that create unnecessary work for LoTW. There may be a more recent version of your QSO Logging system that would prevent this. Please check with your QSO Logging system's vendor for an updated version.");
message += wxT("\n");
message += _("In the meantime, please note that some loggers may exhibit strange behavior if an option other than 'Re-Upload QSOs' is clicked. Choosing 'Cancel' is usually safe, but a defective logger not checking the status messages reported by TrustedQSL may produce strange (but harmless) behavior such as attempting to upload an empty file or marking all chosen QSOs as 'sent'");
} else {
message+= wxT("\n\n");
message += _("The log file you are uploading using your QSO Logging system includes some previously uploaded QSOs that create unnecessary work for LoTW. There may be a more recent version of your QSO Logging system that would prevent this. Please check with your QSO Logging system's vendor for an updated version.");
message += wxT("\n");
message += _("In the meantime, please note that some loggers may exhibit strange behavior if an option other than 'Re-Upload QSOs' is clicked. 'New QSOs Only' is recommended, but a logger that does its own upload tracking may incorrectly set the status in this case. A logger that doesn't track uploads should be unaffected by choosing 'New QSOs Only' and if it tracks 'QSO sent' status, will correctly mark all selected QSOs as sent - they are in your account even though they would not be in this specific batch");
message += wxT("\n");
message += _("Choosing 'Cancel' is usually safe, but a defective logger not checking the status messages reported by TrustedQSL may produce strange (but harmless) behavior such as attempting to upload an empty file or marking all chosen QSOs as 'sent'");
}
}
wxStaticText* mtext = new wxStaticText(this, -1, message);
sizer->Add(mtext, 0, wxALL|wxALIGN_CENTER, 10);
wxSize sz = getTextSize(this);
int em_w = sz.GetWidth();
mtext->Wrap(em_w * 50);
wxBoxSizer *hsizer = new wxBoxSizer(wxHORIZONTAL);
if (qso_count != dupes)
hsizer->Add(new wxButton(this, TQSL_DP_OK, _("New QSOs Only")), 0, wxRIGHT, 5);
hsizer->Add(new wxButton(this, TQSL_DP_CAN, _("Cancel")), 0, wxLEFT, 10);
hsizer->Add(new wxButton(this, TQSL_DP_ALLOW, _("Re-Upload QSOs")), 0, wxLEFT, 20);
sizer->Add(hsizer, 0, wxALIGN_CENTER|wxALL, 10);
SetAutoLayout(TRUE);
SetSizer(sizer);
sizer->Fit(this);
sizer->SetSizeHints(this);
CenterOnParent();
}
void
DupesDialog::OnOk(wxCommandEvent&) {
tqslTrace("DupesDialog::OnOk", NULL);
EndModal(TQSL_DP_OK);
}
void
DupesDialog::OnCancel(wxCommandEvent&) {
tqslTrace("DupesDialog::OnCancel", NULL);
EndModal(TQSL_DP_CAN);
}
void
DupesDialog::OnAllow(wxCommandEvent&) {
tqslTrace("DupesDialog::OnAllow", NULL);
wxString msg = _("The only reason to re-sign previously signed QSOs is if a previous upload was not processed by LoTW, either because it was never uploaded, or there was a server failure");
msg += wxT("\n\n");
msg += _("Are you sure you want to proceed? Click 'No' to review the choices");
if (wxMessageBox(msg, _("Are you sure?"), wxYES_NO|wxICON_EXCLAMATION, this) == wxYES) {
EndModal(TQSL_DP_ALLOW);
}
}
#define TQSL_AE_OK TQSL_ID_LOW+40
#define TQSL_AE_CAN TQSL_ID_LOW+41
class ErrorsDialog : public wxDialog {
public:
explicit ErrorsDialog(wxWindow *parent = 0, wxString msg = wxT(""));
private:
void OnOk(wxCommandEvent&);
void OnCancel(wxCommandEvent&);
DECLARE_EVENT_TABLE()
};
BEGIN_EVENT_TABLE(ErrorsDialog, wxDialog)
EVT_BUTTON(TQSL_AE_OK, ErrorsDialog::OnOk)
EVT_BUTTON(TQSL_AE_CAN, ErrorsDialog::OnCancel)
END_EVENT_TABLE()
ErrorsDialog::ErrorsDialog(wxWindow *parent, wxString msg)
: wxDialog(parent, -1, wxString(_("Errors Detected")), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE) {
tqslTrace("ErrorsDialog::ErrorsDialog", "msg=%s", S(msg));
wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
sizer->Add(new wxStaticText(this, -1, msg), 0, wxALL|wxALIGN_CENTER, 10);
wxBoxSizer *hsizer = new wxBoxSizer(wxHORIZONTAL);
hsizer->Add(new wxButton(this, TQSL_AE_OK, _("Ignore")), 0, wxRIGHT, 5);
hsizer->Add(new wxButton(this, TQSL_AE_CAN, _("Cancel")), 0, wxLEFT, 10);
sizer->Add(hsizer, 0, wxALIGN_CENTER|wxALL, 10);
SetAutoLayout(TRUE);
SetSizer(sizer);
sizer->Fit(this);
sizer->SetSizeHints(this);
CenterOnParent();
}
void
ErrorsDialog::OnOk(wxCommandEvent&) {
tqslTrace("ErrorsDialog::OnOk", NULL);
EndModal(TQSL_AE_OK);
}
void
ErrorsDialog::OnCancel(wxCommandEvent&) {
tqslTrace("ErrorsDialog::OnCancel", NULL);
EndModal(TQSL_AE_CAN);
}
static void
init_modes() {
tqslTrace("init_modes", NULL);
tqsl_clearADIFModes();
wxConfig *config = reinterpret_cast<wxConfig *>(wxConfig::Get());
long cookie;
wxString key, value;
vector<wxString> badModes;
config->SetPath(wxT("/modeMap"));
bool stat = config->GetFirstEntry(key, cookie);
while (stat) {
value = config->Read(key, wxT(""));
key.Replace(wxT("!SLASH!"), wxT("/"), true); // Fix slashes in modes
bool newMode = true;
int numModes;
if (tqsl_getNumMode(&numModes) == 0) {
for (int i = 0; i < numModes; i++) {
const char *modestr;
if (tqsl_getMode(i, &modestr, NULL) == 0) {
if (strcasecmp(key.ToUTF8(), modestr) == 0) {
wxLogWarning(_("Your custom mode map %s conflicts with the standard mode definition for %hs and was deleted."), key.c_str(), modestr);
newMode = false;
badModes.push_back(key);
break;
}
}
}
}
if (newMode)
tqsl_setADIFMode(key.ToUTF8(), value.ToUTF8());
stat = config->GetNextEntry(key, cookie);
}
// Delete the conflicting entries
for (int i = 0; i < static_cast<int>(badModes.size()); i++) {
config->DeleteEntry(badModes[i]);
}
config->SetPath(wxT("/"));
}
static void
init_contests() {
tqslTrace("init_contests", NULL);
tqsl_clearCabrilloMap();
wxConfig *config = reinterpret_cast<wxConfig *>(wxConfig::Get());
long cookie;
wxString key, value;
config->SetPath(wxT("/cabrilloMap"));
bool stat = config->GetFirstEntry(key, cookie);
while (stat) {
value = config->Read(key, wxT(""));
int contest_type = strtol(value.ToUTF8(), NULL, 10);
int callsign_field = strtol(value.AfterFirst(';').ToUTF8(), NULL, 10);
tqsl_setCabrilloMapEntry(key.ToUTF8(), callsign_field, contest_type);
stat = config->GetNextEntry(key, cookie);
}
config->SetPath(wxT("/"));
}
static void
check_tqsl_error(int rval) {
if (rval == 0)
return;
tqslTrace("check_tqsl_error", "rval=%d", rval);
wxString msg = getLocalizedErrorString();
tqslTrace("check_tqsl_error", "msg=%s", S(msg));
throw TQSLException(S(msg));
}
static tQSL_Cert *certlist = 0;
static int ncerts;
static void
free_certlist() {
tqslTrace("free_certlist", NULL);
if (certlist) {
tqsl_freeCertificateList(certlist, ncerts);
}
certlist = NULL;
ncerts = 0;
}
static void
get_certlist(string callsign, int dxcc, bool expired, bool superceded, bool withkeys) {
tqslTrace("get_certlist", "callsign=%s, dxcc=%d, expired=%d, superceded=%d withkeys=%d", callsign.c_str(), dxcc, expired, superceded, withkeys);
free_certlist();
int select = 0;
if (expired) select |= TQSL_SELECT_CERT_EXPIRED;
if (superceded) select |= TQSL_SELECT_CERT_SUPERCEDED;
if (withkeys) select |= TQSL_SELECT_CERT_WITHKEYS;
tqsl_selectCertificates(&certlist, &ncerts,
(callsign == "") ? 0 : callsign.c_str(), dxcc, 0, 0, select);
}
#if wxMAJOR_VERSION > 2
class SimpleLogFormatter : public wxLogFormatter {
virtual wxString Format(wxLogLevel level, const wxString& msg, const wxLogRecordInfo& info) const {
return msg;
}
};
#endif
class LogList : public wxLog {
public:
explicit LogList(MyFrame *frame) : wxLog(), _frame(frame) {}
#if wxMAJOR_VERSION > 2
virtual void DoLogText(const wxString& msg);
#else
virtual void DoLogString(const wxChar *szString, time_t t);
#endif
private:
MyFrame *_frame;
};
#if wxMAJOR_VERSION > 2
void LogList::DoLogText(const wxString& msg) {
const wxChar* szString = msg.wc_str();
#else
void LogList::DoLogString(const wxChar *szString, time_t) {
static wxString msg(szString);
#endif
static const char *smsg = msg.ToUTF8();
tqslTrace(NULL, "%s", smsg);
wxTextCtrl *_logwin = NULL;
if (msg.StartsWith(wxT("Debug:")))
return;
if (msg.StartsWith(wxT("Error: Unable to open requested HTML document:")))
return;
if (_frame != NULL)
_logwin = _frame->logwin;
if (_logwin == NULL) {
#ifdef _WIN32
fwprintf(stderr, L"%ls\n", szString);
#else
fprintf(origStderr, "%ls\n", szString);
#endif
return;
}
_logwin->AppendText(msg);
_logwin->AppendText(wxT("\n"));
// Select the log tab when there's some new message
if (_frame->notebook->GetPageCount() > TQSL_LOG_TAB)
_frame->notebook->SetSelection(TQSL_LOG_TAB);
}
class LogStderr : public wxLog {
public:
LogStderr(void) : wxLog() {}
#if wxMAJOR_VERSION > 2
virtual void DoLogText(const wxString& msg);
#else
virtual void DoLogString(const wxChar *szString, time_t t);
#endif
};
#if wxMAJOR_VERSION > 2
void LogStderr::DoLogText(const wxString& msg) {
const wxChar* szString = msg.wc_str();
#else
void LogStderr::DoLogString(const wxChar *szString, time_t) {
static wxString msg(szString);
#endif
static const char *smsg = msg.ToUTF8();
tqslTrace(NULL, "%s", smsg);
if (msg.StartsWith(wxT("Debug:")))
return;
#ifdef _WIN32
fwprintf(stderr, L"%ls\n", szString);
#else
fprintf(origStderr, "%ls\n", szString);
#endif
return;
}
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(tm_s_add, MyFrame::AddStationLocation)
EVT_BUTTON(tl_AddLoc, MyFrame::AddStationLocation)
EVT_MENU(tm_s_edit, MyFrame::EditStationLocation)
EVT_BUTTON(tl_EditLoc, MyFrame::EditStationLocation)
EVT_MENU(tm_f_new, MyFrame::EnterQSOData)
EVT_BUTTON(tl_Edit, MyFrame::EnterQSOData)
EVT_MENU(tm_f_edit, MyFrame::EditQSOData)
EVT_MENU(tm_f_import_compress, MyFrame::ImportQSODataFile)
EVT_MENU(tm_f_test_signing, MyFrame::TestSignQSODataFile)
EVT_BUTTON(tl_Save, MyFrame::ImportQSODataFile)
EVT_MENU(tm_f_upload, MyFrame::UploadQSODataFile)
EVT_BUTTON(tl_Upload, MyFrame::UploadQSODataFile)
EVT_MENU(tm_f_exit, MyFrame::DoExit)
EVT_MENU(tm_f_preferences, MyFrame::OnPreferences)
EVT_MENU(tm_f_loadconfig, MyFrame::OnLoadConfig)
EVT_MENU(tm_f_saveconfig, MyFrame::OnSaveConfig)
EVT_MENU(tm_h_contents, MyFrame::OnHelpContents)
EVT_MENU(tm_h_about, MyFrame::OnHelpAbout)
EVT_MENU(tm_f_diag, MyFrame::OnHelpDiagnose)
EVT_MENU(tm_f_lang, MyFrame::OnChooseLanguage)
EVT_MENU(tm_h_update, MyFrame::CheckForUpdates)
EVT_MENU(tm_h_sync, MyFrame::DoLoTWSync)
EVT_CLOSE(MyFrame::OnExit)
EVT_MENU(tc_CRQWizard, MyFrame::CRQWizard)
EVT_MENU(tc_c_New, MyFrame::CRQWizard)
EVT_MENU(tc_c_Load, MyFrame::OnLoadCertificateFile)
EVT_BUTTON(tc_Load, MyFrame::OnLoadCertificateFile)
EVT_MENU(tc_c_Properties, MyFrame::OnCertProperties)
EVT_BUTTON(tc_CertProp, MyFrame::OnCertProperties)
EVT_MENU(tc_c_Export, MyFrame::OnCertExport)
EVT_BUTTON(tc_CertSave, MyFrame::OnCertExport)
EVT_MENU(tc_c_Delete, MyFrame::OnCertDelete)
EVT_MENU(tc_c_Undelete, MyFrame::OnCertUndelete)
// EVT_MENU(tc_c_Import, MyFrame::OnCertImport)
// EVT_MENU(tc_c_Sign, MyFrame::OnSign)
EVT_MENU(tc_c_Renew, MyFrame::CRQWizardRenew)
EVT_BUTTON(tc_CertRenew, MyFrame::CRQWizardRenew)
EVT_MENU(tc_h_Contents, MyFrame::OnHelpContents)
EVT_MENU(tc_h_About, MyFrame::OnHelpAbout)
EVT_MENU(tl_c_Properties, MyFrame::OnLocProperties)
EVT_MENU(tm_s_Properties, MyFrame::OnLocProperties)
EVT_MENU(tm_s_undelete, MyFrame::OnLocUndelete)
EVT_BUTTON(tl_PropLoc, MyFrame::OnLocProperties)
EVT_MENU(tl_c_Delete, MyFrame::OnLocDelete)
EVT_BUTTON(tl_DeleteLoc, MyFrame::OnLocDelete)
EVT_MENU(tl_c_Edit, MyFrame::OnLocEdit)
EVT_BUTTON(tl_Login, MyFrame::OnLoginToLogbook)
EVT_TREE_SEL_CHANGED(tc_CertTree, MyFrame::OnCertTreeSel)
EVT_TREE_SEL_CHANGED(tc_LocTree, MyFrame::OnLocTreeSel)
EVT_MENU(bg_updateCheck, MyFrame::OnUpdateCheckDone)
EVT_MENU(bg_expiring, MyFrame::OnExpiredCertFound)
END_EVENT_TABLE()
void
MyFrame::AutoBackup(void) {
wxConfig *config = reinterpret_cast<wxConfig *>(wxConfig::Get());
wxString bdir = config->Read(wxT("BackupFolder"), wxString::FromUTF8(tQSL_BaseDir));
if (bdir.Trim(true).Trim(false).IsEmpty())
bdir = wxString::FromUTF8(tQSL_BaseDir);
SaveOldBackups(bdir, wxT("tqslconfig"), wxT("tbk"));
#ifdef _WIN32
bdir += wxT("\\tqslconfig.tbk");
#else
bdir += wxT("/tqslconfig.tbk");
#endif
tqslTrace("MyFrame::AutoBackup", "Auto Backup %s", S(bdir));
BackupConfig(bdir, true);
}
void
MyFrame::SaveOldBackups(const wxString& directory, const wxString& filename, const wxString& ext) {
#ifdef _WIN32
wxString bfile = directory + wxT("\\") + filename + wxT(".") + ext;
struct _stat32 s;
wchar_t* lfn = utf8_to_wchar(bfile.ToUTF8());
int ret = _wstat32(lfn, &s);
free_wchar(lfn);
if (ret != 0) { // Does it exist?
#else
wxString bfile = directory + wxT("/") + filename + wxT(".") + ext;
struct stat s;
if (lstat(bfile.ToUTF8(), &s) != 0) { // Does it exist?
#endif
return; // No. No need to back it up.
}
// There's a file with that name already.
// Rename it for backup purposes
struct tm tmr;
struct tm *t = &tmr;
#ifdef _WIN32
t = _gmtime32(&s.st_mtime);
wxString newName = directory + wxT("\\") + filename +
#else
t = gmtime_r(&s.st_mtime, t);
wxString newName = directory + wxT("/") + filename +
#endif
wxString::Format(wxT("-%4.4d-%2.2d-%2.2d-%2.2d-%2.2d."),
t->tm_year+1900, t->tm_mon+1, t->tm_mday,
t->tm_hour, t->tm_min) + ext;
#ifdef _WIN32
lfn = utf8_to_wchar(bfile.ToUTF8());
wchar_t* newlfn = utf8_to_wchar(newName.ToUTF8());
ret = _wrename(lfn, newlfn);
free_wchar(lfn);
free_wchar(newlfn);
#else
int ret = rename(bfile.ToUTF8(), newName.ToUTF8());
#endif
if (ret && errno != EEXIST) { // EEXIST means that there's been another backup this minute.
tQSL_Error = TQSL_SYSTEM_ERROR;
tQSL_Errno = errno;
tqslTrace("MyFrame::SaveOldBackups", "Error renaming: %s", strerror(errno));
wxLogError(_("Error renaming backup file %s: %hs"), bfile.c_str(), strerror(errno));
return;
}
// Find any backups and delete older ones
wxArrayString bfiles;
#ifdef _WIN32
wchar_t* wpath = utf8_to_wchar(directory.ToUTF8());
_WDIR *dir = _wopendir(wpath);
free_wchar(wpath);
#else
DIR *dir = opendir(directory.ToUTF8());
#endif
if (dir != NULL) {
#ifdef _WIN32
struct _wdirent *ent = NULL;
while ((ent = _wreaddir(dir)) != NULL) {
if (wcsstr(ent->d_name, L"tqslconfig-") == ent->d_name && wcsstr(ent->d_name, L".tbk")) {
bfiles.Add(wxString(ent->d_name));
}
}
#else
struct dirent *ent = NULL;
while ((ent = readdir(dir)) != NULL) {
if (strstr(ent->d_name, "tqslconfig-") == ent->d_name && strstr(ent->d_name, ".tbk")) {
bfiles.Add(wxString::FromUTF8(ent->d_name));
}
}
#endif
}
bfiles.Sort();
long vlimit = DEFAULT_BACKUP_VERSIONS;
wxConfig::Get()->Read(wxT("BackupVersions"), &vlimit, DEFAULT_BACKUP_VERSIONS);
if (vlimit <= 0)
vlimit = DEFAULT_BACKUP_VERSIONS;
int toRemove = bfiles.GetCount() - vlimit;
if (toRemove <= 0)
return; // Nothing to remove
// Remove, starting from the oldest
for (int i = 0; i < toRemove; i++) {
#ifdef _WIN32
wxString removeIt = directory + wxT("\\") + bfiles[i];
wchar_t* wfname = utf8_to_wchar(removeIt.ToUTF8());
_wunlink(wfname);
free_wchar(wfname);
#else
wxString removeIt = directory + wxT("/") + bfiles[i];
unlink(removeIt.ToUTF8());
#endif
}
}
void
MyFrame::SaveWindowLayout() {
int x, y, w, h;
// Don't save window size/position if minimized or too small
wxConfig *config = reinterpret_cast<wxConfig *>(wxConfig::Get());
if (!IsIconized()) {
GetPosition(&x, &y);
#ifdef __WXGTK__
GetClientSize(&w, &h);
h += 27; // Fudge for menubar. Stops window shrink.
#else
GetSize(&w, &h);
#endif
if (w >= MAIN_WINDOW_MIN_WIDTH && h >= MAIN_WINDOW_MIN_HEIGHT) {
config->Write(wxT("MainWindowX"), x);
config->Write(wxT("MainWindowY"), y);
config->Write(wxT("MainWindowWidth"), w);
config->Write(wxT("MainWindowHeight"), h);
config->Write(wxT("MainWindowMaximized"), IsMaximized());
config->Flush(false);
}
}
}
void
MyFrame::OnExit(TQ_WXCLOSEEVENT& WXUNUSED(event)) {
tqslTrace("MyFrame::OnExit", "exiting");
if (logConv) {
tqsl_converterRollBack(logConv);
tqsl_endConverter(&logConv);
}
SaveWindowLayout();
unlock_db();
wxConfig *config = reinterpret_cast<wxConfig *>(wxConfig::Get());
bool ab;
config->Read(wxT("AutoBackup"), &ab, DEFAULT_AUTO_BACKUP);
if (ab) {
AutoBackup();
}
tqslTrace("MyFrame::OnExit", "GUI Destroy");
Destroy(); // close the window
tqslTrace("MyFrame::OnExit", "Done");
}
void
MyFrame::DoExit(wxCommandEvent& WXUNUSED(event)) {
tqslTrace("MyFrame::DoExit", "About to close");
Close();
tqslTrace("MyFrame::DoExit", "About to destroy GUI");
Destroy();
tqslTrace("MyFrame::DoExit", "Done");
}
static wxSemaphore *updateLocker;
class UpdateThread : public wxThread {
public:
UpdateThread(MyFrame *frame, bool silent, bool noGUI) : wxThread(wxTHREAD_DETACHED) {
_frame = frame;
_silent = silent;
_noGUI = noGUI;
}
virtual void *Entry() {
_frame->DoCheckForUpdates(_silent, _noGUI);
updateLocker->Post();
return NULL;
}
private:
MyFrame *_frame;
bool _silent;
bool _noGUI;
};
void
MyFrame::DoUpdateCheck(bool silent, bool noGUI) {
tqslTrace("MyFrame::DoUpdateCheck", "silent=%d noGUI=%d", silent, noGUI);
//check for updates
if (!noGUI) {
wxBeginBusyCursor();
wxTheApp->Yield(true);
wxLogMessage(_("Checking for TQSL updates..."));
wxTheApp->Yield(true);
}
updateLocker = new wxSemaphore(0, 1); // One waiter
UpdateThread* thread = new UpdateThread(this, silent, noGUI);
thread->Create();
wxTheApp->Yield(true);
thread->Run();
wxTheApp->Yield(true);
while (updateLocker->TryWait() == wxSEMA_BUSY) {
wxTheApp->Yield(true);
#if (wxMAJOR_VERSION == 2 && wxMINOR_VERSION == 9)
wxGetApp().DoIdle();
#endif
wxMilliSleep(300);
}
delete updateLocker;
if (!noGUI) {
wxString val = logwin->GetValue();
wxString chk(_("Checking for TQSL updates..."));
val.Replace(chk + wxT("\n"), wxT(""));
val.Replace(chk, wxT(""));
logwin->SetValue(val); // Clear the checking message
notebook->SetSelection(0);
// Refresh the cert tree in case any new info on expires/supercedes
cert_tree->Build(CERTLIST_FLAGS);
CertTreeReset();
wxEndBusyCursor();
}
}
MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h, bool checkUpdates, bool quiet, wxLocale* loca)
: wxFrame(0, -1, title, wxPoint(x, y), wxSize(w, h)), locale(loca) {
_quiet = quiet;
#ifdef __WXMAC__
DocPaths docpaths(wxT("tqsl.app"));
#else
DocPaths docpaths(wxT("tqslapp"));
#endif
wxBitmap savebm(save_xpm);
wxBitmap uploadbm(upload_xpm);
wxBitmap upload_disbm(upload_dis_xpm);
wxBitmap file_editbm(file_edit_xpm);
wxBitmap file_edit_disbm(file_edit_dis_xpm);
wxBitmap locaddbm(loc_add_xpm);
wxBitmap locadd_disbm(loc_add_dis_xpm);
wxBitmap editbm(edit_xpm);
wxBitmap edit_disbm(edit_dis_xpm);
wxBitmap deletebm(delete_xpm);
wxBitmap delete_disbm(delete_dis_xpm);
wxBitmap downloadbm(download_xpm);
wxBitmap download_disbm(download_dis_xpm);
wxBitmap propertiesbm(properties_xpm);
wxBitmap properties_disbm(properties_dis_xpm);
wxBitmap importbm(import_xpm);
wxBitmap lotwbm(lotw_xpm);
loc_edit_button = NULL;
cert_save_label = NULL;
req = NULL;
curlReq = NULL;
curlLogFile = NULL;
logConv = NULL;
// File menu
file_menu = new wxMenu;
file_menu->Append(tm_f_upload, _("Sign and &upload ADIF or Cabrillo File..."));
file_menu->Append(tm_f_import_compress, _("&Sign and save ADIF or Cabrillo file..."));
file_menu->Append(tm_f_test_signing, _("&Test Signing an ADIF or Cabrillo file..."));
file_menu->AppendSeparator();
file_menu->Append(tm_f_saveconfig, _("&Backup Station Locations, Certificates, and Preferences..."));
file_menu->Append(tm_f_loadconfig, _("&Restore Station Locations, Certificates, and Preferences..."));
file_menu->AppendSeparator();
file_menu->Append(tm_f_new, _("Create &New ADIF file..."));
file_menu->Append(tm_f_edit, _("&Edit existing ADIF file..."));
file_menu->AppendSeparator();
#ifdef __WXMAC__ // On Mac, Preferences not on File menu
file_menu->Append(tm_f_preferences, _("&Preferences..."));
#else
file_menu->Append(tm_f_preferences, _("Display or Modify &Preferences..."));
#endif
file_menu->AppendSeparator();
file_menu->Append(tm_f_lang, _("Language"));
file_menu->AppendSeparator();
file_menu->AppendCheckItem(tm_f_diag, _("Dia&gnostic Mode"));
file_menu->Check(tm_f_diag, false);
#ifndef __WXMAC__ // On Mac, Exit not on File menu
file_menu->AppendSeparator();
#endif
file_menu->Append(tm_f_exit, _("E&xit TQSL\tAlt-X"));
cert_menu = makeCertificateMenu(false);
// Station menu
stn_menu = new wxMenu;
stn_menu->Append(tm_s_Properties, _("&Display Station Location Properties"));
stn_menu->Enable(tm_s_Properties, false);
stn_menu->Append(tm_s_edit, _("&Edit Station Location"));
stn_menu->Append(tm_s_add, _("&Add Station Location"));
stn_menu->AppendSeparator();
int nloc = 0;
char **locp = NULL;
tqsl_getDeletedStationLocations(&locp, &nloc);
stn_menu->Append(tm_s_undelete, _("&Restore a Deleted Station Location"));
stn_menu->Enable(tm_s_undelete, nloc > 0);
if (nloc > 0)
tqsl_freeDeletedLocationList(locp, nloc);
// Help menu
help = new wxHtmlHelpController(wxHF_DEFAULT_STYLE | wxHF_OPEN_FILES);
help_menu = new wxMenu;
help->UseConfig(wxConfig::Get());
wxString hhp = docpaths.FindAbsoluteValidPath(wxT("tqslapp.hhp"));
if (!wxFileNameFromPath(hhp).IsEmpty()) {
if (help->AddBook(hhp))
#ifdef __WXMAC__
help_menu->Append(tm_h_contents, _("&Contents"));
#else
help_menu->Append(tm_h_contents, _("Display &Documentation"));
help_menu->AppendSeparator();
#endif
}
help_menu->Append(tm_h_update, _("Check for &Updates..."));
help_menu->AppendSeparator();
help_menu->Append(tm_h_sync, _("Synchronize upload data with LoTW..."));
help_menu->AppendSeparator();
help_menu->Append(tm_h_about, _("&About"));
// Main menu
wxMenuBar *menu_bar = new wxMenuBar;
menu_bar->Append(file_menu, _("&File"));
menu_bar->Append(stn_menu, _("&Station Location"));
menu_bar->Append(cert_menu, _("Callsign &Certificate"));
menu_bar->Append(help_menu, _("&Help"));
SetMenuBar(menu_bar);
wxPanel* topPanel = new wxPanel(this);
wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL);
topPanel->SetSizer(topSizer);
bool logTab = DEFAULT_LOG_TAB;
wxConfig::Get()->Read(wxT("LogTab"), &logTab, DEFAULT_LOG_TAB);
// Log operations
topSizer->AddSpacer(2);
notebook = new wxNotebook(topPanel, -1, wxDefaultPosition, wxSize(400, 300), wxNB_TOP /* | wxNB_FIXEDWIDTH*/, _("Log Operations"));
wxColor defBkg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
notebook->SetBackgroundColour(defBkg);
topSizer->Add(notebook, 1, wxEXPAND | wxALL, 1);
if (!logTab) {
topSizer->Add(new wxStaticText(topPanel, -1, _("Status Log")), 0, wxEXPAND | wxALL, 1);
logwin = new wxTextCtrl(topPanel, -1, wxT(""), wxDefaultPosition, wxSize(400, 300),
wxTE_MULTILINE|wxTE_READONLY);
ACCESSIBLE(logwin, WindowAccessible);
logwin->SetName(wxT("Status Log"));
topSizer->Add(logwin, 1, wxEXPAND | wxALL, 1);
}
wxPanel* buttons = new wxPanel(notebook, -1);
buttons->SetBackgroundColour(defBkg);
wxBoxSizer* bsizer = new wxBoxSizer(wxVERTICAL);
buttons->SetSizer(bsizer);
wxPanel* b1Panel = new wxPanel(buttons);
b1Panel->SetBackgroundColour(defBkg);
wxBoxSizer* b1sizer = new wxBoxSizer(wxHORIZONTAL);
b1Panel->SetSizer(b1sizer);
wxBitmapButton *up = new wxBitmapButton(b1Panel, tl_Upload, uploadbm);
ACCESSIBLE(up, ButtonAx);
up->SetName(wxT("Sign a log and upload it automatically to LoTW"));
up->SetBitmapDisabled(upload_disbm);
b1sizer->Add(up, 0, wxALL, 1);
wxString b1lbl = wxT("\n");
b1lbl += _("Sign a log and upload it automatically to LoTW");
wxStaticText *b1txt = new wxStaticText(b1Panel, -1, b1lbl);
b1sizer->Add(b1txt, 1, wxFIXED_MINSIZE | wxALL, 1);
b1txt->SetLabel(b1lbl);
bsizer->Add(b1Panel, 0, wxALL, 1);
wxPanel* b2Panel = new wxPanel(buttons);
b2Panel->SetBackgroundColour(defBkg);
wxBoxSizer* b2sizer = new wxBoxSizer(wxHORIZONTAL);
b2Panel->SetSizer(b2sizer);
wxBitmapButton *signsave = new wxBitmapButton(b2Panel, tl_Save, savebm);
ACCESSIBLE(signsave, ButtonAx);
signsave->SetName(wxT("Sign a log and save it for uploading later"));
b2sizer->Add(signsave, 0, wxALL, 1);
wxString b2lbl = wxT("\n");
b2lbl += _("Sign a log and save it for uploading later");
wxStaticText *b2txt = new wxStaticText(b2Panel, -1, b2lbl);
b2txt->SetLabel(b2lbl);
b2sizer->Add(b2txt, 1, wxALL, 1);
bsizer->Add(b2Panel, 0, wxALL, 1);
wxPanel* b3Panel = new wxPanel(buttons);
b3Panel->SetBackgroundColour(defBkg);
wxBoxSizer* b3sizer = new wxBoxSizer(wxHORIZONTAL);
b3Panel->SetSizer(b3sizer);
wxBitmapButton *fed = new wxBitmapButton(b3Panel, tl_Edit, file_editbm);
fed->SetBitmapDisabled(file_edit_disbm);
ACCESSIBLE(fed, ButtonAx);
fed->SetName(wxT("Create an ADIF file for signing and uploading"));
b3sizer->Add(fed, 0, wxALL, 1);
wxString b3lbl = wxT("\n");
b3lbl += _("Create an ADIF file for signing and uploading");
wxStaticText *b3txt = new wxStaticText(b3Panel, -1, b3lbl);
b3sizer->Add(b3txt, 1, wxALL, 1);
b3txt->SetLabel(b3lbl);
bsizer->Add(b3Panel, 0, wxALL, 1);
wxPanel* b4Panel = new wxPanel(buttons);
b4Panel->SetBackgroundColour(defBkg);
wxBoxSizer* b4sizer = new wxBoxSizer(wxHORIZONTAL);
b4Panel->SetSizer(b4sizer);
wxBitmapButton *lotw = new wxBitmapButton(b4Panel, tl_Login, lotwbm);
ACCESSIBLE(lotw, ButtonAx);
lotw->SetName(wxT("Log in to the Logbook of the World Site"));
b4sizer->Add(lotw, 0, wxALL, 1);
wxString b4lbl = wxT("\n");
b4lbl += _("Log in to the Logbook of the World Site");
wxStaticText *b4txt = new wxStaticText(b4Panel, -1, b4lbl);
b4sizer->Add(b4txt, 1, wxALL, 1);
b4txt->SetLabel(b4lbl);
bsizer->Add(b4Panel, 0, wxALL, 1);
notebook->AddPage(buttons, _("Log Operations"));
// notebook->InvalidateBestSize();
// logwin->FitInside();
// Location tab
wxPanel* loctab = new wxPanel(notebook, -1);
wxBoxSizer* locsizer = new wxBoxSizer(wxHORIZONTAL);
loctab->SetSizer(locsizer);
wxPanel* locgrid = new wxPanel(loctab, -1);
locgrid->SetBackgroundColour(defBkg);
wxBoxSizer* lgsizer = new wxBoxSizer(wxVERTICAL);
locgrid->SetSizer(lgsizer);
#if wxUSE_ACCESSIBILITY
int treeStyle = wxTR_HAS_BUTTONS | wxSUNKEN_BORDER;;
#else
int treeStyle = wxTR_DEFAULT_STYLE | wxBORDER_NONE;
#endif
loc_tree = new LocTree(locgrid, tc_LocTree, wxDefaultPosition, wxDefaultSize, treeStyle);
if (!loc_tree) {
tqslTrace("MyFrame::MyFrame", "Out of meemory!");
return;
}
loc_tree->SetBackgroundColour(defBkg);
loc_tree->Build();
LocTreeReset();
ACCESSIBLE(loc_tree, TreeCtrlAx);
loc_tree->SetName(wxT("Station Locations"));
lgsizer->Add(loc_tree, 1, wxEXPAND);
wxString lsl = wxT("\n");
lsl += _("Select a Station Location to process ");
loc_select_label = new wxStaticText(locgrid, -1, lsl);
lgsizer->Add(loc_select_label, 0, wxALL, 1);
locsizer->Add(locgrid, 50, wxEXPAND);
wxStaticLine *locsep = new wxStaticLine(loctab, -1, wxDefaultPosition, wxSize(2, -1), wxLI_VERTICAL);
locsizer->Add(locsep, 0, wxEXPAND);
wxPanel* lbuttons = new wxPanel(loctab, -1);
lbuttons->SetBackgroundColour(defBkg);
locsizer->Add(lbuttons, 50, wxEXPAND);
wxBoxSizer* lbsizer = new wxBoxSizer(wxVERTICAL);
lbuttons->SetSizer(lbsizer);
wxPanel* lb1Panel = new wxPanel(lbuttons);
lb1Panel->SetBackgroundColour(defBkg);
wxBoxSizer* lb1sizer = new wxBoxSizer(wxHORIZONTAL);
lb1Panel->SetSizer(lb1sizer);
loc_add_button = new wxBitmapButton(lb1Panel, tl_AddLoc, locaddbm);
loc_add_button->SetBitmapDisabled(locadd_disbm);
ACCESSIBLE(loc_add_button, ButtonAx);
loc_add_button->SetName(wxT("Create a new Station Location"));
lb1sizer->Add(loc_add_button, 0, wxALL, 1);
loc_tree->SetTabTo(loc_add_button);
// Note - the doubling below is to size the label to allow the control to stretch later
loc_add_label = new wxStaticText(lb1Panel, -1, wxT("\nCreate a new Station LocationCreate a new Station\n"));
lb1sizer->Add(loc_add_label, 1, wxFIXED_MINSIZE | wxALL, 1);
lbsizer->Add(lb1Panel, 0, wxALL, 1);
int tw, th;
loc_add_label->GetSize(&tw, &th);
wxString lal = wxT("\n");
lal += _("Create a new Station Location");
loc_add_label->SetLabel(lal);
wxPanel* lb2Panel = new wxPanel(lbuttons);
lb2Panel->SetBackgroundColour(defBkg);
wxBoxSizer* lb2sizer = new wxBoxSizer(wxHORIZONTAL);
lb2Panel->SetSizer(lb2sizer);
loc_edit_button = new wxBitmapButton(lb2Panel, tl_EditLoc, editbm);
loc_edit_button->SetBitmapDisabled(edit_disbm);
loc_edit_button->Enable(false);
ACCESSIBLE(loc_edit_button, ButtonAx);
loc_edit_button->SetName(wxT("Edit a Station Location"));
lb2sizer->Add(loc_edit_button, 0, wxALL, 1);
wxString lel = wxT("\n");
lel += _("Edit a Station Location");
loc_edit_label = new wxStaticText(lb2Panel, -1, lel, wxDefaultPosition, wxSize(tw, th));
lb2sizer->Add(loc_edit_label, 1, wxFIXED_MINSIZE | wxALL, 1);
lbsizer->Add(lb2Panel, 0, wxALL, 1);
wxPanel* lb3Panel = new wxPanel(lbuttons);
lb3Panel->SetBackgroundColour(defBkg);
wxBoxSizer* lb3sizer = new wxBoxSizer(wxHORIZONTAL);
lb3Panel->SetSizer(lb3sizer);
loc_delete_button = new wxBitmapButton(lb3Panel, tl_DeleteLoc, deletebm);
loc_delete_button->SetBitmapDisabled(delete_disbm);
loc_delete_button->Enable(false);
ACCESSIBLE(loc_delete_button, ButtonAx);
loc_delete_button->SetName(wxT("Delete a Station Location"));
lb3sizer->Add(loc_delete_button, 0, wxALL, 1);
wxString ldl = wxT("\n");
ldl += _("Delete a Station Location");
loc_delete_label = new wxStaticText(lb3Panel, -1, ldl, wxDefaultPosition, wxSize(tw, th));
lb3sizer->Add(loc_delete_label, 1, wxFIXED_MINSIZE | wxALL, 1);
lbsizer->Add(lb3Panel, 0, wxALL, 1);
wxPanel* lb4Panel = new wxPanel(lbuttons);
lb4Panel->SetBackgroundColour(defBkg);
wxBoxSizer* lb4sizer = new wxBoxSizer(wxHORIZONTAL);
lb4Panel->SetSizer(lb4sizer);
loc_prop_button = new wxBitmapButton(lb4Panel, tl_PropLoc, propertiesbm);
loc_prop_button->SetBitmapDisabled(properties_disbm);
loc_prop_button->Enable(false);
ACCESSIBLE(loc_prop_button, ButtonAx);
loc_prop_button->SetName(wxT("Display Station Location Properties"));
lb4sizer->Add(loc_prop_button, 0, wxALL, 1);
wxString lpl = wxT("\n");
lpl += _("Display Station Location Properties");
loc_prop_label = new wxStaticText(lb4Panel, -1, lpl, wxDefaultPosition, wxSize(tw, th));
lb4sizer->Add(loc_prop_label, 1, wxFIXED_MINSIZE | wxALL, 1);
lbsizer->Add(lb4Panel, 0, wxALL, 1);
notebook->AddPage(loctab, _("Station Locations"));
// Certificates tab
wxPanel* certtab = new wxPanel(notebook, -1);
wxBoxSizer* certsizer = new wxBoxSizer(wxHORIZONTAL);
certtab->SetSizer(certsizer);
wxPanel* certgrid = new wxPanel(certtab, -1);
certgrid->SetBackgroundColour(defBkg);
wxBoxSizer* cgsizer = new wxBoxSizer(wxVERTICAL);
certgrid->SetSizer(cgsizer);
cert_tree = new CertTree(certgrid, tc_CertTree, wxDefaultPosition,
wxDefaultSize, treeStyle); //wxTR_HAS_BUTTONS | wxSUNKEN_BORDER);
if (!cert_tree) {
tqslTrace("MyFrame::MyFrame", "Out of memory!!");
return;
}
cert_tree->SetBackgroundColour(defBkg);
ACCESSIBLE(cert_tree, TreeCtrlAx);
cert_tree->SetName(wxT("Callsign Certificates"));
cgsizer->Add(cert_tree, 1, wxEXPAND);
wxString csq = wxT("\n");
csq += _("Select a Callsign Certificate to process");
cert_select_label = new wxStaticText(certgrid, -1, csq);
cgsizer->Add(cert_select_label, 0, wxALL, 1);
certsizer->Add(certgrid, 50, wxEXPAND);
wxStaticLine *certsep = new wxStaticLine(certtab, -1, wxDefaultPosition, wxSize(2, -1), wxLI_VERTICAL);
certsizer->Add(certsep, 0, wxEXPAND);
wxPanel* cbuttons = new wxPanel(certtab, -1);
cbuttons->SetBackgroundColour(defBkg);
certsizer->Add(cbuttons, 50, wxEXPAND, 0);
wxBoxSizer* cbsizer = new wxBoxSizer(wxVERTICAL);
cbuttons->SetSizer(cbsizer);
wxPanel* cb1Panel = new wxPanel(cbuttons);
cb1Panel->SetBackgroundColour(defBkg);
wxBoxSizer* cb1sizer = new wxBoxSizer(wxHORIZONTAL);
cb1Panel->SetSizer(cb1sizer);
cert_load_button = new wxBitmapButton(cb1Panel, tc_Load, importbm);
cert_load_button->SetBitmapDisabled(delete_disbm);
ACCESSIBLE(cert_load_button, ButtonAx);
cert_load_button->SetName(wxT("Load a Callsign Certificate"));
cert_tree->SetTabTo(cert_load_button);
cb1sizer->Add(cert_load_button, 0, wxALL, 1);
wxString lcl = wxT("\n");
lcl += _("Load a Callsign Certificate");
cert_load_label = new wxStaticText(cb1Panel, -1, lcl, wxDefaultPosition, wxSize(tw, th));
cb1sizer->Add(cert_load_label, 1, wxFIXED_MINSIZE | wxALL, 1);
cbsizer->Add(cb1Panel, 0, wxALL, 1);
wxPanel* cb2Panel = new wxPanel(cbuttons);
cb2Panel->SetBackgroundColour(defBkg);
wxBoxSizer* cb2sizer = new wxBoxSizer(wxHORIZONTAL);
cb2Panel->SetSizer(cb2sizer);
cert_save_button = new wxBitmapButton(cb2Panel, tc_CertSave, downloadbm);
cert_save_button->SetBitmapDisabled(download_disbm);
cert_save_button->Enable(false);
ACCESSIBLE(cert_save_button, ButtonAx);
cert_save_button->SetName(wxT("Save a Callsign Certificate"));
cb2sizer->Add(cert_save_button, 0, wxALL, 1);
wxString csl = wxT("\n");
csl += _("Save a Callsign Certificate");
cert_save_label = new wxStaticText(cb2Panel, -1, csl, wxDefaultPosition, wxSize(tw, th));
cb2sizer->Add(cert_save_label, 1, wxFIXED_MINSIZE | wxALL, 1);
cbsizer->Add(cb2Panel, 0, wxALL, 1);
wxPanel* cb3Panel = new wxPanel(cbuttons);
cb3Panel->SetBackgroundColour(defBkg);
wxBoxSizer* cb3sizer = new wxBoxSizer(wxHORIZONTAL);
cb3Panel->SetSizer(cb3sizer);
cert_renew_button = new wxBitmapButton(cb3Panel, tc_CertRenew, uploadbm);
cert_renew_button->SetBitmapDisabled(upload_disbm);
cert_renew_button->Enable(false);
ACCESSIBLE(cert_renew_button, ButtonAx);
cert_renew_button->SetName(wxT("Renew a Callsign Certificate"));
cb3sizer->Add(cert_renew_button, 0, wxALL, 1);
wxString crl = wxT("\n");
crl += _("Renew a Callsign Certificate");
cert_renew_label = new wxStaticText(cb3Panel, -1, crl, wxDefaultPosition, wxSize(tw, th));
cb3sizer->Add(cert_renew_label, 1, wxFIXED_MINSIZE | wxALL, 1);
cbsizer->Add(cb3Panel, 0, wxALL, 1);
wxPanel* cb4Panel = new wxPanel(cbuttons);
cb4Panel->SetBackgroundColour(defBkg);
wxBoxSizer* cb4sizer = new wxBoxSizer(wxHORIZONTAL);
cb4Panel->SetSizer(cb4sizer);
cert_prop_button = new wxBitmapButton(cb4Panel, tc_CertProp, propertiesbm);
cert_prop_button->SetBitmapDisabled(properties_disbm);
cert_prop_button->Enable(false);
ACCESSIBLE(cert_prop_button, ButtonAx);
cert_prop_button->SetName(wxT("Display a Callsign Certificate's Properties"));
cb4sizer->Add(cert_prop_button, 0, wxALL, 1);
wxString dcl = wxT("\n");
dcl += _("Display a Callsign Certificate's Properties");
cert_prop_label = new wxStaticText(cb4Panel, -1, dcl, wxDefaultPosition, wxSize(tw, th));
cb4sizer->Add(cert_prop_label, 1, wxFIXED_MINSIZE | wxALL, 1);
cbsizer->Add(cb4Panel, 0, wxALL, 1);
notebook->AddPage(certtab, _("Callsign Certificates"));
// Status Log tab (if enabled)
if (logTab) {
wxPanel* logtab = new wxPanel(notebook, -1);
wxBoxSizer* ltsizer = new wxBoxSizer(wxHORIZONTAL);
logtab->SetSizer(ltsizer);
logwin = new wxTextCtrl(logtab, -1, wxT(""), wxDefaultPosition, wxSize(800, 600),
wxTE_MULTILINE|wxTE_READONLY);
ACCESSIBLE(logwin, WindowAccessible);
logwin->SetName(wxT("Status Log"));
ltsizer->Add(logwin, 1, wxEXPAND);
notebook->AddPage(logtab, _("Status Log"));
}
//app icon
SetIcon(wxIcon(key_xpm));
if (checkUpdates) {
LogList *log = new LogList(this);
#if wxMAJOR_VERSION > 2
log->SetFormatter(new SimpleLogFormatter);
#endif
wxLog::SetActiveTarget(log);
}
}
MyFrame::~MyFrame(void) {
cert_tree->DeleteAllItems();
loc_tree->DeleteAllItems();
}
static wxString
run_station_wizard(wxWindow *parent, tQSL_Location loc, wxHtmlHelpController *help = 0,
bool expired = false, bool editing = false, wxString title = _("Add Station Location"), wxString dataname = wxT(""), wxString callsign = wxT("")) {
tqslTrace("run_station_wizard", "loc=%lx, expired=%d, editing=%d, title=%s, dataname=%s, callsign=%s", loc, expired, editing, S(title), S(dataname), S(callsign));
wxString rval(wxT(""));
get_certlist("", 0, expired, false, false);
if (ncerts == 0)
throw TQSLException("No certificates available");
TQSLWizard *wiz = new TQSLWizard(loc, parent, help, title, expired);
wiz->SetDefaultCallsign(callsign);
wiz->GetPage(true);
TQSLWizLocPage *cpage = reinterpret_cast<TQSLWizLocPage*>(wiz->GetPage());
if (cpage && !callsign.IsEmpty())
cpage->UpdateFields(0);
TQSLWizPage *page = wiz->GetPage();
if (page == 0)
throw TQSLException("Error getting first wizard page");
wiz->AdjustSize();
// Note: If dynamically created pages are larger than the pages already
// created (the initial page and the final page), the wizard will need to
// be resized, but we don't presently have that happening. (The final
// page is larger than all expected dynamic pages.)
bool okay = wiz->RunWizard(page);
rval = wiz->GetLocationName();
wiz->Destroy();
if (!okay)
return rval;
wxCharBuffer locname = rval.ToUTF8();
check_tqsl_error(tqsl_setStationLocationCaptureName(loc, locname.data()));
check_tqsl_error(tqsl_saveStationLocationCapture(loc, 1));
return rval;
}
void
MyFrame::OnHelpContents(wxCommandEvent& WXUNUSED(event)) {
help->Display(wxT("main.htm"));
}
// Return the "About" string
//
static wxString getAbout() {
wxString msg = wxT("TQSL V") wxT(TQSL_VERSION) wxT(" build ") wxT(TQSL_BUILD);
#ifdef OSX_PLATFORM
msg += wxT("\nBuilt for ") wxT(OSX_PLATFORM);
#endif
msg += wxT("\n(c) 2001-2023 American Radio Relay League\n\n");
int major, minor;
if (tqsl_getVersion(&major, &minor))
wxLogError(getLocalizedErrorString());
else
msg += wxString::Format(wxT("TrustedQSL library V%d.%d\n"), major, minor);
if (tqsl_getConfigVersion(&major, &minor))
wxLogError(getLocalizedErrorString());
else
msg += wxString::Format(wxT("\nConfiguration data V%d.%d\n\n"), major, minor);
msg += wxVERSION_STRING;
#ifdef wxUSE_UNICODE
if (wxUSE_UNICODE)
msg += wxT(" (Unicode)");
#endif
XML_Expat_Version xv = XML_ExpatVersionInfo();
msg+=wxString::Format(wxT("\nexpat v%d.%d.%d\n"), xv.major, xv.minor, xv.micro);
msg+=wxString::Format(wxT("libcurl V%hs\n"), LIBCURL_VERSION);
msg+=wxString::Format(wxT("%hs\n"), OPENSSL_VERSION_TEXT);
msg+=wxString::Format(wxT("zlib V%hs\n"), ZLIB_VERSION);
msg+=wxString::Format(wxT("SQLite %hs"), SQLITE_VERSION);
msg+=wxT("\n\n\nTranslators:\n");
msg+=wxT("Catalan: Xavi, EA3W (SK) and Salva, EB3MA\n");
msg+=wxT("Chinese (Simplified): Caros, BH4TXN\n");
msg+=wxT("Chinese (Traditional): SZE-TO Wing, VR2UPU\n");
msg+=wxT("Finnish: Juhani Tapaninen, OH8MXL\n");
msg+=wxT("French: Laurent Beugnet, F6GOX\n");
msg+=wxT("German: Andreas Rehberg, DF4WC\n");
msg+=wxT("Hindi: Manmohan Bhagat, VU3YBH\n");
msg+=wxT("Italian: Salvatore Besso, I4FYV\n");
msg+=wxT("Japanese: Akihiro KODA, JL3OXR\n");
msg+=wxString::FromUTF8("Polish: Roman Bagiński, SP4JEU\n");
msg+=wxT("Portuguese: Nuno Lopes, CT2IRY\n");
msg+=wxT("Russian: Vic Goncharsky, US5WE\n");
msg+=wxT("Spanish: Jordi Quintero, EA3GCV\n");
msg+=wxT("Swedish: Roger Jonsson, SM0LTV\n");
msg+=wxT("Turkish: Oguzhan Kayhan, TA2NC\n");
return msg;
}
void
MyFrame::OnHelpAbout(wxCommandEvent& WXUNUSED(event)) {
wxMessageBox(getAbout(), _("About"), wxOK | wxCENTRE | wxICON_INFORMATION, this);
}
void
MyFrame::OnHelpDiagnose(wxCommandEvent& event) {
wxString s_fname;
if (tqsl_diagFileOpen()) {
file_menu->Check(tm_f_diag, false);
tqsl_closeDiagFile();
wxMessageBox(wxT("Diagnostic log closed"), wxT("Diagnostics"), wxOK | wxCENTRE| wxICON_INFORMATION, this);
return;
}
s_fname = wxFileSelector(_("Log File"), wxT(""), wxT("tqsldiag.log"), wxT("log"),
_("Log files (*.log)|*.log|All files (*.*)|*.*"),
wxFD_SAVE|wxFD_OVERWRITE_PROMPT, this);
if (s_fname.IsEmpty()) {
file_menu->Check(tm_f_diag, false); //would be better to not check at all, but no, apparently that's a crazy thing to want
return;
}
if (tqsl_openDiagFile(s_fname.ToUTF8())) {
wxString errmsg = wxString::Format(_("Error opening diagnostic log %s: %hs"), s_fname.c_str(), strerror(errno));
wxMessageBox(errmsg, _("Log File Error"), wxOK | wxICON_EXCLAMATION);
return;
}
file_menu->Check(tm_f_diag, true);
wxString about = getAbout();
#ifdef _WIN32
about.Replace(wxT("\\n"), wxT("\\r\\n"));
#endif
tqslTrace(NULL, "TQSL Diagnostics\r\n%s\r\n\r\n", (const char *)about.ToUTF8());
tqslTrace(NULL, "Command Line: %s\r\n", (const char *)origCommandLine.ToUTF8());
tqslTrace(NULL, "Working Directory:%s\r\n", tQSL_BaseDir);
}
class FileUploadHandler {
public:
string s;
FileUploadHandler(): s() { s.reserve(2000); }
size_t internal_recv(char *ptr, size_t size, size_t nmemb) {
s.append(ptr, size*nmemb);
return size*nmemb;
}
static size_t recv(char *ptr, size_t size, size_t nmemb, void *userdata) {
return (reinterpret_cast<FileUploadHandler*>(userdata))->internal_recv(ptr, size, nmemb);
}
};
static void
AddEditStationLocation(tQSL_Location loc, bool expired = false, const wxString& title = _("Add Station Location"), const wxString& callsign = wxT("")) {
tqslTrace("AddEditStationLocation", "loc=%lx, expired=%lx, title=%s, callsign=%s", loc, expired, S(title), S(callsign));
try {
run_station_wizard(frame, loc, frame->help, expired, true, title, wxT(""), callsign);
frame->loc_tree->Build();
}
catch(TQSLException& x) {
wxLogError(wxT("%hs"), x.what());
}
}
void
MyFrame::AddStationLocation(wxCommandEvent& WXUNUSED(event)) {
tqslTrace("MyFrame::AddStationLocation", NULL);
wxTreeItemId root = loc_tree->GetRootItem();
wxTreeItemId id = loc_tree->GetSelection();
wxString call;
if (id != root && id.IsOk()) { // If something selected
LocTreeItemData *data = reinterpret_cast<LocTreeItemData *>(loc_tree->GetItemData(id));
if (data) {
call = data->getCallSign(); // Station location
} else {
call = loc_tree->GetItemText(id); // Callsign folder selected
}
tqslTrace("MyFrame::AddStationLocation", "Call selected is %s", S(call));
}
tQSL_Location loc;
if (tqsl_initStationLocationCapture(&loc)) {
wxLogError(getLocalizedErrorString());
}
AddEditStationLocation(loc, false, _("Add Station Location"), call);
if (tqsl_endStationLocationCapture(&loc)) {
wxLogError(getLocalizedErrorString());
}
loc_tree->Build();
LocTreeReset();
AutoBackup();
}
void
MyFrame::EditStationLocation(wxCommandEvent& event) {
tqslTrace("MyFrame::EditStationLocation", NULL);
if (event.GetId() == tl_EditLoc) {
try {
LocTreeItemData *data = reinterpret_cast<LocTreeItemData *>(loc_tree->GetItemData(loc_tree->GetSelection()));
tQSL_Location loc;
wxString selname;
char errbuf[512];
if (data == NULL) return;
check_tqsl_error(tqsl_getStationLocation(&loc, data->getLocname().ToUTF8()));
if (!verify_cert(loc, true)) // Check if there is a certificate before editing
return;
check_tqsl_error(tqsl_getStationLocationErrors(loc, errbuf, sizeof(errbuf)));
if (strlen(errbuf) > 0) {
wxString fmt = wxT("%hs\n");
// TRANSLATORS: uncommon error - error in a station location, followed by the ignore message that follows.
fmt += _("The invalid data was ignored.");
wxMessageBox(wxString::Format(fmt, errbuf), _("Location data error"), wxOK | wxICON_EXCLAMATION, this);
}
char loccall[512];
check_tqsl_error(tqsl_getLocationCallSign(loc, loccall, sizeof loccall));
selname = run_station_wizard(this, loc, help, true, true, wxString::Format(_("Edit Station Location : %hs - %s"), loccall, data->getLocname().c_str()), data->getLocname());
check_tqsl_error(tqsl_endStationLocationCapture(&loc));
loc_tree->Build();
LocTreeReset();
AutoBackup();
return;
}
catch(TQSLException& x) {
wxLogError(wxT("%hs"), x.what());
return;
}
}
// How many locations are there?
try {
int n;
tQSL_Location loc;
check_tqsl_error(tqsl_initStationLocationCapture(&loc));
check_tqsl_error(tqsl_getNumStationLocations(loc, &n));
if (n == 1) {
// There's only one station location. Use that and don't prompt.
char deflocn[512];
check_tqsl_error(tqsl_getStationLocationName(loc, 0, deflocn, sizeof deflocn));
wxString locname = wxString::FromUTF8(deflocn);
tqsl_endStationLocationCapture(&loc);
check_tqsl_error(tqsl_getStationLocation(&loc, deflocn));
char loccall[512];
check_tqsl_error(tqsl_getLocationCallSign(loc, loccall, sizeof loccall));
run_station_wizard(this, loc, help, true, true, wxString::Format(_("Edit Station Location : %hs - %s"), loccall, locname.c_str()), locname);
check_tqsl_error(tqsl_endStationLocationCapture(&loc));
loc_tree->Build();
LocTreeReset();
AutoBackup();
return;
}
// More than one location or not selected in the tree. Prompt for the location.
check_tqsl_error(tqsl_endStationLocationCapture(&loc));
SelectStationLocation(_("Edit Station Location"), _("Close"), true);
loc_tree->Build();
LocTreeReset();
}
catch(TQSLException& x) {
wxLogError(wxT("%hs"), x.what());
return;
}
}
static tqsl_adifFieldDefinitions fielddefs[] = {
{ "CALL", "", TQSL_ADIF_RANGE_TYPE_NONE, TQSL_CALLSIGN_MAX, 0, 0, 0, 0 },
{ "BAND", "", TQSL_ADIF_RANGE_TYPE_NONE, TQSL_BAND_MAX, 0, 0, 0, 0 },
{ "BAND_RX", "", TQSL_ADIF_RANGE_TYPE_NONE, TQSL_BAND_MAX, 0, 0, 0, 0 },
{ "MODE", "", TQSL_ADIF_RANGE_TYPE_NONE, TQSL_MODE_MAX, 0, 0, 0, 0 },
{ "SUBMODE", "", TQSL_ADIF_RANGE_TYPE_NONE, TQSL_MODE_MAX, 0, 0, 0, 0 },
{ "FREQ", "", TQSL_ADIF_RANGE_TYPE_NONE, TQSL_FREQ_MAX, 0, 0, 0, 0 },
{ "FREQ_RX", "", TQSL_ADIF_RANGE_TYPE_NONE, TQSL_FREQ_MAX, 0, 0, 0, 0 },
{ "QSO_DATE", "", TQSL_ADIF_RANGE_TYPE_NONE, 8, 0, 0, 0, 0 },
{ "TIME_ON", "", TQSL_ADIF_RANGE_TYPE_NONE, 6, 0, 0, 0, 0 },
{ "SAT_NAME", "", TQSL_ADIF_RANGE_TYPE_NONE, TQSL_SATNAME_MAX, 0, 0, 0, 0 },
{ "PROP_MODE", "", TQSL_ADIF_RANGE_TYPE_NONE, TQSL_PROPMODE_MAX, 0, 0, 0, 0 },
{ "*", "", TQSL_ADIF_RANGE_TYPE_NONE, 2048, 0, 0, 0, 0 },
{ "EOR", "", TQSL_ADIF_RANGE_TYPE_NONE, 0, 0, 0, 0, 0 },
{ "", "", TQSL_ADIF_RANGE_TYPE_NONE, 0, 0, 0, 0, 0 },
};
static const char *defined_types[] = { "T", "D", "M", "C", "N", "6" };
static unsigned char *
adif_alloc(size_t n) {
return new unsigned char[n];
}
static void
loadQSOfile(wxString& file, QSORecordList& recs) {
init_modes();
tqsl_adifFieldResults field;
TQSL_ADIF_GET_FIELD_ERROR stat;
tQSL_ADIF adif;
if (tqsl_beginADIF(&adif, file.ToUTF8())) {
wxLogError(getLocalizedErrorString());
}
QSORecord rec;
do {
if (tqsl_getADIFField(adif, &field, &stat, fielddefs, defined_types, adif_alloc)) {
wxLogError(getLocalizedErrorString());
}
if (stat == TQSL_ADIF_GET_FIELD_SUCCESS) {
if (!strcasecmp(field.name, "CALL")) {
rec._call = wxString::FromUTF8((const char *)field.data);
} else if (!strcasecmp(field.name, "BAND")) {
rec._band = wxString::FromUTF8((const char *)field.data);
} else if (!strcasecmp(field.name, "BAND_RX")) {
rec._rxband = wxString::FromUTF8((const char *)field.data);
} else if (!strcasecmp(field.name, "MODE")) {
rec._mode = wxString::FromUTF8((const char *)field.data);
} else if (!strcasecmp(field.name, "SUBMODE")) {
char amode[40];
if (tqsl_getADIFMode((const char *)field.data, amode, sizeof amode) == 0 && amode[0] != '\0')
rec._mode = wxString::FromUTF8(amode);
} else if (!strcasecmp(field.name, "FREQ")) {
rec._freq = wxString::FromUTF8((const char *)field.data);
} else if (!strcasecmp(field.name, "FREQ_RX")) {
rec._rxfreq = wxString::FromUTF8((const char *)field.data);
} else if (!strcasecmp(field.name, "PROP_MODE")) {
rec._propmode = wxString::FromUTF8((const char *)field.data);
} else if (!strcasecmp(field.name, "SAT_NAME")) {
rec._satellite = wxString::FromUTF8((const char *)field.data);
} else if (!strcasecmp(field.name, "QSO_DATE")) {
char *cp = reinterpret_cast<char *>(field.data);
if (strlen(cp) == 8) {
rec._date.day = strtol(cp+6, NULL, 10);
*(cp+6) = '\0';
rec._date.month = strtol(cp+4, NULL, 10);
*(cp+4) = '\0';
rec._date.year = strtol(cp, NULL, 10);
}
} else if (!strcasecmp(field.name, "TIME_ON")) {
char *cp = reinterpret_cast<char *>(field.data);
if (strlen(cp) >= 4) {
rec._time.second = (strlen(cp) > 4) ? strtol(cp+4, NULL, 10) : 0;
*(cp+4) = 0;
rec._time.minute = strtol(cp+2, NULL, 10);
*(cp+2) = '\0';
rec._time.hour = strtol(cp, NULL, 10);
}
} else if (!strcasecmp(field.name, "EOH")) {
rec._extraFields.clear();
} else if (!strcasecmp(field.name, "EOR")) {
recs.push_back(rec);
rec = QSORecord();
} else {
// Not a field I recognize, add it to the extras
const char * val = reinterpret_cast<const char *>(field.data);
if (!val)
val = "";
rec._extraFields[field.name] = val;
}
delete[] field.data;
}
} while (stat == TQSL_ADIF_GET_FIELD_SUCCESS || stat == TQSL_ADIF_GET_FIELD_NO_NAME_MATCH);
tqsl_endADIF(&adif);
return;
}
void
MyFrame::EditQSOData(wxCommandEvent& WXUNUSED(event)) {
tqslTrace("MyFrame::EditQSOData", NULL);
QSORecordList recs;
wxString file = wxFileSelector(_("Open File"), wxConfig::Get()->Read(wxT("QSODataPath"), wxT("")), wxT(""), wxT("adi"),
#if !defined(__APPLE__) && !defined(_WIN32)
_("ADIF files (*.adi;*.adif;*.ADI;*.ADIF)|*.adi;*.adif;*.ADI;*.ADIF|All files (*.*)|*.*"),
#else
_("ADIF files (*.adi;*.adif)|*.adi;*.adif|All files (*.*)|*.*"),
#endif
wxFD_OPEN|wxFD_FILE_MUST_EXIST, this);
if (file.IsEmpty())
return;
loadQSOfile(file, recs);
try {
QSODataDialog dial(this, file, help, &recs);
dial.ShowModal();
} catch(TQSLException& x) {
wxLogError(wxT("%hs"), x.what());
}
}
void
MyFrame::EnterQSOData(wxCommandEvent& WXUNUSED(event)) {
tqslTrace("MyFrame::EnterQSOData", NULL);
QSORecordList recs;
wxString file = wxT("tqsl.adi");
try {
QSODataDialog dial(this, file, help, &recs);
dial.ShowModal();
} catch(TQSLException& x) {
wxLogError(wxT("%hs"), x.what());
}
}
class DBWaitDialog : public wxDialog {
public:
DBWaitDialog(wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxDEFAULT_DIALOG_STYLE, const wxString& name = wxT("dialogBox"));
~DBWaitDialog();
void OnDBTimer(wxTimerEvent& WXUNUSED(event));
void Start(int interval);
void Stop(void);
wxTimer* waitForIt;
};
void DBWaitDialog::OnDBTimer(wxTimerEvent& WXUNUSED(event)) {
if (lock_db(false) < 0) {
return;
}
EndModal(wxOK);
}
void DBWaitDialog::Start(int interval) {
waitForIt->Start(interval);
}
void DBWaitDialog::Stop(void) {
waitForIt->Stop();
}
DBWaitDialog::~DBWaitDialog() {
delete waitForIt;
}
DBWaitDialog::DBWaitDialog(wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style, const wxString& name) : wxDialog(parent, id, title, pos, size, style, name) {
wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
sizer->Add(new wxStaticText(this, -1, name), 0, wxALL|wxALIGN_CENTER, 10);
sizer->Add(new wxButton(this, TQSL_DB_CANBUT, _("Cancel")), 0, wxALIGN_CENTER, 5);
SetEscapeId(TQSL_DB_CANBUT);
SetAutoLayout(TRUE);
SetSizer(sizer);
sizer->Fit(this);
sizer->SetSizeHints(this);
CenterOnParent();
waitForIt = new wxTimer(this, tmr_db);
Connect(tmr_db, wxEVT_TIMER, wxTimerEventHandler(DBWaitDialog::OnDBTimer), NULL, this);
}
int MyFrame::SignLogFile(tQSL_Location loc, const wxString& infile, void *output, bool compressed, int& numrecs, bool suppressdate, tQSL_Date* startdate, tQSL_Date* enddate, int action, int logverify, const char* password, const char* defcall, int signtype) {
tqslTrace("MyFrame::SignLogFile", "loc = %lx, infile=%s, suppressdate=%d, startdate=0x%lx, enddate=0x%lx, action=%d, logverify=%d defcall=%s", reinterpret_cast<void *>(loc), S(infile), suppressdate, reinterpret_cast<void *>(startdate), reinterpret_cast<void *>(enddate), action, logverify, defcall ? defcall : "");
static const char *iam = "TQSL V" TQSL_VERSION;
const char *cp;
char callsign[40];
int dxcc = 0;
wxString name, ext;
bool allow_dupes = false;
bool restarting = false;
bool ignore_err = false;
bool show_dupes = false;
bool dupes_only = false;
int tmp;
DXCC dx;
wxString msg;
// Do we have a vuccgrids.dat?
int vdatOK = 0;
tqsl_validateVUCCGrid(291, "VA", "AA01", &vdatOK);
if (vdatOK == (TQSL_VALID_VUCC_ENT | TQSL_VALID_VUCC_PAS)) {
wxMessageBox(_("The vuccgrids.dat configuration file is missing or damaged. VUCC grid validation has been disabled."), _("Warning"), wxOK | wxICON_EXCLAMATION, this);
}
wxConfig *config = reinterpret_cast<wxConfig *>(wxConfig::Get());
config->Read(wxT("DispDupes"), &show_dupes, DEFAULT_DISP_DUPES);
if (signtype == TQSL_UPLOAD_ONLY) {
dupes_only = true;
allow_dupes = false;
ignore_err = true;
}
if (logverify == -1) { // Not on command line
get_certlist("", 0, false, false, true); // any call, any DXCC, not expired or superceded
if (dupes_only) {
strncpy(callsign, "[None]", sizeof callsign);
logverify = TQSL_LOC_UPDATE;
goto skip_call;
} else {
config->Read(wxT("LogVerify"), &tmp, TQSL_LOC_REPORT);
if (tmp == TQSL_LOC_IGNORE || tmp == TQSL_LOC_REPORT || tmp == TQSL_LOC_UPDATE) {
logverify = tmp;
} else {
logverify = TQSL_LOC_REPORT;
}
goto skip_call;
}
} else { // Command line
show_dupes = true; // So always display dupe details
if (logverify == TQSL_LOC_UPDATE) {
tqsl_getLocationCallSign(loc, callsign, sizeof callsign);
if (strcmp(callsign, "[None]") == 0) {
get_certlist("", 0, false, false, true); // any call, any DXCC, not expired or superceded
goto skip_call;
}
}
}
try {
if (defcall) {
strncpy(callsign, defcall, sizeof callsign);
check_tqsl_error(tqsl_setLocationCallSign(loc, callsign, -1));
} else {
check_tqsl_error(tqsl_getLocationCallSign(loc, callsign, sizeof callsign));
}
} catch(TQSLException &x) {
wxLogError(wxT("%hs"), x.what());
return TQSL_EXIT_LIB_ERROR;
}
tqsl_getLocationDXCCEntity(loc, &dxcc);
dx.getByEntity(dxcc);
get_certlist(callsign, dxcc, false, false, false);
if (ncerts == 0) {
// No certificates for this callsign/dxcc pair.
// If the callsign for this location is set to "[None]", go look
// for a suitable callsign certificate
if (strcmp(callsign, "[None]") == 0) {
// Get all of the certificates for this location's DXCC entity
get_certlist("", dxcc, false, false, false);
// If only one callsign matches the dxcc entity, just use that
if (ncerts == 1) {
tqsl_getCertificateCallSign(certlist[0], callsign, sizeof callsign);
tqsl_setLocationCallSign(loc, callsign, -1);
} else if (ncerts > 1) {
wxString lastCall = wxConfig::Get()->Read(wxT("LastCallsignForNone"));
wxString *choices = new wxString[ncerts];
int def_index = 0;
for (int i = 0; i < ncerts; i++) {
tqsl_getCertificateCallSign(certlist[i], callsign, sizeof callsign);
wxString csign = wxString::FromUTF8(callsign);
int dxcc;
const char *dxccname = NULL;
if (!tqsl_getCertificateDXCCEntity(certlist[i], &dxcc) &&
!tqsl_getDXCCEntityName(dxcc, &dxccname)) {
csign += wxT(" [") + wxString::FromUTF8(dxccname) + wxT("]");
}
choices[i] = csign;
if (lastCall == csign)
def_index = i;
}
wxSingleChoiceDialog dialog(this, _("Please choose a callsign for this Station Location"),
_("Select Callsign"), ncerts, choices);
dialog.SetSelection(def_index);
int idx;
if (dialog.ShowModal() == wxID_OK)
idx = dialog.GetSelection();
else
return TQSL_EXIT_CANCEL;
wxConfig::Get()->Write(wxT("LastCallsignForNone"), choices[idx]);
tqsl_getCertificateCallSign(certlist[idx], callsign, sizeof callsign);
get_certlist(callsign, dxcc, false, false, false);
tqsl_setLocationCallSign(loc, callsign, -1);
}
char loc_name[128];
if (tqsl_getStationLocationCaptureName(loc, loc_name, sizeof loc_name)) {
strncpy(loc_name, "Unknown", sizeof loc_name);
}
LocPropDial dial(wxString::FromUTF8(loc_name), false, infile.ToUTF8(), this);
int locnOK = dial.ShowModal();
if (locnOK != wxID_OK) {
return TQSL_EXIT_CANCEL;
}
}
} else {
// The defcall or callsign from the location has matching certificates
// for this DXCC location. If we used the default call, set that
// for the location
if (defcall) {
tqsl_setLocationCallSign(loc, defcall, -1);
}
}
if (ncerts == 0) {
wxString fmt = _("There are no valid callsign certificates for callsign");
fmt += wxT(" %hs ");
fmt += _("in entity");
fmt += wxT(" %hs.\n");
fmt += _("Signing aborted.");
fmt + wxT("\n");
msg = wxString::Format(fmt, callsign, dx.name());
wxLogError(msg);
return TQSL_EXIT_TQSL_ERROR;
}
// Re-load the certificate list with all valid calls
free_certlist();
get_certlist("", 0, false, false, false);
if (!dupes_only) {
msg = _("Signing using Callsign %hs, DXCC Entity %hs");
msg += wxT("\n");
wxLogMessage(msg, callsign, dx.name());
}
skip_call:
init_modes();
init_contests();
wxString dgMap;
#ifdef MAP_CABRILLO_MODE
config->Read(wxT("CabrilloDGMap"), &dgMap, DEFAULT_CABRILLO_DG_MAP);
tqsl_setCabrilloDGMap(dgMap.ToUTF8());
#endif
if (!dupes_only && (lock_db(false) < 0)) {
if (quiet) { // If the database is locked, don't stall if in batch mode.
return TQSL_EXIT_BUSY;
}
wxSafeYield();
DBWaitDialog* waitForDB = new DBWaitDialog(this, -1, _("Another copy of TQSL is signing a log"), wxDefaultPosition,
wxDefaultSize, wxDEFAULT_DIALOG_STYLE, _("TQSL must wait for other running copies of TQSL to exit before signing ..."));
waitForDB->Start(500);
int result = waitForDB->ShowModal();
waitForDB->Stop();
if (result == wxID_CANCEL) {
wxLogMessage(_("Aborting due to user cancelling"));
return TQSL_EXIT_BUSY;
}
}
restart:
ConvertingDialog *conv_dial = new ConvertingDialog(this, infile.ToUTF8());
numrecs = 0;
bool cancelled = false;
bool aborted = false;
int lineno = 0;
int out_of_range = 0;
int duplicates = 0;
int processed = 0;
int errors = 0;
try {
if (tqsl_beginCabrilloConverter(&logConv, infile.ToUTF8(), certlist, ncerts, loc)) {
if (tQSL_Error != TQSL_CABRILLO_ERROR || tQSL_Cabrillo_Error != TQSL_CABRILLO_NO_START_RECORD)
check_tqsl_error(1); // A bad error
lineno = 0;
check_tqsl_error(tqsl_beginADIFConverter(&logConv, infile.ToUTF8(), certlist, ncerts, loc));
}
bool range = true;
config->Read(wxT("DateRange"), &range);
if (range && !suppressdate && !restarting) {
DateRangeDialog dial(this);
if (dial.ShowModal() != wxOK) {
wxLogMessage(_("Cancelled"));
unlock_db();
return TQSL_EXIT_CANCEL;
}
tqsl_setADIFConverterDateFilter(logConv, &dial.start, &dial.end);
if (this->IsQuiet()) {
this->Show(false);
wxSafeYield(this);
}
}
if (startdate || enddate) {
tqslTrace("MyFrame::SignLogFile", "startdate %d/%d/%d, enddate %d/%d/%d",
startdate ? startdate->year : 0,
startdate ? startdate->month : 0,
startdate ? startdate->day : 0,
enddate ? enddate->year : 0,
enddate ? enddate->month : 0,
enddate ? enddate->day : 0);
tqsl_setADIFConverterDateFilter(logConv, startdate, enddate);
}
bool allow = false;
config->Read(wxT("BadCalls"), &allow);
tqsl_setConverterAllowBadCall(logConv, allow);
tqsl_setConverterAllowDuplicates(logConv, allow_dupes || (signtype == TQSL_TEST_LOG));
config->Read(wxT("IgnoreSeconds"), &allow, DEFAULT_IGNORE_SECONDS);
tqsl_setConverterIgnoreSeconds(logConv, allow);
config->Read(wxT("IgnoreADIFCallsign"), &allow, DEFAULT_IGNORE_CALLSIGN);
tqsl_setConverterIgnoreCallsigns(logConv, allow);
tqsl_setConverterAppName(logConv, iam);
tqsl_setConverterQTHDetails(logConv, logverify);
tqsl_setConverterDupesOnly(logConv, dupes_only);
wxFileName::SplitPath(infile, 0, &name, &ext);
if (!ext.IsEmpty())
name += wxT(".") + ext;
// Only display windows if notin batch mode -- KD6PAG
if (!this->IsQuiet() && !dupes_only) {
conv_dial->Show(TRUE);
}
this->Enable(FALSE);
do {
while ((cp = tqsl_getConverterGABBI(logConv)) != 0) {
if (!this->IsQuiet()) {
if (dupes_only) {
wxSafeYield(this);
} else {
wxSafeYield(conv_dial);
}
}
if (!conv_dial->running) {
break;
}
// Only count QSO records
if (strstr(cp, "tCONTACT")) {
++numrecs;
++processed;
}
if ((processed % 10) == 0) {
wxString progress = wxString::Format(_("QSOs: %d"), processed);
if (!allow_dupes && duplicates > 0)
progress += wxT(" ") + wxString::Format(_("Previously Signed: %d"), duplicates);
if (errors > 0 || out_of_range > 0)
progress += wxT(" ") + wxString::Format(_("Errors: %d"), errors + out_of_range);
conv_dial->msg->SetLabel(progress);
}
if (!dupes_only && output != NULL) {
if (!compressed) {
if (fprintf(reinterpret_cast<FILE *>(output), "%s\n", cp) < 0) {
throw TQSLException(strerror(errno));
}
} else {
int err = gzprintf(reinterpret_cast<gzFile>(output), "%s\n", cp);
if (err < 0) {
throw TQSLException(gzerror(reinterpret_cast<gzFile>(output), &err));
}
}
}
}
if ((processed % 10) == 0) {
wxString progress = wxString::Format(_("QSOs: %d"), processed);
if (!allow_dupes && duplicates > 0)
progress += wxT(" ") + wxString::Format(_("Previously signed: %d"), duplicates);
if (errors > 0 || out_of_range > 0)
progress += wxT(" ") + wxString::Format(_("Errors: %d"), errors + out_of_range);
if (!dupes_only)
conv_dial->msg->SetLabel(progress);
}
// Handle large logs already uploaded.
// More than 50,000 QSOs. 80% dupes or more (40,000 Qs).
// Running with all QSOs allowed
if (processed > 50000 && duplicates > 40000 && allow_dupes) {
wxLogError(_("This log has too many previously uploaded QSOs. Please only upload new QSOs or break the log into smaller pieces (50,000 QSOs or less)."));
aborted = true;
break;
}
if (cp == 0) {
if (!this->IsQuiet()) {
if (dupes_only) {
wxSafeYield(this);
} else {
wxSafeYield(conv_dial);
}
}
if (!conv_dial->running) {
break;
}
}
if (tQSL_Error == TQSL_SIGNINIT_ERROR) {
tQSL_Cert cert;
int rval;
check_tqsl_error(tqsl_getConverterCert(logConv, &cert));
do {
if ((rval = tqsl_beginSigning(cert, const_cast<char *>(password), getCertPassword, cert)) == 0)
break;
if (tQSL_Error == TQSL_PASSWORD_ERROR) {
if ((rval = tqsl_beginSigning(cert, const_cast<char *>(unipwd), NULL, cert)) == 0)
break;
wxLogMessage(_("Passphrase error"));
if (password)
free(reinterpret_cast<void *>(const_cast<char *>(password)));
password = NULL;
}
} while (tQSL_Error == TQSL_PASSWORD_ERROR);
if (tQSL_Error == TQSL_CUSTOM_ERROR && (tQSL_Errno == ENOENT || tQSL_Errno == EPERM)) {
snprintf(tQSL_CustomError, sizeof tQSL_CustomError,
"Can't open the private key file for %s: %s", callsign, strerror(tQSL_Errno));
}
check_tqsl_error(rval);
if (this->IsQuiet()) {
this->Show(false);
wxSafeYield(this);
}
continue;
}
if (tQSL_Error == TQSL_DATE_OUT_OF_RANGE) {
processed++;
out_of_range++;
continue;
}
if (tQSL_Error == TQSL_NEW_UPLOAD_DB) { // New database, use it.
continue;
}
char dupeErrString[256];
dupeErrString[0] = '\0';
bool dupe_error = (tQSL_Error == TQSL_DUPLICATE_QSO);
if (dupe_error) {
strncpy(dupeErrString, tQSL_CustomError, sizeof dupeErrString);
duplicates++;
if (!show_dupes) {
processed++;
continue;
}
}
bool has_error = (tQSL_Error != TQSL_NO_ERROR);
if (has_error && !dupes_only) {
bool dberror = false;
if (tQSL_Error == TQSL_DB_ERROR)
dberror = true;
processed++;
if (!dupe_error) {
errors++;
}
try {
check_tqsl_error(1);
} catch(TQSLException& x) {
if (tqsl_getConverterLine(logConv, &lineno))
lineno = 0;
wxString msg = wxGetTranslation(wxString::FromUTF8(x.what()));
if (lineno)
msg += wxT(" ") + wxString::Format(_("on line %d"), lineno);
const char *bad_text = tqsl_getConverterRecordText(logConv);
if (bad_text)
msg += wxString(wxT("\n")) + wxString::FromUTF8(bad_text);
if (dupeErrString[0] != '\0') {
wxStringTokenizer dupes(wxString::FromUTF8(dupeErrString), wxT("|"));
wxString olddupe = dupes.GetNextToken();
wxString newdupe = dupes.GetNextToken();
if (olddupe != newdupe) {
msg += wxT("\n") + wxString::Format(_("Your QTH Details changed for this QSO.\n\nOriginally these were: %s\nNow they are:%s\n\nPlease verify that you intended to change this QSO!\n"), olddupe.c_str(), newdupe.c_str());
}
}
wxLogError(wxT("%s"), msg.c_str());
if (frame->IsQuiet()) {
switch (action) {
case TQSL_ACTION_ABORT:
aborted = true;
ignore_err = true;
goto abortSigning;
case TQSL_ACTION_NEW: // For ALL or NEW, let the signing proceed
case TQSL_ACTION_ALL:
ignore_err = true;
break;
case TQSL_ACTION_ASK:
case TQSL_ACTION_UNSPEC:
break; // The error will show as a popup
}
}
if (!ignore_err) {
tqslTrace("MyFrame::SignLogFile", "Error: %s/asking for action", S(msg));
wxString errmsg = msg + wxT("\n");
errmsg += _("Click 'Ignore' to continue signing this log while ignoring errors.");
errmsg += wxT("\n");
errmsg += _("Click 'Cancel' to abandon signing this log.");
ErrorsDialog dial(this, errmsg);
int choice = dial.ShowModal();
if (dberror || choice == TQSL_AE_CAN) {
cancelled = true;
goto abortSigning;
}
if (choice == TQSL_AE_OK) {
ignore_err = true;
if (this->IsQuiet()) {
this->Show(false);
wxSafeYield(this);
}
}
}
}
}
tqsl_getErrorString(); // Clear error
if (has_error && ignore_err)
continue;
break;
} while (1);
if (!dupes_only)
cancelled = !conv_dial->running;
abortSigning:
this->Enable(TRUE);
if (cancelled) {
wxLogWarning(_("Signing cancelled"));
numrecs = 0;
} else if (aborted && duplicates > 0 && action == TQSL_ACTION_ABORT) {
wxLogWarning(_("Duplicate QSO detected"));
numrecs = 0;
} else if (aborted) {
wxLogWarning(_("Signing aborted"));
numrecs = 0;
} else if (tQSL_Error != TQSL_NO_ERROR) {
check_tqsl_error(1);
}
delete conv_dial;
} catch(TQSLException& x) {
this->Enable(TRUE);
delete conv_dial;
string msg = x.what();
tqsl_getConverterLine(logConv, &lineno);
tqsl_converterRollBack(logConv);
tqsl_endConverter(&logConv);
unlock_db();
if (lineno) {
msg += " ";
msg += wxString::Format(_("on line %d"), lineno).ToUTF8();
}
wxLogError(_("Signing aborted due to errors"));
throw TQSLException(msg.c_str());
}
if (!cancelled && out_of_range > 0)
wxLogMessage(_("%s: %d QSOs were outside the selected date range"),
infile.c_str(), out_of_range);
if (duplicates > 0) {
if (cancelled || aborted) {
tqslTrace("SignLogFile", "rollback due to cancel = %d aborted = %d", cancelled, aborted);
tqsl_converterRollBack(logConv);
tqsl_endConverter(&logConv);
unlock_db();
}
if (aborted) {
return TQSL_EXIT_UPLOADED_ALREADY;
}
if (cancelled) {
return TQSL_EXIT_CANCEL;
}
if (action == TQSL_ACTION_ASK || action == TQSL_ACTION_UNSPEC) { // want to ask the user
DupesDialog dial(this, processed - errors - out_of_range, duplicates, action);
int choice = dial.ShowModal();
if (choice == TQSL_DP_CAN) {
wxLogMessage(_("Cancelled"));
tqsl_converterRollBack(logConv);
tqsl_endConverter(&logConv);
unlock_db();
return TQSL_EXIT_CANCEL;
}
if (choice == TQSL_DP_ALLOW) {
allow_dupes = true;
tqsl_converterRollBack(logConv);
tqsl_endConverter(&logConv);
restarting = true;
if (this->IsQuiet()) {
this->Show(false);
wxSafeYield(this);
}
goto restart;
}
} else if (action == TQSL_ACTION_ABORT) {
if (processed == duplicates) {
wxLogMessage(_("All QSOs are previously signed; aborted"));
tqsl_converterRollBack(logConv);
tqsl_endConverter(&logConv);
unlock_db();
numrecs = 0;
tqslTrace("SignLogFile", "all qsos previously signed");
return TQSL_EXIT_NO_QSOS;
} else {
wxLogMessage(_("%d of %d QSOs are previously signed; aborted"), duplicates, processed);
tqsl_converterRollBack(logConv);
tqsl_endConverter(&logConv);
unlock_db();
numrecs = 0;
tqslTrace("SignLogFile", "some qsos previously signed");
return TQSL_EXIT_NO_QSOS;
}
} else if (action == TQSL_ACTION_ALL) {
allow_dupes = true;
tqsl_converterRollBack(logConv);
tqsl_endConverter(&logConv);
restarting = true;
goto restart;
}
// Otherwise it must be TQSL_ACTION_NEW, so fall through
// and output the new records.
if (!quiet) {
wxLogMessage(_("%s: %d QSO records were previously signed"),
infile.c_str(), duplicates);
} else {
if (duplicates > 1)
tqslTrace(NULL, "%d duplicate QSO records", duplicates);
else
tqslTrace(NULL, "%s", "Duplicate QSO record");
}
}
//if (!cancelled) tqsl_converterCommit(logConv);
if (cancelled || processed == 0) {
tqslTrace("SignLogFile", "rollback due to cancel=%d processed=%d", cancelled, processed);
tqsl_converterRollBack(logConv);
tqsl_endConverter(&logConv);
}
unlock_db();
if (cancelled)
return TQSL_EXIT_CANCEL;
if (numrecs == 0)
return TQSL_EXIT_NO_QSOS;
if (aborted || duplicates > 0 || out_of_range > 0 || errors > 0)
return TQSL_EXIT_QSOS_SUPPRESSED;
return TQSL_EXIT_SUCCESS;
}
// Errors from tqsllib that indicate bad QSO data.
// Add here so they're picked up for translation.
#ifdef tqsltranslate
static const char* qsoerrs[] = {
__("Invalid contact - QSO does not specify a Callsign"),
__("Invalid contact - QSO does not specify a band or frequency"),
__("Invalid contact - QSO does not specify a mode"),
__("Invalid contact - QSO does not specify a date"),
__("Invalid contact - QSO does not specify a time"),
// Piggy-back this error as well
__("This callsign certificate is already active and cannot be restored.")
}
#endif
int
MyFrame::ConvertLogFile(tQSL_Location loc, const wxString& infile, const wxString& outfile,
int compressed, bool suppressdate, tQSL_Date* startdate, tQSL_Date* enddate, int action, int logverify, const char *password, const char *defcall) {
tqslTrace("MyFrame::ConvertLogFile", "loc=%lx, infile=%s, outfile=%s, compressed=%d, suppressdate=%d, startdate=0x%lx enddate=0x%lx action=%d, logverify=%d", reinterpret_cast<void *>(loc), S(infile), S(outfile), compressed, suppressdate, reinterpret_cast<void *>(startdate), reinterpret_cast<void*>(enddate), action, logverify);
gzFile gout = 0;
#ifdef _WIN32
int fd = -1;
#endif
FILE *out = NULL;
if (compressed == TQSL_COMPRESSED_FILE) {
#ifdef _WIN32
wchar_t* lfn = utf8_to_wchar(outfile.ToUTF8());
fd = _wopen(lfn, _O_WRONLY |_O_CREAT|_O_BINARY, _S_IREAD|_S_IWRITE);
free_wchar(lfn);
if (fd != -1)
gout = gzdopen(fd, "wb9");
#else
gout = gzopen(outfile.ToUTF8(), "wb9");
#endif
} else {
#ifdef _WIN32
wchar_t* lfn = utf8_to_wchar(outfile.ToUTF8());
out = _wfopen(lfn, L"wb");
free_wchar(lfn);
#else
out = fopen(outfile.ToUTF8(), "wb");
#endif
}
if ((compressed == TQSL_COMPRESSED_FILE && !gout) || (!compressed && !out)) {
wxLogError(_("Unable to open %s for output"), outfile.c_str());
return TQSL_EXIT_ERR_OPEN_OUTPUT;
}
int numrecs = 0;
bool comp = (compressed == TQSL_COMPRESSED_FILE);
void * writeTo = comp ? reinterpret_cast<void *> (gout) : reinterpret_cast<void *> (out);
int status = this->SignLogFile(loc, infile, writeTo, comp, numrecs, suppressdate, startdate, enddate, action, logverify, password, defcall, (compressed == TQSL_TEST_SIGNING) ? TQSL_TEST_LOG : TQSL_SIGN_LOG);
if (numrecs == 0) {
if (status != TQSL_EXIT_UPLOADED_ALREADY)
wxLogMessage(_("No records output"));
if (compressed == TQSL_COMPRESSED_FILE) {
gzclose(gout);
} else {
fclose(out);
}
#ifdef _WIN32
wchar_t* lfn = utf8_to_wchar(outfile.ToUTF8());
_wunlink(lfn);
free_wchar(lfn);
#else
unlink(outfile.ToUTF8());
#endif
if (status == TQSL_EXIT_CANCEL || status == TQSL_EXIT_QSOS_SUPPRESSED || status == TQSL_EXIT_UPLOADED_ALREADY || status == TQSL_EXIT_BUSY) {
return status;
} else {
return TQSL_EXIT_NO_QSOS;
}
} else {
if (status == TQSL_EXIT_CANCEL)
return status;
if(compressed == TQSL_COMPRESSED_FILE) {
if (gzflush(gout, Z_FINISH) != Z_OK) {
tqsl_converterRollBack(logConv);
tqsl_endConverter(&logConv);
unlock_db();
gzclose(gout);
return TQSL_EXIT_LIB_ERROR;
}
if (gzclose(gout) != Z_OK) {
tqsl_converterRollBack(logConv);
tqsl_endConverter(&logConv);
unlock_db();
return TQSL_EXIT_LIB_ERROR;
}
} else {
if (fflush(out) || fclose(out)) {
tqsl_converterRollBack(logConv);
tqsl_endConverter(&logConv);
unlock_db();
return TQSL_EXIT_LIB_ERROR;
}
}
tqsl_converterCommit(logConv);
tqsl_endConverter(&logConv);
unlock_db();
wxLogMessage(_("%s: wrote %d records to %s"), infile.c_str(), numrecs,
outfile.c_str());
if (compressed != TQSL_TEST_SIGNING) {
wxLogMessage(_("%s is ready to be emailed or uploaded."), outfile.c_str());
wxLogMessage(_("Note: TQSL assumes that this file will be uploaded to LoTW."));
wxLogMessage(_("Resubmitting these QSOs will cause them to be reported as already signed."));
wxLogMessage(wxT(""));
wxLogMessage(_("To submit the signed log file to LoTW:\n1. Move the signed log file to a computer with internet access\n2. Log in to your LoTW Web Account\n3. Select the Upload File tab\n4. Click the Choose File button, and select the signed log file you created (%s)\n5. Click the Upload file button\n\nAlternatively, you can attach the signed log file to an email message, and send the message to lotw-logs@arrl.org"), outfile.c_str());
} else {
wxLogMessage(_("Note: TQSL has not recorded signing these QSOs. You can submit this log file to LoTW or discard it."));
}
}
return status;
}
class UploadThread: public wxThread {
public:
UploadThread(CURL* handle_, wxDialog* dial_): wxThread(wxTHREAD_JOINABLE),
handle(handle_), dial(dial_) {
wxThread::Create();
}
protected:
CURL* handle;
wxDialog* dial;
virtual wxThread::ExitCode Entry() {
int ret = curl_easy_perform(handle);
wxCommandEvent evt(wxEVT_LOGUPLOAD_DONE, wxID_ANY);
dial->GetEventHandler()->AddPendingEvent(evt);
return (wxThread::ExitCode)((intptr_t)ret);
}
};
int MyFrame::UploadLogFile(tQSL_Location loc, const wxString& infile, bool compressed, bool suppressdate, tQSL_Date* startdate, tQSL_Date* enddate, int action, int logverify, const char* password, const char *defcall) {
tqslTrace("MyFrame::UploadLogFile", "infile=%s, compressed=%d, suppressdate=%d, action=%d, logverify=%d", S(infile), compressed, suppressdate, action, logverify);
int numrecs = 0;
wxString tempname;
gzFile gout = 0;
#ifdef _WIN32
int fd = -1;
tempname = wxString::FromUTF8(tQSL_BaseDir);
tempname += wxT("\\TQSLupl.tmp");
wchar_t* lfn = utf8_to_wchar(tempname.ToUTF8());
fd = _wopen(lfn, _O_WRONLY |_O_CREAT|_O_BINARY, _S_IREAD|_S_IWRITE);
free_wchar(lfn);
if (fd != -1)
gout = gzdopen(fd, "wb9");
#else
tempname = wxString::FromUTF8(tQSL_BaseDir);
tempname += wxT("/TQSLupl.tmp");
gout = gzopen(tempname.ToUTF8(), "wb9");
#endif
if (gout == NULL) {
wxLogMessage(_("Unable to open log file %hs: %hs"), tempname.c_str(), strerror(errno));
return TQSL_EXIT_LIB_ERROR;
}
tqslTrace("MyFrame::UploadLogFile", "About to convert log to temp file");
int status = this->SignLogFile(loc, infile, reinterpret_cast<void *>(gout), true, numrecs, suppressdate, startdate, enddate, action, logverify, password, defcall, TQSL_SIGN_LOG);
tqslTrace("MyFrame::UploadLogFile", "Log converted, status = %d, numrecs=%d", status, numrecs);
if (numrecs == 0) {
gzclose(gout);
#ifdef _WIN32
lfn = utf8_to_wchar(tempname.ToUTF8());
_wunlink(lfn);
free_wchar(lfn);
#else
unlink(tempname.ToUTF8());
#endif
wxLogMessage(_("No QSOs to upload"));
if (status == TQSL_EXIT_CANCEL || status == TQSL_EXIT_QSOS_SUPPRESSED)
return status;
else
return TQSL_EXIT_NO_QSOS;
} else {
if (status == TQSL_EXIT_CANCEL) {
gzclose(gout);
#ifdef _WIN32
lfn = utf8_to_wchar(tempname.ToUTF8());
_wunlink(lfn);
free_wchar(lfn);
#else
unlink(tempname.ToUTF8());
#endif
return status;
}
if (gzflush(gout, Z_FINISH) != Z_OK) {
tqsl_converterRollBack(logConv);
tqsl_endConverter(&logConv);
unlock_db();
gzclose(gout);
#ifdef _WIN32
lfn = utf8_to_wchar(tempname.ToUTF8());
_wunlink(lfn);
free_wchar(lfn);
#else
unlink(tempname.ToUTF8());
#endif
return TQSL_EXIT_LIB_ERROR;
}
if (gzclose(gout) != Z_OK) {
tqsl_converterRollBack(logConv);
tqsl_endConverter(&logConv);
unlock_db();
#ifdef _WIN32
lfn = utf8_to_wchar(tempname.ToUTF8());
_wunlink(lfn);
free_wchar(lfn);
#else
unlink(tempname.ToUTF8());
#endif
return TQSL_EXIT_LIB_ERROR;
}
tqslTrace("MyFrame::UploadLogFile", "Creating filename");
wxDateTime now = wxDateTime::Now().ToUTC();
wxString name, ext;
wxFileName::SplitPath(infile, 0, &name, &ext);
name += wxT(".tq8");
tqslTrace("MyFrame::UploadLogFile", "file=%s", S(name));
//unicode mess. can't just use mb_str directly because it's a temp ptr
// and the curl form expects it to still be there during perform() so
// we have to do all this copying around to please the unicode gods
char filename[TQSL_MAX_PATH_LEN];
strncpy(filename, wxString::Format(wxT("<TQSLUpl %s-%s> %s"),
now.Format(wxT("%Y%m%d")).c_str(),
now.Format(wxT("%H%M")).c_str(),
name.c_str()).ToUTF8(), sizeof filename);
filename[sizeof filename - 1] = '\0';
tqslTrace("MyFrame::UploadLogFile", "Upload Name=%s", filename);
wxString fileType(wxT("Log"));
tqslTrace("MyFrame::UploadLogFile", "About to call UploadFile");
int retval = UploadFile(tempname, filename, numrecs, fileType);
#ifdef _WIN32
lfn = utf8_to_wchar(tempname.ToUTF8());
_wunlink(lfn);
free_wchar(lfn);
#else
unlink(tempname.ToUTF8());
#endif
tqslTrace("MyFrame::UploadLogFile", "UploadFile returns %d", retval);
if (retval == 0) {
tqsl_converterCommit(logConv);
tqslTrace("MyFrame::UploadLogFile", "Committing");
} else {
tqslTrace("MyFrame::UploadLogFile", "Rollback");
tqsl_converterRollBack(logConv);
}
tqsl_endConverter(&logConv);
unlock_db();
tqslTrace("MyFrame::UploadLogFile", "Upload done");
return retval;
}
}
static CURL*
tqsl_curl_init(const char *logTitle, const char *url, FILE **curlLogFile, bool newFile) {
tqslTrace("tqsl_curl_init", "title=%s url=%s newFile=%d", logTitle, url, newFile);
CURL* curlReq = curl_easy_init();
if (!curlReq)
return NULL;
wxString uri = wxString::FromUTF8(url);
#if defined(_WIN32)
retry:
#endif
if (!verifyCA) {
uri.Replace(wxT("https:"), wxT("http:"));
}
wxString filename = wxString::FromUTF8(tQSL_BaseDir);
#ifdef _WIN32
filename = filename + wxT("\\curl.log");
#else
filename = filename + wxT("/curl.log");
#endif
#ifdef _WIN32
wchar_t*lfn = utf8_to_wchar(filename.ToUTF8());
*curlLogFile = _wfopen(lfn, newFile ? L"wb" : L"ab");
free_wchar(lfn);
#else
*curlLogFile = fopen(filename.ToUTF8(), newFile ? "wb" : "ab");
#endif
if (*curlLogFile) {
curl_easy_setopt(curlReq, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curlReq, CURLOPT_STDERR, *curlLogFile);
fprintf(*curlLogFile, "%s:\n", logTitle);
}
//set up options
curl_easy_setopt(curlReq, CURLOPT_URL, (const char *)uri.ToUTF8());
curl_easy_setopt(curlReq, CURLOPT_USERAGENT, "tqsl/" TQSL_VERSION);
#ifdef __WXMAC__
DocPaths docpaths(wxT("tqsl.app"));
#else
DocPaths docpaths(wxT("tqslapp"));
#endif
docpaths.Add(wxString::FromUTF8(tQSL_BaseDir));
#ifdef CONFDIR
docpaths.Add(wxT(CONFDIR));
#endif
#ifdef _WIN32
wxString exePath;
wxFileName::SplitPath(wxStandardPaths::Get().GetExecutablePath(), &exePath, 0, 0);
docpaths.Add(exePath);
#endif
wxString caBundlePath = docpaths.FindAbsoluteValidPath(wxT("ca-bundle.crt"));
if (!caBundlePath.IsEmpty()) {
char caBundle[TQSL_MAX_PATH_LEN];
strncpy(caBundle, caBundlePath.ToUTF8(), sizeof caBundle);
curl_easy_setopt(curlReq, CURLOPT_CAINFO, caBundle);
#if defined(_WIN32)
} else {
if (verifyCA) {
verifyCA = false; // Can't verify if no trusted roots
wxLogMessage(_("Unable to open ca-bundle.crt. Your TQSL installation is incomplete"));
tqslTrace("tqsl_curl_init", "Can't find ca-bundle.crt in the docpaths!");
goto retry;
}
#endif
}
// Get the proxy configuration and pass it to cURL
wxConfig *config = reinterpret_cast<wxConfig *>(wxConfig::Get());
config->SetPath(wxT("/Proxy"));
bool enabled = false;
config->Read(wxT("ProxyEnabled"), &enabled, false);
wxString pHost = config->Read(wxT("proxyHost"), wxT(""));
wxString pPort = config->Read(wxT("proxyPort"), wxT(""));
wxString pType = config->Read(wxT("proxyType"), wxT(""));
config->SetPath(wxT("/"));
if (!enabled) return curlReq; // No proxy defined
long port = strtol(pPort.ToUTF8(), NULL, 10);
if (port == 0 || pHost.IsEmpty())
return curlReq; // Invalid proxy. Ignore it.
curl_easy_setopt(curlReq, CURLOPT_PROXY, (const char *)pHost.ToUTF8());
curl_easy_setopt(curlReq, CURLOPT_PROXYPORT, port);
if (pType == wxT("HTTP")) {
curl_easy_setopt(curlReq, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); // Default is HTTP
} else if (pType == wxT("Socks4")) {
curl_easy_setopt(curlReq, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
} else if (pType == wxT("Socks5")) {
curl_easy_setopt(curlReq, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
}
return curlReq;
}
int MyFrame::UploadFile(const wxString& infile, const char* filename, int numrecs, const wxString& fileType) {
tqslTrace("MyFrame::UploadFile", "infile=%s, filename=%s, numrecs=%d, fileType=%s", S(infile), filename, numrecs, S(fileType));
//upload the file
//get the url from the config, can be overridden by an installer
//defaults are valid for LoTW as of 1/31/2013
wxConfig *config = reinterpret_cast<wxConfig *>(wxConfig::Get());
config->SetPath(wxT("/LogUpload"));
wxString uploadURL = config->Read(wxT("UploadURL"), DEFAULT_UPL_URL);
wxString uploadField = config->Read(wxT("PostField"), DEFAULT_UPL_FIELD);
wxString uplStatus = config->Read(wxT("StatusRegex"), DEFAULT_UPL_STATUSRE);
wxString uplStatusSuccess = config->Read(wxT("StatusSuccess"), DEFAULT_UPL_STATUSOK).Lower();
wxString uplMessage = config->Read(wxT("MessageRegex"), DEFAULT_UPL_MESSAGERE);
bool uplVerifyCA;
config->Read(wxT("VerifyCA"), &uplVerifyCA, DEFAULT_UPL_VERIFYCA);
config->SetPath(wxT("/"));
// Copy the strings so they remain around
char *urlstr = strdup(uploadURL.ToUTF8());
char *cpUF = strdup(uploadField.ToUTF8());
retry_upload:
curlReq = tqsl_curl_init("Upload Log", urlstr, &curlLogFile, true);
if (!curlReq) {
wxLogMessage(_("Error: Could not upload file (CURL Init error)"));
free(urlstr);
free(cpUF);
return TQSL_EXIT_TQSL_ERROR;
}
//the following allow us to write our log and read the result
FileUploadHandler handler;
curl_easy_setopt(curlReq, CURLOPT_WRITEFUNCTION, &FileUploadHandler::recv);
curl_easy_setopt(curlReq, CURLOPT_WRITEDATA, &handler);
curl_easy_setopt(curlReq, CURLOPT_SSL_VERIFYPEER, uplVerifyCA);
char errorbuf[CURL_ERROR_SIZE];
errorbuf[0] = '\0';
curl_easy_setopt(curlReq, CURLOPT_ERRORBUFFER, errorbuf);
curl_mime *mime;
curl_mimepart *part;
mime = curl_mime_init(curlReq);
part = curl_mime_addpart(mime);
curl_mime_filedata(part, infile.ToUTF8());
curl_mime_name(part, cpUF);
curl_mime_filename(part, filename);
curl_easy_setopt(curlReq, CURLOPT_MIMEPOST, mime);
intptr_t retval;
UploadDialog* upload = NULL;
if (numrecs > 0) {
if (numrecs == 1) {
wxLogMessage(_("Attempting to upload one QSO"));
} else {
wxLogMessage(_("Attempting to upload %d QSOs"), numrecs);
}
} else {
wxLogMessage(_("Attempting to upload %s"), fileType.c_str());
}
if (frame && !quiet) {
if (fileType == wxT("Log")) {
upload = new UploadDialog(this);
} else {
upload = new UploadDialog(this, wxString(_("Uploading Callsign Certificate")), wxString(_("Uploading Callsign Certificate Request...")));
}
curl_easy_setopt(curlReq, CURLOPT_XFERINFOFUNCTION, &UploadDialog::UpdateProgress);
curl_easy_setopt(curlReq, CURLOPT_XFERINFODATA, upload);
curl_easy_setopt(curlReq, CURLOPT_NOPROGRESS, 0);
UploadThread thread(curlReq, upload);
if (thread.Run() != wxTHREAD_NO_ERROR) {
wxLogError(_("Could not spawn upload thread!"));
upload->Destroy();
free(urlstr);
free(cpUF);
if (curlLogFile) {
fclose(curlLogFile);
curlLogFile = NULL;
}
return TQSL_EXIT_TQSL_ERROR;
}
upload->ShowModal();
retval = ((intptr_t)thread.Wait());
} else {
retval = curl_easy_perform(curlReq);
}
if (retval == 0) { //success
//check the result
wxString uplresult = wxString::FromAscii(handler.s.c_str());
wxRegEx uplStatusRE(uplStatus);
wxRegEx uplMessageRE(uplMessage);
wxRegEx stripSpacesRE(wxT("\\n +"), wxRE_ADVANCED);
wxRegEx certUploadRE(wxT("(Started processing.*)\\n(.*Your certificate request contains error.*)"), wxRE_ADVANCED);
if (uplStatusRE.Matches(uplresult)) { //we can make sense of the error
//sometimes has leading/trailing spaces
if (uplStatusRE.GetMatch(uplresult, 1).Lower().Trim(true).Trim(false) == uplStatusSuccess) { //success
if (uplMessageRE.Matches(uplresult)) { //and a message
wxString lotwmessage = uplMessageRE.GetMatch(uplresult, 1).Trim(true).Trim(false);
stripSpacesRE.ReplaceAll(&lotwmessage, wxString(wxT("\n")));
if (fileType == wxT("Log")) {
wxLogMessage(_("%s: Log uploaded successfully with result:\n\n%s"),
infile.c_str(), lotwmessage.c_str());
wxLogMessage(_("After reading this message, you may close this program."));
retval = TQSL_EXIT_SUCCESS;
} else {
// Split the log message when there is an error detected
if (certUploadRE.Matches(lotwmessage)) {
wxString log = certUploadRE.GetMatch(lotwmessage, 1).Trim(true).Trim(false);
wxString err = certUploadRE.GetMatch(lotwmessage, 2).Trim(true).Trim(false);
wxLogMessage(_("%s uploaded with result:\n\n%s"),
fileType.c_str(), log.c_str());
wxMessageBox(wxString::Format(_("%s Uploaded with result:\n\n%s"), fileType.c_str(), err.c_str()), _("Error"), wxOK | wxICON_EXCLAMATION);
retval = TQSL_EXIT_REJECTED;
} else {
wxLogMessage(_("%s uploaded with result:\n\n%s"),
fileType.c_str(), lotwmessage.c_str());
retval = TQSL_EXIT_SUCCESS;
}
}
} else { // no message we could find
if (fileType == wxT("Log")) {
wxLogMessage(_("%s: Log uploaded successfully"), infile.c_str());
wxLogMessage(_("After reading this message, you may close this program."));
} else {
wxLogMessage(_("%s uploaded successfully"), fileType.c_str());
}
retval = TQSL_EXIT_SUCCESS;
}
} else { // failure, but site is working
if (uplMessageRE.Matches(uplresult)) { //and a message
wxLogMessage(_("%s: %s upload was rejected with result \"%s\""),
infile.c_str(), fileType.c_str(), uplMessageRE.GetMatch(uplresult, 1).c_str());
} else { // no message we could find
wxLogMessage(_("%s: %s upload was rejected"), infile.c_str(), fileType.c_str());
}
retval = TQSL_EXIT_REJECTED;
}
} else { //site isn't working
wxLogMessage(_("%s: Got an unexpected response on %s upload!\n%s\nMaybe the site is down?"), infile.c_str(), fileType.c_str(), uplresult.c_str());
retval = TQSL_EXIT_UNEXP_RESP;
}
} else {
tqslTrace("MyFrame::UploadFile", "cURL Error: %s (%s)\n", curl_easy_strerror((CURLcode)retval), errorbuf);
if ((retval == CURLE_PEER_FAILED_VERIFICATION) && verifyCA) {
tqslTrace("MyFrame::UploadFile", "cURL SSL Certificate error - disabling verify and retry");
verifyCA = false;
goto retry_upload;
}
if (retval == CURLE_COULDNT_RESOLVE_HOST || retval == CURLE_COULDNT_CONNECT) {
wxLogMessage(_("%s: Unable to upload - either your Internet connection is down or LoTW is unreachable."), infile.c_str());
wxLogMessage(_("Please try uploading the %s later."), fileType.c_str());
retval = TQSL_EXIT_CONNECTION_FAILED;
} else if (retval == CURLE_WRITE_ERROR || retval == CURLE_SEND_ERROR || retval == CURLE_RECV_ERROR) {
wxLogMessage(_("%s: Unable to upload. The network is down or the LoTW site is too busy."), infile.c_str());
wxLogMessage(_("Please try uploading the %s later."), fileType.c_str());
retval = TQSL_EXIT_CONNECTION_FAILED;
} else if (retval == CURLE_SSL_CONNECT_ERROR || retval == CURLE_OPERATION_TIMEDOUT) {
wxLogMessage(_("%s: Unable to connect to the upload site."), infile.c_str());
wxLogMessage(_("Please try uploading the %s later."), fileType.c_str());
retval = TQSL_EXIT_CONNECTION_FAILED;
} else if (retval == CURLE_ABORTED_BY_CALLBACK) { //cancelled.
wxLogMessage(_("%s: Upload cancelled"), infile.c_str());
retval = TQSL_EXIT_CANCEL;
} else { //error
//don't know why the conversion from char* -> wxString -> char* is necessary but it
// was turned into garbage otherwise
wxLogMessage(_("%s: Couldn't upload the file: CURL returned \"%hs\" (%hs)"), infile.c_str(), curl_easy_strerror((CURLcode)retval), errorbuf);
retval = TQSL_EXIT_TQSL_ERROR;
}
}
if (frame && upload) upload->Destroy();
curl_mime_free(mime);
curl_easy_cleanup(curlReq);
curlReq = NULL;
// If there's a GUI and we didn't successfully upload and weren't cancelled,
// ask the user if we should retry the upload.
if ((frame && !quiet) && retval != TQSL_EXIT_CANCEL && retval != TQSL_EXIT_SUCCESS && retval != TQSL_EXIT_REJECTED) {
if (wxMessageBox(_("Your upload appears to have failed. Should TQSL try again?"), _("Retry?"), wxYES_NO | wxICON_QUESTION, this) == wxYES)
goto retry_upload;
}
if (urlstr) free(urlstr);
if (cpUF) free (cpUF);
if (curlLogFile) {
fclose(curlLogFile);
curlLogFile = NULL;
}
return retval;
}
// Verify that a certificate exists for this station location
// before allowing the location to be edited
static bool verify_cert(tQSL_Location loc, bool editing) {
tqslTrace("verify_cert", "loc=%lx, editing=%d", reinterpret_cast<void *>(loc), editing);
char call[128];
tQSL_Cert *certlist;
int ncerts;
// Get the callsign from the location
check_tqsl_error(tqsl_getLocationCallSign(loc, call, sizeof(call)));
// See if there is a certificate for that call
int flags = 0;
if (editing)
flags = TQSL_SELECT_CERT_WITHKEYS | TQSL_SELECT_CERT_EXPIRED;
tqsl_selectCertificates(&certlist, &ncerts, call, 0, 0, 0, flags);
if (ncerts == 0 && strcmp(call, "NONE") && strcmp(call, "[None]")) {
if (editing) {
wxMessageBox(wxString::Format(_("There are no callsign certificates for callsign %hs. This station location cannot be edited."), call), _("No Certificate"), wxOK | wxICON_EXCLAMATION);
} else {
wxMessageBox(wxString::Format(_("There are no current callsign certificates for callsign %hs. This station location cannot be used to sign a log file."), call), _("No Certificate"), wxOK | wxICON_EXCLAMATION);
}
return false;
}
if (editing) {
tqsl_freeCertificateList(certlist, ncerts);
return true;
}
if (!strcmp(call, "NONE") || !strcmp(call, "[None]")) {
return true;
}
// check if there's a good cert right now for this call
bool goodCert = false;
long serial = 0;
wxString status;
wxString errString;
for (int i = 0; i < ncerts; i++) {
tqsl_getCertificateSerial(certlist[i], &serial);
tqsl_checkCertStatus(serial, status);
if (status == wxT("Bad serial")) {
errString = wxString::Format(_("There are no current callsign certificates for callsign %hs. This station location cannot be used to sign a log file."), call);
} else if (status == wxT("Superceded")) {
errString = wxString::Format(_("There is a newer callsign certificate for callsign %hs. This station location cannot be used to sign a log file until the new certificate is installed."), call);
} else if (status == wxT("Expired")) {
errString = wxString::Format(_("The callsign certificate for callsign %hs has expired. This station location cannot be used to sign a log file until a valid callsign certificate is installed."), call);
} else if (status == wxT("Unrevoked")) {
goodCert = true;
break;
} else { // Can't tell - no network?
goodCert = true;
break;
}
}
if (!goodCert) {
wxMessageBox(errString, _("No Certificate"), wxOK | wxICON_EXCLAMATION);
}
tqsl_freeCertificateList(certlist, ncerts);
return goodCert;
}
tQSL_Location
MyFrame::SelectStationLocation(const wxString& title, const wxString& okLabel, bool editonly) {
tqslTrace("MyFrame::SelectStationLocation", "title=%s, okLabel=%s, editonly=%d", S(title), S(okLabel), editonly);
int rval;
tQSL_Location loc;
wxString selname;
char errbuf[512];
do {
TQSLGetStationNameDialog station_dial(this, help, wxDefaultPosition, false, title, okLabel, editonly);
if (!selname.IsEmpty())
station_dial.SelectName(selname);
rval = station_dial.ShowModal();
switch (rval) {
case wxID_CANCEL: // User hit Close
return 0;
case wxID_APPLY: // User hit New
try {
check_tqsl_error(tqsl_initStationLocationCapture(&loc));
selname = run_station_wizard(this, loc, help, false, false);
check_tqsl_error(tqsl_endStationLocationCapture(&loc));
frame->loc_tree->Build();
break;
}
catch(TQSLException& x) {
wxLogError(wxT("%s"), x.what());
return 0;
}
case wxID_MORE: // User hit Edit
try {
check_tqsl_error(tqsl_getStationLocation(&loc, station_dial.Selected().ToUTF8()));
if (verify_cert(loc, true)) { // Check if there is a certificate before editing
check_tqsl_error(tqsl_getStationLocationErrors(loc, errbuf, sizeof(errbuf)));
if (strlen(errbuf) > 0) {
wxString fmt = wxT("%hs\n");
fmt += _("The invalid data was ignored.");
wxMessageBox(wxString::Format(fmt, errbuf), _("Station Location data error"), wxOK | wxICON_EXCLAMATION, this);
}
char loccall[512];
check_tqsl_error(tqsl_getLocationCallSign(loc, loccall, sizeof loccall));
selname = run_station_wizard(this, loc, help, true, true, wxString::Format(_("Edit Station Location : %hs - %s"), loccall, station_dial.Selected().c_str()), station_dial.Selected());
check_tqsl_error(tqsl_endStationLocationCapture(&loc));
}
break;
}
catch(TQSLException& x) {
wxLogError(wxT("%hs"), x.what());
return 0;
}
case wxID_OK: // User hit OK
try {
check_tqsl_error(tqsl_getStationLocation(&loc, station_dial.Selected().ToUTF8()));
check_tqsl_error(tqsl_getStationLocationErrors(loc, errbuf, sizeof(errbuf)));
if (strlen(errbuf) > 0) {
wxString fmt = wxT("%hs\n");
fmt += _("This should be corrected before signing a log file.");
wxMessageBox(wxString::Format(fmt, errbuf), _("Station Location data error"), wxOK | wxICON_EXCLAMATION, this);
}
break;
}
catch(TQSLException& x) {
wxLogError(wxT("%hs"), x.what());
return 0;
}
}
} while (rval != wxID_OK);
return loc;
}
void MyFrame::CheckForUpdates(wxCommandEvent&) {
tqslTrace("MyFrame::CheckForUpdates", NULL);
DoUpdateCheck(false, false);
}
wxString GetUpdatePlatformString() {
tqslTrace("GetUpdatePlatformString", NULL);
wxString ret;
#if defined(_WIN32)
#if defined(_WIN64)
//this is 64 bit code already; if we are running we support 64
ret = wxT("win64 win32");
#else // this is not 64 bit code, but we are on windows
// are we 64-bit compatible? if so prefer it
BOOL val = false;
typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
LPFN_ISWOW64PROCESS fnIsWow64Process =
(LPFN_ISWOW64PROCESS) GetProcAddress(GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
if (fnIsWow64Process != NULL) {
fnIsWow64Process(GetCurrentProcess(), &val);
}
if(val) //32 bit running on 64 bit
ret = wxT("win64 win32");
else //just 32-bit only
ret = wxT("win32");
#endif
#elif defined(__APPLE__) && defined(__MACH__) //osx has universal binaries
ret = wxT("osx");
#elif defined(__gnu_linux__)
#if defined(__amd64__)
ret = wxT("linux64 linux32 source");
#elif defined(__i386__)
ret = wxT("linux32 source");
#else
ret = wxT("source"); //source distribution is kosher on linux
#endif
#else
ret = wxT(""); //give them a homepage
#endif
return ret;
}
// Class for encapsulating version information
class revLevel {
public:
explicit revLevel(long _major = 0, long _minor = 0, long _patch = 0) {
major = _major;
minor = _minor;
patch = _patch;
}
explicit revLevel(const wxString& _value) {
wxString str = _value;
str.Trim(true);
str.Trim(false);
wxStringTokenizer vers(str, wxT("."));
wxString majorVer = vers.GetNextToken();
wxString minorVer = vers.GetNextToken();
wxString patchVer = vers.GetNextToken();
if (majorVer.IsNumber()) {
majorVer.ToLong(&major);
} else {
major = -1;
}
if (minorVer.IsNumber()) {
minorVer.ToLong(&minor);
} else {
minor = -1;
}
if (!patchVer.IsEmpty() && patchVer.IsNumber()) {
patchVer.ToLong(&patch);
} else {
patch = 0;
}
}
wxString Value(void) {
if (patch > 0)
return wxString::Format(wxT("%ld.%ld.%ld"), major, minor, patch);
else
return wxString::Format(wxT("%ld.%ld"), major, minor);
}
long major;
long minor;
long patch;
bool operator >(const revLevel& other) {
if (major > other.major) return true;
if (major == other.major) {
if (minor > other.minor) return true;
if (minor == other.minor) {
if (patch > other.patch) return true;
}
}
return false;
}
bool operator >=(const revLevel& other) {
if (major > other.major) return true;
if (major == other.major) {
if (minor > other.minor) return true;
if (minor == other.minor) {
if (patch >= other.patch) return true;
}
}
return false;
}
};
class revInfo {
public:
explicit revInfo(bool _noGUI = false, bool _silent = false) {
noGUI = _noGUI;
silent = _silent;
error = false;
message = false;
programRev = newProgramRev = NULL;
configRev = newConfigRev = NULL;
mutex = new wxMutex;
condition = new wxCondition(*mutex);
mutex->Lock();
}
~revInfo() {
if (programRev)
delete programRev;
if (newProgramRev)
delete newProgramRev;
if (configRev)
delete configRev;
if (newConfigRev)
delete newConfigRev;
if (condition)
delete condition;
if (mutex)
delete mutex;
}
bool error;
bool message;
bool noGUI;
bool silent;
revLevel* programRev;
revLevel* newProgramRev;
revLevel* configRev;
revLevel* newConfigRev;
bool newProgram;
bool newConfig;
wxString errorText;
wxString homepage;
wxString url;
wxMutex* mutex;
wxCondition* condition;
};
class UpdateDialogMsgBox: public wxDialog {
public:
UpdateDialogMsgBox(wxWindow* parent, bool newProg, bool newConfig, revLevel* currentProgRev, revLevel* newProgRev,
revLevel* currentConfigRev, revLevel* newConfigRev, wxString platformURL, wxString homepage)
: wxDialog(parent, (wxWindowID)wxID_ANY, _("TQSL Update Available"), wxDefaultPosition, wxDefaultSize) {
tqslTrace("UpdateDialogMsgBox::UpdateDialogMsgBox", "parent=%lx, newProg=%d, newConfig=%d, currentProgRev %s, newProgRev %s, currentConfigRev %s, newConfigRev=%s, platformURL=%s, homepage=%s", reinterpret_cast<void *>(parent), newProg, newConfig, S(currentProgRev->Value()), S(newProgRev->Value()), S(currentConfigRev->Value()), S(newConfigRev->Value()), S(platformURL), S(homepage));
wxSizer* overall = new wxBoxSizer(wxVERTICAL);
long flags = wxOK;
#ifndef _WIN32
if (newConfig)
#endif
flags |= wxCANCEL;
wxSizer* buttons = CreateButtonSizer(flags);
wxString notice;
if (newProg)
notice = wxString::Format(_("A new TQSL release (V%s) is available!"), newProgRev->Value().c_str());
else if (newConfig)
notice = wxString::Format(_("An updated TrustedQSL configuration file (V%s) is available!\nThe configuration file installs definitions for entities, modes, etc."), newConfigRev->Value().c_str());
overall->Add(new wxStaticText(this, wxID_ANY, notice), 0, wxALIGN_CENTER_HORIZONTAL);
if (newProg) {
#ifndef _WIN32
if (!platformURL.IsEmpty()) {
wxSizer* thisline = new wxBoxSizer(wxHORIZONTAL);
thisline->Add(new wxStaticText(this, wxID_ANY, _("Download from:")));
thisline->Add(new wxHyperlinkCtrl(this, wxID_ANY, platformURL, platformURL));
overall->AddSpacer(10);
overall->Add(thisline);
}
if (!homepage.IsEmpty()) {
wxSizer* thisline = new wxBoxSizer(wxHORIZONTAL);
thisline->Add(new wxStaticText(this, wxID_ANY, _("More details at:")));
thisline->Add(new wxHyperlinkCtrl(this, wxID_ANY, homepage, homepage));
overall->AddSpacer(10);
overall->Add(thisline);
}
#else
overall->AddSpacer(10);
overall->Add(new wxStaticText(this, wxID_ANY, _("Click 'OK' to install the new version of TQSL, or Cancel to ignore it.")));
#endif
}
if (newConfig) {
overall->AddSpacer(10);
overall->Add(new wxStaticText(this, wxID_ANY, _("Click 'OK' to install the new configuration file, or Cancel to ignore it.")));
}
if (buttons) { //should always be here but documentation says to check
overall->AddSpacer(10);
overall->Add(buttons, 0, wxALIGN_CENTER_HORIZONTAL);
}
#if wxMAJOR_VERSION > 2 || (wxMAJOR_VERSION == 2 && wxMINOR_VERSION == 9)
SetSizerAndFit(overall);
#else
wxSizer* padding = new wxBoxSizer(wxVERTICAL);
padding->Add(overall, 0, wxALL, 10);
SetSizer(padding);
Fit();
#endif
}
private:
};
static size_t file_recv(void *ptr, size_t size, size_t nmemb, void *stream) {
size_t left = nmemb * size;
size_t written;
while (left > 0) {
written = fwrite(ptr, size, nmemb, reinterpret_cast <FILE *>(stream));
if (written == 0)
return 0;
left -= (written * size);
}
wxSafeYield(frame);
return nmemb * size;
}
void MyFrame::UpdateConfigFile() {
tqslTrace("MyFrame::UpdateConfigFile", NULL);
wxConfig* config = reinterpret_cast<wxConfig *>(wxConfig::Get());
wxString newConfigURL = config->Read(wxT("NewConfigURL"), DEFAULT_CONFIG_FILE_URL);
retry:
curlReq = tqsl_curl_init("Config File Download Log", (const char *)newConfigURL.ToUTF8(), &curlLogFile, false);
wxString filename = wxString::FromUTF8(tQSL_BaseDir);
#ifdef _WIN32
filename = filename + wxT("\\config.tq6");
wchar_t* lfn = utf8_to_wchar(filename.ToUTF8());
FILE *configFile = _wfopen(lfn, L"wb");
free_wchar(lfn);
#else
filename = filename + wxT("/config.tq6");
FILE *configFile = fopen(filename.ToUTF8(), "wb");
#endif
if (!configFile) {
tqslTrace("UpdateConfigFile", "Can't open new file %s: %s", static_cast<const char *>(filename.ToUTF8()), strerror(errno));
wxMessageBox(wxString::Format(_("Can't open new configuration file %s: %hs"), filename.c_str(), strerror(errno)), _("Error"), wxOK | wxICON_ERROR, this);
return;
}
curl_easy_setopt(curlReq, CURLOPT_WRITEFUNCTION, &file_recv);
curl_easy_setopt(curlReq, CURLOPT_WRITEDATA, configFile);
curl_easy_setopt(curlReq, CURLOPT_CONNECTTIMEOUT, 60);
curl_easy_setopt(curlReq, CURLOPT_FAILONERROR, 1); //let us find out about a server issue
char errorbuf[CURL_ERROR_SIZE];
curl_easy_setopt(curlReq, CURLOPT_ERRORBUFFER, errorbuf);
int retval = curl_easy_perform(curlReq);
if (retval == CURLE_OK) {
if (fclose(configFile)) {
tqslTrace("UpdateConfigFile", "Error writing new file %s: %s", static_cast<const char *>(filename.ToUTF8()), strerror(errno));
wxMessageBox(wxString::Format(_("Error writing new configuration file %s: %hs"), filename.c_str(), strerror(errno)), _("Error"), wxOK | wxICON_ERROR, this);
return;
}
notifyData nd;
if (tqsl_importTQSLFile(filename.ToUTF8(), notifyImport, &nd)) {
wxLogError(getLocalizedErrorString());
} else {
tqslTrace("UpdateConfigFile", "Config update success");
wxMessageBox(_("Configuration file successfully updated"), _("Update Completed"), wxOK | wxICON_INFORMATION, this);
// Reload the DXCC mapping
DXCC dx;
dx.reset();
}
} else {
tqslTrace("MyFrame::UpdateConfigFile", "cURL Error during config file download: %s (%s)\n", curl_easy_strerror((CURLcode)retval), errorbuf);
if ((retval == CURLE_PEER_FAILED_VERIFICATION) && verifyCA) {
tqslTrace("MyFrame::UpdateConfigFile", "cURL SSL Certificate error - disabling verify and retry");
verifyCA = false;
goto retry;
}
if (curlLogFile) {
fprintf(curlLogFile, "cURL Error during config file download: %s (%s)\n", curl_easy_strerror((CURLcode)retval), errorbuf);
}
if (retval == CURLE_COULDNT_RESOLVE_HOST || retval == CURLE_COULDNT_CONNECT) {
wxLogMessage(_("Unable to update - either your Internet connection is down or LoTW is unreachable."));
wxLogMessage(_("Please try again later."));
} else if (retval == CURLE_WRITE_ERROR || retval == CURLE_SEND_ERROR || retval == CURLE_RECV_ERROR) {
wxLogMessage(_("Unable to update. The network is down or the LoTW site is too busy."));
wxLogMessage(_("Please try again later."));
} else if (retval == CURLE_SSL_CONNECT_ERROR) {
wxLogMessage(_("Unable to connect to the update site."));
wxLogMessage(_("Please try again later."));
} else { // some other error
wxString fmt = _("Error downloading new configuration file:");
fmt += wxT("\n%hs");
wxMessageBox(wxString::Format(fmt, errorbuf), _("Update"), wxOK | wxICON_EXCLAMATION, this);
}
}
}
#if defined(_WIN32) || defined(__APPLE__)
void MyFrame::UpdateTQSL(wxString& url) {
tqslTrace("MyFrame::UpdateTQSL", "url=%s", S(url));
retry:
bool needToCleanUp = false;
if (curlReq) {
curl_easy_setopt(curlReq, CURLOPT_URL, (const char *)url.ToUTF8());
} else {
curlReq = tqsl_curl_init("TQSL Update Download Log", (const char *)url.ToUTF8(), &curlLogFile, false);
needToCleanUp = true;
}
wxString filename = wxString::FromUTF8(tQSL_BaseDir);
#ifdef _WIN32
filename = filename + wxT("\\tqslupdate.msi");
wchar_t* lfn = utf8_to_wchar(filename.ToUTF8());
FILE *updateFile = _wfopen(lfn, L"wb");
free_wchar(lfn);
#else
filename = filename + wxT("/tqslupdate.pkg");
FILE *updateFile = fopen(filename.ToUTF8(), "wb");
#endif
if (!updateFile) {
tqslTrace("UpdateTQSL", "Can't open new file %s: %s", static_cast<const char *>(filename.ToUTF8()), strerror(errno));
wxMessageBox(wxString::Format(_("Can't open TQSL update file %s: %hs"), filename.c_str(), strerror(errno)), _("Error"), wxOK | wxICON_ERROR, this);
return;
}
curl_easy_setopt(curlReq, CURLOPT_WRITEFUNCTION, &file_recv);
curl_easy_setopt(curlReq, CURLOPT_WRITEDATA, updateFile);
curl_easy_setopt(curlReq, CURLOPT_CONNECTTIMEOUT, 60);
curl_easy_setopt(curlReq, CURLOPT_FAILONERROR, 1); //let us find out about a server issue
char errorbuf[CURL_ERROR_SIZE];
curl_easy_setopt(curlReq, CURLOPT_ERRORBUFFER, errorbuf);
int retval = curl_easy_perform(curlReq);
if (retval == CURLE_OK) {
if (fclose(updateFile)) {
tqslTrace("UpdateTQSL", "Error writing new file %s: %s", static_cast<const char *>(filename.ToUTF8()), strerror(errno));
wxMessageBox(wxString::Format(_("Error writing new configuration file %s: %hs"), filename.c_str(), strerror(errno)), _("Error"), wxOK | wxICON_ERROR, this);
return;
}
#ifdef _WIN32
tqslTrace("MyFrame::UpdateTQSL", "Executing msiexec \"%s\"", filename.ToUTF8());
wxExecute(wxString::Format(wxT("msiexec /i \"%s\""), filename), wxEXEC_ASYNC);
#else
tqslTrace("MyFrame::UpdateTQSL", "Executing installer");
wxExecute(wxString::Format(wxT("open \"%s\""), filename.c_str()), wxEXEC_ASYNC);
#endif
tqslTrace("MyFrame::UpdateTQSL", "GUI Destroy");
wxExit();
exit(0);
} else {
tqslTrace("MyFrame::UpdateTQSL", "cURL Error during file download: %s (%s)\n", curl_easy_strerror((CURLcode)retval), errorbuf);
if ((retval == CURLE_PEER_FAILED_VERIFICATION) && verifyCA) {
tqslTrace("MyFrame::UpdateTQSL", "cURL SSL Certificate error - disabling verify and retry");
verifyCA = false;
goto retry;
}
if (curlLogFile) {
fprintf(curlLogFile, "cURL Error during file download: %s (%s)\n", curl_easy_strerror((CURLcode)retval), errorbuf);
}
if (retval == CURLE_COULDNT_RESOLVE_HOST || retval == CURLE_COULDNT_CONNECT) {
wxLogMessage(_("Unable to update - either your Internet connection is down or LoTW is unreachable."));
wxLogMessage(_("Please try again later."));
} else if (retval == CURLE_WRITE_ERROR || retval == CURLE_SEND_ERROR || retval == CURLE_RECV_ERROR) {
wxLogMessage(_("Unable to update. The network is down or the LoTW site is too busy."));
wxLogMessage(_("Please try again later."));
} else if (retval == CURLE_SSL_CONNECT_ERROR) {
wxLogMessage(_("Unable to connect to the update site."));
wxLogMessage(_("Please try again later."));
} else { // some other error
wxString fmt = _("Error downloading new file:");
fmt += wxT("\n%hs");
wxMessageBox(wxString::Format(fmt, errorbuf), _("Update"), wxOK | wxICON_EXCLAMATION, this);
}
}
if (needToCleanUp) {
curl_easy_cleanup(curlReq);
curlReq = NULL;
}
}
#endif /* _WIN32 || __APPLE__ */
// Check if a certificate is still valid and current at LoTW
bool
tqsl_checkCertStatus(long serial, wxString& result) {
tqslTrace("tqsl_checkCertStatus()", "Serial=%ld", serial);
wxConfig* config = reinterpret_cast<wxConfig *>(wxConfig::Get());
wxString certCheckURL = config->Read(wxT("CertCheckURL"), DEFAULT_CERT_CHECK_URL);
wxString certCheckRE = config->Read(wxT("StatusRegex"), DEFAULT_CERT_CHECK_RE);
certCheckURL = certCheckURL + wxString::Format(wxT("%ld"), serial);
bool needToCleanUp = false;
if (!verifyCA) {
certCheckURL.Replace(wxT("https:"), wxT("http:"));
}
if (curlReq == NULL) {
needToCleanUp = true;
curlReq = tqsl_curl_init("checkCert", certCheckURL.ToUTF8(), &curlLogFile, false);
} else {
curl_easy_setopt(curlReq, CURLOPT_URL, (const char *)certCheckURL.ToUTF8());
}
FileUploadHandler handler;
curl_easy_setopt(curlReq, CURLOPT_WRITEFUNCTION, &FileUploadHandler::recv);
curl_easy_setopt(curlReq, CURLOPT_WRITEDATA, &handler);
curl_easy_setopt(curlReq, CURLOPT_CONNECTTIMEOUT, 60);
curl_easy_setopt(curlReq, CURLOPT_FAILONERROR, 1); //let us find out about a server issue
char errorbuf[CURL_ERROR_SIZE];
errorbuf[0] = '\0';
curl_easy_setopt(curlReq, CURLOPT_ERRORBUFFER, errorbuf);
int retval = curl_easy_perform(curlReq);
result = wxString(wxT("Unknown"));
bool ret = false;
if (retval == CURLE_OK) {
wxString checkresult = wxString::FromAscii(handler.s.c_str());
wxRegEx checkStatusRE(certCheckRE);
if (checkStatusRE.Matches(checkresult)) { // valid response
result = checkStatusRE.GetMatch(checkresult, 1).Trim(true).Trim(false);
ret = true;
}
} else {
tqslTrace("tqsl_checkCertStatus", "cURL Error during cert status check: %s (%s)\n", curl_easy_strerror((CURLcode)retval), errorbuf);
if (curlLogFile) {
fprintf(curlLogFile, "cURL Error during cert status check: %s (%s)\n", curl_easy_strerror((CURLcode)retval), errorbuf);
}
if ((retval == CURLE_PEER_FAILED_VERIFICATION) && verifyCA) {
verifyCA = false;
}
}
if (needToCleanUp) {
curl_easy_cleanup(curlReq);
curlReq = NULL;
}
return ret;
}
class expInfo {
public:
explicit expInfo(bool _noGUI = false) {
noGUI = _noGUI;
days = 0;
callsign = NULL;
error = false;
mutex = new wxMutex;
condition = new wxCondition(*mutex);
mutex->Lock();
}
bool noGUI;
int days;
tQSL_Cert cert;
char* callsign;
bool error;
wxString errorText;
wxMutex* mutex;
wxCondition* condition;
~expInfo() {
if (callsign) free(callsign);
if (condition) delete condition;
if (mutex) delete mutex;
}
};
// Report an error back to the main thread
static void
report_error(expInfo **eip) {
expInfo *ei = *eip;
ei->error = true;
ei->errorText = getLocalizedErrorString();
// Send the result back to the main thread
wxCommandEvent* event = new wxCommandEvent(wxEVT_COMMAND_MENU_SELECTED, bg_expiring);
event->SetClientData(ei);
wxPostEvent(frame, *event);
ei->condition->Wait(); // stalls here until the main thread resumes the thread
ei->mutex->Unlock();
delete ei;
delete event;
*eip = new expInfo;
}
// Check for certificates expiring in the next nn (default 60) days
void
MyFrame::DoCheckExpiringCerts(bool noGUI) {
tQSL_Cert *clist;
int nc;
tqsl_selectCertificates(&clist, &nc, 0, 0, 0, 0, TQSL_SELECT_CERT_WITHKEYS | TQSL_SELECT_CERT_EXPIRED);
if (nc == 0) return;
expInfo *ei = new expInfo;
ei->noGUI = noGUI;
bool needToCleanUp = false;
if (curlReq == NULL) {
needToCleanUp = true;
curlReq = tqsl_curl_init("Certificate Check Log", "https://lotw.arrl.org", &curlLogFile, false);
}
long expireDays = DEFAULT_CERT_WARNING;
wxConfig::Get()->Read(wxT("CertWarnDays"), &expireDays);
// Get today's date
struct tm *tm;
#ifdef _WIN32
__time32_t t = _time32(0);
tm = _gmtime32(&t);
#else
time_t t = time(0);
struct tm tmr;
tm = &tmr;
tm = gmtime_r(&t, tm);
#endif
tQSL_Date d;
d.year = tm->tm_year + 1900;
d.month = tm->tm_mon + 1;
d.day = tm->tm_mday;
tQSL_Date exp;
bool networkError = false;
for (int i = 0; i < nc; i ++) {
ei->cert = clist[i];
char callsign[64];
if (tqsl_getCertificateCallSign(clist[i], callsign, sizeof callsign)) {
report_error(&ei);
continue;
}
wxString county;
wxString grid;
// Get the user detail info for this callsign from the ARRL server
int dxcc;
if (tqsl_getCertificateDXCCEntity(clist[i], &dxcc)) {
report_error(&ei);
continue;
}
// Don't keep retrying when multiple certs
if (!networkError) {
if (SaveAddressInfo(callsign, dxcc) < 0) {
networkError = true;
}
}
int expired = 0;
tqsl_isCertificateExpired(clist[i], &expired);
if (expired) continue; // Don't check expired already
int keyonly, pending;
keyonly = pending = 0;
if (tqsl_getCertificateKeyOnly(clist[i], &keyonly)) {
report_error(&ei);
continue;
}
long serial = 0;
wxString status = wxString(wxT("KeyOnly"));
if (!keyonly) {
if (tqsl_getCertificateSerial(clist[i], &serial)) {
report_error(&ei);
continue;
}
tqsl_checkCertStatus(serial, status);
if (tqsl_setCertificateStatus(serial, (const char *)status.ToUTF8())) {
report_error(&ei);
continue;
}
}
wxString reqPending = wxConfig::Get()->Read(wxT("RequestPending"));
wxStringTokenizer tkz(reqPending, wxT(","));
while (tkz.HasMoreTokens()) {
wxString pend = tkz.GetNextToken();
if (pend == wxString::FromUTF8(callsign)) {
pending = true;
break;
}
}
if (keyonly || pending)
continue;
if (0 == tqsl_getCertificateNotAfterDate(clist[i], &exp)) {
int days_left;
tqsl_subtractDates(&d, &exp, &days_left);
if (days_left < expireDays) {
ei->days = days_left;
ei->callsign = strdup(callsign);
ei->cert = clist[i];
// Send the result back to the main thread
wxCommandEvent* event = new wxCommandEvent(wxEVT_COMMAND_MENU_SELECTED, bg_expiring);
event->SetClientData(ei);
wxPostEvent(frame, *event);
ei->condition->Wait();
ei->mutex->Unlock();
delete ei;
delete event;
ei = new expInfo;
ei->noGUI = noGUI;
}
}
}
ei->mutex->Unlock();
delete ei;
if (clist) {
tqsl_freeCertificateList(clist, nc);
clist = NULL;
nc = 0;
}
if (needToCleanUp) {
curl_easy_cleanup(curlReq);
curlReq = NULL;
if (curlLogFile) {
fclose(curlLogFile);
curlLogFile = NULL;
}
}
return;
}
void
MyFrame::OnExpiredCertFound(wxCommandEvent& event) {
expInfo *ei = reinterpret_cast<expInfo *>(event.GetClientData());
if (ei->error) {
if (ei->noGUI) {
wxLogError(ei->errorText);
} else {
wxMessageBox(wxString(_("Error checking for expired callsign certificates:")) + ei->errorText,
_("Check Error"), wxOK | wxICON_EXCLAMATION, this);
}
} else if (ei->noGUI) {
wxLogMessage(_("The certificate for %hs expires in %d days."),
ei->callsign, ei->days);
} else {
wxString fmt = _("The certificate for %hs expires in %d days");
fmt += wxT("\n");
fmt += _("Do you want to renew it now?");
if (wxMessageBox(wxString::Format(fmt, ei->callsign, ei->days),
_("Certificate Expiring"), wxYES_NO|wxICON_QUESTION, this) == wxYES) {
// Select the certificate in the tree
cert_tree->SelectCert(ei->cert);
// Then start the renewal
wxCommandEvent dummy;
CRQWizardRenew(dummy);
}
}
// Tell the background thread that it's OK to continue
wxMutexLocker lock(*ei->mutex);
ei->condition->Signal();
}
void
MyFrame::OnUpdateCheckDone(wxCommandEvent& event) {
revInfo *ri = reinterpret_cast<revInfo *>(event.GetClientData());
if (!ri) return;
if (ri->error) {
if (!ri->silent && !ri->noGUI)
wxLogMessage(ri->errorText);
wxMutexLocker lock(*ri->mutex);
ri->condition->Signal();
return;
}
if (ri->message) {
if (!ri->silent && !ri->noGUI)
wxMessageBox(ri->errorText, _("Update"), wxOK | wxICON_EXCLAMATION, this);
wxMutexLocker lock(*ri->mutex);
ri->condition->Signal();
return;
}
// For win32, can't have config and program updates together.
// randomize which gets flagged
#ifdef _WIN32
if (ri->newProgram && ri->newConfig) {
if (time(0) & 0x1) {
ri->newProgram = false;
} else {
ri->newConfig = false;
}
}
#endif
if (ri->newProgram && ri->newProgramRev) {
if (ri->noGUI) {
wxLogMessage(_("A new TQSL release (V%s) is available."), ri->newProgramRev->Value().c_str());
} else {
//will create ("homepage"->"") if none there, which is what we'd be checking for anyway
UpdateDialogMsgBox msg(this, true, false, ri->programRev, ri->newProgramRev,
ri->configRev, ri->newConfigRev, ri->url, ri->homepage);
#if defined(_WIN32) || defined(__APPLE__)
if (msg.ShowModal() == wxID_OK) {
UpdateTQSL(ri->url);
}
#else
msg.ShowModal();
#endif
}
}
if (ri->newConfig) {
if (ri->noGUI) {
wxLogMessage(_("A new TrustedQSL configuration file (V%s) is available."), ri->newConfigRev->Value().c_str());
} else {
UpdateDialogMsgBox msg(this, false, true, ri->programRev, ri->newProgramRev,
ri->configRev, ri->newConfigRev, wxT(""), wxT(""));
if (msg.ShowModal() == wxID_OK) {
UpdateConfigFile();
}
}
}
if (!ri->newProgram && !ri->newConfig) {
if (!ri->silent && !ri->noGUI) {
wxString fmt = _("Your system is up to date");
fmt += wxT("\n");
fmt += _("TQSL Version %hs and Configuration Data Version %s");
fmt += wxT("\n");
fmt += _("are the newest available");
wxMessageBox(wxString::Format(fmt, TQSL_VERSION, ri->configRev->Value().c_str()), _("No Updates"), wxOK | wxICON_INFORMATION, this);
}
}
// Tell the background thread that it's OK to continue
wxMutexLocker lock(*ri->mutex);
ri->condition->Signal();
}
void
MyFrame::DoCheckForUpdates(bool silent, bool noGUI) {
tqslTrace("MyFrame::DoCheckForUpdates", "silent=%d noGUI=%d", silent, noGUI);
wxConfig* config = reinterpret_cast<wxConfig *>(wxConfig::Get());
wxString lastUpdateTime = config->Read(wxT("UpdateCheckTime"));
int numdays = config->Read(wxT("UpdateCheckInterval"), 1); // in days
bool check = true;
bool networkError = false;
if (!lastUpdateTime.IsEmpty()) {
wxDateTime lastcheck; lastcheck.ParseFormat(lastUpdateTime, wxT("%Y-%m-%d"));
lastcheck+=wxDateSpan::Days(numdays); // x days from when we checked
if (lastcheck > wxDateTime::Today()) //if we checked less than x days ago
check = false; // don't check again
} // else no stored value, means check
if (!silent) check = true; //unless the user explicitly asked
if (!check) return; //if we really weren't supposed to check, get out of here
revInfo* ri = new revInfo;
ri->noGUI = noGUI;
ri->silent = silent;
ri->programRev = new revLevel(wxT(TQSL_VERSION));
int currentConfigMajor, currentConfigMinor;
tqsl_getConfigVersion(¤tConfigMajor, ¤tConfigMinor);
ri->configRev = new revLevel(currentConfigMajor, currentConfigMinor, 0); // config files don't have patch levels
wxString updateURL = config->Read(wxT("UpdateURL"), DEFAULT_UPD_URL);
bool needToCleanUp = false;
retry:
if (!verifyCA) {
updateURL.Replace(wxT("https:"), wxT("http:"));
}
if (curlReq) {
curl_easy_setopt(curlReq, CURLOPT_URL, (const char *)updateURL.ToUTF8());
} else {
needToCleanUp = true;
curlReq = tqsl_curl_init("Version Check Log", (const char*)updateURL.ToUTF8(), &curlLogFile, false);
}
//the following allow us to analyze our file
FileUploadHandler handler;
curl_easy_setopt(curlReq, CURLOPT_WRITEFUNCTION, &FileUploadHandler::recv);
curl_easy_setopt(curlReq, CURLOPT_WRITEDATA, &handler);
if (silent) { // if there's a problem, we don't want the program to hang while we're starting it
curl_easy_setopt(curlReq, CURLOPT_CONNECTTIMEOUT, 30);
} else {
curl_easy_setopt(curlReq, CURLOPT_CONNECTTIMEOUT, 60);
}
curl_easy_setopt(curlReq, CURLOPT_LOW_SPEED_TIME, 60); // Abort if not making at least 30 bytes/sec.
curl_easy_setopt(curlReq, CURLOPT_LOW_SPEED_LIMIT, 30);
curl_easy_setopt(curlReq, CURLOPT_FAILONERROR, 1); //let us find out about a server issue
char errorbuf[CURL_ERROR_SIZE];
curl_easy_setopt(curlReq, CURLOPT_ERRORBUFFER, errorbuf);
tqslTrace("MyFrame::DoCheckForUpdates", "calling curl_easy_perform");
int retval = curl_easy_perform(curlReq);
tqslTrace("MyFrame::DoCheckForUpdates", "Program rev check returns %d", retval);
if (retval == CURLE_OK) {
tqslTrace("MyFrame::DoCheckForUpdates", "Program rev returns %d chars, %s", handler.s.size(), handler.s.c_str());
// Add the config.xml text to the result
wxString configURL = config->Read(wxT("ConfigFileVerURL"), DEFAULT_UPD_CONFIG_URL);
curl_easy_setopt(curlReq, CURLOPT_URL, (const char*)configURL.ToUTF8());
curl_easy_setopt(curlReq, CURLOPT_USERAGENT, "tqsl/" TQSL_VERSION);
retval = curl_easy_perform(curlReq);
if (retval == CURLE_OK) {
tqslTrace("MyFrame::DoCheckForUpdates", "Prog + Config rev returns %d chars, %s", handler.s.size(), handler.s.c_str());
wxString result = wxString::FromAscii(handler.s.c_str());
wxString url;
// The macro for declaring a hash map defines a couple of typedefs
// that it never uses. Current GCC warns about those. The pragma
// below suppresses those warnings for those.
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#endif
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-local-typedef"
#endif
WX_DECLARE_STRING_HASH_MAP(wxString, URLHashMap);
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic warning "-Wunused-local-typedefs"
#endif
URLHashMap map;
ri->newProgramRev = NULL;
ri->newConfigRev = NULL;
wxStringTokenizer urls(result, wxT("\n"));
wxString onlinever;
while(urls.HasMoreTokens()) {
wxString header = urls.GetNextToken().Trim();
if (header.StartsWith(wxT("TQSLVERSION;"), &onlinever)) {
ri->newProgramRev = new revLevel(onlinever);
} else if (header.IsEmpty()) {
continue; //blank line
} else if (header[0] == '#') {
continue; //comments
} else if (header.StartsWith(wxT("config.xml"), &onlinever)) {
onlinever.Replace(wxT(":"), wxT(""));
onlinever.Replace(wxT("Version"), wxT(""));
onlinever.Trim(true);
onlinever.Trim(false);
ri->newConfigRev = new revLevel(onlinever);
} else {
int sep = header.Find(';'); //; is invalid in URLs
if (sep == wxNOT_FOUND) continue; //malformed string
wxString plat = header.Left(sep);
wxString url = header.Right(header.size()-sep-1);
map[plat] = url;
}
}
#ifdef TQSL_TEST_BUILD
ri->newProgram = ri->newProgramRev ? (*ri->newProgramRev >= *ri->programRev) : false;
#else
ri->newProgram = ri->newProgramRev ? (*ri->newProgramRev > *ri->programRev) : false;
#endif
ri->newConfig = ri->newConfigRev ? (*ri->newConfigRev > *ri->configRev) : false;
if (ri->newProgram) {
wxString ourPlatURL; //empty by default (we check against this later)
wxStringTokenizer plats(GetUpdatePlatformString(), wxT(" "));
while(plats.HasMoreTokens()) {
wxString tok = plats.GetNextToken();
//see if this token is here
if (map.count(tok)) {
ourPlatURL = map[tok];
break;
}
}
ri->homepage = map[wxT("homepage")];
ri->url = ourPlatURL;
}
} else {
tqslTrace("MyFrame::DoCheckForUpdates", "cURL Error during config file version check: %d : %s (%s)\n", retval, curl_easy_strerror((CURLcode)retval), errorbuf);
if (curlLogFile) {
fprintf(curlLogFile, "cURL Error during config file version check: %s (%s)\n", curl_easy_strerror((CURLcode)retval), errorbuf);
}
if (retval == CURLE_COULDNT_RESOLVE_HOST || retval == CURLE_COULDNT_CONNECT) {
networkError = true;
ri->error = true;
ri->errorText = wxString(_("Unable to check for updates - either your Internet connection is down or LoTW is unreachable."));
ri->errorText += wxT("\n");
ri->errorText += _("Please try again later.");
} else if (retval == CURLE_WRITE_ERROR || retval == CURLE_SEND_ERROR || retval == CURLE_RECV_ERROR) {
networkError = true;
ri->error = true;
ri->errorText = wxString(_("Unable to check for updates. The network is down or the LoTW site is too busy."));
ri->errorText += wxT("\n");
ri->errorText += _("Please try again later.");
} else if (retval == CURLE_SSL_CONNECT_ERROR || retval == CURLE_OPERATION_TIMEDOUT) {
networkError = true;
ri->error = true;
ri->errorText = wxString(_("Unable to connect to the update site."));
ri->errorText += wxT("\n");
ri->errorText += _("Please try again later.");
} else { // some other error
ri->message = true;
wxString fmt = _("Error downloading new version information:");
fmt += wxT("\n%hs");
ri->errorText = wxString::Format(fmt, errorbuf);
}
}
} else {
tqslTrace("MyFrame::DoCheckForUpdates", "cURL Error during program revision check: %d: %s (%s)\n", retval, curl_easy_strerror((CURLcode)retval), errorbuf);
if ((retval == CURLE_PEER_FAILED_VERIFICATION) && verifyCA) {
tqslTrace("MyFrame::DoCheckForUpdates", "cURL SSL Certificate error - disabling verify and retry");
verifyCA = false;
goto retry;
}
if (curlLogFile) {
fprintf(curlLogFile, "cURL Error during program revision check: %s (%s)\n", curl_easy_strerror((CURLcode)retval), errorbuf);
}
if (retval == CURLE_COULDNT_RESOLVE_HOST || retval == CURLE_COULDNT_CONNECT) {
networkError = true;
ri->error = true;
ri->errorText = wxString(_("Unable to check for updates - either your Internet connection is down or LoTW is unreachable."));
ri->errorText += wxT("\n");
ri->errorText += _("Please try again later.");
} else if (retval == CURLE_WRITE_ERROR || retval == CURLE_SEND_ERROR || retval == CURLE_RECV_ERROR) {
networkError = true;
ri->error = true;
ri->errorText = wxString(_("Unable to check for updates. The network is down or the LoTW site is too busy."));
ri->errorText += wxT("\n");
ri->errorText += _("Please try again later.");
} else if (retval == CURLE_SSL_CONNECT_ERROR || retval == CURLE_OPERATION_TIMEDOUT) {
networkError = true;
ri->error = true;
ri->errorText = wxString(_("Unable to connect to the update site."));
ri->errorText += wxT("\n");
ri->errorText += _("Please try again later.");
} else { // some other error
ri->message = true;
wxString fmt = _("Error downloading update version information:");
fmt += wxT("\n%hs");
ri->errorText = wxString::Format(fmt, errorbuf);
}
}
// Send the result back to the main thread
wxCommandEvent* event = new wxCommandEvent(wxEVT_COMMAND_MENU_SELECTED, bg_updateCheck);
event->SetClientData(ri);
wxPostEvent(frame, *event);
ri->condition->Wait();
ri->mutex->Unlock();
delete ri;
delete event;
if (needToCleanUp) {
if (curlReq) curl_easy_cleanup(curlReq);
if (curlLogFile) fclose(curlLogFile);
curlReq = NULL;
curlLogFile = NULL;
}
// we checked today, and whatever the result, no need to (automatically) check again until the next interval
config->Write(wxT("UpdateCheckTime"), wxDateTime::Today().FormatISODate());
// After update check, validate user certificates
if (!networkError)
DoCheckExpiringCerts(noGUI);
return;
}
void MyFrame::DoLoTWSync(wxCommandEvent&) {
// When was this last done?
wxString lastSyncDate = wxConfig::Get()->Read(wxT("SyncDate"));
if (!lastSyncDate.IsEmpty()) {
wxDateTime lastcheck;
lastcheck.ParseFormat(lastSyncDate, wxT("%Y-%m-%d"));
lastcheck+=wxDateSpan::Days(1); // x days from when we checked
if (lastcheck > wxDateTime::Today()) { //if we checked less than x days ago
wxMessageBox(_("You can not synchronize with LoTW more than once per day"), _("Error"), wxOK | wxICON_ERROR, this);
return;
}
}
// Don't allow other signing
if (lock_db(false) < 0) {
wxSafeYield();
DBWaitDialog* waitForDB = new DBWaitDialog(this, -1, _("Another copy of TQSL is busy"), wxDefaultPosition,
wxDefaultSize, wxDEFAULT_DIALOG_STYLE, _("TQSL must wait for other running copies of TQSL to exit before synchronizing ..."));
waitForDB->Start(500);
int result = waitForDB->ShowModal();
waitForDB->Stop();
if (result == wxID_CANCEL) {
wxLogMessage(_("Aborting due to user cancelling"));
return;
}
}
// Get the user's Logbook credentials
GetUserAndPasswordDialog dial(this, _("LoTW Login"), _("TQSL needs your Logbook of the World (LoTW) username and password to retrieve the data needed to perform this operation.\nPlease note that retrieving all of your LoTW data can take several minutes, so please be patient while LoTW collects that information.\n\nTQSL will appear to be unresponsive until LoTW completes extraction of your QSO history.\n"));
if (dial.ShowModal() != wxID_OK) {
tqslTrace("MyFrame::DoLoTWSync", "Password entry cancelled");
unlock_db();
return;
}
tqslTrace("MyFrame::DoLoTWSync", "Password entered OK");
wxString un = dial.Username();
wxString pw = dial.Password();
urlEncode(pw);
passwordEncode(pw);
wxString url = wxString::Format(wxT("https://lotw.arrl.org/lotwuser/lotwreport.adi?qso_query=1&login=%s&password=%s&qso_qsl=no&qso_mydetail=yes&qso_withown=yes&qso_qslsince=1900-01-01&qso_qsorxsince=1900-01-01"), un.c_str(), pw.c_str());
curlReq = tqsl_curl_init("LoTW QSOs Download Log", (const char *)url.ToUTF8(), &curlLogFile, false);
frame->logwin->AppendText(_("Retrieving your QSO details from LoTW. Please be patient, this may take some time.\n"));
wxSafeYield(this);
wxString filename = wxString::FromUTF8(tQSL_BaseDir);
#ifdef _WIN32
filename = filename + wxT("\\lotwqsos.adif");
wchar_t* lfn = utf8_to_wchar(filename.ToUTF8());
FILE *qsoFile = _wfopen(lfn, L"r+b");
free_wchar(lfn);
#else
filename = filename + wxT("/lotwqsos.adif");
FILE *qsoFile = fopen(filename.ToUTF8(), "r+b");
#endif
if (!qsoFile) {
tqslTrace("SyncLoTW", "Can't open new file %s: %s", static_cast<const char *>(filename.ToUTF8()), strerror(errno));
wxMessageBox(wxString::Format(_("Can't open new configuration file %s: %hs"), filename.c_str(), strerror(errno)), _("Error"), wxOK | wxICON_ERROR, this);
unlock_db();
return;
}
curl_easy_setopt(curlReq, CURLOPT_WRITEFUNCTION, &file_recv);
curl_easy_setopt(curlReq, CURLOPT_WRITEDATA, qsoFile);
curl_easy_setopt(curlReq, CURLOPT_CONNECTTIMEOUT, 60);
curl_easy_setopt(curlReq, CURLOPT_FAILONERROR, 1); //let us find out about a server issue
char errorbuf[CURL_ERROR_SIZE];
curl_easy_setopt(curlReq, CURLOPT_ERRORBUFFER, errorbuf);
curl_easy_setopt(curlReq, CURLOPT_COOKIEFILE, "");
int retval = curl_easy_perform(curlReq);
if (retval == CURLE_OK) {
if (fflush(qsoFile)) {
tqslTrace("SyncLoTW", "Error writing new file %s: %s", static_cast<const char *>(filename.ToUTF8()), strerror(errno));
wxMessageBox(wxString::Format(_("Error writing log download file %s: %hs"), filename.c_str(), strerror(errno)), _("Error"), wxOK | wxICON_ERROR, this);
unlock_db();
return;
}
if (fseek(qsoFile, 0, SEEK_SET)) {
tqslTrace("SyncLoTW", "Error seeking new file %s: %s", static_cast<const char *>(filename.ToUTF8()), strerror(errno));
wxMessageBox(wxString::Format(_("Error writing log download file %s: %hs"), filename.c_str(), strerror(errno)), _("Error"), wxOK | wxICON_ERROR, this);
fclose(qsoFile);
unlock_db();
return;
}
char buf[256];
if (fgets(buf, sizeof buf, qsoFile) == NULL) {
tqslTrace("SyncLoTW", "Error reading new file %s: %s", static_cast<const char *>(filename.ToUTF8()), strerror(errno));
wxMessageBox(wxString::Format(_("Error reading log download file %s: %hs"), filename.c_str(), strerror(errno)), _("Error"), wxOK | wxICON_ERROR, this);
fclose(qsoFile);
unlock_db();
return;
}
if (buf[0] == '<') { // Oops, html.
while (fgets(buf, sizeof buf, qsoFile) != NULL) {
if ((strncmp(buf, "<I>", 3) == 0) && (strncmp(buf + 3, "Note:", 5) != 0)) {
wxString err = wxString::FromUTF8(buf);
err.Replace(wxT("<I>"), wxT(""), true);
err.Replace(wxT("</I>"), wxT(""), true);
fclose(qsoFile);
wxMessageBox(wxString::Format(_("Error logging in to LoTW: %s"), err.c_str()), _("Error"), wxOK | wxICON_ERROR, this);
unlock_db();
return;
}
}
fclose(qsoFile);
wxMessageBox(_("Response from LoTW is not recognized"), _("Error"), wxOK | wxICON_ERROR, this);
unlock_db();
return;
}
fclose(qsoFile);
wxSafeYield(this);
wxConfig::Get()->Write(wxT("SyncDate"), wxDateTime::Today().FormatISODate());
tqsl_removeUploadDatabase(); // Wipe the upload DB
tQSL_Location loc = NULL;
if (tqsl_getStationLocation(&loc, ".empty")) { // Already exists?
tqsl_initStationLocationCapture(&loc); // No, make a new one.
tqsl_setStationLocationCaptureName(loc, ".empty");
tqsl_saveStationLocationCapture(loc, 1);
tqsl_getStationLocation(&loc, ".empty");
}
int numrecs;
frame->logwin->AppendText(_("Loading your LoTW QSO data into TQSL\n"));
wxSafeYield(this);
int status = this->SignLogFile(loc, filename, NULL, false, numrecs, true, NULL, NULL, TQSL_ACTION_ALL, -1, NULL, NULL, TQSL_UPLOAD_ONLY);
if (status) {
tqsl_converterRollBack(logConv);
tqsl_endConverter(&logConv);
} else {
tqsl_converterCommit(logConv);
tqsl_endConverter(&logConv);
}
unlock_db();
frame->logwin->AppendText(_("Synchronizing completed.\n"));
}
}
static void
wx_tokens(const wxString& str, vector<wxString> &toks) {
size_t idx = 0;
size_t newidx;
wxString tok;
do {
newidx = str.find(wxT(" "), idx);
if (newidx != wxString::npos) {
toks.push_back(str.Mid(idx, newidx - idx));
idx = newidx + 1;
}
} while (newidx != wxString::npos);
if (!str.Mid(idx).IsEmpty())
toks.push_back(str.Mid(idx));
}
int
SaveAddressInfo(const char *callsign, int dxcc) {
if (callsign == NULL) {
tQSL_Error = TQSL_ARGUMENT_ERROR;
return 1;
}
#ifdef USE_ADDRESS_INFO
bool needToCleanUp = false;
char url[512];
strncpy(url, (wxString::Format(wxT("https://lotw.arrl.org/tqsl-setup.php?callsign=%hs&dxcc=%d"), callsign, dxcc)).ToUTF8(), sizeof url);
tqslTrace("SaveAddressInfo", "Call = %s, Entity=%d, url = %s", callsign, dxcc, url);
if (curlReq) {
curl_easy_setopt(curlReq, CURLOPT_URL, url);
} else {
curlReq = tqsl_curl_init("checkLoc", url, &curlLogFile, false);
needToCleanUp = true;
}
FileUploadHandler handler;
curl_easy_setopt(curlReq, CURLOPT_WRITEFUNCTION, &FileUploadHandler::recv);
curl_easy_setopt(curlReq, CURLOPT_WRITEDATA, &handler);
curl_easy_setopt(curlReq, CURLOPT_CONNECTTIMEOUT, 60);
curl_easy_setopt(curlReq, CURLOPT_FAILONERROR, 1); //let us find out about a server issue
char errorbuf[CURL_ERROR_SIZE];
errorbuf[0] = '\0';
curl_easy_setopt(curlReq, CURLOPT_ERRORBUFFER, errorbuf);
int retval = curl_easy_perform(curlReq);
wxString checkresult = wxT("");
if (needToCleanUp) {
if (curlLogFile)
fclose(curlLogFile);
curl_easy_cleanup(curlReq);
curlReq = NULL;
}
if (retval == CURLE_OK) {
if (handler.s != "null") {
tqslTrace("SaveAddressInfo", "callsign=%s, result = %s", callsign, handler.s.c_str());
tqsl_saveCallsignLocationInfo(callsign, handler.s.c_str());
}
} else if (retval == CURLE_SSL_CONNECT_ERROR || retval == CURLE_OPERATION_TIMEDOUT) {
tqslTrace("save_address_info", "cURL Error during cert status check: %s (%s)\n", curl_easy_strerror((CURLcode)retval), errorbuf);
return -1;
} else {
tqslTrace("save_address_info", "cURL Error during cert status check: %s (%s)\n", curl_easy_strerror((CURLcode)retval), errorbuf);
return 1;
}
return 0;
#else
tqsl_saveCallsignLocationInfo(callsign, NULL);
return 0;
#endif
}
int
get_address_field(const char *callsign, const char *field, string& result) {
#ifdef USE_ADDRESS_INFO
typedef map<string, string>LocMap;
static LocMap locInfo;
if (callsign == NULL || field == NULL) {
tQSL_Error = TQSL_ARGUMENT_ERROR;
return 1;
}
result = "";
if (strcmp(callsign, "[None]") == 0) {
return 1;
}
if (locInfo["call"] == callsign) { // Already got data for this call
LocMap::iterator it;
it = locInfo.find(field);
if (it == locInfo.end()) {
return 1;
}
string temp = it->second;
if (temp.empty() || temp == "null") {
return 1;
}
result = temp;
return 0;
}
locInfo.clear();
char *buf;
if (tqsl_getCallsignLocationInfo(callsign, &buf)) {
return 1;
}
wxString checkresult = wxString::FromUTF8(buf);
wxJSONReader reader;
wxJSONValue root;
int errors = reader.Parse(checkresult, &root);
if (errors > 0)
return 1;
locInfo["call"] = callsign;
locInfo["status"] = root[wxT("Status")].AsString().ToUTF8();
if (locInfo["status"] != "OK") {
return 1;
}
locInfo["dxcc"] = root[wxT("DXCC_Entity")].AsString().ToUTF8();
locInfo["entity"] = root[wxT("DXCC_Entity_Name")].AsString().ToUTF8();
locInfo["country"] = root[wxT("Country")].AsString().ToUTF8();
locInfo["state"] = root[wxT("STATE")].AsString().ToUTF8();
wxStringTokenizer toker(root[wxT("CNTY")].AsString(), wxT(","));
wxString state = toker.GetNextToken();
wxString cty = toker.GetNextToken();
locInfo["county"] = cty.ToUTF8();
if (locInfo["state"] == "") {
locInfo["state"] = state.ToUTF8();
}
locInfo["grid"] = root[wxT("GRID")].AsString().ToUTF8();
locInfo["cqzone"] = root[wxT("CQ Zone")].AsString().ToUTF8();
if (locInfo["cqzone"].substr(0, 1) == "0") locInfo["cqzone"].erase(0, 1);
locInfo["ituzone"] = root[wxT("ITU Zone")].AsString().ToUTF8();
if (locInfo["ituzone"].substr(0, 1) == "0") locInfo["ituzone"].erase(0, 1);
locInfo["pas"] = root[wxT("Primary_Admnistrative_Subdivision")].AsString().ToUTF8();
locInfo["sas"] = root[wxT("Secondary_Admnistrative_Subdivision")].AsString().ToUTF8();
locInfo["address"] = root[wxT("Address_In")].AsString().ToUTF8();
string grids;
wxJSONValue gridlist = root[wxT("VUCC_Grids")];
for (int x = 0; x < gridlist.Size(); x++) {
if (!grids.empty()) {
grids = grids + "|";
}
grids = grids + ((string)gridlist[x].AsString().ToUTF8());
}
locInfo["grids"] = grids;
wxString first = root[wxT("Source_address_components")][wxT("first_name")].AsString();
if (first == wxT("null")) first = wxT("");
wxString middle = root[wxT("Source_address_components")][wxT("middle_name")].AsString();
if (middle == wxT("null")) middle = wxT("");
wxString last = root[wxT("Source_address_components")][wxT("last_name")].AsString();
if (last == wxT("null")) last = wxT("");
wxString name;
if (middle.IsEmpty())
name = first + wxT(" ") + last;
else
name = first + wxT(" ") + middle + wxT(" ") + last;
if (!name.IsEmpty() || name == wxT(" "))
locInfo["name"] = ((string)name.ToUTF8());
locInfo["addr1"] = root[wxT("Source_address_components")][wxT("addr1")].AsString().ToUTF8();
locInfo["addr2"] = root[wxT("Source_address_components")][wxT("addr2")].AsString().ToUTF8();
locInfo["addr3"] = root[wxT("Source_address_components")][wxT("addr3")].AsString().ToUTF8();
locInfo["city"] = root[wxT("Source_address_components")][wxT("city")].AsString().ToUTF8();
locInfo["addrState"] = root[wxT("Source_address_components")][wxT("state")].AsString().ToUTF8();
locInfo["mailCode"] = root[wxT("Source_address_components")][wxT("mail_code")].AsString().ToUTF8();
locInfo["aCountry"] = root[wxT("Source_address_components")][wxT("country")].AsString().ToUTF8();
LocMap::iterator it;
it = locInfo.find(field);
if (it == locInfo.end()) {
return 1;
}
string temp = it->second;
if (temp.empty() || temp == "null") {
return 1;
}
result = temp;
return 0;
#else
return 1;
#endif
}
/*
* Lookup of ULS info for a call.
* Returns: 0 - Success
* 1 - Unable to check
* 2 - Not found
* 3 - Not reachable
*/
int
GetULSInfo(const char *callsign, wxString &name, wxString &attn, wxString &street, wxString &city, wxString &state, wxString &zip) {
if (callsign == NULL) {
tQSL_Error = TQSL_ARGUMENT_ERROR;
return 1;
}
bool needToCleanUp = false;
char url[512];
strncpy(url, (wxString::Format(wxT("https://lotw.arrl.org/uls.php?callsign=%hs"), callsign)).ToUTF8(), sizeof url);
tqslTrace("GetULSInfo", "Call = %s, url = %s", callsign, url);
if (curlReq) {
curl_easy_setopt(curlReq, CURLOPT_URL, url);
} else {
curlReq = tqsl_curl_init("checkULS", url, &curlLogFile, false);
needToCleanUp = true;
}
FileUploadHandler handler;
curl_easy_setopt(curlReq, CURLOPT_WRITEFUNCTION, &FileUploadHandler::recv);
curl_easy_setopt(curlReq, CURLOPT_WRITEDATA, &handler);
curl_easy_setopt(curlReq, CURLOPT_CONNECTTIMEOUT, 60);
curl_easy_setopt(curlReq, CURLOPT_FAILONERROR, 1); //let us find out about a server issue
char errorbuf[CURL_ERROR_SIZE];
errorbuf[0] = '\0';
curl_easy_setopt(curlReq, CURLOPT_ERRORBUFFER, errorbuf);
int retval = curl_easy_perform(curlReq);
tqslTrace("GetULSInfo", "upload result = %d", retval);
if (needToCleanUp) {
if (curlLogFile)
fclose(curlLogFile);
curl_easy_cleanup(curlReq);
curlReq = NULL;
}
if (retval == CURLE_OK) {
if (handler.s != "null") {
tqslTrace("GetULSInfo", "callsign=%s, result = %s", callsign, handler.s.c_str());
// fields: name, callsign, street, city, state, zip.
wxString checkresult = wxString::FromUTF8(handler.s.c_str());
wxJSONReader reader;
wxJSONValue root;
int errors = reader.Parse(checkresult, &root);
if (errors > 0)
return 1;
if (strcmp(root[wxT("callsign")].AsString().ToUTF8(), callsign) != 0)
return 1;
name = root[wxT("name")].AsString().Trim().Trim(FALSE);
attn = root[wxT("attention")].AsString().Trim().Trim(FALSE);
street = root[wxT("street")].AsString().Trim().Trim(FALSE);
city = root[wxT("city")].AsString().Trim().Trim(FALSE);
state = root[wxT("state")].AsString().Trim().Trim(FALSE);
zip = root[wxT("zip")].AsString().Trim().Trim(FALSE);
return 0;
} else {
return 2; // Not valid
}
} else if (retval == CURLE_SSL_CONNECT_ERROR || retval == CURLE_OPERATION_TIMEDOUT) {
return 3;
} else {
tqslTrace("GetULSInfo", "cURL Error %d during ULS check: %s (%s)\n", retval, curl_easy_strerror((CURLcode)retval), errorbuf);
return 1;
}
return 0;
}
// Common method for sign and (save, upload) a log
void
MyFrame::ProcessQSODataFile(bool upload, int compressed) {
tqslTrace("MyFrame::ProcessQSODataFile", "upload=%d, compressed=%d", upload, compressed);
wxString infile;
wxString outfile;
// Does the user have any certificates?
get_certlist("", 0, false, false, true); // any call, any DXCC, not expired or superceded
if (ncerts == 0) {
wxString msg = _("You have no callsign certificates to use to sign a log file.");
msg += wxT("\n");
msg += _("Please install a callsign certificate then try again.");
wxMessageBox(msg, _("No Callsign Certificates"),
wxOK | wxICON_EXCLAMATION, this);
free_certlist();
return;
}
free_certlist();
try {
wxConfig *config = reinterpret_cast<wxConfig *>(wxConfig::Get());
// Get input file
wxString path = config->Read(wxT("ImportPath"), wxString(wxT("")));
wxString defext = config->Read(wxT("ImportExtension"), wxString(wxT("adi"))).Lower();
bool defFound = false;
// Construct filter string for file-open dialog
wxString filter = wxT("All files (*.*)|*.*");
vector<wxString> exts;
wxString file_exts = config->Read(wxT("ADIFFiles"), wxString(DEFAULT_ADIF_FILES));
wx_tokens(file_exts, exts);
wxString extList;
for (int i = 0; i < static_cast<int>(exts.size()); i++) {
extList += wxT("*.") + exts[i] + wxT(";");
if (exts[i] == defext)
defFound = true;
}
extList.RemoveLast(); // Remove the trailing semicolon
filter += _("|ADIF files (") + extList + wxT(")|") + extList;
exts.clear();
extList.Clear();
file_exts = config->Read(wxT("CabrilloFiles"), wxString(DEFAULT_CABRILLO_FILES));
wx_tokens(file_exts, exts);
for (int i = 0; i < static_cast<int>(exts.size()); i++) {
extList += wxT("*.") + exts[i] + wxT(";");
if (exts[i] == defext)
defFound = true;
}
extList.RemoveLast();
filter += _("|Cabrillo files (") + extList + wxT(")|") + extList;
if (defext.IsEmpty() || !defFound)
defext = wxString(wxT("adi"));
infile = wxFileSelector(_("Select file to Sign"), path, wxT(""), defext, filter,
wxFD_OPEN|wxFD_FILE_MUST_EXIST, this);
if (infile.IsEmpty())
return;
wxString inPath;
wxString inExt;
wxFileName::SplitPath(infile.c_str(), &inPath, NULL, &inExt);
inExt.Lower();
config->Write(wxT("ImportPath"), inPath);
config->Write(wxT("ImportExtension"), inExt);
if (!upload) {
// Get output file
wxString basename;
wxFileName::SplitPath(infile.c_str(), 0, &basename, 0);
path = wxConfig::Get()->Read(wxT("ExportPath"), wxString(wxT("")));
wxString deftype = compressed == TQSL_COMPRESSED_FILE ? wxT("tq8") : wxT("tq7");
filter = compressed == TQSL_COMPRESSED_FILE ? _("TQSL compressed data files (*.tq8)|*.tq8")
: _("TQSL data files (*.tq7)|*.tq7");
basename += wxT(".") + deftype;
outfile = wxFileSelector(_("Select file to write to"),
path, basename, deftype, filter + _("|All files (*.*)|*.*"),
wxFD_SAVE|wxFD_OVERWRITE_PROMPT, this);
if (outfile.IsEmpty())
return;
config->Write(wxT("ExportPath"), wxPathOnly(outfile));
}
// Get Station Location
int n;
tQSL_Location loc;
check_tqsl_error(tqsl_initStationLocationCapture(&loc));
check_tqsl_error(tqsl_getNumStationLocations(loc, &n));
if (n != 1) {
check_tqsl_error(tqsl_endStationLocationCapture(&loc));
frame->Show(true);
loc = SelectStationLocation(_("Select Station Location for Signing"));
} else {
// There's only one station location. Use that and don't prompt.
char deflocn[512];
check_tqsl_error(tqsl_getStationLocationName(loc, 0, deflocn, sizeof deflocn));
check_tqsl_error(tqsl_endStationLocationCapture(&loc));
check_tqsl_error(tqsl_getStationLocation(&loc, deflocn));
}
if (loc == 0)
return;
if (!verify_cert(loc, false))
return;
char callsign[40];
char loc_name[256];
int dxccnum;
check_tqsl_error(tqsl_getLocationCallSign(loc, callsign, sizeof callsign));
check_tqsl_error(tqsl_getLocationDXCCEntity(loc, &dxccnum));
check_tqsl_error(tqsl_getStationLocationCaptureName(loc, loc_name, sizeof loc_name));
DXCC dxcc;
dxcc.getByEntity(dxccnum);
tqslTrace("MyFrame::ProcessQSODataFile", "file=%s location %hs, call %hs dxcc %hs",
S(infile), loc_name, callsign, dxcc.name());
if (strcmp(callsign, "[None]")) {
LocPropDial dial(wxString::FromUTF8(loc_name), false, infile.ToUTF8(), this);
int locnOK = dial.ShowModal();
if (locnOK == wxID_OK) {
if (upload) {
UploadLogFile(loc, infile);
} else {
ConvertLogFile(loc, infile, outfile, compressed);
}
} else {
wxLogMessage(_("Signing abandoned"));
}
} else {
if (upload) {
UploadLogFile(loc, infile);
} else {
ConvertLogFile(loc, infile, outfile, compressed);
}
}
check_tqsl_error(tqsl_endStationLocationCapture(&loc));
}
catch(TQSLException& x) {
wxString s;
wxString err = wxString::FromUTF8(x.what());
if (err.Find(infile) == wxNOT_FOUND) {
if (!infile.IsEmpty())
s = infile + wxT(": ");
}
s += err;
wxLogError(wxT("%s"), (const char *)s.c_str());
}
return;
}
void
MyFrame::ImportQSODataFile(wxCommandEvent& event) {
tqslTrace("MyFrame::ImportQSODataFile", NULL);
bool compressed = (event.GetId() == tm_f_import_compress || event.GetId() == tl_Save);
ProcessQSODataFile(false, compressed ? TQSL_COMPRESSED_FILE : TQSL_UNCOMPRESSED_FILE);
return;
}
void
MyFrame::TestSignQSODataFile(wxCommandEvent& event) {
tqslTrace("MyFrame::ImportQSODataFile", NULL);
ProcessQSODataFile(false, TQSL_TEST_SIGNING);
return;
}
void
MyFrame::UploadQSODataFile(wxCommandEvent& event) {
tqslTrace("MyFrame::UploadQSODataFile", NULL);
ProcessQSODataFile(true, TQSL_COMPRESSED_FILE);
return;
}
void MyFrame::OnPreferences(wxCommandEvent& WXUNUSED(event)) {
tqslTrace("MyFrame::OnPreferences", NULL);
Preferences* dial = new Preferences(this, help);
dial->Show(true);
file_menu->Enable(tm_f_preferences, false);
}
class TQSLConfig {
public:
TQSLConfig() {
callSign = "";
serial = 0;
dxcc = 0;
dupes = 0;
elementBody = wxT("");
locstring = wxT("");
config = NULL;
outstr = NULL;
conv = NULL;
dupesOnly = false;
}
void SaveSettings(gzFile* out, wxString appname);
void RestoreCert(void);
void RestoreConfig(const gzFile& in, bool justDupes);
void ParseLocations(gzFile* out, const tQSL_StationDataEnc loc);
wxConfig *config;
long serial;
int dxcc;
int dupes;
string callSign;
wxString signedCert;
wxString privateKey;
wxString elementBody;
wxString locstring;
gzFile* outstr;
tQSL_Converter conv;
bool dupesOnly;
private:
static void xml_restore_start(void *data, const XML_Char *name, const XML_Char **atts);
static void xml_restore_end(void *data, const XML_Char *name);
static void xml_text(void *data, const XML_Char *text, int len);
static void xml_location_start(void *data, const XML_Char *name, const XML_Char **atts);
static void xml_location_end(void *data, const XML_Char *name);
};
static wxString noBackupPrefs[] = {
wxT("CertFilePath"),
wxT("ImportPath"),
wxT("ExportPath"),
wxT("BackupFolder"),
wxT("QSODataPath")
};
// Save the user's configuration settings - appname is the
// application name (tqslapp)
void TQSLConfig::SaveSettings(gzFile* out, wxString appname) {
tqslTrace("TQSLConfig::SaveSettings", "appname=%s", S(appname));
config = new wxConfig(appname);
wxString name, gname;
long context;
wxString svalue;
long lvalue;
bool bvalue;
double dvalue;
wxArrayString groupNames;
tqslTrace("TQSLConfig::SaveSettings", "... init groups");
groupNames.Add(wxT("/"));
bool more = config->GetFirstGroup(gname, context);
while (more) {
tqslTrace("TQSLConfig::SaveSettings", "... add group %s", S(name));
groupNames.Add(wxT("/") + gname);
more = config->GetNextGroup(gname, context);
}
tqslTrace("TQSLConfig::SaveSettings", "... groups done.");
for (unsigned i = 0; i < groupNames.GetCount(); i++) {
tqslTrace("TQSLConfig::SaveSettings", "Group %d setting path %s", i, S(groupNames[i]));
int err;
config->SetPath(groupNames[i]);
more = config->GetFirstEntry(name, context);
while (more) {
tqslTrace("TQSLConfig::SaveSettings", "name=%s", S(name));
if (name.IsEmpty()) {
more = config->GetNextEntry(name, context);
continue;
}
bool addType = false;
for (size_t j = 0; j < sizeof noBackupPrefs / sizeof noBackupPrefs[0]; j++) {
if (name == noBackupPrefs[j]) {
addType = true;
break;
}
}
if (addType) {
if (gzprintf(*out, "<Setting name=\"%s\" group=\"%s\" osType=\"%s\" ",
(const char *)name.ToUTF8(), (const char *)groupNames[i].ToUTF8(), OSTYPE) < 0) {
throw TQSLException(gzerror(*out, &err));
}
} else {
if (gzprintf(*out, "<Setting name=\"%s\" group=\"%s\" ",
(const char *)name.ToUTF8(), (const char *)groupNames[i].ToUTF8()) < 0) {
throw TQSLException(gzerror(*out, &err));
}
}
wxConfigBase::EntryType etype = config->GetEntryType(name);
switch (etype) {
case wxConfigBase::Type_Unknown:
case wxConfigBase::Type_String:
config->Read(name, &svalue);
long testlong;
if (svalue.ToLong(&testlong)) {
if (gzprintf(*out, "Type=\"Int\" Value=\"%d\"/>\n", testlong) < 0) {
throw TQSLException(gzerror(*out, &err));
}
} else {
urlEncode(svalue);
if (gzprintf(*out, "Type=\"String\" Value=\"%s\"/>\n",
(const char *)svalue.ToUTF8()) < 0) {
throw TQSLException(gzerror(*out, &err));
}
}
break;
case wxConfigBase::Type_Boolean:
config->Read(name, &bvalue);
if (bvalue) {
if (gzprintf(*out, "Type=\"Bool\" Value=\"true\"/>\n") < 0) {
throw TQSLException(gzerror(*out, &err));
}
} else {
if (gzprintf(*out, "Type=\"Bool\" Value=\"false\"/>\n") < 0) {
throw TQSLException(gzerror(*out, &err));
}
} break;
case wxConfigBase::Type_Integer:
config->Read(name, &lvalue);
if (gzprintf(*out, "Type=\"Int\" Value=\"%d\"/>\n", lvalue) < 0)
throw TQSLException(gzerror(*out, &err));
break;
case wxConfigBase::Type_Float:
config->Read(name, &dvalue);
if (gzprintf(*out, "Type=\"Float\" Value=\"%f\"/>\n", dvalue) < 0)
throw TQSLException(gzerror(*out, &err));
break;
}
more = config->GetNextEntry(name, context);
}
}
tqslTrace("TQSLConfig::SaveSettings", "Done.");
config->SetPath(wxT("/"));
return;
}
void
MyFrame::BackupConfig(const wxString& filename, bool quiet) {
tqslTrace("MyFrame::BackupConfig", "filename=%s, quiet=%d", S(filename), quiet);
int i;
gzFile out = 0;
int err;
wxBusyCursor wait;
#ifdef _WIN32
int fd = -1;
#endif
if (lock_db(false) < 0) {
if (quiet) // If there's an active signing thread,
return; // then exit without taking a backup.
wxSafeYield();
wxLogMessage(_("TQSL must wait for other running copies of TQSL to exit before backing up..."));
while (lock_db(false) < 0) {
wxSafeYield();
wxMilliSleep(500);
}
}
try {
#ifdef _WIN32
wchar_t* lfn = utf8_to_wchar(filename.ToUTF8());
fd = _wopen(lfn, _O_WRONLY |_O_CREAT|_O_BINARY, _S_IREAD|_S_IWRITE);
free_wchar(lfn);
if (fd != -1)
out = gzdopen(fd, "wb9");
#else
out = gzopen(filename.ToUTF8(), "wb9");
#endif
if (!out) {
wxLogError(_("Error opening save file %s: %hs"), filename.c_str(), strerror(errno));
unlock_db();
return;
}
TQSLConfig* conf = new TQSLConfig();
if (gzprintf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<TQSL_Configuration>\n") < 0)
throw TQSLException(gzerror(out, &err));
if (gzprintf(out, "<!-- Warning! If you directly edit this file, you are responsible for its content.\n") < 0)
throw TQSLException(gzerror(out, &err));
if (gzprintf(out, "The ARRL's LoTW Help Desk will be unable to assist you. -->\n") < 0)
throw TQSLException(gzerror(out, &err));
if (gzprintf(out, "<Certificates>\n") < 0)
throw TQSLException(gzerror(out, &err));
if (!quiet) {
wxLogMessage(_("Saving callsign certificates"));
wxSafeYield(frame);
} else {
tqslTrace("MyFrame::BackupConfig", "Saving callsign certificates");
}
char buf[8192];
// Save root certificates
check_tqsl_error(tqsl_selectCACertificates(&certlist, &ncerts, "root"));
for (i = 0; i < ncerts; i++) {
if (gzprintf(out, "<RootCert>\n") < 0)
throw TQSLException(gzerror(out, &err));
check_tqsl_error(tqsl_getCertificateEncoded(certlist[i], buf, sizeof buf));
if (gzwrite(out, buf, strlen(buf)) < 0)
throw TQSLException(gzerror(out, &err));
if (gzprintf(out, "</RootCert>\n") < 0)
throw TQSLException(gzerror(out, &err));
}
free_certlist();
// Save CA certificates
check_tqsl_error(tqsl_selectCACertificates(&certlist, &ncerts, "authorities"));
for (i = 0; i < ncerts; i++) {
if (gzprintf(out, "<CACert>\n") < 0)
throw TQSLException(gzerror(out, &err));
check_tqsl_error(tqsl_getCertificateEncoded(certlist[i], buf, sizeof buf));
if (gzwrite(out, buf, strlen(buf)) < 0)
throw TQSLException(gzerror(out, &err));
if (gzprintf(out, "</CACert>\n") < 0)
throw TQSLException(gzerror(out, &err));
}
free_certlist();
tqsl_selectCertificates(&certlist, &ncerts, 0, 0, 0, 0, TQSL_SELECT_CERT_WITHKEYS | TQSL_SELECT_CERT_EXPIRED | TQSL_SELECT_CERT_SUPERCEDED);
for (i = 0; i < ncerts; i++) {
char callsign[64];
long serial = 0;
int dxcc = 0;
int keyonly;
check_tqsl_error(tqsl_getCertificateKeyOnly(certlist[i], &keyonly));
check_tqsl_error(tqsl_getCertificateCallSign(certlist[i], callsign, sizeof callsign));
if (!keyonly) {
check_tqsl_error(tqsl_getCertificateSerial(certlist[i], &serial));
}
check_tqsl_error(tqsl_getCertificateDXCCEntity(certlist[i], &dxcc));
if (!quiet) {
wxLogMessage(wxString(wxT("\t")) + _("Saving callsign certificate for %hs"), callsign);
}
if (gzprintf(out, "<UserCert CallSign=\"%s\" dxcc=\"%d\" serial=\"%d\">\n", callsign, dxcc, serial) < 0)
throw TQSLException(gzerror(out, &err));
if (!keyonly) {
if (gzprintf(out, "<SignedCert>\n") < 0)
throw TQSLException(gzerror(out, &err));
check_tqsl_error(tqsl_getCertificateEncoded(certlist[i], buf, sizeof buf));
if (gzwrite(out, buf, strlen(buf)) < 0)
throw TQSLException(gzerror(out, &err));
if (gzprintf(out, "</SignedCert>\n") < 0)
throw TQSLException(gzerror(out, &err));
}
// Handle case where there's no private key
if (tqsl_getKeyEncoded(certlist[i], buf, sizeof buf) == 0) {
if (gzprintf(out, "<PrivateKey>\n") < 0)
throw TQSLException(gzerror(out, &err));
if (gzwrite(out, buf, strlen(buf)) < 0)
throw TQSLException(gzerror(out, &err));
if (gzprintf(out, "</PrivateKey>\n</UserCert>\n") < 0)
throw TQSLException(gzerror(out, &err));
} else {
// No private key.
if (gzprintf(out, "</UserCert>\n") < 0)
throw TQSLException(gzerror(out, &err));
}
}
free_certlist();
if (gzprintf(out, "</Certificates>\n") < 0)
throw TQSLException(gzerror(out, &err));
if (gzprintf(out, "<Locations>\n") < 0)
throw TQSLException(gzerror(out, &err));
if (!quiet) {
wxLogMessage(_("Saving Station Locations"));
wxSafeYield(frame);
} else {
tqslTrace("MyFrame::BackupConfig", "Saving Station Locations");
}
tQSL_StationDataEnc sdbuf = NULL;
check_tqsl_error(tqsl_getStationDataEnc(&sdbuf));
TQSLConfig* parser = new TQSLConfig();
if (sdbuf)
parser->ParseLocations(&out, sdbuf);
check_tqsl_error(tqsl_freeStationDataEnc(sdbuf));
if (gzprintf(out, "</Locations>\n") < 0)
throw TQSLException(gzerror(out, &err));
if (!quiet) {
wxLogMessage(_("Saving TQSL Preferences"));
wxSafeYield(frame);
} else {
tqslTrace("MyFrame::BackupConfig", "Saving TQSL Preferences - out=0x%lx", reinterpret_cast<void *>(out));
}
if (gzprintf(out, "<TQSLSettings>\n") < 0)
throw TQSLException(gzerror(out, &err));
conf->SaveSettings(&out, wxT("tqslapp"));
delete conf;
tqslTrace("MyFrame::BackupConfig", "Done with settings. out=0x%lx", reinterpret_cast<void *>(out));
if (gzprintf(out, "</TQSLSettings>\n") < 0)
throw TQSLException(gzerror(out, &err));
if (!quiet) {
wxLogMessage(_("Saving QSOs"));
wxSafeYield(frame);
} else {
tqslTrace("MyFrame::BackupConfig", "Saving QSOs");
}
tQSL_Converter conv = NULL;
check_tqsl_error(tqsl_beginConverter(&conv));
tqslTrace("MyFrame::BackupConfig", "beginConverter call success");
if (gzprintf(out, "<DupeDb>\n") < 0)
throw TQSLException(gzerror(out, &err));
char dupekey[256];
char dupedata[256];
int count = 0;
while (true) {
int status = tqsl_getDuplicateRecordsV2(conv, dupekey, dupedata, sizeof(dupekey));
if (status == -1) // End of file
break;
if (status && tQSL_Error == TQSL_DB_ERROR)
break;
check_tqsl_error(status);
wxString dk = wxString::FromUTF8(dupekey);
wxString dd = wxString::FromUTF8(dupedata);
dk = urlEncode(dk);
dd = urlEncode(dd);
strncpy(dupekey, dk.ToUTF8(), sizeof dupekey);
strncpy(dupedata, dd.ToUTF8(), sizeof dupedata);
if (gzprintf(out, "<Dupe key=\"%s\" data=\"%s\" />\n", dupekey, dupedata) < 0)
throw TQSLException(gzerror(out, &err));
if (!quiet && (count++ % 100000) == 0) {
wxSafeYield(frame);
}
}
if (gzprintf(out, "</DupeDb>\n") < 0)
throw TQSLException(gzerror(out, &err));
tqsl_converterCommit(conv);
tqsl_endConverter(&conv);
unlock_db();
tqslTrace("MyFrame::BackupConfig", "Dupes db saved OK");
if (gzprintf(out, "</TQSL_Configuration>\n") < 0)
throw TQSLException(gzerror(out, &err));
if (gzclose(out) != Z_OK)
throw TQSLException(gzerror(out, &err));
if (!quiet) {
wxLogMessage(_("Save operation complete."));
} else {
tqslTrace("MyFrame::BackupConfig", "Save operation complete.");
}
}
catch(TQSLException& x) {
unlock_db();
if (out) gzclose(out);
if (quiet) {
wxString errmsg = wxString::Format(_("Error performing automatic backup: %hs"), x.what());
wxMessageBox(errmsg, _("Backup Error"), wxOK | wxICON_EXCLAMATION, this);
} else {
wxLogError(_("Backup operation failed: %hs"), x.what());
}
}
}
void
MyFrame::OnSaveConfig(wxCommandEvent& WXUNUSED(event)) {
tqslTrace("MyFrame::OnSaveConfig", NULL);
try {
wxString file_default = wxT("tqslconfig.tbk");
wxString filename = wxFileSelector(_("Enter file to save to"), wxT(""),
file_default, wxT(".tbk"), _("Configuration files (*.tbk)|*.tbk|All files (*.*)|*.*"),
wxFD_SAVE|wxFD_OVERWRITE_PROMPT, this);
if (filename.IsEmpty())
return;
BackupConfig(filename, false);
}
catch(TQSLException& x) {
wxLogError(_("Backup operation failed: %hs"), x.what());
}
}
void
restore_user_cert(TQSLConfig* loader) {
if (!loader->dupesOnly) tqslTrace("restore_user_cert", "Restoring certificate for callsign %s", loader->callSign.c_str());
get_certlist(loader->callSign.c_str(), loader->dxcc, true, true, true);
for (int i = 0; i < ncerts; i++) {
long serial;
int dxcc;
int keyonly;
check_tqsl_error(tqsl_getCertificateKeyOnly(certlist[i], &keyonly));
check_tqsl_error(tqsl_getCertificateDXCCEntity(certlist[i], &dxcc));
if (!keyonly) {
check_tqsl_error(tqsl_getCertificateSerial(certlist[i], &serial));
if (serial == loader->serial && dxcc == loader->dxcc) {
// Is there a good private key as well?
int pktype;
pktype = tqsl_getCertificatePrivateKeyType(certlist[i]);
if (pktype == TQSL_PK_TYPE_ERR || pktype == TQSL_PK_TYPE_NONE) {
break;
}
return; // This certificate is already installed.
}
} else {
// See if the keyonly cert is the one we're trying to load
char buf[8192];
check_tqsl_error(tqsl_getKeyEncoded(certlist[i], buf, sizeof buf));
if (!strcmp(buf, loader->privateKey.ToUTF8())) {
return; // Already installed
}
}
}
// There is no certificate matching this callsign/entity/serial.
wxLogMessage(wxString(wxT("\t")) + _("Restoring callsign certificate for %hs"), loader->callSign.c_str());
wxSafeYield(frame);
check_tqsl_error(tqsl_importKeyPairEncoded(loader->callSign.c_str(), "user", loader->privateKey.ToUTF8(), loader->signedCert.ToUTF8()));
}
void
restore_root_cert(TQSLConfig* loader) {
int rval = tqsl_importKeyPairEncoded(NULL, "root", NULL, loader->signedCert.ToUTF8());
if (rval && tQSL_Error != TQSL_CERT_ERROR)
check_tqsl_error(rval);
}
void
restore_ca_cert(TQSLConfig* loader) {
int rval = tqsl_importKeyPairEncoded(NULL, "authorities", NULL, loader->signedCert.ToUTF8());
if (rval && tQSL_Error != TQSL_CERT_ERROR)
check_tqsl_error(rval);
}
void
TQSLConfig::xml_restore_start(void *data, const XML_Char *name, const XML_Char **atts) {
TQSLConfig* loader = reinterpret_cast<TQSLConfig *> (data);
int i;
loader->elementBody = wxT("");
if (!loader->dupesOnly && strcmp(name, "UserCert") == 0) {
for (int i = 0; atts[i]; i+=2) {
if (strcmp(atts[i], "CallSign") == 0) {
loader->callSign = atts[i + 1];
} else if (strcmp(atts[i], "serial") == 0) {
if (strlen(atts[i+1]) == 0) {
loader->serial = 0;
} else {
loader->serial = strtol(atts[i+1], NULL, 10);
}
} else if (strcmp(atts[i], "dxcc") == 0) {
if (strlen(atts[i+1]) == 0) {
loader->dxcc = 0;
} else {
loader->dxcc = strtol(atts[i+1], NULL, 10);
}
}
}
loader->privateKey = wxT("");
loader->signedCert = wxT("");
} else if (!loader->dupesOnly && strcmp(name, "TQSLSettings") == 0) {
wxLogMessage(_("Restoring Preferences"));
wxSafeYield(frame);
loader->config = new wxConfig(wxT("tqslapp"));
} else if (!loader->dupesOnly && strcmp(name, "Setting") == 0) {
wxString sname;
wxString sgroup;
wxString stype;
wxString svalue;
wxString ostype;
for (i = 0; atts[i]; i+=2) {
if (strcmp(atts[i], "name") == 0) {
sname = wxString::FromUTF8(atts[i+1]);
} else if (strcmp(atts[i], "group") == 0) {
sgroup = wxString::FromUTF8(atts[i+1]);
} else if (strcmp(atts[i], "Type") == 0) {
stype = wxString::FromUTF8(atts[i+1]);
} else if (strcmp(atts[i], "Value") == 0) {
svalue = wxString::FromUTF8(atts[i+1]);
} else if (strcmp(atts[i], "osType") == 0) {
ostype = wxString::FromUTF8(atts[i+1]);
}
}
// Don't restore wxHtmlWindow as these settings are OS-specific.
if (sgroup != wxT("wxHtmlWindow") || (ostype.IsEmpty() || (ostype == wxString::FromUTF8(OSTYPE)))) {
loader->config->SetPath(sgroup);
if (stype == wxT("String")) {
svalue.Replace(wxT("<"), wxT("<"), true);
svalue.Replace(wxT(">"), wxT(">"), true);
svalue.Replace(wxT("&"), wxT("&"), true);
if (sname == wxT("BackupFolder") && !svalue.IsEmpty()) {
// If it's the backup directory, don't restore it if the
// referenced directory doesn't exist.
#ifdef _WIN32
struct _stat32 s;
wchar_t* lfn = utf8_to_wchar(svalue.ToUTF8());
int ret = _wstat32(lfn, &s);
free_wchar(lfn);
if (ret == 0) {
#else
struct stat s;
if (lstat(svalue.ToUTF8(), &s) == 0) { // Does it exist?
#endif
if (S_ISDIR(s.st_mode)) { // And is it a directory?
loader->config->Write(sname, svalue); // OK to use it.
}
}
} else {
loader->config->Write(sname, svalue);
}
} else if (stype == wxT("Bool")) {
bool bsw = (svalue == wxT("true"));
loader->config->Write(sname, bsw);
} else if (stype == wxT("Int")) {
long lval = strtol(svalue.ToUTF8(), NULL, 10);
loader->config->Write(sname, lval);
} else if (stype == wxT("Float")) {
double dval = strtod(svalue.ToUTF8(), NULL);
loader->config->Write(sname, dval);
}
loader->config->SetPath(wxT("/"));
}
} else if (!loader->dupesOnly && strcmp(name, "Locations") == 0) {
wxLogMessage(_("Restoring Station Locations"));
wxSafeYield(frame);
loader->locstring = wxT("<StationDataFile>\n");
} else if (!loader->dupesOnly && strcmp(name, "Location") == 0) {
for (i = 0; atts[i]; i+=2) {
wxString attval = wxString::FromUTF8(atts[i+1]);
if (strcmp(atts[i], "name") == 0) {
tqslTrace("TQSLConfig::xml_restore_start", "Restoring location %s", atts[i+1]);
loader->locstring += wxT("<StationData name=\"") + urlEncode(attval) + wxT("\">\n");
break;
}
}
for (i = 0; atts[i]; i+=2) {
wxString attname = wxString::FromUTF8(atts[i]);
wxString attval = wxString::FromUTF8(atts[i+1]);
if (strcmp(atts[i], "name") != 0) {
loader->locstring += wxT("<") + attname + wxT(">") +
urlEncode(attval) + wxT("</") + attname + wxT(">\n");
}
}
} else if (strcmp(name, "DupeDb") == 0) {
if (!loader->dupesOnly) wxLogMessage(_("Restoring QSO records"));
wxSafeYield(frame);
check_tqsl_error(tqsl_beginConverter(&loader->conv));
} else if (strcmp(name, "Dupe") == 0) {
const char *dupekey = NULL;
const char *dupedata = NULL;
for (i = 0; atts[i]; i+=2) {
if (strcmp(atts[i], "key") == 0) {
dupekey = atts[i+1];
}
if (strcmp(atts[i], "data") == 0) {
dupedata = atts[i+1];
}
}
if (dupedata == NULL) {
dupedata = "D"; // Old school dupe record
}
int status = tqsl_putDuplicateRecord(loader->conv, dupekey, dupedata, dupekey ? strlen(dupekey) : 0);
if (status > 0) { // Error writing that record
check_tqsl_error(status);
}
if ((loader->dupes++ % 100000) == 0) {
wxSafeYield(frame);
}
}
}
void
TQSLConfig::xml_restore_end(void *data, const XML_Char *name) {
TQSLConfig* loader = reinterpret_cast<TQSLConfig *> (data);
if (loader->dupesOnly) {
if (strcmp(name, "DupeDb") == 0) {
check_tqsl_error(tqsl_converterCommit(loader->conv));
check_tqsl_error(tqsl_endConverter(&loader->conv));
}
loader->elementBody = wxT("");
return;
}
if (strcmp(name, "SignedCert") == 0) {
loader->signedCert = loader->elementBody.Trim(false);
} else if (strcmp(name, "PrivateKey") == 0) {
loader->privateKey = loader->elementBody.Trim(false);
} else if (strcmp(name, "RootCert") == 0) {
loader->signedCert = loader->elementBody.Trim(false);
restore_root_cert(loader);
} else if (strcmp(name, "CACert") == 0) {
loader->signedCert = loader->elementBody.Trim(false);
restore_ca_cert(loader);
} else if (strcmp(name, "UserCert") == 0) {
restore_user_cert(loader);
} else if (strcmp(name, "Location") == 0) {
loader->locstring += wxT("</StationData>\n");
} else if (strcmp(name, "Locations") == 0) {
loader->locstring += wxT("</StationDataFile>\n");
tqslTrace("TQSLConfig::xml_restore_end", "Merging station locations");
if (tqsl_mergeStationLocations(loader->locstring.ToUTF8()) != 0) {
char buf[500];
strncpy(buf, getLocalizedErrorString().ToUTF8(), sizeof buf);
wxLogError(wxString::Format(wxString(wxT("\t")) + _("Error importing station locations: %hs"), buf));
}
tqslTrace("TQSLConfig::xml_restore_end", "Completed merging station locations");
} else if (strcmp(name, "TQSLSettings") == 0) {
loader->config->Flush(false);
}
loader->elementBody = wxT("");
}
void
TQSLConfig::xml_location_start(void *data, const XML_Char *name, const XML_Char **atts) {
TQSLConfig* parser = reinterpret_cast<TQSLConfig *> (data);
int err;
if (strcmp(name, "StationDataFile") == 0)
return;
if (strcmp(name, "StationData") == 0) {
wxString locname = wxString::FromUTF8(atts[1]);
urlEncode(locname);
if (gzprintf(*parser->outstr, "<Location name=\"%s\"", (const char *)locname.ToUTF8()) < 0)
throw TQSLException(gzerror(*parser->outstr, &err));
}
}
void
TQSLConfig::xml_location_end(void *data, const XML_Char *name) {
TQSLConfig* parser = reinterpret_cast<TQSLConfig *> (data);
int err;
if (strcmp(name, "StationDataFile") == 0)
return;
if (strcmp(name, "StationData") == 0) {
if (gzprintf(*parser->outstr , " />\n") < 0)
throw TQSLException(gzerror(*parser->outstr, &err));
return;
}
// Anything else is a station attribute. Add it to the definition.
parser->elementBody.Trim(false);
parser->elementBody.Trim(true);
urlEncode(parser->elementBody);
if (gzprintf(*parser->outstr, " %s=\"%s\"", name, (const char *)parser->elementBody.ToUTF8()) < 0)
throw TQSLException(gzerror(*parser->outstr, &err));
parser->elementBody = wxT("");
}
void
TQSLConfig::xml_text(void *data, const XML_Char *text, int len) {
TQSLConfig* loader = reinterpret_cast<TQSLConfig *>(data);
char buf[512];
memcpy(buf, text, len);
buf[len] = '\0';
loader->elementBody += wxString::FromUTF8(buf);
}
void
TQSLConfig::RestoreConfig(const gzFile& in, bool justDupes) {
tqslTrace("TQSLConfig::RestoreConfig", NULL);
XML_Parser xp = XML_ParserCreate(0);
XML_SetUserData(xp, reinterpret_cast<void *>(this));
XML_SetStartElementHandler(xp, &TQSLConfig::xml_restore_start);
XML_SetEndElementHandler(xp, &TQSLConfig::xml_restore_end);
XML_SetCharacterDataHandler(xp, &TQSLConfig::xml_text);
dupesOnly = justDupes;
char buf[4096];
wxBusyCursor wait;
if (!dupesOnly) wxLogMessage(_("Restoring Callsign Certificates"));
wxSafeYield(frame);
int rcount = 0;
do {
rcount = gzread(in, buf, sizeof(buf));
if (rcount > 0) {
if (XML_Parse(xp, buf, rcount, 0) == 0) {
wxLogError(_("Error parsing saved configuration file: %hs"), XML_ErrorString(XML_GetErrorCode(xp)));
XML_ParserFree(xp);
return;
}
}
} while (rcount > 0);
if (!gzeof(in)) {
int gerr;
wxLogError(_("Error parsing saved configuration file: %hs"), gzerror(in, &gerr));
XML_ParserFree(xp);
return;
}
if (XML_Parse(xp, "", 0, 1) == 0) {
wxLogError(_("Error parsing saved configuration file: %hs"), XML_ErrorString(XML_GetErrorCode(xp)));
XML_ParserFree(xp);
return;
}
if (!dupesOnly) wxLogMessage(_("Restore Complete."));
}
void
TQSLConfig::ParseLocations(gzFile* out, const tQSL_StationDataEnc loc) {
tqslTrace("TQSL::ParseLocations", "loc=%s", loc);
XML_Parser xp = XML_ParserCreate(0);
XML_SetUserData(xp, reinterpret_cast<void *>(this));
XML_SetStartElementHandler(xp, &TQSLConfig::xml_location_start);
XML_SetEndElementHandler(xp, &TQSLConfig::xml_location_end);
XML_SetCharacterDataHandler(xp, &TQSLConfig::xml_text);
outstr = out;
if (XML_Parse(xp, loc, strlen(loc), 1) == 0) {
wxLogError(_("Error parsing station location file: %hs"), XML_ErrorString(XML_GetErrorCode(xp)));
XML_ParserFree(xp);
return;
}
}
void
MyFrame::DoRestoreConfig(wxString& filename) {
#ifdef _WIN32
int fd = -1;
#endif
gzFile in = 0;
try {
#ifdef _WIN32
wchar_t* lfn = utf8_to_wchar(filename.ToUTF8());
fd = _wopen(lfn, _O_RDONLY|_O_BINARY);
free_wchar(lfn);
if (fd != -1)
in = gzdopen(fd, "rb");
#else
in = gzopen(filename.ToUTF8(), "rb");
#endif
if (!in) {
wxLogError(_("Error opening save file %s: %hs"), filename.c_str(), strerror(errno));
return;
}
TQSLConfig loader;
loader.RestoreConfig(in, false);
cert_tree->Build(CERTLIST_FLAGS);
loc_tree->Build();
LocTreeReset();
CertTreeReset();
gzclose(in);
}
catch(TQSLException& x) {
wxLogError(_("Restore operation failed: %hs"), x.what());
gzclose(in);
}
}
void
MyFrame::OnLoadConfig(wxCommandEvent& WXUNUSED(event)) {
tqslTrace("MyFrame::OnLoadConfig", NULL);
wxString filename = wxFileSelector(_("Select saved configuration file"), wxT(""),
wxT("tqslconfig.tbk"), wxT("tbk"), _("Saved configuration files (*.tbk)|*.tbk"),
wxFD_OPEN|wxFD_FILE_MUST_EXIST);
if (filename.IsEmpty())
return;
DoRestoreConfig(filename);
}
// Reload the QSO database from an autobackup if it exists.
void
MyFrame::LoadDupes(void) {
tqslTrace("MyFrame::LoadDupes", NULL);
wxConfig *config = reinterpret_cast<wxConfig *>(wxConfig::Get());
wxString bdir = config->Read(wxT("BackupFolder"), wxString::FromUTF8(tQSL_BaseDir));
if (bdir.Trim(true).Trim(false).IsEmpty())
bdir = wxString::FromUTF8(tQSL_BaseDir);
#ifdef _WIN32
bdir += wxT("\\tqslconfig.tbk");
#else
bdir += wxT("/tqslconfig.tbk");
#endif
tqslTrace("MyFrame::LoadDupes", "Restoring Backup %s", S(bdir));
#ifdef _WIN32
int fd = -1;
#endif
gzFile in = 0;
try {
#ifdef _WIN32
wchar_t* lfn = utf8_to_wchar(bdir.ToUTF8());
fd = _wopen(lfn, _O_RDONLY|_O_BINARY);
free_wchar(lfn);
if (fd != -1)
in = gzdopen(fd, "rb");
#else
in = gzopen(bdir.ToUTF8(), "rb");
#endif
if (!in) {
return; // Not able to restore
}
TQSLConfig loader;
loader.RestoreConfig(in, true);
gzclose(in);
}
catch(...) {
gzclose(in);
}
}
QSLApp::QSLApp() : wxApp() {
lang = wxLANGUAGE_UNKNOWN;
locale = NULL;
#ifdef __WXMAC__ // Tell wx to put these items on the proper menu
wxApp::s_macAboutMenuItemId = long(tm_h_about);
wxApp::s_macPreferencesMenuItemId = long(tm_f_preferences);
wxApp::s_macExitMenuItemId = long(tm_f_exit);
#endif
wxConfigBase::Set(new wxConfig(wxT("tqslapp")));
}
/*
wxLog *
QSLApp::CreateLogTarget() {
cerr << "called" << endl;
MyFrame *mf = (MyFrame *)GetTopWindow();
if (mf) {
LogList *log = new LogList(this);
#if wxMAJOR_VERSION > 2
log->SetFormatter(new SimpleLogFormatter);
#endif
return log;
}
return 0;
}
*/
MyFrame *
QSLApp::GUIinit(bool checkUpdates, bool quiet) {
tqslTrace("QSLApp::GUIinit", "checkUpdates=%d", checkUpdates);
int x, y, w, h;
bool maximized;
wxConfig *config = reinterpret_cast<wxConfig *>(wxConfig::Get());
config->Read(wxT("MainWindowX"), &x, 50);
config->Read(wxT("MainWindowY"), &y, 50);
config->Read(wxT("MainWindowWidth"), &w, 800);
config->Read(wxT("MainWindowHeight"), &h, 600);
config->Read(wxT("MainWindowMaximized"), &maximized, false);
if (w < MAIN_WINDOW_MIN_WIDTH) w = MAIN_WINDOW_MIN_WIDTH;
if (h < MAIN_WINDOW_MIN_HEIGHT) h = MAIN_WINDOW_MIN_HEIGHT;
frame = new MyFrame(wxT("TQSL"), x, y, w, h, checkUpdates, quiet, locale);
frame->SetMinSize(wxSize(w, h));
#ifdef __WXGTK__
/*
* GTK spews messages to stderr. No way to stop it. So...
*/
int oldStderr = dup(STDERR_FILENO);
int null_fd = open("/dev/null", O_WRONLY);
dup2(null_fd, STDERR_FILENO);
origStderr = fdopen(oldStderr, "a");
#else
origStderr = stderr;
#endif
if (maximized)
frame->Maximize();
#ifdef _WIN32
if (!quiet) {
HWND window = frame->GetHWND();
// If the window isn't visible, force it to be visible
if (MonitorFromWindow(window, MONITOR_DEFAULTTONULL) == NULL)
frame->CenterOnScreen();
}
#endif
if (checkUpdates)
frame->FirstTime();
frame->Show(!quiet);
if (checkUpdates && !quiet)
frame->SetFocus();
SetTopWindow(frame);
return frame;
}
// Override OnRun so we can have a last-chance exception handler
// in case something doesn't handle an error.
int
QSLApp::OnRun() {
tqslTrace("QSLApp::OnRun", NULL);
try {
if (m_exitOnFrameDelete == Later)
m_exitOnFrameDelete = Yes;
return MainLoop();
}
catch(const TQSLException& x) {
string msg = x.what();
cerr << "An exception has occurred! " << msg << endl;
tqslTrace("QSLApp::OnRun", "Last chance handler, string=%s", (const char *)msg.c_str());
wxLogError(wxT("%hs"), x.what());
exitNow(TQSL_EXIT_TQSL_ERROR, false);
}
catch(const std::exception& x) {
string msg = x.what();
cerr << "An exception has occurred! " << msg << endl;
tqslTrace("QSLApp::OnRun", "Last chance handler, string=%s", (const char *)msg.c_str());
wxLogError(wxT("%hs"), x.what());
exitNow(TQSL_EXIT_TQSL_ERROR, false);
}
catch ( ... ) {
cerr << "Unknown exception" << endl;
exitNow(TQSL_EXIT_TQSL_ERROR, false);
}
return 0;
}
static vector<wxLanguage> langIds;
static wxArrayString langNames;
static void
initLang() {
if (langIds.size() == 0) {
char langfile[TQSL_MAX_PATH_LEN];
FILE *lfp;
#ifdef _WIN32
snprintf(langfile, sizeof langfile, "%s\\languages.dat", tQSL_RsrcDir);
wchar_t *wfilename = utf8_to_wchar(langfile);
if ((lfp = _wfopen(wfilename, L"rb, ccs=UTF-8")) == NULL) {
free_wchar(wfilename);
#else
snprintf(langfile, sizeof langfile, "%s/languages.dat", tQSL_RsrcDir);
if ((lfp = fopen(langfile, "rb")) == NULL) {
#endif
goto nosyslang;
}
#ifdef _WIN32
free_wchar(wfilename);
#endif
char lBuf[128];
while (fgets(lBuf, sizeof lBuf, lfp)) {
wxStringTokenizer langData(wxString::FromUTF8(lBuf), wxT(","));
langNames.Add(langData.GetNextToken());
langIds.push_back(static_cast<wxLanguage>(strtol(langData.GetNextToken().ToUTF8(), NULL, 10)));
}
fclose(lfp);
nosyslang:
// Now merge in the user directory copy
#ifdef _WIN32
snprintf(langfile, sizeof langfile, "%s\\languages.dat", tQSL_BaseDir);
wfilename = utf8_to_wchar(langfile);
if ((lfp = _wfopen(wfilename, L"rb, ccs=UTF-8")) == NULL) {
free_wchar(wfilename);
#else
snprintf(langfile, sizeof langfile, "%s/languages.dat", tQSL_BaseDir);
if ((lfp = fopen(langfile, "rb")) == NULL) {
#endif
return;
}
#ifdef _WIN32
free_wchar(wfilename);
#endif
while (fgets(lBuf, sizeof lBuf, lfp)) {
wxStringTokenizer langData(wxString::FromUTF8(lBuf), wxT(","));
wxString ln = langData.GetNextToken();
bool found = false;
for (size_t i = 0; i < langIds.size(); i++) {
if (langNames[i] == ln) {
found = true;
break;
}
}
if (!found) {
langNames.Add(ln);
langIds.push_back(static_cast<wxLanguage>(strtol(langData.GetNextToken().ToUTF8(), NULL, 10)));
}
}
fclose(lfp);
}
return;
}
bool
QSLApp::OnInit() {
frame = 0;
long lng = -1;
#ifdef __LINUX__
// stuff to make flatpak work - we need to make a fake desktop app to allow mime
// type processing to work. Can't call it '.desktop' because flatpak builder objects.
if (wxFileExists(wxT("/.flatpak-info"))) {
wxString rsrcDir;
if (!wxGetEnv(wxT("APPDIR"), &rsrcDir)) {
wxString home;
wxGetEnv(wxT("HOME"), &home);
rsrcDir = home + wxT("/.tqsl");
}
if (!wxDirExists(rsrcDir)) {
wxMkdir(rsrcDir);
}
wxString apps = rsrcDir + wxT("/applications");
if (!wxDirExists(apps)) {
wxMkdir(apps);
}
wxString deflist = apps + wxT("/defaults.list");
wxString dtop = apps + wxT("/fakebrowser.desktop");
if (!wxFileExists(deflist)) {
FILE * f = fopen(deflist.ToUTF8(), "w");
if (f) {
fprintf(f, "[Default Applications]\ntext/html=fakebrowser.desktop\n");
fclose(f);
}
}
if (!wxFileExists(dtop)) {
FILE * f = fopen(dtop.ToUTF8(), "w");
if (f) {
fprintf(f, "[Desktop Entry]\nName=FakeBrowser\nGenericName=Web Browser\nComment=Browse the Web\nExec=true\nMimeType=text/html;text/xml;\nIcon=Firefox\n");
fclose(f);
}
}
wxString oldXDG;
wxGetEnv(wxT("XDG_DATA_DIRS"), &oldXDG);
if (!oldXDG.IsEmpty()) {
if (oldXDG.Find(rsrcDir) == wxNOT_FOUND) {
wxSetEnv(wxT("XDG_DATA_DIRS"), oldXDG + wxT(":") + rsrcDir);
}
} else {
wxSetEnv(wxT("XDG_DATA_DIRS"), rsrcDir);
}
}
#endif
wxConfig *config = reinterpret_cast<wxConfig *>(wxConfig::Get());
wxString rdir;
config->Read(wxT("rsrcDir"), &rdir);
if (!rdir.IsEmpty()) {
wxSetEnv(wxT("TQSLDIR"), rdir);
}
#ifdef _WIN32
bool disa;
config->Read(wxT("DisableAdminCheck"), &disa, false);
if (!disa && IsElevated(NULL) == S_OK) {
TOKEN_ELEVATION_TYPE tet = TokenElevationTypeDefault;
GetElevationType(&tet);
if (tet == TokenElevationTypeFull) {
wxArrayString ch;
ch.Add(_("Exit TQSL so I can re-run as a normal user"));
ch.Add(_("Allow TQSL to continue this time."));
ch.Add(_("Always allow running as Administrator."));
wxSingleChoiceDialog dial(frame, _("TQSL must not be run 'As Administrator'"), _("Administrator Error"), ch);
int res = dial.ShowModal();
switch (res) {
case wxID_CANCEL:
exitNow(TQSL_EXIT_TQSL_ERROR, quiet);
break;
case wxID_OK:
int sel = dial.GetSelection();
switch (sel) {
case 0:
exitNow(TQSL_EXIT_TQSL_ERROR, quiet);
case 1:
break;
case 2:
config->Write(wxT("DisableAdminCheck"), true);
break;
}
break;
}
}
}
#endif // Administrator check
int major, minor;
if (tqsl_getConfigVersion(&major, &minor)) {
wxMessageBox(getLocalizedErrorString(), _("Error"), wxOK | wxICON_ERROR, frame);
exitNow(TQSL_EXIT_TQSL_ERROR, quiet);
}
initLang();
config->Read(wxT("Language"), &lng, wxLANGUAGE_UNKNOWN);
lang = (wxLanguage) lng;
if (lang == wxLANGUAGE_UNKNOWN) {
lang = wxLANGUAGE_DEFAULT;
}
for (lng = 0; (unsigned) lng < langIds.size(); lng++) {
if (lang == langIds[lng])
break;
}
#ifdef __WXGTK__
// Add locale search path for where we install language files
locale->AddCatalogLookupPathPrefix(wxT("/usr/local/share/locale"));
#endif
lang = langWX2toWX3(lang); // Translate to wxWidgets 3 language ID.
#ifdef __WXMAC__
wxLogLevel saved = wxLog::GetLogLevel();// Suppress warnings from
wxLog::SetLogLevel(wxLOG_Error); // Chinese locale issue
#endif
locale = new wxLocale(lang);
#ifdef __WXMAC__
wxLog::SetLogLevel(saved); // Restore logging
#endif
if (!locale) {
#ifdef DEBUG
wxLogError(wxT("This language is not supported by the system."));
#endif
locale = new wxLocale(wxLANGUAGE_DEFAULT);
}
// Add a subdirectory for language files
locale->AddCatalogLookupPathPrefix(wxT("lang"));
#ifdef _WIN32
locale->AddCatalogLookupPathPrefix(wxString::FromUTF8(tQSL_BaseDir) + wxT("\\lang"));
#else
locale->AddCatalogLookupPathPrefix(wxString::FromUTF8(tQSL_BaseDir) + wxT("/lang"));
#endif
// Initialize the catalogs we'll be using
#if defined(TQSL_TESTING)
const wxLanguageInfo* pinfo = wxLocale::GetLanguageInfo(lang);
// Enabling this causes the error to pop up for any locale where we don't have a translation.
// This should not be used in production.
if (!locale->AddCatalog(wxT("tqslapp"))) {
const char* cname = pinfo->CanonicalName.ToUTF8();
wxLogError(wxT("Can't find the tqslapp catalog for locale '%s'."), cname);
}
#else
locale->AddCatalog(wxT("tqslapp"));
#endif
if (!locale->AddCatalog(wxT("wxstd-3.2"))) {
if (!locale->AddCatalog(wxT("wxstd3"))) {
locale->AddCatalog(wxT("wxstd"));
}
}
// this catalog is installed in standard location on Linux systems and
// shows that you may make use of the standard message catalogs as well
//
// If it's not installed on your system, it is just silently ignored
#ifdef __LINUX__
{
wxLogNull nolog;
locale->AddCatalog(wxT("fileutils"));
}
#endif
wxFileSystem::AddHandler(new tqslInternetFSHandler());
// Allow JAWS for windows to speak the context-sensitive help.
wxHelpProvider::Set(new wxSimpleHelpProvider());
/*
* Test the dupes database - if it doesn't exist, create it and
* load the last backup.
*/
tQSL_Converter conv = NULL;
tqsl_beginConverter(&conv);
if (!tqsl_putDuplicateRecord(conv, NULL, NULL, 0) && tQSL_Error == TQSL_NEW_UPLOAD_DB) {
frame->LoadDupes();
} else {
tqsl_endConverter(&conv); // No reload needed
}
//short circuit if no arguments
if (argc <= 1) {
GUIinit(true, quiet);
return true;
}
tQSL_Location loc = NULL;
wxString locname;
bool suppressdate = false;
int action = TQSL_ACTION_UNSPEC;
int logverify;
bool upload = false;
char *password = NULL;
char *defcall = NULL;
wxString infile(wxT(""));
wxString outfile(wxT(""));
wxString importfile(wxT(""));
wxString diagfile(wxT(""));
config->Read(wxT("LogVerify"), &logverify, TQSL_LOC_REPORT);
wxCmdLineParser parser;
#if wxMAJOR_VERSION > 2 || (wxMAJOR_VERSION == 2 && wxMINOR_VERSION == 9)
#define arg(x) (x)
#define i18narg(x) (x)
#else
#define arg(x) wxT(x)
#define i18narg(x) _(x)
#endif
// arg letters used abcdef.hi..lmnopq.stuvwx..
static const wxCmdLineEntryDesc cmdLineDesc[] = {
{ wxCMD_LINE_OPTION, arg("a"), arg("action"), i18narg("Specify dialog action - abort, all, compliant or ask") },
{ wxCMD_LINE_OPTION, arg("b"), arg("begindate"), i18narg("Specify start date for QSOs to sign") },
{ wxCMD_LINE_OPTION, arg("c"), arg("callsign"), i18narg("Specify default callsign for log signing") },
{ wxCMD_LINE_SWITCH, arg("d"), arg("nodate"), i18narg("Suppress date range dialog") },
{ wxCMD_LINE_OPTION, arg("e"), arg("enddate"), i18narg("Specify end date for QSOs to sign") },
{ wxCMD_LINE_OPTION, arg("f"), arg("verify"), i18narg("Specify QSO verification action - ignore, report or update") },
// not used - "g"
{ wxCMD_LINE_SWITCH, arg("h"), arg("help"), i18narg("Display command line help"), wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
{ wxCMD_LINE_OPTION, arg("i"), arg("import"), i18narg("Import a certificate file (.p12 or .tq6)") },
// not used - "j", "k"
{ wxCMD_LINE_OPTION, arg("l"), arg("location"), i18narg("Selects Station Location") },
// not used - "m"
{ wxCMD_LINE_SWITCH, arg("n"), arg("updates"), i18narg("Check for updates to tqsl and the configuration file") },
{ wxCMD_LINE_OPTION, arg("o"), arg("output"), i18narg("Output file name (defaults to input name minus extension plus .tq8") },
{ wxCMD_LINE_OPTION, arg("p"), arg("password"), i18narg("Passphrase for the signing key") },
{ wxCMD_LINE_SWITCH, arg("q"), arg("quiet"), i18narg("Quiet Mode - same behavior as -x") },
// not used - "r"
{ wxCMD_LINE_SWITCH, arg("s"), arg("editlocation"), i18narg("Edit (if used with -l) or create Station Location") },
{ wxCMD_LINE_OPTION, arg("t"), arg("diagnose"), i18narg("File name for diagnostic tracking log") },
{ wxCMD_LINE_SWITCH, arg("u"), arg("upload"), i18narg("Upload after signing instead of saving") },
{ wxCMD_LINE_SWITCH, arg("v"), arg("version"), i18narg("Display the version information and exit") },
{ wxCMD_LINE_SWITCH, arg("w"), arg("wipe"), i18narg("Wipe the TQSL uploads database") },
{ wxCMD_LINE_SWITCH, arg("x"), arg("batch"), i18narg("Exit after processing log (otherwise start normally)") },
// not used - "y", "z"
{ wxCMD_LINE_PARAM, NULL, NULL, i18narg("Input ADIF or Cabrillo log file to sign"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
{ wxCMD_LINE_NONE }
};
// Lowercase command options
origCommandLine = argv[0];
char** myArgv = new char*[argc];
int myArgc;
#if wxMAJOR_VERSION == 2
wxString av = argv[0];
myArgv[0] = strdup(av.ToUTF8());
#else
myArgv[0] = strdup(argv[0].ToUTF8());
#endif
myArgc = 1;
for (int i = 1; i < argc; i++) {
if ((i + 1) < argc) {
wxString av1 = argv[i+1];
#if wxMAJOR_VERSION == 2
av = argv[i];
if (av == wxT("-p") && av1.IsEmpty()) { // -p with blank password
#else
if (argv[i] == "-p" && av1.IsEmpty()) { // -p with blank password
#endif
i++; // skip -p and password
continue;
}
}
origCommandLine += wxT(" ");
#if wxMAJOR_VERSION == 2
myArgv[myArgc] = strdup(wxString(argv[i]).ToUTF8());
#else
myArgv[myArgc] = strdup(argv[i].ToUTF8());
#endif
#ifdef _WIN32
if (myArgv[myArgc][0] == '-' || myArgv[myArgc][0] == '/')
if (wxIsalpha(myArgv[myArgc][1]) && wxIsupper(myArgv[myArgc][1]))
myArgv[myArgc][1] = wxTolower(myArgv[myArgc][1]);
#endif
#if wxMAJOR_VERSION == 2
origCommandLine += wxString::FromUTF8(myArgv[myArgc]);
#else
origCommandLine += myArgv[myArgc];
#endif
myArgc++;
}
parser.SetCmdLine(myArgc, myArgv);
parser.SetDesc(cmdLineDesc);
// only allow "-" for options, otherwise "/path/something.adif"
// is parsed as "-path"
//parser.SetSwitchChars(wxT("-")); //by default, this is '-' on Unix, or '-' or '/' on Windows. We should respect the Win32 conventions, but allow the cross-platform Unix one for cross-plat loggers
int parseStatus = parser.Parse(true);
if (parseStatus == -1) { // said "-h"
return false;
}
// Always display TQSL version
if ((!parser.Found(wxT("n"))) || parser.Found(wxT("v")) || parser.Found(wxT("w"))) {
cerr << "TQSL Version " TQSL_VERSION " " TQSL_BUILD "\n";
}
if (parseStatus != 0) {
exitNow(TQSL_EXIT_COMMAND_ERROR, quiet);
}
// version already displayed - just exit
if (parser.Found(wxT("v"))) {
return false;
}
// Wipe the uploads DB
if (parser.Found(wxT("w"))) {
cout << "Do you really want to wipe the uploads database? Enter APPROVE to allow: ";
string response;
getline(cin, response);
if (response == "APPROVE") {
tqsl_removeUploadDatabase();
} else {
cout << "Aborted." << endl;
}
return false;
}
if (parser.Found(wxT("x")) || parser.Found(wxT("q"))) {
quiet = true;
LogStderr *logger = new LogStderr();
#if wxMAJOR_VERSION > 2
logger->SetFormatter(new SimpleLogFormatter);
#endif
wxLog::SetActiveTarget(logger);
}
if (parser.Found(wxT("t"), &diagfile)) {
if (tqsl_openDiagFile(diagfile.ToUTF8())) {
cerr << "Error opening diagnostic log " << diagfile.ToUTF8() << ": " << strerror(errno) << endl;
} else {
wxString about = getAbout();
tqslTrace(NULL, "TQSL Diagnostics\r\n%s\n\n", (const char *)about.ToUTF8());
tqslTrace(NULL, "Command Line: %s\r\n", (const char *)origCommandLine.ToUTF8());
if (tqsl_init()) {
wxLogError(getLocalizedErrorString());
}
tqslTrace(NULL, "Working Directory: %s\r\n", tQSL_BaseDir);
}
}
if (tqsl_init()) { // Init tqsllib
wxLogError(getLocalizedErrorString());
}
// check for logical command switches
if (parser.Found(wxT("o")) && parser.Found(wxT("u"))) {
cerr << "Option -o cannot be combined with -u" << endl;
exitNow(TQSL_EXIT_COMMAND_ERROR, quiet);
}
if ((parser.Found(wxT("o")) || parser.Found(wxT("u"))) && parser.Found(wxT("s"))) {
cerr << "Option -s cannot be combined with -u or -o" << endl;
exitNow(TQSL_EXIT_COMMAND_ERROR, quiet);
}
if (parser.Found(wxT("s")) && parser.GetParamCount() > 0) {
cerr << "Option -s cannot be combined with an input file" << endl;
exitNow(TQSL_EXIT_COMMAND_ERROR, quiet);
}
// Request to check for new versions of tqsl/config/certs
if (parser.Found(wxT("n"))) {
if (parser.Found(wxT("i")) || parser.Found(wxT("o")) ||
parser.Found(wxT("s")) || parser.Found(wxT("u"))) {
cerr << "Option -n cannot be combined with any other options" << endl;
exitNow(TQSL_EXIT_COMMAND_ERROR, quiet);
}
frame = GUIinit(false, true);
frame->Show(false);
// Check for updates then bail out.
LogStderr *logger = new LogStderr();
#if wxMAJOR_VERSION > 2
logger->SetFormatter(new SimpleLogFormatter);
#endif
wxLog::SetActiveTarget(logger);
frame->DoUpdateCheck(false, true);
return(false);
}
frame = GUIinit(!quiet, quiet);
if (quiet) {
frame->Show(false);
}
if (parser.Found(wxT("l"), &locname)) {
locname.Trim(true); // clean up whitespace
locname.Trim(false);
if (locname.IsEmpty()) {
logverify = TQSL_LOC_UPDATE; // force update mode
} else {
tqsl_endStationLocationCapture(&loc);
if (tqsl_getStationLocation(&loc, locname.ToUTF8())) {
if (quiet) {
wxLogError(getLocalizedErrorString());
exitNow(TQSL_EXIT_COMMAND_ERROR, quiet);
} else {
wxMessageBox(getLocalizedErrorString(), ErrorTitle, wxOK | wxICON_ERROR | wxCENTRE, frame);
return false;
}
}
}
}
wxString call;
if (parser.Found(wxT("c"), &call)) {
call.Trim(true);
call.Trim(false);
defcall = strdup(call.MakeUpper().ToUTF8());
}
wxString pwd;
if (parser.Found(wxT("p"), &pwd)) {
password = strdup(pwd.ToUTF8());
utf8_to_ucs2(password, unipwd, sizeof unipwd);
}
if (parser.Found(wxT("o"))) {
parser.Found(wxT("o"), &outfile);
}
if (parser.Found(wxT("d"))) {
suppressdate = true;
}
wxString start = wxT("");
wxString end = wxT("");
tQSL_Date* startdate = NULL;
tQSL_Date* enddate = NULL;
tQSL_Date s, e;
if (parser.Found(wxT("b"), &start)) {
if (start.Trim().IsEmpty()) {
startdate = NULL;
} else if (tqsl_initDate(&s, start.ToUTF8()) || !tqsl_isDateValid(&s)) {
if (quiet) {
wxLogError(_("Start date of %s is invalid"), start.c_str());
exitNow(TQSL_EXIT_COMMAND_ERROR, quiet);
} else {
wxMessageBox(wxString::Format(_("Start date of %s is invalid"), start.c_str()), ErrorTitle, wxOK | wxICON_ERROR | wxCENTRE, frame);
return false;
}
}
startdate = &s;
}
if (parser.Found(wxT("e"), &end)) {
if (end.Trim().IsEmpty()) {
enddate = NULL;
} else if (tqsl_initDate(&e, end.ToUTF8()) || !tqsl_isDateValid(&e)) {
if (quiet) {
wxLogError(_("End date of %s is invalid"), end.c_str());
exitNow(TQSL_EXIT_COMMAND_ERROR, quiet);
} else {
wxMessageBox(wxString::Format(_("End date of %s is invalid"), end.c_str()), ErrorTitle, wxOK | wxICON_ERROR | wxCENTRE, frame);
return false;
}
}
enddate = &e;
}
wxString act;
if (parser.Found(wxT("a"), &act)) {
if (!act.CmpNoCase(wxT("abort"))) {
action = TQSL_ACTION_ABORT;
} else if (!act.CmpNoCase(wxT("compliant"))) {
action = TQSL_ACTION_NEW;
} else if (!act.CmpNoCase(wxT("all"))) {
action = TQSL_ACTION_ALL;
} else if (!act.CmpNoCase(wxT("ask"))) {
action = TQSL_ACTION_ASK;
} else {
char tmp[100];
strncpy(tmp, (const char *)act.ToUTF8(), sizeof tmp);
tmp[sizeof tmp -1] = '\0';
if (quiet)
wxLogMessage(_("The -a parameter %hs is not recognized"), tmp);
else
cerr << "The action parameter " << tmp << " is not recognized" << endl;
exitNow(TQSL_EXIT_COMMAND_ERROR, quiet);
}
}
wxString verify;
if (parser.Found(wxT("f"), &verify)) { // action for verifying QSOs
if (!verify.CmpNoCase(wxT("ignore"))) {
logverify = TQSL_LOC_IGNORE;
} else if (!verify.CmpNoCase(wxT("report"))) {
logverify = TQSL_LOC_REPORT;
} else if (!verify.CmpNoCase(wxT("update"))) {
logverify = TQSL_LOC_UPDATE;
} else {
char tmp[100];
strncpy(tmp, (const char *)verify.ToUTF8(), sizeof tmp);
tmp[sizeof tmp -1] = '\0';
if (quiet)
// TRANSLATORS: -f is the command line switch for log QTH handling
wxLogMessage(_("The -f parameter %hs is not recognized"), tmp);
else
cerr << "The verify parameter " << tmp << " is not recognized" << endl;
exitNow(TQSL_EXIT_COMMAND_ERROR, quiet);
}
}
if (parser.Found(wxT("u"))) {
upload = true;
}
if (parser.Found(wxT("s"))) {
// Add/Edit station location
if (loc == 0) {
if (tqsl_initStationLocationCapture(&loc)) {
wxLogError(getLocalizedErrorString());
}
AddEditStationLocation(loc, true);
} else {
AddEditStationLocation(loc, false, _("Edit Station Location"));
}
tqsl_endStationLocationCapture(&loc);
return false;
}
if (parser.GetParamCount() > 0) {
infile = parser.GetParam(0);
}
wxString path, name, ext;
wxFileName::SplitPath(infile, &path, &name, &ext);
// Handle "-i" (import cert), or bare cert file on command line
bool tq6File = false;
bool p12File = false;
bool tbkFile = false;
if (!wxIsEmpty(infile)) {
if (ext.CmpNoCase(wxT("tq6")) == 0) {
tq6File = true;
}
if (ext.CmpNoCase(wxT("p12")) == 0) {
p12File = true;
}
if (ext.CmpNoCase(wxT("tbk")) == 0) {
tbkFile = true;
}
}
if (parser.Found(wxT("i"), &infile) && (!wxIsEmpty(infile))) {
wxFileName::SplitPath(infile, &path, &name, &ext);
if (ext.CmpNoCase(wxT("tq6")) == 0) {
tq6File = true;
}
if (ext.CmpNoCase(wxT("p12")) == 0) {
p12File = true;
}
if (ext.CmpNoCase(wxT("tbk")) == 0) {
tbkFile = true;
}
}
if (tq6File || p12File) {
infile.Trim(true).Trim(false);
notifyData nd;
if (tq6File) {
if (tqsl_importTQSLFile(infile.ToUTF8(), notifyImport, &nd)) {
if (tQSL_Error != TQSL_CERT_ERROR) {
if (tQSL_Error != 0) wxLogError(getLocalizedErrorString());
}
} else {
wxString call = wxString::FromUTF8(tQSL_ImportCall);
wxString pending = config->Read(wxT("RequestPending"));
pending.Replace(call, wxT(""), true);
wxString rest;
while (pending.StartsWith(wxT(","), &rest))
pending = rest;
while (pending.EndsWith(wxT(","), &rest))
pending = rest;
config->Write(wxT("RequestPending"), pending);
cert_cleanup();
frame->cert_tree->Build(CERTLIST_FLAGS);
}
wxLogMessage(nd.Message());
return(true);
} else if (p12File) {
// First try with no password
if (!tqsl_importPKCS12File(infile.ToUTF8(), "", 0, NULL, notifyImport, &nd) || tQSL_Error == TQSL_CERT_ERROR) {
if (tQSL_Error != 0) wxLogError(getLocalizedErrorString());
} else if (tQSL_Error == TQSL_PASSWORD_ERROR) {
if (password == NULL) {
wxLogError(_("To import this passphrase protected P12 file, you must pass the passphrase on the command line"));
if (quiet) {
exitNow(TQSL_EXIT_BAD_PASSPHRASE, quiet);
}
}
if (!tqsl_importPKCS12File(infile.ToUTF8(), password, 0, NULL, notifyImport, &nd)) {
wxLogError(_("To import this passphrase protected P12 file, you must pass the passphrase on the command line"));
if (quiet) {
exitNow(TQSL_EXIT_BAD_PASSPHRASE, quiet);
}
}
} else if (tQSL_Error == TQSL_OPENSSL_ERROR) {
wxLogError(_("This file is not a valid P12 file"));
if (quiet) {
exitNow(TQSL_EXIT_LIB_ERROR, quiet);
}
}
cert_cleanup();
frame->cert_tree->Build(CERTLIST_FLAGS);
frame->CertTreeReset();
wxLogMessage(nd.Message());
return(true);
}
} else if (tbkFile) {
frame->DoRestoreConfig(infile);
return(true);
}
// We need a logfile, else there's nothing to do.
if (wxIsEmpty(infile)) { // Nothing to sign
if (quiet) {
wxLogError(_("No logfile to sign!"));
tqslTrace(NULL, "%s", "No logfile to sign!");
exitNow(TQSL_EXIT_COMMAND_ERROR, quiet);
return false;
}
return true;
}
bool editAdif = DEFAULT_ADIF_EDIT;
config->Read(wxT("AdifEdit"), &editAdif, DEFAULT_ADIF_EDIT);
// If it's an ADIF file, invoke the editor if that's the only argument
// unless we're running in batch mode
if (editAdif && !quiet && !wxIsEmpty(infile) && (ext.CmpNoCase(wxT("adi")) || ext.CmpNoCase(wxT("adif")))) {
QSORecordList recs;
loadQSOfile(infile, recs);
wxMessageBox(_("Warning: The TQSL ADIF editor only processes a limited number of ADIF fields.\n\nUsing the editor on an ADIF file can cause QSO details to be lost!"), _("Warning"), wxOK | wxICON_EXCLAMATION, frame);
try {
QSODataDialog dial(frame, infile, frame->help, &recs);
dial.ShowModal();
} catch(TQSLException& x) {
wxLogError(wxT("%hs"), x.what());
}
exitNow(TQSL_EXIT_SUCCESS, quiet);
}
// Assume that it's a log to sign
if (logverify == TQSL_LOC_UPDATE && loc == NULL) { // Update mode, use fake empty location.
if (tqsl_getStationLocation(&loc, ".empty")) { // Already exists?
tqsl_initStationLocationCapture(&loc); // No, make a new one.
tqsl_setStationLocationCaptureName(loc, ".empty");
tqsl_saveStationLocationCapture(loc, 1);
tqsl_getStationLocation(&loc, ".empty");
}
}
if (loc == 0) {
try {
int n;
check_tqsl_error(tqsl_initStationLocationCapture(&loc));
check_tqsl_error(tqsl_getNumStationLocations(loc, &n));
if (n != 1) {
check_tqsl_error(tqsl_endStationLocationCapture(&loc));
frame->Show(true);
loc = frame->SelectStationLocation(_("Select Station Location for Signing"));
} else {
// There's only one station location. Use that and don't prompt.
char deflocn[512];
check_tqsl_error(tqsl_getStationLocationName(loc, 0, deflocn, sizeof deflocn));
check_tqsl_error(tqsl_endStationLocationCapture(&loc));
check_tqsl_error(tqsl_getStationLocation(&loc, deflocn));
}
}
catch(TQSLException& x) {
wxLogError(wxT("%hs"), x.what());
if (quiet)
exitNow(TQSL_EXIT_CANCEL, quiet);
}
}
// If no location specified and not chosen, can't sign. Exit.
if (loc == 0) {
if (quiet)
exitNow(TQSL_EXIT_CANCEL, quiet);
return false;
}
if (!wxIsEmpty(outfile)) {
path = outfile;
} else {
if (!wxIsEmpty(path))
path += wxT("/");
path += name + wxT(".tq8");
}
if (upload) {
try {
int val = frame->UploadLogFile(loc, infile, TQSL_COMPRESSED_FILE, suppressdate, startdate, enddate, action, logverify, password, defcall);
if (quiet)
exitNow(val, quiet);
else
return true; // Run the GUI
} catch(TQSLException& x) {
wxString s;
wxString err = wxString::FromUTF8(x.what());
if (err.Find(infile) == wxNOT_FOUND) {
if (!infile.empty())
s = infile + wxT(": ");
}
s += err;
wxLogError(wxT("%s"), (const char *)s.c_str());
if (quiet)
exitNow(TQSL_EXIT_LIB_ERROR, quiet);
else
return true;
}
} else {
try {
int val = frame->ConvertLogFile(loc, infile, path, TQSL_COMPRESSED_FILE, suppressdate, startdate, enddate, action, logverify, password, defcall);
if (quiet)
exitNow(val, quiet);
else
return true;
} catch(TQSLException& x) {
wxString s;
wxString err = wxString::FromUTF8(x.what());
if (err.Find(infile) == wxNOT_FOUND) {
if (!infile.IsEmpty())
s = infile + wxT(": ");
}
s += err;
wxLogError(wxT("%s"), (const char *)s.c_str());
if (quiet)
exitNow(TQSL_EXIT_LIB_ERROR, quiet);
else
return true;
}
}
check_tqsl_error(tqsl_endStationLocationCapture(&loc));
if (quiet)
exitNow(TQSL_EXIT_SUCCESS, quiet);
return true;
} // NOLINT(readability/fn_size)
void MyFrame::FirstTime(void) {
tqslTrace("MyFrame::FirstTime", NULL);
if (wxConfig::Get()->Read(wxT("HasRun")).IsEmpty()) {
wxConfig::Get()->Write(wxT("HasRun"), wxT("yes"));
DisplayHelp();
wxMessageBox(_("Please review the introductory documentation before using this program."),
_("Notice"), wxOK | wxICON_INFORMATION, this);
}
int ncerts = cert_tree->Build(CERTLIST_FLAGS);
CertTreeReset();
if (ncerts == 0) {
wxString msg = _("You have no callsign certificate installed on this computer with which to sign log submissions.");
msg += wxT("\n");
msg += _("Would you like to request a callsign certificate now?");
msg += wxT("\n\n");
msg += _("If you are planning to restore from a backup, please choose 'No'.");
if (wxMessageBox(msg, _("Alert"), wxYES_NO | wxICON_QUESTION, this) == wxYES) {
wxCommandEvent e;
CRQWizard(e);
}
}
wxString pending = wxConfig::Get()->Read(wxT("RequestPending"));
wxStringTokenizer tkz(pending, wxT(","));
while (tkz.HasMoreTokens()) {
wxString pend = tkz.GetNextToken();
bool found = false;
tQSL_Cert *certs;
int ncerts = 0;
if (!tqsl_selectCertificates(&certs, &ncerts, pend.ToUTF8(), 0, 0, 0, TQSL_SELECT_CERT_WITHKEYS)) {
for (int i = 0; i < ncerts; i++) {
int keyonly;
if (!tqsl_getCertificateKeyOnly(certs[i], &keyonly)) {
if (!found && keyonly)
found = true;
}
}
tqsl_freeCertificateList(certs, ncerts);
certs = NULL;
ncerts = 0;
}
if (!found) {
// Remove this call from the list of pending certificate requests
wxString p = wxConfig::Get()->Read(wxT("RequestPending"));
if (!p.IsEmpty()) {
p.Replace(pend, wxT(""), true);
wxString rest;
while (p.StartsWith(wxT(","), &rest))
p = rest;
while (p.EndsWith(wxT(","), &rest))
p = rest;
wxConfig::Get()->Write(wxT("RequestPending"), p);
}
}
}
// This check has been obsolete for decades. Remove it.
#ifdef CHECK_FOR_BETA_CERTS
if (ncerts > 0) {
TQ_WXCOOKIE cookie;
wxTreeItemId it = cert_tree->GetFirstChild(cert_tree->GetRootItem(), cookie);
while (it.IsOk()) {
if (cert_tree->GetItemText(it) == wxT("Test Certificate Authority")) {
wxMessageBox(wxT("You must delete your beta-test certificates (the ones\n")
wxT("listed under \"Test Certificate Authority\") to ensure proprer\n")
wxT("operation of the TrustedQSL software."), wxT("Warning"), wxOK, this);
break;
}
it = cert_tree->GetNextChild(cert_tree->GetRootItem(), cookie);
}
}
#endif
// Copy tqslcert preferences to tqsl unless already done.
if (wxConfig::Get()->Read(wxT("PrefsMigrated")).IsEmpty()) {
wxConfig::Get()->Write(wxT("PrefsMigrated"), wxT("yes"));
tqslTrace("MyFrame::FirstTime", "Migrating preferences from tqslcert");
wxConfig* certconfig = new wxConfig(wxT("tqslcert"));
wxString name, gname;
long context;
wxString svalue;
long lvalue;
bool bvalue;
double dvalue;
wxArrayString groupNames;
groupNames.Add(wxT("/"));
bool more = certconfig->GetFirstGroup(gname, context);
while (more) {
groupNames.Add(wxT("/") + gname);
more = certconfig->GetNextGroup(gname, context);
}
for (unsigned i = 0; i < groupNames.GetCount(); i++) {
certconfig->SetPath(groupNames[i]);
wxConfig::Get()->SetPath(groupNames[i]);
more = certconfig->GetFirstEntry(name, context);
while (more) {
wxConfigBase::EntryType etype = certconfig->GetEntryType(name);
switch (etype) {
case wxConfigBase::Type_Unknown:
case wxConfigBase::Type_String:
certconfig->Read(name, &svalue);
wxConfig::Get()->Write(name, svalue);
break;
case wxConfigBase::Type_Boolean:
certconfig->Read(name, &bvalue);
wxConfig::Get()->Write(name, bvalue);
break;
case wxConfigBase::Type_Integer:
certconfig->Read(name, &lvalue);
wxConfig::Get()->Write(name, lvalue);
break;
case wxConfigBase::Type_Float:
certconfig->Read(name, &dvalue);
wxConfig::Get()->Write(name, dvalue);
break;
}
more = certconfig->GetNextEntry(name, context);
}
}
delete certconfig;
wxConfig::Get()->SetPath(wxT("/"));
wxConfig::Get()->Flush();
}
// Find and report conflicting mode maps
init_modes();
// Run automatic check for updates - except for wx 2.9, which breaks threading.
wxConfig *config = reinterpret_cast<wxConfig *>(wxConfig::Get());
if (config->Read(wxT("AutoUpdateCheck"), true)) {
DoUpdateCheck(true, false);
}
return;
}
wxMenu *
makeCertificateMenu(bool enable, bool keyonly, const char *callsign) {
tqslTrace("makeCertificateMenu", "enable=%d, keyonly=%d", enable, keyonly);
wxMenu *c_menu = new wxMenu;
c_menu->Append(tc_c_Properties, _("Display Callsign Certificate &Properties"));
c_menu->AppendSeparator();
c_menu->Append(tc_c_Load, _("&Load Callsign Certificate from File"));
c_menu->Append(tc_c_Export, _("&Save Callsign Certificate to File..."));
c_menu->Enable(tc_c_Export, enable);
if (!keyonly) {
c_menu->AppendSeparator();
c_menu->Append(tc_c_New, _("Request &New Callsign Certificate..."));
c_menu->Append(tc_c_Renew, _("&Renew Callsign Certificate"));
c_menu->Enable(tc_c_Renew, enable);
} else {
c_menu->AppendSeparator();
}
c_menu->Append(tc_c_Delete, _("&Delete Callsign Certificate"));
c_menu->Enable(tc_c_Delete, (callsign != NULL));
c_menu->AppendSeparator();
int ncalls = 0;
tqsl_getDeletedCallsignCertificates(NULL, &ncalls, callsign);
c_menu->Append(tc_c_Undelete, _("Restore Deleted Callsign Certificate"));
c_menu->Enable(tc_c_Undelete, ncalls > 0);
return c_menu;
}
wxMenu *
makeLocationMenu(bool enable) {
tqslTrace("makeLocationMenu", "enable=%d", enable);
wxMenu *loc_menu = new wxMenu;
loc_menu->Append(tl_c_Properties, _("&Properties"));
loc_menu->Enable(tl_c_Properties, enable);
stn_menu->Enable(tm_s_Properties, enable);
loc_menu->Append(tl_c_Edit, _("&Edit"));
loc_menu->Enable(tl_c_Edit, enable);
loc_menu->Append(tl_c_Delete, _("&Delete"));
loc_menu->Enable(tl_c_Delete, enable);
return loc_menu;
}
// Handle clean-up after a certificate is imported
static void
cert_cleanup() {
if (tQSL_ImportCall[0] != '\0') { // If a user cert was imported
if (tQSL_ImportSerial != 0) {
wxString status;
tqsl_checkCertStatus(tQSL_ImportSerial, status);// Update from LoTW's "CRL"
tqsl_setCertificateStatus(tQSL_ImportSerial, (const char *)status.ToUTF8());
}
int certstat = tqsl_getCertificateStatus(tQSL_ImportSerial);
if (tQSL_ImportCall[0] != '\0' && tQSL_ImportSerial != 0 && (certstat == TQSL_CERT_STATUS_OK || certstat == TQSL_CERT_STATUS_UNK)) {
get_certlist(tQSL_ImportCall, 0, true, true, true); // Get any superceded ones for this call
for (int i = 0; i < ncerts; i++) {
long serial = 0;
int keyonly = false;
tqsl_getCertificateKeyOnly(certlist[i], &keyonly);
if (keyonly) {
tqsl_deleteCertificate(certlist[i]);
}
if (tqsl_getCertificateSerial(certlist[i], &serial)) {
continue;
}
if (serial == tQSL_ImportSerial) { // Don't delete the one we just imported
continue;
}
// This is not the one we just imported
int sup, exp;
if (tqsl_isCertificateSuperceded(certlist[i], &sup) == 0 && sup) {
tqsl_deleteCertificate(certlist[i]);
} else if (tqsl_isCertificateExpired(certlist[i], &exp) == 0 && exp) {
tqsl_deleteCertificate(certlist[i]);
}
}
}
}
}
/////////// Frame /////////////
void MyFrame::OnLoadCertificateFile(wxCommandEvent& WXUNUSED(event)) {
tqslTrace("MyFrame::OnLoadCertificateFile", NULL);
LoadCertWiz lcw(this, help, _("Load Certificate File"));
lcw.RunWizard();
cert_cleanup();
cert_tree->Build(CERTLIST_FLAGS);
CertTreeReset();
}
void MyFrame::CRQWizardRenew(wxCommandEvent& event) {
tqslTrace("MyFrame::CRQWizardRenew", NULL);
CertTreeItemData *data = reinterpret_cast<CertTreeItemData *>(cert_tree->GetItemData(cert_tree->GetSelection()));
req = 0;
tQSL_Cert cert;
wxString callSign, name, address1, address2, city, state, postalCode,
country, emailAddress;
if (data != NULL && (cert = data->getCert()) != 0) { // Should be true
char buf[256];
req = new TQSL_CERT_REQ;
if (!tqsl_getCertificateIssuerOrganization(cert, buf, sizeof buf))
strncpy(req->providerName, buf, sizeof req->providerName);
if (!tqsl_getCertificateIssuerOrganizationalUnit(cert, buf, sizeof buf))
strncpy(req->providerUnit, buf, sizeof req->providerUnit);
if (!tqsl_getCertificateCallSign(cert, buf, sizeof buf)) {
callSign = wxString::FromUTF8(buf);
strncpy(req->callSign, callSign.ToUTF8(), sizeof req->callSign);
}
if (!tqsl_getCertificateAROName(cert, buf, sizeof buf)) {
name = wxString::FromUTF8(buf);
strncpy(req->name, name.ToUTF8(), sizeof req->name);
}
tqsl_getCertificateDXCCEntity(cert, &(req->dxccEntity));
tqsl_getCertificateQSONotBeforeDate(cert, &(req->qsoNotBefore));
tqsl_getCertificateQSONotAfterDate(cert, &(req->qsoNotAfter));
if (!tqsl_getCertificateEmailAddress(cert, buf, sizeof buf)) {
emailAddress = wxString::FromUTF8(buf);
strncpy(req->emailAddress, emailAddress.ToUTF8(), sizeof req->emailAddress);
}
if (!tqsl_getCertificateRequestAddress1(cert, buf, sizeof buf)) {
address1 = wxString::FromUTF8(buf);
strncpy(req->address1, address1.ToUTF8(), sizeof req->address1);
}
if (!tqsl_getCertificateRequestAddress2(cert, buf, sizeof buf)) {
address2 = wxString::FromUTF8(buf);
strncpy(req->address2, address2.ToUTF8(), sizeof req->address2);
}
if (!tqsl_getCertificateRequestCity(cert, buf, sizeof buf)) {
city = wxString::FromUTF8(buf);
strncpy(req->city, city.ToUTF8(), sizeof req->city);
}
if (!tqsl_getCertificateRequestState(cert, buf, sizeof buf)) {
state = wxString::FromUTF8(buf);
strncpy(req->state, state.ToUTF8(), sizeof req->state);
}
if (!tqsl_getCertificateRequestPostalCode(cert, buf, sizeof buf)) {
postalCode = wxString::FromUTF8(buf);
strncpy(req->postalCode, postalCode.ToUTF8(), sizeof req->postalCode);
}
if (!tqsl_getCertificateRequestCountry(cert, buf, sizeof buf)) {
country = wxString::FromUTF8(buf);
strncpy(req->country, country.ToUTF8(), sizeof req->country);
}
}
CRQWizard(event);
if (req)
delete req;
req = 0;
}
// Delete an abandoned/failed cert request
static void deleteRequest(const char *callsign, int dxccEntity) {
int savedError = tQSL_Error;
free_certlist();
tqsl_selectCertificates(&certlist, &ncerts, callsign, dxccEntity, 0, 0, TQSL_SELECT_CERT_WITHKEYS);
int ko;
for (int i = 0; i < ncerts; i ++) {
if (!tqsl_getCertificateKeyOnly(certlist[i], &ko) && ko) {
if (tqsl_deleteCertificate(certlist[i])) {
wxLogError(getLocalizedErrorString());
}
certlist = NULL; // Invalidated in deleteCertificate flow
ncerts = 0;
tQSL_Error = savedError;
return;
}
}
tQSL_Error = savedError;
free_certlist();
return;
}
void MyFrame::CRQWizard(wxCommandEvent& event) {
tqslTrace("MyFrame::CRQWizard", NULL);
bool renew = (req != 0);
tQSL_Cert cert = (renew ? (reinterpret_cast<CertTreeItemData *>(cert_tree->GetItemData(cert_tree->GetSelection()))->getCert()) : 0);
CRQWiz wiz(req, cert, this, help, renew ? _("Renew a Callsign Certificate") : _("Request a new Callsign Certificate"));
int retval = 0;
/*
CRQ_ProviderPage *prov = new CRQ_ProviderPage(wiz, req);
CRQ_IntroPage *intro = new CRQ_IntroPage(wiz, req);
CRQ_NamePage *name = new CRQ_NamePage(wiz, req);
CRQ_EmailPage *email = new CRQ_EmailPage(wiz, req);
wxSize size = prov->GetSize();
if (intro->GetSize().GetWidth() > size.GetWidth())
size = intro->GetSize();
if (name->GetSize().GetWidth() > size.GetWidth())
size = name->GetSize();
if (email->GetSize().GetWidth() > size.GetWidth())
size = email->GetSize();
CRQ_PasswordPage *pw = new CRQ_PasswordPage(wiz);
CRQ_SignPage *sign = new CRQ_SignPage(wiz, size, &(prov->provider));
wxWizardPageSimple::Chain(prov, intro);
wxWizardPageSimple::Chain(intro, name);
wxWizardPageSimple::Chain(name, email);
wxWizardPageSimple::Chain(email, pw);
if (renew)
sign->cert = ;
else
wxWizardPageSimple::Chain(pw, sign);
wiz.SetPageSize(size);
*/
if (wiz.RunWizard()) {
TQSL_CERT_REQ newreq;
strncpy(newreq.providerName, wiz.provider.organizationName, sizeof newreq.providerName);
strncpy(newreq.providerUnit, wiz.provider.organizationalUnitName, sizeof newreq.providerUnit);
strncpy(newreq.callSign, wiz.callsign.ToUTF8(), sizeof newreq.callSign);
strncpy(newreq.name, wiz.name.ToUTF8(), sizeof newreq.name);
strncpy(newreq.address1, wiz.addr1.ToUTF8(), sizeof newreq.address1);
strncpy(newreq.address2, wiz.addr2.ToUTF8(), sizeof newreq.address2);
strncpy(newreq.city, wiz.city.ToUTF8(), sizeof newreq.city);
strncpy(newreq.state, wiz.state.ToUTF8(), sizeof newreq.state);
strncpy(newreq.postalCode, wiz.zip.ToUTF8(), sizeof newreq.postalCode);
if (wiz.country.IsEmpty())
strncpy(newreq.country, "USA", sizeof newreq.country);
else
strncpy(newreq.country, wiz.country.ToUTF8(), sizeof newreq.country);
strncpy(newreq.emailAddress, wiz.email.ToUTF8(), sizeof newreq.emailAddress);
strncpy(newreq.password, wiz.password.ToUTF8(), sizeof newreq.password);
newreq.dxccEntity = wiz.dxcc;
newreq.qsoNotBefore = wiz.qsonotbefore;
newreq.qsoNotAfter = wiz.qsonotafter;
newreq.signer = wiz.cert;
if (newreq.signer) {
char buf[40];
void *call = 0;
if (!tqsl_getCertificateCallSign(newreq.signer, buf, sizeof(buf)))
call = &buf;
while (tqsl_beginSigning(newreq.signer, 0, getPassword, call)) {
if (tQSL_Error != TQSL_PASSWORD_ERROR) {
if (tQSL_Error == TQSL_CUSTOM_ERROR && (tQSL_Errno == ENOENT || tQSL_Errno == EPERM)) {
snprintf(tQSL_CustomError, sizeof tQSL_CustomError,
"Can't open the private key file for %s: %s", static_cast<char *>(call), strerror(tQSL_Errno));
}
wxLogError(getLocalizedErrorString());
deleteRequest(newreq.callSign, newreq.dxccEntity);
return;
}
// Try signing with the unicode version of the password
if (tqsl_beginSigning(newreq.signer, unipwd, NULL, call) == 0) {
// If OK, signing is ready to go.
break;
}
if (tQSL_Error != TQSL_PASSWORD_ERROR) {
deleteRequest(newreq.callSign, newreq.dxccEntity);
return;
} else {
wxLogError(getLocalizedErrorString());
}
}
}
// Save or upload?
wxString file = flattenCallSign(wiz.callsign) + wxT(".") + wxT(TQSL_CRQ_FILE_EXT);
bool upload = false;
wxString msg = _("Do you want to upload this certificate request to LoTW now?");
if (!renew) {
msg += wxT("\n");
msg += _("You do not need an account on LoTW to do this.");
}
if (wxMessageBox(msg, _("Upload"), wxYES_NO|wxICON_QUESTION, this) == wxYES) {
upload = true;
// Save it in the working directory
#ifdef _WIN32
file = wxString::FromUTF8(tQSL_BaseDir) + wxT("\\") + file;
#else
file = wxString::FromUTF8(tQSL_BaseDir) + wxT("/") + file;
#endif
} else {
// Where to put it?
wxString wildcard = _("tQSL Cert Request files (*.");
wildcard += wxString::FromUTF8(TQSL_CRQ_FILE_EXT ")|*." TQSL_CRQ_FILE_EXT);
wildcard += _("|All files (") + wxString::FromUTF8(ALLFILESWILD ")|" ALLFILESWILD);
file = wxFileSelector(_("Save request"), wxT(""), file, wxT(TQSL_CRQ_FILE_EXT), wildcard,
wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
if (file.IsEmpty()) {
wxLogMessage(_("Request cancelled"));
deleteRequest(wiz.callsign.ToUTF8(), wiz.dxcc);
return;
}
}
newreq.renew = renew ? 1 : 0;
if (tqsl_createCertRequest(file.ToUTF8(), &newreq, 0, 0)) {
wxString msg = getLocalizedErrorString();
if (newreq.signer)
tqsl_endSigning(newreq.signer);
wxLogError(msg);
char m[500];
strncpy(m, msg.ToUTF8(), sizeof m);
wxMessageBox(wxString::Format(_("Error creating callsign certificate request: %hs"), m), _("Error creating Callsign Certificate Request"), wxOK | wxICON_EXCLAMATION, this);
deleteRequest(newreq.callSign, newreq.dxccEntity);
return;
}
if (upload) {
#ifdef _WIN32
wchar_t* wfile = utf8_to_wchar(file.ToUTF8());
ifstream in(wfile, ios::in | ios::binary);
free_wchar(wfile);
#else
ifstream in(file.ToUTF8(), ios::in | ios::binary);
#endif
if (!in) {
wxLogError(_("Error opening certificate request file %s: %hs"), file.c_str(), strerror(errno));
deleteRequest(newreq.callSign, newreq.dxccEntity);
} else {
wxString fileType(_("Certificate Request"));
retval = UploadFile(file, file.ToUTF8(), 0, fileType);
if (retval != 0) {
wxLogError(_("Your certificate request did not upload properly."));
if (wxMessageBox(_("Your upload appears to have failed.\nDo you want to save this request for uploading later?"), _("Retry?"), wxYES_NO | wxICON_QUESTION, this) == wxYES) {
// Where to put it?
wxString wildcard = _("tQSL Cert Request files (*.");
wildcard += wxString::FromUTF8(TQSL_CRQ_FILE_EXT ")|*." TQSL_CRQ_FILE_EXT);
wildcard += _("|All files (") + wxString::FromUTF8(ALLFILESWILD ")|" ALLFILESWILD);
file = wxFileSelector(_("Save request"), wxT(""), file, wxT(TQSL_CRQ_FILE_EXT), wildcard,
wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
if (file.IsEmpty()) {
wxLogMessage(_("Request cancelled"));
deleteRequest(wiz.callsign.ToUTF8(), wiz.dxcc);
return;
}
} else {
deleteRequest(newreq.callSign, newreq.dxccEntity);
}
}
}
} else {
wxString msg = _("You may now send your new certificate request (");
msg += file;
msg += wxT(")");
if (wiz.provider.emailAddress[0] != 0) {
msg += wxT("\n");
msg += _("to:");
msg += wxT("\n ");
msg += wxString::FromUTF8(wiz.provider.emailAddress);
}
if (wiz.provider.url[0] != 0) {
msg += wxT("\n");
if (wiz.provider.emailAddress[0] != 0)
msg += _("or ");
msg += wxString(_("see:"));
msg += wxT("\n ");
msg += wxString::FromUTF8(wiz.provider.url);
}
wxMessageBox(msg, wxT("TQSL"), wxOK | wxICON_ERROR, this);
}
if (retval == 0) {
wxString pending = wxConfig::Get()->Read(wxT("RequestPending"));
if (pending.IsEmpty())
pending = wiz.callsign;
else
pending += wxT(",") + wiz.callsign;
wxConfig::Get()->Write(wxT("RequestPending"), pending);
// Record another certificate
wxString requestRecord = wxConfig::Get()->Read(wxT("RequestRecord"));
time_t now = time(NULL);
if (!requestRecord.IsEmpty()) {
requestRecord = requestRecord + wxT(",");
}
requestRecord = requestRecord + wxString::Format(wxT("%s:%lu"), wiz.callsign.c_str(), now);
wxConfig::Get()->Write(wxT("RequestRecord"), requestRecord);
wxConfig::Get()->Flush();
}
if (newreq.signer)
tqsl_endSigning(newreq.signer);
cert_tree->Build(CERTLIST_FLAGS);
CertTreeReset();
}
}
void
MyFrame::CertTreeReset() {
if (!cert_save_label) return;
wxString nl = wxT("\n");
cert_save_label->SetLabel(nl + _("Save a Callsign Certificate"));
cert_save_button->SetName(wxT("Save a Callsign Certificate"));
cert_renew_label->SetLabel(nl + _("Renew a Callsign Certificate"));
cert_renew_button->SetName(wxT("Renew a Callsign Certificate"));
cert_prop_label->SetLabel(nl + _("Display a Callsign Certificate"));
cert_prop_button->SetName(wxT("Display a Callsign Certificate"));
cert_menu->Enable(tc_c_Renew, false);
cert_renew_button->Enable(false);
cert_select_label->SetLabel(nl + _("Select a Callsign Certificate to process"));
cert_save_button->Enable(false);
cert_prop_button->Enable(false);
int ncalls = 0;
tqsl_getDeletedCallsignCertificates(NULL, &ncalls, NULL);
cert_menu->Enable(tc_c_Undelete, ncalls > 0);
}
void MyFrame::OnCertTreeSel(wxTreeEvent& event) {
tqslTrace("MyFrame::OnCertTreeSel", NULL);
wxTreeItemId id = event.GetItem();
CertTreeItemData *data = reinterpret_cast<CertTreeItemData *>(cert_tree->GetItemData(id));
if (data) {
int window = DEFAULT_CERT_FUZZ;
wxConfig::Get()->Read(wxT("RenewalWindow"), &window, DEFAULT_CERT_FUZZ);
tqsl_isCertificateRenewable(NULL, &window);
int keyonly = 0;
int expired = 0;
int renewable = 0;
int superseded = 0;
int deleted = 0;
char call[40];
tqsl_getCertificateCallSign(data->getCert(), call, sizeof call);
wxString callSign = wxString::FromUTF8(call);
tqsl_getCertificateKeyOnly(data->getCert(), &keyonly);
tqsl_isCertificateExpired(data->getCert(), &expired);
tqsl_isCertificateRenewable(data->getCert(), &renewable);
tqsl_isCertificateSuperceded(data->getCert(), &superseded);
tqsl_getDeletedCallsignCertificates(NULL, &deleted, call);
tqslTrace("MyFrame::OnCertTreeSel", "call=%s", call);
cert_select_label->SetLabel(wxT(""));
cert_menu->Enable(tc_c_Properties, true);
cert_menu->Enable(tc_c_Export, true);
cert_menu->Enable(tc_c_Delete, true);
cert_menu->Enable(tc_c_Renew, true);
cert_menu->Enable(tc_c_Undelete, deleted != 0);
cert_save_button->Enable(true);
cert_load_button->Enable(true);
cert_prop_button->Enable(true);
int w, h;
loc_add_label->GetSize(&w, &h);
wxString nl = wxT("\n");
cert_save_label->SetLabel(nl + _("Save the Callsign Certificate for") + wxT(" ") + callSign);
cert_save_label->Wrap(w - 10);
wxString name = _("Save the Callsign Certificate for");
name += wxT(" ") + callSign;
cert_save_button->SetName(name);
cert_prop_label->SetLabel(nl + _("Display the Callsign Certificate properties for") + wxT(" ") + callSign);
cert_prop_label->Wrap(w - 10);
name = _("Display the Callsign Certificate properties for");
name += wxT(" ") + callSign;
cert_prop_button->SetName(name);
if (renewable) {
cert_renew_label->SetLabel(nl + _("Renew the Callsign Certificate for") +wxT(" ") + callSign);
cert_renew_label->Wrap(w - 10);
name = _("Renew the Callsign Certificate for");
name += wxT(" ") + callSign;
cert_renew_button->SetName(name);
} else {
cert_renew_label->SetLabel(nl + _("Renew a Callsign Certificate"));
cert_renew_button->SetName(wxT("Renew a Callsign Certificate"));
}
cert_menu->Enable(tc_c_Renew, renewable != 0);
cert_menu->Enable(tc_c_Undelete, deleted != 0);
cert_renew_button->Enable(renewable != 0);
} else {
CertTreeReset();
}
}
void MyFrame::OnCertProperties(wxCommandEvent& WXUNUSED(event)) {
tqslTrace("MyFrame::OnCertProperties", NULL);
CertTreeItemData *data = reinterpret_cast<CertTreeItemData *>(cert_tree->GetItemData(cert_tree->GetSelection()));
if (data != NULL)
displayCertProperties(data, this);
}
void MyFrame::OnCertExport(wxCommandEvent& WXUNUSED(event)) {
tqslTrace("MyFrame::OnCertExport", NULL);
CertTreeItemData *data = reinterpret_cast<CertTreeItemData *>(cert_tree->GetItemData(cert_tree->GetSelection()));
if (data == NULL) // "Never happens"
return;
char call[40];
if (tqsl_getCertificateCallSign(data->getCert(), call, sizeof call)) {
wxLogError(getLocalizedErrorString());
return;
}
wxString filename;
if (!data->path.IsEmpty() && !data->basename.IsEmpty()) {
filename = data->path + data->basename + wxT(".p12");
} else {
tqslTrace("MyFrame::OnCertExport", "call=%s", call);
wxString file_default = flattenCallSign(wxString::FromUTF8(call));
int ko = 0;
tqsl_getCertificateKeyOnly(data->getCert(), &ko);
if (ko)
file_default += wxT("-key-only");
file_default += wxT(".p12");
wxString path = wxConfig::Get()->Read(wxT("CertFilePath"), wxT(""));
filename = wxFileSelector(_("Enter the name for the new Certificate Container file"), path,
file_default, wxT(".p12"), _("Certificate Container files (*.p12)|*.p12|All files (*.*)|*.*"),
wxFD_SAVE|wxFD_OVERWRITE_PROMPT, this);
if (filename.IsEmpty())
return;
}
wxConfig::Get()->Write(wxT("CertFilePath"), wxPathOnly(filename));
wxString msg = _("Enter the passphrase for the certificate container file.");
msg += wxT("\n\n");
msg += _("If you are using a computer system that is shared with others, you should specify a passphrase to protect this certificate. However, if you are using a computer in a private residence, no passphrase need be specified.");
msg += wxT("\n\n");
msg += _("You will have to enter the passphrase any time you load the file into TrustedQSL.");
msg += wxT("\n\n");
msg += _("Leave the passphrase blank and click 'OK' unless you want to use a passphrase.");
msg += wxT("\n\n");
GetNewPasswordDialog dial(this, _("Certificate Container Passphrase"), msg, true, help, wxT("save.htm"));
if (dial.ShowModal() != wxID_OK)
return; // Cancelled
int terr;
do {
terr = tqsl_beginSigning(data->getCert(), 0, getPassword, reinterpret_cast<void *>(&call));
if (terr) {
if (tQSL_Error == TQSL_PASSWORD_ERROR) {
terr = tqsl_beginSigning(data->getCert(), unipwd, NULL, reinterpret_cast<void *>(&call));
if (terr) {
if (tQSL_Error == TQSL_PASSWORD_ERROR)
continue;
wxLogError(getLocalizedErrorString());
}
continue;
}
if (tQSL_Error == TQSL_OPERATOR_ABORT)
return;
// Unable to open the private key
if (tQSL_Error == TQSL_CUSTOM_ERROR && (tQSL_Errno == ENOENT || tQSL_Errno == EPERM)) {
snprintf(tQSL_CustomError, sizeof tQSL_CustomError,
"Can't open the private key file for %s: %s", call, strerror(tQSL_Errno));
}
wxLogError(getLocalizedErrorString());
return;
}
} while (terr);
// pass option to use old crypto
bool oldCrypto;
wxConfig::Get()->Read(wxT("P12OldCrypto"), &oldCrypto, DEFAULT_OLDCRYPTO);
// When setting the password, always use UTF8.
int ret;
if (oldCrypto) {
ret = tqsl_exportPKCS12FileWeakCrypto(data->getCert(), filename.ToUTF8(), dial.Password().ToUTF8());
} else {
ret = tqsl_exportPKCS12File(data->getCert(), filename.ToUTF8(), dial.Password().ToUTF8());
}
if (ret) {
char buf[500];
strncpy(buf, getLocalizedErrorString().ToUTF8(), sizeof buf);
wxLogError(wxString::Format(_("Export to %s failed: %hs"), filename.c_str(), buf));
} else {
wxLogMessage(_("Certificate saved in file %s"), filename.c_str());
}
tqsl_endSigning(data->getCert());
}
void MyFrame::OnCertDelete(wxCommandEvent& WXUNUSED(event)) {
tqslTrace("MyFrame::OnCertDelete", NULL);
CertTreeItemData *data = reinterpret_cast<CertTreeItemData *>(cert_tree->GetItemData(cert_tree->GetSelection()));
if (data == NULL) // "Never happens"
return;
wxString warn = _("This will remove the selected callsign certificate from your system.");
warn += wxT("\n");
warn += _("You will NOT be able to recover it by loading a .TQ6 file.");
warn += wxT("\n");
warn += _("You WILL be able to recover it from a container (.p12) file,");
warn += wxT("\n");
warn += _("if you have created one via the Callsign Certificate menu's");
warn += wxT("\n");
warn += _("'Save Callsign Certificate' command.");
warn += wxT("\n\n");
warn += _("Are you sure you want to delete the certificate?");
if (wxMessageBox(warn, _("Warning"), wxYES_NO|wxICON_QUESTION, this) == wxYES) {
char buf[128];
if (!tqsl_getCertificateCallSign(data->getCert(), buf, sizeof buf)) {
wxString call = wxString::FromUTF8(buf);
wxString pending = wxConfig::Get()->Read(wxT("RequestPending"));
pending.Replace(call, wxT(""), true);
wxString rest;
while (pending.StartsWith(wxT(","), &rest))
pending = rest;
while (pending.EndsWith(wxT(","), &rest))
pending = rest;
wxConfig::Get()->Write(wxT("RequestPending"), pending);
}
int keyonly, sup, exp;
long serial;
tqsl_getCertificateKeyOnly(data->getCert(), &keyonly);
tqsl_getCertificateSerial(data->getCert(), &serial);
tqsl_isCertificateExpired(data->getCert(), &exp);
tqsl_isCertificateSuperceded(data->getCert(), &sup);
tqslTrace("MyFrame::OnCertDelete", "About to delete cert for callsign %s, serial %ld, keyonly %d, superceded %d, expired %d", buf, serial, keyonly, sup, exp);
if (tqsl_deleteCertificate(data->getCert()))
wxLogError(getLocalizedErrorString());
cert_tree->Build(CERTLIST_FLAGS);
CertTreeReset();
}
}
void MyFrame::OnCertUndelete(wxCommandEvent& WXUNUSED(event)) {
tqslTrace("MyFrame::OnCertUndelete", NULL);
int ncalls;
char **calls = NULL;
try {
check_tqsl_error(tqsl_getDeletedCallsignCertificates(&calls, &ncalls, NULL));
if (ncalls <= 0) {
wxMessageBox(_("There are no deleted Callsign Certificates to restore"), _("Undelete Error"), wxOK | wxICON_EXCLAMATION, this);
return;
}
wxArrayString choices;
choices.clear();
for (int i = 0; i < ncalls; i++) {
choices.Add(wxString::FromUTF8(calls[i]));
}
choices.Sort();
wxString selected = wxGetSingleChoice(_("Choose a Callsign Certificate to restore"),
_("Callsign Certificates"),
choices);
if (selected.IsEmpty())
return; // Cancelled
check_tqsl_error(tqsl_restoreCallsignCertificate(selected.ToUTF8()));
tqsl_freeDeletedCertificateList(calls, ncalls);
cert_tree->Build(CERTLIST_FLAGS);
CertTreeReset();
} catch(TQSLException& x) {
wxLogError(wxT("%hs"), x.what());
}
}
void
MyFrame::LocTreeReset() {
if (!loc_edit_button) return;
loc_edit_button->Disable();
loc_delete_button->Disable();
loc_prop_button->Disable();
stn_menu->Enable(tm_s_Properties, false);
wxString nl = wxT("\n");
loc_edit_label->SetLabel(nl + _("Edit a Station Location"));
loc_edit_button->SetName(wxT("Edit a Station Location"));
loc_delete_label->SetLabel(nl + _("Delete a Station Location"));
loc_delete_button->SetName(wxT("Delete a Station Location"));
loc_prop_label->SetLabel(nl + _("Display Station Location Properties"));
loc_prop_button->SetName(wxT("Display Station Location Properties"));
loc_select_label->SetLabel(nl + _("Select a Station Location to process"));
}
void MyFrame::OnLocTreeSel(wxTreeEvent& event) {
tqslTrace("MyFrame::OnLocTreeSel", NULL);
wxTreeItemId id = event.GetItem();
LocTreeItemData *data = reinterpret_cast<LocTreeItemData *>(loc_tree->GetItemData(id));
if (data) {
int w, h;
wxString lname = data->getLocname();
wxString call = data->getCallSign();
tqslTrace("MyFrame::OnLocTreeSel", "lname=%s, call=%s", S(lname), S(call));
loc_add_label->GetSize(&w, &h);
loc_edit_button->Enable();
loc_delete_button->Enable();
loc_prop_button->Enable();
stn_menu->Enable(tm_s_Properties, true);
wxString nl = wxT("\n");
loc_edit_label->SetLabel(nl + _("Edit Station Location ") + call + wxT(": ") + lname);
loc_edit_label->Wrap(w - 10);
loc_edit_button->SetName(wxT("Edit Station Location ") + call + wxT(": ") + lname);
loc_delete_label->SetLabel(nl + _("Delete Station Location ") + call + wxT(": ") + lname);
loc_delete_label->Wrap(w - 10);
loc_delete_button->SetName(wxT("Delete Station Location ") + call + wxT(": ") + lname);
loc_prop_label->SetLabel(nl + _("Display Station Location Properties for ") + call + wxT(": ") + lname);
loc_prop_label->Wrap(w - 10);
loc_prop_button->SetName(wxT("Display Station Location Properties for ") + call + wxT(": ") + lname);
loc_select_label->SetLabel(wxT(""));
} else {
LocTreeReset();
}
}
void MyFrame::OnLocProperties(wxCommandEvent& WXUNUSED(event)) {
tqslTrace("MyFrame::OnLocProperties", NULL);
LocTreeItemData *data = reinterpret_cast<LocTreeItemData *>(loc_tree->GetItemData(loc_tree->GetSelection()));
if (data != NULL)
displayLocProperties(data, this);
}
void MyFrame::OnLocDelete(wxCommandEvent& WXUNUSED(event)) {
tqslTrace("MyFrame::OnLocDelete", NULL);
LocTreeItemData *data = reinterpret_cast<LocTreeItemData *>(loc_tree->GetItemData(loc_tree->GetSelection()));
if (data == NULL) // "Never happens"
return;
wxString warn = _("This will remove this station location from your system.");
warn += wxT("\n");
warn += _("Are you sure you want to delete this station location?");
if (wxMessageBox(warn, _("Warning"), wxYES_NO|wxICON_QUESTION, this) == wxYES) {
if (tqsl_deleteStationLocation(data->getLocname().ToUTF8()))
wxLogError(getLocalizedErrorString());
loc_tree->Build();
LocTreeReset();
AutoBackup();
}
}
void MyFrame::OnLocUndelete(wxCommandEvent& WXUNUSED(event)) {
tqslTrace("MyFrame::OnLocUndelete", NULL);
int nloc;
char **locp = NULL;
try {
check_tqsl_error(tqsl_getDeletedStationLocations(&locp, &nloc));
if (nloc <= 0) {
wxMessageBox(_("There are no deleted Station Locations to restore"), _("Undelete Error"), wxOK | wxICON_EXCLAMATION, this);
return;
}
wxArrayString choices;
choices.clear();
for (int i = 0; i < nloc; i++) {
choices.Add(wxString::FromUTF8(locp[i]));
}
choices.Sort();
wxString selected = wxGetSingleChoice(_("Choose a Station Location to restore"),
_("Station Locations"),
choices);
if (selected.IsEmpty()) {
tqsl_freeDeletedLocationList(locp, nloc);
return; // Cancelled
}
check_tqsl_error(tqsl_restoreStationLocation(selected.ToUTF8()));
tqsl_freeDeletedLocationList(locp, nloc);
} catch(TQSLException& x) {
wxLogError(wxT("%hs"), x.what());
}
loc_tree->Build();
LocTreeReset();
AutoBackup();
}
void MyFrame::OnLocEdit(wxCommandEvent& WXUNUSED(event)) {
tqslTrace("MyFrame::OnLocEdit", NULL);
LocTreeItemData *data = reinterpret_cast<LocTreeItemData *>(loc_tree->GetItemData(loc_tree->GetSelection()));
if (data == NULL) // "Never happens"
return;
tQSL_Location loc;
wxString selname;
char errbuf[512];
try {
check_tqsl_error(tqsl_getStationLocation(&loc, data->getLocname().ToUTF8()));
if (verify_cert(loc, true)) { // Check if there is a certificate before editing
check_tqsl_error(tqsl_getStationLocationErrors(loc, errbuf, sizeof(errbuf)));
if (strlen(errbuf) > 0) {
wxString fmt = wxT("%hs\n");
fmt += _("The invalid data was ignored.");
wxMessageBox(wxString::Format(fmt, errbuf), _("Station Location data error"), wxOK | wxICON_EXCLAMATION, this);
}
char loccall[512];
check_tqsl_error(tqsl_getLocationCallSign(loc, loccall, sizeof loccall));
selname = run_station_wizard(this, loc, help, true, true, wxString::Format(_("Edit Station Location : %hs - %s"), loccall, data->getLocname().c_str()));
check_tqsl_error(tqsl_endStationLocationCapture(&loc));
}
}
catch(TQSLException& x) {
wxLogError(wxT("%hs"), x.what());
}
loc_tree->Build();
LocTreeReset();
AutoBackup();
}
void MyFrame::OnLoginToLogbook(wxCommandEvent& WXUNUSED(event)) {
tqslTrace("MyFrame::OnLoginToLogbook", NULL);
wxString url = wxConfig::Get()->Read(wxT("LogbookURL"), DEFAULT_LOTW_LOGIN_URL);
if (!url.IsEmpty())
wxLaunchDefaultBrowser(url);
return;
}
void MyFrame::OnChooseLanguage(wxCommandEvent& WXUNUSED(event)) {
tqslTrace("MyFrame::OnChooseLanguage", "Language choice dialog");
wxLanguage lang = wxGetApp().GetLang();
int sel = 0;
for (size_t i = 0; i < langIds.size(); i++) {
if (langWX2toWX3(langIds[i]) == lang) {
sel = i;
break;
}
}
#if wxMAJOR_VERSION > 2
long lng = wxGetSingleChoiceIndex(_("Please choose language:"),
_("Language"), langNames, sel, this);
#else
wxSingleChoiceDialog dialog(this, _("Please choose language:"), _("Language"), langNames);
dialog.SetSelection(sel);
long lng = dialog.ShowModal() == wxID_OK ? dialog.GetSelection() : -1;
#endif
tqslTrace("MyFrame::OnChooseLanguage", "Language chosen: %d", lng);
if (lng == -1 || langWX2toWX3(langIds[lng]) == lang) // Cancel or No change
return;
wxConfig::Get()->Write(wxT("Language"), static_cast<int>(langIds[lng]));
wxConfig::Get()->Flush();
wxLanguage chosen = langIds[lng];
chosen = langWX2toWX3(chosen);
locale = new wxLocale(chosen);
if (!locale) {
#if defined(TQSL_TESTING)
wxLogError(wxT("This language is not supported by the system."));
#endif
locale = new wxLocale(wxLANGUAGE_DEFAULT);
}
// Add a subdirectory for language files
locale->AddCatalogLookupPathPrefix(wxT("lang"));
#ifdef _WIN32
locale->AddCatalogLookupPathPrefix(wxString::FromUTF8(tQSL_BaseDir) + wxT("\\lang"));
#else
locale->AddCatalogLookupPathPrefix(wxString::FromUTF8(tQSL_BaseDir) + wxT("/lang"));
#endif
// Initialize the catalogs we'll be using
locale->AddCatalog(wxT("tqslapp"));
if (!locale->AddCatalog(wxT("wxstd-3.2"))) {
if (!locale->AddCatalog(wxT("wxstd3"))) {
locale->AddCatalog(wxT("wxstd"));
}
}
// this catalog is installed in standard location on Linux systems and
// shows that you may make use of the standard message catalogs as well
//
// If it's not installed on your system, it is just silently ignored
#ifdef __LINUX__
{
wxLogNull nolog;
locale->AddCatalog(wxT("fileutils"));
}
#endif
SaveWindowLayout();
tqslTrace("MyFrame::OnChooseLanguage", "Destroying GUI");
Destroy();
tqslTrace("MyFrame::OnChooseLanguage", "Recreating GUI");
(reinterpret_cast<QSLApp*>(wxTheApp))->OnInit();
}
class CertPropDial : public wxDialog {
public:
explicit CertPropDial(tQSL_Cert cert, wxWindow *parent = 0);
void closeMe(wxCommandEvent&) { EndModal(wxID_OK); }
DECLARE_EVENT_TABLE()
};
BEGIN_EVENT_TABLE(CertPropDial, wxDialog)
EVT_BUTTON(wxID_OK, CertPropDial::closeMe)
END_EVENT_TABLE()
CertPropDial::CertPropDial(tQSL_Cert cert, wxWindow *parent)
: wxDialog(parent, -1, _("Certificate Properties"), wxDefaultPosition, wxSize(400, 15 * LABEL_HEIGHT)) {
tqslTrace("CertPropDial::CertPropDial", "cert=%lx", static_cast<void *>(cert));
const char *labels[] = {
__("Begins: "),
__("Expires: "),
__("Organization: "),
"",
__("Serial: "),
__("Operator: "),
__("Call sign: "),
__("DXCC Entity: "),
__("QSO Start Date: "),
__("QSO End Date: "),
__("Passphrase: ")
};
wxBoxSizer *topsizer = new wxBoxSizer(wxVERTICAL);
int label_width = 0;
int label_height = 0;
wxStaticText* mst = new wxStaticText(this, -1, wxT("I"));
int char_width = mst->GetSize().GetWidth();
// Measure the widest label - starting with the one that isn't there by default
wxString lab = wxGetTranslation(_("Certificate Request"));
lab = lab + wxT(":");
mst->SetLabel(lab);
int em_w = mst->GetSize().GetWidth();
// Only start with that label if it's going to appear
int keyonly;
tqsl_getCertificateKeyOnly(cert, &keyonly);
if (!keyonly) em_w = 0;
// Find the widest label
for (int i = 0; i < static_cast<int>(sizeof labels / sizeof labels[0]); i++) {
wxString lab = wxGetTranslation(wxString::FromUTF8(labels[i]));
mst->SetLabel(lab);
em_w = mst->GetSize().GetWidth();
if (em_w > label_width) label_width = em_w;
}
char callsign[40];
wxString blob = wxT("");
wxString prefix = wxT("");
for (int i = 0; i < static_cast<int>(sizeof labels / sizeof labels[0]); i++) {
wxString lbl = wxGetTranslation(wxString::FromUTF8(labels[i]));
while (1) {
mst->SetLabel(lbl);
int cur_size = mst->GetSize().GetWidth();
int delta = label_width - cur_size;
if (delta < char_width) break;
lbl += wxT(" ");
}
// Store the "empty" label for later
if (strlen(labels[i]) == 0)
prefix = lbl;
char buf[128];
tQSL_Date certExpDate;
tQSL_Date date;
DXCC DXCC;
int dxcc;
long serial;
buf[0] = '\0';
switch (i) {
case 0:
if (keyonly)
strncpy(buf, "N/A", sizeof buf);
else if (!tqsl_getCertificateNotBeforeDate(cert, &date))
tqsl_convertDateToText(&date, buf, sizeof buf);
break;
case 1:
if (keyonly)
strncpy(buf, "N/A", sizeof buf);
else if (!tqsl_getCertificateNotAfterDate(cert, &certExpDate))
tqsl_convertDateToText(&certExpDate, buf, sizeof buf);
break;
case 2:
tqsl_getCertificateIssuerOrganization(cert, buf, sizeof buf);
break;
case 3:
tqsl_getCertificateIssuerOrganizationalUnit(cert, buf, sizeof buf);
break;
case 4:
if (keyonly) {
strncpy(buf, "N/A", sizeof buf);
} else {
tqsl_getCertificateSerial(cert, &serial);
snprintf(buf, sizeof buf, "%ld", serial);
}
break;
case 5:
if (keyonly)
strncpy(buf, "N/A", sizeof buf);
else
tqsl_getCertificateAROName(cert, buf, sizeof buf);
break;
case 6:
tqsl_getCertificateCallSign(cert, buf, sizeof buf);
strncpy(callsign, buf, sizeof callsign);
break;
case 7:
tqsl_getCertificateDXCCEntity(cert, &dxcc);
DXCC.getByEntity(dxcc);
strncpy(buf, DXCC.name(), sizeof buf);
break;
case 8:
if (!tqsl_getCertificateQSONotBeforeDate(cert, &date))
tqsl_convertDateToText(&date, buf, sizeof buf);
break;
case 9:
if (!tqsl_getCertificateQSONotAfterDate(cert, &date)) {
// If no end-date given, the QSO End Date is one less than
// the expiration date
int delta;
if (keyonly) {
strncpy(buf, "N/A", sizeof buf);
} else if (!tqsl_subtractDates(&date, &certExpDate, &delta)) {
if (delta == 1) {
strncpy(buf, "N/A", sizeof buf);
} else {
tqsl_convertDateToText(&date, buf, sizeof buf);
}
} else {
tqsl_convertDateToText(&date, buf, sizeof buf);
}
}
break;
case 10:
switch (tqsl_getCertificatePrivateKeyType(cert)) {
case TQSL_PK_TYPE_ERR:
if (tQSL_Error == TQSL_CERT_NOT_FOUND) {
strncpy(buf, __("Missing from this computer"), sizeof buf);
break;
}
if (tQSL_Error == TQSL_OPENSSL_ERROR) {
strncpy(buf, __("Passphrase protected"), sizeof buf);
break;
}
if (tQSL_Error == TQSL_CUSTOM_ERROR && tQSL_Errno == ENOENT) {
strncpy(buf, __("Private Key not found"), sizeof buf);
break;
}
if (tQSL_Error == TQSL_CUSTOM_ERROR && tQSL_Errno == EPERM) {
strncpy(buf, __("Unable to read - no permission"), sizeof buf);
break;
}
if (tQSL_Error == TQSL_CUSTOM_ERROR) {
snprintf(tQSL_CustomError, sizeof tQSL_CustomError,
"Can't open the private key file for %s: %s", callsign, strerror(tQSL_Errno));
}
wxMessageBox(getLocalizedErrorString(), _("Error"), wxOK | wxICON_WARNING, this);
strncpy(buf, __("<ERROR>"), sizeof buf);
break;
case TQSL_PK_TYPE_NONE:
strncpy(buf, __("No Private Key"), sizeof buf);
break;
case TQSL_PK_TYPE_UNENC:
strncpy(buf, __("None"), sizeof buf);
break;
case TQSL_PK_TYPE_ENC:
strncpy(buf, __("Passphrase protected"), sizeof buf);
break;
}
break;
}
if (keyonly && i == 0) {
blob += _("Certificate Request");
blob += wxT(":\t");
blob += _("Awaiting response from ARRL");
blob += wxT("\n");
}
if (!keyonly || i > 1) {
blob += lbl;
blob += wxT("\t");
blob += wxGetTranslation(wxString::FromUTF8(buf));
blob += wxT("\n");
}
}
int sup, exp;
if (tqsl_isCertificateSuperceded(cert, &sup) == 0 && sup) {
blob += prefix + wxT("\t");
blob += _("Replaced");
blob += wxT("\n");
}
if (tqsl_isCertificateExpired(cert, &exp) == 0 && exp) {
blob += prefix + wxT("\t");
blob += _("Expired");
blob += wxT("\n");
}
mst->SetLabel(blob);
label_width = mst->GetSize().GetWidth();
label_height = mst->GetSize().GetHeight();
delete mst;
topsizer->Add(new wxStaticText(this, -1, blob, wxDefaultPosition, wxSize(label_width + 20, label_height + 5)));
topsizer->Add(
new wxButton(this, wxID_OK, _("Close")),
0, wxALIGN_CENTER | wxALL, 10
);
SetAutoLayout(TRUE);
SetSizer(topsizer);
topsizer->Fit(this);
topsizer->SetSizeHints(this);
CenterOnParent();
}
void
displayCertProperties(CertTreeItemData *item, wxWindow *parent) {
tqslTrace("displayCertProperties", "item=%lx", static_cast<void *>(item));
if (item != NULL) {
CertPropDial dial(item->getCert(), parent);
dial.ShowModal();
}
}
BEGIN_EVENT_TABLE(LocPropDial, wxDialog)
EVT_BUTTON(wxID_OK, LocPropDial::closeMe)
END_EVENT_TABLE()
LocPropDial::LocPropDial(wxString locname, bool display, const char *filename, wxWindow *parent)
: wxDialog(parent, -1, _("Station Location Properties"), wxDefaultPosition, wxSize(1000, 15 * LABEL_HEIGHT)) {
tqslTrace("LocPropDial", "locname=%s, filename=%s", S(locname), filename);
const char *fields[] = { "CALL", __("Call sign: "),
"DXCC", __("DXCC Entity: "),
"GRIDSQUARE", __("Grid Square: "),
"ITUZ", __("ITU Zone: "),
"CQZ", __("CQ Zone: "),
"IOTA", __("IOTA Locator: "),
"US_STATE", __("State: "),
"US_COUNTY", __("County: "),
"US_PARK", __("Park: "),
"CA_PROVINCE", __("Province: "),
"CA_US_PARK", __("Park: "),
"RU_OBLAST", __("Oblast: "),
"CN_PROVINCE", __("Province: "),
"AU_STATE", __("State: "),
"DX_US_PARK", __("Park: ") };
if (!display) {
SetTitle(_("Verify QTH details: ") + locname);
}
tQSL_Location loc;
try {
check_tqsl_error(tqsl_getStationLocation(&loc, locname.ToUTF8()));
}
catch(TQSLException& x) {
wxLogError(wxT("%hs"), x.what());
return;
}
wxBoxSizer *topsizer = new wxBoxSizer(wxVERTICAL);
int label_width = 0;
wxStaticText* mst = new wxStaticText(this, -1, wxT("I"));
// Measure the widest label
for (int i = 0; i < static_cast<int>(sizeof fields / sizeof fields[0]); i += 2) {
int em_w;
mst->SetLabel(wxString::FromUTF8(fields[i+1]));
em_w = mst->GetSize().GetWidth();
if (em_w > label_width) label_width = em_w;
}
wxFont callSignFont(32, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
wxFont dxccFont(24, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
char fieldbuf[512];
for (int i = 0; i < static_cast<int>(sizeof fields / sizeof fields[0]); i += 2) {
if (tqsl_getStationLocationField(loc, fields[i], fieldbuf, sizeof fieldbuf) == 0) {
if (strlen(fieldbuf) > 0 && strcmp(fieldbuf, "[None]") != 0) {
if (!strcmp(fields[i], "CALL")) {
wxStaticText* callLabel = new wxStaticText(this, -1, wxString::FromUTF8(fieldbuf), wxDefaultPosition, wxSize(label_width*8, -1), wxALIGN_CENTRE_HORIZONTAL|wxST_NO_AUTORESIZE);
callLabel->SetFont(callSignFont);
topsizer->Add(callLabel);
} else if (!strcmp(fields[i], "DXCC")) {
int dxcc = strtol(fieldbuf, NULL, 10);
const char *dxccname = NULL;
if (tqsl_getDXCCEntityName(dxcc, &dxccname))
strncpy(fieldbuf, "Unknown", sizeof fieldbuf);
else
strncpy(fieldbuf, dxccname, sizeof fieldbuf);
wxStaticText* dxccLabel = new wxStaticText(this, -1, wxString::FromUTF8(fieldbuf), wxDefaultPosition, wxSize(label_width*8, -1), wxALIGN_CENTRE_HORIZONTAL|wxST_NO_AUTORESIZE);
dxccLabel->SetFont(dxccFont);
topsizer->Add(dxccLabel);
} else {
wxString lbl = wxGetTranslation(wxString::FromUTF8(fields[i+1]));
wxString det = lbl + wxT(" ") + wxString::FromUTF8(fieldbuf);
wxStaticText* detailsLabel = new wxStaticText(this, -1, det, wxDefaultPosition, wxSize(label_width*8, -1), wxALIGN_CENTRE_HORIZONTAL|wxST_NO_AUTORESIZE);
topsizer->Add(detailsLabel);
}
}
}
}
delete mst;
if (display) {
topsizer->Add(new wxButton(this, wxID_OK, _("Close")),
0, wxALIGN_CENTER | wxALL, 10);
} else {
wxString det = _("Signing File: ") + wxString::FromUTF8(filename);
wxStaticText* detailsLabel = new wxStaticText(this, -1, det, wxDefaultPosition, wxSize(label_width*8, -1), wxALIGN_CENTRE_HORIZONTAL|wxST_NO_AUTORESIZE);
topsizer->Add(detailsLabel);
int fontSize = wxNORMAL_FONT->GetPointSize();
wxFont fnt(fontSize, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD);
wxStaticText* m1 = new wxStaticText(this, -1, _("Please verify that this is the correct Station Location for the QSOs being signed."));
m1->SetFont(fnt);
topsizer->Add(m1, 0, wxALL | wxALIGN_CENTER, 10);
wxStaticText* m2 = new wxStaticText(this, -1, _("Click 'Cancel' if this is not the correct Station Location."));
m2->SetFont(fnt);
topsizer->Add(m2, 0, wxALL | wxALIGN_CENTER, 10);
wxBoxSizer *butSizer = new wxBoxSizer(wxHORIZONTAL);
butSizer->Add(CreateButtonSizer(wxOK|wxCANCEL), 0, wxALL | wxALIGN_CENTER, 10);
topsizer->Add(butSizer, 0, wxALIGN_CENTER |wxALL, 10);
}
topsizer->AddSpacer(50);
SetAutoLayout(TRUE);
SetSizer(topsizer);
topsizer->Fit(this);
topsizer->SetSizeHints(this);
CenterOnParent();
}
void
displayLocProperties(LocTreeItemData *item, wxWindow *parent) {
tqslTrace("displayLocProperties", "item=%lx", item);
if (item != NULL) {
LocPropDial dial(item->getLocname(), true, NULL, parent);
dial.ShowModal();
}
}
int
getPassword(char *buf, int bufsiz, void *callsign) {
tqslTrace("getPassword", "buf=%lx, bufsiz=%d, callsign=%s", buf, bufsiz, callsign ? callsign : "NULL");
wxString prompt(_("Enter the Passphrase to unlock the callsign certificate"));
if (callsign)
prompt = wxString::Format(_T("Enter the Passphrase for your active %hs Callsign Certificate"), reinterpret_cast<char *>(callsign));
tqslTrace("getPassword", "Probing for top window");
wxWindow* top = wxGetApp().GetTopWindow();
tqslTrace("getPassword", "Top window = 0x%lx", reinterpret_cast<void *>(top));
top->SetFocus();
tqslTrace("getPassword", "Focus grabbed. About to pop up password dialog");
GetPasswordDialog dial(top, _("Enter passphrase"), prompt);
if (dial.ShowModal() != wxID_OK) {
tqslTrace("getPassword", "Password entry cancelled");
return 1;
}
tqslTrace("getPassword", "Password entered OK");
strncpy(buf, dial.Password().ToUTF8(), bufsiz);
utf8_to_ucs2(buf, unipwd, sizeof unipwd);
buf[bufsiz-1] = 0;
return 0;
}
void
displayTQSLError(const char *pre) {
tqslTrace("displayTQSLError", "pre=%s", pre);
wxString s = wxGetTranslation(wxString::FromUTF8(pre));
s += wxT(":\n");
s += getLocalizedErrorString();
wxMessageBox(s, _("Error"), wxOK | wxICON_WARNING, frame);
}
static wxString
flattenCallSign(const wxString& call) {
tqslTrace("flattenCallSign", "call=%s", S(call));
wxString flat = call;
size_t idx;
while ((idx = flat.find_first_not_of(wxT("ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_"))) != wxString::npos)
flat[idx] = '_';
return flat;
}
static int lockfileFD = -1;
#ifndef _WIN32
static int
lock_db(bool wait) {
struct flock fl;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 1;
tqslTrace("lock_db", "wait = %d", wait);
wxString lfname = wxString::FromUTF8(tQSL_BaseDir) + wxT("/dblock");
if (lockfileFD < 0) {
lockfileFD = open(lfname.ToUTF8(), O_RDWR| O_CREAT, 0644);
if (lockfileFD < 0) {
tqslTrace("lock_db", "can't set lock: %s", strerror(errno));
return 1;
}
}
if (wait) {
tqslTrace("lock_db", "waiting for lock");
fcntl(lockfileFD, F_SETLKW, &fl);
tqslTrace("lock_db", "got lock");
return 0;
}
int ret = fcntl(lockfileFD, F_SETLK, &fl);
tqslTrace("lock_db", "trying to set a lock");
if (ret < 0 && (errno == EACCES || errno == EAGAIN)) {
tqslTrace("lock_db", "Lock not taken : %s", strerror(errno));
return -1;
}
return 0;
}
static void
unlock_db(void) {
tqslTrace("unlock_db", "entered");
if (lockfileFD < 0) {
tqslTrace("unlock_db", "lock wasn't taken");
return;
}
close(lockfileFD);
lockfileFD = -1;
tqslTrace("unlock_db", "unlocked");
return;
}
#else /* _WIN32 */
static OVERLAPPED ov;
static HANDLE hFile = 0;
static int
lock_db(bool wait) {
BOOL ret = FALSE;
DWORD locktype = LOCKFILE_EXCLUSIVE_LOCK;
wxString lfname = wxString::FromUTF8(tQSL_BaseDir) + wxT("\\dblock");
tqslTrace("lock_db", "wait = %d", wait);
if (lockfileFD < 0) {
wchar_t* wlfname = utf8_to_wchar(lfname.ToUTF8());
lockfileFD = _wopen(wlfname, O_RDWR| O_CREAT, 0644);
free_wchar(wlfname);
if (lockfileFD < 0) {
tqslTrace("lock_db", "can't open file: %s", strerror(errno));
return 1;
}
ZeroMemory(&ov, sizeof(ov));
ov.hEvent = NULL;
ov.Offset = 0;
ov.OffsetHigh = 0x80000000;
}
hFile = (HANDLE) _get_osfhandle(lockfileFD);
if (!wait) {
locktype |= LOCKFILE_FAIL_IMMEDIATELY;
}
tqslTrace("lock_db", "trying to lock");
ret = LockFileEx(hFile, locktype, 0, 0, 0x80000000, &ov);
if (!ret) {
switch (GetLastError()) {
case ERROR_SHARING_VIOLATION:
case ERROR_LOCK_VIOLATION:
case ERROR_IO_PENDING:
tqslTrace("lock_db", "unable to lock");
return -1;
default:
tqslTrace("lock_db", "locked");
return 0;
}
}
return 0;
}
static void
unlock_db(void) {
tqslTrace("unlock_db", "hFile=%lx", hFile);
if (hFile) {
UnlockFileEx(hFile, 0, 0, 0x80000000, &ov);
tqslTrace("unlock_db", "unlocked");
}
if (lockfileFD != -1) {
tqslTrace("unlock_db", "closing lock");
_close(lockfileFD);
}
lockfileFD = -1;
hFile = 0;
}
///////////////////////////////
/* Derived from VistaTools.cxx - version 2.1 http://www.softblog.com/files/VistaTools.cxx
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
Copyright (C) 2008. WinAbility Software Corporation. All rights reserved.
Author: Andrei Belogortseff [ http://www.softblog.com ]
TERMS OF USE: You are free to use this file in any way you like,
for both the commercial and non-commercial purposes, royalty-free,
AS LONG AS you agree with the warranty disclaimer above,
EXCEPT that you may not remove or modify this or any of the
preceeding paragraphs. If you make any changes, please document
them in the MODIFICATIONS section below. If the changes are of general
interest, please let us know and we will consider incorporating them in
this file, as well.
If you use this file in your own project, an acknowledgment will be appreciated,
although it's not required.
SUMMARY:
This file contains several Vista-specific functions helpful when dealing with the
"elevation" features of Windows Vista. See the descriptions of the functions below
for information on what each function does and how to use it.
This file contains the Win32 stuff only, it can be used with or without other frameworks,
such as MFC, ATL, etc.
*/
#if ( NTDDI_VERSION < NTDDI_LONGHORN )
# error NTDDI_VERSION must be defined as NTDDI_LONGHORN or later
#endif
static BOOL
IsVista();
/*
Use IsVista() to determine whether the current process is running under Windows Vista or
(or a later version of Windows, whatever it will be)
Return Values:
If the function succeeds, and the current version of Windows is Vista or later,
the return value is TRUE.
If the function fails, or if the current version of Windows is older than Vista
(that is, if it is Windows XP, Windows 2000, Windows Server 2003, Windows 98, etc.)
the return value is FALSE.
*/
static HRESULT
GetElevationType(__out TOKEN_ELEVATION_TYPE * ptet);
/*
Use GetElevationType() to determine the elevation type of the current process.
Parameters:
ptet
[out] Pointer to a variable that receives the elevation type of the current process.
The possible values are:
TokenElevationTypeDefault - User is not using a "split" token.
This value indicates that either UAC is disabled, or the process is started
by a standard user (not a member of the Administrators group).
The following two values can be returned only if both the UAC is enabled and
the user is a member of the Administrator's group (that is, the user has a "split" token):
TokenElevationTypeFull - the process is running elevated.
TokenElevationTypeLimited - the process is not running elevated.
Return Values:
If the function succeeds, the return value is S_OK.
If the function fails, the return value is E_FAIL. To get extended error information,
call GetLastError().
*/
HRESULT
IsElevated(BOOL * pbElevated);
/*
Use IsElevated() to determine whether the current process is elevated or not.
Parameters:
pbElevated
[out] [optional] Pointer to a BOOL variable that, if non-NULL, receives the result.
The possible values are:
TRUE - the current process is elevated.
This value indicates that either UAC is enabled, and the process was elevated by
the administrator, or that UAC is disabled and the process was started by a user
who is a member of the Administrators group.
FALSE - the current process is not elevated (limited).
This value indicates that either UAC is enabled, and the process was started normally,
without the elevation, or that UAC is disabled and the process was started by a standard user.
Return Values
If the function succeeds, and the current process is elevated, the return value is S_OK.
If the function succeeds, and the current process is not elevated, the return value is S_FALSE.
If the function fails, the return value is E_FAIL. To get extended error information,
call GetLastError().
*/
static
BOOL IsVista() {
OSVERSIONINFO osver;
osver.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
if ( ::GetVersionEx( &osver ) &&
osver.dwPlatformId == VER_PLATFORM_WIN32_NT &&
(osver.dwMajorVersion >= 6) )
return TRUE;
return FALSE;
}
static
HRESULT
GetElevationType(__out TOKEN_ELEVATION_TYPE * ptet) {
if (!IsVista() || ptet == NULL )
return E_FAIL;
HRESULT hResult = E_FAIL; // assume an error occurred
HANDLE hToken = NULL;
if (!::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY, &hToken)) {
return hResult;
}
DWORD dwReturnLength = 0;
if (::GetTokenInformation(hToken, TokenElevationType, ptet, sizeof(*ptet), &dwReturnLength)) {
hResult = S_OK;
}
::CloseHandle(hToken);
return hResult;
}
HRESULT
IsElevated(BOOL * pbElevated) {
if (!IsVista())
return E_FAIL;
HRESULT hResult = E_FAIL; // assume an error occurred
HANDLE hToken = NULL;
if (!::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY, &hToken)) {
return hResult;
}
TOKEN_ELEVATION te = { 0 };
DWORD dwReturnLength = 0;
if (::GetTokenInformation(hToken, TokenElevation, &te, sizeof(te), &dwReturnLength)) {
hResult = te.TokenIsElevated ? S_OK : S_FALSE;
if ( pbElevated)
*pbElevated = (te.TokenIsElevated != 0);
}
::CloseHandle(hToken);
return hResult;
}
#endif /* _WIN32 */
|