1 /* UIxCalDayTable.m - this file is part of SOGo
3 * Copyright (C) 2006 Inverse groupe conseil
5 * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
7 * This file is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This file is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #import <Foundation/NSArray.h>
24 #import <Foundation/NSCalendarDate.h>
25 #import <Foundation/NSDictionary.h>
26 #import <Foundation/NSKeyValueCoding.h>
27 #import <Foundation/NSString.h>
29 #import <EOControl/EOQualifier.h>
31 #import <NGExtensions/NSCalendarDate+misc.h>
32 #import <SOGoUI/SOGoDateFormatter.h>
34 #import "UIxCalDayTable.h"
36 @class SOGoAppointment;
38 @implementation UIxCalDayTable
42 if ((self = [super init]))
44 allAppointments = nil;
49 currentTableDay = nil;
50 currentTableHour = nil;
51 dateFormatter = [[SOGoDateFormatter alloc]
52 initWithLocale: [self locale]];
61 [allAppointments release];
63 [daysToDisplay release];
65 [hoursToDisplay release];
66 [dateFormatter release];
70 - (void) setCSSClass: (NSString *) aCssClass
75 - (NSString *) cssClass
80 - (void) setCSSId: (NSString *) aCssId
90 - (void) setNumberOfDays: (NSString *) aNumber
92 numberOfDays = [aNumber intValue];
95 [daysToDisplay release];
100 - (NSString *) numberOfDays
102 return [NSString stringWithFormat: @"%d", numberOfDays];
105 - (void) setStartDate: (NSCalendarDate *) aStartDate
107 startDate = aStartDate;
110 [daysToDisplay release];
115 - (NSCalendarDate *) startDate
118 startDate = [super startDate];
120 return [startDate beginOfDay];
123 - (NSCalendarDate *) endDate
125 NSCalendarDate *endDate;
127 endDate = [[self startDate] dateByAddingYears: 0
129 days: numberOfDays - 1];
131 return [endDate endOfDay];
134 - (NSArray *) hoursToDisplay
136 unsigned int currentHour, lastHour;
140 currentHour = [self dayStartHour];
141 lastHour = [self dayEndHour];
142 hoursToDisplay = [NSMutableArray new];
144 while (currentHour < lastHour)
147 addObject: [NSString stringWithFormat: @"%d", currentHour]];
152 return hoursToDisplay;
155 - (NSArray *) daysToDisplay
157 NSCalendarDate *currentDate;
162 daysToDisplay = [NSMutableArray new];
163 currentDate = [[self startDate] hour: [self dayStartHour]
165 [daysToDisplay addObject: currentDate];
166 for (count = 1; count < numberOfDays; count++)
167 [daysToDisplay addObject: [currentDate dateByAddingYears: 0
172 return daysToDisplay;
175 - (void) setCurrentTableDay: (NSCalendarDate *) aTableDay
177 currentTableDay = aTableDay;
180 - (NSCalendarDate *) currentTableDay
182 return currentTableDay;
185 - (void) setCurrentTableHour: (NSString *) aTableHour
187 currentTableHour = aTableHour;
190 - (NSString *) currentTableHour
192 return currentTableHour;
195 - (NSString *) currentAppointmentHour
197 return [NSString stringWithFormat: @"%.2d00", [currentTableHour intValue]];
200 - (NSString *) labelForDay
202 return [NSString stringWithFormat: @"%@ %@",
203 [dateFormatter shortDayOfWeek: [currentTableDay dayOfWeek]],
204 [dateFormatter stringForObjectValue: currentTableDay]];
207 - (NSDictionary *) _adjustedAppointment: (NSDictionary *) anAppointment
208 forStart: (NSCalendarDate *) start
209 andEnd: (NSCalendarDate *) end
211 NSMutableDictionary *newMutableAppointment;
212 NSDictionary *newAppointment;
213 BOOL startIsEarlier, endIsLater;
216 = ([[anAppointment objectForKey: @"startDate"] laterDate: start] == start);
218 = ([[anAppointment objectForKey: @"endDate"] earlierDate: end] == end);
220 if (startIsEarlier || endIsLater)
222 newMutableAppointment
223 = [NSMutableDictionary dictionaryWithDictionary: anAppointment];
226 [newMutableAppointment setObject: start
227 forKey: @"startDate"];
229 [newMutableAppointment setObject: end
232 newAppointment = newMutableAppointment;
235 newAppointment = anAppointment;
237 return newAppointment;
240 - (NSArray *) appointmentsForCurrentDay
242 NSMutableArray *filteredAppointments;
243 NSEnumerator *aptsEnumerator;
244 NSDictionary *currentDayAppointment;
245 NSCalendarDate *start, *end;
247 if (!allAppointments)
249 allAppointments = [self fetchCoreAppointmentsInfos];
250 [allAppointments retain];
253 filteredAppointments = [NSMutableArray new];
254 [filteredAppointments autorelease];
256 start = [currentTableDay hour: [self dayStartHour] minute: 0];
257 end = [currentTableDay hour: [self dayEndHour] minute: 0];
259 aptsEnumerator = [allAppointments objectEnumerator];
260 currentDayAppointment = [aptsEnumerator nextObject];
261 while (currentDayAppointment)
263 if (([end laterDate: [currentDayAppointment
264 valueForKey: @"startDate"]] == end)
265 && ([start earlierDate: [currentDayAppointment
266 valueForKey: @"endDate"]] == start))
267 [filteredAppointments
268 addObject: [self _adjustedAppointment: currentDayAppointment
269 forStart: start andEnd: end]];
270 currentDayAppointment = [aptsEnumerator nextObject];
273 return filteredAppointments;
276 - (void) setCurrentAppointment: (NSDictionary *) newCurrentAppointment
278 currentAppointment = newCurrentAppointment;
281 - (NSDictionary *) currentAppointment
283 return currentAppointment;
286 - (NSArray *) appointmentsClasses
288 return [NSString stringWithFormat: @"appointments appointmentsFor%dDays",
292 - (NSString *) daysViewClasses
294 return [NSString stringWithFormat: @"daysView daysViewFor%dDays", numberOfDays];
297 - (NSString *) dayClasses
299 NSMutableString *classes;
302 classes = [NSMutableString new];
303 [classes autorelease];
304 [classes appendFormat: @"day day%d", [currentTableDay dayOfWeek]];
305 if (numberOfDays > 1)
307 dayOfWeek = [currentTableDay dayOfWeek];
308 if (dayOfWeek == 0 || dayOfWeek == 6)
309 [classes appendString: @" weekEndDay"];
310 if ([currentTableDay isToday])
311 [classes appendString: @" dayOfToday"];
312 if ([[self selectedDate] isDateOnSameDay: currentTableDay])
313 [classes appendString: @" selectedDay"];
319 - (NSString *) clickableHourCellClass
321 return [NSString stringWithFormat: @"clickableHourCell clickableHourCell%@", currentTableHour];