]> err.no Git - scalable-opengroupware.org/blob - SOPE/NGCards/iCalTimeZone.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1178 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / SOPE / NGCards / iCalTimeZone.m
1 /* iCalTimeZone.m - this file is part of SOPE
2  *
3  * Copyright (C) 2006 Inverse groupe conseil
4  *
5  * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
6  *
7  * This file is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This file is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #import <Foundation/NSArray.h>
24 #import <Foundation/NSCalendarDate.h>
25 #import <Foundation/NSString.h>
26 #import <Foundation/NSTimeZone.h>
27
28 #import "NSCalendarDate+NGCards.h"
29 #import "NSString+NGCards.h"
30 #import "iCalTimeZonePeriod.h"
31
32 #import "iCalTimeZone.h"
33
34 @implementation iCalTimeZone
35
36 - (Class) classForTag: (NSString *) classTag
37 {
38   Class tagClass;
39
40   if ([classTag isEqualToString: @"STANDARD"]
41       || [classTag isEqualToString: @"DAYLIGHT"])
42     tagClass = [iCalTimeZonePeriod class];
43   else if ([classTag isEqualToString: @"TZID"])
44     tagClass = [CardElement class];
45   else
46     tagClass = [super classForTag: classTag];
47
48   return tagClass;
49 }
50
51 - (void) setTzId: (NSString *) tzId
52 {
53   [[self uniqueChildWithTag: @"tzid"] setValue: 0 to: tzId];
54 }
55
56 - (NSString *) tzId
57 {
58   return [[self uniqueChildWithTag: @"tzid"] value: 0];
59 }
60
61 - (NSCalendarDate *) _occurenceForPeriodNamed: (NSString *) pName
62                                       forDate: (NSCalendarDate *) aDate
63 {
64   NSArray *periods;
65   iCalTimeZonePeriod *period;
66   NSCalendarDate *occurence;
67
68   periods = [self childrenWithTag: pName];
69   if ([periods count])
70     {
71       period = (iCalTimeZonePeriod *) [periods objectAtIndex: 0];
72       occurence = [period occurenceForDate: aDate];
73     }
74   else
75     occurence = nil;
76
77   return occurence;
78 }
79
80 - (iCalTimeZonePeriod *) periodForDate: (NSCalendarDate *) date
81 {
82   NSCalendarDate *daylightOccurence, *standardOccurence;
83   iCalTimeZonePeriod *period;
84
85   /* FIXME, this could cause crashes when timezones are not properly
86      specified, but let's say it won't happen often... */
87
88   daylightOccurence = [self _occurenceForPeriodNamed: @"daylight"
89                             forDate: date];
90   standardOccurence = [self _occurenceForPeriodNamed: @"standard"
91                             forDate: date];
92   if ([date earlierDate: daylightOccurence] == date
93       || [date earlierDate: standardOccurence] == standardOccurence)
94     period = (iCalTimeZonePeriod *) [self uniqueChildWithTag: @"standard"];
95   else
96     period = (iCalTimeZonePeriod *) [self uniqueChildWithTag: @"daylight"];
97
98   NSLog (@"chosen period: '%@'", [period tag]);
99
100   return period;
101 }
102
103 - (NSCalendarDate *) _computedDateTimeForDate: (NSCalendarDate *) date
104 {
105   NSCalendarDate *tmpDate;
106   NSTimeZone *utc;
107
108   utc = [NSTimeZone timeZoneWithName: @"GMT"];
109   tmpDate = [date copy];
110   [tmpDate autorelease];
111   [tmpDate setTimeZone: utc];
112
113   return [tmpDate addYear: 0 month: 0 day: 0
114                   hour: 0 minute: 0
115                   second: [[self periodForDate: date] secondsOffsetFromGMT]];
116 }
117
118 - (NSString *) dateTimeStringForDate: (NSCalendarDate *) date
119 {
120   return [[self _computedDateTimeForDate: date]
121            iCalFormattedDateTimeString];
122 }
123
124 - (NSString *) dateStringForDate: (NSCalendarDate *) date
125 {
126   return [[self _computedDateTimeForDate: date]
127            iCalFormattedDateString];
128 }
129
130 - (NSCalendarDate *) dateForDateTimeString: (NSString *) string
131 {
132   NSCalendarDate *tmpDate;
133   iCalTimeZonePeriod *period;
134
135   tmpDate = [string asCalendarDate];
136   period = [self periodForDate: tmpDate];
137
138   return [tmpDate addYear: 0 month: 0 day: 0
139                   hour: 0 minute: 0
140                   second: -[period secondsOffsetFromGMT]];
141 }
142
143 @end