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/SOGoUser.h>
32 #import <SOGo/NSCalendarDate+SOGo.h>
33 #import <SOGoUI/UIxComponent.h>
35 @interface SOGoUserHomePage : UIxComponent
39 @implementation SOGoUserHomePage
41 - (id <WOActionResults>) defaultAction
43 NSString *baseURL, *url;
45 baseURL = [[context request] uri];
46 url = [baseURL stringByAppendingString:@"/../Calendar"];
48 return [self redirectToLocation: url];
51 - (void) _fillFreeBusyItems: (NSMutableArray *) items
52 withRecords: (NSEnumerator *) records
53 fromStartDate: (NSCalendarDate *) startDate
54 toEndDate: (NSCalendarDate *) endDate
57 int count, startInterval, endInterval, value;
59 NSCalendarDate *currentDate;
61 record = [records nextObject];
64 status = [record objectForKey: @"status"];
66 value = [[record objectForKey: @"startdate"] intValue];
67 currentDate = [NSCalendarDate dateWithTimeIntervalSince1970: value];
68 if ([currentDate earlierDate: startDate] == currentDate)
72 = ([currentDate timeIntervalSinceDate: startDate] / 900);
74 value = [[record objectForKey: @"enddate"] intValue];
75 currentDate = [NSCalendarDate dateWithTimeIntervalSince1970: value];
76 if ([currentDate earlierDate: endDate] == endDate)
77 endInterval = [items count] - 1;
79 endInterval = ([currentDate timeIntervalSinceDate: startDate] / 900);
81 for (count = startInterval; count < endInterval; count++)
82 [items replaceObjectAtIndex: count withObject: status];
84 record = [records nextObject];
88 - (NSString *) _freeBusyAsTextFromStartDate: (NSCalendarDate *) startDate
89 toEndDate: (NSCalendarDate *) endDate
90 forFreeBusy: (SOGoFreeBusyObject *) fb
92 NSEnumerator *records;
93 NSMutableArray *freeBusyItems;
94 NSTimeInterval interval;
97 interval = [endDate timeIntervalSinceDate: startDate] + 60;
98 intervals = interval / 900; /* slices of 15 minutes */
99 freeBusyItems = [NSMutableArray arrayWithCapacity: intervals];
100 for (count = 1; count < intervals; count++)
101 [freeBusyItems addObject: @"0"];
103 records = [[fb fetchFreeBusyInfosFrom: startDate to: endDate] objectEnumerator];
104 [self _fillFreeBusyItems: freeBusyItems withRecords: records
105 fromStartDate: startDate toEndDate: endDate];
107 return [freeBusyItems componentsJoinedByString: @","];
110 - (NSString *) _freeBusyAsText
112 SOGoFreeBusyObject *co;
113 NSCalendarDate *startDate, *endDate;
114 NSString *queryDay, *additionalDays;
118 co = [self clientObject];
119 user = [context activeUser];
120 uTZ = [user timeZone];
122 queryDay = [self queryParameterForKey: @"sday"];
123 if ([queryDay length])
124 startDate = [NSCalendarDate dateFromShortDateString: queryDay
125 andShortTimeString: @"0000"
129 startDate = [NSCalendarDate calendarDate];
130 [startDate setTimeZone: uTZ];
131 startDate = [startDate hour: 0 minute: 0];
134 queryDay = [self queryParameterForKey: @"eday"];
135 if ([queryDay length])
136 endDate = [NSCalendarDate dateFromShortDateString: queryDay
137 andShortTimeString: @"2359"
140 endDate = [startDate hour: 23 minute: 59];
142 additionalDays = [self queryParameterForKey: @"additional"];
143 if ([additionalDays length] > 0)
144 endDate = [endDate dateByAddingYears: 0 months: 0
145 days: [additionalDays intValue]
146 hours: 0 minutes: 0 seconds: 0];
148 return [self _freeBusyAsTextFromStartDate: startDate toEndDate: endDate
152 - (id <WOActionResults>) readFreeBusyAction
154 WOResponse *response;
156 response = [context response];
157 [response setStatus: 200];
158 [response setHeader: @"text/plain; charset=iso-8859-1"
159 forKey: @"Content-Type"];
160 [response appendContentString: [self _freeBusyAsText]];