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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2003,2004 Colin Walters <walters@gnome.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The Rhythmbox authors hereby grant permission for non-GPL compatible
* GStreamer plugins to be used and distributed together with GStreamer
* and Rhythmbox. This permission is above and beyond the permissions granted
* by the GPL license by which Rhythmbox is covered. If you modify this code
* you may extend this exception to your version of the code, but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
/**
* SECTION:rhythmdb
* @short_description: Rhythmbox database functions
*
* RhythmDB is an in-memory database containing #RhythmDBEntry items. It
* runs queries represented as #GPtrArray<!-- -->s containing query criteria,
* feeding the results into #RhythmDBQueryResults implementations such as
* #RhythmDBQueryModel. From there, entries are grouped by particular property
* values to form #RhythmDBPropertyModel<!-- -->s.
*
* #RhythmDBEntry contains a fixed set of properties, defined by #RhythmDBPropType,
*/
#include "config.h"
#define G_IMPLEMENT_INLINES 1
#define __RHYTHMDB_C__
#include "rhythmdb.h"
#undef G_IMPLEMENT_INLINES
#include <string.h>
#include <libxml/tree.h>
#include <glib.h>
#include <glib-object.h>
#include <glib/gi18n.h>
#include <gio/gio.h>
#include <gobject/gvaluecollector.h>
#include <gdk/gdk.h>
#include "rb-file-helpers.h"
#include "rb-debug.h"
#include "rb-util.h"
#include "rb-cut-and-paste-code.h"
#include "rhythmdb-private.h"
#include "rhythmdb-property-model.h"
#include "rb-dialog.h"
#include "rb-string-value-map.h"
#include "rb-async-queue-watch.h"
#include "rb-podcast-entry-types.h"
#include "rb-gst-media-types.h"
#define PROP_ENTRY(p,t,n) { RHYTHMDB_PROP_ ## p, "RHYTHMDB_PROP_" #p "", t, n }
typedef struct _RhythmDBPropertyDef {
RhythmDBPropType prop_id;
const char *prop_name;
GType prop_type;
const char *elt_name;
} RhythmDBPropertyDef;
static const RhythmDBPropertyDef rhythmdb_properties[] = {
PROP_ENTRY(TYPE, G_TYPE_OBJECT, "type"),
PROP_ENTRY(ENTRY_ID, G_TYPE_ULONG, "entry-id"),
PROP_ENTRY(TITLE, G_TYPE_STRING, "title"),
PROP_ENTRY(GENRE, G_TYPE_STRING, "genre"),
PROP_ENTRY(ARTIST, G_TYPE_STRING, "artist"),
PROP_ENTRY(ALBUM, G_TYPE_STRING, "album"),
PROP_ENTRY(TRACK_NUMBER, G_TYPE_ULONG, "track-number"),
PROP_ENTRY(TRACK_TOTAL, G_TYPE_ULONG, "track-total"),
PROP_ENTRY(DISC_NUMBER, G_TYPE_ULONG, "disc-number"),
PROP_ENTRY(DISC_TOTAL, G_TYPE_ULONG, "disc-total"),
PROP_ENTRY(DURATION, G_TYPE_ULONG, "duration"),
PROP_ENTRY(FILE_SIZE, G_TYPE_UINT64, "file-size"),
PROP_ENTRY(LOCATION, G_TYPE_STRING, "location"),
PROP_ENTRY(MOUNTPOINT, G_TYPE_STRING, "mountpoint"),
PROP_ENTRY(MTIME, G_TYPE_ULONG, "mtime"),
PROP_ENTRY(FIRST_SEEN, G_TYPE_ULONG, "first-seen"),
PROP_ENTRY(LAST_SEEN, G_TYPE_ULONG, "last-seen"),
PROP_ENTRY(RATING, G_TYPE_DOUBLE, "rating"),
PROP_ENTRY(PLAY_COUNT, G_TYPE_ULONG, "play-count"),
PROP_ENTRY(LAST_PLAYED, G_TYPE_ULONG, "last-played"),
PROP_ENTRY(BITRATE, G_TYPE_ULONG, "bitrate"),
PROP_ENTRY(DATE, G_TYPE_ULONG, "date"),
PROP_ENTRY(TRACK_GAIN, G_TYPE_DOUBLE, "replaygain-track-gain"),
PROP_ENTRY(TRACK_PEAK, G_TYPE_DOUBLE, "replaygain-track-peak"),
PROP_ENTRY(ALBUM_GAIN, G_TYPE_DOUBLE, "replaygain-album-gain"),
PROP_ENTRY(ALBUM_PEAK, G_TYPE_DOUBLE, "replaygain-album-peak"),
PROP_ENTRY(MEDIA_TYPE, G_TYPE_STRING, "media-type"),
PROP_ENTRY(TITLE_SORT_KEY, G_TYPE_STRING, "title-sort-key"),
PROP_ENTRY(GENRE_SORT_KEY, G_TYPE_STRING, "genre-sort-key"),
PROP_ENTRY(ARTIST_SORT_KEY, G_TYPE_STRING, "artist-sort-key"),
PROP_ENTRY(ALBUM_SORT_KEY, G_TYPE_STRING, "album-sort-key"),
PROP_ENTRY(TITLE_FOLDED, G_TYPE_STRING, "title-folded"),
PROP_ENTRY(GENRE_FOLDED, G_TYPE_STRING, "genre-folded"),
PROP_ENTRY(ARTIST_FOLDED, G_TYPE_STRING, "artist-folded"),
PROP_ENTRY(ALBUM_FOLDED, G_TYPE_STRING, "album-folded"),
PROP_ENTRY(LAST_PLAYED_STR, G_TYPE_STRING, "last-played-str"),
PROP_ENTRY(HIDDEN, G_TYPE_BOOLEAN, "hidden"),
PROP_ENTRY(PLAYBACK_ERROR, G_TYPE_STRING, "playback-error"),
PROP_ENTRY(FIRST_SEEN_STR, G_TYPE_STRING, "first-seen-str"),
PROP_ENTRY(LAST_SEEN_STR, G_TYPE_STRING, "last-seen-str"),
PROP_ENTRY(SEARCH_MATCH, G_TYPE_STRING, "search-match"),
PROP_ENTRY(YEAR, G_TYPE_ULONG, "year"),
PROP_ENTRY(KEYWORD, G_TYPE_STRING, "keyword"),
PROP_ENTRY(STATUS, G_TYPE_ULONG, "status"),
PROP_ENTRY(DESCRIPTION, G_TYPE_STRING, "description"),
PROP_ENTRY(SUBTITLE, G_TYPE_STRING, "subtitle"),
PROP_ENTRY(SUMMARY, G_TYPE_STRING, "summary"),
PROP_ENTRY(LANG, G_TYPE_STRING, "lang"),
PROP_ENTRY(COPYRIGHT, G_TYPE_STRING, "copyright"),
PROP_ENTRY(IMAGE, G_TYPE_STRING, "image"),
PROP_ENTRY(POST_TIME, G_TYPE_ULONG, "post-time"),
PROP_ENTRY(PODCAST_GUID, G_TYPE_STRING, "podcast-guid"),
PROP_ENTRY(MUSICBRAINZ_TRACKID, G_TYPE_STRING, "mb-trackid"),
PROP_ENTRY(MUSICBRAINZ_ARTISTID, G_TYPE_STRING, "mb-artistid"),
PROP_ENTRY(MUSICBRAINZ_ALBUMID, G_TYPE_STRING, "mb-albumid"),
PROP_ENTRY(MUSICBRAINZ_ALBUMARTISTID, G_TYPE_STRING, "mb-albumartistid"),
PROP_ENTRY(ARTIST_SORTNAME, G_TYPE_STRING, "mb-artistsortname"),
PROP_ENTRY(ALBUM_SORTNAME, G_TYPE_STRING, "album-sortname"),
PROP_ENTRY(ARTIST_SORTNAME_SORT_KEY, G_TYPE_STRING, "artist-sortname-sort-key"),
PROP_ENTRY(ARTIST_SORTNAME_FOLDED, G_TYPE_STRING, "artist-sortname-folded"),
PROP_ENTRY(ALBUM_SORTNAME_SORT_KEY, G_TYPE_STRING, "album-sortname-sort-key"),
PROP_ENTRY(ALBUM_SORTNAME_FOLDED, G_TYPE_STRING, "album-sortname-folded"),
PROP_ENTRY(COMMENT, G_TYPE_STRING, "comment"),
PROP_ENTRY(ALBUM_ARTIST, G_TYPE_STRING, "album-artist"),
PROP_ENTRY(ALBUM_ARTIST_SORT_KEY, G_TYPE_STRING, "album-artist-sort-key"),
PROP_ENTRY(ALBUM_ARTIST_FOLDED, G_TYPE_STRING, "album-artist-folded"),
PROP_ENTRY(ALBUM_ARTIST_SORTNAME, G_TYPE_STRING, "album-artist-sortname"),
PROP_ENTRY(ALBUM_ARTIST_SORTNAME_SORT_KEY, G_TYPE_STRING, "album-artist-sortname-sort-key"),
PROP_ENTRY(ALBUM_ARTIST_SORTNAME_FOLDED, G_TYPE_STRING, "album-artist-sortname-folded"),
PROP_ENTRY(BPM, G_TYPE_DOUBLE, "beats-per-minute"),
PROP_ENTRY(COMPOSER, G_TYPE_STRING, "composer"),
PROP_ENTRY(COMPOSER_SORT_KEY, G_TYPE_STRING, "composer-sort-key"),
PROP_ENTRY(COMPOSER_FOLDED, G_TYPE_STRING, "composer-folded"),
PROP_ENTRY(COMPOSER_SORTNAME, G_TYPE_STRING, "composer-sortname"),
PROP_ENTRY(COMPOSER_SORTNAME_SORT_KEY, G_TYPE_STRING, "composer-sortname-sort-key"),
PROP_ENTRY(COMPOSER_SORTNAME_FOLDED, G_TYPE_STRING, "composer-sortname-folded"),
{ 0, 0, 0, 0 }
};
#define RB_PARSE_NICK_START (xmlChar *) "["
#define RB_PARSE_NICK_END (xmlChar *) "]"
#define RHYTHMDB_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), RHYTHMDB_TYPE, RhythmDBPrivate))
G_DEFINE_ABSTRACT_TYPE(RhythmDB, rhythmdb, G_TYPE_OBJECT)
/* file attributes requested in RHYTHMDB_ACTION_STAT and RHYTHMDB_ACTION_LOAD */
#define RHYTHMDB_FILE_INFO_ATTRIBUTES \
G_FILE_ATTRIBUTE_STANDARD_SIZE "," \
G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME "," \
G_FILE_ATTRIBUTE_STANDARD_TYPE "," \
G_FILE_ATTRIBUTE_TIME_MODIFIED
/* file attributes requested in RHYTHMDB_ACTION_ENUM_DIR */
#define RHYTHMDB_FILE_CHILD_INFO_ATTRIBUTES \
RHYTHMDB_FILE_INFO_ATTRIBUTES "," \
G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN "," \
G_FILE_ATTRIBUTE_STANDARD_NAME
/*
* Filters for MIME/media types to ignore.
* The only complication here is that there are some application/ types that
* are used for audio/video files. Otherwise, we'd ignore everything except
* audio/ and video/.
*/
struct media_type_filter {
const char *prefix;
gboolean ignore;
} media_type_filters[] = {
{ "image/", TRUE },
{ "text/", TRUE },
{ "application/ogg", FALSE },
{ "application/x-id3", FALSE },
{ "application/x-apetag", FALSE },
{ "application/x-3gp", FALSE },
{ "application/x-annodex", FALSE },
{ "application/", TRUE },
};
/*
* File size below which we will simply ignore files that can't be identified.
* This is mostly here so we ignore the various text files that are packaged
* with many netlabel releases and other downloads.
*/
#define REALLY_SMALL_FILE_SIZE (4096)
typedef struct
{
RhythmDB *db;
GPtrArray *query;
guint propid;
RhythmDBQueryResults *results;
gboolean cancel;
} RhythmDBQueryThreadData;
typedef struct
{
RhythmDB *db;
RhythmDBEntryType *type;
RhythmDBEntryType *ignore_type;
RhythmDBEntryType *error_type;
} RhythmDBAddThreadData;
typedef struct
{
enum {
RHYTHMDB_ACTION_STAT,
RHYTHMDB_ACTION_LOAD,
RHYTHMDB_ACTION_ENUM_DIR,
RHYTHMDB_ACTION_SYNC,
RHYTHMDB_ACTION_QUIT,
} type;
RBRefString *uri;
union {
struct {
RhythmDBEntryType *entry_type;
RhythmDBEntryType *ignore_type;
RhythmDBEntryType *error_type;
} types;
GSList *changes;
} data;
} RhythmDBAction;
static void rhythmdb_dispose (GObject *object);
static void rhythmdb_finalize (GObject *object);
static void rhythmdb_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void rhythmdb_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
static GThread * rhythmdb_thread_create (RhythmDB *db,
GThreadFunc func,
gpointer data);
static void rhythmdb_read_enter (RhythmDB *db);
static void rhythmdb_read_leave (RhythmDB *db);
static void rhythmdb_process_one_event (RhythmDBEvent *event, RhythmDB *db);
static gpointer action_thread_main (RhythmDB *db);
static gpointer query_thread_main (RhythmDBQueryThreadData *data);
static void rhythmdb_entry_set_mount_point (RhythmDB *db,
RhythmDBEntry *entry,
const gchar *realuri);
static gboolean rhythmdb_idle_save (RhythmDB *db);
static void db_settings_changed_cb (GSettings *settings, const char *key, RhythmDB *db);
static void rhythmdb_sync_library_location (RhythmDB *db);
static void rhythmdb_entry_sync_mirrored (RhythmDBEntry *entry,
guint propid);
static gboolean rhythmdb_entry_extra_metadata_accumulator (GSignalInvocationHint *ihint,
GValue *return_accu,
const GValue *handler_return,
gpointer data);
static void rhythmdb_event_free (RhythmDB *db, RhythmDBEvent *event);
static void rhythmdb_add_to_stat_list (RhythmDB *db,
const char *uri,
RhythmDBEntry *entry,
RhythmDBEntryType *type,
RhythmDBEntryType *ignore_type,
RhythmDBEntryType *error_type);
static void free_entry_changes (GSList *entry_changes);
static RhythmDBEntry *rhythmdb_add_import_error_entry (RhythmDB *db, RhythmDBEvent *event, RhythmDBEntryType *error_entry_type);
static void perform_next_mount (RhythmDB *db);
enum
{
PROP_0,
PROP_NAME,
PROP_DRY_RUN,
PROP_NO_UPDATE,
};
enum
{
ENTRY_ADDED,
ENTRY_CHANGED,
ENTRY_DELETED,
ENTRY_KEYWORD_ADDED,
ENTRY_KEYWORD_REMOVED,
ENTRY_EXTRA_METADATA_REQUEST,
ENTRY_EXTRA_METADATA_NOTIFY,
ENTRY_EXTRA_METADATA_GATHER,
LOAD_COMPLETE,
SAVE_COMPLETE,
SAVE_ERROR,
READ_ONLY,
CREATE_MOUNT_OP,
LAST_SIGNAL
};
static guint rhythmdb_signals[LAST_SIGNAL] = { 0 };
static void
rhythmdb_class_init (RhythmDBClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->dispose = rhythmdb_dispose;
object_class->finalize = rhythmdb_finalize;
object_class->set_property = rhythmdb_set_property;
object_class->get_property = rhythmdb_get_property;
/**
* RhythmDB:name:
*
* Database name. Not sure whta this is used for.
*/
g_object_class_install_property (object_class,
PROP_NAME,
g_param_spec_string ("name",
"name",
"name",
NULL,
G_PARAM_READWRITE));
/**
* RhythmDB:dry-run:
*
* If %TRUE, no metadata changes will be written back to media fies.
*/
g_object_class_install_property (object_class,
PROP_DRY_RUN,
g_param_spec_boolean ("dry-run",
"dry run",
"Whether or not changes should be saved",
FALSE,
G_PARAM_READWRITE));
/**
* RhythmDB:no-update:
*
* If %TRUE, the database will not be updated.
*/
g_object_class_install_property (object_class,
PROP_NO_UPDATE,
g_param_spec_boolean ("no-update",
"no update",
"Whether or not to update the database",
FALSE,
G_PARAM_READWRITE));
/**
* RhythmDB::entry-added:
* @db: the #RhythmDB
* @entry: the newly added #RhythmDBEntry
*
* Emitted when a new entry is added to the database.
*/
rhythmdb_signals[ENTRY_ADDED] =
g_signal_new ("entry_added",
RHYTHMDB_TYPE,
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (RhythmDBClass, entry_added),
NULL, NULL,
NULL,
G_TYPE_NONE,
1, RHYTHMDB_TYPE_ENTRY);
/**
* RhythmDB::entry-deleted:
* @db: the #RhythmDB
* @entry: the deleted #RhythmDBEntry
*
* Emitted when an entry is deleted from the database.
*/
rhythmdb_signals[ENTRY_DELETED] =
g_signal_new ("entry_deleted",
RHYTHMDB_TYPE,
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (RhythmDBClass, entry_deleted),
NULL, NULL,
NULL,
G_TYPE_NONE,
1, RHYTHMDB_TYPE_ENTRY);
/**
* RhythmDB::entry-changed:
* @db: the #RhythmDB
* @entry: the changed #RhythmDBEntry
* @changes: (element-type RB.RhythmDBEntryChange): a #GPtrArray of #RhythmDBEntryChange structures describing the changes
*
* Emitted when a database entry is modified. The @changes list
* contains a structure for each entry property that has been modified.
*/
rhythmdb_signals[ENTRY_CHANGED] =
g_signal_new ("entry-changed",
RHYTHMDB_TYPE,
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (RhythmDBClass, entry_changed),
NULL, NULL,
NULL,
G_TYPE_NONE, 2,
RHYTHMDB_TYPE_ENTRY, G_TYPE_PTR_ARRAY);
/**
* RhythmDB::entry-keyword-added:
* @db: the #RhythmDB
* @entry: the #RhythmDBEntry to which a keyword has been added
* @keyword: the keyword that was added
*
* Emitted when a keyword is added to an entry.
*/
rhythmdb_signals[ENTRY_KEYWORD_ADDED] =
g_signal_new ("entry_keyword_added",
RHYTHMDB_TYPE,
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (RhythmDBClass, entry_added),
NULL, NULL,
NULL,
G_TYPE_NONE,
2, RHYTHMDB_TYPE_ENTRY, RB_TYPE_REFSTRING);
/**
* RhythmDB::entry-keyword-removed:
* @db: the #RhythmDB
* @entry: the #RhythmDBEntry from which a keyword has been removed
* @keyword: the keyword that was removed
*
* Emitted when a keyword is removed from an entry.
*/
rhythmdb_signals[ENTRY_KEYWORD_REMOVED] =
g_signal_new ("entry_keyword_removed",
RHYTHMDB_TYPE,
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (RhythmDBClass, entry_deleted),
NULL, NULL,
NULL,
G_TYPE_NONE,
2, RHYTHMDB_TYPE_ENTRY, RB_TYPE_REFSTRING);
/**
* RhythmDB::entry-extra-metadata-request:
* @db: the #RhythmDB
* @entry: the #RhythmDBEntry for which extra metadata is being requested
*
* This signal is emitted to allow extra (transient) metadata to be supplied
* for the given entry. The detail of the signal invocation describes the
* specific metadata value being requested. If the object handling the signal
* can provide the requested item, but it isn't immediately available, it can
* initiate an attempt to retrieve it. If successful, it would call
* @rhythmdb_emit_entry_extra_metadata_notify when the metadata is available.
*
* Return value: the extra metadata value
*/
rhythmdb_signals[ENTRY_EXTRA_METADATA_REQUEST] =
g_signal_new ("entry_extra_metadata_request",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
G_STRUCT_OFFSET (RhythmDBClass, entry_extra_metadata_request),
rhythmdb_entry_extra_metadata_accumulator, NULL,
NULL,
G_TYPE_VALUE, 1,
RHYTHMDB_TYPE_ENTRY);
/**
* RhythmDB::entry-extra-metadata-notify:
* @db: the #RhythmDB
* @entry: the #RhythmDBEntry for which extra metadata has been supplied
* @field: the extra metadata field being supplied
* @metadata: the extra metadata value
*
* This signal is emitted when an extra metadata value is provided for a specific
* entry independantly of an extra metadata request.
*/
rhythmdb_signals[ENTRY_EXTRA_METADATA_NOTIFY] =
g_signal_new ("entry_extra_metadata_notify",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
G_STRUCT_OFFSET (RhythmDBClass, entry_extra_metadata_notify),
NULL, NULL,
NULL,
G_TYPE_NONE, 3,
RHYTHMDB_TYPE_ENTRY, G_TYPE_STRING, G_TYPE_VALUE);
/**
* RhythmDB::entry-extra-metadata-gather:
* @db: the #RhythmDB
* @entry: the #RhythmDBEntry for which to gather metadata
* @data: a #RBStringValueMap to hold the gathered metadata
*
* Emitted to gather all available extra metadata for a database entry.
* Handlers for this signal should insert any metadata they can provide
* into the string-value map. Only immediately available metadata
* items should be returned. If one or more metadata items is not
* immediately available, the handler should not initiate an attempt to
* retrieve them.
*/
rhythmdb_signals[ENTRY_EXTRA_METADATA_GATHER] =
g_signal_new ("entry_extra_metadata_gather",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (RhythmDBClass, entry_extra_metadata_gather),
NULL, NULL,
NULL,
G_TYPE_NONE, 2,
RHYTHMDB_TYPE_ENTRY, RB_TYPE_STRING_VALUE_MAP);
/**
* RhythmDB::load-complete:
* @db: the #RhythmDB
*
* Emitted when the database is fully loaded.
*/
rhythmdb_signals[LOAD_COMPLETE] =
g_signal_new ("load_complete",
RHYTHMDB_TYPE,
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (RhythmDBClass, load_complete),
NULL, NULL,
NULL,
G_TYPE_NONE,
0);
/**
* RhythmDB::save-complete:
* @db: the #RhythmDB
*
* Emitted when the database has been saved.
*/
rhythmdb_signals[SAVE_COMPLETE] =
g_signal_new ("save_complete",
RHYTHMDB_TYPE,
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (RhythmDBClass, save_complete),
NULL, NULL,
NULL,
G_TYPE_NONE,
0);
/**
* RhythmDB::save-error:
* @db: the #RhythmDB
* @uri: URI of the database file
* @error: the error that occurred
*
* Emitted when an error occurs while saving the database.
*/
rhythmdb_signals[SAVE_ERROR] =
g_signal_new ("save-error",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (RhythmDBClass, save_error),
NULL, NULL,
NULL,
G_TYPE_NONE,
2,
G_TYPE_STRING,
G_TYPE_POINTER);
/**
* RhythmDB::read-only:
* @db: the #RhythmDB
* @readonly: %TRUE if the database is read-only
*
* Emitted when the database becomes temporarily read-only, or becomes
* writeable after being read-only.
*/
rhythmdb_signals[READ_ONLY] =
g_signal_new ("read-only",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (RhythmDBClass, read_only),
NULL, NULL,
NULL,
G_TYPE_NONE,
1,
G_TYPE_BOOLEAN);
/**
* RhythmDB::create-mount-op:
* @db: the #RhythmDB
*
* Emitted to request creation of a #GMountOperation to use to mount a volume.
*
* Returns: (transfer full): a #GMountOperation (usually actually a #GtkMountOperation)
*/
rhythmdb_signals[CREATE_MOUNT_OP] =
g_signal_new ("create-mount-op",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
0, /* no need for an internal handler */
rb_signal_accumulator_object_handled, NULL,
NULL,
G_TYPE_MOUNT_OPERATION,
0);
g_type_class_add_private (klass, sizeof (RhythmDBPrivate));
}
static void
rhythmdb_push_event (RhythmDB *db, RhythmDBEvent *event)
{
g_async_queue_push (db->priv->event_queue, event);
g_main_context_wakeup (g_main_context_default ());
}
static gboolean
metadata_field_from_prop (RhythmDBPropType prop,
RBMetaDataField *field)
{
switch (prop) {
case RHYTHMDB_PROP_TITLE:
*field = RB_METADATA_FIELD_TITLE;
return TRUE;
case RHYTHMDB_PROP_ARTIST:
*field = RB_METADATA_FIELD_ARTIST;
return TRUE;
case RHYTHMDB_PROP_ALBUM:
*field = RB_METADATA_FIELD_ALBUM;
return TRUE;
case RHYTHMDB_PROP_GENRE:
*field = RB_METADATA_FIELD_GENRE;
return TRUE;
case RHYTHMDB_PROP_COMMENT:
*field = RB_METADATA_FIELD_COMMENT;
return TRUE;
case RHYTHMDB_PROP_TRACK_NUMBER:
*field = RB_METADATA_FIELD_TRACK_NUMBER;
return TRUE;
case RHYTHMDB_PROP_TRACK_TOTAL:
*field = RB_METADATA_FIELD_MAX_TRACK_NUMBER;
return TRUE;
case RHYTHMDB_PROP_DISC_NUMBER:
*field = RB_METADATA_FIELD_DISC_NUMBER;
return TRUE;
case RHYTHMDB_PROP_DISC_TOTAL:
*field = RB_METADATA_FIELD_MAX_DISC_NUMBER;
return TRUE;
case RHYTHMDB_PROP_DATE:
*field = RB_METADATA_FIELD_DATE;
return TRUE;
case RHYTHMDB_PROP_BPM:
*field = RB_METADATA_FIELD_BPM;
return TRUE;
case RHYTHMDB_PROP_MUSICBRAINZ_TRACKID:
*field = RB_METADATA_FIELD_MUSICBRAINZ_TRACKID;
return TRUE;
case RHYTHMDB_PROP_MUSICBRAINZ_ARTISTID:
*field = RB_METADATA_FIELD_MUSICBRAINZ_ARTISTID;
return TRUE;
case RHYTHMDB_PROP_MUSICBRAINZ_ALBUMID:
*field = RB_METADATA_FIELD_MUSICBRAINZ_ALBUMID;
return TRUE;
case RHYTHMDB_PROP_MUSICBRAINZ_ALBUMARTISTID:
*field = RB_METADATA_FIELD_MUSICBRAINZ_ALBUMARTISTID;
return TRUE;
case RHYTHMDB_PROP_ARTIST_SORTNAME:
*field = RB_METADATA_FIELD_ARTIST_SORTNAME;
return TRUE;
case RHYTHMDB_PROP_ALBUM_SORTNAME:
*field = RB_METADATA_FIELD_ALBUM_SORTNAME;
return TRUE;
case RHYTHMDB_PROP_ALBUM_ARTIST:
*field = RB_METADATA_FIELD_ALBUM_ARTIST;
return TRUE;
case RHYTHMDB_PROP_ALBUM_ARTIST_SORTNAME:
*field = RB_METADATA_FIELD_ALBUM_ARTIST_SORTNAME;
return TRUE;
case RHYTHMDB_PROP_COMPOSER:
*field = RB_METADATA_FIELD_COMPOSER;
return TRUE;
case RHYTHMDB_PROP_COMPOSER_SORTNAME:
*field = RB_METADATA_FIELD_COMPOSER_SORTNAME;
return TRUE;
default:
return FALSE;
}
}
static void
rhythmdb_init (RhythmDB *db)
{
guint i;
GEnumClass *prop_class;
db->priv = RHYTHMDB_GET_PRIVATE (db);
db->priv->settings = g_settings_new ("org.gnome.rhythmbox.rhythmdb");
g_signal_connect_object (db->priv->settings, "changed", G_CALLBACK (db_settings_changed_cb), db, 0);
db->priv->action_queue = g_async_queue_new ();
db->priv->event_queue = g_async_queue_new ();
db->priv->delayed_write_queue = g_async_queue_new ();
db->priv->event_queue_watch_id = rb_async_queue_watch_new (db->priv->event_queue,
G_PRIORITY_LOW, /* really? */
(RBAsyncQueueWatchFunc) rhythmdb_process_one_event,
db,
NULL,
NULL);
db->priv->restored_queue = g_async_queue_new ();
db->priv->query_thread_pool = g_thread_pool_new ((GFunc)query_thread_main,
NULL,
-1, FALSE, NULL);
db->priv->metadata = rb_metadata_new ();
prop_class = g_type_class_ref (RHYTHMDB_TYPE_PROP_TYPE);
g_assert (prop_class->n_values == RHYTHMDB_NUM_PROPERTIES);
g_type_class_unref (prop_class);
db->priv->propname_map = g_hash_table_new (g_str_hash, g_str_equal);
for (i = 0; i < RHYTHMDB_NUM_PROPERTIES; i++) {
const xmlChar *name = rhythmdb_nice_elt_name_from_propid (db, i);
g_hash_table_insert (db->priv->propname_map, (gpointer) name, GINT_TO_POINTER (i));
}
db->priv->entry_type_map = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
rhythmdb_register_song_entry_types (db);
rb_podcast_register_entry_types (db);
db->priv->changed_entries = g_hash_table_new_full (NULL,
NULL,
(GDestroyNotify) rhythmdb_entry_unref,
NULL);
db->priv->added_entries = g_hash_table_new_full (NULL,
NULL,
(GDestroyNotify) rhythmdb_entry_unref,
NULL);
db->priv->deleted_entries = g_hash_table_new_full (NULL,
NULL,
(GDestroyNotify) rhythmdb_entry_unref,
NULL);
db->priv->can_save = TRUE;
db->priv->exiting = g_cancellable_new ();
db->priv->saving = FALSE;
db->priv->dirty = FALSE;
db->priv->empty_string = rb_refstring_new ("");
db->priv->octet_stream_str = rb_refstring_new ("application/octet-stream");
db->priv->next_entry_id = 1;
rhythmdb_init_monitoring (db);
rhythmdb_dbus_register (db);
}
static GError *
make_access_failed_error (const char *uri, GError *access_error)
{
char *unescaped;
char *utf8ised;
GError *error;
/* make sure the URI we put in the error message is valid utf8 */
unescaped = g_uri_unescape_string (uri, NULL);
utf8ised = rb_make_valid_utf8 (unescaped, '?');
error = g_error_new (RHYTHMDB_ERROR,
RHYTHMDB_ERROR_ACCESS_FAILED,
_("Couldn't access %s: %s"),
utf8ised,
access_error->message);
rb_debug ("got error on %s: %s", uri, error->message);
g_free (unescaped);
g_free (utf8ised);
return error;
}
static gboolean
rhythmdb_ignore_media_type (const char *media_type)
{
int i;
for (i = 0; i < G_N_ELEMENTS (media_type_filters); i++) {
if (g_str_has_prefix (media_type, media_type_filters[i].prefix)) {
return media_type_filters[i].ignore;
}
}
return FALSE;
}
typedef struct {
RhythmDB *db;
GList *stat_list;
} RhythmDBStatThreadData;
static gpointer
stat_thread_main (RhythmDBStatThreadData *data)
{
GList *i;
GError *error = NULL;
RhythmDBEvent *result;
data->db->priv->stat_thread_count = g_list_length (data->stat_list);
data->db->priv->stat_thread_done = 0;
rb_debug ("entering stat thread: %d to process", data->db->priv->stat_thread_count);
for (i = data->stat_list; i != NULL; i = i->next) {
RhythmDBEvent *event = (RhythmDBEvent *)i->data;
GFile *file;
/* if we've been cancelled, just free the event. this will
* clean up the list and then we'll exit the thread.
*/
if (g_cancellable_is_cancelled (data->db->priv->exiting)) {
rhythmdb_event_free (data->db, event);
continue;
}
if (data->db->priv->stat_thread_done > 0 &&
data->db->priv->stat_thread_done % 1000 == 0) {
rb_debug ("%d file info queries done",
data->db->priv->stat_thread_done);
}
file = g_file_new_for_uri (rb_refstring_get (event->uri));
event->real_uri = rb_refstring_ref (event->uri); /* what? */
event->file_info = g_file_query_info (file,
G_FILE_ATTRIBUTE_TIME_MODIFIED, /* anything else? */
G_FILE_QUERY_INFO_NONE,
data->db->priv->exiting,
&error);
if (error != NULL) {
event->error = make_access_failed_error (rb_refstring_get (event->uri), error);
g_clear_error (&error);
if (event->file_info != NULL) {
g_object_unref (event->file_info);
event->file_info = NULL;
}
}
g_async_queue_push (data->db->priv->event_queue, event);
g_object_unref (file);
g_atomic_int_inc (&data->db->priv->stat_thread_done);
}
g_list_free (data->stat_list);
data->db->priv->stat_thread_running = FALSE;
rb_debug ("exiting stat thread");
result = g_slice_new0 (RhythmDBEvent);
result->db = data->db; /* need to unref? */
result->type = RHYTHMDB_EVENT_THREAD_EXITED;
rhythmdb_push_event (data->db, result);
g_free (data);
return NULL;
}
static void
perform_next_mount_cb (GObject *file, GAsyncResult *res, RhythmDB *db)
{
GError *error = NULL;
g_file_mount_enclosing_volume_finish (G_FILE (file), res, &error);
if (error != NULL) {
char *uri;
uri = g_file_get_uri (G_FILE (file));
rb_debug ("Unable to mount %s: %s", uri, error->message);
g_free (uri);
g_clear_error (&error);
}
g_object_unref (file);
perform_next_mount (db);
}
static void
perform_next_mount (RhythmDB *db)
{
GList *l;
char *mountpoint;
GMountOperation *mount_op = NULL;
if (db->priv->mount_list == NULL) {
rb_debug ("finished mounting");
return;
}
l = db->priv->mount_list;
db->priv->mount_list = db->priv->mount_list->next;
mountpoint = l->data;
g_list_free1 (l);
rb_debug ("mounting %s", (char *)mountpoint);
g_signal_emit (G_OBJECT (db), rhythmdb_signals[CREATE_MOUNT_OP], 0, &mount_op);
g_file_mount_enclosing_volume (g_file_new_for_uri (mountpoint),
G_MOUNT_MOUNT_NONE,
mount_op,
db->priv->exiting,
(GAsyncReadyCallback) perform_next_mount_cb,
db);
}
/**
* rhythmdb_start_action_thread:
* @db: the #RhythmDB
*
* Starts the #RhythmDB processing thread. Needs to be called during startup.
*/
void
rhythmdb_start_action_thread (RhythmDB *db)
{
g_mutex_lock (&db->priv->stat_mutex);
db->priv->action_thread_running = TRUE;
rhythmdb_thread_create (db, (GThreadFunc) action_thread_main, db);
if (db->priv->stat_list != NULL) {
RhythmDBStatThreadData *data;
data = g_new0 (RhythmDBStatThreadData, 1);
data->db = g_object_ref (db);
data->stat_list = db->priv->stat_list;
db->priv->stat_list = NULL;
db->priv->stat_thread_running = TRUE;
rhythmdb_thread_create (db, (GThreadFunc) stat_thread_main, data);
}
perform_next_mount (db);
g_mutex_unlock (&db->priv->stat_mutex);
}
static void
rhythmdb_action_free (RhythmDB *db,
RhythmDBAction *action)
{
rb_refstring_unref (action->uri);
if (action->type == RHYTHMDB_ACTION_SYNC) {
free_entry_changes (action->data.changes);
}
g_slice_free (RhythmDBAction, action);
}
static void
free_cached_metadata (GArray *metadata)
{
RhythmDBEntryChange *fields = (RhythmDBEntryChange *)metadata->data;
int i;
for (i = 0; i < metadata->len; i++) {
g_value_unset (&fields[i].new);
}
g_free (fields);
metadata->data = NULL;
metadata->len = 0;
}
static void
rhythmdb_event_free (RhythmDB *db,
RhythmDBEvent *result)
{
switch (result->type) {
case RHYTHMDB_EVENT_THREAD_EXITED:
g_object_unref (db);
g_assert (g_atomic_int_dec_and_test (&db->priv->outstanding_threads) >= 0);
g_async_queue_unref (db->priv->action_queue);
g_async_queue_unref (db->priv->event_queue);
break;
case RHYTHMDB_EVENT_STAT:
case RHYTHMDB_EVENT_METADATA_LOAD:
case RHYTHMDB_EVENT_DB_LOAD:
case RHYTHMDB_EVENT_DB_SAVED:
case RHYTHMDB_EVENT_QUERY_COMPLETE:
break;
case RHYTHMDB_EVENT_ENTRY_SET:
g_value_unset (&result->change.new);
break;
case RHYTHMDB_EVENT_METADATA_CACHE:
free_cached_metadata(&result->cached_metadata);
break;
case RHYTHMDB_EVENT_BARRIER:
break;
}
if (result->error)
g_error_free (result->error);
rb_refstring_unref (result->uri);
rb_refstring_unref (result->real_uri);
if (result->file_info)
g_object_unref (result->file_info);
if (result->metadata)
g_object_unref (result->metadata);
if (result->results)
g_object_unref (result->results);
if (result->entry != NULL) {
rhythmdb_entry_unref (result->entry);
}
g_slice_free (RhythmDBEvent, result);
}
static void
_shutdown_foreach_swapped (RhythmDBEvent *event, RhythmDB *db)
{
rhythmdb_event_free (db, event);
}
/**
* rhythmdb_shutdown:
* @db: the #RhythmDB
*
* Ceases all #RhythmDB operations, including stopping all directory monitoring, and
* removing all actions and events currently queued.
*/
void
rhythmdb_shutdown (RhythmDB *db)
{
RhythmDBEvent *result;
RhythmDBAction *action;
g_return_if_fail (RHYTHMDB_IS (db));
g_cancellable_cancel (db->priv->exiting);
/* force the action thread to wake up and exit */
action = g_slice_new0 (RhythmDBAction);
action->type = RHYTHMDB_ACTION_QUIT;
g_async_queue_push (db->priv->action_queue, action);
/* abort all async io operations */
g_mutex_lock (&db->priv->stat_mutex);
g_list_foreach (db->priv->outstanding_stats, (GFunc)_shutdown_foreach_swapped, db);
g_list_free (db->priv->outstanding_stats);
db->priv->outstanding_stats = NULL;
g_mutex_unlock (&db->priv->stat_mutex);
g_clear_handle_id (&db->priv->sync_library_id, g_source_remove);
rb_debug ("%d outstanding threads", g_atomic_int_get (&db->priv->outstanding_threads));
while (g_atomic_int_get (&db->priv->outstanding_threads) > 0) {
result = g_async_queue_pop (db->priv->event_queue);
rhythmdb_event_free (db, result);
}
/* FIXME */
while ((result = g_async_queue_try_pop (db->priv->event_queue)) != NULL)
rhythmdb_event_free (db, result);
while ((result = g_async_queue_try_pop (db->priv->delayed_write_queue)) != NULL)
rhythmdb_event_free (db, result);
while ((action = g_async_queue_try_pop (db->priv->action_queue)) != NULL) {
rhythmdb_action_free (db, action);
}
}
static void
rhythmdb_dispose (GObject *object)
{
RhythmDB *db;
g_return_if_fail (object != NULL);
g_return_if_fail (RHYTHMDB_IS (object));
rb_debug ("disposing rhythmdb");
db = RHYTHMDB (object);
g_return_if_fail (db->priv != NULL);
rhythmdb_dispose_monitoring (db);
rhythmdb_dbus_unregister (db);
if (db->priv->event_queue_watch_id != 0) {
g_source_remove (db->priv->event_queue_watch_id);
db->priv->event_queue_watch_id = 0;
}
if (db->priv->save_timeout_id != 0) {
g_source_remove (db->priv->save_timeout_id);
db->priv->save_timeout_id = 0;
}
if (db->priv->emit_entry_signals_id != 0) {
g_source_remove (db->priv->emit_entry_signals_id);
db->priv->emit_entry_signals_id = 0;
g_list_foreach (db->priv->added_entries_to_emit, (GFunc)rhythmdb_entry_unref, NULL);
g_list_foreach (db->priv->deleted_entries_to_emit, (GFunc)rhythmdb_entry_unref, NULL);
if (db->priv->changed_entries_to_emit != NULL) {
g_hash_table_destroy (db->priv->changed_entries_to_emit);
}
}
if (db->priv->metadata != NULL) {
g_object_unref (db->priv->metadata);
db->priv->metadata = NULL;
}
if (db->priv->exiting != NULL) {
g_object_unref (db->priv->exiting);
db->priv->exiting = NULL;
}
if (db->priv->settings != NULL) {
g_object_unref (db->priv->settings);
db->priv->settings = NULL;
}
G_OBJECT_CLASS (rhythmdb_parent_class)->dispose (object);
}
static void
rhythmdb_finalize (GObject *object)
{
RhythmDB *db;
g_return_if_fail (object != NULL);
g_return_if_fail (RHYTHMDB_IS (object));
rb_debug ("finalizing rhythmdb");
db = RHYTHMDB (object);
g_return_if_fail (db->priv != NULL);
rhythmdb_finalize_monitoring (db);
g_strfreev (db->priv->library_locations);
db->priv->library_locations = NULL;
g_thread_pool_free (db->priv->query_thread_pool, FALSE, TRUE);
g_async_queue_unref (db->priv->action_queue);
g_async_queue_unref (db->priv->event_queue);
g_async_queue_unref (db->priv->restored_queue);
g_async_queue_unref (db->priv->delayed_write_queue);
g_list_free (db->priv->stat_list);
g_hash_table_destroy (db->priv->propname_map);
g_hash_table_destroy (db->priv->added_entries);
g_hash_table_destroy (db->priv->deleted_entries);
g_hash_table_destroy (db->priv->changed_entries);
rb_refstring_unref (db->priv->empty_string);
rb_refstring_unref (db->priv->octet_stream_str);
g_hash_table_destroy (db->priv->entry_type_map);
g_free (db->priv->name);
G_OBJECT_CLASS (rhythmdb_parent_class)->finalize (object);
}
static void
rhythmdb_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
RhythmDB *db = RHYTHMDB (object);
switch (prop_id) {
case PROP_NAME:
g_free (db->priv->name);
db->priv->name = g_value_dup_string (value);
break;
case PROP_DRY_RUN:
db->priv->dry_run = g_value_get_boolean (value);
break;
case PROP_NO_UPDATE:
db->priv->no_update = g_value_get_boolean (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
rhythmdb_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
RhythmDB *source = RHYTHMDB (object);
switch (prop_id) {
case PROP_NAME:
g_value_set_string (value, source->priv->name);
break;
case PROP_DRY_RUN:
g_value_set_boolean (value, source->priv->dry_run);
break;
case PROP_NO_UPDATE:
g_value_set_boolean (value, source->priv->no_update);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static GThread *
rhythmdb_thread_create (RhythmDB *db,
GThreadFunc func,
gpointer data)
{
g_object_ref (db);
g_atomic_int_inc (&db->priv->outstanding_threads);
g_async_queue_ref (db->priv->action_queue);
g_async_queue_ref (db->priv->event_queue);
return g_thread_new ("rhythmdb-thread", (GThreadFunc) func, data);
}
static gboolean
rhythmdb_get_readonly (RhythmDB *db)
{
return (g_atomic_int_get (&db->priv->read_counter) > 0);
}
static void
rhythmdb_read_enter (RhythmDB *db)
{
gint count;
g_return_if_fail (g_atomic_int_get (&db->priv->read_counter) >= 0);
g_assert (rb_is_main_thread ());
count = g_atomic_int_add (&db->priv->read_counter, 1);
rb_debug ("counter: %d", count+1);
if (count == 0)
g_signal_emit (G_OBJECT (db), rhythmdb_signals[READ_ONLY],
0, TRUE);
}
static void
rhythmdb_read_leave (RhythmDB *db)
{
gint count;
g_return_if_fail (rhythmdb_get_readonly (db));
g_assert (rb_is_main_thread ());
count = g_atomic_int_add (&db->priv->read_counter, -1);
rb_debug ("counter: %d", count-1);
if (count == 1) {
g_signal_emit (G_OBJECT (db), rhythmdb_signals[READ_ONLY],
0, FALSE);
/* move any delayed writes back to the main event queue */
if (g_async_queue_length (db->priv->delayed_write_queue) > 0) {
RhythmDBEvent *event;
while ((event = g_async_queue_try_pop (db->priv->delayed_write_queue)) != NULL)
g_async_queue_push (db->priv->event_queue, event);
g_main_context_wakeup (g_main_context_default ());
}
}
}
static void
rhythmdb_entry_change_free (RhythmDBEntryChange *change)
{
g_value_unset (&change->old);
g_value_unset (&change->new);
g_slice_free (RhythmDBEntryChange, change);
}
static RhythmDBEntryChange *
rhythmdb_entry_change_copy (RhythmDBEntryChange *change)
{
RhythmDBEntryChange *c = g_slice_new0 (RhythmDBEntryChange);
c->prop = change->prop;
g_value_init (&c->old, G_VALUE_TYPE (&change->old));
g_value_init (&c->new, G_VALUE_TYPE (&change->new));
g_value_copy (&change->old, &c->old);
g_value_copy (&change->new, &c->new);
return c;
}
static void
free_entry_changes (GSList *entry_changes)
{
GSList *t;
for (t = entry_changes; t; t = t->next) {
RhythmDBEntryChange *change = t->data;
rhythmdb_entry_change_free (change);
}
g_slist_free (entry_changes);
}
static GSList *
copy_entry_changes (GSList *entry_changes)
{
GSList *r = NULL;
GSList *t;
for (t = entry_changes; t; t = t->next) {
RhythmDBEntryChange *change = t->data;
r = g_slist_prepend (r, rhythmdb_entry_change_copy (change));
}
return g_slist_reverse (r);
}
static gboolean
rhythmdb_emit_entry_signals_idle (RhythmDB *db)
{
GList *added_entries;
GList *deleted_entries;
GHashTable *changed_entries;
GList *l;
GHashTableIter iter;
RhythmDBEntry *entry;
GSList *entry_changes;
/* get lists of entries to emit, reset source id value */
g_mutex_lock (&db->priv->change_mutex);
added_entries = db->priv->added_entries_to_emit;
db->priv->added_entries_to_emit = NULL;
deleted_entries = db->priv->deleted_entries_to_emit;
db->priv->deleted_entries_to_emit = NULL;
changed_entries = db->priv->changed_entries_to_emit;
db->priv->changed_entries_to_emit = NULL;
db->priv->emit_entry_signals_id = 0;
g_mutex_unlock (&db->priv->change_mutex);
/* emit changed entries */
if (changed_entries != NULL) {
g_hash_table_iter_init (&iter, changed_entries);
while (g_hash_table_iter_next (&iter, (gpointer *)&entry, (gpointer *)&entry_changes)) {
GPtrArray *emit_changes;
GSList *c;
emit_changes = g_ptr_array_new_full (g_slist_length (entry_changes), NULL);
for (c = entry_changes; c != NULL; c = c->next) {
g_ptr_array_add (emit_changes, c->data);
}
g_signal_emit (G_OBJECT (db), rhythmdb_signals[ENTRY_CHANGED], 0, entry, emit_changes);
g_ptr_array_unref (emit_changes);
g_hash_table_iter_remove (&iter);
}
}
/* emit added entries */
for (l = added_entries; l; l = g_list_next (l)) {
entry = (RhythmDBEntry *)l->data;
g_signal_emit (G_OBJECT (db), rhythmdb_signals[ENTRY_ADDED], 0, entry);
rhythmdb_entry_unref (entry);
}
/* emit deleted entries */
for (l = deleted_entries; l; l = g_list_next (l)) {
entry = (RhythmDBEntry *)l->data;
g_signal_emit (G_OBJECT (db), rhythmdb_signals[ENTRY_DELETED], 0, entry);
rhythmdb_entry_unref (entry);
}
if (changed_entries != NULL) {
g_hash_table_destroy (changed_entries);
}
g_list_free (added_entries);
g_list_free (deleted_entries);
return FALSE;
}
static gboolean
process_added_entries_cb (RhythmDBEntry *entry,
GThread *thread,
RhythmDB *db)
{
if (thread != g_thread_self ())
return FALSE;
if (entry->type == RHYTHMDB_ENTRY_TYPE_SONG) {
const gchar *uri;
uri = rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_LOCATION);
if (uri == NULL)
return TRUE;
/*
* current plan:
* - only stat things with mountpoint == NULL here
* - collect other mountpoints
* just before starting action/stat threads:
* - find remote mountpoints that aren't mounted, try to mount them
* - for local mountpoints that are mounted, add to stat list
* - for everything else, hide entries on those mountpoints
*/
if (thread == db->priv->load_thread) {
const char *mountpoint;
g_mutex_lock (&db->priv->stat_mutex);
mountpoint = rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_MOUNTPOINT);
if (mountpoint == NULL) {
/* entry is on a core filesystem, always check it */
rhythmdb_add_to_stat_list (db, uri, entry,
RHYTHMDB_ENTRY_TYPE_SONG,
RHYTHMDB_ENTRY_TYPE_IGNORE,
RHYTHMDB_ENTRY_TYPE_IMPORT_ERROR);
} else if (rb_string_list_contains (db->priv->active_mounts, mountpoint)) {
/* mountpoint is mounted - check the file if it's local */
if (rb_uri_is_local (mountpoint)) {
rhythmdb_add_to_stat_list (db,
rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_LOCATION),
entry,
RHYTHMDB_ENTRY_TYPE_SONG,
RHYTHMDB_ENTRY_TYPE_IGNORE,
RHYTHMDB_ENTRY_TYPE_IMPORT_ERROR);
} else {
rhythmdb_entry_update_availability (entry, RHYTHMDB_ENTRY_AVAIL_MOUNTED);
}
} else {
/* mountpoint is not mounted */
rhythmdb_entry_update_availability (entry, RHYTHMDB_ENTRY_AVAIL_UNMOUNTED);
if (rb_string_list_contains (db->priv->mount_list, mountpoint) == FALSE) {
db->priv->mount_list = g_list_prepend (db->priv->mount_list, g_strdup (mountpoint));
}
}
g_mutex_unlock (&db->priv->stat_mutex);
}
}
g_assert ((entry->flags & RHYTHMDB_ENTRY_INSERTED) == 0);
entry->flags |= RHYTHMDB_ENTRY_INSERTED;
rhythmdb_entry_ref (entry);
db->priv->added_entries_to_emit = g_list_prepend (db->priv->added_entries_to_emit, entry);
return TRUE;
}
static gboolean
process_deleted_entries_cb (RhythmDBEntry *entry,
GThread *thread,
RhythmDB *db)
{
if (thread != g_thread_self ())
return FALSE;
rhythmdb_entry_ref (entry);
g_assert ((entry->flags & RHYTHMDB_ENTRY_INSERTED) != 0);
entry->flags &= ~(RHYTHMDB_ENTRY_INSERTED);
db->priv->deleted_entries_to_emit = g_list_prepend (db->priv->deleted_entries_to_emit, entry);
return TRUE;
}
static gboolean
process_changed_entries_cb (RhythmDBEntry *entry,
GSList *changes,
RhythmDB *db)
{
GSList *existing;
if (db->priv->changed_entries_to_emit == NULL) {
/* the value destroy function is just g_slist_free because we
* steal the actual change structures to build the value array.
*/
db->priv->changed_entries_to_emit = g_hash_table_new_full (NULL,
NULL,
(GDestroyNotify) rhythmdb_entry_unref,
(GDestroyNotify) g_slist_free);
}
/* if the entry is already in the change map from a previous commit, add the
* new changes to the end of the existing list.
*/
existing = g_hash_table_lookup (db->priv->changed_entries_to_emit, entry);
if (existing != NULL) {
changes = g_slist_concat (existing, changes);
/* steal the hash entry so it doesn't free the changes; also means we
* don't need to add a reference on the entry.
*/
g_hash_table_steal (db->priv->changed_entries_to_emit, entry);
} else {
rhythmdb_entry_ref (entry);
}
g_hash_table_insert (db->priv->changed_entries_to_emit, entry, changes);
return TRUE;
}
static void
sync_entry_changed (RhythmDBEntry *entry,
GSList *changes,
RhythmDB *db)
{
GSList *t;
for (t = changes; t; t = t->next) {
RBMetaDataField field;
RhythmDBEntryChange *change = t->data;
if (metadata_field_from_prop (change->prop, &field)) {
RhythmDBAction *action;
if (!rhythmdb_entry_can_sync_metadata (entry)) {
g_warning ("trying to sync properties of non-editable file");
break;
}
action = g_slice_new0 (RhythmDBAction);
action->type = RHYTHMDB_ACTION_SYNC;
action->uri = rb_refstring_ref (entry->location);
action->data.changes = copy_entry_changes (changes);
g_async_queue_push (db->priv->action_queue, action);
break;
}
}
}
static void
rhythmdb_commit_internal (RhythmDB *db,
gboolean sync_changes,
GThread *thread)
{
/*
* during normal operation, if committing from a worker thread,
* wait for changes made on the thread to be processed by the main thread.
* this avoids races and ensures the signals emitted are correct.
*/
if (db->priv->action_thread_running && !rb_is_main_thread ()) {
RhythmDBEvent *event;
event = g_slice_new0 (RhythmDBEvent);
event->db = db;
event->type = RHYTHMDB_EVENT_BARRIER;
g_mutex_lock (&db->priv->barrier_mutex);
rhythmdb_push_event (db, event);
while (g_list_find (db->priv->barriers_done, event) == NULL)
g_cond_wait (&db->priv->barrier_condition, &db->priv->barrier_mutex);
db->priv->barriers_done = g_list_remove (db->priv->barriers_done, event);
g_mutex_unlock (&db->priv->barrier_mutex);
rhythmdb_event_free (db, event);
}
g_mutex_lock (&db->priv->change_mutex);
if (sync_changes) {
g_hash_table_foreach (db->priv->changed_entries, (GHFunc) sync_entry_changed, db);
}
/* update the sets of entry changed/added/deleted signals to emit */
g_hash_table_foreach_remove (db->priv->changed_entries, (GHRFunc) process_changed_entries_cb, db);
g_hash_table_foreach_remove (db->priv->added_entries, (GHRFunc) process_added_entries_cb, db);
g_hash_table_foreach_remove (db->priv->deleted_entries, (GHRFunc) process_deleted_entries_cb, db);
/* if there are some signals to emit, add a new idle callback if required */
if (db->priv->added_entries_to_emit || db->priv->deleted_entries_to_emit || db->priv->changed_entries_to_emit) {
if (db->priv->emit_entry_signals_id == 0)
db->priv->emit_entry_signals_id = g_idle_add ((GSourceFunc) rhythmdb_emit_entry_signals_idle, db);
}
g_mutex_unlock (&db->priv->change_mutex);
}
typedef struct {
RhythmDB *db;
gboolean sync;
GThread *thread;
} RhythmDBTimeoutCommitData;
static gboolean
timeout_rhythmdb_commit (RhythmDBTimeoutCommitData *data)
{
rhythmdb_commit_internal (data->db, data->sync, data->thread);
g_object_unref (data->db);
g_free (data);
return FALSE;
}
static void
rhythmdb_add_timeout_commit (RhythmDB *db,
gboolean sync)
{
RhythmDBTimeoutCommitData *data;
g_assert (rb_is_main_thread ());
data = g_new0 (RhythmDBTimeoutCommitData, 1);
data->db = g_object_ref (db);
data->sync = sync;
data->thread = g_thread_self ();
g_timeout_add (100, (GSourceFunc)timeout_rhythmdb_commit, data);
}
/**
* rhythmdb_commit:
* @db: a #RhythmDB.
*
* Apply all database changes, and send notification of changes and new entries.
* This needs to be called after any changes have been made, such as a group of
* rhythmdb_entry_set() calls, or a new entry has been added.
*/
void
rhythmdb_commit (RhythmDB *db)
{
rhythmdb_commit_internal (db, TRUE, g_thread_self ());
}
/**
* rhythmdb_error_quark:
*
* Returns the #GQuark used for #RhythmDBError information
*
* Return value: error quark
*/
GQuark
rhythmdb_error_quark (void)
{
static GQuark quark;
if (!quark)
quark = g_quark_from_static_string ("rhythmdb_error");
return quark;
}
/* structure alignment magic, stolen from glib */
#define STRUCT_ALIGNMENT (2 * sizeof (gsize))
#define ALIGN_STRUCT(offset) \
((offset + (STRUCT_ALIGNMENT - 1)) & -STRUCT_ALIGNMENT)
/**
* rhythmdb_entry_allocate:
* @db: a #RhythmDB.
* @type: type of entry to allocate
*
* Allocate and initialise memory for a new #RhythmDBEntry of the type @type.
* The entry's initial properties needs to be set with rhythmdb_entry_set (),
* the entry added to the database with rhythmdb_entry_insert(), and committed with
* rhythmdb_commit().
*
* This should only be used by RhythmDB itself, or a backend (such as rhythmdb-tree).
*
* Returns: the newly allocated #RhythmDBEntry
*/
RhythmDBEntry *
rhythmdb_entry_allocate (RhythmDB *db,
RhythmDBEntryType *type)
{
RhythmDBEntry *ret;
guint type_data_size = 0;
gsize size = sizeof (RhythmDBEntry);
g_object_get (type, "type-data-size", &type_data_size, NULL);
if (type_data_size > 0) {
size = ALIGN_STRUCT (sizeof (RhythmDBEntry)) + type_data_size;
}
ret = g_malloc0 (size);
ret->id = (guint) g_atomic_int_add (&db->priv->next_entry_id, 1);
ret->type = type;
ret->title = rb_refstring_ref (db->priv->empty_string);
ret->genre = rb_refstring_ref (db->priv->empty_string);
ret->artist = rb_refstring_ref (db->priv->empty_string);
ret->composer = rb_refstring_ref (db->priv->empty_string);
ret->album = rb_refstring_ref (db->priv->empty_string);
ret->comment = rb_refstring_ref (db->priv->empty_string);
ret->album_artist = rb_refstring_ref (db->priv->empty_string);
ret->musicbrainz_trackid = rb_refstring_ref (db->priv->empty_string);
ret->musicbrainz_artistid = rb_refstring_ref (db->priv->empty_string);
ret->musicbrainz_albumid = rb_refstring_ref (db->priv->empty_string);
ret->musicbrainz_albumartistid = rb_refstring_ref (db->priv->empty_string);
ret->artist_sortname = rb_refstring_ref (db->priv->empty_string);
ret->composer_sortname = rb_refstring_ref (db->priv->empty_string);
ret->album_sortname = rb_refstring_ref (db->priv->empty_string);
ret->album_artist_sortname = rb_refstring_ref (db->priv->empty_string);
ret->media_type = rb_refstring_ref (db->priv->octet_stream_str);
ret->flags |= RHYTHMDB_ENTRY_LAST_PLAYED_DIRTY |
RHYTHMDB_ENTRY_FIRST_SEEN_DIRTY |
RHYTHMDB_ENTRY_LAST_SEEN_DIRTY;
/* The refcount is initially 0, we want to set it to 1 */
ret->refcount = 1;
rhythmdb_entry_created (ret);
return ret;
}
/**
* rhythmdb_entry_get_type_data:
* @entry: a #RhythmDBEntry
* @expected_size: expected size of the type-specific data.
*
* Retrieves a pointer to the entry's type-specific data, checking that
* the size of the data structure matches what is expected.
* Callers should use the RHYTHMDB_ENTRY_GET_TYPE_DATA macro for
* a slightly more friendly interface to this functionality.
*
* Return value: (transfer none): type-specific data pointer
*/
gpointer
rhythmdb_entry_get_type_data (RhythmDBEntry *entry,
guint expected_size)
{
g_return_val_if_fail (entry != NULL, NULL);
int type_data_size = 0;
gsize offset;
g_object_get (entry->type, "type-data-size", &type_data_size, NULL);
g_assert (expected_size == type_data_size);
offset = ALIGN_STRUCT (sizeof (RhythmDBEntry));
return (gpointer) (((guint8 *)entry) + offset);
}
/**
* rhythmdb_entry_insert:
* @db: a #RhythmDB.
* @entry: the entry to insert.
*
* Inserts a newly-created entry into the database.
*
* Note that you must call rhythmdb_commit() at some point after invoking
* this function.
*/
void
rhythmdb_entry_insert (RhythmDB *db,
RhythmDBEntry *entry)
{
g_return_if_fail (RHYTHMDB_IS (db));
g_return_if_fail (entry != NULL);
g_assert ((entry->flags & RHYTHMDB_ENTRY_INSERTED) == 0);
g_return_if_fail (entry->location != NULL);
/* ref the entry before adding to hash, it is unreffed when removed */
rhythmdb_entry_ref (entry);
g_mutex_lock (&db->priv->change_mutex);
g_hash_table_insert (db->priv->added_entries, entry, g_thread_self ());
g_mutex_unlock (&db->priv->change_mutex);
}
/**
* rhythmdb_entry_new:
* @db: a #RhythmDB.
* @type: type of entry to create
* @uri: the location of the entry, this be unique amongst all entries.
*
* Creates a new entry of type @type and location @uri, and inserts
* it into the database. You must call rhythmdb_commit() at some point
* after invoking this function.
*
* This may return NULL if entry creation fails. This can occur if there is
* already an entry with the given uri.
*
* Returns: (transfer none): the newly created #RhythmDBEntry
*/
RhythmDBEntry *
rhythmdb_entry_new (RhythmDB *db,
RhythmDBEntryType *type,
const char *uri)
{
RhythmDBEntry *ret;
RhythmDBClass *klass = RHYTHMDB_GET_CLASS (db);
ret = rhythmdb_entry_lookup_by_location (db, uri);
if (ret) {
g_warning ("attempting to create entry that already exists: %s", uri);
return NULL;
}
ret = rhythmdb_entry_allocate (db, type);
ret->location = rb_refstring_new (uri);
klass->impl_entry_new (db, ret);
rb_debug ("emitting entry added");
rhythmdb_entry_insert (db, ret);
return ret;
}
/**
* rhythmdb_entry_example_new:
* @db: a #RhythmDB.
* @type: type of entry to create
* @uri: the location of the entry, this be unique amongst all entries.
*
* Creates a new sample entry of type @type and location @uri, it does not insert
* it into the database. This is indended for use as a example entry.
*
* This may return NULL if entry creation fails.
*
* Returns: the newly created #RhythmDBEntry
*/
RhythmDBEntry *
rhythmdb_entry_example_new (RhythmDB *db,
RhythmDBEntryType *type,
const char *uri)
{
RhythmDBEntry *ret;
ret = rhythmdb_entry_allocate (db, type);
if (uri)
ret->location = rb_refstring_new (uri);
if (type == RHYTHMDB_ENTRY_TYPE_SONG) {
rb_refstring_unref (ret->artist);
/* Translators: this is an example artist name. It should
* not be translated literally, but could be replaced with
* a local artist name if desired. Ensure the album name
* and song title are also replaced in this case.
*/
ret->artist = rb_refstring_new (_("The Beatles"));
rb_refstring_unref (ret->album);
/* Translators: this is an example album name. If the
* example artist name is localised, this should be replaced
* with the name of an album by that artist.
*/
ret->album = rb_refstring_new (_("Help!"));
rb_refstring_unref (ret->title);
/* Translators: this is an example song title. If the example
* artist and album names are localised, this should be replaced
* with the name of the seventh song from the localised album.
*/
ret->title = rb_refstring_new (_("Ticket To Ride"));
ret->tracknum = 7;
} else {
}
return ret;
}
/**
* rhythmdb_entry_ref:
* @entry: a #RhythmDBEntry.
*
* Increase the reference count of the entry.
*
* Returns: the entry
*/
RhythmDBEntry *
rhythmdb_entry_ref (RhythmDBEntry *entry)
{
g_return_val_if_fail (entry != NULL, NULL);
g_return_val_if_fail (entry->refcount > 0, NULL);
g_atomic_int_inc (&entry->refcount);
return entry;
}
static void
rhythmdb_entry_finalize (RhythmDBEntry *entry)
{
rhythmdb_entry_pre_destroy (entry);
rb_refstring_unref (entry->location);
rb_refstring_unref (entry->playback_error);
rb_refstring_unref (entry->title);
rb_refstring_unref (entry->genre);
rb_refstring_unref (entry->artist);
rb_refstring_unref (entry->composer);
rb_refstring_unref (entry->album);
rb_refstring_unref (entry->comment);
rb_refstring_unref (entry->musicbrainz_trackid);
rb_refstring_unref (entry->musicbrainz_artistid);
rb_refstring_unref (entry->musicbrainz_albumid);
rb_refstring_unref (entry->musicbrainz_albumartistid);
rb_refstring_unref (entry->artist_sortname);
rb_refstring_unref (entry->composer_sortname);
rb_refstring_unref (entry->album_sortname);
rb_refstring_unref (entry->media_type);
g_free (entry);
}
/**
* rhythmdb_entry_unref:
* @entry: a #RhythmDBEntry.
*
* Decrease the reference count of the entry, and destroys it if there are
* no references left.
*/
void
rhythmdb_entry_unref (RhythmDBEntry *entry)
{
gboolean is_zero;
g_return_if_fail (entry != NULL);
g_return_if_fail (entry->refcount > 0);
is_zero = g_atomic_int_dec_and_test (&entry->refcount);
if (G_UNLIKELY (is_zero)) {
rhythmdb_entry_finalize (entry);
}
}
static void
set_metadata_string_with_default (RhythmDB *db,
RBMetaData *metadata,
RhythmDBEntry *entry,
RBMetaDataField field,
RhythmDBPropType prop,
const char *default_value)
{
GValue val = {0, };
if (!(rb_metadata_get (metadata,
field,
&val))) {
g_value_init (&val, G_TYPE_STRING);
g_value_set_static_string (&val, default_value);
} else {
const gchar *str = g_value_get_string (&val);
if (str == NULL || str[0] == '\0')
g_value_set_static_string (&val, default_value);
}
rhythmdb_entry_set_internal (db, entry, TRUE, prop, &val);
g_value_unset (&val);
}
static void
set_props_from_metadata (RhythmDB *db,
RhythmDBEntry *entry,
GFileInfo *fileinfo,
RBMetaData *metadata)
{
const char *media_type;
GValue val = {0,};
g_value_init (&val, G_TYPE_STRING);
media_type = rb_metadata_get_media_type (metadata);
if (media_type) {
g_value_set_string (&val, media_type);
rhythmdb_entry_set_internal (db, entry, TRUE,
RHYTHMDB_PROP_MEDIA_TYPE, &val);
}
g_value_unset (&val);
/* track number */
if (!rb_metadata_get (metadata,
RB_METADATA_FIELD_TRACK_NUMBER,
&val)) {
g_value_init (&val, G_TYPE_ULONG);
g_value_set_ulong (&val, 0);
}
rhythmdb_entry_set_internal (db, entry, TRUE,
RHYTHMDB_PROP_TRACK_NUMBER, &val);
g_value_unset (&val);
/* track total */
if (!rb_metadata_get (metadata,
RB_METADATA_FIELD_MAX_TRACK_NUMBER,
&val)) {
g_value_init (&val, G_TYPE_ULONG);
g_value_set_ulong (&val, 0);
}
rhythmdb_entry_set_internal (db, entry, TRUE,
RHYTHMDB_PROP_TRACK_TOTAL, &val);
g_value_unset (&val);
/* disc number */
if (!rb_metadata_get (metadata,
RB_METADATA_FIELD_DISC_NUMBER,
&val)) {
g_value_init (&val, G_TYPE_ULONG);
g_value_set_ulong (&val, 0);
}
rhythmdb_entry_set_internal (db, entry, TRUE,
RHYTHMDB_PROP_DISC_NUMBER, &val);
g_value_unset (&val);
/* disc total */
if (!rb_metadata_get (metadata,
RB_METADATA_FIELD_MAX_DISC_NUMBER,
&val)) {
g_value_init (&val, G_TYPE_ULONG);
g_value_set_ulong (&val, 0);
}
rhythmdb_entry_set_internal (db, entry, TRUE,
RHYTHMDB_PROP_DISC_TOTAL, &val);
g_value_unset (&val);
/* duration */
if (rb_metadata_get (metadata,
RB_METADATA_FIELD_DURATION,
&val)) {
rhythmdb_entry_set_internal (db, entry, TRUE,
RHYTHMDB_PROP_DURATION, &val);
g_value_unset (&val);
}
/* bitrate (only set for non-lossless media types) */
if (!rb_gst_media_type_is_lossless (media_type)) {
if (rb_metadata_get (metadata,
RB_METADATA_FIELD_BITRATE,
&val)) {
rhythmdb_entry_set_internal (db, entry, TRUE,
RHYTHMDB_PROP_BITRATE, &val);
g_value_unset (&val);
}
}
/* date */
if (rb_metadata_get (metadata,
RB_METADATA_FIELD_DATE,
&val)) {
rhythmdb_entry_set_internal (db, entry, TRUE,
RHYTHMDB_PROP_DATE, &val);
g_value_unset (&val);
}
/* musicbrainz trackid */
set_metadata_string_with_default (db, metadata, entry,
RB_METADATA_FIELD_MUSICBRAINZ_TRACKID,
RHYTHMDB_PROP_MUSICBRAINZ_TRACKID,
"");
/* musicbrainz artistid */
set_metadata_string_with_default (db, metadata, entry,
RB_METADATA_FIELD_MUSICBRAINZ_ARTISTID,
RHYTHMDB_PROP_MUSICBRAINZ_ARTISTID,
"");
/* musicbrainz albumid */
set_metadata_string_with_default (db, metadata, entry,
RB_METADATA_FIELD_MUSICBRAINZ_ALBUMID,
RHYTHMDB_PROP_MUSICBRAINZ_ALBUMID,
"");
/* musicbrainz albumartistid */
set_metadata_string_with_default (db, metadata, entry,
RB_METADATA_FIELD_MUSICBRAINZ_ALBUMARTISTID,
RHYTHMDB_PROP_MUSICBRAINZ_ALBUMARTISTID,
"");
/* filesize */
g_value_init (&val, G_TYPE_UINT64);
g_value_set_uint64 (&val, g_file_info_get_attribute_uint64 (fileinfo, G_FILE_ATTRIBUTE_STANDARD_SIZE));
rhythmdb_entry_set_internal (db, entry, TRUE, RHYTHMDB_PROP_FILE_SIZE, &val);
g_value_unset (&val);
/* title */
if (!rb_metadata_get (metadata,
RB_METADATA_FIELD_TITLE,
&val) || g_value_get_string (&val)[0] == '\0') {
const char *fname;
fname = g_file_info_get_display_name (fileinfo);
if (G_VALUE_HOLDS_STRING (&val))
g_value_reset (&val);
else
g_value_init (&val, G_TYPE_STRING);
g_value_set_string (&val, fname);
}
rhythmdb_entry_set_internal (db, entry, TRUE, RHYTHMDB_PROP_TITLE, &val);
g_value_unset (&val);
/* genre */
set_metadata_string_with_default (db, metadata, entry,
RB_METADATA_FIELD_GENRE,
RHYTHMDB_PROP_GENRE,
_("Unknown"));
/* artist */
set_metadata_string_with_default (db, metadata, entry,
RB_METADATA_FIELD_ARTIST,
RHYTHMDB_PROP_ARTIST,
_("Unknown"));
/* beats per minute */
if (rb_metadata_get (metadata,
RB_METADATA_FIELD_BPM,
&val)) {
rhythmdb_entry_set_internal (db, entry, TRUE,
RHYTHMDB_PROP_BPM, &val);
g_value_unset (&val);
}
/* album */
set_metadata_string_with_default (db, metadata, entry,
RB_METADATA_FIELD_ALBUM,
RHYTHMDB_PROP_ALBUM,
_("Unknown"));
/* artist sortname */
set_metadata_string_with_default (db, metadata, entry,
RB_METADATA_FIELD_ARTIST_SORTNAME,
RHYTHMDB_PROP_ARTIST_SORTNAME,
"");
/* album sortname */
set_metadata_string_with_default (db, metadata, entry,
RB_METADATA_FIELD_ALBUM_SORTNAME,
RHYTHMDB_PROP_ALBUM_SORTNAME,
"");
/* comment */
set_metadata_string_with_default (db, metadata, entry,
RB_METADATA_FIELD_COMMENT,
RHYTHMDB_PROP_COMMENT,
"");
/* album artist */
set_metadata_string_with_default (db, metadata, entry,
RB_METADATA_FIELD_ALBUM_ARTIST,
RHYTHMDB_PROP_ALBUM_ARTIST,
"");
/* album artist sortname */
set_metadata_string_with_default (db, metadata, entry,
RB_METADATA_FIELD_ALBUM_ARTIST_SORTNAME,
RHYTHMDB_PROP_ALBUM_ARTIST_SORTNAME,
"");
/* composer */
set_metadata_string_with_default (db, metadata, entry,
RB_METADATA_FIELD_COMPOSER,
RHYTHMDB_PROP_COMPOSER,
_("Unknown"));
/* composer sortname */
set_metadata_string_with_default (db, metadata, entry,
RB_METADATA_FIELD_COMPOSER_SORTNAME,
RHYTHMDB_PROP_COMPOSER_SORTNAME,
"");
}
static void
rhythmdb_process_stat_event (RhythmDB *db,
RhythmDBEvent *event)
{
RhythmDBEntry *entry;
RhythmDBAction *action;
GFileType file_type;
if (event->entry != NULL) {
entry = event->entry;
} else {
entry = rhythmdb_entry_lookup_by_location_refstring (db, event->real_uri);
}
/* handle errors:
* - if a non-ignore entry exists, process ghostliness (hide/delete)
* - otherwise, create an import error entry? hmm.
*/
if (event->error) {
if (entry != NULL) {
rb_debug ("error accessing %s: %s",
rb_refstring_get (event->real_uri),
event->error->message);
rhythmdb_entry_update_availability (entry, RHYTHMDB_ENTRY_AVAIL_NOT_FOUND);
rhythmdb_commit (db);
}
return;
}
g_assert (event->file_info != NULL);
/* figure out what to do based on the file type */
file_type = g_file_info_get_attribute_uint32 (event->file_info,
G_FILE_ATTRIBUTE_STANDARD_TYPE);
switch (file_type) {
case G_FILE_TYPE_UNKNOWN:
case G_FILE_TYPE_REGULAR:
if (entry != NULL) {
guint64 new_mtime;
guint64 new_size;
/* update the existing entry, as long as the entry type matches */
if ((event->entry_type != NULL) &&
(entry->type != event->entry_type) &&
(entry->type != event->ignore_type) &&
(entry->type != event->error_type)) {
if (event->entry_type == RHYTHMDB_ENTRY_TYPE_SONG &&
entry->type == RHYTHMDB_ENTRY_TYPE_PODCAST_POST) {
rb_debug ("Ignoring stat event for '%s', it's already loaded as a podcast",
rb_refstring_get (event->real_uri));
break;
}
g_warning ("attempt to use location %s in multiple entry types (%s and %s)",
rb_refstring_get (event->real_uri),
rhythmdb_entry_type_get_name (event->entry_type),
rhythmdb_entry_type_get_name (entry->type));
}
if (entry->type == event->ignore_type)
rb_debug ("ignoring %p", entry);
rhythmdb_entry_update_availability (entry, RHYTHMDB_ENTRY_AVAIL_CHECKED);
/* compare modification time and size to the values in the database.
* if either has changed, we'll re-read the file.
*/
new_mtime = g_file_info_get_attribute_uint64 (event->file_info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
new_size = g_file_info_get_attribute_uint64 (event->file_info, G_FILE_ATTRIBUTE_STANDARD_SIZE);
if (entry->mtime == new_mtime && (new_size == 0 || entry->file_size == new_size)) {
rb_debug ("not modified: %s", rb_refstring_get (event->real_uri));
} else {
rb_debug ("changed: %s", rb_refstring_get (event->real_uri));
action = g_slice_new0 (RhythmDBAction);
action->type = RHYTHMDB_ACTION_LOAD;
action->uri = rb_refstring_ref (event->real_uri);
action->data.types.entry_type = event->entry_type;
action->data.types.ignore_type = event->ignore_type;
action->data.types.error_type = event->error_type;
g_async_queue_push (db->priv->action_queue, action);
}
} else {
/* push a LOAD action */
action = g_slice_new0 (RhythmDBAction);
action->type = RHYTHMDB_ACTION_LOAD;
action->uri = rb_refstring_ref (event->real_uri);
action->data.types.entry_type = event->entry_type;
action->data.types.ignore_type = event->ignore_type;
action->data.types.error_type = event->error_type;
rb_debug ("queuing a RHYTHMDB_ACTION_LOAD: %s", rb_refstring_get (action->uri));
g_async_queue_push (db->priv->action_queue, action);
}
break;
case G_FILE_TYPE_DIRECTORY:
rb_debug ("processing directory %s", rb_refstring_get (event->real_uri));
/* push an ENUM_DIR action */
action = g_slice_new0 (RhythmDBAction);
action->type = RHYTHMDB_ACTION_ENUM_DIR;
action->uri = rb_refstring_ref (event->real_uri);
action->data.types.entry_type = event->entry_type;
action->data.types.ignore_type = event->ignore_type;
action->data.types.error_type = event->error_type;
rb_debug ("queuing a RHYTHMDB_ACTION_ENUM_DIR: %s", rb_refstring_get (action->uri));
g_async_queue_push (db->priv->action_queue, action);
break;
case G_FILE_TYPE_SYMBOLIC_LINK:
case G_FILE_TYPE_SHORTCUT:
/* this shouldn't happen, but maybe we should handle it anyway? */
rb_debug ("ignoring stat results for %s: is link", rb_refstring_get (event->real_uri));
break;
case G_FILE_TYPE_SPECIAL:
case G_FILE_TYPE_MOUNTABLE: /* hmm. */
rb_debug ("ignoring stat results for %s: is special", rb_refstring_get (event->real_uri));
rhythmdb_add_import_error_entry (db, event, event->ignore_type);
break;
}
rhythmdb_commit (db);
}
static RhythmDBEntry *
create_blank_entry (RhythmDB *db, RhythmDBEvent *event)
{
RhythmDBEntry *entry;
GTimeVal time;
GValue value = {0,};
entry = rhythmdb_entry_new (db, event->entry_type, rb_refstring_get (event->real_uri));
if (entry == NULL) {
rb_debug ("entry already exists");
return NULL;
}
/* initialize the last played date to 0=never */
g_value_init (&value, G_TYPE_ULONG);
g_value_set_ulong (&value, 0);
rhythmdb_entry_set (db, entry,
RHYTHMDB_PROP_LAST_PLAYED, &value);
g_value_unset (&value);
/* initialize the rating */
g_value_init (&value, G_TYPE_DOUBLE);
g_value_set_double (&value, 0);
rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_RATING, &value);
g_value_unset (&value);
/* first seen */
g_get_current_time (&time);
g_value_init (&value, G_TYPE_ULONG);
g_value_set_ulong (&value, time.tv_sec);
rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_FIRST_SEEN, &value);
g_value_unset (&value);
return entry;
}
static void
apply_mtime (RhythmDB *db, RhythmDBEntry *entry, GFileInfo *file_info)
{
guint64 mtime;
GValue value = {0,};
if (file_info == NULL) {
return;
}
mtime = g_file_info_get_attribute_uint64 (file_info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
g_value_init (&value, G_TYPE_ULONG);
g_value_set_ulong (&value, (gulong)mtime);
rhythmdb_entry_set_internal (db, entry, TRUE, RHYTHMDB_PROP_MTIME, &value);
g_value_unset (&value);
}
static RhythmDBEntry *
rhythmdb_add_import_error_entry (RhythmDB *db,
RhythmDBEvent *event,
RhythmDBEntryType *error_entry_type)
{
RhythmDBEntry *entry;
GValue value = {0,};
if (error_entry_type == NULL) {
/* we don't have an error entry type, so we can't add an import error */
return NULL;
}
rb_debug ("adding import error type %s for %s: %s",
rhythmdb_entry_type_get_name (error_entry_type),
rb_refstring_get (event->real_uri),
event->error ? event->error->message : "<no error>");
entry = rhythmdb_entry_lookup_by_location_refstring (db, event->real_uri);
if (entry) {
RhythmDBEntryType *entry_type = rhythmdb_entry_get_entry_type (entry);
if (entry_type != event->error_type &&
entry_type != event->ignore_type) {
/* FIXME we've successfully read this file before.. so what should we do? */
rb_debug ("%s already exists in the library.. ignoring import error?", rb_refstring_get (event->real_uri));
return NULL;
}
if (entry_type != error_entry_type) {
/* delete the existing entry, then create a new one below */
rhythmdb_entry_delete (db, entry);
entry = NULL;
} else if (error_entry_type == event->error_type && event->error) {
/* we've already got an error for this file, so just update it */
g_value_init (&value, G_TYPE_STRING);
g_value_set_string (&value, event->error->message);
rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_PLAYBACK_ERROR, &value);
g_value_unset (&value);
} else {
/* no need to update the ignored file entry */
}
if (entry) {
apply_mtime (db, entry, event->file_info);
}
rhythmdb_add_timeout_commit (db, FALSE);
}
if (entry == NULL) {
/* create a new import error or ignore entry */
entry = rhythmdb_entry_new (db, error_entry_type, rb_refstring_get (event->real_uri));
if (entry == NULL)
return NULL;
/* if we have missing plugin details, store them in the
* comment field so we can collect them later, and set a
* suitable error message
*/
if (event->metadata != NULL && rb_metadata_has_missing_plugins (event->metadata)) {
char **missing_plugins;
char **plugin_descriptions;
char *comment;
char *list;
const char *msg;
/* Translators: the parameter here is a list of GStreamer plugins.
* The plugin names are already translated.
*/
msg = _("Additional GStreamer plugins are required to play this file: %s");
if (rb_metadata_has_audio (event->metadata) == TRUE &&
rb_metadata_has_video (event->metadata) == FALSE &&
rb_metadata_has_missing_plugins (event->metadata) == TRUE) {
rb_metadata_get_missing_plugins (event->metadata, &missing_plugins, &plugin_descriptions);
comment = g_strjoinv ("\n", missing_plugins);
rb_debug ("storing missing plugin details: %s", comment);
g_value_init (&value, G_TYPE_STRING);
g_value_take_string (&value, comment);
rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_COMMENT, &value);
g_value_unset (&value);
g_value_init (&value, G_TYPE_STRING);
list = g_strjoinv (", ", plugin_descriptions);
g_value_take_string (&value, g_strdup_printf (msg, list));
g_free (list);
rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_PLAYBACK_ERROR, &value);
g_value_unset (&value);
g_strfreev (missing_plugins);
g_strfreev (plugin_descriptions);
} else if (rb_metadata_has_missing_plugins (event->metadata)) {
rb_debug ("ignoring missing plugins for non-audio file");
}
} else if (error_entry_type == event->error_type && event->error && event->error->message) {
g_value_init (&value, G_TYPE_STRING);
if (g_utf8_validate (event->error->message, -1, NULL))
g_value_set_string (&value, event->error->message);
else
g_value_set_static_string (&value, _("invalid unicode in error message"));
rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_PLAYBACK_ERROR, &value);
g_value_unset (&value);
}
/* mtime */
if (event->file_info) {
guint64 new_mtime = g_file_info_get_attribute_uint64 (event->file_info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
g_value_init (&value, G_TYPE_ULONG);
g_value_set_ulong (&value, new_mtime); /* hmm, cast */
rhythmdb_entry_set(db, entry, RHYTHMDB_PROP_MTIME, &value);
g_value_unset (&value);
}
/* record the mount point so we can delete entries for unmounted volumes */
rhythmdb_entry_set_mount_point (db, entry, rb_refstring_get (event->real_uri));
rhythmdb_entry_set_visibility (db, entry, TRUE);
rhythmdb_add_timeout_commit (db, FALSE);
}
return entry;
}
static gboolean
rhythmdb_process_metadata_cache (RhythmDB *db, RhythmDBEvent *event)
{
RhythmDBEntry *entry;
RhythmDBEntryChange *fields;
gboolean monitor;
int i;
fields = (RhythmDBEntryChange *)event->cached_metadata.data;
for (i = 0; i < event->cached_metadata.len; i++) {
if (fields[i].prop == RHYTHMDB_PROP_MEDIA_TYPE) {
const char *media_type;
media_type = g_value_get_string (&fields[i].new);
/* if no media type is set, it's an ignore entry */
if (g_strcmp0 (media_type, "application/octet-stream") == 0) {
rhythmdb_add_import_error_entry (db, event, event->ignore_type);
return TRUE;
}
break;
}
}
entry = rhythmdb_entry_lookup_by_location_refstring (db, event->real_uri);
if (entry == NULL) {
entry = create_blank_entry (db, event);
if (entry == NULL) {
return TRUE;
}
}
apply_mtime (db, entry, event->file_info);
rhythmdb_entry_apply_cached_metadata (entry, &event->cached_metadata);
rhythmdb_entry_update_availability (entry, RHYTHMDB_ENTRY_AVAIL_CHECKED);
/* Remember the mount point of the volume the song is on */
rhythmdb_entry_set_mount_point (db, entry, rb_refstring_get (event->real_uri));
/* monitor the file for changes */
/* FIXME: watch for errors */
monitor = g_settings_get_boolean (db->priv->settings, "monitor-library");
if (monitor && event->entry_type == RHYTHMDB_ENTRY_TYPE_SONG)
rhythmdb_monitor_uri_path (db, rb_refstring_get (entry->location), NULL);
rhythmdb_commit_internal (db, FALSE, g_thread_self ());
return TRUE;
}
static gboolean
rhythmdb_process_metadata_load (RhythmDB *db, RhythmDBEvent *event)
{
RhythmDBEntry *entry;
GTimeVal time;
gboolean monitor;
entry = NULL;
if (event->entry_type == NULL)
event->entry_type = RHYTHMDB_ENTRY_TYPE_SONG;
if (event->metadata != NULL) {
/* always ignore anything with video in it */
if (rb_metadata_has_video (event->metadata)) {
entry = rhythmdb_add_import_error_entry (db, event, event->ignore_type);
}
/* if we identified the media type, we can ignore anything
* that matches one of the media types we don't care about,
* as well as anything that doesn't contain audio.
*/
const char *media_type = rb_metadata_get_media_type (event->metadata);
if (entry == NULL && media_type != NULL && media_type[0] != '\0') {
if (rhythmdb_ignore_media_type (media_type) ||
rb_metadata_has_audio (event->metadata) == FALSE) {
entry = rhythmdb_add_import_error_entry (db, event, event->ignore_type);
}
}
if (entry != NULL) {
rhythmdb_entry_cache_metadata (entry);
return TRUE;
}
}
/* also ignore really small files we can't identify */
if (event->error && event->error->code == RB_METADATA_ERROR_UNRECOGNIZED) {
guint64 file_size;
file_size = g_file_info_get_attribute_uint64 (event->file_info,
G_FILE_ATTRIBUTE_STANDARD_SIZE);
if (file_size == 0) {
/* except for empty files */
g_clear_error (&event->error);
g_set_error (&event->error,
RB_METADATA_ERROR,
RB_METADATA_ERROR_EMPTY_FILE,
_("Empty file"));
} else if (file_size < REALLY_SMALL_FILE_SIZE) {
entry = rhythmdb_add_import_error_entry (db, event, event->ignore_type);
if (entry != NULL) {
rhythmdb_entry_cache_metadata (entry);
}
return TRUE;
}
}
if (event->error) {
rhythmdb_add_import_error_entry (db, event, event->error_type);
return TRUE;
}
g_get_current_time (&time);
entry = rhythmdb_entry_lookup_by_location_refstring (db, event->real_uri);
if (entry != NULL) {
RhythmDBEntryType *etype;
etype = rhythmdb_entry_get_entry_type (entry);
if (etype == event->error_type || etype == event->ignore_type) {
/* switching from IGNORE/ERROR to SONG, recreate the entry */
rhythmdb_entry_delete (db, entry);
rhythmdb_add_timeout_commit (db, FALSE);
entry = NULL;
}
}
if (entry == NULL) {
entry = create_blank_entry (db, event);
if (entry == NULL) {
rb_debug ("entry already exists");
return TRUE;
}
}
if ((event->entry_type != NULL) && (entry->type != event->entry_type)) {
g_warning ("attempt to use same location in multiple entry types");
return TRUE;
}
apply_mtime (db, entry, event->file_info);
if (event->entry_type != event->ignore_type &&
event->entry_type != event->error_type) {
set_props_from_metadata (db, entry, event->file_info, event->metadata);
}
rhythmdb_entry_cache_metadata (entry);
rhythmdb_entry_update_availability (entry, RHYTHMDB_ENTRY_AVAIL_CHECKED);
/* Remember the mount point of the volume the song is on */
rhythmdb_entry_set_mount_point (db, entry, rb_refstring_get (event->real_uri));
/* monitor the file for changes */
/* FIXME: watch for errors */
monitor = g_settings_get_boolean (db->priv->settings, "monitor-library");
if (monitor && event->entry_type == RHYTHMDB_ENTRY_TYPE_SONG)
rhythmdb_monitor_uri_path (db, rb_refstring_get (entry->location), NULL);
rhythmdb_commit_internal (db, FALSE, g_thread_self ());
return TRUE;
}
static void
rhythmdb_process_queued_entry_set_event (RhythmDB *db,
RhythmDBEvent *event)
{
rhythmdb_entry_set_internal (db, event->entry,
event->signal_change,
event->change.prop,
&event->change.new);
/* Don't run rhythmdb_commit right now in case there
* we can run a single commit for several queued
* entry_set
*/
rhythmdb_add_timeout_commit (db, TRUE);
}
static void
rhythmdb_process_one_event (RhythmDBEvent *event, RhythmDB *db)
{
gboolean free = TRUE;
/* if the database is read-only, we can't process those events
* since they call rhythmdb_entry_set. Doing it this way
* is safe if we assume all calls to read_enter/read_leave
* are done from the main thread (the thread this function
* runs in).
*/
if (rhythmdb_get_readonly (db) &&
((event->type == RHYTHMDB_EVENT_STAT)
|| (event->type == RHYTHMDB_EVENT_METADATA_LOAD)
|| (event->type == RHYTHMDB_EVENT_METADATA_CACHE)
|| (event->type == RHYTHMDB_EVENT_ENTRY_SET)
|| (event->type == RHYTHMDB_EVENT_BARRIER))) {
rb_debug ("Database is read-only, delaying event processing");
g_async_queue_push (db->priv->delayed_write_queue, event);
return;
}
switch (event->type) {
case RHYTHMDB_EVENT_STAT:
rb_debug ("processing RHYTHMDB_EVENT_STAT");
rhythmdb_process_stat_event (db, event);
break;
case RHYTHMDB_EVENT_METADATA_LOAD:
rb_debug ("processing RHYTHMDB_EVENT_METADATA_LOAD");
free = rhythmdb_process_metadata_load (db, event);
break;
case RHYTHMDB_EVENT_METADATA_CACHE:
rb_debug ("processing RHTHMDB_EVENT_METADATA_CACHE");
free = rhythmdb_process_metadata_cache (db, event);
break;
case RHYTHMDB_EVENT_ENTRY_SET:
rb_debug ("processing RHYTHMDB_EVENT_ENTRY_SET");
rhythmdb_process_queued_entry_set_event (db, event);
break;
case RHYTHMDB_EVENT_DB_LOAD:
rb_debug ("processing RHYTHMDB_EVENT_DB_LOAD");
g_signal_emit (G_OBJECT (db), rhythmdb_signals[LOAD_COMPLETE], 0);
/* save the db every five minutes */
if (db->priv->save_timeout_id > 0) {
g_source_remove (db->priv->save_timeout_id);
}
db->priv->save_timeout_id = g_timeout_add_seconds_full (G_PRIORITY_LOW,
5 * 60,
(GSourceFunc) rhythmdb_idle_save,
db,
NULL);
break;
case RHYTHMDB_EVENT_THREAD_EXITED:
rb_debug ("processing RHYTHMDB_EVENT_THREAD_EXITED");
break;
case RHYTHMDB_EVENT_DB_SAVED:
rb_debug ("processing RHYTHMDB_EVENT_DB_SAVED");
rhythmdb_read_leave (db);
break;
case RHYTHMDB_EVENT_QUERY_COMPLETE:
rb_debug ("processing RHYTHMDB_EVENT_QUERY_COMPLETE");
rhythmdb_read_leave (db);
break;
case RHYTHMDB_EVENT_BARRIER:
rb_debug ("processing RHYTHMDB_EVENT_BARRIER");
g_mutex_lock (&db->priv->barrier_mutex);
db->priv->barriers_done = g_list_prepend (db->priv->barriers_done, event);
g_cond_broadcast (&db->priv->barrier_condition);
g_mutex_unlock (&db->priv->barrier_mutex);
/* freed by the thread waiting on the barrier */
free = FALSE;
break;
}
if (free)
rhythmdb_event_free (db, event);
}
static void
rhythmdb_file_info_query (RhythmDB *db, GFile *file, RhythmDBEvent *event)
{
event->file_info = g_file_query_info (file,
RHYTHMDB_FILE_INFO_ATTRIBUTES,
G_FILE_QUERY_INFO_NONE,
db->priv->exiting,
&event->error);
}
static void
wrap_access_failed_error (RhythmDBEvent *event)
{
GError *wrapped;
wrapped = make_access_failed_error (rb_refstring_get (event->real_uri), event->error);
g_error_free (event->error);
event->error = wrapped;
}
static void
rhythmdb_execute_stat_mount_ready_cb (GObject *source, GAsyncResult *result, RhythmDBEvent *event)
{
GError *error = NULL;
g_file_mount_enclosing_volume_finish (G_FILE (source), result, &error);
if (error != NULL) {
event->error = make_access_failed_error (rb_refstring_get (event->real_uri), error);
g_error_free (error);
g_object_unref (event->file_info);
event->file_info = NULL;
} else {
rhythmdb_file_info_query (event->db, G_FILE (source), event);
}
g_mutex_lock (&event->db->priv->stat_mutex);
event->db->priv->outstanding_stats = g_list_remove (event->db->priv->outstanding_stats, event);
g_mutex_unlock (&event->db->priv->stat_mutex);
g_object_unref (source);
rhythmdb_push_event (event->db, event);
}
static void
rhythmdb_execute_stat (RhythmDB *db,
const char *uri,
RhythmDBEvent *event)
{
GFile *file;
event->real_uri = rb_refstring_new (uri);
file = g_file_new_for_uri (uri);
g_mutex_lock (&db->priv->stat_mutex);
db->priv->outstanding_stats = g_list_prepend (db->priv->outstanding_stats, event);
g_mutex_unlock (&db->priv->stat_mutex);
rhythmdb_file_info_query (db, file, event);
if (event->error != NULL) {
/* if we can't get at it because the location isn't mounted, mount it and try again */
if (g_error_matches (event->error, G_IO_ERROR, G_IO_ERROR_NOT_MOUNTED)) {
GMountOperation *mount_op = NULL;
g_error_free (event->error);
event->error = NULL;
g_signal_emit (G_OBJECT (event->db), rhythmdb_signals[CREATE_MOUNT_OP], 0, &mount_op);
if (mount_op != NULL) {
g_file_mount_enclosing_volume (file,
G_MOUNT_MOUNT_NONE,
mount_op,
event->db->priv->exiting,
(GAsyncReadyCallback)rhythmdb_execute_stat_mount_ready_cb,
event);
return;
}
}
/* if it's some other error, or we couldn't attempt to mount the location, report the error */
wrap_access_failed_error (event);
if (event->file_info != NULL) {
g_object_unref (event->file_info);
event->file_info = NULL;
}
}
/* either way, we're done now */
g_mutex_lock (&event->db->priv->stat_mutex);
event->db->priv->outstanding_stats = g_list_remove (event->db->priv->outstanding_stats, event);
g_mutex_unlock (&event->db->priv->stat_mutex);
rhythmdb_push_event (event->db, event);
g_object_unref (file);
}
static void
rhythmdb_execute_load (RhythmDB *db,
const char *uri,
RhythmDBEvent *event)
{
GError *error = NULL;
char *resolved;
resolved = rb_uri_resolve_symlink (uri, &error);
if (resolved != NULL) {
GFile *file;
file = g_file_new_for_uri (uri);
event->file_info = g_file_query_info (file,
RHYTHMDB_FILE_INFO_ATTRIBUTES,
G_FILE_QUERY_INFO_NONE,
NULL,
&error);
event->real_uri = rb_refstring_new (resolved);
g_free (resolved);
g_object_unref (file);
} else {
event->real_uri = rb_refstring_new (uri);
}
if (error != NULL) {
event->error = make_access_failed_error (uri, error);
if (event->file_info) {
g_object_unref (event->file_info);
event->file_info = NULL;
}
} else {
gboolean valid;
valid = FALSE;
if (rhythmdb_entry_type_fetch_metadata (event->entry_type, uri, &event->cached_metadata)) {
RhythmDBEntryChange *fields = (RhythmDBEntryChange *)event->cached_metadata.data;
guint64 new_filesize;
guint64 new_mtime;
int i;
valid = TRUE;
new_filesize = g_file_info_get_attribute_uint64 (event->file_info,
G_FILE_ATTRIBUTE_STANDARD_SIZE);
new_mtime = g_file_info_get_attribute_uint64 (event->file_info,
G_FILE_ATTRIBUTE_TIME_MODIFIED);
for (i = 0; i < event->cached_metadata.len; i++) {
switch (fields[i].prop) {
case RHYTHMDB_PROP_MTIME:
if (new_mtime != g_value_get_ulong (&fields[i].new)) {
rb_debug ("mtime mismatch, ignoring cached metadata");
valid = FALSE;
}
break;
case RHYTHMDB_PROP_FILE_SIZE:
if (new_filesize != g_value_get_uint64 (&fields[i].new)) {
rb_debug ("size mismatch, ignoring cached metadata");
valid = FALSE;
}
break;
default:
break;
}
}
if (valid) {
event->type = RHYTHMDB_EVENT_METADATA_CACHE;
rb_debug ("got valid cached metadata");
} else {
free_cached_metadata(&event->cached_metadata);
}
}
if (valid == FALSE) {
event->metadata = rb_metadata_new ();
rb_metadata_load (event->metadata,
rb_refstring_get (event->real_uri),
&event->error);
}
}
rhythmdb_push_event (db, event);
}
static void
rhythmdb_execute_enum_dir (RhythmDB *db,
RhythmDBAction *action)
{
GFile *dir;
GFileEnumerator *dir_enum;
GError *error = NULL;
dir = g_file_new_for_uri (rb_refstring_get (action->uri));
dir_enum = g_file_enumerate_children (dir,
RHYTHMDB_FILE_CHILD_INFO_ATTRIBUTES,
G_FILE_QUERY_INFO_NONE,
db->priv->exiting,
&error);
if (error != NULL) {
/* don't need to worry about mounting here, as the mount should have
* occurred on the stat.
*/
/* um.. what now? */
rb_debug ("unable to enumerate children of %s: %s",
rb_refstring_get (action->uri),
error->message);
g_error_free (error);
g_object_unref (dir);
return;
}
while (1) {
RhythmDBEvent *result;
GFileInfo *file_info;
GFile *child;
char *child_uri;
file_info = g_file_enumerator_next_file (dir_enum, db->priv->exiting, &error);
if (file_info == NULL) {
if (error == NULL) {
/* done */
break;
}
g_warning ("error getting next file: %s", error->message);
g_clear_error (&error);
continue;
}
if (g_file_info_get_attribute_boolean (file_info, G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN)) {
rb_debug ("ignoring hidden file %s", g_file_info_get_name (file_info));
g_object_unref (file_info);
continue;
}
child = g_file_get_child (dir, g_file_info_get_name (file_info));
child_uri = g_file_get_uri (child);
result = g_slice_new0 (RhythmDBEvent);
result->db = db;
result->type = RHYTHMDB_EVENT_STAT;
result->entry_type = action->data.types.entry_type;
result->error_type = action->data.types.error_type;
result->ignore_type = action->data.types.ignore_type;
result->real_uri = rb_refstring_new (child_uri);
result->file_info = file_info;
result->error = error;
rhythmdb_push_event (db, result);
g_free (child_uri);
}
g_file_enumerator_close (dir_enum, db->priv->exiting, &error);
if (error != NULL) {
/* hmm.. */
rb_debug ("error closing file enumerator: %s", error->message);
g_error_free (error);
}
g_object_unref (dir);
g_object_unref (dir_enum);
}
/**
* rhythmdb_entry_get:
* @db: the #RhythmDB
* @entry: a #RhythmDBEntry.
* @propid: the id of the property to get.
* @val: return location for the property value.
*
* Gets a property of an entry, storing it in the given #GValue.
*/
void
rhythmdb_entry_get (RhythmDB *db,
RhythmDBEntry *entry,
RhythmDBPropType propid,
GValue *val)
{
g_return_if_fail (RHYTHMDB_IS (db));
g_return_if_fail (entry != NULL);
g_return_if_fail (entry->refcount > 0);
rhythmdb_entry_sync_mirrored (entry, propid);
g_assert (G_VALUE_TYPE (val) == rhythmdb_get_property_type (db, propid));
switch (rhythmdb_properties[propid].prop_type) {
case G_TYPE_STRING:
g_value_set_string (val, rhythmdb_entry_get_string (entry, propid));
break;
case G_TYPE_BOOLEAN:
g_value_set_boolean (val, rhythmdb_entry_get_boolean (entry, propid));
break;
case G_TYPE_ULONG:
g_value_set_ulong (val, rhythmdb_entry_get_ulong (entry, propid));
break;
case G_TYPE_UINT64:
g_value_set_uint64 (val, rhythmdb_entry_get_uint64 (entry, propid));
break;
case G_TYPE_DOUBLE:
g_value_set_double (val, rhythmdb_entry_get_double (entry, propid));
break;
case G_TYPE_OBJECT:
g_value_set_object (val, rhythmdb_entry_get_object (entry, propid));
break;
default:
g_assert_not_reached ();
break;
}
}
typedef struct
{
RhythmDB *db;
char *uri;
GError *error;
} RhythmDBSaveErrorData;
static gboolean
emit_save_error_idle (RhythmDBSaveErrorData *data)
{
g_signal_emit (G_OBJECT (data->db), rhythmdb_signals[SAVE_ERROR], 0, data->uri, data->error);
g_object_unref (G_OBJECT (data->db));
g_free (data->uri);
g_error_free (data->error);
g_free (data);
return FALSE;
}
static gpointer
action_thread_main (RhythmDB *db)
{
RhythmDBEvent *result;
while (!g_cancellable_is_cancelled (db->priv->exiting)) {
RhythmDBAction *action;
action = g_async_queue_pop (db->priv->action_queue);
/* hrm, do we need this check at all? */
if (!g_cancellable_is_cancelled (db->priv->exiting)) {
switch (action->type) {
case RHYTHMDB_ACTION_STAT:
result = g_slice_new0 (RhythmDBEvent);
result->db = db;
result->type = RHYTHMDB_EVENT_STAT;
result->entry_type = action->data.types.entry_type;
result->error_type = action->data.types.error_type;
result->ignore_type = action->data.types.ignore_type;
rb_debug ("executing RHYTHMDB_ACTION_STAT for \"%s\"", rb_refstring_get (action->uri));
rhythmdb_execute_stat (db, rb_refstring_get (action->uri), result);
break;
case RHYTHMDB_ACTION_LOAD:
result = g_slice_new0 (RhythmDBEvent);
result->db = db;
result->type = RHYTHMDB_EVENT_METADATA_LOAD;
result->entry_type = action->data.types.entry_type;
result->error_type = action->data.types.error_type;
result->ignore_type = action->data.types.ignore_type;
rb_debug ("executing RHYTHMDB_ACTION_LOAD for \"%s\"", rb_refstring_get (action->uri));
rhythmdb_execute_load (db, rb_refstring_get (action->uri), result);
break;
case RHYTHMDB_ACTION_ENUM_DIR:
rb_debug ("executing RHYTHMDB_ACTION_ENUM_DIR for \"%s\"", rb_refstring_get (action->uri));
rhythmdb_execute_enum_dir (db, action);
break;
case RHYTHMDB_ACTION_SYNC:
{
GError *error = NULL;
RhythmDBEntry *entry;
if (db->priv->dry_run) {
rb_debug ("dry run is enabled, not syncing metadata");
break;
}
entry = rhythmdb_entry_lookup_by_location_refstring (db, action->uri);
if (!entry)
break;
rhythmdb_entry_sync_metadata (entry, action->data.changes, &error);
if (error != NULL) {
RhythmDBSaveErrorData *data;
data = g_new0 (RhythmDBSaveErrorData, 1);
g_object_ref (db);
data->db = db;
data->uri = g_strdup (rb_refstring_get (action->uri));
data->error = error;
g_idle_add ((GSourceFunc)emit_save_error_idle, data);
break;
}
break;
}
case RHYTHMDB_ACTION_QUIT:
/* don't do any real work here, since we may not process it */
rb_debug ("received QUIT action");
break;
default:
g_assert_not_reached ();
break;
}
}
rhythmdb_action_free (db, action);
}
rb_debug ("exiting action thread");
result = g_slice_new0 (RhythmDBEvent);
result->db = db;
result->type = RHYTHMDB_EVENT_THREAD_EXITED;
rhythmdb_push_event (db, result);
return NULL;
}
/**
* rhythmdb_add_uri:
* @db: a #RhythmDB.
* @uri: the URI to add an entry/entries for
*
* Adds the file(s) pointed to by @uri to the database, as entries of type
* RHYTHMDB_ENTRY_TYPE_SONG. If the URI is that of a file, it will be added.
* If the URI is that of a directory, everything under it will be added recursively.
*/
void
rhythmdb_add_uri (RhythmDB *db,
const char *uri)
{
rhythmdb_add_uri_with_types (db,
uri,
RHYTHMDB_ENTRY_TYPE_SONG,
RHYTHMDB_ENTRY_TYPE_IGNORE,
RHYTHMDB_ENTRY_TYPE_IMPORT_ERROR);
}
static void
rhythmdb_add_to_stat_list (RhythmDB *db,
const char *uri,
RhythmDBEntry *entry,
RhythmDBEntryType *type,
RhythmDBEntryType *ignore_type,
RhythmDBEntryType *error_type)
{
RhythmDBEvent *result;
result = g_slice_new0 (RhythmDBEvent);
result->db = db;
result->type = RHYTHMDB_EVENT_STAT;
result->entry_type = type;
result->ignore_type = ignore_type;
result->error_type = error_type;
if (entry != NULL) {
result->entry = rhythmdb_entry_ref (entry);
}
/* do we really need to check for duplicate requests here? .. nah. */
result->uri = rb_refstring_new (uri);
db->priv->stat_list = g_list_prepend (db->priv->stat_list, result);
}
/**
* rhythmdb_add_uri_with_types:
* @db: a #RhythmDB.
* @uri: the URI to add
* @type: the #RhythmDBEntryType to use for new entries
* @ignore_type: the #RhythmDBEntryType to use for ignored files
* @error_type: the #RhythmDBEntryType to use for import errors
*
* Adds the file(s) pointed to by @uri to the database, as entries
* of the specified type. If the URI points to a file, it will be added.
* The the URI identifies a directory, everything under it will be added
* recursively.
*/
void
rhythmdb_add_uri_with_types (RhythmDB *db,
const char *uri,
RhythmDBEntryType *type,
RhythmDBEntryType *ignore_type,
RhythmDBEntryType *error_type)
{
RhythmDBEntry *entry;
rb_debug ("queueing stat for \"%s\"", uri);
g_assert (uri && *uri);
/* keep this outside the stat mutex, as there are other code
* paths that take the stat mutex while already holding the
* entry mutex.
*/
entry = rhythmdb_entry_lookup_by_location (db, uri);
/*
* before the action thread is started, we queue up stat actions,
* as we're still creating and running queries, as well as loading
* the database. when we start the action thread, we'll kick off
* a thread to process all the stat events too.
*
* when the action thread is already running, stat actions go through
* the normal action queue and are processed by the action thread.
*/
g_mutex_lock (&db->priv->stat_mutex);
if (db->priv->action_thread_running) {
RhythmDBAction *action;
g_mutex_unlock (&db->priv->stat_mutex);
action = g_slice_new0 (RhythmDBAction);
action->type = RHYTHMDB_ACTION_STAT;
action->uri = rb_refstring_new (uri);
action->data.types.entry_type = type;
action->data.types.ignore_type = ignore_type;
action->data.types.error_type = error_type;
g_async_queue_push (db->priv->action_queue, action);
} else {
rhythmdb_add_to_stat_list (db, uri, entry, type, ignore_type, error_type);
g_mutex_unlock (&db->priv->stat_mutex);
}
}
static gboolean
rhythmdb_sync_library_idle (RhythmDB *db)
{
rhythmdb_sync_library_location (db);
db->priv->sync_library_id = 0;
return FALSE;
}
static gboolean
rhythmdb_load_error_cb (GError *error)
{
rb_error_dialog (NULL,
_("Could not load the music database:"),
"%s", error->message);
g_error_free (error);
return FALSE;
}
static gpointer
rhythmdb_load_thread_main (RhythmDB *db)
{
RhythmDBEvent *result;
RhythmDBClass *klass = RHYTHMDB_GET_CLASS (db);
GError *error = NULL;
db->priv->active_mounts = rhythmdb_get_active_mounts (db);
rb_profile_start ("loading db");
g_mutex_lock (&db->priv->saving_mutex);
if (klass->impl_load (db, db->priv->exiting, &error) == FALSE) {
rb_debug ("db load failed: disabling saving");
db->priv->can_save = FALSE;
if (error) {
g_idle_add ((GSourceFunc) rhythmdb_load_error_cb, error);
}
}
g_mutex_unlock (&db->priv->saving_mutex);
rb_list_deep_free (db->priv->active_mounts);
db->priv->active_mounts = NULL;
db->priv->load_thread = NULL;
db->priv->sync_library_id = g_timeout_add_seconds (10, (GSourceFunc) rhythmdb_sync_library_idle, db);
rb_debug ("queuing db load complete signal");
result = g_slice_new0 (RhythmDBEvent);
result->type = RHYTHMDB_EVENT_DB_LOAD;
g_async_queue_push (db->priv->event_queue, result);
rb_debug ("exiting");
result = g_slice_new0 (RhythmDBEvent);
result->type = RHYTHMDB_EVENT_THREAD_EXITED;
rhythmdb_push_event (db, result);
return NULL;
}
/**
* rhythmdb_load:
* @db: a #RhythmDB.
*
* Load the database from disk.
*/
void
rhythmdb_load (RhythmDB *db)
{
db->priv->load_thread = rhythmdb_thread_create (db, (GThreadFunc) rhythmdb_load_thread_main, db);
}
static gpointer
rhythmdb_save_thread_main (RhythmDB *db)
{
RhythmDBClass *klass;
RhythmDBEvent *result;
rb_debug ("entering save thread");
g_mutex_lock (&db->priv->saving_mutex);
db->priv->save_count++;
g_cond_broadcast (&db->priv->saving_condition);
if (!(db->priv->dirty && db->priv->can_save)) {
rb_debug ("no save needed, ignoring");
g_mutex_unlock (&db->priv->saving_mutex);
goto out;
}
while (db->priv->saving)
g_cond_wait (&db->priv->saving_condition, &db->priv->saving_mutex);
db->priv->saving = TRUE;
rb_debug ("saving rhythmdb");
klass = RHYTHMDB_GET_CLASS (db);
klass->impl_save (db);
db->priv->saving = FALSE;
db->priv->dirty = FALSE;
g_mutex_unlock (&db->priv->saving_mutex);
g_cond_broadcast (&db->priv->saving_condition);
out:
result = g_slice_new0 (RhythmDBEvent);
result->db = db;
result->type = RHYTHMDB_EVENT_DB_SAVED;
g_async_queue_push (db->priv->event_queue, result);
result = g_slice_new0 (RhythmDBEvent);
result->db = db;
result->type = RHYTHMDB_EVENT_THREAD_EXITED;
rhythmdb_push_event (db, result);
return NULL;
}
/**
* rhythmdb_save_async:
* @db: a #RhythmDB.
*
* Save the database to disk, asynchronously.
*/
void
rhythmdb_save_async (RhythmDB *db)
{
rb_debug ("saving the rhythmdb in the background");
rhythmdb_read_enter (db);
rhythmdb_thread_create (db, (GThreadFunc) rhythmdb_save_thread_main, db);
}
/**
* rhythmdb_save:
* @db: a #RhythmDB.
*
* Save the database to disk, not returning until it has been saved.
*/
void
rhythmdb_save (RhythmDB *db)
{
int new_save_count;
rb_debug("saving the rhythmdb and blocking");
g_mutex_lock (&db->priv->saving_mutex);
new_save_count = db->priv->save_count + 1;
rhythmdb_save_async (db);
/* wait until this save request is being processed */
while (db->priv->save_count < new_save_count) {
g_cond_wait (&db->priv->saving_condition, &db->priv->saving_mutex);
}
/* wait until it's done */
while (db->priv->saving) {
g_cond_wait (&db->priv->saving_condition, &db->priv->saving_mutex);
}
rb_debug ("done");
g_mutex_unlock (&db->priv->saving_mutex);
}
/**
* rhythmdb_entry_set:
* @db:# a RhythmDB.
* @entry: a #RhythmDBEntry.
* @propid: the id of the property to set.
* @value: the property value.
*
* This function can be called by any code which wishes to change a
* song property and send a notification. It may be called when the
* database is read-only; in this case the change will be queued for
* an unspecified time in the future. The implication of this is that
* rhythmdb_entry_get() may not reflect the changes immediately. However,
* if this property is exposed in the user interface, you should still
* make the change in the widget. Then when the database returns to a
* writable state, your change will take effect in the database too,
* and a notification will be sent at that point.
*
* Note that you must call rhythmdb_commit() at some point after invoking
* this function, and that even after the commit, your change may not
* have taken effect.
*/
void
rhythmdb_entry_set (RhythmDB *db,
RhythmDBEntry *entry,
guint propid,
const GValue *value)
{
g_return_if_fail (RHYTHMDB_IS (db));
g_return_if_fail (entry != NULL);
if ((entry->flags & RHYTHMDB_ENTRY_INSERTED) != 0) {
if (!rhythmdb_get_readonly (db) && rb_is_main_thread ()) {
rhythmdb_entry_set_internal (db, entry, TRUE, propid, value);
} else {
RhythmDBEvent *result;
result = g_slice_new0 (RhythmDBEvent);
result->db = db;
result->type = RHYTHMDB_EVENT_ENTRY_SET;
rb_debug ("queuing RHYTHMDB_ACTION_ENTRY_SET");
result->entry = rhythmdb_entry_ref (entry);
result->change.prop = propid;
result->signal_change = TRUE;
g_value_init (&result->change.new, G_VALUE_TYPE (value));
g_value_copy (value, &result->change.new);
rhythmdb_push_event (db, result);
}
} else {
rhythmdb_entry_set_internal (db, entry, FALSE, propid, value);
}
}
static void
record_entry_change (RhythmDB *db,
RhythmDBEntry *entry,
guint propid,
const GValue *old_value,
const GValue *new_value)
{
RhythmDBEntryChange *changedata;
GSList *changelist;
changedata = g_slice_new0 (RhythmDBEntryChange);
changedata->prop = propid;
g_value_init (&changedata->old, G_VALUE_TYPE (old_value));
g_value_init (&changedata->new, G_VALUE_TYPE (new_value));
g_value_copy (old_value, &changedata->old);
g_value_copy (new_value, &changedata->new);
g_mutex_lock (&db->priv->change_mutex);
/* ref the entry before adding to hash, it is unreffed when removed */
rhythmdb_entry_ref (entry);
changelist = g_hash_table_lookup (db->priv->changed_entries, entry);
changelist = g_slist_append (changelist, changedata);
g_hash_table_insert (db->priv->changed_entries, entry, changelist);
g_mutex_unlock (&db->priv->change_mutex);
}
void
rhythmdb_entry_set_internal (RhythmDB *db,
RhythmDBEntry *entry,
gboolean notify_if_inserted,
guint propid,
const GValue *value)
{
RhythmDBClass *klass = RHYTHMDB_GET_CLASS (db);
gboolean handled;
RhythmDBPodcastFields *podcast = NULL;
GValue conv_value = {0,};
GValue old_value = {0,};
gboolean nop;
g_return_if_fail (entry != NULL);
/* convert the value if necessary */
if (G_VALUE_TYPE (value) != rhythmdb_get_property_type (db, propid)) {
g_value_init (&conv_value, rhythmdb_get_property_type (db, propid));
if (g_value_transform (value, &conv_value) == FALSE) {
g_warning ("Unable to convert new value for property %s from %s to %s",
rhythmdb_nice_elt_name_from_propid (db, propid),
g_type_name (G_VALUE_TYPE (value)),
g_type_name (rhythmdb_get_property_type (db, propid)));
g_assert_not_reached ();
}
value = &conv_value;
}
/* compare the value with what's already there */
g_value_init (&old_value, G_VALUE_TYPE (value));
rhythmdb_entry_get (db, entry, propid, &old_value);
switch (G_VALUE_TYPE (value)) {
case G_TYPE_STRING:
/* some properties are allowed to be NULL */
switch (propid) {
case RHYTHMDB_PROP_PLAYBACK_ERROR:
case RHYTHMDB_PROP_MOUNTPOINT:
break;
default:
g_assert (g_utf8_validate (g_value_get_string (value), -1, NULL));
break;
}
if (g_value_get_string (value) && g_value_get_string (&old_value)) {
nop = (strcmp (g_value_get_string (value), g_value_get_string (&old_value)) == 0);
} else {
nop = FALSE;
}
break;
case G_TYPE_BOOLEAN:
nop = (g_value_get_boolean (value) == g_value_get_boolean (&old_value));
break;
case G_TYPE_ULONG:
nop = (g_value_get_ulong (value) == g_value_get_ulong (&old_value));
break;
case G_TYPE_UINT64:
nop = (g_value_get_uint64 (value) == g_value_get_uint64 (&old_value));
break;
case G_TYPE_DOUBLE:
nop = (g_value_get_double (value) == g_value_get_double (&old_value));
break;
case G_TYPE_OBJECT:
nop = (g_value_get_object (value) == g_value_get_object (&old_value));
break;
default:
g_assert_not_reached ();
break;
}
if (nop == FALSE && (entry->flags & RHYTHMDB_ENTRY_INSERTED) && notify_if_inserted) {
record_entry_change (db, entry, propid, &old_value, value);
}
g_value_unset (&old_value);
if (nop) {
if (value == &conv_value) {
g_value_unset (&conv_value);
}
return;
}
handled = klass->impl_entry_set (db, entry, propid, value);
if (!handled) {
if (entry->type == RHYTHMDB_ENTRY_TYPE_PODCAST_FEED ||
entry->type == RHYTHMDB_ENTRY_TYPE_PODCAST_POST ||
entry->type == RHYTHMDB_ENTRY_TYPE_PODCAST_SEARCH)
podcast = RHYTHMDB_ENTRY_GET_TYPE_DATA (entry, RhythmDBPodcastFields);
switch (propid) {
case RHYTHMDB_PROP_TYPE:
case RHYTHMDB_PROP_ENTRY_ID:
g_assert_not_reached ();
break;
case RHYTHMDB_PROP_TITLE:
if (entry->title != NULL) {
rb_refstring_unref (entry->title);
}
entry->title = rb_refstring_new (g_value_get_string (value));
break;
case RHYTHMDB_PROP_ALBUM:
if (entry->album != NULL) {
rb_refstring_unref (entry->album);
}
entry->album = rb_refstring_new (g_value_get_string (value));
break;
case RHYTHMDB_PROP_ARTIST:
if (entry->artist != NULL) {
rb_refstring_unref (entry->artist);
}
entry->artist = rb_refstring_new (g_value_get_string (value));
break;
case RHYTHMDB_PROP_GENRE:
if (entry->genre != NULL) {
rb_refstring_unref (entry->genre);
}
entry->genre = rb_refstring_new (g_value_get_string (value));
break;
case RHYTHMDB_PROP_COMMENT:
if (entry->comment != NULL) {
rb_refstring_unref (entry->comment);
}
entry->comment = rb_refstring_new (g_value_get_string (value));
break;
case RHYTHMDB_PROP_TRACK_NUMBER:
entry->tracknum = g_value_get_ulong (value);
break;
case RHYTHMDB_PROP_TRACK_TOTAL:
entry->tracktotal = g_value_get_ulong (value);
break;
case RHYTHMDB_PROP_DISC_NUMBER:
entry->discnum = g_value_get_ulong (value);
break;
case RHYTHMDB_PROP_DISC_TOTAL:
entry->disctotal = g_value_get_ulong (value);
break;
case RHYTHMDB_PROP_DURATION:
entry->duration = g_value_get_ulong (value);
break;
case RHYTHMDB_PROP_BITRATE:
entry->bitrate = g_value_get_ulong (value);
break;
case RHYTHMDB_PROP_DATE:
{
gulong julian;
julian = g_value_get_ulong (value);
if (julian > 0)
g_date_set_julian (&entry->date, julian);
else
g_date_clear (&entry->date, 1);
break;
}
case RHYTHMDB_PROP_TRACK_GAIN:
g_warning ("RHYTHMDB_PROP_TRACK_GAIN no longer supported");
break;
case RHYTHMDB_PROP_TRACK_PEAK:
g_warning ("RHYTHMDB_PROP_TRACK_PEAK no longer supported");
break;
case RHYTHMDB_PROP_ALBUM_GAIN:
g_warning ("RHYTHMDB_PROP_ALBUM_GAIN no longer supported");
break;
case RHYTHMDB_PROP_ALBUM_PEAK:
g_warning ("RHYTHMDB_PROP_ALBUM_PEAK no longer supported");
break;
case RHYTHMDB_PROP_LOCATION:
rb_refstring_unref (entry->location);
entry->location = rb_refstring_new (g_value_get_string (value));
break;
case RHYTHMDB_PROP_PLAYBACK_ERROR:
rb_refstring_unref (entry->playback_error);
if (g_value_get_string (value))
entry->playback_error = rb_refstring_new (g_value_get_string (value));
else
entry->playback_error = NULL;
break;
case RHYTHMDB_PROP_MOUNTPOINT:
if (entry->mountpoint != NULL) {
rb_refstring_unref (entry->mountpoint);
entry->mountpoint = NULL;
}
if (g_value_get_string (value) != NULL) {
entry->mountpoint = rb_refstring_new (g_value_get_string (value));
}
break;
case RHYTHMDB_PROP_FILE_SIZE:
entry->file_size = g_value_get_uint64 (value);
break;
case RHYTHMDB_PROP_MEDIA_TYPE:
if (entry->media_type != NULL) {
rb_refstring_unref (entry->media_type);
}
entry->media_type = rb_refstring_new (g_value_get_string (value));
break;
case RHYTHMDB_PROP_MTIME:
entry->mtime = g_value_get_ulong (value);
break;
case RHYTHMDB_PROP_FIRST_SEEN:
entry->first_seen = g_value_get_ulong (value);
entry->flags |= RHYTHMDB_ENTRY_FIRST_SEEN_DIRTY;
break;
case RHYTHMDB_PROP_LAST_SEEN:
entry->last_seen = g_value_get_ulong (value);
entry->flags |= RHYTHMDB_ENTRY_LAST_SEEN_DIRTY;
break;
case RHYTHMDB_PROP_RATING:
entry->rating = g_value_get_double (value);
break;
case RHYTHMDB_PROP_PLAY_COUNT:
entry->play_count = g_value_get_ulong (value);
break;
case RHYTHMDB_PROP_LAST_PLAYED:
entry->last_played = g_value_get_ulong (value);
entry->flags |= RHYTHMDB_ENTRY_LAST_PLAYED_DIRTY;
break;
case RHYTHMDB_PROP_BPM:
entry->bpm = g_value_get_double (value);
break;
case RHYTHMDB_PROP_MUSICBRAINZ_TRACKID:
rb_refstring_unref (entry->musicbrainz_trackid);
entry->musicbrainz_trackid = rb_refstring_new (g_value_get_string (value));
break;
case RHYTHMDB_PROP_MUSICBRAINZ_ARTISTID:
rb_refstring_unref (entry->musicbrainz_artistid);
entry->musicbrainz_artistid = rb_refstring_new (g_value_get_string (value));
break;
case RHYTHMDB_PROP_MUSICBRAINZ_ALBUMID:
rb_refstring_unref (entry->musicbrainz_albumid);
entry->musicbrainz_albumid = rb_refstring_new (g_value_get_string (value));
break;
case RHYTHMDB_PROP_MUSICBRAINZ_ALBUMARTISTID:
rb_refstring_unref (entry->musicbrainz_albumartistid);
entry->musicbrainz_albumartistid = rb_refstring_new (g_value_get_string (value));
break;
case RHYTHMDB_PROP_ARTIST_SORTNAME:
rb_refstring_unref (entry->artist_sortname);
entry->artist_sortname = rb_refstring_new (g_value_get_string (value));
break;
case RHYTHMDB_PROP_ALBUM_SORTNAME:
rb_refstring_unref (entry->album_sortname);
entry->album_sortname = rb_refstring_new (g_value_get_string (value));
break;
case RHYTHMDB_PROP_ALBUM_ARTIST:
rb_refstring_unref (entry->album_artist);
entry->album_artist = rb_refstring_new (g_value_get_string (value));
break;
case RHYTHMDB_PROP_ALBUM_ARTIST_SORTNAME:
rb_refstring_unref (entry->album_artist_sortname);
entry->album_artist_sortname = rb_refstring_new (g_value_get_string (value));
break;
case RHYTHMDB_PROP_COMPOSER:
rb_refstring_unref (entry->composer);
entry->composer = rb_refstring_new (g_value_get_string (value));
break;
case RHYTHMDB_PROP_COMPOSER_SORTNAME:
rb_refstring_unref (entry->composer_sortname);
entry->composer_sortname = rb_refstring_new (g_value_get_string (value));
break;
case RHYTHMDB_PROP_HIDDEN:
if (g_value_get_boolean (value)) {
entry->flags |= RHYTHMDB_ENTRY_HIDDEN;
} else {
entry->flags &= ~RHYTHMDB_ENTRY_HIDDEN;
}
entry->flags |= RHYTHMDB_ENTRY_LAST_SEEN_DIRTY;
break;
case RHYTHMDB_PROP_STATUS:
g_assert (podcast);
podcast->status = g_value_get_ulong (value);
break;
case RHYTHMDB_PROP_DESCRIPTION:
g_assert (podcast);
rb_refstring_unref (podcast->description);
podcast->description = rb_refstring_new (g_value_get_string (value));
break;
case RHYTHMDB_PROP_SUBTITLE:
g_assert (podcast);
rb_refstring_unref (podcast->subtitle);
podcast->subtitle = rb_refstring_new (g_value_get_string (value));
break;
case RHYTHMDB_PROP_SUMMARY:
g_assert_not_reached ();
break;
case RHYTHMDB_PROP_LANG:
g_assert (podcast);
if (podcast->lang != NULL) {
rb_refstring_unref (podcast->lang);
}
podcast->lang = rb_refstring_new (g_value_get_string (value));
break;
case RHYTHMDB_PROP_COPYRIGHT:
g_assert (podcast);
if (podcast->copyright != NULL) {
rb_refstring_unref (podcast->copyright);
}
podcast->copyright = rb_refstring_new (g_value_get_string (value));
break;
case RHYTHMDB_PROP_IMAGE:
g_assert (podcast);
if (podcast->image != NULL) {
rb_refstring_unref (podcast->image);
}
podcast->image = rb_refstring_new (g_value_get_string (value));
break;
case RHYTHMDB_PROP_POST_TIME:
g_assert (podcast);
podcast->post_time = g_value_get_ulong (value);
break;
case RHYTHMDB_PROP_PODCAST_GUID:
g_assert (podcast);
if (podcast->guid != NULL)
rb_refstring_unref (podcast->guid);
podcast->guid = rb_refstring_new (g_value_get_string (value));
break;
case RHYTHMDB_NUM_PROPERTIES:
g_assert_not_reached ();
break;
}
}
if (value == &conv_value) {
g_value_unset (&conv_value);
}
/* set the dirty state */
db->priv->dirty = TRUE;
}
/**
* rhythmdb_entry_sync_mirrored:
* @db: a #RhythmDB.
* @type: a #RhythmDBEntry.
* @propid: the property to sync the mirrored version of.
*
* Synchronise "mirrored" properties, such as the string version of the last-played
* time. This should be called when a property is directly modified, passing the
* original property.
*
* This should only be used by RhythmDB itself, or a backend (such as rhythmdb-tree).
*/
static void
rhythmdb_entry_sync_mirrored (RhythmDBEntry *entry,
guint propid)
{
static const char *never;
char *val;
if (never == NULL)
never = _("Never");
switch (propid) {
case RHYTHMDB_PROP_LAST_PLAYED_STR:
{
RBRefString *old, *new;
if (!(entry->flags & RHYTHMDB_ENTRY_LAST_PLAYED_DIRTY))
break;
old = g_atomic_pointer_get (&entry->last_played_str);
if (entry->last_played == 0) {
new = rb_refstring_new (never);
} else {
val = rb_utf_friendly_time (entry->last_played);
new = rb_refstring_new (val);
g_free (val);
}
if (g_atomic_pointer_compare_and_exchange (&entry->last_played_str, old, new)) {
if (old != NULL) {
rb_refstring_unref (old);
}
} else {
rb_refstring_unref (new);
}
break;
}
case RHYTHMDB_PROP_FIRST_SEEN_STR:
{
RBRefString *old, *new;
if (!(entry->flags & RHYTHMDB_ENTRY_FIRST_SEEN_DIRTY))
break;
old = g_atomic_pointer_get (&entry->first_seen_str);
if (entry->first_seen == 0) {
new = rb_refstring_new (never);
} else {
val = rb_utf_friendly_time (entry->first_seen);
new = rb_refstring_new (val);
g_free (val);
}
if (g_atomic_pointer_compare_and_exchange (&entry->first_seen_str, old, new)) {
if (old != NULL) {
rb_refstring_unref (old);
}
} else {
rb_refstring_unref (new);
}
break;
}
case RHYTHMDB_PROP_LAST_SEEN_STR:
{
RBRefString *old, *new;
if (!(entry->flags & RHYTHMDB_ENTRY_LAST_SEEN_DIRTY))
break;
old = g_atomic_pointer_get (&entry->last_seen_str);
/* only store last seen time as a string for hidden entries */
if (entry->flags & RHYTHMDB_ENTRY_HIDDEN) {
val = rb_utf_friendly_time (entry->last_seen);
new = rb_refstring_new (val);
g_free (val);
} else {
new = NULL;
}
if (g_atomic_pointer_compare_and_exchange (&entry->last_seen_str, old, new)) {
if (old != NULL) {
rb_refstring_unref (old);
}
} else {
rb_refstring_unref (new);
}
break;
}
default:
break;
}
}
/**
* rhythmdb_entry_delete:
* @db: a #RhythmDB.
* @entry: a #RhythmDBEntry.
*
* Delete entry @entry from the database, sending notification of its deletion.
* This is usually used by sources where entries can disappear randomly, such
* as a network source.
*/
void
rhythmdb_entry_delete (RhythmDB *db,
RhythmDBEntry *entry)
{
RhythmDBClass *klass = RHYTHMDB_GET_CLASS (db);
g_return_if_fail (RHYTHMDB_IS (db));
g_return_if_fail (entry != NULL);
rb_debug ("deleting entry %p", entry);
/* ref the entry before adding to hash, it is unreffed when removed */
rhythmdb_entry_ref (entry);
klass->impl_entry_delete (db, entry);
g_mutex_lock (&db->priv->change_mutex);
g_hash_table_insert (db->priv->deleted_entries, entry, g_thread_self ());
g_mutex_unlock (&db->priv->change_mutex);
/* deleting an entry makes the db dirty */
db->priv->dirty = TRUE;
}
/**
* rhythmdb_entry_move_to_trash:
* @db: the #RhythmDB
* @entry: #RhythmDBEntry to trash
*
* Trashes the file represented by #entry. If possible, the file is
* moved to the user's trash directory and the entry is set to hidden,
* otherwise the error will be stored as the playback error for the entry.
*/
void
rhythmdb_entry_move_to_trash (RhythmDB *db,
RhythmDBEntry *entry)
{
const char *uri;
GFile *file;
GError *error = NULL;
uri = rb_refstring_get (entry->location);
file = g_file_new_for_uri (uri);
g_file_trash (file, NULL, &error);
if (error != NULL) {
GValue value = { 0, };
g_value_init (&value, G_TYPE_STRING);
g_value_set_string (&value, error->message);
rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_PLAYBACK_ERROR, &value);
g_value_unset (&value);
rb_debug ("trashing %s failed: %s",
uri,
error->message);
g_error_free (error);
} else {
rhythmdb_entry_set_visibility (db, entry, FALSE);
}
g_object_unref (file);
}
/**
* rhythmdb_entry_delete_by_type:
* @db: a #RhythmDB.
* @type: type of entried to delete.
*
* Delete all entries from the database of the given type.
* This is usually used by non-permanent sources when they disappear, such as
* removable media being removed, or a network share becoming unavailable.
*/
void
rhythmdb_entry_delete_by_type (RhythmDB *db,
RhythmDBEntryType *type)
{
RhythmDBClass *klass = RHYTHMDB_GET_CLASS (db);
if (klass->impl_entry_delete_by_type) {
klass->impl_entry_delete_by_type (db, type);
} else {
g_warning ("delete_by_type not implemented");
}
}
/**
* rhythmdb_nice_elt_name_from_propid:
* @db: the #RhythmDB
* @propid: property ID
*
* Returns a short non-translated name for the property #propid.
* This name is suitable for use as an XML tag name, for example.
*
* Return value: property ID name, must not be freed
*/
const xmlChar *
rhythmdb_nice_elt_name_from_propid (RhythmDB *db,
RhythmDBPropType propid)
{
return (xmlChar *)rhythmdb_properties[propid].elt_name;
}
/**
* rhythmdb_propid_from_nice_elt_name:
* @db: the #RhythmDB
* @name: a property ID name
*
* Converts a property name returned by @rhythmdb_propid_from_nice_elt_name
* back to a #RhythmDBPropType. If the name does not match a property ID,
* -1 will be returned instead.
*
* Return value: a #RhythmDBPropType, or -1
*/
int
rhythmdb_propid_from_nice_elt_name (RhythmDB *db,
const xmlChar *name)
{
gpointer ret, orig;
if (g_hash_table_lookup_extended (db->priv->propname_map, name,
&orig, &ret)) {
return GPOINTER_TO_INT (ret);
}
return -1;
}
/**
* rhythmdb_entry_lookup_by_location:
* @db: a #RhythmDB.
* @uri: the URI of the entry to lookup.
*
* Looks up the entry with location @uri.
*
* Returns: (transfer none): the entry with location @uri, or NULL if no such entry exists.
*/
RhythmDBEntry *
rhythmdb_entry_lookup_by_location (RhythmDB *db,
const char *uri)
{
RBRefString *rs;
rs = rb_refstring_find (uri);
if (rs != NULL) {
return rhythmdb_entry_lookup_by_location_refstring (db, rs);
} else {
return NULL;
}
}
/**
* rhythmdb_entry_lookup_by_location_refstring:
* @db: the #RhythmDB
* @uri: #RBRefString for the entry location
*
* Looks up the entry with location @uri.
*
* Returns: (transfer none): the entry with location @uri, or NULL if no such entry exists.
*/
RhythmDBEntry *
rhythmdb_entry_lookup_by_location_refstring (RhythmDB *db,
RBRefString *uri)
{
RhythmDBClass *klass = RHYTHMDB_GET_CLASS (db);
return klass->impl_lookup_by_location (db, uri);
}
/**
* rhythmdb_entry_lookup_by_id:
* @db: a #RhythmDB.
* @id: entry ID
*
* Looks up the entry with id @id.
*
* Returns: (transfer none): the entry with id @id, or NULL if no such entry exists.
*/
RhythmDBEntry *
rhythmdb_entry_lookup_by_id (RhythmDB *db,
gint id)
{
RhythmDBClass *klass = RHYTHMDB_GET_CLASS (db);
return klass->impl_lookup_by_id (db, id);
}
/**
* rhythmdb_entry_lookup_from_string:
* @db: a #RhythmDB.
* @str: string
* @is_id: whether the string is an entry ID or a location.
*
* Locates an entry using a string containing either an entry ID
* or a location.
*
* Returns: (transfer none): the entry matching the string, or NULL if no such entry exists.
*/
RhythmDBEntry *
rhythmdb_entry_lookup_from_string (RhythmDB *db,
const char *str,
gboolean is_id)
{
if (is_id) {
gint id;
id = strtoul (str, NULL, 10);
if (id == 0)
return NULL;
return rhythmdb_entry_lookup_by_id (db, id);
} else {
return rhythmdb_entry_lookup_by_location (db, str);
}
}
/**
* rhythmdb_entry_foreach:
* @db: a #RhythmDB.
* @func: (scope call): the function to call with each entry.
* @data: user data to pass to the function.
*
* Calls the given function for each of the entries in the database.
*/
void
rhythmdb_entry_foreach (RhythmDB *db,
RhythmDBEntryForeachFunc func,
gpointer data)
{
RhythmDBClass *klass = RHYTHMDB_GET_CLASS (db);
klass->impl_entry_foreach (db, func, data);
}
/**
* rhythmdb_entry_count:
* @db: a #RhythmDB.
*
* Returns the number of entries in the database.
*
* Return value: number of entries
*/
gint64
rhythmdb_entry_count (RhythmDB *db)
{
RhythmDBClass *klass = RHYTHMDB_GET_CLASS (db);
return klass->impl_entry_count (db);
}
/**
* rhythmdb_entry_foreach_by_type:
* @db: a #RhythmDB.
* @entry_type: the type of entry to retrieve
* @func: (scope call): the function to call with each entry
* @data: user data to pass to the function.
*
* Calls the given function for each of the entries in the database
* of a given type.
*/
void
rhythmdb_entry_foreach_by_type (RhythmDB *db,
RhythmDBEntryType *entry_type,
RhythmDBEntryForeachFunc func,
gpointer data)
{
RhythmDBClass *klass = RHYTHMDB_GET_CLASS (db);
klass->impl_entry_foreach_by_type (db, entry_type, func, data);
}
/**
* rhythmdb_entry_count_by_type:
* @db: a #RhythmDB.
* @entry_type: a #RhythmDBEntryType.
*
* Returns the number of entries in the database of a particular type.
*
* Return value: entry count
*/
gint64
rhythmdb_entry_count_by_type (RhythmDB *db,
RhythmDBEntryType *entry_type)
{
RhythmDBClass *klass = RHYTHMDB_GET_CLASS (db);
return klass->impl_entry_count_by_type (db, entry_type);
}
/**
* rhythmdb_evaluate_query:
* @db: a #RhythmDB.
* @query: a query.
* @entry: a @RhythmDBEntry.
*
* Evaluates the given entry against the given query.
*
* Returns: whether the given entry matches the criteria of the given query.
*/
gboolean
rhythmdb_evaluate_query (RhythmDB *db,
GPtrArray *query,
RhythmDBEntry *entry)
{
RhythmDBClass *klass = RHYTHMDB_GET_CLASS (db);
return klass->impl_evaluate_query (db, query, entry);
}
static void
rhythmdb_query_internal (RhythmDBQueryThreadData *data)
{
RhythmDBEvent *result;
RhythmDBClass *klass = RHYTHMDB_GET_CLASS (data->db);
rhythmdb_query_preprocess (data->db, data->query);
rb_debug ("doing query");
klass->impl_do_full_query (data->db, data->query,
data->results,
&data->cancel);
rb_debug ("completed");
rhythmdb_query_results_query_complete (data->results);
result = g_slice_new0 (RhythmDBEvent);
result->db = data->db;
result->type = RHYTHMDB_EVENT_QUERY_COMPLETE;
result->results = data->results;
rhythmdb_push_event (data->db, result);
rhythmdb_query_free (data->query);
}
static gpointer
query_thread_main (RhythmDBQueryThreadData *data)
{
RhythmDBEvent *result;
rb_debug ("entering query thread");
rhythmdb_query_internal (data);
result = g_slice_new0 (RhythmDBEvent);
result->db = data->db;
result->type = RHYTHMDB_EVENT_THREAD_EXITED;
rhythmdb_push_event (data->db, result);
g_free (data);
return NULL;
}
/**
* rhythmdb_do_full_query_async_parsed:
* @db: the #RhythmDB
* @results: a #RhythmDBQueryResults instance to feed results to
* @query: the query to run
*
* Asynchronously runs a parsed query across the database, feeding matching
* entries to @results in chunks. This can only be called from the
* main thread.
*
* Since @results is always a @RhythmDBQueryModel,
* use the RhythmDBQueryModel::complete signal to identify when the
* query is complete.
*/
void
rhythmdb_do_full_query_async_parsed (RhythmDB *db,
RhythmDBQueryResults *results,
GPtrArray *query)
{
RhythmDBQueryThreadData *data;
data = g_new0 (RhythmDBQueryThreadData, 1);
data->db = db;
data->query = rhythmdb_query_copy (query);
data->results = results;
data->cancel = FALSE;
rhythmdb_read_enter (db);
rhythmdb_query_results_set_query (results, query);
g_object_ref (results);
g_object_ref (db);
g_atomic_int_inc (&db->priv->outstanding_threads);
g_async_queue_ref (db->priv->action_queue);
g_async_queue_ref (db->priv->event_queue);
g_thread_pool_push (db->priv->query_thread_pool, data, NULL);
}
/**
* rhythmdb_do_full_query_async:
* @db: the #RhythmDB
* @results: a #RhythmDBQueryResults to feed results to
* @...: query parameters
*
* Asynchronously runs a query specified in the function arguments
* across the database, feeding matching entries to @results in chunks.
* This can only be called from the main thread.
*
* Since @results is always a @RhythmDBQueryModel,
* use the RhythmDBQueryModel::complete signal to identify when the
* query is complete.
*
* FIXME: example
*/
void
rhythmdb_do_full_query_async (RhythmDB *db,
RhythmDBQueryResults *results,
...)
{
GPtrArray *query;
va_list args;
va_start (args, results);
query = rhythmdb_query_parse_valist (db, args);
rhythmdb_do_full_query_async_parsed (db, results, query);
rhythmdb_query_free (query);
va_end (args);
}
static void
rhythmdb_do_full_query_internal (RhythmDB *db,
RhythmDBQueryResults *results,
GPtrArray *query)
{
RhythmDBQueryThreadData *data;
data = g_new0 (RhythmDBQueryThreadData, 1);
data->db = db;
data->query = rhythmdb_query_copy (query);
data->results = results;
data->cancel = FALSE;
rhythmdb_read_enter (db);
rhythmdb_query_results_set_query (results, query);
g_object_ref (results);
rhythmdb_query_internal (data);
g_free (data);
}
/**
* rhythmdb_do_full_query_parsed:
* @db: the #RhythmDB
* @results: a #RhythmDBQueryResults instance to feed results to
* @query: a parsed query
*
* Synchronously evaluates the parsed query @query, feeding results
* to @results in chunks. Does not return until the query is complete.
*/
void
rhythmdb_do_full_query_parsed (RhythmDB *db,
RhythmDBQueryResults *results,
GPtrArray *query)
{
rhythmdb_do_full_query_internal (db, results, query);
}
/**
* rhythmdb_do_full_query:
* @db: the #RhythmDB
* @results: a #RhythmDBQueryResults instance to feed results to
* @...: query parameters
*
* Synchronously evaluates @query, feeding results to @results in
* chunks. Does not return until the query is complete.
* This can only be called from the main thread.
*
* FIXME: example
*/
void
rhythmdb_do_full_query (RhythmDB *db,
RhythmDBQueryResults *results,
...)
{
GPtrArray *query;
va_list args;
va_start (args, results);
query = rhythmdb_query_parse_valist (db, args);
rhythmdb_do_full_query_internal (db, results, query);
rhythmdb_query_free (query);
va_end (args);
}
/* This should really be standard. */
#define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC }
GType
rhythmdb_query_type_get_type (void)
{
static GType etype = 0;
if (etype == 0)
{
static const GEnumValue values[] =
{
ENUM_ENTRY (RHYTHMDB_QUERY_END, "query-end"),
ENUM_ENTRY (RHYTHMDB_QUERY_DISJUNCTION, "disjunctive-marker"),
ENUM_ENTRY (RHYTHMDB_QUERY_SUBQUERY, "subquery"),
ENUM_ENTRY (RHYTHMDB_QUERY_PROP_EQUALS, "equals"),
ENUM_ENTRY (RHYTHMDB_QUERY_PROP_NOT_EQUAL, "not-equal"),
ENUM_ENTRY (RHYTHMDB_QUERY_PROP_LIKE, "fuzzy-match"),
ENUM_ENTRY (RHYTHMDB_QUERY_PROP_NOT_LIKE, "inverted-fuzzy-match"),
ENUM_ENTRY (RHYTHMDB_QUERY_PROP_PREFIX, "starts-with"),
ENUM_ENTRY (RHYTHMDB_QUERY_PROP_SUFFIX, "ends-with"),
ENUM_ENTRY (RHYTHMDB_QUERY_PROP_GREATER, "greater-than"),
ENUM_ENTRY (RHYTHMDB_QUERY_PROP_LESS, "less-than"),
ENUM_ENTRY (RHYTHMDB_QUERY_PROP_CURRENT_TIME_WITHIN, "within-current-time"),
ENUM_ENTRY (RHYTHMDB_QUERY_PROP_CURRENT_TIME_NOT_WITHIN, "not-within-current-time"),
ENUM_ENTRY (RHYTHMDB_QUERY_PROP_YEAR_EQUALS, "year-equals"),
ENUM_ENTRY (RHYTHMDB_QUERY_PROP_YEAR_NOT_EQUAL, "year-not-equals"),
ENUM_ENTRY (RHYTHMDB_QUERY_PROP_YEAR_GREATER, "year-greater-than"),
ENUM_ENTRY (RHYTHMDB_QUERY_PROP_YEAR_LESS, "year-less-than"),
{ 0, 0, 0 }
};
etype = g_enum_register_static ("RhythmDBQueryType", values);
}
return etype;
}
GType
rhythmdb_prop_type_get_type (void)
{
static GType etype = 0;
if (etype == 0)
{
int i;
static GEnumValue values[G_N_ELEMENTS(rhythmdb_properties)];
g_assert(G_N_ELEMENTS(rhythmdb_properties)-1 == RHYTHMDB_NUM_PROPERTIES);
for (i = 0; i < G_N_ELEMENTS(rhythmdb_properties)-1; i++) {
g_assert (i == rhythmdb_properties[i].prop_id);
values[i].value = rhythmdb_properties[i].prop_id;
values[i].value_name = rhythmdb_properties[i].prop_name;
values[i].value_nick = rhythmdb_properties[i].elt_name;
}
etype = g_enum_register_static ("RhythmDBPropType", values);
}
return etype;
}
void
rhythmdb_emit_entry_deleted (RhythmDB *db,
RhythmDBEntry *entry)
{
g_signal_emit (G_OBJECT (db), rhythmdb_signals[ENTRY_DELETED], 0, entry);
}
static gboolean
rhythmdb_entry_extra_metadata_accumulator (GSignalInvocationHint *ihint,
GValue *return_accu,
const GValue *handler_return,
gpointer data)
{
if (handler_return == NULL)
return TRUE;
g_value_copy (handler_return, return_accu);
return (g_value_get_boxed (return_accu) == NULL);
}
/**
* rhythmdb_entry_request_extra_metadata:
* @db: a #RhythmDB
* @entry: a #RhythmDBEntry
* @property_name: the metadata predicate
*
* Emits a request for extra metadata for the @entry.
* The @property_name argument is emitted as the ::detail part of the
* "entry_extra_metadata_request" signal. It should be a namespaced RDF
* predicate e.g. from Dublin Core, MusicBrainz, or internal to Rhythmbox
* (namespace "rb:"). Suitable predicates would be those that are expensive to
* acquire or only apply to a limited range of entries.
* Handlers capable of providing a particular predicate may ensure they only
* see appropriate requests by supplying an appropriate ::detail part when
* connecting to the signal. Upon a handler returning a non-%NULL value,
* emission will be stopped and the value returned to the caller; if no
* handlers return a non-%NULL value, the caller will receive %NULL. Priority
* is determined by signal connection order, with %G_CONNECT_AFTER providing a
* second, lower rank of priority.
* A handler returning a value should do so in a #GValue allocated on the heap;
* the accumulator will take ownership. The caller should unset and free the
* #GValue if non-%NULL when finished with it.
*
* Returns: an allocated, initialised, set #GValue, or NULL
*/
GValue *
rhythmdb_entry_request_extra_metadata (RhythmDB *db,
RhythmDBEntry *entry,
const gchar *property_name)
{
GValue *value = NULL;
g_signal_emit (G_OBJECT (db),
rhythmdb_signals[ENTRY_EXTRA_METADATA_REQUEST],
g_quark_from_string (property_name),
entry,
&value);
return value;
}
/**
* rhythmdb_emit_entry_extra_metadata_notify:
* @db: a #RhythmDB
* @entry: a #RhythmDBEntry
* @property_name: the metadata predicate
* @metadata: a #GValue
*
* Emits a signal describing extra metadata for the @entry. The @property_name
* argument is emitted as the ::detail part of the
* "entry_extra_metadata_notify" signal and as the 'field' parameter. Handlers
* can ensure they only get metadata they are interested in by supplying an
* appropriate ::detail part when connecting to the signal. If handlers are
* interested in the metadata they should ref or copy the contents of @metadata
* and unref or free it when they are finished with it.
*/
void
rhythmdb_emit_entry_extra_metadata_notify (RhythmDB *db,
RhythmDBEntry *entry,
const gchar *property_name,
const GValue *metadata)
{
g_signal_emit (G_OBJECT (db),
rhythmdb_signals[ENTRY_EXTRA_METADATA_NOTIFY],
g_quark_from_string (property_name),
entry,
property_name,
metadata);
}
/**
* rhythmdb_entry_gather_metadata:
* @db: a #RhythmDB
* @entry: a #RhythmDBEntry
*
* Gathers all metadata for the @entry. The returned GHashTable maps property
* names and extra metadata names (described under
* @rhythmdb_entry_request_extra_metadata) to GValues. Anything wanting to
* provide extra metadata should connect to the "entry_extra_metadata_gather"
* signal.
*
* Returns: (transfer full): a RBStringValueMap containing metadata for the entry.
* This must be freed using g_object_unref.
*/
RBStringValueMap *
rhythmdb_entry_gather_metadata (RhythmDB *db,
RhythmDBEntry *entry)
{
RBStringValueMap *metadata;
GEnumClass *klass;
guint i;
metadata = rb_string_value_map_new ();
/* add core properties */
klass = g_type_class_ref (RHYTHMDB_TYPE_PROP_TYPE);
for (i = 0; i < klass->n_values; i++) {
GValue value = {0,};
gint prop;
GType value_type;
const char *name;
prop = klass->values[i].value;
/* only include easily marshallable types in the hash table */
value_type = rhythmdb_get_property_type (db, prop);
switch (value_type) {
case G_TYPE_STRING:
case G_TYPE_BOOLEAN:
case G_TYPE_ULONG:
case G_TYPE_UINT64:
case G_TYPE_DOUBLE:
break;
default:
continue;
}
/* skip deprecated properties */
switch (prop) {
case RHYTHMDB_PROP_TRACK_GAIN:
case RHYTHMDB_PROP_TRACK_PEAK:
case RHYTHMDB_PROP_ALBUM_GAIN:
case RHYTHMDB_PROP_ALBUM_PEAK:
continue;
default:
break;
}
g_value_init (&value, value_type);
rhythmdb_entry_get (db, entry, prop, &value);
name = (char *)rhythmdb_nice_elt_name_from_propid (db, prop);
rb_string_value_map_set (metadata, name, &value);
g_value_unset (&value);
}
g_type_class_unref (klass);
/* gather extra metadata */
g_signal_emit (G_OBJECT (db),
rhythmdb_signals[ENTRY_EXTRA_METADATA_GATHER], 0,
entry,
metadata);
return metadata;
}
/**
* rhythmdb_compute_status_normal:
* @n_songs: the number of tracks.
* @duration: the total duration of the tracks.
* @size: the total size of the tracks.
* @singular: singular form of the format string to use for entries (eg "%d song")
* @plural: plural form of the format string to use for entries (eg "%d songs")
*
* Creates a string containing the "status" information about a list of tracks.
* The singular and plural strings must be used in a direct ngettext call
* elsewhere in order for them to be marked for translation correctly.
*
* Returns: the string, which should be freed with g_free.
*/
char *
rhythmdb_compute_status_normal (gint n_songs,
glong duration,
guint64 size,
const char *singular,
const char *plural)
{
long days, hours, minutes;
char *songcount = NULL;
char *time = NULL;
char *size_str = NULL;
char *ret;
const char *minutefmt;
const char *hourfmt;
const char *dayfmt;
songcount = g_strdup_printf (ngettext (singular, plural, n_songs), n_songs);
days = duration / (60 * 60 * 24);
hours = (duration / (60 * 60)) - (days * 24);
minutes = (duration / 60) - ((days * 24 * 60) + (hours * 60));
minutefmt = ngettext ("%ld minute", "%ld minutes", minutes);
hourfmt = ngettext ("%ld hour", "%ld hours", hours);
dayfmt = ngettext ("%ld day", "%ld days", days);
if (days > 0) {
if (hours > 0)
if (minutes > 0) {
char *fmt;
/* Translators: the format is "X days, X hours and X minutes" */
fmt = g_strdup_printf (_("%s, %s and %s"), dayfmt, hourfmt, minutefmt);
time = g_strdup_printf (fmt, days, hours, minutes);
g_free (fmt);
} else {
char *fmt;
/* Translators: the format is "X days and X hours" */
fmt = g_strdup_printf (_("%s and %s"), dayfmt, hourfmt);
time = g_strdup_printf (fmt, days, hours);
g_free (fmt);
}
else
if (minutes > 0) {
char *fmt;
/* Translators: the format is "X days and X minutes" */
fmt = g_strdup_printf (_("%s and %s"), dayfmt, minutefmt);
time = g_strdup_printf (fmt, days, minutes);
g_free (fmt);
} else {
time = g_strdup_printf (dayfmt, days);
}
} else {
if (hours > 0) {
if (minutes > 0) {
char *fmt;
/* Translators: the format is "X hours and X minutes" */
fmt = g_strdup_printf (_("%s and %s"), hourfmt, minutefmt);
time = g_strdup_printf (fmt, hours, minutes);
g_free (fmt);
} else {
time = g_strdup_printf (hourfmt, hours);
}
} else {
time = g_strdup_printf (minutefmt, minutes);
}
}
size_str = g_format_size (size);
if (size > 0 && duration > 0) {
ret = g_strdup_printf ("%s, %s, %s", songcount, time, size_str);
} else if (duration > 0) {
ret = g_strdup_printf ("%s, %s", songcount, time);
} else if (size > 0) {
ret = g_strdup_printf ("%s, %s", songcount, size_str);
} else {
ret = g_strdup (songcount);
}
g_free (songcount);
g_free (time);
g_free (size_str);
return ret;
}
/**
* rhythmdb_register_entry_type:
* @db: the #RhythmDB
* @entry_type: the new entry type to register
*
* Registers a new entry type. An entry type must be registered before
* any entries can be created for it.
*/
void
rhythmdb_register_entry_type (RhythmDB *db, RhythmDBEntryType *entry_type)
{
RhythmDBClass *klass = RHYTHMDB_GET_CLASS (db);
char *name = NULL;
g_object_get (entry_type, "name", &name, NULL);
g_assert (name != NULL);
g_mutex_lock (&db->priv->entry_type_map_mutex);
g_hash_table_insert (db->priv->entry_type_map, name, g_object_ref (entry_type));
g_mutex_unlock (&db->priv->entry_type_map_mutex);
if (klass->impl_entry_type_registered)
klass->impl_entry_type_registered (db, entry_type);
}
/**
* rhythmdb_entry_type_foreach:
* @db: a #RhythmDB
* @func: callback function to call for each registered entry type
* @data: data to pass to the callback
*
* Calls a function for each registered entry type.
*/
void
rhythmdb_entry_type_foreach (RhythmDB *db,
GHFunc func,
gpointer data)
{
g_mutex_lock (&db->priv->entry_type_mutex);
g_hash_table_foreach (db->priv->entry_type_map, func, data);
g_mutex_unlock (&db->priv->entry_type_mutex);
}
/**
* rhythmdb_entry_type_get_by_name:
* @db: a #RhythmDB
* @name: name of the type to look for
*
* Locates a #RhythmDBEntryType by name. Returns NULL if no entry
* type is registered with the specified name.
*
* Returns: (transfer none): the #RhythmDBEntryType
*/
RhythmDBEntryType *
rhythmdb_entry_type_get_by_name (RhythmDB *db,
const char *name)
{
gpointer t = NULL;
g_mutex_lock (&db->priv->entry_type_map_mutex);
if (db->priv->entry_type_map) {
t = g_hash_table_lookup (db->priv->entry_type_map, name);
}
g_mutex_unlock (&db->priv->entry_type_map_mutex);
return (RhythmDBEntryType *) t;
}
static void
rhythmdb_entry_set_mount_point (RhythmDB *db,
RhythmDBEntry *entry,
const gchar *realuri)
{
gchar *mount_point;
GValue value = {0, };
mount_point = rb_uri_get_mount_point (realuri);
if (mount_point != NULL) {
g_value_init (&value, G_TYPE_STRING);
g_value_take_string (&value, mount_point);
rhythmdb_entry_set_internal (db, entry, FALSE,
RHYTHMDB_PROP_MOUNTPOINT,
&value);
g_value_unset (&value);
}
}
void
rhythmdb_entry_set_visibility (RhythmDB *db,
RhythmDBEntry *entry,
gboolean visible)
{
GValue old_val = {0, };
gboolean old_visible;
g_return_if_fail (RHYTHMDB_IS (db));
g_return_if_fail (entry != NULL);
g_value_init (&old_val, G_TYPE_BOOLEAN);
rhythmdb_entry_get (db, entry, RHYTHMDB_PROP_HIDDEN, &old_val);
old_visible = !g_value_get_boolean (&old_val);
if ((old_visible && !visible) || (!old_visible && visible)) {
GValue new_val = {0, };
g_value_init (&new_val, G_TYPE_BOOLEAN);
g_value_set_boolean (&new_val, !visible);
rhythmdb_entry_set_internal (db, entry, TRUE,
RHYTHMDB_PROP_HIDDEN, &new_val);
g_value_unset (&new_val);
}
g_value_unset (&old_val);
}
static gboolean
rhythmdb_idle_save (RhythmDB *db)
{
if (db->priv->dirty) {
rb_debug ("database is dirty, doing regular save");
rhythmdb_save_async (db);
}
return TRUE;
}
static void
rhythmdb_sync_library_location (RhythmDB *db)
{
if (db->priv->library_locations != NULL &&
g_strv_length (db->priv->library_locations) > 0) {
rb_debug ("ending monitor of old library directories");
rhythmdb_stop_monitoring (db);
g_strfreev (db->priv->library_locations);
db->priv->library_locations = NULL;
}
if (g_settings_get_boolean (db->priv->settings, "monitor-library")) {
rb_debug ("starting library monitoring");
db->priv->library_locations = g_settings_get_strv (db->priv->settings, "locations");
rhythmdb_start_monitoring (db);
}
}
static void
db_settings_changed_cb (GSettings *settings, const char *key, RhythmDB *db)
{
if (g_strcmp0 (key, "locations") == 0 || g_strcmp0 (key, "monitor-library") == 0) {
rhythmdb_sync_library_location (db);
}
}
char *
rhythmdb_entry_dup_string (RhythmDBEntry *entry,
RhythmDBPropType propid)
{
const char *s;
g_return_val_if_fail (entry != NULL, NULL);
s = rhythmdb_entry_get_string (entry, propid);
if (s != NULL) {
return g_strdup (s);
} else {
return NULL;
}
}
/**
* rhythmdb_entry_get_string:
* @entry: a #RhythmDBEntry
* @propid: the #RhythmDBPropType to return
*
* Returns the value of a string property of #entry.
*
* Return value: property value, must not be freed
*/
const char *
rhythmdb_entry_get_string (RhythmDBEntry *entry,
RhythmDBPropType propid)
{
RhythmDBPodcastFields *podcast = NULL;
g_return_val_if_fail (entry != NULL, NULL);
g_return_val_if_fail (entry->refcount > 0, NULL);
if (entry->type == RHYTHMDB_ENTRY_TYPE_PODCAST_FEED ||
entry->type == RHYTHMDB_ENTRY_TYPE_PODCAST_POST ||
entry->type == RHYTHMDB_ENTRY_TYPE_PODCAST_SEARCH)
podcast = RHYTHMDB_ENTRY_GET_TYPE_DATA (entry, RhythmDBPodcastFields);
rhythmdb_entry_sync_mirrored (entry, propid);
switch (propid) {
case RHYTHMDB_PROP_TITLE:
return rb_refstring_get (entry->title);
case RHYTHMDB_PROP_ALBUM:
return rb_refstring_get (entry->album);
case RHYTHMDB_PROP_ARTIST:
return rb_refstring_get (entry->artist);
case RHYTHMDB_PROP_GENRE:
return rb_refstring_get (entry->genre);
case RHYTHMDB_PROP_COMMENT:
return rb_refstring_get (entry->comment);
case RHYTHMDB_PROP_MUSICBRAINZ_TRACKID:
return rb_refstring_get (entry->musicbrainz_trackid);
case RHYTHMDB_PROP_MUSICBRAINZ_ARTISTID:
return rb_refstring_get (entry->musicbrainz_artistid);
case RHYTHMDB_PROP_MUSICBRAINZ_ALBUMID:
return rb_refstring_get (entry->musicbrainz_albumid);
case RHYTHMDB_PROP_MUSICBRAINZ_ALBUMARTISTID:
return rb_refstring_get (entry->musicbrainz_albumartistid);
case RHYTHMDB_PROP_ARTIST_SORTNAME:
return rb_refstring_get (entry->artist_sortname);
case RHYTHMDB_PROP_ALBUM_SORTNAME:
return rb_refstring_get (entry->album_sortname);
case RHYTHMDB_PROP_ALBUM_ARTIST:
return rb_refstring_get (entry->album_artist);
case RHYTHMDB_PROP_ALBUM_ARTIST_SORTNAME:
return rb_refstring_get (entry->album_artist_sortname);
case RHYTHMDB_PROP_COMPOSER:
return rb_refstring_get (entry->composer);
case RHYTHMDB_PROP_COMPOSER_SORTNAME:
return rb_refstring_get (entry->composer_sortname);
case RHYTHMDB_PROP_MEDIA_TYPE:
return rb_refstring_get (entry->media_type);
case RHYTHMDB_PROP_TITLE_SORT_KEY:
return rb_refstring_get_sort_key (entry->title);
case RHYTHMDB_PROP_ALBUM_SORT_KEY:
return rb_refstring_get_sort_key (entry->album);
case RHYTHMDB_PROP_ARTIST_SORT_KEY:
return rb_refstring_get_sort_key (entry->artist);
case RHYTHMDB_PROP_GENRE_SORT_KEY:
return rb_refstring_get_sort_key (entry->genre);
case RHYTHMDB_PROP_ARTIST_SORTNAME_SORT_KEY:
return rb_refstring_get_sort_key (entry->artist_sortname);
case RHYTHMDB_PROP_ALBUM_SORTNAME_SORT_KEY:
return rb_refstring_get_sort_key (entry->album_sortname);
case RHYTHMDB_PROP_ALBUM_ARTIST_SORT_KEY:
return rb_refstring_get_sort_key (entry->album_artist);
case RHYTHMDB_PROP_ALBUM_ARTIST_SORTNAME_SORT_KEY:
return rb_refstring_get_sort_key (entry->album_artist_sortname);
case RHYTHMDB_PROP_COMPOSER_SORT_KEY:
return rb_refstring_get_sort_key (entry->composer);
case RHYTHMDB_PROP_COMPOSER_SORTNAME_SORT_KEY:
return rb_refstring_get_sort_key (entry->composer_sortname);
case RHYTHMDB_PROP_TITLE_FOLDED:
return rb_refstring_get_folded (entry->title);
case RHYTHMDB_PROP_ALBUM_FOLDED:
return rb_refstring_get_folded (entry->album);
case RHYTHMDB_PROP_ARTIST_FOLDED:
return rb_refstring_get_folded (entry->artist);
case RHYTHMDB_PROP_GENRE_FOLDED:
return rb_refstring_get_folded (entry->genre);
case RHYTHMDB_PROP_ARTIST_SORTNAME_FOLDED:
return rb_refstring_get_folded (entry->artist_sortname);
case RHYTHMDB_PROP_ALBUM_SORTNAME_FOLDED:
return rb_refstring_get_folded (entry->album_sortname);
case RHYTHMDB_PROP_ALBUM_ARTIST_FOLDED:
return rb_refstring_get_folded (entry->album_artist);
case RHYTHMDB_PROP_ALBUM_ARTIST_SORTNAME_FOLDED:
return rb_refstring_get_folded (entry->album_artist_sortname);
case RHYTHMDB_PROP_COMPOSER_FOLDED:
return rb_refstring_get_folded (entry->composer);
case RHYTHMDB_PROP_COMPOSER_SORTNAME_FOLDED:
return rb_refstring_get_folded (entry->composer_sortname);
case RHYTHMDB_PROP_LOCATION:
return rb_refstring_get (entry->location);
case RHYTHMDB_PROP_MOUNTPOINT:
return rb_refstring_get (entry->mountpoint);
case RHYTHMDB_PROP_LAST_PLAYED_STR:
return rb_refstring_get (entry->last_played_str);
case RHYTHMDB_PROP_PLAYBACK_ERROR:
return rb_refstring_get (entry->playback_error);
case RHYTHMDB_PROP_FIRST_SEEN_STR:
return rb_refstring_get (entry->first_seen_str);
case RHYTHMDB_PROP_LAST_SEEN_STR:
return rb_refstring_get (entry->last_seen_str);
/* synthetic properties */
case RHYTHMDB_PROP_SEARCH_MATCH:
return NULL;
case RHYTHMDB_PROP_KEYWORD:
return NULL;
/* Podcast properties */
case RHYTHMDB_PROP_DESCRIPTION:
if (podcast)
return rb_refstring_get (podcast->description);
else
return NULL;
case RHYTHMDB_PROP_SUBTITLE:
if (podcast)
return rb_refstring_get (podcast->subtitle);
else
return NULL;
case RHYTHMDB_PROP_SUMMARY:
return NULL;
case RHYTHMDB_PROP_LANG:
if (podcast)
return rb_refstring_get (podcast->lang);
else
return NULL;
case RHYTHMDB_PROP_COPYRIGHT:
if (podcast)
return rb_refstring_get (podcast->copyright);
else
return NULL;
case RHYTHMDB_PROP_IMAGE:
if (podcast)
return rb_refstring_get (podcast->image);
else
return NULL;
case RHYTHMDB_PROP_PODCAST_GUID:
if (podcast)
return rb_refstring_get (podcast->guid);
else
return NULL;
default:
g_assert_not_reached ();
return NULL;
}
}
/**
* rhythmdb_entry_get_refstring:
* @entry: a #RhythmDBEntry
* @propid: the property to return
*
* Returns an #RBRefString containing a string property of @entry.
*
* Return value: a #RBRefString, must be unreffed by caller.
*/
RBRefString *
rhythmdb_entry_get_refstring (RhythmDBEntry *entry,
RhythmDBPropType propid)
{
g_return_val_if_fail (entry != NULL, NULL);
g_return_val_if_fail (entry->refcount > 0, NULL);
rhythmdb_entry_sync_mirrored (entry, propid);
switch (propid) {
case RHYTHMDB_PROP_TITLE:
return rb_refstring_ref (entry->title);
case RHYTHMDB_PROP_ALBUM:
return rb_refstring_ref (entry->album);
case RHYTHMDB_PROP_ARTIST:
return rb_refstring_ref (entry->artist);
case RHYTHMDB_PROP_ALBUM_ARTIST:
return rb_refstring_ref (entry->album_artist);
case RHYTHMDB_PROP_COMPOSER:
return rb_refstring_ref (entry->composer);
case RHYTHMDB_PROP_GENRE:
return rb_refstring_ref (entry->genre);
case RHYTHMDB_PROP_COMMENT:
return rb_refstring_ref (entry->comment);
case RHYTHMDB_PROP_MUSICBRAINZ_TRACKID:
return rb_refstring_ref (entry->musicbrainz_trackid);
case RHYTHMDB_PROP_MUSICBRAINZ_ARTISTID:
return rb_refstring_ref (entry->musicbrainz_artistid);
case RHYTHMDB_PROP_MUSICBRAINZ_ALBUMID:
return rb_refstring_ref (entry->musicbrainz_albumid);
case RHYTHMDB_PROP_MUSICBRAINZ_ALBUMARTISTID:
return rb_refstring_ref (entry->musicbrainz_albumartistid);
case RHYTHMDB_PROP_ARTIST_SORTNAME:
return rb_refstring_ref (entry->artist_sortname);
case RHYTHMDB_PROP_ALBUM_SORTNAME:
return rb_refstring_ref (entry->album_sortname);
case RHYTHMDB_PROP_ALBUM_ARTIST_SORTNAME:
return rb_refstring_ref (entry->album_artist_sortname);
case RHYTHMDB_PROP_COMPOSER_SORTNAME:
return rb_refstring_ref (entry->composer_sortname);
case RHYTHMDB_PROP_MEDIA_TYPE:
return rb_refstring_ref (entry->media_type);
case RHYTHMDB_PROP_MOUNTPOINT:
return rb_refstring_ref (entry->mountpoint);
case RHYTHMDB_PROP_LAST_PLAYED_STR:
return rb_refstring_ref (entry->last_played_str);
case RHYTHMDB_PROP_FIRST_SEEN_STR:
return rb_refstring_ref (entry->first_seen_str);
case RHYTHMDB_PROP_LAST_SEEN_STR:
return rb_refstring_ref (entry->last_seen_str);
case RHYTHMDB_PROP_LOCATION:
return rb_refstring_ref (entry->location);
case RHYTHMDB_PROP_PLAYBACK_ERROR:
return rb_refstring_ref (entry->playback_error);
default:
g_assert_not_reached ();
return NULL;
}
}
/**
* rhythmdb_entry_get_boolean:
* @entry: a #RhythmDBEntry
* @propid: property to return
*
* Returns the value of a boolean property of @entry.
*
* Return value: property value
*/
gboolean
rhythmdb_entry_get_boolean (RhythmDBEntry *entry,
RhythmDBPropType propid)
{
g_return_val_if_fail (entry != NULL, FALSE);
switch (propid) {
case RHYTHMDB_PROP_HIDDEN:
return ((entry->flags & RHYTHMDB_ENTRY_HIDDEN) != 0);
default:
g_assert_not_reached ();
return FALSE;
}
}
/**
* rhythmdb_entry_get_uint64:
* @entry: a #RhythmDBEntry
* @propid: property to return
*
* Returns the value of a 64bit unsigned integer property.
*
* Return value: property value
*/
guint64
rhythmdb_entry_get_uint64 (RhythmDBEntry *entry,
RhythmDBPropType propid)
{
g_return_val_if_fail (entry != NULL, 0);
switch (propid) {
case RHYTHMDB_PROP_FILE_SIZE:
return entry->file_size;
default:
g_assert_not_reached ();
return 0;
}
}
/**
* rhythmdb_entry_get_entry_type:
* @entry: a #RhythmDBEntry
*
* Returns the #RhythmDBEntryType for @entry. This is used to access
* entry type properties, to check that entries are of the same type,
* and to call entry type methods.
*
* Return value: (transfer none): the #RhythmDBEntryType for @entry
*/
RhythmDBEntryType *
rhythmdb_entry_get_entry_type (RhythmDBEntry *entry)
{
g_return_val_if_fail (entry != NULL, NULL);
return entry->type;
}
/**
* rhythmdb_entry_get_object:
* @entry: a #RhythmDBEntry
* @propid: the property to return
*
* Returns the value of an object property of @entry.
*
* Return value: (transfer none): property value
*/
GObject *
rhythmdb_entry_get_object (RhythmDBEntry *entry,
RhythmDBPropType propid)
{
g_return_val_if_fail (entry != NULL, NULL);
switch (propid) {
case RHYTHMDB_PROP_TYPE:
return G_OBJECT (entry->type);
default:
g_assert_not_reached ();
return NULL;
}
}
/**
* rhythmdb_entry_get_ulong:
* @entry: a #RhythmDBEntry
* @propid: property to return
*
* Returns the value of an unsigned long integer property of @entry.
*
* Return value: property value
*/
gulong
rhythmdb_entry_get_ulong (RhythmDBEntry *entry,
RhythmDBPropType propid)
{
RhythmDBPodcastFields *podcast = NULL;
g_return_val_if_fail (entry != NULL, 0);
if (entry->type == RHYTHMDB_ENTRY_TYPE_PODCAST_FEED ||
entry->type == RHYTHMDB_ENTRY_TYPE_PODCAST_POST ||
entry->type == RHYTHMDB_ENTRY_TYPE_PODCAST_SEARCH)
podcast = RHYTHMDB_ENTRY_GET_TYPE_DATA (entry, RhythmDBPodcastFields);
switch (propid) {
case RHYTHMDB_PROP_ENTRY_ID:
return entry->id;
case RHYTHMDB_PROP_TRACK_NUMBER:
return entry->tracknum;
case RHYTHMDB_PROP_TRACK_TOTAL:
return entry->tracktotal;
case RHYTHMDB_PROP_DISC_NUMBER:
return entry->discnum;
case RHYTHMDB_PROP_DISC_TOTAL:
return entry->disctotal;
case RHYTHMDB_PROP_DURATION:
return entry->duration;
case RHYTHMDB_PROP_MTIME:
return entry->mtime;
case RHYTHMDB_PROP_FIRST_SEEN:
return entry->first_seen;
case RHYTHMDB_PROP_LAST_SEEN:
return entry->last_seen;
case RHYTHMDB_PROP_LAST_PLAYED:
return entry->last_played;
case RHYTHMDB_PROP_PLAY_COUNT:
return entry->play_count;
case RHYTHMDB_PROP_BITRATE:
return entry->bitrate;
case RHYTHMDB_PROP_DATE:
if (g_date_valid (&entry->date))
return g_date_get_julian (&entry->date);
else
return 0;
case RHYTHMDB_PROP_YEAR:
if (g_date_valid (&entry->date))
return g_date_get_year (&entry->date);
else
return 0;
case RHYTHMDB_PROP_POST_TIME:
if (podcast)
return podcast->post_time;
else
return 0;
case RHYTHMDB_PROP_STATUS:
if (podcast)
return podcast->status;
else
return 0;
default:
g_assert_not_reached ();
return 0;
}
}
/**
* rhythmdb_entry_get_double:
* @entry: a #RhythmDBEntry
* @propid: the property to return
*
* Returns the value of a double-precision floating point property of @value.
*
* Return value: property value
*/
double
rhythmdb_entry_get_double (RhythmDBEntry *entry,
RhythmDBPropType propid)
{
g_return_val_if_fail (entry != NULL, 0);
switch (propid) {
case RHYTHMDB_PROP_TRACK_GAIN:
g_warning ("RHYTHMDB_PROP_TRACK_GAIN no longer supported");
return 0.0;
case RHYTHMDB_PROP_TRACK_PEAK:
g_warning ("RHYTHMDB_PROP_TRACK_PEAK no longer supported");
return 1.0;
case RHYTHMDB_PROP_ALBUM_GAIN:
g_warning ("RHYTHMDB_PROP_ALBUM_GAIN no longer supported");
return 0.0;
case RHYTHMDB_PROP_ALBUM_PEAK:
g_warning ("RHYTHMDB_PROP_ALBUM_PEAK no longer supported");
return 1.0;
case RHYTHMDB_PROP_RATING:
return entry->rating;
case RHYTHMDB_PROP_BPM:
return entry->bpm;
default:
g_assert_not_reached ();
return 0.0;
}
}
/**
* rhythmdb_entry_keyword_add:
* @db: the #RhythmDB
* @entry: a #RhythmDBEntry.
* @keyword: the keyword to add.
*
* Adds a keyword to an entry.
*
* Returns: whether the keyword was already on the entry
*/
gboolean
rhythmdb_entry_keyword_add (RhythmDB *db,
RhythmDBEntry *entry,
RBRefString *keyword)
{
RhythmDBClass *klass = RHYTHMDB_GET_CLASS (db);
gboolean ret;
ret = klass->impl_entry_keyword_add (db, entry, keyword);
if (!ret) {
g_signal_emit (G_OBJECT (db), rhythmdb_signals[ENTRY_KEYWORD_ADDED], 0, entry, keyword);
}
return ret;
}
/**
* rhythmdb_entry_keyword_remove:
* @db: the #RhythmDB
* @entry: a #RhythmDBEntry.
* @keyword: the keyword to remove.
*
* Removed a keyword from an entry.
*
* Returns: whether the keyword had previously been added to the entry.
*/
gboolean
rhythmdb_entry_keyword_remove (RhythmDB *db,
RhythmDBEntry *entry,
RBRefString *keyword)
{
RhythmDBClass *klass = RHYTHMDB_GET_CLASS (db);
gboolean ret;
ret = klass->impl_entry_keyword_remove (db, entry, keyword);
if (ret) {
g_signal_emit (G_OBJECT (db), rhythmdb_signals[ENTRY_KEYWORD_REMOVED], 0, entry, keyword);
}
return ret;
}
/**
* rhythmdb_entry_keyword_has:
* @db: the #RhythmDB
* @entry: a #RhythmDBEntry.
* @keyword: the keyword to check for.
*
* Checks whether a keyword is has been added to an entry.
*
* Returns: whether the keyword had been added to the entry.
*/
gboolean
rhythmdb_entry_keyword_has (RhythmDB *db,
RhythmDBEntry *entry,
RBRefString *keyword)
{
RhythmDBClass *klass = RHYTHMDB_GET_CLASS (db);
return klass->impl_entry_keyword_has (db, entry, keyword);
}
/**
* rhythmdb_entry_keywords_get:
* @db: the #RhythmDB
* @entry: a #RhythmDBEntry.
*
* Gets the list ofkeywords that have been added to an entry.
*
* Returns: (element-type RBRefString) (transfer full): the list of keywords
* that have been added to the entry.
*/
GList*
rhythmdb_entry_keywords_get (RhythmDB *db,
RhythmDBEntry *entry)
{
RhythmDBClass *klass = RHYTHMDB_GET_CLASS (db);
return klass->impl_entry_keywords_get (db, entry);
}
/**
* rhythmdb_entry_write_metadata_changes:
* @db: the #RhythmDB
* @entry: the #RhythmDBEntry to update
* @changes: (element-type RB.RhythmDBEntryChange): a list of changes to write
* @error: returns error information
*
* This can be called from a #RhythmDBEntryType sync_metadata function
* when the appropriate action is to write the metadata changes
* to the file at the entry's location.
*/
void
rhythmdb_entry_write_metadata_changes (RhythmDB *db,
RhythmDBEntry *entry,
GSList *changes,
GError **error)
{
const char *uri;
GError *local_error = NULL;
GSList *t;
uri = rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_LOCATION);
rb_metadata_reset (db->priv->metadata);
for (t = changes; t; t = t->next) {
RBMetaDataField field;
GValue val = {0,};
RhythmDBEntryChange *change = (RhythmDBEntryChange *)t->data;
if (metadata_field_from_prop (change->prop, &field) == FALSE) {
continue;
}
g_value_init (&val, rhythmdb_get_property_type (db, change->prop));
rhythmdb_entry_get (db, entry, change->prop, &val);
rb_metadata_set (db->priv->metadata, field, &val);
g_value_unset (&val);
}
rb_metadata_save (db->priv->metadata, uri, &local_error);
if (local_error != NULL) {
RhythmDBAction *load_action;
/* reload the metadata, to revert the db changes */
rb_debug ("error saving metadata for %s: %s; reloading metadata to revert",
rb_refstring_get (entry->location),
local_error->message);
load_action = g_slice_new0 (RhythmDBAction);
load_action->type = RHYTHMDB_ACTION_LOAD;
load_action->uri = rb_refstring_ref (entry->location);
load_action->data.types.entry_type = rhythmdb_entry_get_entry_type (entry);
g_async_queue_push (db->priv->action_queue, load_action);
g_propagate_error (error, local_error);
}
}
/**
* rhythmdb_get_property_type:
* @db: the #RhythmDB
* @property_id: a property ID (#RhythmDBPropType)
*
* Returns the #GType for the value of the property.
*
* Return value: property value type
*/
GType
rhythmdb_get_property_type (RhythmDB *db,
guint property_id)
{
g_assert (property_id >= 0 && property_id < RHYTHMDB_NUM_PROPERTIES);
return rhythmdb_properties[property_id].prop_type;
}
/**
* rhythmdb_entry_get_type:
*
* Returns the #GType for #RhythmDBEntry. The #GType for #RhythmDBEntry is a
* boxed type, where copying the value references the entry and freeing it
* unrefs it.
*
* Return value: value type
*/
GType
rhythmdb_entry_get_type (void)
{
static GType type = 0;
if (G_UNLIKELY (type == 0)) {
type = g_boxed_type_register_static ("RhythmDBEntry",
(GBoxedCopyFunc)rhythmdb_entry_ref,
(GBoxedFreeFunc)rhythmdb_entry_unref);
}
return type;
}
/**
* rhythmdb_entry_change_get_type:
*
* Returns the #GType for #RhythmDBEntryChange. #RhythmDBEntryChange is stored as a
* boxed value. Copying the value copies the full change, including old and new values.
*
* Return value: entry change value type
*/
GType
rhythmdb_entry_change_get_type (void)
{
static GType type = 0;
if (G_UNLIKELY (type == 0)) {
type = g_boxed_type_register_static ("RhythmDBEntryChange",
(GBoxedCopyFunc)rhythmdb_entry_change_copy,
(GBoxedFreeFunc)rhythmdb_entry_change_free);
}
return type;
}
/**
* rhythmdb_entry_is_lossless:
* @entry: a #RhythmDBEntry
*
* Checks if @entry represents a file that is losslessly encoded.
* An entry is considered lossless if it has no bitrate value and
* its media type is "audio/x-flac". Other lossless encoding types
* may be added in the future.
*
* Return value: %TRUE if @entry is lossless
*/
gboolean
rhythmdb_entry_is_lossless (RhythmDBEntry *entry)
{
const char *media_type;
if (rhythmdb_entry_get_ulong (entry, RHYTHMDB_PROP_BITRATE) != 0)
return FALSE;
media_type = rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_MEDIA_TYPE);
return rb_gst_media_type_is_lossless (media_type);
}
/**
* rhythmdb_entry_matches_ext_db_key:
* @db: #RhythmDB instance
* @entry: a #RhythmDBEntry
* @key: a #RBExtDBKey
*
* Checks whether @key matches @entry.
*
* Return value: %TRUE if the key matches the entry
*/
gboolean
rhythmdb_entry_matches_ext_db_key (RhythmDB *db, RhythmDBEntry *entry, RBExtDBKey *key)
{
char **fields;
int i;
fields = rb_ext_db_key_get_field_names (key);
for (i = 0; fields[i] != NULL; i++) {
RhythmDBPropType prop;
RhythmDBPropType extra_prop;
const char *v;
prop = rhythmdb_propid_from_nice_elt_name (db, (const xmlChar *)fields[i]);
if (prop == -1) {
if (rb_ext_db_key_field_matches (key, fields[i], NULL) == FALSE) {
g_strfreev (fields);
return FALSE;
}
continue;
}
/* check additional values for some fields */
switch (prop) {
case RHYTHMDB_PROP_ARTIST:
extra_prop = RHYTHMDB_PROP_ALBUM_ARTIST;
break;
default:
extra_prop = -1;
break;
}
if (extra_prop != -1) {
v = rhythmdb_entry_get_string (entry, extra_prop);
if (rb_ext_db_key_field_matches (key, fields[i], v))
continue;
}
v = rhythmdb_entry_get_string (entry, prop);
if (rb_ext_db_key_field_matches (key, fields[i], v) == FALSE) {
g_strfreev (fields);
return FALSE;
}
}
g_strfreev (fields);
return TRUE;
}
|