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