summaryrefslogtreecommitdiffstats
path: root/kmail/kmfoldercachedimap.cpp
blob: dcfcbe7f11c7363a47aac0691acfcfe2814972d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
/**
 *  kmfoldercachedimap.cpp
 *
 *  Copyright (c) 2002-2004 Bo Thorsen <bo@sonofthor.dk>
 *  Copyright (c) 2002-2003 Steffen Hansen <steffen@klaralvdalens-datakonsult.se>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *  In addition, as a special exception, the copyright holders give
 *  permission to link the code of this program with any edition of
 *  the TQt library by Trolltech AS, Norway (or with modified versions
 *  of TQt that use the same license as TQt), and distribute linked
 *  combinations including the two.  You must obey the GNU General
 *  Public License in all respects for all of the code used other than
 *  TQt.  If you modify this file, you may extend this exception to
 *  your version of the file, but you are not obligated to do so.  If
 *  you do not wish to do so, delete this exception statement from
 *  your version.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>

#include <tqvaluevector.h>

#include "kmkernel.h"
#include "kmfoldercachedimap.h"
#include "undostack.h"
#include "kmfoldermgr.h"
#include "kmacctcachedimap.h"
#include "accountmanager.h"
using KMail::AccountManager;
#include "kmailicalifaceimpl.h"
#include "kmfolder.h"
#include "kmglobal.h"
#include "acljobs.h"
#include "broadcaststatus.h"
using KPIM::BroadcastStatus;
#include "progressmanager.h"

using KMail::CachedImapJob;
#include "imapaccountbase.h"
using KMail::ImapAccountBase;
#include "listjob.h"
using KMail::ListJob;

#include "kmfolderseldlg.h"
#include "kmcommands.h"
#include "kmmainwidget.h"

#include <tdeapplication.h>
#include <tdemessagebox.h>
#include <tdelocale.h>
#include <kdebug.h>
#include <tdeconfig.h>
#include <tdeio/global.h>
#include <tdeio/scheduler.h>
#include <tqbuffer.h>
#include <tqbuttongroup.h>
#include <tqcombobox.h>
#include <tqfile.h>
#include <tqhbox.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqradiobutton.h>
#include <tqvaluelist.h>
#include "annotationjobs.h"
#include "quotajobs.h"
using namespace KMail;
#include <globalsettings.h>

#define UIDCACHE_VERSION 1
#define MAIL_LOSS_DEBUGGING 0

static TQString incidencesForToString( KMFolderCachedImap::IncidencesFor r ) {
  switch (r) {
  case KMFolderCachedImap::IncForNobody: return "nobody";
  case KMFolderCachedImap::IncForAdmins: return "admins";
  case KMFolderCachedImap::IncForReaders: return "readers";
  }
  return TQString(); // can't happen
}

static KMFolderCachedImap::IncidencesFor incidencesForFromString( const TQString& str ) {
  if ( str == "nobody" ) return KMFolderCachedImap::IncForNobody;
  if ( str == "admins" ) return KMFolderCachedImap::IncForAdmins;
  if ( str == "readers" ) return KMFolderCachedImap::IncForReaders;
  return KMFolderCachedImap::IncForAdmins; // by default
}

DImapTroubleShootDialog::DImapTroubleShootDialog( TQWidget* parent,
                                                  const char* name )
  : KDialogBase( Plain, i18n( "Troubleshooting IMAP Cache" ),
                 Ok | Cancel, Cancel, parent, name, true ),
    rc( None )
{
  TQFrame* page = plainPage();
  TQVBoxLayout *topLayout = new TQVBoxLayout( page, 0 );
  // spell "lose" correctly. but don't cause a fuzzy.
  TQString txt = i18n( "<p><b>Troubleshooting the IMAP cache.</b></p>"
                      "<p>If you have problems with synchronizing an IMAP "
                      "folder, you should first try rebuilding the index "
                      "file. This will take some time to rebuild, but will "
                      "not cause any problems.</p><p>If that is not enough, "
                      "you can try refreshing the IMAP cache. If you do this, "
                      "you will loose all your local changes for this folder "
                      "and all its subfolders.</p>",
                      "<p><b>Troubleshooting the IMAP cache.</b></p>"
                      "<p>If you have problems with synchronizing an IMAP "
                      "folder, you should first try rebuilding the index "
                      "file. This will take some time to rebuild, but will "
                      "not cause any problems.</p><p>If that is not enough, "
                      "you can try refreshing the IMAP cache. If you do this, "
                      "you will lose all your local changes for this folder "
                      "and all its subfolders.</p>" );
  topLayout->addWidget( new TQLabel( txt, page ) );

  mButtonGroup = new TQButtonGroup( 0 );

  mIndexButton = new TQRadioButton( page );
  mIndexButton->setText( i18n( "Rebuild &Index" ) );
  mButtonGroup->insert( mIndexButton );
  topLayout->addWidget( mIndexButton );

  TQHBox *hbox = new TQHBox( page );
  TQLabel *scopeLabel = new TQLabel( i18n( "Scope:" ), hbox );
  scopeLabel->setEnabled( false );
  mIndexScope = new TQComboBox( hbox );
  mIndexScope->insertItem( i18n( "Only current folder" ) );
  mIndexScope->insertItem( i18n( "Current folder and all subfolders" ) );
  mIndexScope->insertItem( i18n( "All folders of this account" ) );
  mIndexScope->setEnabled( false );
  topLayout->addWidget( hbox );

  mCacheButton = new TQRadioButton( page );
  mCacheButton->setText( i18n( "Refresh &Cache" ) );
  mButtonGroup->insert( mCacheButton );
  topLayout->addWidget( mCacheButton );

  enableButtonSeparator( true );

  connect ( mIndexButton, TQT_SIGNAL(toggled(bool)), mIndexScope, TQT_SLOT(setEnabled(bool)) );
  connect ( mIndexButton, TQT_SIGNAL(toggled(bool)), scopeLabel, TQT_SLOT(setEnabled(bool)) );
  connect( mButtonGroup, TQT_SIGNAL( clicked( int ) ), TQT_SLOT( slotChanged() ) );
  connect( this, TQT_SIGNAL( okClicked () ), this, TQT_SLOT( slotDone() ) );
  enableButtonOK( false );
}

int DImapTroubleShootDialog::run()
{
  DImapTroubleShootDialog d;
  d.exec();
  return d.rc;
}

void DImapTroubleShootDialog::slotChanged()
{
  enableButtonOK( mButtonGroup->selected() != 0 );
}

void DImapTroubleShootDialog::slotDone()
{
  rc = None;
  if ( mIndexButton->isOn() )
    rc = mIndexScope->currentItem();
  else if ( mCacheButton->isOn() )
    rc = RefreshCache;
  done( Ok );
}

KMFolderCachedImap::KMFolderCachedImap( KMFolder* folder, const char* aName )
  : KMFolderMaildir( folder, aName ),
    mSyncState( SYNC_STATE_INITIAL ), mContentState( imapNoInformation ),
    mSubfolderState( imapNoInformation ),
    mIncidencesFor( IncForAdmins ),
    mSharedSeenFlags( false ),
    mIsSelected( false ),
    mCheckFlags( true ), mReadOnly( false ), mAccount( NULL ), uidMapDirty( true ),
    uidWriteTimer( -1 ), mLastUid( 0 ), mTentativeHighestUid( 0 ),
    mFoundAnIMAPDigest( false ),
    mUserRights( 0 ), mOldUserRights( 0 ), mUserRightsState( KMail::ACLJobs::NotFetchedYet ),
    mACLListState( KMail::ACLJobs::NotFetchedYet ),
    mSilentUpload( false ),
    /*mHoldSyncs( false ),*/
    mFolderRemoved( false ),
    mRecurse( true ),
    mQuotaOnly( false ),
    mAnnotationFolderTypeChanged( false ),
    mIncidencesForChanged( false ),
    mSharedSeenFlagsChanged( false ),
    mStatusChangedLocally( false ),
    mPersonalNamespacesCheckDone( true ),
    mQuotaInfo(), mSomeSubFolderCloseToQuotaChanged( false ), mAlarmsBlocked( false ),
    mRescueCommandCount( 0 ),
    mPermanentFlags( 31 ) // assume standard flags by default (see imap4/imapinfo.h for bit fields values)
{
  setUidValidity("");
  // if we fail to read a uid file but there is one, nuke it
  if ( readUidCache() == -1 ) {
    if ( TQFile::exists( uidCacheLocation() ) ) {
        KMessageBox::error( 0,
        i18n( "The UID cache file for folder %1 could not be read. There "
              "could be a problem with file system permission, or it is corrupted."
              ).arg( folder->prettyURL() ) );
        // try to unlink it, in case it was corruped. If it couldn't be read
        // because of permissions, this will fail, which is fine
        unlink( TQFile::encodeName( uidCacheLocation() ) );
    }
  }

  mProgress = 0;
}

KMFolderCachedImap::~KMFolderCachedImap()
{
  if (kmkernel->undoStack()) kmkernel->undoStack()->folderDestroyed( folder() );
  writeConfig();
}

void KMFolderCachedImap::reallyDoClose( const char* owner )
{
  if( !mFolderRemoved ) {
    writeUidCache();
  }
  KMFolderMaildir::reallyDoClose( owner );
}

void KMFolderCachedImap::initializeFrom( KMFolderCachedImap* parent )
{
  setAccount( parent->account() );
  // Now that we have an account, tell it that this folder was created:
  // if this folder was just removed, then we don't really want to remove it from the server.
  mAccount->removeDeletedFolder( imapPath() );
  setUserRights( parent->userRights(), parent->userRightsState() );
}

void KMFolderCachedImap::readConfig()
{
  TDEConfig* config = KMKernel::config();
  TDEConfigGroupSaver saver( config, "Folder-" + folder()->idString() );
  if( mImapPath.isEmpty() ) mImapPath = config->readEntry( "ImapPath" );
  if( TQString( name() ).upper() == "INBOX" && mImapPath == "/INBOX/" )
  {
    folder()->setLabel( i18n( "inbox" ) );
    // for the icon
    folder()->setSystemFolder( true );
  }
  mNoContent = config->readBoolEntry( "NoContent", false );
  mReadOnly = config->readBoolEntry( "ReadOnly", false );
  if ( !config->readEntry( "FolderAttributes" ).isEmpty() )
    mFolderAttributes = config->readEntry( "FolderAttributes" );

  if ( mAnnotationFolderType != "FROMSERVER" ) {
    mAnnotationFolderType = config->readEntry( "Annotation-FolderType" );
    // if there is an annotation, it has to be XML
    if ( !mAnnotationFolderType.isEmpty() && !mAnnotationFolderType.startsWith( "mail" ) )
      kmkernel->iCalIface().setStorageFormat( folder(), KMailICalIfaceImpl::StorageXML );
//    kdDebug(5006) << ( mImapPath.isEmpty() ? label() : mImapPath )
//                  << " readConfig: mAnnotationFolderType=" << mAnnotationFolderType << endl;
  }
  mIncidencesFor = incidencesForFromString( config->readEntry( "IncidencesFor" ) );
  mAlarmsBlocked = config->readBoolEntry( "AlarmsBlocked", false );
//  kdDebug(5006) << ( mImapPath.isEmpty() ? label() : mImapPath )
//                << " readConfig: mIncidencesFor=" << mIncidencesFor << endl;
  mSharedSeenFlags = config->readBoolEntry( "SharedSeenFlags", false );

  mUserRights = config->readNumEntry( "UserRights", 0 );
  mUserRightsState = static_cast<KMail::ACLJobs::ACLFetchState>(
      config->readNumEntry( "UserRightsState", KMail::ACLJobs::NotFetchedYet ) );
  mOldUserRights = mUserRights;

  int storageQuotaUsage = config->readNumEntry( "StorageQuotaUsage", -1 );
  int storageQuotaLimit = config->readNumEntry( "StorageQuotaLimit", -1 );
  TQString storageQuotaRoot = config->readEntry( "StorageQuotaRoot", TQString() );
  if ( !storageQuotaRoot.isNull() ) { // isEmpty() means we know there is no quota set
      mQuotaInfo.setName( "STORAGE" );
      mQuotaInfo.setRoot( storageQuotaRoot );

      if ( storageQuotaUsage > -1 )
        mQuotaInfo.setCurrent( storageQuotaUsage );
      if ( storageQuotaLimit > -1 )
        mQuotaInfo.setMax( storageQuotaLimit );
  }

  KMFolderMaildir::readConfig();

  mStatusChangedLocally =
    config->readBoolEntry( "StatusChangedLocally", false );
  TQStringList uidsChanged = config->readListEntry( "UIDStatusChangedLocally" );
  for ( TQStringList::iterator it = uidsChanged.begin(); it != uidsChanged.end(); it++ ) {
    mUIDsOfLocallyChangedStatuses.insert( ( *it ).toUInt() );
  }

  mAnnotationFolderTypeChanged = config->readBoolEntry( "AnnotationFolderTypeChanged", false );
  mIncidencesForChanged = config->readBoolEntry( "IncidencesForChanged", false );
  mSharedSeenFlagsChanged = config->readBoolEntry( "SharedSeenFlagsChanged", false );

  if ( mImapPath.isEmpty() ) {
    mImapPathCreation = config->readEntry("ImapPathCreation");
  }

  TQStringList delUids = config->readListEntry( "UIDSDeletedSinceLastSync" );
#if MAIL_LOSS_DEBUGGING
  kdDebug( 5006 ) << "READING IN UIDSDeletedSinceLastSync: " << folder()->prettyURL() << endl << uids << endl;
#endif
  for ( TQStringList::iterator it = delUids.begin(); it != delUids.end(); it++ ) {
    mDeletedUIDsSinceLastSync.insert( (*it).toULong(), 0);
  }
}

void KMFolderCachedImap::writeConfig()
{
  // don't re-write the config of a removed folder, this has just been deleted in
  // the folder manager
  if ( mFolderRemoved )
    return;

  TDEConfigGroup configGroup( KMKernel::config(), "Folder-" + folder()->idString() );
  configGroup.writeEntry( "ImapPath", mImapPath );
  configGroup.writeEntry( "NoContent", mNoContent );
  configGroup.writeEntry( "ReadOnly", mReadOnly );
  configGroup.writeEntry( "FolderAttributes", mFolderAttributes );

  // StatusChangedLocally is always false, as we use UIDStatusChangedLocally now
  configGroup.writeEntry( "StatusChangedLocally", false );
  TQStringList uidsToWrite;
  for( std::set<ulong>::iterator it = mUIDsOfLocallyChangedStatuses.begin();
       it != mUIDsOfLocallyChangedStatuses.end(); it++ ) {
    uidsToWrite.append( TQString::number( (*it) ) );
  }
  configGroup.writeEntry( "UIDStatusChangedLocally", uidsToWrite );
  if ( !mImapPathCreation.isEmpty() ) {
    if ( mImapPath.isEmpty() ) {
      configGroup.writeEntry( "ImapPathCreation", mImapPathCreation );
    } else {
      configGroup.deleteEntry( "ImapPathCreation" );
    }
  }
  if ( !mDeletedUIDsSinceLastSync.isEmpty() ) {
      TQValueList<ulong> uids = mDeletedUIDsSinceLastSync.keys();
      TQStringList uidstrings;
      for( TQValueList<ulong>::iterator it = uids.begin(); it != uids.end(); it++ ) {
          uidstrings.append(  TQString::number( (*it) ) );
      }
      configGroup.writeEntry( "UIDSDeletedSinceLastSync", uidstrings );
#if MAIL_LOSS_DEBUGGING
      kdDebug( 5006 ) << "WRITING OUT UIDSDeletedSinceLastSync in: " << folder( )->prettyURL( ) << endl << uidstrings << endl;
#endif
  } else {
    configGroup.deleteEntry( "UIDSDeletedSinceLastSync" );
  }
  writeConfigKeysWhichShouldNotGetOverwrittenByReadConfig();
  KMFolderMaildir::writeConfig();
}

void KMFolderCachedImap::writeConfigKeysWhichShouldNotGetOverwrittenByReadConfig()
{
  TDEConfigGroup configGroup( KMKernel::config(), "Folder-" + folder()->idString() );
  if ( !folder()->noContent() )
  {
    configGroup.writeEntry( "AnnotationFolderTypeChanged", mAnnotationFolderTypeChanged );
    configGroup.writeEntry( "Annotation-FolderType", mAnnotationFolderType );
    configGroup.writeEntry( "IncidencesForChanged", mIncidencesForChanged );
    configGroup.writeEntry( "IncidencesFor", incidencesForToString( mIncidencesFor ) );
    configGroup.writeEntry( "AlarmsBlocked", mAlarmsBlocked );
    configGroup.writeEntry( "SharedSeenFlags", mSharedSeenFlags );
    configGroup.writeEntry( "SharedSeenFlagsChanged", mSharedSeenFlagsChanged );
    if ( mUserRightsState != KMail::ACLJobs::FetchFailed ) { // No point in overwriting valid results with invalid ones
      configGroup.writeEntry( "UserRights", mUserRights );
      configGroup.writeEntry( "UserRightsState", mUserRightsState );
    }

    configGroup.deleteEntry( "StorageQuotaUsage");
    configGroup.deleteEntry( "StorageQuotaRoot");
    configGroup.deleteEntry( "StorageQuotaLimit");

    if ( mQuotaInfo.isValid() ) {
      if ( mQuotaInfo.current().isValid() ) {
        configGroup.writeEntry( "StorageQuotaUsage", mQuotaInfo.current().toInt() );
      }
      if ( mQuotaInfo.max().isValid() ) {
        configGroup.writeEntry( "StorageQuotaLimit", mQuotaInfo.max().toInt() );
      }
      configGroup.writeEntry( "StorageQuotaRoot", mQuotaInfo.root() );
    }
  }
}

