]> err.no Git - scalable-opengroupware.org/blob - UI/SOGoUI/SOGoAptFormatter.m
moved SOGo files up
[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 #include "SOGoAptFormatter.h"
23 #include "common.h"
24
25 @interface SOGoAptFormatter(PrivateAPI)
26 - (NSString *)titleForApt:(id)_apt :(NSCalendarDate *)_refDate;
27 - (NSString *)shortTitleForApt:(id)_apt :(NSCalendarDate *)_refDate;
28 - (NSTimeZone *)displayTZ;
29
30 - (void)appendTimeInfoForDate:(NSCalendarDate *)_date
31   usingReferenceDate:(NSCalendarDate *)_refDate
32   toBuffer:(NSMutableString *)_buf;
33
34 - (void)appendTimeInfoFromApt:(id)_apt
35   usingReferenceDate:(NSCalendarDate *)_refDate
36   toBuffer:(NSMutableString *)_buf;
37 @end
38
39 // TODO: Clean this up, put it into NGExtensions!
40 @interface NSCalendarDate (UIxCalMonthOverviewExtensions_UsedPrivates)
41 - (BOOL)isDateInSameMonth:(NSCalendarDate *)_other;
42 @end
43
44 @implementation SOGoAptFormatter
45
46 - (id)initWithDisplayTimeZone:(NSTimeZone *)_tz {
47   if ((self = [super init])) {
48     self->tz = [_tz retain];
49     [self setFullDetails];
50   }
51   return self;
52 }
53
54 - (void)dealloc {
55   [self->tz               release];
56   [self->privateTitle     release];
57   [self->titlePlaceholder release];
58   [super dealloc];
59 }
60
61 /* accessors */
62
63 - (void)setTooltip {
64   self->formatAction = @selector(tooltipForApt::);
65 }
66
67 - (void)setSingleLineFullDetails {
68   self->formatAction = @selector(singleLineFullDetailsForApt::);
69 }
70
71 - (void)setFullDetails {
72   self->formatAction = @selector(fullDetailsForApt::);
73 }
74
75 - (void)setPrivateTooltip {
76   self->formatAction = @selector(tooltipForPrivateApt::);
77 }
78
79 - (void)setPrivateDetails {
80   self->formatAction = @selector(detailsForPrivateApt::);
81 }
82
83 - (void)setTitleOnly {
84   self->formatAction = @selector(titleForApt::);
85 }
86
87 - (void)setShortTitleOnly {
88   self->formatAction = @selector(shortTitleForApt::);
89 }
90
91 - (void)setPrivateSuppressAll {
92   self->formatAction = @selector(suppressApt::);
93 }
94
95 - (void)setPrivateTitleOnly {
96   self->formatAction = @selector(titleOnlyForPrivateApt::);
97 }
98
99 - (void)setPrivateTitle:(NSString *)_privateTitle {
100   ASSIGN(self->privateTitle, _privateTitle);
101 }
102 - (NSString *)privateTitle {
103   return self->privateTitle;
104 }
105
106 - (void)setTitlePlaceholder:(NSString *)_titlePlaceholder {
107   ASSIGN(self->titlePlaceholder, _titlePlaceholder);
108 }
109 - (NSString *)titlePlaceholder {
110   return self->titlePlaceholder;
111 }
112
113 - (void)setOmitsEndDate {
114   self->omitsEndDate = YES;
115 }
116
117 - (NSString *)stringForObjectValue:(id)_obj {
118   [self warnWithFormat:@"%s called, please use "
119                        @"stringForObjectValue:referenceDate: instead!",
120                        __PRETTY_FUNCTION__];
121   return [self stringForObjectValue:_obj referenceDate:nil];
122 }
123
124 - (NSString *)stringForObjectValue:(id)_obj
125   referenceDate:(NSCalendarDate *)_refDate
126 {
127   return [self performSelector:self->formatAction
128                withObject:_obj
129                withObject:_refDate];
130 }
131
132 /* Private */
133
134 - (NSTimeZone *)displayTZ {
135   return self->tz;
136 }
137
138 - (void)appendTimeInfoForDate:(NSCalendarDate *)_date
139   usingReferenceDate:(NSCalendarDate *)_refDate
140   toBuffer:(NSMutableString *)_buf
141 {
142   /* several cases:
143    * 12:00
144    * 12:00 - 13:00
145    * 12:00 (07-05) - 13:00 (07-07)
146    * 12:00 (12-30-2004) - 13:00 (01-01-2005)
147    */
148
149   [_buf appendFormat:@"%02i:%02i",
150                      [_date hourOfDay],
151                      [_date minuteOfHour]];
152   if (_refDate && ![_date isDateOnSameDay:_refDate]) {
153     [_buf appendFormat:@" (%02i-%02i",
154                        [_date monthOfYear],
155                        [_date dayOfMonth]];
156     if ([_date yearOfCommonEra] != [_refDate yearOfCommonEra])
157       [_buf appendFormat:@"-%04i", [_date yearOfCommonEra]];
158     [_buf appendString:@")"];
159   }
160 }
161
162 - (void)appendTimeInfoFromApt:(id)_apt
163   usingReferenceDate:(NSCalendarDate *)_refDate
164   toBuffer:(NSMutableString *)_buf
165 {
166   NSCalendarDate *startDate, *endDate, *date;
167   NSTimeZone     *dtz;
168   BOOL           spansRange;
169
170   spansRange = NO;
171   dtz        = [self displayTZ];
172   startDate  = [_apt valueForKey:@"startDate"];
173   [startDate setTimeZone:dtz];
174   endDate    = [_apt valueForKey:@"endDate"];
175   if(endDate != nil) {
176     [endDate setTimeZone:dtz];
177     spansRange = ![endDate isEqualToDate:startDate];
178   }
179   if (_refDate)
180     [_refDate setTimeZone:dtz];
181
182 #if 0
183   if (!_refDate || [startDate isDateOnSameDay:_refDate])
184     date = startDate;
185   else
186     date = [startDate hour:0 minute:0];
187 #else
188   date = startDate;
189 #endif
190
191   [self appendTimeInfoForDate:date
192         usingReferenceDate:_refDate
193         toBuffer:_buf];
194
195   if (spansRange && !self->omitsEndDate) {
196     [_buf appendString:@" - "];
197 #if 0
198     if (!_refDate || [endDate isDateOnSameDay:_refDate])
199       date = endDate;
200     else
201       date = [endDate hour:23 minute:59];
202 #else
203     date = endDate;
204 #endif
205     [self appendTimeInfoForDate:date
206           usingReferenceDate:_refDate
207           toBuffer:_buf];
208   }
209 }
210
211 - (NSString *)titleForApt:(id)_apt :(NSCalendarDate *)_refDate {
212   NSString *title;
213   
214   title = [_apt valueForKey:@"title"];
215   if (![title isNotEmpty])
216     title = [self titlePlaceholder];
217   return title;
218 }
219
220 - (NSString *)shortTitleForApt:(id)_apt :(NSCalendarDate *)_refDate {
221   NSString *title;
222   
223   title = [self titleForApt:_apt :_refDate];
224   if ([title length] > 12)
225     title = [[title substringToIndex:11] stringByAppendingString:@"..."];
226   
227   return title;
228 }
229
230 - (NSString *)singleLineFullDetailsForApt:(id)_apt :(NSCalendarDate *)_refDate {
231   NSMutableString *aptDescr;
232   NSString        *s;
233   
234   aptDescr = [NSMutableString stringWithCapacity:60];
235   [self appendTimeInfoFromApt:_apt
236         usingReferenceDate:_refDate
237         toBuffer:aptDescr];
238   if ((s = [_apt valueForKey:@"location"]) != nil) {
239     [aptDescr appendFormat:@"; (%@)", s];
240   }
241   if ((s = [self titleForApt:_apt :_refDate]) != nil)
242     [aptDescr appendFormat:@"; %@", s];
243   return aptDescr;
244 }
245
246 - (NSString *)fullDetailsForApt:(id)_apt :(NSCalendarDate *)_refDate {
247   NSMutableString *aptDescr;
248   NSString        *s;
249     
250   aptDescr = [NSMutableString stringWithCapacity:60];
251   [self appendTimeInfoFromApt:_apt
252         usingReferenceDate:_refDate
253         toBuffer:aptDescr];
254   if ((s = [_apt valueForKey:@"location"]) != nil) {
255     if([s length] > 12)
256       s = [[s substringToIndex:11] stringByAppendingString:@"..."];
257     [aptDescr appendFormat:@" (%@)", s];
258   }
259   if ((s = [self shortTitleForApt:_apt :_refDate]) != nil)
260     [aptDescr appendFormat:@"<br />%@", s];
261   
262   return aptDescr;
263 }
264
265 - (NSString *)detailsForPrivateApt:(id)_apt :(NSCalendarDate *)_refDate {
266   NSMutableString *aptDescr;
267   NSString        *s;
268
269   aptDescr = [NSMutableString stringWithCapacity:40];
270   [self appendTimeInfoFromApt:_apt
271         usingReferenceDate:_refDate
272         toBuffer:aptDescr];
273   if ((s = [self privateTitle]) != nil)
274     [aptDescr appendFormat:@"<br />%@", s];
275   return aptDescr;
276 }
277
278 - (NSString *)titleOnlyForPrivateApt:(id)_apt :(NSCalendarDate *)_refDate {
279   NSString *s;
280   
281   s = [self privateTitle];
282   if(!s)
283     return @"";
284   return s;
285 }
286
287 - (NSString *)tooltipForApt:(id)_apt :(NSCalendarDate *)_refDate {
288   NSMutableString *aptDescr;
289   NSString        *s;
290
291   aptDescr = [NSMutableString stringWithCapacity:60];
292   [self appendTimeInfoFromApt:_apt
293         usingReferenceDate:_refDate
294         toBuffer:aptDescr];  
295   if ((s = [self titleForApt:_apt :_refDate]) != nil)
296     [aptDescr appendFormat:@"\n%@", s];
297   if ((s = [_apt valueForKey:@"location"]) != nil)
298     [aptDescr appendFormat:@"\n%@", s];
299     
300   return aptDescr;
301 }
302
303 - (NSString *)tooltipForPrivateApt:(id)_apt :(NSCalendarDate *)_refDate {
304   NSMutableString *aptDescr;
305   NSString        *s;
306   
307   aptDescr = [NSMutableString stringWithCapacity:25];
308   [self appendTimeInfoFromApt:_apt
309         usingReferenceDate:_refDate
310         toBuffer:aptDescr];  
311   if ((s = [self privateTitle]) != nil)
312     [aptDescr appendFormat:@"\n%@", s];
313
314   return aptDescr;
315 }
316
317 - (NSString *)suppressApt:(id)_apt :(NSCalendarDate *)_refDate {
318   return @"";
319 }
320
321 @end /* SOGoAptFormatter */