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";
68 defaultModule = @"Calendar";
71 defaultModule = @"Calendar";
72 [self logWithFormat: @"default module set to '%@'", defaultModule];
73 [defaultModule retain];
77 - (id <WOActionResults>) defaultAction
82 co = [self clientObject];
83 moduleURL = [NSURL URLWithString: defaultModule
84 relativeToURL: [co soURL]];
86 return [self redirectToLocation: [moduleURL absoluteString]];
89 - (void) _fillFreeBusyItems: (NSMutableArray *) items
90 withRecords: (NSEnumerator *) records
91 fromStartDate: (NSCalendarDate *) startDate
92 toEndDate: (NSCalendarDate *) endDate
95 int count, startInterval, endInterval, value;
97 NSCalendarDate *currentDate;
99 record = [records nextObject];
102 status = [record objectForKey: @"c_status"];
104 value = [[record objectForKey: @"c_startdate"] intValue];
105 currentDate = [NSCalendarDate dateWithTimeIntervalSince1970: value];
106 if ([currentDate earlierDate: startDate] == currentDate)
110 = ([currentDate timeIntervalSinceDate: startDate] / 900);
112 value = [[record objectForKey: @"c_enddate"] intValue];
113 currentDate = [NSCalendarDate dateWithTimeIntervalSince1970: value];
114 if ([currentDate earlierDate: endDate] == endDate)
115 endInterval = [items count] - 1;
117 endInterval = ([currentDate timeIntervalSinceDate: startDate] / 900);
119 for (count = startInterval; count < endInterval; count++)
120 [items replaceObjectAtIndex: count withObject: status];
122 record = [records nextObject];
126 - (NSString *) _freeBusyAsTextFromStartDate: (NSCalendarDate *) startDate
127 toEndDate: (NSCalendarDate *) endDate
128 forFreeBusy: (SOGoFreeBusyObject *) fb
130 NSEnumerator *records;
131 NSMutableArray *freeBusyItems;
132 NSTimeInterval interval;
133 int count, intervals;
135 interval = [endDate timeIntervalSinceDate: startDate] + 60;
136 intervals = interval / 900; /* slices of 15 minutes */
137 freeBusyItems = [NSMutableArray arrayWithCapacity: intervals];
138 for (count = 1; count < intervals; count++)
139 [freeBusyItems addObject: @"0"];
141 records = [[fb fetchFreeBusyInfosFrom: startDate to: endDate] objectEnumerator];
142 [self _fillFreeBusyItems: freeBusyItems withRecords: records
143 fromStartDate: startDate toEndDate: endDate];
145 return [freeBusyItems componentsJoinedByString: @","];
148 - (NSString *) _freeBusyAsText
150 SOGoFreeBusyObject *co;
151 NSCalendarDate *startDate, *endDate;
152 NSString *queryDay, *additionalDays;
156 co = [self clientObject];
157 user = [context activeUser];
158 uTZ = [user timeZone];
160 queryDay = [self queryParameterForKey: @"sday"];
161 if ([queryDay length])
162 startDate = [NSCalendarDate dateFromShortDateString: queryDay
163 andShortTimeString: @"0000"
167 startDate = [NSCalendarDate calendarDate];
168 [startDate setTimeZone: uTZ];
169 startDate = [startDate hour: 0 minute: 0];
172 queryDay = [self queryParameterForKey: @"eday"];
173 if ([queryDay length])
174 endDate = [NSCalendarDate dateFromShortDateString: queryDay
175 andShortTimeString: @"2359"
178 endDate = [startDate hour: 23 minute: 59];
180 additionalDays = [self queryParameterForKey: @"additional"];
181 if ([additionalDays length] > 0)
182 endDate = [endDate dateByAddingYears: 0 months: 0
183 days: [additionalDays intValue]
184 hours: 0 minutes: 0 seconds: 0];
186 return [self _freeBusyAsTextFromStartDate: startDate toEndDate: endDate
190 - (id <WOActionResults>) readFreeBusyAction
192 WOResponse *response;
194 response = [context response];
195 [response setStatus: 200];
196 // [response setHeader: @"text/plain; charset=iso-8859-1"
197 // forKey: @"Content-Type"];
198 [response appendContentString: [self _freeBusyAsText]];
203 - (id <WOActionResults>) logoffAction
205 WOResponse *response;
206 NSEnumerator *cookies;
208 SOGoWebAuthenticator *auth;
211 container = [[self clientObject] container];
213 response = [context response];
214 [response setStatus: 302];
215 [response setHeader: [container baseURLInContext: context]
216 forKey: @"location"];
217 cookies = [[response cookies] objectEnumerator];
218 auth = [[self clientObject] authenticatorInContext: context];
219 cookie = [WOCookie cookieWithName: [auth cookieNameInContext: context]
221 [response addCookie: cookie];