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
|
/*
* e-source-registry-server.c
*
* This library is free software you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* SECTION: e-source-registry-server
* @include: libebackend/libebackend.h
* @short_description: Server-side repository for data sources
*
* The #ESourceRegistryServer is the heart of the registry D-Bus service.
* Acting as a global singleton store for all #EServerSideSource instances,
* its responsibilities include loading data source content from key files,
* exporting data sources to clients over D-Bus, handling authentication
* and content change requests from clients, and saving content changes
* back to key files.
*
* It also hosts any number of built-in or 3rd party data source collection
* backends, which coordinate with #ESourceRegistryServer to automatically
* advertise available data sources on a remote server.
**/
#include "e-source-registry-server.h"
#include <config.h>
#include <string.h>
#include <glib/gi18n-lib.h>
/* Private D-Bus classes. */
#include <e-dbus-source.h>
#include <e-dbus-source-manager.h>
#include <libebackend/e-authentication-mediator.h>
#include <libebackend/e-authentication-session.h>
#include <libebackend/e-server-side-source.h>
#define E_SOURCE_REGISTRY_SERVER_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE \
((obj), E_TYPE_SOURCE_REGISTRY_SERVER, ESourceRegistryServerPrivate))
/* Collection backends get tacked on to
* sources with a [Collection] extension. */
#define BACKEND_DATA_KEY "__e_collection_backend__"
typedef struct _AuthRequest AuthRequest;
struct _ESourceRegistryServerPrivate {
GMainContext *main_context;
GDBusObjectManagerServer *object_manager;
EDBusSourceManager *source_manager;
GHashTable *sources; /* sources added to hierarchy */
GHashTable *orphans; /* sources waiting for parent */
GHashTable *monitors;
GMutex sources_lock;
GMutex orphans_lock;
/* In pseudo-Python notation:
*
* running_auths = { UID : AuthRequest }
* waiting_auths = { UID : [ AuthRequest, ... ] }
*
* We process all authenticators for a given source UID at once.
* The thought being after the first authenticator for a given UID
* completes (the first being most likely to trigger a user prompt),
* then any other authenticators for that same UID should complete
* quickly, hopefully without having to reprompt. That is unless
* the user decides not to cache the secret at all, in which case
* he gets what he asked for: lots of annoying prompts.
*/
GMutex auth_lock;
GHashTable *running_auths;
GHashTable *waiting_auths;
guint authentication_count;
};
struct _AuthRequest {
volatile gint ref_count;
EAuthenticationSession *session;
GSimpleAsyncResult *simple;
ESource *source; /* may be NULL */
GCancellable *cancellable;
};
enum {
LOAD_ERROR,
FILES_LOADED,
SOURCE_ADDED,
SOURCE_REMOVED,
TWEAK_KEY_FILE,
LAST_SIGNAL
};
/* Forward Declarations */
static void source_registry_server_maybe_start_auth_session
(ESourceRegistryServer *server,
const gchar *uid);
static guint signals[LAST_SIGNAL];
G_DEFINE_TYPE (
ESourceRegistryServer,
e_source_registry_server,
E_TYPE_DATA_FACTORY)
static AuthRequest *
auth_request_new (EAuthenticationSession *session,
GSimpleAsyncResult *simple,
GCancellable *cancellable)
{
ESourceRegistryServer *server;
AuthRequest *request;
const gchar *uid;
server = e_authentication_session_get_server (session);
uid = e_authentication_session_get_source_uid (session);
request = g_slice_new0 (AuthRequest);
request->ref_count = 1;
request->session = g_object_ref (session);
request->simple = g_object_ref (simple);
/* This will return NULL if the authenticating data source
* has not yet been submitted to the D-Bus registry server. */
request->source = e_source_registry_server_ref_source (server, uid);
if (G_IS_CANCELLABLE (cancellable))
request->cancellable = g_object_ref (cancellable);
return request;
}
static AuthRequest *
auth_request_ref (AuthRequest *request)
{
g_return_val_if_fail (request != NULL, NULL);
g_return_val_if_fail (request->ref_count > 0, NULL);
g_atomic_int_inc (&request->ref_count);
return request;
}
static void
auth_request_unref (AuthRequest *request)
{
g_return_if_fail (request != NULL);
g_return_if_fail (request->ref_count > 0);
if (g_atomic_int_dec_and_test (&request->ref_count)) {
g_object_unref (request->session);
g_object_unref (request->simple);
if (request->source != NULL)
g_object_unref (request->source);
if (request->cancellable != NULL)
g_object_unref (request->cancellable);
g_slice_free (AuthRequest, request);
}
}
/* GDestroyNotify callback for 'sources' values */
static void
unref_data_source (ESource *source)
{
/* The breaks the reference cycle with ECollectionBackend. */
g_object_set_data (G_OBJECT (source), BACKEND_DATA_KEY, NULL);
g_object_unref (source);
}
/* GDestroyNotify callback for 'waiting_auths' values */
static void
free_auth_queue (GQueue *queue)
{
/* XXX g_queue_clear_full() would be nice here. */
while (!g_queue_is_empty (queue))
g_object_unref (g_queue_pop_head (queue));
g_queue_free (queue);
}
static void
source_registry_server_sources_insert (ESourceRegistryServer *server,
ESource *source)
{
const gchar *uid;
uid = e_source_get_uid (source);
g_return_if_fail (uid != NULL);
g_mutex_lock (&server->priv->sources_lock);
g_hash_table_insert (
server->priv->sources,
g_strdup (uid), g_object_ref (source));
g_mutex_unlock (&server->priv->sources_lock);
}
static gboolean
source_registry_server_sources_remove (ESourceRegistryServer *server,
ESource *source)
{
const gchar *uid;
gboolean removed;
uid = e_source_get_uid (source);
g_return_val_if_fail (uid != NULL, FALSE);
g_mutex_lock (&server->priv->sources_lock);
removed = g_hash_table_remove (server->priv->sources, uid);
g_mutex_unlock (&server->priv->sources_lock);
return removed;
}
static ESource *
source_registry_server_sources_lookup (ESourceRegistryServer *server,
const gchar *uid)
{
ESource *source;
g_return_val_if_fail (uid != NULL, NULL);
g_mutex_lock (&server->priv->sources_lock);
source = g_hash_table_lookup (server->priv->sources, uid);
if (source != NULL)
g_object_ref (source);
g_mutex_unlock (&server->priv->sources_lock);
return source;
}
static GList *
source_registry_server_sources_get_values (ESourceRegistryServer *server)
{
GList *values;
g_mutex_lock (&server->priv->sources_lock);
values = g_hash_table_get_values (server->priv->sources);
g_list_foreach (values, (GFunc) g_object_ref, NULL);
g_mutex_unlock (&server->priv->sources_lock);
return values;
}
static void
source_registry_server_orphans_insert (ESourceRegistryServer *server,
ESource *orphan_source)
{
GHashTable *orphans;
GPtrArray *array;
gchar *parent_uid;
g_mutex_lock (&server->priv->orphans_lock);
orphans = server->priv->orphans;
parent_uid = e_source_dup_parent (orphan_source);
/* A top-level object has no parent UID, so we
* use a special "empty" key in the hash table. */
if (parent_uid == NULL)
parent_uid = g_strdup ("");
array = g_hash_table_lookup (orphans, parent_uid);
if (array == NULL) {
array = g_ptr_array_new_with_free_func (g_object_unref);
/* Takes ownership of the 'parent_uid' string. */
g_hash_table_insert (orphans, parent_uid, array);
parent_uid = NULL;
}
g_ptr_array_add (array, g_object_ref (orphan_source));
g_free (parent_uid);
g_mutex_unlock (&server->priv->orphans_lock);
}
static gboolean
source_registry_server_orphans_remove (ESourceRegistryServer *server,
ESource *orphan_source)
{
GHashTable *orphans;
GPtrArray *array;
gchar *parent_uid;
gboolean removed = FALSE;
g_mutex_lock (&server->priv->orphans_lock);
orphans = server->priv->orphans;
parent_uid = e_source_dup_parent (orphan_source);
/* A top-level object has no parent UID, so we
* use a special "empty" key in the hash table. */
if (parent_uid == NULL)
parent_uid = g_strdup ("");
array = g_hash_table_lookup (orphans, parent_uid);
if (array != NULL) {
/* Array is not ordered, so use "remove_fast". */
removed = g_ptr_array_remove_fast (array, orphan_source);
}
g_free (parent_uid);
g_mutex_unlock (&server->priv->orphans_lock);
return removed;
}
static GPtrArray *
source_registry_server_orphans_steal (ESourceRegistryServer *server,
ESource *parent_source)
{
GHashTable *orphans;
GPtrArray *array;
const gchar *parent_uid;
parent_uid = e_source_get_uid (parent_source);
g_return_val_if_fail (parent_uid != NULL, NULL);
g_mutex_lock (&server->priv->orphans_lock);
orphans = server->priv->orphans;
array = g_hash_table_lookup (orphans, parent_uid);
/* g_hash_table_remove() will unreference the array,
* so we need to reference it first to keep it alive. */
if (array != NULL) {
g_ptr_array_ref (array);
g_hash_table_remove (orphans, parent_uid);
}
g_mutex_unlock (&server->priv->orphans_lock);
return array;
}
static void
source_request_server_auth_request_cancel_all (ESourceRegistryServer *server)
{
GHashTableIter iter;
gpointer value;
g_mutex_lock (&server->priv->auth_lock);
g_hash_table_iter_init (&iter, server->priv->waiting_auths);
while (g_hash_table_iter_next (&iter, NULL, &value)) {
GQueue *queue = value;
GList *list, *link;
list = g_queue_peek_head_link (queue);
for (link = list; link != NULL; link = g_list_next (link)) {
AuthRequest *request = link->data;
g_cancellable_cancel (request->cancellable);
}
}
g_hash_table_iter_init (&iter, server->priv->running_auths);
while (g_hash_table_iter_next (&iter, NULL, &value)) {
AuthRequest *request = value;
g_cancellable_cancel (request->cancellable);
}
g_mutex_unlock (&server->priv->auth_lock);
}
static void
source_registry_server_auth_request_push (ESourceRegistryServer *server,
const gchar *uid,
AuthRequest *request)
{
GQueue *queue;
g_return_if_fail (uid != NULL);
g_mutex_lock (&server->priv->auth_lock);
queue = g_hash_table_lookup (server->priv->waiting_auths, uid);
if (queue == NULL) {
queue = g_queue_new ();
g_hash_table_insert (
server->priv->waiting_auths,
g_strdup (uid), queue);
}
g_queue_push_tail (queue, auth_request_ref (request));
g_mutex_unlock (&server->priv->auth_lock);
}
static AuthRequest *
source_registry_server_auth_request_next (ESourceRegistryServer *server,
const gchar *uid)
{
AuthRequest *request = NULL;
g_return_val_if_fail (uid != NULL, NULL);
g_mutex_lock (&server->priv->auth_lock);
/* If we're already busy processing an authentication request
* for this UID, the next request will have to wait in line. */
if (!g_hash_table_contains (server->priv->running_auths, uid)) {
GQueue *queue;
queue = g_hash_table_lookup (
server->priv->waiting_auths, uid);
if (queue != NULL)
request = g_queue_pop_head (queue);
if (request != NULL)
g_hash_table_insert (
server->priv->running_auths,
g_strdup (uid),
auth_request_ref (request));
}
g_mutex_unlock (&server->priv->auth_lock);
return request;
}
static void
source_registry_server_auth_request_done (ESourceRegistryServer *server,
const gchar *uid)
{
g_return_if_fail (uid != NULL);
g_mutex_lock (&server->priv->auth_lock);
g_hash_table_remove (server->priv->running_auths, uid);
g_mutex_unlock (&server->priv->auth_lock);
}
static void
source_registry_server_auth_session_cb (GObject *source_object,
GAsyncResult *result,
gpointer user_data)
{
ESourceRegistryServer *server;
EAuthenticationSession *session;
EAuthenticationSessionResult auth_result;
AuthRequest *request;
const gchar *uid;
GError *error = NULL;
session = E_AUTHENTICATION_SESSION (source_object);
request = (AuthRequest *) user_data;
auth_result = e_authentication_session_execute_finish (
session, result, &error);
if (error != NULL) {
ESourceAuthenticator *authenticator;
g_warn_if_fail (auth_result == E_AUTHENTICATION_SESSION_ERROR);
g_simple_async_result_take_error (request->simple, error);
/* If the authenticator is an EAuthenticationMediator,
* have it emit a "server-error" signal to the client. */
authenticator =
e_authentication_session_get_authenticator (session);
if (E_IS_AUTHENTICATION_MEDIATOR (authenticator))
e_authentication_mediator_server_error (
E_AUTHENTICATION_MEDIATOR (authenticator),
error);
}
/* Authentication dismissals require additional handling. */
if (auth_result == E_AUTHENTICATION_SESSION_DISMISSED) {
ESourceAuthenticator *authenticator;
/* If the authenticator is an EAuthenticationMediator,
* have it emit a "dismissed" signal to the client. */
authenticator =
e_authentication_session_get_authenticator (session);
if (E_IS_AUTHENTICATION_MEDIATOR (authenticator))
e_authentication_mediator_dismiss (
E_AUTHENTICATION_MEDIATOR (authenticator));
/* Prevent further user interruptions. */
if (request->source != NULL)
e_server_side_source_set_allow_auth_prompt (
E_SERVER_SIDE_SOURCE (request->source), FALSE);
/* e_source_registry_server_authenticate_finish() should
* return an error since authentication did not complete. */
g_simple_async_result_set_error (
request->simple,
G_IO_ERROR, G_IO_ERROR_CANCELLED,
_("The user declined to authenticate"));
}
g_simple_async_result_complete_in_idle (request->simple);
server = e_authentication_session_get_server (session);
uid = e_authentication_session_get_source_uid (session);
source_registry_server_auth_request_done (server, uid);
source_registry_server_maybe_start_auth_session (server, uid);
auth_request_unref (request);
}
static gboolean
source_registry_server_start_auth_session_idle_cb (gpointer user_data)
{
AuthRequest *request = user_data;
/* Execute the new active auth session. This signals it to
* respond with a cached secret in the keyring if it can, or
* else show an authentication prompt and wait for input. */
e_authentication_session_execute (
request->session,
G_PRIORITY_DEFAULT,
request->cancellable,
source_registry_server_auth_session_cb,
auth_request_ref (request));
return FALSE;
}
static void
source_registry_server_maybe_start_auth_session (ESourceRegistryServer *server,
const gchar *uid)
{
AuthRequest *request;
/* We own the returned reference, unless we get NULL. */
request = source_registry_server_auth_request_next (server, uid);
if (request != NULL) {
GSource *idle_source;
/* Process the next AuthRequest from an idle callback
* on our own GMainContext, since the current thread-
* default GMainContext may only be temporary. */
idle_source = g_idle_source_new ();
g_source_set_callback (
idle_source,
source_registry_server_start_auth_session_idle_cb,
auth_request_ref (request),
(GDestroyNotify) auth_request_unref);
g_source_attach (idle_source, server->priv->main_context);
g_source_unref (idle_source);
auth_request_unref (request);
}
}
static void
source_registry_server_authenticate_done_cb (GObject *source_object,
GAsyncResult *result,
gpointer user_data)
{
GError *error = NULL;
e_source_registry_server_authenticate_finish (
E_SOURCE_REGISTRY_SERVER (source_object), result, &error);
/* Ignore cancellations. */
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
g_error_free (error);
} else if (error != NULL) {
g_warning ("%s: %s", G_STRFUNC, error->message);
g_error_free (error);
}
}
static void
source_registry_server_wait_for_client_cb (GObject *source_object,
GAsyncResult *result,
gpointer user_data)
{
EAuthenticationMediator *mediator;
EAuthenticationSession *session;
GError *error = NULL;
mediator = E_AUTHENTICATION_MEDIATOR (source_object);
session = E_AUTHENTICATION_SESSION (user_data);
e_authentication_mediator_wait_for_client_finish (
mediator, result, &error);
if (error == NULL) {
ESourceRegistryServer *server;
server = e_authentication_session_get_server (session);
/* Client is ready and waiting to test passwords, so
* execute the authentication session as soon as all
* other authentication sessions for this same data
* source are finished.
*
* XXX Note this asynchronous operation is not cancellable
* but it does time out on its own after a few minutes. */
e_source_registry_server_authenticate (
server, session, NULL,
source_registry_server_authenticate_done_cb, NULL);
} else {
/* Most likely the client went dark and the operation
* timed out. Emit a dismissed signal anyway just in
* case the client is still alive and listening. */
e_authentication_mediator_dismiss (mediator);
g_warning ("%s: %s", G_STRFUNC, error->message);
g_error_free (error);
}
g_object_unref (session);
}
static gboolean
source_registry_server_allow_auth_prompt_all_cb (EDBusSourceManager *dbus_interface,
GDBusMethodInvocation *invocation,
ESourceRegistryServer *server)
{
GList *list, *link;
list = e_source_registry_server_list_sources (server, NULL);
for (link = list; link != NULL; link = g_list_next (link))
e_server_side_source_set_allow_auth_prompt (
E_SERVER_SIDE_SOURCE (link->data), TRUE);
g_list_free_full (list, (GDestroyNotify) g_object_unref);
e_dbus_source_manager_complete_allow_auth_prompt_all (
dbus_interface, invocation);
return TRUE;
}
static gboolean
source_registry_server_authenticate_cb (EDBusSourceManager *dbus_interface,
GDBusMethodInvocation *invocation,
const gchar *source_uid,
const gchar *prompt_title,
const gchar *prompt_message,
const gchar *prompt_description,
ESourceRegistryServer *server)
{
GDBusConnection *connection;
EAuthenticationSession *session;
ESourceAuthenticator *authenticator;
const gchar *base_object_path;
const gchar *sender;
gchar *auth_object_path;
GError *error = NULL;
/* Export the D-Bus interface to a unique object path. This
* effectively starts a new authentication session with the
* method caller. */
base_object_path = E_SOURCE_REGISTRY_SERVER_OBJECT_PATH;
connection = g_dbus_method_invocation_get_connection (invocation);
sender = g_dbus_method_invocation_get_sender (invocation);
auth_object_path = g_strdup_printf (
"%s/auth_%u", base_object_path,
server->priv->authentication_count++);
authenticator = e_authentication_mediator_new (
connection, auth_object_path, sender, &error);
if (error != NULL) {
g_warn_if_fail (authenticator == NULL);
g_dbus_method_invocation_take_error (invocation, error);
g_free (auth_object_path);
return TRUE;
}
g_return_val_if_fail (
E_IS_SOURCE_AUTHENTICATOR (authenticator), FALSE);
/* Create the authentication session. */
session = e_source_registry_server_new_auth_session (
server, authenticator, source_uid);
/* Configure the authentication session. */
g_object_set (
session,
"prompt-title", prompt_title,
"prompt-message", prompt_message,
"prompt-description", prompt_description,
NULL);
/* Before adding the authentication session to the server we
* must handshake with the client requesting authentication.
* We do this by returning the object path of the exported
* Authenticator interface and then waiting for the client to
* acknowledge by calling the Ready() method on the interface.
* This indicates the client is ready to receive signals.
*
* XXX Note this asynchronous operation is not cancellable
* but it does time out on its own after a few minutes. */
e_authentication_mediator_wait_for_client (
E_AUTHENTICATION_MEDIATOR (authenticator),
NULL, source_registry_server_wait_for_client_cb,
g_object_ref (session));
e_dbus_source_manager_complete_authenticate (
dbus_interface, invocation, auth_object_path);
g_object_unref (authenticator);
g_object_unref (session);
g_free (auth_object_path);
return TRUE;
}
static gboolean
source_registry_server_create_source (ESourceRegistryServer *server,
const gchar *uid,
const gchar *data,
GError **error)
{
ESource *source = NULL;
GFile *file;
GFile *parent;
GKeyFile *key_file;
gboolean success;
gsize length;
GError *local_error = NULL;
g_return_val_if_fail (uid != NULL, FALSE);
g_return_val_if_fail (data != NULL, FALSE);
length = strlen (data);
/* Make sure the data is syntactically valid. */
key_file = g_key_file_new ();
success = g_key_file_load_from_data (
key_file, data, length, G_KEY_FILE_NONE, error);
g_key_file_free (key_file);
if (!success)
return FALSE;
/* Check that the given unique identifier really is unique.
*
* XXX There's a valid case to be made that the server should be
* assigning unique identifiers to new sources to avoid this
* error. That's fine for standalone sources but makes life
* more difficult for clients creating a set or hierarchy of
* sources that cross reference one another, such for a mail
* account. Having CLIENTS generate new UIDs means they can
* prepare any cross references in advance, then submit each
* source as is without having to make further modifications
* as would be necessary if using server-assigned UIDs.
*
* Anyway, if used properly the odds of a UID collision here
* are slim enough that I think it's a reasonable trade-off.
*/
source = e_source_registry_server_ref_source (server, uid);
if (source != NULL) {
g_set_error (
error, G_IO_ERROR, G_IO_ERROR_EXISTS,
_("UID '%s' is already in use"), uid);
g_object_unref (source);
return FALSE;
}
file = e_server_side_source_new_user_file (uid);
/* Create the directory where we'll be writing. */
parent = g_file_get_parent (file);
g_file_make_directory_with_parents (parent, NULL, &local_error);
g_object_unref (parent);
if (g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
g_clear_error (&local_error);
if (local_error != NULL) {
g_propagate_error (error, local_error);
success = FALSE;
}
/* Write the data to disk. The file monitor should eventually
* notice the new file and call e_source_registry_server_load_file()
* per design, but we're going to beat it to the punch since we
* need to return the new D-Bus object path back to the caller.
* By the time the file monitor gets around to loading the file,
* it will simply get back the EDBusSourceObject we've already
* created and exported. */
if (success)
success = g_file_replace_contents (
file, data, length, NULL, FALSE,
G_FILE_CREATE_PRIVATE, NULL, NULL, error);
if (success) {
ESourcePermissionFlags flags;
/* New sources are always writable + removable. */
flags = E_SOURCE_PERMISSION_WRITABLE |
E_SOURCE_PERMISSION_REMOVABLE;
source = e_source_registry_server_load_file (
server, file, flags, error);
/* We don't need the returned reference. */
if (source != NULL)
g_object_unref (source);
else
success = FALSE;
}
g_object_unref (file);
return success;
}
static gboolean
source_registry_server_create_sources_cb (EDBusSourceManager *dbus_interface,
GDBusMethodInvocation *invocation,
GVariant *array,
ESourceRegistryServer *server)
{
GVariantIter iter;
gchar *uid, *data;
GError *error = NULL;
g_variant_iter_init (&iter, array);
while (g_variant_iter_next (&iter, "{ss}", &uid, &data)) {
source_registry_server_create_source (
server, uid, data, &error);
g_free (uid);
g_free (data);
if (error != NULL)
break;
}
if (error != NULL)
g_dbus_method_invocation_take_error (invocation, error);
else
e_dbus_source_manager_complete_create_sources (
dbus_interface, invocation);
return TRUE;
}
static gboolean
source_registry_server_reload_cb (EDBusSourceManager *dbus_interface,
GDBusMethodInvocation *invocation,
ESourceRegistryServer *server)
{
e_dbus_server_quit (
E_DBUS_SERVER (server),
E_DBUS_SERVER_EXIT_RELOAD);
e_dbus_source_manager_complete_reload (dbus_interface, invocation);
return TRUE;
}
static void
source_registry_server_monitor_changed_cb (GFileMonitor *monitor,
GFile *file,
GFile *other_file,
GFileMonitorEvent event_type,
ESourceRegistryServer *server)
{
if (event_type == G_FILE_MONITOR_EVENT_CREATED) {
ESource *source;
GError *error = NULL;
/* it can return NULL source for hidden files */
source = e_server_side_source_new (server, file, &error);
if (!error && source) {
/* File monitors are only placed on directories
* where data sources are writable and removable,
* so it should be safe to assume these flags. */
e_server_side_source_set_writable (
E_SERVER_SIDE_SOURCE (source), TRUE);
e_server_side_source_set_removable (
E_SERVER_SIDE_SOURCE (source), TRUE);
e_source_registry_server_add_source (server, source);
g_object_unref (source);
} else if (error) {
e_source_registry_server_load_error (
server, file, error);
g_error_free (error);
}
}
if (event_type == G_FILE_MONITOR_EVENT_DELETED) {
ESource *source;
gchar *uid;
uid = e_server_side_source_uid_from_file (file, NULL);
if (uid == NULL)
return;
source = e_source_registry_server_ref_source (server, uid);
g_free (uid);
if (source == NULL)
return;
/* If the key file for a non-removable source was
* somehow deleted, disregard the event and leave
* the source object in memory. */
if (e_source_get_removable (source))
e_source_registry_server_remove_source (server, source);
g_object_unref (source);
}
}
static gboolean
source_registry_server_traverse_cb (GNode *node,
GQueue *queue)
{
g_queue_push_tail (queue, g_object_ref (node->data));
return FALSE;
}
static void
source_registry_server_queue_subtree (ESource *source,
GQueue *queue)
{
GNode *node;
node = e_server_side_source_get_node (E_SERVER_SIDE_SOURCE (source));
g_node_traverse (
node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
(GNodeTraverseFunc) source_registry_server_traverse_cb, queue);
}
static gboolean
source_registry_server_find_parent (ESourceRegistryServer *server,
ESource *source)
{
ESource *parent;
const gchar *parent_uid;
/* If the given source references a parent source and the
* parent source is not present in the hierarchy, the given
* source is added to an orphan table until the referenced
* parent is added to the hierarchy. */
parent_uid = e_source_get_parent (source);
if (parent_uid == NULL || *parent_uid == '\0')
return TRUE;
parent = g_hash_table_lookup (server->priv->sources, parent_uid);
if (parent != NULL) {
GNode *parent_node;
GNode *object_node;
parent_node = e_server_side_source_get_node (
E_SERVER_SIDE_SOURCE (parent));
object_node = e_server_side_source_get_node (
E_SERVER_SIDE_SOURCE (source));
g_node_append (parent_node, object_node);
return TRUE;
}
source_registry_server_orphans_insert (server, source);
return FALSE;
}
static void
source_registry_server_adopt_orphans (ESourceRegistryServer *server,
ESource *source)
{
GPtrArray *array;
/* Check if a newly-added source has any orphan sources
* that are waiting for it. The orphans can now be added
* to the hierarchy as children of the newly-added source. */
array = source_registry_server_orphans_steal (server, source);
if (array != NULL) {
guint ii;
for (ii = 0; ii < array->len; ii++) {
ESource *orphan = array->pdata[ii];
e_source_registry_server_add_source (server, orphan);
}
g_ptr_array_unref (array);
}
}
static void
source_registry_server_dispose (GObject *object)
{
ESourceRegistryServerPrivate *priv;
priv = E_SOURCE_REGISTRY_SERVER_GET_PRIVATE (object);
if (priv->main_context != NULL) {
g_main_context_unref (priv->main_context);
priv->main_context = NULL;
}
if (priv->object_manager != NULL) {
g_object_unref (priv->object_manager);
priv->object_manager = NULL;
}
if (priv->source_manager != NULL) {
g_object_unref (priv->source_manager);
priv->source_manager = NULL;
}
g_hash_table_remove_all (priv->sources);
g_hash_table_remove_all (priv->orphans);
g_hash_table_remove_all (priv->monitors);
g_hash_table_remove_all (priv->running_auths);
g_hash_table_remove_all (priv->waiting_auths);
/* Chain up to parent's dispose() method. */
G_OBJECT_CLASS (e_source_registry_server_parent_class)->
dispose (object);
}
static void
source_registry_server_finalize (GObject *object)
{
ESourceRegistryServerPrivate *priv;
priv = E_SOURCE_REGISTRY_SERVER_GET_PRIVATE (object);
g_hash_table_destroy (priv->sources);
g_hash_table_destroy (priv->orphans);
g_hash_table_destroy (priv->monitors);
g_mutex_clear (&priv->sources_lock);
g_mutex_clear (&priv->orphans_lock);
g_mutex_clear (&priv->auth_lock);
g_hash_table_destroy (priv->running_auths);
g_hash_table_destroy (priv->waiting_auths);
/* Chain up to parent's finalize() method. */
G_OBJECT_CLASS (e_source_registry_server_parent_class)->
finalize (object);
}
static void
source_registry_server_bus_acquired (EDBusServer *server,
GDBusConnection *connection)
{
ESourceRegistryServerPrivate *priv;
GError *error = NULL;
priv = E_SOURCE_REGISTRY_SERVER_GET_PRIVATE (server);
g_dbus_object_manager_server_set_connection (
priv->object_manager, connection);
g_dbus_interface_skeleton_export (
G_DBUS_INTERFACE_SKELETON (priv->source_manager),
connection, E_SOURCE_REGISTRY_SERVER_OBJECT_PATH, &error);
/* Terminate the server if we can't export the interface. */
if (error != NULL) {
g_warning ("%s: %s", G_STRFUNC, error->message);
e_dbus_server_quit (server, E_DBUS_SERVER_EXIT_NORMAL);
g_error_free (error);
}
/* Chain up to parent's bus_acquired() method. */
E_DBUS_SERVER_CLASS (e_source_registry_server_parent_class)->
bus_acquired (server, connection);
}
static void
source_registry_server_quit_server (EDBusServer *server,
EDBusServerExitCode code)
{
ESourceRegistryServerPrivate *priv;
priv = E_SOURCE_REGISTRY_SERVER_GET_PRIVATE (server);
source_request_server_auth_request_cancel_all (
E_SOURCE_REGISTRY_SERVER (server));
/* This makes the object manager unexport all objects. */
g_dbus_object_manager_server_set_connection (
priv->object_manager, NULL);
g_dbus_interface_skeleton_unexport (
G_DBUS_INTERFACE_SKELETON (priv->source_manager));
/* Chain up to parent's quit_server() method. */
E_DBUS_SERVER_CLASS (e_source_registry_server_parent_class)->
quit_server (server, code);
}
static void
source_registry_server_source_added (ESourceRegistryServer *server,
ESource *source)
{
GDBusObject *dbus_object;
GDBusObject *g_dbus_object;
const gchar *uid;
const gchar *object_name;
const gchar *object_path;
const gchar *extension_name;
/* Instantiate an ECollectionBackend if appropriate.
*
* Do this BEFORE exporting so backends have a chance
* to make any last-minute tweaks to the data source. */
extension_name = E_SOURCE_EXTENSION_COLLECTION;
if (e_source_has_extension (source, extension_name)) {
EBackend *backend;
ESourceBackend *extension;
const gchar *backend_name;
extension = e_source_get_extension (source, extension_name);
backend_name = e_source_backend_get_backend_name (extension);
/* For convenience, we attach the EBackend to the ESource
* itself, which creates a reference cycle. The cycle is
* explicitly broken when the ESource is removed from the
* 'sources' hash table (see unref_data_source() above). */
backend = e_data_factory_ref_backend (
E_DATA_FACTORY (server), backend_name, source);
if (backend != NULL) {
g_object_set_data_full (
G_OBJECT (source),
BACKEND_DATA_KEY, backend,
(GDestroyNotify) g_object_unref);
} else {
g_warning (
"No collection backend '%s' for %s",
backend_name, e_source_get_uid (source));
}
}
/* Export the data source to clients over D-Bus. */
dbus_object = e_source_ref_dbus_object (source);
g_dbus_object_manager_server_export_uniquely (
server->priv->object_manager,
G_DBUS_OBJECT_SKELETON (dbus_object));
g_object_notify (G_OBJECT (source), "exported");
uid = e_source_get_uid (source);
g_dbus_object = G_DBUS_OBJECT (dbus_object);
object_path = g_dbus_object_get_object_path (g_dbus_object);
object_name = strrchr (object_path, '/') + 1;
g_debug ("Adding %s ('%s')\n", uid, object_name);
g_object_unref (dbus_object);
}
static void
source_registry_server_source_removed (ESourceRegistryServer *server,
ESource *source)
{
GDBusObject *dbus_object;
const gchar *uid;
const gchar *object_name;
const gchar *object_path;
uid = e_source_get_uid (source);
dbus_object = e_source_ref_dbus_object (source);
object_path = g_dbus_object_get_object_path (dbus_object);
object_name = strrchr (object_path, '/') + 1;
e_source_registry_debug_print ("Removing %s ('%s')\n", uid, object_name);
g_dbus_object_manager_server_unexport (
server->priv->object_manager, object_path);
g_object_notify (G_OBJECT (source), "exported");
g_object_unref (dbus_object);
}
static gboolean
source_registry_server_any_true (GSignalInvocationHint *ihint,
GValue *return_accu,
const GValue *handler_return,
gpointer unused)
{
if (g_value_get_boolean (handler_return))
g_value_set_boolean (return_accu, TRUE);
return TRUE;
}
static void
e_source_registry_server_class_init (ESourceRegistryServerClass *class)
{
GObjectClass *object_class;
EDBusServerClass *dbus_server_class;
EDataFactoryClass *data_factory_class;
GType backend_factory_type;
const gchar *modules_directory = MODULE_DIRECTORY;
const gchar *modules_directory_env;
modules_directory_env = g_getenv (EDS_REGISTRY_MODULES);
if (modules_directory_env &&
g_file_test (modules_directory_env, G_FILE_TEST_IS_DIR))
modules_directory = g_strdup (modules_directory_env);
g_type_class_add_private (class, sizeof (ESourceRegistryServerPrivate));
object_class = G_OBJECT_CLASS (class);
object_class->dispose = source_registry_server_dispose;
object_class->finalize = source_registry_server_finalize;
dbus_server_class = E_DBUS_SERVER_CLASS (class);
dbus_server_class->bus_name = SOURCES_DBUS_SERVICE_NAME;
dbus_server_class->module_directory = modules_directory;
dbus_server_class->bus_acquired = source_registry_server_bus_acquired;
dbus_server_class->quit_server = source_registry_server_quit_server;
data_factory_class = E_DATA_FACTORY_CLASS (class);
backend_factory_type = E_TYPE_COLLECTION_BACKEND_FACTORY;
data_factory_class->backend_factory_type = backend_factory_type;
class->source_added = source_registry_server_source_added;
class->source_removed = source_registry_server_source_removed;
/**
* ESourceRegistryServer::load-error:
* @server: the #ESourceRegistryServer which emitted the signal
* @file: the #GFile being loaded
* @error: a #GError describing the error
*
* Emitted when an error occurs while loading or parsing a
* data source key file.
**/
signals[LOAD_ERROR] = g_signal_new (
"load-error",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (ESourceRegistryServerClass, load_error),
NULL, NULL, NULL,
G_TYPE_NONE, 2,
G_TYPE_FILE,
G_TYPE_ERROR | G_SIGNAL_TYPE_STATIC_SCOPE);
/**
* ESourceRegistryServer::files-loaded:
* @server: the #ESourceRegistryServer which emitted the signal
*
* Emitted after all data source key files are loaded on startup.
* Extensions can connect to this signal to perform any additional
* work prior to running the main loop.
**/
signals[FILES_LOADED] = g_signal_new (
"files-loaded",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (ESourceRegistryServerClass, files_loaded),
NULL, NULL, NULL,
G_TYPE_NONE, 0);
/**
* ESourceRegistryServer::source-added:
* @server: the #ESourceRegistryServer which emitted the signal
* @source: the newly-added #EServerSideSource
*
* Emitted when an #EServerSideSource is added to @server.
**/
signals[SOURCE_ADDED] = g_signal_new (
"source-added",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (ESourceRegistryServerClass, source_added),
NULL, NULL, NULL,
G_TYPE_NONE, 1,
E_TYPE_SERVER_SIDE_SOURCE);
/**
* ESourceRegistryServer::source-removed:
* @server: the #ESourceRegistryServer which emitted the signal
* @source: the #EServerSideSource that got removed
*
* Emitted when an #EServerSideSource is removed from @server.
**/
signals[SOURCE_REMOVED] = g_signal_new (
"source-removed",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (ESourceRegistryServerClass, source_removed),
NULL, NULL, NULL,
G_TYPE_NONE, 1,
E_TYPE_SERVER_SIDE_SOURCE);
/**
* ESourceRegistryServer::tweak-key-file:
* @server: the #ESourceRegistryServer which emitted the signal
* @key_file: a #GKeyFile
* @uid: a unique identifier string for @key_file
*
* Emitted from e_source_registry_server_load_file() just prior
* to instantiating an #EServerSideSource. Signal handlers can
* tweak the @key_file content as necessary and return %TRUE to
* write the modified content back to disk.
*
* For the purposes of tweaking, it's easier to deal with a plain
* #GKeyFile than an #ESource instance. An #ESource, for example,
* does not allow key file groups to be removed.
*
* The return value is cumulative. If any signal handler returns
* %TRUE, the @key_file content is written back to disk.
*
* Returns: %TRUE if @key_file was modified, %FALSE otherwise
*
* Since: 3.8
**/
signals[TWEAK_KEY_FILE] = g_signal_new (
"tweak-key-file",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (ESourceRegistryServerClass, tweak_key_file),
source_registry_server_any_true, NULL,
NULL,
G_TYPE_BOOLEAN, 2,
G_TYPE_KEY_FILE,
G_TYPE_STRING);
}
static void
e_source_registry_server_init (ESourceRegistryServer *server)
{
GDBusObjectManagerServer *object_manager;
EDBusSourceManager *source_manager;
GHashTable *sources;
GHashTable *orphans;
GHashTable *monitors;
GHashTable *running_auths;
GHashTable *waiting_auths;
const gchar *object_path;
object_path = E_SOURCE_REGISTRY_SERVER_OBJECT_PATH;
object_manager = g_dbus_object_manager_server_new (object_path);
source_manager = e_dbus_source_manager_skeleton_new ();
/* UID string -> ESource */
sources = g_hash_table_new_full (
(GHashFunc) g_str_hash,
(GEqualFunc) g_str_equal,
(GDestroyNotify) g_free,
(GDestroyNotify) unref_data_source);
/* Parent UID string -> GPtrArray of ESources */
orphans = g_hash_table_new_full (
(GHashFunc) g_str_hash,
(GEqualFunc) g_str_equal,
(GDestroyNotify) g_free,
(GDestroyNotify) g_ptr_array_unref);
/* GFile -> GFileMonitor */
monitors = g_hash_table_new_full (
(GHashFunc) g_file_hash,
(GEqualFunc) g_file_equal,
(GDestroyNotify) g_object_unref,
(GDestroyNotify) g_object_unref);
running_auths = g_hash_table_new_full (
(GHashFunc) g_str_hash,
(GEqualFunc) g_str_equal,
(GDestroyNotify) g_free,
(GDestroyNotify) auth_request_unref);
waiting_auths = g_hash_table_new_full (
(GHashFunc) g_str_hash,
(GEqualFunc) g_str_equal,
(GDestroyNotify) g_free,
(GDestroyNotify) free_auth_queue);
server->priv = E_SOURCE_REGISTRY_SERVER_GET_PRIVATE (server);
server->priv->main_context = g_main_context_ref_thread_default ();
server->priv->object_manager = object_manager;
server->priv->source_manager = source_manager;
server->priv->sources = sources;
server->priv->orphans = orphans;
server->priv->monitors = monitors;
g_mutex_init (&server->priv->sources_lock);
g_mutex_init (&server->priv->orphans_lock);
g_mutex_init (&server->priv->auth_lock);
server->priv->waiting_auths = waiting_auths;
server->priv->running_auths = running_auths;
g_signal_connect (
source_manager, "handle-allow-auth-prompt-all",
G_CALLBACK (source_registry_server_allow_auth_prompt_all_cb),
server);
g_signal_connect (
source_manager, "handle-authenticate",
G_CALLBACK (source_registry_server_authenticate_cb),
server);
g_signal_connect (
source_manager, "handle-create-sources",
G_CALLBACK (source_registry_server_create_sources_cb),
server);
g_signal_connect (
source_manager, "handle-reload",
G_CALLBACK (source_registry_server_reload_cb),
server);
}
/**
* e_source_registry_server_new:
*
* Creates a new instance of #ESourceRegistryServer.
*
* Returns: a new instance of #ESourceRegistryServer
*
* Since: 3.6
**/
EDBusServer *
e_source_registry_server_new (void)
{
return g_object_new (E_TYPE_SOURCE_REGISTRY_SERVER, NULL);
}
/**
* e_source_registry_server_add_source:
* @server: an #ESourceRegistryServer
* @source: an #ESource
*
* Adds @source to @server.
*
* Since: 3.6
**/
void
e_source_registry_server_add_source (ESourceRegistryServer *server,
ESource *source)
{
GDBusObject *dbus_object;
EDBusSource *dbus_source;
const gchar *extension_name;
const gchar *uid;
gchar *data;
g_return_if_fail (E_IS_SOURCE_REGISTRY_SERVER (server));
g_return_if_fail (E_IS_SERVER_SIDE_SOURCE (source));
uid = e_source_get_uid (source);
g_return_if_fail (uid != NULL);
g_mutex_lock (&server->priv->sources_lock);
/* Check if we already have this object in the hierarchy. */
if (g_hash_table_lookup (server->priv->sources, uid) != NULL) {
g_mutex_unlock (&server->priv->sources_lock);
return;
}
/* Make sure the parent object (if any) is in the hierarchy. */
if (!source_registry_server_find_parent (server, source)) {
g_mutex_unlock (&server->priv->sources_lock);
return;
}
g_mutex_unlock (&server->priv->sources_lock);
/* Before we emit, make sure the EDBusSource's "data" property
* is up-to-date. ESource changes get propagated to the "data"
* property from an idle callback, which may still be pending. */
dbus_object = e_source_ref_dbus_object (source);
dbus_source = e_dbus_object_get_source (E_DBUS_OBJECT (dbus_object));
data = e_source_to_string (source, NULL);
e_dbus_source_set_data (dbus_source, data);
g_free (data);
g_object_unref (dbus_source);
g_object_unref (dbus_object);
/* If the added source has a [Collection] extension but the
* corresponding ECollectionBackendFactory is not available,
* the source gets permanently inserted in the orphans table
* to prevent it from being exported to client applications. */
extension_name = E_SOURCE_EXTENSION_COLLECTION;
if (e_source_has_extension (source, extension_name)) {
ECollectionBackendFactory *backend_factory;
backend_factory =
e_source_registry_server_ref_backend_factory (
server, source);
if (backend_factory == NULL) {
source_registry_server_orphans_insert (server, source);
return;
}
g_object_unref (backend_factory);
}
source_registry_server_sources_insert (server, source);
g_signal_emit (server, signals[SOURCE_ADDED], 0, source);
/* This is to ensure the source data gets written to disk, since
* the ESource is exported now. Could be racy otherwise if this
* function is called from a worker thread. */
e_source_changed (source);
/* Adopt any orphans that have been waiting for this object. */
source_registry_server_adopt_orphans (server, source);
}
/* Helper for e_source_registry_server_remove_object() */
static void
source_registry_server_remove_object (ESourceRegistryServer *server,
ESource *source)
{
g_object_ref (source);
if (source_registry_server_sources_remove (server, source)) {
EServerSideSource *ss_source;
ss_source = E_SERVER_SIDE_SOURCE (source);
source_registry_server_orphans_insert (server, source);
g_node_unlink (e_server_side_source_get_node (ss_source));
g_signal_emit (server, signals[SOURCE_REMOVED], 0, source);
}
g_object_unref (source);
}
/**
* e_source_registry_server_remove_source:
* @server: an #ESourceRegistryServer
* @source: an #ESource
*
* Removes @source and all of its descendants from @server.
*
* Since: 3.6
**/
void
e_source_registry_server_remove_source (ESourceRegistryServer *server,
ESource *source)
{
ESource *child;
ESource *exported;
GQueue queue = G_QUEUE_INIT;
const gchar *uid;
g_return_if_fail (E_IS_SOURCE_REGISTRY_SERVER (server));
g_return_if_fail (E_IS_SERVER_SIDE_SOURCE (source));
uid = e_source_get_uid (source);
/* If the removed source is in the server hierarchy, gather
* it and all of its descendants into a queue in "post-order"
* so we're always processing leaf nodes as we pop sources off
* the head of the queue. */
exported = e_source_registry_server_ref_source (server, uid);
if (exported != NULL) {
source_registry_server_queue_subtree (source, &queue);
g_object_unref (exported);
}
/* Move the queued descendants to the orphan table, and emit a
* "source-removed" signal for each source. This will include
* the removed source unless the source was already an orphan,
* in which case the queue will be empty. */
while ((child = g_queue_pop_head (&queue)) != NULL) {
source_registry_server_remove_object (server, child);
g_object_unref (child);
}
/* The removed source should be in the orphan table now. */
source_registry_server_orphans_remove (server, source);
}
/**
* e_source_registry_server_load_all:
* @server: an #ESourceRegistryServer
* @error: return location for a #GError, or %NULL
*
* Loads data source key files from standard system-wide and user-specific
* locations. Because multiple errors can occur when loading multiple files,
* @error is only set if a directory can not be opened. If a data source key
* file fails to load, the error is broadcast through the
* #ESourceRegistryServer::load-error signal.
*
* Returns: %TRUE if the standard directories were successfully opened,
* but this does not imply the key files were successfully loaded
*
* Since: 3.6
*
* Deprecated: 3.8: Instead, implement an equivalent function yourself.
* It was a mistake to encode this much file location
* policy directly into the library API.
**/
gboolean
e_source_registry_server_load_all (ESourceRegistryServer *server,
GError **error)
{
ESourcePermissionFlags flags;
const gchar *directory;
gboolean success;
g_return_val_if_fail (E_IS_SOURCE_REGISTRY_SERVER (server), FALSE);
/* Load the user's sources directory first so that user-specific
* data sources overshadow predefined data sources with identical
* UIDs. The 'local' data source is one such example. */
directory = e_server_side_source_get_user_dir ();
flags = E_SOURCE_PERMISSION_REMOVABLE |
E_SOURCE_PERMISSION_WRITABLE;
success = e_source_registry_server_load_directory (
server, directory, flags, error);
g_prefix_error (error, "%s: ", directory);
if (!success)
return FALSE;
directory = SYSTEM_WIDE_RO_SOURCES_DIRECTORY;
flags = E_SOURCE_PERMISSION_NONE;
success = e_source_registry_server_load_directory (
server, directory, flags, error);
g_prefix_error (error, "%s: ", directory);
if (!success)
return FALSE;
directory = SYSTEM_WIDE_RW_SOURCES_DIRECTORY;
flags = E_SOURCE_PERMISSION_WRITABLE;
success = e_source_registry_server_load_directory (
server, directory, flags, error);
g_prefix_error (error, "%s: ", directory);
if (!success)
return FALSE;
/* Signal that all files are now loaded. */
g_signal_emit (server, signals[FILES_LOADED], 0);
return TRUE;
}
/**
* e_source_registry_server_load_directory:
* @server: an #ESourceRegistryServer
* @path: the path to the directory to load
* @flags: permission flags for files loaded from @path
* @error: return location for a #GError, or %NULL
*
* Loads data source key files in @path. Because multiple errors can
* occur when loading multiple files, @error is only set if @path can
* not be opened. If a key file fails to load, the error is broadcast
* through the #ESourceRegistryServer::load-error signal.
*
* If the #E_DBUS_LOAD_DIRECTORY_REMOVABLE flag is given, then the @server
* will emit signals on the D-Bus interface when key files are created or
* deleted in @path.
*
* Returns: %TRUE if @path was successfully opened, but this
* does not imply the key files were successfully loaded
*
* Since: 3.6
**/
gboolean
e_source_registry_server_load_directory (ESourceRegistryServer *server,
const gchar *path,
ESourcePermissionFlags flags,
GError **error)
{
GDir *dir;
GFile *file;
const gchar *name;
gboolean removable;
g_return_val_if_fail (E_IS_SOURCE_REGISTRY_SERVER (server), FALSE);
g_return_val_if_fail (path != NULL, FALSE);
removable = ((flags & E_SOURCE_PERMISSION_REMOVABLE) != 0);
/* If the directory doesn't exist then there's nothing to load.
* Note we do not use G_FILE_TEST_DIR here. If the given path
* exists but is not a directory then we let g_dir_open() fail. */
if (!g_file_test (path, G_FILE_TEST_EXISTS))
return TRUE;
dir = g_dir_open (path, 0, error);
if (dir == NULL)
return FALSE;
file = g_file_new_for_path (path);
while ((name = g_dir_read_name (dir)) != NULL) {
ESource *source;
GFile *child;
GError *local_error = NULL;
/* Ignore files with no ".source" suffix. */
if (!g_str_has_suffix (name, ".source"))
continue;
child = g_file_get_child (file, name);
source = e_source_registry_server_load_file (
server, child, flags, &local_error);
/* We don't need the returned reference. */
if (source != NULL)
g_object_unref (source);
if (local_error != NULL) {
e_source_registry_server_load_error (
server, child, local_error);
g_error_free (local_error);
}
g_object_unref (child);
}
g_dir_close (dir);
/* Only data source files in the user's
* sources directory should be removable. */
if (removable) {
GFileMonitor *monitor;
GError *local_error = NULL;
/* Directory monitoring is a nice-to-have feature.
* If this fails, leave a breadcrumb on the console
* to indicate something went wrong, but don't return
* an error status. */
monitor = g_file_monitor_directory (
file, G_FILE_MONITOR_NONE, NULL, &local_error);
/* Sanity check. */
g_warn_if_fail (
((monitor != NULL) && (local_error == NULL)) ||
((monitor == NULL) && (local_error != NULL)));
if (monitor != NULL) {
g_signal_connect (
monitor, "changed", G_CALLBACK (
source_registry_server_monitor_changed_cb),
server);
g_hash_table_insert (
server->priv->monitors,
g_object_ref (file),
g_object_ref (monitor));
g_object_unref (monitor);
}
if (local_error != NULL) {
g_warning ("%s: %s", G_STRFUNC, local_error->message);
g_error_free (local_error);
}
}
g_object_unref (file);
return TRUE;
}
/**
* e_source_registry_server_load_resource:
* @server: an #ESourceRegistryServer
* @resource: a #GResource containing data source key files
* @path: the path to the data source key files inside @resource
* @flags: permission flags for files loaded from @path
* @error: return location for a #GError, or %NULL
*
* Loads data source key files from @resource by enumerating the children
* at @path and calling e_source_registry_server_load_file() on each child.
* Because multiple errors can occur when loading multiple files, @error is
* only set if @path is invalid. If a key file fails to load, the error is
* broadcast through the #ESourceRegistryServer::load-error signal.
*
* Returns: %TRUE if @path was successfully located, but this does not
* imply the key files were successfully loaded
*
* Since: 3.8
**/
gboolean
e_source_registry_server_load_resource (ESourceRegistryServer *server,
GResource *resource,
const gchar *path,
ESourcePermissionFlags flags,
GError **error)
{
gchar **children;
gint ii;
g_return_val_if_fail (E_IS_SOURCE_REGISTRY_SERVER (server), FALSE);
g_return_val_if_fail (resource != NULL, FALSE);
g_return_val_if_fail (path != NULL, FALSE);
children = g_resource_enumerate_children (
resource, path, G_RESOURCE_LOOKUP_FLAGS_NONE, error);
if (children == NULL)
return FALSE;
for (ii = 0; children[ii] != NULL; ii++) {
ESource *source;
GFile *file;
gchar *child_path;
gchar *resource_uri;
GError *local_error = NULL;
child_path = g_build_path ("/", path, children[ii], NULL);
resource_uri = g_strconcat ("resource://", child_path, NULL);
file = g_file_new_for_uri (resource_uri);
g_free (resource_uri);
g_free (child_path);
source = e_source_registry_server_load_file (
server, file, flags, &local_error);
/* We don't need the returned reference. */
if (source != NULL)
g_object_unref (source);
if (local_error != NULL) {
e_source_registry_server_load_error (
server, file, local_error);
g_error_free (local_error);
}
g_object_unref (file);
}
g_strfreev (children);
return TRUE;
}
/* Helper for e_source_registry_server_load_file() */
static gboolean
source_registry_server_tweak_key_file (ESourceRegistryServer *server,
GFile *file,
const gchar *uid,
GError **error)
{
GKeyFile *key_file;
gchar *contents = NULL;
gsize length;
gboolean handler_pending;
gboolean success = FALSE;
gboolean tweaked = FALSE;
/* Skip this if no one's listening. */
handler_pending = g_signal_has_handler_pending (
server, signals[TWEAK_KEY_FILE], 0, FALSE);
if (!handler_pending)
return TRUE;
key_file = g_key_file_new ();
g_file_load_contents (file, NULL, &contents, &length, NULL, error);
if (contents != NULL) {
success = g_key_file_load_from_data (
key_file, contents, length,
G_KEY_FILE_KEEP_COMMENTS |
G_KEY_FILE_KEEP_TRANSLATIONS,
error);
g_free (contents);
}
if (success)
g_signal_emit (
server, signals[TWEAK_KEY_FILE], 0,
key_file, uid, &tweaked);
if (tweaked) {
contents = g_key_file_to_data (key_file, &length, NULL);
success = g_file_replace_contents (
file, contents, length, NULL, FALSE,
G_FILE_CREATE_NONE, NULL, NULL, error);
g_free (contents);
}
g_key_file_free (key_file);
return success;
}
/**
* e_source_registry_server_load_file:
* @server: an #ESourceRegistryServer
* @file: the data source key file to load
* @flags: initial permission flags for the data source
* @error: return location for a #GError, or %NULL
*
* Creates an #ESource for a native key file and adds it to @server.
* If an error occurs, the function returns %NULL and sets @error.
*
* The returned #ESource is referenced for thread-safety. Unreference
* the #ESource with g_object_unref() when finished with it.
*
* Returns: the newly-added #ESource, or %NULL on error
*
* Since: 3.6
**/
ESource *
e_source_registry_server_load_file (ESourceRegistryServer *server,
GFile *file,
ESourcePermissionFlags flags,
GError **error)
{
ESource *source;
gboolean writable;
gboolean removable;
gboolean success = TRUE;
gchar *uid;
g_return_val_if_fail (E_IS_SOURCE_REGISTRY_SERVER (server), NULL);
g_return_val_if_fail (G_IS_FILE (file), NULL);
writable = ((flags & E_SOURCE_PERMISSION_WRITABLE) != 0);
removable = ((flags & E_SOURCE_PERMISSION_REMOVABLE) != 0);
uid = e_server_side_source_uid_from_file (file, error);
if (uid == NULL)
return NULL;
/* Check if we already have this file loaded. */
source = e_source_registry_server_ref_source (server, uid);
/* If the source is to be removable then the key file can
* be written back to disk, and can therefore be tweaked. */
if (source == NULL && removable)
success = source_registry_server_tweak_key_file (
server, file, uid, error);
if (source == NULL && success)
source = e_server_side_source_new (server, file, error);
g_free (uid);
if (source == NULL)
return NULL;
/* Set the data source's initial permissions, which
* determines which D-Bus methods it exports: write()
* if writable, remove() if removable. We apply these
* before adding the source to the server because some
* "source-added" signal handlers may wish to override
* the initial permissions.
*
* Note that we apply the initial permission flags even
* if the data source has already been loaded. That is
* intentional. That is why the load_all() function loads
* the user directory before loading system-wide directories.
* If there's a UID collision between a data source in the
* user's directory and a data source in a system-wide
* directory, the permission flags for the system-wide
* directory should win.
*
* Consider an example:
*
* The built-in 'local' data source should always be
* writable but not removable.
*
* Suppose the user temporarily disables the 'local'
* data source. The altered 'local' data source file
* (with Enabled=false) is saved in the user's sources
* directory.
*
* On the next startup, the altered 'local' file is
* first loaded from the user's source directory and
* given removable + writable permissions.
*
* We then load data sources from the 'rw-sources'
* system directory containing the unaltered 'local'
* file (with Enabled=true), which is not removable.
*
* We keep the contents of the altered 'local' file
* (Enabled=false), but override its permissions to
* just be writable, not removable.
*/
e_server_side_source_set_writable (
E_SERVER_SIDE_SOURCE (source), writable);
e_server_side_source_set_removable (
E_SERVER_SIDE_SOURCE (source), removable);
/* This does nothing if the source is already added. */
e_source_registry_server_add_source (server, source);
return source;
}
/**
* e_source_registry_server_load_error:
* @server: an #EBusSourceServer
* @file: the #GFile that failed to load
* @error: a #GError describing the load error
*
* Emits the #ESourceRegistryServer::load-error signal.
*
* Since: 3.6
**/
void
e_source_registry_server_load_error (ESourceRegistryServer *server,
GFile *file,
const GError *error)
{
g_return_if_fail (E_IS_SOURCE_REGISTRY_SERVER (server));
g_return_if_fail (G_IS_FILE (file));
g_return_if_fail (error != NULL);
g_signal_emit (server, signals[LOAD_ERROR], 0, file, error);
}
/**
* e_source_registry_server_ref_source:
* @server: an #ESourceRegistryServer
* @uid: a unique identifier string
*
* Looks up an #ESource in @server by its unique identifier string.
*
* The returned #ESource is referenced for thread-safety and must be
* unreferenced with g_object_unref() when finished with it.
*
* Returns: an #ESource, or %NULL if no match was found
*
* Since: 3.6
**/
ESource *
e_source_registry_server_ref_source (ESourceRegistryServer *server,
const gchar *uid)
{
g_return_val_if_fail (E_IS_SOURCE_REGISTRY_SERVER (server), NULL);
g_return_val_if_fail (uid != NULL, NULL);
return source_registry_server_sources_lookup (server, uid);
}
/**
* e_source_registry_server_list_sources:
* @server: an #ESourceRegistryServer
* @extension_name: an extension name, or %NULL
*
* Returns a list of registered sources, sorted by display name. If
* @extension_name is given, restrict the list to sources having that
* extension name.
*
* The sources returned in the list are referenced for thread-safety.
* They must each be unreferenced with g_object_unref() when finished
* with them. Free the returned #GList itself with g_list_free().
*
* An easy way to free the list properly in one step is as follows:
*
* |[
* g_list_free_full (list, g_object_unref);
* ]|
*
* Returns: a sorted list of sources
*
* Since: 3.6
**/
GList *
e_source_registry_server_list_sources (ESourceRegistryServer *server,
const gchar *extension_name)
{
GList *list, *link;
GQueue trash = G_QUEUE_INIT;
g_return_val_if_fail (E_IS_SOURCE_REGISTRY_SERVER (server), NULL);
list = g_list_sort (
source_registry_server_sources_get_values (server),
(GCompareFunc) e_source_compare_by_display_name);
if (extension_name == NULL)
return list;
for (link = list; link != NULL; link = g_list_next (link)) {
ESource *source = E_SOURCE (link->data);
if (!e_source_has_extension (source, extension_name)) {
g_queue_push_tail (&trash, link);
g_object_unref (source);
}
}
/* We do want pop_head() here, not pop_head_link(). */
while ((link = g_queue_pop_head (&trash)) != NULL)
list = g_list_delete_link (list, link);
return list;
}
/**
* e_source_registry_server_find_extension:
* @server: an #ESourceRegistryServer
* @source: an #ESource
* @extension_name: the extension name to find
*
* Examines @source and its ancestors and returns the "deepest" #ESource
* having an #ESourceExtension with the given @extension_name. If neither
* @source nor any of its ancestors have such an extension, the function
* returns %NULL.
*
* This function is useful in cases when an #ESourceExtension is meant to
* apply to both the #ESource it belongs to and the #ESource's descendants.
*
* A common example is the #ESourceCollection extension, where descendants
* of an #ESource having an #ESourceCollection extension are implied to be
* members of that collection. In that example, this function can be used
* to test whether @source is a member of a collection.
*
* The returned #ESource is referenced for thread-safety and must be
* unreferenced with g_object_unref() when finished with it.
*
* Note the function returns the #ESource containing the #ESourceExtension
* instead of the #ESourceExtension itself because extension instances are
* not to be referenced directly (see e_source_get_extension()).
*
* Returns: an #ESource, or %NULL if no match was found
*
* Since: 3.8
**/
ESource *
e_source_registry_server_find_extension (ESourceRegistryServer *server,
ESource *source,
const gchar *extension_name)
{
g_return_val_if_fail (E_IS_SOURCE_REGISTRY_SERVER (server), NULL);
g_return_val_if_fail (E_IS_SOURCE (source), NULL);
g_return_val_if_fail (extension_name != NULL, NULL);
g_object_ref (source);
while (!e_source_has_extension (source, extension_name)) {
gchar *uid;
uid = e_source_dup_parent (source);
g_object_unref (source);
source = NULL;
if (uid != NULL) {
source = e_source_registry_server_ref_source (
server, uid);
g_free (uid);
}
if (source == NULL)
break;
}
return source;
}
/**
* e_source_registry_server_ref_backend:
* @server: an #ESourceRegistryServer
* @source: an #ESource
*
* Returns the #ECollectionBackend associated with @source, or %NULL if
* there is no #ECollectionBackend associated with @source.
*
* An #ESource is associated with an #ECollectionBackend if the #ESource has
* an #ESourceCollection extension, or if it is a hierarchical descendant of
* another #ESource which has an #ESourceCollection extension.
*
* The returned #ECollectionBackend is referenced for thread-safety.
* Unreference the #ECollectionBackend with g_object_unref() when finished
* with it.
*
* Returns: the #ECollectionBackend for @source, or %NULL
*
* Since: 3.6
**/
ECollectionBackend *
e_source_registry_server_ref_backend (ESourceRegistryServer *server,
ESource *source)
{
ECollectionBackend *backend = NULL;
g_return_val_if_fail (E_IS_SOURCE_REGISTRY_SERVER (server), NULL);
g_return_val_if_fail (E_IS_SOURCE (source), NULL);
source = e_source_registry_server_find_extension (
server, source, E_SOURCE_EXTENSION_COLLECTION);
if (source != NULL) {
backend = g_object_get_data (
G_OBJECT (source), BACKEND_DATA_KEY);
if (backend != NULL)
g_object_ref (backend);
g_object_unref (source);
}
return backend;
}
/**
* e_source_registry_server_ref_backend_factory:
* @server: an #ESourceRegistryServer
* @source: an #ESource
*
* Returns the #ECollectionBackendFactory for @source, if available.
* If @source does not have an #ESourceCollection extension, or if the
* #ESourceCollection extension names a #ESourceBackend:backend-name for
* which there is no corresponding #ECollectionBackendFactory, the function
* returns %NULL.
*
* The returned #ECollectionBackendFactory is referenced for thread-safety.
* Unreference the #ECollectionBackendFactory with g_object_unref() when
* finished with it.
*
* Returns: the #ECollectionBackendFactory for @source, or %NULL
*
* Since: 3.6
**/
ECollectionBackendFactory *
e_source_registry_server_ref_backend_factory (ESourceRegistryServer *server,
ESource *source)
{
EBackendFactory *factory;
ESourceBackend *extension;
const gchar *backend_name;
const gchar *extension_name;
g_return_val_if_fail (E_IS_SOURCE_REGISTRY_SERVER (server), NULL);
g_return_val_if_fail (E_IS_SOURCE (source), NULL);
/* XXX Should we also check ancestor sources for a collection
* extension so this function works for ANY source in the
* collection? Gonna refrain til a real use case emerges
* but it's something to keep in mind. */
extension_name = E_SOURCE_EXTENSION_COLLECTION;
if (!e_source_has_extension (source, extension_name))
return NULL;
extension = e_source_get_extension (source, extension_name);
backend_name = e_source_backend_get_backend_name (extension);
factory = e_data_factory_ref_backend_factory (
E_DATA_FACTORY (server), backend_name);
if (factory == NULL)
return NULL;
/* The factory *should* be an ECollectionBackendFactory.
* We specify this in source_registry_server_class_init(). */
return E_COLLECTION_BACKEND_FACTORY (factory);
}
/**
* e_source_registry_server_new_auth_session:
* @server: an #ESourceRegistryServer
* @authenticator: an #ESourceAuthenticator
* @source_uid: a data source identifier
*
* Convenience function instantiates an appropriate authentication
* session type for @source_uid.
*
* If @server has an #EServerSideSource instance for @source_uid, then
* its #EServerSideSource:auth-session-type is used to instantiate a new
* authentication session. Otherwise a plain #EAuthenticationSession is
* instantiated.
*
* Unreference the returned #EAuthenticationSession with g_object_unref()
* when finished with it.
*
* Returns: a new #EAuthenticationSession for @source_uid
*
* Since: 3.8
**/
EAuthenticationSession *
e_source_registry_server_new_auth_session (ESourceRegistryServer *server,
ESourceAuthenticator *authenticator,
const gchar *source_uid)
{
GType auth_session_type = E_TYPE_AUTHENTICATION_SESSION;
ESource *source;
g_return_val_if_fail (E_IS_SOURCE_REGISTRY_SERVER (server), NULL);
g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATOR (authenticator), NULL);
g_return_val_if_fail (source_uid != NULL, NULL);
source = e_source_registry_server_ref_source (server, source_uid);
if (source != NULL) {
auth_session_type =
e_server_side_source_get_auth_session_type (
E_SERVER_SIDE_SOURCE (source));
g_object_unref (source);
}
g_return_val_if_fail (
g_type_is_a (
auth_session_type,
E_TYPE_AUTHENTICATION_SESSION), NULL);
return g_object_new (
auth_session_type,
"server", server,
"authenticator", authenticator,
"source-uid", source_uid, NULL);
}
/**
* e_source_registry_server_authenticate_sync:
* @server: an #ESourceRegistryServer
* @session: an #EAuthenticationSession
* @cancellable: optional #GCancellable object, or %NULL
* @error: return location for a #GError, or %NULL
*
* Queues the @session behind any ongoing or pending authentication
* sessions for the same data source, and eventually executes @session
* (see e_authentication_session_execute_sync() for more details).
*
* This function blocks until @session is finished executing. For a
* non-blocking variation see e_source_registry_server_authenticate().
*
* If an error occurs, the function sets @error and returns %FALSE.
*
* Returns: %TRUE on success, %FALSE on failure
*
* Since: 3.6
**/
gboolean
e_source_registry_server_authenticate_sync (ESourceRegistryServer *server,
EAuthenticationSession *session,
GCancellable *cancellable,
GError **error)
{
EAsyncClosure *closure;
GAsyncResult *result;
gboolean success;
g_return_val_if_fail (E_IS_SOURCE_REGISTRY_SERVER (server), FALSE);
g_return_val_if_fail (E_IS_AUTHENTICATION_SESSION (session), FALSE);
closure = e_async_closure_new ();
e_source_registry_server_authenticate (
server, session, cancellable,
e_async_closure_callback, closure);
result = e_async_closure_wait (closure);
success = e_source_registry_server_authenticate_finish (
server, result, error);
e_async_closure_free (closure);
return success;
}
/**
* e_source_registry_server_authenticate:
* @server: an #ESourceRegistryServer
* @session: an #EAuthenticationSession
* @cancellable: optional #GCancellable object, or %NULL
* @callback: a #GAsyncReadyCallback to call when the request is satisfied
* @user_data: data to pass to the callback function
*
* Queues the @session behind any ongoing or pending authentication
* sessions for the same data source, and eventually executes @session
* (see e_authentication_session_execute_sync() for more details).
*
* This function returns immediately after enqueuing @session. When
* @session is finished executing, @callback will be called. You can
* then call e_source_registry_server_authenticate_finish() to get the
* result of the operation.
*
* Since: 3.6
**/
void
e_source_registry_server_authenticate (ESourceRegistryServer *server,
EAuthenticationSession *session,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GSimpleAsyncResult *simple;
AuthRequest *request;
const gchar *uid;
g_return_if_fail (E_IS_SOURCE_REGISTRY_SERVER (server));
g_return_if_fail (E_IS_AUTHENTICATION_SESSION (session));
uid = e_authentication_session_get_source_uid (session);
g_return_if_fail (uid != NULL);
simple = g_simple_async_result_new (
G_OBJECT (server), callback, user_data,
e_source_registry_server_authenticate);
g_simple_async_result_set_check_cancellable (simple, cancellable);
request = auth_request_new (session, simple, cancellable);
source_registry_server_auth_request_push (server, uid, request);
auth_request_unref (request);
source_registry_server_maybe_start_auth_session (server, uid);
g_object_unref (simple);
}
/**
* e_source_registry_server_authenticate_finish:
* @server: an #ESourceRegistryServer
* @result: a #GAsyncResult
* @error: return location for a #GError, or %NULL
*
* Finishes the operation started with e_source_registry_server_authenticate().
* If an error occurred, the function will set @error and return %FALSE.
*
* Returns: %TRUE on success, %FALSE on failure
*
* Since: 3.6
**/
gboolean
e_source_registry_server_authenticate_finish (ESourceRegistryServer *server,
GAsyncResult *result,
GError **error)
{
GSimpleAsyncResult *simple;
g_return_val_if_fail (
g_simple_async_result_is_valid (
result, G_OBJECT (server),
e_source_registry_server_authenticate), FALSE);
simple = G_SIMPLE_ASYNC_RESULT (result);
/* Assume success unless a GError is set. */
return !g_simple_async_result_propagate_error (simple, error);
}
|