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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
* Copyright (C) 2006 OpenedHand Ltd
* Copyright (C) 2009 Intel Corporation
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of version 2.1 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 Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author: Ross Burton <ross@linux.intel.com>
*/
#include <config.h>
#include <unistd.h>
#include <string.h>
#include <glib-object.h>
#include <glib/gi18n-lib.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include "e-book.h"
#include "e-error.h"
#include "e-contact.h"
#include "e-name-western.h"
#include "e-book-view-private.h"
#include "e-data-book-factory-bindings.h"
#include "e-data-book-bindings.h"
#include "libedata-book/e-data-book-types.h"
#include "e-book-marshal.h"
#define E_DATA_BOOK_FACTORY_SERVICE_NAME "org.gnome.evolution.dataserver.AddressBook"
static gchar ** flatten_stringlist(GList *list);
static GList *array_to_stringlist (gchar **list);
static gboolean unwrap_gerror(GError *error, GError **client_error);
static EBookStatus get_status_from_error (GError *error);
G_DEFINE_TYPE(EBook, e_book, G_TYPE_OBJECT)
#define E_BOOK_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), E_TYPE_BOOK, EBookPrivate))
enum {
WRITABLE_STATUS,
CONNECTION_STATUS,
AUTH_REQUIRED,
BACKEND_DIED,
LAST_SIGNAL
};
static guint e_book_signals [LAST_SIGNAL];
struct _EBookPrivate {
ESource *source;
gchar *uri;
DBusGProxy *proxy;
gboolean loaded;
gboolean writable;
gboolean connected;
gchar *cap;
gboolean cap_queried;
};
static DBusGConnection *connection = NULL;
static DBusGProxy *factory_proxy = NULL;
/* guards both connection and factory_proxy */
static GStaticRecMutex connection_lock = G_STATIC_REC_MUTEX_INIT;
#define LOCK_CONN() g_static_rec_mutex_lock (&connection_lock)
#define UNLOCK_CONN() g_static_rec_mutex_unlock (&connection_lock)
typedef struct {
EBook *book;
gpointer callback; /* TODO union */
gpointer closure;
gpointer data;
} AsyncData;
GQuark
e_book_error_quark (void)
{
static GQuark q = 0;
if (q == 0)
q = g_quark_from_static_string ("e-book-error-quark");
return q;
}
/*
* Called when the addressbook server dies.
*/
static void
proxy_destroyed (gpointer data, GObject *object)
{
EBook *book = data;
g_assert (E_IS_BOOK (book));
g_warning (G_STRLOC ": e-d-s proxy died");
/* Ensure that everything relevant is NULL */
LOCK_CONN ();
factory_proxy = NULL;
book->priv->proxy = NULL;
UNLOCK_CONN ();
g_signal_emit (G_OBJECT (book), e_book_signals [BACKEND_DIED], 0);
}
static void
e_book_dispose (GObject *object)
{
EBook *book = E_BOOK (object);
book->priv->loaded = FALSE;
if (book->priv->proxy) {
g_object_weak_unref (G_OBJECT (book->priv->proxy), proxy_destroyed, book);
LOCK_CONN ();
org_gnome_evolution_dataserver_addressbook_Book_close (book->priv->proxy, NULL);
g_object_unref (book->priv->proxy);
book->priv->proxy = NULL;
UNLOCK_CONN ();
}
if (book->priv->source) {
g_object_unref (book->priv->source);
book->priv->source = NULL;
}
if (G_OBJECT_CLASS (e_book_parent_class)->dispose)
G_OBJECT_CLASS (e_book_parent_class)->dispose (object);
}
static void
e_book_finalize (GObject *object)
{
EBook *book = E_BOOK (object);
if (book->priv->uri)
g_free (book->priv->uri);
if (book->priv->cap)
g_free (book->priv->cap);
if (G_OBJECT_CLASS (e_book_parent_class)->finalize)
G_OBJECT_CLASS (e_book_parent_class)->finalize (object);
}
static void
e_book_class_init (EBookClass *e_book_class)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (e_book_class);
e_book_signals [WRITABLE_STATUS] =
g_signal_new ("writable_status",
G_OBJECT_CLASS_TYPE (gobject_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (EBookClass, writable_status),
NULL, NULL,
e_book_marshal_NONE__BOOL,
G_TYPE_NONE, 1,
G_TYPE_BOOLEAN);
e_book_signals [CONNECTION_STATUS] =
g_signal_new ("connection_status",
G_OBJECT_CLASS_TYPE (gobject_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (EBookClass, connection_status),
NULL, NULL,
e_book_marshal_NONE__BOOL,
G_TYPE_NONE, 1,
G_TYPE_BOOLEAN);
e_book_signals [AUTH_REQUIRED] =
g_signal_new ("auth_required",
G_OBJECT_CLASS_TYPE (gobject_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (EBookClass, auth_required),
NULL, NULL,
e_book_marshal_NONE__NONE,
G_TYPE_NONE, 0);
e_book_signals [BACKEND_DIED] =
g_signal_new ("backend_died",
G_OBJECT_CLASS_TYPE (gobject_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (EBookClass, backend_died),
NULL, NULL,
e_book_marshal_NONE__NONE,
G_TYPE_NONE, 0);
gobject_class->dispose = e_book_dispose;
gobject_class->finalize = e_book_finalize;
g_type_class_add_private (e_book_class, sizeof (EBookPrivate));
}
static void
e_book_init (EBook *book)
{
EBookPrivate *priv = E_BOOK_GET_PRIVATE (book);
priv->source = NULL;
priv->uri = NULL;
priv->proxy = NULL;
priv->loaded = FALSE;
priv->writable = FALSE;
priv->connected = FALSE;
priv->cap = NULL;
priv->cap_queried = FALSE;
book->priv = priv;
}
static DBusHandlerResult
filter_dbus_msgs_cb (DBusConnection *pconnection, DBusMessage *message, gpointer user_data)
{
if (dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
DBusGConnection *conn = connection;
LOCK_CONN ();
factory_proxy = NULL;
connection = NULL;
UNLOCK_CONN ();
dbus_g_connection_unref (conn);
return DBUS_HANDLER_RESULT_HANDLED;
}
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
static void
factory_proxy_destroy_cb (DBusGProxy *proxy, gpointer user_data)
{
LOCK_CONN ();
factory_proxy = NULL;
UNLOCK_CONN ();
}
static gboolean
e_book_activate(GError **error)
{
DBusError derror;
LOCK_CONN ();
if (G_LIKELY (factory_proxy)) {
UNLOCK_CONN ();
return TRUE;
}
if (!connection) {
connection = dbus_g_bus_get (DBUS_BUS_SESSION, error);
if (!connection) {
UNLOCK_CONN ();
return FALSE;
}
dbus_connection_add_filter (dbus_g_connection_get_connection (connection), filter_dbus_msgs_cb, NULL, NULL);
}
dbus_error_init (&derror);
if (!dbus_bus_start_service_by_name (dbus_g_connection_get_connection (connection),
E_DATA_BOOK_FACTORY_SERVICE_NAME,
0, NULL, &derror)) {
dbus_set_g_error (error, &derror);
dbus_error_free (&derror);
UNLOCK_CONN ();
return FALSE;
}
if (!factory_proxy) {
factory_proxy = dbus_g_proxy_new_for_name_owner (connection,
E_DATA_BOOK_FACTORY_SERVICE_NAME,
"/org/gnome/evolution/dataserver/addressbook/BookFactory",
"org.gnome.evolution.dataserver.addressbook.BookFactory",
error);
if (!factory_proxy) {
UNLOCK_CONN ();
return FALSE;
}
g_signal_connect (factory_proxy, "destroy", G_CALLBACK (factory_proxy_destroy_cb), NULL);
}
UNLOCK_CONN ();
return TRUE;
}
static void
writable_cb (DBusGProxy *proxy, gboolean writable, EBook *book)
{
g_return_if_fail (E_IS_BOOK (book));
book->priv->writable = writable;
g_signal_emit (G_OBJECT (book), e_book_signals [WRITABLE_STATUS], 0, writable);
}
static void
connection_cb (DBusGProxy *proxy, gboolean connected, EBook *book)
{
g_return_if_fail (E_IS_BOOK (book));
book->priv->connected = connected;
g_signal_emit (G_OBJECT (book), e_book_signals [CONNECTION_STATUS], 0, connected);
}
static void
auth_required_cb (DBusGProxy *proxy, EBook *book)
{
g_return_if_fail (E_IS_BOOK (book));
g_signal_emit (G_OBJECT (book), e_book_signals [AUTH_REQUIRED], 0);
}
/**
* e_book_add_contact:
* @book: an #EBook
* @contact: an #EContact
* @error: a #GError to set on failure
*
* Adds @contact to @book.
*
* Returns: %TRUE if successful, %FALSE otherwise.
**/
gboolean
e_book_add_contact (EBook *book,
EContact *contact,
GError **error)
{
GError *err = NULL;
gchar *vcard, *uid = NULL;
e_return_error_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_error_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
e_return_error_if_fail (E_IS_CONTACT (contact), E_BOOK_ERROR_INVALID_ARG);
vcard = e_vcard_to_string (E_VCARD (contact), EVC_FORMAT_VCARD_30);
LOCK_CONN ();
org_gnome_evolution_dataserver_addressbook_Book_add_contact (book->priv->proxy, vcard, &uid, &err);
UNLOCK_CONN ();
g_free (vcard);
if (uid) {
e_contact_set (contact, E_CONTACT_UID, uid);
g_free (uid);
}
return unwrap_gerror (err, error);
}
static void
add_contact_reply (DBusGProxy *proxy, gchar *uid, GError *error, gpointer user_data)
{
AsyncData *data = user_data;
EBookIdCallback cb = data->callback;
/* If there is an error returned the GLib bindings currently return garbage
for the OUT values. This is bad. */
if (error)
uid = NULL;
if (cb)
cb (data->book, get_status_from_error (error), uid, data->closure);
if (uid)
g_free (uid);
g_object_unref (data->book);
g_slice_free (AsyncData, data);
}
/**
* e_book_async_add_contact:
* @book: an #EBook
* @contact: an #EContact
* @cb: function to call when the operation finishes
* @closure: data to pass to callback function
*
* Adds @contact to @book without blocking.
*
* Returns: %TRUE if the operation was started, %FALSE otherwise.
**/
gboolean
e_book_async_add_contact (EBook *book,
EContact *contact,
EBookIdCallback cb,
gpointer closure)
{
gchar *vcard;
AsyncData *data;
e_return_async_error_val_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_async_error_val_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
e_return_async_error_val_if_fail (E_IS_CONTACT (contact), E_BOOK_ERROR_INVALID_ARG);
vcard = e_vcard_to_string (E_VCARD (contact), EVC_FORMAT_VCARD_30);
data = g_slice_new0 (AsyncData);
data->book = g_object_ref (book);
data->callback = cb;
data->closure = closure;
LOCK_CONN ();
org_gnome_evolution_dataserver_addressbook_Book_add_contact_async (book->priv->proxy, vcard, add_contact_reply, data);
UNLOCK_CONN ();
g_free (vcard);
return 0;
}
/**
* e_book_commit_contact:
* @book: an #EBook
* @contact: an #EContact
* @error: a #GError to set on failure
*
* Applies the changes made to @contact to the stored version in
* @book.
*
* Returns: %TRUE if successful, %FALSE otherwise
**/
gboolean
e_book_commit_contact (EBook *book,
EContact *contact,
GError **error)
{
GError *err = NULL;
gchar *vcard;
e_return_error_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_error_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
e_return_error_if_fail (E_IS_CONTACT (contact), E_BOOK_ERROR_INVALID_ARG);
vcard = e_vcard_to_string (E_VCARD (contact), EVC_FORMAT_VCARD_30);
LOCK_CONN ();
org_gnome_evolution_dataserver_addressbook_Book_modify_contact (book->priv->proxy, vcard, &err);
UNLOCK_CONN ();
g_free (vcard);
return unwrap_gerror (err, error);
}
static void
modify_contact_reply (DBusGProxy *proxy, GError *error, gpointer user_data)
{
AsyncData *data = user_data;
EBookCallback cb = data->callback;
if (cb)
cb (data->book, get_status_from_error (error), data->closure);
g_object_unref (data->book);
g_slice_free (AsyncData, data);
}
/**
* e_book_async_commit_contact:
* @book: an #EBook
* @contact: an #EContact
* @cb: function to call when the operation finishes
* @closure: data to pass to callback function
*
* Applies the changes made to @contact to the stored version in
* @book without blocking.
*
* Returns: %TRUE if the operation was started, %FALSE otherwise.
**/
guint
e_book_async_commit_contact (EBook *book,
EContact *contact,
EBookCallback cb,
gpointer closure)
{
gchar *vcard;
AsyncData *data;
e_return_async_error_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_async_error_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
e_return_async_error_if_fail (E_IS_CONTACT (contact), E_BOOK_ERROR_INVALID_ARG);
vcard = e_vcard_to_string (E_VCARD (contact), EVC_FORMAT_VCARD_30);
data = g_slice_new0 (AsyncData);
data->book = g_object_ref (book);
data->callback = cb;
data->closure = closure;
LOCK_CONN ();
org_gnome_evolution_dataserver_addressbook_Book_modify_contact_async (book->priv->proxy, vcard, modify_contact_reply, data);
UNLOCK_CONN ();
g_free (vcard);
return 0;
}
/**
* e_book_get_required_fields:
* @book: an #EBook
* @fields: a #GList of fields to set on success
* @error: a #GError to set on failure
*
* Gets a list of fields that are required to be filled in for
* all contacts in this @book. The list will contain pointers
* to allocated strings, and both the #GList and the strings
* must be freed by the caller.
*
* Returns: %TRUE if successful, %FALSE otherwise.
**/
gboolean
e_book_get_required_fields (EBook *book,
GList **fields,
GError **error)
{
GError *err = NULL;
gchar **list = NULL;
e_return_error_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_error_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
LOCK_CONN ();
org_gnome_evolution_dataserver_addressbook_Book_get_required_fields (book->priv->proxy, &list, &err);
UNLOCK_CONN ();
if (list) {
*fields = array_to_stringlist (list);
return TRUE;
} else {
return unwrap_gerror (err, error);
}
}
static void
get_required_fields_reply(DBusGProxy *proxy, gchar **fields, GError *error, gpointer user_data)
{
AsyncData *data = user_data;
EBookEListCallback cb = data->callback;
gchar **i = fields;
EList *efields = e_list_new (NULL,
(EListFreeFunc) g_free,
NULL);
while (*i != NULL) {
e_list_append (efields, (*i++));
}
if (cb)
cb (data->book, get_status_from_error (error), efields, data->closure);
g_object_unref (efields);
g_free (fields);
g_object_unref (data->book);
g_slice_free (AsyncData, data);
}
/**
* e_book_async_get_required_fields:
* @book: an #EBook
* @cb: function to call when the operation finishes
* @closure: data to pass to callback function
*
* Gets a list of fields that are required to be filled in for
* all contacts in this @book. This function does not block.
*
* Returns: %TRUE if the operation was started, %FALSE otherwise.
**/
guint
e_book_async_get_required_fields (EBook *book,
EBookEListCallback cb,
gpointer closure)
{
AsyncData *data;
e_return_async_error_val_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_async_error_val_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
data = g_slice_new0 (AsyncData);
data->book = g_object_ref (book);
data->callback = cb;
data->closure = closure;
LOCK_CONN ();
org_gnome_evolution_dataserver_addressbook_Book_get_required_fields_async (book->priv->proxy, get_required_fields_reply, data);
UNLOCK_CONN ();
return 0;
}
/**
* e_book_get_supported_fields:
* @book: an #EBook
* @fields: a #GList of fields to set on success
* @error: a #GError to set on failure
*
* Gets a list of fields that can be stored for contacts
* in this @book. Other fields may be discarded. The list
* will contain pointers to allocated strings, and both the
* #GList and the strings must be freed by the caller.
*
* Returns: %TRUE if successful, %FALSE otherwise
**/
gboolean
e_book_get_supported_fields (EBook *book,
GList **fields,
GError **error)
{
GError *err = NULL;
gchar **list = NULL;
e_return_error_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_error_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
LOCK_CONN ();
org_gnome_evolution_dataserver_addressbook_Book_get_supported_fields (book->priv->proxy, &list, &err);
UNLOCK_CONN ();
if (list) {
*fields = array_to_stringlist (list);
return TRUE;
} else {
return unwrap_gerror (err, error);
}
}
static void
get_supported_fields_reply(DBusGProxy *proxy, gchar **fields, GError *error, gpointer user_data)
{
AsyncData *data = user_data;
EBookEListCallback cb = data->callback;
gchar **i = fields;
EList *efields = e_list_new (NULL, (EListFreeFunc) g_free, NULL);
while (*i != NULL) {
e_list_append (efields, (*i++));
}
if (cb)
cb (data->book, get_status_from_error (error), efields, data->closure);
g_object_unref (efields);
g_free (fields);
g_object_unref (data->book);
g_slice_free (AsyncData, data);
}
/**
* e_book_async_get_supported_fields:
* @book: an #EBook
* @cb: function to call when the operation finishes
* @closure: data to pass to callback function
*
* Gets a list of fields that can be stored for contacts
* in this @book. Other fields may be discarded. This
* function does not block.
*
* Returns: %TRUE if successful, %FALSE otherwise.
**/
guint
e_book_async_get_supported_fields (EBook *book,
EBookEListCallback cb,
gpointer closure)
{
AsyncData *data;
e_return_async_error_val_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_async_error_val_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
data = g_slice_new0 (AsyncData);
data->book = g_object_ref (book);
data->callback = cb;
data->closure = closure;
LOCK_CONN ();
org_gnome_evolution_dataserver_addressbook_Book_get_supported_fields_async (book->priv->proxy, get_supported_fields_reply, data);
UNLOCK_CONN ();
return 0;
}
/**
* e_book_get_supported_auth_methods:
* @book: an #EBook
* @auth_methods: a #GList of auth methods to set on success
* @error: a #GError to set on failure
*
* Queries @book for the list of authentication methods it supports.
* The list will contain pointers to allocated strings, and both the
* #GList and the strings must be freed by the caller.
*
* Returns: %TRUE if successful, %FALSE otherwise
**/
gboolean
e_book_get_supported_auth_methods (EBook *book,
GList **auth_methods,
GError **error)
{
GError *err = NULL;
gchar **list = NULL;
e_return_error_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_error_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
LOCK_CONN ();
org_gnome_evolution_dataserver_addressbook_Book_get_supported_auth_methods (book->priv->proxy, &list, &err);
UNLOCK_CONN ();
if (list) {
*auth_methods = array_to_stringlist (list);
return TRUE;
} else {
return unwrap_gerror (err, error);
}
}
static void
get_supported_auth_methods_reply(DBusGProxy *proxy, gchar **methods, GError *error, gpointer user_data)
{
AsyncData *data = user_data;
EBookEListCallback cb = data->callback;
gchar **i = methods;
EList *emethods = e_list_new (NULL,
(EListFreeFunc) g_free,
NULL);
while (*i != NULL) {
e_list_append (emethods, (*i++));
}
if (cb)
cb (data->book, get_status_from_error (error), emethods, data->closure);
g_object_unref (emethods);
g_free (methods);
g_object_unref (data->book);
g_slice_free (AsyncData, data);
}
/**
* e_book_async_get_supported_auth_methods:
* @book: an #EBook
* @cb: function to call when the operation finishes
* @closure: data to pass to callback function
*
* Queries @book for the list of authentication methods it supports.
* This function does not block.
*
* Returns: %TRUE if successful, %FALSE otherwise.
**/
guint
e_book_async_get_supported_auth_methods (EBook *book,
EBookEListCallback cb,
gpointer closure)
{
AsyncData *data;
e_return_async_error_val_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_async_error_val_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
data = g_slice_new0 (AsyncData);
data->book = g_object_ref (book);
data->callback = cb;
data->closure = closure;
LOCK_CONN ();
org_gnome_evolution_dataserver_addressbook_Book_get_supported_auth_methods_async (book->priv->proxy, get_supported_auth_methods_reply, data);
UNLOCK_CONN ();
return 0;
}
/**
* e_book_authenticate_user:
* @book: an #EBook
* @user: a string
* @passwd: a string
* @auth_method: a string
* @error: a #GError to set on failure
*
* Authenticates @user with @passwd, using the auth method
* @auth_method. @auth_method must be one of the authentication
* methods returned using e_book_get_supported_auth_methods.
*
* Returns: %TRUE if successful, %FALSE otherwise
**/
gboolean
e_book_authenticate_user (EBook *book,
const gchar *user,
const gchar *passwd,
const gchar *auth_method,
GError **error)
{
GError *err = NULL;
e_return_error_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_error_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
LOCK_CONN ();
org_gnome_evolution_dataserver_addressbook_Book_authenticate_user (book->priv->proxy, user, passwd, auth_method, &err);
UNLOCK_CONN ();
return unwrap_gerror (err, error);
}
static void
authenticate_user_reply(DBusGProxy *proxy, GError *error, gpointer user_data)
{
AsyncData *data = user_data;
EBookCallback cb = data->callback;
if (cb)
cb (data->book, get_status_from_error (error), data->closure);
g_object_unref (data->book);
g_slice_free (AsyncData, data);
}
/**
* e_book_async_authenticate_user:
* @book: an #EBook
* @user: user name
* @passwd: password
* @auth_method: string indicating authentication method
* @cb: function to call when the operation finishes
* @closure: data to pass to callback function
*
* Authenticate @user with @passwd, using the auth method
* @auth_method. @auth_method must be one of the authentication
* methods returned using e_book_get_supported_auth_methods.
* This function does not block.
*
* Returns: %FALSE if successful, %TRUE otherwise.
**/
guint
e_book_async_authenticate_user (EBook *book,
const gchar *user,
const gchar *passwd,
const gchar *auth_method,
EBookCallback cb,
gpointer closure)
{
AsyncData *data;
e_return_async_error_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_async_error_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
e_return_async_error_if_fail (user, E_BOOK_ERROR_INVALID_ARG);
e_return_async_error_if_fail (passwd, E_BOOK_ERROR_INVALID_ARG);
e_return_async_error_if_fail (auth_method, E_BOOK_ERROR_INVALID_ARG);
data = g_slice_new0 (AsyncData);
data->book = g_object_ref (book);
data->callback = cb;
data->closure = closure;
LOCK_CONN ();
org_gnome_evolution_dataserver_addressbook_Book_authenticate_user_async (book->priv->proxy, user, passwd, auth_method, authenticate_user_reply, data);
UNLOCK_CONN ();
return 0;
}
/**
* e_book_get_contact:
* @book: an #EBook
* @id: a unique string ID specifying the contact
* @contact: an #EContact
* @error: a #GError to set on failure
*
* Fills in @contact with the contents of the vcard in @book
* corresponding to @id.
*
* Returns: %TRUE if successful, %FALSE otherwise
**/
gboolean
e_book_get_contact (EBook *book,
const gchar *id,
EContact **contact,
GError **error)
{
GError *err = NULL;
gchar *vcard = NULL;
e_return_error_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_error_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
LOCK_CONN ();
org_gnome_evolution_dataserver_addressbook_Book_get_contact (book->priv->proxy, id, &vcard, &err);
UNLOCK_CONN ();
if (vcard) {
*contact = e_contact_new_from_vcard (vcard);
g_free (vcard);
}
return unwrap_gerror (err, error);
}
static void
get_contact_reply(DBusGProxy *proxy, gchar *vcard, GError *error, gpointer user_data)
{
AsyncData *data = user_data;
EBookContactCallback cb = data->callback;
EBookStatus status = get_status_from_error (error);
/* Protect against garbage return values on error */
if (error)
vcard = NULL;
if (cb) {
if (error == NULL) {
cb (data->book, status, e_contact_new_from_vcard (vcard), data->closure);
} else {
cb (data->book, status, NULL, data->closure);
}
} else {
g_warning (G_STRLOC ": cannot get contact: %s", error->message);
}
if (error)
g_error_free (error);
g_free (vcard);
g_object_unref (data->book);
g_slice_free (AsyncData, data);
}
/**
* e_book_async_get_contact:
* @book: an #EBook
* @id: a unique string ID specifying the contact
* @cb: function to call when operation finishes
* @closure: data to pass to callback function
*
* Retrieves a contact specified by @id from @book.
*
* Returns: %FALSE if successful, %TRUE otherwise
**/
guint
e_book_async_get_contact (EBook *book,
const gchar *id,
EBookContactCallback cb,
gpointer closure)
{
AsyncData *data;
e_return_async_error_val_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_async_error_val_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
e_return_async_error_val_if_fail (id, E_BOOK_ERROR_INVALID_ARG);
data = g_slice_new0 (AsyncData);
data->book = g_object_ref (book);
data->callback = cb;
data->closure = closure;
LOCK_CONN ();
org_gnome_evolution_dataserver_addressbook_Book_get_contact_async (book->priv->proxy, id, get_contact_reply, data);
UNLOCK_CONN ();
return 0;
}
/**
* e_book_remove_contact:
* @book: an #EBook
* @id: a string
* @error: a #GError to set on failure
*
* Removes the contact with id @id from @book.
*
* Returns: %TRUE if successful, %FALSE otherwise
**/
gboolean
e_book_remove_contact (EBook *book,
const gchar *id,
GError **error)
{
GError *err = NULL;
const gchar *l[2];
e_return_error_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_error_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
e_return_error_if_fail (id, E_BOOK_ERROR_INVALID_ARG);
l[0] = id;
l[1] = NULL;
LOCK_CONN ();
org_gnome_evolution_dataserver_addressbook_Book_remove_contacts (book->priv->proxy, l, &err);
UNLOCK_CONN ();
return unwrap_gerror (err, error);
}
static void
remove_contact_reply (DBusGProxy *proxy, GError *error, gpointer user_data)
{
AsyncData *data = user_data;
EBookCallback cb = data->callback;
if (cb)
cb (data->book, get_status_from_error (error), data->closure);
g_object_unref (data->book);
g_slice_free (AsyncData, data);
}
/**
* e_book_remove_contacts:
* @book: an #EBook
* @ids: an #GList of const gchar *id's
* @error: a #GError to set on failure
*
* Removes the contacts with ids from the list @ids from @book. This is
* always more efficient than calling e_book_remove_contact() if you
* have more than one id to remove, as some backends can implement it
* as a batch request.
*
* Returns: %TRUE if successful, %FALSE otherwise
**/
gboolean
e_book_remove_contacts (EBook *book,
GList *ids,
GError **error)
{
GError *err = NULL;
gchar **l;
e_return_error_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_error_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
e_return_error_if_fail (ids, E_BOOK_ERROR_INVALID_ARG);
l = flatten_stringlist (ids);
LOCK_CONN ();
org_gnome_evolution_dataserver_addressbook_Book_remove_contacts (book->priv->proxy, (const gchar **) l, &err);
UNLOCK_CONN ();
g_free (l);
return unwrap_gerror (err, error);
}
/**
* e_book_async_remove_contact:
* @book: an #EBook
* @contact: an #EContact
* @cb: a function to call when the operation finishes
* @closure: data to pass to callback function
*
* Removes @contact from @book.
*
* Returns: %TRUE if successful, %FALSE otherwise
**/
guint
e_book_async_remove_contact (EBook *book,
EContact *contact,
EBookCallback cb,
gpointer closure)
{
AsyncData *data;
const gchar *l[2];
e_return_async_error_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_async_error_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
e_return_async_error_if_fail (E_IS_CONTACT (contact), E_BOOK_ERROR_INVALID_ARG);
l[0] = e_contact_get_const (contact, E_CONTACT_UID);
l[1] = NULL;
data = g_slice_new0 (AsyncData);
data->book = g_object_ref (book);
data->callback = cb;
data->closure = closure;
LOCK_CONN ();
org_gnome_evolution_dataserver_addressbook_Book_remove_contacts_async (book->priv->proxy, l, remove_contact_reply, data);
UNLOCK_CONN ();
return 0;
}
static void
remove_contact_by_id_reply (DBusGProxy *proxy, GError *error, gpointer user_data)
{
AsyncData *data = user_data;
EBookCallback cb = data->callback;
if (cb)
cb (data->book, get_status_from_error (error), data->closure);
g_object_unref (data->book);
g_slice_free (AsyncData, data);
}
/**
* e_book_async_remove_contact_by_id:
* @book: an #EBook
* @id: a unique ID string specifying the contact
* @cb: a function to call when the operation finishes
* @closure: data to pass to callback function
*
* Removes the contact with id @id from @book.
*
* Returns: %TRUE if successful, %FALSE otherwise
**/
guint
e_book_async_remove_contact_by_id (EBook *book,
const gchar *id,
EBookCallback cb,
gpointer closure)
{
AsyncData *data;
const gchar *l[2];
e_return_async_error_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_async_error_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
e_return_async_error_if_fail (id, E_BOOK_ERROR_INVALID_ARG);
l[0] = id;
l[1] = NULL;
data = g_slice_new0 (AsyncData);
data->book = g_object_ref (book);
data->callback = cb;
data->closure = closure;
LOCK_CONN ();
org_gnome_evolution_dataserver_addressbook_Book_remove_contacts_async (book->priv->proxy, l, remove_contact_by_id_reply, data);
UNLOCK_CONN ();
return 0;
}
static void
remove_contacts_reply (DBusGProxy *proxy, GError *error, gpointer user_data)
{
AsyncData *data = user_data;
EBookCallback cb = data->callback;
if (cb)
cb (data->book, get_status_from_error (error), data->closure);
g_object_unref (data->book);
g_slice_free (AsyncData, data);
}
/**
* e_book_async_remove_contacts:
* @book: an #EBook
* @ids: a #GList of const gchar *id's
* @cb: a function to call when the operation finishes
* @closure: data to pass to callback function
*
* Removes the contacts with ids from the list @ids from @book. This is
* always more efficient than calling e_book_remove_contact() if you
* have more than one id to remove, as some backends can implement it
* as a batch request.
*
* Returns: %TRUE if successful, %FALSE otherwise
**/
guint
e_book_async_remove_contacts (EBook *book,
GList *ids,
EBookCallback cb,
gpointer closure)
{
AsyncData *data;
gchar **l;
e_return_async_error_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_async_error_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
if (ids == NULL) {
if (cb)
cb (book, E_BOOK_ERROR_OK, closure);
return 0;
}
l = flatten_stringlist (ids);
data = g_slice_new0 (AsyncData);
data->book = g_object_ref (book);
data->callback = cb;
data->closure = closure;
LOCK_CONN ();
org_gnome_evolution_dataserver_addressbook_Book_remove_contacts_async (book->priv->proxy, (const gchar **) l, remove_contacts_reply, data);
UNLOCK_CONN ();
g_free (l);
return 0;
}
/**
* e_book_get_book_view:
* @book: an #EBook
* @query: an #EBookQuery
* @requested_fields: a #GList containing the names of fields to return, or NULL for all
* @max_results: the maximum number of contacts to show (or 0 for all)
* @book_view: A #EBookView pointer, will be set to the view
* @error: a #GError to set on failure
*
* Query @book with @query, creating a #EBookView in @book_view with the fields
* specified by @requested_fields and limited at @max_results records. On an
* error, @error is set and %FALSE returned.
*
* Returns: %TRUE if successful, %FALSE otherwise
**/
gboolean
e_book_get_book_view (EBook *book,
EBookQuery *query,
GList *requested_fields,
gint max_results,
EBookView **book_view,
GError **error)
{
GError *err = NULL;
DBusGProxy *view_proxy;
gchar *sexp, *view_path;
gboolean ret = TRUE;
e_return_error_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_error_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
sexp = e_book_query_to_string (query);
LOCK_CONN ();
if (!org_gnome_evolution_dataserver_addressbook_Book_get_book_view (book->priv->proxy, sexp, max_results, &view_path, &err)) {
UNLOCK_CONN ();
*book_view = NULL;
g_free (sexp);
return unwrap_gerror (err, error);
}
view_proxy = dbus_g_proxy_new_for_name_owner (connection,
E_DATA_BOOK_FACTORY_SERVICE_NAME, view_path,
"org.gnome.evolution.dataserver.addressbook.BookView", error);
UNLOCK_CONN ();
if (view_proxy) {
*book_view = _e_book_view_new (book, view_proxy, &connection_lock);
} else {
*book_view = NULL;
g_set_error (error, E_BOOK_ERROR, E_BOOK_ERROR_CORBA_EXCEPTION,
"Cannot get connection to view");
ret = FALSE;
}
g_free (view_path);
g_free (sexp);
return ret;
}
static void
get_book_view_reply (DBusGProxy *proxy, gchar *view_path, GError *error, gpointer user_data)
{
AsyncData *data = user_data;
GError *err = NULL;
EBookView *view = NULL;
EBookBookViewCallback cb = data->callback;
DBusGProxy *view_proxy;
EBookStatus status;
if (view_path) {
LOCK_CONN ();
view_proxy = dbus_g_proxy_new_for_name_owner (connection, E_DATA_BOOK_FACTORY_SERVICE_NAME, view_path,
"org.gnome.evolution.dataserver.addressbook.BookView", &err);
UNLOCK_CONN ();
if (view_proxy) {
view = _e_book_view_new (data->book, view_proxy, &connection_lock);
status = E_BOOK_ERROR_OK;
} else {
g_warning (G_STRLOC ": cannot get connection to view: %s", err->message);
g_error_free (err);
status = E_BOOK_ERROR_CORBA_EXCEPTION;
}
} else {
status = get_status_from_error (error);
}
if (cb)
cb (data->book, status, view, data->closure);
g_object_unref (data->book);
g_slice_free (AsyncData, data);
}
/**
* e_book_async_get_book_view:
* @book: an #EBook
* @query: an #EBookQuery
* @requested_fields: a #GList containing the names of fields to return, or NULL for all
* @max_results: the maximum number of contacts to show (or 0 for all)
* @cb: a function to call when the operation finishes
* @closure: data to pass to callback function
*
* Query @book with @query, creating a #EBookView with the fields
* specified by @requested_fields and limited at @max_results records.
*
* Returns: %FALSE if successful, %TRUE otherwise
**/
guint
e_book_async_get_book_view (EBook *book,
EBookQuery *query,
GList *requested_fields,
gint max_results,
EBookBookViewCallback cb,
gpointer closure)
{
AsyncData *data;
gchar *sexp;
e_return_async_error_val_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_async_error_val_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
e_return_async_error_val_if_fail (query, E_BOOK_ERROR_INVALID_ARG);
data = g_slice_new0 (AsyncData);
data->book = g_object_ref (book);
data->callback = cb;
data->closure = closure;
sexp = e_book_query_to_string (query);
LOCK_CONN ();
org_gnome_evolution_dataserver_addressbook_Book_get_book_view_async (book->priv->proxy, sexp, max_results, get_book_view_reply, data);
UNLOCK_CONN ();
g_free (sexp);
return 0;
}
/**
* e_book_get_contacts:
* @book: an #EBook
* @query: an #EBookQuery
* @contacts: a #GList pointer, will be set to the list of contacts
* @error: a #GError to set on failure
*
* Query @book with @query, setting @contacts to the list of contacts which
* matched. On failed, @error will be set and %FALSE returned.
*
* Returns: %TRUE on success, %FALSE otherwise
**/
gboolean
e_book_get_contacts (EBook *book,
EBookQuery *query,
GList **contacts,
GError **error)
{
GError *err = NULL;
gchar **list = NULL;
gchar *sexp;
e_return_error_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_error_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
sexp = e_book_query_to_string (query);
LOCK_CONN ();
org_gnome_evolution_dataserver_addressbook_Book_get_contact_list (book->priv->proxy, sexp, &list, &err);
UNLOCK_CONN ();
g_free (sexp);
if (!err) {
GList *l = NULL;
gchar **i = list;
while (*i != NULL) {
l = g_list_prepend (l, e_contact_new_from_vcard (*i++));
}
*contacts = g_list_reverse (l);
g_strfreev (list);
return TRUE;
} else {
return unwrap_gerror (err, error);
}
}
static void
get_contacts_reply(DBusGProxy *proxy, gchar **vcards, GError *error, gpointer user_data)
{
AsyncData *data = user_data;
GList *list = NULL;
EBookListCallback cb = data->callback;
if (!error && vcards) {
gchar **i = vcards;
while (*i != NULL) {
list = g_list_prepend (list, e_contact_new_from_vcard (*i++));
}
g_strfreev (vcards);
list = g_list_reverse (list);
}
if (cb)
cb (data->book, get_status_from_error (error), list, data->closure);
g_object_unref (data->book);
g_slice_free (AsyncData, data);
}
/**
* e_book_async_get_contacts:
* @book: an #EBook
* @query: an #EBookQuery
* @cb: a function to call when the operation finishes
* @closure: data to pass to callback function
*
* Query @book with @query.
*
* Returns: %FALSE on success, %TRUE otherwise
**/
guint
e_book_async_get_contacts (EBook *book,
EBookQuery *query,
EBookListCallback cb,
gpointer closure)
{
AsyncData *data;
gchar *sexp;
e_return_async_error_val_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_async_error_val_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
e_return_async_error_val_if_fail (query, E_BOOK_ERROR_INVALID_ARG);
sexp = e_book_query_to_string (query);
data = g_slice_new0 (AsyncData);
data->book = g_object_ref (book);
data->callback = cb;
data->closure = closure;
LOCK_CONN ();
org_gnome_evolution_dataserver_addressbook_Book_get_contact_list_async (book->priv->proxy, sexp, get_contacts_reply, data);
UNLOCK_CONN ();
g_free (sexp);
return 0;
}
static GList *
parse_changes_array (GPtrArray *array)
{
GList *l = NULL;
gint i;
if (array == NULL)
return NULL;
for (i = 0; i < array->len; i++) {
EBookChange *change;
GValueArray *vals;
vals = g_ptr_array_index (array, i);
change = g_slice_new (EBookChange);
change->change_type = g_value_get_uint (g_value_array_get_nth (vals, 0));
change->contact = e_contact_new_from_vcard
(g_value_get_string (g_value_array_get_nth (vals, 1)));
l = g_list_prepend (l, change);
}
g_ptr_array_foreach (array, (GFunc)g_value_array_free, NULL);
g_ptr_array_free (array, TRUE);
return g_list_reverse (l);
}
/**
* e_book_get_changes:
* @book: an #EBook
* @changeid: the change ID
* @changes: return location for a #GList of #EBookChange items
* @error: a #GError to set on failure.
*
* Get the set of changes since the previous call to #e_book_get_changes for a
* given change ID.
*
* Returns: TRUE on success, FALSE otherwise
*/
gboolean
e_book_get_changes (EBook *book,
const gchar *changeid,
GList **changes,
GError **error)
{
GError *err = NULL;
GPtrArray *array = NULL;
e_return_error_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_error_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
LOCK_CONN ();
org_gnome_evolution_dataserver_addressbook_Book_get_changes (book->priv->proxy, changeid, &array, &err);
UNLOCK_CONN ();
if (!err) {
*changes = parse_changes_array (array);
return TRUE;
} else {
return unwrap_gerror (err, error);
}
}
static void
get_changes_reply (DBusGProxy *proxy, GPtrArray *changes, GError *error, gpointer user_data)
{
AsyncData *data = user_data;
EBookListCallback cb = data->callback;
GList *list = NULL;
if (changes)
list = parse_changes_array (changes);
if (cb)
cb (data->book, get_status_from_error (error), list, data->closure);
g_object_unref (data->book);
g_slice_free (AsyncData, data);
}
/**
* e_book_async_get_changes:
* @book: an #EBook
* @changeid: the change ID
* @cb: function to call when operation finishes
* @closure: data to pass to callback function
*
* Get the set of changes since the previous call to #e_book_async_get_changes
* for a given change ID.
*
* Returns: TRUE on success, FALSE otherwise
*/
guint
e_book_async_get_changes (EBook *book,
const gchar *changeid,
EBookListCallback cb,
gpointer closure)
{
AsyncData *data;
e_return_async_error_val_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_async_error_val_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
data = g_slice_new0 (AsyncData);
data->book = g_object_ref (book);
data->callback = cb;
data->closure = closure;
LOCK_CONN ();
org_gnome_evolution_dataserver_addressbook_Book_get_changes_async (book->priv->proxy, changeid, get_changes_reply, data);
UNLOCK_CONN ();
return 0;
}
/**
* e_book_free_change_list:
* @change_list: a #GList of #EBookChange items
*
* Free the contents of #change_list, and the list itself.
*/
void
e_book_free_change_list (GList *change_list)
{
GList *l;
for (l = change_list; l; l = l->next) {
EBookChange *change = l->data;
g_object_unref (change->contact);
g_slice_free (EBookChange, change);
}
g_list_free (change_list);
}
/**
* e_book_cancel:
* @book: an #EBook
* @error: a #GError to set on failure
*
* Used to cancel an already running operation on @book. This
* function makes a synchronous CORBA to the backend telling it to
* cancel the operation. If the operation wasn't cancellable (either
* transiently or permanently) or had already comopleted on the server
* side, this function will return E_BOOK_STATUS_COULD_NOT_CANCEL, and
* the operation will continue uncancelled. If the operation could be
* cancelled, this function will return E_BOOK_ERROR_OK, and the
* blocked e_book function corresponding to current operation will
* return with a status of E_BOOK_STATUS_CANCELLED.
*
* Returns: %TRUE on success, %FALSE otherwise
**/
gboolean
e_book_cancel (EBook *book,
GError **error)
{
gboolean res;
e_return_error_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_error_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
LOCK_CONN ();
res = org_gnome_evolution_dataserver_addressbook_Book_cancel_operation (book->priv->proxy, error);
UNLOCK_CONN ();
return res;
}
/**
* e_book_cancel_async_op:
*
* Similar to above e_book_cancel function, only cancels last, still running,
* asynchronous operation.
*
* Since: 2.24
**/
gboolean
e_book_cancel_async_op (EBook *book, GError **error)
{
gboolean res;
e_return_error_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_error_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
LOCK_CONN ();
res = org_gnome_evolution_dataserver_addressbook_Book_cancel_operation (book->priv->proxy, error);
UNLOCK_CONN ();
return res;
}
/**
* e_book_open:
* @book: an #EBook
* @only_if_exists: if %TRUE, fail if this book doesn't already exist, otherwise create it first
* @error: a #GError to set on failure
*
* Opens the addressbook, making it ready for queries and other operations.
*
* Returns: %TRUE if the book was successfully opened, %FALSE otherwise.
*/
gboolean
e_book_open (EBook *book,
gboolean only_if_exists,
GError **error)
{
GError *err = NULL;
EBookStatus status;
e_return_error_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_error_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
LOCK_CONN ();
if (!org_gnome_evolution_dataserver_addressbook_Book_open (book->priv->proxy, only_if_exists, &err)) {
UNLOCK_CONN ();
g_propagate_error (error, err);
return FALSE;
}
UNLOCK_CONN ();
status = get_status_from_error (err);
if (status == E_BOOK_ERROR_OK) {
book->priv->loaded = TRUE;
return TRUE;
} else {
g_propagate_error (error, err);
return FALSE;
}
}
static void
open_reply(DBusGProxy *proxy, GError *error, gpointer user_data)
{
AsyncData *data = user_data;
EBookCallback cb = data->callback;
EDataBookStatus status;
status = get_status_from_error (error);
data->book->priv->loaded = (status == E_BOOK_ERROR_OK);
if (cb)
cb (data->book, status, data->closure);
g_object_unref (data->book);
g_slice_free (AsyncData, data);
}
/**
* e_book_async_open:
* @book: an #EBook
* @only_if_exists: if %TRUE, fail if this book doesn't already exist, otherwise create it first
* @open_response: a function to call when the operation finishes
* @closure: data to pass to callback function
*
* Opens the addressbook, making it ready for queries and other operations.
* This function does not block.
*
* Returns: %FALSE if successful, %TRUE otherwise.
**/
guint
e_book_async_open (EBook *book,
gboolean only_if_exists,
EBookCallback cb,
gpointer closure)
{
AsyncData *data;
e_return_async_error_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_async_error_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
data = g_slice_new0 (AsyncData);
data->book = g_object_ref (book);
data->callback = cb;
data->closure = closure;
LOCK_CONN ();
org_gnome_evolution_dataserver_addressbook_Book_open_async (book->priv->proxy, only_if_exists, open_reply, data);
UNLOCK_CONN ();
return 0;
}
/**
* e_book_remove:
* @book: an #EBook
* @error: a #GError to set on failure
*
* Removes the backing data for this #EBook. For example, with the file backend this
* deletes the database file. You cannot get it back!
*
* Returns: %TRUE on success, %FALSE on failure.
*/
gboolean
e_book_remove (EBook *book,
GError **error)
{
GError *err = NULL;
e_return_error_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_error_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
LOCK_CONN ();
org_gnome_evolution_dataserver_addressbook_Book_remove (book->priv->proxy, &err);
UNLOCK_CONN ();
return unwrap_gerror (err, error);
}
static void
remove_reply(DBusGProxy *proxy, GError *error, gpointer user_data)
{
AsyncData *data = user_data;
EBookCallback cb = data->callback;
if (cb)
cb (data->book, get_status_from_error (error), data->closure);
g_object_unref (data->book);
g_slice_free (AsyncData, data);
}
/**
* e_book_async_remove:
* @book: an #EBook
* @cb: a function to call when the operation finishes
* @closure: data to pass to callback function
*
* Remove the backing data for this #EBook. For example, with the file backend this
* deletes the database file. You cannot get it back!
*
* Returns: %FALSE if successful, %TRUE otherwise.
**/
guint
e_book_async_remove (EBook *book,
EBookCallback cb,
gpointer closure)
{
AsyncData *data;
e_return_async_error_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_async_error_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
data = g_slice_new0 (AsyncData);
data->book = g_object_ref (book);
data->callback = cb;
data->closure = closure;
LOCK_CONN ();
org_gnome_evolution_dataserver_addressbook_Book_remove_async (book->priv->proxy, remove_reply, data);
UNLOCK_CONN ();
return 0;
}
/**
* e_book_get_uri:
* @book: an #EBook
*
* Get the URI that this book has loaded. This string should not be freed.
*
* Returns: The URI.
*/
const gchar *
e_book_get_uri (EBook *book)
{
g_return_val_if_fail (E_IS_BOOK (book), NULL);
return book->priv->uri;
}
/**
* e_book_get_source:
* @book: an #EBook
*
* Get the #ESource that this book has loaded.
*
* Returns: The source.
*/
ESource *
e_book_get_source (EBook *book)
{
g_return_val_if_fail (E_IS_BOOK (book), NULL);
return book->priv->source;
}
/**
* e_book_get_static_capabilities:
* @book: an #EBook
* @error: an #GError to set on failure
*
* Get the list of capabilities which the backend for this address book
* supports. This string should not be freed.
*
* Returns: The capabilities list
*/
const gchar *
e_book_get_static_capabilities (EBook *book,
GError **error)
{
e_return_error_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_error_if_fail (book->priv->proxy, E_BOOK_ERROR_REPOSITORY_OFFLINE);
if (!book->priv->cap_queried) {
gchar *cap = NULL;
LOCK_CONN ();
if (!org_gnome_evolution_dataserver_addressbook_Book_get_static_capabilities (book->priv->proxy, &cap, error)) {
UNLOCK_CONN ();
return NULL;
}
UNLOCK_CONN ();
book->priv->cap = cap;
book->priv->cap_queried = TRUE;
}
return book->priv->cap;
}
/**
* e_book_check_static_capability:
* @book: an #EBook
* @cap: A capability string
*
* Check to see if the backend for this address book supports the capability
* @cap.
*
* Returns: %TRUE if the backend supports @cap, %FALSE otherwise.
*/
gboolean
e_book_check_static_capability (EBook *book,
const gchar *cap)
{
const gchar *caps;
g_return_val_if_fail (book && E_IS_BOOK (book), FALSE);
caps = e_book_get_static_capabilities (book, NULL);
/* XXX this is an inexact test but it works for our use */
if (caps && strstr (caps, cap))
return TRUE;
return FALSE;
}
/**
* e_book_is_opened:
* @book: and #EBook
*
* Check if this book has been opened.
*
* Returns: %TRUE if this book has been opened, otherwise %FALSE.
*/
gboolean
e_book_is_opened (EBook *book)
{
g_return_val_if_fail (E_IS_BOOK (book), FALSE);
return book->priv->loaded;
}
/**
* e_book_is_writable:
* @book: an #EBook
*
* Check if this book is writable.
*
* Returns: %TRUE if this book is writable, otherwise %FALSE.
*/
gboolean
e_book_is_writable (EBook *book)
{
g_return_val_if_fail (book && E_IS_BOOK (book), FALSE);
return book->priv->writable;
}
/**
* e_book_is_online:
* @book: an #EBook
*
* Check if this book is connected.
*
* Returns: %TRUE if this book is connected, otherwise %FALSE.
**/
gboolean
e_book_is_online (EBook *book)
{
g_return_val_if_fail (book && E_IS_BOOK (book), FALSE);
return book->priv->connected;
}
#define SELF_UID_KEY "/apps/evolution/addressbook/self/self_uid"
static EContact *
make_me_card (void)
{
GString *vcard;
const gchar *s;
EContact *contact;
vcard = g_string_new ("BEGIN:VCARD\nVERSION:3.0\n");
s = g_get_user_name ();
if (s)
g_string_append_printf (vcard, "NICKNAME:%s\n", s);
s = g_get_real_name ();
if (s && strcmp (s, "Unknown") != 0) {
ENameWestern *western;
g_string_append_printf (vcard, "FN:%s\n", s);
western = e_name_western_parse (s);
g_string_append_printf (vcard, "N:%s;%s;%s;%s;%s\n",
western->last ? western->last : "",
western->first ? western->first : "",
western->middle ? western->middle : "",
western->prefix ? western->prefix : "",
western->suffix ? western->suffix : "");
e_name_western_free (western);
}
g_string_append (vcard, "END:VCARD");
contact = e_contact_new_from_vcard (vcard->str);
g_string_free (vcard, TRUE);
return contact;
}
/**
* e_book_get_self:
* @contact: an #EContact pointer to set
* @book: an #EBook pointer to set
* @error: a #GError to set on failure
*
* Get the #EContact referring to the user of the address book
* and set it in @contact and @book.
*
* Returns: %TRUE if successful, otherwise %FALSE.
**/
gboolean
e_book_get_self (EContact **contact, EBook **book, GError **error)
{
GError *e = NULL;
GConfClient *gconf;
gboolean status;
gchar *uid;
*book = e_book_new_system_addressbook (&e);
if (!*book) {
if (error)
g_propagate_error (error, e);
return FALSE;
}
status = e_book_open (*book, FALSE, &e);
if (status == FALSE) {
g_object_unref (*book);
*book = NULL;
if (error)
g_propagate_error (error, e);
return FALSE;
}
gconf = gconf_client_get_default();
uid = gconf_client_get_string (gconf, SELF_UID_KEY, NULL);
g_object_unref (gconf);
if (uid) {
gboolean got;
/* Don't care about errors because we'll create a new card on failure */
got = e_book_get_contact (*book, uid, contact, NULL);
g_free (uid);
if (got)
return TRUE;
}
*contact = make_me_card ();
if (!e_book_add_contact (*book, *contact, &e)) {
/* TODO: return NULL or the contact anyway? */
g_object_unref (*book);
*book = NULL;
g_object_unref (*contact);
*contact = NULL;
if (error)
g_propagate_error (error, e);
return FALSE;
}
e_book_set_self (*book, *contact, NULL);
return TRUE;
}
/**
* e_book_set_self:
* @book: an #EBook
* @contact: an #EContact
* @error: a #GError to set on failure
*
* Specify that @contact residing in @book is the #EContact that
* refers to the user of the address book.
*
* Returns: %TRUE if successful, %FALSE otherwise.
**/
gboolean
e_book_set_self (EBook *book, EContact *contact, GError **error)
{
GConfClient *gconf;
e_return_error_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_error_if_fail (E_IS_CONTACT (contact), E_BOOK_ERROR_INVALID_ARG);
gconf = gconf_client_get_default();
gconf_client_set_string (gconf, SELF_UID_KEY, e_contact_get_const (contact, E_CONTACT_UID), NULL);
g_object_unref (gconf);
return TRUE;
}
/**
* e_book_is_self:
* @contact: an #EContact
*
* Check if @contact is the user of the address book.
*
* Returns: %TRUE if @contact is the user, %FALSE otherwise.
**/
gboolean
e_book_is_self (EContact *contact)
{
GConfClient *gconf;
gchar *uid;
gboolean rv;
/* XXX this should probably be e_return_error_if_fail, but we
need a GError** arg for that */
g_return_val_if_fail (contact && E_IS_CONTACT (contact), FALSE);
gconf = gconf_client_get_default();
uid = gconf_client_get_string (gconf, SELF_UID_KEY, NULL);
g_object_unref (gconf);
rv = (uid && !strcmp (uid, e_contact_get_const (contact, E_CONTACT_UID)));
g_free (uid);
return rv;
}
/**
* e_book_set_default_addressbook:
* @book: An #EBook pointer
* @error: A #GError pointer
*
* sets the #ESource of the #EBook as the "default" addressbook. This is the source
* that will be loaded in the e_book_get_default_addressbook call.
*
* Returns: %TRUE if the setting was stored in libebook's ESourceList, otherwise %FALSE.
*/
gboolean
e_book_set_default_addressbook (EBook *book, GError **error)
{
ESource *source;
e_return_error_if_fail (E_IS_BOOK (book), E_BOOK_ERROR_INVALID_ARG);
e_return_error_if_fail (book->priv->loaded == FALSE, E_BOOK_ERROR_SOURCE_ALREADY_LOADED);
source = e_book_get_source (book);
return e_book_set_default_source (source, error);
}
/**
* e_book_set_default_source:
* @source: An #ESource pointer
* @error: A #GError pointer
*
* sets @source as the "default" addressbook. This is the source that
* will be loaded in the e_book_get_default_addressbook call.
*
* Returns: %TRUE if the setting was stored in libebook's ESourceList, otherwise %FALSE.
*/
gboolean
e_book_set_default_source (ESource *source, GError **error)
{
ESourceList *sources;
const gchar *uid;
GError *err = NULL;
GSList *g;
e_return_error_if_fail (source && E_IS_SOURCE (source), E_BOOK_ERROR_INVALID_ARG);
uid = e_source_peek_uid (source);
if (!e_book_get_addressbooks (&sources, &err)) {
if (error)
g_propagate_error (error, err);
return FALSE;
}
/* make sure the source is actually in the ESourceList. if
it's not we don't bother adding it, just return an error */
source = e_source_list_peek_source_by_uid (sources, uid);
if (!source) {
g_set_error (error, E_BOOK_ERROR, E_BOOK_ERROR_NO_SUCH_SOURCE,
_("%s: there was no source for uid `%s' stored in gconf."), "e_book_set_default_source", uid);
g_object_unref (sources);
return FALSE;
}
/* loop over all the sources clearing out any "default"
properties we find */
for (g = e_source_list_peek_groups (sources); g; g = g->next) {
GSList *s;
for (s = e_source_group_peek_sources (E_SOURCE_GROUP (g->data));
s; s = s->next) {
e_source_set_property (E_SOURCE (s->data), "default", NULL);
}
}
/* set the "default" property on the source */
e_source_set_property (source, "default", "true");
if (!e_source_list_sync (sources, &err)) {
if (error)
g_propagate_error (error, err);
g_object_unref (sources);
return FALSE;
}
g_object_unref (sources);
return TRUE;
}
/**
* e_book_get_addressbooks:
* @addressbook_sources: A pointer to a ESourceList* to set
* @error: A pointer to a GError* to set on error
*
* Populate *addressbook_sources with the list of all sources which have been
* added to Evolution.
*
* Returns: %TRUE if @addressbook_sources was set, otherwise %FALSE.
*/
gboolean
e_book_get_addressbooks (ESourceList **addressbook_sources, GError **error)
{
GConfClient *gconf;
e_return_error_if_fail (addressbook_sources, E_BOOK_ERROR_INVALID_ARG);
gconf = gconf_client_get_default();
*addressbook_sources = e_source_list_new_for_gconf (gconf, "/apps/evolution/addressbook/sources");
g_object_unref (gconf);
return TRUE;
}
/**
* e_book_new:
* @source: An #ESource pointer
* @error: A #GError pointer
*
* Creates a new #EBook corresponding to the given source. There are
* only two operations that are valid on this book at this point:
* e_book_open(), and e_book_remove().
*
* Returns: a new but unopened #EBook.
*/
EBook*
e_book_new (ESource *source, GError **error)
{
GError *err = NULL;
EBook *book;
gchar *path, *xml;
e_return_error_if_fail (E_IS_SOURCE (source), E_BOOK_ERROR_INVALID_ARG);
if (!e_book_activate (&err)) {
g_warning (G_STRLOC ": cannot activate book: %s\n", err->message);
g_propagate_error (error, err);
return NULL;
}
book = g_object_new (E_TYPE_BOOK, NULL);
book->priv->source = g_object_ref (source);
book->priv->uri = e_source_get_uri (source);
xml = e_source_to_standalone_xml (source);
LOCK_CONN ();
if (!org_gnome_evolution_dataserver_addressbook_BookFactory_get_book (factory_proxy, xml, &path, &err)) {
UNLOCK_CONN ();
g_free (xml);
g_warning (G_STRLOC ": cannot get book from factory: %s", err ? err->message : "[no error]");
g_propagate_error (error, err);
g_object_unref (book);
return NULL;
}
g_free (xml);
book->priv->proxy = dbus_g_proxy_new_for_name_owner (connection,
E_DATA_BOOK_FACTORY_SERVICE_NAME, path,
"org.gnome.evolution.dataserver.addressbook.Book",
&err);
UNLOCK_CONN ();
if (!book->priv->proxy) {
g_warning (G_STRLOC ": cannot get proxy for book %s: %s", path, err->message);
g_propagate_error (error, err);
g_free (path);
g_object_unref (book);
return NULL;
}
g_free (path);
g_object_weak_ref (G_OBJECT (book->priv->proxy), proxy_destroyed, book);
dbus_g_proxy_add_signal (book->priv->proxy, "writable", G_TYPE_BOOLEAN, G_TYPE_INVALID);
dbus_g_proxy_connect_signal (book->priv->proxy, "writable", G_CALLBACK (writable_cb), book, NULL);
dbus_g_proxy_add_signal (book->priv->proxy, "connection", G_TYPE_BOOLEAN, G_TYPE_INVALID);
dbus_g_proxy_connect_signal (book->priv->proxy, "connection", G_CALLBACK (connection_cb), book, NULL);
dbus_g_proxy_add_signal (book->priv->proxy, "auth_required", G_TYPE_INVALID);
dbus_g_proxy_connect_signal (book->priv->proxy, "auth_required", G_CALLBACK (auth_required_cb), book, NULL);
return book;
}
/* for each known source calls check_func, which should return TRUE if the required
source have been found. Function returns NULL or the source on which was returned
TRUE by the check_func. Non-NULL pointer should be unreffed by g_object_unref. */
static ESource *
search_known_sources (gboolean (*check_func)(ESource *source, gpointer user_data), gpointer user_data, GError **error)
{
ESourceList *sources;
ESource *res = NULL;
GSList *g;
GError *err = NULL;
g_return_val_if_fail (check_func != NULL, NULL);
if (!e_book_get_addressbooks (&sources, &err)) {
g_propagate_error (error, err);
return NULL;
}
for (g = e_source_list_peek_groups (sources); g; g = g->next) {
ESourceGroup *group = E_SOURCE_GROUP (g->data);
GSList *s;
for (s = e_source_group_peek_sources (group); s; s = s->next) {
ESource *source = E_SOURCE (s->data);
if (check_func (source, user_data)) {
res = g_object_ref (source);
break;
}
}
if (res)
break;
}
g_object_unref (sources);
return res;
}
static gboolean
check_uri (ESource *source, gpointer uri)
{
const gchar *suri;
g_return_val_if_fail (source != NULL, FALSE);
g_return_val_if_fail (uri != NULL, FALSE);
suri = e_source_peek_absolute_uri (source);
return suri && g_ascii_strcasecmp (suri, uri) == 0;
}
/**
* e_book_new_from_uri:
* @uri: the URI to load
* @error: A #GError pointer
*
* Creates a new #EBook corresponding to the given uri. See the
* documentation for e_book_new for further information.
*
* Returns: a new but unopened #EBook.
*/
EBook*
e_book_new_from_uri (const gchar *uri, GError **error)
{
ESource *source;
EBook *book;
GError *err = NULL;
e_return_error_if_fail (uri, E_BOOK_ERROR_INVALID_ARG);
source = search_known_sources (check_uri, (gpointer) uri, &err);
if (err) {
g_propagate_error (error, err);
return NULL;
}
if (!source)
source = e_source_new_with_absolute_uri ("", uri);
book = e_book_new (source, &err);
if (err)
g_propagate_error (error, err);
g_object_unref (source);
return book;
}
struct check_system_data
{
const gchar *uri;
ESource *uri_source;
};
static gboolean
check_system (ESource *source, gpointer data)
{
struct check_system_data *csd = data;
g_return_val_if_fail (source != NULL, FALSE);
g_return_val_if_fail (data != NULL, FALSE);
if (e_source_get_property (source, "system")) {
return TRUE;
}
if (check_uri (source, (gpointer) csd->uri)) {
if (csd->uri_source)
g_object_unref (csd->uri_source);
csd->uri_source = g_object_ref (source);
}
return FALSE;
}
/**
* e_book_new_system_addressbook:
* @error: A #GError pointer
*
* Creates a new #EBook corresponding to the user's system
* addressbook. See the documentation for e_book_new for further
* information.
*
* Returns: a new but unopened #EBook.
*/
EBook*
e_book_new_system_addressbook (GError **error)
{
GError *err = NULL;
ESource *system_source = NULL;
EBook *book;
gchar *uri, *filename;
struct check_system_data csd;
filename = g_build_filename (g_get_home_dir(),
".evolution/addressbook/local/system",
NULL);
uri = g_filename_to_uri (filename, NULL, NULL);
g_free (filename);
csd.uri = uri;
csd.uri_source = NULL;
system_source = search_known_sources (check_system, &csd, &err);
if (err) {
g_propagate_error (error, err);
g_free (uri);
return NULL;
}
if (!system_source) {
system_source = csd.uri_source;
csd.uri_source = NULL;
}
if (system_source) {
book = e_book_new (system_source, &err);
g_object_unref (system_source);
} else {
book = e_book_new_from_uri (uri, &err);
}
if (csd.uri_source)
g_object_unref (csd.uri_source);
g_free (uri);
if (err)
g_propagate_error (error, err);
return book;
}
static gboolean
check_default (ESource *source, gpointer data)
{
g_return_val_if_fail (source != NULL, FALSE);
return e_source_get_property (source, "default") != NULL;
}
/**
* e_book_new_default_addressbook:
* @error: A #GError pointer
*
* Creates a new #EBook corresponding to the user's default
* addressbook. See the documentation for e_book_new for further
* information.
*
* Returns: a new but unopened #EBook.
*/
EBook*
e_book_new_default_addressbook (GError **error)
{
GError *err = NULL;
ESource *default_source = NULL;
EBook *book;
default_source = search_known_sources (check_default, NULL, &err);
if (err) {
g_propagate_error (error, err);
return NULL;
}
if (default_source) {
book = e_book_new (default_source, &err);
g_object_unref (default_source);
} else {
book = e_book_new_system_addressbook (&err);
}
if (err)
g_propagate_error (error, err);
return book;
}
/**
* If the specified GError is a remote error, then create a new error
* representing the remote error. If the error is anything else, then leave it
* alone.
*/
static gboolean
unwrap_gerror (GError *error, GError **client_error)
{
if (error == NULL)
return TRUE;
if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION) {
GError *new;
gint code;
if (client_error) {
code = get_status_from_error (error);
new = g_error_new_literal (E_BOOK_ERROR, code, error->message);
*client_error = new;
}
g_error_free (error);
} else {
if (client_error)
*client_error = error;
}
return FALSE;
}
static gboolean
strcaseequal_no_underscore (const gchar *str1, const gchar *str2)
{
gint i, j;
if (!str1 || !str2)
return FALSE;
for (i = 0, j = 0; str1[i] && str2[j]; i++, j++) {
while (str1[i] == '_' && str1[i + 1])
i++;
while (str2[j] == '_' && str2[j + 1])
j++;
if (g_ascii_tolower (str1[i]) != g_ascii_tolower (str2[j]))
return FALSE;
}
return !str1[i] && !str2[j];
}
/**
* If the GError is a remote error, extract the EBookStatus embedded inside.
* Otherwise return CORBA_EXCEPTION (I know this is DBus...).
*/
static EBookStatus
get_status_from_error (GError *error)
{
#define err(a,b) "org.gnome.evolution.dataserver.addressbook.Book." a, b
static struct {
const gchar *name;
EBookStatus err_code;
} errors[] = {
{ err ("E_DATA_BOOK_STATUS_SUCCESS", E_BOOK_ERROR_OK) },
{ err ("E_DATA_BOOK_STATUS_REPOSITORY_OFFLINE", E_BOOK_ERROR_REPOSITORY_OFFLINE) },
{ err ("E_DATA_BOOK_STATUS_PERMISSION_DENIED", E_BOOK_ERROR_PERMISSION_DENIED) },
{ err ("E_DATA_BOOK_STATUS_CONTACT_NOT_FOUND", E_BOOK_ERROR_CONTACT_NOT_FOUND) },
{ err ("E_DATA_BOOK_STATUS_CONTACTID_ALREADY_EXISTS", E_BOOK_ERROR_CONTACT_ID_ALREADY_EXISTS) },
{ err ("E_DATA_BOOK_STATUS_AUTHENTICATION_FAILED", E_BOOK_ERROR_AUTHENTICATION_FAILED) },
{ err ("E_DATA_BOOK_STATUS_AUTHENTICATION_REQUIRED", E_BOOK_ERROR_AUTHENTICATION_REQUIRED) },
{ err ("E_DATA_BOOK_STATUS_UNSUPPORTED_FIELD", E_BOOK_ERROR_OTHER_ERROR) },
{ err ("E_DATA_BOOK_STATUS_UNSUPPORTED_AUTHENTICATION_METHOD", E_BOOK_ERROR_UNSUPPORTED_AUTHENTICATION_METHOD) },
{ err ("E_DATA_BOOK_STATUS_TLS_NOT_AVAILABLE", E_BOOK_ERROR_TLS_NOT_AVAILABLE) },
{ err ("E_DATA_BOOK_STATUS_NO_SUCH_BOOK", E_BOOK_ERROR_NO_SUCH_BOOK) },
{ err ("E_DATA_BOOK_STATUS_BOOK_REMOVED", E_BOOK_ERROR_NO_SUCH_SOURCE) },
{ err ("E_DATA_BOOK_STATUS_OFFLINE_UNAVAILABLE", E_BOOK_ERROR_OFFLINE_UNAVAILABLE) },
{ err ("E_DATA_BOOK_STATUS_SEARCH_SIZE_LIMIT_EXCEEDED", E_BOOK_ERROR_OTHER_ERROR) },
{ err ("E_DATA_BOOK_STATUS_SEARCH_TIME_LIMIT_EXCEEDED", E_BOOK_ERROR_OTHER_ERROR) },
{ err ("E_DATA_BOOK_STATUS_INVALID_QUERY", E_BOOK_ERROR_OTHER_ERROR) },
{ err ("E_DATA_BOOK_STATUS_QUERY_REFUSED", E_BOOK_ERROR_OTHER_ERROR) },
{ err ("E_DATA_BOOK_STATUS_COULD_NOT_CANCEL", E_BOOK_ERROR_COULD_NOT_CANCEL) },
{ err ("E_DATA_BOOK_STATUS_OTHER_ERROR", E_BOOK_ERROR_OTHER_ERROR) },
{ err ("E_DATA_BOOK_STATUS_INVALID_SERVER_VERSION", E_BOOK_ERROR_INVALID_SERVER_VERSION) },
{ err ("E_DATA_BOOK_STATUS_NO_SPACE", E_BOOK_ERROR_NO_SPACE) }
};
#undef err
if G_LIKELY (error == NULL)
return E_BOOK_ERROR_OK;
if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION) {
const gchar *name;
gint i;
name = dbus_g_error_get_name (error);
for (i = 0; i < G_N_ELEMENTS (errors); i++) {
if (g_ascii_strcasecmp (errors[i].name, name) == 0 ||
strcaseequal_no_underscore (errors[i].name, name))
return errors[i].err_code;
}
g_warning (G_STRLOC ": unmatched error name %s", name);
return E_BOOK_ERROR_OTHER_ERROR;
} else {
/* In this case the error was caused by DBus. Dump the message to the
console as otherwise we have no idea what the problem is. */
g_warning ("DBus error: %s", error->message);
return E_BOOK_ERROR_CORBA_EXCEPTION;
}
}
/**
* Turn a GList of strings into an array of strings.
*/
static gchar **
flatten_stringlist (GList *list)
{
gchar **array = g_new0 (gchar *, g_list_length (list) + 1);
GList *l = list;
gint i = 0;
while (l != NULL) {
array[i++] = l->data;
l = l->next;
}
return array;
}
/**
* Turn an array of strings into a GList.
*/
static GList *
array_to_stringlist (gchar **list)
{
GList *l = NULL;
gchar **i = list;
while (*i != NULL) {
l = g_list_prepend (l, (*i++));
}
g_free (list);
return g_list_reverse(l);
}
|