]> err.no Git - scalable-opengroupware.org/blob - UI/Scheduler/UIxCalMonthView.m
0bd425d98e59afa1d4b527e36f74b68c7ccb860d
[scalable-opengroupware.org] / UI / Scheduler / UIxCalMonthView.m
1 /* UIxCalMonthView.m - this file is part of SOGo
2  *
3  * Copyright (C) 2006, 2007 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/NSString.h>
26
27 #import <NGExtensions/NSCalendarDate+misc.h>
28 #import <SoObjects/SOGo/NSCalendarDate+SOGo.h>
29
30 #import <SOGoUI/SOGoAptFormatter.h>
31 #import <SOGoUI/SOGoDateFormatter.h>
32
33 #import "UIxCalMonthView.h"
34
35 @implementation UIxCalMonthView
36
37 - (id) init
38 {
39   NSDictionary *locale;
40
41   if ((self = [super init]))
42     {
43 //       monthAptFormatter
44 //         = [[SOGoAptFormatter alloc] initWithDisplayTimeZone: timeZone];
45 //       [monthAptFormatter setShortMonthTitleOnly];
46 //       dateFormatter = [[SOGoDateFormatter alloc]
47 //                         initWithLocale: [self locale]];
48       locale = [context valueForKey: @"locale"];
49       dayNames = [locale objectForKey: NSWeekDayNameArray];
50       [dayNames retain];
51       monthNames = [locale objectForKey: NSMonthNameArray];
52       [monthNames retain];
53       sortedAppointments = [NSMutableDictionary new];
54       daysToDisplay = nil;
55     }
56
57   return self;
58 }
59
60 // - (SOGoAptFormatter *) monthAptFormatter
61 // {
62 //   return monthAptFormatter;
63 // }
64
65 - (void) dealloc
66 {
67   [monthNames release];
68   [dayNames release];
69   [daysToDisplay release];
70 //   [monthAptFormatter release];
71 //   [dateFormatter release];
72   [sortedAppointments release];
73   [super dealloc];
74 }
75
76 - (NSArray *) headerDaysToDisplay
77 {
78   NSMutableArray *headerDaysToDisplay;
79   unsigned int counter;
80
81   headerDaysToDisplay = [NSMutableArray arrayWithCapacity: 7];
82   currentTableDay = [[self selectedDate] mondayOfWeek];
83   for (counter = 0; counter < 7; counter++)
84     {
85       [headerDaysToDisplay addObject: currentTableDay];
86       currentTableDay = [currentTableDay tomorrow];
87     }
88
89   return headerDaysToDisplay;
90 }
91
92 - (NSArray *) daysToDisplay
93 {
94   NSMutableArray *days[7];
95   unsigned int counter;
96   NSCalendarDate *firstOfAllDays, *lastDayOfMonth;
97
98   if (!daysToDisplay)
99     {
100       firstOfAllDays = [[[self selectedDate] firstDayOfMonth] mondayOfWeek];
101       lastDayOfMonth  = [[self selectedDate] lastDayOfMonth];
102       for (counter = 0; counter < 7; counter++)
103         {
104           days[counter] = [NSMutableArray new];
105           [days[counter] autorelease];
106         }
107       currentTableDay = firstOfAllDays;
108       while ([currentTableDay earlierDate: lastDayOfMonth] == currentTableDay)
109         for (counter = 0; counter < 7; counter++)
110           {
111             [days[counter] addObject: currentTableDay];
112             currentTableDay = [currentTableDay tomorrow];
113           }
114       daysToDisplay = [NSArray arrayWithObjects: days count: 7];
115       [daysToDisplay retain];
116     }
117
118   return daysToDisplay;
119 }
120
121 - (NSString *) labelForCurrentDayToDisplay
122 {
123   return [dayNames objectAtIndex: [currentTableDay dayOfWeek]];
124 }
125
126 - (NSDictionary *) _dateQueryParametersWithOffset: (int) monthsOffset
127 {
128   NSCalendarDate *date;
129   
130   date = [[self selectedDate] dateByAddingYears: 0 months: monthsOffset
131                               days: 0 hours: 0 minutes: 0 seconds: 0];
132
133   return [self queryParametersBySettingSelectedDate: date];
134 }
135
136 - (NSDictionary *) monthBeforePrevMonthQueryParameters
137 {
138   return [self _dateQueryParametersWithOffset: -2];
139 }
140
141 - (NSDictionary *) prevMonthQueryParameters
142 {
143   return [self _dateQueryParametersWithOffset: -1];
144 }
145
146 - (NSDictionary *) nextMonthQueryParameters
147 {
148   return [self _dateQueryParametersWithOffset: 1];
149 }
150
151 - (NSDictionary *) monthAfterNextMonthQueryParameters
152 {
153   return [self _dateQueryParametersWithOffset: 2];
154 }
155
156 - (NSString *) _monthNameWithOffsetFromThisMonth: (int) offset
157 {
158   NSCalendarDate *date;
159
160   date = [[self selectedDate] dateByAddingYears: 0 months: offset days: 0];
161
162   return [self localizedNameForMonthOfYear: [date monthOfYear]];
163 }
164
165 - (NSString *) monthNameOfTwoMonthAgo
166 {
167   return [self _monthNameWithOffsetFromThisMonth: -2];
168 }
169
170 - (NSString *) monthNameOfOneMonthAgo
171 {
172   return [self _monthNameWithOffsetFromThisMonth: -1];
173 }
174
175 - (NSString *) monthNameOfThisMonth
176 {
177   return [self _monthNameWithOffsetFromThisMonth: 0];
178 }
179
180 - (NSString *) monthNameOfNextMonth
181 {
182   return [self _monthNameWithOffsetFromThisMonth: 1];
183 }
184
185 - (NSString *) monthNameOfTheMonthAfterNextMonth
186 {
187   return [self _monthNameWithOffsetFromThisMonth: 2];
188 }
189
190 /* template accessors */
191 - (void) setCurrentTableDay: (NSCalendarDate *) newCurrentTableDay
192 {
193   currentTableDay = newCurrentTableDay;
194 }
195
196 - (NSCalendarDate *) currentTableDay
197 {
198   return currentTableDay;
199 }
200
201 - (void) setCurrentTableColumn: (NSArray *) newCurrentTableColumn
202 {
203   currentTableColumn = newCurrentTableColumn;
204 }
205
206 - (NSArray *) currentTableColumn
207 {
208   return currentTableColumn;
209 }
210
211 - (NSString *) labelForCurrentDayCell
212 {
213   NSCalendarDate *lastDayOfMonth;
214   NSString *label, *monthOfYear;
215   int dayOfMonth;
216
217   dayOfMonth = [currentTableDay dayOfMonth];
218   lastDayOfMonth = [currentTableDay lastDayOfMonth];
219   if (dayOfMonth == 1
220       || [currentTableDay isDateOnSameDay: lastDayOfMonth])
221     {
222       monthOfYear
223         = [monthNames objectAtIndex: [currentTableDay monthOfYear]];
224       label = [NSString stringWithFormat: @"%d %@", dayOfMonth, monthOfYear];
225     }
226   else
227     label = [NSString stringWithFormat: @"%d", dayOfMonth];
228
229   return label;
230 }
231
232 - (NSString *) headerDayCellClasses
233 {
234   return [NSString stringWithFormat: @"headerDay day%d",
235                    [currentTableDay dayOfWeek]];
236 }
237
238 - (NSString *) dayHeaderNumber
239 {
240   NSString *nameOfMonth, *dayHeaderNumber;
241   unsigned int dayOfMonth;
242
243   dayOfMonth = [currentTableDay dayOfMonth];
244   if (dayOfMonth == 1
245       || [currentTableDay isDateOnSameDay: [currentTableDay lastDayOfMonth]])
246     {
247       nameOfMonth
248         = [self localizedNameForMonthOfYear: [currentTableDay monthOfYear]];
249       dayHeaderNumber = [NSString stringWithFormat: @"%d %@", dayOfMonth,
250                                   nameOfMonth];
251     }
252   else
253     dayHeaderNumber = [NSString stringWithFormat: @"%d", dayOfMonth];
254
255   return dayHeaderNumber;
256 }
257
258 - (NSString *) dayCellClasses
259 {
260   NSMutableString *classes;
261   NSCalendarDate *selectedDate;
262   int dayOfWeek, numberOfWeeks;
263
264   classes = [NSMutableString new];
265   [classes autorelease];
266
267   dayOfWeek = [currentTableDay dayOfWeek];
268   numberOfWeeks = [currentTableColumn count];
269
270   [classes appendFormat: @"day weekOf%d week%dof%d day%d",
271            numberOfWeeks,
272            [currentTableColumn indexOfObject: currentTableDay],
273            numberOfWeeks, dayOfWeek];
274   if (dayOfWeek == 0 || dayOfWeek == 6)
275     [classes appendString: @" weekEndDay"];
276   selectedDate = [self selectedDate];
277   if (![[currentTableDay firstDayOfMonth]
278          isDateOnSameDay: [selectedDate firstDayOfMonth]])
279     [classes appendString: @" dayOfAnotherMonth"];
280   if ([currentTableDay isToday])
281     [classes appendString: @" dayOfToday"];
282   if ([selectedDate isDateOnSameDay: currentTableDay])
283     [classes appendString: @" selectedDay"];
284
285   return classes;
286 }
287
288 - (NSArray *) _rangeOf7DaysForWeekStartingOn: (NSCalendarDate *) weekStart
289 {
290   unsigned int count;
291   NSMutableArray *range;
292   NSCalendarDate *currentDate;
293
294   range = [NSMutableArray arrayWithCapacity: 7];
295   currentDate = weekStart;
296   for (count = 0; count < 7; count++)
297     {
298       [range addObject: currentDate];
299       currentDate = [currentDate dateByAddingYears: 0 months: 0 days: 1];
300     }
301
302   return range;
303 }
304
305 - (NSCalendarDate *) startDate
306 {
307   NSCalendarDate *firstDayOfMonth;
308
309   firstDayOfMonth = [[self selectedDate] firstDayOfMonth];
310
311   return [firstDayOfMonth mondayOfWeek];
312 }
313
314 - (NSCalendarDate *) endDate
315 {
316   NSCalendarDate *lastDayOfMonth;
317
318   lastDayOfMonth = [[self selectedDate] lastDayOfMonth];
319
320   return [[lastDayOfMonth mondayOfWeek] dateByAddingYears: 0 months: 0 days: 6];
321 }
322
323 - (NSArray *) aptsForCurrentDate
324 {
325   return [sortedAppointments objectForKey: [currentTableDay shortDateString]];
326 }
327
328 @end