]> err.no Git - scalable-opengroupware.org/blob - UI/Scheduler/UIxTaskView.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1045 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / Scheduler / UIxTaskView.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 "UIxTaskView.h"
23 #import <NGCards/NGCards.h>
24 #import <SOGo/WOContext+Agenor.h>
25 #import <Appointments/SOGoTaskObject.h>
26 #import <SOGoUI/SOGoDateFormatter.h>
27 #import "UIxComponent+Agenor.h"
28 #import "common.h"
29
30 @interface UIxTaskView (PrivateAPI)
31 - (BOOL)isAttendeeActiveUser;
32 @end
33
34 @implementation UIxTaskView
35
36 - (void)dealloc {
37   [task   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 {
100   NSCalendarDate *date;
101   NSTimeZone *timeZone;
102
103   timeZone = [[context activeUser] timeZone];
104   date = [[self task] startDate];
105   [date setTimeZone: timeZone];
106
107   return date;
108 }
109
110 - (NSCalendarDate *) endTime
111 {
112   NSCalendarDate *date;
113   NSTimeZone *timeZone;
114
115   timeZone = [[context activeUser] timeZone];
116   date = [[self task] due];
117   [date setTimeZone: timeZone];
118
119   return date;
120 }
121
122 - (NSString *)resourcesAsString {
123   NSArray *resources, *cns;
124
125   resources = [[self task] resources];
126   cns = [resources valueForKey:@"cnForDisplay"];
127   return [cns componentsJoinedByString:@"<br />"];
128 }
129
130 - (NSString *) categoriesAsString
131 {
132   NSEnumerator *categories;
133   NSArray *rawCategories;
134   NSMutableArray *l10nCategories;
135   NSString *currentCategory, *l10nCategory;
136
137   rawCategories
138     = [[task categories] componentsSeparatedByString: @","];
139   l10nCategories = [NSMutableArray arrayWithCapacity: [rawCategories count]];
140   categories = [rawCategories objectEnumerator];
141   currentCategory = [categories nextObject];
142   while (currentCategory)
143     {
144       l10nCategory
145         = [self labelForKey: [currentCategory stringByTrimmingSpaces]];
146       if (l10nCategory)
147         [l10nCategories addObject: l10nCategory];
148       currentCategory = [categories nextObject];
149     }
150
151   return [l10nCategories componentsJoinedByString: @", "];
152 }
153
154 // task.organizer.cnForDisplay
155 - (NSString *) eventOrganizer
156 {
157   CardElement *organizer;
158
159   organizer = [[self task] uniqueChildWithTag: @"organizer"];
160
161   return [organizer value: 0 ofAttribute: @"cn"];
162 }
163
164 - (NSString *) priorityLabelKey
165 {
166   return [NSString stringWithFormat: @"prio_%@", [task priority]];
167 }
168
169 /* backend */
170
171 - (iCalToDo *) task
172 {
173   SOGoTaskObject *clientObject;
174
175   if (!task)
176     {
177       clientObject = [self clientObject];
178       task = (iCalToDo *) [clientObject component: NO];
179       [task retain];
180     }
181
182   return task;
183 }
184
185 /* hrefs */
186
187 - (NSString *)attributesTabLink {
188   return [self completeHrefForMethod:[self ownMethodName]
189                withParameter:@"attributes"
190                forKey:@"tab"];
191 }
192
193 - (NSString *)participantsTabLink {
194     return [self completeHrefForMethod:[self ownMethodName]
195                  withParameter:@"participants"
196                  forKey:@"tab"];
197 }
198
199 - (NSString *)debugTabLink {
200   return [self completeHrefForMethod:[self ownMethodName]
201                withParameter:@"debug"
202                forKey:@"tab"];
203 }
204
205 - (NSString *)completeHrefForMethod:(NSString *)_method
206   withParameter:(NSString *)_param
207   forKey:(NSString *)_key
208 {
209   NSString *href;
210
211   [self setQueryParameter:_param forKey:_key];
212   href = [self completeHrefForMethod:[self ownMethodName]];
213   [self setQueryParameter:nil forKey:_key];
214   return href;
215 }
216
217
218 /* access */
219
220 - (BOOL)isMyApt {
221   NSString   *email;
222   iCalPerson *organizer;
223
224   email     = [[[self context] activeUser] email];
225   organizer = [[self task] organizer];
226   if (!organizer) return YES; // assume this is correct to do, right?
227   return [[organizer rfc822Email] isEqualToString:email];
228 }
229
230 - (BOOL)canAccessApt {
231   NSString *email;
232   NSArray  *partMails;
233
234   if ([self isMyApt])
235     return YES;
236   
237   /* not my apt - can access if it's public */
238   if ([[[self task] accessClass] isEqualToString: @"PUBLIC"])
239     return YES;
240
241   /* can access it if I'm invited :-) */
242   email     = [[[self context] activeUser] email];
243   partMails = [[[self task] participants] valueForKey:@"rfc822Email"];
244   return [partMails containsObject:email];
245 }
246
247 - (BOOL)canEditApt {
248   return [self isMyApt];
249 }
250
251
252 /* action */
253
254 - (id<WOActionResults>)defaultAction {
255   if ([self task] == nil) {
256     return [NSException exceptionWithHTTPStatus:404 /* Not Found */
257                         reason:@"could not locate task"];
258   }
259   
260   return self;
261 }
262
263 - (BOOL)isDeletableClientObject {
264   return [[self clientObject] respondsToSelector:@selector(delete)];
265 }
266
267 - (id)deleteAction {
268   NSException *ex;
269   id url;
270
271   if ([self task] == nil) {
272     return [NSException exceptionWithHTTPStatus:404 /* Not Found */
273                         reason:@"could not locate task"];
274   }
275
276   if (![self isDeletableClientObject]) {
277     /* return 400 == Bad Request */
278     return [NSException exceptionWithHTTPStatus:400
279                         reason:@"method cannot be invoked on "
280                                @"the specified object"];
281   }
282   
283   if ((ex = [[self clientObject] delete]) != nil) {
284     // TODO: improve error handling
285     [self debugWithFormat:@"failed to delete: %@", ex];
286     return ex;
287   }
288   
289   url = [[[self clientObject] container] baseURLInContext:[self context]];
290   return [self redirectToLocation:url];
291 }
292
293 @end /* UIxTaskView */