summaryrefslogtreecommitdiffstats
path: root/libkcal/incidence.cpp
blob: 0c27da432727d3ff2cc75112d73d032d766bf74c (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
/*
    This file is part of libkcal.

    Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
    Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>

    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 <kglobal.h>
#include <klocale.h>
#include <kdebug.h>

#include "calformat.h"

#include "incidence.h"
#include "calendar.h"

using namespace KCal;

Incidence::Incidence() :
  IncidenceBase(),
  mRelatedTo(0), mtqStatus(StatusNone), mSecrecy(SecrecyPublic),
  mPriority(0), mRecurrence(0),
  mHasRecurrenceID( false ), mChildRecurrenceEvents()
{
  recreate();

  mAlarms.setAutoDelete(true);
  mAttachments.setAutoDelete(true);
}

Incidence::Incidence( const Incidence &i ) : IncidenceBase( i ),Recurrence::Observer()
{
// TODO: reenable attributes currently commented out.
  mRevision = i.mRevision;
  mCreated = i.mCreated;
  mDescription = i.mDescription;
  mSummary = i.mSummary;
  mCategories = i.mCategories;
//  Incidence *mRelatedTo;          Incidence *mRelatedTo;
  mRelatedTo = 0;
  mRelatedToUid = i.mRelatedToUid;
//  Incidence::List mRelations;    Incidence::List mRelations;
  mResources = i.mResources;
  mStatusString = i.mStatusString;
  mtqStatus = i.mtqStatus;
  mSecrecy = i.mSecrecy;
  mPriority = i.mPriority;
  mLocation = i.mLocation;
  mRecurrenceID = i.mRecurrenceID;
  mHasRecurrenceID = i.mHasRecurrenceID;
  mChildRecurrenceEvents = i.mChildRecurrenceEvents;

  // Alarms and Attachments are stored in ListBase<...>, which is a TQValueList<...*>.
  // We need to really duplicate the objects stored therein, otherwise deleting
  // i will also delete all attachments from this object (setAutoDelete...)
  Alarm::List::ConstIterator it;
  for( it = i.mAlarms.begin(); it != i.mAlarms.end(); ++it ) {
    Alarm *b = new Alarm( **it );
    b->setParent( this );
    mAlarms.append( b );
  }
  mAlarms.setAutoDelete(true);

  Attachment::List::ConstIterator it1;
  for ( it1 = i.mAttachments.begin(); it1 != i.mAttachments.end(); ++it1 ) {
    Attachment *a = new Attachment( **it1 );
    mAttachments.append( a );
  }
  mAttachments.setAutoDelete( true );

  if (i.mRecurrence) {
    mRecurrence = new Recurrence( *(i.mRecurrence) );
    mRecurrence->addObserver( this );
  } else
    mRecurrence = 0;

  mSchedulingID = i.mSchedulingID;
}

Incidence::~Incidence()
{
    Incidence::List Relations = mRelations;
    List::ConstIterator it;
    for ( it = Relations.begin(); it != Relations.end(); ++it ) {
        if ( (*it)->relatedTo() == this ) (*it)->mRelatedTo = 0;
    }
    if ( relatedTo() ) relatedTo()->removeRelation( this );

    delete mRecurrence;
}

// A string comparison that considers that null and empty are the same
static bool stringCompare( const TQString& s1, const TQString& s2 )
{
  return ( s1.isEmpty() && s2.isEmpty() ) || (s1 == s2);
}

Incidence& Incidence::operator=( const Incidence &i )
{
  if ( &i == this )
    return *this;
  IncidenceBase::operator=( i );
  mRevision = i.mRevision;
  mCreated = i.mCreated;
  mDescription = i.mDescription;
  mSummary = i.mSummary;
  mCategories = i.mCategories;
  mRelatedTo = 0;
  mRelatedToUid = i.mRelatedToUid;
  mRelations.clear();
  mResources = i.mResources;
  mStatusString = i.mStatusString;
  mtqStatus = i.mtqStatus;
  mSecrecy = i.mSecrecy;
  mPriority = i.mPriority;
  mLocation = i.mLocation;
  mRecurrenceID = i.mRecurrenceID;
  mHasRecurrenceID = i.mHasRecurrenceID;
  mChildRecurrenceEvents = i.mChildRecurrenceEvents;

  mAlarms.clearAll();
  Alarm::List::ConstIterator it;
  for( it = i.mAlarms.begin(); it != i.mAlarms.end(); ++it ) {
    Alarm *b = new Alarm( **it );
    b->setParent( this );
    mAlarms.append( b );
  }

  mAttachments.clearAll();
  Attachment::List::ConstIterator it1;
  for ( it1 = i.mAttachments.begin(); it1 != i.mAttachments.end(); ++it1 ) {
    Attachment *a = new Attachment( **it1 );
    mAttachments.append( a );
  }

  delete mRecurrence;
  if (i.mRecurrence) {
    mRecurrence = new Recurrence( *(i.mRecurrence) );
    mRecurrence->addObserver( this );
  } else
    mRecurrence = 0;

  mSchedulingID = i.mSchedulingID;
  return *this;
}

bool Incidence::operator==( const Incidence& i2 ) const
{
    if( alarms().count() != i2.alarms().count() ) {
        return false; // no need to check further
    }

    Alarm::List::ConstIterator a1 = alarms().begin();
    Alarm::List::ConstIterator a2 = i2.alarms().begin();
    for( ; a1 != alarms().end() && a2 != i2.alarms().end(); ++a1, ++a2 )
        if( **a1 == **a2 )
            continue;
        else {
            return false;
        }

    if ( !IncidenceBase::operator==(i2) )
        return false;

    bool recurrenceEqual = ( mRecurrence == 0 && i2.mRecurrence == 0 );
    if ( !recurrenceEqual )
    {
        recurrenceEqual = mRecurrence != 0 &&
                          i2.mRecurrence != 0 &&
                          *mRecurrence == *i2.mRecurrence;
    }

    return
        recurrenceEqual &&
        created() == i2.created() &&
        stringCompare( description(), i2.description() ) &&
        stringCompare( summary(), i2.summary() ) &&
        categories() == i2.categories() &&
        // no need to compare mRelatedTo
        stringCompare( relatedToUid(), i2.relatedToUid() ) &&
        relations() == i2.relations() &&
        attachments() == i2.attachments() &&
        resources() == i2.resources() &&
        mtqStatus == i2.mtqStatus &&
        ( mtqStatus == StatusNone || stringCompare( mStatusString, i2.mStatusString ) ) &&
        secrecy() == i2.secrecy() &&
        priority() == i2.priority() &&
        stringCompare( location(), i2.location() ) &&
        stringCompare( schedulingID(), i2.schedulingID() );
}


void Incidence::recreate()
{
  setCreated(TQDateTime::tqcurrentDateTime());

  setUid(CalFormat::createUniqueId());
  setSchedulingID( TQString() );

  setRevision(0);

  setLastModified(TQDateTime::tqcurrentDateTime());
  setPilotId( 0 );
  setSynctqStatus( SYNCNONE );
}

void Incidence::setReadOnly( bool readOnly )
{
  IncidenceBase::setReadOnly( readOnly );
  if ( mRecurrence )
    mRecurrence->setRecurReadOnly( readOnly );
}

void Incidence::setFloats(bool f)
{
  if (mReadOnly) return;
  if ( recurrence() )
    recurrence()->setFloats( f );
  IncidenceBase::setFloats( f );
}

void Incidence::setCreated( const TQDateTime &created )
{
  if (mReadOnly) return;
  mCreated = created;

// FIXME: Shouldn't we call updated for the creation date, too?
//  updated();
}

TQDateTime Incidence::created() const
{
  return mCreated;
}

void Incidence::setRevision( int rev )
{
  if (mReadOnly) return;
  mRevision = rev;

  updated();
}

int Incidence::revision() const
{
  return mRevision;
}

void Incidence::setDtStart(const TQDateTime &dtStart)
{
  if ( mRecurrence ) {
    mRecurrence->setStartDateTime( dtStart );
    mRecurrence->setFloats( doesFloat() );
  }
  IncidenceBase::setDtStart( dtStart );
}

void Incidence::setDescription(const TQString &description)
{
  if (mReadOnly) return;
  mDescription = description;
  updated();
}

TQString Incidence::description() const
{
  return mDescription;
}


void Incidence::setSummary(const TQString &summary)
{
  if (mReadOnly) return;
  mSummary = summary;
  updated();
}

TQString Incidence::summary() const
{
  return mSummary;
}

void Incidence::setCategories(const TQStringList &categories)
{
  if (mReadOnly) return;
  mCategories = categories;
  updated();
}

// TODO: remove setCategories(TQString) function
void Incidence::setCategories(const TQString &catStr)
{
  if (mReadOnly) return;
  mCategories.clear();

  if (catStr.isEmpty()) return;

  mCategories = TQStringList::split(",",catStr);

  TQStringList::Iterator it;
  for(it = mCategories.begin();it != mCategories.end(); ++it) {
    *it = (*it).stripWhiteSpace();
  }

  updated();
}

TQStringList Incidence::categories() const
{
  return mCategories;
}

TQString Incidence::categoriesStr() const
{
  return mCategories.join(",");
}

void Incidence::setRelatedToUid(const TQString &relatedToUid)
{
  if ( mReadOnly || mRelatedToUid == relatedToUid ) return;
  mRelatedToUid = relatedToUid;
  updated();
}

TQString Incidence::relatedToUid() const
{
  return mRelatedToUid;
}

void Incidence::setRelatedTo(Incidence *relatedTo)
{
  if (mReadOnly || mRelatedTo == relatedTo) return;
  if(mRelatedTo)
    mRelatedTo->removeRelation(this);
  mRelatedTo = relatedTo;
  if (mRelatedTo) {
    mRelatedTo->addRelation(this);
    if ( mRelatedTo->uid() != mRelatedToUid )
      setRelatedToUid( mRelatedTo->uid() );
  } else {
    setRelatedToUid( TQString() );
  }
}

Incidence *Incidence::relatedTo() const
{
  return mRelatedTo;
}

Incidence::List Incidence::relations() const
{
  return mRelations;
}

void Incidence::addRelation( Incidence *event )
{
  if ( mRelations.find( event ) == mRelations.end() ) {
    mRelations.append( event );
  }
}

void Incidence::removeRelation(Incidence *event)
// Remove the relation of our incident. E.g. if you have a task t and a
// subtask, the subtask will have its relation to the task t.
{
  mRelations.removeRef(event);
//  if (event->getRelatedTo() == this) event->setRelatedTo(0);
  mRelatedToUid=TQString();
}


// %%%%%%%%%%%%  Recurrence-related methods %%%%%%%%%%%%%%%%%%%%


Recurrence *Incidence::recurrence() const
{
  if (!mRecurrence)
  {
    const_cast<KCal::Incidence*>(this)->mRecurrence = new Recurrence();
    mRecurrence->setStartDateTime( IncidenceBase::dtStart() );
    mRecurrence->setFloats( doesFloat() );
    mRecurrence->setRecurReadOnly( mReadOnly );
    mRecurrence->addObserver( const_cast<KCal::Incidence*>(this) );
  }

  return mRecurrence;
}

void Incidence::clearRecurrence()
{
  delete mRecurrence;
  mRecurrence = 0;
}

uint Incidence::recurrenceType() const
{
  if ( mRecurrence ) return mRecurrence->recurrenceType();
  else return Recurrence::rNone;
}

bool Incidence::doesRecur() const
{
  if ( mRecurrence ) return mRecurrence->doesRecur();
  else return false;
}

bool Incidence::recursOn(const TQDate &qd) const
{
  bool doesRecur = false;
  doesRecur = mRecurrence && mRecurrence->recursOn(qd);

  return doesRecur;
}

bool Incidence::recursAt(const TQDateTime &qdt) const
{
  bool doesRecur = false;
  doesRecur = mRecurrence && mRecurrence->recursAt(qdt);

  return doesRecur;
}

bool Incidence::recursOn(const TQDate &qd, Calendar *cal) const
{
  bool doesRecur = false;
  doesRecur = mRecurrence && mRecurrence->recursOn(qd);

  // Make sure that this instance has not been moved through a RECURRENCE-ID statement
  if (hasRecurrenceID() == false) {
    IncidenceList il = childIncidences();
    IncidenceListIterator it;
    for ( it = il.begin(); it != il.end(); ++it ) {
      TQDateTime modifiedDt = cal->incidence(*it)->recurrenceID();
      modifiedDt.setTime(TQTime());
      if (TQDateTime(qd) == modifiedDt) {
        doesRecur = false;
      }
    }
  }

  return doesRecur;
}

bool Incidence::recursAt(const TQDateTime &qdt, Calendar *cal) const
{
  bool doesRecur = false;
  doesRecur = mRecurrence && mRecurrence->recursAt(qdt);

  // Make sure that this instance has not been moved through a RECURRENCE-ID statement
  if (hasRecurrenceID() == false) {
    IncidenceList il = childIncidences();
    IncidenceListIterator it;
    for ( it = il.begin(); it != il.end(); ++it ) {
      if (qdt == cal->incidence(*it)->recurrenceID()) {
        doesRecur = false;
      }
    }
  }

  return doesRecur;
}

/**
  Calculates the start date/time for all recurrences that happen at some time
  on the given date (might start before that date, but end on or after the
  given date).
  @param date the date where the incidence should occur
  @return the start date/time of all occurences that overlap with the given
      date. Empty list if the incidence does not overlap with the date at all
*/
TQValueList<TQDateTime> Incidence::startDateTimesForDate( const TQDate &date ) const
{
//kdDebug(5800) << "Incidence::startDateTimesForDate " << date << ", incidence=" << summary() << endl;
  TQDateTime start = dtStart();
  TQDateTime end = endDateRecurrenceBase();

  TQValueList<TQDateTime> result;

  // TODO_Recurrence: Also work if only due date is given...
  if ( !start.isValid() && ! end.isValid() ) {
    return result;
  }

  // if the incidence doesn't recur,
  if ( !doesRecur() ) {
    if ( !(start.date() > date || end.date() < date ) ) {
      result << start;
    }
    return result;
  }

  int days = start.daysTo( end );
  // Account for possible recurrences going over midnight, while the original event doesn't
  TQDate tmpday( date.addDays( -days - 1 ) );
  TQDateTime tmp;
  while ( tmpday <= date ) {
    if ( recurrence()->recursOn( tmpday ) ) {
      TQValueList<TQTime> times = recurrence()->recurTimesOn( tmpday );
      for ( TQValueList<TQTime>::ConstIterator it = times.begin(); it != times.end(); ++it ) {
        tmp = TQDateTime( tmpday, *it );
        if ( endDateForStart( tmp ).date() >= date )
          result << tmp;
      }
    }
    tmpday = tmpday.addDays( 1 );
  }
  return result;
}

/**
  Calculates the start date/time for all recurrences that happen at the given
  time.
  @param datetime the date/time where the incidence should occur
  @return the start date/time of all occurences that overlap with the given
      date/time. Empty list if the incidence does not happen at the given
      time at all.
*/
TQValueList<TQDateTime> Incidence::startDateTimesForDateTime( const TQDateTime &datetime ) const
{
// kdDebug(5800) << "Incidence::startDateTimesForDateTime " << datetime << ", incidence=" << summary() << endl;
  TQDateTime start = dtStart();
  TQDateTime end = endDateRecurrenceBase();

  TQValueList<TQDateTime> result;

  // TODO_Recurrence: Also work if only due date is given...
  if ( !start.isValid() && ! end.isValid() ) {
    return result;
  }

  // if the incidence doesn't recur,
  if ( !doesRecur() ) {
    if ( !(start > datetime || end < datetime ) ) {
      result << start;
    }
    return result;
  }

  int days = start.daysTo( end );
  // Account for possible recurrences going over midnight, while the original event doesn't
  TQDate tmpday( datetime.date().addDays( -days - 1 ) );
  TQDateTime tmp;
  while ( tmpday <= datetime.date() ) {
    if ( recurrence()->recursOn( tmpday ) ) {
      TQValueList<TQTime> times = recurrence()->recurTimesOn( tmpday );
      for ( TQValueList<TQTime>::ConstIterator it = times.begin(); it != times.end(); ++it ) {
        tmp = TQDateTime( tmpday, *it );
        if ( !(tmp > datetime || endDateForStart( tmp ) < datetime ) )
          result << tmp;
      }
    }
    tmpday = tmpday.addDays( 1 );
  }
  return result;
}

/** Return the end time of the occurrence if it starts at the given date/time */
TQDateTime Incidence::endDateForStart( const TQDateTime &startDt ) const
{
  TQDateTime start = dtStart();
  TQDateTime end = endDateRecurrenceBase();
  if ( !end.isValid() ) return start;
  if ( !start.isValid() ) return end;

  return startDt.addSecs( start.secsTo( end ) );
}

// %%%%%%%%%%%%%%%%% begin:RecurrenceRule %%%%%%%%%%%%%%%%%

// Exception Dates
/*void Incidence::setExDates(const DateList &exDates)
{
  if ( mReadOnly ) return;
  recurrence()->setExDates( exDates );
  updated();
}

void Incidence::addExDate( const TQDate &date )
{
  if ( mReadOnly ) return;
  recurrence()->addExDate( date );
  updated();
}

DateList Incidence::exDates() const
{
  if ( mRecurrence ) return mRecurrence->exDates();
  else return DateList();
}


// Exception DateTimes
void Incidence::setExDateTimes( const DateTimeList &exDates )
{
  if ( mReadOnly ) return;
  recurrence()->setExDateTimes( exDates );
  updated();
}

void Incidence::addExDateTime( const TQDateTime &date )
{
  if ( mReadOnly ) return;
  recurrence()->addExDateTime( date );
  updated();
}

DateTimeList Incidence::exDateTimes() const
{
  if ( mRecurrence ) return mRecurrence->exDateTimes();
  else return DateTimeList();
}


// Recurrence Dates
void Incidence::setRDates(const DateList &exDates)
{
  if ( mReadOnly ) return;
  recurrence()->setRDates( exDates );
  updated();
}

void Incidence::addRDate( const TQDate &date )
{
  if ( mReadOnly ) return;
  recurrence()->addRDate( date );
  updated();
}

DateList Incidence::rDates() const
{
  if ( mRecurrence ) return mRecurrence->rDates();
  else return DateList();
}


// Recurrence DateTimes
void Incidence::setRDateTimes( const DateTimeList &exDates )
{
  if ( mReadOnly ) return;
  recurrence()->setRDateTimes( exDates );
  updated();
}

void Incidence::addRDateTime( const TQDateTime &date )
{
  if ( mReadOnly ) return;
  recurrence()->addRDateTime( date );
  updated();
}

DateTimeList Incidence::rDateTimes() const
{
  if ( mRecurrence ) return mRecurrence->rDateTimes();
  else return DateTimeList();
}*/

// %%%%%%%%%%%%%%%%% end:RecurrenceRule %%%%%%%%%%%%%%%%%

void Incidence::addAttachment(Attachment *attachment)
{
  if (mReadOnly || !attachment) return;
  mAttachments.append(attachment);
  updated();
}

void Incidence::deleteAttachment(Attachment *attachment)
{
  mAttachments.removeRef(attachment);
}

void Incidence::deleteAttachments( const TQString &mime )
{
  Attachment::List::Iterator it = mAttachments.begin();
  while( it != mAttachments.end() ) {
    if ( (*it)->mimeType() == mime ) mAttachments.remove( it );
    else ++it;
  }
}

Attachment::List Incidence::attachments() const
{
  return mAttachments;
}

Attachment::List Incidence::attachments(const TQString& mime) const
{
  Attachment::List attachments;
  Attachment::List::ConstIterator it;
  for( it = mAttachments.begin(); it != mAttachments.end(); ++it ) {
    if ( (*it)->mimeType() == mime ) attachments.append( *it );
  }

  return attachments;
}

void Incidence::clearAttachments()
{
  mAttachments.clearAll();
}

void Incidence::setResources(const TQStringList &resources)
{
  if (mReadOnly) return;
  mResources = resources;
  updated();
}

TQStringList Incidence::resources() const
{
  return mResources;
}


void Incidence::setPriority(int priority)
{
  if (mReadOnly) return;
  mPriority = priority;
  updated();
}

int Incidence::priority() const
{
  return mPriority;
}

void Incidence::setqStatus(Incidence::tqStatus status)
{
  if (mReadOnly || status == StatusX) return;
  mtqStatus = status;
  mStatusString = TQString();
  updated();
}

void Incidence::setCustomtqStatus(const TQString &status)
{
  if (mReadOnly) return;
  mtqStatus = status.isEmpty() ? StatusNone : StatusX;
  mStatusString = status;
  updated();
}

Incidence::tqStatus Incidence::status() const
{
  return mtqStatus;
}

TQString Incidence::statusStr() const
{
  if (mtqStatus == StatusX)
    return mStatusString;
  return statusName(mtqStatus);
}

TQString Incidence::statusName(Incidence::tqStatus status)
{
  switch (status) {
    case StatusTentative:    return i18n("incidence status", "Tentative");
    case StatusConfirmed:    return i18n("Confirmed");
    case StatusCompleted:    return i18n("Completed");
    case StatusNeedsAction:  return i18n("Needs-Action");
    case StatusCanceled:     return i18n("Canceled");
    case StatusInProcess:    return i18n("In-Process");
    case StatusDraft:        return i18n("Draft");
    case StatusFinal:        return i18n("Final");
    case StatusX:
    case StatusNone:
    default:                 return TQString();
  }
}

void Incidence::setSecrecy(int sec)
{
  if (mReadOnly) return;
  mSecrecy = sec;
  updated();
}

int Incidence::secrecy() const
{
  return mSecrecy;
}

TQString Incidence::secrecyStr() const
{
  return secrecyName(mSecrecy);
}

TQString Incidence::secrecyName(int secrecy)
{
  switch (secrecy) {
    case SecrecyPublic:
      return i18n("Public");
    case SecrecyPrivate:
      return i18n("Private");
    case SecrecyConfidential:
      return i18n("Confidential");
    default:
      return i18n("Undefined");
  }
}

TQStringList Incidence::secrecyList()
{
  TQStringList list;
  list << secrecyName(SecrecyPublic);
  list << secrecyName(SecrecyPrivate);
  list << secrecyName(SecrecyConfidential);

  return list;
}


const Alarm::List &Incidence::alarms() const
{
  return mAlarms;
}

Alarm* Incidence::newAlarm()
{
  Alarm* alarm = new Alarm(this);
  mAlarms.append(alarm);
//  updated();
  return alarm;
}

void Incidence::addAlarm(Alarm *alarm)
{
  mAlarms.append(alarm);
  updated();
}

void Incidence::removeAlarm(Alarm *alarm)
{
  mAlarms.removeRef(alarm);
  updated();
}

void Incidence::clearAlarms()
{
  mAlarms.clearAll();
  updated();
}

bool Incidence::isAlarmEnabled() const
{
  Alarm::List::ConstIterator it;
  for( it = mAlarms.begin(); it != mAlarms.end(); ++it ) {
    if ( (*it)->enabled() ) return true;
  }
  return false;
}

void Incidence::setLocation(const TQString &location)
{
  if (mReadOnly) return;
  mLocation = location;
  updated();
}

TQString Incidence::location() const
{
  return mLocation;
}

void Incidence::setSchedulingID( const TQString& sid )
{
  mSchedulingID = sid;
}

TQString Incidence::schedulingID() const
{
  if ( mSchedulingID.isNull() )
    // Nothing set, so use the normal uid
    return uid();
  return mSchedulingID;
}

bool Incidence::hasRecurrenceID() const
{
  return mHasRecurrenceID;
}

void Incidence::setHasRecurrenceID( bool hasRecurrenceID )
{
  if ( mReadOnly ) {
    return;
  }

  mHasRecurrenceID = hasRecurrenceID;
  updated();
}

TQDateTime Incidence::recurrenceID() const
{
  return mRecurrenceID;
}

void Incidence::setRecurrenceID( const TQDateTime &recurrenceID )
{
  if ( mReadOnly ) {
    return;
  }

//   update();
  mRecurrenceID = recurrenceID;
  updated();
}

void Incidence::addChildIncidence( TQString childIncidence )
{
  mChildRecurrenceEvents.append(childIncidence);
}

void Incidence::deleteChildIncidence( TQString childIncidence )
{
  mChildRecurrenceEvents.remove(childIncidence);
}

IncidenceList Incidence::childIncidences() const
{
  return mChildRecurrenceEvents;
}

/** Observer interface for the recurrence class. If the recurrence is changed,
    this method will be called for the incidence the recurrence object
    belongs to. */
void Incidence::recurrenceUpdated( Recurrence *recurrence )
{
  if ( recurrence == mRecurrence )
    updated();
}