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/NSURL.h>
27 #import <Foundation/NSUserDefaults.h>
28 #import <Foundation/NSValue.h>
29 #import <NGObjWeb/WOCookie.h>
30 #import <NGObjWeb/WORequest.h>
31 #import <NGObjWeb/WOResponse.h>
32 #import <NGExtensions/NSCalendarDate+misc.h>
33 #import <NGExtensions/NSObject+Logs.h>
35 #import <Appointments/SOGoFreeBusyObject.h>
36 #import <SoObjects/SOGo/SOGoWebAuthenticator.h>
37 #import <SoObjects/SOGo/SOGoUser.h>
38 #import <SoObjects/SOGo/NSCalendarDate+SOGo.h>
39 #import <SOGoUI/UIxComponent.h>
41 static NSString *defaultModule = nil;
43 @interface SOGoUserHomePage : UIxComponent
47 @implementation SOGoUserHomePage
55 ud = [NSUserDefaults standardUserDefaults];
56 defaultModule = [ud stringForKey: @"SOGoUIxDefaultModule"];
59 if (!([defaultModule isEqualToString: @"Calendar"]
60 || [defaultModule isEqualToString: @"Contacts"]
61 || [defaultModule isEqualToString: @"Mail"]))
63 [self logWithFormat: @"default module '%@' not accepted (must be"
64 @"'Calendar', 'Contacts' or Mail)", defaultModule];
65 defaultModule = @"Calendar";
69 defaultModule = @"Calendar";
70 [self logWithFormat: @"default module set to '%@'", defaultModule];
71 [defaultModule retain];
75 - (id <WOActionResults>) defaultAction
80 co = [self clientObject];
81 moduleURL = [NSURL URLWithString: defaultModule
82 relativeToURL: [co soURL]];
84 return [self redirectToLocation: [moduleURL absoluteString]];
87 - (void) _fillFreeBusyItems: (NSMutableArray *) items
88 withRecords: (NSEnumerator *) records
89 fromStartDate: (NSCalendarDate *) startDate
90 toEndDate: (NSCalendarDate *) endDate
93 int count, startInterval, endInterval, value;
95 NSCalendarDate *currentDate;
97 record = [records nextObject];
100 status = [record objectForKey: @"c_status"];
102 value = [[record objectForKey: @"c_startdate"] intValue];
103 currentDate = [NSCalendarDate dateWithTimeIntervalSince1970: value];
104 if ([currentDate earlierDate: startDate] == currentDate)
108 = ([currentDate timeIntervalSinceDate: startDate] / 900);
110 value = [[record objectForKey: @"c_enddate"] intValue];
111 currentDate = [NSCalendarDate dateWithTimeIntervalSince1970: value];
112 if ([currentDate earlierDate: endDate] == endDate)
113 endInterval = [items count] - 1;
115 endInterval = ([currentDate timeIntervalSinceDate: startDate] / 900);
117 for (count = startInterval; count < endInterval; count++)
118 [items replaceObjectAtIndex: count withObject: status];
120 record = [records nextObject];
124 - (NSString *) _freeBusyAsTextFromStartDate: (NSCalendarDate *) startDate
125 toEndDate: (NSCalendarDate *) endDate
126 forFreeBusy: (SOGoFreeBusyObject *) fb
128 NSEnumerator *records;
129 NSMutableArray *freeBusyItems;
130 NSTimeInterval interval;
131 int count, intervals;
133 interval = [endDate timeIntervalSinceDate: startDate] + 60;
134 intervals = interval / 900; /* slices of 15 minutes */
135 freeBusyItems = [NSMutableArray arrayWithCapacity: intervals];
136 for (count = 1; count < intervals; count++)
137 [freeBusyItems addObject: @"0"];
139 records = [[fb fetchFreeBusyInfosFrom: startDate to: endDate] objectEnumerator];
140 [self _fillFreeBusyItems: freeBusyItems withRecords: records
141 fromStartDate: startDate toEndDate: endDate];
143 return [freeBusyItems componentsJoinedByString: @","];
146 - (NSString *) _freeBusyAsText
148 SOGoFreeBusyObject *co;
149 NSCalendarDate *startDate, *endDate;
150 NSString *queryDay, *additionalDays;
154 co = [self clientObject];
155 user = [context activeUser];
156 uTZ = [user timeZone];
158 queryDay = [self queryParameterForKey: @"sday"];
159 if ([queryDay length])
160 startDate = [NSCalendarDate dateFromShortDateString: queryDay
161 andShortTimeString: @"0000"
165 startDate = [NSCalendarDate calendarDate];
166 [startDate setTimeZone: uTZ];
167 startDate = [startDate hour: 0 minute: 0];
170 queryDay = [self queryParameterForKey: @"eday"];
171 if ([queryDay length])
172 endDate = [NSCalendarDate dateFromShortDateString: queryDay
173 andShortTimeString: @"2359"
176 endDate = [startDate hour: 23 minute: 59];
178 additionalDays = [self queryParameterForKey: @"additional"];
179 if ([additionalDays length] > 0)
180 endDate = [endDate dateByAddingYears: 0 months: 0
181 days: [additionalDays intValue]
182 hours: 0 minutes: 0 seconds: 0];
184 return [self _freeBusyAsTextFromStartDate: startDate toEndDate: endDate
188 - (id <WOActionResults>) readFreeBusyAction
190 WOResponse *response;
192 response = [context response];
193 [response setStatus: 200];
194 // [response setHeader: @"text/plain; charset=iso-8859-1"
195 // forKey: @"Content-Type"];
196 [response appendContentString: [self _freeBusyAsText]];
201 - (id <WOActionResults>) logoffAction
203 WOResponse *response;
205 SOGoWebAuthenticator *auth;
207 NSCalendarDate *date;
209 container = [[self clientObject] container];
211 response = [context response];
212 [response setStatus: 302];
213 [response setHeader: [container baseURLInContext: context]
214 forKey: @"location"];
215 auth = [[self clientObject] authenticatorInContext: context];
216 cookie = [WOCookie cookieWithName: [auth cookieNameInContext: context]
218 [cookie setPath: @"/"];
219 date = [NSCalendarDate calendarDate];
220 [cookie setExpires: [date yesterday]];
221 [response addCookie: cookie];
223 [response setHeader: date forKey: @"Last-Modified"];
224 [response setHeader: @"no-store, no-cache, must-revalidate, max-age=0" forKey: @"Cache-Control"];
225 [response setHeader: @"post-check=0, pre-check=0" forKey: @"Cache-Control"];
226 [response setHeader: @"no-cache" forKey: @"Pragma"];