summaryrefslogtreecommitdiffstats
path: root/libkpimexchange/core/exchangedownload.cpp
blob: de7da39ba8482ba6a33ca1ee9fe1d9653d0e4591 (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
/*
    This file is part of libkpimexchange
    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 <tqfile.h>
#include <tqtextstream.h>
#include <tqdatastream.h>
#include <tqcstring.h>
#include <tqregexp.h>

#include <tdeapplication.h>
#include <tdeconfig.h>
#include <kstandarddirs.h>
#include <tdemessagebox.h>
#include <tdelocale.h>
#include <tdeaction.h>
#include <kurl.h>
#include <kdebug.h>
#include <krfcdate.h>

#include <tdeio/slave.h>
#include <tdeio/scheduler.h>
#include <tdeio/slavebase.h>
#include <tdeio/davjob.h>
#include <tdeio/http.h>
#include <tdeio/job.h>

#include <libkcal/incidence.h>
#include <libkcal/event.h>
#include <libkcal/recurrence.h>
#include <libkcal/icalformat.h>
#include <libkcal/icalformatimpl.h>
#include <libkcal/calendarlocal.h>

extern "C" {
  #include <libical/ical.h>
}

#include "exchangeclient.h"
#include "exchangeaccount.h"
#include "exchangeprogress.h"
#include "utils.h"

#include "exchangedownload.h"

using namespace KPIM;

ExchangeDownload::ExchangeDownload( ExchangeAccount *account, TQWidget *window )
  : mWindow( window )
{
  kdDebug() << "ExchangeDownload()" << endl;

  mAccount = account;
  mDownloadsBusy = 0;
  mProgress = 0;
  mCalendar = 0;
  mFormat = new KCal::ICalFormat();
}

ExchangeDownload::~ExchangeDownload()
{
  kdDebug() << "ExchangeDownload destructor" << endl;
  delete mFormat;
  if ( mEvents ) delete mEvents;
}

void ExchangeDownload::download( KCal::Calendar *calendar, const TQDate &start,
                                 const TQDate &end, bool showProgress )
{
  mCalendar = calendar;
  mEvents = 0;

  if( showProgress ) {
#if 0
    //kdDebug() << "Creating progress dialog" << endl;
    mProgress = new ExchangeProgress();
    mProgress->show();

    connect( this, TQT_SIGNAL( startDownload() ), mProgress,
             TQT_SLOT( slotTransferStarted() ) );
    connect( this, TQT_SIGNAL(finishDownload() ), mProgress,
             TQT_SLOT( slotTransferFinished() ) );
#endif
  }

  TQString sql = dateSelectQuery( start, end.addDays( 1 ) );

  kdDebug() << "Exchange download query: " << endl << sql << endl;

  increaseDownloads();

  kdDebug() << "ExchangeDownload::download() davSearch URL: "
            << mAccount->calendarURL() << endl;

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

void ExchangeDownload::download( const TQDate& start, const TQDate& end, bool showProgress )
{
  mCalendar = 0;
  mEvents = new TQPtrList<KCal::Event>;

  if( showProgress ) {
    //kdDebug() << "Creating progress dialog" << endl;
    mProgress = new ExchangeProgress();
    mProgress->show();

    connect( this, TQT_SIGNAL(startDownload()), mProgress, TQT_SLOT(slotTransferStarted()) );
    connect( this, TQT_SIGNAL(finishDownload()), mProgress, TQT_SLOT(slotTransferFinished()) );
  }

  TQString sql = dateSelectQuery( start, end.addDays( 1 ) );

  increaseDownloads();

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

// Original query TODO: make query configurable
TQString ExchangeDownload::dateSelectQuery( const TQDate& start, const TQDate& end )
{
  TQString startString;
  startString.sprintf("%04i/%02i/%02i",start.year(),start.month(),start.day());
  TQString endString;
  endString.sprintf("%04i/%02i/%02i",end.year(),end.month(),end.day());
  TQString sql =
        "SELECT \"DAV:href\", \"urn:schemas:calendar:instancetype\", \"urn:schemas:calendar:uid\"\r\n"
        "FROM Scope('shallow traversal of \"\"')\r\n"
        "WHERE \"urn:schemas:calendar:dtend\" > '" + startString + "'\r\n"
        "AND \"urn:schemas:calendar:dtstart\" < '" + endString + "'";
  return sql;
}

#if 0
// That's the "new" code that breaks with Exchange. It was meant for Opengroupware, but that got its own resource anyway
TQString ExchangeDownload::dateSelectQuery( const TQDate& start, const TQDate& end )
{
  TQString startString;
  startString.sprintf( "%04i-%02i-%02iT00:00:00Z", start.year(),
                       start.month(), start.day() );
  TQString endString;
  endString.sprintf( "%04i-%02i-%02iT23:59:59Z", end.year(), end.month(),
                     end.day() );
  TQString sql =
        "SELECT \"DAV:href\", \"urn:schemas:calendar:instancetype\", "
        "\"urn:schemas:calendar:uid\"\r\n"
        "FROM Scope('shallow traversal of \"\"')\r\n"
        "WHERE \"urn:schemas:calendar:dtend\" > '" + startString + "'\r\n"
        "AND \"urn:schemas:calendar:dtstart\" < '" + endString + "'";
  return sql;
}
#endif

void ExchangeDownload::slotSearchResult( TDEIO::Job *job )
{
  if ( job->error() ) {
    kdError() << "ExchangeDownload::slotSearchResult() error: "
              << job->error() << endl;
    TQString text = i18n("ExchangeDownload\nError accessing '%1': %2")
                   .arg( mAccount->calendarURL().prettyURL() )
                   .arg( job->errorString() );
    KMessageBox::error( 0, text );
    finishUp( ExchangeClient::CommunicationError, job );
    return;
  }
  TQDomDocument &response = static_cast<TDEIO::DavJob *>( job )->response();

  kdDebug() << "Search result: " << endl << response.toString() << endl;

  handleAppointments( response, true );

  decreaseDownloads();
}

void ExchangeDownload::slotMasterResult( TDEIO::Job *job )
{
  if ( job->error() ) {
    kdError() << "Error result for Master search: " << job->error() << endl;
    job->showErrorDialog( 0 );
    finishUp( ExchangeClient::CommunicationError, job );
    return;
  }
  TQDomDocument &response = static_cast<TDEIO::DavJob *>( job )->response();

  kdDebug() << "Search (master) result: " << endl << response.toString() << endl;

  handleAppointments( response, false );

  decreaseDownloads();
}

void ExchangeDownload::handleAppointments( const TQDomDocument &response,
                                           bool recurrence )
{
  kdDebug() << "Entering handleAppointments" << endl;
  int successCount = 0;

  if ( response.documentElement().firstChild().toElement().isNull() ) {
    // Got an empty response, but no error. This would mean there are
    // no appointments in this time period.
    return;
  }

  for( TQDomElement item = response.documentElement().firstChild().toElement();
       !item.isNull();
       item = item.nextSibling().toElement() ) {
    //kdDebug() << "Current item:" << item.tagName() << endl;
    TQDomNodeList propstats = item.elementsByTagNameNS( "DAV:", "propstat" );
    // kdDebug() << "Item has " << propstats.count() << " propstat children" << endl;
    for( uint i=0; i < propstats.count(); i++ ) {
      TQDomElement propstat = propstats.item(i).toElement();
      TQDomElement prop = propstat.namedItem( "prop" ).toElement();
      if ( prop.isNull() ) {
        kdError() << "Error: no <prop> in response" << endl;
        continue;
      }

      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;
      }

      TQDomElement hrefElement = prop.namedItem( "href" ).toElement();
      if ( hrefElement.isNull() ) {
        kdError() << "Error: no href in Exchange server reply" << endl;
        continue;
      }
      TQString href = hrefElement.text();
      KURL url(href);

      kdDebug() << "Getting appointment from url: " << url.prettyURL() << endl;

      readAppointment( toDAV( url ) );
      successCount++;
    }
  }
  if ( !successCount ) {
    finishUp( ExchangeClient::ServerResponseError,
              "WebDAV SEARCH response:\n" + response.toString() );
  }
}

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;

  increaseDownloads();

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

void ExchangeDownload::readAppointment( const KURL& url )
{
  TQDomDocument doc;
  TQDomElement root = addElement( doc, doc, "DAV:", "propfind" );
  TQDomElement prop = addElement( doc, root, "DAV:", "prop" );
  addElement( doc, prop, "urn:schemas:calendar:", "uid" );
  addElement( doc, prop, "urn:schemas:calendar:", "timezoneid" );
  addElement( doc, prop, "urn:schemas:calendar:", "timezone" );
  addElement( doc, prop, "urn:schemas:calendar:", "lastmodified" );
  addElement( doc, prop, "urn:schemas:calendar:", "organizer" );
  addElement( doc, prop, "urn:schemas:calendar:", "contact" );
  addElement( doc, prop, "urn:schemas:httpmail:", "to" );
  addElement( doc, prop, "urn:schemas:calendar:", "attendeestatus" );
  addElement( doc, prop, "urn:schemas:calendar:", "attendeerole" );
  addElement( doc, prop, "DAV:", "isreadonly" );
  addElement( doc, prop, "urn:schemas:calendar:", "instancetype" );
  addElement( doc, prop, "urn:schemas:calendar:", "created" );
  addElement( doc, prop, "urn:schemas:calendar:", "dtstart" );
  addElement( doc, prop, "urn:schemas:calendar:", "dtend" );
  addElement( doc, prop, "urn:schemas:calendar:", "alldayevent" );
  addElement( doc, prop, "urn:schemas:calendar:", "transparent" );
  addElement( doc, prop, "urn:schemas:httpmail:", "textdescription" );
  addElement( doc, prop, "urn:schemas:httpmail:", "subject" );
  addElement( doc, prop, "urn:schemas:calendar:", "location" );
  addElement( doc, prop, "urn:schemas:calendar:", "rrule" );
  addElement( doc, prop, "urn:schemas:calendar:", "exdate" );
  addElement( doc, prop, "urn:schemas:mailheader:", "sensitivity" );
  addElement( doc, prop, "urn:schemas:calendar:", "reminderoffset" );

  addElement( doc, prop, "urn:schemas-microsoft-com:office:office",
              "Keywords" );

//  addElement( doc, prop, "", "" );
//  addElement( doc, prop, "DAV:", "" );
//  addElement( doc, prop, "urn:schemas:calendar:", "" );
//  addElement( doc, prop, "urn:content-classes:appointment", "" );
//  addElement( doc, prop, "urn:schemas:httpmail:", "" );

  increaseDownloads();

  TDEIO::DavJob* job = TDEIO::davPropFind( url, doc, "0", false );
  TDEIO::Scheduler::scheduleJob( job );
  job->setWindow( mWindow );
  job->addMetaData( "errorPage", "false" );
  connect( job, TQT_SIGNAL( result( TDEIO::Job * ) ),
           TQT_SLOT( slotPropFindResult( TDEIO::Job * ) ) );
}

void ExchangeDownload::slotPropFindResult( TDEIO::Job *job )
{
  kdDebug() << "slotPropFindResult" << endl;

  int error = job->error();
  if ( error ) {
    job->showErrorDialog( 0 );
    finishUp( ExchangeClient::CommunicationError, job );
    return;
  }

  TQDomDocument response = static_cast<TDEIO::DavJob *>( job )->response();
  kdDebug() << "Response: " << endl;
  kdDebug() << response.toString() << endl;

  TQDomElement prop = response.documentElement().namedItem( "response" )
                     .namedItem( "propstat" ).namedItem( "prop" ).toElement();

  KCal::Event* event = new KCal::Event();

  TQDomElement uidElement = prop.namedItem( "uid" ).toElement();
  if ( uidElement.isNull() ) {
    kdError() << "Error: no uid in Exchange server reply" << endl;
    finishUp( ExchangeClient::IllegalAppointmentError,
              "WebDAV server response:\n" + response.toString() );
    return;
  }
  event->setUid( uidElement.text() );
  // kdDebug() << "Got UID: " << uidElement.text() << endl;

  TQString timezoneid = prop.namedItem( "timezoneid" ).toElement().text();
  // kdDebug() << "DEBUG: timezoneid = " << timezoneid << endl;

  TQString timezone = prop.namedItem( "timezone" ).toElement().text();
  // kdDebug() << "DEBUG: timezone = " << timezone << endl;

  // 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
  }

  TQString lastModified = prop.namedItem( "lastmodified" ).toElement().text();
  if ( !lastModified.isEmpty() ) {
    TQDateTime dt = utcAsZone( TQDateTime::fromString( lastModified, Qt::ISODate ), localTimeZoneId );
    event->setLastModified( dt );
    kdDebug() << "Got lastModified:" << lastModified << ", " << dt.toString() << endl;
  }

  TQString organizer = prop.namedItem( "organizer" ).toElement().text();
  // TODO: Does outlook have a common name? Or does the organizer already contain both?
  event->setOrganizer( organizer );
  // kdDebug() << "Got organizer: " << organizer << endl;

  // Trying to find attendees, not working yet
  TQString contact = prop.namedItem( "contact" ).toElement().text();
//  event->setOrganizer( organizer );
  // kdDebug() << "DEBUG: Got contact: " << contact << endl;

  // This looks promising for finding attendees
  // FIXME: get this to work
  TQString to = prop.namedItem( "to" ).toElement().text();
  // kdDebug() << "DEBUG: Got to: " << to << endl;
  TQStringList attn = TQStringList::split( ",", to ); // This doesn't work: there can be commas between ""
  TQStringList::iterator it;
  for ( it = attn.begin(); it != attn.end(); ++it ) {
    // kdDebug() << "    attendee: " << (*it) << endl;
    TQString name = "";
    // KCal::Attendee* a = new KCal::Attendee( name, email );

    // event->addAttendee( a );
  }

  TQString readonly = prop.namedItem( "isreadonly" ).toElement().text();
  event->setReadOnly( readonly == "1" );
  kdDebug() << "Got readonly: " << readonly << ":" << (readonly != "0") << endl;

  TQString created = prop.namedItem( "created" ).toElement().text();
  if ( !created.isEmpty() ) {
    TQDateTime dt = utcAsZone( TQDateTime::fromString( created, Qt::ISODate ),
                    localTimeZoneId );
    event->setCreated( dt );
    kdDebug() << "got created: " << dt.toString() << endl;
  }

  TQString dtstart = prop.namedItem( "dtstart" ).toElement().text();
  if ( !dtstart.isEmpty() ) {
    TQDateTime dt = utcAsZone( TQDateTime::fromString( dtstart, Qt::ISODate ),
                              localTimeZoneId );
    event->setDtStart( dt );
    kdDebug() << "got dtstart: " << dtstart << " becomes in timezone " << dt.toString() << endl;
  }

  TQString alldayevent = prop.namedItem( "alldayevent" ).toElement().text();
  bool floats = alldayevent.toInt() != 0;
  event->setFloats( floats );
  kdDebug() << "Got alldayevent: \"" << alldayevent << "\":" << floats << endl;

  TQString dtend = prop.namedItem( "dtend" ).toElement().text();
  if ( !dtend.isEmpty() ) {
    TQDateTime dt = utcAsZone( TQDateTime::fromString( dtend, Qt::ISODate ),
                              localTimeZoneId );
    // Outlook thinks differently about floating event timing than libkcal
    if ( floats ) dt = dt.addDays( -1 );
    event->setDtEnd( dt );
    kdDebug() << "got dtend: " << dtend << " becomes in timezone " << dt.toString() << endl;
  }

  TQString transparent = prop.namedItem( "transparent" ).toElement().text();
  event->setTransparency( transparent.toInt() > 0 ? KCal::Event::Transparent
                           : KCal::Event::Opaque );
  // kdDebug() << "Got transparent: " << transparent << endl;

  TQString description = prop.namedItem( "textdescription" ).toElement().text();
  event->setDescription( description );
  kdDebug() << "Got description: " << description << endl;

  TQString subject = prop.namedItem( "subject" ).toElement().text();
  event->setSummary( subject );
  kdDebug() << "Got summary: " << subject << endl;

  TQString location =  prop.namedItem( "location" ).toElement().text();
  event->setLocation( location );
  // kdDebug() << "Got location: " << location << endl;

  TQString rrule = prop.namedItem( "rrule" ).toElement().text();
  kdDebug() << "Got rrule: " << rrule << endl;
  if ( !rrule.isEmpty() ) {
    // Timezone should be handled automatically
    // because we used mFormat->setTimeZone() earlier
    KCal::RecurrenceRule *rr = event->recurrence()->defaultRRule( true );

    if ( !rr || !mFormat->fromString( rr, rrule ) ) {
      kdError() << "ERROR parsing rrule " << rrule << endl;
    }
  }

  TQDomElement keywords = prop.namedItem( "Keywords" ).toElement();
  TQStringList categories;
  TQDomNodeList list = keywords.elementsByTagNameNS( "xml:", "v" );
  for( uint i=0; i < list.count(); i++ ) {
    TQDomElement item = list.item(i).toElement();
    categories.append( item.text() );
  }
  event->setCategories( categories );
  // kdDebug() << "Got categories: " << categories.join( ", " ) << endl;


  TQDomElement exdate = prop.namedItem( "exdate" ).toElement();
  KCal::DateList exdates;
  list = exdate.elementsByTagNameNS( "xml:", "v" );
  for( uint i=0; i < list.count(); i++ ) {
    TQDomElement item = list.item(i).toElement();
    TQDate date = utcAsZone( TQDateTime::fromString( item.text(), Qt::ISODate ), localTimeZoneId ).date();
    exdates.append( date );
    // kdDebug() << "Got exdate: " << date.toString() << endl;
  }
  event->recurrence()->setExDates( exdates );

  // Exchange sentitivity values:
  // 0 None
  // 1 Personal
  // 2 Private
  // 3 Company Confidential
  TQString sensitivity = prop.namedItem( "sensitivity" ).toElement().text();
  if ( ! sensitivity.isNull() )
  switch( sensitivity.toInt() ) {
    case 0: event->setSecrecy( KCal::Incidence::SecrecyPublic ); break;
    case 1: event->setSecrecy( KCal::Incidence::SecrecyPrivate ); break;
    case 2: event->setSecrecy( KCal::Incidence::SecrecyPrivate ); break;
    case 3: event->setSecrecy( KCal::Incidence::SecrecyConfidential ); break;
    default: kdWarning() << "Unknown sensitivity: " << sensitivity << endl;
  }
  // kdDebug() << "Got sensitivity: " << sensitivity << endl;


  TQString reminder = prop.namedItem( "reminderoffset" ).toElement().text();
  // kdDebug() << "Reminder offset: " << reminder << endl;
  if ( !reminder.isEmpty() ) {
    // Duration before event in seconds
    KCal::Duration offset( - reminder.toInt() );
    KCal::Alarm *alarm = event->newAlarm();
    alarm->setStartOffset( offset );
    alarm->setDisplayAlarm("");
    alarm->setEnabled( true );
    // TODO: multiple alarms; alarm->setType( KCal::Alarm::xxxx );
  }
  /** Create a new alarm which is associated with this incidence */
    //Alarm* newAlarm();
    /** Add an alarm which is associated with this incidence */
    //void addAlarm(Alarm*);

    /** point at some other event to which the event relates */
    //void setRelatedTo(Incidence *relatedTo);
    /** Add an event which is related to this event */
    //void addRelation(Incidence *);

    /** set the list of attachments/associated files for this event */
    //void setAttachments(const TQStringList &attachments);

     /** set resources used, such as Office, Car, etc. */
    //void setResources(const TQStringList &resources);

    /** set the event's priority, 0 is undefined, 1 highest (decreasing order) */
    //void setPriority(int priority);

    /**
      Add Attendee to this incidence. IncidenceBase takes ownership of the
      Attendee object.
    */

    //void addAttendee(Attendee *a, bool doupdate=true );

  // THE FOLLOWING EVENT PROPERTIES ARE NOT READ

  // Revision ID in webdav is a String, not an int
    /** set the number of revisions this event has seen */
    //void setRevision(int rev);

  // Problem: When you sync Outlook to a Palm, the conduit splits up
  // multi-day events into single-day events WITH ALL THE SAME UID
  // Grrrrrrr.
  if ( mCalendar ) {
    KCal::Event *oldEvent = mCalendar->event( event->uid() );
    if ( oldEvent ) {
      kdWarning() << "Already got the event, replace it..." << endl;
      mCalendar->deleteEvent( oldEvent );
    }
    kdDebug() << "ADD EVENT" << endl;
    mCalendar->addEvent( event );
  } else {
    kdDebug() << "EMIT gotEvent" << endl;
    emit gotEvent( event, static_cast<TDEIO::DavJob *>( job )->url() );
//    mEvents->append( event );
  }

  decreaseDownloads();
}

