summaryrefslogtreecommitdiffstats
path: root/kresources/newexchange/exchangeconvertercalendar.cpp
blob: 53cc7e78d06f53b17ca7b1aad0131ce5bfba0769 (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
/*
    This file is part of the exchange resource.
    Copyright (c) 2004 Reinhold Kainhofer <reinhold@kainhofer.com>
    Parts are derived from the old libkpimexchange library:
    Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.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.
*/

#include "exchangeconvertercalendar.h"
#include <webdavhandler.h>
#include <libkcal/incidence.h>
#include <libkcal/event.h>
#include <libkcal/journal.h>
#include <libkcal/todo.h>
#include <libkcal/icalformat.h>
#include <libemailfunctions/email.h>

#include <kdebug.h>

using namespace KCal;

#define TaskNamespace1 "http://schemas.microsoft.com/mapi/id/{00062003-0000-0000-C000-000000000046}/"
#define TaskProp_tqStatus            "0x00008101"
#define TaskProp_PercentCompleted  "0x00008102"
#define TaskProp_DtStart           "0x00008104"
#define TaskProp_DtDue             "0x00008105"
#define TaskProp_Duration          "0x00008106"
#define TaskProp_CompletionDate    "0x0000810f"
#define TaskProp_IsCompleted       "0x0000811C"
#define TaskProp_Owner             "0x0000811F"
#define TaskProp_DoesRecur         "0x00008126"

#define TaskNamespace2 "http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/"
#define TaskProp_ReminderTime      "0x00008502"
#define TaskProp_ReminderSet       "0x00008503"
#define TaskProp_ReminderPlaySound "0x0000851E"
#define TaskProp_ReminderSoundFile "0x0000851F"
#define TaskProp_ContactNames      "0x0000853A"


ExchangeConverterCalendar::ExchangeConverterCalendar()
{
}

void ExchangeConverterCalendar::setTimeZone( const TQString &id )
{
 // kdDebug() << "Setting timezone to:  " << id << endl;
  mFormat.setTimeZone( id, true );
}

#define propertyDAV( prop ) \
    WebdavHandler::addElement( doc, root, "d:"prop )
#define propertyNS( ns, prop ) \
    WebdavHandler::addElementNS( doc, root, ns, prop )
#define propertyCalendar( prop ) \
    WebdavHandler::addElement( doc, root, "c:"prop )
#define propertyHTTPMail( prop ) \
    WebdavHandler::addElement( doc, root, "m:"prop )
#define propertyMailHeader( prop ) \
    WebdavHandler::addElement( doc, root, "h:"prop )
#define property( prop ) \
    WebdavHandler::addElement( doc, root, prop )

void ExchangeConverterCalendar::createRequestIncidence( TQDomDocument &doc, TQDomElement &root )
{
  propertyDAV( "contentclass" );
  propertyDAV( "getcontenttype" );
  propertyNS( "http://schemas.microsoft.com/exchange/", "outlookmessageclass" );
  propertyDAV( "getetag" );
  propertyDAV( "href" );
  propertyDAV( "isreadonly" );
  propertyNS( "http://schemas.microsoft.com/repl/", "repl-uid" );
  propertyHTTPMail( "subject" );
  propertyHTTPMail( "textdescription" );
  propertyHTTPMail( "date" );
  propertyDAV( "comment" );
  propertyNS( "urn:schemas-microsoft-com:office:office", "Keywords" );
  propertyNS( "http://schemas.microsoft.com/exchange/", "sensitivity" );
  propertyHTTPMail( "priority" );
  propertyHTTPMail( "from" );
  propertyHTTPMail( "to" );
  propertyHTTPMail( "cc" );
  propertyHTTPMail( "bcc" );
  propertyHTTPMail( "hasattachment" );
}

void ExchangeConverterCalendar::createRequestAppointment( TQDomDocument &doc, TQDomElement &root )
{
  createRequestIncidence( doc, root );
  TQDomAttr att_c = doc.createAttribute( "xmlns:c" );
  att_c.setValue( "urn:schemas:calendar:" );
  doc.documentElement().setAttributeNode( att_c );
  propertyCalendar( "uid" );
  propertyCalendar( "created" );
  propertyCalendar( "lastmodified" );
  propertyCalendar( "dtstamp" );
  propertyCalendar( "sequence" );
  propertyCalendar( "location" );
  propertyCalendar( "busystatus" );
  propertyCalendar( "transparent" );
  propertyCalendar( "timezone" );
  propertyCalendar( "alldayevent" );
  propertyCalendar( "dtstart" );
  propertyCalendar( "dtend" );
  propertyCalendar( "duration" );
  propertyCalendar( "rrule" );
  propertyCalendar( "rdate" );
  propertyCalendar( "exrule" );
  propertyCalendar( "exdate" );
  propertyCalendar( "recurrenceid" );
  propertyCalendar( "instancetype" );
  propertyCalendar( "reminderoffset" );
  propertyCalendar( "resources" );
}

#define propertyTask1( prop ) \
    WebdavHandler::addElement( doc, props, "t1:"prop )
#define propertyTask2( prop ) \
    WebdavHandler::addElement( doc, props, "t2:"prop )

void ExchangeConverterCalendar::createRequestTask( TQDomDocument &doc, TQDomElement &props )
{
  createRequestIncidence( doc, props );

  TQDomElement root = doc.documentElement();

  TQDomAttr att_t1 = doc.createAttribute( "xmlns:t1" );
  att_t1.setValue( TaskNamespace1 );
  root.setAttributeNode( att_t1 );

  TQDomAttr att_t2 = doc.createAttribute( "xmlns:t2" );
  att_t2.setValue( TaskNamespace2 );
  root.setAttributeNode( att_t2 );

  // TODO: Insert the correct namespaces here:
//  propertyTask1( TaskProp_UID );
  propertyDAV( "creationdate" );
  propertyDAV( "getlastmodified" );
  propertyTask1( TaskProp_Owner );
  propertyTask2( TaskProp_ContactNames );
  propertyTask1( TaskProp_DtStart );
  propertyTask1( TaskProp_DtDue );
  propertyTask1( TaskProp_Duration );
  propertyTask1( TaskProp_IsCompleted );
  propertyTask1( TaskProp_PercentCompleted );
  propertyTask1( TaskProp_CompletionDate );
  propertyTask1( TaskProp_DoesRecur );
  // What to do about recurrence rules?
  propertyTask2( TaskProp_ReminderSet );
  propertyTask2( TaskProp_ReminderTime );
  propertyTask2( TaskProp_ReminderPlaySound );
  propertyTask2( TaskProp_ReminderSoundFile );
  propertyTask1( TaskProp_tqStatus );
}
#undef propertyTask1
#undef propertyTask2

void ExchangeConverterCalendar::createRequestJournal( TQDomDocument &doc, TQDomElement &root )
{
  createRequestIncidence( doc, root );
  propertyDAV( "uid" );
  propertyDAV( "creationdate" );
  propertyDAV( "getlastmodified" );
}
#undef propertyDAV
#undef propertyNS
#undef propertyCalendar
#undef propertyHTTPMail
#undef propertyMailHeader
#undef property

bool ExchangeConverterCalendar::readTZ( const TQDomElement &node, Incidence */*incidence*/ )
{
  TQString timezoneid;
  if ( WebdavHandler::extractString( node, "timezoneid", timezoneid ) ) {
    // kdDebug() << "DEBUG: timezoneid = " << timezoneid << endl;
  }

  TQString timezone;
  if ( WebdavHandler::extractString( node, "timezone", timezone ) ) {
    // kdDebug() << "DEBUG: timezone = " << timezone << endl;
  }

  // TODO:
/*  // mFormat is used for parsing recurrence rules.
  TQString localTimeZoneId;
  if ( mCalendar ) {
    mFormat.setTimeZone( mCalendar->timeZoneId(), !mCalendar->isLocalTime() );
    localTimeZoneId = mCalendar->timeZoneId();
  }  else {
    localTimeZoneId = "UTC";
    // If no mCalendar, stay in UTC
  }
*/
  return true;
}

bool ExchangeConverterCalendar::readIncidence( const TQDomElement &node, Incidence *incidence )
{
kdDebug()<<"ExchangeConverterCalendar::readIncidencd"<<endl;
  TQDateTime tmpdt;
  bool tmpbool;
  TQString tmpstr;
  long tmplng;
  TQStringList tmplst;

  readTZ( node, incidence );

  if ( WebdavHandler::extractString( node, "getetag", tmpstr ) )
    incidence->setCustomProperty( "TDEPIM-Exchange-Resource", "fingerprint", tmpstr );
  if ( WebdavHandler::extractString( node, "href", tmpstr ) )
    incidence->setCustomProperty( "TDEPIM-Exchange-Resource", "href", tmpstr );
  
  // FIXME: use repl-uid as scheduling id?
  if ( WebdavHandler::extractString( node, "textdescription", tmpstr ) )
    incidence->setDescription( tmpstr );
  if ( WebdavHandler::extractString( node, "subject", tmpstr ) )
    incidence->setSummary( tmpstr );
  if ( WebdavHandler::extractStringList( node, "Keywords", tmplst ) )
    incidence->setCategories( tmplst );

  // Use "created" or "creationdate"?
  if ( WebdavHandler::extractBool( node, "isreadonly" , tmpbool ) )
    incidence->setReadOnly( tmpbool );

  // FIXME: Ignore the comment for now

  // Exchange sentitivity values:
  // 0 None, 1 Personal, 2 Private, 3 Company Confidential
  if ( WebdavHandler::extractLong( node, "sensitivity", tmplng ) ) {
    switch( tmplng ) {
      case 0: incidence->setSecrecy( KCal::Incidence::SecrecyPublic ); break;
      case 1:
      case 2: incidence->setSecrecy( KCal::Incidence::SecrecyPrivate ); break;
      case 3: incidence->setSecrecy( KCal::Incidence::SecrecyConfidential ); break;
      default: kdWarning() << "Unknown sensitivity: " << tmplng << endl;
    }
  }

  if ( WebdavHandler::extractBool( node, "hasattachment", tmpbool ) && tmpbool ) {
    // FIXME: Extract attachments...
  }

  if ( WebdavHandler::extractLong( node, "priority", tmplng ) )
    incidence->setPriority( tmplng );

  // FIXME: Use the urn:schemes:httpmail:date property for what?

  // Organizer, required and optional Attendees:
  if ( WebdavHandler::extractString( node, "from", tmpstr ) )
    incidence->setOrganizer( tmpstr );
  if ( WebdavHandler::extractString( node, "to", tmpstr ) ) {
    TQStringList atts( KPIM::splitEmailAddrList( tmpstr ) );
    for ( TQStringList::Iterator it = atts.begin(); it != atts.end(); ++it ) {
      TQString name, email;
      KPIM::getNameAndMail( *it, name, email );
      Attendee *att = new Attendee( name, email );
      att->setRole( KCal::Attendee::ReqParticipant );
      // FIXME: Retrieve the other attendee properties somehow...
      // urn:schemas:calendar:method
      // urn:schemas:calendar:responserequested
      // urn:schemas:calendar:meetingstatus
      // urn:schemas:calendar:replytime
      incidence->addAttendee( att );
    }
  }
  if ( WebdavHandler::extractString( node, "cc", tmpstr ) ) {
    TQStringList atts( KPIM::splitEmailAddrList( tmpstr ) );
    for ( TQStringList::Iterator it = atts.begin(); it != atts.end(); ++it ) {
      TQString name, email;
      KPIM::getNameAndMail( *it, name, email );
      Attendee *att = new Attendee( name, email );
      att->setRole( KCal::Attendee::OptParticipant );
      // FIXME: Retrieve the other attendee properties somehow...
      // urn:schemas:calendar:method
      // urn:schemas:calendar:responserequested
      // urn:schemas:calendar:meetingstatus
      // urn:schemas:calendar:replytime
      incidence->addAttendee( att );
    }
  }

  return true;
}

/* FIXME: Handle recurrences
void ExchangeDownload::handleRecurrence( TQString uid )
{
  // kdDebug() << "Handling recurrence info for uid=" << uid << endl;
  TQString query =
        "SELECT \"DAV:href\", \"urn:schemas:calendar:instancetype\"\r\n"
        "FROM Scope('shallow traversal of \"\"')\r\n"
        "WHERE \"urn:schemas:calendar:uid\" = '" + uid + "'\r\n"
	" AND (\"urn:schemas:calendar:instancetype\" = 1)\r\n";
//	"      OR \"urn:schemas:calendar:instancetype\" = 3)\r\n" // FIXME: exception are not handled

  // kdDebug() << "Exchange master query: " << endl << query << endl;

  KIO::DavJob* job = KIO::davSearch( mAccount->calendarURL(), "DAV:", "sql",
                                     query, false );
  KIO::Scheduler::scheduleJob( job );
  job->setWindow( mWindow );
  connect( job, TQT_SIGNAL( result( KIO::Job * ) ),
           TQT_SLOT( slotMasterResult( KIO::Job * ) ) );
}
*/

bool ExchangeConverterCalendar::readEvent( const TQDomElement &node, Event *event )
{
  if ( !readIncidence( node, event ) ) return false;
kdDebug()<<"ExchangeConverterCalendar::readEvent"<<endl;

  TQDateTime tmpdt;
  TQString tmpstr;
  long tmplng;
  bool tmpbool;

  // The UID is absolutely required!
  if ( WebdavHandler::extractString( node, "uid", tmpstr ) ) {
    event->setUid( tmpstr );
  } else {
kdDebug()<<"ExchangeConverterCalendar::readIncidence: ERROR: No UID given"<<endl;
    return false;
  }
  if ( WebdavHandler::extractDateTime( node, "created", tmpdt ) )
    event->setCreated( WebdavHandler::utcAsZone( tmpdt, mFormat.timeZoneId() ) );
  if ( WebdavHandler::extractDateTime( node, "lastmodified", tmpdt ) )
    event->setLastModified( WebdavHandler::utcAsZone( tmpdt, mFormat.timeZoneId() ) );
  // FIXME: Retrieve time zone: "timezone"
  // FIXME: Use the "recurrenceid" prop for the recurrenceId of the event (which is protected!)

  // FIXME: Retrieve MICROSOFT-CDO-* tags first

  if ( WebdavHandler::extractLong( node, "sequence", tmplng ) )
    event->setRevision( tmplng );

  if ( WebdavHandler::extractString( node, "location", tmpstr ) )
    event->setLocation( tmpstr );

  // FIXME: Use "organizer" here instead of the From: person?
/*  if ( WebdavHandler::extractString( node, "organizer", tmpstr ) )
    incidence->setOrganizer( tmpstr );*/

  if ( WebdavHandler::extractDateTime( node, "dtstart", tmpdt ) ) {
    event->setDtStart( WebdavHandler::utcAsZone( tmpdt, mFormat.timeZoneId() ) );
  }
  
  bool allDay = false;
  if ( WebdavHandler::extractBool( node, "alldayevent", allDay ) )
    event->setFloats( allDay );

  if ( WebdavHandler::extractLong( node, "duration", tmplng ) ) {
    if (allDay)
      tmplng--; // Otherwise event extends into next day
    event->setDuration( tmplng );
    //  kdDebug() << "DURATION " << tmplng << "\n";
  } else if ( WebdavHandler::extractDateTime( node, "dtend", tmpdt ) ) {
    event->setDtEnd( WebdavHandler::utcAsZone( tmpdt, mFormat.timeZoneId() ) );
  }

  // FIXME: Here we have two different props for the same thing?!?!?
  if ( WebdavHandler::extractLong( node, "transparent", tmplng ) )
    event->setTransparency( tmplng>0 ? Event::Transparent : Event::Opaque );
  if ( WebdavHandler::extractString( node, "busystatus", tmpstr ) ) {
    if ( tmpstr == "FREE" )
      event->setTransparency( KCal::Event::Transparent );
    if ( tmpstr == "BUSY" )
      event->setTransparency( KCal::Event::Opaque );
  }

  if ( WebdavHandler::extractLong( node, "reminderoffset", tmplng ) ) {
    // Duration before event in seconds
    KCal::Duration offset( -tmplng );
    KCal::Alarm *alarm = event->newAlarm();
    alarm->setStartOffset( offset );
    alarm->setEnabled( true );
    alarm->setType( KCal::Alarm::Display);
    // TODO: multiple alarms; 
  }


  if ( WebdavHandler::extractString( node, "rrule", tmpstr ) && !tmpstr.isEmpty() ) {
    kdDebug() << "Got rrule: " << tmpstr << endl;
    // Timezone should be handled automatically
    // because we used mFormat.setTimeZone() earlier
    // FIXME: Implement this using the format!
    RecurrenceRule *rrule = event->recurrence()->defaultRRule( true );
    if ( ! mFormat.fromString( rrule, tmpstr ) ) {
      kdError() << "ERROR parsing rrule " << tmpstr << endl;
      event->recurrence()->addRRule( rrule );
    }
  }

  TQStringList tmplst;
  if ( WebdavHandler::extractStringList( node, "exdate", tmplst ) ) {
    TQStringList::Iterator it = tmplst.begin();
    KCal::DateList exdates;
    for ( ; it != tmplst.end(); ++it ) {
      exdates.append( /*utcAsZone(*/ TQDateTime::fromString( *it, Qt::ISODate )/*,
          localTimeZoneId )*/.date() );
    }
    event->recurrence()->setExDates( exdates );
  }
  // FIXME: use rdate and exrule!
/* FIXME: Recurring events, they are split up
      TQDomElement instancetypeElement = prop.namedItem( "instancetype" ).toElement();
      if ( instancetypeElement.isNull() ) {
        kdError() << "Error: no instance type in Exchange server reply" << endl;
        continue;
      }
      int instanceType = instancetypeElement.text().toInt();
      //kdDebug() << "Instance type: " << instanceType << endl;

      if ( recurrence && instanceType > 0 ) {
        TQDomElement uidElement = prop.namedItem( "uid" ).toElement();
        if ( uidElement.isNull() ) {
          kdError() << "Error: no uid in Exchange server reply" << endl;
          continue;
        }
        TQString uid = uidElement.text();
        if ( ! m_uids.contains( uid ) ) {
          m_uids[uid] = 1;
          handleRecurrence(uid);
          successCount++;
        }
        continue;
      }
*/


  // FIXME: read the resources from the "resources" tag

  // FIXME: Custom fields not yet implemented
  return true;
}

bool ExchangeConverterCalendar::readTodo( const TQDomElement &node, Todo *todo )
{
  if ( !readIncidence( node, todo ) ) return false;
kdDebug()<<"ExchangeConverterCalendar::readTodo"<<endl;

  // FIXME: Retrieve time zone: "timezone"
  // FIXME: What to with TaskProp_Owner and TaskProp_ContactNames?

  TQDateTime tmpdt;
  float tmpfloat;
  long tmplong;
  bool tmpbool;
  TQString tmpstr;

  // The UID is absolutely required!
  // FIXME: Which field shall be used as uid???
/*  if ( WebdavHandler::extractString( node, "uid", tmpstr ) ) {
    todo->setUid( tmpstr );
  } else {
kdDebug()<<"ExchangeConverterCalendar::readIncidence: ERROR: No UID given"<<endl;
    return false;
  }*/
//  if ( WebdavHandler::extractDateTime( node, "created", tmpdt ) )
/* FIXME: creation and last modification dates:
  if ( WebdavHandler::extractDateTime( node, "creationdate", tmpdt ) )
    incidence->setCreated( tmpdt );
  if ( WebdavHandler::extractDateTime( node, "getlastmodified", tmpdt ) )
    incidence->setLastModified( tmpdt );*/

  if ( WebdavHandler::extractDateTime( node, TaskProp_DtStart, tmpdt ) )
    todo->setDtStart( WebdavHandler::utcAsZone( tmpdt, mFormat.timeZoneId() ) );
  if ( WebdavHandler::extractDateTime( node, TaskProp_DtDue, tmpdt ) )
    todo->setDtDue( WebdavHandler::utcAsZone( tmpdt, mFormat.timeZoneId() ) );
  if ( WebdavHandler::extractLong( node, TaskProp_Duration, tmplong ) )
    todo->setDuration( tmplong );

  if ( WebdavHandler::extractBool( node, TaskProp_IsCompleted, tmpbool ) && tmpbool ) {
    todo->setCompleted( tmpbool );
    if ( tmpbool && WebdavHandler::extractDateTime( node, TaskProp_CompletionDate, tmpdt ) ) {
      todo->setCompleted( WebdavHandler::utcAsZone( tmpdt, mFormat.timeZoneId() ) );
    }
  }
  if ( WebdavHandler::extractFloat( node, TaskProp_PercentCompleted, tmpfloat ) )
    todo->setPercentComplete( (int)(tmpfloat*100) );

  // FIXME: Recurrence, using TaskProp_DoesRecur
  // What to do about recurrence rules?

  // FIXME: Reminders, use TaskProp_ReminderSet, TaskProp_ReminderTime,
  //        TaskProp_ReminderPlaySound, TaskProp_ReminderSoundFile, TaskProp_tqStatus
  //        But how do I get the offset?

  return true;
}

bool ExchangeConverterCalendar::readJournal( const TQDomElement &node, Journal *journal )
{
  if ( !readIncidence( node, journal ) ) return false;
kdDebug()<<"ExchangeConverterCalendar::readJournal"<<endl;
  TQDateTime tmpdt;
  TQString tmpstr;
  // The UID is absolutely required!
  // FIXME: Which field shall be used as UID?
  if ( WebdavHandler::extractString( node, "uid", tmpstr ) ) {
    journal->setUid( tmpstr );
  } else {
kdDebug()<<"ExchangeConverterCalendar::readJournal: ERROR: No UID given"<<endl;
    return false;
  }
/* FIXME: creation and last modification times:
  if ( WebdavHandler::extractDateTime( node, "created", tmpdt ) )
    incidence->setCreated( tmpdt );
  if ( WebdavHandler::extractDateTime( node, "lastmodified", tmpdt ) )
    incidence->setLastModified( tmpdt );*/

  if ( WebdavHandler::extractDateTime( node, "date", tmpdt ) )
    journal->setDtStart( tmpdt );
  return true;
}

Incidence::List ExchangeConverterCalendar::parseWebDAV( const TQDomDocument& davdata )
{
  TQDomElement prop = davdata.documentElement().namedItem( "response" )
                     .namedItem( "propstat" ).namedItem( "prop" ).toElement();
  if ( prop.isNull() ) return Incidence::List();

  TQString contentclass;
  bool success = WebdavHandler::extractString( prop, "contentclass", contentclass );
  if ( !success ) return Incidence::List();

  Incidence *incidence = 0;
  success = false;
  if ( contentclass == "urn:content-classes:appointment" ) {
    Event *event = new Event();
    success = readEvent( prop, event );
    incidence = event;
  } else if ( contentclass == "urn:content-classes:task" ) {
    Todo *todo = new Todo();
    success = readTodo( prop, todo );
    incidence = todo;
  } else if ( contentclass == "urn:content-classes:journal" ||
              contentclass == "urn:content-classes:message" ) {
    Journal *journal = new Journal();
    success = readJournal( prop, journal );
    incidence = journal;
  }

  Incidence::List list;
  if ( success ) {
    list.append( incidence );
  }
  return list;
}


#define domDavProperty( name, value ) \
  WebdavHandler::addElement( mDoc, mElement, "d:"name, value )
#define domProperty( NS, name, value ) \
  WebdavHandler::addElementNS( mDoc, mElement, NS, name, value )
#define domCalendarProperty( name, value ) \
  WebdavHandler::addElement( mDoc, mElement, "c:"name, value )
#define domHTTPMailProperty( name, value ) \
  WebdavHandler::addElement( mDoc, mElement, "m:"name, value )
#define domMailHeaderProperty( name, value ) \
  WebdavHandler::addElement( mDoc, mElement, "h:"name, value )




class ExchangeConverterCalendar::createWebDAVVisitor : public IncidenceBase::Visitor
{
  public:
    createWebDAVVisitor() : Visitor() {}
    bool act( TQDomDocument doc, TQDomElement el, IncidenceBase *incidence, const TQString &timeZoneId )
    {
      mDoc = doc;
      mElement = el;
      mTimeZoneId = timeZoneId;
      return incidence->accept( *this );
    }
  protected:
    void addBoolProp( TQDomElement &el ) { el.setAttribute( "b:dt", "boolean" ); }
    void addDateProp( TQDomElement &el ) { el.setAttribute( "b:dt", "dateTime.tz" ); }
    void addFloatProp( TQDomElement &el ) { el.setAttribute( "b:dt", "float" ); }
    void addIntProp( TQDomElement &el ) { el.setAttribute( "b:dt", "int" ); }
    TQString timePropString( const TQDateTime &dt ) { return dt.toString( Qt::ISODate )+"Z"; }

    bool visitIncidence( Incidence *incidence )
    {
      TQString tmpstr;
      domDavProperty( "isreadonly", (incidence->isReadOnly())?"1":"0" );
      // FIXME: scheduling ID
//       domProperty( "http://schemas.microsoft.com/repl/", "repl-uid", ??? );
      domHTTPMailProperty( "subject", incidence->summary() );
      domHTTPMailProperty( "textdescription", incidence->description() );
      // FIXME: timestampt, comments and categories
//       domHTTPMailProperty( "date", ??? ); // timestamp not available in libkcal
//       domDavProperty( "comment", incidence->comments() ); // libkcal has a TQStringlist, not one string
//       domProperty( "urn:schemas-microsoft-com:office:office", "Keywords", ??? ); // It's a <v>entyr1</v><v>entry2</v> String list!
      tmpstr = TQString();
      switch ( incidence->secrecy() ) {
        case KCal::Incidence::SecrecyPublic: tmpstr = "0"; break;
        case KCal::Incidence::SecrecyPrivate: tmpstr = "2"; break;
        case KCal::Incidence::SecrecyConfidential: tmpstr = "3"; break;
        default: break;
      }
      if ( !tmpstr.isEmpty() )
        domProperty( "http://schemas.microsoft.com/exchange/", "sensitivity", tmpstr );

      domHTTPMailProperty( "priority", TQString::number(incidence->priority()) );

      domMailHeaderProperty( "from", incidence->organizer().fullName() );

      //  Attendees:
      tmpstr = TQString();
      TQStringList reqattnames;
      TQStringList optattnames;
      Attendee::List atts = incidence->attendees();
      for ( Attendee::List::Iterator it = atts.begin(); it != atts.end(); ++it ) {
        switch ( (*it)->role() ) {
          case KCal::Attendee::Chair:
          case KCal::Attendee::ReqParticipant:
            reqattnames << (*it)->fullName();
            break;
          case KCal::Attendee::OptParticipant:
          case KCal::Attendee::NonParticipant:
            optattnames << (*it)->fullName();
            break;
          default: break;
        }
      }

      domMailHeaderProperty( "to", reqattnames.join(", ") );
      
      domMailHeaderProperty( "cc", optattnames.join(", ") );

      // FIXME: Attachments: propertyHTTPMail( "hasattachment" );

      return true;
    }
    bool visit( Event *event )
    {
      if ( !visitIncidence(event) ) return false;

      TQDomAttr att_c = mDoc.createAttribute( "xmlns:c" );
      att_c.setValue( "urn:schemas:calendar:" );
      mDoc.documentElement().setAttributeNode( att_c );

      domDavProperty( "contentclass", "urn:content-classes:appointment" );
      domProperty( "http://schemas.microsoft.com/exchange/",
                   "outlookmessageclass", "IPM.Appointment" );
      domCalendarProperty( "uid", event->uid() );
      TQDomElement el = domCalendarProperty( "created", timePropString( WebdavHandler::zoneAsUtc( event->created(), mTimeZoneId ) ) );
      addDateProp( el );
      el = domCalendarProperty( "lastmodified", timePropString( WebdavHandler::zoneAsUtc( event->lastModified(), mTimeZoneId ) ) );
      addDateProp( el );
      // FIXME: domCalendarProperty( "dtstamp", ??);
//       FIXME: domCalendarProperty( "sequence", event->sequence() );
      domCalendarProperty( "location", event->location() );

      TQString tmpstr;
      switch ( event->transparency() ) {
        case KCal::Event::Transparent: tmpstr = "FREE"; break;
        case KCal::Event::Opaque: tmpstr = "BUSY"; break;
      }
      if ( !tmpstr.isEmpty() )
        domCalendarProperty( "busystatus", tmpstr );
//       FIXME: What do do with the "transparent" property?
//       FIXME: Use the "timezone" property...
      domCalendarProperty( "alldayevent", event->doesFloat()?"1":"0" );
      el = domCalendarProperty( "dtstart", timePropString( WebdavHandler::zoneAsUtc( event->dtStart(), mTimeZoneId ) ) );
      addDateProp( el );
      if ( event->hasEndDate() ) {
        el = domCalendarProperty( "dtend", timePropString( WebdavHandler::zoneAsUtc( event->dtEnd(), mTimeZoneId ) ) );
        addDateProp( el );
      } else {
        domCalendarProperty( "duration", TQString::number( event->duration() ) );
      }
      // FIXME: Convert the recurrence rule to a string:
      if ( event->doesRecur() ) {
//       tmpstr = event->recurrence().....
//       domCalendarProperty( "rrule", tmpstr );
        // FIXME: Use "rdate" and "exrule"
        // FIXME: Use "exdate", what's the syntax?
        // FIXME: use the "instancetype" property
      }
      // FIXME: RecurrenceID is protected!
//       domCalendarProperty( "recurrenceid", event->recurrenceId() );
      // FIXME: "reminderoffset"
      // FIXME: "resources"  Alarm::List alarms = event->alarms();

       Alarm::List::ConstIterator it;
       Alarm::List alarms = event->alarms();
       for( it = alarms.begin(); it != alarms.end(); ++it ) {
	 if ((*it)->hasStartOffset()) {
	   domCalendarProperty( "reminderoffset", TQString::number( (*it)->startOffset().asSeconds() * -1 ) );
	 } else {
	   kdDebug() << "ExchangeConverterCalendar::createWebDAVVisitor:  Alarm type not supported\n";
	 }
       }

      return true;
    }
    bool visit( Todo *todo )
    {
      if ( !visitIncidence(todo) ) return false;

      TQDomAttr att_t1 = mDoc.createAttribute( "xmlns:t1" );
      att_t1.setValue( TaskNamespace1 );
      mDoc.documentElement().setAttributeNode( att_t1 );

      TQDomAttr att_t2 = mDoc.createAttribute( "xmlns:t2" );
      att_t2.setValue( TaskNamespace2 );
      mDoc.documentElement().setAttributeNode( att_t2 );


      domDavProperty( "contentclass", "urn:content-classes:task" );
      domProperty( "http://schemas.microsoft.com/exchange/",
                   "outlookmessageclass", "IPM.Task" );

/* FIXME:
      domCalendarProperty( "uid", todo->uid() );
      domCalendarProperty( "created", todo->created().toString( Qt::ISODate ) );
      domCalendarProperty( "lastmodified", todo->lastModified().toString( Qt::ISODate ) );*/
      // TODO
/*propertyTask1( TaskProp_Owner );
  propertyTask2( TaskProp_ContactNames );
  propertyTask1( TaskProp_DtStart );
  propertyTask1( TaskProp_DtDue );
  propertyTask1( TaskProp_Duration );
  propertyTask1( TaskProp_IsCompleted );
  propertyTask1( TaskProp_PercentCompleted );
  propertyTask1( TaskProp_CompetionDate );
  propertyTask1( TaskProp_DoesRecur );
  // What to do about recurrence rules?
  propertyTask2( TaskProp_ReminderSet );
  propertyTask2( TaskProp_ReminderTime );
  propertyTask2( TaskProp_ReminderPlaySound );
  propertyTask2( TaskProp_ReminderSoundFile );
  propertyTask1( TaskProp_tqStatus );*/
      return true;
    }
    bool visit( Journal *journal )
    {
      if ( !visitIncidence(journal) ) return false;
      domDavProperty( "contentclass", "urn:content-classes:journal" );
      domProperty( "http://schemas.microsoft.com/exchange/",
                   "outlookmessageclass", "IPM.Journal" );
/* FIXME:
      domCalendarProperty( "uid", todo->uid() );
      domCalendarProperty( "created", todo->created().toString( Qt::ISODate ) );
      domCalendarProperty( "lastmodified", todo->lastModified().toString( Qt::ISODate ) );*/
      // TODO
      return true;
    }

  protected:
    TQDomDocument mDoc;
    TQDomElement mElement;
    TQString mTimeZoneId;
};

// Prefixes for the namespaces:
// d... DAV:
// b... urn:schemas-microsoft-com:datatypes
// c... calendar
// m... httpmail
// h... httpheader
// p... mapi
// o... office
//

TQDomDocument ExchangeConverterCalendar::createWebDAV( Incidence *incidence )
{
  // TODO
  TQDomDocument doc;
  TQDomElement root = WebdavHandler::addDavElement( doc, doc, "d:propertyupdate" );
  TQDomElement set = WebdavHandler::addElement( doc, root, "d:set" );
  TQDomElement prop = WebdavHandler::addElement( doc, set, "d:prop" );

  TQDomAttr att_b = doc.createAttribute( "xmlns:b" );
  att_b.setValue( "urn:schemas-microsoft-com:datatypes" );
  root.setAttributeNode( att_b );

  TQDomAttr att_h = doc.createAttribute( "xmlns:h" );
  att_h.setValue( "urn:schemas:mailheader:" );
  root.setAttributeNode( att_h );

  TQDomAttr att_m = doc.createAttribute( "xmlns:m" );
  att_m.setValue( "urn:schemas:httpmail:" );
  root.setAttributeNode( att_m );

//   TQDomAttr att1 = doc.createAttributeNS( "do:whatever:you:like", "x:attname");
//   att1.setValue( "value" );
//   prop.setAttributeNodeNS( att1 );
//   root.setAttributeNodeNS( att1 );
//   set.setAttributeNode( att1 );
// //   prop.setAttributeNS ( "xmlns:b", "xmlns:b", "urn:schemas-microsoft-com:datatypes" );

  ExchangeConverterCalendar::createWebDAVVisitor v;
  v.act( doc, prop, incidence, mFormat.timeZoneId() );
  kdDebug() << "ExchangeConverterCalendar::createWebDAVVisitor=" << doc.toString() << "\n";

  return doc;
}
#undef domDavProperty
#undef domProperty
#undef domCalendarProperty
#undef domHTTPMailProperty
#undef domMailHeaderProperty