int KMFolderCachedImap::create()
{
  int rc = KMFolderMaildir::create();
  // FIXME why the below? - till
  readConfig();
  mUnreadMsgs = -1;
  return rc;
}

void KMFolderCachedImap::remove()
{
  mFolderRemoved = true;

  TQString part1 = folder()->path() + "/." + dotEscape(name());
  TQString uidCacheFile = part1 + ".uidcache";
  // This is the account folder of an account that was just removed
  // When this happens, be sure to delete all traces of the cache
  if( TQFile::exists(uidCacheFile) )
    unlink( TQFile::encodeName( uidCacheFile ) );

  FolderStorage::remove();
}

TQString KMFolderCachedImap::uidCacheLocation() const
{
  TQString sLocation(folder()->path());
  if (!sLocation.isEmpty()) sLocation += '/';
  return sLocation + '.' + dotEscape(fileName()) + ".uidcache";
}

int KMFolderCachedImap::readUidCache()
{
  TQFile uidcache( uidCacheLocation() );
  if( uidcache.open( IO_ReadOnly ) ) {
    char buf[1024];
    int len = uidcache.readLine( buf, sizeof(buf) );
    if( len > 0 ) {
      int cacheVersion;
      sscanf( buf, "# KMail-UidCache V%d\n",  &cacheVersion );
      if( cacheVersion == UIDCACHE_VERSION ) {
        len = uidcache.readLine( buf, sizeof(buf) );
        if( len > 0 ) {
          setUidValidity( TQString(TQString::fromLocal8Bit(buf)).stripWhiteSpace() );
          len = uidcache.readLine( buf, sizeof(buf) );
          if( len > 0 ) {
#if MAIL_LOSS_DEBUGGING
            kdDebug(5006) << "Reading in last uid from cache: " << TQString::fromLocal8Bit(buf).stripWhiteSpace() << " in " << folder()->prettyURL() << endl;
#endif
            // load the last known highest uid from the on disk cache
            setLastUid( TQString(TQString::fromLocal8Bit(buf)).stripWhiteSpace().toULong() );
            return 0;
          }
        }
      }
    }
  }
  return -1;
}

int KMFolderCachedImap::writeUidCache()
{
  if( uidValidity().isEmpty() || uidValidity() == "INVALID" ) {
    // No info from the server yet, remove the file.
    if( TQFile::exists( uidCacheLocation() ) )
      return unlink( TQFile::encodeName( uidCacheLocation() ) );
    return 0;
  }
#if MAIL_LOSS_DEBUGGING
  kdDebug(5006) << "Writing out UID cache lastuid: " << lastUid()  << " in: " << folder()->prettyURL() << endl;
#endif
  TQFile uidcache( uidCacheLocation() );
  if( uidcache.open( IO_WriteOnly ) ) {
    TQTextStream str( &uidcache );
    str << "# KMail-UidCache V" << UIDCACHE_VERSION << endl;
    str << uidValidity() << endl;
    str << lastUid() << endl;
    uidcache.flush();
    if ( uidcache.status() == IO_Ok ) {
      // fsync( uidcache.handle() ); /* this is probably overkill */
      uidcache.close();
      if ( uidcache.status() == IO_Ok )
        return 0;
    }
  }
  KMessageBox::error( 0,
        i18n( "The UID cache file for folder %1 could not be written. There "
              "could be a problem with file system permission." ).arg( folder()->prettyURL() ) );

  return -1;
}

void KMFolderCachedImap::reloadUidMap()
{
  //kdDebug(5006) << "Reloading Uid Map " << endl;
  uidMap.clear();
  open("reloadUdi");
  for( int i = 0; i < count(); ++i ) {
    KMMsgBase *msg = getMsgBase( i );
    if( !msg ) continue;
    ulong uid = msg->UID();
    //kdDebug(5006) << "Inserting: " << i << " with uid: " << uid << endl;
    uidMap.insert( uid, i );
  }
  close("reloadUdi");
  uidMapDirty = false;
}

KMMessage* KMFolderCachedImap::take(int idx)
{
  uidMapDirty = true;
  rememberDeletion( idx );
  return KMFolderMaildir::take(idx);
}

void KMFolderCachedImap::takeTemporarily( int idx )
{
  KMFolderMaildir::take( idx );
}

// Add a message without clearing it's X-UID field.
int KMFolderCachedImap::addMsgInternal( KMMessage* msg, bool newMail,
                                        int* index_return )
{
  // Possible optimization: Only dirty if not filtered below
  ulong uid = msg->UID();
  if( uid != 0 ) {
    uidMapDirty = true;
  }

  KMFolderOpener openThis(folder(), "KMFolderCachedImap::addMsgInternal");
  int rc = openThis.openResult();
  if ( rc ) {
    kdDebug(5006) << k_funcinfo << "open: " << rc << " of folder: " << label() << endl;
    return rc;
  }

  // Add the message
  rc = KMFolderMaildir::addMsg(msg, index_return);

  if( newMail && ( imapPath() == "/INBOX/" ||
      ( ( mUserRights != ACLJobs::Ok || userRights() & ACLJobs::Administer)
      && (contentsType() == ContentsTypeMail || GlobalSettings::self()->filterGroupwareFolders()) ) ) )
  {
    // This is a new message. Filter it - maybe
    bool filter = false;
    if ( GlobalSettings::filterSourceFolders().isEmpty() ) {
      if ( imapPath() == "/INBOX/" )
        filter = true;
    } else {
      if ( GlobalSettings::filterSourceFolders().contains( folder()->id() ) )
        filter = true;
    }
    if ( filter )
      mAccount->processNewMsg( msg );
  }

  return rc;
}

/* Reimplemented from KMFolderMaildir */
int KMFolderCachedImap::addMsg(KMMessage* msg, int* index_return)
{
  if ( !canAddMsgNow( msg, index_return ) ) return 0;
  // Add it to storage
  int rc = KMFolderMaildir::addMsgInternal(msg, index_return, true /*stripUID*/);
  return rc;
}

void KMFolderCachedImap::rememberDeletion( int idx )
{
  KMMsgBase *msg = getMsgBase( idx );
  assert(msg);
  long uid = msg->UID();
  assert(uid>=0);
  mDeletedUIDsSinceLastSync.insert(uid, 0);
  kdDebug(5006) << "Explicit delete of UID " << uid << " at index: " << idx << " in " << folder()->prettyURL() << endl;
}

/* Reimplemented from KMFolderMaildir */
void KMFolderCachedImap::removeMsg(int idx, bool imapQuiet)
{
  if ( contentsType() != ContentsTypeMail ) {
    kdDebug(5006) << k_funcinfo << "Deleting message with idx " << idx << " in folder " << label() << endl;
  }
  uidMapDirty = true;
  rememberDeletion( idx );
  // Remove it from disk
  KMFolderMaildir::removeMsg(idx,imapQuiet);
}

bool KMFolderCachedImap::canRemoveFolder() const {
  // If this has subfolders it can't be removed
  if( folder() && folder()->child() && folder()->child()->count() > 0 )
    return false;

#if 0
  // No special condition here, so let base class decide
  return KMFolderMaildir::canRemoveFolder();
#endif
  return true;
}

/* Reimplemented from KMFolderDir */
int KMFolderCachedImap::rename( const TQString& aName,
                                KMFolderDir* /*aParent*/ )
{
  if ( account() == 0 || imapPath().isEmpty() ) {
    // This can happen when creating a folder and then renaming it without syncing before,
    // see https://issues.kolab.org/issue3658
    TQString err = i18n("You must synchronize with the server before renaming IMAP folders.");
    KMessageBox::error( 0, err );
    return -1;
  }

  TQString oldName = mAccount->renamedFolder( imapPath() );
  if ( oldName.isEmpty() ) oldName = name();
  if ( aName == oldName )
    // Stupid user trying to rename it to it's old name :)
    return 0;

  // Make the change appear to the user with setLabel, but we'll do the change
  // on the server during the next sync. The name() is the name at the time of
  // the last sync. Only rename if the new one is different. If it's the same,
  // don't rename, but also make sure the rename is reset, in the case of
  // A -> B -> A renames.
  if ( name() != aName )
    mAccount->addRenamedFolder( imapPath(), folder()->label(), aName );
  else
    mAccount->removeRenamedFolder( imapPath() );

  folder()->setLabel( aName );
  emit nameChanged(); // for kmailicalifaceimpl

  return 0;
}

KMFolder* KMFolderCachedImap::trashFolder() const
{
  TQString trashStr = account()->trash();
  return kmkernel->dimapFolderMgr()->findIdString( trashStr );
}

void KMFolderCachedImap::setLastUid( ulong uid )
{
#if MAIL_LOSS_DEBUGGING
  kdDebug(5006) << "Setting mLastUid to: " << uid  <<  " in " << folder()->prettyURL() << endl;
#endif
  mLastUid = uid;
  if( uidWriteTimer == -1 )
    // Write in one minute
    uidWriteTimer = startTimer( 60000 );
}

void KMFolderCachedImap::timerEvent( TQTimerEvent* )
{
  killTimer( uidWriteTimer );
  uidWriteTimer = -1;
  if ( writeUidCache() == -1 )
    unlink( TQFile::encodeName( uidCacheLocation() ) );
}

ulong KMFolderCachedImap::lastUid()
{
  return mLastUid;
}

KMMsgBase* KMFolderCachedImap::findByUID( ulong uid )
{
  bool mapReloaded = false;
  if( uidMapDirty ) {
    reloadUidMap();
    mapReloaded = true;
  }

  TQMap<ulong,int>::Iterator it = uidMap.find( uid );
  if( it != uidMap.end() ) {
    KMMsgBase *msg = getMsgBase( *it );
#if MAIL_LOSS_DEBUGGING
    kdDebug(5006) << "Folder: " << folder()->prettyURL() << endl;
    kdDebug(5006) << "UID " << uid << " is supposed to be in the map" << endl;
    kdDebug(5006) << "UID's index is to be " << *it << endl;
    kdDebug(5006) << "There is a message there? " << (msg != 0) << endl;
    if ( msg ) {
      kdDebug(5006) << "Its UID is: " << msg->UID() << endl;
    }
#endif

    if( msg && msg->UID() == uid )
      return msg;
    kdDebug(5006) << "########## Didn't find uid: " << uid << "in cache athough it's supposed to be there!" << endl;
  } else {
#if MAIL_LOSS_DEBUGGING
    kdDebug(5006) << "Didn't find uid: " << uid << "in cache!" << endl;
#endif
  }
  // Not found by now
 // if( mapReloaded )
    // Not here then
    return 0;
  // There could be a problem in the maps. Rebuild them and try again
  reloadUidMap();
  it = uidMap.find( uid );
  if( it != uidMap.end() )
    // Since the uid map is just rebuilt, no need for the sanity check
    return getMsgBase( *it );
#if MAIL_LOSS_DEBUGGING
  else
    kdDebug(5006) << "Reloaded, but stil didn't find uid: " << uid << endl;
#endif
  // Then it's not here
  return 0;
}

// This finds and sets the proper account for this folder if it has
// not been done
KMAcctCachedImap *KMFolderCachedImap::account() const
{
  if( (KMAcctCachedImap *)mAccount == 0 && kmkernel && kmkernel->acctMgr() ) {
    // Find the account
    mAccount = static_cast<KMAcctCachedImap *>( kmkernel->acctMgr()->findByName( name() ) );
  }

  return mAccount;
}

void KMFolderCachedImap::slotTroubleshoot()
{
  const int rc = DImapTroubleShootDialog::run();

  if( rc == DImapTroubleShootDialog::RefreshCache ) {
    // Refresh cache
    if( !account() ) {
      KMessageBox::sorry( 0, i18n("No account setup for this folder.\n"
                                  "Please try running a sync before this.") );
      return;
    }
    TQString str = i18n("Are you sure you want to refresh the IMAP cache of "
                       "the folder %1 and all its subfolders?\nThis will "
                       "remove all changes you have done locally to your "
                       "folders.").arg( label() );
    TQString s1 = i18n("Refresh IMAP Cache");
    TQString s2 = i18n("&Refresh");
    if( KMessageBox::warningContinueCancel( 0, str, s1, s2 ) ==
        KMessageBox::Continue )
      account()->invalidateIMAPFolders( this );
  } else {
    // Rebuild index file
    switch ( rc ) {
      case DImapTroubleShootDialog::ReindexAll:
      {
        KMFolderCachedImap *rootStorage = dynamic_cast<KMFolderCachedImap*>( account()->rootFolder() );
        if ( rootStorage )
          rootStorage->createIndexFromContentsRecursive();
        break;
      }
      case DImapTroubleShootDialog::ReindexCurrent:
        createIndexFromContents();
        break;
      case DImapTroubleShootDialog::ReindexRecursive:
        createIndexFromContentsRecursive();
        break;
      default:
        return;
    }
    KMessageBox::information( 0, i18n( "The index of this folder has been "
                                       "recreated." ) );
    writeIndex();
    kmkernel->getKMMainWidget()->folderSelected();
  }
}

void KMFolderCachedImap::serverSync( bool recurse, bool quotaOnly )
{
  if( mSyncState != SYNC_STATE_INITIAL ) {
    if( KMessageBox::warningYesNo( 0, i18n("Folder %1 is not in initial sync state (state was %2). Do you want to reset it to initial sync state and sync anyway?" ).arg( imapPath() ).arg( mSyncState ), TQString(), i18n("Reset && Sync"), KStdGuiItem::cancel() ) == KMessageBox::Yes ) {
      mSyncState = SYNC_STATE_INITIAL;
    } else return;
  }

  mRecurse = recurse;
  mQuotaOnly = quotaOnly;
  assert( account() );

  ProgressItem *progressItem = mAccount->mailCheckProgressItem();
  if ( progressItem ) {
    progressItem->reset();
    progressItem->setTotalItems( 100 );
  }
  mProgress = 0;

#if 0
  if( mHoldSyncs ) {
    // All done for this folder.
    account()->mailCheckProgressItem()->setProgress( 100 );
    mProgress = 100; // all done
    newState( mProgress, i18n("Synchronization skipped"));
    mSyncState = SYNC_STATE_INITIAL;
    emit folderComplete( this, true );
    return;
  }
#endif
  mTentativeHighestUid = 0; // reset, last sync could have been canceled

  serverSyncInternal();
}

TQString KMFolderCachedImap::state2String( int state ) const
{
  switch( state ) {
  case SYNC_STATE_INITIAL:             return "SYNC_STATE_INITIAL";
  case SYNC_STATE_GET_USERRIGHTS:      return "SYNC_STATE_GET_USERRIGHTS";
  case SYNC_STATE_PUT_MESSAGES:        return "SYNC_STATE_PUT_MESSAGES";
  case SYNC_STATE_UPLOAD_FLAGS:        return "SYNC_STATE_UPLOAD_FLAGS";
  case SYNC_STATE_CREATE_SUBFOLDERS:   return "SYNC_STATE_CREATE_SUBFOLDERS";
  case SYNC_STATE_LIST_SUBFOLDERS:     return "SYNC_STATE_LIST_SUBFOLDERS";
  case SYNC_STATE_LIST_NAMESPACES:     return "SYNC_STATE_LIST_NAMESPACES";
  case SYNC_STATE_LIST_SUBFOLDERS2:    return "SYNC_STATE_LIST_SUBFOLDERS2";
  case SYNC_STATE_DELETE_SUBFOLDERS:   return "SYNC_STATE_DELETE_SUBFOLDERS";
  case SYNC_STATE_LIST_MESSAGES:       return "SYNC_STATE_LIST_MESSAGES";
  case SYNC_STATE_DELETE_MESSAGES:     return "SYNC_STATE_DELETE_MESSAGES";
  case SYNC_STATE_GET_MESSAGES:        return "SYNC_STATE_GET_MESSAGES";
  case SYNC_STATE_EXPUNGE_MESSAGES:    return "SYNC_STATE_EXPUNGE_MESSAGES";
  case SYNC_STATE_HANDLE_INBOX:        return "SYNC_STATE_HANDLE_INBOX";
  case SYNC_STATE_TEST_ANNOTATIONS:    return "SYNC_STATE_TEST_ANNOTATIONS";
  case SYNC_STATE_GET_ANNOTATIONS:     return "SYNC_STATE_GET_ANNOTATIONS";
  case SYNC_STATE_SET_ANNOTATIONS:     return "SYNC_STATE_SET_ANNOTATIONS";
  case SYNC_STATE_GET_ACLS:            return "SYNC_STATE_GET_ACLS";
  case SYNC_STATE_SET_ACLS:            return "SYNC_STATE_SET_ACLS";
  case SYNC_STATE_GET_QUOTA:           return "SYNC_STATE_GET_QUOTA";
  case SYNC_STATE_FIND_SUBFOLDERS:     return "SYNC_STATE_FIND_SUBFOLDERS";
  case SYNC_STATE_SYNC_SUBFOLDERS:     return "SYNC_STATE_SYNC_SUBFOLDERS";
  case SYNC_STATE_RENAME_FOLDER:       return "SYNC_STATE_RENAME_FOLDER";
  case SYNC_STATE_CHECK_UIDVALIDITY:   return "SYNC_STATE_CHECK_UIDVALIDITY";
  case SYNC_STATE_CLOSE:               return "SYNC_STATE_CLOSE";
  case SYNC_STATE_GET_SUBFOLDER_QUOTA: return "SYNC_STATE_GET_SUBFOLDER_QUOTA";
  default:                             return "Unknown state";
  }
}

