]> err.no Git - scalable-opengroupware.org/blob - SOGo/UI/Scheduler/UIxCalView.m
69fa83ae0080c9e5674bf24ce287ea23b6b92926
[scalable-opengroupware.org] / SOGo / UI / Scheduler / UIxCalView.m
1 // $Id$
2
3 #include "UIxCalView.h"
4 #include "common.h"
5 #include "UIxAppointmentFormatter.h"
6 #include <OGoContentStore/OCSFolder.h>
7 #include "SoObjects/Appointments/SOGoAppointmentFolder.h"
8
9 @implementation UIxCalView
10
11 - (void)dealloc {
12   [self->appointment  release];
13   [self->appointments release];
14   [self->currentDay   release];
15   [super dealloc];
16 }
17
18 /* accessors */
19
20 - (void)setAppointments:(NSArray *)_apts {
21   ASSIGN(self->appointments, _apts);
22 }
23 - (NSArray *)appointments {
24   return self->appointments;
25 }
26
27 - (void)setAppointment:(id)_apt {
28   ASSIGN(self->appointment, _apt);
29 }
30 - (id)appointment {
31   return self->appointment;
32 }
33
34 - (NSDictionary *)aptTypeDict {
35     return nil;
36 }
37
38 - (NSString *)aptTypeLabel {
39     return @"aptLabel";
40 }
41
42 - (NSString *)aptTypeIcon {
43     return @"";
44 }
45
46 - (NSString *)shortTextForApt {
47   NSCalendarDate *startDate;
48   NSString *ms;
49   
50   startDate = [[self appointment] valueForKey:@"startDate"];
51   [startDate setTimeZone:[self viewTimeZone]];
52   ms = [NSString stringWithFormat:@"%02i:%02i %@",
53                  [startDate hourOfDay],
54                  [startDate minuteOfHour],
55                  [[self appointment] valueForKey:@"title"]];
56   return ms;
57 }
58
59 - (NSString *)shortTitleForApt {
60   NSString *title;
61     
62   title = [self->appointment valueForKey:@"title"];
63   if ([title length] > 12)
64     title = [[title substringToIndex:11] stringByAppendingString:@"..."];
65   
66   return title;
67 }
68
69 - (NSCalendarDate *)referenceDateForFormatter {
70   return [self selectedDate];
71 }
72
73 /* current day related */
74
75 - (void)setCurrentDay:(NSCalendarDate *)_day {
76   [_day setTimeZone:[self viewTimeZone]];
77   ASSIGN(self->currentDay, _day);
78 }
79 - (NSCalendarDate *)currentDay {
80   return self->currentDay;
81 }
82
83 - (NSString *)currentDayName {
84   return [self localizedNameForDayOfWeek:[self->currentDay dayOfWeek]];
85 }
86
87 - (BOOL)hasDayInfo {
88     return [self hasHoldidayInfo] || ([[self allDayApts] count] != 0);
89 }
90
91 - (BOOL)hasHoldidayInfo {
92     return NO;
93 }
94
95 - (NSArray *)allDayApts {
96     return [NSArray array];
97 }
98
99
100 /* defaults */
101
102
103 - (BOOL)showFullNames {
104     return YES;
105 }
106
107 - (BOOL)showAMPMDates {
108     return NO;
109 }
110
111
112 /* URLs */
113
114 - (NSString *)appointmentViewURL {
115   id pkey;
116   
117   if ((pkey = [[self appointment] valueForKey:@"uid"]) == nil)
118     return nil;
119   
120   return [NSString stringWithFormat:@"%@/view", pkey];
121 }
122
123
124 /* backend */
125
126 /* resource URLs (TODO?) */
127
128 - (NSString *)resourcePath {
129   return @"/sogod.woa/WebServerResources/";
130 }
131
132 - (NSString *)favIconPath {
133   return [[self resourcePath] stringByAppendingPathComponent:@"favicon.ico"];
134 }
135 - (NSString *)cssPath {
136   NSString *path;
137   
138   // TODO: there should be reusable functionality for that!
139 #warning ZideStore specific?
140   path = @"ControlPanel/Products/ZideStoreUI/Resources/uix.css";
141   return [[self context] urlWithRequestHandlerKey:@"so"
142                          path:path
143                          queryString:nil];
144 }
145
146 - (NSString *)calCSSPath {
147   NSString *path;
148   
149   // TODO: there should be reusable functionality for that!
150 #warning ZideStore specific?
151   path = @"ControlPanel/Products/ZideStoreUI/Resources/calendar.css";
152   return [[self context] urlWithRequestHandlerKey:@"so"
153                          path:path
154                          queryString:nil];
155 }
156
157 /* fetching */
158
159 - (NSCalendarDate *)startDate {
160   return [self selectedDate];
161 }
162 - (NSCalendarDate *)endDate {
163   return [[self startDate] tomorrow];
164 }
165
166 - (NSArray *)_fetchCoreInfosForUIDs:(NSArray *)_uids 
167   uniqueWithSet:(NSMutableSet *)_uniquer 
168 {
169   NSMutableArray *ma;
170   unsigned i, count;
171   
172   count = [_uids count];
173   ma    = [NSMutableArray arrayWithCapacity:count * 10];
174   
175   for (i = 0; i < count; i++) {
176     OCSFolder *folder;
177     NSString *path;
178     NSString *uid;
179     NSArray  *res;
180         
181     uid = [_uids objectAtIndex:i];
182     if ([uid length] == 0) continue;
183
184     path = [[@"/Users/" stringByAppendingString:uid]
185                         stringByAppendingString:@"/Calendar"];
186     
187     if ((folder = [[self clientObject] ocsFolderForPath:path]) == nil) {
188       [self logWithFormat:@"ERROR: did not find user: %@", uid];
189       continue;
190     }
191     
192     res = [[self clientObject] fetchCoreInfosFromFolder:folder 
193                                from:[self startDate] to:[self endDate]];
194     if (res == nil) {
195       [self logWithFormat:@"ERROR: fetch failed for user: %@", uid];
196       continue;
197     }
198
199     /* perform uniquing */
200     {
201       int j, rcount;
202       
203       for (j = 0, rcount = [res count]; j < rcount; j++) {
204         NSDictionary *record;
205         
206         record = [res objectAtIndex:j];
207         
208         if ([_uniquer containsObject:[record valueForKey:@"uid"]])
209           continue;
210         
211         [ma addObject:record];
212         [_uniquer addObject:[record valueForKey:@"uid"]];
213       }
214     }
215   }
216   return ma;
217 }
218
219 - (NSArray *)fetchCoreInfos {
220   id      aptFolder;
221   NSArray *more;
222   id      uids;
223   
224   if (self->appointments)
225     return self->appointments;
226   
227   aptFolder = [self clientObject];
228   self->appointments =
229     [[aptFolder fetchCoreInfosFrom:[self startDate] to:[self endDate]] retain];
230   
231   uids = [[[[self context] request] formValueForKey:@"uids"] stringValue];
232   uids = [uids length] > 0 ? [uids componentsSeparatedByString:@","] : nil;
233   if ([uids count] > 0) {
234     NSMutableSet *availUIDs;
235     id tmp;
236     
237     /* make appointments unique, prefer the own "versions" */
238     tmp = [self->appointments valueForKey:@"uid"];
239     availUIDs = [[NSMutableSet alloc] initWithArray:tmp];
240     
241     more = [self _fetchCoreInfosForUIDs:uids uniqueWithSet:availUIDs];
242     if (more > 0) {
243       NSArray *tmp;
244       
245       tmp = self->appointments;
246       self->appointments = [[tmp arrayByAddingObjectsFromArray:more] retain];
247       [tmp release];
248     }
249     
250     [availUIDs release];
251   }
252   
253   return self->appointments;
254 }
255
256 /* date selection & conversion */
257
258
259 - (NSDictionary *)todayQueryParameters {
260   NSCalendarDate *date;
261     
262   date = [NSCalendarDate date]; /* today */
263   return [self queryParametersBySettingSelectedDate:date];
264 }
265
266 - (NSDictionary *)currentDayQueryParameters {
267   return [self queryParametersBySettingSelectedDate:self->currentDay];
268 }
269
270 - (NSDictionary *)queryParametersBySettingSelectedDate:(NSCalendarDate *)_date {
271     NSMutableDictionary *qp;
272     
273     qp = [[self queryParameters] mutableCopy];
274     [self setSelectedDateQueryParameter:_date inDictionary:qp];
275     return [qp autorelease];
276 }
277
278 - (void)setSelectedDateQueryParameter:(NSCalendarDate *)_newDate
279   inDictionary:(NSMutableDictionary *)_qp;
280 {
281   if(_newDate != nil)
282     [_qp setObject:[self dateStringForDate:_newDate] forKey:@"day"];
283   else
284     [_qp removeObjectForKey:@"day"];
285 }
286
287 @end /* UIxCalView */