summaryrefslogtreecommitdiffstats
path: root/kmail/kmailicalifaceimpl.cpp
blob: 05524e02786e0e9568ebb3b1b8276d3119434137 (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
/*
    This file is part of KMail.

    Copyright (c) 2003 Steffen Hansen <steffen@klaralvdalens-datakonsult.se>
    Copyright (c) 2003 - 2004 Bo Thorsen <bo@sonofthor.dk>
    Copyright (c) 2004 Till Adam <adam@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  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 Qt library by Trolltech AS, Norway (or with modified versions
    of Qt that use the same license as Qt), 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
    Qt.  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 "kmailicalifaceimpl.h"
#include "kmfolder.h"
#include "kmfoldertree.h"
#include "kmfolderdir.h"
#include "kmgroupware.h"
#include "kmfoldermgr.h"
#include "kmcommands.h"
#include "kmfolderindex.h"
#include "kmmsgdict.h"
#include "kmmsgpart.h"
using KMail::AccountManager;
#include "kmfolderimap.h"
#include "globalsettings.h"
#include "accountmanager.h"
#include "kmfoldercachedimap.h"
#include "kmacctcachedimap.h"
#include "acljobs.h"

#include "scalix.h"

#include <mimelib/enum.h>
#include <mimelib/utility.h>
#include <mimelib/body.h>
#include <mimelib/mimepp.h>

#include <tqfile.h>
#include <tqmap.h>
#include <tqtextcodec.h>

#include <kdebug.h>
#include <kiconloader.h>
#include <kinputdialog.h>
#include <dcopclient.h>
#include <kmessagebox.h>
#include <kconfig.h>
#include <kurl.h>
#include <ktempfile.h>

using namespace KMail;

TQMap<TQString, TQString> *KMailICalIfaceImpl::mSubResourceUINamesMap = new TQMap<TQString, TQString>;

// Local helper methods
static void vPartMicroParser( const TQString& str, TQString& s );
static void reloadFolderTree();

// The index in this array is the KMail::FolderContentsType enum
static const struct {
  const char* contentsTypeStr; // the string used in the DCOP interface
  const char* mimetype;
  KFolderTreeItem::Type treeItemType;
  const char* annotation;
  const char* translatedName;
} s_folderContentsType[] = {
  { "Mail", "application/x-vnd.kolab.mail", KFolderTreeItem::Other, "mail", I18N_NOOP( "Mail" ) },
  { "Calendar", "application/x-vnd.kolab.event", KFolderTreeItem::Calendar, "event", I18N_NOOP( "Calendar" ) },
  { "Contact", "application/x-vnd.kolab.contact", KFolderTreeItem::Contacts, "contact", I18N_NOOP( "Contacts" ) },
  { "Note", "application/x-vnd.kolab.note", KFolderTreeItem::Notes, "note", I18N_NOOP( "Notes" ) },
  { "Task", "application/x-vnd.kolab.task", KFolderTreeItem::Tasks, "task", I18N_NOOP( "Tasks" ) },
  { "Journal", "application/x-vnd.kolab.journal", KFolderTreeItem::Journals, "journal", I18N_NOOP( "Journal" ) }
};

static TQString folderContentsType( KMail::FolderContentsType type )
{
  return s_folderContentsType[type].contentsTypeStr;
}

static TQString folderKolabMimeType( KMail::FolderContentsType type )
{
  return s_folderContentsType[type].mimetype;
}

KMailICalIfaceImpl::StorageFormat KMailICalIfaceImpl::globalStorageFormat() const {
  return GlobalSettings::self()->theIMAPResourceStorageFormat()
    == GlobalSettings::EnumTheIMAPResourceStorageFormat::XML ? StorageXML : StorageIcalVcard;
}

static KMail::FolderContentsType folderContentsType( const TQString& type )
{
  for ( uint i = 0 ; i < sizeof s_folderContentsType / sizeof *s_folderContentsType; ++i )
    if ( type == s_folderContentsType[i].contentsTypeStr )
      return static_cast<KMail::FolderContentsType>( i );
  return KMail::ContentsTypeMail;
}

static TQString localizedDefaultFolderName( KMail::FolderContentsType type )
{
  return i18n( s_folderContentsType[type].translatedName );
}

const char* KMailICalIfaceImpl::annotationForContentsType( KMail::FolderContentsType type )
{
  return s_folderContentsType[type].annotation;
}

ExtraFolder::ExtraFolder( KMFolder* f )
    : folder( f )
{
    folder->open("kmailicaliface::extrafolder");
}

ExtraFolder::~ExtraFolder()
{
    if ( folder )
        folder->close("kmailicaliface::extrafolder");
}


/*
  This interface has three parts to it - libkcal interface;
  kmail interface; and helper functions.

  The libkcal interface and the kmail interface have the same three
  methods: add, delete and refresh. The only difference is that the
  libkcal interface is used from the IMAP resource in libkcal and
  the kmail interface is used from the groupware object in kmail.
*/

KMailICalIfaceImpl::KMailICalIfaceImpl()
  : DCOPObject( "KMailICalIface" ), TQObject( 0, "KMailICalIfaceImpl" ),
    mContacts( 0 ), mCalendar( 0 ), mNotes( 0 ), mTasks( 0 ), mJournals( 0 ),
    mFolderLanguage( 0 ), mFolderParentDir( 0 ), mFolderType( KMFolderTypeUnknown ),
    mUseResourceIMAP( false ), mResourceQuiet( false ), mHideFolders( true )
{
  // Listen to config changes
  connect( kmkernel, TQT_SIGNAL( configChanged() ), this, TQT_SLOT( readConfig() ) );
  connect( kmkernel, TQT_SIGNAL( folderRemoved( KMFolder* ) ),
           this, TQT_SLOT( slotFolderRemoved( KMFolder* ) ) );

  mExtraFolders.setAutoDelete( true );
  mAccumulators.setAutoDelete( true );
}


/* libkcal part of the interface, called from the resources using this
 * when incidences are added or deleted */

// Helper function to find an attachment of a given mimetype
// Can't use KMMessage::findDwBodyPart since it only works with known mimetypes.
static DwBodyPart* findBodyPartByMimeType( const KMMessage& msg, const char* sType, const char* sSubtype, bool startsWith = false )
{
  // quickly searching for our message part: since Kolab parts are
  // top-level parts we do *not* have to travel into embedded multiparts
  DwBodyPart* part = msg.getFirstDwBodyPart();
  while( part ){
  //    kdDebug() << part->Headers().ContentType().TypeStr().c_str() << " "
  //            << part->Headers().ContentType().SubtypeStr().c_str() << endl;
    if ( part->hasHeaders() ) {
      DwMediaType& contentType = part->Headers().ContentType();
      if ( startsWith ) {
        if ( contentType.TypeStr() == sType
             && TQString( contentType.SubtypeStr().c_str() ).startsWith( sSubtype ) )
          return part;
      }
      else
        if ( contentType.TypeStr() == sType
             && contentType.SubtypeStr() == sSubtype )
          return part;
    }
    part = part->Next();
  }
  return 0;
}

// Helper function to find an attachment with a given filename
static DwBodyPart* findBodyPart( const KMMessage& msg, const TQString& attachmentName )
{
  // quickly searching for our message part: since Kolab parts are
  // top-level parts we do *not* have to travel into embedded multiparts
  for ( DwBodyPart* part = msg.getFirstDwBodyPart(); part; part = part->Next() ) {
    //kdDebug(5006) << "findBodyPart:  - " << part->Headers().ContentDisposition().Filename().c_str() << endl;
    if ( part->hasHeaders()
         && attachmentName == part->Headers().ContentDisposition().Filename().c_str() )
      return part;
    if ( part->hasHeaders() && attachmentName == part->Headers().ContentType().Name().c_str() )
      return part;
  }
  return 0;
}

#if 0
static void debugBodyParts( const char* foo, const KMMessage& msg )
{
  kdDebug(5006) << "--debugBodyParts " << foo << "--" << endl;
  for ( DwBodyPart* part = msg.getFirstDwBodyPart(); part; part = part->Next() ) {
    if ( part->hasHeaders() ) {
      kdDebug(5006) << " bodypart: " << part << endl;
      kdDebug(5006) << "        " << part->Headers().AsString().c_str() << endl;
    }
    else
      kdDebug(5006) << " part " << part << " has no headers" << endl;
  }
}
#else
inline static void debugBodyParts( const char*, const KMMessage& ) {}
#endif


// Add (or overwrite, resp.) an attachment in an existing mail,
// attachments must be local files, they are identified by their names.
// If lookupByName if false the attachment to replace is looked up by mimetype.
// return value: wrong if attachment could not be added/updated
bool KMailICalIfaceImpl::updateAttachment( KMMessage& msg,
                                           const TQString& attachmentURL,
                                           const TQString& attachmentName,
                                           const TQString& attachmentMimetype,
                                           bool lookupByName )
{
  kdDebug(5006) << "KMailICalIfaceImpl::updateAttachment( " << attachmentURL << " )" << endl;

  bool bOK = false;

  KURL url( attachmentURL );
  if ( url.isValid() && url.isLocalFile() ) {
    const TQString fileName( url.path() );
    TQFile file( fileName );
    if( file.open( IO_ReadOnly ) ) {
      TQByteArray rawData = file.readAll();
      file.close();

      // create the new message part with data read from temp file
      KMMessagePart msgPart;
      msgPart.setName( attachmentName );

      const int iSlash = attachmentMimetype.find('/');
      const TQCString sType    = attachmentMimetype.left( iSlash   ).latin1();
      const TQCString sSubtype = attachmentMimetype.mid(  iSlash+1 ).latin1();
      msgPart.setTypeStr( sType );
      msgPart.setSubtypeStr( sSubtype );
      TQCString ctd("attachment;\n  filename=\"");
      ctd.append( attachmentName.latin1() );
      ctd.append("\"");
      msgPart.setContentDisposition( ctd );
      TQValueList<int> dummy;
      msgPart.setBodyAndGuessCte( rawData, dummy );
      msgPart.setPartSpecifier( fileName );

      DwBodyPart* newPart = msg.createDWBodyPart( &msgPart );
      // This whole method is a bit special. We mix code for writing and code for reading.
      // E.g. we need to parse the content-disposition again for ContentDisposition().Filename()
      // to work later on.
      newPart->Headers().ContentDisposition().Parse();

      DwBodyPart* part = lookupByName ? findBodyPart( msg, attachmentName )
                         : findBodyPartByMimeType( msg, sType, sSubtype );
      if ( part ) {
        // Make sure the replacing body part is pointing
        // to the same next part as the original body part.
        newPart->SetNext( part->Next() );
        // call DwBodyPart::operator =
        // which calls DwEntity::operator =
        *part = *newPart;
        delete newPart;
        msg.setNeedsAssembly();
        kdDebug(5006) << "Attachment " << attachmentName << " updated." << endl;
      } else {
        msg.addDwBodyPart( newPart );
        kdDebug(5006) << "Attachment " << attachmentName << " added." << endl;
      }
      bOK = true;
    }else{
      kdDebug(5006) << "Attachment " << attachmentURL << " can not be read." << endl;
    }
  }else{
    kdDebug(5006) << "Attachment " << attachmentURL << " not a local file." << endl;
  }

  return bOK;
}

// Look for the attachment with the right mimetype
bool KMailICalIfaceImpl::kolabXMLFoundAndDecoded( const KMMessage& msg, const TQString& mimetype, TQString& s )
{
  const int iSlash = mimetype.find('/');
  const TQCString sType    = mimetype.left( iSlash   ).latin1();
  const TQCString sSubtype = mimetype.mid(  iSlash+1 ).latin1();
  DwBodyPart* part = findBodyPartByMimeType( msg, sType, sSubtype, true /* starts with sSubtype, to accept application/x-vnd.kolab.contact.distlist */ );
  if ( part ) {
    KMMessagePart msgPart;
    KMMessage::bodyPart(part, &msgPart);
    s = msgPart.bodyToUnicode( TQTextCodec::codecForName( "utf8" ) );
    return true;
  }
  return false;
}

// Delete an attachment in an existing mail.
// return value: wrong if attachment could not be deleted
//
// This code could be optimized: for now we just replace
// the attachment by an empty dummy attachment since Mimelib
// does not provide an option for deleting attachments yet.
bool KMailICalIfaceImpl::deleteAttachment( KMMessage& msg,
                                           const TQString& attachmentName )
{
  kdDebug(5006) << "KMailICalIfaceImpl::deleteAttachment( " << attachmentName << " )" << endl;

  bool bOK = false;

  // quickly searching for our message part: since Kolab parts are
  // top-level parts we do *not* have to travel into embedded multiparts
  DwBodyPart* part = findBodyPart( msg, attachmentName );
  if ( part ) {
    msg.getTopLevelPart()->Body().RemoveBodyPart( part );
    delete part;
    msg.setNeedsAssembly();
    kdDebug(5006) << "Attachment deleted." << endl;
    bOK = true;
  }

  if( !bOK ){
    kdDebug(5006) << "Attachment " << attachmentName << " not found." << endl;
  }

  return bOK;
}

static void setIcalVcardContentTypeHeader( KMMessage *msg, KMail::FolderContentsType t, KMFolder *folder )
{
  KMAcctCachedImap::GroupwareType groupwareType = KMAcctCachedImap::GroupwareKolab;

  KMFolderCachedImap *imapFolder = dynamic_cast<KMFolderCachedImap*>( folder->storage() );
  if ( imapFolder )
    groupwareType = imapFolder->account()->groupwareType();

  msg->setType( DwMime::kTypeText );
  if ( t == KMail::ContentsTypeCalendar || t == KMail::ContentsTypeTask
      || t == KMail::ContentsTypeJournal ) {
    msg->setSubtype( DwMime::kSubtypeVCal );

    if ( groupwareType == KMAcctCachedImap::GroupwareKolab )
      msg->setHeaderField("Content-Type",
          "text/calendar; method=REQUEST; charset=\"utf-8\"");
    else if ( groupwareType == KMAcctCachedImap::GroupwareScalix )
      msg->setHeaderField("Content-Type",
          "text/calendar; method=PUBLISH; charset=\"UTF-8\"");

  } else if ( t == KMail::ContentsTypeContact ) {
    msg->setSubtype( DwMime::kSubtypeXVCard );
    if ( groupwareType == KMAcctCachedImap::GroupwareKolab )
      msg->setHeaderField( "Content-Type", "Text/X-VCard; charset=\"utf-8\"" );
    else if ( groupwareType == KMAcctCachedImap::GroupwareScalix )
      msg->setHeaderField( "Content-Type", "application/scalix-properties; charset=\"UTF-8\"" );
  } else {
    kdWarning(5006) << k_funcinfo << "Attempt to write non-groupware contents to folder" << endl;
  }
}

static void setXMLContentTypeHeader( KMMessage *msg, const TQString plainTextBody )
{
   // add a first body part to be displayed by all mailer
    // than can NOT display Kolab data: no matter if these
    // mailers are MIME compliant or not
    KMMessagePart firstPart;
    firstPart.setType( DwMime::kTypeText );
    firstPart.setSubtype( DwMime::kSubtypePlain );
    msg->removeHeaderField( "Content-Type" );
    msg->setType( DwMime::kTypeMultipart );
    msg->setSubtype( DwMime::kSubtypeMixed );
    msg->headers().ContentType().CreateBoundary( 0 );
    msg->headers().ContentType().Assemble();
    firstPart.setBodyFromUnicode( plainTextBody );
    msg->addBodyPart( &firstPart );
}

// Store a new entry that was received from the resource
Q_UINT32 KMailICalIfaceImpl::addIncidenceKolab( KMFolder& folder,
                                                const TQString& subject,
                                                const TQString& plainTextBody,
                                                const TQMap<TQCString, TQString>& customHeaders,
                                                const TQStringList& attachmentURLs,
                                                const TQStringList& attachmentNames,
                                                const TQStringList& attachmentMimetypes )
{
  kdDebug(5006) << "KMailICalIfaceImpl::addIncidenceKolab( " << attachmentNames << " )" << endl;

  Q_UINT32 sernum = 0;
  bool bAttachOK = true;

  // Make a new message for the incidence
  KMMessage* msg = new KMMessage();
  msg->initHeader();
  msg->setSubject( subject );
  msg->setAutomaticFields( true );

  TQMap<TQCString, TQString>::ConstIterator ith = customHeaders.begin();
  const TQMap<TQCString, TQString>::ConstIterator ithEnd = customHeaders.end();
  for ( ; ith != ithEnd ; ++ith ) {
    msg->setHeaderField( ith.key(), ith.data() );
  }
  // In case of the ical format, simply add the plain text content with the
  // right content type
  if ( storageFormat( &folder ) == StorageXML ) {
    setXMLContentTypeHeader( msg, plainTextBody );
  } else if ( storageFormat( &folder ) == StorageIcalVcard ) {
    const KMail::FolderContentsType t = folder.storage()->contentsType();
    setIcalVcardContentTypeHeader( msg, t, &folder );
    msg->setBodyEncoded( plainTextBody.utf8() );
  } else {
    kdWarning(5006) << k_funcinfo << "Attempt to write to folder with unknown storage type" << endl;
  }

  Q_ASSERT( attachmentMimetypes.count() == attachmentURLs.count() );
  Q_ASSERT( attachmentNames.count() == attachmentURLs.count() );
  // Add all attachments by reading them from their temp. files
  TQStringList::ConstIterator itmime = attachmentMimetypes.begin();
  TQStringList::ConstIterator iturl = attachmentURLs.begin();
  for( TQStringList::ConstIterator itname = attachmentNames.begin();
       itname != attachmentNames.end()
       && itmime != attachmentMimetypes.end()
       && iturl != attachmentURLs.end();
       ++itname, ++iturl, ++itmime ){
    bool byname = !(*itmime).startsWith( "application/x-vnd.kolab." );
    if( !updateAttachment( *msg, *iturl, *itname, *itmime, byname ) ){
      kdWarning(5006) << "Attachment error, can not add Incidence." << endl;
      bAttachOK = false;
      break;
    }
  }

  if( bAttachOK ){
    // Mark the message as read and store it in the folder
    msg->cleanupHeader();
    //debugBodyParts( "after cleanup", *msg );
    msg->touch();
    if ( folder.addMsg( msg ) == 0 )
      // Message stored
      sernum = msg->getMsgSerNum();
    kdDebug(5006) << "addIncidenceKolab(): Message done and saved. Sernum: "
                  << sernum << endl;

    //debugBodyParts( "after addMsg", *msg );
    addFolderChange( &folder, Contents );
    syncFolder( &folder );
  } else
    kdError(5006) << "addIncidenceKolab(): Message *NOT* saved!\n";

  return sernum;
}

bool KMailICalIfaceImpl::deleteIncidenceKolab( const TQString& resource,
                                               Q_UINT32 sernum )
{
  // Find the message from the serial number and delete it.
  if( !mUseResourceIMAP )
    return false;

  kdDebug(5006) << "KMailICalIfaceImpl::deleteIncidenceKolab( "
                << resource << ", " << sernum << ")\n";

  // Find the folder
  KMFolder* f = findResourceFolder( resource );
  if( !f ) {
    kdError(5006) << "deleteIncidenceKolab(" << resource << ") : Not an IMAP resource folder" << endl;
    return false;
  }

  bool rc = false;

  KMMessage* msg = findMessageBySerNum( sernum, f );
  if( msg ) {
    // Message found - delete it and return happy
    deleteMsg( msg );
    syncFolder( f );
    rc = true;
  } else {
    kdDebug(5006) << "Message not found, cannot remove serNum " << sernum << endl;
  }
  return rc;
}


int KMailICalIfaceImpl::incidencesKolabCount( const TQString& mimetype,
                                              const TQString& resource )
{
  Q_UNUSED( mimetype ); // honouring that would be too slow...

  if( !mUseResourceIMAP )
    return 0;

  KMFolder* f = findResourceFolder( resource );
  if( !f ) {
    kdError(5006) << "incidencesKolab(" << resource << ") : Not an IMAP resource folder" << endl;
    return 0;
  }

  f->open("kolabcount");
  int n = f->count();
  f->close("kolabcount");
  kdDebug(5006) << "KMailICalIfaceImpl::incidencesKolabCount( "
                << resource << " ) returned " << n << endl;
  return n;
}

TQMap<Q_UINT32, TQString> KMailICalIfaceImpl::incidencesKolab( const TQString& mimetype,
                                                             const TQString& resource,
                                                             int startIndex,
                                                             int nbMessages )
{
  /// Get the mimetype attachments from this folder. Returns a
  /// TQMap with serialNumber/attachment pairs.
  /// (serial numbers of the mail are provided for easier later update)

  TQMap<Q_UINT32, TQString> aMap;
  if( !mUseResourceIMAP )
    return aMap;

  KMFolder* f = findResourceFolder( resource );
  if( !f ) {
    kdError(5006) << "incidencesKolab(" << resource << ") : Not an IMAP resource folder" << endl;
    return aMap;
  }

  f->open( "incidences" );

  kdDebug(5006) << k_funcinfo << "Getting incidences (" << mimetype << ") for folder " << f->label()
                << ", starting with index " << startIndex << ", " << nbMessages << " messages." << endl;
  kdDebug(5006) << "The folder has " << f->count() << " messages." << endl;

  int stopIndex = nbMessages == -1 ? f->count() :
                  QMIN( f->count(), startIndex + nbMessages );

  for(int i = startIndex; i < stopIndex; ++i) {
#if 0
    bool unget = !f->isMessage(i);
    KMMessage* msg = f->getMsg( i );
#else // faster
    KMMessage* msg = f->storage()->readTemporaryMsg(i);
#endif
    if ( msg ) {
      const int iSlash = mimetype.find('/');
      const TQCString sType    = mimetype.left( iSlash   ).latin1();
      const TQCString sSubtype = mimetype.mid(  iSlash+1 ).latin1();
      if ( sType.isEmpty() || sSubtype.isEmpty() ) {
        kdError(5006) << mimetype << " not an type/subtype combination" << endl;
      } else {
        DwBodyPart* dwPart = findBodyPartByMimeType( *msg, sType, sSubtype );
        if ( dwPart ) {
          KMMessagePart msgPart;
          KMMessage::bodyPart(dwPart, &msgPart);
          aMap.insert(msg->getMsgSerNum(), msgPart.bodyToUnicode( TQTextCodec::codecForName( "utf8" ) ));
        } else {
          // Check if the whole message has the right types. This is what
          // happens in the case of ical storage, where the whole mail is
          // the data
          const TQCString type( msg->typeStr() );
          const TQCString subtype( msg->subtypeStr() );
          if (type.lower() == sType && subtype.lower() == sSubtype ) {
            aMap.insert( msg->getMsgSerNum(), msg->bodyToUnicode() );
          }
          // This is *not* an error: it may be that not all of the messages
          // have a message part that is matching the wanted MIME type
        }
      }
#if 0
      if( unget ) f->unGetMsg(i);
#else
      delete msg;
#endif
    } else {
      kdDebug(5006) << k_funcinfo << " Unable to retrieve message " << i << " for incidence!" << endl;
    }
  }
  f->close( "incidences" );
  return aMap;
}


/* Called when a message that was downloaded from an online imap folder
 * arrives. Needed when listing incidences on online account folders. */
// TODO: Till, port me
void KMailICalIfaceImpl::slotMessageRetrieved( KMMessage* msg )
{
  if( !msg ) return;

  KMFolder *parent = msg->parent();
  Q_ASSERT( parent );
  Q_UINT32 sernum = msg->getMsgSerNum();

  // do we have an accumulator for this folder?
  Accumulator *ac = mAccumulators.find( parent->location() );
  if( ac ) {
    TQString s;
    if ( !vPartFoundAndDecoded( msg, s ) ) return;
    TQString uid( "UID" );
    vPartMicroParser( s, uid );
    const Q_UINT32 sernum = msg->getMsgSerNum();
    mUIDToSerNum.insert( uid, sernum );
    ac->add( s );
    if( ac->isFull() ) {
      /* if this was the last one we were waiting for, tell the resource
       * about the new incidences and clean up. */
      //asyncLoadResult( ac->incidences, ac->type, ac->folder );
      mAccumulators.remove( ac->folder ); // autodelete
    }
  } else {
    /* We are not accumulating for this folder, so this one was added
     * by KMail. Do your thang. */
     slotIncidenceAdded( msg->parent(), msg->getMsgSerNum() );
  }

  if ( mTheUnGetMes.contains( sernum ) ) {
    mTheUnGetMes.remove( sernum );
    int i = 0;
    KMFolder* folder = 0;
    KMMsgDict::instance()->getLocation( sernum, &folder, &i );
    folder->unGetMsg( i );
  }
}

static int dimapAccountCount()
{
  KMail::AccountManager *mgr = kmkernel->acctMgr();
  KMAccount *account = mgr->first();
  int count = 0;
  while ( account ) {
    if ( dynamic_cast<KMAcctCachedImap*>( account ) )
      ++count;
    account = mgr->next();
  }
  return count;
}

int KMailICalIfaceImpl::dimapAccounts()
{
  return dimapAccountCount();
}

static TQString subresourceLabelForPresentation( const KMFolder * folder )
{
    if( KMailICalIfaceImpl::getResourceMap()->contains( folder->location() ) ) {
        return folder->label();
    }

    TQString label = folder->prettyURL();
    TQStringList parts = TQStringList::split( TQString::fromLatin1("/"), label );

    // In the common special case of some other user's folder shared with us
    // the url looks like "Server Name/user/$USERNAME/Folder/Name". Make
    // those a bit nicer.
    if ( parts[1] == TQString::fromLatin1("user") ) {
        TQStringList remainder(parts);
        remainder.pop_front();
        remainder.pop_front();
        remainder.pop_front();
        label = i18n("%1's %2")
            .arg( parts[2] )
            .arg( remainder.join( TQString::fromLatin1("/") ) );
    }
    // Another special case is our own folders, under the imap INBOX, make
    // those prettier too
    const KMFolder *parent = folder;
    while ( parent->parent() && parent->parent()->owner() ) {
      parent = parent->parent()->owner();
      if ( parent->isSystemFolder() ) {
        TQStringList remainder(parts);
        remainder.pop_front();
        remainder.pop_front();
        if ( dimapAccountCount() > 1 ) {
          // Fix kolab issue 2531 folder->storage() )->account() can be null
          if( folder->storage() && static_cast<const KMFolderCachedImap*>( folder->storage() )->account() ) {
          label = i18n( "My %1 (%2)")
              .arg( remainder.join( TQString::fromLatin1("/") ),
                    static_cast<const KMFolderCachedImap*>( folder->storage() )->account()->name() );
          } else {
            label = i18n("My %1")
              .arg( remainder.join( TQString::fromLatin1("/") ) );
          }
        } else {
          label = i18n("My %1")
              .arg( remainder.join( TQString::fromLatin1("/") ) );
        }
        break;
      }
    }
    return label;
}

/* list all available subresources */
TQValueList<KMailICalIfaceImpl::SubResource> KMailICalIfaceImpl::subresourcesKolab( const TQString& contentsType )
{
  TQValueList<SubResource> subResources;

  // Add the default one
  KMFolder* f = folderFromType( contentsType, TQString::null );
  if ( f ) {
    subResources.append( SubResource( f->location(), subresourceLabelForPresentation( f ),
                                      f->isWritable(), folderIsAlarmRelevant( f ) ) );
    kdDebug(5006) << "Adding(1) folder " << f->location() << "    " <<
      ( !f->isWritable() ? "readonly" : "" ) << endl;
  }

  // get the extra ones
  const KMail::FolderContentsType t = folderContentsType( contentsType );
  TQDictIterator<ExtraFolder> it( mExtraFolders );
  for ( ; it.current(); ++it ){
    f = it.current()->folder;
    if ( f && f->storage()->contentsType() == t ) {
      subResources.append( SubResource( f->location(), subresourceLabelForPresentation( f ),
                                        f->isWritable(), folderIsAlarmRelevant( f ) ) );
      kdDebug(5006) << "Adding(2) folder " << f->location() << "     " <<
              ( !f->isWritable() ? "readonly" : "" ) << endl;
    }
  }

  if ( subResources.isEmpty() )
    kdDebug(5006) << "subresourcesKolab: No folder found for " << contentsType << endl;
  return subResources;
}

bool KMailICalIfaceImpl::triggerSync( const TQString& contentsType )
{
  kdDebug(5006) << k_funcinfo << endl;
  TQValueList<KMailICalIfaceImpl::SubResource> folderList = subresourcesKolab( contentsType );
  for ( TQValueList<KMailICalIfaceImpl::SubResource>::const_iterator it( folderList.begin() ),
                                                                    end( folderList.end() );
        it != end ; ++it ) {
    KMFolder * const f = findResourceFolder( (*it).location );
    if ( !f ) continue;
    if ( f->folderType() == KMFolderTypeImap || f->folderType() == KMFolderTypeCachedImap ) {
      if ( !kmkernel->askToGoOnline() ) {
        return false;
      }
    }

    if ( f->folderType() == KMFolderTypeImap ) {
      KMFolderImap *imap = static_cast<KMFolderImap*>( f->storage() );
      imap->getAndCheckFolder();
    } else if ( f->folderType() == KMFolderTypeCachedImap ) {
      KMFolderCachedImap* cached = static_cast<KMFolderCachedImap*>( f->storage() );
      if ( cached->account() ) {
        cached->account()->processNewMailInFolder( f );
      }
    }
  }
  return true;
}

/* Used by the resource to query whether folders are writable. */
bool KMailICalIfaceImpl::isWritableFolder( const TQString& type,
                                           const TQString& resource )
{
  KMFolder* f = folderFromType( type, resource );
  if ( !f )
    // Definitely not writable
    return false;

  return f->isWritable();
}

/* Used by the resource to query the storage format of the folder. */
KMailICalIfaceImpl::StorageFormat KMailICalIfaceImpl::storageFormat( const TQString& resource )
{
  StorageFormat format;
  KMFolder* f = findResourceFolder( resource );
  if ( f )
    format = storageFormat( f );
  else
    format = globalStorageFormat();
  return format;
}

/**
  // This finds the message with serial number "sernum", sets the
  // xml attachments to hold the contents of "xml", and updates all
  // attachments.
  // The mail can have additional attachments, and these are not
  // touched!  They belong to other clients - like Outlook
  // So we delete all the attachments listed in the
  // "deletedAttachments" arg, and then update/add all the attachments
  // given by the urllist attachments.

  // If the mail does not already exist, id will not be a valid serial
  // number, and the mail is just added instead. In this case
  // the deletedAttachments can be forgotten.
*/
Q_UINT32 KMailICalIfaceImpl::update( const TQString& resource,
                                     Q_UINT32 sernum,
                                     const TQString& subject,
                                     const TQString& plainTextBody,
                                     const TQMap<TQCString, TQString>& customHeaders,
                                     const TQStringList& attachmentURLs,
                                     const TQStringList& attachmentMimetypes,
                                     const TQStringList& attachmentNames,
                                     const TQStringList& deletedAttachments )
{
  Q_UINT32 rc = 0;

   if( !mUseResourceIMAP )
    return rc;

  Q_ASSERT( !resource.isEmpty() );

  kdDebug(5006) << "KMailICalIfaceImpl::update( " << resource << ", " << sernum << " )\n";
  kdDebug(5006) << attachmentURLs << "\n";
  kdDebug(5006) << attachmentMimetypes << "\n";
  kdDebug(5006) << attachmentNames << "\n";
  kdDebug(5006) << "deleted attachments:" << deletedAttachments << "\n";

  // Find the folder
  KMFolder* f = findResourceFolder( resource );
  if( !f ) {
    kdError(5006) << "update(" << resource << ") : Not an IMAP resource folder" << endl;
    return rc;
  }

  f->open( "ifaceupdate" );

  KMMessage* msg = 0;
  if ( sernum != 0 ) {
    msg = findMessageBySerNum( sernum, f );
    if ( !msg ) return 0;
    // Message found - make a copy and update it:
    KMMessage* newMsg = new KMMessage( *msg );
    newMsg->setSubject( subject );
    TQMap<TQCString, TQString>::ConstIterator ith = customHeaders.begin();
    const TQMap<TQCString, TQString>::ConstIterator ithEnd = customHeaders.begin();
    for ( ; ith != ithEnd ; ++ith )
      newMsg->setHeaderField( ith.key(), ith.data() );
    newMsg->setParent( 0 ); // workaround strange line in KMMsgBase::assign. newMsg is not in any folder yet.
    // Note that plainTextBody isn't used in this branch. We assume it's still valid from when the mail was created.

    // Delete some attachments according to list
    for( TQStringList::ConstIterator it = deletedAttachments.begin();
         it != deletedAttachments.end();
         ++it ){
      if( !deleteAttachment( *newMsg, *it ) ){
        // Note: It is _not_ an error if an attachment was already deleted.
      }
    }

    const KMail::FolderContentsType t = f->storage()->contentsType();
    const TQCString type = msg->typeStr();
    const TQCString subtype = msg->subtypeStr();
    const bool messageWasIcalVcardFormat = ( type.lower() == "text" &&
        ( subtype.lower() == "calendar" || subtype.lower() == "x-vcard" ) );

    if ( storageFormat( f ) == StorageIcalVcard ) {
      //kdDebug(5006) << k_funcinfo << " StorageFormatIcalVcard " << endl;
      if ( !messageWasIcalVcardFormat ) {
        setIcalVcardContentTypeHeader( newMsg, t, f );
      }
      newMsg->setBodyEncoded( plainTextBody.utf8() );
    } else if ( storageFormat( f ) == StorageXML ) {
      if ( messageWasIcalVcardFormat ) {
        // this was originally an ical event, but the folder changed to xml,
        // convert
       setXMLContentTypeHeader( newMsg, plainTextBody );
      }
      //kdDebug(5006) << k_funcinfo << " StorageFormatXML " << endl;
      // Add all attachments by reading them from their temp. files
      TQStringList::ConstIterator iturl = attachmentURLs.begin();
      TQStringList::ConstIterator itmime = attachmentMimetypes.begin();
      TQStringList::ConstIterator itname = attachmentNames.begin();
      for( ;
          iturl != attachmentURLs.end()
          && itmime != attachmentMimetypes.end()
          && itname != attachmentNames.end();
          ++iturl, ++itname, ++itmime ){
        bool byname = !(*itmime).startsWith( "application/x-vnd.kolab." );
        if( !updateAttachment( *newMsg, *iturl, *itname, *itmime, byname ) ){
          kdDebug(5006) << "Attachment error, can not update attachment " << *iturl << endl;
          break;
        }
      }
    }

    //debugBodyParts( "in update, before cleanup", *newMsg );

    // This is necessary for the headers to be readable later on
    newMsg->cleanupHeader();

    //debugBodyParts( "in update, after cleanup", *newMsg );

    deleteMsg( msg );
    if ( f->addMsg( newMsg ) == 0 ) {
      // Message stored
      rc = newMsg->getMsgSerNum();
      kdDebug(5006) << "forget about " << sernum << ", it's " << rc << " now" << endl;
    }
    addFolderChange( f, Contents );
    syncFolder( f );
  } else {
    // Message not found - store it newly
    rc = addIncidenceKolab( *f, subject, plainTextBody, customHeaders,
                            attachmentURLs,
                            attachmentNames,
                            attachmentMimetypes );
  }

  f->close("ifaceupdate");
  return rc;
}

KURL KMailICalIfaceImpl::getAttachment( const TQString& resource,
                                        Q_UINT32 sernum,
                                        const TQString& filename )
{
  // This finds the attachment with the filename, saves it to a
  // temp file and returns a URL to it. It's up to the resource
  // to delete the tmp file later.
  if( !mUseResourceIMAP )
    return KURL();

  kdDebug(5006) << "KMailICalIfaceImpl::getAttachment( "
                << resource << ", " << sernum << ", " << filename << " )\n";

  // Find the folder
  KMFolder* f = findResourceFolder( resource );
  if( !f ) {
    kdError(5006) << "getAttachment(" << resource << ") : Not an IMAP resource folder" << endl;
    return KURL();
  }
  if ( storageFormat( f ) != StorageXML ) {
    kdError(5006) << "getAttachment(" << resource << ") : Folder has wrong storage format " << storageFormat( f ) << endl;
    return KURL();
  }

  KURL url;

  bool bOK = false;
  bool quiet = mResourceQuiet;
  mResourceQuiet = true;

  KMMessage* msg = findMessageBySerNum( sernum, f );
  if( msg ) {
    // Message found - look for the attachment:

    DwBodyPart* part = findBodyPart( *msg, filename );
    if ( part ) {
      // Save the contents of the attachment.
      KMMessagePart aPart;
      msg->bodyPart( part, &aPart );
      TQByteArray rawData( aPart.bodyDecodedBinary() );

      KTempFile file;
      file.file()->writeBlock( rawData.data(), rawData.size() );

      url.setPath( file.name() );

      bOK = true;
    }

    if( !bOK ){
      kdDebug(5006) << "Attachment " << filename << " not found." << endl;
    }
  }else{
    kdDebug(5006) << "Message not found." << endl;
  }

  mResourceQuiet = quiet;
  return url;
}

TQString KMailICalIfaceImpl::attachmentMimetype( const TQString & resource,
                                                Q_UINT32 sernum,
                                                const TQString & filename )
{
  if( !mUseResourceIMAP )
    return TQString();
  KMFolder* f = findResourceFolder( resource );
  if( !f || storageFormat( f ) != StorageXML ) {
    kdError(5006) << "attachmentMimetype(" << resource << ") : Wrong folder" << endl;
    return TQString();
  }

  KMMessage* msg = findMessageBySerNum( sernum, f );
  if( msg ) {
    // Message found - look for the attachment:
    DwBodyPart* part = findBodyPart( *msg, filename );
    if ( part ) {
      KMMessagePart kmPart;
      msg->bodyPart( part, &kmPart );
      return TQString( kmPart.typeStr() ) + "/" + TQString( kmPart.subtypeStr() );
    } else {
      kdDebug(5006) << "Attachment " << filename << " not found." << endl;
    }
  } else {
    kdDebug(5006) << "Message not found." << endl;
  }

  return TQString();
}

TQStringList KMailICalIfaceImpl::listAttachments(const TQString & resource, Q_UINT32 sernum)
{
  TQStringList rv;
  if( !mUseResourceIMAP )
    return rv;

  // Find the folder
  KMFolder* f = findResourceFolder( resource );
  if( !f ) {
    kdError(5006) << "listAttachments(" << resource << ") : Not an IMAP resource folder" << endl;
    return rv;
  }
  if ( storageFormat( f ) != StorageXML ) {
    kdError(5006) << "listAttachment(" << resource << ") : Folder has wrong storage format " << storageFormat( f ) << endl;
    return rv;
  }

  KMMessage* msg = findMessageBySerNum( sernum, f );
  if( msg ) {
    for ( DwBodyPart* part = msg->getFirstDwBodyPart(); part; part = part->Next() ) {
      if ( part->hasHeaders() ) {
        TQString name;
        DwMediaType& contentType = part->Headers().ContentType();
        if ( TQString( contentType.SubtypeStr().c_str() ).startsWith( "x-vnd.kolab." )
           || TQString( contentType.SubtypeStr().c_str() ).contains( "tnef" ) )
          continue;
        if ( !part->Headers().ContentDisposition().Filename().empty() )
          name = part->Headers().ContentDisposition().Filename().c_str();
        else if ( !contentType.Name().empty() )
          name = contentType.Name().c_str();
        if ( !name.isEmpty() )
          rv.append( name );
      }
    }
  } else {
    kdDebug(5006) << "Message not found." << endl;
  }

  return rv;
}


// ============================================================================

/* KMail part of the interface. These slots are connected to the resource
 * folders and inform us of folders or incidences in them changing, being
 * added or going away. */

void KMailICalIfaceImpl::slotFolderRemoved( KMFolder* folder )
{
  // pretend the folder just changed back to the mail type, which
  // does the right thing, namely remove resource
  folderContentsTypeChanged( folder, KMail::ContentsTypeMail );
  KConfigGroup configGroup( kmkernel->config(), "GroupwareFolderInfo" );
  configGroup.deleteEntry( folder->idString() + "-storageFormat" );
  configGroup.deleteEntry( folder->idString() + "-changes" );
}

// KMail added a file to one of the groupware folders
void KMailICalIfaceImpl::slotIncidenceAdded( KMFolder* folder,
                                             Q_UINT32 sernum )
{
  if( mResourceQuiet || !mUseResourceIMAP )
    return;

//  kdDebug(5006) << "KMailICalIfaceImpl::slotIncidenceAdded" << endl;
  TQString type = folderContentsType( folder->storage()->contentsType() );
  if( type.isEmpty() ) {
    kdError(5006) << "Not an IMAP resource folder" << endl;
    return;
  }
  // Get the index of the mail
  int i = 0;
  KMFolder* aFolder = 0;
  KMMsgDict::instance()->getLocation( sernum, &aFolder, &i );
  assert( folder == aFolder );

  bool unget = !folder->isMessage( i );
  TQString s;
  TQString uid( "UID" );
  KMMessage *msg = folder->getMsg( i );
  if( !msg ) return;
  if( msg->isComplete() ) {

    bool ok = false;
    StorageFormat format = storageFormat( folder );
    switch( format ) {
      case StorageIcalVcard:
        // Read the iCal or vCard
        ok = vPartFoundAndDecoded( msg, s );
        if ( ok )
          vPartMicroParser( s, uid );
        break;
      case StorageXML:
        // Read the XML from the attachment with the given mimetype
        if ( kolabXMLFoundAndDecoded( *msg,
              folderKolabMimeType( folder->storage()->contentsType() ), s ) ) {
          uid = msg->subject();
          ok = true;
        }
        break;
    }
    if ( !ok ) {
      if ( unget )
        folder->unGetMsg( i );
      return;
    }
    const Q_UINT32 sernum = msg->getMsgSerNum();
    mUIDToSerNum.insert( uid, sernum );

    // tell the resource if we didn't trigger this ourselves
    if ( mInTransit.contains( uid ) ) {
      mInTransit.remove( uid );
    }
    incidenceAdded( type, folder->location(), sernum, format, s );
  } else {
    // go get the rest of it, then try again
    // TODO: Till, port me
    if ( unget ) mTheUnGetMes.insert( msg->getMsgSerNum(), true );
    FolderJob *job = msg->parent()->createJob( msg );
    connect( job, TQT_SIGNAL( messageRetrieved( KMMessage* ) ),
        this, TQT_SLOT( slotMessageRetrieved( KMMessage* ) ) );
    job->start();
    return;
  }
  if( unget ) folder->unGetMsg(i);
}

// KMail deleted a file
void KMailICalIfaceImpl::slotIncidenceDeleted( KMFolder* folder,
                                               Q_UINT32 sernum )
{
  if( mResourceQuiet || !mUseResourceIMAP )
    return;

  TQString type = folderContentsType( folder->storage()->contentsType() );
  //kdDebug(5006) << folder << " " << type << " " << sernum << endl;
  if( !type.isEmpty() ) {
    // Get the index of the mail
    int i = 0;
    KMFolder* aFolder = 0;
    KMMsgDict::instance()->getLocation( sernum, &aFolder, &i );
    assert( folder == aFolder );

    // Read the iCal or vCard
    bool unget = !folder->isMessage( i );
    TQString s;
    bool ok = false;
    KMMessage* msg = folder->getMsg( i );
    TQString uid( "UID" );
    switch( storageFormat( folder ) ) {
    case StorageIcalVcard:
        if( vPartFoundAndDecoded( msg, s ) ) {
            vPartMicroParser( s, uid );
            ok = true;
        }
        break;
    case StorageXML:
        if ( kolabXMLFoundAndDecoded( *msg, folderKolabMimeType( folder->storage()->contentsType() ), s ) ) {
          uid = msg->subject();
          ok = true;
        }
        break;
    }
    if ( ok ) {
        kdDebug(5006) << "Emitting DCOP signal incidenceDeleted( "
                      << type << ", " << folder->location() << ", " << uid
                      << " )" << endl;
        incidenceDeleted( type, folder->location(), uid );
    }
    if( unget ) folder->unGetMsg(i);
  } else
    kdError(5006) << "Not a groupware folder" << endl;
}

// KMail orders a refresh
void KMailICalIfaceImpl::slotRefresh( const TQString& type )
{
  if( mUseResourceIMAP ) {
    signalRefresh( type, TQString::null /* PENDING(bo) folder->location() */ );
    kdDebug(5006) << "Emitting DCOP signal signalRefresh( " << type << " )" << endl;
  }
}

// This is among other things called when an expunge of a folder happens
void KMailICalIfaceImpl::slotRefreshFolder( KMFolder* folder)
{
  // TODO: The resources would of course be better off, if only this
  // folder would need refreshing. Currently it just orders a reload of
  // the type of the folder
  if( mUseResourceIMAP && folder ) {
    if( folder == mCalendar || folder == mContacts
        || folder == mNotes || folder == mTasks
        || folder == mJournals || mExtraFolders.find( folder->location() ) ) {
      // Refresh the folder of this type
      KMail::FolderContentsType ct = folder->storage()->contentsType();
      slotRefresh( s_folderContentsType[ct].contentsTypeStr );
    }
  }
}

/****************************
 * The folder and message stuff code
 */

KMFolder* KMailICalIfaceImpl::folderFromType( const TQString& type,
                                              const TQString& folder )
{
  if( mUseResourceIMAP ) {
    KMFolder* f = 0;
    if ( !folder.isEmpty() ) {
      f = extraFolder( type, folder );
      if ( f )
        return f;
    }

    if( type == "Calendar" ) f = mCalendar;
    else if( type == "Contact" ) f = mContacts;
    else if( type == "Note" ) f = mNotes;
    else if( type == "Task" || type == "Todo" ) f = mTasks;
    else if( type == "Journal" ) f = mJournals;

    if ( f && ( folder.isEmpty() || folder == f->location() ) )
      return f;

    kdError(5006) << "No folder ( " << type << ", " << folder << " )\n";
  }

  return 0;
}


// Returns true if folder is a resource folder. If the resource isn't enabled
// this always returns false
bool KMailICalIfaceImpl::isResourceFolder( KMFolder* folder ) const
{
  return mUseResourceIMAP && folder &&
    ( isStandardResourceFolder( folder ) || mExtraFolders.find( folder->location() )!=0 );
}

bool KMailICalIfaceImpl::isStandardResourceFolder( KMFolder* folder ) const
{
  return ( folder == mCalendar || folder == mTasks || folder == mJournals ||
           folder == mNotes || folder == mContacts );
}

bool KMailICalIfaceImpl::hideResourceFolder( KMFolder* folder ) const
{
  return mHideFolders && isResourceFolder( folder );
}

bool KMailICalIfaceImpl::hideResourceAccountRoot( KMFolder* folder ) const
{
  KMFolderCachedImap *dimapFolder = dynamic_cast<KMFolderCachedImap*>( folder->storage() );
  bool hide = dimapFolder && mHideFolders
       && (int)dimapFolder->account()->id() == GlobalSettings::self()->theIMAPResourceAccount()
       && GlobalSettings::self()->showOnlyGroupwareFoldersForGroupwareAccount();
  return hide;

}

KFolderTreeItem::Type KMailICalIfaceImpl::folderType( KMFolder* folder ) const
{
  if( mUseResourceIMAP && folder ) {
    if( folder == mCalendar || folder == mContacts
        || folder == mNotes || folder == mTasks
        || folder == mJournals || mExtraFolders.find( folder->location() ) ) {
      KMail::FolderContentsType ct = folder->storage()->contentsType();
      return s_folderContentsType[ct].treeItemType;
    }
  }

  return KFolderTreeItem::Other;
}

// Global tables of foldernames is different languages
// For now: 0->English, 1->German, 2->French, 3->Dutch
static TQMap<KFolderTreeItem::Type,TQString> folderNames[4];
TQString KMailICalIfaceImpl::folderName( KFolderTreeItem::Type type, int language ) const
{
  // With the XML storage, folders are always (internally) named in English
  if ( GlobalSettings::self()->theIMAPResourceStorageFormat() == GlobalSettings::EnumTheIMAPResourceStorageFormat::XML )
    language = 0;

  static bool folderNamesSet = false;
  if( !folderNamesSet ) {
    folderNamesSet = true;
    /* NOTE: If you add something here, you also need to update
       GroupwarePage in configuredialog.cpp */

    // English
    folderNames[0][KFolderTreeItem::Calendar] = TQString::fromLatin1("Calendar");
    folderNames[0][KFolderTreeItem::Tasks] = TQString::fromLatin1("Tasks");
    folderNames[0][KFolderTreeItem::Journals] = TQString::fromLatin1("Journal");
    folderNames[0][KFolderTreeItem::Contacts] = TQString::fromLatin1("Contacts");
    folderNames[0][KFolderTreeItem::Notes] = TQString::fromLatin1("Notes");

    // German
    folderNames[1][KFolderTreeItem::Calendar] = TQString::fromLatin1("Kalender");
    folderNames[1][KFolderTreeItem::Tasks] = TQString::fromLatin1("Aufgaben");
    folderNames[1][KFolderTreeItem::Journals] = TQString::fromLatin1("Journal");
    folderNames[1][KFolderTreeItem::Contacts] = TQString::fromLatin1("Kontakte");
    folderNames[1][KFolderTreeItem::Notes] = TQString::fromLatin1("Notizen");

    // French
    folderNames[2][KFolderTreeItem::Calendar] = TQString::fromLatin1("Calendrier");
    // Tasks = Tâches (â == 0xE2 in latin1)
    folderNames[2][KFolderTreeItem::Tasks] = TQString::fromLatin1("T\342ches");
    folderNames[2][KFolderTreeItem::Journals] = TQString::fromLatin1("Journal");
    folderNames[2][KFolderTreeItem::Contacts] = TQString::fromLatin1("Contacts");
    folderNames[2][KFolderTreeItem::Notes] = TQString::fromLatin1("Notes");

    // Dutch
    folderNames[3][KFolderTreeItem::Calendar] = TQString::fromLatin1("Agenda");
    folderNames[3][KFolderTreeItem::Tasks] = TQString::fromLatin1("Taken");
    folderNames[3][KFolderTreeItem::Journals] = TQString::fromLatin1("Logboek");
    folderNames[3][KFolderTreeItem::Contacts] = TQString::fromLatin1("Contactpersonen");
    folderNames[3][KFolderTreeItem::Notes] = TQString::fromLatin1("Notities");
  }

  if( language < 0 || language > 3 ) {
    return folderNames[mFolderLanguage][type];
  }
  else {
    return folderNames[language][type];
  }
}


// Find message matching a given UID
KMMessage *KMailICalIfaceImpl::findMessageByUID( const TQString& uid, KMFolder* folder )
{
  if( !folder || !mUIDToSerNum.contains( uid ) ) return 0;
  int i;
  KMFolder *aFolder;
  KMMsgDict::instance()->getLocation( mUIDToSerNum[uid], &aFolder, &i );
  Q_ASSERT( aFolder == folder );
  return folder->getMsg( i );
}

// Find message matching a given serial number
KMMessage *KMailICalIfaceImpl::findMessageBySerNum( Q_UINT32 serNum, KMFolder* folder )
{
  if( !folder ) return 0;

  KMMessage *message = 0;
  KMFolder* aFolder = 0;
  int index;
  KMMsgDict::instance()->getLocation( serNum, &aFolder, &index );

  if( aFolder && aFolder != folder ) {
    kdWarning(5006) << "findMessageBySerNum( " << serNum << " ) found it in folder " << aFolder->location() << ", expected " << folder->location() << endl;
  } else {
    if( aFolder )
      message = aFolder->getMsg( index );
    if (!message)
      kdWarning(5006) << "findMessageBySerNum( " << serNum << " ) invalid serial number\n" << endl;
  }
  return message;
}

void KMailICalIfaceImpl::deleteMsg( KMMessage *msg )
{
  if( !msg ) return;
  // Commands are now delayed; can't use that anymore, we need immediate deletion
  //( new KMDeleteMsgCommand( msg->parent(), msg ) )->start();
  KMFolder *srcFolder = msg->parent();
  int idx = srcFolder->find(msg);
  assert(idx != -1);
  // kill existing jobs since we are about to delete the message
  srcFolder->ignoreJobsForMessage( msg );
  if ( !msg->transferInProgress() ) {
    srcFolder->removeMsg(idx);
    delete msg;
  } else {
    kdDebug(5006) << k_funcinfo << "Message cannot be deleted now because it is currently in use " << msg << endl;
    msg->deleteWhenUnused();
  }
  addFolderChange( srcFolder, Contents );
}

void KMailICalIfaceImpl::folderContentsTypeChanged( KMFolder* folder,
                                                    KMail::FolderContentsType contentsType )
{
  if ( !mUseResourceIMAP )
    return;
//  kdDebug(5006) << "folderContentsTypeChanged( " << folder->name()
//                << ", " << contentsType << ")\n";

  // The builtins can't change type
  if ( isStandardResourceFolder( folder ) )
    return;

  // Check if already know that 'extra folder'
  const TQString location = folder->location();
  ExtraFolder* ef = mExtraFolders.find( location );
  if ( ef && ef->folder ) {
    // Notify that the old folder resource is no longer available
    subresourceDeleted(folderContentsType( folder->storage()->contentsType() ), location );

    if ( contentsType == KMail::ContentsTypeMail ) {
      // Delete the old entry, stop listening and stop here
      mExtraFolders.remove( location );
      folder->disconnect( this );
      return;
    }
    // So the type changed to another groupware type, ok.
  } else {
    if ( ef && !ef->folder ) // deleted folder, clean up
      mExtraFolders.remove( location );
    if ( contentsType == KMail::ContentsTypeMail )
        return;

    //kdDebug(5006) << "registering " << location << " as extra folder" << endl;
    // Make a new entry for the list
    ef = new ExtraFolder( folder );
    mExtraFolders.insert( location, ef );

    FolderInfo info = readFolderInfo( folder );
    mFolderInfoMap.insert( folder, info );

    // Adjust the folder names of all foo.default folders.
    // German users will get Kalender as the name of all default Calendar folders,
    // including their own, so that the default calendar folder of their Japanese
    // coworker appears as /user/hirohito/Kalender, although Hirohito sees his folder
    // in Japanese. On the server the folders are always in English.
    if ( folder->folderType() == KMFolderTypeCachedImap ) {
      TQString annotation = static_cast<KMFolderCachedImap*>( folder->storage() )->annotationFolderType();
      kdDebug(5006) << "folderContentsTypeChanged: " << folder->name() << " has annotation " << annotation << endl;
      if ( annotation == TQString( s_folderContentsType[contentsType].annotation ) + ".default" )
        folder->setLabel( localizedDefaultFolderName( contentsType ) );
    }

    connectFolder( folder );
  }
  // Tell about the new resource
  subresourceAdded( folderContentsType( contentsType ), location, subresourceLabelForPresentation(folder),
                    folder->isWritable(), folderIsAlarmRelevant( folder ) );
}

KMFolder* KMailICalIfaceImpl::extraFolder( const TQString& type,
                                           const TQString& folder )
{
  // If an extra folder exists that matches the type and folder location,
  // use that
  int t = folderContentsType( type );
  if ( t < 1 || t > 5 )
    return 0;

  ExtraFolder* ef = mExtraFolders.find( folder );
  if ( ef && ef->folder && ef->folder->storage()->contentsType() == t )
    return ef->folder;

  return 0;
}

KMailICalIfaceImpl::StorageFormat KMailICalIfaceImpl::storageFormat( KMFolder* folder ) const
{
  FolderInfoMap::ConstIterator it = mFolderInfoMap.find( folder );
  if ( it != mFolderInfoMap.end() )
    return (*it).mStorageFormat;
  return globalStorageFormat();
}

void KMailICalIfaceImpl::setStorageFormat( KMFolder* folder, StorageFormat format )
{
  FolderInfoMap::Iterator it = mFolderInfoMap.find( folder );
  if ( it != mFolderInfoMap.end() ) {
    (*it).mStorageFormat = format;
  } else {
    FolderInfo info( format, NoChange );
    mFolderInfoMap.insert( folder, info );
  }
  KConfigGroup configGroup( kmkernel->config(), "GroupwareFolderInfo" );
  configGroup.writeEntry( folder->idString() + "-storageFormat",
                          format == StorageXML ? "xml" : "icalvcard" );
}

void KMailICalIfaceImpl::addFolderChange( KMFolder* folder, FolderChanges changes )
{
  FolderInfoMap::Iterator it = mFolderInfoMap.find( folder );
  if ( it != mFolderInfoMap.end() ) {
    (*it).mChanges = static_cast<FolderChanges>( (*it).mChanges | changes );
  } else { // Otherwise, well, it's a folder we don't care about.
    kdDebug(5006) << "addFolderChange: nothing known about folder " << folder->location() << endl;
  }
  KConfigGroup configGroup( kmkernel->config(), "GroupwareFolderInfo" );
  configGroup.writeEntry( folder->idString() + "-changes", (*it).mChanges );
}

KMailICalIfaceImpl::FolderInfo KMailICalIfaceImpl::readFolderInfo( const KMFolder * const folder ) const
{
  KConfigGroup configGroup( kmkernel->config(), "GroupwareFolderInfo" );
  TQString str = configGroup.readEntry( folder->idString() + "-storageFormat", "unset" );
  FolderInfo info;
  if ( str == "unset" ) {
    info.mStorageFormat = globalStorageFormat();
    configGroup.writeEntry( folder->idString() + "-storageFormat",
                            info.mStorageFormat == StorageXML ? "xml" : "icalvcard" );
  } else {
    info.mStorageFormat = ( str == "xml" ) ? StorageXML : StorageIcalVcard;
  }
  info.mChanges = (FolderChanges) configGroup.readNumEntry( folder->idString() + "-changes" );
  return info;
}


void KMailICalIfaceImpl::folderSynced( KMFolder* folder, const KURL& folderURL )
{
  FolderInfoMap::Iterator it = mFolderInfoMap.find( folder );
  if ( it != mFolderInfoMap.end() && (*it).mChanges ) {
    handleFolderSynced( folder, folderURL, (*it).mChanges );
    (*it).mChanges = NoChange;
  }
}

void KMailICalIfaceImpl::handleFolderSynced( KMFolder* folder,
                                             const KURL& folderURL,
                                             int _changes )
{
  // This is done here instead of in the resource, because
  // there could be 0, 1, or N kolab resources at this point.
  // We can hack the N case, but not the 0 case.
  // So the idea of a DCOP signal for this wouldn't work.
  if ( ( _changes & KMailICalIface::Contents ) ||
       ( _changes & KMailICalIface::ACL ) ) {
    if ( storageFormat( folder ) == StorageXML && folder->storage()->contentsType() == KMail::ContentsTypeCalendar )
      triggerKolabFreeBusy( folderURL );
  }
}

void KMailICalIfaceImpl::folderDeletedOnServer( const KURL& folderURL )
{
  triggerKolabFreeBusy( folderURL );
}

void KMailICalIfaceImpl::triggerKolabFreeBusy( const KURL& folderURL )
{
  /* Steffen said: you must issue an authenticated HTTP GET request to
     https://kolabserver/freebusy/trigger/user@domain/Folder/NestedFolder.pfb
     (replace .pfb with .xpfb for extended fb lists). */
  KURL httpURL( folderURL );
  // Keep username ("user@domain"), pass, and host from the imap url
  httpURL.setProtocol( "https" );
  httpURL.setPort( 0 ); // remove imap port

  // IMAP path is either /INBOX/<path> or /user/someone/<path>
  TQString path = folderURL.path( -1 );
  Q_ASSERT( path.startsWith( "/" ) );
  int secondSlash = path.find( '/', 1 );
  if ( secondSlash == -1 ) {
    kdWarning() << "KCal::ResourceKolab::fromKMailFolderSynced path is too short: " << path << endl;
    return;
  }
  if ( path.startsWith( "/INBOX/", false ) ) {
    // If INBOX, replace it with the username (which is user@domain)
    path = path.mid( secondSlash );
    path.prepend( folderURL.user() );
  } else {
    // If user, just remove it. So we keep the IMAP-returned username.
    // This assumes it's a known user on the same domain.
    path = path.mid( secondSlash );
  }

  httpURL.setPath( "/freebusy/trigger/" + path + ".pfb" );
  httpURL.setQuery( TQString::null );
  // Ensure that we encode everything with UTF8
  httpURL = KURL( httpURL.url(0,106), 106 );
  kdDebug() << "Triggering PFB update for " << folderURL << " : getting " << httpURL << endl;
  // "Fire and forget". No need for error handling, nor for explicit deletion.
  // Maybe we should try to prevent launching it if it's already running (for this URL) though.
  /*KIO::Job* job =*/ KIO::get( httpURL, false, false /*no progress info*/ );
}

void KMailICalIfaceImpl::slotFolderPropertiesChanged( KMFolder* folder )
{
  if ( isResourceFolder( folder ) ) {
    const TQString location = folder->location();
    const TQString contentsTypeStr = folderContentsType( folder->storage()->contentsType() );
    subresourceDeleted( contentsTypeStr, location );

    subresourceAdded( contentsTypeStr, location, subresourceLabelForPresentation( folder ),
                      folder->isWritable(), folderIsAlarmRelevant( folder ) );
  }
}

// Must only be connected to a signal from KMFolder!
void KMailICalIfaceImpl::slotFolderRenamed()
{
  const KMFolder* folder = static_cast<const KMFolder *>( sender() );
  slotFolderPropertiesChanged( const_cast<KMFolder*>( folder ) );
}

void KMailICalIfaceImpl::slotFolderLocationChanged( const TQString &oldLocation,
                                                    const TQString &newLocation )
{
  KMFolder *folder = findResourceFolder( oldLocation );
  ExtraFolder* ef = mExtraFolders.find( oldLocation );
  if ( ef ) {
    // reuse the ExtraFolder entry, but adjust the key
    mExtraFolders.setAutoDelete( false );
    mExtraFolders.remove( oldLocation );
    mExtraFolders.setAutoDelete( true );
    mExtraFolders.insert( newLocation, ef );
  }
  if (  folder )
    subresourceDeleted( folderContentsType(  folder->storage()->contentsType() ), oldLocation );

}

KMFolder* KMailICalIfaceImpl::findResourceFolder( const TQString& resource )
{
  // Try the standard folders
  if( mCalendar && mCalendar->location() == resource )
    return mCalendar;
  if ( mContacts && mContacts->location() == resource )
    return mContacts;
  if ( mNotes && mNotes->location() == resource )
    return mNotes;
  if ( mTasks && mTasks->location() == resource )
    return mTasks;
  if ( mJournals && mJournals->location() == resource )
    return mJournals;

  // No luck. Try the extrafolders
  ExtraFolder* ef = mExtraFolders.find( resource );
  if ( ef )
    return ef->folder;

  // No luck at all
  return 0;
}

void KMailICalIfaceImpl::changeResourceUIName( const TQString &folderPath, const TQString &newName )
{
  kdDebug() << "Folder path " << folderPath << endl;
  KMFolder *f = findResourceFolder( folderPath );
  if ( f ) {
    KMailICalIfaceImpl::getResourceMap()->insert( folderPath, newName );
    kmkernel->folderMgr()->renameFolder( f, newName );
    KConfigGroup configGroup( kmkernel->config(), "Resource UINames" );
    configGroup.writeEntry( folderPath, newName );
  }
}

// Builds a folder list from the dimap and the local folder list.
static void createFolderList( TQStringList &folderNames, TQValueList<TQGuardedPtr<KMFolder> > &folderList )
{
  TQStringList dimapFolderNames;
  TQStringList localFolderNames;
  TQValueList<TQGuardedPtr<KMFolder> > dimapFolderList;
  TQValueList<TQGuardedPtr<KMFolder> > localFolderList;
  kmkernel->dimapFolderMgr()->createFolderList( &dimapFolderNames, &dimapFolderList );
  kmkernel->folderMgr()->createFolderList( &localFolderNames, &localFolderList );
  folderNames += dimapFolderNames;
  folderNames += localFolderNames;
  folderList += dimapFolderList;
  folderList += localFolderList;
}

/****************************
 * The config stuff
 */

void KMailICalIfaceImpl::readConfig()
{
  bool enabled = GlobalSettings::self()->theIMAPResourceEnabled() &&
                 ( GlobalSettings::self()->theIMAPResourceAccount() != 0 );

  if( !enabled ) {
    if( mUseResourceIMAP == true ) {
      // Shutting down
      mUseResourceIMAP = false;
      cleanup();
      reloadFolderTree();
    }
    return;
  }
  mUseResourceIMAP = enabled;

  // Read remaining options
  const bool hideFolders = GlobalSettings::self()->hideGroupwareFolders();
  TQString parentName = GlobalSettings::self()->theIMAPResourceFolderParent();

  // Find the folder parent
  KMFolderDir* folderParentDir;
  KMFolderType folderType;
  KMFolder* folderParent = kmkernel->findFolderById( parentName );
  if( folderParent == 0 ) {
    // Parent folder not found. It was probably deleted. The user will have to
    // configure things again.
    kdDebug(5006) << "Groupware folder " << parentName << " not found. Groupware functionality disabled" << endl;
    // Or maybe the inbox simply wasn't created on the first startup
    KMAccount* account = kmkernel->acctMgr()->find( GlobalSettings::self()->theIMAPResourceAccount() );
    Q_ASSERT( account );
    if ( account ) {
      // just in case we were connected already
      disconnect( account, TQT_SIGNAL( finishedCheck( bool, CheckStatus ) ),
               this, TQT_SLOT( slotCheckDone() ) );
      connect( account, TQT_SIGNAL( finishedCheck( bool, CheckStatus ) ),
               this, TQT_SLOT( slotCheckDone() ) );
    }
    mUseResourceIMAP = false;
    // We can't really call cleanup(), if those folders were completely deleted.
    mCalendar = 0;
    mTasks    = 0;
    mJournals = 0;
    mContacts = 0;
    mNotes    = 0;
    return;
  } else {
    folderParentDir = folderParent->createChildFolder();
    folderType = folderParent->folderType();
  }

  KMAcctCachedImap::GroupwareType groupwareType = dynamic_cast<KMFolderCachedImap *>( folderParent->storage() )->account()->groupwareType();

  if ( groupwareType == KMAcctCachedImap::GroupwareKolab ) {
    // Make sure the folder parent has the subdirs
    // Globally there are 3 cases: nothing found, some stuff found by type/name heuristics, or everything found OK
    bool noneFound = true;
    bool mustFix = false; // true when at least one was found by heuristics
    TQValueVector<StandardFolderSearchResult> results( KMail::ContentsTypeLast + 1 );
    for ( int i = 0; i < KMail::ContentsTypeLast+1; ++i ) {
      if ( i != KMail::ContentsTypeMail ) {
        results[i] = findStandardResourceFolder( folderParentDir, static_cast<KMail::FolderContentsType>(i) );
        if ( results[i].found == StandardFolderSearchResult::FoundAndStandard )
          noneFound = false;
        else if ( results[i].found == StandardFolderSearchResult::FoundByType ||
                  results[i].found == StandardFolderSearchResult::FoundByName ) {
          mustFix = true;
          noneFound = false;
        } else // NotFound
          mustFix = true;
      }
    }

    // Check if something changed
    if( mUseResourceIMAP && !noneFound && !mustFix && mFolderParentDir == folderParentDir
        && mFolderType == folderType ) {
      // Nothing changed
      if ( hideFolders != mHideFolders ) {
        // Well, the folder hiding has changed
        mHideFolders = hideFolders;
        reloadFolderTree();
      }
      return;
    }

    if( noneFound || mustFix ) {
      TQString msg;
      TQString parentFolderName = folderParent != 0 ? folderParent->name() : folderParentDir->name();
      if ( noneFound ) {
        // No subfolder was found, so ask if we can make them
        msg = i18n("KMail will now create the required groupware folders"
                   " as subfolders of %1; if you do not want this, cancel"
                   " and the IMAP resource will be disabled").arg(parentFolderName);
      } else {
        // Some subfolders were found, be more precise
        TQString operations = "<ul>";
        for ( int i = 0; i < KMail::ContentsTypeLast+1; ++i ) {
          if ( i != KMail::ContentsTypeMail ) {
            TQString typeName = localizedDefaultFolderName( static_cast<KMail::FolderContentsType>( i ) );
            if ( results[i].found == StandardFolderSearchResult::NotFound )
              operations += "<li>" + i18n( "%1: no folder found. It will be created." ).arg( typeName ) + "</li>";
            else if ( results[i].found == StandardFolderSearchResult::FoundByType || results[i].found == StandardFolderSearchResult::FoundByName )
              operations += "<li>" + i18n( "%1: found folder %2. It will be set as the main groupware folder." ).
                            arg( typeName ).arg( results[i].folder->label() ) + "</li>";
          }
        }
        operations += "</ul>";

        msg = i18n("<qt>KMail found the following groupware folders in %1 and needs to perform the following operations: %2"
                   "<br>If you do not want this, cancel"
                   " and the IMAP resource will be disabled").arg(parentFolderName, operations);

      }

      if( KMessageBox::questionYesNo( 0, msg,
                                      i18n("Standard Groupware Folders"), KStdGuiItem::cont(), KStdGuiItem::cancel() ) == KMessageBox::No ) {

        GlobalSettings::self()->setTheIMAPResourceEnabled( false );
        mUseResourceIMAP = false;
        mFolderParentDir = 0;
        mFolderParent = 0;
        reloadFolderTree();
        return;
      }
    }

    // Make the new settings work
    mUseResourceIMAP = true;
    mFolderLanguage = GlobalSettings::self()->theIMAPResourceFolderLanguage();
    if( mFolderLanguage > 3 ) mFolderLanguage = 0;
    mFolderParentDir = folderParentDir;
    mFolderParent = folderParent;
    mFolderType = folderType;
    mHideFolders = hideFolders;

    // Close the previous folders
    cleanup();

    // Set the new folders
    mCalendar = initFolder( KMail::ContentsTypeCalendar );
    mTasks    = initFolder( KMail::ContentsTypeTask );
    mJournals = initFolder( KMail::ContentsTypeJournal );
    mContacts = initFolder( KMail::ContentsTypeContact );
    mNotes    = initFolder( KMail::ContentsTypeNote );

    // Store final annotation (with .default) so that we won't ask again on next startup
    if ( mCalendar->folderType() == KMFolderTypeCachedImap )
      static_cast<KMFolderCachedImap *>( mCalendar->storage() )->updateAnnotationFolderType();
    if ( mTasks->folderType() == KMFolderTypeCachedImap )
      static_cast<KMFolderCachedImap *>( mTasks->storage() )->updateAnnotationFolderType();
    if ( mJournals->folderType() == KMFolderTypeCachedImap )
      static_cast<KMFolderCachedImap *>( mJournals->storage() )->updateAnnotationFolderType();
    if ( mContacts->folderType() == KMFolderTypeCachedImap )
      static_cast<KMFolderCachedImap *>( mContacts->storage() )->updateAnnotationFolderType();
    if ( mNotes->folderType() == KMFolderTypeCachedImap )
      static_cast<KMFolderCachedImap *>( mNotes->storage() )->updateAnnotationFolderType();

    //kdDebug(5006) << k_funcinfo << "mCalendar=" << mCalendar << " " << mCalendar->location() << endl;
    //kdDebug(5006) << k_funcinfo << "mContacts=" << mContacts << " " << mContacts->location() << endl;
    //kdDebug(5006) << k_funcinfo << "mNotes=" << mNotes << " " << mNotes->location() << endl;

    // Find all extra folders
    TQStringList folderNames;
    TQValueList<TQGuardedPtr<KMFolder> > folderList;
    createFolderList( folderNames, folderList );
    for( TQValueList<TQGuardedPtr<KMFolder> >::iterator it = folderList.begin();
         it != folderList.end(); ++it )
    {
      FolderStorage *storage = (*it)->storage();
      KMFolderCachedImap* dimapStorage = dynamic_cast<KMFolderCachedImap*>( storage );
      if ( storage && storage->contentsType() != 0 ) {
        if ( dimapStorage )
          dimapStorage->updateAnnotationFolderType();
        folderContentsTypeChanged( *it, storage->contentsType() );
      }
    }

    // If we just created them, they might have been registered as extra folders temporarily.
    // -> undo that.
    mExtraFolders.remove( mCalendar->location() );
    mExtraFolders.remove( mTasks->location() );
    mExtraFolders.remove( mJournals->location() );
    mExtraFolders.remove( mContacts->location() );
    mExtraFolders.remove( mNotes->location() );

    subresourceAdded( folderContentsType( KMail::ContentsTypeCalendar ), mCalendar->location(), mCalendar->label(), true, true );
    subresourceAdded( folderContentsType( KMail::ContentsTypeTask ), mTasks->location(), mTasks->label(), true, true );
    subresourceAdded( folderContentsType( KMail::ContentsTypeJournal ), mJournals->location(), mJournals->label(), true, false );
    subresourceAdded( folderContentsType( KMail::ContentsTypeContact ), mContacts->location(), mContacts->label(), true, false );
    subresourceAdded( folderContentsType( KMail::ContentsTypeNote ), mNotes->location(), mNotes->label(), true, false );
  } else if ( groupwareType == KMAcctCachedImap::GroupwareScalix ) {
    // Make the new settings work
    mUseResourceIMAP = true;
    mFolderParentDir = folderParentDir;
    mFolderParent = folderParent;
    mFolderType = folderType;
    mHideFolders = false;

    // Close the previous folders
    cleanup();

    // Set the new folders
    mCalendar = initScalixFolder( KMail::ContentsTypeCalendar );
    mTasks    = initScalixFolder( KMail::ContentsTypeTask );
    mJournals = 0;
    mContacts = initScalixFolder( KMail::ContentsTypeContact );
    mNotes    = initScalixFolder( KMail::ContentsTypeNote );

    // Store final annotation (with .default) so that we won't ask again on next startup
    if ( mCalendar->folderType() == KMFolderTypeCachedImap )
      static_cast<KMFolderCachedImap *>( mCalendar->storage() )->updateAnnotationFolderType();
    if ( mTasks->folderType() == KMFolderTypeCachedImap )
      static_cast<KMFolderCachedImap *>( mTasks->storage() )->updateAnnotationFolderType();
    if ( mContacts->folderType() == KMFolderTypeCachedImap )
      static_cast<KMFolderCachedImap *>( mContacts->storage() )->updateAnnotationFolderType();
    if ( mNotes->folderType() == KMFolderTypeCachedImap )
      static_cast<KMFolderCachedImap *>( mNotes->storage() )->updateAnnotationFolderType();

    // BEGIN TILL TODO The below only uses the dimap folder manager, which
    // will fail for all other folder types. Adjust.

    kdDebug(5006) << k_funcinfo << "mCalendar=" << mCalendar << " " << mCalendar->location() << endl;
    kdDebug(5006) << k_funcinfo << "mContacts=" << mContacts << " " << mContacts->location() << endl;
    kdDebug(5006) << k_funcinfo << "mNotes=" << mNotes << " " << mNotes->location() << endl;

    // Find all extra folders
    TQStringList folderNames;
    TQValueList<TQGuardedPtr<KMFolder> > folderList;
    kmkernel->dimapFolderMgr()->createFolderList(&folderNames, &folderList);
    TQValueList<TQGuardedPtr<KMFolder> >::iterator it;
    for(it = folderList.begin(); it != folderList.end(); ++it)
    {
      FolderStorage *storage = (*it)->storage();

      if ( (*it)->folderType() == KMFolderTypeCachedImap ) {
        KMFolderCachedImap *imapFolder = static_cast<KMFolderCachedImap*>( storage );

        const TQString attributes = imapFolder->folderAttributes();
        if ( attributes.contains( "X-FolderClass" ) ) {
          if ( !attributes.contains( "X-SpecialFolder" ) || (*it)->location().contains( "@" ) ) {
            const Scalix::FolderAttributeParser parser( attributes );
            if ( !parser.folderClass().isEmpty() ) {
              FolderContentsType type = Scalix::Utils::scalixIdToContentsType( parser.folderClass() );
              imapFolder->setContentsType( type );
              folderContentsTypeChanged( *it, type );
            }
          }
        }
      }
    }

    // If we just created them, they might have been registered as extra folders temporarily.
    // -> undo that.
    mExtraFolders.remove( mCalendar->location() );
    mExtraFolders.remove( mTasks->location() );
    mExtraFolders.remove( mContacts->location() );
    mExtraFolders.remove( mNotes->location() );

    // END TILL TODO

    subresourceAdded( folderContentsType( KMail::ContentsTypeCalendar ), mCalendar->location(), mCalendar->label(), true, true );
    subresourceAdded( folderContentsType( KMail::ContentsTypeTask ), mTasks->location(), mTasks->label(), true, true );
    subresourceAdded( folderContentsType( KMail::ContentsTypeContact ), mContacts->location(), mContacts->label(), true, false );
    subresourceAdded( folderContentsType( KMail::ContentsTypeNote ), mNotes->location(), mNotes->label(), true, false );
  }

  KConfig *config = kmkernel->config();
  config->setGroup("Resource UINames");
  *KMailICalIfaceImpl::mSubResourceUINamesMap =  config->entryMap( "Resource UINames" );

  reloadFolderTree();
}

void KMailICalIfaceImpl::slotCheckDone()
{
  TQString parentName = GlobalSettings::self()->theIMAPResourceFolderParent();
  KMFolder* folderParent = kmkernel->findFolderById( parentName );
  //kdDebug(5006) << k_funcinfo << " folderParent=" << folderParent << endl;
  if ( folderParent )  // cool it exists now
  {
    KMAccount* account = kmkernel->acctMgr()->find( GlobalSettings::self()->theIMAPResourceAccount() );
    if ( account )
      disconnect( account, TQT_SIGNAL( finishedCheck( bool, CheckStatus ) ),
                  this, TQT_SLOT( slotCheckDone() ) );
    readConfig();
  }
}

KMFolder* KMailICalIfaceImpl::initFolder( KMail::FolderContentsType contentsType )
{
  // Figure out what type of folder this is supposed to be
  KMFolderType type = mFolderType;
  if( type == KMFolderTypeUnknown ) type = KMFolderTypeMaildir;

  KFolderTreeItem::Type itemType = s_folderContentsType[contentsType].treeItemType;
  //kdDebug(5006) << "KMailICalIfaceImpl::initFolder " << folderName( itemType ) << endl;

  // Find the folder
  StandardFolderSearchResult result = findStandardResourceFolder( mFolderParentDir, contentsType );

  // deal with multiple default groupware folders
  if ( result.folders.count() > 1 && result.found == StandardFolderSearchResult::FoundAndStandard ) {
    TQStringList labels;
    for ( TQValueList<KMFolder*>::ConstIterator it = result.folders.begin(); it != result.folders.end(); ++it )
      labels << (*it)->prettyURL();
    const TQString selected = KInputDialog::getItem( i18n("Default folder"),
        i18n("There are multiple %1 default folders, please choose one:")
        .arg( localizedDefaultFolderName( contentsType ) ), labels );
    if ( !selected.isEmpty() )
      result.folder = result.folders[ labels.findIndex( selected ) ];
  }

  KMFolder* folder = result.folder;

  if ( !folder ) {
    // The folder isn't there yet - create it
    folder =
      mFolderParentDir->createFolder( localizedDefaultFolderName( contentsType ), false, type );
    if( mFolderType == KMFolderTypeImap ) {
      KMFolderImap* parentFolder = static_cast<KMFolderImap*>( mFolderParent->storage() );
      parentFolder->createFolder( localizedDefaultFolderName( contentsType ) );
      static_cast<KMFolderImap*>( folder->storage() )->setAccount( parentFolder->account() );
    }
    // Groupware folder created, use the global setting for storage format
    setStorageFormat( folder, globalStorageFormat() );
  } else {
    FolderInfo info = readFolderInfo( folder );
    mFolderInfoMap.insert( folder, info );
    //kdDebug(5006) << "Found existing folder type " << itemType << " : " << folder->location()  << endl;
  }

  if( folder->canAccess() != 0 ) {
    KMessageBox::sorry(0, i18n("You do not have read/write permission to your %1 folder.")
                       .arg( folderName( itemType ) ) );
    return 0;
  }
  folder->storage()->setContentsType( contentsType );
  folder->setSystemFolder( true );
  folder->storage()->writeConfig();
  folder->open("ifacefolder");
  connectFolder( folder );
  return folder;
}

KMFolder* KMailICalIfaceImpl::initScalixFolder( KMail::FolderContentsType contentsType )
{
  // Figure out what type of folder this is supposed to be
  KMFolderType type = mFolderType;
  if( type == KMFolderTypeUnknown ) type = KMFolderTypeMaildir;

  KMFolder* folder = 0;

  // Find all extra folders
  TQStringList folderNames;
  TQValueList<TQGuardedPtr<KMFolder> > folderList;
  Q_ASSERT( kmkernel );
  Q_ASSERT( kmkernel->dimapFolderMgr() );
  kmkernel->dimapFolderMgr()->createFolderList(&folderNames, &folderList);
  TQValueList<TQGuardedPtr<KMFolder> >::iterator it = folderList.begin();
  for(; it != folderList.end(); ++it)
  {
    FolderStorage *storage = (*it)->storage();

    if ( (*it)->folderType() == KMFolderTypeCachedImap ) {
      KMFolderCachedImap *imapFolder = static_cast<KMFolderCachedImap*>( storage );

      const TQString attributes = imapFolder->folderAttributes();
      if ( attributes.contains( "X-SpecialFolder" ) ) {
        const Scalix::FolderAttributeParser parser( attributes );
        if ( contentsType == Scalix::Utils::scalixIdToContentsType( parser.folderClass() ) ) {
          folder = *it;
          break;
        }
      }
    }
  }

  if ( !folder ) {
    return 0;
  } else {
    FolderInfo info = readFolderInfo( folder );
    mFolderInfoMap.insert( folder, info );
    //kdDebug(5006) << "Found existing folder type " << itemType << " : " << folder->location()  << endl;
  }

  if( folder->canAccess() != 0 ) {
    KMessageBox::sorry(0, i18n("You do not have read/write permission to your folder.") );
    return 0;
  }
  folder->storage()->setContentsType( contentsType );
  folder->setSystemFolder( true );
  folder->storage()->writeConfig();
  folder->open( "scalixfolder" );
  connectFolder( folder );
  return folder;
}

void KMailICalIfaceImpl::connectFolder( KMFolder* folder )
{
  // avoid multiple connections
  disconnect( folder, TQT_SIGNAL( msgAdded( KMFolder*, Q_UINT32 ) ),
              this, TQT_SLOT( slotIncidenceAdded( KMFolder*, Q_UINT32 ) ) );
  disconnect( folder, TQT_SIGNAL( msgRemoved( KMFolder*, Q_UINT32 ) ),
              this, TQT_SLOT( slotIncidenceDeleted( KMFolder*, Q_UINT32 ) ) );
  disconnect( folder, TQT_SIGNAL( expunged( KMFolder* ) ),
              this, TQT_SLOT( slotRefreshFolder( KMFolder* ) ) );
  disconnect( folder->storage(), TQT_SIGNAL( readOnlyChanged( KMFolder* ) ),
              this, TQT_SLOT( slotFolderPropertiesChanged( KMFolder* ) ) );
  disconnect( folder, TQT_SIGNAL( nameChanged() ),
              this, TQT_SLOT( slotFolderRenamed() ) );
  disconnect( folder->storage(), TQT_SIGNAL( locationChanged( const TQString&, const TQString&) ),
              this, TQT_SLOT( slotFolderLocationChanged( const TQString&, const TQString&) ) );

  // Setup the signals to listen for changes
  connect( folder, TQT_SIGNAL( msgAdded( KMFolder*, Q_UINT32 ) ),
           this, TQT_SLOT( slotIncidenceAdded( KMFolder*, Q_UINT32 ) ) );
  connect( folder, TQT_SIGNAL( msgRemoved( KMFolder*, Q_UINT32 ) ),
           this, TQT_SLOT( slotIncidenceDeleted( KMFolder*, Q_UINT32 ) ) );
  connect( folder, TQT_SIGNAL( expunged( KMFolder* ) ),
           this, TQT_SLOT( slotRefreshFolder( KMFolder* ) ) );
  connect( folder->storage(), TQT_SIGNAL( readOnlyChanged( KMFolder* ) ),
           this, TQT_SLOT( slotFolderPropertiesChanged( KMFolder* ) ) );
  connect( folder, TQT_SIGNAL( nameChanged() ),
           this, TQT_SLOT( slotFolderRenamed() ) );
  connect( folder->storage(), TQT_SIGNAL( locationChanged( const TQString&, const TQString&) ),
           this, TQT_SLOT( slotFolderLocationChanged( const TQString&, const TQString&) ) );

}

static void cleanupFolder( KMFolder* folder, KMailICalIfaceImpl* _this )
{
  if( folder ) {
    folder->setSystemFolder( false );
    folder->disconnect( _this );
    folder->close("ifacefolder");
  }
}

void KMailICalIfaceImpl::cleanup()
{
  cleanupFolder( mContacts, this );
  cleanupFolder( mCalendar, this );
  cleanupFolder( mNotes, this );
  cleanupFolder( mTasks, this );
  cleanupFolder( mJournals, this );

  mContacts = mCalendar = mNotes = mTasks = mJournals = 0;
}

TQString KMailICalIfaceImpl::folderPixmap( KFolderTreeItem::Type type ) const
{
  if( !mUseResourceIMAP )
    return TQString::null;

  if( type == KFolderTreeItem::Contacts )
    return TQString::fromLatin1( "kmgroupware_folder_contacts" );
  else if( type == KFolderTreeItem::Calendar )
    return TQString::fromLatin1( "kmgroupware_folder_calendar" );
  else if( type == KFolderTreeItem::Notes )
    return TQString::fromLatin1( "kmgroupware_folder_notes" );
  else if( type == KFolderTreeItem::Tasks )
    return TQString::fromLatin1( "kmgroupware_folder_tasks" );
  else if( type == KFolderTreeItem::Journals )
    return TQString::fromLatin1( "kmgroupware_folder_journals" );

  return TQString::null;
}

static void reloadFolderTree()
{
  // Make the folder tree show the icons or not
  kmkernel->folderMgr()->contentsChanged();
}

// This is a very light-weight and fast 'parser' to retrieve
// a data entry from a vCal taking continuation lines
// into account
static void vPartMicroParser( const TQString& str, TQString& s )
{
  TQString line;
  uint len = str.length();

  for( uint i=0; i<len; ++i){
    if( str[i] == '\r' || str[i] == '\n' ){
      if( str[i] == '\r' )
        ++i;
      if( i+1 < len && str[i+1] == ' ' ){
        // found a continuation line, skip it's leading blanc
        ++i;
      }else{
        // found a logical line end, process the line
        if( line.startsWith( s ) ) {
          s = line.mid( s.length() + 1 );
          return;
        }
        line = "";
      }
    } else {
      line += str[i];
    }
  }

  // Not found. Clear it
  s.truncate(0);
}

// Returns the first child folder having the given annotation
static TQValueList<KMFolder*> findFolderByAnnotation( KMFolderDir* folderParentDir, const TQString& annotation )
{
  TQValueList<KMFolder*> rv;
  TQPtrListIterator<KMFolderNode> it( *folderParentDir );
  for ( ; it.current(); ++it ) {
    if ( !it.current()->isDir() ) {
      KMFolder* folder = static_cast<KMFolder *>( it.current() );
      if ( folder->folderType() == KMFolderTypeCachedImap ) {
        TQString folderAnnotation = static_cast<KMFolderCachedImap*>( folder->storage() )->annotationFolderType();
        //kdDebug(5006) << "findStandardResourceFolder: " << folder->name() << " has annotation " << folderAnnotation << endl;
        if ( folderAnnotation == annotation )
          rv.append( folder );
      }
    }
  }
  return rv;
}

KMailICalIfaceImpl::StandardFolderSearchResult KMailICalIfaceImpl::findStandardResourceFolder( KMFolderDir* folderParentDir, KMail::FolderContentsType contentsType )
{
  if ( GlobalSettings::self()->theIMAPResourceStorageFormat() == GlobalSettings::EnumTheIMAPResourceStorageFormat::XML )
  {
    // Look for a folder with an annotation like "event.default"
    TQValueList<KMFolder*> folders = findFolderByAnnotation( folderParentDir, TQString( s_folderContentsType[contentsType].annotation ) + ".default" );
    if ( !folders.isEmpty() )
      return StandardFolderSearchResult( folders, StandardFolderSearchResult::FoundAndStandard );

    // Fallback: look for a folder with an annotation like "event"
    folders = findFolderByAnnotation( folderParentDir, TQString( s_folderContentsType[contentsType].annotation ) );
    if ( !folders.isEmpty() )
      return StandardFolderSearchResult( folders, StandardFolderSearchResult::FoundByType );

    // Fallback: look for the folder by name (we'll need to change its type)
    KMFolderNode* node = folderParentDir->hasNamedFolder( localizedDefaultFolderName( contentsType ) );
    if ( node && !node->isDir() )
      return StandardFolderSearchResult( static_cast<KMFolder *>( node ), StandardFolderSearchResult::FoundByName );

    kdDebug(5006) << "findStandardResourceFolder: found no resource folder for " << s_folderContentsType[contentsType].annotation << endl;
    return StandardFolderSearchResult( 0, StandardFolderSearchResult::NotFound );
  }
  else // icalvcard: look up standard resource folders by name
  {
    KFolderTreeItem::Type itemType = s_folderContentsType[contentsType].treeItemType;
    unsigned int folderLanguage = GlobalSettings::self()->theIMAPResourceFolderLanguage();
    if( folderLanguage > 3 ) folderLanguage = 0;
    KMFolderNode* node = folderParentDir->hasNamedFolder( folderName( itemType, folderLanguage ) );
    if ( !node || node->isDir() )
      return StandardFolderSearchResult( 0, StandardFolderSearchResult::NotFound );
    return StandardFolderSearchResult( static_cast<KMFolder*>( node ), StandardFolderSearchResult::FoundAndStandard );
  }
}

/* We treat all folders as relevant wrt alarms for which we have Administer
 * rights or for which the "Incidences relevant for everyone" annotation has
 * been set. It can be reasonably assumed that those are "ours". All local folders
 * must be ours anyhow. */
bool KMailICalIfaceImpl::folderIsAlarmRelevant( const KMFolder *folder )
{
  bool administerRights = true;
  bool relevantForOwner = true;
  bool relevantForEveryone = false;
  if ( folder->folderType() == KMFolderTypeImap ) {
    const KMFolderImap *imapFolder = static_cast<const KMFolderImap*>( folder->storage() );
    administerRights =
      imapFolder->userRightsState() != KMail::ACLJobs::Ok ||
      imapFolder->userRights() & KMail::ACLJobs::Administer;
  }
  if ( folder->folderType() == KMFolderTypeCachedImap ) {
    const KMFolderCachedImap *dimapFolder = static_cast<const KMFolderCachedImap*>( folder->storage() );
    administerRights =
      dimapFolder->userRightsState() != KMail::ACLJobs::Ok ||
      dimapFolder->userRights() & KMail::ACLJobs::Administer;
    relevantForOwner = !dimapFolder->alarmsBlocked() && ( dimapFolder->incidencesFor () == KMFolderCachedImap::IncForAdmins );
    relevantForEveryone = !dimapFolder->alarmsBlocked() && ( dimapFolder->incidencesFor() == KMFolderCachedImap::IncForReaders );
  }
#if 0
  kdDebug(5006) << k_funcinfo << endl;
  kdDebug(5006) << "Folder: " << folder->label() << " has administer rights: " << administerRights << endl;
  kdDebug(5006) << "and is relevant for owner: " << relevantForOwner <<  endl;
  kdDebug(5006) << "and relevant for everyone: "  << relevantForEveryone << endl;
#endif
  return ( administerRights && relevantForOwner ) || relevantForEveryone;
}

void KMailICalIfaceImpl::setResourceQuiet(bool q)
{
  mResourceQuiet = q;
}

bool KMailICalIfaceImpl::isResourceQuiet() const
{
  return mResourceQuiet;
}


bool KMailICalIfaceImpl::addSubresource( const TQString& resource,
                                         const TQString& parent,
                                         const TQString& contentsType )
{
  kdDebug(5006) << "Adding subresource to parent: " << parent << " with name: " << resource << endl;
  kdDebug(5006) << "contents type: " << contentsType << endl;
  KMFolder *folder = findResourceFolder( parent );
  KMFolderDir *parentFolderDir = !parent.isEmpty() && folder ? folder->createChildFolder(): mFolderParentDir;
  if ( !parentFolderDir || parentFolderDir->hasNamedFolder( resource ) ) return false;

  TQString msg;
  if ( parentFolderDir->owner() && !parentFolderDir->owner()->isValidName( resource, msg ) ) {
    KMessageBox::error( 0, msg );
    return false;
  }

  KMFolderType type = mFolderType;
  if( type == KMFolderTypeUnknown ) type = KMFolderTypeMaildir;

  KMFolder* newFolder = parentFolderDir->createFolder( resource, false, type );
  if ( !newFolder ) return false;
  if( mFolderType == KMFolderTypeImap )
    static_cast<KMFolderImap*>( folder->storage() )->createFolder( resource );

  StorageFormat defaultFormat = GlobalSettings::self()->theIMAPResourceStorageFormat() == GlobalSettings::EnumTheIMAPResourceStorageFormat::XML ? StorageXML : StorageIcalVcard;
  setStorageFormat( newFolder, folder ? storageFormat( folder ) : defaultFormat );
  newFolder->storage()->setContentsType( folderContentsType( contentsType ) );
  newFolder->storage()->writeConfig();
  newFolder->open( "ical_subresource" );
  connectFolder( newFolder );
  reloadFolderTree();

  return true;
}

bool KMailICalIfaceImpl::removeSubresource( const TQString& location )
{
  kdDebug(5006) << k_funcinfo << endl;

  KMFolder *folder = findResourceFolder( location );

  // We don't allow the default folders to be deleted, so check for
  // those first. It would be nicer to produce a more meaningful error,
  // or prevent deletion of the builtin folders from the gui already.
  if ( !folder || isStandardResourceFolder( folder ) )
      return false;

  // the folder will be removed, which implies closed, so make sure
  // nothing is using it anymore first
  subresourceDeleted( folderContentsType( folder->storage()->contentsType() ), location );
  mExtraFolders.remove( location );
  folder->disconnect( this );

  if ( folder->folderType() == KMFolderTypeImap )
    kmkernel->imapFolderMgr()->remove( folder );
  else if ( folder->folderType() == KMFolderTypeCachedImap ) {
    // Deleted by user -> tell the account (see KMFolderCachedImap::listDirectory2)
    KMFolderCachedImap* storage = static_cast<KMFolderCachedImap*>( folder->storage() );
    KMAcctCachedImap* acct = storage->account();
    if ( acct )
      acct->addDeletedFolder( folder );
    kmkernel->dimapFolderMgr()->remove( folder );
  }
  return true;
}

void KMailICalIfaceImpl::syncFolder(KMFolder * folder) const
{
  if ( kmkernel->isOffline() || !GlobalSettings::immediatlySyncDIMAPOnGroupwareChanges() )
    return;
  KMFolderCachedImap *dimapFolder = dynamic_cast<KMFolderCachedImap*>( folder->storage() );
  if ( !dimapFolder )
    return;
  // check if the folder exists already, otherwise sync its parent as well to create it
  if ( dimapFolder->imapPath().isEmpty() ) {
    if ( folder->parent() && folder->parent()->owner() )
      syncFolder( folder->parent()->owner() );
    else
      return;
  }
  dimapFolder->account()->processNewMailInFolder( folder );
}

#include "kmailicalifaceimpl.moc"