/* Copyright (C) 2000-2004 SKYRIX Software AG This file is part of OGo OGo is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. OGo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with OGo; see the file COPYING. If not, write to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ // $Id$ #import "UIxCalDayView.h" #include #include "common.h" @interface UIxCalDayView (PrivateAPI) - (BOOL)isCurrentDateInApt:(id)_apt; - (NSArray *)_getDatesFrom:(NSCalendarDate *)_from to:(NSCalendarDate *)_to; @end @implementation UIxCalDayView - (void)dealloc { [self->currentDate release]; [super dealloc]; } - (void)setCurrentDate:(NSCalendarDate *)_date { ASSIGN(self->currentDate, _date); } - (NSCalendarDate *)currentDate { return self->currentDate; } - (BOOL)isCurrentDateInApt { return [self isCurrentDateInApt:[self appointment]]; } - (BOOL)isCurrentDateInApt:(id)_apt { NSCalendarDate *start, *end, *aptStartDate, *aptEndDate; start = self->currentDate; end = [start dateByAddingYears:0 months:0 days:0 hours:0 minutes:59 seconds:59]; aptStartDate = [_apt valueForKey:@"startDate"]; aptEndDate = [_apt valueForKey:@"endDate"]; /* does apt fully enclose start/end? */ if([start isGreaterThanOrEqualTo:aptStartDate] && [end isLessThanOrEqualTo:aptEndDate]) return YES; if([aptStartDate isGreaterThanOrEqualTo:start] && [aptStartDate isLessThan:end]) return YES; if([aptEndDate isGreaterThan:start] && [aptEndDate isLessThanOrEqualTo:end]) return YES; return NO; } - (NSArray *)dateRange { /* default range is from dayStartHour to dayEndHour. Any values before or after are also fine */ NSCalendarDate *min, *max; NSArray *aptDateRanges; min = [[self startDate] hour:[self dayStartHour] minute:0]; max = [[self startDate] hour:[self dayEndHour] minute:0]; aptDateRanges = [[self appointments] valueForKey:@"startDate"]; if([aptDateRanges count] != 0) { NSCalendarDate *d; aptDateRanges = [aptDateRanges sortedArrayUsingSelector:@selector(compareAscending:)]; d = [aptDateRanges objectAtIndex:0]; min = (NSCalendarDate *)[d earlierDate:min]; d = [aptDateRanges objectAtIndex:[aptDateRanges count] - 1]; max = (NSCalendarDate *)[d laterDate:max]; } return [self _getDatesFrom:min to:max]; } - (NSArray *)_getDatesFrom:(NSCalendarDate *)_from to:(NSCalendarDate *)_to { NSMutableArray *dates; unsigned i, count, offset; offset = [_from hourOfDay]; count = ([_to hourOfDay] + 1) - offset; dates = [[NSMutableArray alloc] initWithCapacity:count]; for(i = 0; i < count; i++) { NSCalendarDate *date; date = [_from hour:offset + i minute:0]; [dates addObject:date]; } return [dates autorelease]; } - (NSString *)shortTextForApt { NSCalendarDate *startDate, *endDate; NSMutableString *aptDescr; NSString *s; BOOL spansRange; id apt; apt = [self appointment]; spansRange = NO; startDate = [apt valueForKey:@"startDate"]; [startDate setTimeZone:[self viewTimeZone]]; endDate = [apt valueForKey:@"endDate"]; if(endDate != nil) { [endDate setTimeZone:[self viewTimeZone]]; spansRange = ![endDate isEqualToDate:startDate]; } aptDescr = [[NSMutableString alloc] init]; [aptDescr appendFormat:@"%02i:%02i", [startDate hourOfDay], [startDate minuteOfHour]]; if(spansRange) { [aptDescr appendFormat:@"- %02i:%02i", [endDate hourOfDay], [endDate minuteOfHour]]; } s = [apt valueForKey:@"title"]; if(s) { [aptDescr appendFormat:@"; %@", s]; } s = [apt valueForKey:@"location"]; if(s) { [aptDescr appendFormat:@"; %@", s]; } return [aptDescr autorelease]; } /* URLs */ - (NSDictionary *)prevDayQueryParameters { NSCalendarDate *date; date = [[self startDate] dateByAddingYears:0 months:0 days:-1 hours:0 minutes:0 seconds:0]; return [self queryParametersBySettingSelectedDate:date]; } - (NSDictionary *)nextDayQueryParameters { NSCalendarDate *date; date = [[self startDate] dateByAddingYears:0 months:0 days:1 hours:0 minutes:0 seconds:0]; return [self queryParametersBySettingSelectedDate:date]; } - (NSDictionary *)currentDateQueryParameters { NSMutableDictionary *qp; NSString *hmString; NSCalendarDate *date; date = [self currentDate]; hmString = [NSString stringWithFormat:@"%02d%02d", [date hourOfDay], [date minuteOfHour]]; qp = [[self queryParameters] mutableCopy]; [self setSelectedDateQueryParameter:date inDictionary:qp]; [qp setObject:hmString forKey:@"hm"]; return [qp autorelease]; } /* fetching */ - (NSCalendarDate *)startDate { return [[self selectedDate] beginOfDay]; } - (NSCalendarDate *)endDate { return [[self startDate] endOfDay]; } /* appointments */ - (NSArray *)appointments { return [self fetchCoreInfos]; } - (NSArray *)aptsForCurrentDate { NSArray *apts; NSMutableArray *filtered; unsigned i, count; NSCalendarDate *start, *end; start = self->currentDate; end = [start dateByAddingYears:0 months:0 days:0 hours:0 minutes:59 seconds:59]; apts = [self appointments]; filtered = [[NSMutableArray alloc] initWithCapacity:1]; count = [apts count]; for(i = 0; i < count; i++) { id apt; NSCalendarDate *aptStartDate; /* NOTE: appointments are totally opaque objects, we don't know much about them. The reason for this is that they are backend-dependent and we'd like to use UIx for SOGo *and* ZideStore also. We have to accept the fact that we know just a little bit of their API which is completely KVC driven. */ apt = [apts objectAtIndex:i]; aptStartDate = [apt valueForKey:@"startDate"]; if([aptStartDate isGreaterThanOrEqualTo:start] && [aptStartDate isLessThan:end]) { [filtered addObject:apt]; } } return [filtered autorelease]; } - (BOOL)hasAptsForCurrentDate { return [[self aptsForCurrentDate] count] != 0; } @end