]> err.no Git - scalable-opengroupware.org/blob - SoObjects/Appointments/SOGoFreeBusyObject.m
ff9bc72ee0382bfaa30d419fbc32093596d72904
[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 *startDate, *endDate;
50   
51   startDate = [[[NSCalendarDate calendarDate] mondayOfWeek] beginOfDay];
52   endDate   = [startDate dateByAddingYears: 0 months: 0 days: 7
53                                      hours: 23 minutes: 59 seconds: 59];
54   return [self contentAsStringFrom: startDate to: endDate];
55 }
56
57 - (NSString *) contentAsStringFrom: (NSCalendarDate *) _startDate
58                                 to: (NSCalendarDate *) _endDate
59 {
60   NSArray *infos;
61   
62   infos = [self fetchFreeBusyInfosFrom:_startDate to:_endDate];
63   return [self iCalStringForFreeBusyInfos:infos from:_startDate to:_endDate];
64 }
65
66 - (NSArray *) fetchFreeBusyInfosFrom: (NSCalendarDate *) _startDate
67                                   to: (NSCalendarDate *) _endDate
68 {
69   id calFolder;
70   SoSecurityManager *sm;
71   WOApplication *woApp;
72   NSArray *infos;
73
74   woApp = [WOApplication application];
75
76   calFolder = [container lookupName: @"Calendar" inContext: nil acquire: NO];
77   sm = [SoSecurityManager sharedSecurityManager];
78   if (![sm validatePermission: SOGoPerm_FreeBusyLookup
79            onObject: calFolder
80            inContext: [woApp context]])
81     infos = [calFolder fetchFreeBusyInfosFrom: _startDate
82                        to: _endDate];
83   else
84     {
85       infos = [NSArray new];
86       [infos autorelease];
87     }
88
89   return infos;
90 }
91
92 /* Private API */
93 - (iCalFreeBusyType) _fbTypeForEventStatus: (NSNumber *) eventStatus
94 {
95   unsigned int status;
96   iCalFreeBusyType fbType;
97
98   status = [eventStatus unsignedIntValue];
99   if (status == 0)
100     fbType = iCalFBBusyTentative;
101   else if (status == 1)
102     fbType = iCalFBBusy;
103   else
104     fbType = iCalFBFree;
105
106   return fbType;    
107 }
108
109 - (NSString *) iCalStringForFreeBusyInfos: (NSArray *) _infos
110                                      from: (NSCalendarDate *) _startDate
111                                        to: (NSCalendarDate *) _endDate
112 {
113   AgenorUserManager *um;
114   NSString *uid;
115   NSEnumerator *events;
116   iCalCalendar *calendar;
117   iCalFreeBusy *freebusy;
118   NSDictionary *info;
119   iCalFreeBusyType type;
120
121   um  = [AgenorUserManager sharedUserManager];
122   uid = [[self container] login];
123
124   calendar = [iCalCalendar groupWithTag: @"vcalendar"];
125   [calendar setProdID: @"//Inverse groupe conseil/SOGo 0.9"];
126   [calendar setVersion: @"2.0"];
127
128   freebusy = [iCalFreeBusy groupWithTag: @"vfreebusy"];
129   [freebusy addToAttendees: [um iCalPersonWithUid: uid]];
130   [freebusy setTimeStampAsDate: [NSCalendarDate calendarDate]];
131   [freebusy setStartDate: _startDate];
132   [freebusy setEndDate: _endDate];
133
134   /* ORGANIZER - strictly required but missing for now */
135
136   /* ATTENDEE */
137 //   person = [um iCalPersonWithUid: uid];
138 //   [person setTag: @"ATTENDEE"];
139 //   [ms appendString: [person versitString]];
140
141   /* FREEBUSY */
142   events = [_infos objectEnumerator];
143   info = [events nextObject];
144   while (info)
145     {
146       if ([[info objectForKey: @"isopaque"] boolValue])
147         {
148           type = [self _fbTypeForEventStatus: [info objectForKey: @"status"]];
149           [freebusy addFreeBusyFrom: [info objectForKey: @"startDate"]
150                     to: [info objectForKey: @"endDate"]
151                     type: type];
152         }
153       info = [events nextObject];
154     }
155
156   [calendar setUniqueChild: freebusy];
157
158   return [calendar versitString];
159 }
160
161 /* deliver content without need for view method */
162
163 - (id) GETAction: (id)_ctx
164 {
165   WOResponse *r;
166   NSData     *contentData;
167
168   contentData = [[self contentAsString] dataUsingEncoding: NSUTF8StringEncoding];
169
170   r = [(WOContext *) _ctx response];
171   [r setHeader: @"text/calendar" forKey: @"content-type"];
172   [r setContent: contentData];
173   [r setStatus: 200];
174
175   return r;
176 }
177
178 @end