Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 2565 lines (2247 sloc) 87.073 kb
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
//
// Database.m
// Vienna
//
// Created by Steve on Tue Feb 03 2004.
// Copyright (c) 2004-2005 Steve Palmer. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#import "Database.h"
#import "Preferences.h"
#import "StringExtensions.h"
#import "CalendarExtensions.h"
#import "Constants.h"
#import "ArticleRef.h"
#import "SearchString.h"
#import "NSNotificationAdditions.h"
#import "RefreshManager.h"

// Private scope flags
#define MA_Scope_Inclusive 1
#define MA_Scope_SubFolders 2

// Private functions
@interface Database (Private)
-(NSString *)relocateLockedDatabase:(NSString *)path;
-(void)setDatabaseVersion:(NSInteger)newVersion;
-(BOOL)initArticleArray:(Folder *)folder;
-(void)verifyThreadSafety;
-(CriteriaTree *)criteriaForFolder:(NSInteger)folderId;
-(NSArray *)arrayOfSubFolders:(Folder *)folder;
-(NSString *)sqlScopeForFolder:(Folder *)folder flags:(NSInteger)scopeFlags;
-(void)createInitialSmartFolder:(NSString *)folderName withCriteria:(Criteria *)criteria;
-(NSInteger)createFolderOnDatabase:(NSString *)name underParent:(NSInteger)parentId withType:(NSInteger)type;
-(NSInteger)executeSQL:(NSString *)sqlStatement;
-(NSInteger)executeSQLWithFormat:(NSString *)sqlStatement, ...;
@end

// The current database version number
const NSInteger MA_Min_Supported_DB_Version = 12;
const NSInteger MA_Current_DB_Version = 18;

// There's just one database and we manage access to it through a
// singleton object.
static Database * _sharedDatabase = nil;

@implementation Database

/* init
* General object initialization.
*/
-(id)init
{
if ((self = [super init]) != nil)
{
inTransaction = NO;
sqlDatabase = NULL;
initializedfoldersDict = NO;
initializedSmartfoldersDict = NO;
countOfUnread = 0;
trashFolder = nil;
searchFolder = nil;
searchString = @"";
smartfoldersDict = [[NSMutableDictionary dictionary] retain];
foldersDict = [[NSMutableDictionary dictionary] retain];
}
return self;
}

/* sharedDatabase
* Returns the single instance of the refresh manager.
*/
+(Database *)sharedDatabase
{
if (!_sharedDatabase)
{
_sharedDatabase = [[Database alloc] init];
if (![_sharedDatabase initDatabase:[[Preferences standardPreferences] defaultDatabase]])
{
[_sharedDatabase release];
_sharedDatabase = nil;
}
}
return _sharedDatabase;
}

/* initDatabase
* Initalizes the database. The database is first checked to ensure it exists
* and, if not, it is created with all the tables.
*/
-(BOOL)initDatabase:(NSString *)databaseFileName
{
// Don't allow nested opens
if (sqlDatabase)
return NO;

// Fully expand the path and make sure it exists because if the
// database file itself doesn't exist, we want to create it and
// we can't create it on a non-existent path.
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * qualifiedDatabaseFileName = [databaseFileName stringByExpandingTildeInPath];
NSString * databaseFolder = [qualifiedDatabaseFileName stringByDeletingLastPathComponent];
BOOL isDir;


if (![fileManager fileExistsAtPath:databaseFolder isDirectory:&isDir])
{
NSError *error;
if (![fileManager createDirectoryAtPath:databaseFolder withIntermediateDirectories:YES attributes:NULL error:&error])
{
NSRunAlertPanel(NSLocalizedString(@"Cannot create database folder", nil),
[NSString stringWithFormat:NSLocalizedString(@"Cannot create database folder text: %@", nil), error],
NSLocalizedString(@"Close", nil), @"", @"",
databaseFolder);
[error release];
return NO;
}
}

// Open the database at the well known location
sqlDatabase = [[SQLDatabase alloc] initWithFile:qualifiedDatabaseFileName];
if (!sqlDatabase || ![sqlDatabase open])
{
NSRunAlertPanel(NSLocalizedString(@"Cannot open database", nil),
NSLocalizedString(@"Cannot open database text", nil),
NSLocalizedString(@"Close", nil), @"", @"",
qualifiedDatabaseFileName);
return NO;
}

// Get the info table. If it doesn't exist then the database is new
SQLResult * results = [sqlDatabase performQuery:@"select version from info"];
databaseVersion = 0;
if (results && [results rowCount])
{
NSString * versionString = [[results rowAtIndex:0] stringForColumn:@"version"];
databaseVersion = [versionString intValue];
}
[results release];

// Save this thread handle to ensure we trap cases of calling the db on
// the wrong thread.
mainThread = [NSThread currentThread];

// Trap unsupported databases
if (databaseVersion > 0 && databaseVersion < MA_Min_Supported_DB_Version)
{
NSRunAlertPanel(NSLocalizedString(@"Unrecognised database format", nil),
NSLocalizedString(@"Unrecognised database format text", nil),
NSLocalizedString(@"Close", nil), @"", @"",
qualifiedDatabaseFileName);
return NO;
}

// Create the tables when the database is empty.
if (databaseVersion == 0)
{
// Create the tables. We use the first table as a test whether we can actually
// write to the specified location. If not then we need to prompt the user for
// a different location.
NSInteger resultCode;
while ((resultCode = [self executeSQL:@"create table info (version, last_opened, first_folder, folder_sort)"]) != SQLITE_OK)
{
if (resultCode != SQLITE_LOCKED)
return NO;

// Database was opened but table was locked.
NSString * newPath = [self relocateLockedDatabase:qualifiedDatabaseFileName];
if (newPath == nil)
return NO;
qualifiedDatabaseFileName = newPath;
}

[self beginTransaction];

[self executeSQL:@"create table folders (folder_id integer primary key, parent_id, foldername, unread_count, last_update, type, flags, next_sibling, first_child)"];
[self executeSQL:@"create table messages (message_id, folder_id, parent_id, read_flag, marked_flag, deleted_flag, title, sender, link, createddate, date, text, revised_flag, enclosuredownloaded_flag, hasenclosure_flag, enclosure)"];
[self executeSQL:@"create table smart_folders (folder_id, search_string)"];
[self executeSQL:@"create table rss_folders (folder_id, feed_url, username, last_update_string, description, home_page, bloglines_id)"];
[self executeSQL:@"create table rss_guids (message_id, folder_id)"];
[self executeSQL:@"create index messages_folder_idx on messages (folder_id)"];
[self executeSQL:@"create index messages_message_idx on messages (message_id)"];
[self executeSQL:@"create index rss_guids_idx on rss_guids (folder_id)"];

// Create a criteria to find all marked articles
Criteria * markedCriteria = [[Criteria alloc] initWithField:MA_Field_Flagged withOperator:MA_CritOper_Is withValue:@"Yes"];
[self createInitialSmartFolder:NSLocalizedString(@"Marked Articles", nil) withCriteria:markedCriteria];
[markedCriteria release];

// Create a criteria to show all unread articles
Criteria * unreadCriteria = [[Criteria alloc] initWithField:MA_Field_Read withOperator:MA_CritOper_Is withValue:@"No"];
[self createInitialSmartFolder:NSLocalizedString(@"Unread Articles", nil) withCriteria:unreadCriteria];
[unreadCriteria release];

// Create a criteria to show all articles received today
Criteria * todayCriteria = [[Criteria alloc] initWithField:MA_Field_Date withOperator:MA_CritOper_Is withValue:@"today"];
[self createInitialSmartFolder:NSLocalizedString(@"Today's Articles", nil) withCriteria:todayCriteria];
[todayCriteria release];

// Create the trash folder
[self executeSQLWithFormat:@"insert into folders (parent_id, foldername, unread_count, last_update, type, flags, next_sibling, first_child) values (-1, '%@', 0, 0, %d, 0, 0, 0)",
NSLocalizedString(@"Trash", nil),
MA_Trash_Folder];

// Set the initial version
databaseVersion = MA_Current_DB_Version;
[self executeSQLWithFormat:@"insert into info (version, first_folder, folder_sort) values (%d, 0, %d)", databaseVersion, MA_FolderSort_Manual];
[[Preferences standardPreferences] setFoldersTreeSortMethod:MA_FolderSort_Manual];

// Set the initial folder order
[self initFolderArray];
NSInteger folderId = 0;
NSInteger previousSibling = 0;
NSArray * allFolders = [foldersDict allKeys];
NSUInteger count = [allFolders count];
NSUInteger index;
for (index = 0u; index < count; ++index)
{
previousSibling = folderId;
folderId = [[allFolders objectAtIndex:index] intValue];
if (index == 0u)
[self setFirstChild:folderId forFolder:MA_Root_Folder];
else
[self setNextSibling:folderId forFolder:previousSibling];
}

// If we have a DemoFeeds.plist in the resources then use it to create some initial demo
// RSS feeds.
NSBundle *thisBundle = [NSBundle bundleForClass:[self class]];
NSString * pathToPList = [thisBundle pathForResource:@"DemoFeeds.plist" ofType:@""];
if (pathToPList != nil)
{
NSDictionary * demoFeedsDict = [NSDictionary dictionaryWithContentsOfFile:pathToPList];
if (demoFeedsDict)
{
for (NSString * feedName in demoFeedsDict)
{
NSDictionary * itemDict = [demoFeedsDict objectForKey:feedName];
NSString * feedURL = [itemDict valueForKey:@"URL"];
if (feedURL != nil && feedName != nil)
previousSibling = [self addRSSFolder:feedName underParent:MA_Root_Folder afterChild:previousSibling subscriptionURL:feedURL];
}
}
}

[self commitTransaction];
}
else if (databaseVersion < MA_Current_DB_Version)
{
NSAlert * alert = [[NSAlert alloc] init];
[alert setMessageText:NSLocalizedString(@"Database Upgrade", nil)];
[alert setInformativeText:NSLocalizedString(@"Vienna must upgrade its database to the latest version. This may take a minute or so. We apologize for the inconveninece.", nil)];
[alert addButtonWithTitle:NSLocalizedString(@"Upgrade Database", nil)];
[alert addButtonWithTitle:NSLocalizedString(@"Quit Vienna", nil)];
NSInteger modalReturn = [alert runModal];
[alert release];
if (modalReturn == NSAlertSecondButtonReturn)
{
return NO;
}

// Backup the database before any upgrade
NSString * backupDatabaseFileName = [qualifiedDatabaseFileName stringByAppendingPathExtension:@"bak"];
[[NSFileManager defaultManager] copyItemAtPath:qualifiedDatabaseFileName toPath:backupDatabaseFileName error:nil];
}

// Upgrade to rev 13.
// Add createddate field to the messages table and initialise it to a date in the past.
// Create an index on the message_id column.
if (databaseVersion < 13)
{
[self beginTransaction];

[self executeSQL:@"alter table messages add column createddate"];
[self executeSQLWithFormat:@"update messages set createddate=%f", [[NSDate distantPast] timeIntervalSince1970]];
[self executeSQL:@"create index messages_message_idx on messages (message_id)"];

[self commitTransaction];
NSLog(@"Updated database schema to version %d.", databaseVersion);
}

// Upgrade to rev 14.
// Add next_sibling and next_child columns to folders table and first_folder column to info table to allow for manual sorting.
// Initialize all values to 0. The correct values will be set by -[FoldersTree setManualSortOrderForNode:].
// Make sure that all parent_id values are integers rather than strings, because previous versions of setParent:forFolder:
// set them as strings.
if (databaseVersion < 14)
{
[self beginTransaction];

[self executeSQL:@"alter table info add column first_folder"];
[self executeSQL:@"update info set first_folder=0"];

[self executeSQL:@"alter table folders add column next_sibling"];
[self executeSQL:@"update folders set next_sibling=0"];

[self executeSQL:@"alter table folders add column first_child"];
[self executeSQL:@"update folders set first_child=0"];

[[Preferences standardPreferences] setFoldersTreeSortMethod:MA_FolderSort_ByName];

SQLResult * results = [sqlDatabase performQuery:@"select folder_id, parent_id from folders"];
if (results && [results rowCount])
{
for (SQLRow * row in [results rowEnumerator])
{
NSInteger folderId = [[row stringForColumn:@"folder_id"] intValue];
NSInteger parentId = [[row stringForColumn:@"parent_id"] intValue];
[self executeSQLWithFormat:@"update folders set parent_id=%d where folder_id=%d", parentId, folderId];
}
}
[results release];

[self commitTransaction];
NSLog(@"Updated database schema to version %d.", databaseVersion);

}

// Upgrade to rev 15.
// Move the folders tree sort method preference to the database, so that it can survive deletion of the preferences file.
// Do not disturb the manual sort order, if it exists.
if (databaseVersion < 15)
{
[self beginTransaction];

[self executeSQL:@"alter table info add column folder_sort"];

NSInteger oldFoldersTreeSortMethod = [[Preferences standardPreferences] foldersTreeSortMethod];
[self executeSQLWithFormat:@"update info set folder_sort=%d", oldFoldersTreeSortMethod];
[self commitTransaction];
NSLog(@"Updated database schema to version %d.", databaseVersion);
}

// Upgrade to rev 16.
// Add revised_flag to messages table, and initialize all values to 0.
if (databaseVersion < 16)
{
[self beginTransaction];

[self executeSQL:@"alter table messages add column revised_flag"];
[self executeSQL:@"update messages set revised_flag=0"];

// Set the new version
[self setDatabaseVersion:16];
[self commitTransaction];
}


// Upgrade to rev 17.
// Add hasenclosure_flag, enclosuredownloaded_flag and enclosure to messages table, and initialize stuff.
if (databaseVersion < 17)
{
[self beginTransaction];

[self executeSQL:@"alter table messages add column hasenclosure_flag"];
[self executeSQL:@"update messages set hasenclosure_flag=0"];
[self executeSQL:@"alter table messages add column enclosure"];
[self executeSQL:@"update messages set enclosure=''"];
[self executeSQL:@"alter table messages add column enclosuredownloaded_flag"];
[self executeSQL:@"update messages set enclosuredownloaded_flag=0"];

// Set the new version
[self setDatabaseVersion:17];
[self commitTransaction];
}

// Upgrade to rev 18.
// Add table all message guids.
if (databaseVersion < 18)
{
[self beginTransaction];

[self executeSQL:@"create table rss_guids as select message_id, folder_id from messages"];
[self executeSQL:@"create index rss_guids_idx on rss_guids (folder_id)"];

// Set the new version
[self setDatabaseVersion:18];
[self commitTransaction];
}

// Read the folders tree sort method from the database.
// Make sure that the folders tree is not yet registered to receive notifications at this point.
NSInteger newFoldersTreeSortMethod = MA_FolderSort_ByName;
SQLResult * sortResults = [sqlDatabase performQuery:@"select folder_sort from info"];
if (sortResults && [sortResults rowCount])
{
newFoldersTreeSortMethod = [[[sortResults rowAtIndex:0] stringForColumn:@"folder_sort"] intValue];
}
[sortResults release];
[[Preferences standardPreferences] setFoldersTreeSortMethod:newFoldersTreeSortMethod];

// Register for notifications of change in folders tree sort method.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAutoSortFoldersTreeChange:) name:@"MA_Notify_AutoSortFoldersTreeChange" object:nil];

// Initial check if the database is read-only
[self syncLastUpdate];

// Create fields
fieldsByName = [[NSMutableDictionary dictionary] retain];
fieldsOrdered = [[NSMutableArray alloc] init];

[self addField:MA_Field_Read type:MA_FieldType_Flag tag:MA_FieldID_Read sqlField:@"read_flag" visible:YES width:17];
[self addField:MA_Field_Flagged type:MA_FieldType_Flag tag:MA_FieldID_Flagged sqlField:@"marked_flag" visible:YES width:17];
[self addField:MA_Field_HasEnclosure type:MA_FieldType_Flag tag:MA_FieldID_HasEnclosure sqlField:@"hasenclosure_flag" visible:YES width:17];
[self addField:MA_Field_Deleted type:MA_FieldType_Flag tag:MA_FieldID_Deleted sqlField:@"deleted_flag" visible:NO width:15];
[self addField:MA_Field_Comments type:MA_FieldType_Integer tag:MA_FieldID_Comments sqlField:@"comment_flag" visible:NO width:15];
[self addField:MA_Field_GUID type:MA_FieldType_Integer tag:MA_FieldID_GUID sqlField:@"message_id" visible:NO width:72];
[self addField:MA_Field_Subject type:MA_FieldType_String tag:MA_FieldID_Subject sqlField:@"title" visible:YES width:472];
[self addField:MA_Field_Folder type:MA_FieldType_Folder tag:MA_FieldID_Folder sqlField:@"folder_id" visible:NO width:130];
[self addField:MA_Field_Date type:MA_FieldType_Date tag:MA_FieldID_Date sqlField:@"date" visible:YES width:152];
[self addField:MA_Field_Parent type:MA_FieldType_Integer tag:MA_FieldID_Parent sqlField:@"parent_id" visible:NO width:72];
[self addField:MA_Field_Author type:MA_FieldType_String tag:MA_FieldID_Author sqlField:@"sender" visible:YES width:138];
[self addField:MA_Field_Link type:MA_FieldType_String tag:MA_FieldID_Link sqlField:@"link" visible:NO width:138];
[self addField:MA_Field_Text type:MA_FieldType_String tag:MA_FieldID_Text sqlField:@"text" visible:NO width:152];
[self addField:MA_Field_Summary type:MA_FieldType_String tag:MA_FieldID_Summary sqlField:@"summary" visible:NO width:152];
[self addField:MA_Field_Headlines type:MA_FieldType_String tag:MA_FieldID_Headlines sqlField:@"" visible:NO width:100];
[self addField:MA_Field_Enclosure type:MA_FieldType_String tag:MA_FieldID_Enclosure sqlField:@"enclosure" visible:NO width:100];
[self addField:MA_Field_EnclosureDownloaded type:MA_FieldType_Flag tag:MA_FieldID_EnclosureDownloaded sqlField:@"enclosuredownloaded_flag" visible:NO width:100];

return YES;
}

