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