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