]> err.no Git - scalable-opengroupware.org/blob - UI/Scheduler/UIxCalDateLabel.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1263 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / Scheduler / UIxCalDateLabel.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
24 #import <NGCards/NSCalendarDate+ICal.h>
25 #import <NGExtensions/NSCalendarDate+misc.h>
26
27 #import <SOGoUI/UIxComponent.h>
28
29 @interface UIxCalDateLabel : UIxComponent
30 {
31   NSString       *selection;
32   NSCalendarDate *startDate;
33   NSCalendarDate *endDate;
34 }
35
36 - (NSString *)dayLabel;
37 - (NSString *)weekLabel;
38 - (NSString *)monthLabel;
39 - (NSString *)yearLabel;
40
41 @end
42
43 @implementation UIxCalDateLabel
44
45 - (void)dealloc {
46   [self->selection release];
47   [self->startDate release];
48   [self->endDate   release];
49   [super dealloc];
50 }
51
52 /* accessors */
53
54 - (void)setSelection:(NSString *)_selection {
55   ASSIGNCOPY(self->selection, _selection);
56 }
57 - (NSString *)selection {
58   return self->selection;
59 }
60
61 - (void)setStartDate:(NSCalendarDate *)_date {
62   ASSIGN(self->startDate, _date);
63 }
64 - (NSCalendarDate *)startDate {
65   return self->startDate;
66 }
67
68 - (void)setEndDate:(NSCalendarDate *)_date {
69   ASSIGN(self->endDate, _date);
70 }
71 - (NSCalendarDate *)endDate {
72   return self->endDate;
73 }
74
75 - (NSString *)label {
76   NSString *key = [self selection];
77   
78   if([key isEqualToString:@"day"])
79     return [self dayLabel];
80   if ([key isEqualToString:@"week"])
81     return [self weekLabel];
82   if ([key isEqualToString:@"month"])
83     return [self monthLabel];
84   return [self yearLabel];
85 }
86
87 - (NSString *)dayLabel {
88   NSString *fmt;
89   
90   fmt = [self labelForKey:@"dayLabelFormat"];
91   return [self->startDate descriptionWithCalendarFormat:fmt];
92 }
93
94 - (NSString *)weekLabel {
95   NSString *label, *le;
96   
97   if ([self->startDate monthOfYear] == [self->endDate monthOfYear])
98     label = [NSString stringWithFormat:@"%@ %d",
99                       [self localizedNameForMonthOfYear: [self->startDate monthOfYear]],
100                       [self->startDate yearOfCommonEra]];
101   else
102     {
103       le = [self localizedNameForMonthOfYear:[self->endDate monthOfYear]];
104       label = [NSString stringWithFormat:@"<nobr>%@ / %@ %d</nobr>",
105                         label, le,
106                         [self->endDate yearOfCommonEra]];
107     }
108
109   return label;
110 }
111
112 - (NSString *)monthLabel {
113   NSString *l;
114   unsigned diff;
115
116   diff = [self->startDate monthsBetweenDate:self->endDate];
117   if (diff == 0) {
118     l = [self localizedNameForMonthOfYear:[self->startDate monthOfYear]];
119   }
120   else {
121     NSCalendarDate *date;
122
123     date = [self->startDate dateByAddingYears:0 months:0 days:15];
124     l    = [self localizedNameForMonthOfYear:[date monthOfYear]];
125   }
126   return [NSString stringWithFormat:@"%@ %d",
127                    l, [self->startDate yearOfCommonEra]];
128 }
129
130 - (NSString *)yearLabel {
131   return [NSString stringWithFormat:@"%d",
132                    [self->startDate yearOfCommonEra]]; 
133 }
134
135 @end /* UIxCalDateLabel */