]> err.no Git - scalable-opengroupware.org/blob - UI/Scheduler/UIxAppointmentView.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1017 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / Scheduler / UIxAppointmentView.m
1 /*
2   Copyright (C) 2004-2005 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
22 #import "UIxAppointmentView.h"
23 #import <NGCards/NGCards.h>
24 #import <SOGo/WOContext+Agenor.h>
25 #import <Appointments/SOGoAppointmentObject.h>
26 #import <SOGoUI/SOGoDateFormatter.h>
27 #import "UIxComponent+Agenor.h"
28 #import "common.h"
29
30 @interface UIxAppointmentView (PrivateAPI)
31 - (BOOL)isAttendeeActiveUser;
32 @end
33
34 @implementation UIxAppointmentView
35
36 - (void)dealloc {
37   [appointment   release];
38   [attendee      release];
39   [dateFormatter release];
40   [item          release];
41   [super dealloc];
42 }
43
44 /* accessors */
45
46 - (NSString *)tabSelection {
47   NSString *selection;
48     
49   selection = [self queryParameterForKey:@"tab"];
50   if (selection == nil)
51     selection = @"attributes";
52   return selection;
53 }
54
55 - (void)setAttendee:(id)_attendee {
56   ASSIGN(attendee, _attendee);
57 }
58 - (id)attendee {
59   return attendee;
60 }
61
62 - (BOOL)isAttendeeActiveUser {
63   NSString *email, *attEmail;
64
65   email    = [[[self context] activeUser] email];
66   attendee = [self attendee];
67   attEmail = [attendee rfc822Email];
68
69   return [email isEqualToString: attEmail];
70 }
71
72 - (BOOL)showAcceptButton {
73   return [[self attendee] participationStatus] != iCalPersonPartStatAccepted;
74 }
75 - (BOOL)showRejectButton {
76   return [[self attendee] participationStatus] != iCalPersonPartStatDeclined;
77 }
78 - (NSString *)attendeeStatusColspan {
79   return [self isAttendeeActiveUser] ? @"1" : @"2";
80 }
81
82 - (void)setItem:(id)_item {
83   ASSIGN(item, _item);
84 }
85 - (id)item {
86   return item;
87 }
88
89 - (SOGoDateFormatter *)dateFormatter {
90   if (dateFormatter == nil) {
91     dateFormatter =
92       [[SOGoDateFormatter alloc] initWithLocale:[self locale]];
93     [dateFormatter setFullWeekdayNameAndDetails];
94   }
95   return dateFormatter;
96 }
97
98 - (NSCalendarDate *)startTime {
99   NSCalendarDate *date;
100     
101   date = [[self appointment] startDate];
102   [date setTimeZone:[[self clientObject] userTimeZone]];
103   return date;
104 }
105
106 - (NSCalendarDate *)endTime {
107   NSCalendarDate *date;
108   
109   date = [[self appointment] endDate];
110   [date setTimeZone:[[self clientObject] userTimeZone]];
111   return date;
112 }
113
114 - (NSString *)resourcesAsString {
115   NSArray *resources, *cns;
116
117   resources = [[self appointment] resources];
118   cns = [resources valueForKey:@"cnForDisplay"];
119   return [cns componentsJoinedByString:@"<br />"];
120 }
121
122 - (NSString *) categoriesAsString
123 {
124   NSEnumerator *categories;
125   NSArray *rawCategories;
126   NSMutableArray *l10nCategories;
127   NSString *currentCategory, *l10nCategory;
128
129   rawCategories
130     = [[appointment categories] componentsSeparatedByString: @","];
131   l10nCategories = [NSMutableArray arrayWithCapacity: [rawCategories count]];
132   categories = [rawCategories objectEnumerator];
133   currentCategory = [categories nextObject];
134   while (currentCategory)
135     {
136       l10nCategory
137         = [self labelForKey: [currentCategory stringByTrimmingSpaces]];
138       if (l10nCategory)
139         [l10nCategories addObject: l10nCategory];
140       currentCategory = [categories nextObject];
141     }
142
143   return [l10nCategories componentsJoinedByString: @", "];
144 }
145
146 // appointment.organizer.cnForDisplay
147 - (NSString *) eventOrganizer
148 {
149   CardElement *organizer;
150
151   organizer = [[self appointment] uniqueChildWithTag: @"organizer"];
152
153   return [organizer value: 0 ofAttribute: @"cn"];
154 }
155
156 - (NSString *) priorityLabelKey
157 {
158   return [NSString stringWithFormat: @"prio_%@", [appointment priority]];
159 }
160
161 /* backend */
162
163 - (iCalEvent *) appointment
164 {
165   NSString *iCalString;
166   iCalCalendar *calendar;
167   SOGoAppointmentObject *clientObject;
168     
169   if (appointment != nil)
170     return appointment;
171
172   clientObject = [self clientObject];
173
174   iCalString = [[self clientObject] valueForKey:@"iCalString"];
175   if (![iCalString isNotNull] || [iCalString length] == 0) {
176     [self errorWithFormat:@"(%s): missing iCal string!", 
177             __PRETTY_FUNCTION__];
178     return nil;
179   }
180
181   calendar = [iCalCalendar parseSingleFromSource: iCalString];
182   appointment = [clientObject firstEventFromCalendar: calendar];
183   [appointment retain];
184
185   return appointment;
186 }
187
188 /* hrefs */
189
190 - (NSString *)attributesTabLink {
191   return [self completeHrefForMethod:[self ownMethodName]
192                withParameter:@"attributes"
193                forKey:@"tab"];
194 }
195
196 - (NSString *)participantsTabLink {
197     return [self completeHrefForMethod:[self ownMethodName]
198                  withParameter:@"participants"
199                  forKey:@"tab"];
200 }
201
202 - (NSString *)debugTabLink {
203   return [self completeHrefForMethod:[self ownMethodName]
204                withParameter:@"debug"
205                forKey:@"tab"];
206 }
207
208 - (NSString *)completeHrefForMethod:(NSString *)_method
209   withParameter:(NSString *)_param
210   forKey:(NSString *)_key
211 {
212   NSString *href;
213
214   [self setQueryParameter:_param forKey:_key];
215   href = [self completeHrefForMethod:[self ownMethodName]];
216   [self setQueryParameter:nil forKey:_key];
217   return href;
218 }
219
220
221 /* access */
222
223 - (BOOL)isMyApt {
224   NSString   *email;
225   iCalPerson *organizer;
226
227   email     = [[[self context] activeUser] email];
228   organizer = [[self appointment] organizer];
229   if (!organizer) return YES; // assume this is correct to do, right?
230   return [[organizer rfc822Email] isEqualToString:email];
231 }
232
233 - (BOOL)canAccessApt {
234   NSString *email;
235   NSArray  *partMails;
236
237   if ([self isMyApt])
238     return YES;
239   
240   /* not my apt - can access if it's public */
241   if ([[[self appointment] accessClass] isEqualToString: @"PUBLIC"])
242     return YES;
243
244   /* can access it if I'm invited :-) */
245   email     = [[[self context] activeUser] email];
246   partMails = [[[self appointment] participants] valueForKey:@"rfc822Email"];
247   return [partMails containsObject:email];
248 }
249
250 - (BOOL)canEditApt {
251   return [self isMyApt];
252 }
253
254
255 /* action */
256
257 - (id<WOActionResults>)defaultAction {
258   if ([self appointment] == nil) {
259     return [NSException exceptionWithHTTPStatus:404 /* Not Found */
260                         reason:@"could not locate appointment"];
261   }
262   
263   return self;
264 }
265
266 - (BOOL)isDeletableClientObject {
267   return [[self clientObject] respondsToSelector:@selector(delete)];
268 }
269
270 - (id)deleteAction {
271   NSException *ex;
272   id url;
273
274   if ([self appointment] == nil) {
275     return [NSException exceptionWithHTTPStatus:404 /* Not Found */
276                         reason:@"could not locate appointment"];
277   }
278
279   if (![self isDeletableClientObject]) {
280     /* return 400 == Bad Request */
281     return [NSException exceptionWithHTTPStatus:400
282                         reason:@"method cannot be invoked on "
283                                @"the specified object"];
284   }
285   
286   if ((ex = [[self clientObject] delete]) != nil) {
287     // TODO: improve error handling
288     [self debugWithFormat:@"failed to delete: %@", ex];
289     return ex;
290   }
291   
292   url = [[[self clientObject] container] baseURLInContext:[self context]];
293   return [self redirectToLocation:url];
294 }
295
296 @end /* UIxAppointmentView */