/* relocateLockedDatabase
* Tell the user that the database could not be created at the path specified by path
* and prompt for an alternative location. Opens and returns the new location if we were successful.
*/
-(NSString *)relocateLockedDatabase:(NSString *)path
{
NSString * errorTitle = NSLocalizedString(@"Locate Title", nil);
NSString * errorText = NSLocalizedString(@"Locate Text", nil);
NSInteger option = NSRunAlertPanel(errorTitle, errorText, NSLocalizedString(@"Locate", nil), NSLocalizedString(@"Exit", nil), nil, path);
if (option == 0)
return nil;

// Locate button.
if (option == 1)
{
// Delete any existing database.
if (sqlDatabase != nil)
{
[sqlDatabase close];
[sqlDatabase release];
sqlDatabase = nil;
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}

// Bring up modal UI to select the new location
NSOpenPanel * openPanel = [NSOpenPanel openPanel];
[openPanel setCanChooseFiles:NO];
[openPanel setCanChooseDirectories:YES];
if ([openPanel runModalForDirectory:nil file:nil] == NSCancelButton)
return nil;

// Make the new database name.
NSString * databaseName = [path lastPathComponent];
NSString * newPath = [[[openPanel filenames] objectAtIndex:0] stringByAppendingPathComponent:databaseName];

// And try to open it.
sqlDatabase = [[SQLDatabase alloc] initWithFile:newPath];
if (!sqlDatabase || ![sqlDatabase open])
{
NSRunAlertPanel(NSLocalizedString(@"Cannot open database", nil),
NSLocalizedString(@"Cannot open database text", nil),
NSLocalizedString(@"Close", nil), @"", @"",
newPath);
return nil;
}

// Save this to the preferences
[[Preferences standardPreferences] setDefaultDatabase:newPath];
return newPath;
}
return nil;
}

/* createInitialSmartFolder
* Create a smart folder in the database as part of creating a new database. This is intended to be
* called from the database initialisation code and makes a number of assumptions about the state
* of the class at that point. It is NOT a substitute for addSmartFolder. Specifically it does
* not add the new folder to the internal cache nor broadcast any notifications.
*/
-(void)createInitialSmartFolder:(NSString *)folderName withCriteria:(Criteria *)criteria
{
if ([self createFolderOnDatabase:folderName underParent:MA_Root_Folder withType:MA_Smart_Folder] >= 0)
{
CriteriaTree * criteriaTree = [[CriteriaTree alloc] init];
[criteriaTree addCriteria:criteria];

NSString * preparedCriteriaString = [SQLDatabase prepareStringForQuery:[criteriaTree string]];

[self executeSQLWithFormat:@"insert into smart_folders (folder_id, search_string) values (%d, '%@')", [sqlDatabase lastInsertRowId], preparedCriteriaString];
[criteriaTree release];
}
}

/* executeSQL
* Executes the specified SQL statement and discards the result. Should be used for
* SQL statements that do not return results.
*/
-(NSInteger)executeSQL:(NSString *)sqlStatement
{
[self verifyThreadSafety];
[[sqlDatabase performQuery:sqlStatement] release];
return [sqlDatabase lastError];
}

/* executeSQLWithFormat
* Formats and executes the specified SQL statement and discards the result. Should be used for
* SQL statements that do not return results.
*/
-(NSInteger)executeSQLWithFormat:(NSString *)sqlStatement, ...
{
va_list arguments;
va_start(arguments, sqlStatement);
NSString * query = [[NSString alloc] initWithFormat:sqlStatement arguments:arguments];
[self executeSQL:query];
[query release];
return [sqlDatabase lastError];
}

/* verifyThreadSafety
* Formerly, in debug mode we asserted if the caller thread wasn't the thread
* on which the database was created. Now, we just do nothing.
*/
-(void)verifyThreadSafety
{
//NSAssert([NSThread currentThread] == mainThread, @"Calling database on wrong thread!");
}

