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/NSValue.h>
26 #import <NGObjWeb/WORequest.h>
27 #import <NGObjWeb/WOResponse.h>
28 #import <NGExtensions/NSCalendarDate+misc.h>
30 #import <Appointments/SOGoFreeBusyObject.h>
31 #import <SOGo/NSCalendarDate+SOGo.h>
32 #import <SOGoUI/UIxComponent.h>
34 @interface SOGoUserHomePage : UIxComponent
38 @implementation SOGoUserHomePage
40 - (id <WOActionResults>) defaultAction
42 NSString *baseURL, *url;
44 baseURL = [[context request] uri];
45 url = [baseURL stringByAppendingString:@"/../Calendar"];
47 return [self redirectToLocation: url];
50 - (void) _fillFreeBusyItems: (NSMutableArray *) items
51 withRecords: (NSEnumerator *) records
52 fromStartDate: (NSCalendarDate *) startDate
53 toEndDate: (NSCalendarDate *) endDate
56 int count, startInterval, endInterval, value;
58 NSCalendarDate *currentDate;
60 record = [records nextObject];
63 status = [record objectForKey: @"status"];
65 value = [[record objectForKey: @"startdate"] intValue];
66 currentDate = [NSCalendarDate dateWithTimeIntervalSince1970: value];
67 if ([currentDate earlierDate: startDate] == currentDate)
71 = ([currentDate timeIntervalSinceDate: startDate] / 900);
73 value = [[record objectForKey: @"enddate"] intValue];
74 currentDate = [NSCalendarDate dateWithTimeIntervalSince1970: value];
75 if ([currentDate earlierDate: endDate] == endDate)
76 endInterval = [items count] - 1;
78 endInterval = ([currentDate timeIntervalSinceDate: startDate] / 900);
80 for (count = startInterval; count < endInterval; count++)
81 [items replaceObjectAtIndex: count withObject: status];
83 record = [records nextObject];
87 - (NSString *) _freeBusyAsTextFromStartDate: (NSCalendarDate *) startDate
88 toEndDate: (NSCalendarDate *) endDate
89 forFreeBusy: (SOGoFreeBusyObject *) fb
91 NSEnumerator *records;
92 NSMutableArray *freeBusyItems;
93 NSTimeInterval interval;
96 interval = [endDate timeIntervalSinceDate: startDate] + 60;
97 intervals = interval / 900; /* slices of 15 minutes */
98 freeBusyItems = [NSMutableArray arrayWithCapacity: intervals];
99 for (count = 1; count < intervals; count++)
100 [freeBusyItems addObject: @"0"];
102 records = [[fb fetchFreeBusyInfosFrom: startDate to: endDate] objectEnumerator];
103 [self _fillFreeBusyItems: freeBusyItems withRecords: records
104 fromStartDate: startDate toEndDate: endDate];
106 return [freeBusyItems componentsJoinedByString: @","];
109 - (NSString *) _freeBusyAsText
111 SOGoFreeBusyObject *co;
112 NSCalendarDate *startDate, *endDate;
113 NSString *queryDay, *additionalDays;
116 co = [self clientObject];
117 uTZ = [co userTimeZone];
119 queryDay = [self queryParameterForKey: @"sday"];
120 if ([queryDay length])
121 startDate = [NSCalendarDate dateFromShortDateString: queryDay
122 andShortTimeString: @"0000"
126 startDate = [NSCalendarDate calendarDate];
127 [startDate setTimeZone: uTZ];
128 startDate = [startDate hour: 0 minute: 0];
131 queryDay = [self queryParameterForKey: @"eday"];
132 if ([queryDay length])
133 endDate = [NSCalendarDate dateFromShortDateString: queryDay
134 andShortTimeString: @"2359"
137 endDate = [startDate hour: 23 minute: 59];
139 additionalDays = [self queryParameterForKey: @"additional"];
140 if ([additionalDays length] > 0)
141 endDate = [endDate dateByAddingYears: 0 months: 0
142 days: [additionalDays intValue]
143 hours: 0 minutes: 0 seconds: 0];
145 return [self _freeBusyAsTextFromStartDate: startDate toEndDate: endDate
149 - (id <WOActionResults>) readFreeBusyAction
151 WOResponse *response;
153 response = [context response];
154 [response setStatus: 200];
155 [response setHeader: @"text/plain; charset=iso-8859-1"
156 forKey: @"Content-Type"];
157 [response appendContentString: [self _freeBusyAsText]];