/*
  Progress calculation: each step is assigned a span. Initially the total is 100.
  But if we skip a step, don't increase the progress.
  This leaves more room for the step a with variable size (get_messages)
   connecting 5
   getuserrights 5
   rename 5
   check_uidvalidity 5
   create_subfolders 5
   put_messages 10 (but it can take a very long time, with many messages....)
   upload_flags 5
   list_subfolders 5
   list_subfolders2 0 (all local)
   delete_subfolders 5
   list_messages 10
   delete_messages 10
   expunge_messages 5
   get_messages variable (remaining-5) i.e. minimum 15.
   check_annotations 0 (rare)
   set_annotations 0 (rare)
   get_annotations 2
   set_acls 0 (rare)
   get_acls 3

  noContent folders have only a few of the above steps
  (permissions, and all subfolder stuff), so its steps should be given more span

 */

// While the server synchronization is running, mSyncState will hold
// the state that should be executed next
void KMFolderCachedImap::serverSyncInternal()
{
  // This is used to stop processing when we're about to exit
  // and the current job wasn't cancellable.
  // For user-requested abort, we'll use signalAbortRequested instead.
  if( kmkernel->mailCheckAborted() ) {
    resetSyncState();
    emit folderComplete( this, false );
    return;
  }

  //kdDebug(5006) << label() << ": " << state2String( mSyncState ) << endl;
  switch( mSyncState ) {
  case SYNC_STATE_INITIAL:
  {
    mProgress = 0;
    foldersForDeletionOnServer.clear();
    newState( mProgress, i18n("Synchronizing"));

    open("cachedimap");
    if ( !noContent() )
        mAccount->addLastUnreadMsgCount( this, countUnread() );

    // Connect to the server (i.e. prepare the slave)
    ImapAccountBase::ConnectionState cs = mAccount->makeConnection();
    if ( cs == ImapAccountBase::Error ) {
      // Cancelled by user, or slave can't start
      // kdDebug(5006) << "makeConnection said Error, aborting." << endl;
      // We stop here. We're already in SYNC_STATE_INITIAL for the next time.
      newState( mProgress, i18n( "Error connecting to server %1" ).arg( mAccount->host() ) );
      close("cachedimap");
      emit folderComplete(this, false);
      break;
    } else if ( cs == ImapAccountBase::Connecting ) {
      mAccount->setAnnotationCheckPassed( false );
      // kdDebug(5006) << "makeConnection said Connecting, waiting for signal." << endl;
      newState( mProgress, i18n("Connecting to %1").arg( mAccount->host() ) );
      // We'll wait for the connectionResult signal from the account.
      connect( mAccount, TQT_SIGNAL( connectionResult(int, const TQString&) ),
               this, TQT_SLOT( slotConnectionResult(int, const TQString&) ) );
      break;
    } else {
      // Connected
      // kdDebug(5006) << "makeConnection said Connected, proceeding." << endl;
      mSyncState = SYNC_STATE_GET_USERRIGHTS;
      // Fall through to next state
    }
  }


  case SYNC_STATE_GET_USERRIGHTS:

    // Now we have started the sync, emit changed() so that the folder tree can update the status
    emit syncStateChanged();
    //kdDebug(5006) << "===== Syncing " << ( mImapPath.isEmpty() ? label() : mImapPath ) << endl;

    mSyncState = SYNC_STATE_RENAME_FOLDER;

    if( !mQuotaOnly && !noContent() && mAccount->hasACLSupport() ) {
      // Check the user's own rights. We do this every time in case they changed.
      mOldUserRights = mUserRights;
      newState( mProgress, i18n("Checking permissions"));
      connect( mAccount, TQT_SIGNAL( receivedUserRights( KMFolder* ) ),
               this, TQT_SLOT( slotReceivedUserRights( KMFolder* ) ) );
      mAccount->getUserRights( folder(), imapPath() ); // after connecting, due to the INBOX case
      break;
    }

    else if ( !mQuotaOnly && noContent() && mAccount->hasACLSupport() ) {
      // This is a no content folder. The server would simply say that mailbox does not exist when
      // querying the rights for it. So pretend we have no rights.
      mUserRights = 0;
      mUserRightsState = KMail::ACLJobs::Ok;
      writeConfigKeysWhichShouldNotGetOverwrittenByReadConfig();
    }

  case SYNC_STATE_RENAME_FOLDER:
  {
    mSyncState = SYNC_STATE_CHECK_UIDVALIDITY;
    // Returns the new name if the folder was renamed, empty otherwise.
    bool isResourceFolder = kmkernel->iCalIface().isStandardResourceFolder( folder() );
    TQString newName = mAccount->renamedFolder( imapPath() );
    if ( !newName.isEmpty() && !folder()->isSystemFolder() && !isResourceFolder ) {
      newState( mProgress, i18n("Renaming folder") );
      CachedImapJob *job = new CachedImapJob( newName, CachedImapJob::tRenameFolder, this );
      connect( job, TQT_SIGNAL( result(KMail::FolderJob *) ), this, TQT_SLOT( slotIncreaseProgress() ) );
      connect( job, TQT_SIGNAL( finished() ), this, TQT_SLOT( slotRenameFolderFinished() ) );
      job->start();
      break;
    }
  }

  case SYNC_STATE_CHECK_UIDVALIDITY:
    mSyncState = SYNC_STATE_CREATE_SUBFOLDERS;
    if( !mQuotaOnly && !noContent() ) {
      checkUidValidity();
      break;
    }
    // Else carry on

  case SYNC_STATE_CREATE_SUBFOLDERS:
    mSyncState = SYNC_STATE_PUT_MESSAGES;
    if ( !mQuotaOnly ) {
      createNewFolders();
      break;
    }

  case SYNC_STATE_PUT_MESSAGES:
    mSyncState = SYNC_STATE_UPLOAD_FLAGS;
    if( !mQuotaOnly && !noContent() ) {
      uploadNewMessages();
      break;
    }
    // Else carry on
  case SYNC_STATE_UPLOAD_FLAGS:
    mSyncState = SYNC_STATE_LIST_NAMESPACES;
    if( !mQuotaOnly && !noContent() ) {
       // We haven't downloaded messages yet, so we need to build the map.
       if( uidMapDirty )
         reloadUidMap();
       // Upload flags, unless we know from the ACL that we're not allowed
       // to do that or they did not change locally
       if ( mUserRightsState != KMail::ACLJobs::Ok ||
            ( mUserRights & (KMail::ACLJobs::WriteFlags ) ) ) {
         if ( !mUIDsOfLocallyChangedStatuses.empty() || mStatusChangedLocally ) {
           uploadFlags();
           break;
         } else {
           //kdDebug(5006) << "Skipping flags upload, folder unchanged: " << label() << endl;
         }
       } else if ( mUserRights & KMail::ACLJobs::WriteSeenFlag ) {
         if ( !mUIDsOfLocallyChangedStatuses.empty() || mStatusChangedLocally ) {
           uploadSeenFlags();
           break;
         }
       }
    }
    // Else carry on

  case SYNC_STATE_LIST_NAMESPACES:
    if ( !mQuotaOnly && this == mAccount->rootFolder() ) {
      listNamespaces();
      break;
    }
    mSyncState = SYNC_STATE_LIST_SUBFOLDERS;
    // Else carry on

  case SYNC_STATE_LIST_SUBFOLDERS:
    newState( mProgress, i18n("Retrieving folderlist"));
    mSyncState = SYNC_STATE_LIST_SUBFOLDERS2;
    if ( !mQuotaOnly ) {
      if( !listDirectory() ) {
        mSyncState = SYNC_STATE_INITIAL;
        KMessageBox::error(0, i18n("Error while retrieving the folderlist"));
      }
      break;
    }

  case SYNC_STATE_LIST_SUBFOLDERS2:
    mSyncState = SYNC_STATE_DELETE_SUBFOLDERS;
    mProgress += 10;
    if ( !mQuotaOnly ) {
      newState( mProgress, i18n("Retrieving subfolders"));
      listDirectory2();
      break;
    }

  case SYNC_STATE_DELETE_SUBFOLDERS:
    mSyncState = SYNC_STATE_LIST_MESSAGES;
    if( !mQuotaOnly && !foldersForDeletionOnServer.isEmpty() ) {
      newState( mProgress, i18n("Deleting folders from server"));
      CachedImapJob* job = new CachedImapJob( foldersForDeletionOnServer,
                                                  CachedImapJob::tDeleteFolders, this );
      connect( job, TQT_SIGNAL( result(KMail::FolderJob *) ), this, TQT_SLOT( slotIncreaseProgress() ) );
      connect( job, TQT_SIGNAL( finished() ), this, TQT_SLOT( slotFolderDeletionOnServerFinished() ) );
      job->start();
      break;
    }
    // Not needed, the next step emits newState very quick
    //newState( mProgress, i18n("No folders to delete from server"));
      // Carry on

  case SYNC_STATE_LIST_MESSAGES:
    mSyncState = SYNC_STATE_DELETE_MESSAGES;
    if( !mQuotaOnly && !noContent() ) {
      newState( mProgress, i18n("Retrieving message list"));
      listMessages();
      break;
    }
    // Else carry on

  case SYNC_STATE_DELETE_MESSAGES:
    mSyncState = SYNC_STATE_EXPUNGE_MESSAGES;
    if( !mQuotaOnly && !noContent() ) {
      if( deleteMessages() ) {
        // Fine, we will continue with the next state
      } else {
        // No messages to delete, skip to GET_MESSAGES
        newState( mProgress, i18n("No messages to delete..."));
        mSyncState = SYNC_STATE_GET_MESSAGES;
        serverSyncInternal();
      }
      break;
    }
    // Else carry on

  case SYNC_STATE_EXPUNGE_MESSAGES:
    mSyncState = SYNC_STATE_GET_MESSAGES;
    if( !mQuotaOnly && !noContent() ) {
      newState( mProgress, i18n("Expunging deleted messages"));
      CachedImapJob *job = new CachedImapJob( TQString(),
                                              CachedImapJob::tExpungeFolder, this );
      connect( job, TQT_SIGNAL( result(KMail::FolderJob *) ), this, TQT_SLOT( slotIncreaseProgress() ) );
      connect( job, TQT_SIGNAL( finished() ), this, TQT_SLOT( serverSyncInternal() ) );
      job->start();
      break;
    }
    // Else carry on

  case SYNC_STATE_GET_MESSAGES:
    mSyncState = SYNC_STATE_HANDLE_INBOX;
    if( !mQuotaOnly && !noContent() ) {
      if( !mMsgsForDownload.isEmpty() ) {
        newState( mProgress, i18n("Retrieving one new message","Retrieving %n new messages",mMsgsForDownload.size()));
        CachedImapJob *job = new CachedImapJob( mMsgsForDownload,
                                                CachedImapJob::tGetMessage,
                                                this );
        connect( job, TQT_SIGNAL( progress(unsigned long, unsigned long) ),
                 this, TQT_SLOT( slotProgress(unsigned long, unsigned long) ) );
        connect( job, TQT_SIGNAL( finished() ), this, TQT_SLOT( slotUpdateLastUid() ) );
        connect( job, TQT_SIGNAL( finished() ), this, TQT_SLOT( serverSyncInternal() ) );
        job->start();
        mMsgsForDownload.clear();
        break;
      } else {
        newState( mProgress, i18n("No new messages from server"));
        /* There were no messages to download, but it could be that we uploaded some
           which we didn't need to download again because we already knew the uid.
           Now that we are sure there is nothing to download, and everything that had
           to be deleted on the server has been deleted, adjust our local notion of the
           highes uid seen thus far. */
        slotUpdateLastUid();
        if( mLastUid == 0 && uidWriteTimer == -1 ) {
          // This is probably a new and empty folder. Write the UID cache
          if ( writeUidCache() == -1 ) {
            resetSyncState();
            emit folderComplete( this, false );
            return;
          }
        }
      }
    }

    // Else carry on

  case SYNC_STATE_HANDLE_INBOX:
    // Wrap up the 'download emails' stage. We always end up at 95 here.
    mProgress = 95;
    mSyncState = SYNC_STATE_TEST_ANNOTATIONS;

  #define KOLAB_FOLDERTEST "/vendor/kolab/folder-test"
  case SYNC_STATE_TEST_ANNOTATIONS:
    mSyncState = SYNC_STATE_GET_ANNOTATIONS;
    // The first folder with user rights to write annotations
    if( !mQuotaOnly && !mAccount->annotationCheckPassed() &&
         ( mUserRightsState != KMail::ACLJobs::Ok || ( mUserRights & ACLJobs::Administer ) )
         && !imapPath().isEmpty() && imapPath() != "/" ) {
      kdDebug(5006) << "Setting test attribute on folder: "<< folder()->prettyURL() << endl;
      newState( mProgress, i18n("Checking annotation support"));

      KURL url = mAccount->getUrl();
      url.setPath( imapPath() );
      KMail::AnnotationList annotations; // to be set

      KMail::AnnotationAttribute attr( KOLAB_FOLDERTEST, "value.shared", "true" );
      annotations.append( attr );

      kdDebug(5006) << "Setting test attribute to "<< url << endl;
      TDEIO::Job* job = AnnotationJobs::multiSetAnnotation( mAccount->slave(),
          url, annotations );
      ImapAccountBase::jobData jd( url.url(), folder() );
      jd.cancellable = true; // we can always do so later
      mAccount->insertJob(job, jd);
       connect(job, TQT_SIGNAL(result(TDEIO::Job *)),
              TQT_SLOT(slotTestAnnotationResult(TDEIO::Job *)));
      break;
    }

  case SYNC_STATE_GET_ANNOTATIONS: {
#define KOLAB_FOLDERTYPE "/vendor/kolab/folder-type"
#define KOLAB_INCIDENCESFOR "/vendor/kolab/incidences-for"
#define KOLAB_SHAREDSEEN "/vendor/cmu/cyrus-imapd/sharedseen"
//#define KOLAB_FOLDERTYPE "/comment"  //for testing, while cyrus-imap doesn't support /vendor/*
    mSyncState = SYNC_STATE_SET_ANNOTATIONS;

    bool needToGetInitialAnnotations = false;
    if ( !mQuotaOnly && !noContent() ) {
      // for a folder we didn't create ourselves: get annotation from server
      if ( mAnnotationFolderType == "FROMSERVER" ) {
        needToGetInitialAnnotations = true;
        mAnnotationFolderType = TQString();
      } else {
        updateAnnotationFolderType();
      }
    }

    // First retrieve the annotation, so that we know we have to set it if it's not set.
    // On the other hand, if the user changed the contentstype, there's no need to get first.
    if ( !mQuotaOnly && !noContent() && mAccount->hasAnnotationSupport() &&
        ( kmkernel->iCalIface().isEnabled() || needToGetInitialAnnotations ) ) {
      TQStringList annotations; // list of annotations to be fetched
      if ( !mAnnotationFolderTypeChanged || mAnnotationFolderType.isEmpty() )
        annotations << KOLAB_FOLDERTYPE;
      if ( !mIncidencesForChanged )
        annotations << KOLAB_INCIDENCESFOR;
      if ( !mSharedSeenFlagsChanged )
        annotations << KOLAB_SHAREDSEEN;
      if ( !annotations.isEmpty() ) {
        newState( mProgress, i18n("Retrieving annotations"));
        KURL url = mAccount->getUrl();
        url.setPath( imapPath() );
        AnnotationJobs::MultiGetAnnotationJob* job =
          AnnotationJobs::multiGetAnnotation( mAccount->slave(), url, annotations );
        ImapAccountBase::jobData jd( url.url(), folder() );
        jd.cancellable = true;
        mAccount->insertJob(job, jd);

        connect( job, TQT_SIGNAL(annotationResult(const TQString&, const TQString&, bool)),
                 TQT_SLOT(slotAnnotationResult(const TQString&, const TQString&, bool)) );
        connect( job, TQT_SIGNAL(result(TDEIO::Job *)),
                 TQT_SLOT(slotGetAnnotationResult(TDEIO::Job *)) );
        break;
      }
    }
  } // case
  case SYNC_STATE_SET_ANNOTATIONS:

    mSyncState = SYNC_STATE_SET_ACLS;
    if ( !mQuotaOnly && !noContent() && mAccount->hasAnnotationSupport() &&
         ( mUserRightsState != KMail::ACLJobs::Ok || ( mUserRights & ACLJobs::Administer ) ) ) {
      newState( mProgress, i18n("Setting annotations"));
      KURL url = mAccount->getUrl();
      url.setPath( imapPath() );
      KMail::AnnotationList annotations; // to be set
      if ( mAnnotationFolderTypeChanged && !mAnnotationFolderType.isEmpty() ) {
        KMail::AnnotationAttribute attr( KOLAB_FOLDERTYPE, "value.shared", mAnnotationFolderType );
        annotations.append( attr );
        kdDebug(5006) << "Setting folder-type annotation for " << label() << " to " << mAnnotationFolderType << endl;
      }
      if ( mIncidencesForChanged ) {
        const TQString val = incidencesForToString( mIncidencesFor );
        KMail::AnnotationAttribute attr( KOLAB_INCIDENCESFOR, "value.shared", val );
        annotations.append( attr );
        kdDebug(5006) << "Setting incidences-for annotation for " << label() << " to " << val << endl;
      }
      if ( mSharedSeenFlagsChanged ) {
        const TQString val = mSharedSeenFlags ? "true" : "false";
        KMail::AnnotationAttribute attr( KOLAB_SHAREDSEEN, "value.shared", val );
        annotations.append( attr );
        kdDebug(5006) << k_funcinfo << "Setting sharedseen annotation for " << label() << " to " << val << endl;
      }
      if ( !annotations.isEmpty() ) {
        TDEIO::Job* job =
          AnnotationJobs::multiSetAnnotation( mAccount->slave(), url, annotations );
        ImapAccountBase::jobData jd( url.url(), folder() );
        jd.cancellable = true; // we can always do so later
        mAccount->insertJob(job, jd);

        connect(job, TQT_SIGNAL(annotationChanged( const TQString&, const TQString&, const TQString& ) ),
                TQT_SLOT( slotAnnotationChanged( const TQString&, const TQString&, const TQString& ) ));
        connect(job, TQT_SIGNAL(result(TDEIO::Job *)),
                TQT_SLOT(slotSetAnnotationResult(TDEIO::Job *)));
        break;
      }
    }

  case SYNC_STATE_SET_ACLS:
    mSyncState = SYNC_STATE_GET_ACLS;

    if( !mQuotaOnly && !noContent() && mAccount->hasACLSupport() &&
      ( mUserRightsState != KMail::ACLJobs::Ok || ( mUserRights & ACLJobs::Administer ) ) ) {
      bool hasChangedACLs = false;
      ACLList::ConstIterator it = mACLList.begin();
      for ( ; it != mACLList.end() && !hasChangedACLs; ++it ) {
        hasChangedACLs = (*it).changed;
      }
      if ( hasChangedACLs ) {
        newState( mProgress, i18n("Setting permissions"));
        KURL url = mAccount->getUrl();
        url.setPath( imapPath() );
        TDEIO::Job* job = KMail::ACLJobs::multiSetACL( mAccount->slave(), url, mACLList );
        ImapAccountBase::jobData jd( url.url(), folder() );
        mAccount->insertJob(job, jd);

        connect(job, TQT_SIGNAL(result(TDEIO::Job *)),
                TQT_SLOT(slotMultiSetACLResult(TDEIO::Job *)));
        connect(job, TQT_SIGNAL(aclChanged( const TQString&, int )),
                TQT_SLOT(slotACLChanged( const TQString&, int )) );
        break;
      }
    }

  case SYNC_STATE_GET_ACLS:
    mSyncState = SYNC_STATE_FIND_SUBFOLDERS;

    if( !mQuotaOnly && !noContent() && mAccount->hasACLSupport() ) {
      newState( mProgress, i18n( "Retrieving permissions" ) );
      mAccount->getACL( folder(), mImapPath );
      connect( mAccount, TQT_SIGNAL(receivedACL( KMFolder*, TDEIO::Job*, const KMail::ACLList& )),
               this, TQT_SLOT(slotReceivedACL( KMFolder*, TDEIO::Job*, const KMail::ACLList& )) );
      break;
    }
  case SYNC_STATE_FIND_SUBFOLDERS:
    {
      mSyncState = SYNC_STATE_SYNC_SUBFOLDERS;
      mSomeSubFolderCloseToQuotaChanged = false;
      buildSubFolderList();
    }

    // Carry on
  case SYNC_STATE_SYNC_SUBFOLDERS:
    syncNextSubFolder( false );
    break;
  case SYNC_STATE_GET_SUBFOLDER_QUOTA:

    // Sync the subfolders again, so that the quota information is updated for all. This state is
    // only entered if the close to quota property of one subfolder changed in the previous state.
    syncNextSubFolder( true );
    break;
  case SYNC_STATE_GET_QUOTA:
    mSyncState = SYNC_STATE_CLOSE;
    if( !noContent() && mAccount->hasQuotaSupport() ) {
      mProgress = 98;
      newState( mProgress, i18n("Getting quota information"));
      KURL url = mAccount->getUrl();
      url.setPath( imapPath() );
      TDEIO::Job* job = KMail::QuotaJobs::getStorageQuota( mAccount->slave(), url );
      ImapAccountBase::jobData jd( url.url(), folder() );
      mAccount->insertJob(job, jd);
      connect( job, TQT_SIGNAL( storageQuotaResult( const QuotaInfo& ) ),
          TQT_SLOT( slotStorageQuotaResult( const QuotaInfo& ) ) );
      connect( job, TQT_SIGNAL(result(TDEIO::Job *)),
          TQT_SLOT(slotQuotaResult(TDEIO::Job *)) );
      break;
    }
  case SYNC_STATE_CLOSE:
    {
      mProgress = 100; // all done
      newState( mProgress, i18n("Synchronization done"));
      KURL url = mAccount->getUrl();
      url.setPath( imapPath() );
      kmkernel->iCalIface().folderSynced( folder(), url );

      mSyncState = SYNC_STATE_INITIAL;
      mAccount->addUnreadMsgCount( this, countUnread() ); // before closing
      close( "cachedimap" );
      emit syncStateChanged();
      emit folderComplete( this, true );
    }
    break;

  default:
    kdDebug(5006) << "KMFolderCachedImap::serverSyncInternal() WARNING: no such state "
                  << mSyncState << endl;
  }
}

