]> err.no Git - scalable-opengroupware.org/blob - UI/Scheduler/UIxCalMulticolumnDayView.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1045 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / Scheduler / UIxCalMulticolumnDayView.m
1 /* UIxCalMulticolumnDayView.h - 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/NSDictionary.h>
25 #import <Foundation/NSString.h>
26 #import <Foundation/NSValue.h>
27
28 #import <NGExtensions/NSCalendarDate+misc.h>
29
30 #import <SOGo/SOGoUser.h>
31 #import <SOGoUI/SOGoDateFormatter.h>
32
33 #import "UIxCalMulticolumnDayView.h"
34
35 @implementation UIxCalMulticolumnDayView : UIxCalDayView
36
37 - (id) init
38 {
39   if ((self = [super init]))
40     {
41       allAppointments = nil;
42       subscriptionUsers = nil;
43       hoursToDisplay = nil;
44       currentTableUser = nil;
45       currentTableHour = nil;
46       dateFormatter = [[SOGoDateFormatter alloc]
47                         initWithLocale: [self locale]];
48     }
49
50   return self;
51 }
52
53 - (void) dealloc
54 {
55   if (allAppointments)
56     [allAppointments release];
57   if (subscriptionUsers)
58     [subscriptionUsers release];
59   if (hoursToDisplay)
60     [hoursToDisplay release];
61   [dateFormatter release];
62   [super dealloc];
63 }
64
65 - (void) setCSSClass: (NSString *) aCssClass
66 {
67   cssClass = aCssClass;
68 }
69
70 - (NSString *) cssClass
71 {
72   return cssClass;
73 }
74
75 - (void) setCSSId: (NSString *) aCssId
76 {
77   cssId = aCssId;
78 }
79
80 - (NSString *) cssId
81 {
82   return cssId;
83 }
84
85 - (NSArray *) hoursToDisplay
86 {
87   unsigned int currentHour, lastHour;
88
89   if (!hoursToDisplay)
90     {
91       currentHour = [self dayStartHour];
92       lastHour = [self dayEndHour];
93       hoursToDisplay = [NSMutableArray new];
94
95       while (currentHour < lastHour)
96         {
97           [hoursToDisplay
98             addObject: [NSString stringWithFormat: @"%d", currentHour]];
99           currentHour++;
100         }
101       [hoursToDisplay
102         addObject: [NSString stringWithFormat: @"%d", currentHour]];
103     }
104
105   return hoursToDisplay;
106 }
107
108 - (NSArray *) subscriptionUsers
109 {
110   SOGoUser *activeUser;
111   NSString *userList, *currentUserLogin;
112   NSEnumerator *users;
113
114   if (!subscriptionUsers)
115     {
116       subscriptionUsers = [NSMutableArray new];
117       activeUser = [context activeUser];
118       userList = [[activeUser userDefaults] objectForKey: @"calendaruids"];
119       users = [[userList componentsSeparatedByString: @","] objectEnumerator];
120       currentUserLogin = [users nextObject];
121       while (currentUserLogin)
122         {
123           if (![currentUserLogin hasPrefix: @"-"])
124             [subscriptionUsers addObject: currentUserLogin];
125           currentUserLogin = [users nextObject];
126         }
127     }
128
129   return subscriptionUsers;
130 }
131
132 - (void) setCurrentTableUser: (NSString *) aTableUser;
133 {
134   currentTableUser = aTableUser;
135 }
136
137 - (NSString *) currentTableUser;
138 {
139   return currentTableUser;
140 }
141
142 - (void) setCurrentTableHour: (NSString *) aTableHour
143 {
144   currentTableHour = aTableHour;
145 }
146
147 - (NSString *) currentTableHour
148 {
149   return currentTableHour;
150 }
151
152 - (NSString *) currentAppointmentHour
153 {
154   return [NSString stringWithFormat: @"%.2d00", [currentTableHour intValue]];
155 }
156
157 - (NSDictionary *) _adjustedAppointment: (NSDictionary *) anAppointment
158                                forStart: (NSCalendarDate *) start
159                                  andEnd: (NSCalendarDate *) end
160 {
161   NSMutableDictionary *newMutableAppointment;
162   NSDictionary *newAppointment;
163   BOOL startIsEarlier, endIsLater;
164
165   startIsEarlier
166     = ([[anAppointment objectForKey: @"startDate"] laterDate: start] == start);
167   endIsLater
168     = ([[anAppointment objectForKey: @"endDate"] earlierDate: end] == end);
169
170   if (startIsEarlier || endIsLater)
171     {
172       newMutableAppointment
173         = [NSMutableDictionary dictionaryWithDictionary: anAppointment];
174       
175       if (startIsEarlier)
176         [newMutableAppointment setObject: start
177                                forKey: @"startDate"];
178       if (endIsLater)
179         [newMutableAppointment setObject: end
180                                forKey: @"endDate"];
181
182       newAppointment = newMutableAppointment;
183     }
184   else
185     newAppointment = anAppointment;
186
187   return newAppointment;
188 }
189
190 /* fetching */
191
192 - (NSCalendarDate *) startDate
193 {
194   return [[self selectedDate] beginOfDay];
195 }
196
197 - (NSCalendarDate *) endDate
198 {
199   return [[self selectedDate] endOfDay];
200 }
201
202 - (NSArray *) appointmentsForCurrentUser
203 {
204   NSMutableArray *filteredAppointments;
205   NSEnumerator *aptsEnumerator;
206   NSDictionary *userAppointment;
207   NSCalendarDate *start, *end;
208   int endHour;
209
210   if (!allAppointments)
211     {
212       allAppointments = [self fetchCoreAppointmentsInfos];
213       [allAppointments retain];
214     }
215
216   start = [[self selectedDate] hour: [self dayStartHour] minute: 0];
217   endHour = [self dayEndHour];
218   if (endHour < 24)
219     end = [[self selectedDate] hour: [self dayEndHour] minute: 59];
220   else
221     end = [[[self selectedDate] tomorrow] hour: 0 minute: 0];
222
223   filteredAppointments = [NSMutableArray new];
224   [filteredAppointments autorelease];
225
226   aptsEnumerator = [allAppointments objectEnumerator];
227   userAppointment = [aptsEnumerator nextObject];
228   while (userAppointment)
229     {
230       if ([[userAppointment objectForKey: @"owner"]
231             isEqualToString: currentTableUser])
232         [filteredAppointments
233           addObject: [self _adjustedAppointment: userAppointment
234                            forStart: start andEnd: end]];
235       userAppointment = [aptsEnumerator nextObject];
236     }
237
238   return filteredAppointments;
239 }
240
241 - (void) setCurrentAppointment: (NSDictionary *) newCurrentAppointment
242 {
243   currentAppointment = newCurrentAppointment;
244 }
245
246 - (NSDictionary *) currentAppointment
247 {
248   return currentAppointment;
249 }
250
251 - (NSString *) appointmentsClasses
252 {
253   return @"appointments appointmentsFor1Days";
254 }
255
256 - (NSString *) currentUserClasses
257 {
258   NSArray *users;
259   NSString *lastDayUser;
260
261   users = [self subscriptionUsers];
262
263   if (currentTableUser == [users lastObject])
264     lastDayUser = @" lastDayUser";
265   else
266     lastDayUser = @"";
267     
268   return [NSString stringWithFormat: @"day appointmentsOf%@%@",
269                    currentTableUser, lastDayUser];
270 }
271
272 - (NSString *) clickableHourCellClass
273 {
274   return [NSString stringWithFormat: @"clickableHourCell clickableHourCell%@", currentTableHour];
275 }
276
277 - (NSNumber *) dayWidthPercentage
278 {
279   NSArray *users;
280
281   users = [self subscriptionUsers];
282
283   return [NSNumber numberWithFloat: (100.0 / [users count])];
284 }
285
286 - (NSNumber *) currentTableUserDayLeftPercentage
287 {
288   NSArray *users;
289
290   users = [self subscriptionUsers];
291
292   return [NSNumber numberWithFloat: ([users indexOfObject: currentTableUser]
293                                      * (100.0 / [users count]))];
294 }
295
296 @end