]> err.no Git - scalable-opengroupware.org/blob - UI/MailerUI/UIxMailFormatter.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1097 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / MailerUI / UIxMailFormatter.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
22 #include "UIxMailFormatter.h"
23 #include "common.h"
24
25 static Class StrClass     = Nil;
26 static Class CalDateClass = Nil;
27
28 @implementation UIxMailFormatter
29
30 static BOOL debugOn = YES;
31
32 + (void)initialize {
33   StrClass     = [NSString       class];
34   CalDateClass = [NSCalendarDate class];
35 }
36
37 /* labels */
38
39 - (NSString *)labelForKey:(NSString *)_key {
40   // TODO: fetch labels from context
41   return _key;
42 }
43
44 /* debugging */
45
46 - (BOOL)isDebuggingEnabled {
47   return debugOn;
48 }
49
50 @end /* UIxMailFormatter */
51
52 @implementation UIxMailDateFormatter
53
54 - (id)init {
55   if ((self = [super init])) {
56     static NSTimeZone *met = nil;
57     if (met == nil) met = [[NSTimeZone timeZoneWithName:@"MET"] retain];
58     
59     self->timeZone = [met retain];
60     self->dfFlags.showOnlyTimeForToday  = 1;
61     self->dfFlags.showLabelsForNearDays = 1;
62   }
63   return self;
64 }
65
66 - (void)dealloc {
67   [self->timeZone release];
68   [self->now      release];
69   [super dealloc];
70 }
71
72 /* configuration */
73
74 - (NSTimeZone *)timeZone
75 {
76   return self->timeZone;
77 }
78
79 - (void) setTimeZone: (NSTimeZone *) newTimeZone
80 {
81   if (timeZone)
82     [timeZone release];
83
84   timeZone = newTimeZone;
85
86   if (timeZone)
87     [timeZone retain];
88 }
89
90 - (BOOL)showOnlyTimeForToday {
91   return self->dfFlags.showOnlyTimeForToday ? YES : NO;
92 }
93 - (BOOL)showLabelsForNearDays {
94   return self->dfFlags.showLabelsForNearDays ? YES : NO;
95 }
96
97 /* formatting dates */
98
99 - (NSString *)stringForTime:(NSCalendarDate *)_d prefix:(char *)_p {
100   /* Note: prefix is not allowed to be long! */
101   char buf[32];
102   
103   if (_p == NULL) _p = "";
104   sprintf(buf, "%s%02i:%02i", _p, [_d hourOfDay], [_d minuteOfHour]);
105   return [StrClass stringWithCString:buf];
106 }
107
108 - (NSString *)stringForCalendarDate:(NSCalendarDate *)_date {
109   char buf[32];
110   
111   if (self->now == nil) {
112     self->now = [[NSCalendarDate alloc] init];
113     [self->now setTimeZone:[self timeZone]];
114   }
115   [_date setTimeZone:[self timeZone]];
116
117   if ([self showOnlyTimeForToday] && [_date isDateOnSameDay:self->now])
118     return [self stringForTime:_date prefix:NULL];
119   
120   if ([self showLabelsForNearDays]) {
121     NSString *label;
122     
123     if ([_date isDateOnSameDay:self->now])
124       label = [self labelForKey:@"today"];
125     else if ([_date isDateOnSameDay:[self->now yesterday]])
126       label = [self labelForKey:@"yesterday"];
127     else
128       label = nil;
129     
130     if (label != nil) {
131       return [label stringByAppendingString:
132                       [self stringForTime:_date prefix:", "]];
133     }
134   }
135   
136   /* 26.08.2004 13:24 */
137   sprintf(buf, "%02d.%02d.%04d %02d:%02d",
138           [_date dayOfMonth], [_date monthOfYear], [_date yearOfCommonEra],
139           [_date hourOfDay], [_date minuteOfHour]);
140   return [StrClass stringWithCString:buf];
141 }
142
143 /* formatter entry function */
144
145 - (NSString *)stringForObjectValue:(id)_date {
146   if (![_date isNotNull])
147     return nil;
148   
149   if ([_date isKindOfClass:StrClass]) /* already formatted */
150     return _date;
151   
152   if ([_date isKindOfClass:CalDateClass])
153     return [self stringForCalendarDate:_date];
154   
155   [self debugWithFormat:
156           @"NOTE: unexpected object for date formatter: %@<%@>",
157           _date, NSStringFromClass([_date class])];
158   return [_date description];
159 }
160
161 @end /* UIxMailDateFormatter */