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