/* syncLastUpdate
* Call this function to update the field in the info table which contains the last_updated
* date. This is basically auditing data and is only called when the database is first opened
* in this session.
*/
-(void)syncLastUpdate
{
[self verifyThreadSafety];
SQLResult * result = [sqlDatabase performQueryWithFormat:@"update info set last_opened='%@'", [NSDate date]];
readOnly = (result == nil);
[result release];
}

/* countOfUnread
* Return the total number of unread articles in the database.
*/
-(NSInteger)countOfUnread
{
[self initFolderArray];
return countOfUnread;
}

/* addField
* Add the specified field to our fields array.
*/
-(void)addField:(NSString *)name type:(NSInteger)type tag:(NSInteger)tag sqlField:(NSString *)sqlField visible:(BOOL)visible width:(NSInteger)width
{
Field * field = [[Field alloc] init];
if (field != nil)
{
[field setName:name];
[field setDisplayName:NSLocalizedString(name, nil)];
[field setType:type];
[field setTag:tag];
[field setVisible:visible];
[field setWidth:width];
[field setSqlField:sqlField];
[fieldsOrdered addObject:field];
[fieldsByName setValue:field forKey:name];
[field release];
}
}

/* arrayOfFields
* Return the array of fields.
*/
-(NSArray *)arrayOfFields
{
return fieldsOrdered;
}

/* fieldByTitle
* Given a name, this function returns the field represented by
* that name.
*/
-(Field *)fieldByName:(NSString *)name
{
return [fieldsByName valueForKey:name];
}

/* databaseVersion
* Return the database version.
*/
-(NSInteger)databaseVersion
{
return databaseVersion;
}

/* setDatabaseVersion
* Sets the version stamp in the database.
*/
-(void)setDatabaseVersion:(NSInteger)newVersion
{
[self executeSQLWithFormat:@"update info set version=%d", newVersion];
databaseVersion = newVersion;
NSLog(@"Updated database schema to version %d.", databaseVersion);
}

/* readOnly
* Returns whether or not this database is read-only.
*/
-(BOOL)readOnly
{
return readOnly;
}

/* beginTransaction
* Starts a SQL transaction.
*/
-(void)beginTransaction
{
NSAssert(!inTransaction, @"Whoops! Already in a transaction. You forgot to call commitTransaction somewhere");
[self executeSQL:@"begin transaction"];
inTransaction = YES;
}

/* commitTransaction
* Commits a SQL transaction.
*/
-(void)commitTransaction
{
NSAssert(inTransaction, @"Whoops! Not in a transaction. You forgot to call beginTransaction first");
[self executeSQL:@"commit transaction"];
inTransaction = NO;
}

/* compactDatabase
* Compact the database using the vacuum command.
*/
-(void)compactDatabase
{
if (!readOnly)
[self executeSQL:@"vacuum"];
}

/* clearFolderFlag
* Clears the specified flag for the folder.
*/
-(void)clearFolderFlag:(NSInteger)folderId flagToClear:(NSUInteger)flag
{
// Exit now if we're read-only
if (readOnly)
return;

Folder * folder = [self folderFromID:folderId];
if (folder != nil)
{
[folder clearFlag:flag];
[self executeSQLWithFormat:@"update folders set flags=%d where folder_id=%d", [folder flags], folderId];
}
}

/* setFolderFlag
* Sets the specified flag for the folder.
*/
-(void)setFolderFlag:(NSInteger)folderId flagToSet:(NSUInteger)flag
{
// Exit now if we're read-only
if (readOnly)
return;

Folder * folder = [self folderFromID:folderId];
if (folder != nil)
{
[folder setFlag:flag];
[self executeSQLWithFormat:@"update folders set flags=%d where folder_id=%d", [folder flags], folderId];
}
}

/* setFolderLastUpdate
* Sets the date when the folder was last updated.
*/
-(void)setFolderLastUpdate:(NSInteger)folderId lastUpdate:(NSDate *)lastUpdate
{
// Exit now if we're read-only
if (readOnly)
return;

// If no change to last update, do nothing
Folder * folder = [self folderFromID:folderId];
if (folder != nil && (IsRSSFolder(folder) || IsGoogleReaderFolder(folder)))
{
if ([[folder lastUpdate] isEqualToDate:lastUpdate])
return;

[folder setLastUpdate:lastUpdate];
NSTimeInterval interval = [lastUpdate timeIntervalSince1970];
[self executeSQLWithFormat:@"update folders set last_update=%f where folder_id=%d", interval, folderId];
}
}

/* setFolderLastUpdateString
* Sets the last update string for the folder.
*/
-(void)setFolderLastUpdateString:(NSInteger)folderId lastUpdateString:(NSString *)lastUpdateString
{
// Exit now if we're read-only
if (readOnly)
return;

// If no change to last update string, do nothing
Folder * folder = [self folderFromID:folderId];
if (folder != nil && (IsRSSFolder(folder) || IsGoogleReaderFolder(folder)))
{
if ([[folder lastUpdateString] isEqualToString:lastUpdateString])
return;

[folder setLastUpdateString:lastUpdateString];
[self executeSQLWithFormat:@"update rss_folders set last_update_string='%@' where folder_id=%d", [folder lastUpdateString], folderId];
}
}

/* setFolderFeedURL
* Change the URL of the feed on the specified RSS folder subscription.
*/
-(BOOL)setFolderFeedURL:(NSInteger)folderId newFeedURL:(NSString *)url
{
// Exit now if we're read-only
if (readOnly)
return NO;

Folder * folder = [self folderFromID:folderId];
if (folder != nil && ![[folder feedURL] isEqualToString:url])
{
NSString * preparedURL = [SQLDatabase prepareStringForQuery:url];

[folder setFeedURL:url];
[self executeSQLWithFormat:@"update rss_folders set feed_url='%@' where folder_id=%d", preparedURL, folderId];
}
return YES;
}


-(NSInteger)addGoogleReaderFolder:(NSString *)feedName underParent:(NSInteger)parentId afterChild:(NSInteger)predecessorId subscriptionURL:(NSString *)url {
NSInteger folderId = [self addFolder:parentId afterChild:predecessorId folderName:feedName type:MA_GoogleReader_Folder canAppendIndex:YES];
//TODO: optimization using unique add function for addRSSFolder
if (folderId != -1)
{
NSString * preparedURL = [SQLDatabase prepareStringForQuery:url];
NSString *preparedName = [SQLDatabase prepareStringForQuery:feedName];

[self verifyThreadSafety];
SQLResult * results = [sqlDatabase performQueryWithFormat:
@"insert into rss_folders (folder_id, description, username, home_page, last_update_string, feed_url, bloglines_id) "
"values (%d, '%@', '', '', '', '%@', %d)",
folderId,
preparedName,
preparedURL,
0];
if (!results)
return -1;

// Add this new folder to our internal cache
Folder * folder = [self folderFromID:folderId];
[folder setFeedURL:url];
[results release];
}
return folderId;
}


/* addRSSFolder
* Add an RSS Feed folder and return the ID of the new folder.
*/
-(NSInteger)addRSSFolder:(NSString *)feedName underParent:(NSInteger)parentId afterChild:(NSInteger)predecessorId subscriptionURL:(NSString *)url
{
NSInteger folderId = [self addFolder:parentId afterChild:predecessorId folderName:feedName type:MA_RSS_Folder canAppendIndex:YES];
if (folderId != -1)
{
NSString * preparedURL = [SQLDatabase prepareStringForQuery:url];

[self verifyThreadSafety];
SQLResult * results = [sqlDatabase performQueryWithFormat:
@"insert into rss_folders (folder_id, description, username, home_page, last_update_string, feed_url, bloglines_id) "
"values (%d, '', '', '', '', '%@', %d)",
folderId,
preparedURL,
0];
if (!results)
return -1;

// Add this new folder to our internal cache
Folder * folder = [self folderFromID:folderId];
[folder setFeedURL:url];
[results release];
}
return folderId;
}

/* addFolder
* Create a new folder under the specified parent and give it the requested name and type. If
* canAppendIndex is YES then we adjust the name to ensure that the folder name remains unique. If
* we hit an error, the function returns -1.
*/
-(NSInteger)addFolder:(NSInteger)parentId afterChild:(NSInteger)predecessorId folderName:(NSString *)name type:(NSInteger)type canAppendIndex:(BOOL)canAppendIndex
{
Folder * folder = nil;

// Prime the cache
[self initFolderArray];

// Exit now if we're read-only
if (readOnly)
return -1;

if (!canAppendIndex)
{
folder = [self folderFromName:name];
if (folder)
return [folder itemId];
}
else
{
// If a folder of that name already exists then adjust the name by appending
// an index number to make it unique.
NSString * oldName = name;
NSUInteger index = 1;

while (([self folderFromName:name]) != nil)
name = [NSString stringWithFormat:@"%@ (%li)", oldName, index++];
}

NSInteger nextSibling = 0;
BOOL manualSort = [[Preferences standardPreferences] foldersTreeSortMethod] == MA_FolderSort_Manual;
if (manualSort)
{
if (predecessorId > 0)
{
Folder * predecessor = [self folderFromID:predecessorId];
if (predecessor != nil)
nextSibling = [predecessor nextSiblingId];
else
predecessorId = 0;
}
if (predecessorId < 0)
{
[self verifyThreadSafety];
SQLResult * siblings = [sqlDatabase performQueryWithFormat:@"select folder_id from folders where parent_id=%d and next_sibling=0", parentId];
predecessorId = (siblings && [siblings rowCount]) ? [[[siblings rowAtIndex:0] stringForColumn:@"folder_id"] intValue] : 0;
[siblings release];
}
if (predecessorId == 0)
{
if (parentId == MA_Root_Folder)
nextSibling = [self firstFolderId];
else
{
Folder * parent = [self folderFromID:parentId];
if (parent != nil)
nextSibling = [parent firstChildId];
}
}
}

// Here we create the folder anew.
NSInteger newItemId = [self createFolderOnDatabase:name underParent:parentId withType:type];
if (newItemId != -1)
{
// Add this new folder to our internal cache. If this is an RSS or Google Reader
// folder, mark it so that somewhere down the line we'll request the
// image for the folder.
folder = [[[Folder alloc] initWithId:newItemId parentId:parentId name:name type:type] autorelease];
if ((type == MA_RSS_Folder)||(type == MA_GoogleReader_Folder))
[folder setFlag:MA_FFlag_CheckForImage];
[foldersDict setObject:folder forKey:[NSNumber numberWithInt:newItemId]];

if (manualSort)
{
if (nextSibling > 0)
[self setNextSibling:nextSibling forFolder:newItemId];
if (predecessorId > 0)
[self setNextSibling:newItemId forFolder:predecessorId];
else
[self setFirstChild:newItemId forFolder:parentId];
}

// Send a notification when new folders are added
[[NSNotificationCenter defaultCenter] postNotificationOnMainThreadWithName:@"MA_Notify_FolderAdded" object:folder];
}
return newItemId;
}

