]> err.no Git - scalable-opengroupware.org/blob - SoObjects/Appointments/SOGoFreeBusyObject.m
ae378a123822c5af27c8d58eb08bafb31b649d49
[scalable-opengroupware.org] / SoObjects / Appointments / SOGoFreeBusyObject.m
1 /*
2   Copyright (C) 2000-2004 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
22 #import <NGCards/iCalCalendar.h>
23 #import <NGCards/iCalFreeBusy.h>
24 #import <NGCards/iCalPerson.h>
25
26 #import "common.h"
27
28 #import <SOGo/LDAPUserManager.h>
29 #import <SOGo/SOGoPermissions.h>
30
31 #import "SOGoFreeBusyObject.h"
32
33 @interface SOGoFreeBusyObject (PrivateAPI)
34 - (NSString *) iCalStringForFreeBusyInfos: (NSArray *) _infos
35                                      from: (NSCalendarDate *) _startDate
36                                        to: (NSCalendarDate *) _endDate;
37 @end
38
39 @implementation SOGoFreeBusyObject
40
41 - (NSString *) iCalString
42 {
43   // for UI-X appointment viewer
44   return [self contentAsString];
45 }
46
47 - (NSString *) contentAsString
48 {
49   NSCalendarDate *today, *startDate, *endDate;
50   NSTimeZone *timeZone;
51   
52   today = [[NSCalendarDate calendarDate] beginOfDay];
53   timeZone = [[context activeUser] timeZone];
54   [today setTimeZone: timeZone];
55
56   startDate = [today dateByAddingYears: 0 months: 0 days: -14
57                      hours: 0 minutes: 0 seconds: 0];
58   endDate = [startDate dateByAddingYears: 0 months: 1 days: 0
59                        hours: 0 minutes: 0 seconds: 0];
60
61   return [self contentAsStringFrom: startDate to: endDate];
62 }
63
64 - (NSString *) contentAsStringFrom: (NSCalendarDate *) _startDate
65                                 to: (NSCalendarDate *) _endDate
66 {
67   NSArray *infos;
68   
69   infos = [self fetchFreeBusyInfosFrom:_startDate to:_endDate];
70   return [self iCalStringForFreeBusyInfos:infos from:_startDate to:_endDate];
71 }
72
73 - (NSArray *) fetchFreeBusyInfosFrom: (NSCalendarDate *) _startDate
74                                   to: (NSCalendarDate *) _endDate
75 {
76   id calFolder;
77 //   SoSecurityManager *sm;
78   NSArray *infos;
79
80   calFolder = [container lookupName: @"Calendar" inContext: nil acquire: NO];
81 //   sm = [SoSecurityManager sharedSecurityManager];
82 //   if (![sm validatePermission: SOGoPerm_FreeBusyLookup
83 //            onObject: calFolder
84 //            inContext: context])
85     infos = [calFolder fetchFreeBusyInfosFrom: _startDate
86                        to: _endDate];
87 //   else
88 //     {
89 //       infos = [NSArray new];
90 //       [infos autorelease];
91 //     }
92
93   return infos;
94 }
95
96 /* Private API */
97 - (iCalFreeBusyType) _fbTypeForEventStatus: (NSNumber *) eventStatus
98 {
99   unsigned int status;
100   iCalFreeBusyType fbType;
101
102   status = [eventStatus unsignedIntValue];
103   if (status == 0)
104     fbType = iCalFBBusyTentative;
105   else if (status == 1)
106     fbType = iCalFBBusy;
107   else
108     fbType = iCalFBFree;
109
110   return fbType;    
111 }
112
113 - (iCalPerson *) iCalPersonWithUID: (NSString *) uid
114 {
115   iCalPerson *person;
116   LDAPUserManager *um;
117   NSDictionary *contactInfos;
118
119   um = [LDAPUserManager sharedUserManager];
120   contactInfos = [um contactInfosForUserWithUIDorEmail: uid];
121
122   person = [iCalPerson new];
123   [person autorelease];
124   [person setCn: [contactInfos objectForKey: @"cn"]];
125   [person setEmail: [contactInfos objectForKey: @"c_email"]];
126
127   return person;
128 }
129
130 - (NSString *) iCalStringForFreeBusyInfos: (NSArray *) _infos
131                                      from: (NSCalendarDate *) _startDate
132                                        to: (NSCalendarDate *) _endDate
133 {
134   NSString *uid;
135   NSEnumerator *events;
136   iCalCalendar *calendar;
137   iCalFreeBusy *freebusy;
138   NSDictionary *info;
139   iCalFreeBusyType type;
140
141   uid = [[self container] login];
142
143   calendar = [iCalCalendar groupWithTag: @"vcalendar"];
144   [calendar setProdID: @"//Inverse groupe conseil/SOGo 0.9"];
145   [calendar setVersion: @"2.0"];
146
147   freebusy = [iCalFreeBusy groupWithTag: @"vfreebusy"];
148   [freebusy addToAttendees: [self iCalPersonWithUID: uid]];
149   [freebusy setTimeStampAsDate: [NSCalendarDate calendarDate]];
150   [freebusy setStartDate: _startDate];
151   [freebusy setEndDate: _endDate];
152
153   /* ORGANIZER - strictly required but missing for now */
154
155   /* ATTENDEE */
156 //   person = [self iCalPersonWithUid: uid];
157 //   [person setTag: @"ATTENDEE"];
158 //   [ms appendString: [person versitString]];
159
160   /* FREEBUSY */
161   events = [_infos objectEnumerator];
162   info = [events nextObject];
163   while (info)
164     {
165       if ([[info objectForKey: @"isopaque"] boolValue])
166         {
167           type = [self _fbTypeForEventStatus: [info objectForKey: @"status"]];
168           [freebusy addFreeBusyFrom: [info objectForKey: @"startDate"]
169                     to: [info objectForKey: @"endDate"]
170                     type: type];
171         }
172       info = [events nextObject];
173     }
174
175   [calendar setUniqueChild: freebusy];
176
177   return [calendar versitString];
178 }
179
180 /* deliver content without need for view method */
181
182 - (id) GETAction: (id)_ctx
183 {
184   WOResponse *r;
185   NSData     *contentData;
186
187   contentData = [[self contentAsString] dataUsingEncoding: NSUTF8StringEncoding];
188
189   r = [(WOContext *) _ctx response];
190   [r setHeader: @"text/calendar" forKey: @"content-type"];
191   [r setContent: contentData];
192   [r setStatus: 200];
193
194   return r;
195 }
196
197 @end