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