]> err.no Git - scalable-opengroupware.org/blob - UI/MailerUI/UIxMailFormatter.m
fixed strings file for Cocoa
[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   return self->timeZone;
76 }
77
78 - (BOOL)showOnlyTimeForToday {
79   return self->dfFlags.showOnlyTimeForToday ? YES : NO;
80 }
81 - (BOOL)showLabelsForNearDays {
82   return self->dfFlags.showLabelsForNearDays ? YES : NO;
83 }
84
85 /* formatting dates */
86
87 - (NSString *)stringForTime:(NSCalendarDate *)_d prefix:(char *)_p {
88   /* Note: prefix is not allowed to be long! */
89   char buf[32];
90   
91   if (_p == NULL) _p = "";
92   sprintf(buf, "%s%02i:%02i", _p, [_d hourOfDay], [_d minuteOfHour]);
93   return [StrClass stringWithCString:buf];
94 }
95
96 - (NSString *)stringForCalendarDate:(NSCalendarDate *)_date {
97   char buf[32];
98   
99   if (self->now == nil) {
100     self->now = [[NSCalendarDate alloc] init];
101     [self->now setTimeZone:[self timeZone]];
102   }
103   [_date setTimeZone:[self timeZone]];
104   
105   if ([self showOnlyTimeForToday] && [_date isDateOnSameDay:self->now])
106     return [self stringForTime:_date prefix:NULL];
107   
108   if ([self showLabelsForNearDays]) {
109     NSString *label;
110     
111     if ([_date isDateOnSameDay:self->now])
112       label = [self labelForKey:@"today"];
113     else if ([_date isDateOnSameDay:[self->now yesterday]])
114       label = [self labelForKey:@"yesterday"];
115     else
116       label = nil;
117     
118     if (label != nil) {
119       return [label stringByAppendingString:
120                       [self stringForTime:_date prefix:", "]];
121     }
122   }
123   
124   /* 26.08.2004 13:24 */
125   sprintf(buf, "%02d.%02d.%04d %02d:%02d",
126           [_date dayOfMonth], [_date monthOfYear], [_date yearOfCommonEra],
127           [_date hourOfDay], [_date minuteOfHour]);
128   return [StrClass stringWithCString:buf];
129 }
130
131 /* formatter entry function */
132
133 - (NSString *)stringForObjectValue:(id)_date {
134   if (![_date isNotNull])
135     return nil;
136   
137   if ([_date isKindOfClass:StrClass]) /* already formatted */
138     return _date;
139   
140   if ([_date isKindOfClass:CalDateClass])
141     return [self stringForCalendarDate:_date];
142   
143   [self debugWithFormat:
144           @"NOTE: unexpected object for date formatter: %@<%@>",
145           _date, NSStringFromClass([_date class])];
146   return [_date description];
147 }
148
149 @end /* UIxMailDateFormatter */