void KMFolderCachedImap::syncNextSubFolder( bool secondSync )
{
  if( mCurrentSubfolder ) {
    disconnectSubFolderSignals();
  }

  if( mSubfoldersForSync.isEmpty() ) {

    // Sync finished, and a close to quota property of an subfolder changed, therefore go into
    // the SYNC_STATE_GET_SUBFOLDER_QUOTA state and sync again
    if ( mSomeSubFolderCloseToQuotaChanged && mRecurse && !secondSync ) {
      buildSubFolderList();
      mSyncState = SYNC_STATE_GET_SUBFOLDER_QUOTA;
      serverSyncInternal();
    }

    else {

      // Quota checking has to come after syncing subfolder, otherwise the quota information would
      // be outdated, since the subfolders can change in size during the syncing.
      // https://issues.kolab.org/issue4066
      mSyncState = SYNC_STATE_GET_QUOTA;
      serverSyncInternal();
    }
  } else {
    mCurrentSubfolder = mSubfoldersForSync.front();
    mSubfoldersForSync.pop_front();
    if ( mCurrentSubfolder ) {
      connect( mCurrentSubfolder, TQT_SIGNAL( folderComplete(KMFolderCachedImap*, bool) ),
               this, TQT_SLOT( slotSubFolderComplete(KMFolderCachedImap*, bool) ) );
      connect( mCurrentSubfolder, TQT_SIGNAL( closeToQuotaChanged() ),
               this, TQT_SLOT( slotSubFolderCloseToQuotaChanged() ) );

      //kdDebug(5006) << "Sync'ing subfolder " << mCurrentSubfolder->imapPath() << endl;
      assert( !mCurrentSubfolder->imapPath().isEmpty() );
      mCurrentSubfolder->setAccount( account() );
      const bool recurse = mCurrentSubfolder->noChildren() ? false : true;
      const bool quotaOnly = secondSync || mQuotaOnly;
      mCurrentSubfolder->serverSync( recurse, quotaOnly );
    }
    else {
      // mCurrentSubfolder is empty, probably because it was deleted while syncing. Go on with the
      // next subfolder instead.
      syncNextSubFolder( secondSync );
    }
  }
}

void KMFolderCachedImap::buildSubFolderList()
{
  mSubfoldersForSync.clear();
  mCurrentSubfolder = 0;
  if( folder() && folder()->child() ) {
    KMFolderNode *node = folder()->child()->first();
    while( node ) {
      if( !node->isDir() ) {
        KMFolderCachedImap* storage = static_cast<KMFolderCachedImap*>(static_cast<KMFolder*>(node)->storage());
        const bool folderIsNew = mNewlyCreatedSubfolders.contains( TQGuardedPtr<KMFolderCachedImap>( storage ) );
        // Only sync folders that have been accepted by the server
        if ( !storage->imapPath().isEmpty()
             // and that were not just deleted from it
             && !foldersForDeletionOnServer.contains( storage->imapPath() ) ) {
          if ( mRecurse || folderIsNew ) {
            mSubfoldersForSync << storage;
          }
        } else {
          kdDebug(5006) << "Do not add " << storage->label()
                        << " to synclist" << endl;
        }
      }
      node = folder()->child()->next();
    }
  }

  mNewlyCreatedSubfolders.clear();
}

void KMFolderCachedImap::disconnectSubFolderSignals()
{
  disconnect( mCurrentSubfolder, TQT_SIGNAL( folderComplete(KMFolderCachedImap*, bool) ),
              this, TQT_SLOT( slotSubFolderComplete(KMFolderCachedImap*, bool) ) );
  disconnect( mCurrentSubfolder, TQT_SIGNAL( closeToQuotaChanged() ),
              this, TQT_SLOT( slotSubFolderCloseToQuotaChanged() ) );
  mCurrentSubfolder = 0;
}

/* Connected to the imap account's connectionResult signal.
   Emitted when the slave connected or failed to connect.
*/
void KMFolderCachedImap::slotConnectionResult( int errorCode, const TQString& errorMsg )
{
  disconnect( mAccount, TQT_SIGNAL( connectionResult(int, const TQString&) ),
              this, TQT_SLOT( slotConnectionResult(int, const TQString&) ) );
  if ( !errorCode ) {
    // Success
    mSyncState = SYNC_STATE_GET_USERRIGHTS;
    mProgress += 5;
    serverSyncInternal();
  } else {
    // Error (error message already shown by the account)
    newState( mProgress, TDEIO::buildErrorString( errorCode, errorMsg ));
    emit folderComplete(this, false);
  }
}

/* find new messages (messages without a UID) */
TQValueList<unsigned long> KMFolderCachedImap::findNewMessages()
{
  TQValueList<unsigned long> result;
  for( int i = 0; i < count(); ++i ) {
    KMMsgBase *msg = getMsgBase( i );
    if( !msg ) continue; /* what goes on if getMsg() returns 0? */
    if ( msg->UID() == 0 )
      result.append( msg->getMsgSerNum() );
  }
  return result;
}

/* Upload new messages to server */
void KMFolderCachedImap::uploadNewMessages()
{
  TQValueList<unsigned long> newMsgs = findNewMessages();
  if( !newMsgs.isEmpty() ) {
    if ( mUserRightsState != KMail::ACLJobs::Ok || ( mUserRights & ( KMail::ACLJobs::Insert ) ) ) {
      newState( mProgress, i18n("Uploading messages to server"));
      CachedImapJob *job = new CachedImapJob( newMsgs, CachedImapJob::tPutMessage, this );
      connect( job, TQT_SIGNAL( progress( unsigned long, unsigned long) ),
               this, TQT_SLOT( slotPutProgress(unsigned long, unsigned long) ) );
      connect( job, TQT_SIGNAL( finished() ), this, TQT_SLOT( serverSyncInternal() ) );
      job->start();
      return;
    } else {
      KMCommand *command = rescueUnsyncedMessages();
      connect( command, TQT_SIGNAL( completed( KMCommand * ) ),
               this, TQT_SLOT( serverSyncInternal() ) );
    }
  } else { // nothing to upload
    if ( mUserRights != mOldUserRights && (mOldUserRights & KMail::ACLJobs::Insert)
         && !(mUserRights & KMail::ACLJobs::Insert) ) {
      // write access revoked
      KMessageBox::information( 0, i18n("<p>Your access rights to folder <b>%1</b> have been restricted, "
          "it will no longer be possible to add messages to this folder.</p>").arg( folder()->prettyURL() ),
          i18n("Acces rights revoked"), "KMailACLRevocationNotification" );
    }
  }
  newState( mProgress, i18n("No messages to upload to server"));
  serverSyncInternal();
}

/* Progress info during uploadNewMessages */
void KMFolderCachedImap::slotPutProgress( unsigned long done, unsigned long total )
{
  // (going from mProgress to mProgress+10)
  int progressSpan = 10;
  newState( mProgress + (progressSpan * done) / total, TQString() );
  if ( done == total ) // we're done
    mProgress += progressSpan;
}

/* Upload message flags to server */
void KMFolderCachedImap::uploadFlags()
{
  if ( !uidMap.isEmpty() ) {
    mStatusFlagsJobs = 0;
    newState( mProgress, i18n("Uploading status of messages to server"));

    // FIXME DUPLICATED FROM KMFOLDERIMAP
    TQMap< TQString, TQStringList > groups;
    //open(); //already done
    for( int i = 0; i < count(); ++i ) {
      KMMsgBase* msg = getMsgBase( i );
      if( !msg || msg->UID() == 0 )
        // Either not a valid message or not one that is on the server yet
        continue;
      if ( mUIDsOfLocallyChangedStatuses.find( msg->UID() ) == mUIDsOfLocallyChangedStatuses.end()
           && !mStatusChangedLocally ) {
        // This message has not had its status changed locally
        continue;
      }

      TQString flags = KMFolderImap::statusToFlags(msg->status(), mPermanentFlags);
      // Collect uids for each typem of flags.
      TQString uid;
      uid.setNum( msg->UID() );
      groups[flags].append(uid);
    }
    TQMapIterator< TQString, TQStringList > dit;
    for( dit = groups.begin(); dit != groups.end(); ++dit ) {
      TQCString flags = dit.key().latin1();
      TQStringList sets = KMFolderImap::makeSets( (*dit), true );
      mStatusFlagsJobs += sets.count(); // ### that's not in kmfolderimap....
      // Send off a status setting job for each set.
      for( TQStringList::Iterator slit = sets.begin(); slit != sets.end(); ++slit ) {
        TQString imappath = imapPath() + ";UID=" + ( *slit );
        mAccount->setImapStatus(folder(), imappath, flags);
      }
    }
    // FIXME END DUPLICATED FROM KMFOLDERIMAP

    if ( mStatusFlagsJobs ) {
      connect( mAccount, TQT_SIGNAL( imapStatusChanged(KMFolder*, const TQString&, bool) ),
               this, TQT_SLOT( slotImapStatusChanged(KMFolder*, const TQString&, bool) ) );
      return;
    }
  }
  newState( mProgress, i18n("No messages to upload to server"));
  serverSyncInternal();
}

void KMFolderCachedImap::uploadSeenFlags()
{
  if ( !uidMap.isEmpty() ) {
    mStatusFlagsJobs = 0;
    newState( mProgress, i18n("Uploading status of messages to server"));

    TQValueList<ulong> seenUids, unseenUids;
    for( int i = 0; i < count(); ++i ) {
      KMMsgBase* msg = getMsgBase( i );
      if( !msg || msg->UID() == 0 )
        // Either not a valid message or not one that is on the server yet
        continue;

      if ( mUIDsOfLocallyChangedStatuses.find( msg->UID() ) == mUIDsOfLocallyChangedStatuses.end()
           && !mStatusChangedLocally ) {
        // This message has not had its status changed locally
        continue;
      }

      if ( msg->status() & KMMsgStatusOld || msg->status() & KMMsgStatusRead )
        seenUids.append( msg->UID() );
      else
        unseenUids.append( msg->UID() );
    }
    if ( !seenUids.isEmpty() ) {
      TQStringList sets = KMFolderImap::makeSets( seenUids, true );
      mStatusFlagsJobs += sets.count();
      for( TQStringList::Iterator it = sets.begin(); it != sets.end(); ++it ) {
        TQString imappath = imapPath() + ";UID=" + ( *it );
        mAccount->setImapSeenStatus( folder(), imappath, true );
      }
    }
    if ( !unseenUids.isEmpty() ) {
      TQStringList sets = KMFolderImap::makeSets( unseenUids, true );
      mStatusFlagsJobs += sets.count();
      for( TQStringList::Iterator it = sets.begin(); it != sets.end(); ++it ) {
        TQString imappath = imapPath() + ";UID=" + ( *it );
        mAccount->setImapSeenStatus( folder(), imappath, false );
      }
    }

    if ( mStatusFlagsJobs ) {
      connect( mAccount, TQT_SIGNAL( imapStatusChanged(KMFolder*, const TQString&, bool) ),
               this, TQT_SLOT( slotImapStatusChanged(KMFolder*, const TQString&, bool) ) );
      return;
    }
  }
  newState( mProgress, i18n("No messages to upload to server"));
  serverSyncInternal();
}