/* createFolderOnDatabase:underParent:withType
* Generic (and internal!) function that creates a new folder in the database. It just creates
* the folder without any real sanity checks which are assumed to have been done by the caller.
* Returns the ID of the newly created folder or -1 if we failed.
*/
-(NSInteger)createFolderOnDatabase:(NSString *)name underParent:(NSInteger)parentId withType:(NSInteger)type
{
NSString * preparedName = [SQLDatabase prepareStringForQuery:name];
NSInteger newItemId = -1;
NSInteger flags = 0;
NSInteger nextSibling = 0;
NSInteger firstChild = 0;

// For new folders, last update is set to before now
NSDate * lastUpdate = [NSDate distantPast];
NSTimeInterval interval = [lastUpdate timeIntervalSince1970];

// Require an image check if we're a subscription folder
if ((type == MA_RSS_Folder) || (type == MA_GoogleReader_Folder))
flags = MA_FFlag_CheckForImage;

// Create the folder in the database. One thing to watch out for here that has
// bit me before. When adding new fields to the folders table, remember to init
// the field here even if its just to an empty value.
[self verifyThreadSafety];
SQLResult * results = [sqlDatabase performQueryWithFormat:
@"insert into folders (foldername, parent_id, unread_count, last_update, type, flags, next_sibling, first_child) values('%@', %d, 0, %f, %d, %d, %d, %d)",
preparedName,
parentId,
interval,
type,
flags,
nextSibling,
firstChild];

// Quick way of getting the last autoincrement primary key value (the folder_id).
if (results)
{
newItemId = [sqlDatabase lastInsertRowId];
[results release];
}

return newItemId;
}

/* untitledFeedFolderName
* Returns the name given to untitled feed folders.
*/
+(NSString *)untitledFeedFolderName
{
return NSLocalizedString(@"(Untitled Feed)", nil);
}

/* wrappedDeleteFolder
* Delete the specified folder. This function should be called from within a
* transaction wrapper since it can be very SQL intensive.
*/
-(BOOL)wrappedDeleteFolder:(NSInteger)folderId
{
NSArray * arrayOfChildFolders = [self arrayOfFolders:folderId];
Folder * folder;

// Recurse and delete child folders
for (folder in arrayOfChildFolders)
[self wrappedDeleteFolder:[folder itemId]];

// Adjust unread counts on parents
folder = [self folderFromID:folderId];
NSInteger adjustment = -[folder unreadCount];
while ([folder parentId] != MA_Root_Folder)
{
folder = [self folderFromID:[folder parentId]];
[folder setChildUnreadCount:[folder childUnreadCount] + adjustment];
}

// Delete all articles in this folder then delete ourselves.
folder = [self folderFromID:folderId];
countOfUnread -= [folder unreadCount];
if (IsSmartFolder(folder))
[self executeSQLWithFormat:@"delete from smart_folders where folder_id=%d", folderId];

// If this is an RSS feed, delete from the feeds
// and delete raw feed source
if (IsRSSFolder(folder) || IsGoogleReaderFolder(folder))
{
[self executeSQLWithFormat:@"delete from rss_folders where folder_id=%d", folderId];
[self executeSQLWithFormat:@"delete from rss_guids where folder_id=%d", folderId];

NSString * feedSourceFilePath = [folder feedSourceFilePath];
if (feedSourceFilePath != nil)
{
BOOL isDirectory = YES;
if ([[NSFileManager defaultManager] fileExistsAtPath:feedSourceFilePath isDirectory:&isDirectory] && !isDirectory)
{
[[NSFileManager defaultManager] removeItemAtPath:feedSourceFilePath error:NULL];
}

NSString * backupPath = [feedSourceFilePath stringByAppendingPathExtension:@"bak"];
if ([[NSFileManager defaultManager] fileExistsAtPath:backupPath isDirectory:&isDirectory] && !isDirectory)
{
[[NSFileManager defaultManager] removeItemAtPath:backupPath error:NULL];
}
}
}

// If we deleted the search folder, null out our cached handle
if (IsSearchFolder(folder))
{
[searchFolder release];
searchFolder = nil;
}

// Update the sort order if necessary
if ([[Preferences standardPreferences] foldersTreeSortMethod] == MA_FolderSort_Manual)
{
[self verifyThreadSafety];
SQLResult * results = [sqlDatabase performQueryWithFormat:@"select folder_id from folders where parent_id=%d and next_sibling=%d", [folder parentId], folderId];
if (results && [results rowCount])
{
NSInteger previousSibling = [[[results rowAtIndex:0] stringForColumn:@"folder_id"] intValue];
[self setNextSibling:[folder nextSiblingId] forFolder:previousSibling];
}
else
[self setFirstChild:[folder nextSiblingId] forFolder:[folder parentId]];
[results release];
}

// For a smart folder, the next line is a no-op but it helpfully takes care of the case where a
// normal folder had it's type grobbed to MA_Smart_Folder.
[self executeSQLWithFormat:@"delete from messages where folder_id=%d", folderId];
[self executeSQLWithFormat:@"delete from folders where folder_id=%d", folderId];

// Remove from the folders array. Do this after we send the notification
// so that the notification handlers don't fail if they try to dereference the
// folder.
[foldersDict removeObjectForKey:[NSNumber numberWithInt:folderId]];
return YES;
}

/* deleteFolder
* Delete the specified folder. If the folder has any children, delete them too. Also delete
* all articles associated with the folder. Then send a notification that the folder went bye-bye.
*/
-(BOOL)deleteFolder:(NSInteger)folderId
{
NSMutableArray * arrayOfFolderIds;
NSArray * arrayOfChildFolders;
NSNumber * numFolder;
Folder * folder;
BOOL result;

// Exit now if we're read-only
if (readOnly)
return NO;

// Make sure this is a valid folder
folder = [self folderFromID:folderId];
if (folder == nil)
return NO;

arrayOfChildFolders = [self arrayOfSubFolders:folder];
arrayOfFolderIds = [NSMutableArray arrayWithCapacity:[arrayOfChildFolders count]];

// Send the pre-delete notification before we start the transaction so that the handlers can
// safely do any database access.
for (folder in arrayOfChildFolders)
{
numFolder = [NSNumber numberWithInt:[folder itemId]];
[arrayOfFolderIds addObject:numFolder];
[[NSNotificationCenter defaultCenter] postNotificationOnMainThreadWithName:@"MA_Notify_WillDeleteFolder" object:numFolder];
}

// Now do the deletion.
[self beginTransaction];
result = [self wrappedDeleteFolder:folderId];
[self commitTransaction];

// Send the post-delete notification after we're finished. Note that the folder actually corresponding to
// each numFolder won't exist any more and the handlers need to be aware of this.
for (numFolder in arrayOfFolderIds)
[[NSNotificationCenter defaultCenter] postNotificationOnMainThreadWithName:@"MA_Notify_FolderDeleted" object:numFolder];

return result;
}

/* setFolderName
* Renames the specified folder.
*/
-(BOOL)setFolderName:(NSInteger)folderId newName:(NSString *)newName
{
// Exit now if we're read-only
if (readOnly)
return NO;

// Find our folder element.
Folder * folder = [self folderFromID:folderId];
if (!folder)
return NO;

// Do nothing if the name hasn't changed. Otherwise it is wasted
// effort, basically.
if ([[folder name] isEqualToString:newName])
return NO;

[folder setName:newName];

// Rename in the database
NSString * preparedNewName = [SQLDatabase prepareStringForQuery:newName];
[self executeSQLWithFormat:@"update folders set foldername='%@' where folder_id=%d", preparedNewName, folderId];

// Send a notification that the folder has changed. It is the responsibility of the
// notifiee that they work out that the name is the part that has changed.
[[NSNotificationCenter defaultCenter] postNotificationOnMainThreadWithName:@"MA_Notify_FolderNameChanged" object:[NSNumber numberWithInt:folderId]];
return YES;
}

/* setFolderDescription
* Sets the folder description both in the internal structure and in the folder_description table.
*/
-(BOOL)setFolderDescription:(NSInteger)folderId newDescription:(NSString *)newDescription
{
// Exit now if we're read-only
if (readOnly)
return NO;

// Find our folder element.
Folder * folder = [self folderFromID:folderId];
if (!folder)
return NO;

// Do nothing if the description hasn't changed. Otherwise it is wasted
// effort, basically.
if ([[folder feedDescription] isEqualToString:newDescription])
return NO;

[folder setFeedDescription:newDescription];

// Add a new description or update the one we have
NSString * preparedNewDescription = [SQLDatabase prepareStringForQuery:newDescription];
[self executeSQLWithFormat:@"update rss_folders set description='%@' where folder_id=%d", preparedNewDescription, folderId];

// Send a notification that the folder has changed. It is the responsibility of the
// notifiee that they work out that the description is the part that has changed.
[[NSNotificationCenter defaultCenter] postNotificationOnMainThreadWithName:@"MA_Notify_FolderDescriptionChanged" object:[NSNumber numberWithInt:folderId]];
return YES;
}

/* setFolderHomePage
* Sets the folder's associated URL link in both in the internal structure and in the folder_description table.
*/
-(BOOL)setFolderHomePage:(NSInteger)folderId newHomePage:(NSString *)newHomePage
{
// Exit now if we're read-only
if (readOnly)
return NO;

// Find our folder element.
Folder * folder = [self folderFromID:folderId];
if (!folder)
return NO;

// Do nothing if the link hasn't changed. Otherwise it is wasted
// effort, basically.
if ([[folder homePage] isEqualToString:newHomePage])
return NO;

[folder setHomePage:newHomePage];

// Add a new link or update the one we have
NSString * preparedNewLink = [SQLDatabase prepareStringForQuery:newHomePage];
[self executeSQLWithFormat:@"update rss_folders set home_page='%@' where folder_id=%d", preparedNewLink, folderId];

// Send a notification that the folder has changed. It is the responsibility of the
// notifiee that they work out that the link is the part that has changed.
[[NSNotificationCenter defaultCenter] postNotificationOnMainThreadWithName:@"MA_Notify_FolderHomePageChanged" object:[NSNumber numberWithInt:folderId]];
return YES;
}

/* setFolderUsername
* Sets the folder's user name in both in the internal structure and in the folder_description table.
*/
-(BOOL)setFolderUsername:(NSInteger)folderId newUsername:(NSString *)name
{
// Exit now if we're read-only
if (readOnly)
return NO;

// Find our folder element.
Folder * folder = [self folderFromID:folderId];
if (!folder)
return NO;

// Do nothing if the link hasn't changed. Otherwise it is wasted
// effort, basically.
if ([[folder username] isEqualToString:name])
return NO;

[folder setUsername:name];

// Add a new link or update the one we have
NSString * preparedName = [SQLDatabase prepareStringForQuery:name];
[self executeSQLWithFormat:@"update rss_folders set username='%@' where folder_id=%d", preparedName, folderId];
return YES;
}

