]> err.no Git - scalable-opengroupware.org/blob - UI/SOGoUI/SOGoAptFormatter.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1137 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / SOGoUI / SOGoAptFormatter.m
1 /*
2   Copyright (C) 2004-2005 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #import <NGExtensions/NSCalendarDate+misc.h>
23 #import <NGExtensions/NSObject+Logs.h>
24 #import <NGExtensions/NSNull+misc.h>
25
26 #import "SOGoAptFormatter.h"
27
28 @interface SOGoAptFormatter(PrivateAPI)
29 - (NSString *)titleForApt:(id)_apt :(NSCalendarDate *)_refDate;
30 - (NSString *)shortTitleForApt:(id)_apt :(NSCalendarDate *)_refDate;
31 - (NSTimeZone *)displayTZ;
32
33 - (void)appendTimeInfoForDate:(NSCalendarDate *)_date
34   usingReferenceDate:(NSCalendarDate *)_refDate
35   toBuffer:(NSMutableString *)_buf;
36
37 - (void)appendTimeInfoFromApt:(id)_apt
38   usingReferenceDate:(NSCalendarDate *)_refDate
39   toBuffer:(NSMutableString *)_buf;
40 @end
41
42 // TODO: Clean this up, put it into NGExtensions!
43 @interface NSCalendarDate (UIxCalMonthOverviewExtensions_UsedPrivates)
44 - (BOOL)isDateInSameMonth:(NSCalendarDate *)_other;
45 @end
46
47 @implementation SOGoAptFormatter
48
49 - (id)initWithDisplayTimeZone:(NSTimeZone *)_tz {
50   if ((self = [super init])) {
51     self->tz = [_tz retain];
52     [self setFullDetails];
53   }
54   return self;
55 }
56
57 - (void)dealloc {
58   [self->tz               release];
59   [self->privateTitle     release];
60   [self->titlePlaceholder release];
61   [super dealloc];
62 }
63
64 /* accessors */
65
66 - (void)setTooltip {
67   self->formatAction = @selector(tooltipForApt::);
68 }
69
70 - (void)setSingleLineFullDetails {
71   self->formatAction = @selector(singleLineFullDetailsForApt::);
72 }
73
74 - (void)setFullDetails {
75   self->formatAction = @selector(fullDetailsForApt::);
76 }
77
78 - (void)setPrivateTooltip {
79   self->formatAction = @selector(tooltipForPrivateApt::);
80 }
81
82 - (void)setPrivateDetails {
83   self->formatAction = @selector(detailsForPrivateApt::);
84 }
85
86 - (void)setTitleOnly {
87   self->formatAction = @selector(titleForApt::);
88 }
89
90 - (void)setShortTitleOnly {
91   self->formatAction = @selector(shortTitleForApt::);
92 }
93
94 - (void)setShortMonthTitleOnly {
95   self->formatAction = @selector(shortMonthTitleForApt::);
96 }
97
98 - (void)setPrivateSuppressAll {
99   self->formatAction = @selector(suppressApt::);
100 }
101
102 - (void)setPrivateTitleOnly {
103   self->formatAction = @selector(titleOnlyForPrivateApt::);
104 }
105
106 - (void)setPrivateTitle:(NSString *)_privateTitle {
107   ASSIGN(self->privateTitle, _privateTitle);
108 }
109 - (NSString *)privateTitle {
110   return self->privateTitle;
111 }
112
113 - (void)setTitlePlaceholder:(NSString *)_titlePlaceholder {
114   ASSIGN(self->titlePlaceholder, _titlePlaceholder);
115 }
116 - (NSString *)titlePlaceholder {
117   return self->titlePlaceholder;
118 }
119
120 - (void)setOmitsEndDate {
121   self->omitsEndDate = YES;
122 }
123
124 - (NSString *)stringForObjectValue:(id)_obj {
125   [self warnWithFormat:@"%s called, please use "
126                        @"stringForObjectValue:referenceDate: instead!",
127                        __PRETTY_FUNCTION__];
128   return [self stringForObjectValue:_obj referenceDate:nil];
129 }
130
131 - (NSString *)stringForObjectValue:(id)_obj
132   referenceDate:(NSCalendarDate *)_refDate
133 {
134   return [self performSelector:self->formatAction
135                withObject:_obj
136                withObject:_refDate];
137 }
138
139 /* Private */
140
141 - (NSTimeZone *)displayTZ {
142   return self->tz;
143 }
144
145 - (void)appendTimeInfoForDate:(NSCalendarDate *)_date
146   usingReferenceDate:(NSCalendarDate *)_refDate
147   toBuffer:(NSMutableString *)_buf
148 {
149   /* several cases:
150    * 12:00
151    * 12:00 - 13:00
152    * 12:00 (07-05) - 13:00 (07-07)
153    * 12:00 (12-30-2004) - 13:00 (01-01-2005)
154    */
155
156   [_buf appendFormat:@"%02i:%02i",
157                      [_date hourOfDay],
158                      [_date minuteOfHour]];
159   if (_refDate && ![_date isDateOnSameDay:_refDate]) {
160     [_buf appendFormat:@" (%02i-%02i",
161                        [_date monthOfYear],
162                        [_date dayOfMonth]];
163     if ([_date yearOfCommonEra] != [_refDate yearOfCommonEra])
164       [_buf appendFormat:@"-%04i", [_date yearOfCommonEra]];
165     [_buf appendString:@")"];
166   }
167 }
168
169 - (void)appendTimeInfoFromApt:(id)_apt
170   usingReferenceDate:(NSCalendarDate *)_refDate
171   toBuffer:(NSMutableString *)_buf
172 {
173   NSCalendarDate *startDate, *endDate, *date;
174   NSTimeZone     *dtz;
175   BOOL           spansRange;
176
177   spansRange = NO;
178   dtz        = [self displayTZ];
179   startDate  = [_apt valueForKey:@"startDate"];
180   [startDate setTimeZone:dtz];
181   endDate    = [_apt valueForKey:@"endDate"];
182   if(endDate != nil) {
183     [endDate setTimeZone:dtz];
184     spansRange = ![endDate isEqualToDate:startDate];
185   }
186   if (_refDate)
187     [_refDate setTimeZone:dtz];
188
189 #if 0
190   if (!_refDate || [startDate isDateOnSameDay:_refDate])
191     date = startDate;
192   else
193     date = [startDate hour:0 minute:0];
194 #else
195   date = startDate;
196 #endif
197
198   [self appendTimeInfoForDate:date
199         usingReferenceDate:_refDate
200         toBuffer:_buf];
201
202   if (spansRange && !self->omitsEndDate) {
203     [_buf appendString:@" - "];
204 #if 0
205     if (!_refDate || [endDate isDateOnSameDay:_refDate])
206       date = endDate;
207     else
208       date = [endDate hour:23 minute:59];
209 #else
210     date = endDate;
211 #endif
212     [self appendTimeInfoForDate:date
213           usingReferenceDate:_refDate
214           toBuffer:_buf];
215   }
216 }
217
218 - (NSString *)titleForApt:(id)_apt :(NSCalendarDate *)_refDate {
219   NSString *title;
220   
221   title = [_apt valueForKey:@"title"];
222   if (![title isNotEmpty])
223     title = [self titlePlaceholder];
224   return title;
225 }
226
227 - (NSString *)shortTitleForApt:(id)_apt :(NSCalendarDate *)_refDate {
228   NSString *title;
229   
230   title = [self titleForApt:_apt :_refDate];
231   if ([title length] > 50)
232     title = [[title substringToIndex: 49] stringByAppendingString:@"..."];
233   
234   return title;
235 }
236
237 - (NSString *)shortMonthTitleForApt:(id)_apt :(NSCalendarDate *)_refDate {
238   NSMutableString *title;
239   NSCalendarDate *startDate;
240   NSTimeZone *dtz;
241
242   title = [NSMutableString new];
243   [title autorelease];
244
245   dtz        = [self displayTZ];
246   startDate  = [_apt valueForKey: @"startDate"];
247   [startDate setTimeZone:dtz];
248   [self appendTimeInfoForDate: startDate usingReferenceDate: nil
249         toBuffer: title];
250   [title appendFormat: @" %@", [self titleForApt:_apt :_refDate]];
251   
252   return title;
253 }
254
255 - (NSString *)singleLineFullDetailsForApt:(id)_apt :(NSCalendarDate *)_refDate {
256   NSMutableString *aptDescr;
257   NSString        *s;
258   
259   aptDescr = [NSMutableString stringWithCapacity:60];
260   [self appendTimeInfoFromApt:_apt
261         usingReferenceDate:_refDate
262         toBuffer:aptDescr];
263   if ((s = [_apt valueForKey:@"location"]) != nil) {
264     [aptDescr appendFormat:@"; (%@)", s];
265   }
266   if ((s = [self titleForApt:_apt :_refDate]) != nil)
267     [aptDescr appendFormat:@"; %@", s];
268   return aptDescr;
269 }
270
271 - (NSString *) fullDetailsForApt: (id)_apt
272                                 : (NSCalendarDate *)_refDate
273 {
274   NSMutableString *aptDescr;
275   NSString *s;
276
277   aptDescr = [NSMutableString stringWithCapacity: 60];
278   [self appendTimeInfoFromApt: _apt
279         usingReferenceDate: _refDate
280         toBuffer: aptDescr];
281   s = [_apt valueForKey: @"location"];
282   if ([s length] > 0)
283     {
284       if ([s length] > 50)
285         s = [[s substringToIndex: 49] stringByAppendingString: @"..."];
286       [aptDescr appendFormat:@" (%@)", s];
287     }
288   s = [self shortTitleForApt: _apt : _refDate];
289   if ([s length] > 0)
290     [aptDescr appendFormat:@"<br />%@", s];
291   
292   return aptDescr;
293 }
294
295 - (NSString *) detailsForPrivateApt: (id) _apt
296                                    : (NSCalendarDate *) _refDate
297 {
298   NSMutableString *aptDescr;
299   NSString        *s;
300
301   aptDescr = [NSMutableString stringWithCapacity:40];
302   [self appendTimeInfoFromApt:_apt
303         usingReferenceDate:_refDate
304         toBuffer:aptDescr];
305   if ((s = [self privateTitle]) != nil)
306     [aptDescr appendFormat:@"<br />%@", s];
307   return aptDescr;
308 }
309
310 - (NSString *) titleOnlyForPrivateApt: (id)_apt
311                                      : (NSCalendarDate *) _refDate
312 {
313   NSString *s;
314   
315   s = [self privateTitle];
316   if (!s)
317     s = @"";
318
319   return s;
320 }
321
322 - (NSString *) tooltipForApt: (id)_apt
323                             : (NSCalendarDate *) _refDate
324 {
325   NSMutableString *aptDescr;
326   NSString *s;
327
328   aptDescr = [NSMutableString stringWithCapacity: 60];
329   [aptDescr appendString: @"Date: "];
330   [self appendTimeInfoFromApt: _apt
331         usingReferenceDate: _refDate
332         toBuffer: aptDescr];
333   s = [self titleForApt: _apt : _refDate];
334   if ([s length] > 0)
335     [aptDescr appendFormat: @"\nTitle: %@", s];
336   s = [_apt valueForKey: @"location"];
337   if ([s length] > 0)
338     [aptDescr appendFormat: @"\nLocation: %@", s];
339   s = [_apt valueForKey: @"description"];
340   if ([s length] > 0)
341     [aptDescr appendFormat:@"\n%@", s];
342
343   return aptDescr;
344 }
345
346 - (NSString *) tooltipForPrivateApt: (id) _apt
347                                    : (NSCalendarDate *) _refDate
348 {
349   NSMutableString *aptDescr;
350   NSString *s;
351   
352   aptDescr = [NSMutableString stringWithCapacity: 25];
353   [self appendTimeInfoFromApt: _apt
354         usingReferenceDate: _refDate
355         toBuffer: aptDescr];  
356   if ((s = [self privateTitle]) != nil)
357     [aptDescr appendFormat:@"\n%@", s];
358
359   return aptDescr;
360 }
361
362 - (NSString *) suppressApt: (id) _apt
363                           : (NSCalendarDate *) _refDate
364 {
365   return @"";
366 }
367
368 @end /* SOGoAptFormatter */