void KMFolderCachedImap::slotImapStatusChanged(KMFolder* folder, const TQString&, bool cont)
{
  if ( mSyncState == SYNC_STATE_INITIAL ){
      //kdDebug(5006) << "IMAP status changed but reset " << endl;
      return; // we were reset
  }
  //kdDebug(5006) << "IMAP status changed for folder: " << folder->prettyURL() << endl;
  if ( folder->storage() == this ) {
    --mStatusFlagsJobs;
    if ( mStatusFlagsJobs == 0 || !cont ) // done or aborting
      disconnect( mAccount, TQT_SIGNAL( imapStatusChanged(KMFolder*, const TQString&, bool) ),
                  this, TQT_SLOT( slotImapStatusChanged(KMFolder*, const TQString&, bool) ) );
    if ( mStatusFlagsJobs == 0 && cont ) {
      mProgress += 5;
      serverSyncInternal();
      //kdDebug(5006) << "Proceeding with mailcheck." << endl;
    }
  }
}

// This is not perfect, what if the status didn't really change? Oh well ...
void KMFolderCachedImap::setStatus( int idx, KMMsgStatus status, bool toggle)
{
  KMFolderMaildir::setStatus( idx, status, toggle );
  const KMMsgBase *msg = getMsgBase( idx );
  Q_ASSERT( msg );
  if ( msg )
    mUIDsOfLocallyChangedStatuses.insert( msg->UID() );
}

void KMFolderCachedImap::setStatus(TQValueList<int>& ids, KMMsgStatus status, bool toggle)
{
  KMFolderMaildir::setStatus(ids, status, toggle);
  for (TQValueList<int>::iterator it = ids.begin(); it != ids.end(); it++ ) {
    const KMMsgBase *msg = getMsgBase( *it );
    Q_ASSERT( msg );
    if ( msg )
      mUIDsOfLocallyChangedStatuses.insert( msg->UID() );
  }
}

/* Upload new folders to server */
void KMFolderCachedImap::createNewFolders()
{
  TQValueList<KMFolderCachedImap*> newFolders = findNewFolders();
  //kdDebug(5006) << label() << " createNewFolders:" << newFolders.count() << " new folders." << endl;
  if( !newFolders.isEmpty() ) {
    newState( mProgress, i18n("Creating subfolders on server"));
    CachedImapJob *job = new CachedImapJob( newFolders, CachedImapJob::tAddSubfolders, this );
    connect( job, TQT_SIGNAL( result(KMail::FolderJob *) ), this, TQT_SLOT( slotIncreaseProgress() ) );
    connect( job, TQT_SIGNAL( finished() ), this, TQT_SLOT( serverSyncInternal() ) );
    job->start();
  } else {
    serverSyncInternal();
  }
}

TQValueList<KMFolderCachedImap*> KMFolderCachedImap::findNewFolders()
{
  TQValueList<KMFolderCachedImap*> newFolders;
  if( folder() && folder()->child() ) {
    KMFolderNode *node = folder()->child()->first();
    while( node ) {
      if( !node->isDir() ) {
        if( static_cast<KMFolder*>(node)->folderType() != KMFolderTypeCachedImap ) {
          kdError(5006) << "KMFolderCachedImap::findNewFolders(): ARGH!!! "
                        << node->name() << " is not an IMAP folder\n";
          node = folder()->child()->next();
          assert(0);
        }
        KMFolderCachedImap* folder = static_cast<KMFolderCachedImap*>(static_cast<KMFolder*>(node)->storage());
        if( folder->imapPath().isEmpty() ) {
          newFolders << folder;
        }
      }
      node = folder()->child()->next();
    }
  }
  return newFolders;
}

bool KMFolderCachedImap::deleteMessages()
{
  /* Delete messages from cache that are gone from the server */
  TQPtrList<KMMsgBase> msgsForDeletion;

  // It is not possible to just go over all indices and remove
  // them one by one because the index list can get resized under
  // us. So use msg pointers instead

  TQStringList uids;
  TQMap<ulong,int>::const_iterator it = uidMap.constBegin();
  for( ; it != uidMap.end(); it++ ) {
    ulong uid ( it.key() );
    if( uid!=0 && !uidsOnServer.find( uid ) ) {
      uids << TQString::number( uid );
      msgsForDeletion.append( getMsgBase( *it ) );
    }
  }

  if( !msgsForDeletion.isEmpty() ) {
    if ( contentsType() != ContentsTypeMail ) {
      kdDebug(5006) << k_funcinfo << label() << " Going to locally delete " << msgsForDeletion.count()
                    << " messages, with the uids " << uids.join( "," ) << endl;
    }
#if MAIL_LOSS_DEBUGGING
      if ( KMessageBox::warningYesNo(
             0, i18n( "<qt><p>Mails on the server in folder <b>%1</b> were deleted. "
                 "Do you want to delete them locally?<br>UIDs: %2</p></qt>" )
             .arg( folder()->prettyURL() ).arg( uids.join(",") ) ) == KMessageBox::Yes )
#endif
        removeMsg( msgsForDeletion );
  }

  if ( mUserRightsState == KMail::ACLJobs::Ok && !( mUserRights & KMail::ACLJobs::Delete ) )
    return false;

  /* Delete messages from the server that we dont have anymore */
  if( !uidsForDeletionOnServer.isEmpty() ) {
    newState( mProgress, i18n("Deleting removed messages from server"));
    TQStringList sets = KMFolderImap::makeSets( uidsForDeletionOnServer, true );
    uidsForDeletionOnServer.clear();
    kdDebug(5006) << "Deleting " << sets.count() << " sets of messages from server folder " << imapPath() << endl;
    CachedImapJob *job = new CachedImapJob( sets, CachedImapJob::tDeleteMessage, this );
    connect( job, TQT_SIGNAL( result(KMail::FolderJob *) ),
             this, TQT_SLOT( slotDeleteMessagesResult(KMail::FolderJob *) ) );
    job->start();
    return true;
  } else {

    // Nothing to delete on the server, make sure the map is clear again.
    // Normally this wouldn't be necessary, but there can be stale maps because of
    // https://issues.kolab.org/issue3833.
    mDeletedUIDsSinceLastSync.clear();
    return false;
  }
}

void KMFolderCachedImap::slotDeleteMessagesResult( KMail::FolderJob* job )
{
  if ( job->error() ) {
    // Skip the EXPUNGE state if deleting didn't work, no need to show two error messages
    mSyncState = SYNC_STATE_GET_MESSAGES;
  } else {
    // deleting on the server went fine, clear the pending deletions cache
    mDeletedUIDsSinceLastSync.clear();
  }
  mProgress += 10;
  serverSyncInternal();
}

void KMFolderCachedImap::checkUidValidity() {
  // IMAP root folders don't seem to have a UID validity setting.
  // Also, don't try the uid validity on new folders
  if( imapPath().isEmpty() || imapPath() == "/" )
    // Just proceed
    serverSyncInternal();
  else {
    newState( mProgress, i18n("Checking folder validity"));
    CachedImapJob *job = new CachedImapJob( FolderJob::tCheckUidValidity, this );
    connect( job, TQT_SIGNAL(permanentFlags(int)), TQT_SLOT(slotPermanentFlags(int)) );
    connect( job, TQT_SIGNAL( result( KMail::FolderJob* ) ),
             this, TQT_SLOT( slotCheckUidValidityResult( KMail::FolderJob* ) ) );
    job->start();
  }
}

void KMFolderCachedImap::slotCheckUidValidityResult( KMail::FolderJob* job )
{
  if ( job->error() ) { // there was an error and the user chose "continue"
    // We can't continue doing anything in the same folder though, it would delete all mails.
    // But we can continue to subfolders if any. Well we can also try annotation/acl stuff...
    mSyncState = SYNC_STATE_HANDLE_INBOX;
  }
  mProgress += 5;
  serverSyncInternal();
}

void KMFolderCachedImap::slotPermanentFlags(int flags)
{
  mPermanentFlags = flags;
}

/* This will only list the messages in a folder.
   No directory listing done*/
void KMFolderCachedImap::listMessages() {
  bool groupwareOnly = GlobalSettings::self()->showOnlyGroupwareFoldersForGroupwareAccount()
               && GlobalSettings::self()->theIMAPResourceAccount() == (int)mAccount->id()
               && folder()->isSystemFolder()
               && mImapPath == "/INBOX/";
  // Don't list messages on the root folder, and skip the inbox, if this is
  // the inbox of a groupware-only dimap account
  if( imapPath() == "/" || groupwareOnly ) {
    serverSyncInternal();
    return;
  }

  if( !mAccount->slave() ) { // sync aborted
    resetSyncState();
    emit folderComplete( this, false );
    return;
  }
  uidsOnServer.clear();
  uidsOnServer.resize( count() * 2 );
  uidsForDeletionOnServer.clear();
  mMsgsForDownload.clear();
  mUidsForDownload.clear();
  // listing is only considered successful if saw a syntactically correct imapdigest
  mFoundAnIMAPDigest = false;

  CachedImapJob* job = new CachedImapJob( FolderJob::tListMessages, this );
  connect( job, TQT_SIGNAL( result(KMail::FolderJob *) ),
           this, TQT_SLOT( slotGetLastMessagesResult(KMail::FolderJob *) ) );
  job->start();
}

void KMFolderCachedImap::slotGetLastMessagesResult(KMail::FolderJob *job)
{
  getMessagesResult(job, true);
}

// Connected to the listMessages job in CachedImapJob
void KMFolderCachedImap::slotGetMessagesData(TDEIO::Job * job, const TQByteArray & data)
{
  KMAcctCachedImap::JobIterator it = mAccount->findJob(job);
  if ( it == mAccount->jobsEnd() ) { // Shouldn't happen
    kdDebug(5006) << "could not find job!?!?!" << endl;
    // be sure to reset the sync state, if the listing was partial we would
    // otherwise delete not-listed mail locally, and on the next sync on the server
    // as well
    mSyncState = SYNC_STATE_HANDLE_INBOX;
    serverSyncInternal(); /* HACK^W Fix: we should at least try to keep going */
    return;
  }
  (*it).cdata += TQCString(data, data.size() + 1);
  int pos = (*it).cdata.find("\r\n--IMAPDIGEST");
  if (pos > 0) {
    int a = (*it).cdata.find("\r\nX-uidValidity:");
    if (a != -1) {
      int b = (*it).cdata.find("\r\n", a + 17);
      setUidValidity((*it).cdata.mid(a + 17, b - a - 17));
    }
    a = (*it).cdata.find("\r\nX-Access:");
    // Only trust X-Access (i.e. the imap select info) if we don't know mUserRights.
    // The latter is more accurate (checked on every sync) whereas X-Access is only
    // updated when selecting the folder again, which might not happen if using
    // RMB / Check Mail in this folder. We don't need two (potentially conflicting)
    // sources for the readonly setting, in any case.
    if (a != -1 && mUserRightsState != KMail::ACLJobs::Ok ) {
      int b = (*it).cdata.find("\r\n", a + 12);
      const TQString access = (*it).cdata.mid(a + 12, b - a - 12);
      setReadOnly( access == "Read only" );
    }
    (*it).cdata.remove(0, pos);
    mFoundAnIMAPDigest = true;
  }
  pos = (*it).cdata.find("\r\n--IMAPDIGEST", 1);
  // Start with something largish when rebuilding the cache
  if ( uidsOnServer.size() == 0 )
    uidsOnServer.resize( KMail::nextPrime( 2000 ) );
  const int v = 42;
  while (pos >= 0) {
      /*
    KMMessage msg;
    msg.fromString((*it).cdata.mid(16, pos - 16));
    const int flags = msg.headerField("X-Flags").toInt();
    const ulong size = msg.headerField("X-Length").toULong();
    const ulong uid = msg.UID();
       */
    // The below is optimized for speed, not prettiness. The commented out chunk
    // above was the solution copied from kmfolderimap, and it's 15-20% slower.
    const TQCString& entry( (*it).cdata );
    const int indexOfUID = entry.find("X-UID", 16);
    const int startOfUIDValue = indexOfUID  + 7;
    const int indexOfLength = entry.find("X-Length", startOfUIDValue ); // we know length comes after UID
    const int startOfLengthValue = indexOfLength + 10;
    const int indexOfFlags = entry.find("X-Flags", startOfLengthValue ); // we know flags comes last
    const int startOfFlagsValue = indexOfFlags + 9;

    const int flags = entry.mid( startOfFlagsValue, entry.find( '\r', startOfFlagsValue ) - startOfFlagsValue ).toInt();
    const ulong size = entry.mid( startOfLengthValue, entry.find( '\r', startOfLengthValue ) - startOfLengthValue ).toULong();
    const ulong uid = entry.mid( startOfUIDValue, entry.find( '\r', startOfUIDValue ) - startOfUIDValue ).toULong();

    const bool deleted = ( flags & 8 );
    if ( !deleted ) {
      if( uid != 0 ) {
        if ( uidsOnServer.count() == uidsOnServer.size() ) {
          uidsOnServer.resize( KMail::nextPrime( uidsOnServer.size() * 2 ) );
          //kdDebug( 5006 ) << "Resizing to: " << uidsOnServer.size() << endl;
        }
        uidsOnServer.insert( uid, &v );
      }
      bool redownload = false;
      if (  uid <= lastUid() ) {
       /*
        * If this message UID is not present locally, then it must
        * have been deleted by the user, so we delete it on the
        * server also. If we don't have delete permissions on the server,
        * re-download the message, it must have vanished by some error, or
        * while we still thought we were allowed to delete (ACL change).
        *
        * This relies heavily on lastUid() being correct at all times.
        */
        // kdDebug(5006) << "KMFolderCachedImap::slotGetMessagesData() : folder "<<label()<<" already has msg="<<msg->headerField("Subject") << ", UID="<<uid << ", lastUid = " << mLastUid << endl;
        KMMsgBase *existingMessage = findByUID(uid);
        if( !existingMessage ) {
#if MAIL_LOSS_DEBUGGING
           kdDebug(5006) << "Looking at uid " << uid << " high water is: " << lastUid() << " we should delete it" << endl;
#endif
          // double check we deleted it since the last sync
           if ( mDeletedUIDsSinceLastSync.contains(uid) ) {
               if ( mUserRightsState != KMail::ACLJobs::Ok || ( mUserRights & KMail::ACLJobs::Delete ) ) {
#if MAIL_LOSS_DEBUGGING
                   kdDebug(5006) << "message with uid " << uid << " is gone from local cache. Must be deleted on server!!!" << endl;
#endif
                   uidsForDeletionOnServer << uid;
               } else {
                   redownload = true;
               }
           } else {
               kdDebug(5006) << "WARNING: ####### " << endl;
               kdDebug(5006) << "Message locally missing but not deleted in folder: " << folder()->prettyURL() << endl;
               kdDebug(5006) << "The missing UID: " << uid << ". It will be redownloaded " << endl;
               redownload = true;
           }

        } else {
          // if this is a read only folder, ignore status updates from the server
          // since we can't write our status back our local version is what has to
          // be considered correct.
          if ( !mReadOnly || !GlobalSettings::allowLocalFlags() ) {
            /* The message is OK, update flags */
            KMFolderImap::flagsToStatus( existingMessage, flags,  false, mReadOnly ? INT_MAX : mPermanentFlags );
          } else if ( mUserRights & KMail::ACLJobs::WriteSeenFlag ) {
            KMFolderImap::seenFlagToStatus( existingMessage, flags );
          }
        }
        // kdDebug(5006) << "message with uid " << uid << " found in the local cache. " << endl;
      }
      if ( uid > lastUid() || redownload ) {
#if MAIL_LOSS_DEBUGGING
        kdDebug(5006) << "Looking at uid " << uid << " high water is: " << lastUid() << " we should download it" << endl;
#endif
        // The message is new since the last sync, but we might have just uploaded it, in which case
        // the uid map already contains it.
        if ( !uidMap.contains( uid ) ) {
          mMsgsForDownload << KMail::CachedImapJob::MsgForDownload(uid, flags, size);
          if( imapPath() == "/INBOX/" )
            mUidsForDownload << uid;
        }
        // Remember the highest uid and once the download is completed, update mLastUid
        if ( uid > mTentativeHighestUid ) {
#if MAIL_LOSS_DEBUGGING
          kdDebug(5006) << "Setting the tentative highest UID to: " << uid << endl;
#endif
          mTentativeHighestUid = uid;
        }
      }
    }
    (*it).cdata.remove(0, pos);
    (*it).done++;
    pos = (*it).cdata.find("\r\n--IMAPDIGEST", 1);
  }
}

void KMFolderCachedImap::getMessagesResult( KMail::FolderJob *job, bool lastSet )
{
  mProgress += 10;
  if ( !job->error() && !mFoundAnIMAPDigest ) {
      kdWarning(5006) << "######## Folderlisting did not complete, but there was no error! "
          "Aborting sync of folder: " << folder()->prettyURL() << endl;
#if MAIL_LOSS_DEBUGGING
      kmkernel->emergencyExit( i18n("Folder listing failed in interesting ways." ) );
#endif
  }
  if( job->error() ) { // error listing messages but the user chose to continue
    mContentState = imapNoInformation;
    mSyncState = SYNC_STATE_HANDLE_INBOX; // be sure not to continue in this folder
  } else {
    if( lastSet ) { // always true here (this comes from online-imap...)
      mContentState = imapFinished;
      mUIDsOfLocallyChangedStatuses.clear(); // we are up to date again
      mStatusChangedLocally = false;
    }
  }
  serverSyncInternal();
}

