1 /* SOGoUserHomePage.m - this file is part of SOGo
3 * Copyright (C) 2007 Inverse groupe conseil
5 * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
7 * This file is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This file is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #import <Foundation/NSArray.h>
24 #import <Foundation/NSCalendarDate.h>
25 #import <Foundation/NSDictionary.h>
26 #import <Foundation/NSValue.h>
27 #import <NGObjWeb/WORequest.h>
28 #import <NGObjWeb/WOResponse.h>
29 #import <NGExtensions/NSCalendarDate+misc.h>
31 #import <Appointments/SOGoFreeBusyObject.h>
32 #import <SOGo/SOGoUser.h>
33 #import <SOGo/NSCalendarDate+SOGo.h>
34 #import <SOGoUI/UIxComponent.h>
36 @interface SOGoUserHomePage : UIxComponent
40 @implementation SOGoUserHomePage
42 - (id <WOActionResults>) defaultAction
44 NSString *baseURL, *url;
46 baseURL = [[context request] uri];
47 url = [baseURL stringByAppendingString:@"/../Calendar"];
49 return [self redirectToLocation: url];
52 - (void) _fillFreeBusyItems: (NSMutableArray *) items
53 withRecords: (NSEnumerator *) records
54 fromStartDate: (NSCalendarDate *) startDate
55 toEndDate: (NSCalendarDate *) endDate
58 int count, startInterval, endInterval, value;
60 NSCalendarDate *currentDate;
62 record = [records nextObject];
65 status = [record objectForKey: @"status"];
67 value = [[record objectForKey: @"startdate"] intValue];
68 currentDate = [NSCalendarDate dateWithTimeIntervalSince1970: value];
69 if ([currentDate earlierDate: startDate] == currentDate)
73 = ([currentDate timeIntervalSinceDate: startDate] / 900);
75 value = [[record objectForKey: @"enddate"] intValue];
76 currentDate = [NSCalendarDate dateWithTimeIntervalSince1970: value];
77 if ([currentDate earlierDate: endDate] == endDate)
78 endInterval = [items count] - 1;
80 endInterval = ([currentDate timeIntervalSinceDate: startDate] / 900);
82 for (count = startInterval; count < endInterval; count++)
83 [items replaceObjectAtIndex: count withObject: status];
85 record = [records nextObject];
89 - (NSString *) _freeBusyAsTextFromStartDate: (NSCalendarDate *) startDate
90 toEndDate: (NSCalendarDate *) endDate
91 forFreeBusy: (SOGoFreeBusyObject *) fb
93 NSEnumerator *records;
94 NSMutableArray *freeBusyItems;
95 NSTimeInterval interval;
98 interval = [endDate timeIntervalSinceDate: startDate] + 60;
99 intervals = interval / 900; /* slices of 15 minutes */
100 freeBusyItems = [NSMutableArray arrayWithCapacity: intervals];
101 for (count = 1; count < intervals; count++)
102 [freeBusyItems addObject: @"0"];
104 records = [[fb fetchFreeBusyInfosFrom: startDate to: endDate] objectEnumerator];
105 [self _fillFreeBusyItems: freeBusyItems withRecords: records
106 fromStartDate: startDate toEndDate: endDate];
108 return [freeBusyItems componentsJoinedByString: @","];
111 - (NSString *) _freeBusyAsText
113 SOGoFreeBusyObject *co;
114 NSCalendarDate *startDate, *endDate;
115 NSString *queryDay, *additionalDays;
119 co = [self clientObject];
120 user = [context activeUser];
121 uTZ = [user timeZone];
123 queryDay = [self queryParameterForKey: @"sday"];
124 if ([queryDay length])
125 startDate = [NSCalendarDate dateFromShortDateString: queryDay
126 andShortTimeString: @"0000"
130 startDate = [NSCalendarDate calendarDate];
131 [startDate setTimeZone: uTZ];
132 startDate = [startDate hour: 0 minute: 0];
135 queryDay = [self queryParameterForKey: @"eday"];
136 if ([queryDay length])
137 endDate = [NSCalendarDate dateFromShortDateString: queryDay
138 andShortTimeString: @"2359"
141 endDate = [startDate hour: 23 minute: 59];
143 additionalDays = [self queryParameterForKey: @"additional"];
144 if ([additionalDays length] > 0)
145 endDate = [endDate dateByAddingYears: 0 months: 0
146 days: [additionalDays intValue]
147 hours: 0 minutes: 0 seconds: 0];
149 return [self _freeBusyAsTextFromStartDate: startDate toEndDate: endDate
153 - (id <WOActionResults>) readFreeBusyAction
155 WOResponse *response;
157 response = [context response];
158 [response setStatus: 200];
159 // [response setHeader: @"text/plain; charset=iso-8859-1"
160 // forKey: @"Content-Type"];
161 [response appendContentString: [self _freeBusyAsText]];