]> err.no Git - scalable-opengroupware.org/blob - UI/Scheduler/UIxCalDayTable.m
initial sync
[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
247   if (!allAppointments)
248     {
249       allAppointments = [self fetchCoreAppointmentsInfos];
250       [allAppointments retain];
251     }
252
253   filteredAppointments = [NSMutableArray new];
254   [filteredAppointments autorelease];
255
256   start = [currentTableDay hour: [self dayStartHour] minute: 0];
257   end = [currentTableDay hour: [self dayEndHour] minute: 0];
258
259   aptsEnumerator = [allAppointments objectEnumerator];
260   currentDayAppointment = [aptsEnumerator nextObject];
261   while (currentDayAppointment)
262     {
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];
271     }
272
273   return filteredAppointments;
274 }
275
276 - (void) setCurrentAppointment: (NSDictionary *) newCurrentAppointment
277 {
278   currentAppointment = newCurrentAppointment;
279 }
280
281 - (NSDictionary *) currentAppointment
282 {
283   return currentAppointment;
284 }
285
286 - (NSArray *) appointmentsClasses
287 {
288   return [NSString stringWithFormat: @"appointments appointmentsFor%dDays",
289                    numberOfDays];
290 }
291
292 - (NSString *) daysViewClasses
293 {
294   return [NSString stringWithFormat: @"daysView daysViewFor%dDays", numberOfDays];
295 }
296
297 - (NSString *) dayClasses
298 {
299   NSMutableString *classes;
300   int dayOfWeek;
301
302   classes = [NSMutableString new];
303   [classes autorelease];
304   [classes appendFormat: @"day day%d", [currentTableDay dayOfWeek]];
305   if (numberOfDays > 1)
306     {
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"];
314     }
315
316   return classes;
317 }
318
319 - (NSString *) clickableHourCellClass
320 {
321   return [NSString stringWithFormat: @"clickableHourCell clickableHourCell%@", currentTableHour];
322 }
323
324 @end