void KMFolderCachedImap::slotProgress(unsigned long done, unsigned long total)
{
  int progressSpan = 100 - 5 - mProgress;
  int additionalProgress = ( total == 0 ) ?
                           progressSpan :
                           ( progressSpan * done ) / total;

  // Progress info while retrieving new emails
  // (going from mProgress to mProgress+progressSpan)
  newState( mProgress + additionalProgress, TQString() );
}

void KMFolderCachedImap::setAccount(KMAcctCachedImap *aAccount)
{
  assert( aAccount->isA("KMAcctCachedImap") );
  mAccount = aAccount;
  if( imapPath()=="/" ) aAccount->setFolder( folder() );

  // Folder was renamed in a previous session, and the user didn't sync yet
  TQString newName = mAccount->renamedFolder( imapPath() );
  if ( !newName.isEmpty() )
    folder()->setLabel( newName );

  if( !folder() || !folder()->child() || !folder()->child()->count() ) return;
  for( KMFolderNode* node = folder()->child()->first(); node;
       node = folder()->child()->next() )
    if (!node->isDir())
      static_cast<KMFolderCachedImap*>(static_cast<KMFolder*>(node)->storage())->setAccount(aAccount);
}

void KMFolderCachedImap::listNamespaces()
{
  ImapAccountBase::ListType type = ImapAccountBase::List;
  if ( mAccount->onlySubscribedFolders() )
    type = ImapAccountBase::ListSubscribed;

  kdDebug(5006) << "listNamespaces " << mNamespacesToList << endl;
  if ( mNamespacesToList.isEmpty() ) {
    mSyncState = SYNC_STATE_DELETE_SUBFOLDERS;
    mPersonalNamespacesCheckDone = true;

    TQStringList ns = mAccount->namespaces()[ImapAccountBase::OtherUsersNS];
    ns += mAccount->namespaces()[ImapAccountBase::SharedNS];
    mNamespacesToCheck = ns.count();
    for ( TQStringList::Iterator it = ns.begin(); it != ns.end(); ++it )
    {
      if ( (*it).isEmpty() ) {
        // ignore empty listings as they have been listed before
        --mNamespacesToCheck;
        continue;
      }
      KMail::ListJob* job = new KMail::ListJob( mAccount, type, this, mAccount->addPathToNamespace( *it ) );
      job->setHonorLocalSubscription( true );
      connect( job, TQT_SIGNAL(receivedFolders(const TQStringList&, const TQStringList&,
              const TQStringList&, const TQStringList&, const ImapAccountBase::jobData&)),
          this, TQT_SLOT(slotCheckNamespace(const TQStringList&, const TQStringList&,
              const TQStringList&, const TQStringList&, const ImapAccountBase::jobData&)));
      job->start();
    }
    if ( mNamespacesToCheck == 0 ) {
      serverSyncInternal();
    }
    return;
  }
  mPersonalNamespacesCheckDone = false;

  TQString ns = mNamespacesToList.front();
  mNamespacesToList.pop_front();

  mSyncState = SYNC_STATE_LIST_SUBFOLDERS2;
  newState( mProgress, i18n("Retrieving folders for namespace %1").arg(ns));
  KMail::ListJob* job = new KMail::ListJob( mAccount, type, this,
      mAccount->addPathToNamespace( ns ) );
  job->setNamespace( ns );
  job->setHonorLocalSubscription( true );
  connect( job, TQT_SIGNAL(receivedFolders(const TQStringList&, const TQStringList&,
          const TQStringList&, const TQStringList&, const ImapAccountBase::jobData&)),
      this, TQT_SLOT(slotListResult(const TQStringList&, const TQStringList&,
          const TQStringList&, const TQStringList&, const ImapAccountBase::jobData&)));
  job->start();
}

void KMFolderCachedImap::slotCheckNamespace( const TQStringList& subfolderNames,
                                             const TQStringList& subfolderPaths,
                                             const TQStringList& subfolderMimeTypes,
                                             const TQStringList& subfolderAttributes,
                                             const ImapAccountBase::jobData& jobData )
{
  Q_UNUSED( subfolderPaths );
  Q_UNUSED( subfolderMimeTypes );
  Q_UNUSED( subfolderAttributes );
  --mNamespacesToCheck;
  kdDebug(5006) << "slotCheckNamespace " << subfolderNames << ",remain=" <<
   mNamespacesToCheck << endl;

  // get a correct foldername:
  // strip / and make sure it does not contain the delimiter
  TQString name = jobData.path.mid( 1, jobData.path.length()-2 );
  name.remove( mAccount->delimiterForNamespace( name ) );
  if ( name.isEmpty() ) {
    // should not happen
    kdWarning(5006) << "slotCheckNamespace: ignoring empty folder!" << endl;
    return;
  }

  folder()->createChildFolder();
  KMFolderNode *node = 0;
  for ( node = folder()->child()->first(); node;
        node = folder()->child()->next())
  {
    if ( !node->isDir() && node->name() == name )
      break;
  }
  if ( !subfolderNames.isEmpty() ) {
    if ( node ) {
      // folder exists so we have nothing to do - it will be listed later
      kdDebug(5006) << "found namespace folder " << name << endl;
    } else
    {
      // create folder
      kdDebug(5006) << "create namespace folder " << name << endl;
      KMFolder* newFolder = folder()->child()->createFolder( name, false,
          KMFolderTypeCachedImap );
      if ( newFolder ) {
        KMFolderCachedImap *f = static_cast<KMFolderCachedImap*>( newFolder->storage() );
        f->setImapPath( mAccount->addPathToNamespace( name ) );
        f->setNoContent( true );
        f->setAccount( mAccount );
        f->close("cachedimap");
        kmkernel->dimapFolderMgr()->contentsChanged();
      }
    }
  } else {
    if ( node ) {
      kdDebug(5006) << "delete namespace folder " << name << endl;
      KMFolder* fld = static_cast<KMFolder*>(node);
      kmkernel->dimapFolderMgr()->remove( fld );
    }
  }

  if ( mNamespacesToCheck == 0 ) {
    // all namespaces are done so continue with the next step
    serverSyncInternal();
  }
}

// This lists the subfolders on the server
// and (in slotListResult) takes care of folders that have been removed on the server
bool KMFolderCachedImap::listDirectory()
{
  if( !mAccount->slave() ) { // sync aborted
    resetSyncState();
    emit folderComplete( this, false );
    return false;
  }
  mSubfolderState = imapInProgress;

  // get the folders
  ImapAccountBase::ListType type = ImapAccountBase::List;
  if ( mAccount->onlySubscribedFolders() )
    type = ImapAccountBase::ListSubscribed;
  KMail::ListJob* job = new KMail::ListJob( mAccount, type, this );
  job->setHonorLocalSubscription( true );
  connect( job, TQT_SIGNAL(receivedFolders(const TQStringList&, const TQStringList&,
          const TQStringList&, const TQStringList&, const ImapAccountBase::jobData&)),
      this, TQT_SLOT(slotListResult(const TQStringList&, const TQStringList&,
          const TQStringList&, const TQStringList&, const ImapAccountBase::jobData&)));
  job->start();

  return true;
}

void KMFolderCachedImap::slotListResult( const TQStringList& folderNames,
                                         const TQStringList& folderPaths,
                                         const TQStringList& folderMimeTypes,
                                         const TQStringList& folderAttributes,
                                         const ImapAccountBase::jobData& jobData )
{
  Q_UNUSED( jobData );
  //kdDebug(5006) << label() << ": folderNames=" << folderNames << " folderPaths="
  //<< folderPaths << " mimeTypes=" << folderMimeTypes << endl;
  mSubfolderNames = folderNames;
  mSubfolderPaths = folderPaths;
  mSubfolderMimeTypes = folderMimeTypes;
  mSubfolderState = imapFinished;
  mSubfolderAttributes = folderAttributes;
  //kdDebug(5006) << "##### setting subfolder attributes: " << mSubfolderAttributes << endl;

  folder()->createChildFolder();
  KMFolderNode *node = folder()->child()->first();
  bool root = ( this == mAccount->rootFolder() );

  TQPtrList<KMFolder> toRemove;
  bool emptyList = ( root && mSubfolderNames.empty() );
  if ( !emptyList ) {
    while (node) {
      if (!node->isDir() ) {
        KMFolderCachedImap *f = static_cast<KMFolderCachedImap*>(static_cast<KMFolder*>(node)->storage());

        if ( mSubfolderNames.findIndex(node->name()) == -1 ) {
          TQString name = node->name();
          // as more than one namespace can be listed in the root folder we need to make sure
          // that the folder is within the current namespace
          bool isInNamespace = ( jobData.curNamespace.isEmpty() ||
              jobData.curNamespace == mAccount->namespaceForFolder( f ) );
          // ignore some cases
          bool ignore = root && ( f->imapPath() == "/INBOX/" ||
              mAccount->isNamespaceFolder( name ) || !isInNamespace );

          // This subfolder isn't present on the server
          if( !f->imapPath().isEmpty() && !ignore  ) {
            // The folder has an imap path set, so it has been
            // on the server before. Delete it locally.
            toRemove.append( f->folder() );
            kdDebug(5006) << node->name() << " isn't on the server. It has an imapPath -> delete it locally" << endl;
          }
        } else { // folder both local and on server
          //kdDebug(5006) << node->name() << " is on the server." << endl;

          /**
           * Store the folder attributes for every subfolder.
           */
          int index = mSubfolderNames.findIndex( node->name() );
          f->mFolderAttributes = folderAttributes[ index ];
        }
      } else {
        //kdDebug(5006) << "skipping dir node:" << node->name() << endl;
      }
      node = folder()->child()->next();
    }
  }

  for ( KMFolder* doomed=toRemove.first(); doomed; doomed = toRemove.next() ) {
    rescueUnsyncedMessagesAndDeleteFolder( doomed );
  }

  mProgress += 5;

  // just in case there is nothing to rescue
  slotRescueDone( 0 );
}

// This synchronizes the local folders as needed (creation/deletion). No network communication here.
void KMFolderCachedImap::listDirectory2()
{
  TQString path = folder()->path();
  kmkernel->dimapFolderMgr()->quiet(true);

  bool root = ( this == mAccount->rootFolder() );
  if ( root && !mAccount->hasInbox() )
  {
    KMFolderCachedImap *f = 0;
    KMFolderNode *node;
    // create the INBOX
    for (node = folder()->child()->first(); node; node = folder()->child()->next())
      if (!node->isDir() && node->name() == "INBOX") break;
    if (node) {
      f = static_cast<KMFolderCachedImap*>(static_cast<KMFolder*>(node)->storage());
    } else {
      KMFolder* newFolder = folder()->child()->createFolder("INBOX", true, KMFolderTypeCachedImap);
      if ( newFolder ) {
        f = static_cast<KMFolderCachedImap*>(newFolder->storage());
      }
    }
    if ( f ) {
      f->setAccount( mAccount );
      f->setImapPath( "/INBOX/" );
      f->folder()->setLabel( i18n("inbox") );
    }
    if (!node) {
      if ( f )
        f->close("cachedimap");
      kmkernel->dimapFolderMgr()->contentsChanged();
    }
    // so we have an INBOX
    mAccount->setHasInbox( true );
  }

  if ( root && !mSubfolderNames.isEmpty() ) {
    KMFolderCachedImap* parent =
      findParent( mSubfolderPaths.first(), mSubfolderNames.first() );
    if ( parent ) {
      kdDebug(5006) << "KMFolderCachedImap::listDirectory2 - pass listing to "
        << parent->label() << endl;
      mSubfolderNames.clear();
    }
  }

  // Find all subfolders present on server but not on disk
  TQValueVector<int> foldersNewOnServer;
  for (uint i = 0; i < mSubfolderNames.count(); i++) {

    // Find the subdir, if already present
    KMFolderCachedImap *f = 0;
    KMFolderNode *node = 0;
    for (node = folder()->child()->first(); node;
         node = folder()->child()->next())
      if (!node->isDir() && node->name() == mSubfolderNames[i]) break;

    if (!node) {
      // This folder is not present here
      // Either it's new on the server, or we just deleted it.
      TQString subfolderPath = mSubfolderPaths[i];
      // The code used to look at the uidcache to know if it was "just deleted".
      // But this breaks with noContent folders and with shared folders.
      // So instead we keep a list in the account.
      bool locallyDeleted = mAccount->isDeletedFolder( subfolderPath );
      // That list is saved/restored across sessions, but to avoid any mistake,
      // ask for confirmation if the folder was deleted in a previous session
      // (could be that the folder was deleted & recreated meanwhile from another client...)
      if ( !locallyDeleted && mAccount->isPreviouslyDeletedFolder( subfolderPath ) ) {
           locallyDeleted = KMessageBox::warningYesNo(
             0, i18n( "<qt><p>It seems that the folder <b>%1</b> was deleted. Do you want to delete it from the server?</p></qt>" ).arg( mSubfolderNames[i] ), TQString(), KStdGuiItem::del(), KStdGuiItem::cancel() ) == KMessageBox::Yes;
      }

      if ( locallyDeleted ) {
        kdDebug(5006) << subfolderPath << " was deleted locally => delete on server." << endl;
        foldersForDeletionOnServer += mAccount->deletedFolderPaths( subfolderPath ); // grab all subsubfolders too
      } else {
        kdDebug(5006) << subfolderPath << " is a new folder on the server => create local cache" << endl;
        foldersNewOnServer.append( i );
      }
    } else { // Folder found locally
      if( static_cast<KMFolder*>(node)->folderType() == KMFolderTypeCachedImap )
        f = dynamic_cast<KMFolderCachedImap*>(static_cast<KMFolder*>(node)->storage());
      if( f ) {
        // kdDebug(5006) << "folder("<<f->name()<<")->imapPath()=" << f->imapPath()
        //               << "\nSetting imapPath " << mSubfolderPaths[i] << endl;
        // Write folder settings
        f->setAccount(mAccount);
        f->setNoContent(mSubfolderMimeTypes[i] == "inode/directory");
        f->setNoChildren(mSubfolderMimeTypes[i] == "message/digest");
        f->setImapPath(mSubfolderPaths[i]);
      }
    }
  }

  /* In case we are ignoring non-groupware folders, and this is the groupware
   * main account, find out the contents types of folders that have newly
   * appeared on the server. Otherwise just create them and finish listing.
   * If a folder is already known to be locally unsubscribed, it won't be
   * listed at all, on this level, so these are only folders that we are
   * seeing for the first time. */

  /*  Note: We ask the globalsettings, and not the current state of the
   *  kmkernel->iCalIface().isEnabled(), since that is false during the
   *  very first sync, where we already want to filter. */
  if ( GlobalSettings::self()->showOnlyGroupwareFoldersForGroupwareAccount()
     && GlobalSettings::self()->theIMAPResourceAccount() == (int)mAccount->id()
     && mAccount->hasAnnotationSupport()
     && GlobalSettings::self()->theIMAPResourceEnabled()
     && !foldersNewOnServer.isEmpty() ) {

    TQStringList paths;
    for ( uint i = 0; i < foldersNewOnServer.count(); ++i )
      paths << mSubfolderPaths[ foldersNewOnServer[i] ];

    AnnotationJobs::MultiUrlGetAnnotationJob* job =
      AnnotationJobs::multiUrlGetAnnotation( mAccount->slave(), mAccount->getUrl(), paths, KOLAB_FOLDERTYPE );
    ImapAccountBase::jobData jd( TQString(), folder() );
    jd.cancellable = true;
    mAccount->insertJob(job, jd);
    connect( job, TQT_SIGNAL(result(TDEIO::Job *)),
        TQT_SLOT(slotMultiUrlGetAnnotationResult(TDEIO::Job *)) );

  } else {
    createFoldersNewOnServerAndFinishListing( foldersNewOnServer );
  }
}

void KMFolderCachedImap::createFoldersNewOnServerAndFinishListing( const TQValueVector<int> foldersNewOnServer )
{
  for ( uint i = 0; i < foldersNewOnServer.count(); ++i ) {
    int idx = foldersNewOnServer[i];
    KMFolder* newFolder = folder()->child()->createFolder( mSubfolderNames[idx], false, KMFolderTypeCachedImap);
    if (newFolder) {
      KMFolderCachedImap *f = dynamic_cast<KMFolderCachedImap*>(newFolder->storage());
      kdDebug(5006) << " ####### Locally creating folder " << mSubfolderNames[idx] <<endl;
      f->close("cachedimap");
      f->setAccount(mAccount);
      f->mAnnotationFolderType = "FROMSERVER";
      f->setNoContent(mSubfolderMimeTypes[idx] == "inode/directory");
      f->setNoChildren(mSubfolderMimeTypes[idx] == "message/digest");
      f->setImapPath(mSubfolderPaths[idx]);
      f->mFolderAttributes = mSubfolderAttributes[idx];
      mNewlyCreatedSubfolders.append( TQGuardedPtr<KMFolderCachedImap>( f ) );
      kdDebug(5006) << " ####### Attributes: " << f->mFolderAttributes <<endl;
      //kdDebug(5006) << subfolderPath << ": mAnnotationFolderType set to FROMSERVER" << endl;
      kmkernel->dimapFolderMgr()->contentsChanged();
    } else {
      kdDebug(5006) << "can't create folder " << mSubfolderNames[idx] <<endl;
    }
  }

  kmkernel->dimapFolderMgr()->quiet(false);
  emit listComplete(this);
  if ( !mPersonalNamespacesCheckDone ) {
    // we're not done with the namespaces
    mSyncState = SYNC_STATE_LIST_NAMESPACES;
  }
  serverSyncInternal();
}

