]> err.no Git - scalable-opengroupware.org/blob - UI/Scheduler/UIxCalDayTable.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1021 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / Scheduler / UIxCalDayTable.m
1 /* UIxCalDayTable.m - this file is part of SOGo
2  *
3  * Copyright (C) 2006 Inverse groupe conseil
4  *
5  * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
6  *
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)
10  * any later version.
11  *
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.
16  *
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.
21  */
22
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>
28
29 #import <EOControl/EOQualifier.h>
30
31 #import <NGExtensions/NSCalendarDate+misc.h>
32 #import <SOGoUI/SOGoDateFormatter.h>
33
34 #import "UIxCalDayTable.h"
35
36 @class SOGoAppointment;
37
38 @implementation UIxCalDayTable
39
40 - (id) init
41 {
42   if ((self = [super init]))
43     {
44       allAppointments = nil;
45       daysToDisplay = nil;
46       hoursToDisplay = nil;
47       numberOfDays = 1;
48       startDate = nil;
49       currentTableDay = nil;
50       currentTableHour = nil;
51       dateFormatter = [[SOGoDateFormatter alloc]
52                         initWithLocale: [self locale]];
53     }
54
55   return self;
56 }
57
58 - (void) dealloc
59 {
60   if (allAppointments)
61     [allAppointments release];
62   if (daysToDisplay)
63     [daysToDisplay release];
64   if (hoursToDisplay)
65     [hoursToDisplay release];
66   [dateFormatter release];
67   [super dealloc];
68 }
69
70 - (void) setCSSClass: (NSString *) aCssClass
71 {
72   cssClass = aCssClass;
73 }
74
75 - (NSString *) cssClass
76 {
77   return cssClass;
78 }
79
80 - (void) setCSSId: (NSString *) aCssId
81 {
82   cssId = aCssId;
83 }
84
85 - (NSString *) cssId
86 {
87   return cssId;
88 }
89
90 - (void) setNumberOfDays: (NSString *) aNumber
91 {
92   numberOfDays = [aNumber intValue];
93   if (daysToDisplay)
94     {
95       [daysToDisplay release];
96       daysToDisplay = nil;
97     }
98 }
99
100 - (NSString *) numberOfDays
101 {
102   return [NSString stringWithFormat: @"%d", numberOfDays];
103 }
104
105 - (void) setStartDate: (NSCalendarDate *) aStartDate
106 {
107   startDate = aStartDate;
108   if (daysToDisplay)
109     {
110       [daysToDisplay release];
111       daysToDisplay = nil;
112     }
113 }
114
115 - (NSCalendarDate *) startDate
116 {
117   if (!startDate)
118     startDate = [super startDate];
119
120   return [startDate beginOfDay];
121 }
122
123 - (NSCalendarDate *) endDate
124 {
125   NSCalendarDate *endDate;
126
127   endDate = [[self startDate] dateByAddingYears: 0
128                               months: 0
129                               days: numberOfDays - 1];
130
131   return [endDate endOfDay];
132 }
133
134 - (NSArray *) hoursToDisplay
135 {
136   unsigned int currentHour, lastHour;
137
138   if (!hoursToDisplay)
139     {
140       currentHour = [self dayStartHour];
141       lastHour = [self dayEndHour];
142       hoursToDisplay = [NSMutableArray new];
143
144       while (currentHour < lastHour)
145         {
146           [hoursToDisplay
147             addObject: [NSString stringWithFormat: @"%d", currentHour]];
148           currentHour++;
149         }
150     }
151
152   return hoursToDisplay;
153 }
154
155 - (NSArray *) daysToDisplay
156 {
157   NSCalendarDate *currentDate;
158   int count;
159
160   if (!daysToDisplay)
161     {
162       daysToDisplay = [NSMutableArray new];
163       currentDate = [[self startDate] hour: [self dayStartHour]
164                                       minute: 0];
165       [daysToDisplay addObject: currentDate];
166       for (count = 1; count < numberOfDays; count++)
167         [daysToDisplay addObject: [currentDate dateByAddingYears: 0
168                                                months: 0
169                                                days: count]];
170     }
171
172   return daysToDisplay;
173 }
174
175 - (void) setCurrentTableDay: (NSCalendarDate *) aTableDay
176 {
177   currentTableDay = aTableDay;
178 }
179
180 - (NSCalendarDate *) currentTableDay
181 {
182   return currentTableDay;
183 }
184
185 - (void) setCurrentTableHour: (NSString *) aTableHour
186 {
187   currentTableHour = aTableHour;
188 }
189
190 - (NSString *) currentTableHour
191 {
192   return currentTableHour;
193 }
194
195 - (NSString *) currentAppointmentHour
196 {
197   return [NSString stringWithFormat: @"%.2d00", [currentTableHour intValue]];
198 }
199
200 - (NSString *) labelForDay
201 {
202   return [NSString stringWithFormat: @"%@ %@",
203                    [dateFormatter shortDayOfWeek: [currentTableDay dayOfWeek]],
204                    [dateFormatter stringForObjectValue: currentTableDay]];
205 }
206
207 - (NSDictionary *) _adjustedAppointment: (NSDictionary *) anAppointment
208                                forStart: (NSCalendarDate *) start
209                                  andEnd: (NSCalendarDate *) end
210 {
211   NSMutableDictionary *newMutableAppointment;
212   NSDictionary *newAppointment;
213   BOOL startIsEarlier, endIsLater;
214
215   startIsEarlier
216     = ([[anAppointment objectForKey: @"startDate"] laterDate: start] == start);
217   endIsLater
218     = ([[anAppointment objectForKey: @"endDate"] earlierDate: end] == end);
219
220   if (startIsEarlier || endIsLater)
221     {
222       newMutableAppointment
223         = [NSMutableDictionary dictionaryWithDictionary: anAppointment];
224       
225       if (startIsEarlier)
226         [newMutableAppointment setObject: start
227                                forKey: @"startDate"];
228       if (endIsLater)
229         [newMutableAppointment setObject: end
230                                forKey: @"endDate"];
231
232       newAppointment = newMutableAppointment;
233     }
234   else
235     newAppointment = anAppointment;
236
237   return newAppointment;
238 }
239
240 - (NSArray *) appointmentsForCurrentDay
241 {
242   NSMutableArray *filteredAppointments;
243   NSEnumerator *aptsEnumerator;
244   NSDictionary *currentDayAppointment;
245   NSCalendarDate *start, *end;
246   int endHour;
247
248   if (!allAppointments)
249     {
250       allAppointments = [self fetchCoreAppointmentsInfos];
251       [allAppointments retain];
252     }
253
254   filteredAppointments = [NSMutableArray new];
255   [filteredAppointments autorelease];
256
257   start = [currentTableDay hour: [self dayStartHour] minute: 0];
258   endHour = [self dayEndHour];
259   if (endHour < 24)
260     end = [currentTableDay hour: [self dayEndHour] minute: 0];
261   else
262     end = [[currentTableDay tomorrow] hour: 0 minute: 0];
263
264   aptsEnumerator = [allAppointments objectEnumerator];
265   currentDayAppointment = [aptsEnumerator nextObject];
266   while (currentDayAppointment)
267     {
268       if (([end laterDate: [currentDayAppointment
269                              valueForKey: @"startDate"]] == end)
270           && ([start earlierDate: [currentDayAppointment
271                                     valueForKey: @"endDate"]] == start))
272         [filteredAppointments
273           addObject: [self _adjustedAppointment: currentDayAppointment
274                            forStart: start andEnd: end]];
275       currentDayAppointment = [aptsEnumerator nextObject];
276     }
277
278   return filteredAppointments;
279 }
280
281 - (void) setCurrentAppointment: (NSDictionary *) newCurrentAppointment
282 {
283   currentAppointment = newCurrentAppointment;
284 }
285
286 - (NSDictionary *) currentAppointment
287 {
288   return currentAppointment;
289 }
290
291 - (NSArray *) appointmentsClasses
292 {
293   return [NSString stringWithFormat: @"appointments appointmentsFor%dDays",
294                    numberOfDays];
295 }
296
297 - (NSString *) daysViewClasses
298 {
299   return [NSString stringWithFormat: @"daysView daysViewFor%dDays", numberOfDays];
300 }
301
302 - (NSString *) dayClasses
303 {
304   NSMutableString *classes;
305   int dayOfWeek;
306
307   classes = [NSMutableString new];
308   [classes autorelease];
309   [classes appendFormat: @"day day%d", [currentTableDay dayOfWeek]];
310   if (numberOfDays > 1)
311     {
312       dayOfWeek = [currentTableDay dayOfWeek];
313       if (dayOfWeek == 0 || dayOfWeek == 6)
314         [classes appendString: @" weekEndDay"];
315       if ([currentTableDay isToday])
316         [classes appendString: @" dayOfToday"];
317       if ([[self selectedDate] isDateOnSameDay: currentTableDay])
318         [classes appendString: @" selectedDay"];
319     }
320
321   return classes;
322 }
323
324 - (NSString *) clickableHourCellClass
325 {
326   return [NSString stringWithFormat: @"clickableHourCell clickableHourCell%@", currentTableHour];
327 }
328
329 @end