]> err.no Git - sope/blob - sope-ical/NGiCal/iCalEventChanges.m
new differ and accompanied functionality
[sope] / sope-ical / NGiCal / iCalEventChanges.m
1 /*
2  Copyright (C) 2004 SKYRIX Software AG
3
4  This file is part of OpenGroupware.org.
5
6  OGo 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  OGo 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 OGo; 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 // $Id$
22
23
24 #include "iCalEventChanges.h"
25 #include "iCalEvent.h"
26 #include "iCalPerson.h"
27 #include "common.h"
28
29 @interface iCalEventChanges (PrivateAPI)
30 - (void)_trackAttendeeChanges:(iCalEvent *)_from :(iCalEvent *)_to;
31 - (void)_trackAlarmChanges:(iCalEvent *)_from :(iCalEvent *)_to;
32 - (void)_trackPropertyChanges:(iCalEvent *)_from :(iCalEvent *)_to;
33 @end
34
35 @implementation iCalEventChanges
36
37 + (id)changesFromEvent:(iCalEvent *)_from toEvent:(iCalEvent *)_to {
38   return [[[self alloc] initWithFromEvent:_from toEvent:_to] autorelease];
39 }
40
41 - (id)initWithFromEvent:(iCalEvent *)_from toEvent:(iCalEvent *)_to {
42   self = [super init];
43   if(self) {
44     self->insertedAttendees = [[NSMutableArray alloc] init];
45     self->deletedAttendees  = [[NSMutableArray alloc] init];
46     self->updatedAttendees  = [[NSMutableArray alloc] init];
47     self->insertedAlarms    = [[NSMutableArray alloc] init];
48     self->deletedAlarms     = [[NSMutableArray alloc] init];
49     self->updatedAlarms     = [[NSMutableArray alloc] init];
50     self->updatedProperties = [[NSMutableArray alloc] init];
51     [self _trackAttendeeChanges:_from :_to];
52     [self _trackPropertyChanges:_from :_to];
53   }
54   return self;
55 }
56
57 - (void)dealloc {
58   [self->insertedAttendees release];
59   [self->deletedAttendees  release];
60   [self->updatedAttendees  release];
61   [self->insertedAlarms    release];
62   [self->deletedAlarms     release];
63   [self->updatedAlarms     release];
64   [self->updatedProperties release];
65   [super dealloc];
66 }
67
68 - (void)_trackAttendeeChanges:(iCalEvent *)_from :(iCalEvent *)_to {
69   unsigned f, t, fcount, tcount;
70   NSArray *fromAttendees, *toAttendees;
71
72   fromAttendees = [_from attendees];
73   fcount = [fromAttendees count];
74   toAttendees = [_to attendees];
75   tcount = [toAttendees count];
76   for(f = 0; f < fcount; f++) {
77     iCalPerson *fp;
78     BOOL found = NO;
79
80     fp = [fromAttendees objectAtIndex:f];
81
82     for(t = 0; t < tcount; t++) {
83       iCalPerson *tp;
84
85       tp = [toAttendees objectAtIndex:t];
86       if([fp hasSameEmailAddress:tp]) {
87         found = YES;
88         if(![fp isEqualToPerson:tp]) {
89           [self->updatedAttendees addObject:tp];
90         }
91         break;
92       }
93     }
94     if(!found) {
95       [self->deletedAttendees addObject:fp];
96     }
97   }
98   for(t = 0; t < tcount; t++) {
99     iCalPerson *tp;
100     BOOL found = NO;
101
102     tp = [toAttendees objectAtIndex:t];
103     for(f = 0; f < fcount; f++) {
104       iCalPerson *fp;
105
106       fp = [fromAttendees objectAtIndex:f];
107       if([tp hasSameEmailAddress:fp]) {
108         found = YES;
109         break;
110       }
111     }
112     if(!found)
113       [self->insertedAttendees addObject:tp];
114   }
115 }
116
117 - (void)_trackAlarmChanges:(iCalEvent *)_from :(iCalEvent *)_to {
118 }
119
120 - (void)_trackPropertyChanges:(iCalEvent *)_from :(iCalEvent *)_to {
121   if(!IS_EQUAL([_from startDate], [_to startDate], isEqualToDate:))
122     [self->updatedProperties addObject:@"startDate"];
123   if(!IS_EQUAL([_from endDate], [_to endDate], isEqualToDate:))
124     [self->updatedProperties addObject:@"endDate"];
125   if(!IS_EQUAL([_from created], [_to created], isEqualToDate:))
126     [self->updatedProperties addObject:@"created"];
127   if(!IS_EQUAL([_from lastModified], [_to lastModified], isEqualToDate:))
128     [self->updatedProperties addObject:@"lastModified"];
129   if(![_from durationAsTimeInterval] == [_to durationAsTimeInterval])
130     [self->updatedProperties addObject:@"duration"];
131   if(!IS_EQUAL([_from summary], [_to summary], isEqualToString:))
132     [self->updatedProperties addObject:@"summary"];
133   if(!IS_EQUAL([_from location], [_to location], isEqualToString:))
134     [self->updatedProperties addObject:@"location"];
135   if(!IS_EQUAL([_from comment], [_to comment], isEqualToString:))
136     [self->updatedProperties addObject:@"comment"];
137   if(!IS_EQUAL([_from priority], [_to priority], isEqualToString:))
138     [self->updatedProperties addObject:@"priority"];
139   if(!IS_EQUAL([_from status], [_to status], isEqualToString:))
140     [self->updatedProperties addObject:@"status"];
141   if(!IS_EQUAL([_from accessClass], [_to accessClass], isEqualToString:))
142     [self->updatedProperties addObject:@"accessClass"];
143   if(!IS_EQUAL([_from sequence], [_to sequence], isEqualToNumber:))
144     [self->updatedProperties addObject:@"sequence"];
145   if(!IS_EQUAL([_from organizer], [_to organizer], isEqual:))
146     [self->updatedProperties addObject:@"organizer"];
147 }
148
149 - (BOOL)hasChanges {
150   return [self hasAttendeeChanges]  ||
151          [self hasAlarmChanges]     ||
152          [self hasPropertyChanges];
153 }
154
155 - (BOOL)hasAttendeeChanges {
156   return [[self insertedAttendees] count] > 0 ||
157          [[self deletedAttendees]  count] > 0 ||
158          [[self updatedAttendees]  count] > 0;
159 }
160
161 - (BOOL)hasAlarmChanges {
162   return [[self insertedAlarms] count] > 0 ||
163          [[self deletedAlarms]  count] > 0 ||
164          [[self updatedAlarms]  count] > 0;
165 }
166
167 - (BOOL)hasPropertyChanges {
168   return [[self updatedProperties] count] > 0;
169 }
170
171 - (NSArray *)insertedAttendees {
172   return self->insertedAttendees;
173 }
174 - (NSArray *)deletedAttendees {
175   return self->deletedAttendees;
176 }
177 - (NSArray *)updatedAttendees {
178   return self->updatedAttendees;
179 }
180
181 - (NSArray *)insertedAlarms {
182   return self->insertedAlarms;
183 }
184 - (NSArray *)deletedAlarms {
185   return self->deletedAlarms;
186 }
187 - (NSArray *)updatedAlarms {
188   return self->updatedAlarms;
189 }
190
191 - (NSArray *)updatedProperties {
192   return self->updatedProperties;
193 }
194
195 /* descriptions */
196
197 - (NSString *)description {
198   NSMutableString *ms;
199   
200   ms = [NSMutableString stringWithCapacity:128];
201   [ms appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
202   
203   [ms appendFormat:@" updatedProperties=%@", self->updatedProperties];
204   [ms appendFormat:@" insertedAttendees=%@", self->insertedAttendees];
205   [ms appendFormat:@" deletedAttendees=%@", self->deletedAttendees];
206   [ms appendFormat:@" updatedAttendees=%@", self->updatedAttendees];
207   
208   [ms appendString:@">"];
209   return ms;
210 }
211
212 @end