/* setParent
* Changes the parent for the specified folder then updates the database.
*/
-(BOOL)setParent:(NSInteger)newParentID forFolder:(NSInteger)folderId
{
// Exit now if we're read-only
if (readOnly)
return NO;

Folder * folder = [self folderFromID:folderId];
if ([folder parentId] == newParentID)
return NO;

// Sanity check. Make sure we're not reparenting to our
// subordinate.
Folder * parentFolder = [self folderFromID:newParentID];
while (parentFolder != nil)
{
if ([parentFolder itemId] == folderId)
return NO;
parentFolder = [self folderFromID:[parentFolder parentId]];
}

// Adjust the child unread count for the old parent.
NSInteger adjustment = 0;
if ([folder isRSSFolder])
adjustment = [folder unreadCount];
else if ([folder isGroupFolder])
adjustment = [folder childUnreadCount];
if (adjustment > 0)
{
parentFolder = [self folderFromID:[folder parentId]];
while (parentFolder != nil)
{
[parentFolder setChildUnreadCount:[parentFolder childUnreadCount] - adjustment];
parentFolder = [self folderFromID:[parentFolder parentId]];
}
}

// Do the re-parent
[folder setParent:newParentID];

// In addition to reparenting the child, we also need to fix up the unread count for all
// precedent parents.
if (adjustment > 0)
{
parentFolder = [self folderFromID:newParentID];
while (parentFolder != nil)
{
[parentFolder setChildUnreadCount:[parentFolder childUnreadCount] + adjustment];
parentFolder = [self folderFromID:[parentFolder parentId]];
}
}

// Update the database now
[self executeSQLWithFormat:@"update folders set parent_id=%d where folder_id=%d", newParentID, folderId];
return YES;
}

/* setFirstChild
* Changes the first child of the specified folder and then updates the database.
*/
-(BOOL)setFirstChild:(NSInteger)childId forFolder:(NSInteger)folderId
{
// Exit now if we're read-only
if (readOnly)
return NO;

if (folderId == MA_Root_Folder)
{
[self executeSQLWithFormat:@"update info set first_folder=%d", childId];
}
else
{
Folder * folder = [self folderFromID:folderId];
if (folder == nil)
return NO;

[folder setFirstChildId:childId];

[self executeSQLWithFormat:@"update folders set first_child=%d where folder_id=%d", childId, folderId];
}

return YES;
}

/* setNextSibling
* Changes the next sibling for the specified folder and then updates the database.
*/
-(BOOL)setNextSibling:(NSUInteger)nextSiblingId forFolder:(NSInteger)folderId
{
// Exit now if we're read-only
if (readOnly)
return NO;

Folder * folder = [self folderFromID:folderId];
if (folder == nil)
return NO;

[folder setNextSiblingId:nextSiblingId];

[self executeSQLWithFormat:@"update folders set next_sibling=%d where folder_id=%d", nextSiblingId, folderId];
return YES;
}

/* firstFolderId
* Returns the ID of the first folder (first child of root).
*/
-(NSInteger)firstFolderId
{
NSInteger folderId = 0;
[self verifyThreadSafety];
SQLResult * results = [sqlDatabase performQuery:@"select first_folder from info"];
if (results && [results rowCount])
{
folderId = [[[results rowAtIndex:0] stringForColumn:@"first_folder"] intValue];
}
[results release];
return folderId;
}

/* trashFolderId;
* Returns the ID of the trash folder.
*/
-(NSInteger)trashFolderId
{
return [trashFolder itemId];
}

/* searchFolderId;
* Returns the ID of the search folder. If it doesn't exist then we create
* it now.
*/
-(NSInteger)searchFolderId
{
if (searchFolder == nil)
{
NSInteger folderId = [self addFolder:MA_Root_Folder afterChild:0 folderName: NSLocalizedString(@"Search Results", nil) type:MA_Search_Folder canAppendIndex:YES];
searchFolder = [[self folderFromID:folderId] retain];
}
return [searchFolder itemId];
}

/* folderFromID
* Retrieve a Folder given it's ID.
*/
-(Folder *)folderFromID:(NSInteger)wantedId
{
return [foldersDict objectForKey:[NSNumber numberWithInt:wantedId]];
}

/* folderFromName
* Retrieve a Folder given it's name.
*/
-(Folder *)folderFromName:(NSString *)wantedName
{
Folder * folder;
for (folder in [foldersDict objectEnumerator])
{
if ([[folder name] isEqualToString:wantedName])
break;
}
return folder;
}

/* folderFromFeedURL
* Returns the RSSFolder that is subscribed to the specified feed URL.
*/
-(Folder *)folderFromFeedURL:(NSString *)wantedFeedURL;
{
Folder * folder;

for (folder in [foldersDict objectEnumerator])
{
if ([[folder feedURL] isEqualToString:wantedFeedURL])
break;
}
return folder;
}

/* handleAutoSortFoldersTreeChange
* Called when preference changes for sorting folders tree.
* Store the new method in the database.
*/
-(void)handleAutoSortFoldersTreeChange:(NSNotification *)notification
{
if (!readOnly)
[self executeSQLWithFormat:@"update info set folder_sort=%d", [[Preferences standardPreferences] foldersTreeSortMethod]];
}

/* createArticle
* Adds or updates an article in the specified folder. Returns YES if the
* article was added or updated or NO if we couldn't add the article for
* some reason.
*/
-(BOOL)createArticle:(NSInteger)folderID article:(Article *)article guidHistory:(NSArray *)guidHistory
{
// Exit now if we're read-only
if (readOnly)
return NO;

// Make sure the folder ID is valid. We need it to decipher
// some info before we add the article.
Folder * folder = [self folderFromID:folderID];
if (folder != nil)
{
// Prime the article cache
[self initArticleArray:folder];

// Extract the article data from the dictionary.
NSString * articleBody = [article body];
NSString * articleTitle = [article title];
NSDate * articleDate = [article date];
NSString * articleLink = [article link];
NSString * userName = [article author];
NSString * articleEnclosure = [article enclosure];
NSString * articleGuid = [article guid];
NSInteger parentId = [article parentId];
BOOL marked_flag = [article isFlagged];
BOOL read_flag = [article isRead];
BOOL revised_flag = [article isRevised];
BOOL deleted_flag = [article isDeleted];
BOOL hasenclosure_flag = [article hasEnclosure];

// We always set the created date ourselves
[article setCreatedDate:[NSDate date]];

// Set some defaults
if (articleDate == nil)
articleDate = [NSDate date];
if (userName == nil)
userName = @"";

// Parse off the title
if (articleTitle == nil || [articleTitle isBlank])
articleTitle = [articleBody firstNonBlankLine];

// Save date as time intervals
NSTimeInterval interval = [articleDate timeIntervalSince1970];
NSTimeInterval createdInterval = [[article createdDate] timeIntervalSince1970];

// Does this article already exist?
Article * existingArticle = [folder articleFromGuid:articleGuid];
// We're going to ignore the problem of feeds re-using guids, which is very naughty! Bad feed!

// Fix title and article body so they're acceptable to SQL
NSString * preparedArticleTitle = [SQLDatabase prepareStringForQuery:articleTitle];
NSString * preparedArticleText = [SQLDatabase prepareStringForQuery:articleBody];
NSString * preparedArticleLink = [SQLDatabase prepareStringForQuery:articleLink];
NSString * preparedUserName = [SQLDatabase prepareStringForQuery:userName];
NSString * preparedArticleGuid = [SQLDatabase prepareStringForQuery:articleGuid];
NSString * preparedEnclosure = [SQLDatabase prepareStringForQuery:articleEnclosure];

// Unread count adjustment factor
NSInteger adjustment = 0;

// Verify we're on the right thread
[self verifyThreadSafety];

if (existingArticle == nil && [guidHistory containsObject:articleGuid])
{
return NO; // Article has been deleted and removed from database, so ignore
}
else if (existingArticle == nil)
{
SQLResult * results;

results = [sqlDatabase performQueryWithFormat:
@"insert into messages (message_id, parent_id, folder_id, sender, link, date, createddate, read_flag, marked_flag, deleted_flag, title, text, revised_flag, enclosure, hasenclosure_flag) "
@"values('%@', %d, %d, '%@', '%@', %f, %f, %d, %d, %d, '%@', '%@', %d, '%@',%d)",
preparedArticleGuid,
parentId,
folderID,
preparedUserName,
preparedArticleLink,
interval,
createdInterval,
read_flag,
marked_flag,
deleted_flag,
preparedArticleTitle,
preparedArticleText,
revised_flag,
preparedEnclosure,
hasenclosure_flag];
if (!results)
return NO;
[results release];
[self executeSQLWithFormat:@"insert into rss_guids (message_id, folder_id) values ('%@', %d)", preparedArticleGuid, folderID];

// Add the article to the folder
[article setStatus:MA_MsgStatus_New];
[folder addArticleToCache:article];

// Update folder unread count
if (!read_flag)
adjustment = 1;
}
else if ([existingArticle isDeleted])
{
return NO;
}
else if (![[Preferences standardPreferences] boolForKey:MAPref_CheckForUpdatedArticles])
{
return NO;
}
else
{
// The article is revised if either the title or the body has changed.

NSString * existingTitle = [existingArticle title];
BOOL isArticleRevised = ![existingTitle isEqualToString:articleTitle];

if (!isArticleRevised)
{
NSString * existingBody = [existingArticle body];
// If the folder is not displayed, then the article text has not been loaded yet.
if (existingBody == nil)
{
SQLResult * results = [sqlDatabase performQueryWithFormat:@"select text from messages where folder_id=%d and message_id='%@'", folderID, preparedArticleGuid];
if (results && [results rowCount])
{
existingBody = [[results rowAtIndex:0] stringForColumn:@"text"];
}
else
existingBody = @"";
[results release];
}

isArticleRevised = ![existingBody isEqualToString:articleBody];
}

if (isArticleRevised)
{
// Only pre-existing articles should be marked as revised.
// New articles created during the current refresh should not be marked as revised,
// even if there are multiple versions of the new article in the feed.
revised_flag = [existingArticle isRevised];
if (!revised_flag && ([existingArticle status] == MA_MsgStatus_Empty))
revised_flag = YES;

SQLResult * results;
results = [sqlDatabase performQueryWithFormat:@"update messages set parent_id=%d, sender='%@', link='%@', date=%f, "
@"read_flag=0, title='%@', text='%@', revised_flag=%d where folder_id=%d and message_id='%@'",
parentId,
preparedUserName,
preparedArticleLink,
interval,
preparedArticleTitle,
preparedArticleText,
revised_flag,
folderID,
preparedArticleGuid];
if (!results)
return NO;
[results release];

[existingArticle setTitle:articleTitle];
[existingArticle setBody:articleBody];
[existingArticle markRevised:revised_flag];

// Update folder unread count if necessary
if ([existingArticle isRead])
{
adjustment = 1;
[article setStatus:MA_MsgStatus_New];
[existingArticle markRead:NO];
}
else
[article setStatus:MA_MsgStatus_Updated];
}
else
{
return NO;
}
}

// Fix unread count on parent folders
if (adjustment != 0)
{
countOfUnread += adjustment;
[self setFolderUnreadCount:folder adjustment:adjustment];
}
return YES;
}
return NO;
}

