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