2 Copyright (C) 2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
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
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.
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
21 // $Id: UIxAppointmentEditor.m 181 2004-08-11 15:13:25Z helge $
23 #include <SOGoUI/UIxComponent.h>
25 @interface UIxAppointmentProposal : UIxComponent
30 /* individual values */
43 NSArray *participants; /* array of iCalPerson's */
44 NSArray *resources; /* array of iCalPerson's */
47 NSArray *blockedRanges;
48 NSMutableDictionary *currentQueryParameters;
51 - (NSMutableDictionary *)currentQueryParameters;
52 - (NSDictionary *)currentHourQueryParametersForFirstHalf:(BOOL)_first;
54 - (void)setICalPersons:(NSArray *)_ps asQueryParameter:(NSString *)_qp;
57 #include <SoObjects/Appointments/SOGoAppointmentFolder.h>
58 #include <SoObjects/Appointments/SOGoFreeBusyObject.h>
59 #include <NGExtensions/NGCalendarDateRange.h>
60 #include <NGCards/NGCards.h>
63 @implementation UIxAppointmentProposal
66 [self->blockedRanges release];
68 [self->startDateHour release];
69 [self->startDateMinute release];
70 [self->startDateDay release];
71 [self->startDateMonth release];
72 [self->startDateYear release];
74 [self->endDateHour release];
75 [self->endDateMinute release];
76 [self->endDateDay release];
77 [self->endDateMonth release];
78 [self->endDateYear release];
79 [self->duration release];
81 [self->participants release];
82 [self->resources release];
84 [self->currentQueryParameters release];
91 [self->currentDay release]; self->currentDay = nil;
92 [self->item release]; self->item = nil;
98 - (void)setItem:(id)_item {
99 ASSIGN(self->item, _item);
105 - (void)setStartDateHour:(id)_startDateHour {
106 ASSIGN(self->startDateHour, _startDateHour);
108 - (id)startDateHour {
109 return self->startDateHour;
111 - (void)setStartDateMinute:(id)_startDateMinute {
112 ASSIGN(self->startDateMinute, _startDateMinute);
114 - (id)startDateMinute {
115 return self->startDateMinute;
117 - (void)setStartDateDay:(id)_startDateDay {
118 ASSIGN(self->startDateDay, _startDateDay);
121 return self->startDateDay;
123 - (void)setStartDateMonth:(id)_startDateMonth {
124 ASSIGN(self->startDateMonth, _startDateMonth);
126 - (id)startDateMonth {
127 return self->startDateMonth;
129 - (void)setStartDateYear:(id)_startDateYear {
130 ASSIGN(self->startDateYear, _startDateYear);
132 - (id)startDateYear {
133 return self->startDateYear;
135 - (void)setEndDateHour:(id)_endDateHour {
136 ASSIGN(self->endDateHour, _endDateHour);
139 return self->endDateHour;
141 - (void)setEndDateMinute:(id)_endDateMinute {
142 ASSIGN(self->endDateMinute, _endDateMinute);
144 - (id)endDateMinute {
145 return self->endDateMinute;
147 - (void)setEndDateDay:(id)_endDateDay {
148 ASSIGN(self->endDateDay, _endDateDay);
151 return self->endDateDay;
153 - (void)setEndDateMonth:(id)_endDateMonth {
154 ASSIGN(self->endDateMonth, _endDateMonth);
157 return self->endDateMonth;
159 - (void)setEndDateYear:(id)_endDateYear {
160 ASSIGN(self->endDateYear, _endDateYear);
163 return self->endDateYear;
166 - (void)setStartDate:(NSCalendarDate *)_date {
167 [self setStartDateHour:[NSNumber numberWithInt:[_date hourOfDay]]];
168 [self setStartDateMinute:[NSNumber numberWithInt:[_date minuteOfHour]]];
169 [self setStartDateDay:[NSNumber numberWithInt:[_date dayOfMonth]]];
170 [self setStartDateMonth:[NSNumber numberWithInt:[_date monthOfYear]]];
171 [self setStartDateYear:[NSNumber numberWithInt:[_date yearOfCommonEra]]];
173 - (NSCalendarDate *)startDate {
174 return [NSCalendarDate dateWithYear:[[self startDateYear] intValue]
175 month:[[self startDateMonth] intValue]
176 day:[[self startDateDay] intValue]
177 hour:[[self startDateHour] intValue]
178 minute:[[self startDateMinute] intValue]
180 timeZone:[[self clientObject] userTimeZone]];
182 - (void)setEndDate:(NSCalendarDate *)_date {
183 [self setEndDateHour:[NSNumber numberWithInt:[_date hourOfDay]]];
184 [self setEndDateMinute:[NSNumber numberWithInt:[_date minuteOfHour]]];
185 [self setEndDateDay:[NSNumber numberWithInt:[_date dayOfMonth]]];
186 [self setEndDateMonth:[NSNumber numberWithInt:[_date monthOfYear]]];
187 [self setEndDateYear:[NSNumber numberWithInt:[_date yearOfCommonEra]]];
189 - (NSCalendarDate *)endDate {
190 return [NSCalendarDate dateWithYear:[[self endDateYear] intValue]
191 month:[[self endDateMonth] intValue]
192 day:[[self endDateDay] intValue]
193 hour:[[self endDateHour] intValue]
194 minute:[[self endDateMinute] intValue]
196 timeZone:[[self clientObject] userTimeZone]];
199 - (void)setDuration:(id)_duration {
200 ASSIGN(self->duration, _duration);
203 return self->duration;
205 - (int)durationInMinutes {
206 return [[self duration] intValue];
208 - (NSTimeInterval)durationAsTimeInterval {
209 return [self durationInMinutes] * 60;
212 - (NSString *)itemDurationText {
213 // TODO: use a formatter
215 switch ([[self item] intValue]) {
216 case 30: return @"30 minutes";
217 case 60: return @"1 hour";
218 case 120: return @"2 hours";
219 case 240: return @"4 hours";
220 case 480: return @"8 hours";
222 return [NSString stringWithFormat:@"%@ minutes", [self item]];
226 - (void)setParticipants:(NSArray *)_parts {
227 ASSIGN(self->participants, _parts);
229 - (NSArray *)participants {
230 return self->participants;
232 - (void)setResources:(NSArray *)_res {
233 ASSIGN(self->resources, _res);
235 - (NSArray *)resources {
236 return self->resources;
239 - (NSArray *)attendees {
242 a = [self participants];
243 b = [self resources];
244 if ([b count] == 0) return a;
245 if ([a count] == 0) return b;
246 return [a arrayByAddingObjectsFromArray:b];
249 - (void)setCurrentDay:(id)_day {
250 ASSIGN(self->currentDay, _day);
253 return self->currentDay;
257 // TODO: from 'earliest start' to 'latest endtime'
258 unsigned lStartHour = 9, lEndHour = 17, i;
261 lStartHour = [[self startDateHour] intValue];
262 lEndHour = [[self endDateHour] intValue];
263 if (lStartHour < 1) lStartHour = 1;
264 if (lEndHour < lStartHour) lEndHour = lStartHour + 1;
266 ma = [NSMutableArray arrayWithCapacity:lEndHour - lStartHour + 2];
267 for (i = lStartHour; i <= lEndHour; i++)
268 [ma addObject:[NSNumber numberWithInt:i]];
273 // TODO: from startdate to enddate
275 NSCalendarDate *base, *stop, *current;
277 base = [NSCalendarDate dateWithYear:[[self startDateYear] intValue]
278 month:[[self startDateMonth] intValue]
279 day:[[self startDateDay] intValue]
280 hour:12 minute:0 second:0
281 timeZone:[[self clientObject] userTimeZone]];
282 stop = [NSCalendarDate dateWithYear:[[self endDateYear] intValue]
283 month:[[self endDateMonth] intValue]
284 day:[[self endDateDay] intValue]
285 hour:12 minute:0 second:0
286 timeZone:[[self clientObject] userTimeZone]];
288 ma = [NSMutableArray arrayWithCapacity:16];
291 while ([current compare:stop] != NSOrderedDescending) {
292 [current setTimeZone:[[self clientObject] userTimeZone]];
293 [ma addObject:current];
295 /* Note: remember the timezone behaviour of the method below! */
296 current = [current dateByAddingYears:0 months:0 days:1];
301 - (NSArray *)durationSteps {
302 // TODO: make configurable
303 return [NSArray arrayWithObjects:
304 @"30", @"60", @"120", @"240", @"480", nil];
309 - (BOOL)isRangeGreen:(NGCalendarDateRange *)_range {
312 idx = [self->blockedRanges indexOfFirstIntersectingDateRange:_range];
313 if (idx != NSNotFound) {
314 [self debugWithFormat:@"blocked range:\n range: %@\n block: %@\nintersection:%@",
316 [self->blockedRanges objectAtIndex:idx],
317 [_range intersectionDateRange:[self->blockedRanges objectAtIndex:idx]]];
320 return idx == NSNotFound ? YES : NO;
323 - (BOOL)isSlotRangeGreen:(NGCalendarDateRange *)_slotRange {
324 NGCalendarDateRange *aptRange;
325 NSCalendarDate *aptStartDate, *aptEndDate;
327 if (_slotRange == nil)
330 /* calculate the interval requested by the user (can be larger) */
332 aptStartDate = [_slotRange startDate];
333 // TODO: gives warning on MacOSX
334 aptEndDate = [[NSCalendarDate alloc] initWithTimeIntervalSince1970:
335 [aptStartDate timeIntervalSince1970]
336 + [self durationAsTimeInterval]];
337 [aptStartDate setTimeZone:[[self clientObject] userTimeZone]];
338 [aptEndDate setTimeZone:[[self clientObject] userTimeZone]];
339 aptRange = [NGCalendarDateRange calendarDateRangeWithStartDate:aptStartDate
341 [aptEndDate release]; aptEndDate = nil;
343 return [self isRangeGreen:aptRange];
346 - (BOOL)isFirstHalfGreen {
347 /* currentday is the date, self->item the hour */
348 NSCalendarDate *from, *to;
349 NGCalendarDateRange *range;
351 from = [self->currentDay hour:[[self item] intValue] minute:0];
352 to = [self->currentDay hour:[[self item] intValue] minute:30];
353 range = [NGCalendarDateRange calendarDateRangeWithStartDate:from endDate:to];
354 return [self isSlotRangeGreen:range];
356 - (BOOL)isSecondHalfGreen {
357 /* currentday is the date, self->item the hour */
358 NSCalendarDate *from, *to;
359 NGCalendarDateRange *range;
361 from = [self->currentDay hour:[[self item] intValue] minute:30];
362 to = [self->currentDay hour:[[self item] intValue] + 1 minute:0];
363 range = [NGCalendarDateRange calendarDateRangeWithStartDate:from endDate:to];
364 return [self isSlotRangeGreen:range];
367 - (BOOL)isFirstHalfBlocked {
368 return [self isFirstHalfGreen] ? NO : YES;
370 - (BOOL)isSecondHalfBlocked {
371 return [self isSecondHalfGreen] ? NO : YES;
376 - (BOOL)shouldTakeValuesFromRequest:(WORequest *)_rq inContext:(WOContext*)_c{
380 - (id<WOActionResults>)defaultAction {
383 now = [NSCalendarDate date];
385 [self setDuration:@"120"]; /* 1 hour as default */
386 [self setStartDate:[now hour:9 minute:0]];
387 [self setEndDate:[now hour:18 minute:0]];
392 - (id)proposalSearchAction {
393 NSArray *attendees, *uids, *fbos, *ranges;
395 NSMutableArray *allInfos;
397 [self logWithFormat:@"search from %@ to %@",
398 [self startDate], [self endDate]];
400 attendees = [self attendees];
401 uids = [[self clientObject] uidsFromICalPersons:attendees];
402 if ([uids count] == 0) {
403 [self logWithFormat:@"Note: no UIDs selected."];
407 fbos = [[self clientObject] lookupFreeBusyObjectsForUIDs:uids
408 inContext:[self context]];
409 count = [fbos count];
410 // wild guess at capacity
411 allInfos = [[[NSMutableArray alloc] initWithCapacity:count * 10] autorelease];
413 for (i = 0; i < count; i++) {
414 SOGoFreeBusyObject *fb;
417 fb = [fbos objectAtIndex:i];
418 if (fb != (SOGoFreeBusyObject *)[NSNull null]) {
419 infos = [fb fetchFreeBusyInfosFrom:[self startDate] to:[self endDate]];
420 [allInfos addObjectsFromArray:infos];
423 [self debugWithFormat:@" processing: %d infos", [allInfos count]];
424 ranges = [allInfos arrayByCreatingDateRangesFromObjectsWithStartDateKey:
426 andEndDateKey:@"endDate"];
427 ranges = [ranges arrayByCompactingContainedDateRanges];
428 [self debugWithFormat:@" ranges: %@", ranges];
430 ASSIGNCOPY(self->blockedRanges, ranges);
437 - (NSMutableDictionary *)currentQueryParameters {
438 if(!self->currentQueryParameters) {
439 self->currentQueryParameters = [[self queryParameters] mutableCopy];
440 [self->currentQueryParameters setObject:[self duration] forKey:@"dur"];
441 [self setICalPersons:[self resources] asQueryParameter:@"rs"];
442 [self setICalPersons:[self participants] asQueryParameter:@"ps"];
444 return self->currentQueryParameters;
447 - (void)setICalPersons:(NSArray *)_ps asQueryParameter:(NSString *)_qp {
451 s = [[NSMutableString alloc] init];
453 for(i = 0; i < count; i++) {
454 iCalPerson *p = [_ps objectAtIndex:i];
455 [s appendString:[p rfc822Email]];
457 [s appendString:@","];
459 [[self currentQueryParameters] setObject:s forKey:_qp];
463 - (NSDictionary *)currentHourQueryParametersForFirstHalf:(BOOL)_first {
464 NSMutableDictionary *qp;
466 NSCalendarDate *date;
469 minute = _first ? 0 : 30;
470 qp = [self currentQueryParameters];
472 date = [self currentDay];
473 hmString = [NSString stringWithFormat:@"%02d%02d",
474 [item intValue], minute];
475 [self setSelectedDateQueryParameter:date inDictionary:qp];
476 [qp setObject:hmString forKey:@"hm"];
480 - (NSDictionary *)currentFirstHalfQueryParameters {
481 return [self currentHourQueryParametersForFirstHalf:YES];
484 - (NSDictionary *)currentSecondHalfQueryParameters {
485 return [self currentHourQueryParametersForFirstHalf:NO];
488 @end /* UIxAppointmentProposal */