/* purgeArticlesOlderThanDays
* Deletes all non-flagged articles from the messages list that are older than the specified
* number of days.
*/
-(void)purgeArticlesOlderThanDays:(NSUInteger)daysToKeep
{
if (daysToKeep > 0)
{
NSInteger dayDelta = (daysToKeep % 1000);
NSInteger monthDelta = (daysToKeep / 1000);
NSTimeInterval timeDiff = [[[NSCalendarDate calendarDate] dateByAddingYears:0 months:-monthDelta days:-dayDelta hours:0 minutes:0 seconds:0] timeIntervalSince1970];

[self verifyThreadSafety];
SQLResult * results = [sqlDatabase performQueryWithFormat:@"update messages set deleted_flag=1 where deleted_flag=0 and marked_flag=0 and read_flag=1 and date < %f", timeDiff];
[results release];
}
}

/* purgeDeletedArticles
* Remove from the database all articles which have the deleted_flag field set to YES. This
* also requires that we remove the same articles from all folder caches.
*/
-(void)purgeDeletedArticles
{
// Verify we're on the right thread
[self verifyThreadSafety];

SQLResult * results = [sqlDatabase performQuery:@"delete from messages where deleted_flag=1"];
if (results)
{
[self compactDatabase];
[trashFolder clearCache];

[[NSNotificationCenter defaultCenter] postNotificationOnMainThreadWithName:@"MA_Notify_FoldersUpdated" object:[NSNumber numberWithInt:[self trashFolderId]]];
}
[results release];
}

/* deleteArticle
* Permanently deletes a article from the specified folder
*/
-(BOOL)deleteArticle:(NSInteger)folderId guid:(NSString *)guid
{
Folder * folder = [self folderFromID:folderId];
if (folder != nil)
{
// Prime the article cache
[self initArticleArray:folder];

Article * article = [folder articleFromGuid:guid];
if (article != nil)
{
NSString * preparedGuid = [SQLDatabase prepareStringForQuery:guid];

// Verify we're on the right thread
[self verifyThreadSafety];

SQLResult * results = [sqlDatabase performQueryWithFormat:@"delete from messages where folder_id=%d and message_id='%@'", folderId, preparedGuid];
if (results)
{
if (![article isRead])
{
[self setFolderUnreadCount:folder adjustment:-1];
--countOfUnread;
}
[folder removeArticleFromCache:guid];
[results release];
return YES;
}
}
}
return NO;
}

/* initSmartfoldersDict
* Preloads all the smart folders into the smartfoldersDict dictionary.
*/
-(void)initSmartfoldersDict
{
if (!initializedSmartfoldersDict)
{
// Make sure we have a database.
NSAssert(sqlDatabase, @"Database not assigned for this item");

SQLResult * results;

// Verify we're on the right thread
[self verifyThreadSafety];

results = [sqlDatabase performQuery:@"select * from smart_folders"];
if (results && [results rowCount])
{
for (SQLRow * row in [results rowEnumerator])
{
NSString * search_string = [row stringForColumn:@"search_string"];
NSInteger folderId = [[row stringForColumn:@"folder_id"] intValue];

CriteriaTree * criteriaTree = [[CriteriaTree alloc] initWithString:search_string];
[smartfoldersDict setObject:criteriaTree forKey:[NSNumber numberWithInt:folderId]];
[criteriaTree release];
}
}
[results release];
initializedSmartfoldersDict = YES;
}
}

/* searchStringForSmartFolder
* Retrieve the smart folder criteria string for the specified folderId. Returns nil if
* folderId is not a smart folder.
*/
-(CriteriaTree *)searchStringForSmartFolder:(NSInteger)folderId
{
[self initSmartfoldersDict];
return [smartfoldersDict objectForKey:[NSNumber numberWithInt:folderId]];
}

/* addSmartFolder
* Create a new smart folder. If the specified folder already exists, then this is synonymous to
* calling updateSearchFolder.
*/
-(NSInteger)addSmartFolder:(NSString *)folderName underParent:(NSInteger)parentId withQuery:(CriteriaTree *)criteriaTree
{
Folder * folder = [self folderFromName:folderName];

if (folder)
{
[self updateSearchFolder:[folder itemId] withFolder:folderName withQuery:criteriaTree];
return [folder itemId];
}

NSInteger folderId = [self addFolder:parentId afterChild:0 folderName:folderName type:MA_Smart_Folder canAppendIndex:NO];
if (folderId != -1)
{
NSString * preparedQueryString = [SQLDatabase prepareStringForQuery:[criteriaTree string]];
[self executeSQLWithFormat:@"insert into smart_folders (folder_id, search_string) values (%d, '%@')", folderId, preparedQueryString];
[smartfoldersDict setObject:criteriaTree forKey:[NSNumber numberWithInt:folderId]];
}
return folderId;
}

/* updateSearchFolder
* Updates the search string for the specified folder.
*/
-(BOOL)updateSearchFolder:(NSInteger)folderId withFolder:(NSString *)folderName withQuery:(CriteriaTree *)criteriaTree
{
Folder * folder = [self folderFromID:folderId];
if (![[folder name] isEqualToString:folderName])
[self setFolderName:folderId newName:folderName];

// Update the smart folder string
NSString * preparedQueryString = [SQLDatabase prepareStringForQuery:[criteriaTree string]];
[self executeSQLWithFormat:@"update smart_folders set search_string='%@' where folder_id=%d", preparedQueryString, folderId];
[smartfoldersDict setObject:criteriaTree forKey:[NSNumber numberWithInt:folderId]];

NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
[nc postNotificationOnMainThreadWithName:@"MA_Notify_FoldersUpdated" object:[NSNumber numberWithInt:folderId]];
return YES;
}

/* initFolderArray
* Initializes the folder array if necessary.
*/
-(void)initFolderArray
{
if (!initializedfoldersDict)
{
// Make sure we have a database.
NSAssert(sqlDatabase, @"Database not assigned for this item");

// Keep running count of total unread articles
countOfUnread = 0;

SQLResult * results;

// Verify we're on the right thread
[self verifyThreadSafety];

results = [sqlDatabase performQuery:@"select * from folders order by folder_id"];
if (results && [results rowCount])
{
for (SQLRow * row in [results rowEnumerator])
{
NSString * name = [row stringForColumn:@"foldername"];
NSDate * lastUpdate = [NSDate dateWithTimeIntervalSince1970:[[row stringForColumn:@"last_update"] doubleValue]];
NSInteger newItemId = [[row stringForColumn:@"folder_id"] intValue];
NSInteger newParentId = [[row stringForColumn:@"parent_id"] intValue];
NSInteger unreadCount = [[row stringForColumn:@"unread_count"] intValue];
NSInteger type = [[row stringForColumn:@"type"] intValue];
NSInteger flags = [[row stringForColumn:@"flags"] intValue];
NSInteger nextSibling = [[row stringForColumn:@"next_sibling"] intValue];
NSInteger firstChild = [[row stringForColumn:@"first_child"] intValue];

Folder * folder = [[[Folder alloc] initWithId:newItemId parentId:newParentId name:name type:type] autorelease];
[folder setNextSiblingId:nextSibling];
[folder setFirstChildId:firstChild];
if (!IsRSSFolder(folder) && !IsGoogleReaderFolder(folder))
unreadCount = 0;
[folder setUnreadCount:unreadCount];
[folder setLastUpdate:lastUpdate];
[folder setFlag:flags];
if (unreadCount > 0)
countOfUnread += unreadCount;
[foldersDict setObject:folder forKey:[NSNumber numberWithInt:newItemId]];

// Remember the trash folder
if (IsTrashFolder(folder))
trashFolder = [folder retain];

// Remember the search folder
if (IsSearchFolder(folder))
searchFolder = [folder retain];
}
}
[results release];

// Load all RSS folders and add them to the list.
results = [sqlDatabase performQuery:@"select * from rss_folders"];
if (results && [results rowCount])
{
for (SQLRow * row in[results rowEnumerator])
{
NSInteger folderId = [[row stringForColumn:@"folder_id"] intValue];
NSString * descriptiontext = [row stringForColumn:@"description"];
NSString * url = [row stringForColumn:@"feed_url"];
NSString * linktext = [row stringForColumn:@"home_page"];
NSString * username = [row stringForColumn:@"username"];
NSString * lastUpdateString = [row stringForColumn:@"last_update_string"];

Folder * folder = [self folderFromID:folderId];
[folder setFeedDescription:descriptiontext];
[folder setHomePage:linktext];
[folder setFeedURL:url];
[folder setLastUpdateString:lastUpdateString];
[folder setUsername:username];
}
}
[results release];

// Fix the childUnreadCount for every parent
for (Folder * folder in [foldersDict objectEnumerator])
{
if ([folder unreadCount] > 0 && [folder parentId] != MA_Root_Folder)
{
Folder * parentFolder = [self folderFromID:[folder parentId]];
while (parentFolder != nil)
{
[parentFolder setChildUnreadCount:[parentFolder childUnreadCount] + [folder unreadCount]];
parentFolder = [self folderFromID:[parentFolder parentId]];
}
}
}
// Done
initializedfoldersDict = YES;
}
}

/* arrayOfFolders
* Returns an NSArray of all folders with the specified parent. It does not include the
* parent folder nor does it include any folders within groups under that parent. Specifically
* it is a single level search and is actually slightly faster than arrayofSubFolders for
* callers that require this distinction.
*/
-(NSArray *)arrayOfFolders:(NSInteger)parentId
{
// Prime the cache
if (initializedfoldersDict == NO)
[self initFolderArray];

NSMutableArray * newArray = [NSMutableArray array];
if (newArray != nil)
{
for (Folder * folder in [foldersDict objectEnumerator])
{
if ([folder parentId] == parentId)
[newArray addObject:folder];
}
}
return [newArray sortedArrayUsingSelector:@selector(folderNameCompare:)];
}

/* arrayOfSubFolders
* Returns an NSArray of all folders from the specified folder down.
*/
-(NSArray *)arrayOfSubFolders:(Folder *)folder
{
NSMutableArray * newArray = [NSMutableArray arrayWithObject:folder];
if (newArray != nil)
{
NSInteger parentId = [folder itemId];

for (Folder * item in [foldersDict objectEnumerator])
{
if ([item parentId] == parentId)
{
if (IsGroupFolder(item))
[newArray addObjectsFromArray:[self arrayOfSubFolders:item]];
else
[newArray addObject:item];
}
}
}
return [newArray sortedArrayUsingSelector:@selector(folderIDCompare:)];
}

/* arrayOfAllFolders
* Returns an unsorted array of all regular folders (RSS folders and group folders).
*/
-(NSArray *)arrayOfAllFolders
{
// Prime the cache
if (initializedfoldersDict == NO)
[self initFolderArray];

return [foldersDict allValues];
}

