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