]> err.no Git - scalable-opengroupware.org/blob - UI/Scheduler/UIxTaskView.m
c2004dd858910b63b11c9969d1c18692bbf0b856
[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   NSCalendarDate *date;
100     
101   date = [[self task] startDate];
102   [date setTimeZone:[[self clientObject] userTimeZone]];
103   return date;
104 }
105
106 - (NSCalendarDate *)endTime {
107   NSCalendarDate *date;
108   
109   date = [[self task] due];
110   [date setTimeZone:[[self clientObject] userTimeZone]];
111   return date;
112 }
113
114 - (NSString *)resourcesAsString {
115   NSArray *resources, *cns;
116
117   resources = [[self task] 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     = [[task 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 // task.organizer.cnForDisplay
147 - (NSString *) eventOrganizer
148 {
149   CardElement *organizer;
150
151   organizer = [[self task] uniqueChildWithTag: @"organizer"];
152
153   return [organizer value: 0 ofAttribute: @"cn"];
154 }
155
156 - (NSString *) priorityLabelKey
157 {
158   return [NSString stringWithFormat: @"prio_%@", [task priority]];
159 }
160
161 /* backend */
162
163 - (iCalToDo *) task
164 {
165   SOGoTaskObject *clientObject;
166
167   if (!task)
168     {
169       clientObject = [self clientObject];
170       task = (iCalToDo *) [clientObject component: NO];
171       [task retain];
172     }
173
174   return task;
175 }
176
177 /* hrefs */
178
179 - (NSString *)attributesTabLink {
180   return [self completeHrefForMethod:[self ownMethodName]
181                withParameter:@"attributes"
182                forKey:@"tab"];
183 }
184
185 - (NSString *)participantsTabLink {
186     return [self completeHrefForMethod:[self ownMethodName]
187                  withParameter:@"participants"
188                  forKey:@"tab"];
189 }
190
191 - (NSString *)debugTabLink {
192   return [self completeHrefForMethod:[self ownMethodName]
193                withParameter:@"debug"
194                forKey:@"tab"];
195 }
196
197 - (NSString *)completeHrefForMethod:(NSString *)_method
198   withParameter:(NSString *)_param
199   forKey:(NSString *)_key
200 {
201   NSString *href;
202
203   [self setQueryParameter:_param forKey:_key];
204   href = [self completeHrefForMethod:[self ownMethodName]];
205   [self setQueryParameter:nil forKey:_key];
206   return href;
207 }
208
209
210 /* access */
211
212 - (BOOL)isMyApt {
213   NSString   *email;
214   iCalPerson *organizer;
215
216   email     = [[[self context] activeUser] email];
217   organizer = [[self task] organizer];
218   if (!organizer) return YES; // assume this is correct to do, right?
219   return [[organizer rfc822Email] isEqualToString:email];
220 }
221
222 - (BOOL)canAccessApt {
223   NSString *email;
224   NSArray  *partMails;
225
226   if ([self isMyApt])
227     return YES;
228   
229   /* not my apt - can access if it's public */
230   if ([[[self task] accessClass] isEqualToString: @"PUBLIC"])
231     return YES;
232
233   /* can access it if I'm invited :-) */
234   email     = [[[self context] activeUser] email];
235   partMails = [[[self task] participants] valueForKey:@"rfc822Email"];
236   return [partMails containsObject:email];
237 }
238
239 - (BOOL)canEditApt {
240   return [self isMyApt];
241 }
242
243
244 /* action */
245
246 - (id<WOActionResults>)defaultAction {
247   if ([self task] == nil) {
248     return [NSException exceptionWithHTTPStatus:404 /* Not Found */
249                         reason:@"could not locate task"];
250   }
251   
252   return self;
253 }
254
255 - (BOOL)isDeletableClientObject {
256   return [[self clientObject] respondsToSelector:@selector(delete)];
257 }
258
259 - (id)deleteAction {
260   NSException *ex;
261   id url;
262
263   if ([self task] == nil) {
264     return [NSException exceptionWithHTTPStatus:404 /* Not Found */
265                         reason:@"could not locate task"];
266   }
267
268   if (![self isDeletableClientObject]) {
269     /* return 400 == Bad Request */
270     return [NSException exceptionWithHTTPStatus:400
271                         reason:@"method cannot be invoked on "
272                                @"the specified object"];
273   }
274   
275   if ((ex = [[self clientObject] delete]) != nil) {
276     // TODO: improve error handling
277     [self debugWithFormat:@"failed to delete: %@", ex];
278     return ex;
279   }
280   
281   url = [[[self clientObject] container] baseURLInContext:[self context]];
282   return [self redirectToLocation:url];
283 }
284
285 @end /* UIxTaskView */