/* initArticleArray
* Ensures that the specified folder has a minimal cache of article information.
*/
-(BOOL)initArticleArray:(Folder *)folder
{
// Prime the folder cache
[self initFolderArray];

// Exit now if we're already initialized
if ([folder countOfCachedArticles] == -1)
{
NSInteger folderId = [folder itemId];
SQLResult * results;

// Initialize to indicate that the folder array is valid.
[folder markFolderEmpty];

// Verify we're on the right thread
[self verifyThreadSafety];

results = [sqlDatabase performQueryWithFormat:@"select message_id, read_flag, deleted_flag, title, link, revised_flag, hasenclosure_flag, enclosure from messages where folder_id=%d", folderId];
if (results && [results rowCount])
{
NSInteger unread_count = 0;
SQLRow * row;

for (row in [results rowEnumerator])
{
NSString * guid = [row stringForColumn:@"message_id"];
BOOL read_flag = [[row stringForColumn:@"read_flag"] intValue];
BOOL revised_flag = [[row stringForColumn:@"revised_flag"] intValue];
BOOL deleted_flag = [[row stringForColumn:@"deleted_flag"] intValue];
BOOL hasenclosure_flag = [[row stringForColumn:@"hasenclosure_flag"] intValue];
NSString * title = [row stringForColumn:@"title"];
NSString * link = [row stringForColumn:@"link"];
NSString * enclosure = [row stringForColumn:@"enclosure"];

// Keep our own track of unread articles
if (!read_flag)
++unread_count;

Article * article = [[Article alloc] initWithGuid:guid];
[article markRead:read_flag];
[article markRevised:revised_flag];
[article markDeleted:deleted_flag];
[article setFolderId:folderId];
[article setTitle:title];
[article setLink:link];
[article setEnclosure:enclosure];
[article setHasEnclosure:hasenclosure_flag];
[folder addArticleToCache:article];
[article release];
}

// This is a good time to do a quick check to ensure that our
// own count of unread is in sync with the folders count and fix
// them if not.
if (unread_count != [folder unreadCount])
{
NSLog(@"Fixing unread count for %@ (%ld on folder versus %ld in articles)", [folder name], (long)[folder unreadCount], (long)unread_count);
NSInteger diff = (unread_count - [folder unreadCount]);
[self setFolderUnreadCount:folder adjustment:diff];
countOfUnread += diff;
}
}
[results release];
}
return YES;
}

/* setSearchString
* Sets the current search string for the search folder.
*/
-(void)setSearchString:(NSString *)newSearchString
{
[newSearchString retain];
[searchString release];
searchString = newSearchString;
}

/* sqlScopeForFolder
* Create a SQL 'where' clause that scopes to either the individual folder or the folder and
* all sub-folders.
*/
-(NSString *)sqlScopeForFolder:(Folder *)folder flags:(NSInteger)scopeFlags
{
Field * field = [self fieldByName:MA_Field_Folder];
NSString * operatorString = (scopeFlags & MA_Scope_Inclusive) ? @"=" : @"<>";
NSString * conditionString = (scopeFlags & MA_Scope_Inclusive) ? @" or " : @" and ";
BOOL subScope = (scopeFlags & MA_Scope_SubFolders) ? YES : NO; // Avoid problems casting into BOOL.
NSInteger folderId;

// If folder is nil, rather than report an error, default to some impossible value
if (folder != nil)
folderId = [folder itemId];
else
{
subScope = NO;
folderId = 0;
}

// Group folders must always have subscope
if (folder && IsGroupFolder(folder))
subScope = YES;

// Straightforward folder is <something>
if (!subScope)
return [NSString stringWithFormat:@"%@%@%ld", [field sqlField], operatorString, folderId];

// For under/not-under operators, we're creating a SQL statement of the format
// (folder_id = <value1> || folder_id = <value2>...). It is possible to try and simplify
// the string by looking for ranges but I suspect that given the spread of IDs this may
// well be false optimisation.
//
NSArray * childFolders = [self arrayOfSubFolders:folder];
NSMutableString * sqlString = [[NSMutableString alloc] init];
NSInteger count = [childFolders count];
NSInteger index;

if (count > 1)
[sqlString appendString:@"("];
for (index = 0; index < count; ++index)
{
Folder * folder = [childFolders objectAtIndex:index];
if (index > 0)
[sqlString appendString:conditionString];
[sqlString appendFormat:@"%@%@%ld", [field sqlField], operatorString, [folder itemId]];
}
if (count > 1)
[sqlString appendString:@")"];
return [sqlString autorelease];
}

/* criteriaToSQL
* Converts a criteria tree to it's SQL representative.
*/
-(NSString *)criteriaToSQL:(CriteriaTree *)criteriaTree
{
NSMutableString * sqlString = [[NSMutableString alloc] init];
NSInteger count = 0;

for (Criteria * criteria in [criteriaTree criteriaEnumerator])
{
Field * field = [self fieldByName:[criteria field]];
NSAssert1(field != nil, @"Criteria field %@ does not have an associated database field", [criteria field]);

NSString * operatorString = nil;
NSString * valueString = nil;

switch ([criteria operator])
{
case MA_CritOper_Is: operatorString = @"=%@"; break;
case MA_CritOper_IsNot: operatorString = @"<>%@"; break;
case MA_CritOper_IsLessThan: operatorString = @"<%@"; break;
case MA_CritOper_IsGreaterThan: operatorString = @">%@"; break;
case MA_CritOper_IsLessThanOrEqual: operatorString = @"<=%@"; break;
case MA_CritOper_IsGreaterThanOrEqual: operatorString = @">=%@"; break;
case MA_CritOper_Contains: operatorString = @" regexp '%@'"; break;
case MA_CritOper_NotContains: operatorString = @" not regexp '%@'"; break;
case MA_CritOper_IsBefore: operatorString = @"<%@"; break;
case MA_CritOper_IsAfter: operatorString = @">%@"; break;
case MA_CritOper_IsOnOrBefore: operatorString = @"<=%@"; break;
case MA_CritOper_IsOnOrAfter: operatorString = @">=%@"; break;

case MA_CritOper_Under:
case MA_CritOper_NotUnder:
// Handle the operatorString later. For now just make sure we're working with the
// right field types.
NSAssert([field type] == MA_FieldType_Folder, @"Under operators only valid for folder field types");
break;
}

// Unknown operator - skip this clause
if (operatorString == nil)
continue;

if (count++ > 0)
[sqlString appendString:[criteriaTree condition] == MA_CritCondition_All ? @" and " : @" or "];

switch ([field type])
{
case MA_FieldType_Flag:
valueString = [[criteria value] isEqualToString:@"Yes"] ? @"1" : @"0";
break;

case MA_FieldType_Folder: {
Folder * folder = [self folderFromName:[criteria value]];
NSInteger scopeFlags = 0;

switch ([criteria operator])
{
case MA_CritOper_Under: scopeFlags = MA_Scope_SubFolders|MA_Scope_Inclusive; break;
case MA_CritOper_NotUnder: scopeFlags = MA_Scope_SubFolders; break;
case MA_CritOper_Is: scopeFlags = MA_Scope_Inclusive; break;
case MA_CritOper_IsNot: scopeFlags = 0; break;
default: NSAssert(false, @"Invalid operator for folder field type");
}
[sqlString appendString:[self sqlScopeForFolder:folder flags:scopeFlags]];
break;
}

case MA_FieldType_Date: {
NSCalendarDate * startDate = [NSCalendarDate date];
NSString * criteriaValue = [[criteria value] lowercaseString];
NSInteger spanOfDays = 1;

// "yesterday" is a short hand way of specifying the previous day.
if ([criteriaValue isEqualToString:@"yesterday"])
{
startDate = [startDate dateByAddingYears:0 months:0 days:-1 hours:0 minutes:0 seconds:0];
}
// "last week" is a short hand way of specifying a range from 7 days ago to today.
else if ([criteriaValue isEqualToString:@"last week"])
{
startDate = [startDate dateByAddingYears:0 months:0 days:-6 hours:0 minutes:0 seconds:0];
spanOfDays = 7;
}

criteriaValue = [NSString stringWithFormat:@"%ld/%ld/%ld %d:%d:%d", [startDate dayOfMonth], [startDate monthOfYear], [startDate yearOfCommonEra], 0, 0, 0];
startDate = [NSCalendarDate dateWithString:criteriaValue calendarFormat:@"%d/%m/%Y %H:%M:%S"];

if ([criteria operator] == MA_CritOper_Is)
{
NSCalendarDate * endDate;

// Special case for Date is <date> because the resolution of the date field is in
// milliseconds. So we need to translate this to a range for this to make sense.
endDate = [startDate dateByAddingYears:0 months:0 days:spanOfDays hours:0 minutes:0 seconds:0];
operatorString = [NSString stringWithFormat:@">=%f and %@<%f", [startDate timeIntervalSince1970], [field sqlField], [endDate timeIntervalSince1970]];
valueString = @"";
}
else
{
if (([criteria operator] == MA_CritOper_IsAfter) || ([criteria operator] == MA_CritOper_IsOnOrBefore))
startDate = [startDate dateByAddingYears:0 months:0 days:0 hours:23 minutes:59 seconds:59];
valueString = [NSString stringWithFormat:@"%f", [startDate timeIntervalSince1970]];
}
break;
}

case MA_FieldType_String:
if ([field tag] == MA_FieldID_Text)
{
// Special case for searching the text field. We always include the title field in the
// search so the resulting SQL statement becomes:
//
// (text op value or title op value)
//
// where op is the appropriate operator.
//
Field * titleField = [self fieldByName:MA_Field_Subject];
NSString * value = [NSString stringWithFormat:operatorString, [criteria value]];
[sqlString appendFormat:@"(%@%@ or %@%@)", [field sqlField], value, [titleField sqlField], value];
break;
}

case MA_FieldType_Integer:
valueString = [NSString stringWithFormat:@"%@", [criteria value]];
break;
}

if (valueString != nil)
{
[sqlString appendString:[field sqlField]];
[sqlString appendFormat:operatorString, valueString];
}
}
return [sqlString autorelease];
}

/* criteriaForFolder
* Returns the CriteriaTree that will return the folder contents.
*/
-(CriteriaTree *)criteriaForFolder:(NSInteger)folderId
{
Folder * folder = [self folderFromID:folderId];
if (folder == nil)
return nil;

if (IsSearchFolder(folder))
return [self searchStringToTree];

if (IsTrashFolder(folder))
{
CriteriaTree * tree = [[CriteriaTree alloc] init];
Criteria * clause = [[Criteria alloc] initWithField:MA_Field_Deleted withOperator:MA_CritOper_Is withValue:@"Yes"];
[tree addCriteria:clause];
[clause release];
return [tree autorelease];
}

if (IsSmartFolder(folder))
{
[self initSmartfoldersDict];
return [smartfoldersDict objectForKey:[NSNumber numberWithInt:folderId]];
}

CriteriaTree * tree = [[CriteriaTree alloc] init];
Criteria * clause = [[Criteria alloc] initWithField:MA_Field_Folder withOperator:MA_CritOper_Under withValue:[folder name]];
[tree addCriteria:clause];
[clause release];
return [tree autorelease];
}

