/usr/lib/xul-ext/lightning/components/calStorageCalendar.js is in xul-ext-lightning 1.3+build1-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Oracle Corporation code.
*
* The Initial Developer of the Original Code is
* Oracle Corporation
* Portions created by the Initial Developer are Copyright (C) 2005, 2006
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
* Joey Minta <jminta@gmail.com>
* Dan Mosedale <dan.mosedale@oracle.com>
* Thomas Benisch <thomas.benisch@sun.com>
* Matthew Willis <lilmatt@mozilla.com>
* Philipp Kewisch <mozilla@kewis.ch>
* Daniel Boelzle <daniel.boelzle@sun.com>
* Sebastian Schwieger <sebo.moz@googlemail.com>
* Fred Jendrzejewski <fred.jen@web.de>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://calendar/modules/calUtils.jsm");
Components.utils.import("resource://calendar/modules/calAlarmUtils.jsm");
Components.utils.import("resource://calendar/modules/calProviderUtils.jsm");
Components.utils.import("resource://calendar/modules/calStorageUpgrade.jsm");
Components.utils.import("resource://calendar/modules/calStorageHelpers.jsm");
const USECS_PER_SECOND = 1000000;
const kCalICalendar = Components.interfaces.calICalendar;
const cICL = Components.interfaces.calIChangeLog;
//
// calStorageCalendar
//
function calStorageCalendar() {
this.initProviderBase();
this.mItemCache = {};
this.mRecEventCache = {};
this.mRecTodoCache = {};
}
calStorageCalendar.prototype = {
__proto__: cal.ProviderBase.prototype,
classID: Components.ID("{b3eaa1c4-5dfe-4c0a-b62a-b3a514218461}"),
contractID: "@mozilla.org/calendar/calendar;1?type=storage",
classDescription: "Calendar Storage Provider",
getInterfaces: function (count) {
let ifaces = [
Components.interfaces.nsISupports,
Components.interfaces.calICalendarManager,
Components.interfaces.calIStartupService,
Components.interfaces.nsIObserver,
Components.interfaces.nsIClassInfo
];
count.value = ifaces.length;
return ifaces;
},
getHelperForLanguage: function (language) {
return null;
},
implementationLanguage: Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT,
flags: 0,
//
// private members
//
mDB: null,
mItemCache: null,
mRecItemCacheInited: false,
mRecEventCache: null,
mRecTodoCache: null,
mLastStatement: null,
//
// nsISupports interface
//
QueryInterface: function (aIID) {
return cal.doQueryInterface(this, calStorageCalendar.prototype, aIID,
[Components.interfaces.calICalendarProvider,
Components.interfaces.calIOfflineStorage,
Components.interfaces.calISyncWriteCalendar]);
},
//
// calICalendarProvider interface
//
get prefChromeOverlay() {
return null;
},
get displayName() {
return cal.calGetString("calendar", "storageName");
},
createCalendar: function cSC_createCalendar() {
throw NS_ERROR_NOT_IMPLEMENTED;
},
deleteCalendar: function cSC_deleteCalendar(aCalendar, listener) {
aCalendar = aCalendar.wrappedJSObject;
for each (let stmt in this.mDeleteEventExtras) {
try {
this.prepareStatement(stmt);
stmt.execute();
} finally {
stmt.reset();
}
}
for each (let stmt in this.mDeleteTodoExtras) {
try {
this.prepareStatement(stmt);
stmt.execute();
} finally {
stmt.reset();
}
}
try {
this.prepareStatement(this.mDeleteAllEvents);
this.mDeleteAllEvents.execute();
} finally {
this.mDeleteAllEvents.reset();
}
try {
this.prepareStatement(this.mDeleteAllTodos);
this.mDeleteAllTodos.execute();
} finally {
this.mDeleteAllTodos.reset();
}
try {
this.prepareStatement(this.mDeleteAllMetaData);
this.mDeleteAllMetaData.execute();
} finally {
this.mDeleteAllMetaData.reset();
}
try {
if (listener) {
listener.onDeleteCalendar(aCalendar, Components.results.NS_OK, null);
}
} catch (ex) {
this.logError("error calling listener.onDeleteCalendar", ex);
}
},
mRelaxedMode: undefined,
get relaxedMode() {
if (this.mRelaxedMode === undefined) {
this.mRelaxedMode = this.getProperty("relaxedMode");
}
return this.mRelaxedMode;
},
//
// calICalendar interface
//
getProperty: function cSC_getProperty(aName) {
switch (aName) {
case "cache.supported":
return false;
case "requiresNetwork":
return false;
}
return this.__proto__.__proto__.getProperty.apply(this, arguments);
},
// readonly attribute AUTF8String type;
get type() { return "storage"; },
// attribute AUTF8String id;
get id() {
return this.__proto__.__proto__.__lookupGetter__("id").call(this);
},
set id(val) {
let id = this.__proto__.__proto__.__lookupSetter__("id").call(this, val);
if (!this.mDB && this.uri && this.id) {
// Prepare the database as soon as we have an id and an uri.
this.prepareInitDB();
}
return id;
},
// attribute nsIURI uri;
get uri() {
return this.__proto__.__proto__.__lookupGetter__("uri").call(this);
},
set uri(aUri) {
// We can only load once
if (this.uri) {
throw Components.results.NS_ERROR_FAILURE;
}
let uri = this.__proto__.__proto__.__lookupSetter__("uri").call(this, aUri);
if (!this.mDB && this.uri && this.id) {
// Prepare the database as soon as we have an id and an uri.
this.prepareInitDB();
}
return uri;
},
/**
* Initialize the Database. This should only be called from the uri or id
* setter and requires those two attributes to be set.
*/
prepareInitDB: function cSC_prepareInitDB() {
if (this.uri.schemeIs("file")) {
let fileURL = this.uri.QueryInterface(Components.interfaces.nsIFileURL);
if (!fileURL) {
throw new Components.Exception("Invalid file", Components.results.NS_ERROR_NOT_IMPLEMENTED);
}
// open the database
this.mDB = Services.storage.openDatabase(fileURL.file);
upgradeDB(this.mDB);
} else if (this.uri.schemeIs("moz-profile-calendar")) {
// This is an old-style moz-profile-calendar. It requires some
// migration steps.
let localDB = cal.getCalendarDirectory();
localDB.append("local.sqlite");
localDB = Services.storage.openDatabase(localDB);
// First, we need to check if this is from 0.9, i.e we need to
// migrate from storage.sdb to local.sqlite.
let storageSdb = Services.dirsvc.get("ProfD", Components.interfaces.nsILocalFile);
storageSdb.append("storage.sdb");
this.mDB = Services.storage.openDatabase(storageSdb);
if (this.mDB.tableExists("cal_events")) {
cal.LOG("[calStorageCalendar] Migrating storage.sdb -> local.sqlite");
upgradeDB(this.mDB); // upgrade schema before migating data
let attachStatement = createStatement(this.mDB, "ATTACH DATABASE :file_path AS local_sqlite");
try {
attachStatement.params.file_path = localDB.databaseFile.path;
attachStatement.execute();
} catch (exc) {
this.logError("prepareInitDB attachStatement.execute exception", exc);
throw exc;
} finally {
attachStatement.reset();
}
try {
// hold lock on storage.sdb until we've migrated data from storage.sdb:
this.mDB.beginTransactionAs(Components.interfaces.mozIStorageConnection.TRANSACTION_EXCLUSIVE);
try {
if (this.mDB.tableExists("cal_events")) { // check again (with lock)
// take over data and drop from storage.sdb tables:
for (let table in getSqlTable(DB_SCHEMA_VERSION)) {
if (table.substr(0, 4) != "idx_") {
this.mDB.executeSimpleSQL("CREATE TABLE local_sqlite." + table +
" AS SELECT * FROM " + table +
"; DROP TABLE IF EXISTS " + table);
}
}
this.mDB.commitTransaction();
} else { // migration done in the meantime
this.mDB.rollbackTransaction();
}
} catch (exc) {
this.logError("prepareInitDB storage.sdb migration exception", exc);
this.mDB.rollbackTransaction();
throw exc;
}
} finally {
this.mDB.executeSimpleSQL("DETACH DATABASE local_sqlite");
}
}
// Now that we are through, set the database to the new local.sqlite
// and start the upgraders.
this.mDB = localDB;
upgradeDB(this.mDB);
// Afterwards, we have to migrate the moz-profile-calendars to the
// new moz-storage-calendar schema. This is needed due to bug 479867
// and its regression bug 561735. The first calendar created before
// v19 already has a moz-profile-calendar:// uri without an ?id=
// parameter (the id in the databse is 0). We need to migrate this
// special calendar differently.
// WARNING: This is a somewhat fragile process. Great care should be
// taken during future schema upgrades to make sure this still
// works.
this.mDB.beginTransactionAs(Components.interfaces.mozIStorageConnection.TRANSACTION_EXCLUSIVE);
try {
/**
* Helper function to migrate all tables from one id to the next
*
* @param db The database to use
* @param newCalId The new calendar id to set
* @param oldCalId The old calendar id to look for
*/
function migrateTables(db, newCalId, oldCalId) {
for each (let tbl in ["cal_alarms", "cal_attachments",
"cal_attendees", "cal_events",
"cal_metadata", "cal_properties",
"cal_recurrence", "cal_relations",
"cal_todos"]) {
let stmt;
try {
stmt = createStatement(db, "UPDATE " + tbl +
" SET cal_id = :cal_id" +
" WHERE cal_id = :old_cal_id");
stmt.params.cal_id = newCalId;
stmt.params.old_cal_id = oldCalId;
stmt.execute();
} catch (e) {
// Pass error through to enclosing try/catch block
throw e;
} finally {
if (stmt) {
stmt.reset();
}
}
}
}
let id = 0;
let path = this.uri.path;
let pos = path.indexOf("?id=");
if (pos != -1) {
// There is an "id" parameter in the uri. This calendar
// has not been migrated to using the uuid as its cal_id.
pos = this.uri.path.indexOf("?id=");
if (pos != -1) {
cal.LOG("[calStorageCalendar] Migrating numeric cal_id to uuid");
id = parseInt(path.substr(pos + 4), 10);
migrateTables(this.mDB, this.id, id);
// Now remove the id from the uri to make sure we don't do this
// again. Remeber the id, so we can recover in case something
// goes wrong.
this.setProperty("uri", "moz-storage-calendar://");
this.setProperty("old_calendar_id", id);
this.mDB.commitTransaction();
} else {
this.mDB.rollbackTransaction();
}
} else {
// For some reason, the first storage calendar before the
// v19 upgrade has cal_id=0. If we still have a
// moz-profile-calendar here, then this is the one and we
// need to move all events with cal_id=0 to this id.
cal.LOG("[calStorageCalendar] Migrating stray cal_id=0 calendar to uuid");
migrateTables(this.mDB, this.id, 0);
this.setProperty("uri", "moz-storage-calendar://");
this.setProperty("old_calendar_id", 0);
this.mDB.commitTransaction();
}
} catch (exc) {
this.logError("prepareInitDB moz-profile-calendar migration exception", exc);
this.mDB.rollbackTransaction();
throw exc;
}
} else if (this.uri.schemeIs("moz-storage-calendar")) {
// New style uri, no need for migration here
let localDB = cal.getCalendarDirectory();
localDB.append("local.sqlite");
localDB = Services.storage.openDatabase(localDB);
this.mDB = localDB;
upgradeDB(this.mDB);
} else {
throw new Components.Exception("Invalid Scheme " + this.uri.spec);
}
this.initDB();
},
/**
* Takes care of necessary preparations for most of our statements.
*
* @param aStmt The statement to prepare.
*/
prepareStatement: function cSC_prepareStatement(aStmt) {
try {
aStmt.params.cal_id = this.id;
this.mLastStatement = aStmt;
} catch (e) {
this.logError("prepareStatement exception", e);
}
},
refresh: function cSC_refresh() {
// no-op
},
// void addItem( in calIItemBase aItem, in calIOperationListener aListener );
addItem: function cSC_addItem(aItem, aListener) {
let newItem = aItem.clone();
return this.adoptItem(newItem, aListener);
},
// void adoptItem( in calIItemBase aItem, in calIOperationListener aListener );
adoptItem: function cSC_adoptItem(aItem, aListener) {
if (this.readOnly) {
this.notifyOperationComplete(aListener,
Components.interfaces.calIErrors.CAL_IS_READONLY,
Components.interfaces.calIOperationListener.ADD,
null,
"Calendar is readonly");
return;
}
if (aItem.id == null) {
// is this an error? Or should we generate an IID?
aItem.id = cal.getUUID();
} else {
var olditem = this.getItemById(aItem.id);
if (olditem) {
if (this.relaxedMode) {
// we possibly want to interact with the user before deleting
this.deleteItemById(aItem.id);
} else {
this.notifyOperationComplete(aListener,
Components.interfaces.calIErrors.DUPLICATE_ID,
Components.interfaces.calIOperationListener.ADD,
aItem.id,
"ID already exists for addItem");
return;
}
}
}
let parentItem = aItem.parentItem;
if (parentItem != aItem) {
parentItem = parentItem.clone();
parentItem.recurrenceInfo.modifyException(aItem, true);
}
parentItem.calendar = this.superCalendar;
parentItem.makeImmutable();
this.flushItem(parentItem, null);
// notify the listener
this.notifyOperationComplete(aListener,
Components.results.NS_OK,
Components.interfaces.calIOperationListener.ADD,
aItem.id,
aItem);
// notify observers
this.observers.notify("onAddItem", [aItem]);
},
// void modifyItem( in calIItemBase aNewItem, in calIItemBase aOldItem, in calIOperationListener aListener );
// Actually uses doModifyItem
modifyItem: function cSC_modifyItem(aNewItem, aOldItem, aListener) {
let this_ = this;
// HACK Just modifying the item would clear the offline flag, we need to
// retrieve the flag and pass it to the real modify function.
let offlineJournalFlagListener = {
onGetResult: function (calendar, status, opType, id, detail) {
},
onOperationComplete: function (this_, status, opType, id, offlineFlag) {
this_.doModifyItem(aNewItem, aOldItem, aListener, offlineFlag);
}
};
this.getItemOfflineFlag(aOldItem, offlineJournalFlagListener);
},
doModifyItem: function cSC_doModifyItem(aNewItem, aOldItem, aListener, offlineFlag) {
let oldOfflineFlag = offlineFlag;
if (this.readOnly) {
this.notifyOperationComplete(aListener,
Components.interfaces.calIErrors.CAL_IS_READONLY,
Components.interfaces.calIOperationListener.MODIFY,
null,
"Calendar is readonly");
return null;
}
if (!aNewItem) {
throw Components.results.NS_ERROR_INVALID_ARG;
}
let this_ = this;
function reportError(errStr, errId) {
this_.notifyOperationComplete(aListener,
errId ? errId : Components.results.NS_ERROR_FAILURE,
Components.interfaces.calIOperationListener.MODIFY,
aNewItem.id,
errStr);
return null;
}
if (aNewItem.id == null) {
// this is definitely an error
return reportError("ID for modifyItem item is null");
}
// Ensure that we're looking at the base item if we were given an
// occurrence. Later we can optimize this.
let modifiedItem = aNewItem.parentItem.clone();
if (aNewItem.parentItem != aNewItem) {
modifiedItem.recurrenceInfo.modifyException(aNewItem, false);
}
// If no old item was passed, then we should overwrite in any case.
// Pick up the old item from the database and use this as an old item
// later on.
if (!aOldItem) {
aOldItem = this.getItemById(aNewItem.id);
}
if (this.relaxedMode) {
// We've already filled in the old item above, if this doesn't exist
// then just take the current item as its old version
if (!aOldItem) {
aOldItem = aNewItem;
}
aOldItem = aOldItem.parentItem;
} else {
var storedOldItem = (aOldItem ? this.getItemById(aOldItem.id) : null);
if (!aOldItem || !storedOldItem) {
// no old item found? should be using addItem, then.
return reportError("ID does not already exist for modifyItem");
}
aOldItem = aOldItem.parentItem;
if (aOldItem.generation != storedOldItem.generation) {
return reportError("generation too old for for modifyItem");
}
// xxx todo: this only modified master item's generation properties
// I start asking myself why we need a separate X-MOZ-GENERATION.
// Just for the sake of checking inconsistencies of modifyItem calls?
if (aOldItem.generation == modifiedItem.generation) { // has been cloned and modified
// Only take care of incrementing the generation if relaxed mode is
// off. Users of relaxed mode need to take care of this themselves.
modifiedItem.generation += 1;
}
}
modifiedItem.makeImmutable();
this.flushItem (modifiedItem, aOldItem);
this.setOfflineJournalFlag(aNewItem, oldOfflineFlag);
this.notifyOperationComplete(aListener,
Components.results.NS_OK,
Components.interfaces.calIOperationListener.MODIFY,
modifiedItem.id,
modifiedItem);
// notify observers
this.observers.notify("onModifyItem", [modifiedItem, aOldItem]);
return null;
},
// void deleteItem( in string id, in calIOperationListener aListener );
deleteItem: function cSC_deleteItem(aItem, aListener) {
if (this.readOnly) {
this.notifyOperationComplete(aListener,
Components.interfaces.calIErrors.CAL_IS_READONLY,
Components.interfaces.calIOperationListener.DELETE,
null,
"Calendar is readonly");
return;
}
if (aItem.parentItem != aItem) {
aItem.parentItem.recurrenceInfo.removeExceptionFor(aItem.recurrenceId);
// xxx todo: would we want to support this case? Removing an occurrence currently results
// in a modifyItem(parent)
return;
}
if (aItem.id == null) {
this.notifyOperationComplete(aListener,
Components.results.NS_ERROR_FAILURE,
Components.interfaces.calIOperationListener.DELETE,
null,
"ID is null for deleteItem");
return;
}
this.deleteItemById(aItem.id);
this.notifyOperationComplete(aListener,
Components.results.NS_OK,
Components.interfaces.calIOperationListener.DELETE,
aItem.id,
aItem);
// notify observers
this.observers.notify("onDeleteItem", [aItem]);
},
// void getItem( in string id, in calIOperationListener aListener );
getItem: function cSC_getItem(aId, aListener) {
if (!aListener)
return;
var item = this.getItemById (aId);
if (!item) {
// querying by id is a valid use case, even if no item is returned:
this.notifyOperationComplete(aListener,
Components.results.NS_OK,
Components.interfaces.calIOperationListener.GET,
aId,
null);
return;
}
var item_iid = null;
if (cal.isEvent(item)) {
item_iid = Components.interfaces.calIEvent;
} else if (cal.isToDo(item)) {
item_iid = Components.interfaces.calITodo;
} else {
this.notifyOperationComplete(aListener,
Components.results.NS_ERROR_FAILURE,
Components.interfaces.calIOperationListener.GET,
aId,
"Can't deduce item type based on QI");
return;
}
aListener.onGetResult (this.superCalendar,
Components.results.NS_OK,
item_iid, null,
1, [item]);
this.notifyOperationComplete(aListener,
Components.results.NS_OK,
Components.interfaces.calIOperationListener.GET,
aId,
null);
},
// void getItems( in unsigned long aItemFilter, in unsigned long aCount,
// in calIDateTime aRangeStart, in calIDateTime aRangeEnd,
// in calIOperationListener aListener );
getItems: function cSC_getItems(aItemFilter, aCount,
aRangeStart, aRangeEnd, aListener) {
let this_ = this;
cal.postPone(function() {
this_.getItems_(aItemFilter, aCount, aRangeStart, aRangeEnd, aListener);
});
},
getItems_: function cSC_getItems_(aItemFilter, aCount,
aRangeStart, aRangeEnd, aListener)
{
//var profStartTime = Date.now();
if (!aListener)
return;
var self = this;
var itemsFound = Array();
var startTime = -0x7fffffffffffffff;
// endTime needs to be the max value a PRTime can be
var endTime = 0x7fffffffffffffff;
var count = 0;
if (aRangeStart)
startTime = aRangeStart.nativeTime;
if (aRangeEnd)
endTime = aRangeEnd.nativeTime;
var wantUnrespondedInvitations = ((aItemFilter & kCalICalendar.ITEM_FILTER_REQUEST_NEEDS_ACTION) != 0);
var superCal;
try {
superCal = this.superCalendar.QueryInterface(Components.interfaces.calISchedulingSupport);
} catch (exc) {
wantUnrespondedInvitations = false;
}
function checkUnrespondedInvitation(item) {
var att = superCal.getInvitedAttendee(item);
return (att && (att.participationStatus == "NEEDS-ACTION"));
}
var wantEvents = ((aItemFilter & kCalICalendar.ITEM_FILTER_TYPE_EVENT) != 0);
var wantTodos = ((aItemFilter & kCalICalendar.ITEM_FILTER_TYPE_TODO) != 0);
var asOccurrences = ((aItemFilter & kCalICalendar.ITEM_FILTER_CLASS_OCCURRENCES) != 0);
var wantOfflineDeletedItems = ((aItemFilter & kCalICalendar.ITEM_FILTER_OFFLINE_DELETED) != 0);
var wantOfflineCreatedItems = ((aItemFilter & kCalICalendar.ITEM_FILTER_OFFLINE_CREATED) != 0);
var wantOfflineModifiedItems = ((aItemFilter & kCalICalendar.ITEM_FILTER_OFFLINE_MODIFIED) != 0);
if (!wantEvents && !wantTodos) {
// nothing to do
this.notifyOperationComplete(aListener,
Components.results.NS_OK,
Components.interfaces.calIOperationListener.GET,
null,
null);
return;
}
// HACK because recurring offline events/todos objects dont have offline_journal information
// Hence we need to update the mRecEventCacheOfflineFlags and mRecTodoCacheOfflineFlags hash-tables
// It can be an expensive operation but is only used in Online Reconciliation mode
if (wantOfflineCreatedItems | wantOfflineDeletedItems | wantOfflineModifiedItems) {
this.mRecItemCacheInited = false;
}
this.assureRecurringItemCaches();
var itemCompletedFilter = ((aItemFilter & kCalICalendar.ITEM_FILTER_COMPLETED_YES) != 0);
var itemNotCompletedFilter = ((aItemFilter & kCalICalendar.ITEM_FILTER_COMPLETED_NO) != 0);
function checkCompleted(item) {
return (item.isCompleted ? itemCompletedFilter : itemNotCompletedFilter);
}
// sending items to the listener 1 at a time sucks. instead,
// queue them up.
// if we ever have more than maxQueueSize items outstanding,
// call the listener. Calling with null theItems forces
// a send and a queue clear.
var maxQueueSize = 10;
var queuedItems = [ ];
var queuedItemsIID;
function queueItems(theItems, theIID) {
// if we're about to start sending a different IID,
// flush the queue
if (theIID && queuedItemsIID != theIID) {
if (queuedItemsIID)
queueItems(null);
queuedItemsIID = theIID;
}
if (theItems)
queuedItems = queuedItems.concat(theItems);
if (queuedItems.length != 0 && (!theItems || queuedItems.length > maxQueueSize)) {
//var listenerStart = Date.now();
aListener.onGetResult(self.superCalendar,
Components.results.NS_OK,
queuedItemsIID, null,
queuedItems.length, queuedItems);
//var listenerEnd = Date.now();
//dump ("++++ listener callback took: " + (listenerEnd - listenerStart) + " ms\n");
queuedItems = [ ];
}
}
// helper function to handle converting a row to an item,
// expanding occurrences, and queue the items for the listener
function handleResultItem(item, theIID, optionalFilterFunc) {
var expandedItems = [];
if (item.recurrenceInfo && asOccurrences) {
// If the item is recurring, get all ocurrences that fall in
// the range. If the item doesn't fall into the range at all,
// this expands to 0 items.
expandedItems = item.recurrenceInfo.getOccurrences(aRangeStart, aRangeEnd, 0, {});
if (wantUnrespondedInvitations) {
expandedItems = expandedItems.filter(checkUnrespondedInvitation);
}
} else if ((!wantUnrespondedInvitations || checkUnrespondedInvitation(item)) &&
checkIfInRange(item, aRangeStart, aRangeEnd)) {
// If no occurrences are wanted, check only the parent item.
// This will be changed with bug 416975.
expandedItems = [ item ];
}
if (expandedItems.length && optionalFilterFunc) {
expandedItems = expandedItems.filter(optionalFilterFunc);
}
queueItems (expandedItems, theIID);
return expandedItems.length;
}
// check the count and send end if count is exceeded
function checkCount() {
if (aCount && count >= aCount) {
// flush queue
queueItems(null);
// send operation complete
self.notifyOperationComplete(aListener,
Components.results.NS_OK,
Components.interfaces.calIOperationListener.GET,
null,
null);
// tell caller we're done
return true;
}
return false;
}
// First fetch all the events
if (wantEvents) {
var sp; // stmt params
var resultItems = [];
// first get non-recurring events that happen to fall within the range
//
try {
this.prepareStatement(this.mSelectNonRecurringEventsByRange);
sp = this.mSelectNonRecurringEventsByRange.params;
sp.range_start = startTime;
sp.range_end = endTime;
sp.start_offset = aRangeStart ? aRangeStart.timezoneOffset * USECS_PER_SECOND : 0;
sp.end_offset = aRangeEnd ? aRangeEnd.timezoneOffset * USECS_PER_SECOND : 0;
sp.offline_journal = null;
if (wantOfflineDeletedItems) sp.offline_journal = cICL.OFFLINE_FLAG_DELETED_RECORD;
else if (wantOfflineCreatedItems) sp.offline_journal = cICL.OFFLINE_FLAG_CREATED_RECORD;
else if (wantOfflineModifiedItems) sp.offline_journal = cICL.OFFLINE_FLAG_MODIFIED_RECORD;
while (this.mSelectNonRecurringEventsByRange.step()) {
let row = this.mSelectNonRecurringEventsByRange.row;
resultItems.push(this.getEventFromRow(row, {}));
}
} catch (e) {
this.logError("Error selecting non recurring events by range!\n", e);
} finally {
this.mSelectNonRecurringEventsByRange.reset();
}
// Process the non-recurring events:
for each (var evitem in resultItems) {
count += handleResultItem(evitem, Components.interfaces.calIEvent);
if (checkCount()) {
return;
}
}
// Process the recurring events from the cache
for each (let evitem in this.mRecEventCache) {
let offline_journal_flag = this.mRecEventCacheOfflineFlags[evitem.id] || null;
// No need to return flagged unless asked i.e. sp.offline_journal == offline_journal_flag
// Return created and modified offline records if sp.offline_journal is null alongwith events that have no flag
if ((sp.offline_journal == null && offline_journal_flag != cICL.OFFLINE_FLAG_DELETED_RECORD)
|| (sp.offline_journal != null && offline_journal_flag == sp.offline_journal)) {
count += handleResultItem(evitem, Components.interfaces.calIEvent);
if (checkCount()) {
return;
}
}
}
}
// if todos are wanted, do them next
if (wantTodos) {
var sp; // stmt params
var resultItems = [];
// first get non-recurring todos that happen to fall within the range
try {
this.prepareStatement(this.mSelectNonRecurringTodosByRange);
sp = this.mSelectNonRecurringTodosByRange.params;
sp.range_start = startTime;
sp.range_end = endTime;
sp.start_offset = aRangeStart ? aRangeStart.timezoneOffset * USECS_PER_SECOND : 0;
sp.end_offset = aRangeEnd ? aRangeEnd.timezoneOffset * USECS_PER_SECOND : 0;
sp.offline_journal = null;
if (wantOfflineCreatedItems) sp.offline_journal = cICL.OFFLINE_FLAG_CREATED_RECORD;
if (wantOfflineDeletedItems) sp.offline_journal = cICL.OFFLINE_FLAG_DELETED_RECORD;
if (wantOfflineModifiedItems) sp.offline_journal = cICL.OFFLINE_FLAG_MODIFIED_RECORD;
while (this.mSelectNonRecurringTodosByRange.step()) {
let row = this.mSelectNonRecurringTodosByRange.row;
resultItems.push(this.getTodoFromRow(row, {}));
}
} catch (e) {
this.logError("Error selecting non recurring todos by range", e);
} finally {
this.mSelectNonRecurringTodosByRange.reset();
}
// process the non-recurring todos:
for each (var todoitem in resultItems) {
count += handleResultItem(todoitem, Components.interfaces.calITodo, checkCompleted);
if (checkCount()) {
return;
}
}
// Note: Reading the code, completed *occurrences* seems to be broken, because
// only the parent item has been filtered; I fixed that.
// Moreover item.todo_complete etc seems to be a leftover...
// process the recurring todos from the cache
for each (let todoitem in this.mRecTodoCache) {
let offline_journal_flag = this.mRecTodoCacheOfflineFlags[todoitem.id] || null;
if ((sp.offline_journal == null &&
(offline_journal_flag == cICL.OFFLINE_FLAG_MODIFIED_RECORD ||
offline_journal_flag == cICL.OFFLINE_FLAG_CREATED_RECORD ||
offline_journal_flag == null)) ||
(sp.offline_journal != null &&
(offline_journal_flag == sp.offline_journal))) {
count += handleResultItem(todoitem,
Components.interfaces.calITodo,
checkCompleted);
if (checkCount()) {
return;
}
}
}
}
// flush the queue
queueItems(null);
// and finish
this.notifyOperationComplete(aListener,
Components.results.NS_OK,
Components.interfaces.calIOperationListener.GET,
null,
null);
//var profEndTime = Date.now();
//dump ("++++ getItems took: " + (profEndTime - profStartTime) + " ms\n");
},
getItemOfflineFlag: function cSC_getOfflineJournalFlag(aItem, aListener) {
let flag = null;
if (!aItem) {
// It is possible that aItem can be null, flag provided should be null in this case
aListener.onOperationComplete(this, Components.results.NS_OK,
Components.interfaces.calIOperationListener.GET, null, flag);
} else {
let aID = aItem.id;
let this_ = this;
let listener = {
handleResult: function(aResultSet) {
let row = aResultSet.getNextRow();
flag = row.getResultByName("offline_journal") || null;
},
handleError: function(aError) {
this_.logError("Error getting offline flag", aError);
aListener.onOperationComplete(this_, Components.results.NS_ERROR_FAILURE,
Components.interfaces.calIOperationListener.GET, aItem.id, aItem);
},
handleCompletion: function(aReason) {
aListener.onOperationComplete(this_, Components.results.NS_OK,
Components.interfaces.calIOperationListener.GET, aItem.id, flag);
}
};
if (cal.isEvent(aItem)) {
this.prepareStatement(this.mSelectEvent);
this.mSelectEvent.params.id = aID;
this.mSelectEvent.statement.executeAsync(listener);
} else if (cal.isToDo(aItem)) {
this.prepareStatement(this.mSelectTodo);
this.mSelectTodo.params.id = aID;
this.mSelectTodo.statement.executeAsync(listener);
}
}
},
setOfflineJournalFlag: function cSC_setOfflineJournalFlag(aItem, flag) {
let aID = aItem.id;
if (cal.isEvent(aItem)) {
this.prepareStatement(this.mEditEventOfflineFlag);
this.mEditEventOfflineFlag.params.id = aID;
this.mEditEventOfflineFlag.params.offline_journal = flag || null;
try {
this.mEditEventOfflineFlag.execute();
} catch (e) {
this.logError("Error setting offline journal flag for " + aItem.title, e);
} finally {
this.mEditEventOfflineFlag.reset();
}
} else if (cal.isToDo(aItem)) {
this.prepareStatement(this.mEditTodoOfflineFlag);
this.mEditTodoOfflineFlag.params.id = aID;
this.mEditTodoOfflineFlag.params.offline_journal = flag || null;
try {
this.mEditTodoOfflineFlag.execute();
} catch (e) {
this.logError("Error setting offline journal flag for " + aItem.title, e);
} finally {
this.mEditEventOfflineFlag.reset();
}
}
},
//
// calIOfflineStorage interface
//
addOfflineItem: function(aItem, aListener) {
let newOfflineJournalFlag = cICL.OFFLINE_FLAG_CREATED_RECORD;
this.setOfflineJournalFlag(aItem, newOfflineJournalFlag);
this.notifyOperationComplete(aListener,
Components.results.NS_OK,
Components.interfaces.calIOperationListener.ADD,
aItem.id,
aItem);
},
modifyOfflineItem: function(aItem, aListener) {
let this_ = this;
let opListener = {
onGetResult: function (calendar, status, itemType, detail, count, items) {
},
onOperationComplete: function(calendar, status, opType, id, oldOfflineJournalFlag ) {
let newOfflineJournalFlag = cICL.OFFLINE_FLAG_MODIFIED_RECORD;
if (oldOfflineJournalFlag == cICL.OFFLINE_FLAG_CREATED_RECORD || oldOfflineJournalFlag == cICL.OFFLINE_FLAG_DELETED_RECORD) {
// Do nothing since a flag of "created" or "deleted" exists
} else {
this_.setOfflineJournalFlag(aItem, newOfflineJournalFlag);
}
this_.notifyOperationComplete(aListener,
Components.results.NS_OK,
Components.interfaces.calIOperationListener.MODIFY,
aItem.id,
aItem);
}
};
this.getItemOfflineFlag(aItem, opListener);
},
deleteOfflineItem: function(aItem, aListener) {
let this_ = this;
let opListener = {
onGetResult: function (calendar, status, itemType, detail, count, items) {
},
onOperationComplete: function(calendar, status, opType, id, oldOfflineJournalFlag) {
var newOfflineJournalFlag = cICL.OFFLINE_FLAG_DELETED_RECORD;
if (oldOfflineJournalFlag) {
// Delete item if flag is c
if (oldOfflineJournalFlag == cICL.OFFLINE_FLAG_CREATED_RECORD) {
this_.deleteItemById(aItem.id);
} else if (oldOfflineJournalFlag == cICL.OFFLINE_FLAG_MODIFIED_RECORD) {
this_.setOfflineJournalFlag(aItem, cICL.OFFLINE_FLAG_DELETED_RECORD);
}
} else {
this_.setOfflineJournalFlag(aItem, cICL.OFFLINE_FLAG_DELETED_RECORD);
}
this_.notifyOperationComplete(aListener,
Components.results.NS_OK,
Components.interfaces.calIOperationListener.DELETE,
aItem.id,
aItem);
// notify observers
this_.observers.notify("onDeleteItem", [aItem]);
}
};
this.getItemOfflineFlag(aItem, opListener);
},
resetItemOfflineFlag: function(aItem, aListener) {
this.setOfflineJournalFlag(aItem,null);
this.notifyOperationComplete(aListener,
Components.results.NS_OK,
Components.interfaces.calIOperationListener.MODIFY,
aItem.id,
aItem);
},
//
// database handling
//
// database initialization
// assumes mDB is valid
initDB: function cSC_initDB() {
cal.ASSERT(this.mDB, "Database has not been opened!", true);
this.mSelectEvent = createStatement (
this.mDB,
"SELECT * FROM cal_events " +
"WHERE id = :id AND cal_id = :cal_id " +
" AND recurrence_id IS NULL " +
"LIMIT 1"
);
this.mSelectTodo = createStatement (
this.mDB,
"SELECT * FROM cal_todos " +
"WHERE id = :id AND cal_id = :cal_id " +
" AND recurrence_id IS NULL " +
"LIMIT 1"
);
// The more readable version of the next where-clause is:
// WHERE ((event_end > :range_start OR
// (event_end = :range_start AND
// event_start = :range_start))
// AND event_start < :range_end)
//
// but that doesn't work with floating start or end times. The logic
// is the same though.
// For readability, a few helpers:
var floatingEventStart = "event_start_tz = 'floating' AND event_start"
var nonFloatingEventStart = "event_start_tz != 'floating' AND event_start"
var floatingEventEnd = "event_end_tz = 'floating' AND event_end"
var nonFloatingEventEnd = "event_end_tz != 'floating' AND event_end"
// The query needs to take both floating and non floating into account
this.mSelectNonRecurringEventsByRange = createStatement(
this.mDB,
"SELECT * FROM cal_events " +
"WHERE " +
" (("+floatingEventEnd+" > :range_start + :start_offset) OR " +
" ("+nonFloatingEventEnd+" > :range_start) OR " +
" ((("+floatingEventEnd+" = :range_start + :start_offset) OR " +
" ("+nonFloatingEventEnd+" = :range_start)) AND " +
" (("+floatingEventStart+" = :range_start + :start_offset) OR " +
" ("+nonFloatingEventStart+" = :range_start)))) " +
" AND " +
" (("+floatingEventStart+" < :range_end + :end_offset) OR " +
" ("+nonFloatingEventStart+" < :range_end)) " +
" AND cal_id = :cal_id AND flags & 16 == 0 AND recurrence_id IS NULL" +
" AND ((:offline_journal IS NULL " +
" AND (offline_journal IS NULL " +
" OR offline_journal != " + cICL.OFFLINE_FLAG_DELETED_RECORD + ")) " +
" OR (offline_journal == :offline_journal))"
);
/**
* WHERE (due > rangeStart AND start < rangeEnd) OR
* (due = rangeStart AND start = rangeStart) OR
* (due IS NULL AND ((start >= rangeStart AND start < rangeEnd) OR
* (start IS NULL AND
* (completed > rangeStart OR completed IS NULL))) OR
* (start IS NULL AND due >= rangeStart AND due < rangeEnd)
*/
var floatingTodoEntry = "todo_entry_tz = 'floating' AND todo_entry";
var nonFloatingTodoEntry = "todo_entry_tz != 'floating' AND todo_entry";
var floatingTodoDue = "todo_due_tz = 'floating' AND todo_due";
var nonFloatingTodoDue = "todo_due_tz != 'floating' AND todo_due";
var floatingCompleted = "todo_completed_tz = 'floating' AND todo_completed";
var nonFloatingCompleted = "todo_completed_tz != 'floating' AND todo_completed";
this.mSelectNonRecurringTodosByRange = createStatement(
this.mDB,
"SELECT * FROM cal_todos " +
"WHERE " +
"(((("+floatingTodoDue+" > :range_start + :start_offset) OR " +
" ("+nonFloatingTodoDue+" > :range_start)) AND " +
" (("+floatingTodoEntry+" < :range_end + :end_offset) OR " +
" ("+nonFloatingTodoEntry+" < :range_end))) OR " +
" ((("+floatingTodoDue+" = :range_start + :start_offset) OR " +
" ("+nonFloatingTodoDue+" = :range_start)) AND " +
" (("+floatingTodoEntry+" = :range_start + :start_offset) OR " +
" ("+nonFloatingTodoEntry+" = :range_start))) OR " +
" ((todo_due IS NULL) AND " +
" ((("+floatingTodoEntry+" >= :range_start + :start_offset) OR " +
" ("+nonFloatingTodoEntry+" >= :range_start)) AND " +
" (("+floatingTodoEntry+" < :range_end + :end_offset) OR " +
" ("+nonFloatingTodoEntry+" < :range_end)))) OR " +
" ((todo_entry IS NULL) AND " +
" ((("+floatingCompleted+" > :range_start + :start_offset) OR " +
" ("+nonFloatingCompleted+" > :range_start)) OR " +
" (todo_completed IS NULL)))) " +
" AND cal_id = :cal_id AND flags & 16 == 0 AND recurrence_id IS NULL " +
" AND ((:offline_journal IS NULL" +
" AND (offline_journal IS NULL" +
" OR offline_journal != " + cICL.OFFLINE_FLAG_DELETED_RECORD + ")) " +
" OR (offline_journal == :offline_journal))"
);
this.mSelectEventsWithRecurrence = createStatement(
this.mDB,
"SELECT * FROM cal_events " +
" WHERE flags & 16 == 16 " +
" AND cal_id = :cal_id AND recurrence_id is NULL"
);
this.mSelectTodosWithRecurrence = createStatement(
this.mDB,
"SELECT * FROM cal_todos " +
" WHERE flags & 16 == 16 " +
" AND cal_id = :cal_id AND recurrence_id IS NULL"
);
this.mSelectEventExceptions = createStatement (
this.mDB,
"SELECT * FROM cal_events " +
"WHERE id = :id AND cal_id = :cal_id" +
" AND recurrence_id IS NOT NULL"
);
this.mSelectTodoExceptions = createStatement (
this.mDB,
"SELECT * FROM cal_todos " +
"WHERE id = :id AND cal_id = :cal_id" +
" AND recurrence_id IS NOT NULL"
);
// For the extra-item data, we used to use mDBTwo, so that
// these could be executed while a selectItems was running.
// This no longer seems to be needed and actually causes
// havoc when transactions are in use.
this.mSelectAttendeesForItem = createStatement(
this.mDB,
"SELECT * FROM cal_attendees " +
"WHERE item_id = :item_id AND cal_id = :cal_id" +
" AND recurrence_id IS NULL"
);
this.mSelectAttendeesForItemWithRecurrenceId = createStatement(
this.mDB,
"SELECT * FROM cal_attendees " +
"WHERE item_id = :item_id AND cal_id = :cal_id" +
" AND recurrence_id = :recurrence_id" +
" AND recurrence_id_tz = :recurrence_id_tz"
);
this.mSelectPropertiesForItem = createStatement(
this.mDB,
"SELECT * FROM cal_properties" +
" WHERE item_id = :item_id" +
" AND cal_id = :cal_id" +
" AND recurrence_id IS NULL"
);
this.mSelectPropertiesForItemWithRecurrenceId = createStatement(
this.mDB,
"SELECT * FROM cal_properties " +
"WHERE item_id = :item_id AND cal_id = :cal_id" +
" AND recurrence_id = :recurrence_id" +
" AND recurrence_id_tz = :recurrence_id_tz"
);
this.mSelectRecurrenceForItem = createStatement(
this.mDB,
"SELECT * FROM cal_recurrence " +
"WHERE item_id = :item_id AND cal_id = :cal_id" +
" ORDER BY recur_index"
);
this.mSelectAttachmentsForItem = createStatement(
this.mDB,
"SELECT * FROM cal_attachments " +
"WHERE item_id = :item_id AND cal_id = :cal_id" +
" AND recurrence_id IS NULL"
);
this.mSelectAttachmentsForItemWithRecurrenceId = createStatement(
this.mDB,
"SELECT * FROM cal_attachments" +
" WHERE item_id = :item_id AND cal_id = :cal_id" +
" AND recurrence_id = :recurrence_id" +
" AND recurrence_id_tz = :recurrence_id_tz"
);
this.mSelectRelationsForItem = createStatement(
this.mDB,
"SELECT * FROM cal_relations " +
"WHERE item_id = :item_id AND cal_id = :cal_id" +
" AND recurrence_id IS NULL"
);
this.mSelectRelationsForItemWithRecurrenceId = createStatement(
this.mDB,
"SELECT * FROM cal_relations" +
" WHERE item_id = :item_id AND cal_id = :cal_id" +
" AND recurrence_id = :recurrence_id" +
" AND recurrence_id_tz = :recurrence_id_tz"
);
this.mSelectMetaData = createStatement(
this.mDB,
"SELECT * FROM cal_metadata"
+ " WHERE item_id = :item_id AND cal_id = :cal_id");
this.mSelectAllMetaData = createStatement(
this.mDB,
"SELECT * FROM cal_metadata"
+ " WHERE cal_id = :cal_id");
this.mSelectAlarmsForItem = createStatement(
this.mDB,
"SELECT icalString FROM cal_alarms"
+ " WHERE item_id = :item_id AND cal_id = :cal_id" +
" AND recurrence_id IS NULL"
);
this.mSelectAlarmsForItemWithRecurrenceId = createStatement(
this.mDB,
"SELECT icalString FROM cal_alarms" +
" WHERE item_id = :item_id AND cal_id = :cal_id" +
" AND recurrence_id = :recurrence_id" +
" AND recurrence_id_tz = :recurrence_id_tz"
);
// insert statements
this.mInsertEvent = createStatement (
this.mDB,
"INSERT INTO cal_events " +
" (cal_id, id, time_created, last_modified, " +
" title, priority, privacy, ical_status, flags, " +
" event_start, event_start_tz, event_end, event_end_tz, event_stamp, " +
" recurrence_id, recurrence_id_tz, alarm_last_ack) " +
"VALUES (:cal_id, :id, :time_created, :last_modified, " +
" :title, :priority, :privacy, :ical_status, :flags, " +
" :event_start, :event_start_tz, :event_end, :event_end_tz, :event_stamp, " +
" :recurrence_id, :recurrence_id_tz, :alarm_last_ack)"
);
this.mInsertTodo = createStatement (
this.mDB,
"INSERT INTO cal_todos " +
" (cal_id, id, time_created, last_modified, " +
" title, priority, privacy, ical_status, flags, " +
" todo_entry, todo_entry_tz, todo_due, todo_due_tz, todo_stamp, " +
" todo_completed, todo_completed_tz, todo_complete, " +
" recurrence_id, recurrence_id_tz, alarm_last_ack)" +
"VALUES (:cal_id, :id, :time_created, :last_modified, " +
" :title, :priority, :privacy, :ical_status, :flags, " +
" :todo_entry, :todo_entry_tz, :todo_due, :todo_due_tz, :todo_stamp, " +
" :todo_completed, :todo_completed_tz, :todo_complete, " +
" :recurrence_id, :recurrence_id_tz, :alarm_last_ack)"
);
this.mInsertProperty = createStatement (
this.mDB,
"INSERT INTO cal_properties (cal_id, item_id, recurrence_id, recurrence_id_tz, key, value) " +
"VALUES (:cal_id, :item_id, :recurrence_id, :recurrence_id_tz, :key, :value)"
);
this.mInsertAttendee = createStatement (
this.mDB,
"INSERT INTO cal_attendees " +
" (cal_id, item_id, recurrence_id, recurrence_id_tz, attendee_id, common_name, rsvp, role, status, type, is_organizer, properties) " +
"VALUES (:cal_id, :item_id, :recurrence_id, :recurrence_id_tz, :attendee_id, :common_name, :rsvp, :role, :status, :type, :is_organizer, :properties)"
);
this.mInsertRecurrence = createStatement (
this.mDB,
"INSERT INTO cal_recurrence " +
" (cal_id, item_id, recur_index, recur_type, is_negative, dates, count, end_date, interval, second, minute, hour, day, monthday, yearday, weekno, month, setpos) " +
"VALUES (:cal_id, :item_id, :recur_index, :recur_type, :is_negative, :dates, :count, :end_date, :interval, :second, :minute, :hour, :day, :monthday, :yearday, :weekno, :month, :setpos)"
);
this.mInsertAttachment = createStatement (
this.mDB,
"INSERT INTO cal_attachments " +
" (cal_id, item_id, data, format_type, encoding, recurrence_id, recurrence_id_tz) " +
"VALUES (:cal_id, :item_id, :data, :format_type, :encoding, :recurrence_id, :recurrence_id_tz)"
);
this.mInsertRelation = createStatement (
this.mDB,
"INSERT INTO cal_relations " +
" (cal_id, item_id, rel_type, rel_id, recurrence_id, recurrence_id_tz) " +
"VALUES (:cal_id, :item_id, :rel_type, :rel_id, :recurrence_id, :recurrence_id_tz)"
);
this.mInsertMetaData = createStatement(
this.mDB,
"INSERT INTO cal_metadata"
+ " (cal_id, item_id, value)"
+ " VALUES (:cal_id, :item_id, :value)");
this.mInsertAlarm = createStatement(
this.mDB,
"INSERT INTO cal_alarms " +
" (cal_id, item_id, icalString, recurrence_id, recurrence_id_tz) " +
"VALUES (:cal_id, :item_id, :icalString, :recurrence_id, :recurrence_id_tz) "
);
//Offline Operations
this.mEditEventOfflineFlag = createStatement(
this.mDB,
"UPDATE cal_events SET offline_journal = :offline_journal" +
" WHERE id = :id AND cal_id = :cal_id"
);
this.mEditTodoOfflineFlag = createStatement(
this.mDB,
"UPDATE cal_todos SET offline_journal = :offline_journal" +
" WHERE id = :id AND cal_id = :cal_id"
);
// delete statements
this.mDeleteEvent = createStatement (
this.mDB,
"DELETE FROM cal_events WHERE id = :id AND cal_id = :cal_id"
);
this.mDeleteTodo = createStatement (
this.mDB,
"DELETE FROM cal_todos WHERE id = :id AND cal_id = :cal_id"
);
this.mDeleteAttendees = createStatement (
this.mDB,
"DELETE FROM cal_attendees WHERE item_id = :item_id AND cal_id = :cal_id"
);
this.mDeleteProperties = createStatement (
this.mDB,
"DELETE FROM cal_properties WHERE item_id = :item_id AND cal_id = :cal_id"
);
this.mDeleteRecurrence = createStatement (
this.mDB,
"DELETE FROM cal_recurrence WHERE item_id = :item_id AND cal_id = :cal_id"
);
this.mDeleteAttachments = createStatement (
this.mDB,
"DELETE FROM cal_attachments WHERE item_id = :item_id AND cal_id = :cal_id"
);
this.mDeleteRelations = createStatement (
this.mDB,
"DELETE FROM cal_relations WHERE item_id = :item_id AND cal_id = :cal_id"
);
this.mDeleteMetaData = createStatement(
this.mDB,
"DELETE FROM cal_metadata WHERE item_id = :item_id AND cal_id = :cal_id"
);
this.mDeleteAlarms = createStatement (
this.mDB,
"DELETE FROM cal_alarms WHERE item_id = :item_id AND cal_id = :cal_id"
);
// These are only used when deleting an entire calendar
var extrasTables = [ "cal_attendees", "cal_properties",
"cal_recurrence", "cal_attachments",
"cal_metadata", "cal_relations",
"cal_alarms"];
this.mDeleteEventExtras = new Array();
this.mDeleteTodoExtras = new Array();
for (var table in extrasTables) {
this.mDeleteEventExtras[table] = createStatement (
this.mDB,
"DELETE FROM " + extrasTables[table] + " WHERE item_id IN" +
" (SELECT id FROM cal_events WHERE cal_id = :cal_id)" +
" AND cal_id = :cal_id"
);
this.mDeleteTodoExtras[table] = createStatement (
this.mDB,
"DELETE FROM " + extrasTables[table] + " WHERE item_id IN" +
" (SELECT id FROM cal_todos WHERE cal_id = :cal_id)" +
" AND cal_id = :cal_id"
);
}
// Note that you must delete the "extras" _first_ using the above two
// statements, before you delete the events themselves.
this.mDeleteAllEvents = createStatement (
this.mDB,
"DELETE from cal_events WHERE cal_id = :cal_id"
);
this.mDeleteAllTodos = createStatement (
this.mDB,
"DELETE from cal_todos WHERE cal_id = :cal_id"
);
this.mDeleteAllMetaData = createStatement(
this.mDB,
"DELETE FROM cal_metadata" +
" WHERE cal_id = :cal_id"
);
},
//
// database reading functions
//
// read in the common ItemBase attributes from aDBRow, and stick
// them on item
getItemBaseFromRow: function cSC_getItemBaseFromRow(row, flags, item) {
item.calendar = this.superCalendar;
item.id = row.id;
if (row.title)
item.title = row.title;
if (row.priority)
item.priority = row.priority;
if (row.privacy)
item.privacy = row.privacy;
if (row.ical_status)
item.status = row.ical_status;
if (row.alarm_last_ack) {
// alarm acks are always in utc
item.alarmLastAck = newDateTime(row.alarm_last_ack, "UTC");
}
if (row.recurrence_id) {
item.recurrenceId = newDateTime(row.recurrence_id, row.recurrence_id_tz);
if ((row.flags & CAL_ITEM_FLAG.RECURRENCE_ID_ALLDAY) != 0) {
item.recurrenceId.isDate = true;
}
}
if (flags)
flags.value = row.flags;
if (row.time_created) {
item.setProperty("CREATED", newDateTime(row.time_created, "UTC"));
}
// This must be done last because the setting of any other property
// after this would overwrite it again.
if (row.last_modified) {
item.setProperty("LAST-MODIFIED", newDateTime(row.last_modified, "UTC"));
}
},
cacheItem: function cSC_cacheItem(item) {
this.mItemCache[item.id] = item;
if (item.recurrenceInfo) {
if (cal.isEvent(item)) {
this.mRecEventCache[item.id] = item;
} else {
this.mRecTodoCache[item.id] = item;
}
}
},
mRecEventCacheOfflineFlags: {},
mRecTodoCacheOfflineFlags : {},
assureRecurringItemCaches: function cSC_assureRecurringItemCaches() {
if (this.mRecItemCacheInited) {
return;
}
// build up recurring event and todo cache with its offline flags,
// because we need that on every query: for recurring items, we need to
// query database-wide.. yuck
try {
this.prepareStatement(this.mSelectEventsWithRecurrence);
let sp = this.mSelectEventsWithRecurrence.params;
while (this.mSelectEventsWithRecurrence.step()) {
let row = this.mSelectEventsWithRecurrence.row;
let item = this.getEventFromRow(row, {});
this.mRecEventCache[item.id] = item;
this.mRecEventCacheOfflineFlags[item.id] = row.offline_journal || null;
}
} catch (e) {
this.logError("Error selecting events with recurrence!", e);
} finally {
this.mSelectEventsWithRecurrence.reset();
}
try {
this.prepareStatement(this.mSelectTodosWithRecurrence);
sp = this.mSelectTodosWithRecurrence.params;
while (this.mSelectTodosWithRecurrence.step()) {
var row = this.mSelectTodosWithRecurrence.row;
var item = this.getTodoFromRow(row, {});
this.mRecTodoCache[item.id] = item;
this.mRecTodoCacheOfflineFlags[item.id] = row.offline_journal || null;
}
} catch (e) {
this.logError("Error selecting todos with recurrence!", e);
} finally {
this.mSelectTodosWithRecurrence.reset();
}
this.mRecItemCacheInited = true;
},
// xxx todo: consider removing flags parameter
getEventFromRow: function cSC_getEventFromRow(row, flags, isException) {
var item;
if (!isException) { // only parent items are cached
item = this.mItemCache[row.id];
if (item) {
return item;
}
}
item = createEvent();
if (row.event_start)
item.startDate = newDateTime(row.event_start, row.event_start_tz);
if (row.event_end)
item.endDate = newDateTime(row.event_end, row.event_end_tz);
if (row.event_stamp)
item.setProperty("DTSTAMP", newDateTime(row.event_stamp, "UTC"));
if ((row.flags & CAL_ITEM_FLAG.EVENT_ALLDAY) != 0) {
item.startDate.isDate = true;
item.endDate.isDate = true;
}
// This must be done last to keep the modification time intact.
this.getItemBaseFromRow (row, flags, item);
this.getAdditionalDataForItem(item, flags.value);
if (!isException) { // keep exceptions modifyable to set the parentItem
item.makeImmutable();
this.cacheItem(item);
}
return item;
},
getTodoFromRow: function cSC_getTodoFromRow(row, flags, isException) {
var item;
if (!isException) { // only parent items are cached
item = this.mItemCache[row.id];
if (item) {
return item;
}
}
item = createTodo();
if (row.todo_entry)
item.entryDate = newDateTime(row.todo_entry, row.todo_entry_tz);
if (row.todo_due)
item.dueDate = newDateTime(row.todo_due, row.todo_due_tz);
if (row.todo_stamp)
item.setProperty("DTSTAMP", newDateTime(row.todo_stamp, "UTC"));
if (row.todo_completed)
item.completedDate = newDateTime(row.todo_completed, row.todo_completed_tz);
if (row.todo_complete)
item.percentComplete = row.todo_complete;
// This must be done last to keep the modification time intact.
this.getItemBaseFromRow (row, flags, item);
this.getAdditionalDataForItem(item, flags.value);
if (!isException) { // keep exceptions modifyable to set the parentItem
item.makeImmutable();
this.cacheItem(item);
}
return item;
},
// after we get the base item, we need to check if we need to pull in
// any extra data from other tables. We do that here.
// We used to use mDBTwo for this, so this can be run while a
// select is executing but this no longer seems to be required.
getAdditionalDataForItem: function cSC_getAdditionalDataForItem(item, flags) {
// This is needed to keep the modification time intact.
var savedLastModifiedTime = item.lastModifiedTime;
if (flags & CAL_ITEM_FLAG.HAS_ATTENDEES) {
var selectItem = null;
if (item.recurrenceId == null) {
selectItem = this.mSelectAttendeesForItem;
} else {
selectItem = this.mSelectAttendeesForItemWithRecurrenceId;
this.setDateParamHelper(selectItem.params, "recurrence_id", item.recurrenceId);
}
try {
this.prepareStatement(selectItem);
selectItem.params.item_id = item.id;
while (selectItem.step()) {
var attendee = this.getAttendeeFromRow(selectItem.row);
if (attendee.isOrganizer) {
item.organizer = attendee;
} else {
item.addAttendee(attendee);
}
}
} catch (e) {
this.logError("Error getting attendees for item '" +
item.title + "' (" + item.id + ")!", e);
} finally {
selectItem.reset();
}
}
var row;
if (flags & CAL_ITEM_FLAG.HAS_PROPERTIES) {
var selectItem = null;
if (item.recurrenceId == null) {
selectItem = this.mSelectPropertiesForItem;
} else {
selectItem = this.mSelectPropertiesForItemWithRecurrenceId;
this.setDateParamHelper(selectItem.params, "recurrence_id", item.recurrenceId);
}
try {
this.prepareStatement(selectItem);
selectItem.params.item_id = item.id;
while (selectItem.step()) {
row = selectItem.row;
var name = row.key;
switch (name) {
case "DURATION":
// for events DTEND/DUE is enforced by calEvent/calTodo, so suppress DURATION:
break;
case "CATEGORIES": {
var cats = categoriesStringToArray(row.value);
item.setCategories(cats.length, cats);
break;
}
default:
item.setProperty(name, row.value);
break;
}
}
} catch (e) {
this.logError("Error getting extra properties for item '" +
item.title + "' (" + item.id + ")!", e);
} finally {
selectItem.reset();
}
}
var i;
if (flags & CAL_ITEM_FLAG.HAS_RECURRENCE) {
if (item.recurrenceId)
throw Components.results.NS_ERROR_UNEXPECTED;
var rec = null;
try {
this.prepareStatement(this.mSelectRecurrenceForItem);
this.mSelectRecurrenceForItem.params.item_id = item.id;
while (this.mSelectRecurrenceForItem.step()) {
row = this.mSelectRecurrenceForItem.row;
var ritem = null;
if (row.recur_type == null ||
row.recur_type == "x-dateset")
{
ritem = Components.classes["@mozilla.org/calendar/recurrence-date-set;1"]
.createInstance(Components.interfaces.calIRecurrenceDateSet);
var dates = row.dates.split(",");
for (i = 0; i < dates.length; i++) {
var date = textToDate(dates[i]);
ritem.addDate(date);
}
} else if (row.recur_type == "x-date") {
ritem = Components.classes["@mozilla.org/calendar/recurrence-date;1"]
.createInstance(Components.interfaces.calIRecurrenceDate);
var d = row.dates;
ritem.date = textToDate(d);
} else {
ritem = cal.createRecurrenceRule();
ritem.type = row.recur_type;
if (row.count) {
try {
ritem.count = row.count;
} catch (exc) {
}
} else {
if (row.end_date) {
ritem.untilDate = newDateTime(row.end_date, "UTC");
} else {
ritem.untilDate = null;
}
}
try {
ritem.interval = row.interval;
} catch (exc) {
}
var rtypes = ["second",
"minute",
"hour",
"day",
"monthday",
"yearday",
"weekno",
"month",
"setpos"];
for (i = 0; i < rtypes.length; i++) {
var comp = "BY" + rtypes[i].toUpperCase();
if (row[rtypes[i]]) {
var rstr = row[rtypes[i]].toString().split(",");
var rarray = [];
for (var j = 0; j < rstr.length; j++) {
rarray[j] = parseInt(rstr[j]);
}
ritem.setComponent (comp, rarray.length, rarray);
}
}
}
if (row.is_negative)
ritem.isNegative = true;
if (rec == null) {
rec = cal.createRecurrenceInfo(item);
}
rec.appendRecurrenceItem(ritem);
}
} catch (e) {
this.logError("Error getting recurrence for item '" +
item.title + "' (" + item.id + ")!", e);
} finally {
this.mSelectRecurrenceForItem.reset();
}
if (rec == null) {
dump ("XXXX Expected to find recurrence, but got no items!\n");
}
item.recurrenceInfo = rec;
}
if (flags & CAL_ITEM_FLAG.HAS_EXCEPTIONS) {
// it's safe that we don't run into this branch again for exceptions
// (getAdditionalDataForItem->get[Event|Todo]FromRow->getAdditionalDataForItem):
// every excepton has a recurrenceId and isn't flagged as CAL_ITEM_FLAG.HAS_EXCEPTIONS
if (item.recurrenceId)
throw Components.results.NS_ERROR_UNEXPECTED;
var rec = item.recurrenceInfo;
if (cal.isEvent(item)) {
this.mSelectEventExceptions.params.id = item.id;
this.prepareStatement(this.mSelectEventExceptions);
try {
while (this.mSelectEventExceptions.step()) {
var row = this.mSelectEventExceptions.row;
var exc = this.getEventFromRow(row, {}, true /*isException*/);
rec.modifyException(exc, true);
}
} catch (e) {
this.logError("Error getting exceptions for event '" +
item.title + "' (" + item.id + ")!", e);
} finally {
this.mSelectEventExceptions.reset();
}
} else if (cal.isToDo(item)) {
this.mSelectTodoExceptions.params.id = item.id;
this.prepareStatement(this.mSelectTodoExceptions);
try {
while (this.mSelectTodoExceptions.step()) {
var row = this.mSelectTodoExceptions.row;
var exc = this.getTodoFromRow(row, {}, true /*isException*/);
rec.modifyException(exc, true);
}
} catch (e) {
this.logError("Error getting exceptions for task '" +
item.title + "' (" + item.id + ")!", e);
} finally {
this.mSelectTodoExceptions.reset();
}
} else {
throw Components.results.NS_ERROR_UNEXPECTED;
}
}
if (flags & CAL_ITEM_FLAG.HAS_ATTACHMENTS) {
let selectAttachment = this.mSelectAttachmentsForItem;
if (item.recurrenceId != null) {
selectAttachment = this.mSelectAttachmentsForItemWithRecurrenceId;
this.setDateParamHelper(selectAttachment.params, "recurrence_id", item.recurrenceId);
}
try {
this.prepareStatement(selectAttachment);
selectAttachment.params.item_id = item.id;
while (selectAttachment.step()) {
let row = selectAttachment.row;
let attachment = this.getAttachmentFromRow(row);
item.addAttachment(attachment);
}
} catch (e) {
this.logError("Error getting attachments for item '" +
item.title + "' (" + item.id + ")!", e);
} finally {
selectAttachment.reset();
}
}
if (flags & CAL_ITEM_FLAG.HAS_RELATIONS) {
let selectRelation = this.mSelectRelationsForItem;
if (item.recurrenceId != null) {
selectRelation = this.mSelectRelationsForItemWithRecurrenceId;
this.setDateParamHelper(selectRelation.params, "recurrence_id", item.recurrenceId);
}
try {
this.prepareStatement(selectRelation);
selectRelation.params.item_id = item.id;
while (selectRelation.step()) {
let row = selectRelation.row;
let relation = this.getRelationFromRow(row);
item.addRelation(relation);
}
} catch (e) {
this.logError("Error getting relations for item '" +
item.title + "' (" + item.id + ")!", e);
} finally {
selectRelation.reset();
}
}
if (flags & CAL_ITEM_FLAG.HAS_ALARMS) {
let selectAlarm = this.mSelectAlarmsForItem;
if (item.recurrenceId != null) {
selectAlarm = this.mSelectAlarmsForItemWithRecurrenceId;
this.setDateParamHelper(selectAlarm.params, "recurrence_id", item.recurrenceId);
}
try {
selectAlarm.params.item_id = item.id;
this.prepareStatement(selectAlarm);
while (selectAlarm.step()) {
let row = selectAlarm.row;
let alarm = cal.createAlarm();
alarm.icalString = row.icalString;
item.addAlarm(alarm);
}
} catch (e) {
this.logError("Error getting alarms for item '" +
item.title + "' (" + item.id + ")!", e);
} finally {
selectAlarm.reset();
}
}
// Restore the saved modification time
item.setProperty("LAST-MODIFIED", savedLastModifiedTime);
},
getAttendeeFromRow: function cSC_getAttendeeFromRow(row) {
let a = cal.createAttendee();
a.id = row.attendee_id;
a.commonName = row.common_name;
switch (row.rsvp) {
case 0:
a.rsvp = "FALSE";
break;
case 1:
a.rsvp = "TRUE";
break;
// default: keep undefined
}
a.role = row.role;
a.participationStatus = row.status;
a.userType = row.type;
a.isOrganizer = row.is_organizer;
let props = row.properties;
if (props) {
for each (let pair in props.split(",")) {
[key, value] = pair.split(":");
a.setProperty(decodeURIComponent(key), decodeURIComponent(value));
}
}
return a;
},
getAttachmentFromRow: function cSC_getAttachmentFromRow(row) {
let a = cal.createAttachment();
// TODO we don't support binary data here, libical doesn't either.
a.uri = makeURL(row.data);
a.formatType = row.format_type;
a.encoding = row.encoding;
return a;
},
getRelationFromRow: function cSC_getRelationFromRow(row) {
let r = cal.createRelation();
r.relType = row.rel_type;
r.relId = row.rel_id;
return r;
},
//
// get item from db or from cache with given iid
//
getItemById: function cSC_getItemById(aID) {
this.assureRecurringItemCaches();
// cached?
var item = this.mItemCache[aID];
if (item) {
return item;
}
// not cached; need to read from the db
var flags = {};
try {
// try events first
this.prepareStatement(this.mSelectEvent);
this.mSelectEvent.params.id = aID;
if (this.mSelectEvent.step()) {
item = this.getEventFromRow(this.mSelectEvent.row, flags);
}
} catch (e) {
this.logError("Error selecting item by id " + aID + "!", e);
} finally {
this.mSelectEvent.reset();
}
// try todo if event fails
if (!item) {
try {
this.prepareStatement(this.mSelectTodo);
this.mSelectTodo.params.id = aID;
if (this.mSelectTodo.step()) {
item = this.getTodoFromRow(this.mSelectTodo.row, flags);
}
} catch (e) {
this.logError("Error selecting item by id " + aID + "!", e);
} finally {
this.mSelectTodo.reset();
}
}
return item;
},
//
// database writing functions
//
setDateParamHelper: function cSC_setDateParamHelper(params, entryname, cdt) {
if (cdt) {
params[entryname] = cdt.nativeTime;
var tz = cdt.timezone;
var ownTz = cal.getTimezoneService().getTimezone(tz.tzid);
if (ownTz) { // if we know that TZID, we use it
params[entryname + "_tz"] = ownTz.tzid;
} else if (!tz.icalComponent) { // timezone component missing
params[entryname + "_tz"] = "floating";
} else { // foreign one
params[entryname + "_tz"] = tz.icalComponent.serializeToICS();
}
} else {
params[entryname] = null;
params[entryname + "_tz"] = null;
}
},
flushItem: function cSC_flushItem(item, olditem) {
ASSERT(!item.recurrenceId, "no parent item passed!", true);
try {
this.deleteItemById(olditem ? olditem.id : item.id, !!olditem);
this.acquireTransaction();
this.writeItem(item, olditem);
} catch (e) {
this.releaseTransaction(e);
throw e;
}
this.releaseTransaction();
this.cacheItem(item);
},
//
// The write* functions execute the database bits
// to write the given item type. They're to return
// any bits they want or'd into flags, which will be passed
// to writeEvent/writeTodo to actually do the writing.
//
writeItem: function cSC_writeItem(item, olditem) {
var flags = 0;
flags |= this.writeAttendees(item, olditem);
flags |= this.writeRecurrence(item, olditem);
flags |= this.writeProperties(item, olditem);
flags |= this.writeAttachments(item, olditem);
flags |= this.writeRelations(item, olditem);
flags |= this.writeAlarms(item, olditem);
if (cal.isEvent(item)) {
this.writeEvent(item, olditem, flags);
} else if (cal.isToDo(item)) {
this.writeTodo(item, olditem, flags);
} else {
throw Components.results.NS_ERROR_UNEXPECTED;
}
},
writeEvent: function cSC_writeEvent(item, olditem, flags) {
try {
this.prepareStatement(this.mInsertEvent);
let ip = this.mInsertEvent.params;
this.setupItemBaseParams(item, olditem, ip);
this.setDateParamHelper(ip, "event_start", item.startDate);
this.setDateParamHelper(ip, "event_end", item.endDate);
let dtstamp = item.stampTime;
if (dtstamp) {
ip.event_stamp = dtstamp.nativeTime;
}
if (item.startDate.isDate) {
flags |= CAL_ITEM_FLAG.EVENT_ALLDAY;
}
ip.flags = flags;
this.mInsertEvent.execute();
} finally {
this.mInsertEvent.reset();
}
},
writeTodo: function cSC_writeTodo(item, olditem, flags) {
try {
this.prepareStatement(this.mInsertTodo);
let ip = this.mInsertTodo.params;
this.setupItemBaseParams(item, olditem, ip);
this.setDateParamHelper(ip, "todo_entry", item.entryDate);
this.setDateParamHelper(ip, "todo_due", item.dueDate);
let dtstamp = item.stampTime;
if (dtstamp) {
ip.todo_stamp = dtstamp.nativeTime;
}
this.setDateParamHelper(ip, "todo_completed", item.getProperty("COMPLETED"));
ip.todo_complete = item.getProperty("PERCENT-COMPLETED");
let someDate = (item.entryDate || item.dueDate);
if (someDate && someDate.isDate) {
flags |= CAL_ITEM_FLAG.EVENT_ALLDAY;
}
ip.flags = flags;
this.mInsertTodo.execute();
} finally {
this.mInsertTodo.reset();
}
},
setupItemBaseParams: function cSC_setupItemBaseParams(item, olditem, ip) {
ip.id = item.id;
if (item.recurrenceId) {
this.setDateParamHelper(ip, "recurrence_id", item.recurrenceId);
}
var tmp;
if ((tmp = item.getProperty("CREATED")))
ip.time_created = tmp.nativeTime;
if ((tmp = item.getProperty("LAST-MODIFIED")))
ip.last_modified = tmp.nativeTime;
ip.title = item.getProperty("SUMMARY");
ip.priority = item.getProperty("PRIORITY");
ip.privacy = item.getProperty("CLASS");
ip.ical_status = item.getProperty("STATUS");
if (item.alarmLastAck) {
ip.alarm_last_ack = item.alarmLastAck.nativeTime;
}
},
writeAttendees: function cSC_writeAttendees(item, olditem) {
var attendees = item.getAttendees({});
if (item.organizer) {
attendees = attendees.concat([]);
attendees.push(item.organizer);
}
if (attendees.length > 0) {
for each (var att in attendees) {
var ap = this.mInsertAttendee.params;
ap.item_id = item.id;
try {
this.prepareStatement(this.mInsertAttendee);
this.setDateParamHelper(ap, "recurrence_id", item.recurrenceId);
ap.attendee_id = att.id;
ap.common_name = att.commonName;
switch (att.rsvp) {
case "FALSE":
ap.rsvp = 0;
break;
case "TRUE":
ap.rsvp = 1;
break;
default:
ap.rsvp = 2;
break;
}
ap.role = att.role;
ap.status = att.participationStatus;
ap.type = att.userType;
ap.is_organizer = att.isOrganizer;
var props = "";
var propEnum = att.propertyEnumerator;
while (propEnum && propEnum.hasMoreElements()) {
var prop = propEnum.getNext().QueryInterface(Components.interfaces.nsIProperty);
if (props.length) {
props += ",";
}
props += encodeURIComponent(prop.name);
props += ":";
props += encodeURIComponent(prop.value);
}
if (props.length) {
ap.properties = props;
}
this.mInsertAttendee.execute();
} finally {
this.mInsertAttendee.reset();
}
}
return CAL_ITEM_FLAG.HAS_ATTENDEES;
}
return 0;
},
writeProperty: function cSC_writeProperty(item, propName, propValue) {
try {
this.prepareStatement(this.mInsertProperty);
var pp = this.mInsertProperty.params;
pp.key = propName;
if (calInstanceOf(propValue, Components.interfaces.calIDateTime)) {
pp.value = propValue.nativeTime;
} else {
try {
pp.value = propValue;
} catch (e) {
// The storage service throws an NS_ERROR_ILLEGAL_VALUE in
// case pval is something complex (i.e not a string or
// number). Swallow this error, leaving the value empty.
if (e.result != Components.results.NS_ERROR_ILLEGAL_VALUE) {
throw e;
}
}
}
pp.item_id = item.id;
this.setDateParamHelper(pp, "recurrence_id", item.recurrenceId);
this.mInsertProperty.execute();
} finally {
this.mInsertProperty.reset();
}
},
writeProperties: function cSC_writeProperties(item, olditem) {
var ret = 0;
var propEnumerator = item.propertyEnumerator;
while (propEnumerator.hasMoreElements()) {
ret = CAL_ITEM_FLAG.HAS_PROPERTIES;
var prop = propEnumerator.getNext().QueryInterface(Components.interfaces.nsIProperty);
if (item.isPropertyPromoted(prop.name))
continue;
this.writeProperty(item, prop.name, prop.value);
}
var cats = item.getCategories({});
if (cats.length > 0) {
ret = CAL_ITEM_FLAG.HAS_PROPERTIES;
this.writeProperty(item, "CATEGORIES", categoriesArrayToString(cats));
}
return ret;
},
writeRecurrence: function cSC_writeRecurrence(item, olditem) {
var flags = 0;
var rec = item.recurrenceInfo;
if (rec) {
flags = CAL_ITEM_FLAG.HAS_RECURRENCE;
var ritems = rec.getRecurrenceItems ({});
for (let i in ritems) {
var ritem = ritems[i];
var ap = this.mInsertRecurrence.params;
try {
this.prepareStatement(this.mInsertRecurrence);
ap.item_id = item.id;
ap.recur_index = i;
ap.is_negative = ritem.isNegative;
if (calInstanceOf(ritem, Components.interfaces.calIRecurrenceDate)) {
ap.recur_type = "x-date";
ap.dates = dateToText(getInUtcOrKeepFloating(ritem.date));
} else if (calInstanceOf(ritem, Components.interfaces.calIRecurrenceDateSet)) {
ap.recur_type = "x-dateset";
var rdates = ritem.getDates({});
var datestr = "";
for (j in rdates) {
if (j != 0)
datestr += ",";
datestr += dateToText(getInUtcOrKeepFloating(rdates[j]));
}
ap.dates = datestr;
} else if (calInstanceOf(ritem, Components.interfaces.calIRecurrenceRule)) {
ap.recur_type = ritem.type;
if (ritem.isByCount) {
ap.count = ritem.count;
} else {
ap.end_date = ritem.untilDate ? ritem.untilDate.nativeTime : null;
}
ap.interval = ritem.interval;
var rtypes = ["second",
"minute",
"hour",
"day",
"monthday",
"yearday",
"weekno",
"month",
"setpos"];
for (var j = 0; j < rtypes.length; j++) {
var comp = "BY" + rtypes[j].toUpperCase();
var comps = ritem.getComponent(comp, {});
if (comps && comps.length > 0) {
var compstr = comps.join(",");
ap[rtypes[j]] = compstr;
}
}
} else {
dump ("##### Don't know how to serialize recurrence item " + ritem + "!\n");
}
this.mInsertRecurrence.execute();
} finally {
this.mInsertRecurrence.reset();
}
}
var exceptions = rec.getExceptionIds ({});
if (exceptions.length > 0) {
flags |= CAL_ITEM_FLAG.HAS_EXCEPTIONS;
// we need to serialize each exid as a separate
// event/todo; setupItemBase will handle
// writing the recurrenceId for us
for each (let exid in exceptions) {
let ex = rec.getExceptionFor(exid);
if (!ex)
throw Components.results.NS_ERROR_UNEXPECTED;
this.writeItem(ex, null);
}
}
} else if (item.recurrenceId && item.recurrenceId.isDate) {
flags |= CAL_ITEM_FLAG.RECURRENCE_ID_ALLDAY;
}
return flags;
},
writeAttachments: function cSC_writeAttachments(item, olditem) {
let attachments = item.getAttachments({});
if (attachments && attachments.length > 0) {
for each (let att in attachments) {
let ap = this.mInsertAttachment.params;
try {
this.prepareStatement(this.mInsertAttachment);
this.setDateParamHelper(ap, "recurrence_id", item.recurrenceId);
ap.item_id = item.id;
ap.data = (att.uri ? att.uri.spec : "");
ap.format_type = att.formatType;
ap.encoding = att.encoding;
this.mInsertAttachment.execute();
} finally {
this.mInsertAttachment.reset();
}
}
return CAL_ITEM_FLAG.HAS_ATTACHMENTS;
}
return 0;
},
writeRelations: function cSC_writeRelations(item, olditem) {
let relations = item.getRelations({});
if (relations && relations.length > 0) {
for each (var rel in relations) {
let rp = this.mInsertRelation.params;
try {
this.prepareStatement(this.mInsertRelation);
this.setDateParamHelper(rp, "recurrence_id", item.recurrenceId);
rp.item_id = item.id;
rp.rel_type = rel.relType;
rp.rel_id = rel.relId;
this.mInsertRelation.execute();
} finally {
this.mInsertRelation.reset();
}
}
return CAL_ITEM_FLAG.HAS_RELATIONS;
}
return 0;
},
writeAlarms: function cSC_writeAlarms(item, olditem) {
let alarms = item.getAlarms({});
if (alarms.length < 1) {
return 0;
}
for each (let alarm in alarms) {
let pp = this.mInsertAlarm.params;
try {
this.prepareStatement(this.mInsertAlarm);
this.setDateParamHelper(pp, "recurrence_id", item.recurrenceId);
pp.item_id = item.id;
pp.icalString = alarm.icalString;
this.mInsertAlarm.execute();
} catch (e) {
this.logError("Error writing alarm for item " + item.title + " (" + item.id + ")", e);
} finally {
this.mInsertAlarm.reset();
}
}
return CAL_ITEM_FLAG.HAS_ALARMS;
},
/**
* Deletes the item with the given item id.
*
* @param aID The id of the item to delete.
* @param aIsModify If true, then leave in metadata for the item
*/
deleteItemById: function cSC_deleteItemById(aID, aIsModify) {
this.acquireTransaction();
try {
this.mDeleteAttendees(aID, this.id);
this.mDeleteProperties(aID, this.id);
this.mDeleteRecurrence(aID, this.id);
this.mDeleteEvent(aID, this.id);
this.mDeleteTodo(aID, this.id);
this.mDeleteAttachments(aID, this.id);
this.mDeleteRelations(aID, this.id);
if (!aIsModify) {
this.mDeleteMetaData(aID, this.id);
}
this.mDeleteAlarms(aID, this.id);
} catch (e) {
this.releaseTransaction(e);
throw e;
}
this.releaseTransaction();
delete this.mItemCache[aID];
delete this.mRecEventCache[aID];
delete this.mRecTodoCache[aID];
},
/**
* Acquire a transaction for this calendar.
*/
acquireTransaction: function cSC_acquireTransaction() {
this.mDB.beginTransaction();
},
/**
* Releases one level of transactions for this calendar.
*
* @param err (optional) If set, the transaction is set to fail when
* the count reaches zero.
*/
releaseTransaction: function cSC_releaseTransaction(err) {
if (err) {
cal.ERROR("[calStorageCalendar] DB error: " + this.mDB.lastErrorString + "\nexc: " + err);
this.mDB.rollbackTransaction();
} else {
this.mDB.commitTransaction();
}
},
//
// calISyncWriteCalendar interface
//
setMetaData: function cSC_setMetaData(id, value) {
this.mDeleteMetaData(id, this.id);
try {
this.prepareStatement(this.mInsertMetaData);
var sp = this.mInsertMetaData.params;
sp.item_id = id;
sp.value = value;
this.mInsertMetaData.execute();
} catch (e if e.result != Components.results.NS_ERROR_ILLEGAL_VALUE) {
// The storage service throws an NS_ERROR_ILLEGAL_VALUE in
// case pval is something complex (i.e not a string or
// number). Swallow this error, leaving the value empty.
this.logError("Error setting metadata for id " + id + "!", e);
} finally {
this.mInsertMetaData.reset();
}
},
deleteMetaData: function cSC_deleteMetaData(id) {
this.mDeleteMetaData(id, this.id);
},
getMetaData: function cSC_getMetaData(id) {
let query = this.mSelectMetaData;
let value = null;
try {
this.prepareStatement(query);
query.params.item_id = id;
if (query.step()) {
value = query.row.value;
}
} catch (e) {
this.logError("Error getting metadata for id " + id + "!", e);
} finally {
query.reset();
}
return value;
},
getAllMetaData: function cSC_getAllMetaData(out_count,
out_ids,
out_values) {
let query = this.mSelectAllMetaData;
try {
this.prepareStatement(query);
let ids = [];
let values = [];
while (query.step()) {
ids.push(query.row.item_id);
values.push(query.row.value);
}
out_count.value = ids.length;
out_ids.value = ids;
out_values.value = values;
} catch (e) {
this.logError("Error getting all metadata!", e);
} finally {
query.reset();
}
},
/**
* Internal logging function that should be called on any database error,
* it will log as much info as possible about the database context and
* last statement so the problem can be investigated more easilly.
*
* @param message Error message to log.
* @param exception Exception that caused the error.
*/
logError: function cSC_logError(message,exception) {
let logMessage = "Message: " + message;
if (this.mDB) {
if (this.mDB.connectionReady) {
logMessage += "\nConnection Ready: " + this.mDB.connectionReady;
}
if (this.mDB.lastError) {
logMessage += "\nLast DB Error Number: " + this.mDB.lastError;
}
if (this.mDB.lastErrorString) {
logMessage += "\nLast DB Error Message: " + this.mDB.lastErrorString;
}
if (this.mDB.databaseFile) {
logMessage += "\nDatabase File: " + this.mDB.databaseFile.path;
}
if (this.mDB.lastInsertRowId) {
logMessage += "\nLast Insert Row Id: " + this.mDB.lastInsertRowId;
}
if (this.mDB.transactionInProgress) {
logMessage += "\nTransaction In Progress: " + this.mDB.transactionInProgress;
}
}
if (this.mLastStatement) {
logMessage += "\nLast DB Statement: " + this.mLastStatement;
if (this.mLastStatement.params) {
for (let param in this.mLastStatement.params) {
logMessage += "\nLast Statement param [" + param + "]: " + this.mLastStatement.params[param];
}
}
}
if (exception) {
logMessage += "\nException: " + exception;
}
cal.ERROR("[calStorageCalendar] " + logMessage + "\n" + cal.STACK(10));
}
};
/** Module Registration */
const scriptLoadOrder = [
"calUtils.js",
];
function NSGetFactory(cid) {
if (!this.scriptsLoaded) {
Services.io.getProtocolHandler("resource")
.QueryInterface(Components.interfaces.nsIResProtocolHandler)
.setSubstitution("calendar", Services.io.newFileURI(__LOCATION__.parent.parent));
Components.utils.import("resource://calendar/modules/calUtils.jsm");
cal.loadScripts(scriptLoadOrder, Components.utils.getGlobalForObject(this));
this.scriptsLoaded = true;
}
return (XPCOMUtils.generateNSGetFactory([calStorageCalendar]))(cid);
}
|