void ExchangeDownload::increaseDownloads()
{
  mDownloadsBusy++;
  emit startDownload();
}

void ExchangeDownload::decreaseDownloads()
{
  mDownloadsBusy--;
  // kdDebug() << "Download finished, waiting for " << mDownloadsBusy << " more" << endl;
  emit finishDownload();
  if ( mDownloadsBusy == 0 ) {
    kdDebug() << "All downloads finished" << endl;
    finishUp( ExchangeClient::ResultOK );
  }
}

void ExchangeDownload::finishUp( int result, const TQString &moreInfo )
{
  kdDebug() << "ExchangeDownload::finishUp() " << result << " "
            << moreInfo << endl;

  if ( mCalendar ) mCalendar->setModified( true );
  // Disconnect from progress bar
  if ( mProgress ) {
    disconnect( this, 0, mProgress, 0 );
    disconnect( mProgress, 0, this, 0 );
    mProgress->delayedDestruct();
  }

//  if ( mEvents ) {
//    emit finished( this, result, moreInfo, *mEvents );
//  } else {
    emit finished( this, result, moreInfo );
//  }
}

void ExchangeDownload::finishUp( int result, TDEIO::Job *job )
{
  finishUp( result, TQString("WebDAV job error code = ") +
                    TQString::number( job->error() ) + ";\n" + "\"" +
                    job->errorString() + "\"" );
}

#include "exchangedownload.moc"