]> err.no Git - scalable-opengroupware.org/blob - SoObjects/Appointments/SOGoFreeBusyObject.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1011 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 // $Id$
22
23 #import <NGCards/iCalCalendar.h>
24 #import <NGCards/iCalFreeBusy.h>
25
26 #import "common.h"
27
28 #import <SOGo/AgenorUserManager.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   
51   today = [[NSCalendarDate calendarDate] beginOfDay];
52   [today setTimeZone: [self userTimeZone]];
53
54   startDate = [today dateByAddingYears: 0 months: 0 days: -14
55                      hours: 0 minutes: 0 seconds: 0];
56   endDate = [startDate dateByAddingYears: 0 months: 1 days: 0
57                        hours: 0 minutes: 0 seconds: 0];
58   return [self contentAsStringFrom: startDate to: endDate];
59 }
60
61 - (NSString *) contentAsStringFrom: (NSCalendarDate *) _startDate
62                                 to: (NSCalendarDate *) _endDate
63 {
64   NSArray *infos;
65   
66   infos = [self fetchFreeBusyInfosFrom:_startDate to:_endDate];
67   return [self iCalStringForFreeBusyInfos:infos from:_startDate to:_endDate];
68 }
69
70 - (NSArray *) fetchFreeBusyInfosFrom: (NSCalendarDate *) _startDate
71                                   to: (NSCalendarDate *) _endDate
72 {
73   id calFolder;
74   SoSecurityManager *sm;
75   WOApplication *woApp;
76   NSArray *infos;
77
78   woApp = [WOApplication application];
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: [woApp 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 - (NSString *) iCalStringForFreeBusyInfos: (NSArray *) _infos
114                                      from: (NSCalendarDate *) _startDate
115                                        to: (NSCalendarDate *) _endDate
116 {
117   AgenorUserManager *um;
118   NSString *uid;
119   NSEnumerator *events;
120   iCalCalendar *calendar;
121   iCalFreeBusy *freebusy;
122   NSDictionary *info;
123   iCalFreeBusyType type;
124
125   um  = [AgenorUserManager sharedUserManager];
126   uid = [[self container] login];
127
128   calendar = [iCalCalendar groupWithTag: @"vcalendar"];
129   [calendar setProdID: @"//Inverse groupe conseil/SOGo 0.9"];
130   [calendar setVersion: @"2.0"];
131
132   freebusy = [iCalFreeBusy groupWithTag: @"vfreebusy"];
133   [freebusy addToAttendees: [um iCalPersonWithUid: uid]];
134   [freebusy setTimeStampAsDate: [NSCalendarDate calendarDate]];
135   [freebusy setStartDate: _startDate];
136   [freebusy setEndDate: _endDate];
137
138   /* ORGANIZER - strictly required but missing for now */
139
140   /* ATTENDEE */
141 //   person = [um iCalPersonWithUid: uid];
142 //   [person setTag: @"ATTENDEE"];
143 //   [ms appendString: [person versitString]];
144
145   /* FREEBUSY */
146   events = [_infos objectEnumerator];
147   info = [events nextObject];
148   while (info)
149     {
150       if ([[info objectForKey: @"isopaque"] boolValue])
151         {
152           type = [self _fbTypeForEventStatus: [info objectForKey: @"status"]];
153           [freebusy addFreeBusyFrom: [info objectForKey: @"startDate"]
154                     to: [info objectForKey: @"endDate"]
155                     type: type];
156         }
157       info = [events nextObject];
158     }
159
160   [calendar setUniqueChild: freebusy];
161
162   return [calendar versitString];
163 }
164
165 /* deliver content without need for view method */
166
167 - (id) GETAction: (id)_ctx
168 {
169   WOResponse *r;
170   NSData     *contentData;
171
172   contentData = [[self contentAsString] dataUsingEncoding: NSUTF8StringEncoding];
173
174   r = [(WOContext *) _ctx response];
175   [r setHeader: @"text/calendar" forKey: @"content-type"];
176   [r setContent: contentData];
177   [r setStatus: 200];
178
179   return r;
180 }
181
182 @end