//-----------------------------------------------------------------------------
KMFolderCachedImap* KMFolderCachedImap::findParent( const TQString& path,
                                                    const TQString& name )
{
  TQString parent = path.left( path.length() - name.length() - 2 );
  if ( parent.length() > 1 )
  {
    // extract name of the parent
    parent = parent.right( parent.length() - 1 );
    if ( parent != label() )
    {
      KMFolderNode *node = folder()->child()->first();
      // look for a better parent
      while ( node )
      {
        if ( node->name() == parent )
        {
          KMFolder* fld = static_cast<KMFolder*>(node);
          KMFolderCachedImap* imapFld =
            static_cast<KMFolderCachedImap*>( fld->storage() );
          return imapFld;
        }
        node = folder()->child()->next();
      }
    }
  }
  return 0;
}

void KMFolderCachedImap::slotSubFolderComplete(KMFolderCachedImap* sub, bool success)
{
  Q_UNUSED(sub);
  //kdDebug(5006) << label() << " slotSubFolderComplete: " << sub->label() << endl;
  if ( success ) {
    serverSyncInternal();
  }
  else
  {
    // success == false means the sync was aborted.
    if ( mCurrentSubfolder ) {
      Q_ASSERT( sub == mCurrentSubfolder );
      disconnectSubFolderSignals();
    }

    // Next step would be to check quota limits and then to close the folder, but don't bother with
    // both and close the folder right here, since we aborted.
    mSubfoldersForSync.clear();
    mSyncState = SYNC_STATE_INITIAL;
    close("cachedimap");
    emit syncStateChanged();
    emit folderComplete( this, false );
  }
}

void KMFolderCachedImap::slotSubFolderCloseToQuotaChanged()
{
  if ( !mQuotaOnly ) {
    mSomeSubFolderCloseToQuotaChanged = true;
  }
}

void KMFolderCachedImap::slotSimpleData(TDEIO::Job * job, const TQByteArray & data)
{
  KMAcctCachedImap::JobIterator it = mAccount->findJob(job);
  if (it == mAccount->jobsEnd()) return;
  TQBuffer buff((*it).data);
  buff.open(IO_WriteOnly | IO_Append);
  buff.writeBlock(data.data(), data.size());
  buff.close();
}

FolderJob*
KMFolderCachedImap::doCreateJob( KMMessage *msg, FolderJob::JobType jt, KMFolder *folder,
                                 TQString, const AttachmentStrategy* ) const
{
  TQPtrList<KMMessage> msgList;
  msgList.append( msg );
  CachedImapJob *job = new CachedImapJob( msgList, jt, folder? static_cast<KMFolderCachedImap*>( folder->storage() ):0 );
  job->setParentFolder( this );
  return job;
}

FolderJob*
KMFolderCachedImap::doCreateJob( TQPtrList<KMMessage>& msgList, const TQString& sets,
                                 FolderJob::JobType jt, KMFolder *folder ) const
{
  //FIXME: how to handle sets here?
  Q_UNUSED( sets );
  CachedImapJob *job = new CachedImapJob( msgList, jt, folder? static_cast<KMFolderCachedImap*>( folder->storage() ):0 );
  job->setParentFolder( this );
  return job;
}

void
KMFolderCachedImap::setUserRights( unsigned int userRights, KMail::ACLJobs::ACLFetchState state )
{
  mUserRights = userRights;
  mUserRightsState = state;
  writeConfigKeysWhichShouldNotGetOverwrittenByReadConfig();
}

void
KMFolderCachedImap::slotReceivedUserRights( KMFolder* folder )
{
  if ( folder->storage() == this ) {
    disconnect( mAccount, TQT_SIGNAL( receivedUserRights( KMFolder* ) ),
                this, TQT_SLOT( slotReceivedUserRights( KMFolder* ) ) );
    if ( mUserRightsState == KMail::ACLJobs::Ok ) {
      setReadOnly( ( mUserRights & KMail::ACLJobs::Insert ) == 0 );
    }
    mProgress += 5;
    serverSyncInternal();
  }
}

void
KMFolderCachedImap::setReadOnly( bool readOnly )
{
  if ( readOnly != mReadOnly ) {
    mReadOnly = readOnly;
    emit readOnlyChanged( folder() );
  }
}

void
KMFolderCachedImap::slotReceivedACL( KMFolder* folder, TDEIO::Job* job, const KMail::ACLList& aclList )
{
  if ( folder->storage() == this ) {
    disconnect( mAccount, TQT_SIGNAL(receivedACL( KMFolder*, TDEIO::Job*, const KMail::ACLList& )),
                this, TQT_SLOT(slotReceivedACL( KMFolder*, TDEIO::Job*, const KMail::ACLList& )) );
    mACLListState = job->error() ? KMail::ACLJobs::FetchFailed : KMail::ACLJobs::Ok;
    mACLList = aclList;
    serverSyncInternal();
  }
}

void
KMFolderCachedImap::slotStorageQuotaResult( const QuotaInfo& info )
{
  setQuotaInfo( info );
}

void KMFolderCachedImap::setQuotaInfo( const QuotaInfo & info )
{
    if ( info != mQuotaInfo ) {
      const bool wasCloseToQuota = isCloseToQuota();
      mQuotaInfo = info;
      writeConfigKeysWhichShouldNotGetOverwrittenByReadConfig();
      if ( wasCloseToQuota != isCloseToQuota() ) {
        emit closeToQuotaChanged();
      }
      emit folderSizeChanged();
    }
}

void
KMFolderCachedImap::setACLList( const ACLList& arr )
{
  mACLList = arr;
  mACLListState = KMail::ACLJobs::Ok;
}

void
KMFolderCachedImap::slotMultiSetACLResult(TDEIO::Job *job)
{
  KMAcctCachedImap::JobIterator it = mAccount->findJob(job);
  if ( it == mAccount->jobsEnd() ) return; // Shouldn't happen
  if ( (*it).parent != folder() ) return; // Shouldn't happen

  if ( job->error() )
    // Display error but don't abort the sync just for this
    // PENDING(dfaure) reconsider using handleJobError now that it offers continue/cancel
    job->showErrorDialog();
  else
    kmkernel->iCalIface().addFolderChange( folder(), KMailICalIfaceImpl::ACL );

  if (mAccount->slave()) mAccount->removeJob(job);
  serverSyncInternal();
}

void
KMFolderCachedImap::slotACLChanged( const TQString& userId, int permissions )
{
  // The job indicates success in changing the permissions for this user
  // -> we note that it's been done.
  for( ACLList::Iterator it = mACLList.begin(); it != mACLList.end(); ++it ) {
    if ( (*it).userId == userId && (*it).permissions == permissions ) {
      if ( permissions == -1 ) // deleted
        mACLList.erase( it );
      else // added/modified
        (*it).changed = false;
      return;
    }
  }
}

// called by KMAcctCachedImap::killAllJobs
void KMFolderCachedImap::resetSyncState()
{
  if ( mSyncState == SYNC_STATE_INITIAL ) return;
  mSubfoldersForSync.clear();
  mNewlyCreatedSubfolders.clear();
  mSyncState = SYNC_STATE_INITIAL;
  close("cachedimap");
  // Don't use newState here, it would revert to mProgress (which is < current value when listing messages)
  KPIM::ProgressItem *progressItem = mAccount->mailCheckProgressItem();
  TQString str = i18n("Aborted");
  if (progressItem)
     progressItem->setStatus( str );
  emit statusMsg( str );
  emit syncStateChanged();
}

void KMFolderCachedImap::slotIncreaseProgress()
{
  mProgress += 5;
}

void KMFolderCachedImap::newState( int progress, const TQString& syncStatus )
{
  //kdDebug() << k_funcinfo << folder() << " " << mProgress << " " << syncStatus << endl;
  KPIM::ProgressItem *progressItem = mAccount->mailCheckProgressItem();
  if( progressItem )
    progressItem->setCompletedItems( progress );
  if ( !syncStatus.isEmpty() ) {
    TQString str;
    // For a subfolder, show the label. But for the main folder, it's already shown.
    if ( mAccount->imapFolder() == this )
      str = syncStatus;
    else
      str = TQString( "%1: %2" ).arg( label() ).arg( syncStatus );
    if( progressItem )
      progressItem->setStatus( str );
    emit statusMsg( str );
  }
  if( progressItem )
    progressItem->updateProgress();
}

void KMFolderCachedImap::setSubfolderState( imapState state )
{
  mSubfolderState = state;
  if ( state == imapNoInformation && folder()->child() )
  {
    // pass through to childs
    KMFolderNode* node;
    TQPtrListIterator<KMFolderNode> it( *folder()->child() );
    for ( ; (node = it.current()); )
    {
      ++it;
      if (node->isDir()) continue;
      KMFolder *folder = static_cast<KMFolder*>(node);
      static_cast<KMFolderCachedImap*>(folder->storage())->setSubfolderState( state );
    }
  }
}

void KMFolderCachedImap::setImapPath(const TQString &path)
{
  mImapPath = path;
}

static bool isFolderTypeKnownToUs( const TQString &type )
{
  for ( uint i = 0 ; i <= ContentsTypeLast; ++i ) {
    FolderContentsType contentsType = static_cast<KMail::FolderContentsType>( i );
    if ( type == KMailICalIfaceImpl::annotationForContentsType( contentsType ) )
      return true;
  }
  return false;
}

// mAnnotationFolderType is the annotation as known to the server (and stored in kmailrc)
// It is updated from the folder contents type and whether it's a standard resource folder.
// This happens during the syncing phase and during initFolder for a new folder.
// Don't do it earlier, e.g. from setContentsType:
// on startup, it's too early there to know if this is a standard resource folder.
void KMFolderCachedImap::updateAnnotationFolderType()
{
  TQString oldType = mAnnotationFolderType;
  TQString oldSubType;
  int dot = oldType.find( '.' );
  if ( dot != -1 ) {
    oldType.truncate( dot );
    oldSubType = mAnnotationFolderType.mid( dot + 1 );
  }

  TQString newType, newSubType;
  // We want to store an annotation on the folder only if using the kolab storage.
  if ( kmkernel->iCalIface().storageFormat( folder() ) == KMailICalIfaceImpl::StorageXML ) {
    newType = KMailICalIfaceImpl::annotationForContentsType( mContentsType );
    if ( kmkernel->iCalIface().isStandardResourceFolder( folder() ) )
      newSubType = "default";
    else if ( oldSubType != "default" )
      newSubType = oldSubType; // preserve unknown subtypes, like drafts etc.
  }

  // We do not want to overwrite custom folder types (which we treat as mail folders).
  // So only overwrite custom folder types if the user changed the folder type himself to something
  // other than mail.
  const bool changingTypeAllowed = isFolderTypeKnownToUs( oldType ) ||
                                   ( mContentsType != ContentsTypeMail );

  //kdDebug(5006) << mImapPath << ": updateAnnotationFolderType: " << newType << " " << newSubType << endl;
  if ( ( newType != oldType || newSubType != oldSubType ) && changingTypeAllowed ) {
    mAnnotationFolderType = newType + ( newSubType.isEmpty() ? TQString() : "."+newSubType );
    mAnnotationFolderTypeChanged = true; // force a "set annotation" on next sync
    kdDebug(5006) << mImapPath << ": updateAnnotationFolderType: '" << mAnnotationFolderType << "', was (" << oldType << " " << oldSubType << ") => mAnnotationFolderTypeChanged set to TRUE" << endl;
  }
  // Ensure that further readConfig()s don't lose mAnnotationFolderType
  writeConfigKeysWhichShouldNotGetOverwrittenByReadConfig();
}

void KMFolderCachedImap::setIncidencesFor( IncidencesFor incfor )
{
  if ( mIncidencesFor != incfor ) {
    mIncidencesFor = incfor;
    mIncidencesForChanged = true;
  }
}

void KMFolderCachedImap::setSharedSeenFlags(bool b)
{
  if ( mSharedSeenFlags != b ) {
    mSharedSeenFlags = b;
    mSharedSeenFlagsChanged = true;
  }
}

void KMFolderCachedImap::slotAnnotationResult(const TQString& entry, const TQString& value, bool found)
{
  if ( entry == KOLAB_FOLDERTYPE ) {
    // There are four cases.
    // 1) no content-type on server -> set it
    // 2) different content-type on server, locally changed -> set it (we don't even come here)
    // 3) different (known) content-type on server, no local change -> get it
    // 4) different unknown content-type on server, probably some older version -> set it
    if ( found ) {
      TQString type = value;
      TQString subtype;
      int dot = value.find( '.' );
      if ( dot != -1 ) {
        type.truncate( dot );
        subtype = value.mid( dot + 1 );
      }
      bool foundKnownType = false;
      for ( uint i = 0 ; i <= ContentsTypeLast; ++i ) {
        FolderContentsType contentsType = static_cast<KMail::FolderContentsType>( i );
        if ( type == KMailICalIfaceImpl::annotationForContentsType( contentsType ) ) {
          // Case 3: known content-type on server, get it
          //kdDebug(5006) << mImapPath << ": slotGetAnnotationResult: found known type of annotation" << endl;
          if ( contentsType != ContentsTypeMail )
            kmkernel->iCalIface().setStorageFormat( folder(), KMailICalIfaceImpl::StorageXML );
          mAnnotationFolderType = value;
          if ( folder()->parent()->owner()->idString() != GlobalSettings::self()->theIMAPResourceFolderParent()
               && GlobalSettings::self()->theIMAPResourceEnabled()
               && subtype == "default" ) {
            // Truncate subtype if this folder can't be a default resource folder for us,
            // although it apparently is for someone else.
            mAnnotationFolderType = type;
            kdDebug(5006) << mImapPath << ": slotGetAnnotationResult: parent folder is " << folder()->parent()->owner()->idString() << " => truncating annotation to " << value << endl;
          }
          setContentsType( contentsType );
          mAnnotationFolderTypeChanged = false; // we changed it, not the user
          foundKnownType = true;

          // Users don't read events/contacts/etc. in kmail, so mark them all as read.
          // This is done in cachedimapjob when getting new messages, but do it here too,
          // for the initial set of messages when we didn't know this was a resource folder yet,
          // for old folders, etc.
          if ( contentsType != ContentsTypeMail )
            markUnreadAsRead();

          break;
        }
      }
      if ( !foundKnownType ) {
        //kdDebug(5006) << "slotGetAnnotationResult: no known type of annotation found, leaving it untouched" << endl;

        // Case 4: Server has strange content-type. We must not overwrite it, see https://issues.kolab.org/issue2069.
        //         Treat the content-type as mail until we change it ourselves.
        mAnnotationFolderTypeChanged = false;
        mAnnotationFolderType = value;
        setContentsType( ContentsTypeMail );
      }

      // Ensure that further readConfig()s don't lose mAnnotationFolderType
      writeConfigKeysWhichShouldNotGetOverwrittenByReadConfig();
      // TODO handle subtype (inbox, drafts, sentitems, junkemail)
    }
    else if ( !mReadOnly ) {
      // Case 1: server doesn't have content-type, set it
      //kdDebug(5006) << "slotGetAnnotationResult: no annotation found, will need to set it" << endl;
      mAnnotationFolderTypeChanged = true;
    }
  } else if ( entry == KOLAB_INCIDENCESFOR ) {
    if ( found ) {
      mIncidencesFor = incidencesForFromString( value );
      Q_ASSERT( mIncidencesForChanged == false );
    }
  } else if ( entry == KOLAB_SHAREDSEEN ) {
    if ( found ) {
      mSharedSeenFlags = value == "true";
    }
  }
}

void KMFolderCachedImap::slotGetAnnotationResult( TDEIO::Job* job )
{
  KMAcctCachedImap::JobIterator it = mAccount->findJob(job);
  Q_ASSERT( it != mAccount->jobsEnd() );
  if ( it == mAccount->jobsEnd() ) return; // Shouldn't happen
  Q_ASSERT( (*it).parent == folder() );
  if ( (*it).parent != folder() ) return; // Shouldn't happen

  AnnotationJobs::GetAnnotationJob* annjob = static_cast<AnnotationJobs::GetAnnotationJob *>( job );
  if ( annjob->error() ) {
    if ( job->error() == TDEIO::ERR_UNSUPPORTED_ACTION ) {
      // that's when the imap server doesn't support annotations
      if ( GlobalSettings::self()->theIMAPResourceStorageFormat() == GlobalSettings::EnumTheIMAPResourceStorageFormat::XML
           && (uint)GlobalSettings::self()->theIMAPResourceAccount() == mAccount->id() )
	KMessageBox::error( 0, i18n( "The IMAP server %1 does not have support for IMAP annotations. The XML storage cannot be used on this server; please re-configure KMail differently." ).arg( mAccount->host() ) );
      mAccount->setHasNoAnnotationSupport();
    }
    else
      kdWarning(5006) << "slotGetAnnotationResult: " << job->errorString() << endl;
  }

  if (mAccount->slave()) mAccount->removeJob(job);
  mProgress += 2;
  serverSyncInternal();
}

