]> err.no Git - sope/blob - sope-ical/iCalSaxDriver/NSCalendarDate+ICal.m
fixed sope-ical for SOPE 3.3 layout
[sope] / sope-ical / iCalSaxDriver / NSCalendarDate+ICal.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
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 "NSCalendarDate+ICal.h"
24 #include "common.h"
25 #include <ical.h>
26
27 static NSTimeZone *gmt = nil;
28 static inline void _setupGMT(void) {
29   if (gmt == nil)
30     gmt = [[NSTimeZone timeZoneWithAbbreviation:@"GMT"] retain];
31 }
32
33 @implementation NSCalendarDate(ICalValue)
34
35 - (NSTimeZone *)timeZoneFromICalTime:(struct icaltimetype *)_dt
36   defaultTimeZone:(NSTimeZone *)_tz
37 {
38   _setupGMT();
39
40   if (_dt->is_utc)
41     return gmt;
42
43   if (_tz)
44     return _tz;
45   
46   NSLog(@"WARNING(%s): using localtimezone !", __PRETTY_FUNCTION__);
47   return [NSTimeZone localTimeZone];
48 }
49
50 - (id)initWithICalDate:(struct icaltimetype)_dt timeZone:(NSTimeZone *)_tz {
51   NSTimeZone *tz;
52   
53   tz = [self timeZoneFromICalTime:&_dt defaultTimeZone:_tz];
54   
55   self = [self initWithYear:_dt.year month:_dt.month day:_dt.day
56                hour:12 minute:0 second:0
57                timeZone:_tz];
58   return self;
59 }
60
61 - (id)initWithICalTime:(struct icaltimetype)_dt timeZone:(NSTimeZone *)_tz {
62   NSTimeZone *tz;
63   
64   if (_dt.is_date)
65     return [self initWithICalDate:_dt timeZone:_tz];
66   
67   tz = [self timeZoneFromICalTime:&_dt defaultTimeZone:_tz];
68   
69   
70   self = [self initWithYear:_dt.year month:_dt.month day:_dt.day
71                hour:_dt.hour minute:_dt.minute second:_dt.second
72                timeZone:tz];
73   return self;
74 }
75
76 - (id)initWithICalDate:(struct icaltimetype)_dt {
77   _setupGMT();
78   return [self initWithICalDate:_dt timeZone:gmt];
79 }
80 - (id)initWithICalTime:(struct icaltimetype)_dt {
81   _setupGMT();
82   return [self initWithICalTime:_dt timeZone:gmt];
83 }
84
85 - (id)initWithICalValueHandle:(icalvalue *)_handle {
86   if (_handle == NULL) {
87     RELEASE(self);
88     return nil;
89   }
90   return [self initWithICalTime:icalvalue_get_datetime(_handle)];
91 }
92 - (id)initWithICalValueOfProperty:(icalproperty *)_prop {
93   icalvalue *val;
94   
95   if (_prop == NULL) {
96     RELEASE(self);
97     return nil;
98   }
99
100   if ((val = icalproperty_get_value(_prop)) == NULL) {
101     NSLog(@"%s: ical property has no value ??", __PRETTY_FUNCTION__);
102     RELEASE(self);
103     return nil;
104   }
105   
106   return [self initWithICalValueHandle:val];
107 }
108
109 /* durations */
110
111 - (NSCalendarDate *)dateByApplyingICalDuration:(struct icaldurationtype)_dur {
112   if (_dur.is_neg) {
113     return [self dateByAddingYears:0 months:0
114                  days:-(_dur.days + (_dur.weeks * 7))
115                  hours:-(_dur.hours)
116                  minutes:-(_dur.minutes)
117                  seconds:-(_dur.seconds)];
118   }
119   else {
120     return [self dateByAddingYears:0 months:0
121                  days:(_dur.days + (_dur.weeks * 7))
122                  hours:_dur.hours minutes:_dur.minutes seconds:_dur.seconds];
123   }
124 }
125
126 /* represention */
127
128 static NSString *gmtcalfmt = @"%Y%m%dT%H%M00Z";
129
130 - (NSString *)icalStringInGMT {
131   NSTimeZone *oldtz;
132   NSString   *s;
133   _setupGMT();
134   
135   /* set GMT as timezone */
136   oldtz = [[self timeZone] retain];
137   if (oldtz == gmt) {
138     [oldtz release];
139     oldtz = nil;
140   }
141   else {
142     [self setTimeZone:gmt];
143   }
144   
145   /* calc string */
146   s = [self descriptionWithCalendarFormat:gmtcalfmt];
147   
148   /* restore old timezone */
149   if (oldtz) {
150     [self setTimeZone:oldtz];
151     [oldtz release];
152   }
153   
154   return s;
155 }
156
157 - (NSString *)icalStringWithTimeZone:(NSTimeZone *)_tz {
158   _setupGMT();
159   
160   if (_tz == gmt || _tz == nil)
161     return [self icalStringInGMT];
162   else if ([_tz isEqual:gmt])
163     return [self icalStringInGMT];
164   else {
165     /* not in GMT */
166     NSLog(@"WARNING(%s): arbitary timezones not supported yet: %@",
167           __PRETTY_FUNCTION__, _tz);
168     return [self icalStringInGMT];
169   }
170 }
171
172 - (NSString *)icalString {
173   _setupGMT();
174   return [self icalStringWithTimeZone:gmt];
175 }
176
177 @end /* NSDate(ICalValue) */