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]];
151 addObject: [NSString stringWithFormat: @"%d", currentHour]];
154 return hoursToDisplay;
157 - (NSArray *) daysToDisplay
159 NSCalendarDate *currentDate;
164 daysToDisplay = [NSMutableArray new];
165 currentDate = [[self startDate] hour: [self dayStartHour]
167 [daysToDisplay addObject: currentDate];
168 for (count = 1; count < numberOfDays; count++)
169 [daysToDisplay addObject: [currentDate dateByAddingYears: 0
174 return daysToDisplay;
177 - (void) setCurrentTableDay: (NSCalendarDate *) aTableDay
179 currentTableDay = aTableDay;
182 - (NSCalendarDate *) currentTableDay
184 return currentTableDay;
187 - (void) setCurrentTableHour: (NSString *) aTableHour
189 currentTableHour = aTableHour;
192 - (NSString *) currentTableHour
194 return currentTableHour;
197 - (NSString *) currentAppointmentHour
199 return [NSString stringWithFormat: @"%.2d00", [currentTableHour intValue]];
202 - (NSString *) labelForDay
204 return [NSString stringWithFormat: @"%@<br />%@",
205 [dateFormatter shortDayOfWeek: [currentTableDay dayOfWeek]],
206 [dateFormatter stringForObjectValue: currentTableDay]];
209 - (NSDictionary *) _adjustedAppointment: (NSDictionary *) anAppointment
210 forStart: (NSCalendarDate *) start
211 andEnd: (NSCalendarDate *) end
213 NSMutableDictionary *newMutableAppointment;
214 NSDictionary *newAppointment;
215 BOOL startIsEarlier, endIsLater;
218 = ([[anAppointment objectForKey: @"startDate"] laterDate: start] == start);
220 = ([[anAppointment objectForKey: @"endDate"] earlierDate: end] == end);
222 if (startIsEarlier || endIsLater)
224 newMutableAppointment
225 = [NSMutableDictionary dictionaryWithDictionary: anAppointment];
228 [newMutableAppointment setObject: start
229 forKey: @"startDate"];
231 [newMutableAppointment setObject: end
234 newAppointment = newMutableAppointment;
237 newAppointment = anAppointment;
239 return newAppointment;
242 - (NSArray *) appointmentsForCurrentDay
244 NSMutableArray *filteredAppointments;
245 NSEnumerator *aptsEnumerator;
246 NSDictionary *currentDayAppointment;
247 NSCalendarDate *start, *end;
250 if (!allAppointments)
252 allAppointments = [self fetchCoreAppointmentsInfos];
253 [allAppointments retain];
256 filteredAppointments = [NSMutableArray new];
257 [filteredAppointments autorelease];
259 start = [currentTableDay hour: [self dayStartHour] minute: 0];
260 endHour = [self dayEndHour];
262 end = [currentTableDay hour: [self dayEndHour] minute: 59];
264 end = [[currentTableDay tomorrow] hour: 0 minute: 0];
266 aptsEnumerator = [allAppointments objectEnumerator];
267 currentDayAppointment = [aptsEnumerator nextObject];
268 while (currentDayAppointment)
270 if (([end laterDate: [currentDayAppointment
271 valueForKey: @"startDate"]] == end)
272 && ([start earlierDate: [currentDayAppointment
273 valueForKey: @"endDate"]] == start))
274 [filteredAppointments
275 addObject: [self _adjustedAppointment: currentDayAppointment
276 forStart: start andEnd: end]];
277 currentDayAppointment = [aptsEnumerator nextObject];
280 return filteredAppointments;
283 - (void) setCurrentAppointment: (NSDictionary *) newCurrentAppointment
285 currentAppointment = newCurrentAppointment;
288 - (NSDictionary *) currentAppointment
290 return currentAppointment;
293 - (NSString *) appointmentsClasses
295 return [NSString stringWithFormat: @"appointments appointmentsFor%dDays",
299 - (NSString *) daysViewClasses
301 return [NSString stringWithFormat: @"daysView daysViewFor%dDays", numberOfDays];
304 - (NSString *) dayClasses
306 NSMutableString *classes;
309 classes = [NSMutableString new];
310 [classes autorelease];
311 [classes appendFormat: @"day day%d", [currentTableDay dayOfWeek]];
312 if (numberOfDays > 1)
314 dayOfWeek = [currentTableDay dayOfWeek];
315 if (dayOfWeek == 0 || dayOfWeek == 6)
316 [classes appendString: @" weekEndDay"];
317 if ([currentTableDay isToday])
318 [classes appendString: @" dayOfToday"];
319 if ([[self selectedDate] isDateOnSameDay: currentTableDay])
320 [classes appendString: @" selectedDay"];
326 - (NSString *) clickableHourCellClass
328 return [NSString stringWithFormat: @"clickableHourCell clickableHourCell%@", currentTableHour];