void KMFolderCachedImap::slotMultiUrlGetAnnotationResult( TDEIO::Job* job )
{
  KMAcctCachedImap::JobIterator it = mAccount->findJob(job);
  Q_ASSERT( it != mAccount->jobsEnd() );
  if ( it == mAccount->jobsEnd() ) return; // Shouldn't happen
  Q_ASSERT( (*it).parent == folder() );
  if ( (*it).parent != folder() ) return; // Shouldn't happen

  TQValueVector<int> folders;
  AnnotationJobs::MultiUrlGetAnnotationJob* annjob
    = static_cast<AnnotationJobs::MultiUrlGetAnnotationJob *>( job );
  if ( annjob->error() ) {
    if ( job->error() == TDEIO::ERR_UNSUPPORTED_ACTION ) {
      // that's when the imap server doesn't support annotations
      if ( GlobalSettings::self()->theIMAPResourceStorageFormat() == GlobalSettings::EnumTheIMAPResourceStorageFormat::XML
           && (uint)GlobalSettings::self()->theIMAPResourceAccount() == mAccount->id() )
        KMessageBox::error( 0, i18n( "The IMAP server %1 doesn't have support for imap annotations. The XML storage cannot be used on this server, please re-configure KMail differently" ).arg( mAccount->host() ) );
      mAccount->setHasNoAnnotationSupport();
    }
    else
      kdWarning(5006) << "slotGetMultiUrlAnnotationResult: " << job->errorString() << endl;
  } else {
    // we got the annotation allright, let's filter out the ones with the wrong type
    TQMap<TQString, TQString> annotations = annjob->annotations();
    TQMap<TQString, TQString>::Iterator it = annotations.begin();
    for ( ; it != annotations.end(); ++it ) {
      const TQString folderPath = it.key();
      const TQString annotation = it.data();
      kdDebug(5006) << k_funcinfo << "Folder: " << folderPath << " has type: " << annotation << endl;
      // we're only interested in the main type
      TQString type(annotation);
      int dot = annotation.find( '.' );
      if ( dot != -1 ) type.truncate( dot );
      type = type.simplifyWhiteSpace();

      const int idx = mSubfolderPaths.findIndex( folderPath );
      const bool isNoContent =  mSubfolderMimeTypes[idx] == "inode/directory";
      if ( ( isNoContent && type.isEmpty() )
        || ( !type.isEmpty() && type != KMailICalIfaceImpl::annotationForContentsType( ContentsTypeMail ) ) ) {
        folders.append( idx );
        kdDebug(5006) << k_funcinfo << " subscribing to: " << folderPath << endl;
      } else {
        kdDebug(5006) << k_funcinfo << " automatically unsubscribing from: " << folderPath << endl;
        mAccount->changeLocalSubscription( folderPath, false );
      }
    }
  }

  if (mAccount->slave()) mAccount->removeJob(job);
  createFoldersNewOnServerAndFinishListing( folders );
}

void KMFolderCachedImap::slotQuotaResult( TDEIO::Job* job )
{
  KMAcctCachedImap::JobIterator it = mAccount->findJob(job);
  Q_ASSERT( it != mAccount->jobsEnd() );
  if ( it == mAccount->jobsEnd() ) return; // Shouldn't happen
  Q_ASSERT( (*it).parent == folder() );
  if ( (*it).parent != folder() ) return; // Shouldn't happen

  QuotaJobs::GetStorageQuotaJob* quotajob = static_cast<QuotaJobs::GetStorageQuotaJob *>( job );
  QuotaInfo empty;
  if ( quotajob->error() ) {
    if ( job->error() == TDEIO::ERR_UNSUPPORTED_ACTION ) {
      // that's when the imap server doesn't support quota
      mAccount->setHasNoQuotaSupport();
      setQuotaInfo( empty );
    }
    else
      kdWarning(5006) << "slotGetQuotaResult: " << job->errorString() << endl;
  }

  if (mAccount->slave()) mAccount->removeJob(job);
  mProgress += 2;
  serverSyncInternal();
}

void
KMFolderCachedImap::slotAnnotationChanged( const TQString& entry, const TQString& attribute, const TQString& value )
{
  Q_UNUSED( attribute );
  Q_UNUSED( value );
  //kdDebug(5006) << k_funcinfo << entry << " " << attribute << " " << value << endl;
  if ( entry == KOLAB_FOLDERTYPE )
    mAnnotationFolderTypeChanged = false;
  else if ( entry == KOLAB_INCIDENCESFOR ) {
    mIncidencesForChanged = false;
    // The incidences-for changed, we must trigger the freebusy creation.
    // HACK: in theory we would need a new enum value for this.
    kmkernel->iCalIface().addFolderChange( folder(), KMailICalIfaceImpl::ACL );
  } else if ( entry == KOLAB_SHAREDSEEN ) {
    mSharedSeenFlagsChanged = false;
  }
}

void KMFolderCachedImap::slotTestAnnotationResult(TDEIO::Job *job)
{
  KMAcctCachedImap::JobIterator it = mAccount->findJob(job);
  Q_ASSERT( it != mAccount->jobsEnd() );
  if ( it == mAccount->jobsEnd() ) return; // Shouldn't happen
  Q_ASSERT( (*it).parent == folder() );
  if ( (*it).parent != folder() ) return; // Shouldn't happen

  mAccount->setAnnotationCheckPassed( true );
  if ( job->error() ) {
    kdDebug(5006) << "Test Annotation was not passed, disabling annotation support" << endl;
    mAccount->setHasNoAnnotationSupport( );
  } else {
    kdDebug(5006) << "Test Annotation was passed   OK" << endl;
  }
  if (mAccount->slave()) mAccount->removeJob(job);
  serverSyncInternal();
}

void
KMFolderCachedImap::slotSetAnnotationResult(TDEIO::Job *job)
{
  KMAcctCachedImap::JobIterator it = mAccount->findJob(job);
  if ( it == mAccount->jobsEnd() ) return; // Shouldn't happen
  if ( (*it).parent != folder() ) return; // Shouldn't happen

  bool cont = true;
  if ( job->error() ) {
    // Don't show error if the server doesn't support ANNOTATEMORE and this folder only contains mail
    if ( job->error() == TDEIO::ERR_UNSUPPORTED_ACTION && contentsType() == ContentsTypeMail ) {
      if (mAccount->slave()) mAccount->removeJob(job);
    } else {
      cont = mAccount->handleJobError( job, i18n( "Error while setting annotation: " ) + '\n' );
    }
  } else {
    if (mAccount->slave()) mAccount->removeJob(job);
  }
  if ( cont )
    serverSyncInternal();
}

void KMFolderCachedImap::slotUpdateLastUid()
{
  if( mTentativeHighestUid != 0 ) {

      // Sanity checking:
      // By now all new mails should be downloaded, which means
      // that iteration over the folder should yield only UIDs
      // lower or equal to what we think the highes ist, and the
      // highest one as well. If not, our notion of the highest
      // uid we've seen thus far is wrong, which is dangerous, so
      // don't update the mLastUid, then.
      // Not entirely true though, mails might have been moved out
      // of the folder already by filters, thus giving us a higher tentative
      // uid than we actually observe here.
      bool sane = count() == 0;

      for (int i=0;i<count(); i++ ) {
          ulong uid = getMsgBase(i)->UID();
          if ( uid > mTentativeHighestUid && uid > lastUid() ) {
              kdWarning(5006) << "DANGER: Either the server listed a wrong highest uid, "
                  "or we parsed it wrong. Send email to adam@kde.org, please, and include this log." << endl;
              kdWarning(5006) << "uid: " << uid << " mTentativeHighestUid: " << mTentativeHighestUid << endl;
              assert( false );
              break;
          } else {
              sane = true;
          }
      }
      if (sane) {
#if MAIL_LOSS_DEBUGGING
          kdDebug(5006) << "Tentative highest UID test was sane, writing out: " << mTentativeHighestUid << endl;
#endif
          setLastUid( mTentativeHighestUid );
      }
  }
  mTentativeHighestUid = 0;
}

bool KMFolderCachedImap::isMoveable() const
{
  return ( hasChildren() == HasNoChildren &&
      !folder()->isSystemFolder() ) ? true : false;
}

void KMFolderCachedImap::slotFolderDeletionOnServerFinished()
{
  for ( TQStringList::const_iterator it = foldersForDeletionOnServer.constBegin();
      it != foldersForDeletionOnServer.constEnd(); ++it ) {
    KURL url( mAccount->getUrl() );
    url.setPath( *it );
    kmkernel->iCalIface().folderDeletedOnServer( url );
  }
  serverSyncInternal();
}

int KMFolderCachedImap::createIndexFromContentsRecursive()
{
  if ( !folder() || !folder()->child() )
    return 0;

  KMFolderNode *node = 0;
  for( TQPtrListIterator<KMFolderNode> it( *folder()->child() ); (node = it.current()); ++it ) {
    if( !node->isDir() ) {
      KMFolderCachedImap* storage = static_cast<KMFolderCachedImap*>(static_cast<KMFolder*>(node)->storage());
      kdDebug() << k_funcinfo << "Re-indexing: " << storage->folder()->label() << endl;
      int rv = storage->createIndexFromContentsRecursive();
      if ( rv > 0 )
        return rv;
    }
  }

  return createIndexFromContents();
}

void KMFolderCachedImap::setAlarmsBlocked( bool blocked )
{
  mAlarmsBlocked = blocked;
}

bool KMFolderCachedImap::alarmsBlocked() const
{
  return mAlarmsBlocked;
}

bool KMFolderCachedImap::isCloseToQuota() const
{
  bool closeToQuota = false;
  if ( mQuotaInfo.isValid() && mQuotaInfo.max().toInt() > 0 ) {
    const int ratio = mQuotaInfo.current().toInt() * 100  / mQuotaInfo.max().toInt();
    //kdDebug(5006) << "Quota ratio: " << ratio << "% " << mQuotaInfo.toString() << endl;
    closeToQuota = ( ratio > 0 && ratio >= GlobalSettings::closeToQuotaThreshold() );
  }
  //kdDebug(5006) << "Folder: " << folder()->prettyURL() << " is over quota: " << closeToQuota << endl;
  return closeToQuota;
}

KMCommand* KMFolderCachedImap::rescueUnsyncedMessages()
{
  TQValueList<unsigned long> newMsgs = findNewMessages();
  kdDebug() << k_funcinfo << newMsgs << " of " << count() << endl;
  if ( newMsgs.isEmpty() )
    return 0;
  KMFolder *dest = 0;
  bool manualMove = true;
  while ( GlobalSettings::autoLostFoundMove() ) {
    // find the inbox of this account
    KMFolder *inboxFolder = kmkernel->findFolderById( TQString(".%1.directory/INBOX").arg( account()->id() ) );
    if ( !inboxFolder ) {
      kdWarning(5006) << k_funcinfo << "inbox not found!" << endl;
      break;
    }
    KMFolderDir *inboxDir = inboxFolder->child();
    if ( !inboxDir && !inboxFolder->storage() )
      break;
    assert( inboxFolder->storage()->folderType() == KMFolderTypeCachedImap );

    // create lost+found folder if needed
    KMFolderNode *node;
    KMFolder *lfFolder = 0;
    if ( !(node = inboxDir->hasNamedFolder( i18n("lost+found") )) ) {
      kdDebug(5006) << k_funcinfo << "creating lost+found folder" << endl;
      KMFolder* folder = kmkernel->dimapFolderMgr()->createFolder(
          i18n("lost+found"), false, KMFolderTypeCachedImap, inboxDir );
      if ( !folder || !folder->storage() )
        break;
      static_cast<KMFolderCachedImap*>( folder->storage() )->initializeFrom(
        static_cast<KMFolderCachedImap*>( inboxFolder->storage() ) );
      folder->storage()->setContentsType( KMail::ContentsTypeMail );
      folder->storage()->writeConfig();
      lfFolder = folder;
    } else {
      kdDebug(5006) << k_funcinfo << "found lost+found folder" << endl;
      lfFolder = dynamic_cast<KMFolder*>( node );
    }
    if ( !lfFolder || !lfFolder->createChildFolder() || !lfFolder->storage() )
      break;

    // create subfolder for this incident
    TQDate today = TQDate::currentDate();
    TQString baseName = folder()->label() + "-" + TQString::number( today.year() )
        + (today.month() < 10 ? "0" : "" ) + TQString::number( today.month() )
        + (today.day() < 10 ? "0" : "" ) + TQString::number( today.day() );
    TQString name = baseName;
    int suffix = 0;
    while ( (node = lfFolder->child()->hasNamedFolder( name )) ) {
      ++suffix;
      name = baseName + '-' + TQString::number( suffix );
    }
    kdDebug(5006) << k_funcinfo << "creating lost+found folder " << name << endl;
    dest = kmkernel->dimapFolderMgr()->createFolder( name, false, KMFolderTypeCachedImap, lfFolder->child() );
    if ( !dest || !dest->storage() )
        break;
    static_cast<KMFolderCachedImap*>( dest->storage() )->initializeFrom(
      static_cast<KMFolderCachedImap*>( lfFolder->storage() ) );
    dest->storage()->setContentsType( contentsType() );
    dest->storage()->writeConfig();

    KMessageBox::sorry( 0, i18n("<p>There are new messages in folder <b>%1</b>, which "
          "have not been uploaded to the server yet, but the folder has been deleted "
          "on the server or you do not "
          "have sufficient access rights on the folder to upload them.</p>"
          "<p>All affected messages will therefore be moved to <b>%2</b> "
          "to avoid data loss.</p>").arg( folder()->prettyURL() ).arg( dest->prettyURL() ),
          i18n("Insufficient access rights") );
    manualMove = false;
    break;
  }

  if ( manualMove ) {
    const TQString msg ( i18n( "<p>There are new messages in this folder (%1), which "
          "have not been uploaded to the server yet, but the folder has been deleted "
          "on the server or you do not "
          "have sufficient access rights on the folder now to upload them. "
          "Please contact your administrator to allow upload of new messages "
          "to you, or move them out of this folder.</p> "
          "<p>Do you want to move these messages to another folder now?</p>").arg( folder()->prettyURL() ) );
    if ( KMessageBox::warningYesNo( 0, msg, TQString(), i18n("Move"), i18n("Do Not Move") ) == KMessageBox::Yes ) {
      KMail::KMFolderSelDlg dlg( kmkernel->getKMMainWidget(),
          i18n("Move Messages to Folder"), true );
      if ( dlg.exec() ) {
        dest = dlg.folder();
      }
    }
  }
  if ( dest ) {
    TQPtrList<KMMsgBase> msgs;
    for( int i = 0; i < count(); ++i ) {
      KMMsgBase *msg = getMsgBase( i );
      if( !msg ) continue; /* what goes on if getMsg() returns 0? */
      if ( msg->UID() == 0 )
        msgs.append( msg );
    }
    KMCommand *command = new KMMoveCommand( dest, msgs );
    command->start();
    return command;
  }
  return 0;
}

void KMFolderCachedImap::rescueUnsyncedMessagesAndDeleteFolder( KMFolder *folder, bool root )
{
  kdDebug() << k_funcinfo << folder << " " << root << endl;
  if ( root )
    mToBeDeletedAfterRescue.append( folder );
  folder->open("cachedimap");
  KMFolderCachedImap* storage = dynamic_cast<KMFolderCachedImap*>( folder->storage() );
  if ( storage ) {
    KMCommand *command = storage->rescueUnsyncedMessages();
    if ( command ) {
      connect( command, TQT_SIGNAL(completed(KMCommand*)),
               TQT_SLOT(slotRescueDone(KMCommand*)) );
      ++mRescueCommandCount;
    } else {
      // nothing to rescue, close folder
      // (we don't need to close it in the other case, it will be deleted anyway)
      folder->close("cachedimap");
    }
  }
  if ( folder->child() ) {
    KMFolderNode *node = folder->child()->first();
    while (node) {
      if (!node->isDir() ) {
        KMFolder *subFolder = static_cast<KMFolder*>( node );
        rescueUnsyncedMessagesAndDeleteFolder( subFolder, false );
      }
      node = folder->child()->next();
    }
  }
}

void KMFolderCachedImap::slotRescueDone(KMCommand * command)
{
  // FIXME: check command result
  if ( command )
    --mRescueCommandCount;
  if ( mRescueCommandCount > 0 )
    return;
  for ( TQValueList<KMFolder*>::ConstIterator it = mToBeDeletedAfterRescue.constBegin();
        it != mToBeDeletedAfterRescue.constEnd(); ++it ) {
    kmkernel->dimapFolderMgr()->remove( *it );
  }
  mToBeDeletedAfterRescue.clear();
  serverSyncInternal();
}

void KMFolderCachedImap::slotRenameFolderFinished()
{
  // The syncing code assumes the folder was opened by us, and later closes it. So better
  // make sure the reference count is correct, since the folder was force-closed by the rename.
  // Otherwise bad things can happen, see https://issues.kolab.org/issue3853.
  open( "cachedimap" );
  serverSyncInternal();
}

bool KMFolderCachedImap::canDeleteMessages() const
{
  if ( isReadOnly() )
    return false;
  if ( mUserRightsState == KMail::ACLJobs::Ok && !(userRights() & ACLJobs::Delete) )
    return false;
  return true;
}

bool KMFolderCachedImap::mailCheckInProgress() const
{
  return mSyncState != SYNC_STATE_INITIAL;
}

#include "kmfoldercachedimap.moc"