]> err.no Git - scalable-opengroupware.org/blob - UI/Scheduler/UIxCalInlineAptView.m
b989711f82a13732169639636e24f631e580f15e
[scalable-opengroupware.org] / UI / Scheduler / UIxCalInlineAptView.m
1 /*
2  Copyright (C) 2000-2004 SKYRIX Software AG
3  
4  This file is part of OGo
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 // $Id$
22
23 #import <math.h>
24
25 #import <Foundation/NSDictionary.h>
26
27 #import <NGObjWeb/NGObjWeb.h>
28 #import <SOGo/SOGoAuthenticator.h>
29 #import <NGCards/iCalEntityObject.h>
30
31 @interface UIxCalInlineAptView : WOComponent
32 {
33   NSDictionary *appointment;
34   id formatter;
35   id tooltipFormatter;
36   id url;
37   id style;
38   id queryDictionary;
39   id referenceDate;
40   int dayStartHour;
41   int dayEndHour;
42   BOOL canAccess;
43 }
44
45 @end
46
47 #include "common.h"
48 #include <SOGoUI/SOGoAptFormatter.h>
49 #include <SOGo/SOGoUser.h>
50 #include <NGObjWeb/WOContext+SoObjects.h>
51
52 @implementation UIxCalInlineAptView
53
54 - (id) init
55 {
56   if ((self = [super init]))
57     {
58       dayStartHour = 0;
59       dayEndHour = 24;
60       appointment = nil;
61     }
62
63   return self;
64 }
65
66 - (void) dealloc
67 {
68   [appointment release];
69   [formatter release];
70   [tooltipFormatter release];
71   [url release];
72   [style release];
73   [queryDictionary release];
74   [referenceDate release];
75   [super dealloc];
76 }
77
78 - (void) setAppointment: (NSDictionary *) _appointment
79 {
80   ASSIGN(appointment, _appointment);
81 }
82
83 - (NSDictionary *) appointment
84 {
85   return appointment;
86 }
87
88 - (void) setDayStartHour: (unsigned int) anHour
89 {
90   dayStartHour = anHour;
91 }
92
93 - (void) setDayEndHour: (unsigned int) anHour
94 {
95   dayEndHour = anHour;
96 }
97
98 - (void) setFormatter: (id) _formatter
99 {
100   ASSIGN(formatter, _formatter);
101 }
102
103 - (id) formatter
104 {
105   return formatter;
106 }
107
108 - (void) setTooltipFormatter: (id) _tooltipFormatter
109 {
110   ASSIGN(tooltipFormatter, _tooltipFormatter);
111 }
112
113 - (id) tooltipFormatter
114 {
115   return tooltipFormatter;
116 }
117
118 - (void) setUrl: (id) _url
119 {
120   ASSIGN(url, _url);
121 }
122
123 - (id) url
124 {
125   return url;
126 }
127
128 - (void) setStyle: (id) _style 
129 {
130   NSMutableString *ms;
131   NSNumber *prio;
132   NSString *s;
133   NSString *email;
134
135   if (_style)
136     ms = [NSMutableString stringWithString: _style];
137   else
138     ms = (NSMutableString *)[NSMutableString string];
139
140   if ((prio = [appointment valueForKey:@"priority"])) {
141     [ms appendFormat:@" apt_prio%@", prio];
142   }
143   email = [[[self context] activeUser] email];
144   s = [appointment valueForKey:@"orgmail"];
145   if ([s isNotNull])
146     {
147       if ([s rangeOfString: email].length > 0)
148         [ms appendString:@" apt_organizer"];
149       else
150         [ms appendString:@" apt_other"];
151     }
152   s = [appointment valueForKey:@"partmails"];
153   if ([s isNotNull])
154     {
155       if ([s rangeOfString:email].length > 0)
156         [ms appendString:@" apt_participant"];
157       else
158         [ms appendString:@" apt_nonparticipant"];
159     }
160   ASSIGNCOPY(style, ms);
161 }
162
163 - (id)style {
164   return style;
165 }
166
167 - (void) setQueryDictionary: (id) _queryDictionary
168 {
169   ASSIGN(queryDictionary, _queryDictionary);
170 }
171
172 - (id) queryDictionary
173 {
174   return queryDictionary;
175 }
176
177 - (void) setReferenceDate: (id) _referenceDate
178 {
179   ASSIGN(referenceDate, _referenceDate);
180 }
181
182 - (id) referenceDate
183 {
184   return referenceDate;
185 }
186
187 - (void) setCanAccess: (BOOL) _canAccess
188 {
189   canAccess = _canAccess;
190 }
191
192 - (BOOL) canAccess
193 {
194   return canAccess;
195 }
196
197 - (NSString *) displayClasses
198 {
199   NSTimeInterval secondsStart, secondsEnd, delta;
200   NSCalendarDate *startDate;
201   int deltaStart, deltaLength;
202
203   startDate = [appointment objectForKey: @"startDate"];
204   secondsStart = [startDate timeIntervalSince1970];
205   secondsEnd = [[appointment objectForKey: @"endDate"] timeIntervalSince1970];
206   delta = (secondsEnd - [startDate timeIntervalSince1970]) / 60;
207   deltaLength = delta / 15;
208   if (((int) delta % 15) > 0)
209     deltaLength += 1;
210
211   deltaStart = (([startDate hourOfDay] * 60 + [startDate minuteOfHour]
212                  - dayStartHour * 60) / 15);
213
214   return [NSString stringWithFormat: @"appointment starts%d lasts%d",
215                    deltaStart, deltaLength, [startDate dayOfWeek]];
216 }
217
218 - (NSString *) innerDisplayClasses
219 {
220   return [NSString stringWithFormat: @"appointmentInside ownerIs%@",
221                    [appointment objectForKey: @"owner"]];
222 }
223
224 - (NSString *) displayStyle
225 {
226   NSCalendarDate *startDate, *endDate, *dayStart, *dayEnd;
227   int sSeconds, eSeconds, deltaMinutes;
228   unsigned int height;
229   NSTimeZone *uTZ;
230
231   uTZ = [referenceDate timeZone];
232   dayStart = [referenceDate beginOfDay];
233   dayEnd = [referenceDate endOfDay];
234
235   sSeconds = [[appointment objectForKey: @"startdate"] intValue];
236   eSeconds = [[appointment objectForKey: @"enddate"] intValue];
237   startDate = [NSCalendarDate dateWithTimeIntervalSince1970: sSeconds];
238   [startDate setTimeZone: uTZ];
239   if ([startDate earlierDate: dayStart] == startDate)
240     startDate = dayStart;
241   endDate = [NSCalendarDate dateWithTimeIntervalSince1970: eSeconds];
242   [endDate setTimeZone: uTZ];
243   if ([endDate earlierDate: dayEnd] == dayEnd)
244     endDate = dayEnd;
245
246   deltaMinutes = (([endDate hourOfDay] - [startDate hourOfDay]) * 60
247                   + [endDate minuteOfHour] - [startDate minuteOfHour]);
248   height = ceil(deltaMinutes / 15) * 25;
249
250   return [NSString stringWithFormat: @"height: %d%%;", height];
251 }
252
253 /* helpers */
254
255 - (NSString *) startHour
256 {
257   NSCalendarDate *start;
258
259   start = [appointment objectForKey: @"startDate"];
260
261   return [NSString stringWithFormat: @"%.2d:%.2d",
262                    [start hourOfDay], [start minuteOfHour]];
263 }
264
265 - (NSString *) title
266 {
267   return [formatter stringForObjectValue: appointment
268                     referenceDate: [self referenceDate]];
269 }
270
271 - (NSString *) tooltip
272 {
273   return [tooltipFormatter stringForObjectValue: appointment
274                            referenceDate: [self referenceDate]];
275 }
276
277 - (BOOL) _userIsInTheCard: (NSString *) email
278 {
279   NSString *orgMailString, *partMailsString;
280   NSArray *partMails;
281   BOOL userIsInTheCard;
282
283   orgMailString = [appointment objectForKey: @"orgmail"];
284   if ([orgMailString isNotNull] && [orgMailString isEqualToString: email])
285     userIsInTheCard = YES;
286   else
287     {
288       partMailsString = [appointment objectForKey: @"partmails"];
289       if ([partMailsString isNotNull])
290         {
291           partMails = [partMailsString componentsSeparatedByString: @"\n"];
292           userIsInTheCard = [partMails containsObject: email];
293         }
294       else
295         userIsInTheCard = NO;
296     }
297
298   return userIsInTheCard;
299 }
300
301 - (BOOL) titleShouldBeHidden
302 {
303   BOOL shouldBeHidden;
304   SOGoUser *user;
305   SOGoAuthenticator *sAuth;
306
307   sAuth = [SOGoAuthenticator sharedSOGoAuthenticator];
308   user = [sAuth userInContext: context];
309
310   if ([[appointment objectForKey: @"owner"] isEqualToString: [user login]]
311       || ([[appointment objectForKey: @"classification"] intValue]
312           != iCalAccessConfidential))
313     shouldBeHidden = NO;
314   else
315     shouldBeHidden = ![self _userIsInTheCard: [user email]];
316
317   return shouldBeHidden;
318 }
319
320 @end