1/*
2  Copyright (C) 2000-2005 SKYRIX Software AG
3
4  This file is part of SOPE.
5
6  SOPE is free software; you can redistribute it and/or modify it under
7  the terms of the GNU Lesser General Public License as published by the
8  Free Software Foundation; either version 2, or (at your option) any
9  later version.
10
11  SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12  WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14  License for more details.
15
16  You should have received a copy of the GNU Lesser General Public
17  License along with SOPE; see the file COPYING.  If not, write to the
18  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19  02111-1307, USA.
20*/
21
22#import <Foundation/NSString.h>
23
24#import "NSCalendarDate+NGCards.h"
25
26#import "iCalDateTime.h"
27#import "iCalToDo.h"
28
29#import <NGExtensions/NGCalendarDateRange.h>
30
31@implementation iCalToDo
32
33- (Class) classForTag: (NSString *) classTag
34{
35  Class tagClass;
36
37  if ([classTag isEqualToString: @"DUE"]
38      || [classTag isEqualToString: @"COMPLETED"])
39    tagClass = [iCalDateTime class];
40  else if ([classTag isEqualToString: @"PERCENT-COMPLETE"])
41    tagClass = [CardElement class];
42  else
43    tagClass = [super classForTag: classTag];
44
45  return tagClass;
46}
47
48/* accessors */
49
50- (void) setPercentComplete: (NSString *) _value
51{
52  [[self uniqueChildWithTag: @"percent-complete"] setSingleValue: _value
53                                                          forKey: @""];
54}
55
56- (NSString *) percentComplete
57{
58  return [[self uniqueChildWithTag: @"percent-complete"]
59           flattenedValuesForKey: @""];
60}
61
62- (void) setDue: (NSCalendarDate *) newDueDate
63{
64  [(iCalDateTime *) [self uniqueChildWithTag: @"due"]
65		    setDateTime: newDueDate];
66}
67
68- (NSCalendarDate *) due
69{
70  return [(iCalDateTime *) [self uniqueChildWithTag: @"due"]
71			   dateTime];
72}
73
74- (void) setCompleted: (NSCalendarDate *) newCompletedDate
75{
76  [(iCalDateTime *) [self uniqueChildWithTag: @"completed"]
77		    setDateTime: newCompletedDate];
78  if (newCompletedDate)
79    [self setStatus: @"COMPLETED"];
80  else
81    [self setStatus: @"IN-PROCESS"];
82}
83
84- (NSCalendarDate *) completed
85{
86  return [(iCalDateTime *) [self uniqueChildWithTag: @"completed"]
87			   dateTime];
88}
89
90/* ical typing */
91
92- (NSString *) entityName
93{
94  return @"vtodo";
95}
96
97// /* descriptions */
98
99// - (NSString *)description {
100//   NSMutableString *ms;
101
102//   ms = [NSMutableString stringWithCapacity:128];
103//   [ms appendFormat:@"<0x%p[%@]:", self, NSStringFromClass([self class])];
104
105//   if (uid)       [ms appendFormat:@" uid=%@", uid];
106//   if (startDate) [ms appendFormat:@" start=%@", startDate];
107//   if (due)       [ms appendFormat:@" due=%@", due];
108//   if (priority)  [ms appendFormat:@" pri=%@", priority];
109
110//   if (completed)
111//     [ms appendFormat:@" completed=%@", completed];
112//   if (percentComplete)
113//     [ms appendFormat:@" complete=%@", percentComplete];
114//   if (accessClass)
115//     [ms appendFormat:@" class=%@", accessClass];
116
117//   if (summary)
118//     [ms appendFormat:@" summary=%@", summary];
119
120//   [ms appendString:@">"];
121//   return ms;
122// }
123
124- (NSCalendarDate *) lastPossibleRecurrenceStartDate
125{
126  NGCalendarDateRange *fir;
127
128  if (![self isRecurrent])
129    return nil;
130
131  fir = [NGCalendarDateRange calendarDateRangeWithStartDate: [self startDate]
132                             endDate: [self due]];
133
134  return [self lastPossibleRecurrenceStartDateUsingFirstInstanceCalendarDateRange: fir];
135}
136
137@end /* iCalToDo */
138