]> err.no Git - scalable-opengroupware.org/blob - SOGo/UI/SOGoUI/SOGoAptFormatter.m
"private appointment" prefix for private appointments
[scalable-opengroupware.org] / SOGo / UI / SOGoUI / SOGoAptFormatter.m
1 /*
2   Copyright (C) 2004 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 // $Id$
22
23 #include "SOGoAptFormatter.h"
24 #include "common.h"
25
26 @interface SOGoAptFormatter(PrivateAPI)
27 - (NSString *)titleForApt:(id)_apt;
28 - (NSString *)shortTitleForApt:(id)_apt;
29 - (NSTimeZone *)displayTZ;
30 - (void)appendTimeInfoFromApt:(id)_apt toBuffer:(NSMutableString *)_buf;
31 @end
32
33 @implementation SOGoAptFormatter
34
35 - (id)initWithDisplayTimeZone:(NSTimeZone *)_tz {
36   if ((self = [super init])) {
37     self->tz = [_tz retain];
38     [self setFullDetails];
39   }
40   return self;
41 }
42
43 - (void)dealloc {
44   [self->tz           release];
45   [self->privateTitle release];
46   [super dealloc];
47 }
48
49 /* accessors */
50
51 - (void)setTooltip {
52   self->formatAction = @selector(tooltipForApt:);
53 }
54
55 - (void)setFullDetails {
56   self->formatAction = @selector(fullDetailsForApt:);
57 }
58
59 - (void)setPrivateTooltip {
60   self->formatAction = @selector(tooltipForPrivateApt:);
61 }
62
63 - (void)setPrivateDetails {
64   self->formatAction = @selector(detailsForPrivateApt:);
65 }
66
67 - (void)setTitleOnly {
68   self->formatAction = @selector(titleForApt:);
69 }
70
71 - (void)setShortTitleOnly {
72   self->formatAction = @selector(shortTitleForApt:);
73 }
74
75 - (void)setPrivateSuppressAll {
76   self->formatAction = @selector(suppressApt:);
77 }
78
79 - (void)setPrivateTitle:(NSString *)_privateTitle {
80   ASSIGN(self->privateTitle, _privateTitle);
81 }
82 - (NSString *)privateTitle {
83   return self->privateTitle;
84 }
85
86 - (NSString *)stringForObjectValue:(id)_obj {
87   return [self performSelector:self->formatAction withObject:_obj];
88 }
89
90 /* Private */
91
92 - (NSTimeZone *)displayTZ {
93   return self->tz;
94 }
95
96 - (void)appendTimeInfoFromApt:(id)_apt toBuffer:(NSMutableString *)_buf {
97   NSCalendarDate *startDate, *endDate;
98   BOOL           spansRange;
99
100   spansRange = NO;
101   startDate  = [_apt valueForKey:@"startDate"];
102   [startDate setTimeZone:[self displayTZ]];
103   endDate    = [_apt valueForKey:@"endDate"];
104   if(endDate != nil) {
105     [endDate setTimeZone:[self displayTZ]];
106     spansRange = ![endDate isEqualToDate:startDate];
107   }
108   [_buf appendFormat:@"%02i:%02i",
109                        [startDate hourOfDay],
110                        [startDate minuteOfHour]];
111   if(spansRange) {
112     [_buf appendFormat:@", %02i:%02i",
113                          [endDate hourOfDay],
114                          [endDate minuteOfHour]];
115   }
116 }
117
118 - (NSString *)titleForApt:(id)_apt {
119   return [_apt valueForKey:@"title"];
120 }
121
122 - (NSString *)shortTitleForApt:(id)_apt {
123   NSString *title;
124   
125   title = [self titleForApt:_apt];
126   if ([title length] > 12)
127     title = [[title substringToIndex:11] stringByAppendingString:@"..."];
128   
129   return title;
130 }
131
132 - (NSString *)fullDetailsForApt:(id)_apt {
133   NSMutableString *aptDescr;
134   NSString *s;
135     
136   aptDescr = [NSMutableString stringWithCapacity:60];
137   [self appendTimeInfoFromApt:_apt toBuffer:aptDescr];
138   if ((s = [_apt valueForKey:@"location"]) != nil) {
139     if([s length] > 12)
140       s = [[s substringToIndex:11] stringByAppendingString:@"..."];
141     [aptDescr appendFormat:@" (%@)", s];
142   }
143   if ((s = [_apt valueForKey:@"title"]) != nil)
144     [aptDescr appendFormat:@"<br />%@", [self shortTitleForApt:_apt]];
145   
146   return aptDescr;
147 }
148
149 - (NSString *)detailsForPrivateApt:(id)_apt {
150   NSMutableString *aptDescr;
151   NSString        *s;
152
153   aptDescr = [NSMutableString stringWithCapacity:40];
154   [self appendTimeInfoFromApt:_apt toBuffer:aptDescr];
155   if ((s = [self privateTitle]) != nil)
156     [aptDescr appendFormat:@"<br />%@", s];
157   return aptDescr;
158 }
159
160 - (NSString *)tooltipForApt:(id)_apt {
161   NSCalendarDate  *startDate, *endDate;
162   NSMutableString *aptDescr;
163   NSString        *s;
164   BOOL            spansRange;
165     
166   spansRange = NO;
167   startDate = [_apt valueForKey:@"startDate"];
168   [startDate setTimeZone:[self displayTZ]];
169   endDate = [_apt valueForKey:@"endDate"];
170   if(endDate != nil) {
171     [endDate setTimeZone:[self displayTZ]];
172     spansRange = ![endDate isEqualToDate:startDate];
173   }
174   aptDescr = [NSMutableString stringWithCapacity:60];
175   [aptDescr appendString:@"appointment"];
176   [aptDescr appendFormat:@"\n%02i:%02i",
177             [startDate hourOfDay],
178             [startDate minuteOfHour]];
179   if (spansRange) {
180     [aptDescr appendFormat:@" - %02i:%02i",
181               [endDate hourOfDay],
182               [endDate minuteOfHour]];
183   }
184     
185   if ((s = [_apt valueForKey:@"title"]) != nil)
186     [aptDescr appendFormat:@"\n%@", s];
187   if ((s = [_apt valueForKey:@"location"]) != nil)
188     [aptDescr appendFormat:@"\n%@", s];
189     
190   return aptDescr;
191 }
192
193 - (NSString *)tooltipForPrivateApt:(id)_apt {
194   NSCalendarDate  *startDate, *endDate;
195   NSMutableString *aptDescr;
196   NSString        *s;
197   BOOL            spansRange;
198   
199   spansRange = NO;
200   startDate  = [_apt valueForKey:@"startDate"];
201   [startDate setTimeZone:[self displayTZ]];
202   endDate = [_apt valueForKey:@"endDate"];
203   if(endDate != nil) {
204     [endDate setTimeZone:[self displayTZ]];
205     spansRange = ![endDate isEqualToDate:startDate];
206   }
207   aptDescr = [NSMutableString stringWithCapacity:25];
208   [aptDescr appendString:@"appointment"];
209   [aptDescr appendFormat:@"\n%02i:%02i",
210     [startDate hourOfDay],
211     [startDate minuteOfHour]];
212   if (spansRange) {
213     [aptDescr appendFormat:@" - %02i:%02i",
214       [endDate hourOfDay],
215       [endDate minuteOfHour]];
216   }
217
218   if ((s = [self privateTitle]) != nil)
219     [aptDescr appendFormat:@"\n%@", s];
220
221   return aptDescr;
222 }
223
224 - (NSString *)suppressApt:(id)_apt {
225   return @"";
226 }
227
228 @end /* SOGoAptFormatter */