/* arrayOfUnreadArticles
* Retrieves an array of ArticleReference objects that represent all unread
* articles in the specified folder.
*/
-(NSArray *)arrayOfUnreadArticles:(NSInteger)folderId
{
Folder * folder = [self folderFromID:folderId];
NSMutableArray * newArray = [NSMutableArray arrayWithCapacity:[folder unreadCount]];
if (folder != nil)
{
if ([folder countOfCachedArticles] > 0)
{
// Messages already cached in this folder so use those. Note the use of
// reverseObjectEnumerator since the odds are that the unread articles are
// likely to be clustered with the most recent articles at the end of the
// array so it makes the code slightly faster.
NSInteger unreadCount = [folder unreadCount];
NSEnumerator * enumerator = [[folder articles] reverseObjectEnumerator];
Article * theRecord;

while (unreadCount > 0 && (theRecord = [enumerator nextObject]) != nil)
if (![theRecord isRead])
{
[newArray addObject:[ArticleReference makeReference:theRecord]];
--unreadCount;
}
}
else
{
[self verifyThreadSafety];
SQLResult * results = [sqlDatabase performQueryWithFormat:@"select message_id from messages where folder_id=%d and read_flag=0", folderId];
if (results && [results rowCount])
{
for (SQLRow * row in [results rowEnumerator])
{
NSString * guid = [row stringForColumn:@"message_id"];
[newArray addObject:[ArticleReference makeReferenceFromGUID:guid inFolder:folderId]];
}
}
[results release];
}
}
return newArray;
}

/* arrayOfArticles
* Retrieves an array containing all articles (except for text) for the
* specified folder. If folderId is zero, all folders are searched. The
* filterString option constrains the array to all those articles that
* contain the specified filter.
*/
-(NSArray *)arrayOfArticles:(NSInteger)folderId filterString:(NSString *)filterString
{
NSMutableArray * newArray = [NSMutableArray array];
NSString * filterClause = @"";
NSString * queryString;
Folder * folder = nil;
NSInteger unread_count = 0;

// If folderId is zero then we're searching the entire
// database with or without a filter string.
if (folderId == 0)
{
if ([filterString isNotEqualTo:@""])
filterClause = [NSString stringWithFormat:@" where text like '%%%@%%'", filterString];
queryString = [NSString stringWithFormat:@"select * from messages%@", filterClause];
}
else
{
folder = [self folderFromID:folderId];
if (folder == nil)
return nil;
[folder clearCache];

// Construct a criteria tree for this query
CriteriaTree * tree = [self criteriaForFolder:folderId];

if ([filterString isNotEqualTo:@""])
filterClause = [NSString stringWithFormat:@" and (title like '%%%@%%' or text like '%%%@%%')", filterString, filterString];
queryString = [NSString stringWithFormat:@"select * from messages where (%@)%@", [self criteriaToSQL:tree], filterClause];
}

[[RefreshManager articlesUpdateSemaphore] lock];
// Verify we're on the right thread
[self verifyThreadSafety];

// Time to run the query
SQLResult * results = [sqlDatabase performQuery:queryString];
if (results && [results rowCount])
{
        Article * article;
        NSString * text;

for (SQLRow * row in [results rowEnumerator])
{
article = [[Article alloc] initWithGuid:[row stringForColumn:@"message_id"]];
[article setTitle:[row stringForColumn:@"title"]];
[article setAuthor:[row stringForColumn:@"sender"]];
[article setLink:[row stringForColumn:@"link"]];
[article setEnclosure:[row stringForColumn:@"enclosure"]];
[article setHasEnclosure:[[row stringForColumn:@"hasenclosure_flag"] intValue]];
[article setDate:[NSDate dateWithTimeIntervalSince1970:[[row stringForColumn:@"date"] doubleValue]]];
[article setCreatedDate:[NSDate dateWithTimeIntervalSince1970:[[row stringForColumn:@"createddate"] doubleValue]]];
[article markRead:[[row stringForColumn:@"read_flag"] intValue]];
[article markRevised:[[row stringForColumn:@"revised_flag"] intValue]];
[article markFlagged:[[row stringForColumn:@"marked_flag"] intValue]];
[article markDeleted:[[row stringForColumn:@"deleted_flag"] intValue]];
[article setFolderId:[[row stringForColumn:@"folder_id"] intValue]];
[article setParentId:[[row stringForColumn:@"parent_id"] intValue]];
            text = [row stringForColumn:@"text"];
[article setBody:text];
if (folder == nil || ![article isDeleted] || IsTrashFolder(folder))
[newArray addObject:article];
[folder addArticleToCache:article];
            
// Keep our own track of unread articles
if (![article isRead])
++unread_count;
            
[article release];
}

// This is a good time to do a quick check to ensure that our
// own count of unread is in sync with the folders count and fix
// them if not.
if (folder && [filterString isEqualTo:@""] && IsRSSFolder(folder))
{
if (unread_count != [folder unreadCount])
{
NSLog(@"Fixing unread count for %@ (%ld on folder versus %ld in articles)", [folder name], (long)[folder unreadCount], (long)unread_count);
NSInteger diff = (unread_count - [folder unreadCount]);
[self setFolderUnreadCount:folder adjustment:diff];
countOfUnread += diff;
}
}
}

// Deallocate
[results release];
[[RefreshManager articlesUpdateSemaphore] unlock];
return newArray;
}

/* markFolderRead
* Mark all articles in the folder and sub-folders read. This should be called
* within a transaction since it is SQL intensive.
*/
-(BOOL)markFolderRead:(NSInteger)folderId
{
Folder * folder;
BOOL result = NO;

// Recurse and mark child folders read too
for (folder in [self arrayOfFolders:folderId])
{
if ([self markFolderRead:[folder itemId]])
result = YES;
}

folder = [self folderFromID:folderId];
if (folder != nil && [folder unreadCount] > 0)
{
[self verifyThreadSafety];
SQLResult * results = [sqlDatabase performQueryWithFormat:@"update messages set read_flag=1 where folder_id=%d and read_flag=0", folderId];
if (results)
{
NSInteger count = [folder unreadCount];
if ([folder countOfCachedArticles] > 0)
{
NSEnumerator * enumerator = [[folder articles] objectEnumerator];
NSInteger remainingUnread = count;
Article * article;

while (remainingUnread > 0 && (article = [enumerator nextObject]) != nil)
if (![article isRead])
{
[article markRead:YES];
--remainingUnread;
}
}
countOfUnread -= count;
[self setFolderUnreadCount:folder adjustment:-count];
}
[results release];
result = YES;
}
return result;
}

/* markArticleRead
* Marks a article as read or unread.
*/
-(void)markArticleRead:(NSInteger)folderId guid:(NSString *)guid isRead:(BOOL)isRead
{
Folder * folder = [self folderFromID:folderId];
if (folder != nil)
{
// Prime the article cache
[self initArticleArray:folder];

Article * article = [folder articleFromGuid:guid];
if (article != nil && isRead != [article isRead])
{
NSString * preparedGuid = [SQLDatabase prepareStringForQuery:guid];

// Verify we're on the right thread
[self verifyThreadSafety];

// Mark an individual article read
SQLResult * results = [sqlDatabase performQueryWithFormat:@"update messages set read_flag=%d where folder_id=%d and message_id='%@'", isRead, folderId, preparedGuid];
if (results)
{
NSInteger adjustment = (isRead ? -1 : 1);

[article markRead:isRead];
countOfUnread += adjustment;
[self setFolderUnreadCount:folder adjustment:adjustment];
}
[results release];
}
}
}

/* setFolderUnreadCount
* Adjusts the unread count on the specified folder by the given delta. The same delta is
* also applied to the childUnreadCount of all ancestor folders.
*/
-(void)setFolderUnreadCount:(Folder *)folder adjustment:(NSUInteger)adjustment
{
NSInteger unreadCount = [folder unreadCount];
[folder setUnreadCount:unreadCount + adjustment];
[self executeSQLWithFormat:@"update folders set unread_count=%d where folder_id=%d", [folder unreadCount], [folder itemId]];

// Update childUnreadCount for our parent. Since we're just working
// on one article, we do this the faster way.
Folder * tmpFolder = folder;
while ([tmpFolder parentId] != MA_Root_Folder)
{
tmpFolder = [self folderFromID:[tmpFolder parentId]];
[tmpFolder setChildUnreadCount:[tmpFolder childUnreadCount] + adjustment];
}
}

/* markArticleFlagged
* Marks a article as flagged or unflagged.
*/
-(void)markArticleFlagged:(NSInteger)folderId guid:(NSString *)guid isFlagged:(BOOL)isFlagged
{
NSString * preparedGuid = [SQLDatabase prepareStringForQuery:guid];
[self executeSQLWithFormat:@"update messages set marked_flag=%d where folder_id=%d and message_id='%@'", isFlagged, folderId, preparedGuid];
}

/* markArticleDeleted
* Marks a article as deleted. Deleted articles always get marked read first.
*/
-(void)markArticleDeleted:(NSInteger)folderId guid:(NSString *)guid isDeleted:(BOOL)isDeleted
{
if (isDeleted)
[self markArticleRead:folderId guid:guid isRead:YES];
NSString * preparedGuid = [SQLDatabase prepareStringForQuery:guid];
[self executeSQLWithFormat:@"update messages set deleted_flag=%d where folder_id=%d and message_id='%@'", isDeleted, folderId, preparedGuid];
}

/* isTrashEmpty
* Returns YES if there are no deleted articles, NO if there are deleted articles
*/
-(BOOL)isTrashEmpty
{
[self verifyThreadSafety];
SQLResult * results = [sqlDatabase performQuery:@"select deleted_flag from messages where deleted_flag=1"];
if (results)
{
if ([results rowCount] > 0)
{
[results release];
return NO;
}
else
{
[results release];
return YES;
}
}
else
return YES;
}

/* guidHistoryForFolderId
* Returns an array of all article guids ever downloaded for the specified folder.
*/
-(NSArray *)guidHistoryForFolderId:(NSInteger)folderId
{
NSMutableArray * articleGuids = [NSMutableArray array];

[self verifyThreadSafety];
SQLResult * results = [sqlDatabase performQueryWithFormat:@"select message_id from rss_guids where folder_id=%i", folderId];
if (results)
{
if ([results rowCount] > 0)
{
for (SQLRow * row in [results rowEnumerator])
{
NSString * guid = [row stringForColumn:@"message_id"];
if (guid != nil)
{
[articleGuids addObject:guid];
}
}
}

[results release];
}

return articleGuids;
}

/* close
* Close the database. All internal resources are released and a new,
* possibly different, database can be opened instead.
*/
-(void)close
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[foldersDict removeAllObjects];
[smartfoldersDict removeAllObjects];
[fieldsOrdered release];
[fieldsByName release];
[trashFolder release];
[sqlDatabase close];
initializedfoldersDict = NO;
initializedSmartfoldersDict = NO;
countOfUnread = 0;
sqlDatabase = nil;
}

/* dealloc
* Clean up and release resources.
*/
-(void)dealloc
{
[searchString release];
[foldersDict release];
[smartfoldersDict release];
if (sqlDatabase)
[self close];
[sqlDatabase release];
[super dealloc];
}
@end
Something went wrong with that request. Please try again.