]> err.no Git - scalable-opengroupware.org/blob - SOGo/UI/Scheduler/UIxCalView.m
git-svn-id: http://svn.opengroupware.org/SOGo/trunk@137 d1b88da0-ebda-0310-925b-ed51d...
[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   ASSIGN(self->currentDay, _day);
77 }
78 - (NSCalendarDate *)currentDay {
79   return self->currentDay;
80 }
81
82 - (NSString *)currentDayName {
83   // TODO: this is slow, use locale dictionary to speed this up
84   [self->currentDay setTimeZone:[self viewTimeZone]];
85   return [self->currentDay descriptionWithCalendarFormat:@"%A"];
86 }
87
88 - (BOOL)hasDayInfo {
89     return [self hasHoldidayInfo] || ([[self allDayApts] count] != 0);
90 }
91
92 - (BOOL)hasHoldidayInfo {
93     return NO;
94 }
95
96 - (NSArray *)allDayApts {
97     return [NSArray array];
98 }
99
100
101 /* defaults */
102
103
104 - (BOOL)showFullNames {
105     return YES;
106 }
107
108 - (BOOL)showAMPMDates {
109     return NO;
110 }
111
112
113 /* URLs */
114
115 - (NSString *)appointmentViewURL {
116   id pkey;
117   
118   if ((pkey = [[self appointment] valueForKey:@"uid"]) == nil)
119     return nil;
120   
121   return [NSString stringWithFormat:@"%@/view", pkey];
122 }
123
124
125 /* backend */
126
127 /* resource URLs (TODO?) */
128
129 - (NSString *)resourcePath {
130   return @"/sogod.woa/WebServerResources/";
131 }
132
133 - (NSString *)favIconPath {
134   return [[self resourcePath] stringByAppendingPathComponent:@"favicon.ico"];
135 }
136 - (NSString *)cssPath {
137   NSString *path;
138   
139   // TODO: there should be reusable functionality for that!
140 #warning ZideStore specific?
141   path = @"ControlPanel/Products/ZideStoreUI/Resources/zidestoreui.css";
142   return [[self context] urlWithRequestHandlerKey:@"so"
143                          path:path
144                          queryString:nil];
145 }
146
147 - (NSString *)calCSSPath {
148   NSString *path;
149   
150   // TODO: there should be reusable functionality for that!
151 #warning ZideStore specific?
152   path = @"ControlPanel/Products/ZideStoreUI/Resources/calendar.css";
153   return [[self context] urlWithRequestHandlerKey:@"so"
154                          path:path
155                          queryString:nil];
156 }
157
158 /* fetching */
159
160 - (NSCalendarDate *)startDate {
161   return [self selectedDate];
162 }
163 - (NSCalendarDate *)endDate {
164   return [[self startDate] tomorrow];
165 }
166
167 - (NSArray *)_fetchCoreInfosForUIDs:(NSArray *)_uids 
168   uniqueWithSet:(NSMutableSet *)_uniquer 
169 {
170   NSMutableArray *ma;
171   unsigned i, count;
172   
173   count = [_uids count];
174   ma    = [NSMutableArray arrayWithCapacity:count * 10];
175   
176   for (i = 0; i < count; i++) {
177     OCSFolder *folder;
178     NSString *path;
179     NSString *uid;
180     NSArray  *res;
181         
182     uid = [_uids objectAtIndex:i];
183     if ([uid length] == 0) continue;
184
185     path = [[@"/Users/" stringByAppendingString:uid]
186                         stringByAppendingString:@"/Calendar"];
187     
188     if ((folder = [[self clientObject] ocsFolderForPath:path]) == nil) {
189       [self logWithFormat:@"ERROR: did not find user: %@", uid];
190       continue;
191     }
192     
193     res = [[self clientObject] fetchCoreInfosFromFolder:folder 
194                                from:[self startDate] to:[self endDate]];
195     if (res == nil) {
196       [self logWithFormat:@"ERROR: fetch failed for user: %@", uid];
197       continue;
198     }
199
200     /* perform uniquing */
201     {
202       int j, rcount;
203       
204       for (j = 0, rcount = [res count]; j < rcount; j++) {
205         NSDictionary *record;
206         
207         record = [res objectAtIndex:j];
208         
209         if ([_uniquer containsObject:[record valueForKey:@"uid"]])
210           continue;
211         
212         [ma addObject:record];
213         [_uniquer addObject:[record valueForKey:@"uid"]];
214       }
215     }
216   }
217   return ma;
218 }
219
220 - (NSArray *)fetchCoreInfos {
221   id      aptFolder;
222   NSArray *more;
223   id      uids;
224   
225   if (self->appointments)
226     return self->appointments;
227   
228   aptFolder = [self clientObject];
229   self->appointments =
230     [[aptFolder fetchCoreInfosFrom:[self startDate] to:[self endDate]] retain];
231   
232   uids = [[[[self context] request] formValueForKey:@"uids"] stringValue];
233   uids = [uids length] > 0 ? [uids componentsSeparatedByString:@","] : nil;
234   if ([uids count] > 0) {
235     NSMutableSet *availUIDs;
236     id tmp;
237     
238     /* make appointments unique, prefer the own "versions" */
239     tmp = [self->appointments valueForKey:@"uid"];
240     availUIDs = [[NSMutableSet alloc] initWithArray:tmp];
241     
242     more = [self _fetchCoreInfosForUIDs:uids uniqueWithSet:availUIDs];
243     if (more > 0) {
244       NSArray *tmp;
245       
246       tmp = self->appointments;
247       self->appointments = [[tmp arrayByAddingObjectsFromArray:more] retain];
248       [tmp release];
249     }
250     
251     [availUIDs release];
252   }
253   
254   return self->appointments;
255 }
256
257 /* date selection & conversion */
258
259
260 - (NSDictionary *)todayQueryParameters {
261   NSCalendarDate *date;
262     
263   date = [NSCalendarDate date]; /* today */
264   return [self queryParametersBySettingSelectedDate:date];
265 }
266
267 - (NSDictionary *)currentDayQueryParameters {
268   return [self queryParametersBySettingSelectedDate:self->currentDay];
269 }
270
271 - (NSDictionary *)queryParametersBySettingSelectedDate:(NSCalendarDate *)_date {
272     NSMutableDictionary *qp;
273     
274     qp = [[self queryParameters] mutableCopy];
275     [self setSelectedDateQueryParameter:_date inDictionary:qp];
276     return [qp autorelease];
277 }
278
279 - (void)setSelectedDateQueryParameter:(NSCalendarDate *)_newDate
280   inDictionary:(NSMutableDictionary *)_qp;
281 {
282   if(_newDate != nil)
283     [_qp setObject:[self dateStringForDate:_newDate] forKey:@"day"];
284   else
285     [_qp removeObjectForKey:@"day"];
286 }
287
288 @end /* UIxCalView */