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